Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4: |
| 2 | * |
| 3 | * VIM - Vi IMproved by Bram Moolenaar |
| 4 | * |
| 5 | * Do ":help uganda" in Vim to read copying and usage conditions. |
| 6 | * Do ":help credits" in Vim to see a list of people who contributed. |
| 7 | * See README.txt for an overview of the Vim source code. |
| 8 | */ |
| 9 | /* |
| 10 | * os_win32.c |
| 11 | * |
| 12 | * Used for both the console version and the Win32 GUI. A lot of code is for |
| 13 | * the console version only, so there is a lot of "#ifndef FEAT_GUI_W32". |
| 14 | * |
| 15 | * Win32 (Windows NT and Windows 95) system-dependent routines. |
| 16 | * Portions lifted from the Win32 SDK samples, the MSDOS-dependent code, |
| 17 | * NetHack 3.1.3, GNU Emacs 19.30, and Vile 5.5. |
| 18 | * |
| 19 | * George V. Reilly <george@reilly.org> wrote most of this. |
| 20 | * Roger Knobbe <rogerk@wonderware.com> did the initial port of Vim 3.0. |
| 21 | */ |
| 22 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 23 | #include "vim.h" |
| 24 | |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 25 | #ifdef FEAT_MZSCHEME |
| 26 | # include "if_mzsch.h" |
| 27 | #endif |
| 28 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 29 | #include <sys/types.h> |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 30 | #include <signal.h> |
| 31 | #include <limits.h> |
| 32 | #include <process.h> |
| 33 | |
| 34 | #undef chdir |
| 35 | #ifdef __GNUC__ |
| 36 | # ifndef __MINGW32__ |
| 37 | # include <dirent.h> |
| 38 | # endif |
| 39 | #else |
| 40 | # include <direct.h> |
| 41 | #endif |
| 42 | |
| 43 | #if defined(FEAT_TITLE) && !defined(FEAT_GUI_W32) |
| 44 | # include <shellapi.h> |
| 45 | #endif |
| 46 | |
| 47 | #ifdef __MINGW32__ |
| 48 | # ifndef FROM_LEFT_1ST_BUTTON_PRESSED |
| 49 | # define FROM_LEFT_1ST_BUTTON_PRESSED 0x0001 |
| 50 | # endif |
| 51 | # ifndef RIGHTMOST_BUTTON_PRESSED |
| 52 | # define RIGHTMOST_BUTTON_PRESSED 0x0002 |
| 53 | # endif |
| 54 | # ifndef FROM_LEFT_2ND_BUTTON_PRESSED |
| 55 | # define FROM_LEFT_2ND_BUTTON_PRESSED 0x0004 |
| 56 | # endif |
| 57 | # ifndef FROM_LEFT_3RD_BUTTON_PRESSED |
| 58 | # define FROM_LEFT_3RD_BUTTON_PRESSED 0x0008 |
| 59 | # endif |
| 60 | # ifndef FROM_LEFT_4TH_BUTTON_PRESSED |
| 61 | # define FROM_LEFT_4TH_BUTTON_PRESSED 0x0010 |
| 62 | # endif |
| 63 | |
| 64 | /* |
| 65 | * EventFlags |
| 66 | */ |
| 67 | # ifndef MOUSE_MOVED |
| 68 | # define MOUSE_MOVED 0x0001 |
| 69 | # endif |
| 70 | # ifndef DOUBLE_CLICK |
| 71 | # define DOUBLE_CLICK 0x0002 |
| 72 | # endif |
| 73 | #endif |
| 74 | |
| 75 | /* Record all output and all keyboard & mouse input */ |
| 76 | /* #define MCH_WRITE_DUMP */ |
| 77 | |
| 78 | #ifdef MCH_WRITE_DUMP |
| 79 | FILE* fdDump = NULL; |
| 80 | #endif |
| 81 | |
| 82 | /* |
| 83 | * When generating prototypes for Win32 on Unix, these lines make the syntax |
| 84 | * errors disappear. They do not need to be correct. |
| 85 | */ |
| 86 | #ifdef PROTO |
| 87 | #define WINAPI |
| 88 | #define WINBASEAPI |
| 89 | typedef char * LPCSTR; |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 90 | typedef char * LPWSTR; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 91 | typedef int ACCESS_MASK; |
| 92 | typedef int BOOL; |
| 93 | typedef int COLORREF; |
| 94 | typedef int CONSOLE_CURSOR_INFO; |
| 95 | typedef int COORD; |
| 96 | typedef int DWORD; |
| 97 | typedef int HANDLE; |
| 98 | typedef int HDC; |
| 99 | typedef int HFONT; |
| 100 | typedef int HICON; |
| 101 | typedef int HINSTANCE; |
| 102 | typedef int HWND; |
| 103 | typedef int INPUT_RECORD; |
| 104 | typedef int KEY_EVENT_RECORD; |
| 105 | typedef int LOGFONT; |
| 106 | typedef int LPBOOL; |
| 107 | typedef int LPCTSTR; |
| 108 | typedef int LPDWORD; |
| 109 | typedef int LPSTR; |
| 110 | typedef int LPTSTR; |
| 111 | typedef int LPVOID; |
| 112 | typedef int MOUSE_EVENT_RECORD; |
| 113 | typedef int PACL; |
| 114 | typedef int PDWORD; |
| 115 | typedef int PHANDLE; |
| 116 | typedef int PRINTDLG; |
| 117 | typedef int PSECURITY_DESCRIPTOR; |
| 118 | typedef int PSID; |
| 119 | typedef int SECURITY_INFORMATION; |
| 120 | typedef int SHORT; |
| 121 | typedef int SMALL_RECT; |
| 122 | typedef int TEXTMETRIC; |
| 123 | typedef int TOKEN_INFORMATION_CLASS; |
| 124 | typedef int TRUSTEE; |
| 125 | typedef int WORD; |
| 126 | typedef int WCHAR; |
| 127 | typedef void VOID; |
| 128 | #endif |
| 129 | |
| 130 | #ifndef FEAT_GUI_W32 |
| 131 | /* Undocumented API in kernel32.dll needed to work around dead key bug in |
| 132 | * console-mode applications in NT 4.0. If you switch keyboard layouts |
| 133 | * in a console app to a layout that includes dead keys and then hit a |
| 134 | * dead key, a call to ToAscii will trash the stack. My thanks to Ian James |
| 135 | * and Michael Dietrich for helping me figure out this workaround. |
| 136 | */ |
| 137 | |
| 138 | /* WINBASEAPI BOOL WINAPI GetConsoleKeyboardLayoutNameA(LPSTR); */ |
| 139 | #ifndef WINBASEAPI |
| 140 | # define WINBASEAPI __stdcall |
| 141 | #endif |
| 142 | #if defined(__BORLANDC__) |
| 143 | typedef BOOL (__stdcall *PFNGCKLN)(LPSTR); |
| 144 | #else |
| 145 | typedef WINBASEAPI BOOL (WINAPI *PFNGCKLN)(LPSTR); |
| 146 | #endif |
Bram Moolenaar | d6f676d | 2005-06-01 21:51:55 +0000 | [diff] [blame] | 147 | static PFNGCKLN s_pfnGetConsoleKeyboardLayoutName = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 148 | #endif |
| 149 | |
| 150 | #if defined(__BORLANDC__) |
| 151 | /* Strangely Borland uses a non-standard name. */ |
| 152 | # define wcsicmp(a, b) wcscmpi((a), (b)) |
| 153 | #endif |
| 154 | |
| 155 | #ifndef FEAT_GUI_W32 |
| 156 | /* Win32 Console handles for input and output */ |
| 157 | static HANDLE g_hConIn = INVALID_HANDLE_VALUE; |
| 158 | static HANDLE g_hConOut = INVALID_HANDLE_VALUE; |
| 159 | |
| 160 | /* Win32 Screen buffer,coordinate,console I/O information */ |
| 161 | static SMALL_RECT g_srScrollRegion; |
| 162 | static COORD g_coord; /* 0-based, but external coords are 1-based */ |
| 163 | |
| 164 | /* The attribute of the screen when the editor was started */ |
| 165 | static WORD g_attrDefault = 7; /* lightgray text on black background */ |
| 166 | static WORD g_attrCurrent; |
| 167 | |
| 168 | static int g_fCBrkPressed = FALSE; /* set by ctrl-break interrupt */ |
| 169 | static int g_fCtrlCPressed = FALSE; /* set when ctrl-C or ctrl-break detected */ |
| 170 | static int g_fForceExit = FALSE; /* set when forcefully exiting */ |
| 171 | |
| 172 | static void termcap_mode_start(void); |
| 173 | static void termcap_mode_end(void); |
| 174 | static void clear_chars(COORD coord, DWORD n); |
| 175 | static void clear_screen(void); |
| 176 | static void clear_to_end_of_display(void); |
| 177 | static void clear_to_end_of_line(void); |
| 178 | static void scroll(unsigned cLines); |
| 179 | static void set_scroll_region(unsigned left, unsigned top, |
| 180 | unsigned right, unsigned bottom); |
| 181 | static void insert_lines(unsigned cLines); |
| 182 | static void delete_lines(unsigned cLines); |
| 183 | static void gotoxy(unsigned x, unsigned y); |
| 184 | static void normvideo(void); |
| 185 | static void textattr(WORD wAttr); |
| 186 | static void textcolor(WORD wAttr); |
| 187 | static void textbackground(WORD wAttr); |
| 188 | static void standout(void); |
| 189 | static void standend(void); |
| 190 | static void visual_bell(void); |
| 191 | static void cursor_visible(BOOL fVisible); |
| 192 | static BOOL write_chars(LPCSTR pchBuf, DWORD cchToWrite); |
| 193 | static char_u tgetch(int *pmodifiers, char_u *pch2); |
| 194 | static void create_conin(void); |
| 195 | static int s_cursor_visible = TRUE; |
| 196 | static int did_create_conin = FALSE; |
| 197 | #else |
| 198 | static int s_dont_use_vimrun = TRUE; |
| 199 | static int need_vimrun_warning = FALSE; |
| 200 | static char *vimrun_path = "vimrun "; |
| 201 | #endif |
| 202 | |
| 203 | #ifndef FEAT_GUI_W32 |
| 204 | static int suppress_winsize = 1; /* don't fiddle with console */ |
| 205 | #endif |
| 206 | |
Bram Moolenaar | ebbcb82 | 2010-10-23 14:02:54 +0200 | [diff] [blame] | 207 | static char_u *exe_path = NULL; |
| 208 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 209 | static void |
| 210 | get_exe_name(void) |
| 211 | { |
Bram Moolenaar | 27d9ece | 2010-11-10 15:37:05 +0100 | [diff] [blame] | 212 | /* Maximum length of $PATH is more than MAXPATHL. 8191 is often mentioned |
| 213 | * as the maximum length that works (plus a NUL byte). */ |
| 214 | #define MAX_ENV_PATH_LEN 8192 |
| 215 | char temp[MAX_ENV_PATH_LEN]; |
Bram Moolenaar | ebbcb82 | 2010-10-23 14:02:54 +0200 | [diff] [blame] | 216 | char_u *p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 217 | |
| 218 | if (exe_name == NULL) |
| 219 | { |
| 220 | /* store the name of the executable, may be used for $VIM */ |
Bram Moolenaar | 27d9ece | 2010-11-10 15:37:05 +0100 | [diff] [blame] | 221 | GetModuleFileName(NULL, temp, MAX_ENV_PATH_LEN - 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 222 | if (*temp != NUL) |
| 223 | exe_name = FullName_save((char_u *)temp, FALSE); |
| 224 | } |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 225 | |
Bram Moolenaar | ebbcb82 | 2010-10-23 14:02:54 +0200 | [diff] [blame] | 226 | if (exe_path == NULL && exe_name != NULL) |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 227 | { |
Bram Moolenaar | 6b5ef06 | 2010-10-27 12:18:00 +0200 | [diff] [blame] | 228 | exe_path = vim_strnsave(exe_name, |
| 229 | (int)(gettail_sep(exe_name) - exe_name)); |
Bram Moolenaar | ebbcb82 | 2010-10-23 14:02:54 +0200 | [diff] [blame] | 230 | if (exe_path != NULL) |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 231 | { |
Bram Moolenaar | ebbcb82 | 2010-10-23 14:02:54 +0200 | [diff] [blame] | 232 | /* Append our starting directory to $PATH, so that when doing |
| 233 | * "!xxd" it's found in our starting directory. Needed because |
| 234 | * SearchPath() also looks there. */ |
| 235 | p = mch_getenv("PATH"); |
Bram Moolenaar | 27d9ece | 2010-11-10 15:37:05 +0100 | [diff] [blame] | 236 | if (p == NULL |
| 237 | || STRLEN(p) + STRLEN(exe_path) + 2 < MAX_ENV_PATH_LEN) |
Bram Moolenaar | ebbcb82 | 2010-10-23 14:02:54 +0200 | [diff] [blame] | 238 | { |
Bram Moolenaar | 27d9ece | 2010-11-10 15:37:05 +0100 | [diff] [blame] | 239 | if (p == NULL || *p == NUL) |
| 240 | temp[0] = NUL; |
| 241 | else |
| 242 | { |
| 243 | STRCPY(temp, p); |
| 244 | STRCAT(temp, ";"); |
| 245 | } |
Bram Moolenaar | ebbcb82 | 2010-10-23 14:02:54 +0200 | [diff] [blame] | 246 | STRCAT(temp, exe_path); |
| 247 | vim_setenv((char_u *)"PATH", temp); |
| 248 | } |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 249 | } |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 250 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Bram Moolenaar | ebbcb82 | 2010-10-23 14:02:54 +0200 | [diff] [blame] | 253 | /* |
| 254 | * Load library "name". |
| 255 | */ |
| 256 | HINSTANCE |
| 257 | vimLoadLib(char *name) |
| 258 | { |
| 259 | HINSTANCE dll = NULL; |
| 260 | char old_dir[MAXPATHL]; |
| 261 | |
| 262 | if (exe_path == NULL) |
| 263 | get_exe_name(); |
| 264 | if (exe_path != NULL && mch_dirname(old_dir, MAXPATHL) == OK) |
| 265 | { |
| 266 | /* Change directory to where the executable is, both to make sure we |
| 267 | * find a .dll there and to avoid looking for a .dll in the current |
| 268 | * directory. */ |
| 269 | mch_chdir(exe_path); |
| 270 | dll = LoadLibrary(name); |
| 271 | mch_chdir(old_dir); |
| 272 | } |
| 273 | return dll; |
| 274 | } |
| 275 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 276 | #if defined(DYNAMIC_GETTEXT) || defined(PROTO) |
| 277 | # ifndef GETTEXT_DLL |
| 278 | # define GETTEXT_DLL "libintl.dll" |
| 279 | # endif |
| 280 | /* Dummy funcitons */ |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 281 | static char *null_libintl_gettext(const char *); |
| 282 | static char *null_libintl_textdomain(const char *); |
| 283 | static char *null_libintl_bindtextdomain(const char *, const char *); |
| 284 | static char *null_libintl_bind_textdomain_codeset(const char *, const char *); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 285 | |
Bram Moolenaar | ebbcb82 | 2010-10-23 14:02:54 +0200 | [diff] [blame] | 286 | static HINSTANCE hLibintlDLL = NULL; |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 287 | char *(*dyn_libintl_gettext)(const char *) = null_libintl_gettext; |
| 288 | char *(*dyn_libintl_textdomain)(const char *) = null_libintl_textdomain; |
| 289 | char *(*dyn_libintl_bindtextdomain)(const char *, const char *) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 290 | = null_libintl_bindtextdomain; |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 291 | char *(*dyn_libintl_bind_textdomain_codeset)(const char *, const char *) |
| 292 | = null_libintl_bind_textdomain_codeset; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 293 | |
| 294 | int |
| 295 | dyn_libintl_init(char *libname) |
| 296 | { |
| 297 | int i; |
| 298 | static struct |
| 299 | { |
| 300 | char *name; |
| 301 | FARPROC *ptr; |
| 302 | } libintl_entry[] = |
| 303 | { |
| 304 | {"gettext", (FARPROC*)&dyn_libintl_gettext}, |
| 305 | {"textdomain", (FARPROC*)&dyn_libintl_textdomain}, |
| 306 | {"bindtextdomain", (FARPROC*)&dyn_libintl_bindtextdomain}, |
| 307 | {NULL, NULL} |
| 308 | }; |
| 309 | |
| 310 | /* No need to initialize twice. */ |
| 311 | if (hLibintlDLL) |
| 312 | return 1; |
| 313 | /* Load gettext library (libintl.dll) */ |
Bram Moolenaar | ebbcb82 | 2010-10-23 14:02:54 +0200 | [diff] [blame] | 314 | hLibintlDLL = vimLoadLib(libname != NULL ? libname : GETTEXT_DLL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 315 | if (!hLibintlDLL) |
| 316 | { |
Bram Moolenaar | ebbcb82 | 2010-10-23 14:02:54 +0200 | [diff] [blame] | 317 | if (p_verbose > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 318 | { |
Bram Moolenaar | ebbcb82 | 2010-10-23 14:02:54 +0200 | [diff] [blame] | 319 | verbose_enter(); |
| 320 | EMSG2(_(e_loadlib), GETTEXT_DLL); |
| 321 | verbose_leave(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 322 | } |
Bram Moolenaar | ebbcb82 | 2010-10-23 14:02:54 +0200 | [diff] [blame] | 323 | return 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 324 | } |
| 325 | for (i = 0; libintl_entry[i].name != NULL |
| 326 | && libintl_entry[i].ptr != NULL; ++i) |
| 327 | { |
| 328 | if ((*libintl_entry[i].ptr = (FARPROC)GetProcAddress(hLibintlDLL, |
| 329 | libintl_entry[i].name)) == NULL) |
| 330 | { |
| 331 | dyn_libintl_end(); |
| 332 | if (p_verbose > 0) |
Bram Moolenaar | a04f10b | 2005-05-31 22:09:46 +0000 | [diff] [blame] | 333 | { |
| 334 | verbose_enter(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 335 | EMSG2(_(e_loadfunc), libintl_entry[i].name); |
Bram Moolenaar | a04f10b | 2005-05-31 22:09:46 +0000 | [diff] [blame] | 336 | verbose_leave(); |
| 337 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 338 | return 0; |
| 339 | } |
| 340 | } |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 341 | |
| 342 | /* The bind_textdomain_codeset() function is optional. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 343 | dyn_libintl_bind_textdomain_codeset = (void *)GetProcAddress(hLibintlDLL, |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 344 | "bind_textdomain_codeset"); |
| 345 | if (dyn_libintl_bind_textdomain_codeset == NULL) |
| 346 | dyn_libintl_bind_textdomain_codeset = |
| 347 | null_libintl_bind_textdomain_codeset; |
| 348 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 349 | return 1; |
| 350 | } |
| 351 | |
| 352 | void |
| 353 | dyn_libintl_end() |
| 354 | { |
| 355 | if (hLibintlDLL) |
| 356 | FreeLibrary(hLibintlDLL); |
| 357 | hLibintlDLL = NULL; |
| 358 | dyn_libintl_gettext = null_libintl_gettext; |
| 359 | dyn_libintl_textdomain = null_libintl_textdomain; |
| 360 | dyn_libintl_bindtextdomain = null_libintl_bindtextdomain; |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 361 | dyn_libintl_bind_textdomain_codeset = null_libintl_bind_textdomain_codeset; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 362 | } |
| 363 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 364 | /*ARGSUSED*/ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 365 | static char * |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 366 | null_libintl_gettext(const char *msgid) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 367 | { |
| 368 | return (char*)msgid; |
| 369 | } |
| 370 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 371 | /*ARGSUSED*/ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 372 | static char * |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 373 | null_libintl_bindtextdomain(const char *domainname, const char *dirname) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 374 | { |
| 375 | return NULL; |
| 376 | } |
| 377 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 378 | /*ARGSUSED*/ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 379 | static char * |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 380 | null_libintl_bind_textdomain_codeset(const char *domainname, |
| 381 | const char *codeset) |
| 382 | { |
| 383 | return NULL; |
| 384 | } |
| 385 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 386 | /*ARGSUSED*/ |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 387 | static char * |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 388 | null_libintl_textdomain(const char *domainname) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 389 | { |
| 390 | return NULL; |
| 391 | } |
| 392 | |
| 393 | #endif /* DYNAMIC_GETTEXT */ |
| 394 | |
| 395 | /* This symbol is not defined in older versions of the SDK or Visual C++ */ |
| 396 | |
| 397 | #ifndef VER_PLATFORM_WIN32_WINDOWS |
| 398 | # define VER_PLATFORM_WIN32_WINDOWS 1 |
| 399 | #endif |
| 400 | |
| 401 | DWORD g_PlatformId; |
| 402 | |
| 403 | #ifdef HAVE_ACL |
| 404 | # include <aclapi.h> |
| 405 | /* |
| 406 | * These are needed to dynamically load the ADVAPI DLL, which is not |
| 407 | * implemented under Windows 95 (and causes VIM to crash) |
| 408 | */ |
| 409 | typedef DWORD (WINAPI *PSNSECINFO) (LPTSTR, enum SE_OBJECT_TYPE, |
| 410 | SECURITY_INFORMATION, PSID, PSID, PACL, PACL); |
| 411 | typedef DWORD (WINAPI *PGNSECINFO) (LPSTR, enum SE_OBJECT_TYPE, |
| 412 | SECURITY_INFORMATION, PSID *, PSID *, PACL *, PACL *, |
| 413 | PSECURITY_DESCRIPTOR *); |
| 414 | |
| 415 | static HANDLE advapi_lib = NULL; /* Handle for ADVAPI library */ |
| 416 | static PSNSECINFO pSetNamedSecurityInfo; |
| 417 | static PGNSECINFO pGetNamedSecurityInfo; |
| 418 | #endif |
| 419 | |
| 420 | /* |
| 421 | * Set g_PlatformId to VER_PLATFORM_WIN32_NT (NT) or |
| 422 | * VER_PLATFORM_WIN32_WINDOWS (Win95). |
| 423 | */ |
| 424 | void |
| 425 | PlatformId(void) |
| 426 | { |
| 427 | static int done = FALSE; |
| 428 | |
| 429 | if (!done) |
| 430 | { |
| 431 | OSVERSIONINFO ovi; |
| 432 | |
| 433 | ovi.dwOSVersionInfoSize = sizeof(ovi); |
| 434 | GetVersionEx(&ovi); |
| 435 | |
| 436 | g_PlatformId = ovi.dwPlatformId; |
| 437 | |
| 438 | #ifdef HAVE_ACL |
| 439 | /* |
| 440 | * Load the ADVAPI runtime if we are on anything |
| 441 | * other than Windows 95 |
| 442 | */ |
| 443 | if (g_PlatformId == VER_PLATFORM_WIN32_NT) |
| 444 | { |
| 445 | /* |
| 446 | * do this load. Problems: Doesn't unload at end of run (this is |
| 447 | * theoretically okay, since Windows should unload it when VIM |
| 448 | * terminates). Should we be using the 'mch_libcall' routines? |
| 449 | * Seems like a lot of overhead to load/unload ADVAPI32.DLL each |
| 450 | * time we verify security... |
| 451 | */ |
Bram Moolenaar | ebbcb82 | 2010-10-23 14:02:54 +0200 | [diff] [blame] | 452 | advapi_lib = vimLoadLib("ADVAPI32.DLL"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 453 | if (advapi_lib != NULL) |
| 454 | { |
| 455 | pSetNamedSecurityInfo = (PSNSECINFO)GetProcAddress(advapi_lib, |
| 456 | "SetNamedSecurityInfoA"); |
| 457 | pGetNamedSecurityInfo = (PGNSECINFO)GetProcAddress(advapi_lib, |
| 458 | "GetNamedSecurityInfoA"); |
| 459 | if (pSetNamedSecurityInfo == NULL |
| 460 | || pGetNamedSecurityInfo == NULL) |
| 461 | { |
| 462 | /* If we can't get the function addresses, set advapi_lib |
| 463 | * to NULL so that we don't use them. */ |
| 464 | FreeLibrary(advapi_lib); |
| 465 | advapi_lib = NULL; |
| 466 | } |
| 467 | } |
| 468 | } |
| 469 | #endif |
| 470 | done = TRUE; |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | /* |
| 475 | * Return TRUE when running on Windows 95 (or 98 or ME). |
| 476 | * Only to be used after mch_init(). |
| 477 | */ |
| 478 | int |
| 479 | mch_windows95(void) |
| 480 | { |
| 481 | return g_PlatformId == VER_PLATFORM_WIN32_WINDOWS; |
| 482 | } |
| 483 | |
| 484 | #ifdef FEAT_GUI_W32 |
| 485 | /* |
| 486 | * Used to work around the "can't do synchronous spawn" |
| 487 | * problem on Win32s, without resorting to Universal Thunk. |
| 488 | */ |
| 489 | static int old_num_windows; |
| 490 | static int num_windows; |
| 491 | |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 492 | /*ARGSUSED*/ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 493 | static BOOL CALLBACK |
| 494 | win32ssynch_cb(HWND hwnd, LPARAM lparam) |
| 495 | { |
| 496 | num_windows++; |
| 497 | return TRUE; |
| 498 | } |
| 499 | #endif |
| 500 | |
| 501 | #ifndef FEAT_GUI_W32 |
| 502 | |
| 503 | #define SHIFT (SHIFT_PRESSED) |
| 504 | #define CTRL (RIGHT_CTRL_PRESSED | LEFT_CTRL_PRESSED) |
| 505 | #define ALT (RIGHT_ALT_PRESSED | LEFT_ALT_PRESSED) |
| 506 | #define ALT_GR (RIGHT_ALT_PRESSED | LEFT_CTRL_PRESSED) |
| 507 | |
| 508 | |
| 509 | /* When uChar.AsciiChar is 0, then we need to look at wVirtualKeyCode. |
| 510 | * We map function keys to their ANSI terminal equivalents, as produced |
| 511 | * by ANSI.SYS, for compatibility with the MS-DOS version of Vim. Any |
| 512 | * ANSI key with a value >= '\300' is nonstandard, but provided anyway |
| 513 | * so that the user can have access to all SHIFT-, CTRL-, and ALT- |
| 514 | * combinations of function/arrow/etc keys. |
| 515 | */ |
| 516 | |
Bram Moolenaar | d6f676d | 2005-06-01 21:51:55 +0000 | [diff] [blame] | 517 | static const struct |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 518 | { |
| 519 | WORD wVirtKey; |
| 520 | BOOL fAnsiKey; |
| 521 | int chAlone; |
| 522 | int chShift; |
| 523 | int chCtrl; |
| 524 | int chAlt; |
| 525 | } VirtKeyMap[] = |
| 526 | { |
| 527 | |
| 528 | /* Key ANSI alone shift ctrl alt */ |
| 529 | { VK_ESCAPE,FALSE, ESC, ESC, ESC, ESC, }, |
| 530 | |
| 531 | { VK_F1, TRUE, ';', 'T', '^', 'h', }, |
| 532 | { VK_F2, TRUE, '<', 'U', '_', 'i', }, |
| 533 | { VK_F3, TRUE, '=', 'V', '`', 'j', }, |
| 534 | { VK_F4, TRUE, '>', 'W', 'a', 'k', }, |
| 535 | { VK_F5, TRUE, '?', 'X', 'b', 'l', }, |
| 536 | { VK_F6, TRUE, '@', 'Y', 'c', 'm', }, |
| 537 | { VK_F7, TRUE, 'A', 'Z', 'd', 'n', }, |
| 538 | { VK_F8, TRUE, 'B', '[', 'e', 'o', }, |
| 539 | { VK_F9, TRUE, 'C', '\\', 'f', 'p', }, |
| 540 | { VK_F10, TRUE, 'D', ']', 'g', 'q', }, |
| 541 | { VK_F11, TRUE, '\205', '\207', '\211', '\213', }, |
| 542 | { VK_F12, TRUE, '\206', '\210', '\212', '\214', }, |
| 543 | |
| 544 | { VK_HOME, TRUE, 'G', '\302', 'w', '\303', }, |
| 545 | { VK_UP, TRUE, 'H', '\304', '\305', '\306', }, |
| 546 | { VK_PRIOR, TRUE, 'I', '\307', '\204', '\310', }, /*PgUp*/ |
| 547 | { VK_LEFT, TRUE, 'K', '\311', 's', '\312', }, |
| 548 | { VK_RIGHT, TRUE, 'M', '\313', 't', '\314', }, |
| 549 | { VK_END, TRUE, 'O', '\315', 'u', '\316', }, |
| 550 | { VK_DOWN, TRUE, 'P', '\317', '\320', '\321', }, |
| 551 | { VK_NEXT, TRUE, 'Q', '\322', 'v', '\323', }, /*PgDn*/ |
| 552 | { VK_INSERT,TRUE, 'R', '\324', '\325', '\326', }, |
| 553 | { VK_DELETE,TRUE, 'S', '\327', '\330', '\331', }, |
| 554 | |
| 555 | { VK_SNAPSHOT,TRUE, 0, 0, 0, 'r', }, /*PrtScrn*/ |
| 556 | |
| 557 | #if 0 |
| 558 | /* Most people don't have F13-F20, but what the hell... */ |
| 559 | { VK_F13, TRUE, '\332', '\333', '\334', '\335', }, |
| 560 | { VK_F14, TRUE, '\336', '\337', '\340', '\341', }, |
| 561 | { VK_F15, TRUE, '\342', '\343', '\344', '\345', }, |
| 562 | { VK_F16, TRUE, '\346', '\347', '\350', '\351', }, |
| 563 | { VK_F17, TRUE, '\352', '\353', '\354', '\355', }, |
| 564 | { VK_F18, TRUE, '\356', '\357', '\360', '\361', }, |
| 565 | { VK_F19, TRUE, '\362', '\363', '\364', '\365', }, |
| 566 | { VK_F20, TRUE, '\366', '\367', '\370', '\371', }, |
| 567 | #endif |
| 568 | { VK_ADD, TRUE, 'N', 'N', 'N', 'N', }, /* keyp '+' */ |
| 569 | { VK_SUBTRACT, TRUE,'J', 'J', 'J', 'J', }, /* keyp '-' */ |
| 570 | /* { VK_DIVIDE, TRUE,'N', 'N', 'N', 'N', }, keyp '/' */ |
| 571 | { VK_MULTIPLY, TRUE,'7', '7', '7', '7', }, /* keyp '*' */ |
| 572 | |
| 573 | { VK_NUMPAD0,TRUE, '\332', '\333', '\334', '\335', }, |
| 574 | { VK_NUMPAD1,TRUE, '\336', '\337', '\340', '\341', }, |
| 575 | { VK_NUMPAD2,TRUE, '\342', '\343', '\344', '\345', }, |
| 576 | { VK_NUMPAD3,TRUE, '\346', '\347', '\350', '\351', }, |
| 577 | { VK_NUMPAD4,TRUE, '\352', '\353', '\354', '\355', }, |
| 578 | { VK_NUMPAD5,TRUE, '\356', '\357', '\360', '\361', }, |
| 579 | { VK_NUMPAD6,TRUE, '\362', '\363', '\364', '\365', }, |
| 580 | { VK_NUMPAD7,TRUE, '\366', '\367', '\370', '\371', }, |
| 581 | { VK_NUMPAD8,TRUE, '\372', '\373', '\374', '\375', }, |
| 582 | /* Sorry, out of number space! <negri>*/ |
| 583 | { VK_NUMPAD9,TRUE, '\376', '\377', '\377', '\367', }, |
| 584 | |
| 585 | }; |
| 586 | |
| 587 | |
| 588 | #ifdef _MSC_VER |
| 589 | // The ToAscii bug destroys several registers. Need to turn off optimization |
| 590 | // or the GetConsoleKeyboardLayoutName hack will fail in non-debug versions |
Bram Moolenaar | 7b5f832 | 2006-03-23 22:47:08 +0000 | [diff] [blame] | 591 | # pragma warning(push) |
| 592 | # pragma warning(disable: 4748) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 593 | # pragma optimize("", off) |
| 594 | #endif |
| 595 | |
| 596 | #if defined(__GNUC__) && !defined(__MINGW32__) && !defined(__CYGWIN__) |
| 597 | # define AChar AsciiChar |
| 598 | #else |
| 599 | # define AChar uChar.AsciiChar |
| 600 | #endif |
| 601 | |
| 602 | /* The return code indicates key code size. */ |
| 603 | static int |
| 604 | #ifdef __BORLANDC__ |
| 605 | __stdcall |
| 606 | #endif |
| 607 | win32_kbd_patch_key( |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 608 | KEY_EVENT_RECORD *pker) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 609 | { |
| 610 | UINT uMods = pker->dwControlKeyState; |
| 611 | static int s_iIsDead = 0; |
| 612 | static WORD awAnsiCode[2]; |
| 613 | static BYTE abKeystate[256]; |
| 614 | |
| 615 | |
| 616 | if (s_iIsDead == 2) |
| 617 | { |
| 618 | pker->AChar = (CHAR) awAnsiCode[1]; |
| 619 | s_iIsDead = 0; |
| 620 | return 1; |
| 621 | } |
| 622 | |
| 623 | if (pker->AChar != 0) |
| 624 | return 1; |
| 625 | |
Bram Moolenaar | 7db5fc8 | 2010-05-24 11:59:29 +0200 | [diff] [blame] | 626 | vim_memset(abKeystate, 0, sizeof (abKeystate)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 627 | |
| 628 | // Should only be non-NULL on NT 4.0 |
| 629 | if (s_pfnGetConsoleKeyboardLayoutName != NULL) |
| 630 | { |
| 631 | CHAR szKLID[KL_NAMELENGTH]; |
| 632 | |
| 633 | if ((*s_pfnGetConsoleKeyboardLayoutName)(szKLID)) |
| 634 | (void)LoadKeyboardLayout(szKLID, KLF_ACTIVATE); |
| 635 | } |
| 636 | |
| 637 | /* Clear any pending dead keys */ |
| 638 | ToAscii(VK_SPACE, MapVirtualKey(VK_SPACE, 0), abKeystate, awAnsiCode, 0); |
| 639 | |
| 640 | if (uMods & SHIFT_PRESSED) |
| 641 | abKeystate[VK_SHIFT] = 0x80; |
| 642 | if (uMods & CAPSLOCK_ON) |
| 643 | abKeystate[VK_CAPITAL] = 1; |
| 644 | |
| 645 | if ((uMods & ALT_GR) == ALT_GR) |
| 646 | { |
| 647 | abKeystate[VK_CONTROL] = abKeystate[VK_LCONTROL] = |
| 648 | abKeystate[VK_MENU] = abKeystate[VK_RMENU] = 0x80; |
| 649 | } |
| 650 | |
| 651 | s_iIsDead = ToAscii(pker->wVirtualKeyCode, pker->wVirtualScanCode, |
| 652 | abKeystate, awAnsiCode, 0); |
| 653 | |
| 654 | if (s_iIsDead > 0) |
| 655 | pker->AChar = (CHAR) awAnsiCode[0]; |
| 656 | |
| 657 | return s_iIsDead; |
| 658 | } |
| 659 | |
| 660 | #ifdef _MSC_VER |
| 661 | /* MUST switch optimization on again here, otherwise a call to |
| 662 | * decode_key_event() may crash (e.g. when hitting caps-lock) */ |
| 663 | # pragma optimize("", on) |
Bram Moolenaar | 7b5f832 | 2006-03-23 22:47:08 +0000 | [diff] [blame] | 664 | # pragma warning(pop) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 665 | |
| 666 | # if (_MSC_VER < 1100) |
| 667 | /* MUST turn off global optimisation for this next function, or |
| 668 | * pressing ctrl-minus in insert mode crashes Vim when built with |
| 669 | * VC4.1. -- negri. */ |
| 670 | # pragma optimize("g", off) |
| 671 | # endif |
| 672 | #endif |
| 673 | |
| 674 | static BOOL g_fJustGotFocus = FALSE; |
| 675 | |
| 676 | /* |
| 677 | * Decode a KEY_EVENT into one or two keystrokes |
| 678 | */ |
| 679 | static BOOL |
| 680 | decode_key_event( |
| 681 | KEY_EVENT_RECORD *pker, |
| 682 | char_u *pch, |
| 683 | char_u *pch2, |
| 684 | int *pmodifiers, |
| 685 | BOOL fDoPost) |
| 686 | { |
| 687 | int i; |
| 688 | const int nModifs = pker->dwControlKeyState & (SHIFT | ALT | CTRL); |
| 689 | |
| 690 | *pch = *pch2 = NUL; |
| 691 | g_fJustGotFocus = FALSE; |
| 692 | |
| 693 | /* ignore key up events */ |
| 694 | if (!pker->bKeyDown) |
| 695 | return FALSE; |
| 696 | |
| 697 | /* ignore some keystrokes */ |
| 698 | switch (pker->wVirtualKeyCode) |
| 699 | { |
| 700 | /* modifiers */ |
| 701 | case VK_SHIFT: |
| 702 | case VK_CONTROL: |
| 703 | case VK_MENU: /* Alt key */ |
| 704 | return FALSE; |
| 705 | |
| 706 | default: |
| 707 | break; |
| 708 | } |
| 709 | |
| 710 | /* special cases */ |
| 711 | if ((nModifs & CTRL) != 0 && (nModifs & ~CTRL) == 0 && pker->AChar == NUL) |
| 712 | { |
| 713 | /* Ctrl-6 is Ctrl-^ */ |
| 714 | if (pker->wVirtualKeyCode == '6') |
| 715 | { |
| 716 | *pch = Ctrl_HAT; |
| 717 | return TRUE; |
| 718 | } |
| 719 | /* Ctrl-2 is Ctrl-@ */ |
| 720 | else if (pker->wVirtualKeyCode == '2') |
| 721 | { |
| 722 | *pch = NUL; |
| 723 | return TRUE; |
| 724 | } |
| 725 | /* Ctrl-- is Ctrl-_ */ |
| 726 | else if (pker->wVirtualKeyCode == 0xBD) |
| 727 | { |
| 728 | *pch = Ctrl__; |
| 729 | return TRUE; |
| 730 | } |
| 731 | } |
| 732 | |
| 733 | /* Shift-TAB */ |
| 734 | if (pker->wVirtualKeyCode == VK_TAB && (nModifs & SHIFT_PRESSED)) |
| 735 | { |
| 736 | *pch = K_NUL; |
| 737 | *pch2 = '\017'; |
| 738 | return TRUE; |
| 739 | } |
| 740 | |
| 741 | for (i = sizeof(VirtKeyMap) / sizeof(VirtKeyMap[0]); --i >= 0; ) |
| 742 | { |
| 743 | if (VirtKeyMap[i].wVirtKey == pker->wVirtualKeyCode) |
| 744 | { |
| 745 | if (nModifs == 0) |
| 746 | *pch = VirtKeyMap[i].chAlone; |
| 747 | else if ((nModifs & SHIFT) != 0 && (nModifs & ~SHIFT) == 0) |
| 748 | *pch = VirtKeyMap[i].chShift; |
| 749 | else if ((nModifs & CTRL) != 0 && (nModifs & ~CTRL) == 0) |
| 750 | *pch = VirtKeyMap[i].chCtrl; |
| 751 | else if ((nModifs & ALT) != 0 && (nModifs & ~ALT) == 0) |
| 752 | *pch = VirtKeyMap[i].chAlt; |
| 753 | |
| 754 | if (*pch != 0) |
| 755 | { |
| 756 | if (VirtKeyMap[i].fAnsiKey) |
| 757 | { |
| 758 | *pch2 = *pch; |
| 759 | *pch = K_NUL; |
| 760 | } |
| 761 | |
| 762 | return TRUE; |
| 763 | } |
| 764 | } |
| 765 | } |
| 766 | |
| 767 | i = win32_kbd_patch_key(pker); |
| 768 | |
| 769 | if (i < 0) |
| 770 | *pch = NUL; |
| 771 | else |
| 772 | { |
| 773 | *pch = (i > 0) ? pker->AChar : NUL; |
| 774 | |
| 775 | if (pmodifiers != NULL) |
| 776 | { |
| 777 | /* Pass on the ALT key as a modifier, but only when not combined |
| 778 | * with CTRL (which is ALTGR). */ |
| 779 | if ((nModifs & ALT) != 0 && (nModifs & CTRL) == 0) |
| 780 | *pmodifiers |= MOD_MASK_ALT; |
| 781 | |
| 782 | /* Pass on SHIFT only for special keys, because we don't know when |
| 783 | * it's already included with the character. */ |
| 784 | if ((nModifs & SHIFT) != 0 && *pch <= 0x20) |
| 785 | *pmodifiers |= MOD_MASK_SHIFT; |
| 786 | |
| 787 | /* Pass on CTRL only for non-special keys, because we don't know |
| 788 | * when it's already included with the character. And not when |
| 789 | * combined with ALT (which is ALTGR). */ |
| 790 | if ((nModifs & CTRL) != 0 && (nModifs & ALT) == 0 |
| 791 | && *pch >= 0x20 && *pch < 0x80) |
| 792 | *pmodifiers |= MOD_MASK_CTRL; |
| 793 | } |
| 794 | } |
| 795 | |
| 796 | return (*pch != NUL); |
| 797 | } |
| 798 | |
| 799 | #ifdef _MSC_VER |
| 800 | # pragma optimize("", on) |
| 801 | #endif |
| 802 | |
| 803 | #endif /* FEAT_GUI_W32 */ |
| 804 | |
| 805 | |
| 806 | #ifdef FEAT_MOUSE |
| 807 | |
| 808 | /* |
| 809 | * For the GUI the mouse handling is in gui_w32.c. |
| 810 | */ |
| 811 | # ifdef FEAT_GUI_W32 |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 812 | /*ARGSUSED*/ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 813 | void |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 814 | mch_setmouse(int on) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 815 | { |
| 816 | } |
| 817 | # else |
| 818 | static int g_fMouseAvail = FALSE; /* mouse present */ |
| 819 | static int g_fMouseActive = FALSE; /* mouse enabled */ |
| 820 | static int g_nMouseClick = -1; /* mouse status */ |
| 821 | static int g_xMouse; /* mouse x coordinate */ |
| 822 | static int g_yMouse; /* mouse y coordinate */ |
| 823 | |
| 824 | /* |
| 825 | * Enable or disable mouse input |
| 826 | */ |
| 827 | void |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 828 | mch_setmouse(int on) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 829 | { |
| 830 | DWORD cmodein; |
| 831 | |
| 832 | if (!g_fMouseAvail) |
| 833 | return; |
| 834 | |
| 835 | g_fMouseActive = on; |
| 836 | GetConsoleMode(g_hConIn, &cmodein); |
| 837 | |
| 838 | if (g_fMouseActive) |
| 839 | cmodein |= ENABLE_MOUSE_INPUT; |
| 840 | else |
| 841 | cmodein &= ~ENABLE_MOUSE_INPUT; |
| 842 | |
| 843 | SetConsoleMode(g_hConIn, cmodein); |
| 844 | } |
| 845 | |
| 846 | |
| 847 | /* |
| 848 | * Decode a MOUSE_EVENT. If it's a valid event, return MOUSE_LEFT, |
| 849 | * MOUSE_MIDDLE, or MOUSE_RIGHT for a click; MOUSE_DRAG for a mouse |
| 850 | * move with a button held down; and MOUSE_RELEASE after a MOUSE_DRAG |
| 851 | * or a MOUSE_LEFT, _MIDDLE, or _RIGHT. We encode the button type, |
| 852 | * the number of clicks, and the Shift/Ctrl/Alt modifiers in g_nMouseClick, |
| 853 | * and we return the mouse position in g_xMouse and g_yMouse. |
| 854 | * |
| 855 | * Every MOUSE_LEFT, _MIDDLE, or _RIGHT will be followed by zero or more |
| 856 | * MOUSE_DRAGs and one MOUSE_RELEASE. MOUSE_RELEASE will be followed only |
| 857 | * by MOUSE_LEFT, _MIDDLE, or _RIGHT. |
| 858 | * |
| 859 | * For multiple clicks, we send, say, MOUSE_LEFT/1 click, MOUSE_RELEASE, |
| 860 | * MOUSE_LEFT/2 clicks, MOUSE_RELEASE, MOUSE_LEFT/3 clicks, MOUSE_RELEASE, .... |
| 861 | * |
| 862 | * Windows will send us MOUSE_MOVED notifications whenever the mouse |
| 863 | * moves, even if it stays within the same character cell. We ignore |
| 864 | * all MOUSE_MOVED messages if the position hasn't really changed, and |
| 865 | * we ignore all MOUSE_MOVED messages where no button is held down (i.e., |
| 866 | * we're only interested in MOUSE_DRAG). |
| 867 | * |
| 868 | * All of this is complicated by the code that fakes MOUSE_MIDDLE on |
| 869 | * 2-button mouses by pressing the left & right buttons simultaneously. |
| 870 | * In practice, it's almost impossible to click both at the same time, |
| 871 | * so we need to delay a little. Also, we tend not to get MOUSE_RELEASE |
| 872 | * in such cases, if the user is clicking quickly. |
| 873 | */ |
| 874 | static BOOL |
| 875 | decode_mouse_event( |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 876 | MOUSE_EVENT_RECORD *pmer) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 877 | { |
| 878 | static int s_nOldButton = -1; |
| 879 | static int s_nOldMouseClick = -1; |
| 880 | static int s_xOldMouse = -1; |
| 881 | static int s_yOldMouse = -1; |
| 882 | static linenr_T s_old_topline = 0; |
| 883 | #ifdef FEAT_DIFF |
| 884 | static int s_old_topfill = 0; |
| 885 | #endif |
| 886 | static int s_cClicks = 1; |
| 887 | static BOOL s_fReleased = TRUE; |
| 888 | static DWORD s_dwLastClickTime = 0; |
| 889 | static BOOL s_fNextIsMiddle = FALSE; |
| 890 | |
| 891 | static DWORD cButtons = 0; /* number of buttons supported */ |
| 892 | |
| 893 | const DWORD LEFT = FROM_LEFT_1ST_BUTTON_PRESSED; |
| 894 | const DWORD MIDDLE = FROM_LEFT_2ND_BUTTON_PRESSED; |
| 895 | const DWORD RIGHT = RIGHTMOST_BUTTON_PRESSED; |
| 896 | const DWORD LEFT_RIGHT = LEFT | RIGHT; |
| 897 | |
| 898 | int nButton; |
| 899 | |
| 900 | if (cButtons == 0 && !GetNumberOfConsoleMouseButtons(&cButtons)) |
| 901 | cButtons = 2; |
| 902 | |
| 903 | if (!g_fMouseAvail || !g_fMouseActive) |
| 904 | { |
| 905 | g_nMouseClick = -1; |
| 906 | return FALSE; |
| 907 | } |
| 908 | |
| 909 | /* get a spurious MOUSE_EVENT immediately after receiving focus; ignore */ |
| 910 | if (g_fJustGotFocus) |
| 911 | { |
| 912 | g_fJustGotFocus = FALSE; |
| 913 | return FALSE; |
| 914 | } |
| 915 | |
| 916 | /* unprocessed mouse click? */ |
| 917 | if (g_nMouseClick != -1) |
| 918 | return TRUE; |
| 919 | |
| 920 | nButton = -1; |
| 921 | g_xMouse = pmer->dwMousePosition.X; |
| 922 | g_yMouse = pmer->dwMousePosition.Y; |
| 923 | |
| 924 | if (pmer->dwEventFlags == MOUSE_MOVED) |
| 925 | { |
| 926 | /* ignore MOUSE_MOVED events if (x, y) hasn't changed. (We get these |
| 927 | * events even when the mouse moves only within a char cell.) */ |
| 928 | if (s_xOldMouse == g_xMouse && s_yOldMouse == g_yMouse) |
| 929 | return FALSE; |
| 930 | } |
| 931 | |
| 932 | /* If no buttons are pressed... */ |
| 933 | if ((pmer->dwButtonState & ((1 << cButtons) - 1)) == 0) |
| 934 | { |
| 935 | /* If the last thing returned was MOUSE_RELEASE, ignore this */ |
| 936 | if (s_fReleased) |
| 937 | return FALSE; |
| 938 | |
| 939 | nButton = MOUSE_RELEASE; |
| 940 | s_fReleased = TRUE; |
| 941 | } |
| 942 | else /* one or more buttons pressed */ |
| 943 | { |
| 944 | /* on a 2-button mouse, hold down left and right buttons |
| 945 | * simultaneously to get MIDDLE. */ |
| 946 | |
| 947 | if (cButtons == 2 && s_nOldButton != MOUSE_DRAG) |
| 948 | { |
| 949 | DWORD dwLR = (pmer->dwButtonState & LEFT_RIGHT); |
| 950 | |
| 951 | /* if either left or right button only is pressed, see if the |
| 952 | * the next mouse event has both of them pressed */ |
| 953 | if (dwLR == LEFT || dwLR == RIGHT) |
| 954 | { |
| 955 | for (;;) |
| 956 | { |
| 957 | /* wait a short time for next input event */ |
| 958 | if (WaitForSingleObject(g_hConIn, p_mouset / 3) |
| 959 | != WAIT_OBJECT_0) |
| 960 | break; |
| 961 | else |
| 962 | { |
| 963 | DWORD cRecords = 0; |
| 964 | INPUT_RECORD ir; |
| 965 | MOUSE_EVENT_RECORD* pmer2 = &ir.Event.MouseEvent; |
| 966 | |
| 967 | PeekConsoleInput(g_hConIn, &ir, 1, &cRecords); |
| 968 | |
| 969 | if (cRecords == 0 || ir.EventType != MOUSE_EVENT |
| 970 | || !(pmer2->dwButtonState & LEFT_RIGHT)) |
| 971 | break; |
| 972 | else |
| 973 | { |
| 974 | if (pmer2->dwEventFlags != MOUSE_MOVED) |
| 975 | { |
| 976 | ReadConsoleInput(g_hConIn, &ir, 1, &cRecords); |
| 977 | |
| 978 | return decode_mouse_event(pmer2); |
| 979 | } |
| 980 | else if (s_xOldMouse == pmer2->dwMousePosition.X && |
| 981 | s_yOldMouse == pmer2->dwMousePosition.Y) |
| 982 | { |
| 983 | /* throw away spurious mouse move */ |
| 984 | ReadConsoleInput(g_hConIn, &ir, 1, &cRecords); |
| 985 | |
| 986 | /* are there any more mouse events in queue? */ |
| 987 | PeekConsoleInput(g_hConIn, &ir, 1, &cRecords); |
| 988 | |
| 989 | if (cRecords==0 || ir.EventType != MOUSE_EVENT) |
| 990 | break; |
| 991 | } |
| 992 | else |
| 993 | break; |
| 994 | } |
| 995 | } |
| 996 | } |
| 997 | } |
| 998 | } |
| 999 | |
| 1000 | if (s_fNextIsMiddle) |
| 1001 | { |
| 1002 | nButton = (pmer->dwEventFlags == MOUSE_MOVED) |
| 1003 | ? MOUSE_DRAG : MOUSE_MIDDLE; |
| 1004 | s_fNextIsMiddle = FALSE; |
| 1005 | } |
| 1006 | else if (cButtons == 2 && |
| 1007 | ((pmer->dwButtonState & LEFT_RIGHT) == LEFT_RIGHT)) |
| 1008 | { |
| 1009 | nButton = MOUSE_MIDDLE; |
| 1010 | |
| 1011 | if (! s_fReleased && pmer->dwEventFlags != MOUSE_MOVED) |
| 1012 | { |
| 1013 | s_fNextIsMiddle = TRUE; |
| 1014 | nButton = MOUSE_RELEASE; |
| 1015 | } |
| 1016 | } |
| 1017 | else if ((pmer->dwButtonState & LEFT) == LEFT) |
| 1018 | nButton = MOUSE_LEFT; |
| 1019 | else if ((pmer->dwButtonState & MIDDLE) == MIDDLE) |
| 1020 | nButton = MOUSE_MIDDLE; |
| 1021 | else if ((pmer->dwButtonState & RIGHT) == RIGHT) |
| 1022 | nButton = MOUSE_RIGHT; |
| 1023 | |
| 1024 | if (! s_fReleased && ! s_fNextIsMiddle |
| 1025 | && nButton != s_nOldButton && s_nOldButton != MOUSE_DRAG) |
| 1026 | return FALSE; |
| 1027 | |
| 1028 | s_fReleased = s_fNextIsMiddle; |
| 1029 | } |
| 1030 | |
| 1031 | if (pmer->dwEventFlags == 0 || pmer->dwEventFlags == DOUBLE_CLICK) |
| 1032 | { |
| 1033 | /* button pressed or released, without mouse moving */ |
| 1034 | if (nButton != -1 && nButton != MOUSE_RELEASE) |
| 1035 | { |
| 1036 | DWORD dwCurrentTime = GetTickCount(); |
| 1037 | |
| 1038 | if (s_xOldMouse != g_xMouse |
| 1039 | || s_yOldMouse != g_yMouse |
| 1040 | || s_nOldButton != nButton |
| 1041 | || s_old_topline != curwin->w_topline |
| 1042 | #ifdef FEAT_DIFF |
| 1043 | || s_old_topfill != curwin->w_topfill |
| 1044 | #endif |
| 1045 | || (int)(dwCurrentTime - s_dwLastClickTime) > p_mouset) |
| 1046 | { |
| 1047 | s_cClicks = 1; |
| 1048 | } |
| 1049 | else if (++s_cClicks > 4) |
| 1050 | { |
| 1051 | s_cClicks = 1; |
| 1052 | } |
| 1053 | |
| 1054 | s_dwLastClickTime = dwCurrentTime; |
| 1055 | } |
| 1056 | } |
| 1057 | else if (pmer->dwEventFlags == MOUSE_MOVED) |
| 1058 | { |
| 1059 | if (nButton != -1 && nButton != MOUSE_RELEASE) |
| 1060 | nButton = MOUSE_DRAG; |
| 1061 | |
| 1062 | s_cClicks = 1; |
| 1063 | } |
| 1064 | |
| 1065 | if (nButton == -1) |
| 1066 | return FALSE; |
| 1067 | |
| 1068 | if (nButton != MOUSE_RELEASE) |
| 1069 | s_nOldButton = nButton; |
| 1070 | |
| 1071 | g_nMouseClick = nButton; |
| 1072 | |
| 1073 | if (pmer->dwControlKeyState & SHIFT_PRESSED) |
| 1074 | g_nMouseClick |= MOUSE_SHIFT; |
| 1075 | if (pmer->dwControlKeyState & (RIGHT_CTRL_PRESSED | LEFT_CTRL_PRESSED)) |
| 1076 | g_nMouseClick |= MOUSE_CTRL; |
| 1077 | if (pmer->dwControlKeyState & (RIGHT_ALT_PRESSED | LEFT_ALT_PRESSED)) |
| 1078 | g_nMouseClick |= MOUSE_ALT; |
| 1079 | |
| 1080 | if (nButton != MOUSE_DRAG && nButton != MOUSE_RELEASE) |
| 1081 | SET_NUM_MOUSE_CLICKS(g_nMouseClick, s_cClicks); |
| 1082 | |
| 1083 | /* only pass on interesting (i.e., different) mouse events */ |
| 1084 | if (s_xOldMouse == g_xMouse |
| 1085 | && s_yOldMouse == g_yMouse |
| 1086 | && s_nOldMouseClick == g_nMouseClick) |
| 1087 | { |
| 1088 | g_nMouseClick = -1; |
| 1089 | return FALSE; |
| 1090 | } |
| 1091 | |
| 1092 | s_xOldMouse = g_xMouse; |
| 1093 | s_yOldMouse = g_yMouse; |
| 1094 | s_old_topline = curwin->w_topline; |
| 1095 | #ifdef FEAT_DIFF |
| 1096 | s_old_topfill = curwin->w_topfill; |
| 1097 | #endif |
| 1098 | s_nOldMouseClick = g_nMouseClick; |
| 1099 | |
| 1100 | return TRUE; |
| 1101 | } |
| 1102 | |
| 1103 | # endif /* FEAT_GUI_W32 */ |
| 1104 | #endif /* FEAT_MOUSE */ |
| 1105 | |
| 1106 | |
| 1107 | #ifdef MCH_CURSOR_SHAPE |
| 1108 | /* |
| 1109 | * Set the shape of the cursor. |
| 1110 | * 'thickness' can be from 1 (thin) to 99 (block) |
| 1111 | */ |
| 1112 | static void |
| 1113 | mch_set_cursor_shape(int thickness) |
| 1114 | { |
| 1115 | CONSOLE_CURSOR_INFO ConsoleCursorInfo; |
| 1116 | ConsoleCursorInfo.dwSize = thickness; |
| 1117 | ConsoleCursorInfo.bVisible = s_cursor_visible; |
| 1118 | |
| 1119 | SetConsoleCursorInfo(g_hConOut, &ConsoleCursorInfo); |
| 1120 | if (s_cursor_visible) |
| 1121 | SetConsoleCursorPosition(g_hConOut, g_coord); |
| 1122 | } |
| 1123 | |
| 1124 | void |
| 1125 | mch_update_cursor(void) |
| 1126 | { |
| 1127 | int idx; |
| 1128 | int thickness; |
| 1129 | |
| 1130 | /* |
| 1131 | * How the cursor is drawn depends on the current mode. |
| 1132 | */ |
| 1133 | idx = get_shape_idx(FALSE); |
| 1134 | |
| 1135 | if (shape_table[idx].shape == SHAPE_BLOCK) |
| 1136 | thickness = 99; /* 100 doesn't work on W95 */ |
| 1137 | else |
| 1138 | thickness = shape_table[idx].percentage; |
| 1139 | mch_set_cursor_shape(thickness); |
| 1140 | } |
| 1141 | #endif |
| 1142 | |
| 1143 | #ifndef FEAT_GUI_W32 /* this isn't used for the GUI */ |
| 1144 | /* |
| 1145 | * Handle FOCUS_EVENT. |
| 1146 | */ |
| 1147 | static void |
| 1148 | handle_focus_event(INPUT_RECORD ir) |
| 1149 | { |
| 1150 | g_fJustGotFocus = ir.Event.FocusEvent.bSetFocus; |
| 1151 | ui_focus_change((int)g_fJustGotFocus); |
| 1152 | } |
| 1153 | |
| 1154 | /* |
| 1155 | * Wait until console input from keyboard or mouse is available, |
| 1156 | * or the time is up. |
| 1157 | * Return TRUE if something is available FALSE if not. |
| 1158 | */ |
| 1159 | static int |
| 1160 | WaitForChar(long msec) |
| 1161 | { |
| 1162 | DWORD dwNow = 0, dwEndTime = 0; |
| 1163 | INPUT_RECORD ir; |
| 1164 | DWORD cRecords; |
| 1165 | char_u ch, ch2; |
| 1166 | |
| 1167 | if (msec > 0) |
| 1168 | /* Wait until the specified time has elapsed. */ |
| 1169 | dwEndTime = GetTickCount() + msec; |
| 1170 | else if (msec < 0) |
| 1171 | /* Wait forever. */ |
| 1172 | dwEndTime = INFINITE; |
| 1173 | |
| 1174 | /* We need to loop until the end of the time period, because |
| 1175 | * we might get multiple unusable mouse events in that time. |
| 1176 | */ |
| 1177 | for (;;) |
| 1178 | { |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 1179 | #ifdef FEAT_MZSCHEME |
| 1180 | mzvim_check_threads(); |
| 1181 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1182 | #ifdef FEAT_CLIENTSERVER |
| 1183 | serverProcessPendingMessages(); |
| 1184 | #endif |
| 1185 | if (0 |
| 1186 | #ifdef FEAT_MOUSE |
| 1187 | || g_nMouseClick != -1 |
| 1188 | #endif |
| 1189 | #ifdef FEAT_CLIENTSERVER |
| 1190 | || input_available() |
| 1191 | #endif |
| 1192 | ) |
| 1193 | return TRUE; |
| 1194 | |
| 1195 | if (msec > 0) |
| 1196 | { |
| 1197 | /* If the specified wait time has passed, return. */ |
| 1198 | dwNow = GetTickCount(); |
| 1199 | if (dwNow >= dwEndTime) |
| 1200 | break; |
| 1201 | } |
| 1202 | if (msec != 0) |
| 1203 | { |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 1204 | DWORD dwWaitTime = dwEndTime - dwNow; |
| 1205 | |
| 1206 | #ifdef FEAT_MZSCHEME |
| 1207 | if (mzthreads_allowed() && p_mzq > 0 |
| 1208 | && (msec < 0 || (long)dwWaitTime > p_mzq)) |
| 1209 | dwWaitTime = p_mzq; /* don't wait longer than 'mzquantum' */ |
| 1210 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1211 | #ifdef FEAT_CLIENTSERVER |
| 1212 | /* Wait for either an event on the console input or a message in |
| 1213 | * the client-server window. */ |
| 1214 | if (MsgWaitForMultipleObjects(1, &g_hConIn, FALSE, |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 1215 | dwWaitTime, QS_SENDMESSAGE) != WAIT_OBJECT_0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1216 | #else |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 1217 | if (WaitForSingleObject(g_hConIn, dwWaitTime) != WAIT_OBJECT_0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1218 | #endif |
| 1219 | continue; |
| 1220 | } |
| 1221 | |
| 1222 | cRecords = 0; |
| 1223 | PeekConsoleInput(g_hConIn, &ir, 1, &cRecords); |
| 1224 | |
| 1225 | #ifdef FEAT_MBYTE_IME |
| 1226 | if (State & CMDLINE && msg_row == Rows - 1) |
| 1227 | { |
| 1228 | CONSOLE_SCREEN_BUFFER_INFO csbi; |
| 1229 | |
| 1230 | if (GetConsoleScreenBufferInfo(g_hConOut, &csbi)) |
| 1231 | { |
| 1232 | if (csbi.dwCursorPosition.Y != msg_row) |
| 1233 | { |
| 1234 | /* The screen is now messed up, must redraw the |
| 1235 | * command line and later all the windows. */ |
| 1236 | redraw_all_later(CLEAR); |
| 1237 | cmdline_row -= (msg_row - csbi.dwCursorPosition.Y); |
| 1238 | redrawcmd(); |
| 1239 | } |
| 1240 | } |
| 1241 | } |
| 1242 | #endif |
| 1243 | |
| 1244 | if (cRecords > 0) |
| 1245 | { |
| 1246 | if (ir.EventType == KEY_EVENT && ir.Event.KeyEvent.bKeyDown) |
| 1247 | { |
| 1248 | #ifdef FEAT_MBYTE_IME |
| 1249 | /* Windows IME sends two '\n's with only one 'ENTER'. First: |
| 1250 | * wVirtualKeyCode == 13. second: wVirtualKeyCode == 0 */ |
| 1251 | if (ir.Event.KeyEvent.uChar.UnicodeChar == 0 |
| 1252 | && ir.Event.KeyEvent.wVirtualKeyCode == 13) |
| 1253 | { |
| 1254 | ReadConsoleInput(g_hConIn, &ir, 1, &cRecords); |
| 1255 | continue; |
| 1256 | } |
| 1257 | #endif |
| 1258 | if (decode_key_event(&ir.Event.KeyEvent, &ch, &ch2, |
| 1259 | NULL, FALSE)) |
| 1260 | return TRUE; |
| 1261 | } |
| 1262 | |
| 1263 | ReadConsoleInput(g_hConIn, &ir, 1, &cRecords); |
| 1264 | |
| 1265 | if (ir.EventType == FOCUS_EVENT) |
| 1266 | handle_focus_event(ir); |
| 1267 | else if (ir.EventType == WINDOW_BUFFER_SIZE_EVENT) |
| 1268 | shell_resized(); |
| 1269 | #ifdef FEAT_MOUSE |
| 1270 | else if (ir.EventType == MOUSE_EVENT |
| 1271 | && decode_mouse_event(&ir.Event.MouseEvent)) |
| 1272 | return TRUE; |
| 1273 | #endif |
| 1274 | } |
| 1275 | else if (msec == 0) |
| 1276 | break; |
| 1277 | } |
| 1278 | |
| 1279 | #ifdef FEAT_CLIENTSERVER |
| 1280 | /* Something might have been received while we were waiting. */ |
| 1281 | if (input_available()) |
| 1282 | return TRUE; |
| 1283 | #endif |
| 1284 | return FALSE; |
| 1285 | } |
| 1286 | |
| 1287 | #ifndef FEAT_GUI_MSWIN |
| 1288 | /* |
| 1289 | * return non-zero if a character is available |
| 1290 | */ |
| 1291 | int |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 1292 | mch_char_avail(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1293 | { |
| 1294 | return WaitForChar(0L); |
| 1295 | } |
| 1296 | #endif |
| 1297 | |
| 1298 | /* |
| 1299 | * Create the console input. Used when reading stdin doesn't work. |
| 1300 | */ |
| 1301 | static void |
| 1302 | create_conin(void) |
| 1303 | { |
| 1304 | g_hConIn = CreateFile("CONIN$", GENERIC_READ|GENERIC_WRITE, |
| 1305 | FILE_SHARE_READ|FILE_SHARE_WRITE, |
| 1306 | (LPSECURITY_ATTRIBUTES) NULL, |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 1307 | OPEN_EXISTING, 0, (HANDLE)NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1308 | did_create_conin = TRUE; |
| 1309 | } |
| 1310 | |
| 1311 | /* |
| 1312 | * Get a keystroke or a mouse event |
| 1313 | */ |
| 1314 | static char_u |
| 1315 | tgetch(int *pmodifiers, char_u *pch2) |
| 1316 | { |
| 1317 | char_u ch; |
| 1318 | |
| 1319 | for (;;) |
| 1320 | { |
| 1321 | INPUT_RECORD ir; |
| 1322 | DWORD cRecords = 0; |
| 1323 | |
| 1324 | #ifdef FEAT_CLIENTSERVER |
| 1325 | (void)WaitForChar(-1L); |
| 1326 | if (input_available()) |
| 1327 | return 0; |
| 1328 | # ifdef FEAT_MOUSE |
| 1329 | if (g_nMouseClick != -1) |
| 1330 | return 0; |
| 1331 | # endif |
| 1332 | #endif |
| 1333 | if (ReadConsoleInput(g_hConIn, &ir, 1, &cRecords) == 0) |
| 1334 | { |
| 1335 | if (did_create_conin) |
| 1336 | read_error_exit(); |
| 1337 | create_conin(); |
| 1338 | continue; |
| 1339 | } |
| 1340 | |
| 1341 | if (ir.EventType == KEY_EVENT) |
| 1342 | { |
| 1343 | if (decode_key_event(&ir.Event.KeyEvent, &ch, pch2, |
| 1344 | pmodifiers, TRUE)) |
| 1345 | return ch; |
| 1346 | } |
| 1347 | else if (ir.EventType == FOCUS_EVENT) |
| 1348 | handle_focus_event(ir); |
| 1349 | else if (ir.EventType == WINDOW_BUFFER_SIZE_EVENT) |
| 1350 | shell_resized(); |
| 1351 | #ifdef FEAT_MOUSE |
| 1352 | else if (ir.EventType == MOUSE_EVENT) |
| 1353 | { |
| 1354 | if (decode_mouse_event(&ir.Event.MouseEvent)) |
| 1355 | return 0; |
| 1356 | } |
| 1357 | #endif |
| 1358 | } |
| 1359 | } |
| 1360 | #endif /* !FEAT_GUI_W32 */ |
| 1361 | |
| 1362 | |
| 1363 | /* |
| 1364 | * mch_inchar(): low-level input funcion. |
| 1365 | * Get one or more characters from the keyboard or the mouse. |
| 1366 | * If time == 0, do not wait for characters. |
| 1367 | * If time == n, wait a short time for characters. |
| 1368 | * If time == -1, wait forever for characters. |
| 1369 | * Returns the number of characters read into buf. |
| 1370 | */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 1371 | /*ARGSUSED*/ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1372 | int |
| 1373 | mch_inchar( |
| 1374 | char_u *buf, |
| 1375 | int maxlen, |
| 1376 | long time, |
| 1377 | int tb_change_cnt) |
| 1378 | { |
| 1379 | #ifndef FEAT_GUI_W32 /* this isn't used for the GUI */ |
| 1380 | |
| 1381 | int len; |
| 1382 | int c; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1383 | #define TYPEAHEADLEN 20 |
| 1384 | static char_u typeahead[TYPEAHEADLEN]; /* previously typed bytes. */ |
| 1385 | static int typeaheadlen = 0; |
| 1386 | |
| 1387 | /* First use any typeahead that was kept because "buf" was too small. */ |
| 1388 | if (typeaheadlen > 0) |
| 1389 | goto theend; |
| 1390 | |
| 1391 | #ifdef FEAT_SNIFF |
| 1392 | if (want_sniff_request) |
| 1393 | { |
| 1394 | if (sniff_request_waiting) |
| 1395 | { |
| 1396 | /* return K_SNIFF */ |
| 1397 | typeahead[typeaheadlen++] = CSI; |
| 1398 | typeahead[typeaheadlen++] = (char_u)KS_EXTRA; |
| 1399 | typeahead[typeaheadlen++] = (char_u)KE_SNIFF; |
| 1400 | sniff_request_waiting = 0; |
| 1401 | want_sniff_request = 0; |
| 1402 | goto theend; |
| 1403 | } |
| 1404 | else if (time < 0 || time > 250) |
| 1405 | { |
| 1406 | /* don't wait too long, a request might be pending */ |
| 1407 | time = 250; |
| 1408 | } |
| 1409 | } |
| 1410 | #endif |
| 1411 | |
| 1412 | if (time >= 0) |
| 1413 | { |
| 1414 | if (!WaitForChar(time)) /* no character available */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1415 | return 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1416 | } |
| 1417 | else /* time == -1, wait forever */ |
| 1418 | { |
| 1419 | mch_set_winsize_now(); /* Allow winsize changes from now on */ |
| 1420 | |
Bram Moolenaar | 3918c95 | 2005-03-15 22:34:55 +0000 | [diff] [blame] | 1421 | /* |
| 1422 | * If there is no character available within 2 seconds (default) |
| 1423 | * write the autoscript file to disk. Or cause the CursorHold event |
| 1424 | * to be triggered. |
| 1425 | */ |
| 1426 | if (!WaitForChar(p_ut)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1427 | { |
| 1428 | #ifdef FEAT_AUTOCMD |
Bram Moolenaar | d35f971 | 2005-12-18 22:02:33 +0000 | [diff] [blame] | 1429 | if (trigger_cursorhold() && maxlen >= 3) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1430 | { |
Bram Moolenaar | 3918c95 | 2005-03-15 22:34:55 +0000 | [diff] [blame] | 1431 | buf[0] = K_SPECIAL; |
| 1432 | buf[1] = KS_EXTRA; |
| 1433 | buf[2] = (int)KE_CURSORHOLD; |
| 1434 | return 3; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1435 | } |
| 1436 | #endif |
Bram Moolenaar | 702517d | 2005-06-27 22:34:07 +0000 | [diff] [blame] | 1437 | before_blocking(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1438 | } |
| 1439 | } |
| 1440 | |
| 1441 | /* |
| 1442 | * Try to read as many characters as there are, until the buffer is full. |
| 1443 | */ |
| 1444 | |
| 1445 | /* we will get at least one key. Get more if they are available. */ |
| 1446 | g_fCBrkPressed = FALSE; |
| 1447 | |
| 1448 | #ifdef MCH_WRITE_DUMP |
| 1449 | if (fdDump) |
| 1450 | fputc('[', fdDump); |
| 1451 | #endif |
| 1452 | |
| 1453 | /* Keep looping until there is something in the typeahead buffer and more |
| 1454 | * to get and still room in the buffer (up to two bytes for a char and |
| 1455 | * three bytes for a modifier). */ |
| 1456 | while ((typeaheadlen == 0 || WaitForChar(0L)) |
| 1457 | && typeaheadlen + 5 <= TYPEAHEADLEN) |
| 1458 | { |
| 1459 | if (typebuf_changed(tb_change_cnt)) |
| 1460 | { |
| 1461 | /* "buf" may be invalid now if a client put something in the |
| 1462 | * typeahead buffer and "buf" is in the typeahead buffer. */ |
| 1463 | typeaheadlen = 0; |
| 1464 | break; |
| 1465 | } |
| 1466 | #ifdef FEAT_MOUSE |
| 1467 | if (g_nMouseClick != -1) |
| 1468 | { |
| 1469 | # ifdef MCH_WRITE_DUMP |
| 1470 | if (fdDump) |
| 1471 | fprintf(fdDump, "{%02x @ %d, %d}", |
| 1472 | g_nMouseClick, g_xMouse, g_yMouse); |
| 1473 | # endif |
| 1474 | typeahead[typeaheadlen++] = ESC + 128; |
| 1475 | typeahead[typeaheadlen++] = 'M'; |
| 1476 | typeahead[typeaheadlen++] = g_nMouseClick; |
| 1477 | typeahead[typeaheadlen++] = g_xMouse + '!'; |
| 1478 | typeahead[typeaheadlen++] = g_yMouse + '!'; |
| 1479 | g_nMouseClick = -1; |
| 1480 | } |
| 1481 | else |
| 1482 | #endif |
| 1483 | { |
| 1484 | char_u ch2 = NUL; |
| 1485 | int modifiers = 0; |
| 1486 | |
| 1487 | c = tgetch(&modifiers, &ch2); |
| 1488 | |
| 1489 | if (typebuf_changed(tb_change_cnt)) |
| 1490 | { |
| 1491 | /* "buf" may be invalid now if a client put something in the |
| 1492 | * typeahead buffer and "buf" is in the typeahead buffer. */ |
| 1493 | typeaheadlen = 0; |
| 1494 | break; |
| 1495 | } |
| 1496 | |
| 1497 | if (c == Ctrl_C && ctrl_c_interrupts) |
| 1498 | { |
| 1499 | #if defined(FEAT_CLIENTSERVER) |
| 1500 | trash_input_buf(); |
| 1501 | #endif |
| 1502 | got_int = TRUE; |
| 1503 | } |
| 1504 | |
| 1505 | #ifdef FEAT_MOUSE |
| 1506 | if (g_nMouseClick == -1) |
| 1507 | #endif |
| 1508 | { |
| 1509 | int n = 1; |
| 1510 | |
| 1511 | /* A key may have one or two bytes. */ |
| 1512 | typeahead[typeaheadlen] = c; |
| 1513 | if (ch2 != NUL) |
| 1514 | { |
| 1515 | typeahead[typeaheadlen + 1] = ch2; |
| 1516 | ++n; |
| 1517 | } |
| 1518 | #ifdef FEAT_MBYTE |
| 1519 | /* Only convert normal characters, not special keys. Need to |
| 1520 | * convert before applying ALT, otherwise mapping <M-x> breaks |
| 1521 | * when 'tenc' is set. */ |
| 1522 | if (input_conv.vc_type != CONV_NONE |
| 1523 | && (ch2 == NUL || c != K_NUL)) |
| 1524 | n = convert_input(typeahead + typeaheadlen, n, |
| 1525 | TYPEAHEADLEN - typeaheadlen); |
| 1526 | #endif |
| 1527 | |
| 1528 | /* Use the ALT key to set the 8th bit of the character |
| 1529 | * when it's one byte, the 8th bit isn't set yet and not |
| 1530 | * using a double-byte encoding (would become a lead |
| 1531 | * byte). */ |
| 1532 | if ((modifiers & MOD_MASK_ALT) |
| 1533 | && n == 1 |
| 1534 | && (typeahead[typeaheadlen] & 0x80) == 0 |
| 1535 | #ifdef FEAT_MBYTE |
| 1536 | && !enc_dbcs |
| 1537 | #endif |
| 1538 | ) |
| 1539 | { |
Bram Moolenaar | 85a3e5c | 2007-11-20 16:22:16 +0000 | [diff] [blame] | 1540 | #ifdef FEAT_MBYTE |
| 1541 | n = (*mb_char2bytes)(typeahead[typeaheadlen] | 0x80, |
| 1542 | typeahead + typeaheadlen); |
| 1543 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1544 | typeahead[typeaheadlen] |= 0x80; |
Bram Moolenaar | 85a3e5c | 2007-11-20 16:22:16 +0000 | [diff] [blame] | 1545 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1546 | modifiers &= ~MOD_MASK_ALT; |
| 1547 | } |
| 1548 | |
| 1549 | if (modifiers != 0) |
| 1550 | { |
| 1551 | /* Prepend modifiers to the character. */ |
| 1552 | mch_memmove(typeahead + typeaheadlen + 3, |
| 1553 | typeahead + typeaheadlen, n); |
| 1554 | typeahead[typeaheadlen++] = K_SPECIAL; |
| 1555 | typeahead[typeaheadlen++] = (char_u)KS_MODIFIER; |
| 1556 | typeahead[typeaheadlen++] = modifiers; |
| 1557 | } |
| 1558 | |
| 1559 | typeaheadlen += n; |
| 1560 | |
| 1561 | #ifdef MCH_WRITE_DUMP |
| 1562 | if (fdDump) |
| 1563 | fputc(c, fdDump); |
| 1564 | #endif |
| 1565 | } |
| 1566 | } |
| 1567 | } |
| 1568 | |
| 1569 | #ifdef MCH_WRITE_DUMP |
| 1570 | if (fdDump) |
| 1571 | { |
| 1572 | fputs("]\n", fdDump); |
| 1573 | fflush(fdDump); |
| 1574 | } |
| 1575 | #endif |
| 1576 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1577 | theend: |
| 1578 | /* Move typeahead to "buf", as much as fits. */ |
| 1579 | len = 0; |
| 1580 | while (len < maxlen && typeaheadlen > 0) |
| 1581 | { |
| 1582 | buf[len++] = typeahead[0]; |
| 1583 | mch_memmove(typeahead, typeahead + 1, --typeaheadlen); |
| 1584 | } |
| 1585 | return len; |
| 1586 | |
| 1587 | #else /* FEAT_GUI_W32 */ |
| 1588 | return 0; |
| 1589 | #endif /* FEAT_GUI_W32 */ |
| 1590 | } |
| 1591 | |
| 1592 | #ifndef __MINGW32__ |
| 1593 | # include <shellapi.h> /* required for FindExecutable() */ |
| 1594 | #endif |
| 1595 | |
Bram Moolenaar | 69a7cb4 | 2004-06-20 12:51:53 +0000 | [diff] [blame] | 1596 | /* |
| 1597 | * Return TRUE if "name" is in $PATH. |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 1598 | * TODO: Should somehow check if it's really executable. |
Bram Moolenaar | 69a7cb4 | 2004-06-20 12:51:53 +0000 | [diff] [blame] | 1599 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1600 | static int |
| 1601 | executable_exists(char *name) |
| 1602 | { |
Bram Moolenaar | 69a7cb4 | 2004-06-20 12:51:53 +0000 | [diff] [blame] | 1603 | char *dum; |
| 1604 | char fname[_MAX_PATH]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1605 | |
Bram Moolenaar | 69a7cb4 | 2004-06-20 12:51:53 +0000 | [diff] [blame] | 1606 | #ifdef FEAT_MBYTE |
| 1607 | if (enc_codepage >= 0 && (int)GetACP() != enc_codepage) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1608 | { |
Bram Moolenaar | 36f692d | 2008-11-20 16:10:17 +0000 | [diff] [blame] | 1609 | WCHAR *p = enc_to_utf16(name, NULL); |
Bram Moolenaar | 69a7cb4 | 2004-06-20 12:51:53 +0000 | [diff] [blame] | 1610 | WCHAR fnamew[_MAX_PATH]; |
| 1611 | WCHAR *dumw; |
| 1612 | long n; |
| 1613 | |
| 1614 | if (p != NULL) |
| 1615 | { |
| 1616 | n = (long)SearchPathW(NULL, p, NULL, _MAX_PATH, fnamew, &dumw); |
| 1617 | vim_free(p); |
| 1618 | if (n > 0 || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED) |
| 1619 | { |
| 1620 | if (n == 0) |
| 1621 | return FALSE; |
| 1622 | if (GetFileAttributesW(fnamew) & FILE_ATTRIBUTE_DIRECTORY) |
| 1623 | return FALSE; |
| 1624 | return TRUE; |
| 1625 | } |
| 1626 | /* Retry with non-wide function (for Windows 98). */ |
| 1627 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1628 | } |
Bram Moolenaar | 69a7cb4 | 2004-06-20 12:51:53 +0000 | [diff] [blame] | 1629 | #endif |
| 1630 | if (SearchPath(NULL, name, NULL, _MAX_PATH, fname, &dum) == 0) |
| 1631 | return FALSE; |
| 1632 | if (mch_isdir(fname)) |
| 1633 | return FALSE; |
| 1634 | return TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1635 | } |
| 1636 | |
Bram Moolenaar | d32a99a | 2010-09-21 17:29:23 +0200 | [diff] [blame] | 1637 | #if ((defined(__MINGW32__) || defined (__CYGWIN32__)) && \ |
| 1638 | __MSVCRT_VERSION__ >= 0x800) || (defined(_MSC_VER) && _MSC_VER >= 1400) |
| 1639 | /* |
| 1640 | * Bad parameter handler. |
| 1641 | * |
| 1642 | * Certain MS CRT functions will intentionally crash when passed invalid |
| 1643 | * parameters to highlight possible security holes. Setting this function as |
| 1644 | * the bad parameter handler will prevent the crash. |
| 1645 | * |
| 1646 | * In debug builds the parameters contain CRT information that might help track |
| 1647 | * down the source of a problem, but in non-debug builds the arguments are all |
| 1648 | * NULL/0. Debug builds will also produce assert dialogs from the CRT, it is |
| 1649 | * worth allowing these to make debugging of issues easier. |
| 1650 | */ |
| 1651 | static void |
| 1652 | bad_param_handler(const wchar_t *expression, |
| 1653 | const wchar_t *function, |
| 1654 | const wchar_t *file, |
| 1655 | unsigned int line, |
| 1656 | uintptr_t pReserved) |
| 1657 | { |
| 1658 | } |
| 1659 | |
| 1660 | # define SET_INVALID_PARAM_HANDLER \ |
| 1661 | ((void)_set_invalid_parameter_handler(bad_param_handler)) |
| 1662 | #else |
| 1663 | # define SET_INVALID_PARAM_HANDLER |
| 1664 | #endif |
| 1665 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1666 | #ifdef FEAT_GUI_W32 |
| 1667 | |
| 1668 | /* |
| 1669 | * GUI version of mch_init(). |
| 1670 | */ |
| 1671 | void |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 1672 | mch_init(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1673 | { |
| 1674 | #ifndef __MINGW32__ |
| 1675 | extern int _fmode; |
| 1676 | #endif |
| 1677 | |
Bram Moolenaar | d32a99a | 2010-09-21 17:29:23 +0200 | [diff] [blame] | 1678 | /* Silently handle invalid parameters to CRT functions */ |
| 1679 | SET_INVALID_PARAM_HANDLER; |
| 1680 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1681 | /* Let critical errors result in a failure, not in a dialog box. Required |
| 1682 | * for the timestamp test to work on removed floppies. */ |
| 1683 | SetErrorMode(SEM_FAILCRITICALERRORS); |
| 1684 | |
| 1685 | _fmode = O_BINARY; /* we do our own CR-LF translation */ |
| 1686 | |
| 1687 | /* Specify window size. Is there a place to get the default from? */ |
| 1688 | Rows = 25; |
| 1689 | Columns = 80; |
| 1690 | |
| 1691 | /* Look for 'vimrun' */ |
| 1692 | if (!gui_is_win32s()) |
| 1693 | { |
| 1694 | char_u vimrun_location[_MAX_PATH + 4]; |
| 1695 | |
| 1696 | /* First try in same directory as gvim.exe */ |
| 1697 | STRCPY(vimrun_location, exe_name); |
| 1698 | STRCPY(gettail(vimrun_location), "vimrun.exe"); |
| 1699 | if (mch_getperm(vimrun_location) >= 0) |
| 1700 | { |
| 1701 | if (*skiptowhite(vimrun_location) != NUL) |
| 1702 | { |
| 1703 | /* Enclose path with white space in double quotes. */ |
| 1704 | mch_memmove(vimrun_location + 1, vimrun_location, |
| 1705 | STRLEN(vimrun_location) + 1); |
| 1706 | *vimrun_location = '"'; |
| 1707 | STRCPY(gettail(vimrun_location), "vimrun\" "); |
| 1708 | } |
| 1709 | else |
| 1710 | STRCPY(gettail(vimrun_location), "vimrun "); |
| 1711 | |
| 1712 | vimrun_path = (char *)vim_strsave(vimrun_location); |
| 1713 | s_dont_use_vimrun = FALSE; |
| 1714 | } |
| 1715 | else if (executable_exists("vimrun.exe")) |
| 1716 | s_dont_use_vimrun = FALSE; |
| 1717 | |
| 1718 | /* Don't give the warning for a missing vimrun.exe right now, but only |
| 1719 | * when vimrun was supposed to be used. Don't bother people that do |
| 1720 | * not need vimrun.exe. */ |
| 1721 | if (s_dont_use_vimrun) |
| 1722 | need_vimrun_warning = TRUE; |
| 1723 | } |
| 1724 | |
| 1725 | /* |
| 1726 | * If "finstr.exe" doesn't exist, use "grep -n" for 'grepprg'. |
| 1727 | * Otherwise the default "findstr /n" is used. |
| 1728 | */ |
| 1729 | if (!executable_exists("findstr.exe")) |
| 1730 | set_option_value((char_u *)"grepprg", 0, (char_u *)"grep -n", 0); |
| 1731 | |
| 1732 | #ifdef FEAT_CLIPBOARD |
| 1733 | clip_init(TRUE); |
| 1734 | |
| 1735 | /* |
Bram Moolenaar | 7528dd6 | 2007-05-06 12:32:12 +0000 | [diff] [blame] | 1736 | * Vim's own clipboard format recognises whether the text is char, line, |
| 1737 | * or rectangular block. Only useful for copying between two Vims. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1738 | * "VimClipboard" was used for previous versions, using the first |
| 1739 | * character to specify MCHAR, MLINE or MBLOCK. |
| 1740 | */ |
| 1741 | clip_star.format = RegisterClipboardFormat("VimClipboard2"); |
| 1742 | clip_star.format_raw = RegisterClipboardFormat("VimRawBytes"); |
| 1743 | #endif |
| 1744 | } |
| 1745 | |
| 1746 | |
| 1747 | #else /* FEAT_GUI_W32 */ |
| 1748 | |
| 1749 | #define SRWIDTH(sr) ((sr).Right - (sr).Left + 1) |
| 1750 | #define SRHEIGHT(sr) ((sr).Bottom - (sr).Top + 1) |
| 1751 | |
| 1752 | /* |
| 1753 | * ClearConsoleBuffer() |
| 1754 | * Description: |
| 1755 | * Clears the entire contents of the console screen buffer, using the |
| 1756 | * specified attribute. |
| 1757 | * Returns: |
| 1758 | * TRUE on success |
| 1759 | */ |
| 1760 | static BOOL |
| 1761 | ClearConsoleBuffer(WORD wAttribute) |
| 1762 | { |
| 1763 | CONSOLE_SCREEN_BUFFER_INFO csbi; |
| 1764 | COORD coord; |
| 1765 | DWORD NumCells, dummy; |
| 1766 | |
| 1767 | if (!GetConsoleScreenBufferInfo(g_hConOut, &csbi)) |
| 1768 | return FALSE; |
| 1769 | |
| 1770 | NumCells = csbi.dwSize.X * csbi.dwSize.Y; |
| 1771 | coord.X = 0; |
| 1772 | coord.Y = 0; |
| 1773 | if (!FillConsoleOutputCharacter(g_hConOut, ' ', NumCells, |
| 1774 | coord, &dummy)) |
| 1775 | { |
| 1776 | return FALSE; |
| 1777 | } |
| 1778 | if (!FillConsoleOutputAttribute(g_hConOut, wAttribute, NumCells, |
| 1779 | coord, &dummy)) |
| 1780 | { |
| 1781 | return FALSE; |
| 1782 | } |
| 1783 | |
| 1784 | return TRUE; |
| 1785 | } |
| 1786 | |
| 1787 | /* |
| 1788 | * FitConsoleWindow() |
| 1789 | * Description: |
| 1790 | * Checks if the console window will fit within given buffer dimensions. |
| 1791 | * Also, if requested, will shrink the window to fit. |
| 1792 | * Returns: |
| 1793 | * TRUE on success |
| 1794 | */ |
| 1795 | static BOOL |
| 1796 | FitConsoleWindow( |
| 1797 | COORD dwBufferSize, |
| 1798 | BOOL WantAdjust) |
| 1799 | { |
| 1800 | CONSOLE_SCREEN_BUFFER_INFO csbi; |
| 1801 | COORD dwWindowSize; |
| 1802 | BOOL NeedAdjust = FALSE; |
| 1803 | |
| 1804 | if (GetConsoleScreenBufferInfo(g_hConOut, &csbi)) |
| 1805 | { |
| 1806 | /* |
| 1807 | * A buffer resize will fail if the current console window does |
| 1808 | * not lie completely within that buffer. To avoid this, we might |
| 1809 | * have to move and possibly shrink the window. |
| 1810 | */ |
| 1811 | if (csbi.srWindow.Right >= dwBufferSize.X) |
| 1812 | { |
| 1813 | dwWindowSize.X = SRWIDTH(csbi.srWindow); |
| 1814 | if (dwWindowSize.X > dwBufferSize.X) |
| 1815 | dwWindowSize.X = dwBufferSize.X; |
| 1816 | csbi.srWindow.Right = dwBufferSize.X - 1; |
| 1817 | csbi.srWindow.Left = dwBufferSize.X - dwWindowSize.X; |
| 1818 | NeedAdjust = TRUE; |
| 1819 | } |
| 1820 | if (csbi.srWindow.Bottom >= dwBufferSize.Y) |
| 1821 | { |
| 1822 | dwWindowSize.Y = SRHEIGHT(csbi.srWindow); |
| 1823 | if (dwWindowSize.Y > dwBufferSize.Y) |
| 1824 | dwWindowSize.Y = dwBufferSize.Y; |
| 1825 | csbi.srWindow.Bottom = dwBufferSize.Y - 1; |
| 1826 | csbi.srWindow.Top = dwBufferSize.Y - dwWindowSize.Y; |
| 1827 | NeedAdjust = TRUE; |
| 1828 | } |
| 1829 | if (NeedAdjust && WantAdjust) |
| 1830 | { |
| 1831 | if (!SetConsoleWindowInfo(g_hConOut, TRUE, &csbi.srWindow)) |
| 1832 | return FALSE; |
| 1833 | } |
| 1834 | return TRUE; |
| 1835 | } |
| 1836 | |
| 1837 | return FALSE; |
| 1838 | } |
| 1839 | |
| 1840 | typedef struct ConsoleBufferStruct |
| 1841 | { |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 1842 | BOOL IsValid; |
| 1843 | CONSOLE_SCREEN_BUFFER_INFO Info; |
| 1844 | PCHAR_INFO Buffer; |
| 1845 | COORD BufferSize; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1846 | } ConsoleBuffer; |
| 1847 | |
| 1848 | /* |
| 1849 | * SaveConsoleBuffer() |
| 1850 | * Description: |
| 1851 | * Saves important information about the console buffer, including the |
| 1852 | * actual buffer contents. The saved information is suitable for later |
| 1853 | * restoration by RestoreConsoleBuffer(). |
| 1854 | * Returns: |
| 1855 | * TRUE if all information was saved; FALSE otherwise |
| 1856 | * If FALSE, still sets cb->IsValid if buffer characteristics were saved. |
| 1857 | */ |
| 1858 | static BOOL |
| 1859 | SaveConsoleBuffer( |
| 1860 | ConsoleBuffer *cb) |
| 1861 | { |
| 1862 | DWORD NumCells; |
| 1863 | COORD BufferCoord; |
| 1864 | SMALL_RECT ReadRegion; |
| 1865 | WORD Y, Y_incr; |
| 1866 | |
| 1867 | if (cb == NULL) |
| 1868 | return FALSE; |
| 1869 | |
| 1870 | if (!GetConsoleScreenBufferInfo(g_hConOut, &cb->Info)) |
| 1871 | { |
| 1872 | cb->IsValid = FALSE; |
| 1873 | return FALSE; |
| 1874 | } |
| 1875 | cb->IsValid = TRUE; |
| 1876 | |
| 1877 | /* |
| 1878 | * Allocate a buffer large enough to hold the entire console screen |
| 1879 | * buffer. If this ConsoleBuffer structure has already been initialized |
| 1880 | * with a buffer of the correct size, then just use that one. |
| 1881 | */ |
| 1882 | if (!cb->IsValid || cb->Buffer == NULL || |
| 1883 | cb->BufferSize.X != cb->Info.dwSize.X || |
| 1884 | cb->BufferSize.Y != cb->Info.dwSize.Y) |
| 1885 | { |
| 1886 | cb->BufferSize.X = cb->Info.dwSize.X; |
| 1887 | cb->BufferSize.Y = cb->Info.dwSize.Y; |
| 1888 | NumCells = cb->BufferSize.X * cb->BufferSize.Y; |
Bram Moolenaar | 3c2d653 | 2011-02-01 13:48:53 +0100 | [diff] [blame] | 1889 | vim_free(cb->Buffer); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1890 | cb->Buffer = (PCHAR_INFO)alloc(NumCells * sizeof(CHAR_INFO)); |
| 1891 | if (cb->Buffer == NULL) |
| 1892 | return FALSE; |
| 1893 | } |
| 1894 | |
| 1895 | /* |
| 1896 | * We will now copy the console screen buffer into our buffer. |
| 1897 | * ReadConsoleOutput() seems to be limited as far as how much you |
| 1898 | * can read at a time. Empirically, this number seems to be about |
| 1899 | * 12000 cells (rows * columns). Start at position (0, 0) and copy |
| 1900 | * in chunks until it is all copied. The chunks will all have the |
| 1901 | * same horizontal characteristics, so initialize them now. The |
| 1902 | * height of each chunk will be (12000 / width). |
| 1903 | */ |
| 1904 | BufferCoord.X = 0; |
| 1905 | ReadRegion.Left = 0; |
| 1906 | ReadRegion.Right = cb->Info.dwSize.X - 1; |
| 1907 | Y_incr = 12000 / cb->Info.dwSize.X; |
| 1908 | for (Y = 0; Y < cb->BufferSize.Y; Y += Y_incr) |
| 1909 | { |
| 1910 | /* |
| 1911 | * Read into position (0, Y) in our buffer. |
| 1912 | */ |
| 1913 | BufferCoord.Y = Y; |
| 1914 | /* |
| 1915 | * Read the region whose top left corner is (0, Y) and whose bottom |
| 1916 | * right corner is (width - 1, Y + Y_incr - 1). This should define |
| 1917 | * a region of size width by Y_incr. Don't worry if this region is |
| 1918 | * too large for the remaining buffer; it will be cropped. |
| 1919 | */ |
| 1920 | ReadRegion.Top = Y; |
| 1921 | ReadRegion.Bottom = Y + Y_incr - 1; |
| 1922 | if (!ReadConsoleOutput(g_hConOut, /* output handle */ |
| 1923 | cb->Buffer, /* our buffer */ |
| 1924 | cb->BufferSize, /* dimensions of our buffer */ |
| 1925 | BufferCoord, /* offset in our buffer */ |
| 1926 | &ReadRegion)) /* region to save */ |
| 1927 | { |
| 1928 | vim_free(cb->Buffer); |
| 1929 | cb->Buffer = NULL; |
| 1930 | return FALSE; |
| 1931 | } |
| 1932 | } |
| 1933 | |
| 1934 | return TRUE; |
| 1935 | } |
| 1936 | |
| 1937 | /* |
| 1938 | * RestoreConsoleBuffer() |
| 1939 | * Description: |
| 1940 | * Restores important information about the console buffer, including the |
| 1941 | * actual buffer contents, if desired. The information to restore is in |
| 1942 | * the same format used by SaveConsoleBuffer(). |
| 1943 | * Returns: |
| 1944 | * TRUE on success |
| 1945 | */ |
| 1946 | static BOOL |
| 1947 | RestoreConsoleBuffer( |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 1948 | ConsoleBuffer *cb, |
| 1949 | BOOL RestoreScreen) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1950 | { |
| 1951 | COORD BufferCoord; |
| 1952 | SMALL_RECT WriteRegion; |
| 1953 | |
| 1954 | if (cb == NULL || !cb->IsValid) |
| 1955 | return FALSE; |
| 1956 | |
| 1957 | /* |
| 1958 | * Before restoring the buffer contents, clear the current buffer, and |
| 1959 | * restore the cursor position and window information. Doing this now |
| 1960 | * prevents old buffer contents from "flashing" onto the screen. |
| 1961 | */ |
| 1962 | if (RestoreScreen) |
| 1963 | ClearConsoleBuffer(cb->Info.wAttributes); |
| 1964 | |
| 1965 | FitConsoleWindow(cb->Info.dwSize, TRUE); |
| 1966 | if (!SetConsoleScreenBufferSize(g_hConOut, cb->Info.dwSize)) |
| 1967 | return FALSE; |
| 1968 | if (!SetConsoleTextAttribute(g_hConOut, cb->Info.wAttributes)) |
| 1969 | return FALSE; |
| 1970 | |
| 1971 | if (!RestoreScreen) |
| 1972 | { |
| 1973 | /* |
| 1974 | * No need to restore the screen buffer contents, so we're done. |
| 1975 | */ |
| 1976 | return TRUE; |
| 1977 | } |
| 1978 | |
| 1979 | if (!SetConsoleCursorPosition(g_hConOut, cb->Info.dwCursorPosition)) |
| 1980 | return FALSE; |
| 1981 | if (!SetConsoleWindowInfo(g_hConOut, TRUE, &cb->Info.srWindow)) |
| 1982 | return FALSE; |
| 1983 | |
| 1984 | /* |
| 1985 | * Restore the screen buffer contents. |
| 1986 | */ |
| 1987 | if (cb->Buffer != NULL) |
| 1988 | { |
| 1989 | BufferCoord.X = 0; |
| 1990 | BufferCoord.Y = 0; |
| 1991 | WriteRegion.Left = 0; |
| 1992 | WriteRegion.Top = 0; |
| 1993 | WriteRegion.Right = cb->Info.dwSize.X - 1; |
| 1994 | WriteRegion.Bottom = cb->Info.dwSize.Y - 1; |
| 1995 | if (!WriteConsoleOutput(g_hConOut, /* output handle */ |
| 1996 | cb->Buffer, /* our buffer */ |
| 1997 | cb->BufferSize, /* dimensions of our buffer */ |
| 1998 | BufferCoord, /* offset in our buffer */ |
| 1999 | &WriteRegion)) /* region to restore */ |
| 2000 | { |
| 2001 | return FALSE; |
| 2002 | } |
| 2003 | } |
| 2004 | |
| 2005 | return TRUE; |
| 2006 | } |
| 2007 | |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 2008 | #define FEAT_RESTORE_ORIG_SCREEN |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2009 | #ifdef FEAT_RESTORE_ORIG_SCREEN |
| 2010 | static ConsoleBuffer g_cbOrig = { 0 }; |
| 2011 | #endif |
| 2012 | static ConsoleBuffer g_cbNonTermcap = { 0 }; |
| 2013 | static ConsoleBuffer g_cbTermcap = { 0 }; |
| 2014 | |
| 2015 | #ifdef FEAT_TITLE |
| 2016 | #ifdef __BORLANDC__ |
| 2017 | typedef HWND (__stdcall *GETCONSOLEWINDOWPROC)(VOID); |
| 2018 | #else |
| 2019 | typedef WINBASEAPI HWND (WINAPI *GETCONSOLEWINDOWPROC)(VOID); |
| 2020 | #endif |
| 2021 | char g_szOrigTitle[256] = { 0 }; |
| 2022 | HWND g_hWnd = NULL; /* also used in os_mswin.c */ |
| 2023 | static HICON g_hOrigIconSmall = NULL; |
| 2024 | static HICON g_hOrigIcon = NULL; |
| 2025 | static HICON g_hVimIcon = NULL; |
| 2026 | static BOOL g_fCanChangeIcon = FALSE; |
| 2027 | |
| 2028 | /* ICON* are not defined in VC++ 4.0 */ |
| 2029 | #ifndef ICON_SMALL |
| 2030 | #define ICON_SMALL 0 |
| 2031 | #endif |
| 2032 | #ifndef ICON_BIG |
| 2033 | #define ICON_BIG 1 |
| 2034 | #endif |
| 2035 | /* |
| 2036 | * GetConsoleIcon() |
| 2037 | * Description: |
| 2038 | * Attempts to retrieve the small icon and/or the big icon currently in |
| 2039 | * use by a given window. |
| 2040 | * Returns: |
| 2041 | * TRUE on success |
| 2042 | */ |
| 2043 | static BOOL |
| 2044 | GetConsoleIcon( |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 2045 | HWND hWnd, |
| 2046 | HICON *phIconSmall, |
| 2047 | HICON *phIcon) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2048 | { |
| 2049 | if (hWnd == NULL) |
| 2050 | return FALSE; |
| 2051 | |
| 2052 | if (phIconSmall != NULL) |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 2053 | *phIconSmall = (HICON)SendMessage(hWnd, WM_GETICON, |
| 2054 | (WPARAM)ICON_SMALL, (LPARAM)0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2055 | if (phIcon != NULL) |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 2056 | *phIcon = (HICON)SendMessage(hWnd, WM_GETICON, |
| 2057 | (WPARAM)ICON_BIG, (LPARAM)0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2058 | return TRUE; |
| 2059 | } |
| 2060 | |
| 2061 | /* |
| 2062 | * SetConsoleIcon() |
| 2063 | * Description: |
| 2064 | * Attempts to change the small icon and/or the big icon currently in |
| 2065 | * use by a given window. |
| 2066 | * Returns: |
| 2067 | * TRUE on success |
| 2068 | */ |
| 2069 | static BOOL |
| 2070 | SetConsoleIcon( |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 2071 | HWND hWnd, |
| 2072 | HICON hIconSmall, |
| 2073 | HICON hIcon) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2074 | { |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 2075 | HICON hPrevIconSmall; |
| 2076 | HICON hPrevIcon; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2077 | |
| 2078 | if (hWnd == NULL) |
| 2079 | return FALSE; |
| 2080 | |
| 2081 | if (hIconSmall != NULL) |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 2082 | hPrevIconSmall = (HICON)SendMessage(hWnd, WM_SETICON, |
| 2083 | (WPARAM)ICON_SMALL, (LPARAM)hIconSmall); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2084 | if (hIcon != NULL) |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 2085 | hPrevIcon = (HICON)SendMessage(hWnd, WM_SETICON, |
| 2086 | (WPARAM)ICON_BIG,(LPARAM) hIcon); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2087 | return TRUE; |
| 2088 | } |
| 2089 | |
| 2090 | /* |
| 2091 | * SaveConsoleTitleAndIcon() |
| 2092 | * Description: |
| 2093 | * Saves the current console window title in g_szOrigTitle, for later |
| 2094 | * restoration. Also, attempts to obtain a handle to the console window, |
| 2095 | * and use it to save the small and big icons currently in use by the |
| 2096 | * console window. This is not always possible on some versions of Windows; |
| 2097 | * nor is it possible when running Vim remotely using Telnet (since the |
| 2098 | * console window the user sees is owned by a remote process). |
| 2099 | */ |
| 2100 | static void |
| 2101 | SaveConsoleTitleAndIcon(void) |
| 2102 | { |
| 2103 | GETCONSOLEWINDOWPROC GetConsoleWindowProc; |
| 2104 | |
| 2105 | /* Save the original title. */ |
| 2106 | if (!GetConsoleTitle(g_szOrigTitle, sizeof(g_szOrigTitle))) |
| 2107 | return; |
| 2108 | |
| 2109 | /* |
| 2110 | * Obtain a handle to the console window using GetConsoleWindow() from |
| 2111 | * KERNEL32.DLL; we need to handle in order to change the window icon. |
| 2112 | * This function only exists on NT-based Windows, starting with Windows |
| 2113 | * 2000. On older operating systems, we can't change the window icon |
| 2114 | * anyway. |
| 2115 | */ |
| 2116 | if ((GetConsoleWindowProc = (GETCONSOLEWINDOWPROC) |
| 2117 | GetProcAddress(GetModuleHandle("KERNEL32.DLL"), |
| 2118 | "GetConsoleWindow")) != NULL) |
| 2119 | { |
| 2120 | g_hWnd = (*GetConsoleWindowProc)(); |
| 2121 | } |
| 2122 | if (g_hWnd == NULL) |
| 2123 | return; |
| 2124 | |
| 2125 | /* Save the original console window icon. */ |
| 2126 | GetConsoleIcon(g_hWnd, &g_hOrigIconSmall, &g_hOrigIcon); |
| 2127 | if (g_hOrigIconSmall == NULL || g_hOrigIcon == NULL) |
| 2128 | return; |
| 2129 | |
| 2130 | /* Extract the first icon contained in the Vim executable. */ |
| 2131 | g_hVimIcon = ExtractIcon(NULL, exe_name, 0); |
| 2132 | if (g_hVimIcon != NULL) |
| 2133 | g_fCanChangeIcon = TRUE; |
| 2134 | } |
| 2135 | #endif |
| 2136 | |
| 2137 | static int g_fWindInitCalled = FALSE; |
| 2138 | static int g_fTermcapMode = FALSE; |
| 2139 | static CONSOLE_CURSOR_INFO g_cci; |
| 2140 | static DWORD g_cmodein = 0; |
| 2141 | static DWORD g_cmodeout = 0; |
| 2142 | |
| 2143 | /* |
| 2144 | * non-GUI version of mch_init(). |
| 2145 | */ |
| 2146 | void |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 2147 | mch_init(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2148 | { |
| 2149 | #ifndef FEAT_RESTORE_ORIG_SCREEN |
| 2150 | CONSOLE_SCREEN_BUFFER_INFO csbi; |
| 2151 | #endif |
| 2152 | #ifndef __MINGW32__ |
| 2153 | extern int _fmode; |
| 2154 | #endif |
| 2155 | |
Bram Moolenaar | d32a99a | 2010-09-21 17:29:23 +0200 | [diff] [blame] | 2156 | /* Silently handle invalid parameters to CRT functions */ |
| 2157 | SET_INVALID_PARAM_HANDLER; |
| 2158 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2159 | /* Let critical errors result in a failure, not in a dialog box. Required |
| 2160 | * for the timestamp test to work on removed floppies. */ |
| 2161 | SetErrorMode(SEM_FAILCRITICALERRORS); |
| 2162 | |
| 2163 | _fmode = O_BINARY; /* we do our own CR-LF translation */ |
| 2164 | out_flush(); |
| 2165 | |
| 2166 | /* Obtain handles for the standard Console I/O devices */ |
| 2167 | if (read_cmd_fd == 0) |
| 2168 | g_hConIn = GetStdHandle(STD_INPUT_HANDLE); |
| 2169 | else |
| 2170 | create_conin(); |
| 2171 | g_hConOut = GetStdHandle(STD_OUTPUT_HANDLE); |
| 2172 | |
| 2173 | #ifdef FEAT_RESTORE_ORIG_SCREEN |
| 2174 | /* Save the initial console buffer for later restoration */ |
| 2175 | SaveConsoleBuffer(&g_cbOrig); |
| 2176 | g_attrCurrent = g_attrDefault = g_cbOrig.Info.wAttributes; |
| 2177 | #else |
| 2178 | /* Get current text attributes */ |
| 2179 | GetConsoleScreenBufferInfo(g_hConOut, &csbi); |
| 2180 | g_attrCurrent = g_attrDefault = csbi.wAttributes; |
| 2181 | #endif |
| 2182 | if (cterm_normal_fg_color == 0) |
| 2183 | cterm_normal_fg_color = (g_attrCurrent & 0xf) + 1; |
| 2184 | if (cterm_normal_bg_color == 0) |
| 2185 | cterm_normal_bg_color = ((g_attrCurrent >> 4) & 0xf) + 1; |
| 2186 | |
| 2187 | /* set termcap codes to current text attributes */ |
| 2188 | update_tcap(g_attrCurrent); |
| 2189 | |
| 2190 | GetConsoleCursorInfo(g_hConOut, &g_cci); |
| 2191 | GetConsoleMode(g_hConIn, &g_cmodein); |
| 2192 | GetConsoleMode(g_hConOut, &g_cmodeout); |
| 2193 | |
| 2194 | #ifdef FEAT_TITLE |
| 2195 | SaveConsoleTitleAndIcon(); |
| 2196 | /* |
| 2197 | * Set both the small and big icons of the console window to Vim's icon. |
| 2198 | * Note that Vim presently only has one size of icon (32x32), but it |
| 2199 | * automatically gets scaled down to 16x16 when setting the small icon. |
| 2200 | */ |
| 2201 | if (g_fCanChangeIcon) |
| 2202 | SetConsoleIcon(g_hWnd, g_hVimIcon, g_hVimIcon); |
| 2203 | #endif |
| 2204 | |
| 2205 | ui_get_shellsize(); |
| 2206 | |
| 2207 | #ifdef MCH_WRITE_DUMP |
| 2208 | fdDump = fopen("dump", "wt"); |
| 2209 | |
| 2210 | if (fdDump) |
| 2211 | { |
| 2212 | time_t t; |
| 2213 | |
| 2214 | time(&t); |
| 2215 | fputs(ctime(&t), fdDump); |
| 2216 | fflush(fdDump); |
| 2217 | } |
| 2218 | #endif |
| 2219 | |
| 2220 | g_fWindInitCalled = TRUE; |
| 2221 | |
| 2222 | #ifdef FEAT_MOUSE |
| 2223 | g_fMouseAvail = GetSystemMetrics(SM_MOUSEPRESENT); |
| 2224 | #endif |
| 2225 | |
| 2226 | #ifdef FEAT_CLIPBOARD |
| 2227 | clip_init(TRUE); |
| 2228 | |
| 2229 | /* |
| 2230 | * Vim's own clipboard format recognises whether the text is char, line, or |
| 2231 | * rectangular block. Only useful for copying between two Vims. |
| 2232 | * "VimClipboard" was used for previous versions, using the first |
| 2233 | * character to specify MCHAR, MLINE or MBLOCK. |
| 2234 | */ |
| 2235 | clip_star.format = RegisterClipboardFormat("VimClipboard2"); |
| 2236 | clip_star.format_raw = RegisterClipboardFormat("VimRawBytes"); |
| 2237 | #endif |
| 2238 | |
| 2239 | /* This will be NULL on anything but NT 4.0 */ |
| 2240 | s_pfnGetConsoleKeyboardLayoutName = |
| 2241 | (PFNGCKLN) GetProcAddress(GetModuleHandle("kernel32.dll"), |
| 2242 | "GetConsoleKeyboardLayoutNameA"); |
| 2243 | } |
| 2244 | |
| 2245 | /* |
| 2246 | * non-GUI version of mch_exit(). |
| 2247 | * Shut down and exit with status `r' |
| 2248 | * Careful: mch_exit() may be called before mch_init()! |
| 2249 | */ |
| 2250 | void |
| 2251 | mch_exit(int r) |
| 2252 | { |
| 2253 | stoptermcap(); |
| 2254 | |
| 2255 | if (g_fWindInitCalled) |
| 2256 | settmode(TMODE_COOK); |
| 2257 | |
| 2258 | ml_close_all(TRUE); /* remove all memfiles */ |
| 2259 | |
| 2260 | if (g_fWindInitCalled) |
| 2261 | { |
| 2262 | #ifdef FEAT_TITLE |
| 2263 | mch_restore_title(3); |
| 2264 | /* |
| 2265 | * Restore both the small and big icons of the console window to |
| 2266 | * what they were at startup. Don't do this when the window is |
| 2267 | * closed, Vim would hang here. |
| 2268 | */ |
| 2269 | if (g_fCanChangeIcon && !g_fForceExit) |
| 2270 | SetConsoleIcon(g_hWnd, g_hOrigIconSmall, g_hOrigIcon); |
| 2271 | #endif |
| 2272 | |
| 2273 | #ifdef MCH_WRITE_DUMP |
| 2274 | if (fdDump) |
| 2275 | { |
| 2276 | time_t t; |
| 2277 | |
| 2278 | time(&t); |
| 2279 | fputs(ctime(&t), fdDump); |
| 2280 | fclose(fdDump); |
| 2281 | } |
| 2282 | fdDump = NULL; |
| 2283 | #endif |
| 2284 | } |
| 2285 | |
| 2286 | SetConsoleCursorInfo(g_hConOut, &g_cci); |
| 2287 | SetConsoleMode(g_hConIn, g_cmodein); |
| 2288 | SetConsoleMode(g_hConOut, g_cmodeout); |
| 2289 | |
| 2290 | #ifdef DYNAMIC_GETTEXT |
| 2291 | dyn_libintl_end(); |
| 2292 | #endif |
| 2293 | |
| 2294 | exit(r); |
| 2295 | } |
| 2296 | #endif /* !FEAT_GUI_W32 */ |
| 2297 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2298 | /* |
| 2299 | * Do we have an interactive window? |
| 2300 | */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 2301 | /*ARGSUSED*/ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2302 | int |
| 2303 | mch_check_win( |
| 2304 | int argc, |
| 2305 | char **argv) |
| 2306 | { |
| 2307 | get_exe_name(); |
| 2308 | |
| 2309 | #ifdef FEAT_GUI_W32 |
| 2310 | return OK; /* GUI always has a tty */ |
| 2311 | #else |
| 2312 | if (isatty(1)) |
| 2313 | return OK; |
| 2314 | return FAIL; |
| 2315 | #endif |
| 2316 | } |
| 2317 | |
| 2318 | |
| 2319 | /* |
| 2320 | * fname_case(): Set the case of the file name, if it already exists. |
| 2321 | * When "len" is > 0, also expand short to long filenames. |
| 2322 | */ |
| 2323 | void |
| 2324 | fname_case( |
| 2325 | char_u *name, |
| 2326 | int len) |
| 2327 | { |
| 2328 | char szTrueName[_MAX_PATH + 2]; |
Bram Moolenaar | 464c925 | 2010-10-13 20:37:41 +0200 | [diff] [blame] | 2329 | char szTrueNameTemp[_MAX_PATH + 2]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2330 | char *ptrue, *ptruePrev; |
| 2331 | char *porig, *porigPrev; |
| 2332 | int flen; |
| 2333 | WIN32_FIND_DATA fb; |
| 2334 | HANDLE hFind; |
| 2335 | int c; |
Bram Moolenaar | 464c925 | 2010-10-13 20:37:41 +0200 | [diff] [blame] | 2336 | int slen; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2337 | |
Bram Moolenaar | a3ffd9c | 2005-07-21 21:03:15 +0000 | [diff] [blame] | 2338 | flen = (int)STRLEN(name); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2339 | if (flen == 0 || flen > _MAX_PATH) |
| 2340 | return; |
| 2341 | |
| 2342 | slash_adjust(name); |
| 2343 | |
| 2344 | /* Build the new name in szTrueName[] one component at a time. */ |
| 2345 | porig = name; |
| 2346 | ptrue = szTrueName; |
| 2347 | |
| 2348 | if (isalpha(porig[0]) && porig[1] == ':') |
| 2349 | { |
| 2350 | /* copy leading drive letter */ |
| 2351 | *ptrue++ = *porig++; |
| 2352 | *ptrue++ = *porig++; |
| 2353 | *ptrue = NUL; /* in case nothing follows */ |
| 2354 | } |
| 2355 | |
| 2356 | while (*porig != NUL) |
| 2357 | { |
| 2358 | /* copy \ characters */ |
| 2359 | while (*porig == psepc) |
| 2360 | *ptrue++ = *porig++; |
| 2361 | |
| 2362 | ptruePrev = ptrue; |
| 2363 | porigPrev = porig; |
| 2364 | while (*porig != NUL && *porig != psepc) |
| 2365 | { |
| 2366 | #ifdef FEAT_MBYTE |
| 2367 | int l; |
| 2368 | |
| 2369 | if (enc_dbcs) |
| 2370 | { |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2371 | l = (*mb_ptr2len)(porig); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2372 | while (--l >= 0) |
| 2373 | *ptrue++ = *porig++; |
| 2374 | } |
| 2375 | else |
| 2376 | #endif |
| 2377 | *ptrue++ = *porig++; |
| 2378 | } |
| 2379 | *ptrue = NUL; |
| 2380 | |
Bram Moolenaar | 464c925 | 2010-10-13 20:37:41 +0200 | [diff] [blame] | 2381 | /* To avoid a slow failure append "\*" when searching a directory, |
| 2382 | * server or network share. */ |
| 2383 | STRCPY(szTrueNameTemp, szTrueName); |
Bram Moolenaar | 6b5ef06 | 2010-10-27 12:18:00 +0200 | [diff] [blame] | 2384 | slen = (int)strlen(szTrueNameTemp); |
Bram Moolenaar | 464c925 | 2010-10-13 20:37:41 +0200 | [diff] [blame] | 2385 | if (*porig == psepc && slen + 2 < _MAX_PATH) |
| 2386 | STRCPY(szTrueNameTemp + slen, "\\*"); |
| 2387 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2388 | /* Skip "", "." and "..". */ |
| 2389 | if (ptrue > ptruePrev |
| 2390 | && (ptruePrev[0] != '.' |
| 2391 | || (ptruePrev[1] != NUL |
| 2392 | && (ptruePrev[1] != '.' || ptruePrev[2] != NUL))) |
Bram Moolenaar | 464c925 | 2010-10-13 20:37:41 +0200 | [diff] [blame] | 2393 | && (hFind = FindFirstFile(szTrueNameTemp, &fb)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2394 | != INVALID_HANDLE_VALUE) |
| 2395 | { |
| 2396 | c = *porig; |
| 2397 | *porig = NUL; |
| 2398 | |
| 2399 | /* Only use the match when it's the same name (ignoring case) or |
| 2400 | * expansion is allowed and there is a match with the short name |
| 2401 | * and there is enough room. */ |
| 2402 | if (_stricoll(porigPrev, fb.cFileName) == 0 |
| 2403 | || (len > 0 |
| 2404 | && (_stricoll(porigPrev, fb.cAlternateFileName) == 0 |
| 2405 | && (int)(ptruePrev - szTrueName) |
| 2406 | + (int)strlen(fb.cFileName) < len))) |
| 2407 | { |
| 2408 | STRCPY(ptruePrev, fb.cFileName); |
| 2409 | |
| 2410 | /* Look for exact match and prefer it if found. Must be a |
| 2411 | * long name, otherwise there would be only one match. */ |
| 2412 | while (FindNextFile(hFind, &fb)) |
| 2413 | { |
| 2414 | if (*fb.cAlternateFileName != NUL |
| 2415 | && (strcoll(porigPrev, fb.cFileName) == 0 |
| 2416 | || (len > 0 |
| 2417 | && (_stricoll(porigPrev, |
| 2418 | fb.cAlternateFileName) == 0 |
| 2419 | && (int)(ptruePrev - szTrueName) |
| 2420 | + (int)strlen(fb.cFileName) < len)))) |
| 2421 | { |
| 2422 | STRCPY(ptruePrev, fb.cFileName); |
| 2423 | break; |
| 2424 | } |
| 2425 | } |
| 2426 | } |
| 2427 | FindClose(hFind); |
| 2428 | *porig = c; |
| 2429 | ptrue = ptruePrev + strlen(ptruePrev); |
| 2430 | } |
| 2431 | } |
| 2432 | |
| 2433 | STRCPY(name, szTrueName); |
| 2434 | } |
| 2435 | |
| 2436 | |
| 2437 | /* |
| 2438 | * Insert user name in s[len]. |
| 2439 | */ |
| 2440 | int |
| 2441 | mch_get_user_name( |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 2442 | char_u *s, |
| 2443 | int len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2444 | { |
Bram Moolenaar | 41a0903 | 2007-10-01 18:34:34 +0000 | [diff] [blame] | 2445 | char szUserName[256 + 1]; /* UNLEN is 256 */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2446 | DWORD cch = sizeof szUserName; |
| 2447 | |
| 2448 | if (GetUserName(szUserName, &cch)) |
| 2449 | { |
Bram Moolenaar | fe3ca8d | 2005-07-18 21:43:02 +0000 | [diff] [blame] | 2450 | vim_strncpy(s, szUserName, len - 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2451 | return OK; |
| 2452 | } |
| 2453 | s[0] = NUL; |
| 2454 | return FAIL; |
| 2455 | } |
| 2456 | |
| 2457 | |
| 2458 | /* |
| 2459 | * Insert host name in s[len]. |
| 2460 | */ |
| 2461 | void |
| 2462 | mch_get_host_name( |
| 2463 | char_u *s, |
| 2464 | int len) |
| 2465 | { |
| 2466 | DWORD cch = len; |
| 2467 | |
| 2468 | if (!GetComputerName(s, &cch)) |
Bram Moolenaar | fe3ca8d | 2005-07-18 21:43:02 +0000 | [diff] [blame] | 2469 | vim_strncpy(s, "PC (Win32 Vim)", len - 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2470 | } |
| 2471 | |
| 2472 | |
| 2473 | /* |
| 2474 | * return process ID |
| 2475 | */ |
| 2476 | long |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 2477 | mch_get_pid(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2478 | { |
| 2479 | return (long)GetCurrentProcessId(); |
| 2480 | } |
| 2481 | |
| 2482 | |
| 2483 | /* |
| 2484 | * Get name of current directory into buffer 'buf' of length 'len' bytes. |
| 2485 | * Return OK for success, FAIL for failure. |
| 2486 | */ |
| 2487 | int |
| 2488 | mch_dirname( |
| 2489 | char_u *buf, |
| 2490 | int len) |
| 2491 | { |
| 2492 | /* |
| 2493 | * Originally this was: |
| 2494 | * return (getcwd(buf, len) != NULL ? OK : FAIL); |
| 2495 | * But the Win32s known bug list says that getcwd() doesn't work |
| 2496 | * so use the Win32 system call instead. <Negri> |
| 2497 | */ |
| 2498 | #ifdef FEAT_MBYTE |
| 2499 | if (enc_codepage >= 0 && (int)GetACP() != enc_codepage) |
| 2500 | { |
| 2501 | WCHAR wbuf[_MAX_PATH + 1]; |
| 2502 | |
| 2503 | if (GetCurrentDirectoryW(_MAX_PATH, wbuf) != 0) |
| 2504 | { |
Bram Moolenaar | 36f692d | 2008-11-20 16:10:17 +0000 | [diff] [blame] | 2505 | char_u *p = utf16_to_enc(wbuf, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2506 | |
| 2507 | if (p != NULL) |
| 2508 | { |
Bram Moolenaar | fe3ca8d | 2005-07-18 21:43:02 +0000 | [diff] [blame] | 2509 | vim_strncpy(buf, p, len - 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2510 | vim_free(p); |
| 2511 | return OK; |
| 2512 | } |
| 2513 | } |
| 2514 | /* Retry with non-wide function (for Windows 98). */ |
| 2515 | } |
| 2516 | #endif |
| 2517 | return (GetCurrentDirectory(len, buf) != 0 ? OK : FAIL); |
| 2518 | } |
| 2519 | |
| 2520 | /* |
| 2521 | * get file permissions for `name' |
| 2522 | * -1 : error |
| 2523 | * else FILE_ATTRIBUTE_* defined in winnt.h |
| 2524 | */ |
| 2525 | long |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 2526 | mch_getperm(char_u *name) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2527 | { |
| 2528 | #ifdef FEAT_MBYTE |
| 2529 | if (enc_codepage >= 0 && (int)GetACP() != enc_codepage) |
| 2530 | { |
Bram Moolenaar | 36f692d | 2008-11-20 16:10:17 +0000 | [diff] [blame] | 2531 | WCHAR *p = enc_to_utf16(name, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2532 | long n; |
| 2533 | |
| 2534 | if (p != NULL) |
| 2535 | { |
| 2536 | n = (long)GetFileAttributesW(p); |
| 2537 | vim_free(p); |
| 2538 | if (n >= 0 || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED) |
| 2539 | return n; |
| 2540 | /* Retry with non-wide function (for Windows 98). */ |
| 2541 | } |
| 2542 | } |
| 2543 | #endif |
| 2544 | return (long)GetFileAttributes((char *)name); |
| 2545 | } |
| 2546 | |
| 2547 | |
| 2548 | /* |
| 2549 | * set file permission for `name' to `perm' |
| 2550 | */ |
| 2551 | int |
| 2552 | mch_setperm( |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 2553 | char_u *name, |
| 2554 | long perm) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2555 | { |
| 2556 | perm |= FILE_ATTRIBUTE_ARCHIVE; /* file has changed, set archive bit */ |
| 2557 | #ifdef FEAT_MBYTE |
| 2558 | if (enc_codepage >= 0 && (int)GetACP() != enc_codepage) |
| 2559 | { |
Bram Moolenaar | 36f692d | 2008-11-20 16:10:17 +0000 | [diff] [blame] | 2560 | WCHAR *p = enc_to_utf16(name, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2561 | long n; |
| 2562 | |
| 2563 | if (p != NULL) |
| 2564 | { |
| 2565 | n = (long)SetFileAttributesW(p, perm); |
| 2566 | vim_free(p); |
| 2567 | if (n || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED) |
| 2568 | return n ? OK : FAIL; |
| 2569 | /* Retry with non-wide function (for Windows 98). */ |
| 2570 | } |
| 2571 | } |
| 2572 | #endif |
| 2573 | return SetFileAttributes((char *)name, perm) ? OK : FAIL; |
| 2574 | } |
| 2575 | |
| 2576 | /* |
| 2577 | * Set hidden flag for "name". |
| 2578 | */ |
| 2579 | void |
| 2580 | mch_hide(char_u *name) |
| 2581 | { |
| 2582 | int perm; |
| 2583 | #ifdef FEAT_MBYTE |
| 2584 | WCHAR *p = NULL; |
| 2585 | |
| 2586 | if (enc_codepage >= 0 && (int)GetACP() != enc_codepage) |
Bram Moolenaar | 36f692d | 2008-11-20 16:10:17 +0000 | [diff] [blame] | 2587 | p = enc_to_utf16(name, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2588 | #endif |
| 2589 | |
| 2590 | #ifdef FEAT_MBYTE |
| 2591 | if (p != NULL) |
| 2592 | { |
| 2593 | perm = GetFileAttributesW(p); |
| 2594 | if (perm < 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED) |
| 2595 | { |
| 2596 | /* Retry with non-wide function (for Windows 98). */ |
| 2597 | vim_free(p); |
| 2598 | p = NULL; |
| 2599 | } |
| 2600 | } |
| 2601 | if (p == NULL) |
| 2602 | #endif |
| 2603 | perm = GetFileAttributes((char *)name); |
| 2604 | if (perm >= 0) |
| 2605 | { |
| 2606 | perm |= FILE_ATTRIBUTE_HIDDEN; |
| 2607 | #ifdef FEAT_MBYTE |
| 2608 | if (p != NULL) |
| 2609 | { |
| 2610 | if (SetFileAttributesW(p, perm) == 0 |
| 2611 | && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED) |
| 2612 | { |
| 2613 | /* Retry with non-wide function (for Windows 98). */ |
| 2614 | vim_free(p); |
| 2615 | p = NULL; |
| 2616 | } |
| 2617 | } |
| 2618 | if (p == NULL) |
| 2619 | #endif |
| 2620 | SetFileAttributes((char *)name, perm); |
| 2621 | } |
| 2622 | #ifdef FEAT_MBYTE |
| 2623 | vim_free(p); |
| 2624 | #endif |
| 2625 | } |
| 2626 | |
| 2627 | /* |
| 2628 | * return TRUE if "name" is a directory |
| 2629 | * return FALSE if "name" is not a directory or upon error |
| 2630 | */ |
| 2631 | int |
| 2632 | mch_isdir(char_u *name) |
| 2633 | { |
| 2634 | int f = mch_getperm(name); |
| 2635 | |
| 2636 | if (f == -1) |
| 2637 | return FALSE; /* file does not exist at all */ |
| 2638 | |
| 2639 | return (f & FILE_ATTRIBUTE_DIRECTORY) != 0; |
| 2640 | } |
| 2641 | |
| 2642 | /* |
Bram Moolenaar | 03f4855 | 2006-02-28 23:52:23 +0000 | [diff] [blame] | 2643 | * Return TRUE if file "fname" has more than one link. |
| 2644 | */ |
| 2645 | int |
| 2646 | mch_is_linked(char_u *fname) |
| 2647 | { |
| 2648 | HANDLE hFile; |
| 2649 | int res = 0; |
| 2650 | BY_HANDLE_FILE_INFORMATION inf; |
| 2651 | #ifdef FEAT_MBYTE |
| 2652 | WCHAR *wn = NULL; |
| 2653 | |
| 2654 | if (enc_codepage >= 0 && (int)GetACP() != enc_codepage) |
Bram Moolenaar | 36f692d | 2008-11-20 16:10:17 +0000 | [diff] [blame] | 2655 | wn = enc_to_utf16(fname, NULL); |
Bram Moolenaar | 03f4855 | 2006-02-28 23:52:23 +0000 | [diff] [blame] | 2656 | if (wn != NULL) |
| 2657 | { |
| 2658 | hFile = CreateFileW(wn, /* file name */ |
| 2659 | GENERIC_READ, /* access mode */ |
| 2660 | 0, /* share mode */ |
| 2661 | NULL, /* security descriptor */ |
| 2662 | OPEN_EXISTING, /* creation disposition */ |
| 2663 | 0, /* file attributes */ |
| 2664 | NULL); /* handle to template file */ |
| 2665 | if (hFile == INVALID_HANDLE_VALUE |
| 2666 | && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED) |
| 2667 | { |
| 2668 | /* Retry with non-wide function (for Windows 98). */ |
| 2669 | vim_free(wn); |
| 2670 | wn = NULL; |
| 2671 | } |
| 2672 | } |
| 2673 | if (wn == NULL) |
| 2674 | #endif |
| 2675 | hFile = CreateFile(fname, /* file name */ |
| 2676 | GENERIC_READ, /* access mode */ |
| 2677 | 0, /* share mode */ |
| 2678 | NULL, /* security descriptor */ |
| 2679 | OPEN_EXISTING, /* creation disposition */ |
| 2680 | 0, /* file attributes */ |
| 2681 | NULL); /* handle to template file */ |
| 2682 | |
| 2683 | if (hFile != INVALID_HANDLE_VALUE) |
| 2684 | { |
| 2685 | if (GetFileInformationByHandle(hFile, &inf) != 0 |
| 2686 | && inf.nNumberOfLinks > 1) |
| 2687 | res = 1; |
| 2688 | CloseHandle(hFile); |
| 2689 | } |
| 2690 | |
| 2691 | #ifdef FEAT_MBYTE |
| 2692 | vim_free(wn); |
| 2693 | #endif |
| 2694 | return res; |
| 2695 | } |
| 2696 | |
| 2697 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2698 | * Return TRUE if file or directory "name" is writable (not readonly). |
| 2699 | * Strange semantics of Win32: a readonly directory is writable, but you can't |
| 2700 | * delete a file. Let's say this means it is writable. |
| 2701 | */ |
| 2702 | int |
| 2703 | mch_writable(char_u *name) |
| 2704 | { |
| 2705 | int perm = mch_getperm(name); |
| 2706 | |
| 2707 | return (perm != -1 && (!(perm & FILE_ATTRIBUTE_READONLY) |
| 2708 | || (perm & FILE_ATTRIBUTE_DIRECTORY))); |
| 2709 | } |
| 2710 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2711 | /* |
| 2712 | * Return 1 if "name" can be executed, 0 if not. |
| 2713 | * Return -1 if unknown. |
| 2714 | */ |
| 2715 | int |
| 2716 | mch_can_exe(char_u *name) |
| 2717 | { |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 2718 | char_u buf[_MAX_PATH]; |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 2719 | int len = (int)STRLEN(name); |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 2720 | char_u *p; |
| 2721 | |
| 2722 | if (len >= _MAX_PATH) /* safety check */ |
| 2723 | return FALSE; |
| 2724 | |
| 2725 | /* If there already is an extension try using the name directly. Also do |
| 2726 | * this with a Unix-shell like 'shell'. */ |
| 2727 | if (vim_strchr(gettail(name), '.') != NULL |
| 2728 | || strstr((char *)gettail(p_sh), "sh") != NULL) |
| 2729 | if (executable_exists((char *)name)) |
| 2730 | return TRUE; |
| 2731 | |
| 2732 | /* |
| 2733 | * Loop over all extensions in $PATHEXT. |
| 2734 | */ |
Bram Moolenaar | fe3ca8d | 2005-07-18 21:43:02 +0000 | [diff] [blame] | 2735 | vim_strncpy(buf, name, _MAX_PATH - 1); |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 2736 | p = mch_getenv("PATHEXT"); |
| 2737 | if (p == NULL) |
| 2738 | p = (char_u *)".com;.exe;.bat;.cmd"; |
| 2739 | while (*p) |
| 2740 | { |
| 2741 | if (p[0] == '.' && (p[1] == NUL || p[1] == ';')) |
| 2742 | { |
| 2743 | /* A single "." means no extension is added. */ |
| 2744 | buf[len] = NUL; |
| 2745 | ++p; |
| 2746 | if (*p) |
| 2747 | ++p; |
| 2748 | } |
| 2749 | else |
| 2750 | copy_option_part(&p, buf + len, _MAX_PATH - len, ";"); |
| 2751 | if (executable_exists((char *)buf)) |
| 2752 | return TRUE; |
| 2753 | } |
| 2754 | return FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2755 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2756 | |
| 2757 | /* |
| 2758 | * Check what "name" is: |
| 2759 | * NODE_NORMAL: file or directory (or doesn't exist) |
| 2760 | * NODE_WRITABLE: writable device, socket, fifo, etc. |
| 2761 | * NODE_OTHER: non-writable things |
| 2762 | */ |
| 2763 | int |
| 2764 | mch_nodetype(char_u *name) |
| 2765 | { |
| 2766 | HANDLE hFile; |
| 2767 | int type; |
| 2768 | |
Bram Moolenaar | 043545e | 2006-10-10 16:44:07 +0000 | [diff] [blame] | 2769 | /* We can't open a file with a name "\\.\con" or "\\.\prn" and trying to |
| 2770 | * read from it later will cause Vim to hang. Thus return NODE_WRITABLE |
| 2771 | * here. */ |
| 2772 | if (STRNCMP(name, "\\\\.\\", 4) == 0) |
| 2773 | return NODE_WRITABLE; |
| 2774 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2775 | hFile = CreateFile(name, /* file name */ |
| 2776 | GENERIC_WRITE, /* access mode */ |
| 2777 | 0, /* share mode */ |
| 2778 | NULL, /* security descriptor */ |
| 2779 | OPEN_EXISTING, /* creation disposition */ |
| 2780 | 0, /* file attributes */ |
| 2781 | NULL); /* handle to template file */ |
| 2782 | |
| 2783 | if (hFile == INVALID_HANDLE_VALUE) |
| 2784 | return NODE_NORMAL; |
| 2785 | |
| 2786 | type = GetFileType(hFile); |
| 2787 | CloseHandle(hFile); |
| 2788 | if (type == FILE_TYPE_CHAR) |
| 2789 | return NODE_WRITABLE; |
| 2790 | if (type == FILE_TYPE_DISK) |
| 2791 | return NODE_NORMAL; |
| 2792 | return NODE_OTHER; |
| 2793 | } |
| 2794 | |
| 2795 | #ifdef HAVE_ACL |
| 2796 | struct my_acl |
| 2797 | { |
| 2798 | PSECURITY_DESCRIPTOR pSecurityDescriptor; |
| 2799 | PSID pSidOwner; |
| 2800 | PSID pSidGroup; |
| 2801 | PACL pDacl; |
| 2802 | PACL pSacl; |
| 2803 | }; |
| 2804 | #endif |
| 2805 | |
| 2806 | /* |
| 2807 | * Return a pointer to the ACL of file "fname" in allocated memory. |
| 2808 | * Return NULL if the ACL is not available for whatever reason. |
| 2809 | */ |
| 2810 | vim_acl_T |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 2811 | mch_get_acl(char_u *fname) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2812 | { |
| 2813 | #ifndef HAVE_ACL |
| 2814 | return (vim_acl_T)NULL; |
| 2815 | #else |
| 2816 | struct my_acl *p = NULL; |
| 2817 | |
| 2818 | /* This only works on Windows NT and 2000. */ |
| 2819 | if (g_PlatformId == VER_PLATFORM_WIN32_NT && advapi_lib != NULL) |
| 2820 | { |
| 2821 | p = (struct my_acl *)alloc_clear((unsigned)sizeof(struct my_acl)); |
| 2822 | if (p != NULL) |
| 2823 | { |
| 2824 | if (pGetNamedSecurityInfo( |
| 2825 | (LPTSTR)fname, // Abstract filename |
| 2826 | SE_FILE_OBJECT, // File Object |
| 2827 | // Retrieve the entire security descriptor. |
| 2828 | OWNER_SECURITY_INFORMATION | |
| 2829 | GROUP_SECURITY_INFORMATION | |
| 2830 | DACL_SECURITY_INFORMATION | |
| 2831 | SACL_SECURITY_INFORMATION, |
| 2832 | &p->pSidOwner, // Ownership information. |
| 2833 | &p->pSidGroup, // Group membership. |
| 2834 | &p->pDacl, // Discretionary information. |
| 2835 | &p->pSacl, // For auditing purposes. |
| 2836 | &p->pSecurityDescriptor |
| 2837 | ) != ERROR_SUCCESS) |
| 2838 | { |
| 2839 | mch_free_acl((vim_acl_T)p); |
| 2840 | p = NULL; |
| 2841 | } |
| 2842 | } |
| 2843 | } |
| 2844 | |
| 2845 | return (vim_acl_T)p; |
| 2846 | #endif |
| 2847 | } |
| 2848 | |
| 2849 | /* |
| 2850 | * Set the ACL of file "fname" to "acl" (unless it's NULL). |
| 2851 | * Errors are ignored. |
| 2852 | * This must only be called with "acl" equal to what mch_get_acl() returned. |
| 2853 | */ |
| 2854 | void |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 2855 | mch_set_acl(char_u *fname, vim_acl_T acl) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2856 | { |
| 2857 | #ifdef HAVE_ACL |
| 2858 | struct my_acl *p = (struct my_acl *)acl; |
| 2859 | |
| 2860 | if (p != NULL && advapi_lib != NULL) |
| 2861 | (void)pSetNamedSecurityInfo( |
| 2862 | (LPTSTR)fname, // Abstract filename |
| 2863 | SE_FILE_OBJECT, // File Object |
| 2864 | // Retrieve the entire security descriptor. |
| 2865 | OWNER_SECURITY_INFORMATION | |
| 2866 | GROUP_SECURITY_INFORMATION | |
| 2867 | DACL_SECURITY_INFORMATION | |
| 2868 | SACL_SECURITY_INFORMATION, |
| 2869 | p->pSidOwner, // Ownership information. |
| 2870 | p->pSidGroup, // Group membership. |
| 2871 | p->pDacl, // Discretionary information. |
| 2872 | p->pSacl // For auditing purposes. |
| 2873 | ); |
| 2874 | #endif |
| 2875 | } |
| 2876 | |
| 2877 | void |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 2878 | mch_free_acl(vim_acl_T acl) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2879 | { |
| 2880 | #ifdef HAVE_ACL |
| 2881 | struct my_acl *p = (struct my_acl *)acl; |
| 2882 | |
| 2883 | if (p != NULL) |
| 2884 | { |
| 2885 | LocalFree(p->pSecurityDescriptor); // Free the memory just in case |
| 2886 | vim_free(p); |
| 2887 | } |
| 2888 | #endif |
| 2889 | } |
| 2890 | |
| 2891 | #ifndef FEAT_GUI_W32 |
| 2892 | |
| 2893 | /* |
| 2894 | * handler for ctrl-break, ctrl-c interrupts, and fatal events. |
| 2895 | */ |
| 2896 | static BOOL WINAPI |
| 2897 | handler_routine( |
| 2898 | DWORD dwCtrlType) |
| 2899 | { |
| 2900 | switch (dwCtrlType) |
| 2901 | { |
| 2902 | case CTRL_C_EVENT: |
| 2903 | if (ctrl_c_interrupts) |
| 2904 | g_fCtrlCPressed = TRUE; |
| 2905 | return TRUE; |
| 2906 | |
| 2907 | case CTRL_BREAK_EVENT: |
| 2908 | g_fCBrkPressed = TRUE; |
| 2909 | return TRUE; |
| 2910 | |
| 2911 | /* fatal events: shut down gracefully */ |
| 2912 | case CTRL_CLOSE_EVENT: |
| 2913 | case CTRL_LOGOFF_EVENT: |
| 2914 | case CTRL_SHUTDOWN_EVENT: |
| 2915 | windgoto((int)Rows - 1, 0); |
| 2916 | g_fForceExit = TRUE; |
| 2917 | |
Bram Moolenaar | 0fde290 | 2008-03-16 13:54:13 +0000 | [diff] [blame] | 2918 | vim_snprintf((char *)IObuff, IOSIZE, _("Vim: Caught %s event\n"), |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2919 | (dwCtrlType == CTRL_CLOSE_EVENT |
| 2920 | ? _("close") |
| 2921 | : dwCtrlType == CTRL_LOGOFF_EVENT |
| 2922 | ? _("logoff") |
| 2923 | : _("shutdown"))); |
| 2924 | #ifdef DEBUG |
| 2925 | OutputDebugString(IObuff); |
| 2926 | #endif |
| 2927 | |
| 2928 | preserve_exit(); /* output IObuff, preserve files and exit */ |
| 2929 | |
| 2930 | return TRUE; /* not reached */ |
| 2931 | |
| 2932 | default: |
| 2933 | return FALSE; |
| 2934 | } |
| 2935 | } |
| 2936 | |
| 2937 | |
| 2938 | /* |
| 2939 | * set the tty in (raw) ? "raw" : "cooked" mode |
| 2940 | */ |
| 2941 | void |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 2942 | mch_settmode(int tmode) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2943 | { |
| 2944 | DWORD cmodein; |
| 2945 | DWORD cmodeout; |
| 2946 | BOOL bEnableHandler; |
| 2947 | |
| 2948 | GetConsoleMode(g_hConIn, &cmodein); |
| 2949 | GetConsoleMode(g_hConOut, &cmodeout); |
| 2950 | if (tmode == TMODE_RAW) |
| 2951 | { |
| 2952 | cmodein &= ~(ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT | |
| 2953 | ENABLE_ECHO_INPUT); |
| 2954 | #ifdef FEAT_MOUSE |
| 2955 | if (g_fMouseActive) |
| 2956 | cmodein |= ENABLE_MOUSE_INPUT; |
| 2957 | #endif |
| 2958 | cmodeout &= ~(ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT); |
| 2959 | bEnableHandler = TRUE; |
| 2960 | } |
| 2961 | else /* cooked */ |
| 2962 | { |
| 2963 | cmodein |= (ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT | |
| 2964 | ENABLE_ECHO_INPUT); |
| 2965 | cmodeout |= (ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT); |
| 2966 | bEnableHandler = FALSE; |
| 2967 | } |
| 2968 | SetConsoleMode(g_hConIn, cmodein); |
| 2969 | SetConsoleMode(g_hConOut, cmodeout); |
| 2970 | SetConsoleCtrlHandler(handler_routine, bEnableHandler); |
| 2971 | |
| 2972 | #ifdef MCH_WRITE_DUMP |
| 2973 | if (fdDump) |
| 2974 | { |
| 2975 | fprintf(fdDump, "mch_settmode(%s, in = %x, out = %x)\n", |
| 2976 | tmode == TMODE_RAW ? "raw" : |
| 2977 | tmode == TMODE_COOK ? "cooked" : "normal", |
| 2978 | cmodein, cmodeout); |
| 2979 | fflush(fdDump); |
| 2980 | } |
| 2981 | #endif |
| 2982 | } |
| 2983 | |
| 2984 | |
| 2985 | /* |
| 2986 | * Get the size of the current window in `Rows' and `Columns' |
| 2987 | * Return OK when size could be determined, FAIL otherwise. |
| 2988 | */ |
| 2989 | int |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 2990 | mch_get_shellsize(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2991 | { |
| 2992 | CONSOLE_SCREEN_BUFFER_INFO csbi; |
| 2993 | |
| 2994 | if (!g_fTermcapMode && g_cbTermcap.IsValid) |
| 2995 | { |
| 2996 | /* |
| 2997 | * For some reason, we are trying to get the screen dimensions |
| 2998 | * even though we are not in termcap mode. The 'Rows' and 'Columns' |
| 2999 | * variables are really intended to mean the size of Vim screen |
| 3000 | * while in termcap mode. |
| 3001 | */ |
| 3002 | Rows = g_cbTermcap.Info.dwSize.Y; |
| 3003 | Columns = g_cbTermcap.Info.dwSize.X; |
| 3004 | } |
| 3005 | else if (GetConsoleScreenBufferInfo(g_hConOut, &csbi)) |
| 3006 | { |
| 3007 | Rows = csbi.srWindow.Bottom - csbi.srWindow.Top + 1; |
| 3008 | Columns = csbi.srWindow.Right - csbi.srWindow.Left + 1; |
| 3009 | } |
| 3010 | else |
| 3011 | { |
| 3012 | Rows = 25; |
| 3013 | Columns = 80; |
| 3014 | } |
| 3015 | return OK; |
| 3016 | } |
| 3017 | |
| 3018 | /* |
| 3019 | * Set a console window to `xSize' * `ySize' |
| 3020 | */ |
| 3021 | static void |
| 3022 | ResizeConBufAndWindow( |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 3023 | HANDLE hConsole, |
| 3024 | int xSize, |
| 3025 | int ySize) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3026 | { |
| 3027 | CONSOLE_SCREEN_BUFFER_INFO csbi; /* hold current console buffer info */ |
| 3028 | SMALL_RECT srWindowRect; /* hold the new console size */ |
| 3029 | COORD coordScreen; |
| 3030 | |
| 3031 | #ifdef MCH_WRITE_DUMP |
| 3032 | if (fdDump) |
| 3033 | { |
| 3034 | fprintf(fdDump, "ResizeConBufAndWindow(%d, %d)\n", xSize, ySize); |
| 3035 | fflush(fdDump); |
| 3036 | } |
| 3037 | #endif |
| 3038 | |
| 3039 | /* get the largest size we can size the console window to */ |
| 3040 | coordScreen = GetLargestConsoleWindowSize(hConsole); |
| 3041 | |
| 3042 | /* define the new console window size and scroll position */ |
| 3043 | srWindowRect.Left = srWindowRect.Top = (SHORT) 0; |
| 3044 | srWindowRect.Right = (SHORT) (min(xSize, coordScreen.X) - 1); |
| 3045 | srWindowRect.Bottom = (SHORT) (min(ySize, coordScreen.Y) - 1); |
| 3046 | |
| 3047 | if (GetConsoleScreenBufferInfo(g_hConOut, &csbi)) |
| 3048 | { |
| 3049 | int sx, sy; |
| 3050 | |
| 3051 | sx = csbi.srWindow.Right - csbi.srWindow.Left + 1; |
| 3052 | sy = csbi.srWindow.Bottom - csbi.srWindow.Top + 1; |
| 3053 | if (sy < ySize || sx < xSize) |
| 3054 | { |
| 3055 | /* |
| 3056 | * Increasing number of lines/columns, do buffer first. |
| 3057 | * Use the maximal size in x and y direction. |
| 3058 | */ |
| 3059 | if (sy < ySize) |
| 3060 | coordScreen.Y = ySize; |
| 3061 | else |
| 3062 | coordScreen.Y = sy; |
| 3063 | if (sx < xSize) |
| 3064 | coordScreen.X = xSize; |
| 3065 | else |
| 3066 | coordScreen.X = sx; |
| 3067 | SetConsoleScreenBufferSize(hConsole, coordScreen); |
| 3068 | } |
| 3069 | } |
| 3070 | |
| 3071 | if (!SetConsoleWindowInfo(g_hConOut, TRUE, &srWindowRect)) |
| 3072 | { |
| 3073 | #ifdef MCH_WRITE_DUMP |
| 3074 | if (fdDump) |
| 3075 | { |
| 3076 | fprintf(fdDump, "SetConsoleWindowInfo failed: %lx\n", |
| 3077 | GetLastError()); |
| 3078 | fflush(fdDump); |
| 3079 | } |
| 3080 | #endif |
| 3081 | } |
| 3082 | |
| 3083 | /* define the new console buffer size */ |
| 3084 | coordScreen.X = xSize; |
| 3085 | coordScreen.Y = ySize; |
| 3086 | |
| 3087 | if (!SetConsoleScreenBufferSize(hConsole, coordScreen)) |
| 3088 | { |
| 3089 | #ifdef MCH_WRITE_DUMP |
| 3090 | if (fdDump) |
| 3091 | { |
| 3092 | fprintf(fdDump, "SetConsoleScreenBufferSize failed: %lx\n", |
| 3093 | GetLastError()); |
| 3094 | fflush(fdDump); |
| 3095 | } |
| 3096 | #endif |
| 3097 | } |
| 3098 | } |
| 3099 | |
| 3100 | |
| 3101 | /* |
| 3102 | * Set the console window to `Rows' * `Columns' |
| 3103 | */ |
| 3104 | void |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 3105 | mch_set_shellsize(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3106 | { |
| 3107 | COORD coordScreen; |
| 3108 | |
| 3109 | /* Don't change window size while still starting up */ |
| 3110 | if (suppress_winsize != 0) |
| 3111 | { |
| 3112 | suppress_winsize = 2; |
| 3113 | return; |
| 3114 | } |
| 3115 | |
| 3116 | if (term_console) |
| 3117 | { |
| 3118 | coordScreen = GetLargestConsoleWindowSize(g_hConOut); |
| 3119 | |
| 3120 | /* Clamp Rows and Columns to reasonable values */ |
| 3121 | if (Rows > coordScreen.Y) |
| 3122 | Rows = coordScreen.Y; |
| 3123 | if (Columns > coordScreen.X) |
| 3124 | Columns = coordScreen.X; |
| 3125 | |
| 3126 | ResizeConBufAndWindow(g_hConOut, Columns, Rows); |
| 3127 | } |
| 3128 | } |
| 3129 | |
| 3130 | /* |
| 3131 | * Rows and/or Columns has changed. |
| 3132 | */ |
| 3133 | void |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 3134 | mch_new_shellsize(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3135 | { |
| 3136 | set_scroll_region(0, 0, Columns - 1, Rows - 1); |
| 3137 | } |
| 3138 | |
| 3139 | |
| 3140 | /* |
| 3141 | * Called when started up, to set the winsize that was delayed. |
| 3142 | */ |
| 3143 | void |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 3144 | mch_set_winsize_now(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3145 | { |
| 3146 | if (suppress_winsize == 2) |
| 3147 | { |
| 3148 | suppress_winsize = 0; |
| 3149 | mch_set_shellsize(); |
| 3150 | shell_resized(); |
| 3151 | } |
| 3152 | suppress_winsize = 0; |
| 3153 | } |
| 3154 | #endif /* FEAT_GUI_W32 */ |
| 3155 | |
| 3156 | |
| 3157 | |
| 3158 | #if defined(FEAT_GUI_W32) || defined(PROTO) |
| 3159 | |
| 3160 | /* |
| 3161 | * Specialised version of system() for Win32 GUI mode. |
| 3162 | * This version proceeds as follows: |
| 3163 | * 1. Create a console window for use by the subprocess |
| 3164 | * 2. Run the subprocess (it gets the allocated console by default) |
| 3165 | * 3. Wait for the subprocess to terminate and get its exit code |
| 3166 | * 4. Prompt the user to press a key to close the console window |
| 3167 | */ |
| 3168 | static int |
| 3169 | mch_system(char *cmd, int options) |
| 3170 | { |
| 3171 | STARTUPINFO si; |
| 3172 | PROCESS_INFORMATION pi; |
| 3173 | DWORD ret = 0; |
| 3174 | HWND hwnd = GetFocus(); |
| 3175 | |
| 3176 | si.cb = sizeof(si); |
| 3177 | si.lpReserved = NULL; |
| 3178 | si.lpDesktop = NULL; |
| 3179 | si.lpTitle = NULL; |
| 3180 | si.dwFlags = STARTF_USESHOWWINDOW; |
| 3181 | /* |
| 3182 | * It's nicer to run a filter command in a minimized window, but in |
| 3183 | * Windows 95 this makes the command MUCH slower. We can't do it under |
| 3184 | * Win32s either as it stops the synchronous spawn workaround working. |
Bram Moolenaar | 96e5cee | 2010-11-24 12:35:21 +0100 | [diff] [blame] | 3185 | * Don't activate the window to keep focus on Vim. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3186 | */ |
| 3187 | if ((options & SHELL_DOOUT) && !mch_windows95() && !gui_is_win32s()) |
Bram Moolenaar | 96e5cee | 2010-11-24 12:35:21 +0100 | [diff] [blame] | 3188 | si.wShowWindow = SW_SHOWMINNOACTIVE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3189 | else |
| 3190 | si.wShowWindow = SW_SHOWNORMAL; |
| 3191 | si.cbReserved2 = 0; |
| 3192 | si.lpReserved2 = NULL; |
| 3193 | |
| 3194 | /* There is a strange error on Windows 95 when using "c:\\command.com". |
| 3195 | * When the "c:\\" is left out it works OK...? */ |
| 3196 | if (mch_windows95() |
| 3197 | && (STRNICMP(cmd, "c:/command.com", 14) == 0 |
| 3198 | || STRNICMP(cmd, "c:\\command.com", 14) == 0)) |
| 3199 | cmd += 3; |
| 3200 | |
| 3201 | /* Now, run the command */ |
| 3202 | CreateProcess(NULL, /* Executable name */ |
| 3203 | cmd, /* Command to execute */ |
| 3204 | NULL, /* Process security attributes */ |
| 3205 | NULL, /* Thread security attributes */ |
| 3206 | FALSE, /* Inherit handles */ |
| 3207 | CREATE_DEFAULT_ERROR_MODE | /* Creation flags */ |
| 3208 | CREATE_NEW_CONSOLE, |
| 3209 | NULL, /* Environment */ |
| 3210 | NULL, /* Current directory */ |
| 3211 | &si, /* Startup information */ |
| 3212 | &pi); /* Process information */ |
| 3213 | |
| 3214 | |
| 3215 | /* Wait for the command to terminate before continuing */ |
| 3216 | if (g_PlatformId != VER_PLATFORM_WIN32s) |
| 3217 | { |
| 3218 | #ifdef FEAT_GUI |
| 3219 | int delay = 1; |
| 3220 | |
| 3221 | /* Keep updating the window while waiting for the shell to finish. */ |
| 3222 | for (;;) |
| 3223 | { |
| 3224 | MSG msg; |
| 3225 | |
| 3226 | if (PeekMessage(&msg, (HWND)NULL, 0, 0, PM_REMOVE)) |
| 3227 | { |
| 3228 | TranslateMessage(&msg); |
| 3229 | DispatchMessage(&msg); |
| 3230 | } |
| 3231 | if (WaitForSingleObject(pi.hProcess, delay) != WAIT_TIMEOUT) |
| 3232 | break; |
| 3233 | |
| 3234 | /* We start waiting for a very short time and then increase it, so |
| 3235 | * that we respond quickly when the process is quick, and don't |
| 3236 | * consume too much overhead when it's slow. */ |
| 3237 | if (delay < 50) |
| 3238 | delay += 10; |
| 3239 | } |
| 3240 | #else |
| 3241 | WaitForSingleObject(pi.hProcess, INFINITE); |
| 3242 | #endif |
| 3243 | |
| 3244 | /* Get the command exit code */ |
| 3245 | GetExitCodeProcess(pi.hProcess, &ret); |
| 3246 | } |
| 3247 | else |
| 3248 | { |
| 3249 | /* |
| 3250 | * This ugly code is the only quick way of performing |
| 3251 | * a synchronous spawn under Win32s. Yuk. |
| 3252 | */ |
| 3253 | num_windows = 0; |
| 3254 | EnumWindows(win32ssynch_cb, 0); |
| 3255 | old_num_windows = num_windows; |
| 3256 | do |
| 3257 | { |
| 3258 | Sleep(1000); |
| 3259 | num_windows = 0; |
| 3260 | EnumWindows(win32ssynch_cb, 0); |
| 3261 | } while (num_windows == old_num_windows); |
| 3262 | ret = 0; |
| 3263 | } |
| 3264 | |
| 3265 | /* Close the handles to the subprocess, so that it goes away */ |
| 3266 | CloseHandle(pi.hThread); |
| 3267 | CloseHandle(pi.hProcess); |
| 3268 | |
| 3269 | /* Try to get input focus back. Doesn't always work though. */ |
| 3270 | PostMessage(hwnd, WM_SETFOCUS, 0, 0); |
| 3271 | |
| 3272 | return ret; |
| 3273 | } |
| 3274 | #else |
| 3275 | |
| 3276 | # define mch_system(c, o) system(c) |
| 3277 | |
| 3278 | #endif |
| 3279 | |
| 3280 | /* |
| 3281 | * Either execute a command by calling the shell or start a new shell |
| 3282 | */ |
| 3283 | int |
| 3284 | mch_call_shell( |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 3285 | char_u *cmd, |
| 3286 | int options) /* SHELL_*, see vim.h */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3287 | { |
| 3288 | int x = 0; |
| 3289 | int tmode = cur_tmode; |
| 3290 | #ifdef FEAT_TITLE |
| 3291 | char szShellTitle[512]; |
| 3292 | |
| 3293 | /* Change the title to reflect that we are in a subshell. */ |
| 3294 | if (GetConsoleTitle(szShellTitle, sizeof(szShellTitle) - 4) > 0) |
| 3295 | { |
| 3296 | if (cmd == NULL) |
| 3297 | strcat(szShellTitle, " :sh"); |
| 3298 | else |
| 3299 | { |
| 3300 | strcat(szShellTitle, " - !"); |
| 3301 | if ((strlen(szShellTitle) + strlen(cmd) < sizeof(szShellTitle))) |
| 3302 | strcat(szShellTitle, cmd); |
| 3303 | } |
| 3304 | mch_settitle(szShellTitle, NULL); |
| 3305 | } |
| 3306 | #endif |
| 3307 | |
| 3308 | out_flush(); |
| 3309 | |
| 3310 | #ifdef MCH_WRITE_DUMP |
| 3311 | if (fdDump) |
| 3312 | { |
| 3313 | fprintf(fdDump, "mch_call_shell(\"%s\", %d)\n", cmd, options); |
| 3314 | fflush(fdDump); |
| 3315 | } |
| 3316 | #endif |
| 3317 | |
| 3318 | /* |
| 3319 | * Catch all deadly signals while running the external command, because a |
| 3320 | * CTRL-C, Ctrl-Break or illegal instruction might otherwise kill us. |
| 3321 | */ |
| 3322 | signal(SIGINT, SIG_IGN); |
| 3323 | #if defined(__GNUC__) && !defined(__MINGW32__) |
| 3324 | signal(SIGKILL, SIG_IGN); |
| 3325 | #else |
| 3326 | signal(SIGBREAK, SIG_IGN); |
| 3327 | #endif |
| 3328 | signal(SIGILL, SIG_IGN); |
| 3329 | signal(SIGFPE, SIG_IGN); |
| 3330 | signal(SIGSEGV, SIG_IGN); |
| 3331 | signal(SIGTERM, SIG_IGN); |
| 3332 | signal(SIGABRT, SIG_IGN); |
| 3333 | |
| 3334 | if (options & SHELL_COOKED) |
| 3335 | settmode(TMODE_COOK); /* set to normal mode */ |
| 3336 | |
| 3337 | if (cmd == NULL) |
| 3338 | { |
| 3339 | x = mch_system(p_sh, options); |
| 3340 | } |
| 3341 | else |
| 3342 | { |
| 3343 | /* we use "command" or "cmd" to start the shell; slow but easy */ |
| 3344 | char_u *newcmd; |
Bram Moolenaar | 0fde290 | 2008-03-16 13:54:13 +0000 | [diff] [blame] | 3345 | long_u cmdlen = ( |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3346 | #ifdef FEAT_GUI_W32 |
| 3347 | STRLEN(vimrun_path) + |
| 3348 | #endif |
Bram Moolenaar | 0fde290 | 2008-03-16 13:54:13 +0000 | [diff] [blame] | 3349 | STRLEN(p_sh) + STRLEN(p_shcf) + STRLEN(cmd) + 10); |
| 3350 | |
| 3351 | newcmd = lalloc(cmdlen, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3352 | if (newcmd != NULL) |
| 3353 | { |
| 3354 | char_u *cmdbase = (*cmd == '"' ? cmd + 1 : cmd); |
| 3355 | |
| 3356 | if ((STRNICMP(cmdbase, "start", 5) == 0) && vim_iswhite(cmdbase[5])) |
| 3357 | { |
| 3358 | STARTUPINFO si; |
| 3359 | PROCESS_INFORMATION pi; |
| 3360 | |
| 3361 | si.cb = sizeof(si); |
| 3362 | si.lpReserved = NULL; |
| 3363 | si.lpDesktop = NULL; |
| 3364 | si.lpTitle = NULL; |
| 3365 | si.dwFlags = 0; |
| 3366 | si.cbReserved2 = 0; |
| 3367 | si.lpReserved2 = NULL; |
| 3368 | |
| 3369 | cmdbase = skipwhite(cmdbase + 5); |
| 3370 | if ((STRNICMP(cmdbase, "/min", 4) == 0) |
| 3371 | && vim_iswhite(cmdbase[4])) |
| 3372 | { |
| 3373 | cmdbase = skipwhite(cmdbase + 4); |
| 3374 | si.dwFlags = STARTF_USESHOWWINDOW; |
| 3375 | si.wShowWindow = SW_SHOWMINNOACTIVE; |
| 3376 | } |
| 3377 | |
| 3378 | /* When the command is in double quotes, but 'shellxquote' is |
| 3379 | * empty, keep the double quotes around the command. |
| 3380 | * Otherwise remove the double quotes, they aren't needed |
| 3381 | * here, because we don't use a shell to run the command. */ |
| 3382 | if (*cmd == '"' && *p_sxq == NUL) |
| 3383 | { |
| 3384 | newcmd[0] = '"'; |
| 3385 | STRCPY(newcmd + 1, cmdbase); |
| 3386 | } |
| 3387 | else |
| 3388 | { |
| 3389 | STRCPY(newcmd, cmdbase); |
| 3390 | if (*cmd == '"' && *newcmd != NUL) |
| 3391 | newcmd[STRLEN(newcmd) - 1] = NUL; |
| 3392 | } |
| 3393 | |
| 3394 | /* |
| 3395 | * Now, start the command as a process, so that it doesn't |
| 3396 | * inherit our handles which causes unpleasant dangling swap |
| 3397 | * files if we exit before the spawned process |
| 3398 | */ |
| 3399 | if (CreateProcess (NULL, // Executable name |
| 3400 | newcmd, // Command to execute |
| 3401 | NULL, // Process security attributes |
| 3402 | NULL, // Thread security attributes |
| 3403 | FALSE, // Inherit handles |
| 3404 | CREATE_NEW_CONSOLE, // Creation flags |
| 3405 | NULL, // Environment |
| 3406 | NULL, // Current directory |
| 3407 | &si, // Startup information |
| 3408 | &pi)) // Process information |
| 3409 | x = 0; |
| 3410 | else |
| 3411 | { |
| 3412 | x = -1; |
| 3413 | #ifdef FEAT_GUI_W32 |
| 3414 | EMSG(_("E371: Command not found")); |
| 3415 | #endif |
| 3416 | } |
| 3417 | /* Close the handles to the subprocess, so that it goes away */ |
| 3418 | CloseHandle(pi.hThread); |
| 3419 | CloseHandle(pi.hProcess); |
| 3420 | } |
| 3421 | else |
| 3422 | { |
| 3423 | #if defined(FEAT_GUI_W32) |
| 3424 | if (need_vimrun_warning) |
| 3425 | { |
| 3426 | MessageBox(NULL, |
| 3427 | _("VIMRUN.EXE not found in your $PATH.\n" |
| 3428 | "External commands will not pause after completion.\n" |
| 3429 | "See :help win32-vimrun for more information."), |
| 3430 | _("Vim Warning"), |
| 3431 | MB_ICONWARNING); |
| 3432 | need_vimrun_warning = FALSE; |
| 3433 | } |
| 3434 | if (!s_dont_use_vimrun) |
| 3435 | /* Use vimrun to execute the command. It opens a console |
| 3436 | * window, which can be closed without killing Vim. */ |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 3437 | vim_snprintf((char *)newcmd, cmdlen, "%s%s%s %s %s", |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3438 | vimrun_path, |
| 3439 | (msg_silent != 0 || (options & SHELL_DOOUT)) |
| 3440 | ? "-s " : "", |
| 3441 | p_sh, p_shcf, cmd); |
| 3442 | else |
| 3443 | #endif |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 3444 | vim_snprintf((char *)newcmd, cmdlen, "%s %s %s", |
Bram Moolenaar | 0fde290 | 2008-03-16 13:54:13 +0000 | [diff] [blame] | 3445 | p_sh, p_shcf, cmd); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3446 | x = mch_system((char *)newcmd, options); |
| 3447 | } |
| 3448 | vim_free(newcmd); |
| 3449 | } |
| 3450 | } |
| 3451 | |
| 3452 | if (tmode == TMODE_RAW) |
| 3453 | settmode(TMODE_RAW); /* set to raw mode */ |
| 3454 | |
| 3455 | /* Print the return value, unless "vimrun" was used. */ |
| 3456 | if (x != 0 && !(options & SHELL_SILENT) && !emsg_silent |
| 3457 | #if defined(FEAT_GUI_W32) |
| 3458 | && ((options & SHELL_DOOUT) || s_dont_use_vimrun) |
| 3459 | #endif |
| 3460 | ) |
| 3461 | { |
| 3462 | smsg(_("shell returned %d"), x); |
| 3463 | msg_putchar('\n'); |
| 3464 | } |
| 3465 | #ifdef FEAT_TITLE |
| 3466 | resettitle(); |
| 3467 | #endif |
| 3468 | |
| 3469 | signal(SIGINT, SIG_DFL); |
| 3470 | #if defined(__GNUC__) && !defined(__MINGW32__) |
| 3471 | signal(SIGKILL, SIG_DFL); |
| 3472 | #else |
| 3473 | signal(SIGBREAK, SIG_DFL); |
| 3474 | #endif |
| 3475 | signal(SIGILL, SIG_DFL); |
| 3476 | signal(SIGFPE, SIG_DFL); |
| 3477 | signal(SIGSEGV, SIG_DFL); |
| 3478 | signal(SIGTERM, SIG_DFL); |
| 3479 | signal(SIGABRT, SIG_DFL); |
| 3480 | |
| 3481 | return x; |
| 3482 | } |
| 3483 | |
| 3484 | |
| 3485 | #ifndef FEAT_GUI_W32 |
| 3486 | |
| 3487 | /* |
| 3488 | * Start termcap mode |
| 3489 | */ |
| 3490 | static void |
| 3491 | termcap_mode_start(void) |
| 3492 | { |
| 3493 | DWORD cmodein; |
| 3494 | |
| 3495 | if (g_fTermcapMode) |
| 3496 | return; |
| 3497 | |
| 3498 | SaveConsoleBuffer(&g_cbNonTermcap); |
| 3499 | |
| 3500 | if (g_cbTermcap.IsValid) |
| 3501 | { |
| 3502 | /* |
| 3503 | * We've been in termcap mode before. Restore certain screen |
| 3504 | * characteristics, including the buffer size and the window |
| 3505 | * size. Since we will be redrawing the screen, we don't need |
| 3506 | * to restore the actual contents of the buffer. |
| 3507 | */ |
| 3508 | RestoreConsoleBuffer(&g_cbTermcap, FALSE); |
| 3509 | SetConsoleWindowInfo(g_hConOut, TRUE, &g_cbTermcap.Info.srWindow); |
| 3510 | Rows = g_cbTermcap.Info.dwSize.Y; |
| 3511 | Columns = g_cbTermcap.Info.dwSize.X; |
| 3512 | } |
| 3513 | else |
| 3514 | { |
| 3515 | /* |
| 3516 | * This is our first time entering termcap mode. Clear the console |
| 3517 | * screen buffer, and resize the buffer to match the current window |
| 3518 | * size. We will use this as the size of our editing environment. |
| 3519 | */ |
| 3520 | ClearConsoleBuffer(g_attrCurrent); |
| 3521 | ResizeConBufAndWindow(g_hConOut, Columns, Rows); |
| 3522 | } |
| 3523 | |
| 3524 | #ifdef FEAT_TITLE |
| 3525 | resettitle(); |
| 3526 | #endif |
| 3527 | |
| 3528 | GetConsoleMode(g_hConIn, &cmodein); |
| 3529 | #ifdef FEAT_MOUSE |
| 3530 | if (g_fMouseActive) |
| 3531 | cmodein |= ENABLE_MOUSE_INPUT; |
| 3532 | else |
| 3533 | cmodein &= ~ENABLE_MOUSE_INPUT; |
| 3534 | #endif |
| 3535 | cmodein |= ENABLE_WINDOW_INPUT; |
| 3536 | SetConsoleMode(g_hConIn, cmodein); |
| 3537 | |
| 3538 | redraw_later_clear(); |
| 3539 | g_fTermcapMode = TRUE; |
| 3540 | } |
| 3541 | |
| 3542 | |
| 3543 | /* |
| 3544 | * End termcap mode |
| 3545 | */ |
| 3546 | static void |
| 3547 | termcap_mode_end(void) |
| 3548 | { |
| 3549 | DWORD cmodein; |
| 3550 | ConsoleBuffer *cb; |
| 3551 | COORD coord; |
| 3552 | DWORD dwDummy; |
| 3553 | |
| 3554 | if (!g_fTermcapMode) |
| 3555 | return; |
| 3556 | |
| 3557 | SaveConsoleBuffer(&g_cbTermcap); |
| 3558 | |
| 3559 | GetConsoleMode(g_hConIn, &cmodein); |
| 3560 | cmodein &= ~(ENABLE_MOUSE_INPUT | ENABLE_WINDOW_INPUT); |
| 3561 | SetConsoleMode(g_hConIn, cmodein); |
| 3562 | |
| 3563 | #ifdef FEAT_RESTORE_ORIG_SCREEN |
| 3564 | cb = exiting ? &g_cbOrig : &g_cbNonTermcap; |
| 3565 | #else |
| 3566 | cb = &g_cbNonTermcap; |
| 3567 | #endif |
| 3568 | RestoreConsoleBuffer(cb, p_rs); |
| 3569 | SetConsoleCursorInfo(g_hConOut, &g_cci); |
| 3570 | |
| 3571 | if (p_rs || exiting) |
| 3572 | { |
| 3573 | /* |
| 3574 | * Clear anything that happens to be on the current line. |
| 3575 | */ |
| 3576 | coord.X = 0; |
| 3577 | coord.Y = (SHORT) (p_rs ? cb->Info.dwCursorPosition.Y : (Rows - 1)); |
| 3578 | FillConsoleOutputCharacter(g_hConOut, ' ', |
| 3579 | cb->Info.dwSize.X, coord, &dwDummy); |
| 3580 | /* |
| 3581 | * The following is just for aesthetics. If we are exiting without |
| 3582 | * restoring the screen, then we want to have a prompt string |
| 3583 | * appear at the bottom line. However, the command interpreter |
| 3584 | * seems to always advance the cursor one line before displaying |
| 3585 | * the prompt string, which causes the screen to scroll. To |
| 3586 | * counter this, move the cursor up one line before exiting. |
| 3587 | */ |
| 3588 | if (exiting && !p_rs) |
| 3589 | coord.Y--; |
| 3590 | /* |
| 3591 | * Position the cursor at the leftmost column of the desired row. |
| 3592 | */ |
| 3593 | SetConsoleCursorPosition(g_hConOut, coord); |
| 3594 | } |
| 3595 | |
| 3596 | g_fTermcapMode = FALSE; |
| 3597 | } |
| 3598 | #endif /* FEAT_GUI_W32 */ |
| 3599 | |
| 3600 | |
| 3601 | #ifdef FEAT_GUI_W32 |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3602 | /*ARGSUSED*/ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3603 | void |
| 3604 | mch_write( |
| 3605 | char_u *s, |
| 3606 | int len) |
| 3607 | { |
| 3608 | /* never used */ |
| 3609 | } |
| 3610 | |
| 3611 | #else |
| 3612 | |
| 3613 | /* |
| 3614 | * clear `n' chars, starting from `coord' |
| 3615 | */ |
| 3616 | static void |
| 3617 | clear_chars( |
| 3618 | COORD coord, |
| 3619 | DWORD n) |
| 3620 | { |
| 3621 | DWORD dwDummy; |
| 3622 | |
| 3623 | FillConsoleOutputCharacter(g_hConOut, ' ', n, coord, &dwDummy); |
| 3624 | FillConsoleOutputAttribute(g_hConOut, g_attrCurrent, n, coord, &dwDummy); |
| 3625 | } |
| 3626 | |
| 3627 | |
| 3628 | /* |
| 3629 | * Clear the screen |
| 3630 | */ |
| 3631 | static void |
| 3632 | clear_screen(void) |
| 3633 | { |
| 3634 | g_coord.X = g_coord.Y = 0; |
| 3635 | clear_chars(g_coord, Rows * Columns); |
| 3636 | } |
| 3637 | |
| 3638 | |
| 3639 | /* |
| 3640 | * Clear to end of display |
| 3641 | */ |
| 3642 | static void |
| 3643 | clear_to_end_of_display(void) |
| 3644 | { |
| 3645 | clear_chars(g_coord, (Rows - g_coord.Y - 1) |
| 3646 | * Columns + (Columns - g_coord.X)); |
| 3647 | } |
| 3648 | |
| 3649 | |
| 3650 | /* |
| 3651 | * Clear to end of line |
| 3652 | */ |
| 3653 | static void |
| 3654 | clear_to_end_of_line(void) |
| 3655 | { |
| 3656 | clear_chars(g_coord, Columns - g_coord.X); |
| 3657 | } |
| 3658 | |
| 3659 | |
| 3660 | /* |
| 3661 | * Scroll the scroll region up by `cLines' lines |
| 3662 | */ |
| 3663 | static void |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 3664 | scroll(unsigned cLines) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3665 | { |
| 3666 | COORD oldcoord = g_coord; |
| 3667 | |
| 3668 | gotoxy(g_srScrollRegion.Left + 1, g_srScrollRegion.Top + 1); |
| 3669 | delete_lines(cLines); |
| 3670 | |
| 3671 | g_coord = oldcoord; |
| 3672 | } |
| 3673 | |
| 3674 | |
| 3675 | /* |
| 3676 | * Set the scroll region |
| 3677 | */ |
| 3678 | static void |
| 3679 | set_scroll_region( |
| 3680 | unsigned left, |
| 3681 | unsigned top, |
| 3682 | unsigned right, |
| 3683 | unsigned bottom) |
| 3684 | { |
| 3685 | if (left >= right |
| 3686 | || top >= bottom |
| 3687 | || right > (unsigned) Columns - 1 |
| 3688 | || bottom > (unsigned) Rows - 1) |
| 3689 | return; |
| 3690 | |
| 3691 | g_srScrollRegion.Left = left; |
| 3692 | g_srScrollRegion.Top = top; |
| 3693 | g_srScrollRegion.Right = right; |
| 3694 | g_srScrollRegion.Bottom = bottom; |
| 3695 | } |
| 3696 | |
| 3697 | |
| 3698 | /* |
| 3699 | * Insert `cLines' lines at the current cursor position |
| 3700 | */ |
| 3701 | static void |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 3702 | insert_lines(unsigned cLines) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3703 | { |
| 3704 | SMALL_RECT source; |
| 3705 | COORD dest; |
| 3706 | CHAR_INFO fill; |
| 3707 | |
| 3708 | dest.X = 0; |
| 3709 | dest.Y = g_coord.Y + cLines; |
| 3710 | |
| 3711 | source.Left = 0; |
| 3712 | source.Top = g_coord.Y; |
| 3713 | source.Right = g_srScrollRegion.Right; |
| 3714 | source.Bottom = g_srScrollRegion.Bottom - cLines; |
| 3715 | |
| 3716 | fill.Char.AsciiChar = ' '; |
| 3717 | fill.Attributes = g_attrCurrent; |
| 3718 | |
| 3719 | ScrollConsoleScreenBuffer(g_hConOut, &source, NULL, dest, &fill); |
| 3720 | |
| 3721 | /* Here we have to deal with a win32 console flake: If the scroll |
| 3722 | * region looks like abc and we scroll c to a and fill with d we get |
| 3723 | * cbd... if we scroll block c one line at a time to a, we get cdd... |
| 3724 | * vim expects cdd consistently... So we have to deal with that |
| 3725 | * here... (this also occurs scrolling the same way in the other |
| 3726 | * direction). */ |
| 3727 | |
| 3728 | if (source.Bottom < dest.Y) |
| 3729 | { |
| 3730 | COORD coord; |
| 3731 | |
| 3732 | coord.X = 0; |
| 3733 | coord.Y = source.Bottom; |
| 3734 | clear_chars(coord, Columns * (dest.Y - source.Bottom)); |
| 3735 | } |
| 3736 | } |
| 3737 | |
| 3738 | |
| 3739 | /* |
| 3740 | * Delete `cLines' lines at the current cursor position |
| 3741 | */ |
| 3742 | static void |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 3743 | delete_lines(unsigned cLines) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3744 | { |
| 3745 | SMALL_RECT source; |
| 3746 | COORD dest; |
| 3747 | CHAR_INFO fill; |
| 3748 | int nb; |
| 3749 | |
| 3750 | dest.X = 0; |
| 3751 | dest.Y = g_coord.Y; |
| 3752 | |
| 3753 | source.Left = 0; |
| 3754 | source.Top = g_coord.Y + cLines; |
| 3755 | source.Right = g_srScrollRegion.Right; |
| 3756 | source.Bottom = g_srScrollRegion.Bottom; |
| 3757 | |
| 3758 | fill.Char.AsciiChar = ' '; |
| 3759 | fill.Attributes = g_attrCurrent; |
| 3760 | |
| 3761 | ScrollConsoleScreenBuffer(g_hConOut, &source, NULL, dest, &fill); |
| 3762 | |
| 3763 | /* Here we have to deal with a win32 console flake: If the scroll |
| 3764 | * region looks like abc and we scroll c to a and fill with d we get |
| 3765 | * cbd... if we scroll block c one line at a time to a, we get cdd... |
| 3766 | * vim expects cdd consistently... So we have to deal with that |
| 3767 | * here... (this also occurs scrolling the same way in the other |
| 3768 | * direction). */ |
| 3769 | |
| 3770 | nb = dest.Y + (source.Bottom - source.Top) + 1; |
| 3771 | |
| 3772 | if (nb < source.Top) |
| 3773 | { |
| 3774 | COORD coord; |
| 3775 | |
| 3776 | coord.X = 0; |
| 3777 | coord.Y = nb; |
| 3778 | clear_chars(coord, Columns * (source.Top - nb)); |
| 3779 | } |
| 3780 | } |
| 3781 | |
| 3782 | |
| 3783 | /* |
| 3784 | * Set the cursor position |
| 3785 | */ |
| 3786 | static void |
| 3787 | gotoxy( |
| 3788 | unsigned x, |
| 3789 | unsigned y) |
| 3790 | { |
| 3791 | if (x < 1 || x > (unsigned)Columns || y < 1 || y > (unsigned)Rows) |
| 3792 | return; |
| 3793 | |
| 3794 | /* external cursor coords are 1-based; internal are 0-based */ |
| 3795 | g_coord.X = x - 1; |
| 3796 | g_coord.Y = y - 1; |
| 3797 | SetConsoleCursorPosition(g_hConOut, g_coord); |
| 3798 | } |
| 3799 | |
| 3800 | |
| 3801 | /* |
| 3802 | * Set the current text attribute = (foreground | background) |
| 3803 | * See ../doc/os_win32.txt for the numbers. |
| 3804 | */ |
| 3805 | static void |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 3806 | textattr(WORD wAttr) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3807 | { |
| 3808 | g_attrCurrent = wAttr; |
| 3809 | |
| 3810 | SetConsoleTextAttribute(g_hConOut, wAttr); |
| 3811 | } |
| 3812 | |
| 3813 | |
| 3814 | static void |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 3815 | textcolor(WORD wAttr) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3816 | { |
| 3817 | g_attrCurrent = (g_attrCurrent & 0xf0) + wAttr; |
| 3818 | |
| 3819 | SetConsoleTextAttribute(g_hConOut, g_attrCurrent); |
| 3820 | } |
| 3821 | |
| 3822 | |
| 3823 | static void |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 3824 | textbackground(WORD wAttr) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3825 | { |
| 3826 | g_attrCurrent = (g_attrCurrent & 0x0f) + (wAttr << 4); |
| 3827 | |
| 3828 | SetConsoleTextAttribute(g_hConOut, g_attrCurrent); |
| 3829 | } |
| 3830 | |
| 3831 | |
| 3832 | /* |
| 3833 | * restore the default text attribute (whatever we started with) |
| 3834 | */ |
| 3835 | static void |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 3836 | normvideo(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3837 | { |
| 3838 | textattr(g_attrDefault); |
| 3839 | } |
| 3840 | |
| 3841 | |
| 3842 | static WORD g_attrPreStandout = 0; |
| 3843 | |
| 3844 | /* |
| 3845 | * Make the text standout, by brightening it |
| 3846 | */ |
| 3847 | static void |
| 3848 | standout(void) |
| 3849 | { |
| 3850 | g_attrPreStandout = g_attrCurrent; |
| 3851 | textattr((WORD) (g_attrCurrent|FOREGROUND_INTENSITY|BACKGROUND_INTENSITY)); |
| 3852 | } |
| 3853 | |
| 3854 | |
| 3855 | /* |
| 3856 | * Turn off standout mode |
| 3857 | */ |
| 3858 | static void |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 3859 | standend(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3860 | { |
| 3861 | if (g_attrPreStandout) |
| 3862 | { |
| 3863 | textattr(g_attrPreStandout); |
| 3864 | g_attrPreStandout = 0; |
| 3865 | } |
| 3866 | } |
| 3867 | |
| 3868 | |
| 3869 | /* |
Bram Moolenaar | ff1d0d4 | 2007-05-10 17:24:16 +0000 | [diff] [blame] | 3870 | * Set normal fg/bg color, based on T_ME. Called when t_me has been set. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3871 | */ |
| 3872 | void |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 3873 | mch_set_normal_colors(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3874 | { |
| 3875 | char_u *p; |
| 3876 | int n; |
| 3877 | |
| 3878 | cterm_normal_fg_color = (g_attrDefault & 0xf) + 1; |
| 3879 | cterm_normal_bg_color = ((g_attrDefault >> 4) & 0xf) + 1; |
| 3880 | if (T_ME[0] == ESC && T_ME[1] == '|') |
| 3881 | { |
| 3882 | p = T_ME + 2; |
| 3883 | n = getdigits(&p); |
| 3884 | if (*p == 'm' && n > 0) |
| 3885 | { |
| 3886 | cterm_normal_fg_color = (n & 0xf) + 1; |
| 3887 | cterm_normal_bg_color = ((n >> 4) & 0xf) + 1; |
| 3888 | } |
| 3889 | } |
| 3890 | } |
| 3891 | |
| 3892 | |
| 3893 | /* |
| 3894 | * visual bell: flash the screen |
| 3895 | */ |
| 3896 | static void |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 3897 | visual_bell(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3898 | { |
| 3899 | COORD coordOrigin = {0, 0}; |
| 3900 | WORD attrFlash = ~g_attrCurrent & 0xff; |
| 3901 | |
| 3902 | DWORD dwDummy; |
| 3903 | LPWORD oldattrs = (LPWORD)alloc(Rows * Columns * sizeof(WORD)); |
| 3904 | |
| 3905 | if (oldattrs == NULL) |
| 3906 | return; |
| 3907 | ReadConsoleOutputAttribute(g_hConOut, oldattrs, Rows * Columns, |
| 3908 | coordOrigin, &dwDummy); |
| 3909 | FillConsoleOutputAttribute(g_hConOut, attrFlash, Rows * Columns, |
| 3910 | coordOrigin, &dwDummy); |
| 3911 | |
| 3912 | Sleep(15); /* wait for 15 msec */ |
| 3913 | WriteConsoleOutputAttribute(g_hConOut, oldattrs, Rows * Columns, |
| 3914 | coordOrigin, &dwDummy); |
| 3915 | vim_free(oldattrs); |
| 3916 | } |
| 3917 | |
| 3918 | |
| 3919 | /* |
| 3920 | * Make the cursor visible or invisible |
| 3921 | */ |
| 3922 | static void |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 3923 | cursor_visible(BOOL fVisible) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3924 | { |
| 3925 | s_cursor_visible = fVisible; |
| 3926 | #ifdef MCH_CURSOR_SHAPE |
| 3927 | mch_update_cursor(); |
| 3928 | #endif |
| 3929 | } |
| 3930 | |
| 3931 | |
| 3932 | /* |
| 3933 | * write `cchToWrite' characters in `pchBuf' to the screen |
| 3934 | * Returns the number of characters actually written (at least one). |
| 3935 | */ |
| 3936 | static BOOL |
| 3937 | write_chars( |
| 3938 | LPCSTR pchBuf, |
| 3939 | DWORD cchToWrite) |
| 3940 | { |
| 3941 | COORD coord = g_coord; |
| 3942 | DWORD written; |
| 3943 | |
| 3944 | FillConsoleOutputAttribute(g_hConOut, g_attrCurrent, cchToWrite, |
| 3945 | coord, &written); |
| 3946 | /* When writing fails or didn't write a single character, pretend one |
| 3947 | * character was written, otherwise we get stuck. */ |
| 3948 | if (WriteConsoleOutputCharacter(g_hConOut, pchBuf, cchToWrite, |
| 3949 | coord, &written) == 0 |
| 3950 | || written == 0) |
| 3951 | written = 1; |
| 3952 | |
| 3953 | g_coord.X += (SHORT) written; |
| 3954 | |
| 3955 | while (g_coord.X > g_srScrollRegion.Right) |
| 3956 | { |
| 3957 | g_coord.X -= (SHORT) Columns; |
| 3958 | if (g_coord.Y < g_srScrollRegion.Bottom) |
| 3959 | g_coord.Y++; |
| 3960 | } |
| 3961 | |
| 3962 | gotoxy(g_coord.X + 1, g_coord.Y + 1); |
| 3963 | |
| 3964 | return written; |
| 3965 | } |
| 3966 | |
| 3967 | |
| 3968 | /* |
| 3969 | * mch_write(): write the output buffer to the screen, translating ESC |
| 3970 | * sequences into calls to console output routines. |
| 3971 | */ |
| 3972 | void |
| 3973 | mch_write( |
| 3974 | char_u *s, |
| 3975 | int len) |
| 3976 | { |
| 3977 | s[len] = NUL; |
| 3978 | |
| 3979 | if (!term_console) |
| 3980 | { |
| 3981 | write(1, s, (unsigned)len); |
| 3982 | return; |
| 3983 | } |
| 3984 | |
| 3985 | /* translate ESC | sequences into faked bios calls */ |
| 3986 | while (len--) |
| 3987 | { |
| 3988 | /* optimization: use one single write_chars for runs of text, |
| 3989 | * rather than once per character It ain't curses, but it helps. */ |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 3990 | DWORD prefix = (DWORD)strcspn(s, "\n\r\b\a\033"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3991 | |
| 3992 | if (p_wd) |
| 3993 | { |
| 3994 | WaitForChar(p_wd); |
| 3995 | if (prefix != 0) |
| 3996 | prefix = 1; |
| 3997 | } |
| 3998 | |
| 3999 | if (prefix != 0) |
| 4000 | { |
| 4001 | DWORD nWritten; |
| 4002 | |
| 4003 | nWritten = write_chars(s, prefix); |
| 4004 | #ifdef MCH_WRITE_DUMP |
| 4005 | if (fdDump) |
| 4006 | { |
| 4007 | fputc('>', fdDump); |
| 4008 | fwrite(s, sizeof(char_u), nWritten, fdDump); |
| 4009 | fputs("<\n", fdDump); |
| 4010 | } |
| 4011 | #endif |
| 4012 | len -= (nWritten - 1); |
| 4013 | s += nWritten; |
| 4014 | } |
| 4015 | else if (s[0] == '\n') |
| 4016 | { |
| 4017 | /* \n, newline: go to the beginning of the next line or scroll */ |
| 4018 | if (g_coord.Y == g_srScrollRegion.Bottom) |
| 4019 | { |
| 4020 | scroll(1); |
| 4021 | gotoxy(g_srScrollRegion.Left + 1, g_srScrollRegion.Bottom + 1); |
| 4022 | } |
| 4023 | else |
| 4024 | { |
| 4025 | gotoxy(g_srScrollRegion.Left + 1, g_coord.Y + 2); |
| 4026 | } |
| 4027 | #ifdef MCH_WRITE_DUMP |
| 4028 | if (fdDump) |
| 4029 | fputs("\\n\n", fdDump); |
| 4030 | #endif |
| 4031 | s++; |
| 4032 | } |
| 4033 | else if (s[0] == '\r') |
| 4034 | { |
| 4035 | /* \r, carriage return: go to beginning of line */ |
| 4036 | gotoxy(g_srScrollRegion.Left+1, g_coord.Y + 1); |
| 4037 | #ifdef MCH_WRITE_DUMP |
| 4038 | if (fdDump) |
| 4039 | fputs("\\r\n", fdDump); |
| 4040 | #endif |
| 4041 | s++; |
| 4042 | } |
| 4043 | else if (s[0] == '\b') |
| 4044 | { |
| 4045 | /* \b, backspace: move cursor one position left */ |
| 4046 | if (g_coord.X > g_srScrollRegion.Left) |
| 4047 | g_coord.X--; |
| 4048 | else if (g_coord.Y > g_srScrollRegion.Top) |
| 4049 | { |
| 4050 | g_coord.X = g_srScrollRegion.Right; |
| 4051 | g_coord.Y--; |
| 4052 | } |
| 4053 | gotoxy(g_coord.X + 1, g_coord.Y + 1); |
| 4054 | #ifdef MCH_WRITE_DUMP |
| 4055 | if (fdDump) |
| 4056 | fputs("\\b\n", fdDump); |
| 4057 | #endif |
| 4058 | s++; |
| 4059 | } |
| 4060 | else if (s[0] == '\a') |
| 4061 | { |
| 4062 | /* \a, bell */ |
| 4063 | MessageBeep(0xFFFFFFFF); |
| 4064 | #ifdef MCH_WRITE_DUMP |
| 4065 | if (fdDump) |
| 4066 | fputs("\\a\n", fdDump); |
| 4067 | #endif |
| 4068 | s++; |
| 4069 | } |
| 4070 | else if (s[0] == ESC && len >= 3-1 && s[1] == '|') |
| 4071 | { |
| 4072 | #ifdef MCH_WRITE_DUMP |
Bram Moolenaar | c0197e2 | 2004-09-13 20:26:32 +0000 | [diff] [blame] | 4073 | char_u *old_s = s; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4074 | #endif |
Bram Moolenaar | c0197e2 | 2004-09-13 20:26:32 +0000 | [diff] [blame] | 4075 | char_u *p; |
| 4076 | int arg1 = 0, arg2 = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4077 | |
| 4078 | switch (s[2]) |
| 4079 | { |
| 4080 | /* one or two numeric arguments, separated by ';' */ |
| 4081 | |
| 4082 | case '0': case '1': case '2': case '3': case '4': |
| 4083 | case '5': case '6': case '7': case '8': case '9': |
| 4084 | p = s + 2; |
| 4085 | arg1 = getdigits(&p); /* no check for length! */ |
| 4086 | if (p > s + len) |
| 4087 | break; |
| 4088 | |
| 4089 | if (*p == ';') |
| 4090 | { |
| 4091 | ++p; |
| 4092 | arg2 = getdigits(&p); /* no check for length! */ |
| 4093 | if (p > s + len) |
| 4094 | break; |
| 4095 | |
| 4096 | if (*p == 'H') |
| 4097 | gotoxy(arg2, arg1); |
| 4098 | else if (*p == 'r') |
| 4099 | set_scroll_region(0, arg1 - 1, Columns - 1, arg2 - 1); |
| 4100 | } |
| 4101 | else if (*p == 'A') |
| 4102 | { |
| 4103 | /* move cursor up arg1 lines in same column */ |
| 4104 | gotoxy(g_coord.X + 1, |
| 4105 | max(g_srScrollRegion.Top, g_coord.Y - arg1) + 1); |
| 4106 | } |
| 4107 | else if (*p == 'C') |
| 4108 | { |
| 4109 | /* move cursor right arg1 columns in same line */ |
| 4110 | gotoxy(min(g_srScrollRegion.Right, g_coord.X + arg1) + 1, |
| 4111 | g_coord.Y + 1); |
| 4112 | } |
| 4113 | else if (*p == 'H') |
| 4114 | { |
| 4115 | gotoxy(1, arg1); |
| 4116 | } |
| 4117 | else if (*p == 'L') |
| 4118 | { |
| 4119 | insert_lines(arg1); |
| 4120 | } |
| 4121 | else if (*p == 'm') |
| 4122 | { |
| 4123 | if (arg1 == 0) |
| 4124 | normvideo(); |
| 4125 | else |
| 4126 | textattr((WORD) arg1); |
| 4127 | } |
| 4128 | else if (*p == 'f') |
| 4129 | { |
| 4130 | textcolor((WORD) arg1); |
| 4131 | } |
| 4132 | else if (*p == 'b') |
| 4133 | { |
| 4134 | textbackground((WORD) arg1); |
| 4135 | } |
| 4136 | else if (*p == 'M') |
| 4137 | { |
| 4138 | delete_lines(arg1); |
| 4139 | } |
| 4140 | |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 4141 | len -= (int)(p - s); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4142 | s = p + 1; |
| 4143 | break; |
| 4144 | |
| 4145 | |
| 4146 | /* Three-character escape sequences */ |
| 4147 | |
| 4148 | case 'A': |
| 4149 | /* move cursor up one line in same column */ |
| 4150 | gotoxy(g_coord.X + 1, |
| 4151 | max(g_srScrollRegion.Top, g_coord.Y - 1) + 1); |
| 4152 | goto got3; |
| 4153 | |
| 4154 | case 'B': |
| 4155 | visual_bell(); |
| 4156 | goto got3; |
| 4157 | |
| 4158 | case 'C': |
| 4159 | /* move cursor right one column in same line */ |
| 4160 | gotoxy(min(g_srScrollRegion.Right, g_coord.X + 1) + 1, |
| 4161 | g_coord.Y + 1); |
| 4162 | goto got3; |
| 4163 | |
| 4164 | case 'E': |
| 4165 | termcap_mode_end(); |
| 4166 | goto got3; |
| 4167 | |
| 4168 | case 'F': |
| 4169 | standout(); |
| 4170 | goto got3; |
| 4171 | |
| 4172 | case 'f': |
| 4173 | standend(); |
| 4174 | goto got3; |
| 4175 | |
| 4176 | case 'H': |
| 4177 | gotoxy(1, 1); |
| 4178 | goto got3; |
| 4179 | |
| 4180 | case 'j': |
| 4181 | clear_to_end_of_display(); |
| 4182 | goto got3; |
| 4183 | |
| 4184 | case 'J': |
| 4185 | clear_screen(); |
| 4186 | goto got3; |
| 4187 | |
| 4188 | case 'K': |
| 4189 | clear_to_end_of_line(); |
| 4190 | goto got3; |
| 4191 | |
| 4192 | case 'L': |
| 4193 | insert_lines(1); |
| 4194 | goto got3; |
| 4195 | |
| 4196 | case 'M': |
| 4197 | delete_lines(1); |
| 4198 | goto got3; |
| 4199 | |
| 4200 | case 'S': |
| 4201 | termcap_mode_start(); |
| 4202 | goto got3; |
| 4203 | |
| 4204 | case 'V': |
| 4205 | cursor_visible(TRUE); |
| 4206 | goto got3; |
| 4207 | |
| 4208 | case 'v': |
| 4209 | cursor_visible(FALSE); |
| 4210 | goto got3; |
| 4211 | |
| 4212 | got3: |
| 4213 | s += 3; |
| 4214 | len -= 2; |
| 4215 | } |
| 4216 | |
| 4217 | #ifdef MCH_WRITE_DUMP |
| 4218 | if (fdDump) |
| 4219 | { |
| 4220 | fputs("ESC | ", fdDump); |
| 4221 | fwrite(old_s + 2, sizeof(char_u), s - old_s - 2, fdDump); |
| 4222 | fputc('\n', fdDump); |
| 4223 | } |
| 4224 | #endif |
| 4225 | } |
| 4226 | else |
| 4227 | { |
| 4228 | /* Write a single character */ |
| 4229 | DWORD nWritten; |
| 4230 | |
| 4231 | nWritten = write_chars(s, 1); |
| 4232 | #ifdef MCH_WRITE_DUMP |
| 4233 | if (fdDump) |
| 4234 | { |
| 4235 | fputc('>', fdDump); |
| 4236 | fwrite(s, sizeof(char_u), nWritten, fdDump); |
| 4237 | fputs("<\n", fdDump); |
| 4238 | } |
| 4239 | #endif |
| 4240 | |
| 4241 | len -= (nWritten - 1); |
| 4242 | s += nWritten; |
| 4243 | } |
| 4244 | } |
| 4245 | |
| 4246 | #ifdef MCH_WRITE_DUMP |
| 4247 | if (fdDump) |
| 4248 | fflush(fdDump); |
| 4249 | #endif |
| 4250 | } |
| 4251 | |
| 4252 | #endif /* FEAT_GUI_W32 */ |
| 4253 | |
| 4254 | |
| 4255 | /* |
| 4256 | * Delay for half a second. |
| 4257 | */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4258 | /*ARGSUSED*/ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4259 | void |
| 4260 | mch_delay( |
| 4261 | long msec, |
| 4262 | int ignoreinput) |
| 4263 | { |
| 4264 | #ifdef FEAT_GUI_W32 |
| 4265 | Sleep((int)msec); /* never wait for input */ |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 4266 | #else /* Console */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4267 | if (ignoreinput) |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 4268 | # ifdef FEAT_MZSCHEME |
| 4269 | if (mzthreads_allowed() && p_mzq > 0 && msec > p_mzq) |
| 4270 | { |
| 4271 | int towait = p_mzq; |
| 4272 | |
| 4273 | /* if msec is large enough, wait by portions in p_mzq */ |
| 4274 | while (msec > 0) |
| 4275 | { |
| 4276 | mzvim_check_threads(); |
| 4277 | if (msec < towait) |
| 4278 | towait = msec; |
| 4279 | Sleep(towait); |
| 4280 | msec -= towait; |
| 4281 | } |
| 4282 | } |
| 4283 | else |
| 4284 | # endif |
| 4285 | Sleep((int)msec); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4286 | else |
| 4287 | WaitForChar(msec); |
| 4288 | #endif |
| 4289 | } |
| 4290 | |
| 4291 | |
| 4292 | /* |
| 4293 | * this version of remove is not scared by a readonly (backup) file |
| 4294 | * Return 0 for success, -1 for failure. |
| 4295 | */ |
| 4296 | int |
| 4297 | mch_remove(char_u *name) |
| 4298 | { |
| 4299 | #ifdef FEAT_MBYTE |
| 4300 | WCHAR *wn = NULL; |
| 4301 | int n; |
| 4302 | |
| 4303 | if (enc_codepage >= 0 && (int)GetACP() != enc_codepage) |
| 4304 | { |
Bram Moolenaar | 36f692d | 2008-11-20 16:10:17 +0000 | [diff] [blame] | 4305 | wn = enc_to_utf16(name, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4306 | if (wn != NULL) |
| 4307 | { |
| 4308 | SetFileAttributesW(wn, FILE_ATTRIBUTE_NORMAL); |
| 4309 | n = DeleteFileW(wn) ? 0 : -1; |
| 4310 | vim_free(wn); |
| 4311 | if (n == 0 || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED) |
| 4312 | return n; |
| 4313 | /* Retry with non-wide function (for Windows 98). */ |
| 4314 | } |
| 4315 | } |
| 4316 | #endif |
| 4317 | SetFileAttributes(name, FILE_ATTRIBUTE_NORMAL); |
| 4318 | return DeleteFile(name) ? 0 : -1; |
| 4319 | } |
| 4320 | |
| 4321 | |
| 4322 | /* |
| 4323 | * check for an "interrupt signal": CTRL-break or CTRL-C |
| 4324 | */ |
| 4325 | void |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 4326 | mch_breakcheck(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4327 | { |
| 4328 | #ifndef FEAT_GUI_W32 /* never used */ |
| 4329 | if (g_fCtrlCPressed || g_fCBrkPressed) |
| 4330 | { |
| 4331 | g_fCtrlCPressed = g_fCBrkPressed = FALSE; |
| 4332 | got_int = TRUE; |
| 4333 | } |
| 4334 | #endif |
| 4335 | } |
| 4336 | |
| 4337 | |
| 4338 | /* |
| 4339 | * How much memory is available? |
| 4340 | * Return sum of available physical and page file memory. |
| 4341 | */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 4342 | /*ARGSUSED*/ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4343 | long_u |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 4344 | mch_avail_mem(int special) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4345 | { |
| 4346 | MEMORYSTATUS ms; |
| 4347 | |
| 4348 | ms.dwLength = sizeof(MEMORYSTATUS); |
| 4349 | GlobalMemoryStatus(&ms); |
| 4350 | return (long_u) (ms.dwAvailPhys + ms.dwAvailPageFile); |
| 4351 | } |
| 4352 | |
| 4353 | #ifdef FEAT_MBYTE |
| 4354 | /* |
| 4355 | * Same code as below, but with wide functions and no comments. |
| 4356 | * Return 0 for success, non-zero for failure. |
| 4357 | */ |
| 4358 | int |
| 4359 | mch_wrename(WCHAR *wold, WCHAR *wnew) |
| 4360 | { |
| 4361 | WCHAR *p; |
| 4362 | int i; |
| 4363 | WCHAR szTempFile[_MAX_PATH + 1]; |
| 4364 | WCHAR szNewPath[_MAX_PATH + 1]; |
| 4365 | HANDLE hf; |
| 4366 | |
| 4367 | if (!mch_windows95()) |
| 4368 | { |
| 4369 | p = wold; |
| 4370 | for (i = 0; wold[i] != NUL; ++i) |
| 4371 | if ((wold[i] == '/' || wold[i] == '\\' || wold[i] == ':') |
| 4372 | && wold[i + 1] != 0) |
| 4373 | p = wold + i + 1; |
| 4374 | if ((int)(wold + i - p) < 8 || p[6] != '~') |
| 4375 | return (MoveFileW(wold, wnew) == 0); |
| 4376 | } |
| 4377 | |
| 4378 | if (GetFullPathNameW(wnew, _MAX_PATH, szNewPath, &p) == 0 || p == NULL) |
| 4379 | return -1; |
| 4380 | *p = NUL; |
| 4381 | |
| 4382 | if (GetTempFileNameW(szNewPath, L"VIM", 0, szTempFile) == 0) |
| 4383 | return -2; |
| 4384 | |
| 4385 | if (!DeleteFileW(szTempFile)) |
| 4386 | return -3; |
| 4387 | |
| 4388 | if (!MoveFileW(wold, szTempFile)) |
| 4389 | return -4; |
| 4390 | |
| 4391 | if ((hf = CreateFileW(wold, GENERIC_WRITE, 0, NULL, CREATE_NEW, |
| 4392 | FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE) |
| 4393 | return -5; |
| 4394 | if (!CloseHandle(hf)) |
| 4395 | return -6; |
| 4396 | |
| 4397 | if (!MoveFileW(szTempFile, wnew)) |
| 4398 | { |
| 4399 | (void)MoveFileW(szTempFile, wold); |
| 4400 | return -7; |
| 4401 | } |
| 4402 | |
| 4403 | DeleteFileW(szTempFile); |
| 4404 | |
| 4405 | if (!DeleteFileW(wold)) |
| 4406 | return -8; |
| 4407 | |
| 4408 | return 0; |
| 4409 | } |
| 4410 | #endif |
| 4411 | |
| 4412 | |
| 4413 | /* |
| 4414 | * mch_rename() works around a bug in rename (aka MoveFile) in |
| 4415 | * Windows 95: rename("foo.bar", "foo.bar~") will generate a |
| 4416 | * file whose short file name is "FOO.BAR" (its long file name will |
| 4417 | * be correct: "foo.bar~"). Because a file can be accessed by |
| 4418 | * either its SFN or its LFN, "foo.bar" has effectively been |
| 4419 | * renamed to "foo.bar", which is not at all what was wanted. This |
| 4420 | * seems to happen only when renaming files with three-character |
| 4421 | * extensions by appending a suffix that does not include ".". |
| 4422 | * Windows NT gets it right, however, with an SFN of "FOO~1.BAR". |
| 4423 | * |
| 4424 | * There is another problem, which isn't really a bug but isn't right either: |
| 4425 | * When renaming "abcdef~1.txt" to "abcdef~1.txt~", the short name can be |
| 4426 | * "abcdef~1.txt" again. This has been reported on Windows NT 4.0 with |
| 4427 | * service pack 6. Doesn't seem to happen on Windows 98. |
| 4428 | * |
| 4429 | * Like rename(), returns 0 upon success, non-zero upon failure. |
| 4430 | * Should probably set errno appropriately when errors occur. |
| 4431 | */ |
| 4432 | int |
| 4433 | mch_rename( |
| 4434 | const char *pszOldFile, |
| 4435 | const char *pszNewFile) |
| 4436 | { |
| 4437 | char szTempFile[_MAX_PATH+1]; |
| 4438 | char szNewPath[_MAX_PATH+1]; |
| 4439 | char *pszFilePart; |
| 4440 | HANDLE hf; |
| 4441 | #ifdef FEAT_MBYTE |
| 4442 | WCHAR *wold = NULL; |
| 4443 | WCHAR *wnew = NULL; |
| 4444 | int retval = -1; |
| 4445 | |
| 4446 | if (enc_codepage >= 0 && (int)GetACP() != enc_codepage) |
| 4447 | { |
Bram Moolenaar | 36f692d | 2008-11-20 16:10:17 +0000 | [diff] [blame] | 4448 | wold = enc_to_utf16((char_u *)pszOldFile, NULL); |
| 4449 | wnew = enc_to_utf16((char_u *)pszNewFile, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4450 | if (wold != NULL && wnew != NULL) |
| 4451 | retval = mch_wrename(wold, wnew); |
| 4452 | vim_free(wold); |
| 4453 | vim_free(wnew); |
| 4454 | if (retval == 0 || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED) |
| 4455 | return retval; |
| 4456 | /* Retry with non-wide function (for Windows 98). */ |
| 4457 | } |
| 4458 | #endif |
| 4459 | |
| 4460 | /* |
| 4461 | * No need to play tricks if not running Windows 95, unless the file name |
| 4462 | * contains a "~" as the seventh character. |
| 4463 | */ |
| 4464 | if (!mch_windows95()) |
| 4465 | { |
| 4466 | pszFilePart = (char *)gettail((char_u *)pszOldFile); |
| 4467 | if (STRLEN(pszFilePart) < 8 || pszFilePart[6] != '~') |
| 4468 | return rename(pszOldFile, pszNewFile); |
| 4469 | } |
| 4470 | |
| 4471 | /* Get base path of new file name. Undocumented feature: If pszNewFile is |
| 4472 | * a directory, no error is returned and pszFilePart will be NULL. */ |
| 4473 | if (GetFullPathName(pszNewFile, _MAX_PATH, szNewPath, &pszFilePart) == 0 |
| 4474 | || pszFilePart == NULL) |
| 4475 | return -1; |
| 4476 | *pszFilePart = NUL; |
| 4477 | |
| 4478 | /* Get (and create) a unique temporary file name in directory of new file */ |
| 4479 | if (GetTempFileName(szNewPath, "VIM", 0, szTempFile) == 0) |
| 4480 | return -2; |
| 4481 | |
| 4482 | /* blow the temp file away */ |
| 4483 | if (!DeleteFile(szTempFile)) |
| 4484 | return -3; |
| 4485 | |
| 4486 | /* rename old file to the temp file */ |
| 4487 | if (!MoveFile(pszOldFile, szTempFile)) |
| 4488 | return -4; |
| 4489 | |
| 4490 | /* now create an empty file called pszOldFile; this prevents the operating |
| 4491 | * system using pszOldFile as an alias (SFN) if we're renaming within the |
| 4492 | * same directory. For example, we're editing a file called |
| 4493 | * filename.asc.txt by its SFN, filena~1.txt. If we rename filena~1.txt |
| 4494 | * to filena~1.txt~ (i.e., we're making a backup while writing it), the |
| 4495 | * SFN for filena~1.txt~ will be filena~1.txt, by default, which will |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 4496 | * cause all sorts of problems later in buf_write(). So, we create an |
| 4497 | * empty file called filena~1.txt and the system will have to find some |
| 4498 | * other SFN for filena~1.txt~, such as filena~2.txt |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4499 | */ |
| 4500 | if ((hf = CreateFile(pszOldFile, GENERIC_WRITE, 0, NULL, CREATE_NEW, |
| 4501 | FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE) |
| 4502 | return -5; |
| 4503 | if (!CloseHandle(hf)) |
| 4504 | return -6; |
| 4505 | |
| 4506 | /* rename the temp file to the new file */ |
| 4507 | if (!MoveFile(szTempFile, pszNewFile)) |
| 4508 | { |
| 4509 | /* Renaming failed. Rename the file back to its old name, so that it |
| 4510 | * looks like nothing happened. */ |
| 4511 | (void)MoveFile(szTempFile, pszOldFile); |
| 4512 | |
| 4513 | return -7; |
| 4514 | } |
| 4515 | |
| 4516 | /* Seems to be left around on Novell filesystems */ |
| 4517 | DeleteFile(szTempFile); |
| 4518 | |
| 4519 | /* finally, remove the empty old file */ |
| 4520 | if (!DeleteFile(pszOldFile)) |
| 4521 | return -8; |
| 4522 | |
| 4523 | return 0; /* success */ |
| 4524 | } |
| 4525 | |
| 4526 | /* |
| 4527 | * Get the default shell for the current hardware platform |
| 4528 | */ |
| 4529 | char * |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 4530 | default_shell(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4531 | { |
| 4532 | char* psz = NULL; |
| 4533 | |
| 4534 | PlatformId(); |
| 4535 | |
| 4536 | if (g_PlatformId == VER_PLATFORM_WIN32_NT) /* Windows NT */ |
| 4537 | psz = "cmd.exe"; |
| 4538 | else if (g_PlatformId == VER_PLATFORM_WIN32_WINDOWS) /* Windows 95 */ |
| 4539 | psz = "command.com"; |
| 4540 | |
| 4541 | return psz; |
| 4542 | } |
| 4543 | |
| 4544 | /* |
| 4545 | * mch_access() extends access() to do more detailed check on network drives. |
| 4546 | * Returns 0 if file "n" has access rights according to "p", -1 otherwise. |
| 4547 | */ |
| 4548 | int |
| 4549 | mch_access(char *n, int p) |
| 4550 | { |
| 4551 | HANDLE hFile; |
| 4552 | DWORD am; |
| 4553 | int retval = -1; /* default: fail */ |
| 4554 | #ifdef FEAT_MBYTE |
| 4555 | WCHAR *wn = NULL; |
| 4556 | |
| 4557 | if (enc_codepage >= 0 && (int)GetACP() != enc_codepage) |
Bram Moolenaar | 36f692d | 2008-11-20 16:10:17 +0000 | [diff] [blame] | 4558 | wn = enc_to_utf16(n, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4559 | #endif |
| 4560 | |
| 4561 | if (mch_isdir(n)) |
| 4562 | { |
| 4563 | char TempName[_MAX_PATH + 16] = ""; |
| 4564 | #ifdef FEAT_MBYTE |
| 4565 | WCHAR TempNameW[_MAX_PATH + 16] = L""; |
| 4566 | #endif |
| 4567 | |
| 4568 | if (p & R_OK) |
| 4569 | { |
| 4570 | /* Read check is performed by seeing if we can do a find file on |
| 4571 | * the directory for any file. */ |
| 4572 | #ifdef FEAT_MBYTE |
| 4573 | if (wn != NULL) |
| 4574 | { |
| 4575 | int i; |
| 4576 | WIN32_FIND_DATAW d; |
| 4577 | |
| 4578 | for (i = 0; i < _MAX_PATH && wn[i] != 0; ++i) |
| 4579 | TempNameW[i] = wn[i]; |
| 4580 | if (TempNameW[i - 1] != '\\' && TempNameW[i - 1] != '/') |
| 4581 | TempNameW[i++] = '\\'; |
| 4582 | TempNameW[i++] = '*'; |
| 4583 | TempNameW[i++] = 0; |
| 4584 | |
| 4585 | hFile = FindFirstFileW(TempNameW, &d); |
| 4586 | if (hFile == INVALID_HANDLE_VALUE) |
| 4587 | { |
| 4588 | if (GetLastError() != ERROR_CALL_NOT_IMPLEMENTED) |
| 4589 | goto getout; |
| 4590 | |
| 4591 | /* Retry with non-wide function (for Windows 98). */ |
| 4592 | vim_free(wn); |
| 4593 | wn = NULL; |
| 4594 | } |
| 4595 | else |
| 4596 | (void)FindClose(hFile); |
| 4597 | } |
| 4598 | if (wn == NULL) |
| 4599 | #endif |
| 4600 | { |
| 4601 | char *pch; |
| 4602 | WIN32_FIND_DATA d; |
| 4603 | |
Bram Moolenaar | fe3ca8d | 2005-07-18 21:43:02 +0000 | [diff] [blame] | 4604 | vim_strncpy(TempName, n, _MAX_PATH); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4605 | pch = TempName + STRLEN(TempName) - 1; |
| 4606 | if (*pch != '\\' && *pch != '/') |
| 4607 | *++pch = '\\'; |
| 4608 | *++pch = '*'; |
| 4609 | *++pch = NUL; |
| 4610 | |
| 4611 | hFile = FindFirstFile(TempName, &d); |
| 4612 | if (hFile == INVALID_HANDLE_VALUE) |
| 4613 | goto getout; |
| 4614 | (void)FindClose(hFile); |
| 4615 | } |
| 4616 | } |
| 4617 | |
| 4618 | if (p & W_OK) |
| 4619 | { |
| 4620 | /* Trying to create a temporary file in the directory should catch |
| 4621 | * directories on read-only network shares. However, in |
| 4622 | * directories whose ACL allows writes but denies deletes will end |
| 4623 | * up keeping the temporary file :-(. */ |
| 4624 | #ifdef FEAT_MBYTE |
| 4625 | if (wn != NULL) |
| 4626 | { |
| 4627 | if (!GetTempFileNameW(wn, L"VIM", 0, TempNameW)) |
| 4628 | { |
| 4629 | if (GetLastError() != ERROR_CALL_NOT_IMPLEMENTED) |
| 4630 | goto getout; |
| 4631 | |
| 4632 | /* Retry with non-wide function (for Windows 98). */ |
| 4633 | vim_free(wn); |
| 4634 | wn = NULL; |
| 4635 | } |
| 4636 | else |
| 4637 | DeleteFileW(TempNameW); |
| 4638 | } |
| 4639 | if (wn == NULL) |
| 4640 | #endif |
| 4641 | { |
| 4642 | if (!GetTempFileName(n, "VIM", 0, TempName)) |
| 4643 | goto getout; |
| 4644 | mch_remove((char_u *)TempName); |
| 4645 | } |
| 4646 | } |
| 4647 | } |
| 4648 | else |
| 4649 | { |
| 4650 | /* Trying to open the file for the required access does ACL, read-only |
| 4651 | * network share, and file attribute checks. */ |
| 4652 | am = ((p & W_OK) ? GENERIC_WRITE : 0) |
| 4653 | | ((p & R_OK) ? GENERIC_READ : 0); |
| 4654 | #ifdef FEAT_MBYTE |
| 4655 | if (wn != NULL) |
| 4656 | { |
| 4657 | hFile = CreateFileW(wn, am, 0, NULL, OPEN_EXISTING, 0, NULL); |
| 4658 | if (hFile == INVALID_HANDLE_VALUE |
| 4659 | && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED) |
| 4660 | { |
| 4661 | /* Retry with non-wide function (for Windows 98). */ |
| 4662 | vim_free(wn); |
| 4663 | wn = NULL; |
| 4664 | } |
| 4665 | } |
| 4666 | if (wn == NULL) |
| 4667 | #endif |
| 4668 | hFile = CreateFile(n, am, 0, NULL, OPEN_EXISTING, 0, NULL); |
| 4669 | if (hFile == INVALID_HANDLE_VALUE) |
| 4670 | goto getout; |
| 4671 | CloseHandle(hFile); |
| 4672 | } |
| 4673 | |
| 4674 | retval = 0; /* success */ |
| 4675 | getout: |
| 4676 | #ifdef FEAT_MBYTE |
| 4677 | vim_free(wn); |
| 4678 | #endif |
| 4679 | return retval; |
| 4680 | } |
| 4681 | |
| 4682 | #if defined(FEAT_MBYTE) || defined(PROTO) |
| 4683 | /* |
Bram Moolenaar | 36f692d | 2008-11-20 16:10:17 +0000 | [diff] [blame] | 4684 | * Version of open() that may use UTF-16 file name. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4685 | */ |
| 4686 | int |
| 4687 | mch_open(char *name, int flags, int mode) |
| 4688 | { |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 4689 | /* _wopen() does not work with Borland C 5.5: creates a read-only file. */ |
| 4690 | # ifndef __BORLANDC__ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4691 | WCHAR *wn; |
| 4692 | int f; |
| 4693 | |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 4694 | if (enc_codepage >= 0 && (int)GetACP() != enc_codepage) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4695 | { |
Bram Moolenaar | 36f692d | 2008-11-20 16:10:17 +0000 | [diff] [blame] | 4696 | wn = enc_to_utf16(name, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4697 | if (wn != NULL) |
| 4698 | { |
| 4699 | f = _wopen(wn, flags, mode); |
| 4700 | vim_free(wn); |
| 4701 | if (f >= 0) |
| 4702 | return f; |
| 4703 | /* Retry with non-wide function (for Windows 98). Can't use |
| 4704 | * GetLastError() here and it's unclear what errno gets set to if |
| 4705 | * the _wopen() fails for missing wide functions. */ |
| 4706 | } |
| 4707 | } |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 4708 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4709 | |
| 4710 | return open(name, flags, mode); |
| 4711 | } |
| 4712 | |
| 4713 | /* |
Bram Moolenaar | 36f692d | 2008-11-20 16:10:17 +0000 | [diff] [blame] | 4714 | * Version of fopen() that may use UTF-16 file name. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4715 | */ |
| 4716 | FILE * |
| 4717 | mch_fopen(char *name, char *mode) |
| 4718 | { |
| 4719 | WCHAR *wn, *wm; |
| 4720 | FILE *f = NULL; |
| 4721 | |
| 4722 | if (enc_codepage >= 0 && (int)GetACP() != enc_codepage |
| 4723 | # ifdef __BORLANDC__ |
| 4724 | /* Wide functions of Borland C 5.5 do not work on Windows 98. */ |
| 4725 | && g_PlatformId == VER_PLATFORM_WIN32_NT |
| 4726 | # endif |
| 4727 | ) |
| 4728 | { |
Bram Moolenaar | e6a91fd | 2008-07-24 18:51:11 +0000 | [diff] [blame] | 4729 | # if defined(DEBUG) && _MSC_VER >= 1400 |
Bram Moolenaar | 0fde290 | 2008-03-16 13:54:13 +0000 | [diff] [blame] | 4730 | /* Work around an annoying assertion in the Microsoft debug CRT |
| 4731 | * when mode's text/binary setting doesn't match _get_fmode(). */ |
| 4732 | char newMode = mode[strlen(mode) - 1]; |
| 4733 | int oldMode = 0; |
| 4734 | |
| 4735 | _get_fmode(&oldMode); |
| 4736 | if (newMode == 't') |
| 4737 | _set_fmode(_O_TEXT); |
| 4738 | else if (newMode == 'b') |
| 4739 | _set_fmode(_O_BINARY); |
| 4740 | # endif |
Bram Moolenaar | 36f692d | 2008-11-20 16:10:17 +0000 | [diff] [blame] | 4741 | wn = enc_to_utf16(name, NULL); |
| 4742 | wm = enc_to_utf16(mode, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4743 | if (wn != NULL && wm != NULL) |
| 4744 | f = _wfopen(wn, wm); |
| 4745 | vim_free(wn); |
| 4746 | vim_free(wm); |
Bram Moolenaar | 0fde290 | 2008-03-16 13:54:13 +0000 | [diff] [blame] | 4747 | |
Bram Moolenaar | e6a91fd | 2008-07-24 18:51:11 +0000 | [diff] [blame] | 4748 | # if defined(DEBUG) && _MSC_VER >= 1400 |
Bram Moolenaar | 0fde290 | 2008-03-16 13:54:13 +0000 | [diff] [blame] | 4749 | _set_fmode(oldMode); |
| 4750 | # endif |
| 4751 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4752 | if (f != NULL) |
| 4753 | return f; |
| 4754 | /* Retry with non-wide function (for Windows 98). Can't use |
| 4755 | * GetLastError() here and it's unclear what errno gets set to if |
| 4756 | * the _wfopen() fails for missing wide functions. */ |
| 4757 | } |
| 4758 | |
| 4759 | return fopen(name, mode); |
| 4760 | } |
| 4761 | #endif |
| 4762 | |
| 4763 | #ifdef FEAT_MBYTE |
| 4764 | /* |
| 4765 | * SUB STREAM (aka info stream) handling: |
| 4766 | * |
| 4767 | * NTFS can have sub streams for each file. Normal contents of file is |
| 4768 | * stored in the main stream, and extra contents (author information and |
| 4769 | * title and so on) can be stored in sub stream. After Windows 2000, user |
| 4770 | * can access and store those informations in sub streams via explorer's |
| 4771 | * property menuitem in right click menu. Those informations in sub streams |
| 4772 | * were lost when copying only the main stream. So we have to copy sub |
| 4773 | * streams. |
| 4774 | * |
| 4775 | * Incomplete explanation: |
| 4776 | * http://msdn.microsoft.com/library/en-us/dnw2k/html/ntfs5.asp |
| 4777 | * More useful info and an example: |
| 4778 | * http://www.sysinternals.com/ntw2k/source/misc.shtml#streams |
| 4779 | */ |
| 4780 | |
| 4781 | /* |
| 4782 | * Copy info stream data "substream". Read from the file with BackupRead(sh) |
| 4783 | * and write to stream "substream" of file "to". |
| 4784 | * Errors are ignored. |
| 4785 | */ |
| 4786 | static void |
| 4787 | copy_substream(HANDLE sh, void *context, WCHAR *to, WCHAR *substream, long len) |
| 4788 | { |
| 4789 | HANDLE hTo; |
| 4790 | WCHAR *to_name; |
| 4791 | |
| 4792 | to_name = malloc((wcslen(to) + wcslen(substream) + 1) * sizeof(WCHAR)); |
| 4793 | wcscpy(to_name, to); |
| 4794 | wcscat(to_name, substream); |
| 4795 | |
| 4796 | hTo = CreateFileW(to_name, GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, |
| 4797 | FILE_ATTRIBUTE_NORMAL, NULL); |
| 4798 | if (hTo != INVALID_HANDLE_VALUE) |
| 4799 | { |
| 4800 | long done; |
| 4801 | DWORD todo; |
| 4802 | DWORD readcnt, written; |
| 4803 | char buf[4096]; |
| 4804 | |
| 4805 | /* Copy block of bytes at a time. Abort when something goes wrong. */ |
| 4806 | for (done = 0; done < len; done += written) |
| 4807 | { |
| 4808 | /* (size_t) cast for Borland C 5.5 */ |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 4809 | todo = (DWORD)((size_t)(len - done) > sizeof(buf) ? sizeof(buf) |
| 4810 | : (size_t)(len - done)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4811 | if (!BackupRead(sh, (LPBYTE)buf, todo, &readcnt, |
| 4812 | FALSE, FALSE, context) |
| 4813 | || readcnt != todo |
| 4814 | || !WriteFile(hTo, buf, todo, &written, NULL) |
| 4815 | || written != todo) |
| 4816 | break; |
| 4817 | } |
| 4818 | CloseHandle(hTo); |
| 4819 | } |
| 4820 | |
| 4821 | free(to_name); |
| 4822 | } |
| 4823 | |
| 4824 | /* |
| 4825 | * Copy info streams from file "from" to file "to". |
| 4826 | */ |
| 4827 | static void |
| 4828 | copy_infostreams(char_u *from, char_u *to) |
| 4829 | { |
| 4830 | WCHAR *fromw; |
| 4831 | WCHAR *tow; |
| 4832 | HANDLE sh; |
| 4833 | WIN32_STREAM_ID sid; |
| 4834 | int headersize; |
| 4835 | WCHAR streamname[_MAX_PATH]; |
| 4836 | DWORD readcount; |
| 4837 | void *context = NULL; |
| 4838 | DWORD lo, hi; |
| 4839 | int len; |
| 4840 | |
| 4841 | /* Convert the file names to wide characters. */ |
Bram Moolenaar | 36f692d | 2008-11-20 16:10:17 +0000 | [diff] [blame] | 4842 | fromw = enc_to_utf16(from, NULL); |
| 4843 | tow = enc_to_utf16(to, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4844 | if (fromw != NULL && tow != NULL) |
| 4845 | { |
| 4846 | /* Open the file for reading. */ |
| 4847 | sh = CreateFileW(fromw, GENERIC_READ, FILE_SHARE_READ, NULL, |
| 4848 | OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL); |
| 4849 | if (sh != INVALID_HANDLE_VALUE) |
| 4850 | { |
| 4851 | /* Use BackupRead() to find the info streams. Repeat until we |
| 4852 | * have done them all.*/ |
| 4853 | for (;;) |
| 4854 | { |
| 4855 | /* Get the header to find the length of the stream name. If |
| 4856 | * the "readcount" is zero we have done all info streams. */ |
| 4857 | ZeroMemory(&sid, sizeof(WIN32_STREAM_ID)); |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 4858 | headersize = (int)((char *)&sid.cStreamName - (char *)&sid.dwStreamId); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4859 | if (!BackupRead(sh, (LPBYTE)&sid, headersize, |
| 4860 | &readcount, FALSE, FALSE, &context) |
| 4861 | || readcount == 0) |
| 4862 | break; |
| 4863 | |
| 4864 | /* We only deal with streams that have a name. The normal |
| 4865 | * file data appears to be without a name, even though docs |
| 4866 | * suggest it is called "::$DATA". */ |
| 4867 | if (sid.dwStreamNameSize > 0) |
| 4868 | { |
| 4869 | /* Read the stream name. */ |
| 4870 | if (!BackupRead(sh, (LPBYTE)streamname, |
| 4871 | sid.dwStreamNameSize, |
| 4872 | &readcount, FALSE, FALSE, &context)) |
| 4873 | break; |
| 4874 | |
| 4875 | /* Copy an info stream with a name ":anything:$DATA". |
| 4876 | * Skip "::$DATA", it has no stream name (examples suggest |
| 4877 | * it might be used for the normal file contents). |
| 4878 | * Note that BackupRead() counts bytes, but the name is in |
| 4879 | * wide characters. */ |
| 4880 | len = readcount / sizeof(WCHAR); |
| 4881 | streamname[len] = 0; |
| 4882 | if (len > 7 && wcsicmp(streamname + len - 6, |
| 4883 | L":$DATA") == 0) |
| 4884 | { |
| 4885 | streamname[len - 6] = 0; |
| 4886 | copy_substream(sh, &context, tow, streamname, |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 4887 | (long)sid.Size.u.LowPart); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4888 | } |
| 4889 | } |
| 4890 | |
| 4891 | /* Advance to the next stream. We might try seeking too far, |
| 4892 | * but BackupSeek() doesn't skip over stream borders, thus |
| 4893 | * that's OK. */ |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 4894 | (void)BackupSeek(sh, sid.Size.u.LowPart, sid.Size.u.HighPart, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4895 | &lo, &hi, &context); |
| 4896 | } |
| 4897 | |
| 4898 | /* Clear the context. */ |
| 4899 | (void)BackupRead(sh, NULL, 0, &readcount, TRUE, FALSE, &context); |
| 4900 | |
| 4901 | CloseHandle(sh); |
| 4902 | } |
| 4903 | } |
| 4904 | vim_free(fromw); |
| 4905 | vim_free(tow); |
| 4906 | } |
| 4907 | #endif |
| 4908 | |
| 4909 | /* |
| 4910 | * Copy file attributes from file "from" to file "to". |
| 4911 | * For Windows NT and later we copy info streams. |
| 4912 | * Always returns zero, errors are ignored. |
| 4913 | */ |
| 4914 | int |
| 4915 | mch_copy_file_attribute(char_u *from, char_u *to) |
| 4916 | { |
| 4917 | #ifdef FEAT_MBYTE |
| 4918 | /* File streams only work on Windows NT and later. */ |
| 4919 | PlatformId(); |
| 4920 | if (g_PlatformId == VER_PLATFORM_WIN32_NT) |
| 4921 | copy_infostreams(from, to); |
| 4922 | #endif |
| 4923 | return 0; |
| 4924 | } |
| 4925 | |
| 4926 | #if defined(MYRESETSTKOFLW) || defined(PROTO) |
| 4927 | /* |
| 4928 | * Recreate a destroyed stack guard page in win32. |
| 4929 | * Written by Benjamin Peterson. |
| 4930 | */ |
| 4931 | |
| 4932 | /* These magic numbers are from the MS header files */ |
| 4933 | #define MIN_STACK_WIN9X 17 |
| 4934 | #define MIN_STACK_WINNT 2 |
| 4935 | |
| 4936 | /* |
| 4937 | * This function does the same thing as _resetstkoflw(), which is only |
| 4938 | * available in DevStudio .net and later. |
| 4939 | * Returns 0 for failure, 1 for success. |
| 4940 | */ |
| 4941 | int |
| 4942 | myresetstkoflw(void) |
| 4943 | { |
| 4944 | BYTE *pStackPtr; |
| 4945 | BYTE *pGuardPage; |
| 4946 | BYTE *pStackBase; |
| 4947 | BYTE *pLowestPossiblePage; |
| 4948 | MEMORY_BASIC_INFORMATION mbi; |
| 4949 | SYSTEM_INFO si; |
| 4950 | DWORD nPageSize; |
| 4951 | DWORD dummy; |
| 4952 | |
| 4953 | /* This code will not work on win32s. */ |
| 4954 | PlatformId(); |
| 4955 | if (g_PlatformId == VER_PLATFORM_WIN32s) |
| 4956 | return 0; |
| 4957 | |
| 4958 | /* We need to know the system page size. */ |
| 4959 | GetSystemInfo(&si); |
| 4960 | nPageSize = si.dwPageSize; |
| 4961 | |
| 4962 | /* ...and the current stack pointer */ |
| 4963 | pStackPtr = (BYTE*)_alloca(1); |
| 4964 | |
| 4965 | /* ...and the base of the stack. */ |
| 4966 | if (VirtualQuery(pStackPtr, &mbi, sizeof mbi) == 0) |
| 4967 | return 0; |
| 4968 | pStackBase = (BYTE*)mbi.AllocationBase; |
| 4969 | |
| 4970 | /* ...and the page thats min_stack_req pages away from stack base; this is |
| 4971 | * the lowest page we could use. */ |
| 4972 | pLowestPossiblePage = pStackBase + ((g_PlatformId == VER_PLATFORM_WIN32_NT) |
| 4973 | ? MIN_STACK_WINNT : MIN_STACK_WIN9X) * nPageSize; |
| 4974 | |
| 4975 | /* On Win95, we want the next page down from the end of the stack. */ |
| 4976 | if (g_PlatformId == VER_PLATFORM_WIN32_WINDOWS) |
| 4977 | { |
| 4978 | /* Find the page that's only 1 page down from the page that the stack |
| 4979 | * ptr is in. */ |
| 4980 | pGuardPage = (BYTE*)((DWORD)nPageSize * (((DWORD)pStackPtr |
| 4981 | / (DWORD)nPageSize) - 1)); |
| 4982 | if (pGuardPage < pLowestPossiblePage) |
| 4983 | return 0; |
| 4984 | |
| 4985 | /* Apply the noaccess attribute to the page -- there's no guard |
| 4986 | * attribute in win95-type OSes. */ |
| 4987 | if (!VirtualProtect(pGuardPage, nPageSize, PAGE_NOACCESS, &dummy)) |
| 4988 | return 0; |
| 4989 | } |
| 4990 | else |
| 4991 | { |
| 4992 | /* On NT, however, we want the first committed page in the stack Start |
| 4993 | * at the stack base and move forward through memory until we find a |
| 4994 | * committed block. */ |
| 4995 | BYTE *pBlock = pStackBase; |
| 4996 | |
Bram Moolenaar | a466c99 | 2005-07-09 21:03:22 +0000 | [diff] [blame] | 4997 | for (;;) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4998 | { |
| 4999 | if (VirtualQuery(pBlock, &mbi, sizeof mbi) == 0) |
| 5000 | return 0; |
| 5001 | |
| 5002 | pBlock += mbi.RegionSize; |
| 5003 | |
| 5004 | if (mbi.State & MEM_COMMIT) |
| 5005 | break; |
| 5006 | } |
| 5007 | |
| 5008 | /* mbi now describes the first committed block in the stack. */ |
| 5009 | if (mbi.Protect & PAGE_GUARD) |
| 5010 | return 1; |
| 5011 | |
| 5012 | /* decide where the guard page should start */ |
| 5013 | if ((long_u)(mbi.BaseAddress) < (long_u)pLowestPossiblePage) |
| 5014 | pGuardPage = pLowestPossiblePage; |
| 5015 | else |
| 5016 | pGuardPage = (BYTE*)mbi.BaseAddress; |
| 5017 | |
| 5018 | /* allocate the guard page */ |
| 5019 | if (!VirtualAlloc(pGuardPage, nPageSize, MEM_COMMIT, PAGE_READWRITE)) |
| 5020 | return 0; |
| 5021 | |
| 5022 | /* apply the guard attribute to the page */ |
| 5023 | if (!VirtualProtect(pGuardPage, nPageSize, PAGE_READWRITE | PAGE_GUARD, |
| 5024 | &dummy)) |
| 5025 | return 0; |
| 5026 | } |
| 5027 | |
| 5028 | return 1; |
| 5029 | } |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 5030 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5031 | |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 5032 | |
| 5033 | #if defined(FEAT_MBYTE) || defined(PROTO) |
| 5034 | /* |
| 5035 | * The command line arguments in UCS2 |
| 5036 | */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5037 | static int nArgsW = 0; |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 5038 | static LPWSTR *ArglistW = NULL; |
| 5039 | static int global_argc = 0; |
| 5040 | static char **global_argv; |
| 5041 | |
| 5042 | static int used_file_argc = 0; /* last argument in global_argv[] used |
| 5043 | for the argument list. */ |
| 5044 | static int *used_file_indexes = NULL; /* indexes in global_argv[] for |
| 5045 | command line arguments added to |
| 5046 | the argument list */ |
| 5047 | static int used_file_count = 0; /* nr of entries in used_file_indexes */ |
| 5048 | static int used_file_literal = FALSE; /* take file names literally */ |
| 5049 | static int used_file_full_path = FALSE; /* file name was full path */ |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 5050 | static int used_file_diff_mode = FALSE; /* file name was with diff mode */ |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 5051 | static int used_alist_count = 0; |
| 5052 | |
| 5053 | |
| 5054 | /* |
| 5055 | * Get the command line arguments. Unicode version. |
| 5056 | * Returns argc. Zero when something fails. |
| 5057 | */ |
| 5058 | int |
| 5059 | get_cmd_argsW(char ***argvp) |
| 5060 | { |
| 5061 | char **argv = NULL; |
| 5062 | int argc = 0; |
| 5063 | int i; |
| 5064 | |
| 5065 | ArglistW = CommandLineToArgvW(GetCommandLineW(), &nArgsW); |
| 5066 | if (ArglistW != NULL) |
| 5067 | { |
| 5068 | argv = malloc((nArgsW + 1) * sizeof(char *)); |
| 5069 | if (argv != NULL) |
| 5070 | { |
| 5071 | argc = nArgsW; |
| 5072 | argv[argc] = NULL; |
| 5073 | for (i = 0; i < argc; ++i) |
| 5074 | { |
| 5075 | int len; |
| 5076 | |
| 5077 | /* Convert each Unicode argument to the current codepage. */ |
| 5078 | WideCharToMultiByte_alloc(GetACP(), 0, |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 5079 | ArglistW[i], (int)wcslen(ArglistW[i]) + 1, |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 5080 | (LPSTR *)&argv[i], &len, 0, 0); |
| 5081 | if (argv[i] == NULL) |
| 5082 | { |
| 5083 | /* Out of memory, clear everything. */ |
| 5084 | while (i > 0) |
| 5085 | free(argv[--i]); |
| 5086 | free(argv); |
| 5087 | argc = 0; |
| 5088 | } |
| 5089 | } |
| 5090 | } |
| 5091 | } |
| 5092 | |
| 5093 | global_argc = argc; |
| 5094 | global_argv = argv; |
| 5095 | if (argc > 0) |
| 5096 | used_file_indexes = malloc(argc * sizeof(int)); |
| 5097 | |
| 5098 | if (argvp != NULL) |
| 5099 | *argvp = argv; |
| 5100 | return argc; |
| 5101 | } |
| 5102 | |
| 5103 | void |
| 5104 | free_cmd_argsW(void) |
| 5105 | { |
| 5106 | if (ArglistW != NULL) |
| 5107 | { |
| 5108 | GlobalFree(ArglistW); |
| 5109 | ArglistW = NULL; |
| 5110 | } |
| 5111 | } |
| 5112 | |
| 5113 | /* |
| 5114 | * Remember "name" is an argument that was added to the argument list. |
| 5115 | * This avoids that we have to re-parse the argument list when fix_arg_enc() |
| 5116 | * is called. |
| 5117 | */ |
| 5118 | void |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 5119 | used_file_arg(char *name, int literal, int full_path, int diff_mode) |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 5120 | { |
| 5121 | int i; |
| 5122 | |
| 5123 | if (used_file_indexes == NULL) |
| 5124 | return; |
| 5125 | for (i = used_file_argc + 1; i < global_argc; ++i) |
| 5126 | if (STRCMP(global_argv[i], name) == 0) |
| 5127 | { |
| 5128 | used_file_argc = i; |
| 5129 | used_file_indexes[used_file_count++] = i; |
| 5130 | break; |
| 5131 | } |
| 5132 | used_file_literal = literal; |
| 5133 | used_file_full_path = full_path; |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 5134 | used_file_diff_mode = diff_mode; |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 5135 | } |
| 5136 | |
| 5137 | /* |
| 5138 | * Remember the length of the argument list as it was. If it changes then we |
| 5139 | * leave it alone when 'encoding' is set. |
| 5140 | */ |
| 5141 | void |
| 5142 | set_alist_count(void) |
| 5143 | { |
| 5144 | used_alist_count = GARGCOUNT; |
| 5145 | } |
| 5146 | |
| 5147 | /* |
| 5148 | * Fix the encoding of the command line arguments. Invoked when 'encoding' |
| 5149 | * has been changed while starting up. Use the UCS-2 command line arguments |
| 5150 | * and convert them to 'encoding'. |
| 5151 | */ |
| 5152 | void |
| 5153 | fix_arg_enc(void) |
| 5154 | { |
| 5155 | int i; |
| 5156 | int idx; |
| 5157 | char_u *str; |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 5158 | int *fnum_list; |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 5159 | |
| 5160 | /* Safety checks: |
| 5161 | * - if argument count differs between the wide and non-wide argument |
| 5162 | * list, something must be wrong. |
| 5163 | * - the file name arguments must have been located. |
| 5164 | * - the length of the argument list wasn't changed by the user. |
| 5165 | */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 5166 | if (global_argc != nArgsW |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 5167 | || ArglistW == NULL |
| 5168 | || used_file_indexes == NULL |
| 5169 | || used_file_count == 0 |
| 5170 | || used_alist_count != GARGCOUNT) |
| 5171 | return; |
| 5172 | |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 5173 | /* Remember the buffer numbers for the arguments. */ |
| 5174 | fnum_list = (int *)alloc((int)sizeof(int) * GARGCOUNT); |
| 5175 | if (fnum_list == NULL) |
| 5176 | return; /* out of memory */ |
| 5177 | for (i = 0; i < GARGCOUNT; ++i) |
| 5178 | fnum_list[i] = GARGLIST[i].ae_fnum; |
| 5179 | |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 5180 | /* Clear the argument list. Make room for the new arguments. */ |
| 5181 | alist_clear(&global_alist); |
| 5182 | if (ga_grow(&global_alist.al_ga, used_file_count) == FAIL) |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 5183 | return; /* out of memory */ |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 5184 | |
| 5185 | for (i = 0; i < used_file_count; ++i) |
| 5186 | { |
| 5187 | idx = used_file_indexes[i]; |
Bram Moolenaar | 36f692d | 2008-11-20 16:10:17 +0000 | [diff] [blame] | 5188 | str = utf16_to_enc(ArglistW[idx], NULL); |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 5189 | if (str != NULL) |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 5190 | { |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 5191 | #ifdef FEAT_DIFF |
| 5192 | /* When using diff mode may need to concatenate file name to |
| 5193 | * directory name. Just like it's done in main(). */ |
| 5194 | if (used_file_diff_mode && mch_isdir(str) && GARGCOUNT > 0 |
| 5195 | && !mch_isdir(alist_name(&GARGLIST[0]))) |
| 5196 | { |
| 5197 | char_u *r; |
| 5198 | |
| 5199 | r = concat_fnames(str, gettail(alist_name(&GARGLIST[0])), TRUE); |
| 5200 | if (r != NULL) |
| 5201 | { |
| 5202 | vim_free(str); |
| 5203 | str = r; |
| 5204 | } |
| 5205 | } |
| 5206 | #endif |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 5207 | /* Re-use the old buffer by renaming it. When not using literal |
| 5208 | * names it's done by alist_expand() below. */ |
| 5209 | if (used_file_literal) |
| 5210 | buf_set_name(fnum_list[i], str); |
| 5211 | |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 5212 | alist_add(&global_alist, str, used_file_literal ? 2 : 0); |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 5213 | } |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 5214 | } |
| 5215 | |
| 5216 | if (!used_file_literal) |
| 5217 | { |
| 5218 | /* Now expand wildcards in the arguments. */ |
| 5219 | /* Temporarily add '(' and ')' to 'isfname'. These are valid |
| 5220 | * filename characters but are excluded from 'isfname' to make |
| 5221 | * "gf" work on a file name in parenthesis (e.g.: see vim.h). */ |
| 5222 | do_cmdline_cmd((char_u *)":let SaVe_ISF = &isf|set isf+=(,)"); |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 5223 | alist_expand(fnum_list, used_alist_count); |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 5224 | do_cmdline_cmd((char_u *)":let &isf = SaVe_ISF|unlet SaVe_ISF"); |
| 5225 | } |
| 5226 | |
| 5227 | /* If wildcard expansion failed, we are editing the first file of the |
| 5228 | * arglist and there is no file name: Edit the first argument now. */ |
| 5229 | if (curwin->w_arg_idx == 0 && curbuf->b_fname == NULL) |
| 5230 | { |
| 5231 | do_cmdline_cmd((char_u *)":rewind"); |
| 5232 | if (GARGCOUNT == 1 && used_file_full_path) |
| 5233 | (void)vim_chdirfile(alist_name(&GARGLIST[0])); |
| 5234 | } |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 5235 | |
| 5236 | set_alist_count(); |
Bram Moolenaar | 3fdfa4a | 2004-10-07 21:02:47 +0000 | [diff] [blame] | 5237 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5238 | #endif |