blob: 3d74bdc7a43176aa44e0b3ae7f30fa0bbec15383 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
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/*
11 * os_mswin.c
12 *
Bram Moolenaarcf7164a2016-02-20 13:55:06 +010013 * Routines for Win32.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014 */
15
Bram Moolenaar071d4272004-06-13 20:20:40 +000016#include "vim.h"
17
Bram Moolenaar071d4272004-06-13 20:20:40 +000018#include <sys/types.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000019#include <signal.h>
20#include <limits.h>
Bram Moolenaar82881492012-11-20 16:53:39 +010021#ifndef PROTO
22# include <process.h>
23#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024
25#undef chdir
26#ifdef __GNUC__
27# ifndef __MINGW32__
28# include <dirent.h>
29# endif
30#else
31# include <direct.h>
32#endif
33
Bram Moolenaar82881492012-11-20 16:53:39 +010034#ifndef PROTO
35# if defined(FEAT_TITLE) && !defined(FEAT_GUI_W32)
36# include <shellapi.h>
37# endif
38
39# if defined(FEAT_PRINTER) && !defined(FEAT_POSTSCRIPT)
40# include <dlgs.h>
Bram Moolenaarcea912a2016-10-12 14:20:24 +020041# include <winspool.h>
Bram Moolenaar82881492012-11-20 16:53:39 +010042# include <commdlg.h>
Bram Moolenaarb04a98f2016-12-01 20:32:29 +010043# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000044
Bram Moolenaar82881492012-11-20 16:53:39 +010045#endif /* PROTO */
Bram Moolenaar071d4272004-06-13 20:20:40 +000046
47#ifdef __MINGW32__
48# ifndef FROM_LEFT_1ST_BUTTON_PRESSED
49# define FROM_LEFT_1ST_BUTTON_PRESSED 0x0001
50# endif
51# ifndef RIGHTMOST_BUTTON_PRESSED
52# define RIGHTMOST_BUTTON_PRESSED 0x0002
53# endif
54# ifndef FROM_LEFT_2ND_BUTTON_PRESSED
55# define FROM_LEFT_2ND_BUTTON_PRESSED 0x0004
56# endif
57# ifndef FROM_LEFT_3RD_BUTTON_PRESSED
58# define FROM_LEFT_3RD_BUTTON_PRESSED 0x0008
59# endif
60# ifndef FROM_LEFT_4TH_BUTTON_PRESSED
61# define FROM_LEFT_4TH_BUTTON_PRESSED 0x0010
62# endif
63
64/*
65 * EventFlags
66 */
67# ifndef MOUSE_MOVED
68# define MOUSE_MOVED 0x0001
69# endif
70# ifndef DOUBLE_CLICK
71# define DOUBLE_CLICK 0x0002
72# endif
73#endif
74
75/*
76 * When generating prototypes for Win32 on Unix, these lines make the syntax
77 * errors disappear. They do not need to be correct.
78 */
79#ifdef PROTO
80#define WINAPI
81#define WINBASEAPI
82typedef int BOOL;
83typedef int CALLBACK;
84typedef int COLORREF;
85typedef int CONSOLE_CURSOR_INFO;
86typedef int COORD;
87typedef int DWORD;
88typedef int ENUMLOGFONT;
89typedef int HANDLE;
90typedef int HDC;
91typedef int HFONT;
92typedef int HICON;
93typedef int HWND;
94typedef int INPUT_RECORD;
95typedef int KEY_EVENT_RECORD;
96typedef int LOGFONT;
97typedef int LPARAM;
98typedef int LPBOOL;
99typedef int LPCSTR;
100typedef int LPCWSTR;
101typedef int LPSTR;
102typedef int LPTSTR;
103typedef int LPWSTR;
104typedef int LRESULT;
105typedef int MOUSE_EVENT_RECORD;
106typedef int NEWTEXTMETRIC;
107typedef int PACL;
108typedef int PRINTDLG;
109typedef int PSECURITY_DESCRIPTOR;
110typedef int PSID;
111typedef int SECURITY_INFORMATION;
112typedef int SHORT;
113typedef int SMALL_RECT;
114typedef int TEXTMETRIC;
115typedef int UINT;
116typedef int WCHAR;
Bram Moolenaarc447d8d2018-12-18 21:56:28 +0100117typedef int WNDENUMPROC;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118typedef int WORD;
119typedef int WPARAM;
120typedef void VOID;
121#endif
122
123/* Record all output and all keyboard & mouse input */
124/* #define MCH_WRITE_DUMP */
125
126#ifdef MCH_WRITE_DUMP
127FILE* fdDump = NULL;
128#endif
129
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130#ifndef FEAT_GUI_MSWIN
131extern char g_szOrigTitle[];
132#endif
133
134#ifdef FEAT_GUI
135extern HWND s_hwnd;
136#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137static HWND s_hwnd = 0; /* console window handle, set by GetConsoleHwnd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138#endif
139
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100140#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100141int WSInitialized = FALSE; /* WinSock is initialized */
142#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143
144/* Don't generate prototypes here, because some systems do have these
145 * functions. */
146#if defined(__GNUC__) && !defined(PROTO)
147# ifndef __MINGW32__
148int _stricoll(char *a, char *b)
149{
150 // the ANSI-ish correct way is to use strxfrm():
151 char a_buff[512], b_buff[512]; // file names, so this is enough on Win32
152 strxfrm(a_buff, a, 512);
153 strxfrm(b_buff, b, 512);
154 return strcoll(a_buff, b_buff);
155}
156
157char * _fullpath(char *buf, char *fname, int len)
158{
159 LPTSTR toss;
160
161 return (char *)GetFullPathName(fname, len, buf, &toss);
162}
163# endif
164
Bram Moolenaaraf62ff32013-03-19 14:48:29 +0100165# if !defined(__MINGW32__) || (__GNUC__ < 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166int _chdrive(int drive)
167{
168 char temp [3] = "-:";
169 temp[0] = drive + 'A' - 1;
170 return !SetCurrentDirectory(temp);
171}
Bram Moolenaaraf62ff32013-03-19 14:48:29 +0100172# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173#else
174# ifdef __BORLANDC__
175/* being a more ANSI compliant compiler, BorlandC doesn't define _stricoll:
176 * but it does in BC 5.02! */
177# if __BORLANDC__ < 0x502
178int _stricoll(char *a, char *b)
179{
180# if 1
181 // this is fast but not correct:
182 return stricmp(a, b);
183# else
184 // the ANSI-ish correct way is to use strxfrm():
185 char a_buff[512], b_buff[512]; // file names, so this is enough on Win32
186 strxfrm(a_buff, a, 512);
187 strxfrm(b_buff, b, 512);
188 return strcoll(a_buff, b_buff);
189# endif
190}
191# endif
192# endif
193#endif
194
195
196#if defined(FEAT_GUI_MSWIN) || defined(PROTO)
197/*
198 * GUI version of mch_exit().
199 * Shut down and exit with status `r'
200 * Careful: mch_exit() may be called before mch_init()!
201 */
202 void
203mch_exit(int r)
204{
Bram Moolenaar955f1982017-02-05 15:10:51 +0100205 exiting = TRUE;
206
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207 display_errors();
208
209 ml_close_all(TRUE); /* remove all memfiles */
210
211# ifdef FEAT_OLE
212 UninitOLE();
213# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100214# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215 if (WSInitialized)
216 {
217 WSInitialized = FALSE;
218 WSACleanup();
219 }
220# endif
221#ifdef DYNAMIC_GETTEXT
222 dyn_libintl_end();
223#endif
224
225 if (gui.in_use)
226 gui_exit(r);
Bram Moolenaar85c79d32007-02-20 01:59:20 +0000227
228#ifdef EXITFREE
229 free_all_mem();
230#endif
231
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232 exit(r);
233}
234
235#endif /* FEAT_GUI_MSWIN */
236
237
238/*
239 * Init the tables for toupper() and tolower().
240 */
241 void
242mch_early_init(void)
243{
244 int i;
245
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246 PlatformId();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000247
248 /* Init the tables for toupper() and tolower() */
249 for (i = 0; i < 256; ++i)
250 toupper_tab[i] = tolower_tab[i] = i;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100251 CharUpperBuff((LPSTR)toupper_tab, 256);
252 CharLowerBuff((LPSTR)tolower_tab, 256);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253}
254
255
256/*
257 * Return TRUE if the input comes from a terminal, FALSE otherwise.
258 */
259 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100260mch_input_isatty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261{
262#ifdef FEAT_GUI_MSWIN
263 return OK; /* GUI always has a tty */
264#else
265 if (isatty(read_cmd_fd))
266 return TRUE;
267 return FALSE;
268#endif
269}
270
271#ifdef FEAT_TITLE
272/*
273 * mch_settitle(): set titlebar of our window
274 */
275 void
276mch_settitle(
277 char_u *title,
278 char_u *icon)
279{
280# ifdef FEAT_GUI_MSWIN
281 gui_mch_settitle(title, icon);
282# else
283 if (title != NULL)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000284 {
285# ifdef FEAT_MBYTE
286 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
287 {
288 /* Convert the title from 'encoding' to the active codepage. */
Bram Moolenaar36f692d2008-11-20 16:10:17 +0000289 WCHAR *wp = enc_to_utf16(title, NULL);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000290
291 if (wp != NULL)
292 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200293 SetConsoleTitleW(wp);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000294 vim_free(wp);
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200295 return;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000296 }
297 }
298# endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100299 SetConsoleTitle((LPCSTR)title);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000300 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301# endif
302}
303
304
305/*
306 * Restore the window/icon title.
307 * which is one of:
Bram Moolenaar40385db2018-08-07 22:31:44 +0200308 * SAVE_RESTORE_TITLE: Just restore title
309 * SAVE_RESTORE_ICON: Just restore icon (which we don't have)
310 * SAVE_RESTORE_BOTH: Restore title and icon (which we don't have)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311 */
312 void
Bram Moolenaar1266d672017-02-01 13:43:36 +0100313mch_restore_title(int which UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314{
315#ifndef FEAT_GUI_MSWIN
Bram Moolenaar1df52d72014-10-15 22:50:10 +0200316 SetConsoleTitle(g_szOrigTitle);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317#endif
318}
319
320
321/*
322 * Return TRUE if we can restore the title (we can)
323 */
324 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100325mch_can_restore_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326{
327 return TRUE;
328}
329
330
331/*
332 * Return TRUE if we can restore the icon title (we can't)
333 */
334 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100335mch_can_restore_icon(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336{
337 return FALSE;
338}
339#endif /* FEAT_TITLE */
340
341
342/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000343 * Get absolute file name into buffer "buf" of length "len" bytes,
344 * turning all '/'s into '\\'s and getting the correct case of each component
345 * of the file name. Append a (back)slash to a directory name.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346 * When 'shellslash' set do it the other way around.
347 * Return OK or FAIL.
348 */
349 int
350mch_FullName(
351 char_u *fname,
352 char_u *buf,
353 int len,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100354 int force UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355{
356 int nResult = FAIL;
357
358#ifdef __BORLANDC__
359 if (*fname == NUL) /* Borland behaves badly here - make it consistent */
360 nResult = mch_dirname(buf, len);
361 else
362#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000364#ifdef FEAT_MBYTE
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200365 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000366 {
367 WCHAR *wname;
368 WCHAR wbuf[MAX_PATH];
369 char_u *cname = NULL;
370
371 /* Use the wide function:
372 * - convert the fname from 'encoding' to UCS2.
373 * - invoke _wfullpath()
374 * - convert the result from UCS2 to 'encoding'.
375 */
Bram Moolenaar36f692d2008-11-20 16:10:17 +0000376 wname = enc_to_utf16(fname, NULL);
Bram Moolenaar374bf022014-11-05 19:33:24 +0100377 if (wname != NULL && _wfullpath(wbuf, wname, MAX_PATH) != NULL)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000378 {
Bram Moolenaar36f692d2008-11-20 16:10:17 +0000379 cname = utf16_to_enc((short_u *)wbuf, NULL);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000380 if (cname != NULL)
381 {
Bram Moolenaarfe3ca8d2005-07-18 21:43:02 +0000382 vim_strncpy(buf, cname, len - 1);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000383 nResult = OK;
384 }
385 }
386 vim_free(wname);
387 vim_free(cname);
388 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000389 if (nResult == FAIL) /* fall back to non-wide function */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000391 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100392 if (_fullpath((char *)buf, (const char *)fname, len - 1) == NULL)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000393 {
Bram Moolenaarfe3ca8d2005-07-18 21:43:02 +0000394 /* failed, use relative path name */
395 vim_strncpy(buf, fname, len - 1);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000396 }
397 else
398 nResult = OK;
399 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401
402#ifdef USE_FNAME_CASE
403 fname_case(buf, len);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000404#else
405 slash_adjust(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406#endif
407
408 return nResult;
409}
410
411
412/*
413 * Return TRUE if "fname" does not depend on the current directory.
414 */
415 int
416mch_isFullName(char_u *fname)
417{
Bram Moolenaard2a203b2013-08-30 16:51:18 +0200418#ifdef FEAT_MBYTE
419 /* WinNT and later can use _MAX_PATH wide characters for a pathname, which
420 * means that the maximum pathname is _MAX_PATH * 3 bytes when 'enc' is
421 * UTF-8. */
422 char szName[_MAX_PATH * 3 + 1];
423#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424 char szName[_MAX_PATH + 1];
Bram Moolenaard2a203b2013-08-30 16:51:18 +0200425#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426
427 /* A name like "d:/foo" and "//server/share" is absolute */
428 if ((fname[0] && fname[1] == ':' && (fname[2] == '/' || fname[2] == '\\'))
429 || (fname[0] == fname[1] && (fname[0] == '/' || fname[0] == '\\')))
430 return TRUE;
431
432 /* A name that can't be made absolute probably isn't absolute. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100433 if (mch_FullName(fname, (char_u *)szName, sizeof(szName) - 1, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434 return FALSE;
435
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100436 return pathcmp((const char *)fname, (const char *)szName, -1) == 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437}
438
439/*
440 * Replace all slashes by backslashes.
441 * This used to be the other way around, but MS-DOS sometimes has problems
442 * with slashes (e.g. in a command name). We can't have mixed slashes and
443 * backslashes, because comparing file names will not work correctly. The
444 * commands that use a file name should try to avoid the need to type a
445 * backslash twice.
446 * When 'shellslash' set do it the other way around.
Bram Moolenaarb4f6a462015-10-13 19:43:17 +0200447 * When the path looks like a URL leave it unmodified.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448 */
449 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100450slash_adjust(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451{
Bram Moolenaarb4f6a462015-10-13 19:43:17 +0200452 if (path_with_url(p))
453 return;
Bram Moolenaar39d21e32017-08-05 23:09:31 +0200454
455 if (*p == '`')
456 {
Bram Moolenaar116a0f82017-08-07 21:17:57 +0200457 size_t len = STRLEN(p);
458
Bram Moolenaar39d21e32017-08-05 23:09:31 +0200459 /* don't replace backslash in backtick quoted strings */
Bram Moolenaar39d21e32017-08-05 23:09:31 +0200460 if (len > 2 && *(p + len - 1) == '`')
461 return;
462 }
463
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +0000464 while (*p)
465 {
466 if (*p == psepcN)
467 *p = psepc;
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100468 MB_PTR_ADV(p);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +0000469 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470}
471
Bram Moolenaar8767f522016-07-01 17:17:39 +0200472/* Use 64-bit stat functions if available. */
473#ifdef HAVE_STAT64
474# undef stat
475# undef _stat
476# undef _wstat
477# undef _fstat
478# define stat _stat64
479# define _stat _stat64
480# define _wstat _wstat64
481# define _fstat _fstat64
482#endif
483
Bram Moolenaar9d1685d2014-01-14 12:18:45 +0100484#if (defined(_MSC_VER) && (_MSC_VER >= 1300)) || defined(__MINGW32__)
Bram Moolenaar8962fda2013-09-29 19:05:21 +0200485# define OPEN_OH_ARGTYPE intptr_t
486#else
487# define OPEN_OH_ARGTYPE long
488#endif
489
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200490 static int
Bram Moolenaar8767f522016-07-01 17:17:39 +0200491stat_symlink_aware(const char *name, stat_T *stp)
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200492{
Bram Moolenaarfce7b3d2016-01-19 19:00:32 +0100493#if (defined(_MSC_VER) && (_MSC_VER < 1900)) || defined(__MINGW32__)
494 /* Work around for VC12 or earlier (and MinGW). stat() can't handle
495 * symlinks properly.
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200496 * VC9 or earlier: stat() doesn't support a symlink at all. It retrieves
497 * status of a symlink itself.
498 * VC10: stat() supports a symlink to a normal file, but it doesn't support
Bram Moolenaarfce7b3d2016-01-19 19:00:32 +0100499 * a symlink to a directory (always returns an error).
500 * VC11 and VC12: stat() doesn't return an error for a symlink to a
501 * directory, but it doesn't set S_IFDIR flag.
502 * MinGW: Same as VC9. */
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200503 WIN32_FIND_DATA findData;
504 HANDLE hFind, h;
505 DWORD attr = 0;
506 BOOL is_symlink = FALSE;
507
508 hFind = FindFirstFile(name, &findData);
509 if (hFind != INVALID_HANDLE_VALUE)
510 {
511 attr = findData.dwFileAttributes;
512 if ((attr & FILE_ATTRIBUTE_REPARSE_POINT)
513 && (findData.dwReserved0 == IO_REPARSE_TAG_SYMLINK))
514 is_symlink = TRUE;
515 FindClose(hFind);
516 }
517 if (is_symlink)
518 {
519 h = CreateFile(name, FILE_READ_ATTRIBUTES,
520 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
521 OPEN_EXISTING,
522 (attr & FILE_ATTRIBUTE_DIRECTORY)
523 ? FILE_FLAG_BACKUP_SEMANTICS : 0,
524 NULL);
525 if (h != INVALID_HANDLE_VALUE)
526 {
527 int fd, n;
528
Bram Moolenaar8962fda2013-09-29 19:05:21 +0200529 fd = _open_osfhandle((OPEN_OH_ARGTYPE)h, _O_RDONLY);
Bram Moolenaar8767f522016-07-01 17:17:39 +0200530 n = _fstat(fd, (struct _stat *)stp);
Bram Moolenaarfce7b3d2016-01-19 19:00:32 +0100531 if ((n == 0) && (attr & FILE_ATTRIBUTE_DIRECTORY))
532 stp->st_mode = (stp->st_mode & ~S_IFREG) | S_IFDIR;
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200533 _close(fd);
534 return n;
535 }
536 }
537#endif
538 return stat(name, stp);
539}
540
541#ifdef FEAT_MBYTE
542 static int
Bram Moolenaar8767f522016-07-01 17:17:39 +0200543wstat_symlink_aware(const WCHAR *name, stat_T *stp)
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200544{
Bram Moolenaarfce7b3d2016-01-19 19:00:32 +0100545# if (defined(_MSC_VER) && (_MSC_VER < 1900)) || defined(__MINGW32__)
546 /* Work around for VC12 or earlier (and MinGW). _wstat() can't handle
547 * symlinks properly.
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200548 * VC9 or earlier: _wstat() doesn't support a symlink at all. It retrieves
549 * status of a symlink itself.
550 * VC10: _wstat() supports a symlink to a normal file, but it doesn't
Bram Moolenaarfce7b3d2016-01-19 19:00:32 +0100551 * support a symlink to a directory (always returns an error).
552 * VC11 and VC12: _wstat() doesn't return an error for a symlink to a
553 * directory, but it doesn't set S_IFDIR flag.
554 * MinGW: Same as VC9. */
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200555 int n;
556 BOOL is_symlink = FALSE;
557 HANDLE hFind, h;
558 DWORD attr = 0;
559 WIN32_FIND_DATAW findDataW;
560
561 hFind = FindFirstFileW(name, &findDataW);
562 if (hFind != INVALID_HANDLE_VALUE)
563 {
564 attr = findDataW.dwFileAttributes;
565 if ((attr & FILE_ATTRIBUTE_REPARSE_POINT)
566 && (findDataW.dwReserved0 == IO_REPARSE_TAG_SYMLINK))
567 is_symlink = TRUE;
568 FindClose(hFind);
569 }
570 if (is_symlink)
571 {
572 h = CreateFileW(name, FILE_READ_ATTRIBUTES,
573 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
574 OPEN_EXISTING,
575 (attr & FILE_ATTRIBUTE_DIRECTORY)
576 ? FILE_FLAG_BACKUP_SEMANTICS : 0,
577 NULL);
578 if (h != INVALID_HANDLE_VALUE)
579 {
580 int fd;
581
Bram Moolenaar8962fda2013-09-29 19:05:21 +0200582 fd = _open_osfhandle((OPEN_OH_ARGTYPE)h, _O_RDONLY);
Bram Moolenaar8767f522016-07-01 17:17:39 +0200583 n = _fstat(fd, (struct _stat *)stp);
Bram Moolenaarfce7b3d2016-01-19 19:00:32 +0100584 if ((n == 0) && (attr & FILE_ATTRIBUTE_DIRECTORY))
585 stp->st_mode = (stp->st_mode & ~S_IFREG) | S_IFDIR;
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200586 _close(fd);
587 return n;
588 }
589 }
590# endif
Bram Moolenaar8767f522016-07-01 17:17:39 +0200591 return _wstat(name, (struct _stat *)stp);
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200592}
593#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594
595/*
596 * stat() can't handle a trailing '/' or '\', remove it first.
597 */
598 int
Bram Moolenaar8767f522016-07-01 17:17:39 +0200599vim_stat(const char *name, stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600{
Bram Moolenaard2a203b2013-08-30 16:51:18 +0200601#ifdef FEAT_MBYTE
602 /* WinNT and later can use _MAX_PATH wide characters for a pathname, which
603 * means that the maximum pathname is _MAX_PATH * 3 bytes when 'enc' is
604 * UTF-8. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100605 char_u buf[_MAX_PATH * 3 + 1];
Bram Moolenaard2a203b2013-08-30 16:51:18 +0200606#else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100607 char_u buf[_MAX_PATH + 1];
Bram Moolenaard2a203b2013-08-30 16:51:18 +0200608#endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100609 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610
Bram Moolenaard2a203b2013-08-30 16:51:18 +0200611 vim_strncpy((char_u *)buf, (char_u *)name, sizeof(buf) - 1);
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100612 p = buf + STRLEN(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613 if (p > buf)
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100614 MB_PTR_BACK(buf, p);
Bram Moolenaarb1cb35f2014-01-10 13:05:20 +0100615
616 /* Remove trailing '\\' except root path. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617 if (p > buf && (*p == '\\' || *p == '/') && p[-1] != ':')
618 *p = NUL;
Bram Moolenaarb1cb35f2014-01-10 13:05:20 +0100619
620 if ((buf[0] == '\\' && buf[1] == '\\') || (buf[0] == '/' && buf[1] == '/'))
621 {
622 /* UNC root path must be followed by '\\'. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100623 p = vim_strpbrk(buf + 2, (char_u *)"\\/");
Bram Moolenaarb1cb35f2014-01-10 13:05:20 +0100624 if (p != NULL)
625 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100626 p = vim_strpbrk(p + 1, (char_u *)"\\/");
Bram Moolenaarb1cb35f2014-01-10 13:05:20 +0100627 if (p == NULL)
628 STRCAT(buf, "\\");
629 }
630 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631#ifdef FEAT_MBYTE
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200632 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 {
Bram Moolenaar36f692d2008-11-20 16:10:17 +0000634 WCHAR *wp = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635 int n;
636
637 if (wp != NULL)
638 {
Bram Moolenaar8767f522016-07-01 17:17:39 +0200639 n = wstat_symlink_aware(wp, stp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 vim_free(wp);
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200641 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 }
643 }
644#endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100645 return stat_symlink_aware((char *)buf, stp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646}
647
648#if defined(FEAT_GUI_MSWIN) || defined(PROTO)
649 void
Bram Moolenaar1266d672017-02-01 13:43:36 +0100650mch_settmode(int tmode UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651{
652 /* nothing to do */
653}
654
655 int
656mch_get_shellsize(void)
657{
658 /* never used */
659 return OK;
660}
661
662 void
663mch_set_shellsize(void)
664{
665 /* never used */
666}
667
668/*
669 * Rows and/or Columns has changed.
670 */
671 void
672mch_new_shellsize(void)
673{
674 /* never used */
675}
676
677#endif
678
679/*
680 * We have no job control, so fake it by starting a new shell.
681 */
682 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100683mch_suspend(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684{
685 suspend_shell();
686}
687
688#if defined(USE_MCH_ERRMSG) || defined(PROTO)
689
690#ifdef display_errors
691# undef display_errors
692#endif
693
694/*
695 * Display the saved error message(s).
696 */
697 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100698display_errors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699{
700 char *p;
701
702 if (error_ga.ga_data != NULL)
703 {
704 /* avoid putting up a message box with blanks only */
705 for (p = (char *)error_ga.ga_data; *p; ++p)
706 if (!isspace(*p))
707 {
Bram Moolenaarc17ef8e2006-03-25 21:48:58 +0000708 (void)gui_mch_dialog(
709#ifdef FEAT_GUI
710 gui.starting ? VIM_INFO :
711#endif
712 VIM_ERROR,
713#ifdef FEAT_GUI
714 gui.starting ? (char_u *)_("Message") :
715#endif
716 (char_u *)_("Error"),
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100717 (char_u *)p, (char_u *)_("&Ok"),
718 1, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719 break;
720 }
721 ga_clear(&error_ga);
722 }
723}
724#endif
725
726
727/*
728 * Return TRUE if "p" contain a wildcard that can be expanded by
729 * dos_expandpath().
730 */
731 int
732mch_has_exp_wildcard(char_u *p)
733{
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100734 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735 {
736 if (vim_strchr((char_u *)"?*[", *p) != NULL
737 || (*p == '~' && p[1] != NUL))
738 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 }
740 return FALSE;
741}
742
743/*
744 * Return TRUE if "p" contain a wildcard or a "~1" kind of thing (could be a
745 * shortened file name).
746 */
747 int
748mch_has_wildcard(char_u *p)
749{
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100750 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 {
752 if (vim_strchr((char_u *)
753# ifdef VIM_BACKTICK
754 "?*$[`"
755# else
756 "?*$["
757# endif
758 , *p) != NULL
759 || (*p == '~' && p[1] != NUL))
760 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 }
762 return FALSE;
763}
764
765
766/*
767 * The normal _chdir() does not change the default drive. This one does.
768 * Returning 0 implies success; -1 implies failure.
769 */
770 int
771mch_chdir(char *path)
772{
773 if (path[0] == NUL) /* just checking... */
774 return -1;
775
Bram Moolenaara2974d72009-07-14 16:38:36 +0000776 if (p_verbose >= 5)
777 {
778 verbose_enter();
779 smsg((char_u *)"chdir(%s)", path);
780 verbose_leave();
781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 if (isalpha(path[0]) && path[1] == ':') /* has a drive name */
783 {
784 /* If we can change to the drive, skip that part of the path. If we
785 * can't then the current directory may be invalid, try using chdir()
786 * with the whole path. */
787 if (_chdrive(TOLOWER_ASC(path[0]) - 'a' + 1) == 0)
788 path += 2;
789 }
790
791 if (*path == NUL) /* drive name only */
792 return 0;
793
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000794#ifdef FEAT_MBYTE
795 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
796 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100797 WCHAR *p = enc_to_utf16((char_u *)path, NULL);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000798 int n;
799
800 if (p != NULL)
801 {
802 n = _wchdir(p);
803 vim_free(p);
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200804 return n;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000805 }
806 }
807#endif
808
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 return chdir(path); /* let the normal chdir() do the rest */
810}
811
812
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813#ifdef FEAT_GUI_MSWIN
814/*
815 * return non-zero if a character is available
816 */
817 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100818mch_char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819{
820 /* never used */
821 return TRUE;
822}
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200823
824# if defined(FEAT_TERMINAL) || defined(PROTO)
825/*
826 * Check for any pending input or messages.
827 */
828 int
829mch_check_messages(void)
830{
831 /* TODO: check for messages */
832 return TRUE;
833}
834# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835#endif
836
837
838/*
839 * set screen mode, always fails.
840 */
841 int
Bram Moolenaar1266d672017-02-01 13:43:36 +0100842mch_screenmode(char_u *arg UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843{
844 EMSG(_(e_screenmode));
845 return FAIL;
846}
847
848
849#if defined(FEAT_LIBCALL) || defined(PROTO)
850/*
851 * Call a DLL routine which takes either a string or int param
852 * and returns an allocated string.
853 * Return OK if it worked, FAIL if not.
854 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855typedef LPTSTR (*MYSTRPROCSTR)(LPTSTR);
856typedef LPTSTR (*MYINTPROCSTR)(int);
857typedef int (*MYSTRPROCINT)(LPTSTR);
858typedef int (*MYINTPROCINT)(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860/*
861 * Check if a pointer points to a valid NUL terminated string.
862 * Return the length of the string, including terminating NUL.
863 * Returns 0 for an invalid pointer, 1 for an empty string.
864 */
865 static size_t
866check_str_len(char_u *str)
867{
868 SYSTEM_INFO si;
869 MEMORY_BASIC_INFORMATION mbi;
870 size_t length = 0;
871 size_t i;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100872 const char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873
874 /* get page size */
875 GetSystemInfo(&si);
876
877 /* get memory information */
878 if (VirtualQuery(str, &mbi, sizeof(mbi)))
879 {
880 /* pre cast these (typing savers) */
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000881 long_u dwStr = (long_u)str;
882 long_u dwBaseAddress = (long_u)mbi.BaseAddress;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883
884 /* get start address of page that str is on */
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000885 long_u strPage = dwStr - (dwStr - dwBaseAddress) % si.dwPageSize;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886
887 /* get length from str to end of page */
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000888 long_u pageLength = si.dwPageSize - (dwStr - strPage);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889
Bram Moolenaar442b4222010-05-24 21:34:22 +0200890 for (p = str; !IsBadReadPtr(p, (UINT)pageLength);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 p += pageLength, pageLength = si.dwPageSize)
892 for (i = 0; i < pageLength; ++i, ++length)
893 if (p[i] == NUL)
894 return length + 1;
895 }
896
897 return 0;
898}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200900/*
901 * Passed to do_in_runtimepath() to load a vim.ico file.
902 */
903 static void
904mch_icon_load_cb(char_u *fname, void *cookie)
905{
906 HANDLE *h = (HANDLE *)cookie;
907
908 *h = LoadImage(NULL,
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100909 (LPSTR)fname,
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200910 IMAGE_ICON,
911 64,
912 64,
913 LR_LOADFROMFILE | LR_LOADMAP3DCOLORS);
914}
915
916/*
917 * Try loading an icon file from 'runtimepath'.
918 */
919 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100920mch_icon_load(HANDLE *iconp)
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200921{
922 return do_in_runtimepath((char_u *)"bitmaps/vim.ico",
Bram Moolenaar7f8989d2016-03-12 22:11:39 +0100923 0, mch_icon_load_cb, iconp);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200924}
925
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 int
927mch_libcall(
928 char_u *libname,
929 char_u *funcname,
930 char_u *argstring, /* NULL when using a argint */
931 int argint,
932 char_u **string_result,/* NULL when using number_result */
933 int *number_result)
934{
935 HINSTANCE hinstLib;
936 MYSTRPROCSTR ProcAdd;
937 MYINTPROCSTR ProcAddI;
938 char_u *retval_str = NULL;
939 int retval_int = 0;
940 size_t len;
941
942 BOOL fRunTimeLinkSuccess = FALSE;
943
944 // Get a handle to the DLL module.
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100945 hinstLib = vimLoadLib((char *)libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946
947 // If the handle is valid, try to get the function address.
948 if (hinstLib != NULL)
949 {
950#ifdef HAVE_TRY_EXCEPT
951 __try
952 {
953#endif
954 if (argstring != NULL)
955 {
956 /* Call with string argument */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100957 ProcAdd = (MYSTRPROCSTR)GetProcAddress(hinstLib, (LPCSTR)funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 if ((fRunTimeLinkSuccess = (ProcAdd != NULL)) != 0)
959 {
960 if (string_result == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100961 retval_int = ((MYSTRPROCINT)ProcAdd)((LPSTR)argstring);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100963 retval_str = (char_u *)(ProcAdd)((LPSTR)argstring);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 }
965 }
966 else
967 {
968 /* Call with number argument */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100969 ProcAddI = (MYINTPROCSTR) GetProcAddress(hinstLib, (LPCSTR)funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 if ((fRunTimeLinkSuccess = (ProcAddI != NULL)) != 0)
971 {
972 if (string_result == NULL)
973 retval_int = ((MYINTPROCINT)ProcAddI)(argint);
974 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100975 retval_str = (char_u *)(ProcAddI)(argint);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 }
977 }
978
979 // Save the string before we free the library.
980 // Assume that a "1" result is an illegal pointer.
981 if (string_result == NULL)
982 *number_result = retval_int;
983 else if (retval_str != NULL
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100984 && (len = check_str_len(retval_str)) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 {
986 *string_result = lalloc((long_u)len, TRUE);
987 if (*string_result != NULL)
988 mch_memmove(*string_result, retval_str, len);
989 }
990
991#ifdef HAVE_TRY_EXCEPT
992 }
993 __except(EXCEPTION_EXECUTE_HANDLER)
994 {
995 if (GetExceptionCode() == EXCEPTION_STACK_OVERFLOW)
996 RESETSTKOFLW();
997 fRunTimeLinkSuccess = 0;
998 }
999#endif
1000
1001 // Free the DLL module.
1002 (void)FreeLibrary(hinstLib);
1003 }
1004
1005 if (!fRunTimeLinkSuccess)
1006 {
1007 EMSG2(_(e_libcall), funcname);
1008 return FAIL;
1009 }
1010
1011 return OK;
1012}
1013#endif
1014
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015/*
1016 * Debugging helper: expose the MCH_WRITE_DUMP stuff to other modules
1017 */
1018 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01001019DumpPutS(const char *psz UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020{
1021# ifdef MCH_WRITE_DUMP
1022 if (fdDump)
1023 {
1024 fputs(psz, fdDump);
1025 if (psz[strlen(psz) - 1] != '\n')
1026 fputc('\n', fdDump);
1027 fflush(fdDump);
1028 }
1029# endif
1030}
1031
1032#ifdef _DEBUG
1033
1034void __cdecl
1035Trace(
1036 char *pszFormat,
1037 ...)
1038{
1039 CHAR szBuff[2048];
1040 va_list args;
1041
1042 va_start(args, pszFormat);
1043 vsprintf(szBuff, pszFormat, args);
1044 va_end(args);
1045
1046 OutputDebugString(szBuff);
1047}
1048
1049#endif //_DEBUG
1050
Bram Moolenaar843ee412004-06-30 16:16:41 +00001051#if !defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001052# ifdef FEAT_TITLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053extern HWND g_hWnd; /* This is in os_win32.c. */
1054# endif
1055
1056/*
1057 * Showing the printer dialog is tricky since we have no GUI
1058 * window to parent it. The following routines are needed to
1059 * get the window parenting and Z-order to work properly.
1060 */
1061 static void
1062GetConsoleHwnd(void)
1063{
1064# define MY_BUFSIZE 1024 // Buffer size for console window titles.
1065
1066 char pszNewWindowTitle[MY_BUFSIZE]; // Contains fabricated WindowTitle.
1067 char pszOldWindowTitle[MY_BUFSIZE]; // Contains original WindowTitle.
1068
1069 /* Skip if it's already set. */
1070 if (s_hwnd != 0)
1071 return;
1072
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001073# ifdef FEAT_TITLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 /* Window handle may have been found by init code (Windows NT only) */
1075 if (g_hWnd != 0)
1076 {
1077 s_hwnd = g_hWnd;
1078 return;
1079 }
1080# endif
1081
1082 GetConsoleTitle(pszOldWindowTitle, MY_BUFSIZE);
1083
1084 wsprintf(pszNewWindowTitle, "%s/%d/%d",
1085 pszOldWindowTitle,
1086 GetTickCount(),
1087 GetCurrentProcessId());
1088 SetConsoleTitle(pszNewWindowTitle);
1089 Sleep(40);
1090 s_hwnd = FindWindow(NULL, pszNewWindowTitle);
1091
1092 SetConsoleTitle(pszOldWindowTitle);
1093}
Bram Moolenaar843ee412004-06-30 16:16:41 +00001094
1095/*
1096 * Console implementation of ":winpos".
1097 */
1098 int
1099mch_get_winpos(int *x, int *y)
1100{
1101 RECT rect;
1102
1103 GetConsoleHwnd();
1104 GetWindowRect(s_hwnd, &rect);
1105 *x = rect.left;
1106 *y = rect.top;
1107 return OK;
1108}
1109
1110/*
1111 * Console implementation of ":winpos x y".
1112 */
1113 void
1114mch_set_winpos(int x, int y)
1115{
1116 GetConsoleHwnd();
1117 SetWindowPos(s_hwnd, NULL, x, y, 0, 0,
1118 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
1119}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120#endif
1121
1122#if (defined(FEAT_PRINTER) && !defined(FEAT_POSTSCRIPT)) || defined(PROTO)
1123
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124/*=================================================================
1125 * Win32 printer stuff
1126 */
1127
1128static HFONT prt_font_handles[2][2][2];
1129static PRINTDLG prt_dlg;
1130static const int boldface[2] = {FW_REGULAR, FW_BOLD};
1131static TEXTMETRIC prt_tm;
1132static int prt_line_height;
1133static int prt_number_width;
1134static int prt_left_margin;
1135static int prt_right_margin;
1136static int prt_top_margin;
1137static char_u szAppName[] = TEXT("VIM");
1138static HWND hDlgPrint;
1139static int *bUserAbort = NULL;
1140static char_u *prt_name = NULL;
1141
1142/* Defines which are also in vim.rc. */
1143#define IDC_BOX1 400
1144#define IDC_PRINTTEXT1 401
1145#define IDC_PRINTTEXT2 402
1146#define IDC_PROGRESS 403
1147
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001148#if !defined(FEAT_MBYTE)
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001149# define vimSetDlgItemText(h, i, s) SetDlgItemText(h, i, s)
1150#else
1151 static BOOL
1152vimSetDlgItemText(HWND hDlg, int nIDDlgItem, char_u *s)
1153{
1154 WCHAR *wp = NULL;
1155 BOOL ret;
1156
1157 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
1158 {
1159 wp = enc_to_utf16(s, NULL);
1160 }
1161 if (wp != NULL)
1162 {
1163 ret = SetDlgItemTextW(hDlg, nIDDlgItem, wp);
1164 vim_free(wp);
1165 return ret;
1166 }
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001167 return SetDlgItemText(hDlg, nIDDlgItem, (LPCSTR)s);
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001168}
1169#endif
1170
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171/*
1172 * Convert BGR to RGB for Windows GDI calls
1173 */
1174 static COLORREF
1175swap_me(COLORREF colorref)
1176{
1177 int temp;
1178 char *ptr = (char *)&colorref;
1179
1180 temp = *(ptr);
1181 *(ptr ) = *(ptr + 2);
1182 *(ptr + 2) = temp;
1183 return colorref;
1184}
1185
Bram Moolenaared39e1d2008-08-09 17:55:22 +00001186/* Attempt to make this work for old and new compilers */
Bram Moolenaar9f733d12011-09-21 20:09:42 +02001187#if !defined(_WIN64) && (!defined(_MSC_VER) || _MSC_VER < 1300)
Bram Moolenaared39e1d2008-08-09 17:55:22 +00001188# define PDP_RETVAL BOOL
1189#else
1190# define PDP_RETVAL INT_PTR
1191#endif
1192
Bram Moolenaared39e1d2008-08-09 17:55:22 +00001193 static PDP_RETVAL CALLBACK
Bram Moolenaar1266d672017-02-01 13:43:36 +01001194PrintDlgProc(
1195 HWND hDlg,
1196 UINT message,
1197 WPARAM wParam UNUSED,
1198 LPARAM lParam UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199{
1200#ifdef FEAT_GETTEXT
1201 NONCLIENTMETRICS nm;
1202 static HFONT hfont;
1203#endif
1204
1205 switch (message)
1206 {
1207 case WM_INITDIALOG:
1208#ifdef FEAT_GETTEXT
1209 nm.cbSize = sizeof(NONCLIENTMETRICS);
1210 if (SystemParametersInfo(
1211 SPI_GETNONCLIENTMETRICS,
1212 sizeof(NONCLIENTMETRICS),
1213 &nm,
1214 0))
1215 {
1216 char buff[MAX_PATH];
1217 int i;
1218
1219 /* Translate the dialog texts */
1220 hfont = CreateFontIndirect(&nm.lfMessageFont);
1221 for (i = IDC_PRINTTEXT1; i <= IDC_PROGRESS; i++)
1222 {
1223 SendDlgItemMessage(hDlg, i, WM_SETFONT, (WPARAM)hfont, 1);
1224 if (GetDlgItemText(hDlg,i, buff, sizeof(buff)))
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001225 vimSetDlgItemText(hDlg,i, (char_u *)_(buff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 }
1227 SendDlgItemMessage(hDlg, IDCANCEL,
1228 WM_SETFONT, (WPARAM)hfont, 1);
1229 if (GetDlgItemText(hDlg,IDCANCEL, buff, sizeof(buff)))
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001230 vimSetDlgItemText(hDlg,IDCANCEL, (char_u *)_(buff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 }
1232#endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001233 SetWindowText(hDlg, (LPCSTR)szAppName);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234 if (prt_name != NULL)
1235 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001236 vimSetDlgItemText(hDlg, IDC_PRINTTEXT2, (char_u *)prt_name);
Bram Moolenaard23a8232018-02-10 18:45:26 +01001237 VIM_CLEAR(prt_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 }
1239 EnableMenuItem(GetSystemMenu(hDlg, FALSE), SC_CLOSE, MF_GRAYED);
1240#ifndef FEAT_GUI
1241 BringWindowToTop(s_hwnd);
1242#endif
1243 return TRUE;
1244
1245 case WM_COMMAND:
1246 *bUserAbort = TRUE;
1247 EnableWindow(GetParent(hDlg), TRUE);
1248 DestroyWindow(hDlg);
1249 hDlgPrint = NULL;
1250#ifdef FEAT_GETTEXT
1251 DeleteObject(hfont);
1252#endif
1253 return TRUE;
1254 }
1255 return FALSE;
1256}
1257
1258 static BOOL CALLBACK
Bram Moolenaar1266d672017-02-01 13:43:36 +01001259AbortProc(HDC hdcPrn UNUSED, int iCode UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260{
1261 MSG msg;
1262
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02001263 while (!*bUserAbort && pPeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 {
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02001265 if (!hDlgPrint || !pIsDialogMessage(hDlgPrint, &msg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 {
1267 TranslateMessage(&msg);
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02001268 pDispatchMessage(&msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 }
1270 }
1271 return !*bUserAbort;
1272}
1273
1274#ifndef FEAT_GUI
1275
Bram Moolenaar26fdd7d2011-11-30 13:42:44 +01001276 static UINT_PTR CALLBACK
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277PrintHookProc(
1278 HWND hDlg, // handle to dialog box
1279 UINT uiMsg, // message identifier
1280 WPARAM wParam, // message parameter
1281 LPARAM lParam // message parameter
1282 )
1283{
1284 HWND hwndOwner;
1285 RECT rc, rcDlg, rcOwner;
1286 PRINTDLG *pPD;
1287
1288 if (uiMsg == WM_INITDIALOG)
1289 {
1290 // Get the owner window and dialog box rectangles.
1291 if ((hwndOwner = GetParent(hDlg)) == NULL)
1292 hwndOwner = GetDesktopWindow();
1293
1294 GetWindowRect(hwndOwner, &rcOwner);
1295 GetWindowRect(hDlg, &rcDlg);
1296 CopyRect(&rc, &rcOwner);
1297
1298 // Offset the owner and dialog box rectangles so that
1299 // right and bottom values represent the width and
1300 // height, and then offset the owner again to discard
1301 // space taken up by the dialog box.
1302
1303 OffsetRect(&rcDlg, -rcDlg.left, -rcDlg.top);
1304 OffsetRect(&rc, -rc.left, -rc.top);
1305 OffsetRect(&rc, -rcDlg.right, -rcDlg.bottom);
1306
1307 // The new position is the sum of half the remaining
1308 // space and the owner's original position.
1309
1310 SetWindowPos(hDlg,
1311 HWND_TOP,
1312 rcOwner.left + (rc.right / 2),
1313 rcOwner.top + (rc.bottom / 2),
1314 0, 0, // ignores size arguments
1315 SWP_NOSIZE);
1316
1317 /* tackle the printdlg copiesctrl problem */
1318 pPD = (PRINTDLG *)lParam;
1319 pPD->nCopies = (WORD)pPD->lCustData;
1320 SetDlgItemInt( hDlg, edt3, pPD->nCopies, FALSE );
1321 /* Bring the window to top */
1322 BringWindowToTop(GetParent(hDlg));
1323 SetForegroundWindow(hDlg);
1324 }
1325
1326 return FALSE;
1327}
1328#endif
1329
1330 void
1331mch_print_cleanup(void)
1332{
1333 int pifItalic;
1334 int pifBold;
1335 int pifUnderline;
1336
1337 for (pifBold = 0; pifBold <= 1; pifBold++)
1338 for (pifItalic = 0; pifItalic <= 1; pifItalic++)
1339 for (pifUnderline = 0; pifUnderline <= 1; pifUnderline++)
1340 DeleteObject(prt_font_handles[pifBold][pifItalic][pifUnderline]);
1341
1342 if (prt_dlg.hDC != NULL)
1343 DeleteDC(prt_dlg.hDC);
1344 if (!*bUserAbort)
1345 SendMessage(hDlgPrint, WM_COMMAND, 0, 0);
1346}
1347
1348 static int
1349to_device_units(int idx, int dpi, int physsize, int offset, int def_number)
1350{
1351 int ret = 0;
1352 int u;
1353 int nr;
1354
1355 u = prt_get_unit(idx);
1356 if (u == PRT_UNIT_NONE)
1357 {
1358 u = PRT_UNIT_PERC;
1359 nr = def_number;
1360 }
1361 else
1362 nr = printer_opts[idx].number;
1363
1364 switch (u)
1365 {
1366 case PRT_UNIT_PERC:
1367 ret = (physsize * nr) / 100;
1368 break;
1369 case PRT_UNIT_INCH:
1370 ret = (nr * dpi);
1371 break;
1372 case PRT_UNIT_MM:
1373 ret = (nr * 10 * dpi) / 254;
1374 break;
1375 case PRT_UNIT_POINT:
1376 ret = (nr * 10 * dpi) / 720;
1377 break;
1378 }
1379
1380 if (ret < offset)
1381 return 0;
1382 else
1383 return ret - offset;
1384}
1385
1386 static int
1387prt_get_cpl(void)
1388{
1389 int hr;
1390 int phyw;
1391 int dvoff;
1392 int rev_offset;
1393 int dpi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394
1395 GetTextMetrics(prt_dlg.hDC, &prt_tm);
1396 prt_line_height = prt_tm.tmHeight + prt_tm.tmExternalLeading;
1397
1398 hr = GetDeviceCaps(prt_dlg.hDC, HORZRES);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 phyw = GetDeviceCaps(prt_dlg.hDC, PHYSICALWIDTH);
1400 dvoff = GetDeviceCaps(prt_dlg.hDC, PHYSICALOFFSETX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 dpi = GetDeviceCaps(prt_dlg.hDC, LOGPIXELSX);
1402
1403 rev_offset = phyw - (dvoff + hr);
1404
1405 prt_left_margin = to_device_units(OPT_PRINT_LEFT, dpi, phyw, dvoff, 10);
1406 if (prt_use_number())
1407 {
1408 prt_number_width = PRINT_NUMBER_WIDTH * prt_tm.tmAveCharWidth;
1409 prt_left_margin += prt_number_width;
1410 }
1411 else
1412 prt_number_width = 0;
1413
1414 prt_right_margin = hr - to_device_units(OPT_PRINT_RIGHT, dpi, phyw,
1415 rev_offset, 5);
1416
1417 return (prt_right_margin - prt_left_margin) / prt_tm.tmAveCharWidth;
1418}
1419
1420 static int
1421prt_get_lpp(void)
1422{
1423 int vr;
1424 int phyw;
1425 int dvoff;
1426 int rev_offset;
1427 int bottom_margin;
1428 int dpi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429
1430 vr = GetDeviceCaps(prt_dlg.hDC, VERTRES);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431 phyw = GetDeviceCaps(prt_dlg.hDC, PHYSICALHEIGHT);
1432 dvoff = GetDeviceCaps(prt_dlg.hDC, PHYSICALOFFSETY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 dpi = GetDeviceCaps(prt_dlg.hDC, LOGPIXELSY);
1434
1435 rev_offset = phyw - (dvoff + vr);
1436
1437 prt_top_margin = to_device_units(OPT_PRINT_TOP, dpi, phyw, dvoff, 5);
1438
1439 /* adjust top margin if there is a header */
1440 prt_top_margin += prt_line_height * prt_header_height();
1441
1442 bottom_margin = vr - to_device_units(OPT_PRINT_BOT, dpi, phyw,
1443 rev_offset, 5);
1444
1445 return (bottom_margin - prt_top_margin) / prt_line_height;
1446}
1447
1448 int
1449mch_print_init(prt_settings_T *psettings, char_u *jobname, int forceit)
1450{
1451 static HGLOBAL stored_dm = NULL;
1452 static HGLOBAL stored_devn = NULL;
1453 static int stored_nCopies = 1;
1454 static int stored_nFlags = 0;
1455
1456 LOGFONT fLogFont;
1457 int pifItalic;
1458 int pifBold;
1459 int pifUnderline;
1460
1461 DEVMODE *mem;
1462 DEVNAMES *devname;
1463 int i;
1464
1465 bUserAbort = &(psettings->user_abort);
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02001466 vim_memset(&prt_dlg, 0, sizeof(PRINTDLG));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 prt_dlg.lStructSize = sizeof(PRINTDLG);
1468#ifndef FEAT_GUI
1469 GetConsoleHwnd(); /* get value of s_hwnd */
1470#endif
1471 prt_dlg.hwndOwner = s_hwnd;
1472 prt_dlg.Flags = PD_NOPAGENUMS | PD_NOSELECTION | PD_RETURNDC;
1473 if (!forceit)
1474 {
1475 prt_dlg.hDevMode = stored_dm;
1476 prt_dlg.hDevNames = stored_devn;
1477 prt_dlg.lCustData = stored_nCopies; // work around bug in print dialog
1478#ifndef FEAT_GUI
1479 /*
1480 * Use hook to prevent console window being sent to back
1481 */
1482 prt_dlg.lpfnPrintHook = PrintHookProc;
1483 prt_dlg.Flags |= PD_ENABLEPRINTHOOK;
1484#endif
1485 prt_dlg.Flags |= stored_nFlags;
1486 }
1487
1488 /*
1489 * If bang present, return default printer setup with no dialog
1490 * never show dialog if we are running over telnet
1491 */
1492 if (forceit
1493#ifndef FEAT_GUI
1494 || !term_console
1495#endif
1496 )
1497 {
1498 prt_dlg.Flags |= PD_RETURNDEFAULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 /*
1500 * MSDN suggests setting the first parameter to WINSPOOL for
1501 * NT, but NULL appears to work just as well.
1502 */
1503 if (*p_pdev != NUL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001504 prt_dlg.hDC = CreateDC(NULL, (LPCSTR)p_pdev, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 {
1507 prt_dlg.Flags |= PD_RETURNDEFAULT;
1508 if (PrintDlg(&prt_dlg) == 0)
1509 goto init_fail_dlg;
1510 }
1511 }
1512 else if (PrintDlg(&prt_dlg) == 0)
1513 goto init_fail_dlg;
1514 else
1515 {
1516 /*
1517 * keep the previous driver context
1518 */
1519 stored_dm = prt_dlg.hDevMode;
1520 stored_devn = prt_dlg.hDevNames;
1521 stored_nFlags = prt_dlg.Flags;
1522 stored_nCopies = prt_dlg.nCopies;
1523 }
1524
1525 if (prt_dlg.hDC == NULL)
1526 {
1527 EMSG(_("E237: Printer selection failed"));
1528 mch_print_cleanup();
1529 return FALSE;
1530 }
1531
1532 /* Not all printer drivers report the support of color (or grey) in the
1533 * same way. Let's set has_color if there appears to be some way to print
1534 * more than B&W. */
1535 i = GetDeviceCaps(prt_dlg.hDC, NUMCOLORS);
1536 psettings->has_color = (GetDeviceCaps(prt_dlg.hDC, BITSPIXEL) > 1
1537 || GetDeviceCaps(prt_dlg.hDC, PLANES) > 1
1538 || i > 2 || i == -1);
1539
1540 /* Ensure all font styles are baseline aligned */
1541 SetTextAlign(prt_dlg.hDC, TA_BASELINE|TA_LEFT);
1542
1543 /*
1544 * On some windows systems the nCopies parameter is not
1545 * passed back correctly. It must be retrieved from the
1546 * hDevMode struct.
1547 */
1548 mem = (DEVMODE *)GlobalLock(prt_dlg.hDevMode);
1549 if (mem != NULL)
1550 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 if (mem->dmCopies != 1)
1552 stored_nCopies = mem->dmCopies;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 if ((mem->dmFields & DM_DUPLEX) && (mem->dmDuplex & ~DMDUP_SIMPLEX))
1554 psettings->duplex = TRUE;
1555 if ((mem->dmFields & DM_COLOR) && (mem->dmColor & DMCOLOR_COLOR))
1556 psettings->has_color = TRUE;
1557 }
1558 GlobalUnlock(prt_dlg.hDevMode);
1559
1560 devname = (DEVNAMES *)GlobalLock(prt_dlg.hDevNames);
1561 if (devname != 0)
1562 {
1563 char_u *printer_name = (char_u *)devname + devname->wDeviceOffset;
1564 char_u *port_name = (char_u *)devname +devname->wOutputOffset;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001565 char_u *text = (char_u *)_("to %s on %s");
Bram Moolenaarf191d552014-10-09 17:05:56 +02001566#ifdef FEAT_MBYTE
1567 char_u *printer_name_orig = printer_name;
1568 char_u *port_name_orig = port_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569
Bram Moolenaarf191d552014-10-09 17:05:56 +02001570 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
1571 {
1572 char_u *to_free = NULL;
1573 int maxlen;
1574
Bram Moolenaar861d80a2014-10-16 20:35:36 +02001575 acp_to_enc(printer_name, (int)STRLEN(printer_name), &to_free,
1576 &maxlen);
Bram Moolenaarf191d552014-10-09 17:05:56 +02001577 if (to_free != NULL)
1578 printer_name = to_free;
Bram Moolenaar861d80a2014-10-16 20:35:36 +02001579 acp_to_enc(port_name, (int)STRLEN(port_name), &to_free, &maxlen);
Bram Moolenaarf191d552014-10-09 17:05:56 +02001580 if (to_free != NULL)
1581 port_name = to_free;
1582 }
1583#endif
Bram Moolenaare6a91fd2008-07-24 18:51:11 +00001584 prt_name = alloc((unsigned)(STRLEN(printer_name) + STRLEN(port_name)
1585 + STRLEN(text)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 if (prt_name != NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001587 wsprintf((char *)prt_name, (const char *)text,
1588 printer_name, port_name);
Bram Moolenaarf191d552014-10-09 17:05:56 +02001589#ifdef FEAT_MBYTE
1590 if (printer_name != printer_name_orig)
1591 vim_free(printer_name);
1592 if (port_name != port_name_orig)
1593 vim_free(port_name);
1594#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 }
1596 GlobalUnlock(prt_dlg.hDevNames);
1597
1598 /*
1599 * Initialise the font according to 'printfont'
1600 */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02001601 vim_memset(&fLogFont, 0, sizeof(fLogFont));
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001602 if (get_logfont(&fLogFont, p_pfn, prt_dlg.hDC, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 {
1604 EMSG2(_("E613: Unknown printer font: %s"), p_pfn);
1605 mch_print_cleanup();
1606 return FALSE;
1607 }
1608
1609 for (pifBold = 0; pifBold <= 1; pifBold++)
1610 for (pifItalic = 0; pifItalic <= 1; pifItalic++)
1611 for (pifUnderline = 0; pifUnderline <= 1; pifUnderline++)
1612 {
1613 fLogFont.lfWeight = boldface[pifBold];
1614 fLogFont.lfItalic = pifItalic;
1615 fLogFont.lfUnderline = pifUnderline;
1616 prt_font_handles[pifBold][pifItalic][pifUnderline]
1617 = CreateFontIndirect(&fLogFont);
1618 }
1619
1620 SetBkMode(prt_dlg.hDC, OPAQUE);
1621 SelectObject(prt_dlg.hDC, prt_font_handles[0][0][0]);
1622
1623 /*
1624 * Fill in the settings struct
1625 */
1626 psettings->chars_per_line = prt_get_cpl();
1627 psettings->lines_per_page = prt_get_lpp();
Bram Moolenaar7ddc6422014-09-27 11:18:19 +02001628 if (prt_dlg.Flags & PD_USEDEVMODECOPIESANDCOLLATE)
1629 {
1630 psettings->n_collated_copies = (prt_dlg.Flags & PD_COLLATE)
1631 ? prt_dlg.nCopies : 1;
1632 psettings->n_uncollated_copies = (prt_dlg.Flags & PD_COLLATE)
1633 ? 1 : prt_dlg.nCopies;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634
Bram Moolenaar7ddc6422014-09-27 11:18:19 +02001635 if (psettings->n_collated_copies == 0)
1636 psettings->n_collated_copies = 1;
1637
1638 if (psettings->n_uncollated_copies == 0)
1639 psettings->n_uncollated_copies = 1;
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01001640 }
1641 else
1642 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 psettings->n_collated_copies = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 psettings->n_uncollated_copies = 1;
Bram Moolenaar7ddc6422014-09-27 11:18:19 +02001645 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646
1647 psettings->jobname = jobname;
1648
1649 return TRUE;
1650
1651init_fail_dlg:
1652 {
1653 DWORD err = CommDlgExtendedError();
1654
1655 if (err)
1656 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 char_u *buf;
1658
1659 /* I suspect FormatMessage() doesn't work for values returned by
1660 * CommDlgExtendedError(). What does? */
1661 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
1662 FORMAT_MESSAGE_FROM_SYSTEM |
1663 FORMAT_MESSAGE_IGNORE_INSERTS,
1664 NULL, err, 0, (LPTSTR)(&buf), 0, NULL);
1665 EMSG2(_("E238: Print error: %s"),
1666 buf == NULL ? (char_u *)_("Unknown") : buf);
1667 LocalFree((LPVOID)(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 }
1669 else
1670 msg_clr_eos(); /* Maybe canceled */
1671
1672 mch_print_cleanup();
1673 return FALSE;
1674 }
1675}
1676
1677
1678 int
1679mch_print_begin(prt_settings_T *psettings)
1680{
1681 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682 char szBuffer[300];
Bram Moolenaar2290b1f2018-05-13 17:30:45 +02001683#if defined(FEAT_MBYTE)
1684 WCHAR *wp = NULL;
1685#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686
1687 hDlgPrint = CreateDialog(GetModuleHandle(NULL), TEXT("PrintDlgBox"),
1688 prt_dlg.hwndOwner, PrintDlgProc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 SetAbortProc(prt_dlg.hDC, AbortProc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 wsprintf(szBuffer, _("Printing '%s'"), gettail(psettings->jobname));
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001691 vimSetDlgItemText(hDlgPrint, IDC_PRINTTEXT1, (char_u *)szBuffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692
Bram Moolenaar2290b1f2018-05-13 17:30:45 +02001693#if defined(FEAT_MBYTE)
1694 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
1695 wp = enc_to_utf16(psettings->jobname, NULL);
1696 if (wp != NULL)
1697 {
1698 DOCINFOW di;
1699
1700 vim_memset(&di, 0, sizeof(di));
1701 di.cbSize = sizeof(di);
1702 di.lpszDocName = wp;
1703 ret = StartDocW(prt_dlg.hDC, &di);
1704 vim_free(wp);
1705 }
1706 else
1707#endif
1708 {
1709 DOCINFO di;
1710
1711 vim_memset(&di, 0, sizeof(di));
1712 di.cbSize = sizeof(di);
1713 di.lpszDocName = (LPCSTR)psettings->jobname;
1714 ret = StartDoc(prt_dlg.hDC, &di);
1715 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716
1717#ifdef FEAT_GUI
1718 /* Give focus back to main window (when using MDI). */
1719 SetFocus(s_hwnd);
1720#endif
1721
1722 return (ret > 0);
1723}
1724
1725 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01001726mch_print_end(prt_settings_T *psettings UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727{
1728 EndDoc(prt_dlg.hDC);
1729 if (!*bUserAbort)
1730 SendMessage(hDlgPrint, WM_COMMAND, 0, 0);
1731}
1732
1733 int
1734mch_print_end_page(void)
1735{
1736 return (EndPage(prt_dlg.hDC) > 0);
1737}
1738
1739 int
1740mch_print_begin_page(char_u *msg)
1741{
1742 if (msg != NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001743 vimSetDlgItemText(hDlgPrint, IDC_PROGRESS, msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744 return (StartPage(prt_dlg.hDC) > 0);
1745}
1746
1747 int
1748mch_print_blank_page(void)
1749{
1750 return (mch_print_begin_page(NULL) ? (mch_print_end_page()) : FALSE);
1751}
1752
1753static int prt_pos_x = 0;
1754static int prt_pos_y = 0;
1755
1756 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001757mch_print_start_line(int margin, int page_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758{
1759 if (margin)
1760 prt_pos_x = -prt_number_width;
1761 else
1762 prt_pos_x = 0;
1763 prt_pos_y = page_line * prt_line_height
1764 + prt_tm.tmAscent + prt_tm.tmExternalLeading;
1765}
1766
1767 int
1768mch_print_text_out(char_u *p, int len)
1769{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001770#if defined(FEAT_PROPORTIONAL_FONTS) || defined(FEAT_MBYTE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 SIZE sz;
1772#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001773#if defined(FEAT_MBYTE)
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001774 WCHAR *wp = NULL;
1775 int wlen = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001777 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
1778 {
1779 wp = enc_to_utf16(p, &wlen);
1780 }
1781 if (wp != NULL)
1782 {
1783 int ret = FALSE;
1784
1785 TextOutW(prt_dlg.hDC, prt_pos_x + prt_left_margin,
1786 prt_pos_y + prt_top_margin, wp, wlen);
1787 GetTextExtentPoint32W(prt_dlg.hDC, wp, wlen, &sz);
1788 vim_free(wp);
1789 prt_pos_x += (sz.cx - prt_tm.tmOverhang);
1790 /* This is wrong when printing spaces for a TAB. */
1791 if (p[len] != NUL)
1792 {
1793 wlen = MB_PTR2LEN(p + len);
1794 wp = enc_to_utf16(p + len, &wlen);
1795 if (wp != NULL)
1796 {
1797 GetTextExtentPoint32W(prt_dlg.hDC, wp, 1, &sz);
1798 ret = (prt_pos_x + prt_left_margin + sz.cx > prt_right_margin);
1799 vim_free(wp);
1800 }
1801 }
1802 return ret;
1803 }
1804#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 TextOut(prt_dlg.hDC, prt_pos_x + prt_left_margin,
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001806 prt_pos_y + prt_top_margin,
1807 (LPCSTR)p, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808#ifndef FEAT_PROPORTIONAL_FONTS
1809 prt_pos_x += len * prt_tm.tmAveCharWidth;
1810 return (prt_pos_x + prt_left_margin + prt_tm.tmAveCharWidth
1811 + prt_tm.tmOverhang > prt_right_margin);
1812#else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001813 GetTextExtentPoint32(prt_dlg.hDC, (LPCSTR)p, len, &sz);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 prt_pos_x += (sz.cx - prt_tm.tmOverhang);
1815 /* This is wrong when printing spaces for a TAB. */
1816 if (p[len] == NUL)
1817 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 GetTextExtentPoint32(prt_dlg.hDC, p + len, 1, &sz);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 return (prt_pos_x + prt_left_margin + sz.cx > prt_right_margin);
1820#endif
1821}
1822
1823 void
1824mch_print_set_font(int iBold, int iItalic, int iUnderline)
1825{
1826 SelectObject(prt_dlg.hDC, prt_font_handles[iBold][iItalic][iUnderline]);
1827}
1828
1829 void
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001830mch_print_set_bg(long_u bgcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831{
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001832 SetBkColor(prt_dlg.hDC, GetNearestColor(prt_dlg.hDC,
1833 swap_me((COLORREF)bgcol)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 /*
1835 * With a white background we can draw characters transparent, which is
1836 * good for italic characters that overlap to the next char cell.
1837 */
1838 if (bgcol == 0xffffffUL)
1839 SetBkMode(prt_dlg.hDC, TRANSPARENT);
1840 else
1841 SetBkMode(prt_dlg.hDC, OPAQUE);
1842}
1843
1844 void
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001845mch_print_set_fg(long_u fgcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846{
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001847 SetTextColor(prt_dlg.hDC, GetNearestColor(prt_dlg.hDC,
1848 swap_me((COLORREF)fgcol)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849}
1850
1851#endif /*FEAT_PRINTER && !FEAT_POSTSCRIPT*/
1852
Bram Moolenaar58d98232005-07-23 22:25:46 +00001853
1854
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855#if defined(FEAT_SHORTCUT) || defined(PROTO)
Bram Moolenaar82881492012-11-20 16:53:39 +01001856# ifndef PROTO
1857# include <shlobj.h>
1858# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859
1860/*
1861 * When "fname" is the name of a shortcut (*.lnk) resolve the file it points
1862 * to and return that name in allocated memory.
1863 * Otherwise NULL is returned.
1864 */
1865 char_u *
1866mch_resolve_shortcut(char_u *fname)
1867{
1868 HRESULT hr;
1869 IShellLink *psl = NULL;
1870 IPersistFile *ppf = NULL;
1871 OLECHAR wsz[MAX_PATH];
1872 WIN32_FIND_DATA ffd; // we get those free of charge
Bram Moolenaar604729e2013-08-30 16:44:19 +02001873 CHAR buf[MAX_PATH]; // could have simply reused 'wsz'...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 char_u *rfname = NULL;
1875 int len;
Bram Moolenaar604729e2013-08-30 16:44:19 +02001876# ifdef FEAT_MBYTE
1877 IShellLinkW *pslw = NULL;
1878 WIN32_FIND_DATAW ffdw; // we get those free of charge
1879# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880
1881 /* Check if the file name ends in ".lnk". Avoid calling
1882 * CoCreateInstance(), it's quite slow. */
1883 if (fname == NULL)
1884 return rfname;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001885 len = (int)STRLEN(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 if (len <= 4 || STRNICMP(fname + len - 4, ".lnk", 4) != 0)
1887 return rfname;
1888
1889 CoInitialize(NULL);
1890
Bram Moolenaar604729e2013-08-30 16:44:19 +02001891# ifdef FEAT_MBYTE
1892 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
1893 {
1894 // create a link manager object and request its interface
1895 hr = CoCreateInstance(
1896 &CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
1897 &IID_IShellLinkW, (void**)&pslw);
1898 if (hr == S_OK)
1899 {
1900 WCHAR *p = enc_to_utf16(fname, NULL);
1901
1902 if (p != NULL)
1903 {
1904 // Get a pointer to the IPersistFile interface.
1905 hr = pslw->lpVtbl->QueryInterface(
1906 pslw, &IID_IPersistFile, (void**)&ppf);
1907 if (hr != S_OK)
1908 goto shortcut_errorw;
1909
1910 // "load" the name and resolve the link
1911 hr = ppf->lpVtbl->Load(ppf, p, STGM_READ);
1912 if (hr != S_OK)
1913 goto shortcut_errorw;
1914# if 0 // This makes Vim wait a long time if the target does not exist.
1915 hr = pslw->lpVtbl->Resolve(pslw, NULL, SLR_NO_UI);
1916 if (hr != S_OK)
1917 goto shortcut_errorw;
1918# endif
1919
1920 // Get the path to the link target.
1921 ZeroMemory(wsz, MAX_PATH * sizeof(WCHAR));
1922 hr = pslw->lpVtbl->GetPath(pslw, wsz, MAX_PATH, &ffdw, 0);
1923 if (hr == S_OK && wsz[0] != NUL)
1924 rfname = utf16_to_enc(wsz, NULL);
1925
1926shortcut_errorw:
1927 vim_free(p);
Bram Moolenaarcd981f22014-02-11 17:06:00 +01001928 goto shortcut_end;
Bram Moolenaar604729e2013-08-30 16:44:19 +02001929 }
1930 }
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001931 goto shortcut_end;
Bram Moolenaar604729e2013-08-30 16:44:19 +02001932 }
1933# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 // create a link manager object and request its interface
1935 hr = CoCreateInstance(
1936 &CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
1937 &IID_IShellLink, (void**)&psl);
1938 if (hr != S_OK)
Bram Moolenaar604729e2013-08-30 16:44:19 +02001939 goto shortcut_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940
1941 // Get a pointer to the IPersistFile interface.
1942 hr = psl->lpVtbl->QueryInterface(
1943 psl, &IID_IPersistFile, (void**)&ppf);
1944 if (hr != S_OK)
Bram Moolenaar604729e2013-08-30 16:44:19 +02001945 goto shortcut_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946
1947 // full path string must be in Unicode.
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001948 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)fname, -1, wsz, MAX_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00001950 // "load" the name and resolve the link
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 hr = ppf->lpVtbl->Load(ppf, wsz, STGM_READ);
1952 if (hr != S_OK)
Bram Moolenaar604729e2013-08-30 16:44:19 +02001953 goto shortcut_end;
1954# if 0 // This makes Vim wait a long time if the target doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 hr = psl->lpVtbl->Resolve(psl, NULL, SLR_NO_UI);
1956 if (hr != S_OK)
Bram Moolenaar604729e2013-08-30 16:44:19 +02001957 goto shortcut_end;
1958# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959
1960 // Get the path to the link target.
1961 ZeroMemory(buf, MAX_PATH);
1962 hr = psl->lpVtbl->GetPath(psl, buf, MAX_PATH, &ffd, 0);
1963 if (hr == S_OK && buf[0] != NUL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001964 rfname = vim_strsave((char_u *)buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965
Bram Moolenaar604729e2013-08-30 16:44:19 +02001966shortcut_end:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 // Release all interface pointers (both belong to the same object)
1968 if (ppf != NULL)
1969 ppf->lpVtbl->Release(ppf);
1970 if (psl != NULL)
1971 psl->lpVtbl->Release(psl);
Bram Moolenaar604729e2013-08-30 16:44:19 +02001972# ifdef FEAT_MBYTE
1973 if (pslw != NULL)
1974 pslw->lpVtbl->Release(pslw);
1975# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976
1977 CoUninitialize();
1978 return rfname;
1979}
1980#endif
1981
1982#if (defined(FEAT_EVAL) && !defined(FEAT_GUI)) || defined(PROTO)
1983/*
1984 * Bring ourselves to the foreground. Does work if the OS doesn't allow it.
1985 */
1986 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001987win32_set_foreground(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988{
1989# ifndef FEAT_GUI
1990 GetConsoleHwnd(); /* get value of s_hwnd */
1991# endif
1992 if (s_hwnd != 0)
1993 SetForegroundWindow(s_hwnd);
1994}
1995#endif
1996
1997#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
1998/*
1999 * Client-server code for Vim
2000 *
2001 * Originally written by Paul Moore
2002 */
2003
2004/* In order to handle inter-process messages, we need to have a window. But
2005 * the functions in this module can be called before the main GUI window is
2006 * created (and may also be called in the console version, where there is no
2007 * GUI window at all).
2008 *
2009 * So we create a hidden window, and arrange to destroy it on exit.
2010 */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002011HWND message_window = 0; /* window that's handling messages */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012
2013#define VIM_CLASSNAME "VIM_MESSAGES"
2014#define VIM_CLASSNAME_LEN (sizeof(VIM_CLASSNAME) - 1)
2015
2016/* Communication is via WM_COPYDATA messages. The message type is send in
2017 * the dwData parameter. Types are defined here. */
2018#define COPYDATA_KEYS 0
2019#define COPYDATA_REPLY 1
2020#define COPYDATA_EXPR 10
2021#define COPYDATA_RESULT 11
2022#define COPYDATA_ERROR_RESULT 12
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002023#define COPYDATA_ENCODING 20
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024
2025/* This is a structure containing a server HWND and its name. */
2026struct server_id
2027{
2028 HWND hwnd;
2029 char_u *name;
2030};
2031
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002032/* Last received 'encoding' that the client uses. */
2033static char_u *client_enc = NULL;
2034
2035/*
2036 * Tell the other side what encoding we are using.
2037 * Errors are ignored.
2038 */
2039 static void
2040serverSendEnc(HWND target)
2041{
2042 COPYDATASTRUCT data;
2043
2044 data.dwData = COPYDATA_ENCODING;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002045#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002046 data.cbData = (DWORD)STRLEN(p_enc) + 1;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002047 data.lpData = p_enc;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002048#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +02002049 data.cbData = (DWORD)STRLEN("latin1") + 1;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002050 data.lpData = "latin1";
2051#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002052 (void)SendMessage(target, WM_COPYDATA, (WPARAM)message_window,
2053 (LPARAM)(&data));
2054}
2055
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056/*
2057 * Clean up on exit. This destroys the hidden message window.
2058 */
2059 static void
2060#ifdef __BORLANDC__
2061 _RTLENTRYF
2062#endif
2063CleanUpMessaging(void)
2064{
2065 if (message_window != 0)
2066 {
2067 DestroyWindow(message_window);
2068 message_window = 0;
2069 }
2070}
2071
2072static int save_reply(HWND server, char_u *reply, int expr);
2073
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002074/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 * The window procedure for the hidden message window.
2076 * It handles callback messages and notifications from servers.
2077 * In order to process these messages, it is necessary to run a
2078 * message loop. Code which may run before the main message loop
2079 * is started (in the GUI) is careful to pump messages when it needs
2080 * to. Features which require message delivery during normal use will
2081 * not work in the console version - this basically means those
2082 * features which allow Vim to act as a server, rather than a client.
2083 */
2084 static LRESULT CALLBACK
2085Messaging_WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
2086{
2087 if (msg == WM_COPYDATA)
2088 {
2089 /* This is a message from another Vim. The dwData member of the
2090 * COPYDATASTRUCT determines the type of message:
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002091 * COPYDATA_ENCODING:
2092 * The encoding that the client uses. Following messages will
2093 * use this encoding, convert if needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 * COPYDATA_KEYS:
2095 * A key sequence. We are a server, and a client wants these keys
2096 * adding to the input queue.
2097 * COPYDATA_REPLY:
2098 * A reply. We are a client, and a server has sent this message
2099 * in response to a request. (server2client())
2100 * COPYDATA_EXPR:
2101 * An expression. We are a server, and a client wants us to
2102 * evaluate this expression.
2103 * COPYDATA_RESULT:
2104 * A reply. We are a client, and a server has sent this message
2105 * in response to a COPYDATA_EXPR.
2106 * COPYDATA_ERROR_RESULT:
2107 * A reply. We are a client, and a server has sent this message
2108 * in response to a COPYDATA_EXPR that failed to evaluate.
2109 */
2110 COPYDATASTRUCT *data = (COPYDATASTRUCT*)lParam;
2111 HWND sender = (HWND)wParam;
2112 COPYDATASTRUCT reply;
2113 char_u *res;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 int retval;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002115 char_u *str;
2116 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117
2118 switch (data->dwData)
2119 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002120 case COPYDATA_ENCODING:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002121# ifdef FEAT_MBYTE
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002122 /* Remember the encoding that the client uses. */
2123 vim_free(client_enc);
2124 client_enc = enc_canonize((char_u *)data->lpData);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002125# endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002126 return 1;
2127
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 case COPYDATA_KEYS:
2129 /* Remember who sent this, for <client> */
2130 clientWindow = sender;
2131
2132 /* Add the received keys to the input buffer. The loop waiting
2133 * for the user to do something should check the input buffer. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002134 str = serverConvert(client_enc, (char_u *)data->lpData, &tofree);
2135 server_to_input_buf(str);
2136 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137
2138# ifdef FEAT_GUI
2139 /* Wake up the main GUI loop. */
2140 if (s_hwnd != 0)
2141 PostMessage(s_hwnd, WM_NULL, 0, 0);
2142# endif
2143 return 1;
2144
2145 case COPYDATA_EXPR:
2146 /* Remember who sent this, for <client> */
2147 clientWindow = sender;
2148
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002149 str = serverConvert(client_enc, (char_u *)data->lpData, &tofree);
2150 res = eval_client_expr_to_string(str);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002151
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 if (res == NULL)
2153 {
Bram Moolenaar15bf76d2017-03-18 16:18:37 +01002154 char *err = _(e_invexprmsg);
2155 size_t len = STRLEN(str) + STRLEN(err) + 5;
2156
Bram Moolenaar1662ce12017-03-19 21:47:50 +01002157 res = alloc((unsigned)len);
Bram Moolenaar15bf76d2017-03-18 16:18:37 +01002158 if (res != NULL)
2159 vim_snprintf((char *)res, len, "%s: \"%s\"", err, str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 reply.dwData = COPYDATA_ERROR_RESULT;
2161 }
2162 else
2163 reply.dwData = COPYDATA_RESULT;
2164 reply.lpData = res;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002165 reply.cbData = (DWORD)STRLEN(res) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002167 serverSendEnc(sender);
Bram Moolenaar5246cd72013-06-16 16:41:47 +02002168 retval = (int)SendMessage(sender, WM_COPYDATA,
2169 (WPARAM)message_window, (LPARAM)(&reply));
Bram Moolenaar15bf76d2017-03-18 16:18:37 +01002170 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 vim_free(res);
2172 return retval;
2173
2174 case COPYDATA_REPLY:
2175 case COPYDATA_RESULT:
2176 case COPYDATA_ERROR_RESULT:
2177 if (data->lpData != NULL)
2178 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002179 str = serverConvert(client_enc, (char_u *)data->lpData,
2180 &tofree);
2181 if (tofree == NULL)
2182 str = vim_strsave(str);
2183 if (save_reply(sender, str,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 (data->dwData == COPYDATA_REPLY ? 0 :
2185 (data->dwData == COPYDATA_RESULT ? 1 :
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002186 2))) == FAIL)
2187 vim_free(str);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002188 else if (data->dwData == COPYDATA_REPLY)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 {
Bram Moolenaar427d51c2013-06-16 16:01:25 +02002190 char_u winstr[30];
2191
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002192 sprintf((char *)winstr, PRINTF_HEX_LONG_U, (long_u)sender);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002193 apply_autocmds(EVENT_REMOTEREPLY, winstr, str,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 TRUE, curbuf);
2195 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196 }
2197 return 1;
2198 }
2199
2200 return 0;
2201 }
2202
2203 else if (msg == WM_ACTIVATE && wParam == WA_ACTIVE)
2204 {
2205 /* When the message window is activated (brought to the foreground),
2206 * this actually applies to the text window. */
2207#ifndef FEAT_GUI
2208 GetConsoleHwnd(); /* get value of s_hwnd */
2209#endif
2210 if (s_hwnd != 0)
2211 {
2212 SetForegroundWindow(s_hwnd);
2213 return 0;
2214 }
2215 }
2216
2217 return DefWindowProc(hwnd, msg, wParam, lParam);
2218}
2219
2220/*
2221 * Initialise the message handling process. This involves creating a window
2222 * to handle messages - the window will not be visible.
2223 */
2224 void
2225serverInitMessaging(void)
2226{
2227 WNDCLASS wndclass;
2228 HINSTANCE s_hinst;
2229
2230 /* Clean up on exit */
2231 atexit(CleanUpMessaging);
2232
2233 /* Register a window class - we only really care
2234 * about the window procedure
2235 */
2236 s_hinst = (HINSTANCE)GetModuleHandle(0);
2237 wndclass.style = 0;
2238 wndclass.lpfnWndProc = Messaging_WndProc;
2239 wndclass.cbClsExtra = 0;
2240 wndclass.cbWndExtra = 0;
2241 wndclass.hInstance = s_hinst;
2242 wndclass.hIcon = NULL;
2243 wndclass.hCursor = NULL;
2244 wndclass.hbrBackground = NULL;
2245 wndclass.lpszMenuName = NULL;
2246 wndclass.lpszClassName = VIM_CLASSNAME;
2247 RegisterClass(&wndclass);
2248
2249 /* Create the message window. It will be hidden, so the details don't
2250 * matter. Don't use WS_OVERLAPPEDWINDOW, it will make a shortcut remove
2251 * focus from gvim. */
2252 message_window = CreateWindow(VIM_CLASSNAME, "",
2253 WS_POPUPWINDOW | WS_CAPTION,
2254 CW_USEDEFAULT, CW_USEDEFAULT,
2255 100, 100, NULL, NULL,
2256 s_hinst, NULL);
2257}
2258
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002259/* Used by serverSendToVim() to find an alternate server name. */
2260static char_u *altname_buf_ptr = NULL;
2261
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262/*
2263 * Get the title of the window "hwnd", which is the Vim server name, in
2264 * "name[namelen]" and return the length.
2265 * Returns zero if window "hwnd" is not a Vim server.
2266 */
2267 static int
2268getVimServerName(HWND hwnd, char *name, int namelen)
2269{
2270 int len;
2271 char buffer[VIM_CLASSNAME_LEN + 1];
2272
2273 /* Ignore windows which aren't Vim message windows */
2274 len = GetClassName(hwnd, buffer, sizeof(buffer));
2275 if (len != VIM_CLASSNAME_LEN || STRCMP(buffer, VIM_CLASSNAME) != 0)
2276 return 0;
2277
2278 /* Get the title of the window */
2279 return GetWindowText(hwnd, name, namelen);
2280}
2281
2282 static BOOL CALLBACK
2283enumWindowsGetServer(HWND hwnd, LPARAM lparam)
2284{
2285 struct server_id *id = (struct server_id *)lparam;
2286 char server[MAX_PATH];
2287
2288 /* Get the title of the window */
2289 if (getVimServerName(hwnd, server, sizeof(server)) == 0)
2290 return TRUE;
2291
2292 /* If this is the server we're looking for, return its HWND */
2293 if (STRICMP(server, id->name) == 0)
2294 {
2295 id->hwnd = hwnd;
2296 return FALSE;
2297 }
2298
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002299 /* If we are looking for an alternate server, remember this name. */
2300 if (altname_buf_ptr != NULL
2301 && STRNICMP(server, id->name, STRLEN(id->name)) == 0
2302 && vim_isdigit(server[STRLEN(id->name)]))
2303 {
2304 STRCPY(altname_buf_ptr, server);
2305 altname_buf_ptr = NULL; /* don't use another name */
2306 }
2307
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 /* Otherwise, keep looking */
2309 return TRUE;
2310}
2311
2312 static BOOL CALLBACK
2313enumWindowsGetNames(HWND hwnd, LPARAM lparam)
2314{
2315 garray_T *ga = (garray_T *)lparam;
2316 char server[MAX_PATH];
2317
2318 /* Get the title of the window */
2319 if (getVimServerName(hwnd, server, sizeof(server)) == 0)
2320 return TRUE;
2321
2322 /* Add the name to the list */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002323 ga_concat(ga, (char_u *)server);
2324 ga_concat(ga, (char_u *)"\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 return TRUE;
2326}
2327
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002328struct enum_windows_s
2329{
2330 WNDENUMPROC lpEnumFunc;
2331 LPARAM lParam;
2332};
2333
2334 static BOOL CALLBACK
2335enum_windows_child(HWND hwnd, LPARAM lParam)
2336{
2337 struct enum_windows_s *ew = (struct enum_windows_s *)lParam;
2338
2339 return (ew->lpEnumFunc)(hwnd, ew->lParam);
2340}
2341
2342 static BOOL CALLBACK
2343enum_windows_toplevel(HWND hwnd, LPARAM lParam)
2344{
2345 struct enum_windows_s *ew = (struct enum_windows_s *)lParam;
2346
Bram Moolenaar95ba5c32018-10-07 22:47:07 +02002347 if ((ew->lpEnumFunc)(hwnd, ew->lParam))
2348 return TRUE;
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002349 return EnumChildWindows(hwnd, enum_windows_child, lParam);
2350}
2351
2352/* Enumerate all windows including children. */
2353 static BOOL
2354enum_windows(WNDENUMPROC lpEnumFunc, LPARAM lParam)
2355{
2356 struct enum_windows_s ew;
2357
2358 ew.lpEnumFunc = lpEnumFunc;
2359 ew.lParam = lParam;
2360 return EnumWindows(enum_windows_toplevel, (LPARAM)&ew);
2361}
2362
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 static HWND
2364findServer(char_u *name)
2365{
2366 struct server_id id;
2367
2368 id.name = name;
2369 id.hwnd = 0;
2370
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002371 enum_windows(enumWindowsGetServer, (LPARAM)(&id));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372
2373 return id.hwnd;
2374}
2375
2376 void
2377serverSetName(char_u *name)
2378{
2379 char_u *ok_name;
2380 HWND hwnd = 0;
2381 int i = 0;
2382 char_u *p;
2383
2384 /* Leave enough space for a 9-digit suffix to ensure uniqueness! */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002385 ok_name = alloc((unsigned)STRLEN(name) + 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386
2387 STRCPY(ok_name, name);
2388 p = ok_name + STRLEN(name);
2389
2390 for (;;)
2391 {
2392 /* This is inefficient - we're doing an EnumWindows loop for each
2393 * possible name. It would be better to grab all names in one go,
2394 * and scan the list each time...
2395 */
2396 hwnd = findServer(ok_name);
2397 if (hwnd == 0)
2398 break;
2399
2400 ++i;
2401 if (i >= 1000)
2402 break;
2403
2404 sprintf((char *)p, "%d", i);
2405 }
2406
2407 if (hwnd != 0)
2408 vim_free(ok_name);
2409 else
2410 {
2411 /* Remember the name */
2412 serverName = ok_name;
2413#ifdef FEAT_TITLE
2414 need_maketitle = TRUE; /* update Vim window title later */
2415#endif
2416
2417 /* Update the message window title */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002418 SetWindowText(message_window, (LPCSTR)ok_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419
2420#ifdef FEAT_EVAL
2421 /* Set the servername variable */
2422 set_vim_var_string(VV_SEND_SERVER, serverName, -1);
2423#endif
2424 }
2425}
2426
2427 char_u *
2428serverGetVimNames(void)
2429{
2430 garray_T ga;
2431
2432 ga_init2(&ga, 1, 100);
2433
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002434 enum_windows(enumWindowsGetNames, (LPARAM)(&ga));
Bram Moolenaar269ec652004-07-29 08:43:53 +00002435 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436
2437 return ga.ga_data;
2438}
2439
2440 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002441serverSendReply(
2442 char_u *name, /* Where to send. */
2443 char_u *reply) /* What to send. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444{
2445 HWND target;
2446 COPYDATASTRUCT data;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002447 long_u n = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448
2449 /* The "name" argument is a magic cookie obtained from expand("<client>").
2450 * It should be of the form 0xXXXXX - i.e. a C hex literal, which is the
2451 * value of the client's message window HWND.
2452 */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002453 sscanf((char *)name, SCANF_HEX_LONG_U, &n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 if (n == 0)
2455 return -1;
2456
2457 target = (HWND)n;
2458 if (!IsWindow(target))
2459 return -1;
2460
2461 data.dwData = COPYDATA_REPLY;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002462 data.cbData = (DWORD)STRLEN(reply) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 data.lpData = reply;
2464
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002465 serverSendEnc(target);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 if (SendMessage(target, WM_COPYDATA, (WPARAM)message_window,
2467 (LPARAM)(&data)))
2468 return 0;
2469
2470 return -1;
2471}
2472
2473 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002474serverSendToVim(
2475 char_u *name, /* Where to send. */
2476 char_u *cmd, /* What to send. */
2477 char_u **result, /* Result of eval'ed expression */
2478 void *ptarget, /* HWND of server */
2479 int asExpr, /* Expression or keys? */
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002480 int timeout, /* timeout in seconds or zero */
Bram Moolenaar05540972016-01-30 20:31:25 +01002481 int silent) /* don't complain about no server */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482{
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002483 HWND target;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 COPYDATASTRUCT data;
2485 char_u *retval = NULL;
2486 int retcode = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002487 char_u altname_buf[MAX_PATH];
2488
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01002489 /* Execute locally if no display or target is ourselves */
2490 if (serverName != NULL && STRICMP(name, serverName) == 0)
2491 return sendToLocalVim(cmd, asExpr, result);
2492
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002493 /* If the server name does not end in a digit then we look for an
2494 * alternate name. e.g. when "name" is GVIM the we may find GVIM2. */
2495 if (STRLEN(name) > 1 && !vim_isdigit(name[STRLEN(name) - 1]))
2496 altname_buf_ptr = altname_buf;
2497 altname_buf[0] = NUL;
2498 target = findServer(name);
2499 altname_buf_ptr = NULL;
2500 if (target == 0 && altname_buf[0] != NUL)
2501 /* Use another server name we found. */
2502 target = findServer(altname_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503
2504 if (target == 0)
2505 {
2506 if (!silent)
2507 EMSG2(_(e_noserver), name);
2508 return -1;
2509 }
2510
2511 if (ptarget)
2512 *(HWND *)ptarget = target;
2513
2514 data.dwData = asExpr ? COPYDATA_EXPR : COPYDATA_KEYS;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002515 data.cbData = (DWORD)STRLEN(cmd) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 data.lpData = cmd;
2517
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002518 serverSendEnc(target);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 if (SendMessage(target, WM_COPYDATA, (WPARAM)message_window,
2520 (LPARAM)(&data)) == 0)
2521 return -1;
2522
2523 if (asExpr)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002524 retval = serverGetReply(target, &retcode, TRUE, TRUE, timeout);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525
2526 if (result == NULL)
2527 vim_free(retval);
2528 else
2529 *result = retval; /* Caller assumes responsibility for freeing */
2530
2531 return retcode;
2532}
2533
2534/*
2535 * Bring the server to the foreground.
2536 */
2537 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002538serverForeground(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539{
2540 HWND target = findServer(name);
2541
2542 if (target != 0)
2543 SetForegroundWindow(target);
2544}
2545
2546/* Replies from server need to be stored until the client picks them up via
2547 * remote_read(). So we maintain a list of server-id/reply pairs.
2548 * Note that there could be multiple replies from one server pending if the
2549 * client is slow picking them up.
2550 * We just store the replies in a simple list. When we remove an entry, we
2551 * move list entries down to fill the gap.
2552 * The server ID is simply the HWND.
2553 */
2554typedef struct
2555{
2556 HWND server; /* server window */
2557 char_u *reply; /* reply string */
2558 int expr_result; /* 0 for REPLY, 1 for RESULT 2 for error */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002559} reply_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560
2561static garray_T reply_list = {0, 0, sizeof(reply_T), 5, 0};
2562
2563#define REPLY_ITEM(i) ((reply_T *)(reply_list.ga_data) + (i))
2564#define REPLY_COUNT (reply_list.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565
2566/* Flag which is used to wait for a reply */
2567static int reply_received = 0;
2568
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002569/*
2570 * Store a reply. "reply" must be allocated memory (or NULL).
2571 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 static int
2573save_reply(HWND server, char_u *reply, int expr)
2574{
2575 reply_T *rep;
2576
2577 if (ga_grow(&reply_list, 1) == FAIL)
2578 return FAIL;
2579
2580 rep = REPLY_ITEM(REPLY_COUNT);
2581 rep->server = server;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002582 rep->reply = reply;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 rep->expr_result = expr;
2584 if (rep->reply == NULL)
2585 return FAIL;
2586
2587 ++REPLY_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 reply_received = 1;
2589 return OK;
2590}
2591
2592/*
2593 * Get a reply from server "server".
2594 * When "expr_res" is non NULL, get the result of an expression, otherwise a
2595 * server2client() message.
2596 * When non NULL, point to return code. 0 => OK, -1 => ERROR
2597 * If "remove" is TRUE, consume the message, the caller must free it then.
2598 * if "wait" is TRUE block until a message arrives (or the server exits).
2599 */
2600 char_u *
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002601serverGetReply(HWND server, int *expr_res, int remove, int wait, int timeout)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602{
2603 int i;
2604 char_u *reply;
2605 reply_T *rep;
Bram Moolenaar15e737f2017-03-18 21:22:47 +01002606 int did_process = FALSE;
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002607 time_t start;
2608 time_t now;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609
2610 /* When waiting, loop until the message waiting for is received. */
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002611 time(&start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 for (;;)
2613 {
2614 /* Reset this here, in case a message arrives while we are going
2615 * through the already received messages. */
2616 reply_received = 0;
2617
2618 for (i = 0; i < REPLY_COUNT; ++i)
2619 {
2620 rep = REPLY_ITEM(i);
2621 if (rep->server == server
2622 && ((rep->expr_result != 0) == (expr_res != NULL)))
2623 {
2624 /* Save the values we've found for later */
2625 reply = rep->reply;
2626 if (expr_res != NULL)
2627 *expr_res = rep->expr_result == 1 ? 0 : -1;
2628
2629 if (remove)
2630 {
2631 /* Move the rest of the list down to fill the gap */
2632 mch_memmove(rep, rep + 1,
2633 (REPLY_COUNT - i - 1) * sizeof(reply_T));
2634 --REPLY_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 }
2636
2637 /* Return the reply to the caller, who takes on responsibility
2638 * for freeing it if "remove" is TRUE. */
2639 return reply;
2640 }
2641 }
2642
2643 /* If we got here, we didn't find a reply. Return immediately if the
2644 * "wait" parameter isn't set. */
2645 if (!wait)
Bram Moolenaar15e737f2017-03-18 21:22:47 +01002646 {
2647 /* Process pending messages once. Without this, looping on
2648 * remote_peek() would never get the reply. */
2649 if (!did_process)
2650 {
2651 did_process = TRUE;
2652 serverProcessPendingMessages();
2653 continue;
2654 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 break;
Bram Moolenaar15e737f2017-03-18 21:22:47 +01002656 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657
2658 /* We need to wait for a reply. Enter a message loop until the
2659 * "reply_received" flag gets set. */
2660
2661 /* Loop until we receive a reply */
2662 while (reply_received == 0)
2663 {
Bram Moolenaar42205552017-03-18 19:42:22 +01002664#ifdef FEAT_TIMERS
Bram Moolenaard23a8232018-02-10 18:45:26 +01002665 /* TODO: use the return value to decide how long to wait. */
Bram Moolenaar42205552017-03-18 19:42:22 +01002666 check_due_timer();
2667#endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002668 time(&now);
2669 if (timeout > 0 && (now - start) >= timeout)
2670 break;
2671
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 /* Wait for a SendMessage() call to us. This could be the reply
2673 * we are waiting for. Use a timeout of a second, to catch the
2674 * situation that the server died unexpectedly. */
2675 MsgWaitForMultipleObjects(0, NULL, TRUE, 1000, QS_ALLINPUT);
2676
2677 /* If the server has died, give up */
2678 if (!IsWindow(server))
2679 return NULL;
2680
2681 serverProcessPendingMessages();
2682 }
2683 }
2684
2685 return NULL;
2686}
2687
2688/*
2689 * Process any messages in the Windows message queue.
2690 */
2691 void
2692serverProcessPendingMessages(void)
2693{
2694 MSG msg;
2695
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02002696 while (pPeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 {
2698 TranslateMessage(&msg);
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02002699 pDispatchMessage(&msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 }
2701}
2702
2703#endif /* FEAT_CLIENTSERVER */
2704
2705#if defined(FEAT_GUI) || (defined(FEAT_PRINTER) && !defined(FEAT_POSTSCRIPT)) \
2706 || defined(PROTO)
2707
2708struct charset_pair
2709{
2710 char *name;
2711 BYTE charset;
2712};
2713
2714static struct charset_pair
2715charset_pairs[] =
2716{
2717 {"ANSI", ANSI_CHARSET},
2718 {"CHINESEBIG5", CHINESEBIG5_CHARSET},
2719 {"DEFAULT", DEFAULT_CHARSET},
2720 {"HANGEUL", HANGEUL_CHARSET},
2721 {"OEM", OEM_CHARSET},
2722 {"SHIFTJIS", SHIFTJIS_CHARSET},
2723 {"SYMBOL", SYMBOL_CHARSET},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 {"ARABIC", ARABIC_CHARSET},
2725 {"BALTIC", BALTIC_CHARSET},
2726 {"EASTEUROPE", EASTEUROPE_CHARSET},
2727 {"GB2312", GB2312_CHARSET},
2728 {"GREEK", GREEK_CHARSET},
2729 {"HEBREW", HEBREW_CHARSET},
2730 {"JOHAB", JOHAB_CHARSET},
2731 {"MAC", MAC_CHARSET},
2732 {"RUSSIAN", RUSSIAN_CHARSET},
2733 {"THAI", THAI_CHARSET},
2734 {"TURKISH", TURKISH_CHARSET},
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002735#ifdef VIETNAMESE_CHARSET
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 {"VIETNAMESE", VIETNAMESE_CHARSET},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737#endif
2738 {NULL, 0}
2739};
2740
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002741struct quality_pair
2742{
2743 char *name;
2744 DWORD quality;
2745};
2746
2747static struct quality_pair
2748quality_pairs[] = {
2749#ifdef CLEARTYPE_QUALITY
2750 {"CLEARTYPE", CLEARTYPE_QUALITY},
2751#endif
2752#ifdef ANTIALIASED_QUALITY
2753 {"ANTIALIASED", ANTIALIASED_QUALITY},
2754#endif
Bram Moolenaar73a733e2016-05-11 21:05:05 +02002755#ifdef NONANTIALIASED_QUALITY
2756 {"NONANTIALIASED", NONANTIALIASED_QUALITY},
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002757#endif
2758#ifdef PROOF_QUALITY
2759 {"PROOF", PROOF_QUALITY},
2760#endif
Bram Moolenaar4c9ce052016-04-04 21:06:19 +02002761#ifdef DRAFT_QUALITY
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002762 {"DRAFT", DRAFT_QUALITY},
2763#endif
2764 {"DEFAULT", DEFAULT_QUALITY},
2765 {NULL, 0}
2766};
2767
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768/*
2769 * Convert a charset ID to a name.
2770 * Return NULL when not recognized.
2771 */
2772 char *
2773charset_id2name(int id)
2774{
2775 struct charset_pair *cp;
2776
2777 for (cp = charset_pairs; cp->name != NULL; ++cp)
2778 if ((BYTE)id == cp->charset)
2779 break;
2780 return cp->name;
2781}
2782
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002783/*
2784 * Convert a quality ID to a name.
2785 * Return NULL when not recognized.
2786 */
2787 char *
2788quality_id2name(DWORD id)
2789{
2790 struct quality_pair *qp;
2791
2792 for (qp = quality_pairs; qp->name != NULL; ++qp)
2793 if (id == qp->quality)
2794 break;
2795 return qp->name;
2796}
2797
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798static const LOGFONT s_lfDefault =
2799{
2800 -12, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, DEFAULT_CHARSET,
2801 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
2802 PROOF_QUALITY, FIXED_PITCH | FF_DONTCARE,
2803 "Fixedsys" /* see _ReadVimIni */
2804};
2805
2806/* Initialise the "current height" to -12 (same as s_lfDefault) just
2807 * in case the user specifies a font in "guifont" with no size before a font
2808 * with an explicit size has been set. This defaults the size to this value
2809 * (-12 equates to roughly 9pt).
2810 */
2811int current_font_height = -12; /* also used in gui_w48.c */
2812
2813/* Convert a string representing a point size into pixels. The string should
2814 * be a positive decimal number, with an optional decimal point (eg, "12", or
2815 * "10.5"). The pixel value is returned, and a pointer to the next unconverted
2816 * character is stored in *end. The flag "vertical" says whether this
2817 * calculation is for a vertical (height) size or a horizontal (width) one.
2818 */
2819 static int
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002820points_to_pixels(char_u *str, char_u **end, int vertical, long_i pprinter_dc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821{
2822 int pixels;
2823 int points = 0;
2824 int divisor = 0;
2825 HWND hwnd = (HWND)0;
2826 HDC hdc;
2827 HDC printer_dc = (HDC)pprinter_dc;
2828
2829 while (*str != NUL)
2830 {
2831 if (*str == '.' && divisor == 0)
2832 {
2833 /* Start keeping a divisor, for later */
2834 divisor = 1;
2835 }
2836 else
2837 {
2838 if (!VIM_ISDIGIT(*str))
2839 break;
2840
2841 points *= 10;
2842 points += *str - '0';
2843 divisor *= 10;
2844 }
2845 ++str;
2846 }
2847
2848 if (divisor == 0)
2849 divisor = 1;
2850
2851 if (printer_dc == NULL)
2852 {
2853 hwnd = GetDesktopWindow();
2854 hdc = GetWindowDC(hwnd);
2855 }
2856 else
2857 hdc = printer_dc;
2858
2859 pixels = MulDiv(points,
2860 GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX),
2861 72 * divisor);
2862
2863 if (printer_dc == NULL)
2864 ReleaseDC(hwnd, hdc);
2865
2866 *end = str;
2867 return pixels;
2868}
2869
2870 static int CALLBACK
2871font_enumproc(
2872 ENUMLOGFONT *elf,
Bram Moolenaar1266d672017-02-01 13:43:36 +01002873 NEWTEXTMETRIC *ntm UNUSED,
2874 int type UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 LPARAM lparam)
2876{
2877 /* Return value:
2878 * 0 = terminate now (monospace & ANSI)
2879 * 1 = continue, still no luck...
2880 * 2 = continue, but we have an acceptable LOGFONT
2881 * (monospace, not ANSI)
2882 * We use these values, as EnumFontFamilies returns 1 if the
2883 * callback function is never called. So, we check the return as
2884 * 0 = perfect, 2 = OK, 1 = no good...
2885 * It's not pretty, but it works!
2886 */
2887
2888 LOGFONT *lf = (LOGFONT *)(lparam);
2889
2890#ifndef FEAT_PROPORTIONAL_FONTS
2891 /* Ignore non-monospace fonts without further ado */
2892 if ((ntm->tmPitchAndFamily & 1) != 0)
2893 return 1;
2894#endif
2895
2896 /* Remember this LOGFONT as a "possible" */
2897 *lf = elf->elfLogFont;
2898
2899 /* Terminate the scan as soon as we find an ANSI font */
2900 if (lf->lfCharSet == ANSI_CHARSET
2901 || lf->lfCharSet == OEM_CHARSET
2902 || lf->lfCharSet == DEFAULT_CHARSET)
2903 return 0;
2904
2905 /* Continue the scan - we have a non-ANSI font */
2906 return 2;
2907}
2908
2909 static int
2910init_logfont(LOGFONT *lf)
2911{
2912 int n;
2913 HWND hwnd = GetDesktopWindow();
2914 HDC hdc = GetWindowDC(hwnd);
2915
2916 n = EnumFontFamilies(hdc,
2917 (LPCSTR)lf->lfFaceName,
2918 (FONTENUMPROC)font_enumproc,
2919 (LPARAM)lf);
2920
2921 ReleaseDC(hwnd, hdc);
2922
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002923 /* If we couldn't find a usable font, return failure */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 if (n == 1)
2925 return FAIL;
2926
2927 /* Tidy up the rest of the LOGFONT structure. We set to a basic
2928 * font - get_logfont() sets bold, italic, etc based on the user's
2929 * input.
2930 */
2931 lf->lfHeight = current_font_height;
2932 lf->lfWidth = 0;
2933 lf->lfItalic = FALSE;
2934 lf->lfUnderline = FALSE;
2935 lf->lfStrikeOut = FALSE;
2936 lf->lfWeight = FW_NORMAL;
2937
2938 /* Return success */
2939 return OK;
2940}
2941
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002942/*
2943 * Get font info from "name" into logfont "lf".
2944 * Return OK for a valid name, FAIL otherwise.
2945 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 int
2947get_logfont(
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002948 LOGFONT *lf,
2949 char_u *name,
2950 HDC printer_dc,
2951 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952{
2953 char_u *p;
2954 int i;
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002955 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 static LOGFONT *lastlf = NULL;
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002957#ifdef FEAT_MBYTE
2958 char_u *acpname = NULL;
2959#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960
2961 *lf = s_lfDefault;
2962 if (name == NULL)
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002963 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002965#ifdef FEAT_MBYTE
2966 /* Convert 'name' from 'encoding' to the current codepage, because
2967 * lf->lfFaceName uses the current codepage.
2968 * TODO: Use Wide APIs instead of ANSI APIs. */
2969 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
2970 {
2971 int len;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01002972 enc_to_acp(name, (int)STRLEN(name), &acpname, &len);
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002973 name = acpname;
2974 }
2975#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 if (STRCMP(name, "*") == 0)
2977 {
2978#if defined(FEAT_GUI_W32)
2979 CHOOSEFONT cf;
2980 /* if name is "*", bring up std font dialog: */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02002981 vim_memset(&cf, 0, sizeof(cf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 cf.lStructSize = sizeof(cf);
2983 cf.hwndOwner = s_hwnd;
2984 cf.Flags = CF_SCREENFONTS | CF_FIXEDPITCHONLY | CF_INITTOLOGFONTSTRUCT;
2985 if (lastlf != NULL)
2986 *lf = *lastlf;
2987 cf.lpLogFont = lf;
2988 cf.nFontType = 0 ; //REGULAR_FONTTYPE;
2989 if (ChooseFont(&cf))
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002990 ret = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991#endif
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002992 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993 }
2994
2995 /*
2996 * Split name up, it could be <name>:h<height>:w<width> etc.
2997 */
2998 for (p = name; *p && *p != ':'; p++)
2999 {
Bram Moolenaar38bc4952016-11-10 17:10:51 +01003000 if (p - name + 1 >= LF_FACESIZE)
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003001 goto theend; /* Name too long */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002 lf->lfFaceName[p - name] = *p;
3003 }
3004 if (p != name)
3005 lf->lfFaceName[p - name] = NUL;
3006
3007 /* First set defaults */
3008 lf->lfHeight = -12;
3009 lf->lfWidth = 0;
3010 lf->lfWeight = FW_NORMAL;
3011 lf->lfItalic = FALSE;
3012 lf->lfUnderline = FALSE;
3013 lf->lfStrikeOut = FALSE;
3014
3015 /*
3016 * If the font can't be found, try replacing '_' by ' '.
3017 */
3018 if (init_logfont(lf) == FAIL)
3019 {
3020 int did_replace = FALSE;
3021
3022 for (i = 0; lf->lfFaceName[i]; ++i)
Bram Moolenaar89e06c82017-08-11 20:55:55 +02003023 if (IsDBCSLeadByte(lf->lfFaceName[i]))
3024 ++i;
3025 else if (lf->lfFaceName[i] == '_')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 {
3027 lf->lfFaceName[i] = ' ';
3028 did_replace = TRUE;
3029 }
3030 if (!did_replace || init_logfont(lf) == FAIL)
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003031 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 }
3033
3034 while (*p == ':')
3035 p++;
3036
3037 /* Set the values found after ':' */
3038 while (*p)
3039 {
3040 switch (*p++)
3041 {
3042 case 'h':
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003043 lf->lfHeight = - points_to_pixels(p, &p, TRUE, (long_i)printer_dc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044 break;
3045 case 'w':
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003046 lf->lfWidth = points_to_pixels(p, &p, FALSE, (long_i)printer_dc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047 break;
3048 case 'b':
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049 lf->lfWeight = FW_BOLD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 break;
3051 case 'i':
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052 lf->lfItalic = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053 break;
3054 case 'u':
3055 lf->lfUnderline = TRUE;
3056 break;
3057 case 's':
3058 lf->lfStrikeOut = TRUE;
3059 break;
3060 case 'c':
3061 {
3062 struct charset_pair *cp;
3063
3064 for (cp = charset_pairs; cp->name != NULL; ++cp)
3065 if (STRNCMP(p, cp->name, strlen(cp->name)) == 0)
3066 {
3067 lf->lfCharSet = cp->charset;
3068 p += strlen(cp->name);
3069 break;
3070 }
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003071 if (cp->name == NULL && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 {
Bram Moolenaar051b7822005-05-19 21:00:46 +00003073 vim_snprintf((char *)IObuff, IOSIZE,
3074 _("E244: Illegal charset name \"%s\" in font name \"%s\""), p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 EMSG(IObuff);
3076 break;
3077 }
3078 break;
3079 }
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003080 case 'q':
3081 {
3082 struct quality_pair *qp;
3083
3084 for (qp = quality_pairs; qp->name != NULL; ++qp)
3085 if (STRNCMP(p, qp->name, strlen(qp->name)) == 0)
3086 {
3087 lf->lfQuality = qp->quality;
3088 p += strlen(qp->name);
3089 break;
3090 }
3091 if (qp->name == NULL && verbose)
3092 {
3093 vim_snprintf((char *)IObuff, IOSIZE,
3094 _("E244: Illegal quality name \"%s\" in font name \"%s\""), p, name);
3095 EMSG(IObuff);
3096 break;
3097 }
3098 break;
3099 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 default:
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003101 if (verbose)
3102 {
Bram Moolenaar051b7822005-05-19 21:00:46 +00003103 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003104 _("E245: Illegal char '%c' in font name \"%s\""),
3105 p[-1], name);
3106 EMSG(IObuff);
3107 }
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003108 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 }
3110 while (*p == ':')
3111 p++;
3112 }
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003113 ret = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 /* ron: init lastlf */
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003117 if (ret == OK && printer_dc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 {
3119 vim_free(lastlf);
3120 lastlf = (LOGFONT *)alloc(sizeof(LOGFONT));
3121 if (lastlf != NULL)
3122 mch_memmove(lastlf, lf, sizeof(LOGFONT));
3123 }
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003124#ifdef FEAT_MBYTE
3125 vim_free(acpname);
3126#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003128 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129}
3130
3131#endif /* defined(FEAT_GUI) || defined(FEAT_PRINTER) */
Bram Moolenaarf12d9832016-01-29 21:11:25 +01003132
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003133#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaarf12d9832016-01-29 21:11:25 +01003134/*
3135 * Initialize the Winsock dll.
3136 */
3137 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003138channel_init_winsock(void)
Bram Moolenaarf12d9832016-01-29 21:11:25 +01003139{
3140 WSADATA wsaData;
3141 int wsaerr;
3142
3143 if (WSInitialized)
3144 return;
3145
3146 wsaerr = WSAStartup(MAKEWORD(2, 2), &wsaData);
3147 if (wsaerr == 0)
3148 WSInitialized = TRUE;
3149}
3150#endif