To: vim_dev@googlegroups.com Subject: Patch 9.0.1313 Fcc: outbox From: Bram Moolenaar Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ------------ Patch 9.0.1313 Problem: Some settings use the current codepage instead of 'encoding'. Solution: Adjust how options are initialized. (Ken Takata, closes #11992) Files: src/misc1.c, src/proto/misc1.pro, src/option.c, src/optionstr.c, src/os_win32.c, src/proto/os_win32.pro *** ../vim-9.0.1312/src/misc1.c 2023-01-17 21:38:22.138223711 +0000 --- src/misc1.c 2023-02-15 19:07:38.198470349 +0000 *************** *** 1311,1316 **** --- 1311,1342 ---- } #endif + #if defined(MSWIN) || defined(PROTO) + /* + * Initilize $VIM and $VIMRUNTIME when 'enc' is updated. + */ + void + init_vimdir(void) + { + int mustfree; + char_u *p; + + mch_get_exe_name(); + + mustfree = FALSE; + didset_vim = FALSE; + p = vim_getenv((char_u *)"VIM", &mustfree); + if (mustfree) + vim_free(p); + + mustfree = FALSE; + didset_vimruntime = FALSE; + p = vim_getenv((char_u *)"VIMRUNTIME", &mustfree); + if (mustfree) + vim_free(p); + } + #endif + /* * Call expand_env() and store the result in an allocated string. * This is not very memory efficient, this expects the result to be freed *************** *** 1696,1702 **** * Vim's version of getenv(). * Special handling of $HOME, $VIM and $VIMRUNTIME. * Also does ACP to 'enc' conversion for Win32. ! * "mustfree" is set to TRUE when returned is allocated, it must be * initialized to FALSE by the caller. */ char_u * --- 1722,1728 ---- * Vim's version of getenv(). * Special handling of $HOME, $VIM and $VIMRUNTIME. * Also does ACP to 'enc' conversion for Win32. ! * "mustfree" is set to TRUE when the returned string is allocated. It must be * initialized to FALSE by the caller. */ char_u * *** ../vim-9.0.1312/src/proto/misc1.pro 2022-06-27 23:15:15.000000000 +0100 --- src/proto/misc1.pro 2023-02-15 19:04:54.798114687 +0000 *************** *** 26,31 **** --- 26,32 ---- void init_homedir(void); void free_homedir(void); void free_users(void); + void init_vimdir(void); char_u *expand_env_save(char_u *src); char_u *expand_env_save_opt(char_u *src, int one); void expand_env(char_u *src, char_u *dst, int dstlen); *** ../vim-9.0.1312/src/option.c 2023-02-15 14:26:21.999922800 +0000 --- src/option.c 2023-02-15 19:01:29.993722174 +0000 *************** *** 600,605 **** --- 600,607 ---- init_spell_chartab(); #endif + set_init_default_encoding(); + // Expand environment variables and things like "~" for the defaults. set_init_expand_env(); *************** *** 618,624 **** didset_options2(); set_init_lang_env(); - set_init_default_encoding(); #ifdef FEAT_MULTI_LANG // Set the default for 'helplang'. --- 620,625 ---- *** ../vim-9.0.1312/src/optionstr.c 2023-02-13 16:10:00.375551633 +0000 --- src/optionstr.c 2023-02-15 19:01:29.993722174 +0000 *************** *** 1054,1062 **** } #if defined(MSWIN) ! // $HOME may have characters in active code page. if (varp == &p_enc) init_homedir(); #endif } --- 1054,1065 ---- } #if defined(MSWIN) ! // $HOME, $VIM and $VIMRUNTIME may have characters in active code page. if (varp == &p_enc) + { init_homedir(); + init_vimdir(); + } #endif } *** ../vim-9.0.1312/src/os_win32.c 2023-01-28 10:28:05.206125799 +0000 --- src/os_win32.c 2023-02-15 19:11:01.316818554 +0000 *************** *** 254,260 **** static int suppress_winsize = 1; // don't fiddle with console #endif ! static char_u *exe_path = NULL; static BOOL win8_or_later = FALSE; static BOOL win10_22H2_or_later = FALSE; --- 254,260 ---- static int suppress_winsize = 1; // don't fiddle with console #endif ! static WCHAR *exe_pathw = NULL; static BOOL win8_or_later = FALSE; static BOOL win10_22H2_or_later = FALSE; *************** *** 462,488 **** # endif #endif // !FEAT_GUI_MSWIN || VIMDLL ! static void ! get_exe_name(void) { // Maximum length of $PATH is more than MAXPATHL. 8191 is often mentioned // as the maximum length that works (plus a NUL byte). #define MAX_ENV_PATH_LEN 8192 char temp[MAX_ENV_PATH_LEN]; char_u *p; ! if (exe_name == NULL) { // store the name of the executable, may be used for $VIM ! GetModuleFileName(NULL, temp, MAX_ENV_PATH_LEN - 1); ! if (*temp != NUL) ! exe_name = FullName_save((char_u *)temp, FALSE); } ! if (exe_path != NULL || exe_name == NULL) return; ! exe_path = vim_strnsave(exe_name, gettail_sep(exe_name) - exe_name); if (exe_path == NULL) return; --- 462,506 ---- # endif #endif // !FEAT_GUI_MSWIN || VIMDLL ! void ! mch_get_exe_name(void) { // Maximum length of $PATH is more than MAXPATHL. 8191 is often mentioned // as the maximum length that works (plus a NUL byte). #define MAX_ENV_PATH_LEN 8192 char temp[MAX_ENV_PATH_LEN]; char_u *p; + WCHAR buf[MAX_PATH]; + int updated = FALSE; + static int enc_prev = -1; ! if (exe_name == NULL || exe_pathw == NULL || enc_prev != enc_codepage) { // store the name of the executable, may be used for $VIM ! GetModuleFileNameW(NULL, buf, MAX_PATH); ! if (*buf != NUL) ! { ! if (enc_codepage == -1) ! enc_codepage = GetACP(); ! if (exe_name != NULL) ! vim_free(exe_name); ! exe_name = utf16_to_enc(buf, NULL); ! enc_prev = enc_codepage; ! ! WCHAR *wp = wcsrchr(buf, '\\'); ! if (wp != NULL) ! *wp = NUL; ! if (exe_pathw != NULL) ! vim_free(exe_pathw); ! exe_pathw = _wcsdup(buf); ! updated = TRUE; ! } } ! if (exe_pathw == NULL || !updated) return; ! char_u *exe_path = utf16_to_enc(exe_pathw, NULL); if (exe_path == NULL) return; *************** *** 503,508 **** --- 521,527 ---- STRCAT(temp, exe_path); vim_setenv((char_u *)"PATH", (char_u *)temp); } + vim_free(exe_path); } /* *************** *** 538,547 **** // NOTE: Do not use mch_dirname() and mch_chdir() here, they may call // vimLoadLib() recursively, which causes a stack overflow. ! if (exe_path == NULL) ! get_exe_name(); ! if (exe_path == NULL) return NULL; WCHAR old_dirw[MAXPATHL]; --- 557,566 ---- // NOTE: Do not use mch_dirname() and mch_chdir() here, they may call // vimLoadLib() recursively, which causes a stack overflow. ! if (exe_pathw == NULL) ! mch_get_exe_name(); ! if (exe_pathw == NULL) return NULL; WCHAR old_dirw[MAXPATHL]; *************** *** 552,558 **** // Change directory to where the executable is, both to make // sure we find a .dll there and to avoid looking for a .dll // in the current directory. ! SetCurrentDirectory((LPCSTR)exe_path); dll = LoadLibrary(name); SetCurrentDirectoryW(old_dirw); return dll; --- 571,577 ---- // Change directory to where the executable is, both to make // sure we find a .dll there and to avoid looking for a .dll // in the current directory. ! SetCurrentDirectoryW(exe_pathw); dll = LoadLibrary(name); SetCurrentDirectoryW(old_dirw); return dll; *************** *** 3586,3592 **** int argc UNUSED, char **argv UNUSED) { ! get_exe_name(); #if defined(FEAT_GUI_MSWIN) && !defined(VIMDLL) return OK; // GUI always has a tty --- 3605,3611 ---- int argc UNUSED, char **argv UNUSED) { ! mch_get_exe_name(); #if defined(FEAT_GUI_MSWIN) && !defined(VIMDLL) return OK; // GUI always has a tty *** ../vim-9.0.1312/src/proto/os_win32.pro 2023-01-23 12:33:15.822715784 +0000 --- src/proto/os_win32.pro 2023-02-15 19:01:29.997722181 +0000 *************** *** 1,4 **** --- 1,5 ---- /* os_win32.c */ + void mch_get_exe_name(void); HINSTANCE vimLoadLib(const char *name); int mch_is_gui_executable(void); HINSTANCE find_imported_module_by_funcname(HINSTANCE hInst, const char *funcname); *** ../vim-9.0.1312/src/version.c 2023-02-15 16:45:22.977959441 +0000 --- src/version.c 2023-02-15 19:04:26.026055508 +0000 *************** *** 697,698 **** --- 697,700 ---- { /* Add new patch number below this line */ + /**/ + 1313, /**/ -- The difference between theory and practice, is that in theory, there is no difference between theory and practice. /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net \\\ /// \\\ \\\ sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ /// \\\ help me help AIDS victims -- http://ICCF-Holland.org ///