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