blob: 8d46e53cf1ebc72b3aecfc2ada34f4b02534a042 [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 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000285 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
286 {
287 /* Convert the title from 'encoding' to the active codepage. */
Bram Moolenaar36f692d2008-11-20 16:10:17 +0000288 WCHAR *wp = enc_to_utf16(title, NULL);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000289
290 if (wp != NULL)
291 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200292 SetConsoleTitleW(wp);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000293 vim_free(wp);
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200294 return;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000295 }
296 }
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100297 SetConsoleTitle((LPCSTR)title);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000298 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000299# endif
300}
301
302
303/*
304 * Restore the window/icon title.
305 * which is one of:
Bram Moolenaar40385db2018-08-07 22:31:44 +0200306 * SAVE_RESTORE_TITLE: Just restore title
307 * SAVE_RESTORE_ICON: Just restore icon (which we don't have)
308 * SAVE_RESTORE_BOTH: Restore title and icon (which we don't have)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309 */
310 void
Bram Moolenaar1266d672017-02-01 13:43:36 +0100311mch_restore_title(int which UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312{
313#ifndef FEAT_GUI_MSWIN
Bram Moolenaar1df52d72014-10-15 22:50:10 +0200314 SetConsoleTitle(g_szOrigTitle);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315#endif
316}
317
318
319/*
320 * Return TRUE if we can restore the title (we can)
321 */
322 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100323mch_can_restore_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324{
325 return TRUE;
326}
327
328
329/*
330 * Return TRUE if we can restore the icon title (we can't)
331 */
332 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100333mch_can_restore_icon(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334{
335 return FALSE;
336}
337#endif /* FEAT_TITLE */
338
339
340/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000341 * Get absolute file name into buffer "buf" of length "len" bytes,
342 * turning all '/'s into '\\'s and getting the correct case of each component
343 * of the file name. Append a (back)slash to a directory name.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344 * When 'shellslash' set do it the other way around.
345 * Return OK or FAIL.
346 */
347 int
348mch_FullName(
349 char_u *fname,
350 char_u *buf,
351 int len,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100352 int force UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353{
354 int nResult = FAIL;
355
356#ifdef __BORLANDC__
357 if (*fname == NUL) /* Borland behaves badly here - make it consistent */
358 nResult = mch_dirname(buf, len);
359 else
360#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200362 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000363 {
364 WCHAR *wname;
365 WCHAR wbuf[MAX_PATH];
366 char_u *cname = NULL;
367
368 /* Use the wide function:
369 * - convert the fname from 'encoding' to UCS2.
370 * - invoke _wfullpath()
371 * - convert the result from UCS2 to 'encoding'.
372 */
Bram Moolenaar36f692d2008-11-20 16:10:17 +0000373 wname = enc_to_utf16(fname, NULL);
Bram Moolenaar374bf022014-11-05 19:33:24 +0100374 if (wname != NULL && _wfullpath(wbuf, wname, MAX_PATH) != NULL)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000375 {
Bram Moolenaar36f692d2008-11-20 16:10:17 +0000376 cname = utf16_to_enc((short_u *)wbuf, NULL);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000377 if (cname != NULL)
378 {
Bram Moolenaarfe3ca8d2005-07-18 21:43:02 +0000379 vim_strncpy(buf, cname, len - 1);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000380 nResult = OK;
381 }
382 }
383 vim_free(wname);
384 vim_free(cname);
385 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000386 if (nResult == FAIL) /* fall back to non-wide function */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000387 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100388 if (_fullpath((char *)buf, (const char *)fname, len - 1) == NULL)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000389 {
Bram Moolenaarfe3ca8d2005-07-18 21:43:02 +0000390 /* failed, use relative path name */
391 vim_strncpy(buf, fname, len - 1);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000392 }
393 else
394 nResult = OK;
395 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397
398#ifdef USE_FNAME_CASE
399 fname_case(buf, len);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000400#else
401 slash_adjust(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402#endif
403
404 return nResult;
405}
406
407
408/*
409 * Return TRUE if "fname" does not depend on the current directory.
410 */
411 int
412mch_isFullName(char_u *fname)
413{
Bram Moolenaard2a203b2013-08-30 16:51:18 +0200414 /* WinNT and later can use _MAX_PATH wide characters for a pathname, which
415 * means that the maximum pathname is _MAX_PATH * 3 bytes when 'enc' is
416 * UTF-8. */
417 char szName[_MAX_PATH * 3 + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000418
419 /* A name like "d:/foo" and "//server/share" is absolute */
420 if ((fname[0] && fname[1] == ':' && (fname[2] == '/' || fname[2] == '\\'))
421 || (fname[0] == fname[1] && (fname[0] == '/' || fname[0] == '\\')))
422 return TRUE;
423
424 /* A name that can't be made absolute probably isn't absolute. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100425 if (mch_FullName(fname, (char_u *)szName, sizeof(szName) - 1, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426 return FALSE;
427
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100428 return pathcmp((const char *)fname, (const char *)szName, -1) == 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000429}
430
431/*
432 * Replace all slashes by backslashes.
433 * This used to be the other way around, but MS-DOS sometimes has problems
434 * with slashes (e.g. in a command name). We can't have mixed slashes and
435 * backslashes, because comparing file names will not work correctly. The
436 * commands that use a file name should try to avoid the need to type a
437 * backslash twice.
438 * When 'shellslash' set do it the other way around.
Bram Moolenaarb4f6a462015-10-13 19:43:17 +0200439 * When the path looks like a URL leave it unmodified.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440 */
441 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100442slash_adjust(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443{
Bram Moolenaarb4f6a462015-10-13 19:43:17 +0200444 if (path_with_url(p))
445 return;
Bram Moolenaar39d21e32017-08-05 23:09:31 +0200446
447 if (*p == '`')
448 {
Bram Moolenaar116a0f82017-08-07 21:17:57 +0200449 size_t len = STRLEN(p);
450
Bram Moolenaar39d21e32017-08-05 23:09:31 +0200451 /* don't replace backslash in backtick quoted strings */
Bram Moolenaar39d21e32017-08-05 23:09:31 +0200452 if (len > 2 && *(p + len - 1) == '`')
453 return;
454 }
455
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +0000456 while (*p)
457 {
458 if (*p == psepcN)
459 *p = psepc;
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100460 MB_PTR_ADV(p);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +0000461 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462}
463
Bram Moolenaar8767f522016-07-01 17:17:39 +0200464/* Use 64-bit stat functions if available. */
465#ifdef HAVE_STAT64
466# undef stat
467# undef _stat
468# undef _wstat
469# undef _fstat
470# define stat _stat64
471# define _stat _stat64
472# define _wstat _wstat64
473# define _fstat _fstat64
474#endif
475
Bram Moolenaar9d1685d2014-01-14 12:18:45 +0100476#if (defined(_MSC_VER) && (_MSC_VER >= 1300)) || defined(__MINGW32__)
Bram Moolenaar8962fda2013-09-29 19:05:21 +0200477# define OPEN_OH_ARGTYPE intptr_t
478#else
479# define OPEN_OH_ARGTYPE long
480#endif
481
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200482 static int
Bram Moolenaar8767f522016-07-01 17:17:39 +0200483stat_symlink_aware(const char *name, stat_T *stp)
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200484{
Bram Moolenaarfce7b3d2016-01-19 19:00:32 +0100485#if (defined(_MSC_VER) && (_MSC_VER < 1900)) || defined(__MINGW32__)
486 /* Work around for VC12 or earlier (and MinGW). stat() can't handle
487 * symlinks properly.
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200488 * VC9 or earlier: stat() doesn't support a symlink at all. It retrieves
489 * status of a symlink itself.
490 * VC10: stat() supports a symlink to a normal file, but it doesn't support
Bram Moolenaarfce7b3d2016-01-19 19:00:32 +0100491 * a symlink to a directory (always returns an error).
492 * VC11 and VC12: stat() doesn't return an error for a symlink to a
493 * directory, but it doesn't set S_IFDIR flag.
494 * MinGW: Same as VC9. */
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200495 WIN32_FIND_DATA findData;
496 HANDLE hFind, h;
497 DWORD attr = 0;
498 BOOL is_symlink = FALSE;
499
500 hFind = FindFirstFile(name, &findData);
501 if (hFind != INVALID_HANDLE_VALUE)
502 {
503 attr = findData.dwFileAttributes;
504 if ((attr & FILE_ATTRIBUTE_REPARSE_POINT)
505 && (findData.dwReserved0 == IO_REPARSE_TAG_SYMLINK))
506 is_symlink = TRUE;
507 FindClose(hFind);
508 }
509 if (is_symlink)
510 {
511 h = CreateFile(name, FILE_READ_ATTRIBUTES,
512 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
513 OPEN_EXISTING,
514 (attr & FILE_ATTRIBUTE_DIRECTORY)
515 ? FILE_FLAG_BACKUP_SEMANTICS : 0,
516 NULL);
517 if (h != INVALID_HANDLE_VALUE)
518 {
519 int fd, n;
520
Bram Moolenaar8962fda2013-09-29 19:05:21 +0200521 fd = _open_osfhandle((OPEN_OH_ARGTYPE)h, _O_RDONLY);
Bram Moolenaar8767f522016-07-01 17:17:39 +0200522 n = _fstat(fd, (struct _stat *)stp);
Bram Moolenaarfce7b3d2016-01-19 19:00:32 +0100523 if ((n == 0) && (attr & FILE_ATTRIBUTE_DIRECTORY))
524 stp->st_mode = (stp->st_mode & ~S_IFREG) | S_IFDIR;
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200525 _close(fd);
526 return n;
527 }
528 }
529#endif
530 return stat(name, stp);
531}
532
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200533 static int
Bram Moolenaar8767f522016-07-01 17:17:39 +0200534wstat_symlink_aware(const WCHAR *name, stat_T *stp)
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200535{
Bram Moolenaara12a1612019-01-24 16:39:02 +0100536#if (defined(_MSC_VER) && (_MSC_VER < 1900)) || defined(__MINGW32__)
Bram Moolenaarfce7b3d2016-01-19 19:00:32 +0100537 /* Work around for VC12 or earlier (and MinGW). _wstat() can't handle
538 * symlinks properly.
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200539 * VC9 or earlier: _wstat() doesn't support a symlink at all. It retrieves
540 * status of a symlink itself.
541 * VC10: _wstat() supports a symlink to a normal file, but it doesn't
Bram Moolenaarfce7b3d2016-01-19 19:00:32 +0100542 * support a symlink to a directory (always returns an error).
543 * VC11 and VC12: _wstat() doesn't return an error for a symlink to a
544 * directory, but it doesn't set S_IFDIR flag.
545 * MinGW: Same as VC9. */
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200546 int n;
547 BOOL is_symlink = FALSE;
548 HANDLE hFind, h;
549 DWORD attr = 0;
550 WIN32_FIND_DATAW findDataW;
551
552 hFind = FindFirstFileW(name, &findDataW);
553 if (hFind != INVALID_HANDLE_VALUE)
554 {
555 attr = findDataW.dwFileAttributes;
556 if ((attr & FILE_ATTRIBUTE_REPARSE_POINT)
557 && (findDataW.dwReserved0 == IO_REPARSE_TAG_SYMLINK))
558 is_symlink = TRUE;
559 FindClose(hFind);
560 }
561 if (is_symlink)
562 {
563 h = CreateFileW(name, FILE_READ_ATTRIBUTES,
564 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
565 OPEN_EXISTING,
566 (attr & FILE_ATTRIBUTE_DIRECTORY)
567 ? FILE_FLAG_BACKUP_SEMANTICS : 0,
568 NULL);
569 if (h != INVALID_HANDLE_VALUE)
570 {
571 int fd;
572
Bram Moolenaar8962fda2013-09-29 19:05:21 +0200573 fd = _open_osfhandle((OPEN_OH_ARGTYPE)h, _O_RDONLY);
Bram Moolenaar8767f522016-07-01 17:17:39 +0200574 n = _fstat(fd, (struct _stat *)stp);
Bram Moolenaarfce7b3d2016-01-19 19:00:32 +0100575 if ((n == 0) && (attr & FILE_ATTRIBUTE_DIRECTORY))
576 stp->st_mode = (stp->st_mode & ~S_IFREG) | S_IFDIR;
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200577 _close(fd);
578 return n;
579 }
580 }
Bram Moolenaara12a1612019-01-24 16:39:02 +0100581#endif
Bram Moolenaar8767f522016-07-01 17:17:39 +0200582 return _wstat(name, (struct _stat *)stp);
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200583}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584
585/*
586 * stat() can't handle a trailing '/' or '\', remove it first.
587 */
588 int
Bram Moolenaar8767f522016-07-01 17:17:39 +0200589vim_stat(const char *name, stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590{
Bram Moolenaard2a203b2013-08-30 16:51:18 +0200591 /* WinNT and later can use _MAX_PATH wide characters for a pathname, which
592 * means that the maximum pathname is _MAX_PATH * 3 bytes when 'enc' is
593 * UTF-8. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100594 char_u buf[_MAX_PATH * 3 + 1];
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100595 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596
Bram Moolenaard2a203b2013-08-30 16:51:18 +0200597 vim_strncpy((char_u *)buf, (char_u *)name, sizeof(buf) - 1);
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100598 p = buf + STRLEN(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599 if (p > buf)
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100600 MB_PTR_BACK(buf, p);
Bram Moolenaarb1cb35f2014-01-10 13:05:20 +0100601
602 /* Remove trailing '\\' except root path. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603 if (p > buf && (*p == '\\' || *p == '/') && p[-1] != ':')
604 *p = NUL;
Bram Moolenaarb1cb35f2014-01-10 13:05:20 +0100605
606 if ((buf[0] == '\\' && buf[1] == '\\') || (buf[0] == '/' && buf[1] == '/'))
607 {
608 /* UNC root path must be followed by '\\'. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100609 p = vim_strpbrk(buf + 2, (char_u *)"\\/");
Bram Moolenaarb1cb35f2014-01-10 13:05:20 +0100610 if (p != NULL)
611 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100612 p = vim_strpbrk(p + 1, (char_u *)"\\/");
Bram Moolenaarb1cb35f2014-01-10 13:05:20 +0100613 if (p == NULL)
614 STRCAT(buf, "\\");
615 }
616 }
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200617 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618 {
Bram Moolenaar36f692d2008-11-20 16:10:17 +0000619 WCHAR *wp = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 int n;
621
622 if (wp != NULL)
623 {
Bram Moolenaar8767f522016-07-01 17:17:39 +0200624 n = wstat_symlink_aware(wp, stp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625 vim_free(wp);
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200626 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627 }
628 }
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100629 return stat_symlink_aware((char *)buf, stp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630}
631
632#if defined(FEAT_GUI_MSWIN) || defined(PROTO)
633 void
Bram Moolenaar1266d672017-02-01 13:43:36 +0100634mch_settmode(int tmode UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635{
636 /* nothing to do */
637}
638
639 int
640mch_get_shellsize(void)
641{
642 /* never used */
643 return OK;
644}
645
646 void
647mch_set_shellsize(void)
648{
649 /* never used */
650}
651
652/*
653 * Rows and/or Columns has changed.
654 */
655 void
656mch_new_shellsize(void)
657{
658 /* never used */
659}
660
661#endif
662
663/*
664 * We have no job control, so fake it by starting a new shell.
665 */
666 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100667mch_suspend(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668{
669 suspend_shell();
670}
671
672#if defined(USE_MCH_ERRMSG) || defined(PROTO)
673
674#ifdef display_errors
675# undef display_errors
676#endif
677
678/*
679 * Display the saved error message(s).
680 */
681 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100682display_errors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683{
684 char *p;
685
686 if (error_ga.ga_data != NULL)
687 {
688 /* avoid putting up a message box with blanks only */
689 for (p = (char *)error_ga.ga_data; *p; ++p)
690 if (!isspace(*p))
691 {
Bram Moolenaarc17ef8e2006-03-25 21:48:58 +0000692 (void)gui_mch_dialog(
693#ifdef FEAT_GUI
694 gui.starting ? VIM_INFO :
695#endif
696 VIM_ERROR,
697#ifdef FEAT_GUI
698 gui.starting ? (char_u *)_("Message") :
699#endif
700 (char_u *)_("Error"),
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100701 (char_u *)p, (char_u *)_("&Ok"),
702 1, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 break;
704 }
705 ga_clear(&error_ga);
706 }
707}
708#endif
709
710
711/*
712 * Return TRUE if "p" contain a wildcard that can be expanded by
713 * dos_expandpath().
714 */
715 int
716mch_has_exp_wildcard(char_u *p)
717{
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100718 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719 {
720 if (vim_strchr((char_u *)"?*[", *p) != NULL
721 || (*p == '~' && p[1] != NUL))
722 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723 }
724 return FALSE;
725}
726
727/*
728 * Return TRUE if "p" contain a wildcard or a "~1" kind of thing (could be a
729 * shortened file name).
730 */
731 int
732mch_has_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 *)
737# ifdef VIM_BACKTICK
738 "?*$[`"
739# else
740 "?*$["
741# endif
742 , *p) != NULL
743 || (*p == '~' && p[1] != NUL))
744 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 }
746 return FALSE;
747}
748
749
750/*
751 * The normal _chdir() does not change the default drive. This one does.
752 * Returning 0 implies success; -1 implies failure.
753 */
754 int
755mch_chdir(char *path)
756{
757 if (path[0] == NUL) /* just checking... */
758 return -1;
759
Bram Moolenaara2974d72009-07-14 16:38:36 +0000760 if (p_verbose >= 5)
761 {
762 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100763 smsg("chdir(%s)", path);
Bram Moolenaara2974d72009-07-14 16:38:36 +0000764 verbose_leave();
765 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 if (isalpha(path[0]) && path[1] == ':') /* has a drive name */
767 {
768 /* If we can change to the drive, skip that part of the path. If we
769 * can't then the current directory may be invalid, try using chdir()
770 * with the whole path. */
771 if (_chdrive(TOLOWER_ASC(path[0]) - 'a' + 1) == 0)
772 path += 2;
773 }
774
775 if (*path == NUL) /* drive name only */
776 return 0;
777
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000778 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
779 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100780 WCHAR *p = enc_to_utf16((char_u *)path, NULL);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000781 int n;
782
783 if (p != NULL)
784 {
785 n = _wchdir(p);
786 vim_free(p);
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200787 return n;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000788 }
789 }
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000790
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 return chdir(path); /* let the normal chdir() do the rest */
792}
793
794
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795#ifdef FEAT_GUI_MSWIN
796/*
797 * return non-zero if a character is available
798 */
799 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100800mch_char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801{
802 /* never used */
803 return TRUE;
804}
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200805
806# if defined(FEAT_TERMINAL) || defined(PROTO)
807/*
808 * Check for any pending input or messages.
809 */
810 int
811mch_check_messages(void)
812{
813 /* TODO: check for messages */
814 return TRUE;
815}
816# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817#endif
818
819
820/*
821 * set screen mode, always fails.
822 */
823 int
Bram Moolenaar1266d672017-02-01 13:43:36 +0100824mch_screenmode(char_u *arg UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100826 emsg(_(e_screenmode));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 return FAIL;
828}
829
830
831#if defined(FEAT_LIBCALL) || defined(PROTO)
832/*
833 * Call a DLL routine which takes either a string or int param
834 * and returns an allocated string.
835 * Return OK if it worked, FAIL if not.
836 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837typedef LPTSTR (*MYSTRPROCSTR)(LPTSTR);
838typedef LPTSTR (*MYINTPROCSTR)(int);
839typedef int (*MYSTRPROCINT)(LPTSTR);
840typedef int (*MYINTPROCINT)(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842/*
843 * Check if a pointer points to a valid NUL terminated string.
844 * Return the length of the string, including terminating NUL.
845 * Returns 0 for an invalid pointer, 1 for an empty string.
846 */
847 static size_t
848check_str_len(char_u *str)
849{
850 SYSTEM_INFO si;
851 MEMORY_BASIC_INFORMATION mbi;
852 size_t length = 0;
853 size_t i;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100854 const char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855
856 /* get page size */
857 GetSystemInfo(&si);
858
859 /* get memory information */
860 if (VirtualQuery(str, &mbi, sizeof(mbi)))
861 {
862 /* pre cast these (typing savers) */
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000863 long_u dwStr = (long_u)str;
864 long_u dwBaseAddress = (long_u)mbi.BaseAddress;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865
866 /* get start address of page that str is on */
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000867 long_u strPage = dwStr - (dwStr - dwBaseAddress) % si.dwPageSize;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868
869 /* get length from str to end of page */
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000870 long_u pageLength = si.dwPageSize - (dwStr - strPage);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871
Bram Moolenaar442b4222010-05-24 21:34:22 +0200872 for (p = str; !IsBadReadPtr(p, (UINT)pageLength);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 p += pageLength, pageLength = si.dwPageSize)
874 for (i = 0; i < pageLength; ++i, ++length)
875 if (p[i] == NUL)
876 return length + 1;
877 }
878
879 return 0;
880}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200882/*
883 * Passed to do_in_runtimepath() to load a vim.ico file.
884 */
885 static void
886mch_icon_load_cb(char_u *fname, void *cookie)
887{
888 HANDLE *h = (HANDLE *)cookie;
889
890 *h = LoadImage(NULL,
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100891 (LPSTR)fname,
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200892 IMAGE_ICON,
893 64,
894 64,
895 LR_LOADFROMFILE | LR_LOADMAP3DCOLORS);
896}
897
898/*
899 * Try loading an icon file from 'runtimepath'.
900 */
901 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100902mch_icon_load(HANDLE *iconp)
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200903{
904 return do_in_runtimepath((char_u *)"bitmaps/vim.ico",
Bram Moolenaar7f8989d2016-03-12 22:11:39 +0100905 0, mch_icon_load_cb, iconp);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200906}
907
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 int
909mch_libcall(
910 char_u *libname,
911 char_u *funcname,
912 char_u *argstring, /* NULL when using a argint */
913 int argint,
914 char_u **string_result,/* NULL when using number_result */
915 int *number_result)
916{
917 HINSTANCE hinstLib;
918 MYSTRPROCSTR ProcAdd;
919 MYINTPROCSTR ProcAddI;
920 char_u *retval_str = NULL;
921 int retval_int = 0;
922 size_t len;
923
924 BOOL fRunTimeLinkSuccess = FALSE;
925
926 // Get a handle to the DLL module.
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100927 hinstLib = vimLoadLib((char *)libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928
929 // If the handle is valid, try to get the function address.
930 if (hinstLib != NULL)
931 {
932#ifdef HAVE_TRY_EXCEPT
933 __try
934 {
935#endif
936 if (argstring != NULL)
937 {
938 /* Call with string argument */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100939 ProcAdd = (MYSTRPROCSTR)GetProcAddress(hinstLib, (LPCSTR)funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 if ((fRunTimeLinkSuccess = (ProcAdd != NULL)) != 0)
941 {
942 if (string_result == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100943 retval_int = ((MYSTRPROCINT)ProcAdd)((LPSTR)argstring);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100945 retval_str = (char_u *)(ProcAdd)((LPSTR)argstring);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 }
947 }
948 else
949 {
950 /* Call with number argument */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100951 ProcAddI = (MYINTPROCSTR) GetProcAddress(hinstLib, (LPCSTR)funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 if ((fRunTimeLinkSuccess = (ProcAddI != NULL)) != 0)
953 {
954 if (string_result == NULL)
955 retval_int = ((MYINTPROCINT)ProcAddI)(argint);
956 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100957 retval_str = (char_u *)(ProcAddI)(argint);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 }
959 }
960
961 // Save the string before we free the library.
962 // Assume that a "1" result is an illegal pointer.
963 if (string_result == NULL)
964 *number_result = retval_int;
965 else if (retval_str != NULL
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100966 && (len = check_str_len(retval_str)) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 {
968 *string_result = lalloc((long_u)len, TRUE);
969 if (*string_result != NULL)
970 mch_memmove(*string_result, retval_str, len);
971 }
972
973#ifdef HAVE_TRY_EXCEPT
974 }
975 __except(EXCEPTION_EXECUTE_HANDLER)
976 {
977 if (GetExceptionCode() == EXCEPTION_STACK_OVERFLOW)
978 RESETSTKOFLW();
979 fRunTimeLinkSuccess = 0;
980 }
981#endif
982
983 // Free the DLL module.
984 (void)FreeLibrary(hinstLib);
985 }
986
987 if (!fRunTimeLinkSuccess)
988 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100989 semsg(_(e_libcall), funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 return FAIL;
991 }
992
993 return OK;
994}
995#endif
996
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997/*
998 * Debugging helper: expose the MCH_WRITE_DUMP stuff to other modules
999 */
1000 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01001001DumpPutS(const char *psz UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002{
1003# ifdef MCH_WRITE_DUMP
1004 if (fdDump)
1005 {
1006 fputs(psz, fdDump);
1007 if (psz[strlen(psz) - 1] != '\n')
1008 fputc('\n', fdDump);
1009 fflush(fdDump);
1010 }
1011# endif
1012}
1013
1014#ifdef _DEBUG
1015
1016void __cdecl
1017Trace(
1018 char *pszFormat,
1019 ...)
1020{
1021 CHAR szBuff[2048];
1022 va_list args;
1023
1024 va_start(args, pszFormat);
1025 vsprintf(szBuff, pszFormat, args);
1026 va_end(args);
1027
1028 OutputDebugString(szBuff);
1029}
1030
1031#endif //_DEBUG
1032
Bram Moolenaar843ee412004-06-30 16:16:41 +00001033#if !defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001034# ifdef FEAT_TITLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035extern HWND g_hWnd; /* This is in os_win32.c. */
1036# endif
1037
1038/*
1039 * Showing the printer dialog is tricky since we have no GUI
1040 * window to parent it. The following routines are needed to
1041 * get the window parenting and Z-order to work properly.
1042 */
1043 static void
1044GetConsoleHwnd(void)
1045{
1046# define MY_BUFSIZE 1024 // Buffer size for console window titles.
1047
1048 char pszNewWindowTitle[MY_BUFSIZE]; // Contains fabricated WindowTitle.
1049 char pszOldWindowTitle[MY_BUFSIZE]; // Contains original WindowTitle.
1050
1051 /* Skip if it's already set. */
1052 if (s_hwnd != 0)
1053 return;
1054
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001055# ifdef FEAT_TITLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 /* Window handle may have been found by init code (Windows NT only) */
1057 if (g_hWnd != 0)
1058 {
1059 s_hwnd = g_hWnd;
1060 return;
1061 }
1062# endif
1063
1064 GetConsoleTitle(pszOldWindowTitle, MY_BUFSIZE);
1065
1066 wsprintf(pszNewWindowTitle, "%s/%d/%d",
1067 pszOldWindowTitle,
1068 GetTickCount(),
1069 GetCurrentProcessId());
1070 SetConsoleTitle(pszNewWindowTitle);
1071 Sleep(40);
1072 s_hwnd = FindWindow(NULL, pszNewWindowTitle);
1073
1074 SetConsoleTitle(pszOldWindowTitle);
1075}
Bram Moolenaar843ee412004-06-30 16:16:41 +00001076
1077/*
1078 * Console implementation of ":winpos".
1079 */
1080 int
1081mch_get_winpos(int *x, int *y)
1082{
1083 RECT rect;
1084
1085 GetConsoleHwnd();
1086 GetWindowRect(s_hwnd, &rect);
1087 *x = rect.left;
1088 *y = rect.top;
1089 return OK;
1090}
1091
1092/*
1093 * Console implementation of ":winpos x y".
1094 */
1095 void
1096mch_set_winpos(int x, int y)
1097{
1098 GetConsoleHwnd();
1099 SetWindowPos(s_hwnd, NULL, x, y, 0, 0,
1100 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
1101}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102#endif
1103
1104#if (defined(FEAT_PRINTER) && !defined(FEAT_POSTSCRIPT)) || defined(PROTO)
1105
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106/*=================================================================
1107 * Win32 printer stuff
1108 */
1109
1110static HFONT prt_font_handles[2][2][2];
1111static PRINTDLG prt_dlg;
1112static const int boldface[2] = {FW_REGULAR, FW_BOLD};
1113static TEXTMETRIC prt_tm;
1114static int prt_line_height;
1115static int prt_number_width;
1116static int prt_left_margin;
1117static int prt_right_margin;
1118static int prt_top_margin;
1119static char_u szAppName[] = TEXT("VIM");
1120static HWND hDlgPrint;
1121static int *bUserAbort = NULL;
1122static char_u *prt_name = NULL;
1123
1124/* Defines which are also in vim.rc. */
1125#define IDC_BOX1 400
1126#define IDC_PRINTTEXT1 401
1127#define IDC_PRINTTEXT2 402
1128#define IDC_PROGRESS 403
1129
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001130 static BOOL
1131vimSetDlgItemText(HWND hDlg, int nIDDlgItem, char_u *s)
1132{
1133 WCHAR *wp = NULL;
1134 BOOL ret;
1135
1136 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
1137 {
1138 wp = enc_to_utf16(s, NULL);
1139 }
1140 if (wp != NULL)
1141 {
1142 ret = SetDlgItemTextW(hDlg, nIDDlgItem, wp);
1143 vim_free(wp);
1144 return ret;
1145 }
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001146 return SetDlgItemText(hDlg, nIDDlgItem, (LPCSTR)s);
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001147}
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001148
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149/*
1150 * Convert BGR to RGB for Windows GDI calls
1151 */
1152 static COLORREF
1153swap_me(COLORREF colorref)
1154{
1155 int temp;
1156 char *ptr = (char *)&colorref;
1157
1158 temp = *(ptr);
1159 *(ptr ) = *(ptr + 2);
1160 *(ptr + 2) = temp;
1161 return colorref;
1162}
1163
Bram Moolenaared39e1d2008-08-09 17:55:22 +00001164/* Attempt to make this work for old and new compilers */
Bram Moolenaar9f733d12011-09-21 20:09:42 +02001165#if !defined(_WIN64) && (!defined(_MSC_VER) || _MSC_VER < 1300)
Bram Moolenaared39e1d2008-08-09 17:55:22 +00001166# define PDP_RETVAL BOOL
1167#else
1168# define PDP_RETVAL INT_PTR
1169#endif
1170
Bram Moolenaared39e1d2008-08-09 17:55:22 +00001171 static PDP_RETVAL CALLBACK
Bram Moolenaar1266d672017-02-01 13:43:36 +01001172PrintDlgProc(
1173 HWND hDlg,
1174 UINT message,
1175 WPARAM wParam UNUSED,
1176 LPARAM lParam UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177{
1178#ifdef FEAT_GETTEXT
1179 NONCLIENTMETRICS nm;
1180 static HFONT hfont;
1181#endif
1182
1183 switch (message)
1184 {
1185 case WM_INITDIALOG:
1186#ifdef FEAT_GETTEXT
1187 nm.cbSize = sizeof(NONCLIENTMETRICS);
1188 if (SystemParametersInfo(
1189 SPI_GETNONCLIENTMETRICS,
1190 sizeof(NONCLIENTMETRICS),
1191 &nm,
1192 0))
1193 {
1194 char buff[MAX_PATH];
1195 int i;
1196
1197 /* Translate the dialog texts */
1198 hfont = CreateFontIndirect(&nm.lfMessageFont);
1199 for (i = IDC_PRINTTEXT1; i <= IDC_PROGRESS; i++)
1200 {
1201 SendDlgItemMessage(hDlg, i, WM_SETFONT, (WPARAM)hfont, 1);
1202 if (GetDlgItemText(hDlg,i, buff, sizeof(buff)))
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001203 vimSetDlgItemText(hDlg,i, (char_u *)_(buff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 }
1205 SendDlgItemMessage(hDlg, IDCANCEL,
1206 WM_SETFONT, (WPARAM)hfont, 1);
1207 if (GetDlgItemText(hDlg,IDCANCEL, buff, sizeof(buff)))
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001208 vimSetDlgItemText(hDlg,IDCANCEL, (char_u *)_(buff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 }
1210#endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001211 SetWindowText(hDlg, (LPCSTR)szAppName);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 if (prt_name != NULL)
1213 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001214 vimSetDlgItemText(hDlg, IDC_PRINTTEXT2, (char_u *)prt_name);
Bram Moolenaard23a8232018-02-10 18:45:26 +01001215 VIM_CLEAR(prt_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 }
1217 EnableMenuItem(GetSystemMenu(hDlg, FALSE), SC_CLOSE, MF_GRAYED);
1218#ifndef FEAT_GUI
1219 BringWindowToTop(s_hwnd);
1220#endif
1221 return TRUE;
1222
1223 case WM_COMMAND:
1224 *bUserAbort = TRUE;
1225 EnableWindow(GetParent(hDlg), TRUE);
1226 DestroyWindow(hDlg);
1227 hDlgPrint = NULL;
1228#ifdef FEAT_GETTEXT
1229 DeleteObject(hfont);
1230#endif
1231 return TRUE;
1232 }
1233 return FALSE;
1234}
1235
1236 static BOOL CALLBACK
Bram Moolenaar1266d672017-02-01 13:43:36 +01001237AbortProc(HDC hdcPrn UNUSED, int iCode UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238{
1239 MSG msg;
1240
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02001241 while (!*bUserAbort && pPeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 {
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02001243 if (!hDlgPrint || !pIsDialogMessage(hDlgPrint, &msg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 {
1245 TranslateMessage(&msg);
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02001246 pDispatchMessage(&msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 }
1248 }
1249 return !*bUserAbort;
1250}
1251
1252#ifndef FEAT_GUI
1253
Bram Moolenaar26fdd7d2011-11-30 13:42:44 +01001254 static UINT_PTR CALLBACK
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255PrintHookProc(
1256 HWND hDlg, // handle to dialog box
1257 UINT uiMsg, // message identifier
1258 WPARAM wParam, // message parameter
1259 LPARAM lParam // message parameter
1260 )
1261{
1262 HWND hwndOwner;
1263 RECT rc, rcDlg, rcOwner;
1264 PRINTDLG *pPD;
1265
1266 if (uiMsg == WM_INITDIALOG)
1267 {
1268 // Get the owner window and dialog box rectangles.
1269 if ((hwndOwner = GetParent(hDlg)) == NULL)
1270 hwndOwner = GetDesktopWindow();
1271
1272 GetWindowRect(hwndOwner, &rcOwner);
1273 GetWindowRect(hDlg, &rcDlg);
1274 CopyRect(&rc, &rcOwner);
1275
1276 // Offset the owner and dialog box rectangles so that
1277 // right and bottom values represent the width and
1278 // height, and then offset the owner again to discard
1279 // space taken up by the dialog box.
1280
1281 OffsetRect(&rcDlg, -rcDlg.left, -rcDlg.top);
1282 OffsetRect(&rc, -rc.left, -rc.top);
1283 OffsetRect(&rc, -rcDlg.right, -rcDlg.bottom);
1284
1285 // The new position is the sum of half the remaining
1286 // space and the owner's original position.
1287
1288 SetWindowPos(hDlg,
1289 HWND_TOP,
1290 rcOwner.left + (rc.right / 2),
1291 rcOwner.top + (rc.bottom / 2),
1292 0, 0, // ignores size arguments
1293 SWP_NOSIZE);
1294
1295 /* tackle the printdlg copiesctrl problem */
1296 pPD = (PRINTDLG *)lParam;
1297 pPD->nCopies = (WORD)pPD->lCustData;
1298 SetDlgItemInt( hDlg, edt3, pPD->nCopies, FALSE );
1299 /* Bring the window to top */
1300 BringWindowToTop(GetParent(hDlg));
1301 SetForegroundWindow(hDlg);
1302 }
1303
1304 return FALSE;
1305}
1306#endif
1307
1308 void
1309mch_print_cleanup(void)
1310{
1311 int pifItalic;
1312 int pifBold;
1313 int pifUnderline;
1314
1315 for (pifBold = 0; pifBold <= 1; pifBold++)
1316 for (pifItalic = 0; pifItalic <= 1; pifItalic++)
1317 for (pifUnderline = 0; pifUnderline <= 1; pifUnderline++)
1318 DeleteObject(prt_font_handles[pifBold][pifItalic][pifUnderline]);
1319
1320 if (prt_dlg.hDC != NULL)
1321 DeleteDC(prt_dlg.hDC);
1322 if (!*bUserAbort)
1323 SendMessage(hDlgPrint, WM_COMMAND, 0, 0);
1324}
1325
1326 static int
1327to_device_units(int idx, int dpi, int physsize, int offset, int def_number)
1328{
1329 int ret = 0;
1330 int u;
1331 int nr;
1332
1333 u = prt_get_unit(idx);
1334 if (u == PRT_UNIT_NONE)
1335 {
1336 u = PRT_UNIT_PERC;
1337 nr = def_number;
1338 }
1339 else
1340 nr = printer_opts[idx].number;
1341
1342 switch (u)
1343 {
1344 case PRT_UNIT_PERC:
1345 ret = (physsize * nr) / 100;
1346 break;
1347 case PRT_UNIT_INCH:
1348 ret = (nr * dpi);
1349 break;
1350 case PRT_UNIT_MM:
1351 ret = (nr * 10 * dpi) / 254;
1352 break;
1353 case PRT_UNIT_POINT:
1354 ret = (nr * 10 * dpi) / 720;
1355 break;
1356 }
1357
1358 if (ret < offset)
1359 return 0;
1360 else
1361 return ret - offset;
1362}
1363
1364 static int
1365prt_get_cpl(void)
1366{
1367 int hr;
1368 int phyw;
1369 int dvoff;
1370 int rev_offset;
1371 int dpi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372
1373 GetTextMetrics(prt_dlg.hDC, &prt_tm);
1374 prt_line_height = prt_tm.tmHeight + prt_tm.tmExternalLeading;
1375
1376 hr = GetDeviceCaps(prt_dlg.hDC, HORZRES);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 phyw = GetDeviceCaps(prt_dlg.hDC, PHYSICALWIDTH);
1378 dvoff = GetDeviceCaps(prt_dlg.hDC, PHYSICALOFFSETX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 dpi = GetDeviceCaps(prt_dlg.hDC, LOGPIXELSX);
1380
1381 rev_offset = phyw - (dvoff + hr);
1382
1383 prt_left_margin = to_device_units(OPT_PRINT_LEFT, dpi, phyw, dvoff, 10);
1384 if (prt_use_number())
1385 {
1386 prt_number_width = PRINT_NUMBER_WIDTH * prt_tm.tmAveCharWidth;
1387 prt_left_margin += prt_number_width;
1388 }
1389 else
1390 prt_number_width = 0;
1391
1392 prt_right_margin = hr - to_device_units(OPT_PRINT_RIGHT, dpi, phyw,
1393 rev_offset, 5);
1394
1395 return (prt_right_margin - prt_left_margin) / prt_tm.tmAveCharWidth;
1396}
1397
1398 static int
1399prt_get_lpp(void)
1400{
1401 int vr;
1402 int phyw;
1403 int dvoff;
1404 int rev_offset;
1405 int bottom_margin;
1406 int dpi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407
1408 vr = GetDeviceCaps(prt_dlg.hDC, VERTRES);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 phyw = GetDeviceCaps(prt_dlg.hDC, PHYSICALHEIGHT);
1410 dvoff = GetDeviceCaps(prt_dlg.hDC, PHYSICALOFFSETY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 dpi = GetDeviceCaps(prt_dlg.hDC, LOGPIXELSY);
1412
1413 rev_offset = phyw - (dvoff + vr);
1414
1415 prt_top_margin = to_device_units(OPT_PRINT_TOP, dpi, phyw, dvoff, 5);
1416
1417 /* adjust top margin if there is a header */
1418 prt_top_margin += prt_line_height * prt_header_height();
1419
1420 bottom_margin = vr - to_device_units(OPT_PRINT_BOT, dpi, phyw,
1421 rev_offset, 5);
1422
1423 return (bottom_margin - prt_top_margin) / prt_line_height;
1424}
1425
1426 int
1427mch_print_init(prt_settings_T *psettings, char_u *jobname, int forceit)
1428{
1429 static HGLOBAL stored_dm = NULL;
1430 static HGLOBAL stored_devn = NULL;
1431 static int stored_nCopies = 1;
1432 static int stored_nFlags = 0;
1433
1434 LOGFONT fLogFont;
1435 int pifItalic;
1436 int pifBold;
1437 int pifUnderline;
1438
1439 DEVMODE *mem;
1440 DEVNAMES *devname;
1441 int i;
1442
1443 bUserAbort = &(psettings->user_abort);
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02001444 vim_memset(&prt_dlg, 0, sizeof(PRINTDLG));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 prt_dlg.lStructSize = sizeof(PRINTDLG);
1446#ifndef FEAT_GUI
1447 GetConsoleHwnd(); /* get value of s_hwnd */
1448#endif
1449 prt_dlg.hwndOwner = s_hwnd;
1450 prt_dlg.Flags = PD_NOPAGENUMS | PD_NOSELECTION | PD_RETURNDC;
1451 if (!forceit)
1452 {
1453 prt_dlg.hDevMode = stored_dm;
1454 prt_dlg.hDevNames = stored_devn;
1455 prt_dlg.lCustData = stored_nCopies; // work around bug in print dialog
1456#ifndef FEAT_GUI
1457 /*
1458 * Use hook to prevent console window being sent to back
1459 */
1460 prt_dlg.lpfnPrintHook = PrintHookProc;
1461 prt_dlg.Flags |= PD_ENABLEPRINTHOOK;
1462#endif
1463 prt_dlg.Flags |= stored_nFlags;
1464 }
1465
1466 /*
1467 * If bang present, return default printer setup with no dialog
1468 * never show dialog if we are running over telnet
1469 */
1470 if (forceit
1471#ifndef FEAT_GUI
1472 || !term_console
1473#endif
1474 )
1475 {
1476 prt_dlg.Flags |= PD_RETURNDEFAULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 /*
1478 * MSDN suggests setting the first parameter to WINSPOOL for
1479 * NT, but NULL appears to work just as well.
1480 */
1481 if (*p_pdev != NUL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001482 prt_dlg.hDC = CreateDC(NULL, (LPCSTR)p_pdev, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 {
1485 prt_dlg.Flags |= PD_RETURNDEFAULT;
1486 if (PrintDlg(&prt_dlg) == 0)
1487 goto init_fail_dlg;
1488 }
1489 }
1490 else if (PrintDlg(&prt_dlg) == 0)
1491 goto init_fail_dlg;
1492 else
1493 {
1494 /*
1495 * keep the previous driver context
1496 */
1497 stored_dm = prt_dlg.hDevMode;
1498 stored_devn = prt_dlg.hDevNames;
1499 stored_nFlags = prt_dlg.Flags;
1500 stored_nCopies = prt_dlg.nCopies;
1501 }
1502
1503 if (prt_dlg.hDC == NULL)
1504 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001505 emsg(_("E237: Printer selection failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 mch_print_cleanup();
1507 return FALSE;
1508 }
1509
1510 /* Not all printer drivers report the support of color (or grey) in the
1511 * same way. Let's set has_color if there appears to be some way to print
1512 * more than B&W. */
1513 i = GetDeviceCaps(prt_dlg.hDC, NUMCOLORS);
1514 psettings->has_color = (GetDeviceCaps(prt_dlg.hDC, BITSPIXEL) > 1
1515 || GetDeviceCaps(prt_dlg.hDC, PLANES) > 1
1516 || i > 2 || i == -1);
1517
1518 /* Ensure all font styles are baseline aligned */
1519 SetTextAlign(prt_dlg.hDC, TA_BASELINE|TA_LEFT);
1520
1521 /*
1522 * On some windows systems the nCopies parameter is not
1523 * passed back correctly. It must be retrieved from the
1524 * hDevMode struct.
1525 */
1526 mem = (DEVMODE *)GlobalLock(prt_dlg.hDevMode);
1527 if (mem != NULL)
1528 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 if (mem->dmCopies != 1)
1530 stored_nCopies = mem->dmCopies;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531 if ((mem->dmFields & DM_DUPLEX) && (mem->dmDuplex & ~DMDUP_SIMPLEX))
1532 psettings->duplex = TRUE;
1533 if ((mem->dmFields & DM_COLOR) && (mem->dmColor & DMCOLOR_COLOR))
1534 psettings->has_color = TRUE;
1535 }
1536 GlobalUnlock(prt_dlg.hDevMode);
1537
1538 devname = (DEVNAMES *)GlobalLock(prt_dlg.hDevNames);
1539 if (devname != 0)
1540 {
1541 char_u *printer_name = (char_u *)devname + devname->wDeviceOffset;
1542 char_u *port_name = (char_u *)devname +devname->wOutputOffset;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001543 char_u *text = (char_u *)_("to %s on %s");
Bram Moolenaarf191d552014-10-09 17:05:56 +02001544 char_u *printer_name_orig = printer_name;
1545 char_u *port_name_orig = port_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546
Bram Moolenaarf191d552014-10-09 17:05:56 +02001547 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
1548 {
1549 char_u *to_free = NULL;
1550 int maxlen;
1551
Bram Moolenaar861d80a2014-10-16 20:35:36 +02001552 acp_to_enc(printer_name, (int)STRLEN(printer_name), &to_free,
1553 &maxlen);
Bram Moolenaarf191d552014-10-09 17:05:56 +02001554 if (to_free != NULL)
1555 printer_name = to_free;
Bram Moolenaar861d80a2014-10-16 20:35:36 +02001556 acp_to_enc(port_name, (int)STRLEN(port_name), &to_free, &maxlen);
Bram Moolenaarf191d552014-10-09 17:05:56 +02001557 if (to_free != NULL)
1558 port_name = to_free;
1559 }
Bram Moolenaare6a91fd2008-07-24 18:51:11 +00001560 prt_name = alloc((unsigned)(STRLEN(printer_name) + STRLEN(port_name)
1561 + STRLEN(text)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 if (prt_name != NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001563 wsprintf((char *)prt_name, (const char *)text,
1564 printer_name, port_name);
Bram Moolenaarf191d552014-10-09 17:05:56 +02001565 if (printer_name != printer_name_orig)
1566 vim_free(printer_name);
1567 if (port_name != port_name_orig)
1568 vim_free(port_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 }
1570 GlobalUnlock(prt_dlg.hDevNames);
1571
1572 /*
1573 * Initialise the font according to 'printfont'
1574 */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02001575 vim_memset(&fLogFont, 0, sizeof(fLogFont));
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001576 if (get_logfont(&fLogFont, p_pfn, prt_dlg.hDC, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001578 semsg(_("E613: Unknown printer font: %s"), p_pfn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 mch_print_cleanup();
1580 return FALSE;
1581 }
1582
1583 for (pifBold = 0; pifBold <= 1; pifBold++)
1584 for (pifItalic = 0; pifItalic <= 1; pifItalic++)
1585 for (pifUnderline = 0; pifUnderline <= 1; pifUnderline++)
1586 {
1587 fLogFont.lfWeight = boldface[pifBold];
1588 fLogFont.lfItalic = pifItalic;
1589 fLogFont.lfUnderline = pifUnderline;
1590 prt_font_handles[pifBold][pifItalic][pifUnderline]
1591 = CreateFontIndirect(&fLogFont);
1592 }
1593
1594 SetBkMode(prt_dlg.hDC, OPAQUE);
1595 SelectObject(prt_dlg.hDC, prt_font_handles[0][0][0]);
1596
1597 /*
1598 * Fill in the settings struct
1599 */
1600 psettings->chars_per_line = prt_get_cpl();
1601 psettings->lines_per_page = prt_get_lpp();
Bram Moolenaar7ddc6422014-09-27 11:18:19 +02001602 if (prt_dlg.Flags & PD_USEDEVMODECOPIESANDCOLLATE)
1603 {
1604 psettings->n_collated_copies = (prt_dlg.Flags & PD_COLLATE)
1605 ? prt_dlg.nCopies : 1;
1606 psettings->n_uncollated_copies = (prt_dlg.Flags & PD_COLLATE)
1607 ? 1 : prt_dlg.nCopies;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608
Bram Moolenaar7ddc6422014-09-27 11:18:19 +02001609 if (psettings->n_collated_copies == 0)
1610 psettings->n_collated_copies = 1;
1611
1612 if (psettings->n_uncollated_copies == 0)
1613 psettings->n_uncollated_copies = 1;
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01001614 }
1615 else
1616 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617 psettings->n_collated_copies = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 psettings->n_uncollated_copies = 1;
Bram Moolenaar7ddc6422014-09-27 11:18:19 +02001619 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620
1621 psettings->jobname = jobname;
1622
1623 return TRUE;
1624
1625init_fail_dlg:
1626 {
1627 DWORD err = CommDlgExtendedError();
1628
1629 if (err)
1630 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 char_u *buf;
1632
1633 /* I suspect FormatMessage() doesn't work for values returned by
1634 * CommDlgExtendedError(). What does? */
1635 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
1636 FORMAT_MESSAGE_FROM_SYSTEM |
1637 FORMAT_MESSAGE_IGNORE_INSERTS,
1638 NULL, err, 0, (LPTSTR)(&buf), 0, NULL);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001639 semsg(_("E238: Print error: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 buf == NULL ? (char_u *)_("Unknown") : buf);
1641 LocalFree((LPVOID)(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642 }
1643 else
1644 msg_clr_eos(); /* Maybe canceled */
1645
1646 mch_print_cleanup();
1647 return FALSE;
1648 }
1649}
1650
1651
1652 int
1653mch_print_begin(prt_settings_T *psettings)
1654{
1655 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 char szBuffer[300];
Bram Moolenaar2290b1f2018-05-13 17:30:45 +02001657 WCHAR *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658
1659 hDlgPrint = CreateDialog(GetModuleHandle(NULL), TEXT("PrintDlgBox"),
1660 prt_dlg.hwndOwner, PrintDlgProc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 SetAbortProc(prt_dlg.hDC, AbortProc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 wsprintf(szBuffer, _("Printing '%s'"), gettail(psettings->jobname));
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001663 vimSetDlgItemText(hDlgPrint, IDC_PRINTTEXT1, (char_u *)szBuffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664
Bram Moolenaar2290b1f2018-05-13 17:30:45 +02001665 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
1666 wp = enc_to_utf16(psettings->jobname, NULL);
1667 if (wp != NULL)
1668 {
1669 DOCINFOW di;
1670
1671 vim_memset(&di, 0, sizeof(di));
1672 di.cbSize = sizeof(di);
1673 di.lpszDocName = wp;
1674 ret = StartDocW(prt_dlg.hDC, &di);
1675 vim_free(wp);
1676 }
1677 else
Bram Moolenaar2290b1f2018-05-13 17:30:45 +02001678 {
1679 DOCINFO di;
1680
1681 vim_memset(&di, 0, sizeof(di));
1682 di.cbSize = sizeof(di);
1683 di.lpszDocName = (LPCSTR)psettings->jobname;
1684 ret = StartDoc(prt_dlg.hDC, &di);
1685 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686
1687#ifdef FEAT_GUI
1688 /* Give focus back to main window (when using MDI). */
1689 SetFocus(s_hwnd);
1690#endif
1691
1692 return (ret > 0);
1693}
1694
1695 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01001696mch_print_end(prt_settings_T *psettings UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697{
1698 EndDoc(prt_dlg.hDC);
1699 if (!*bUserAbort)
1700 SendMessage(hDlgPrint, WM_COMMAND, 0, 0);
1701}
1702
1703 int
1704mch_print_end_page(void)
1705{
1706 return (EndPage(prt_dlg.hDC) > 0);
1707}
1708
1709 int
1710mch_print_begin_page(char_u *msg)
1711{
1712 if (msg != NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001713 vimSetDlgItemText(hDlgPrint, IDC_PROGRESS, msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 return (StartPage(prt_dlg.hDC) > 0);
1715}
1716
1717 int
1718mch_print_blank_page(void)
1719{
1720 return (mch_print_begin_page(NULL) ? (mch_print_end_page()) : FALSE);
1721}
1722
1723static int prt_pos_x = 0;
1724static int prt_pos_y = 0;
1725
1726 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001727mch_print_start_line(int margin, int page_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728{
1729 if (margin)
1730 prt_pos_x = -prt_number_width;
1731 else
1732 prt_pos_x = 0;
1733 prt_pos_y = page_line * prt_line_height
1734 + prt_tm.tmAscent + prt_tm.tmExternalLeading;
1735}
1736
1737 int
1738mch_print_text_out(char_u *p, int len)
1739{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 SIZE sz;
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001741 WCHAR *wp = NULL;
1742 int wlen = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001744 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
1745 {
1746 wp = enc_to_utf16(p, &wlen);
1747 }
1748 if (wp != NULL)
1749 {
1750 int ret = FALSE;
1751
1752 TextOutW(prt_dlg.hDC, prt_pos_x + prt_left_margin,
1753 prt_pos_y + prt_top_margin, wp, wlen);
1754 GetTextExtentPoint32W(prt_dlg.hDC, wp, wlen, &sz);
1755 vim_free(wp);
1756 prt_pos_x += (sz.cx - prt_tm.tmOverhang);
1757 /* This is wrong when printing spaces for a TAB. */
1758 if (p[len] != NUL)
1759 {
1760 wlen = MB_PTR2LEN(p + len);
1761 wp = enc_to_utf16(p + len, &wlen);
1762 if (wp != NULL)
1763 {
1764 GetTextExtentPoint32W(prt_dlg.hDC, wp, 1, &sz);
1765 ret = (prt_pos_x + prt_left_margin + sz.cx > prt_right_margin);
1766 vim_free(wp);
1767 }
1768 }
1769 return ret;
1770 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 TextOut(prt_dlg.hDC, prt_pos_x + prt_left_margin,
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001772 prt_pos_y + prt_top_margin,
1773 (LPCSTR)p, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774#ifndef FEAT_PROPORTIONAL_FONTS
1775 prt_pos_x += len * prt_tm.tmAveCharWidth;
1776 return (prt_pos_x + prt_left_margin + prt_tm.tmAveCharWidth
1777 + prt_tm.tmOverhang > prt_right_margin);
1778#else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001779 GetTextExtentPoint32(prt_dlg.hDC, (LPCSTR)p, len, &sz);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 prt_pos_x += (sz.cx - prt_tm.tmOverhang);
1781 /* This is wrong when printing spaces for a TAB. */
1782 if (p[len] == NUL)
1783 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 GetTextExtentPoint32(prt_dlg.hDC, p + len, 1, &sz);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 return (prt_pos_x + prt_left_margin + sz.cx > prt_right_margin);
1786#endif
1787}
1788
1789 void
1790mch_print_set_font(int iBold, int iItalic, int iUnderline)
1791{
1792 SelectObject(prt_dlg.hDC, prt_font_handles[iBold][iItalic][iUnderline]);
1793}
1794
1795 void
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001796mch_print_set_bg(long_u bgcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797{
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001798 SetBkColor(prt_dlg.hDC, GetNearestColor(prt_dlg.hDC,
1799 swap_me((COLORREF)bgcol)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 /*
1801 * With a white background we can draw characters transparent, which is
1802 * good for italic characters that overlap to the next char cell.
1803 */
1804 if (bgcol == 0xffffffUL)
1805 SetBkMode(prt_dlg.hDC, TRANSPARENT);
1806 else
1807 SetBkMode(prt_dlg.hDC, OPAQUE);
1808}
1809
1810 void
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001811mch_print_set_fg(long_u fgcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812{
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001813 SetTextColor(prt_dlg.hDC, GetNearestColor(prt_dlg.hDC,
1814 swap_me((COLORREF)fgcol)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815}
1816
1817#endif /*FEAT_PRINTER && !FEAT_POSTSCRIPT*/
1818
Bram Moolenaar58d98232005-07-23 22:25:46 +00001819
1820
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821#if defined(FEAT_SHORTCUT) || defined(PROTO)
Bram Moolenaar82881492012-11-20 16:53:39 +01001822# ifndef PROTO
1823# include <shlobj.h>
1824# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825
Bram Moolenaardce1e892019-02-10 23:18:53 +01001826typedef enum _FILE_INFO_BY_HANDLE_CLASS_ {
1827 FileBasicInfo_,
1828 FileStandardInfo_,
1829 FileNameInfo_,
1830 FileRenameInfo_,
1831 FileDispositionInfo_,
1832 FileAllocationInfo_,
1833 FileEndOfFileInfo_,
1834 FileStreamInfo_,
1835 FileCompressionInfo_,
1836 FileAttributeTagInfo_,
1837 FileIdBothDirectoryInfo_,
1838 FileIdBothDirectoryRestartInfo_,
1839 FileIoPriorityHintInfo_,
1840 FileRemoteProtocolInfo_,
1841 FileFullDirectoryInfo_,
1842 FileFullDirectoryRestartInfo_,
1843 FileStorageInfo_,
1844 FileAlignmentInfo_,
1845 FileIdInfo_,
1846 FileIdExtdDirectoryInfo_,
1847 FileIdExtdDirectoryRestartInfo_,
1848 FileDispositionInfoEx_,
1849 FileRenameInfoEx_,
1850 MaximumFileInfoByHandleClass_
1851} FILE_INFO_BY_HANDLE_CLASS_;
1852
1853typedef struct _FILE_NAME_INFO_ {
1854 DWORD FileNameLength;
1855 WCHAR FileName[1];
1856} FILE_NAME_INFO_;
1857
1858typedef BOOL (WINAPI *pfnGetFileInformationByHandleEx)(
1859 HANDLE hFile,
1860 FILE_INFO_BY_HANDLE_CLASS_ FileInformationClass,
1861 LPVOID lpFileInformation,
1862 DWORD dwBufferSize);
1863static pfnGetFileInformationByHandleEx pGetFileInformationByHandleEx = NULL;
1864
1865typedef BOOL (WINAPI *pfnGetVolumeInformationByHandleW)(
1866 HANDLE hFile,
1867 LPWSTR lpVolumeNameBuffer,
1868 DWORD nVolumeNameSize,
1869 LPDWORD lpVolumeSerialNumber,
1870 LPDWORD lpMaximumComponentLength,
1871 LPDWORD lpFileSystemFlags,
1872 LPWSTR lpFileSystemNameBuffer,
1873 DWORD nFileSystemNameSize);
1874static pfnGetVolumeInformationByHandleW pGetVolumeInformationByHandleW = NULL;
1875
1876 char_u *
1877resolve_reparse_point(char_u *fname)
1878{
1879 HANDLE h = INVALID_HANDLE_VALUE;
1880 DWORD size;
1881 char_u *rfname = NULL;
1882 FILE_NAME_INFO_ *nameinfo = NULL;
1883 WCHAR buff[MAX_PATH], *volnames = NULL;
1884 HANDLE hv;
1885 DWORD snfile, snfind;
1886 static BOOL loaded = FALSE;
1887
1888 if (pGetFileInformationByHandleEx == NULL ||
1889 pGetVolumeInformationByHandleW == NULL)
1890 {
1891 HMODULE hmod = GetModuleHandle("kernel32.dll");
1892
1893 if (loaded == TRUE)
1894 return NULL;
1895 pGetFileInformationByHandleEx = (pfnGetFileInformationByHandleEx)
1896 GetProcAddress(hmod, "GetFileInformationByHandleEx");
1897 pGetVolumeInformationByHandleW = (pfnGetVolumeInformationByHandleW)
1898 GetProcAddress(hmod, "GetVolumeInformationByHandleW");
1899 loaded = TRUE;
1900 if (pGetFileInformationByHandleEx == NULL ||
1901 pGetVolumeInformationByHandleW == NULL)
1902 return NULL;
1903 }
1904
1905 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
1906 {
1907 WCHAR *p;
1908
1909 p = enc_to_utf16(fname, NULL);
1910 if (p == NULL)
1911 goto fail;
1912
1913 if ((GetFileAttributesW(p) & FILE_ATTRIBUTE_REPARSE_POINT) == 0)
1914 {
1915 vim_free(p);
1916 goto fail;
1917 }
1918
1919 h = CreateFileW(p, 0, 0, NULL, OPEN_EXISTING,
1920 FILE_FLAG_BACKUP_SEMANTICS, NULL);
1921 vim_free(p);
1922 }
1923 else
1924 {
1925 if ((GetFileAttributes((char*) fname) &
1926 FILE_ATTRIBUTE_REPARSE_POINT) == 0)
1927 goto fail;
1928
1929 h = CreateFile((char*) fname, 0, 0, NULL, OPEN_EXISTING,
1930 FILE_FLAG_BACKUP_SEMANTICS, NULL);
1931 }
1932
1933 if (h == INVALID_HANDLE_VALUE)
1934 goto fail;
1935
1936 size = sizeof(FILE_NAME_INFO_) + sizeof(WCHAR) * (MAX_PATH - 1);
1937 nameinfo = (FILE_NAME_INFO_*)alloc(size + sizeof(WCHAR));
1938 if (nameinfo == NULL)
1939 goto fail;
1940
1941 if (!pGetFileInformationByHandleEx(h, FileNameInfo_, nameinfo, size))
1942 goto fail;
1943
1944 nameinfo->FileName[nameinfo->FileNameLength / sizeof(WCHAR)] = 0;
1945
1946 if (!pGetVolumeInformationByHandleW(
1947 h, NULL, 0, &snfile, NULL, NULL, NULL, 0))
1948 goto fail;
1949
1950 hv = FindFirstVolumeW(buff, MAX_PATH);
1951 if (hv == INVALID_HANDLE_VALUE)
1952 goto fail;
1953
1954 do {
1955 GetVolumeInformationW(
1956 buff, NULL, 0, &snfind, NULL, NULL, NULL, 0);
1957 if (snfind == snfile)
1958 break;
1959 } while (FindNextVolumeW(hv, buff, MAX_PATH));
1960
1961 FindVolumeClose(hv);
1962
1963 if (snfind != snfile)
1964 goto fail;
1965
1966 size = 0;
1967 if (!GetVolumePathNamesForVolumeNameW(buff, NULL, 0, &size) &&
1968 GetLastError() != ERROR_MORE_DATA)
1969 goto fail;
1970
1971 volnames = (WCHAR*)alloc(size * sizeof(WCHAR));
1972 if (!GetVolumePathNamesForVolumeNameW(buff, volnames, size,
1973 &size))
1974 goto fail;
1975
1976 wcscpy(buff, volnames);
1977 if (nameinfo->FileName[0] == '\\')
1978 wcscat(buff, nameinfo->FileName + 1);
1979 else
1980 wcscat(buff, nameinfo->FileName);
1981 rfname = utf16_to_enc(buff, NULL);
1982
1983fail:
1984 if (h != INVALID_HANDLE_VALUE)
1985 CloseHandle(h);
1986 if (nameinfo != NULL)
1987 vim_free(nameinfo);
1988 if (volnames != NULL)
1989 vim_free(volnames);
1990
1991 return rfname;
1992}
1993
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994/*
1995 * When "fname" is the name of a shortcut (*.lnk) resolve the file it points
1996 * to and return that name in allocated memory.
1997 * Otherwise NULL is returned.
1998 */
Bram Moolenaardce1e892019-02-10 23:18:53 +01001999 static char_u *
2000resolve_shortcut(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001{
2002 HRESULT hr;
2003 IShellLink *psl = NULL;
2004 IPersistFile *ppf = NULL;
2005 OLECHAR wsz[MAX_PATH];
2006 WIN32_FIND_DATA ffd; // we get those free of charge
Bram Moolenaar604729e2013-08-30 16:44:19 +02002007 CHAR buf[MAX_PATH]; // could have simply reused 'wsz'...
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 char_u *rfname = NULL;
2009 int len;
Bram Moolenaar604729e2013-08-30 16:44:19 +02002010 IShellLinkW *pslw = NULL;
2011 WIN32_FIND_DATAW ffdw; // we get those free of charge
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012
2013 /* Check if the file name ends in ".lnk". Avoid calling
2014 * CoCreateInstance(), it's quite slow. */
2015 if (fname == NULL)
2016 return rfname;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002017 len = (int)STRLEN(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 if (len <= 4 || STRNICMP(fname + len - 4, ".lnk", 4) != 0)
2019 return rfname;
2020
2021 CoInitialize(NULL);
2022
Bram Moolenaar604729e2013-08-30 16:44:19 +02002023 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
2024 {
2025 // create a link manager object and request its interface
2026 hr = CoCreateInstance(
2027 &CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
2028 &IID_IShellLinkW, (void**)&pslw);
2029 if (hr == S_OK)
2030 {
2031 WCHAR *p = enc_to_utf16(fname, NULL);
2032
2033 if (p != NULL)
2034 {
2035 // Get a pointer to the IPersistFile interface.
2036 hr = pslw->lpVtbl->QueryInterface(
2037 pslw, &IID_IPersistFile, (void**)&ppf);
2038 if (hr != S_OK)
2039 goto shortcut_errorw;
2040
2041 // "load" the name and resolve the link
2042 hr = ppf->lpVtbl->Load(ppf, p, STGM_READ);
2043 if (hr != S_OK)
2044 goto shortcut_errorw;
2045# if 0 // This makes Vim wait a long time if the target does not exist.
2046 hr = pslw->lpVtbl->Resolve(pslw, NULL, SLR_NO_UI);
2047 if (hr != S_OK)
2048 goto shortcut_errorw;
2049# endif
2050
2051 // Get the path to the link target.
2052 ZeroMemory(wsz, MAX_PATH * sizeof(WCHAR));
2053 hr = pslw->lpVtbl->GetPath(pslw, wsz, MAX_PATH, &ffdw, 0);
2054 if (hr == S_OK && wsz[0] != NUL)
2055 rfname = utf16_to_enc(wsz, NULL);
2056
2057shortcut_errorw:
2058 vim_free(p);
Bram Moolenaarcd981f22014-02-11 17:06:00 +01002059 goto shortcut_end;
Bram Moolenaar604729e2013-08-30 16:44:19 +02002060 }
2061 }
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002062 goto shortcut_end;
Bram Moolenaar604729e2013-08-30 16:44:19 +02002063 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 // create a link manager object and request its interface
2065 hr = CoCreateInstance(
2066 &CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
2067 &IID_IShellLink, (void**)&psl);
2068 if (hr != S_OK)
Bram Moolenaar604729e2013-08-30 16:44:19 +02002069 goto shortcut_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070
2071 // Get a pointer to the IPersistFile interface.
2072 hr = psl->lpVtbl->QueryInterface(
2073 psl, &IID_IPersistFile, (void**)&ppf);
2074 if (hr != S_OK)
Bram Moolenaar604729e2013-08-30 16:44:19 +02002075 goto shortcut_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076
2077 // full path string must be in Unicode.
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002078 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)fname, -1, wsz, MAX_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00002080 // "load" the name and resolve the link
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 hr = ppf->lpVtbl->Load(ppf, wsz, STGM_READ);
2082 if (hr != S_OK)
Bram Moolenaar604729e2013-08-30 16:44:19 +02002083 goto shortcut_end;
2084# if 0 // This makes Vim wait a long time if the target doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 hr = psl->lpVtbl->Resolve(psl, NULL, SLR_NO_UI);
2086 if (hr != S_OK)
Bram Moolenaar604729e2013-08-30 16:44:19 +02002087 goto shortcut_end;
2088# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089
2090 // Get the path to the link target.
2091 ZeroMemory(buf, MAX_PATH);
2092 hr = psl->lpVtbl->GetPath(psl, buf, MAX_PATH, &ffd, 0);
2093 if (hr == S_OK && buf[0] != NUL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002094 rfname = vim_strsave((char_u *)buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095
Bram Moolenaar604729e2013-08-30 16:44:19 +02002096shortcut_end:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097 // Release all interface pointers (both belong to the same object)
2098 if (ppf != NULL)
2099 ppf->lpVtbl->Release(ppf);
2100 if (psl != NULL)
2101 psl->lpVtbl->Release(psl);
Bram Moolenaar604729e2013-08-30 16:44:19 +02002102 if (pslw != NULL)
2103 pslw->lpVtbl->Release(pslw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104
2105 CoUninitialize();
2106 return rfname;
2107}
Bram Moolenaardce1e892019-02-10 23:18:53 +01002108
2109 char_u *
2110mch_resolve_path(char_u *fname, int reparse_point)
2111{
2112 char_u *path = resolve_shortcut(fname);
2113
2114 if (path == NULL && reparse_point)
2115 path = resolve_reparse_point(fname);
2116 return path;
2117}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118#endif
2119
2120#if (defined(FEAT_EVAL) && !defined(FEAT_GUI)) || defined(PROTO)
2121/*
2122 * Bring ourselves to the foreground. Does work if the OS doesn't allow it.
2123 */
2124 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002125win32_set_foreground(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126{
2127# ifndef FEAT_GUI
2128 GetConsoleHwnd(); /* get value of s_hwnd */
2129# endif
2130 if (s_hwnd != 0)
2131 SetForegroundWindow(s_hwnd);
2132}
2133#endif
2134
2135#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
2136/*
2137 * Client-server code for Vim
2138 *
2139 * Originally written by Paul Moore
2140 */
2141
2142/* In order to handle inter-process messages, we need to have a window. But
2143 * the functions in this module can be called before the main GUI window is
2144 * created (and may also be called in the console version, where there is no
2145 * GUI window at all).
2146 *
2147 * So we create a hidden window, and arrange to destroy it on exit.
2148 */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002149HWND message_window = 0; /* window that's handling messages */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150
2151#define VIM_CLASSNAME "VIM_MESSAGES"
2152#define VIM_CLASSNAME_LEN (sizeof(VIM_CLASSNAME) - 1)
2153
2154/* Communication is via WM_COPYDATA messages. The message type is send in
2155 * the dwData parameter. Types are defined here. */
2156#define COPYDATA_KEYS 0
2157#define COPYDATA_REPLY 1
2158#define COPYDATA_EXPR 10
2159#define COPYDATA_RESULT 11
2160#define COPYDATA_ERROR_RESULT 12
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002161#define COPYDATA_ENCODING 20
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162
2163/* This is a structure containing a server HWND and its name. */
2164struct server_id
2165{
2166 HWND hwnd;
2167 char_u *name;
2168};
2169
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002170/* Last received 'encoding' that the client uses. */
2171static char_u *client_enc = NULL;
2172
2173/*
2174 * Tell the other side what encoding we are using.
2175 * Errors are ignored.
2176 */
2177 static void
2178serverSendEnc(HWND target)
2179{
2180 COPYDATASTRUCT data;
2181
2182 data.dwData = COPYDATA_ENCODING;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002183 data.cbData = (DWORD)STRLEN(p_enc) + 1;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002184 data.lpData = p_enc;
2185 (void)SendMessage(target, WM_COPYDATA, (WPARAM)message_window,
2186 (LPARAM)(&data));
2187}
2188
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189/*
2190 * Clean up on exit. This destroys the hidden message window.
2191 */
2192 static void
2193#ifdef __BORLANDC__
2194 _RTLENTRYF
2195#endif
2196CleanUpMessaging(void)
2197{
2198 if (message_window != 0)
2199 {
2200 DestroyWindow(message_window);
2201 message_window = 0;
2202 }
2203}
2204
2205static int save_reply(HWND server, char_u *reply, int expr);
2206
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002207/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 * The window procedure for the hidden message window.
2209 * It handles callback messages and notifications from servers.
2210 * In order to process these messages, it is necessary to run a
2211 * message loop. Code which may run before the main message loop
2212 * is started (in the GUI) is careful to pump messages when it needs
2213 * to. Features which require message delivery during normal use will
2214 * not work in the console version - this basically means those
2215 * features which allow Vim to act as a server, rather than a client.
2216 */
2217 static LRESULT CALLBACK
2218Messaging_WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
2219{
2220 if (msg == WM_COPYDATA)
2221 {
2222 /* This is a message from another Vim. The dwData member of the
2223 * COPYDATASTRUCT determines the type of message:
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002224 * COPYDATA_ENCODING:
2225 * The encoding that the client uses. Following messages will
2226 * use this encoding, convert if needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 * COPYDATA_KEYS:
2228 * A key sequence. We are a server, and a client wants these keys
2229 * adding to the input queue.
2230 * COPYDATA_REPLY:
2231 * A reply. We are a client, and a server has sent this message
2232 * in response to a request. (server2client())
2233 * COPYDATA_EXPR:
2234 * An expression. We are a server, and a client wants us to
2235 * evaluate this expression.
2236 * COPYDATA_RESULT:
2237 * A reply. We are a client, and a server has sent this message
2238 * in response to a COPYDATA_EXPR.
2239 * COPYDATA_ERROR_RESULT:
2240 * A reply. We are a client, and a server has sent this message
2241 * in response to a COPYDATA_EXPR that failed to evaluate.
2242 */
2243 COPYDATASTRUCT *data = (COPYDATASTRUCT*)lParam;
2244 HWND sender = (HWND)wParam;
2245 COPYDATASTRUCT reply;
2246 char_u *res;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 int retval;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002248 char_u *str;
2249 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250
2251 switch (data->dwData)
2252 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002253 case COPYDATA_ENCODING:
2254 /* Remember the encoding that the client uses. */
2255 vim_free(client_enc);
2256 client_enc = enc_canonize((char_u *)data->lpData);
2257 return 1;
2258
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 case COPYDATA_KEYS:
2260 /* Remember who sent this, for <client> */
2261 clientWindow = sender;
2262
2263 /* Add the received keys to the input buffer. The loop waiting
2264 * for the user to do something should check the input buffer. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002265 str = serverConvert(client_enc, (char_u *)data->lpData, &tofree);
2266 server_to_input_buf(str);
2267 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268
2269# ifdef FEAT_GUI
2270 /* Wake up the main GUI loop. */
2271 if (s_hwnd != 0)
2272 PostMessage(s_hwnd, WM_NULL, 0, 0);
2273# endif
2274 return 1;
2275
2276 case COPYDATA_EXPR:
2277 /* Remember who sent this, for <client> */
2278 clientWindow = sender;
2279
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002280 str = serverConvert(client_enc, (char_u *)data->lpData, &tofree);
2281 res = eval_client_expr_to_string(str);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002282
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 if (res == NULL)
2284 {
Bram Moolenaar15bf76d2017-03-18 16:18:37 +01002285 char *err = _(e_invexprmsg);
2286 size_t len = STRLEN(str) + STRLEN(err) + 5;
2287
Bram Moolenaar1662ce12017-03-19 21:47:50 +01002288 res = alloc((unsigned)len);
Bram Moolenaar15bf76d2017-03-18 16:18:37 +01002289 if (res != NULL)
2290 vim_snprintf((char *)res, len, "%s: \"%s\"", err, str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 reply.dwData = COPYDATA_ERROR_RESULT;
2292 }
2293 else
2294 reply.dwData = COPYDATA_RESULT;
2295 reply.lpData = res;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002296 reply.cbData = (DWORD)STRLEN(res) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002298 serverSendEnc(sender);
Bram Moolenaar5246cd72013-06-16 16:41:47 +02002299 retval = (int)SendMessage(sender, WM_COPYDATA,
2300 (WPARAM)message_window, (LPARAM)(&reply));
Bram Moolenaar15bf76d2017-03-18 16:18:37 +01002301 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 vim_free(res);
2303 return retval;
2304
2305 case COPYDATA_REPLY:
2306 case COPYDATA_RESULT:
2307 case COPYDATA_ERROR_RESULT:
2308 if (data->lpData != NULL)
2309 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002310 str = serverConvert(client_enc, (char_u *)data->lpData,
2311 &tofree);
2312 if (tofree == NULL)
2313 str = vim_strsave(str);
2314 if (save_reply(sender, str,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 (data->dwData == COPYDATA_REPLY ? 0 :
2316 (data->dwData == COPYDATA_RESULT ? 1 :
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002317 2))) == FAIL)
2318 vim_free(str);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002319 else if (data->dwData == COPYDATA_REPLY)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 {
Bram Moolenaar427d51c2013-06-16 16:01:25 +02002321 char_u winstr[30];
2322
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002323 sprintf((char *)winstr, PRINTF_HEX_LONG_U, (long_u)sender);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002324 apply_autocmds(EVENT_REMOTEREPLY, winstr, str,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 TRUE, curbuf);
2326 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 }
2328 return 1;
2329 }
2330
2331 return 0;
2332 }
2333
2334 else if (msg == WM_ACTIVATE && wParam == WA_ACTIVE)
2335 {
2336 /* When the message window is activated (brought to the foreground),
2337 * this actually applies to the text window. */
2338#ifndef FEAT_GUI
2339 GetConsoleHwnd(); /* get value of s_hwnd */
2340#endif
2341 if (s_hwnd != 0)
2342 {
2343 SetForegroundWindow(s_hwnd);
2344 return 0;
2345 }
2346 }
2347
2348 return DefWindowProc(hwnd, msg, wParam, lParam);
2349}
2350
2351/*
2352 * Initialise the message handling process. This involves creating a window
2353 * to handle messages - the window will not be visible.
2354 */
2355 void
2356serverInitMessaging(void)
2357{
2358 WNDCLASS wndclass;
2359 HINSTANCE s_hinst;
2360
2361 /* Clean up on exit */
2362 atexit(CleanUpMessaging);
2363
2364 /* Register a window class - we only really care
2365 * about the window procedure
2366 */
2367 s_hinst = (HINSTANCE)GetModuleHandle(0);
2368 wndclass.style = 0;
2369 wndclass.lpfnWndProc = Messaging_WndProc;
2370 wndclass.cbClsExtra = 0;
2371 wndclass.cbWndExtra = 0;
2372 wndclass.hInstance = s_hinst;
2373 wndclass.hIcon = NULL;
2374 wndclass.hCursor = NULL;
2375 wndclass.hbrBackground = NULL;
2376 wndclass.lpszMenuName = NULL;
2377 wndclass.lpszClassName = VIM_CLASSNAME;
2378 RegisterClass(&wndclass);
2379
2380 /* Create the message window. It will be hidden, so the details don't
2381 * matter. Don't use WS_OVERLAPPEDWINDOW, it will make a shortcut remove
2382 * focus from gvim. */
2383 message_window = CreateWindow(VIM_CLASSNAME, "",
2384 WS_POPUPWINDOW | WS_CAPTION,
2385 CW_USEDEFAULT, CW_USEDEFAULT,
2386 100, 100, NULL, NULL,
2387 s_hinst, NULL);
2388}
2389
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002390/* Used by serverSendToVim() to find an alternate server name. */
2391static char_u *altname_buf_ptr = NULL;
2392
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393/*
2394 * Get the title of the window "hwnd", which is the Vim server name, in
2395 * "name[namelen]" and return the length.
2396 * Returns zero if window "hwnd" is not a Vim server.
2397 */
2398 static int
2399getVimServerName(HWND hwnd, char *name, int namelen)
2400{
2401 int len;
2402 char buffer[VIM_CLASSNAME_LEN + 1];
2403
2404 /* Ignore windows which aren't Vim message windows */
2405 len = GetClassName(hwnd, buffer, sizeof(buffer));
2406 if (len != VIM_CLASSNAME_LEN || STRCMP(buffer, VIM_CLASSNAME) != 0)
2407 return 0;
2408
2409 /* Get the title of the window */
2410 return GetWindowText(hwnd, name, namelen);
2411}
2412
2413 static BOOL CALLBACK
2414enumWindowsGetServer(HWND hwnd, LPARAM lparam)
2415{
2416 struct server_id *id = (struct server_id *)lparam;
2417 char server[MAX_PATH];
2418
2419 /* Get the title of the window */
2420 if (getVimServerName(hwnd, server, sizeof(server)) == 0)
2421 return TRUE;
2422
2423 /* If this is the server we're looking for, return its HWND */
2424 if (STRICMP(server, id->name) == 0)
2425 {
2426 id->hwnd = hwnd;
2427 return FALSE;
2428 }
2429
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002430 /* If we are looking for an alternate server, remember this name. */
2431 if (altname_buf_ptr != NULL
2432 && STRNICMP(server, id->name, STRLEN(id->name)) == 0
2433 && vim_isdigit(server[STRLEN(id->name)]))
2434 {
2435 STRCPY(altname_buf_ptr, server);
2436 altname_buf_ptr = NULL; /* don't use another name */
2437 }
2438
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 /* Otherwise, keep looking */
2440 return TRUE;
2441}
2442
2443 static BOOL CALLBACK
2444enumWindowsGetNames(HWND hwnd, LPARAM lparam)
2445{
2446 garray_T *ga = (garray_T *)lparam;
2447 char server[MAX_PATH];
2448
2449 /* Get the title of the window */
2450 if (getVimServerName(hwnd, server, sizeof(server)) == 0)
2451 return TRUE;
2452
2453 /* Add the name to the list */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002454 ga_concat(ga, (char_u *)server);
2455 ga_concat(ga, (char_u *)"\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 return TRUE;
2457}
2458
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002459struct enum_windows_s
2460{
2461 WNDENUMPROC lpEnumFunc;
2462 LPARAM lParam;
2463};
2464
2465 static BOOL CALLBACK
2466enum_windows_child(HWND hwnd, LPARAM lParam)
2467{
2468 struct enum_windows_s *ew = (struct enum_windows_s *)lParam;
2469
2470 return (ew->lpEnumFunc)(hwnd, ew->lParam);
2471}
2472
2473 static BOOL CALLBACK
2474enum_windows_toplevel(HWND hwnd, LPARAM lParam)
2475{
2476 struct enum_windows_s *ew = (struct enum_windows_s *)lParam;
2477
Bram Moolenaar95ba5c32018-10-07 22:47:07 +02002478 if ((ew->lpEnumFunc)(hwnd, ew->lParam))
2479 return TRUE;
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002480 return EnumChildWindows(hwnd, enum_windows_child, lParam);
2481}
2482
2483/* Enumerate all windows including children. */
2484 static BOOL
2485enum_windows(WNDENUMPROC lpEnumFunc, LPARAM lParam)
2486{
2487 struct enum_windows_s ew;
2488
2489 ew.lpEnumFunc = lpEnumFunc;
2490 ew.lParam = lParam;
2491 return EnumWindows(enum_windows_toplevel, (LPARAM)&ew);
2492}
2493
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 static HWND
2495findServer(char_u *name)
2496{
2497 struct server_id id;
2498
2499 id.name = name;
2500 id.hwnd = 0;
2501
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002502 enum_windows(enumWindowsGetServer, (LPARAM)(&id));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503
2504 return id.hwnd;
2505}
2506
2507 void
2508serverSetName(char_u *name)
2509{
2510 char_u *ok_name;
2511 HWND hwnd = 0;
2512 int i = 0;
2513 char_u *p;
2514
2515 /* Leave enough space for a 9-digit suffix to ensure uniqueness! */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002516 ok_name = alloc((unsigned)STRLEN(name) + 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517
2518 STRCPY(ok_name, name);
2519 p = ok_name + STRLEN(name);
2520
2521 for (;;)
2522 {
2523 /* This is inefficient - we're doing an EnumWindows loop for each
2524 * possible name. It would be better to grab all names in one go,
2525 * and scan the list each time...
2526 */
2527 hwnd = findServer(ok_name);
2528 if (hwnd == 0)
2529 break;
2530
2531 ++i;
2532 if (i >= 1000)
2533 break;
2534
2535 sprintf((char *)p, "%d", i);
2536 }
2537
2538 if (hwnd != 0)
2539 vim_free(ok_name);
2540 else
2541 {
2542 /* Remember the name */
2543 serverName = ok_name;
2544#ifdef FEAT_TITLE
2545 need_maketitle = TRUE; /* update Vim window title later */
2546#endif
2547
2548 /* Update the message window title */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002549 SetWindowText(message_window, (LPCSTR)ok_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550
2551#ifdef FEAT_EVAL
2552 /* Set the servername variable */
2553 set_vim_var_string(VV_SEND_SERVER, serverName, -1);
2554#endif
2555 }
2556}
2557
2558 char_u *
2559serverGetVimNames(void)
2560{
2561 garray_T ga;
2562
2563 ga_init2(&ga, 1, 100);
2564
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002565 enum_windows(enumWindowsGetNames, (LPARAM)(&ga));
Bram Moolenaar269ec652004-07-29 08:43:53 +00002566 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567
2568 return ga.ga_data;
2569}
2570
2571 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002572serverSendReply(
2573 char_u *name, /* Where to send. */
2574 char_u *reply) /* What to send. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575{
2576 HWND target;
2577 COPYDATASTRUCT data;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002578 long_u n = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579
2580 /* The "name" argument is a magic cookie obtained from expand("<client>").
2581 * It should be of the form 0xXXXXX - i.e. a C hex literal, which is the
2582 * value of the client's message window HWND.
2583 */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002584 sscanf((char *)name, SCANF_HEX_LONG_U, &n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 if (n == 0)
2586 return -1;
2587
2588 target = (HWND)n;
2589 if (!IsWindow(target))
2590 return -1;
2591
2592 data.dwData = COPYDATA_REPLY;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002593 data.cbData = (DWORD)STRLEN(reply) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 data.lpData = reply;
2595
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002596 serverSendEnc(target);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 if (SendMessage(target, WM_COPYDATA, (WPARAM)message_window,
2598 (LPARAM)(&data)))
2599 return 0;
2600
2601 return -1;
2602}
2603
2604 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002605serverSendToVim(
2606 char_u *name, /* Where to send. */
2607 char_u *cmd, /* What to send. */
2608 char_u **result, /* Result of eval'ed expression */
2609 void *ptarget, /* HWND of server */
2610 int asExpr, /* Expression or keys? */
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002611 int timeout, /* timeout in seconds or zero */
Bram Moolenaar05540972016-01-30 20:31:25 +01002612 int silent) /* don't complain about no server */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613{
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002614 HWND target;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 COPYDATASTRUCT data;
2616 char_u *retval = NULL;
2617 int retcode = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002618 char_u altname_buf[MAX_PATH];
2619
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01002620 /* Execute locally if no display or target is ourselves */
2621 if (serverName != NULL && STRICMP(name, serverName) == 0)
2622 return sendToLocalVim(cmd, asExpr, result);
2623
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002624 /* If the server name does not end in a digit then we look for an
2625 * alternate name. e.g. when "name" is GVIM the we may find GVIM2. */
2626 if (STRLEN(name) > 1 && !vim_isdigit(name[STRLEN(name) - 1]))
2627 altname_buf_ptr = altname_buf;
2628 altname_buf[0] = NUL;
2629 target = findServer(name);
2630 altname_buf_ptr = NULL;
2631 if (target == 0 && altname_buf[0] != NUL)
2632 /* Use another server name we found. */
2633 target = findServer(altname_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634
2635 if (target == 0)
2636 {
2637 if (!silent)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002638 semsg(_(e_noserver), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 return -1;
2640 }
2641
2642 if (ptarget)
2643 *(HWND *)ptarget = target;
2644
2645 data.dwData = asExpr ? COPYDATA_EXPR : COPYDATA_KEYS;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002646 data.cbData = (DWORD)STRLEN(cmd) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 data.lpData = cmd;
2648
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002649 serverSendEnc(target);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 if (SendMessage(target, WM_COPYDATA, (WPARAM)message_window,
2651 (LPARAM)(&data)) == 0)
2652 return -1;
2653
2654 if (asExpr)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002655 retval = serverGetReply(target, &retcode, TRUE, TRUE, timeout);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656
2657 if (result == NULL)
2658 vim_free(retval);
2659 else
2660 *result = retval; /* Caller assumes responsibility for freeing */
2661
2662 return retcode;
2663}
2664
2665/*
2666 * Bring the server to the foreground.
2667 */
2668 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002669serverForeground(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670{
2671 HWND target = findServer(name);
2672
2673 if (target != 0)
2674 SetForegroundWindow(target);
2675}
2676
2677/* Replies from server need to be stored until the client picks them up via
2678 * remote_read(). So we maintain a list of server-id/reply pairs.
2679 * Note that there could be multiple replies from one server pending if the
2680 * client is slow picking them up.
2681 * We just store the replies in a simple list. When we remove an entry, we
2682 * move list entries down to fill the gap.
2683 * The server ID is simply the HWND.
2684 */
2685typedef struct
2686{
2687 HWND server; /* server window */
2688 char_u *reply; /* reply string */
2689 int expr_result; /* 0 for REPLY, 1 for RESULT 2 for error */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002690} reply_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691
2692static garray_T reply_list = {0, 0, sizeof(reply_T), 5, 0};
2693
2694#define REPLY_ITEM(i) ((reply_T *)(reply_list.ga_data) + (i))
2695#define REPLY_COUNT (reply_list.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696
2697/* Flag which is used to wait for a reply */
2698static int reply_received = 0;
2699
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002700/*
2701 * Store a reply. "reply" must be allocated memory (or NULL).
2702 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 static int
2704save_reply(HWND server, char_u *reply, int expr)
2705{
2706 reply_T *rep;
2707
2708 if (ga_grow(&reply_list, 1) == FAIL)
2709 return FAIL;
2710
2711 rep = REPLY_ITEM(REPLY_COUNT);
2712 rep->server = server;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002713 rep->reply = reply;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 rep->expr_result = expr;
2715 if (rep->reply == NULL)
2716 return FAIL;
2717
2718 ++REPLY_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 reply_received = 1;
2720 return OK;
2721}
2722
2723/*
2724 * Get a reply from server "server".
2725 * When "expr_res" is non NULL, get the result of an expression, otherwise a
2726 * server2client() message.
2727 * When non NULL, point to return code. 0 => OK, -1 => ERROR
2728 * If "remove" is TRUE, consume the message, the caller must free it then.
2729 * if "wait" is TRUE block until a message arrives (or the server exits).
2730 */
2731 char_u *
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002732serverGetReply(HWND server, int *expr_res, int remove, int wait, int timeout)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733{
2734 int i;
2735 char_u *reply;
2736 reply_T *rep;
Bram Moolenaar15e737f2017-03-18 21:22:47 +01002737 int did_process = FALSE;
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002738 time_t start;
2739 time_t now;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740
2741 /* When waiting, loop until the message waiting for is received. */
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002742 time(&start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 for (;;)
2744 {
2745 /* Reset this here, in case a message arrives while we are going
2746 * through the already received messages. */
2747 reply_received = 0;
2748
2749 for (i = 0; i < REPLY_COUNT; ++i)
2750 {
2751 rep = REPLY_ITEM(i);
2752 if (rep->server == server
2753 && ((rep->expr_result != 0) == (expr_res != NULL)))
2754 {
2755 /* Save the values we've found for later */
2756 reply = rep->reply;
2757 if (expr_res != NULL)
2758 *expr_res = rep->expr_result == 1 ? 0 : -1;
2759
2760 if (remove)
2761 {
2762 /* Move the rest of the list down to fill the gap */
2763 mch_memmove(rep, rep + 1,
2764 (REPLY_COUNT - i - 1) * sizeof(reply_T));
2765 --REPLY_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 }
2767
2768 /* Return the reply to the caller, who takes on responsibility
2769 * for freeing it if "remove" is TRUE. */
2770 return reply;
2771 }
2772 }
2773
2774 /* If we got here, we didn't find a reply. Return immediately if the
2775 * "wait" parameter isn't set. */
2776 if (!wait)
Bram Moolenaar15e737f2017-03-18 21:22:47 +01002777 {
2778 /* Process pending messages once. Without this, looping on
2779 * remote_peek() would never get the reply. */
2780 if (!did_process)
2781 {
2782 did_process = TRUE;
2783 serverProcessPendingMessages();
2784 continue;
2785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 break;
Bram Moolenaar15e737f2017-03-18 21:22:47 +01002787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788
2789 /* We need to wait for a reply. Enter a message loop until the
2790 * "reply_received" flag gets set. */
2791
2792 /* Loop until we receive a reply */
2793 while (reply_received == 0)
2794 {
Bram Moolenaar42205552017-03-18 19:42:22 +01002795#ifdef FEAT_TIMERS
Bram Moolenaard23a8232018-02-10 18:45:26 +01002796 /* TODO: use the return value to decide how long to wait. */
Bram Moolenaar42205552017-03-18 19:42:22 +01002797 check_due_timer();
2798#endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002799 time(&now);
2800 if (timeout > 0 && (now - start) >= timeout)
2801 break;
2802
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 /* Wait for a SendMessage() call to us. This could be the reply
2804 * we are waiting for. Use a timeout of a second, to catch the
2805 * situation that the server died unexpectedly. */
2806 MsgWaitForMultipleObjects(0, NULL, TRUE, 1000, QS_ALLINPUT);
2807
2808 /* If the server has died, give up */
2809 if (!IsWindow(server))
2810 return NULL;
2811
2812 serverProcessPendingMessages();
2813 }
2814 }
2815
2816 return NULL;
2817}
2818
2819/*
2820 * Process any messages in the Windows message queue.
2821 */
2822 void
2823serverProcessPendingMessages(void)
2824{
2825 MSG msg;
2826
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02002827 while (pPeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 {
2829 TranslateMessage(&msg);
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02002830 pDispatchMessage(&msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 }
2832}
2833
2834#endif /* FEAT_CLIENTSERVER */
2835
2836#if defined(FEAT_GUI) || (defined(FEAT_PRINTER) && !defined(FEAT_POSTSCRIPT)) \
2837 || defined(PROTO)
2838
2839struct charset_pair
2840{
2841 char *name;
2842 BYTE charset;
2843};
2844
2845static struct charset_pair
2846charset_pairs[] =
2847{
2848 {"ANSI", ANSI_CHARSET},
2849 {"CHINESEBIG5", CHINESEBIG5_CHARSET},
2850 {"DEFAULT", DEFAULT_CHARSET},
2851 {"HANGEUL", HANGEUL_CHARSET},
2852 {"OEM", OEM_CHARSET},
2853 {"SHIFTJIS", SHIFTJIS_CHARSET},
2854 {"SYMBOL", SYMBOL_CHARSET},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 {"ARABIC", ARABIC_CHARSET},
2856 {"BALTIC", BALTIC_CHARSET},
2857 {"EASTEUROPE", EASTEUROPE_CHARSET},
2858 {"GB2312", GB2312_CHARSET},
2859 {"GREEK", GREEK_CHARSET},
2860 {"HEBREW", HEBREW_CHARSET},
2861 {"JOHAB", JOHAB_CHARSET},
2862 {"MAC", MAC_CHARSET},
2863 {"RUSSIAN", RUSSIAN_CHARSET},
2864 {"THAI", THAI_CHARSET},
2865 {"TURKISH", TURKISH_CHARSET},
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002866#ifdef VIETNAMESE_CHARSET
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 {"VIETNAMESE", VIETNAMESE_CHARSET},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868#endif
2869 {NULL, 0}
2870};
2871
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002872struct quality_pair
2873{
2874 char *name;
2875 DWORD quality;
2876};
2877
2878static struct quality_pair
2879quality_pairs[] = {
2880#ifdef CLEARTYPE_QUALITY
2881 {"CLEARTYPE", CLEARTYPE_QUALITY},
2882#endif
2883#ifdef ANTIALIASED_QUALITY
2884 {"ANTIALIASED", ANTIALIASED_QUALITY},
2885#endif
Bram Moolenaar73a733e2016-05-11 21:05:05 +02002886#ifdef NONANTIALIASED_QUALITY
2887 {"NONANTIALIASED", NONANTIALIASED_QUALITY},
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002888#endif
2889#ifdef PROOF_QUALITY
2890 {"PROOF", PROOF_QUALITY},
2891#endif
Bram Moolenaar4c9ce052016-04-04 21:06:19 +02002892#ifdef DRAFT_QUALITY
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002893 {"DRAFT", DRAFT_QUALITY},
2894#endif
2895 {"DEFAULT", DEFAULT_QUALITY},
2896 {NULL, 0}
2897};
2898
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899/*
2900 * Convert a charset ID to a name.
2901 * Return NULL when not recognized.
2902 */
2903 char *
2904charset_id2name(int id)
2905{
2906 struct charset_pair *cp;
2907
2908 for (cp = charset_pairs; cp->name != NULL; ++cp)
2909 if ((BYTE)id == cp->charset)
2910 break;
2911 return cp->name;
2912}
2913
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002914/*
2915 * Convert a quality ID to a name.
2916 * Return NULL when not recognized.
2917 */
2918 char *
2919quality_id2name(DWORD id)
2920{
2921 struct quality_pair *qp;
2922
2923 for (qp = quality_pairs; qp->name != NULL; ++qp)
2924 if (id == qp->quality)
2925 break;
2926 return qp->name;
2927}
2928
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929static const LOGFONT s_lfDefault =
2930{
2931 -12, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, DEFAULT_CHARSET,
2932 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
2933 PROOF_QUALITY, FIXED_PITCH | FF_DONTCARE,
2934 "Fixedsys" /* see _ReadVimIni */
2935};
2936
2937/* Initialise the "current height" to -12 (same as s_lfDefault) just
2938 * in case the user specifies a font in "guifont" with no size before a font
2939 * with an explicit size has been set. This defaults the size to this value
2940 * (-12 equates to roughly 9pt).
2941 */
2942int current_font_height = -12; /* also used in gui_w48.c */
2943
2944/* Convert a string representing a point size into pixels. The string should
2945 * be a positive decimal number, with an optional decimal point (eg, "12", or
2946 * "10.5"). The pixel value is returned, and a pointer to the next unconverted
2947 * character is stored in *end. The flag "vertical" says whether this
2948 * calculation is for a vertical (height) size or a horizontal (width) one.
2949 */
2950 static int
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002951points_to_pixels(char_u *str, char_u **end, int vertical, long_i pprinter_dc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952{
2953 int pixels;
2954 int points = 0;
2955 int divisor = 0;
2956 HWND hwnd = (HWND)0;
2957 HDC hdc;
2958 HDC printer_dc = (HDC)pprinter_dc;
2959
2960 while (*str != NUL)
2961 {
2962 if (*str == '.' && divisor == 0)
2963 {
2964 /* Start keeping a divisor, for later */
2965 divisor = 1;
2966 }
2967 else
2968 {
2969 if (!VIM_ISDIGIT(*str))
2970 break;
2971
2972 points *= 10;
2973 points += *str - '0';
2974 divisor *= 10;
2975 }
2976 ++str;
2977 }
2978
2979 if (divisor == 0)
2980 divisor = 1;
2981
2982 if (printer_dc == NULL)
2983 {
2984 hwnd = GetDesktopWindow();
2985 hdc = GetWindowDC(hwnd);
2986 }
2987 else
2988 hdc = printer_dc;
2989
2990 pixels = MulDiv(points,
2991 GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX),
2992 72 * divisor);
2993
2994 if (printer_dc == NULL)
2995 ReleaseDC(hwnd, hdc);
2996
2997 *end = str;
2998 return pixels;
2999}
3000
3001 static int CALLBACK
3002font_enumproc(
3003 ENUMLOGFONT *elf,
Bram Moolenaar1266d672017-02-01 13:43:36 +01003004 NEWTEXTMETRIC *ntm UNUSED,
3005 int type UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006 LPARAM lparam)
3007{
3008 /* Return value:
3009 * 0 = terminate now (monospace & ANSI)
3010 * 1 = continue, still no luck...
3011 * 2 = continue, but we have an acceptable LOGFONT
3012 * (monospace, not ANSI)
3013 * We use these values, as EnumFontFamilies returns 1 if the
3014 * callback function is never called. So, we check the return as
3015 * 0 = perfect, 2 = OK, 1 = no good...
3016 * It's not pretty, but it works!
3017 */
3018
3019 LOGFONT *lf = (LOGFONT *)(lparam);
3020
3021#ifndef FEAT_PROPORTIONAL_FONTS
3022 /* Ignore non-monospace fonts without further ado */
3023 if ((ntm->tmPitchAndFamily & 1) != 0)
3024 return 1;
3025#endif
3026
3027 /* Remember this LOGFONT as a "possible" */
3028 *lf = elf->elfLogFont;
3029
3030 /* Terminate the scan as soon as we find an ANSI font */
3031 if (lf->lfCharSet == ANSI_CHARSET
3032 || lf->lfCharSet == OEM_CHARSET
3033 || lf->lfCharSet == DEFAULT_CHARSET)
3034 return 0;
3035
3036 /* Continue the scan - we have a non-ANSI font */
3037 return 2;
3038}
3039
3040 static int
3041init_logfont(LOGFONT *lf)
3042{
3043 int n;
3044 HWND hwnd = GetDesktopWindow();
3045 HDC hdc = GetWindowDC(hwnd);
3046
3047 n = EnumFontFamilies(hdc,
3048 (LPCSTR)lf->lfFaceName,
3049 (FONTENUMPROC)font_enumproc,
3050 (LPARAM)lf);
3051
3052 ReleaseDC(hwnd, hdc);
3053
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003054 /* If we couldn't find a usable font, return failure */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 if (n == 1)
3056 return FAIL;
3057
3058 /* Tidy up the rest of the LOGFONT structure. We set to a basic
3059 * font - get_logfont() sets bold, italic, etc based on the user's
3060 * input.
3061 */
3062 lf->lfHeight = current_font_height;
3063 lf->lfWidth = 0;
3064 lf->lfItalic = FALSE;
3065 lf->lfUnderline = FALSE;
3066 lf->lfStrikeOut = FALSE;
3067 lf->lfWeight = FW_NORMAL;
3068
3069 /* Return success */
3070 return OK;
3071}
3072
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003073/*
3074 * Get font info from "name" into logfont "lf".
3075 * Return OK for a valid name, FAIL otherwise.
3076 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 int
3078get_logfont(
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003079 LOGFONT *lf,
3080 char_u *name,
3081 HDC printer_dc,
3082 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083{
3084 char_u *p;
3085 int i;
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003086 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087 static LOGFONT *lastlf = NULL;
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003088 char_u *acpname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089
3090 *lf = s_lfDefault;
3091 if (name == NULL)
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003092 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003094 /* Convert 'name' from 'encoding' to the current codepage, because
3095 * lf->lfFaceName uses the current codepage.
3096 * TODO: Use Wide APIs instead of ANSI APIs. */
3097 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
3098 {
3099 int len;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01003100 enc_to_acp(name, (int)STRLEN(name), &acpname, &len);
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003101 name = acpname;
3102 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 if (STRCMP(name, "*") == 0)
3104 {
3105#if defined(FEAT_GUI_W32)
3106 CHOOSEFONT cf;
3107 /* if name is "*", bring up std font dialog: */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02003108 vim_memset(&cf, 0, sizeof(cf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 cf.lStructSize = sizeof(cf);
3110 cf.hwndOwner = s_hwnd;
3111 cf.Flags = CF_SCREENFONTS | CF_FIXEDPITCHONLY | CF_INITTOLOGFONTSTRUCT;
3112 if (lastlf != NULL)
3113 *lf = *lastlf;
3114 cf.lpLogFont = lf;
3115 cf.nFontType = 0 ; //REGULAR_FONTTYPE;
3116 if (ChooseFont(&cf))
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003117 ret = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118#endif
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003119 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 }
3121
3122 /*
3123 * Split name up, it could be <name>:h<height>:w<width> etc.
3124 */
3125 for (p = name; *p && *p != ':'; p++)
3126 {
Bram Moolenaar38bc4952016-11-10 17:10:51 +01003127 if (p - name + 1 >= LF_FACESIZE)
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003128 goto theend; /* Name too long */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129 lf->lfFaceName[p - name] = *p;
3130 }
3131 if (p != name)
3132 lf->lfFaceName[p - name] = NUL;
3133
3134 /* First set defaults */
3135 lf->lfHeight = -12;
3136 lf->lfWidth = 0;
3137 lf->lfWeight = FW_NORMAL;
3138 lf->lfItalic = FALSE;
3139 lf->lfUnderline = FALSE;
3140 lf->lfStrikeOut = FALSE;
3141
3142 /*
3143 * If the font can't be found, try replacing '_' by ' '.
3144 */
3145 if (init_logfont(lf) == FAIL)
3146 {
3147 int did_replace = FALSE;
3148
3149 for (i = 0; lf->lfFaceName[i]; ++i)
Bram Moolenaar89e06c82017-08-11 20:55:55 +02003150 if (IsDBCSLeadByte(lf->lfFaceName[i]))
3151 ++i;
3152 else if (lf->lfFaceName[i] == '_')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 {
3154 lf->lfFaceName[i] = ' ';
3155 did_replace = TRUE;
3156 }
3157 if (!did_replace || init_logfont(lf) == FAIL)
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003158 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 }
3160
3161 while (*p == ':')
3162 p++;
3163
3164 /* Set the values found after ':' */
3165 while (*p)
3166 {
3167 switch (*p++)
3168 {
3169 case 'h':
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003170 lf->lfHeight = - points_to_pixels(p, &p, TRUE, (long_i)printer_dc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 break;
3172 case 'w':
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003173 lf->lfWidth = points_to_pixels(p, &p, FALSE, (long_i)printer_dc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 break;
3175 case 'b':
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 lf->lfWeight = FW_BOLD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 break;
3178 case 'i':
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 lf->lfItalic = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180 break;
3181 case 'u':
3182 lf->lfUnderline = TRUE;
3183 break;
3184 case 's':
3185 lf->lfStrikeOut = TRUE;
3186 break;
3187 case 'c':
3188 {
3189 struct charset_pair *cp;
3190
3191 for (cp = charset_pairs; cp->name != NULL; ++cp)
3192 if (STRNCMP(p, cp->name, strlen(cp->name)) == 0)
3193 {
3194 lf->lfCharSet = cp->charset;
3195 p += strlen(cp->name);
3196 break;
3197 }
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003198 if (cp->name == NULL && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003200 semsg(_("E244: Illegal charset name \"%s\" in font name \"%s\""), p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 break;
3202 }
3203 break;
3204 }
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003205 case 'q':
3206 {
3207 struct quality_pair *qp;
3208
3209 for (qp = quality_pairs; qp->name != NULL; ++qp)
3210 if (STRNCMP(p, qp->name, strlen(qp->name)) == 0)
3211 {
3212 lf->lfQuality = qp->quality;
3213 p += strlen(qp->name);
3214 break;
3215 }
3216 if (qp->name == NULL && verbose)
3217 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003218 semsg(_("E244: Illegal quality name \"%s\" in font name \"%s\""), p, name);
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003219 break;
3220 }
3221 break;
3222 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 default:
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003224 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003225 semsg(_("E245: Illegal char '%c' in font name \"%s\""), p[-1], name);
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003226 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 }
3228 while (*p == ':')
3229 p++;
3230 }
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003231 ret = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 /* ron: init lastlf */
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003235 if (ret == OK && printer_dc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 {
3237 vim_free(lastlf);
3238 lastlf = (LOGFONT *)alloc(sizeof(LOGFONT));
3239 if (lastlf != NULL)
3240 mch_memmove(lastlf, lf, sizeof(LOGFONT));
3241 }
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003242 vim_free(acpname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003244 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245}
3246
3247#endif /* defined(FEAT_GUI) || defined(FEAT_PRINTER) */
Bram Moolenaarf12d9832016-01-29 21:11:25 +01003248
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003249#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaarf12d9832016-01-29 21:11:25 +01003250/*
3251 * Initialize the Winsock dll.
3252 */
3253 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003254channel_init_winsock(void)
Bram Moolenaarf12d9832016-01-29 21:11:25 +01003255{
3256 WSADATA wsaData;
3257 int wsaerr;
3258
3259 if (WSInitialized)
3260 return;
3261
3262 wsaerr = WSAStartup(MAKEWORD(2, 2), &wsaData);
3263 if (wsaerr == 0)
3264 WSInitialized = TRUE;
3265}
3266#endif