blob: bd70e84b7399ba6c320e977c7d92c01bfff63550 [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
Bram Moolenaar4f974752019-02-17 17:44:42 +010035# if defined(FEAT_TITLE) && !defined(FEAT_GUI_MSWIN)
Bram Moolenaar82881492012-11-20 16:53:39 +010036# 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;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +010088typedef int ENUMLOGFONTW;
Bram Moolenaar071d4272004-06-13 20:20:40 +000089typedef int HANDLE;
90typedef int HDC;
91typedef int HFONT;
92typedef int HICON;
93typedef int HWND;
94typedef int INPUT_RECORD;
95typedef int KEY_EVENT_RECORD;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +010096typedef int LOGFONTW;
Bram Moolenaar071d4272004-06-13 20:20:40 +000097typedef 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;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100106typedef int NEWTEXTMETRICW;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107typedef 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
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +0100678#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679/*
680 * Display the saved error message(s).
681 */
682 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100683display_errors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684{
685 char *p;
686
687 if (error_ga.ga_data != NULL)
688 {
689 /* avoid putting up a message box with blanks only */
690 for (p = (char *)error_ga.ga_data; *p; ++p)
691 if (!isspace(*p))
692 {
Bram Moolenaarc17ef8e2006-03-25 21:48:58 +0000693 (void)gui_mch_dialog(
Bram Moolenaarc17ef8e2006-03-25 21:48:58 +0000694 gui.starting ? VIM_INFO :
Bram Moolenaarc17ef8e2006-03-25 21:48:58 +0000695 VIM_ERROR,
Bram Moolenaarc17ef8e2006-03-25 21:48:58 +0000696 gui.starting ? (char_u *)_("Message") :
Bram Moolenaarc17ef8e2006-03-25 21:48:58 +0000697 (char_u *)_("Error"),
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100698 (char_u *)p, (char_u *)_("&Ok"),
699 1, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 break;
701 }
702 ga_clear(&error_ga);
703 }
704}
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +0100705#else
706 void
707display_errors(void)
708{
709 FlushFileBuffers(GetStdHandle(STD_ERROR_HANDLE));
710}
711#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712#endif
713
714
715/*
716 * Return TRUE if "p" contain a wildcard that can be expanded by
717 * dos_expandpath().
718 */
719 int
720mch_has_exp_wildcard(char_u *p)
721{
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100722 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723 {
724 if (vim_strchr((char_u *)"?*[", *p) != NULL
725 || (*p == '~' && p[1] != NUL))
726 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727 }
728 return FALSE;
729}
730
731/*
732 * Return TRUE if "p" contain a wildcard or a "~1" kind of thing (could be a
733 * shortened file name).
734 */
735 int
736mch_has_wildcard(char_u *p)
737{
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100738 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 {
740 if (vim_strchr((char_u *)
741# ifdef VIM_BACKTICK
742 "?*$[`"
743# else
744 "?*$["
745# endif
746 , *p) != NULL
747 || (*p == '~' && p[1] != NUL))
748 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 }
750 return FALSE;
751}
752
753
754/*
755 * The normal _chdir() does not change the default drive. This one does.
756 * Returning 0 implies success; -1 implies failure.
757 */
758 int
759mch_chdir(char *path)
760{
761 if (path[0] == NUL) /* just checking... */
762 return -1;
763
Bram Moolenaara2974d72009-07-14 16:38:36 +0000764 if (p_verbose >= 5)
765 {
766 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100767 smsg("chdir(%s)", path);
Bram Moolenaara2974d72009-07-14 16:38:36 +0000768 verbose_leave();
769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 if (isalpha(path[0]) && path[1] == ':') /* has a drive name */
771 {
772 /* If we can change to the drive, skip that part of the path. If we
773 * can't then the current directory may be invalid, try using chdir()
774 * with the whole path. */
775 if (_chdrive(TOLOWER_ASC(path[0]) - 'a' + 1) == 0)
776 path += 2;
777 }
778
779 if (*path == NUL) /* drive name only */
780 return 0;
781
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000782 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
783 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100784 WCHAR *p = enc_to_utf16((char_u *)path, NULL);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000785 int n;
786
787 if (p != NULL)
788 {
789 n = _wchdir(p);
790 vim_free(p);
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200791 return n;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000792 }
793 }
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000794
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795 return chdir(path); /* let the normal chdir() do the rest */
796}
797
798
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799#ifdef FEAT_GUI_MSWIN
800/*
801 * return non-zero if a character is available
802 */
803 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100804mch_char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805{
806 /* never used */
807 return TRUE;
808}
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200809
810# if defined(FEAT_TERMINAL) || defined(PROTO)
811/*
812 * Check for any pending input or messages.
813 */
814 int
815mch_check_messages(void)
816{
817 /* TODO: check for messages */
818 return TRUE;
819}
820# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821#endif
822
823
824/*
825 * set screen mode, always fails.
826 */
827 int
Bram Moolenaar1266d672017-02-01 13:43:36 +0100828mch_screenmode(char_u *arg UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100830 emsg(_(e_screenmode));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 return FAIL;
832}
833
834
835#if defined(FEAT_LIBCALL) || defined(PROTO)
836/*
837 * Call a DLL routine which takes either a string or int param
838 * and returns an allocated string.
839 * Return OK if it worked, FAIL if not.
840 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841typedef LPTSTR (*MYSTRPROCSTR)(LPTSTR);
842typedef LPTSTR (*MYINTPROCSTR)(int);
843typedef int (*MYSTRPROCINT)(LPTSTR);
844typedef int (*MYINTPROCINT)(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846/*
847 * Check if a pointer points to a valid NUL terminated string.
848 * Return the length of the string, including terminating NUL.
849 * Returns 0 for an invalid pointer, 1 for an empty string.
850 */
851 static size_t
852check_str_len(char_u *str)
853{
854 SYSTEM_INFO si;
855 MEMORY_BASIC_INFORMATION mbi;
856 size_t length = 0;
857 size_t i;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100858 const char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859
860 /* get page size */
861 GetSystemInfo(&si);
862
863 /* get memory information */
864 if (VirtualQuery(str, &mbi, sizeof(mbi)))
865 {
866 /* pre cast these (typing savers) */
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000867 long_u dwStr = (long_u)str;
868 long_u dwBaseAddress = (long_u)mbi.BaseAddress;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869
870 /* get start address of page that str is on */
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000871 long_u strPage = dwStr - (dwStr - dwBaseAddress) % si.dwPageSize;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872
873 /* get length from str to end of page */
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000874 long_u pageLength = si.dwPageSize - (dwStr - strPage);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875
Bram Moolenaar442b4222010-05-24 21:34:22 +0200876 for (p = str; !IsBadReadPtr(p, (UINT)pageLength);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877 p += pageLength, pageLength = si.dwPageSize)
878 for (i = 0; i < pageLength; ++i, ++length)
879 if (p[i] == NUL)
880 return length + 1;
881 }
882
883 return 0;
884}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200886/*
887 * Passed to do_in_runtimepath() to load a vim.ico file.
888 */
889 static void
890mch_icon_load_cb(char_u *fname, void *cookie)
891{
892 HANDLE *h = (HANDLE *)cookie;
893
894 *h = LoadImage(NULL,
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100895 (LPSTR)fname,
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200896 IMAGE_ICON,
897 64,
898 64,
899 LR_LOADFROMFILE | LR_LOADMAP3DCOLORS);
900}
901
902/*
903 * Try loading an icon file from 'runtimepath'.
904 */
905 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100906mch_icon_load(HANDLE *iconp)
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200907{
908 return do_in_runtimepath((char_u *)"bitmaps/vim.ico",
Bram Moolenaar7f8989d2016-03-12 22:11:39 +0100909 0, mch_icon_load_cb, iconp);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200910}
911
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912 int
913mch_libcall(
914 char_u *libname,
915 char_u *funcname,
916 char_u *argstring, /* NULL when using a argint */
917 int argint,
918 char_u **string_result,/* NULL when using number_result */
919 int *number_result)
920{
921 HINSTANCE hinstLib;
922 MYSTRPROCSTR ProcAdd;
923 MYINTPROCSTR ProcAddI;
924 char_u *retval_str = NULL;
925 int retval_int = 0;
926 size_t len;
927
928 BOOL fRunTimeLinkSuccess = FALSE;
929
930 // Get a handle to the DLL module.
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100931 hinstLib = vimLoadLib((char *)libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932
933 // If the handle is valid, try to get the function address.
934 if (hinstLib != NULL)
935 {
936#ifdef HAVE_TRY_EXCEPT
937 __try
938 {
939#endif
940 if (argstring != NULL)
941 {
942 /* Call with string argument */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100943 ProcAdd = (MYSTRPROCSTR)GetProcAddress(hinstLib, (LPCSTR)funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 if ((fRunTimeLinkSuccess = (ProcAdd != NULL)) != 0)
945 {
946 if (string_result == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100947 retval_int = ((MYSTRPROCINT)ProcAdd)((LPSTR)argstring);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100949 retval_str = (char_u *)(ProcAdd)((LPSTR)argstring);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 }
951 }
952 else
953 {
954 /* Call with number argument */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100955 ProcAddI = (MYINTPROCSTR) GetProcAddress(hinstLib, (LPCSTR)funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 if ((fRunTimeLinkSuccess = (ProcAddI != NULL)) != 0)
957 {
958 if (string_result == NULL)
959 retval_int = ((MYINTPROCINT)ProcAddI)(argint);
960 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100961 retval_str = (char_u *)(ProcAddI)(argint);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 }
963 }
964
965 // Save the string before we free the library.
966 // Assume that a "1" result is an illegal pointer.
967 if (string_result == NULL)
968 *number_result = retval_int;
969 else if (retval_str != NULL
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100970 && (len = check_str_len(retval_str)) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 {
972 *string_result = lalloc((long_u)len, TRUE);
973 if (*string_result != NULL)
974 mch_memmove(*string_result, retval_str, len);
975 }
976
977#ifdef HAVE_TRY_EXCEPT
978 }
979 __except(EXCEPTION_EXECUTE_HANDLER)
980 {
981 if (GetExceptionCode() == EXCEPTION_STACK_OVERFLOW)
982 RESETSTKOFLW();
983 fRunTimeLinkSuccess = 0;
984 }
985#endif
986
987 // Free the DLL module.
988 (void)FreeLibrary(hinstLib);
989 }
990
991 if (!fRunTimeLinkSuccess)
992 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100993 semsg(_(e_libcall), funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 return FAIL;
995 }
996
997 return OK;
998}
999#endif
1000
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001/*
1002 * Debugging helper: expose the MCH_WRITE_DUMP stuff to other modules
1003 */
1004 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01001005DumpPutS(const char *psz UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006{
1007# ifdef MCH_WRITE_DUMP
1008 if (fdDump)
1009 {
1010 fputs(psz, fdDump);
1011 if (psz[strlen(psz) - 1] != '\n')
1012 fputc('\n', fdDump);
1013 fflush(fdDump);
1014 }
1015# endif
1016}
1017
1018#ifdef _DEBUG
1019
1020void __cdecl
1021Trace(
1022 char *pszFormat,
1023 ...)
1024{
1025 CHAR szBuff[2048];
1026 va_list args;
1027
1028 va_start(args, pszFormat);
1029 vsprintf(szBuff, pszFormat, args);
1030 va_end(args);
1031
1032 OutputDebugString(szBuff);
1033}
1034
1035#endif //_DEBUG
1036
Bram Moolenaar843ee412004-06-30 16:16:41 +00001037#if !defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001038# ifdef FEAT_TITLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039extern HWND g_hWnd; /* This is in os_win32.c. */
1040# endif
1041
1042/*
1043 * Showing the printer dialog is tricky since we have no GUI
1044 * window to parent it. The following routines are needed to
1045 * get the window parenting and Z-order to work properly.
1046 */
1047 static void
1048GetConsoleHwnd(void)
1049{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 /* Skip if it's already set. */
1051 if (s_hwnd != 0)
1052 return;
1053
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001054# ifdef FEAT_TITLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055 /* Window handle may have been found by init code (Windows NT only) */
1056 if (g_hWnd != 0)
1057 {
1058 s_hwnd = g_hWnd;
1059 return;
1060 }
1061# endif
1062
Bram Moolenaare1ed53f2019-02-12 23:12:37 +01001063 s_hwnd = GetConsoleWindow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064}
Bram Moolenaar843ee412004-06-30 16:16:41 +00001065
1066/*
1067 * Console implementation of ":winpos".
1068 */
1069 int
1070mch_get_winpos(int *x, int *y)
1071{
1072 RECT rect;
1073
1074 GetConsoleHwnd();
1075 GetWindowRect(s_hwnd, &rect);
1076 *x = rect.left;
1077 *y = rect.top;
1078 return OK;
1079}
1080
1081/*
1082 * Console implementation of ":winpos x y".
1083 */
1084 void
1085mch_set_winpos(int x, int y)
1086{
1087 GetConsoleHwnd();
1088 SetWindowPos(s_hwnd, NULL, x, y, 0, 0,
1089 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
1090}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091#endif
1092
1093#if (defined(FEAT_PRINTER) && !defined(FEAT_POSTSCRIPT)) || defined(PROTO)
1094
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095/*=================================================================
1096 * Win32 printer stuff
1097 */
1098
1099static HFONT prt_font_handles[2][2][2];
1100static PRINTDLG prt_dlg;
1101static const int boldface[2] = {FW_REGULAR, FW_BOLD};
1102static TEXTMETRIC prt_tm;
1103static int prt_line_height;
1104static int prt_number_width;
1105static int prt_left_margin;
1106static int prt_right_margin;
1107static int prt_top_margin;
1108static char_u szAppName[] = TEXT("VIM");
1109static HWND hDlgPrint;
1110static int *bUserAbort = NULL;
1111static char_u *prt_name = NULL;
1112
1113/* Defines which are also in vim.rc. */
1114#define IDC_BOX1 400
1115#define IDC_PRINTTEXT1 401
1116#define IDC_PRINTTEXT2 402
1117#define IDC_PROGRESS 403
1118
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001119 static BOOL
1120vimSetDlgItemText(HWND hDlg, int nIDDlgItem, char_u *s)
1121{
1122 WCHAR *wp = NULL;
1123 BOOL ret;
1124
1125 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001126 wp = enc_to_utf16(s, NULL);
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001127 if (wp != NULL)
1128 {
1129 ret = SetDlgItemTextW(hDlg, nIDDlgItem, wp);
1130 vim_free(wp);
1131 return ret;
1132 }
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001133 return SetDlgItemText(hDlg, nIDDlgItem, (LPCSTR)s);
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001134}
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001135
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136/*
1137 * Convert BGR to RGB for Windows GDI calls
1138 */
1139 static COLORREF
1140swap_me(COLORREF colorref)
1141{
1142 int temp;
1143 char *ptr = (char *)&colorref;
1144
1145 temp = *(ptr);
1146 *(ptr ) = *(ptr + 2);
1147 *(ptr + 2) = temp;
1148 return colorref;
1149}
1150
Bram Moolenaared39e1d2008-08-09 17:55:22 +00001151/* Attempt to make this work for old and new compilers */
Bram Moolenaar9f733d12011-09-21 20:09:42 +02001152#if !defined(_WIN64) && (!defined(_MSC_VER) || _MSC_VER < 1300)
Bram Moolenaared39e1d2008-08-09 17:55:22 +00001153# define PDP_RETVAL BOOL
1154#else
1155# define PDP_RETVAL INT_PTR
1156#endif
1157
Bram Moolenaared39e1d2008-08-09 17:55:22 +00001158 static PDP_RETVAL CALLBACK
Bram Moolenaar1266d672017-02-01 13:43:36 +01001159PrintDlgProc(
1160 HWND hDlg,
1161 UINT message,
1162 WPARAM wParam UNUSED,
1163 LPARAM lParam UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164{
1165#ifdef FEAT_GETTEXT
1166 NONCLIENTMETRICS nm;
1167 static HFONT hfont;
1168#endif
1169
1170 switch (message)
1171 {
1172 case WM_INITDIALOG:
1173#ifdef FEAT_GETTEXT
1174 nm.cbSize = sizeof(NONCLIENTMETRICS);
1175 if (SystemParametersInfo(
1176 SPI_GETNONCLIENTMETRICS,
1177 sizeof(NONCLIENTMETRICS),
1178 &nm,
1179 0))
1180 {
1181 char buff[MAX_PATH];
1182 int i;
1183
1184 /* Translate the dialog texts */
1185 hfont = CreateFontIndirect(&nm.lfMessageFont);
1186 for (i = IDC_PRINTTEXT1; i <= IDC_PROGRESS; i++)
1187 {
1188 SendDlgItemMessage(hDlg, i, WM_SETFONT, (WPARAM)hfont, 1);
1189 if (GetDlgItemText(hDlg,i, buff, sizeof(buff)))
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001190 vimSetDlgItemText(hDlg,i, (char_u *)_(buff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 }
1192 SendDlgItemMessage(hDlg, IDCANCEL,
1193 WM_SETFONT, (WPARAM)hfont, 1);
1194 if (GetDlgItemText(hDlg,IDCANCEL, buff, sizeof(buff)))
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001195 vimSetDlgItemText(hDlg,IDCANCEL, (char_u *)_(buff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 }
1197#endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001198 SetWindowText(hDlg, (LPCSTR)szAppName);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 if (prt_name != NULL)
1200 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001201 vimSetDlgItemText(hDlg, IDC_PRINTTEXT2, (char_u *)prt_name);
Bram Moolenaard23a8232018-02-10 18:45:26 +01001202 VIM_CLEAR(prt_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 }
1204 EnableMenuItem(GetSystemMenu(hDlg, FALSE), SC_CLOSE, MF_GRAYED);
1205#ifndef FEAT_GUI
1206 BringWindowToTop(s_hwnd);
1207#endif
1208 return TRUE;
1209
1210 case WM_COMMAND:
1211 *bUserAbort = TRUE;
1212 EnableWindow(GetParent(hDlg), TRUE);
1213 DestroyWindow(hDlg);
1214 hDlgPrint = NULL;
1215#ifdef FEAT_GETTEXT
1216 DeleteObject(hfont);
1217#endif
1218 return TRUE;
1219 }
1220 return FALSE;
1221}
1222
1223 static BOOL CALLBACK
Bram Moolenaar1266d672017-02-01 13:43:36 +01001224AbortProc(HDC hdcPrn UNUSED, int iCode UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225{
1226 MSG msg;
1227
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02001228 while (!*bUserAbort && pPeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 {
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02001230 if (!hDlgPrint || !pIsDialogMessage(hDlgPrint, &msg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 {
1232 TranslateMessage(&msg);
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02001233 pDispatchMessage(&msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234 }
1235 }
1236 return !*bUserAbort;
1237}
1238
1239#ifndef FEAT_GUI
1240
Bram Moolenaar26fdd7d2011-11-30 13:42:44 +01001241 static UINT_PTR CALLBACK
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242PrintHookProc(
1243 HWND hDlg, // handle to dialog box
1244 UINT uiMsg, // message identifier
1245 WPARAM wParam, // message parameter
1246 LPARAM lParam // message parameter
1247 )
1248{
1249 HWND hwndOwner;
1250 RECT rc, rcDlg, rcOwner;
1251 PRINTDLG *pPD;
1252
1253 if (uiMsg == WM_INITDIALOG)
1254 {
1255 // Get the owner window and dialog box rectangles.
1256 if ((hwndOwner = GetParent(hDlg)) == NULL)
1257 hwndOwner = GetDesktopWindow();
1258
1259 GetWindowRect(hwndOwner, &rcOwner);
1260 GetWindowRect(hDlg, &rcDlg);
1261 CopyRect(&rc, &rcOwner);
1262
1263 // Offset the owner and dialog box rectangles so that
1264 // right and bottom values represent the width and
1265 // height, and then offset the owner again to discard
1266 // space taken up by the dialog box.
1267
1268 OffsetRect(&rcDlg, -rcDlg.left, -rcDlg.top);
1269 OffsetRect(&rc, -rc.left, -rc.top);
1270 OffsetRect(&rc, -rcDlg.right, -rcDlg.bottom);
1271
1272 // The new position is the sum of half the remaining
1273 // space and the owner's original position.
1274
1275 SetWindowPos(hDlg,
1276 HWND_TOP,
1277 rcOwner.left + (rc.right / 2),
1278 rcOwner.top + (rc.bottom / 2),
1279 0, 0, // ignores size arguments
1280 SWP_NOSIZE);
1281
1282 /* tackle the printdlg copiesctrl problem */
1283 pPD = (PRINTDLG *)lParam;
1284 pPD->nCopies = (WORD)pPD->lCustData;
1285 SetDlgItemInt( hDlg, edt3, pPD->nCopies, FALSE );
1286 /* Bring the window to top */
1287 BringWindowToTop(GetParent(hDlg));
1288 SetForegroundWindow(hDlg);
1289 }
1290
1291 return FALSE;
1292}
1293#endif
1294
1295 void
1296mch_print_cleanup(void)
1297{
1298 int pifItalic;
1299 int pifBold;
1300 int pifUnderline;
1301
1302 for (pifBold = 0; pifBold <= 1; pifBold++)
1303 for (pifItalic = 0; pifItalic <= 1; pifItalic++)
1304 for (pifUnderline = 0; pifUnderline <= 1; pifUnderline++)
1305 DeleteObject(prt_font_handles[pifBold][pifItalic][pifUnderline]);
1306
1307 if (prt_dlg.hDC != NULL)
1308 DeleteDC(prt_dlg.hDC);
1309 if (!*bUserAbort)
1310 SendMessage(hDlgPrint, WM_COMMAND, 0, 0);
1311}
1312
1313 static int
1314to_device_units(int idx, int dpi, int physsize, int offset, int def_number)
1315{
1316 int ret = 0;
1317 int u;
1318 int nr;
1319
1320 u = prt_get_unit(idx);
1321 if (u == PRT_UNIT_NONE)
1322 {
1323 u = PRT_UNIT_PERC;
1324 nr = def_number;
1325 }
1326 else
1327 nr = printer_opts[idx].number;
1328
1329 switch (u)
1330 {
1331 case PRT_UNIT_PERC:
1332 ret = (physsize * nr) / 100;
1333 break;
1334 case PRT_UNIT_INCH:
1335 ret = (nr * dpi);
1336 break;
1337 case PRT_UNIT_MM:
1338 ret = (nr * 10 * dpi) / 254;
1339 break;
1340 case PRT_UNIT_POINT:
1341 ret = (nr * 10 * dpi) / 720;
1342 break;
1343 }
1344
1345 if (ret < offset)
1346 return 0;
1347 else
1348 return ret - offset;
1349}
1350
1351 static int
1352prt_get_cpl(void)
1353{
1354 int hr;
1355 int phyw;
1356 int dvoff;
1357 int rev_offset;
1358 int dpi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359
1360 GetTextMetrics(prt_dlg.hDC, &prt_tm);
1361 prt_line_height = prt_tm.tmHeight + prt_tm.tmExternalLeading;
1362
1363 hr = GetDeviceCaps(prt_dlg.hDC, HORZRES);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 phyw = GetDeviceCaps(prt_dlg.hDC, PHYSICALWIDTH);
1365 dvoff = GetDeviceCaps(prt_dlg.hDC, PHYSICALOFFSETX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 dpi = GetDeviceCaps(prt_dlg.hDC, LOGPIXELSX);
1367
1368 rev_offset = phyw - (dvoff + hr);
1369
1370 prt_left_margin = to_device_units(OPT_PRINT_LEFT, dpi, phyw, dvoff, 10);
1371 if (prt_use_number())
1372 {
1373 prt_number_width = PRINT_NUMBER_WIDTH * prt_tm.tmAveCharWidth;
1374 prt_left_margin += prt_number_width;
1375 }
1376 else
1377 prt_number_width = 0;
1378
1379 prt_right_margin = hr - to_device_units(OPT_PRINT_RIGHT, dpi, phyw,
1380 rev_offset, 5);
1381
1382 return (prt_right_margin - prt_left_margin) / prt_tm.tmAveCharWidth;
1383}
1384
1385 static int
1386prt_get_lpp(void)
1387{
1388 int vr;
1389 int phyw;
1390 int dvoff;
1391 int rev_offset;
1392 int bottom_margin;
1393 int dpi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394
1395 vr = GetDeviceCaps(prt_dlg.hDC, VERTRES);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 phyw = GetDeviceCaps(prt_dlg.hDC, PHYSICALHEIGHT);
1397 dvoff = GetDeviceCaps(prt_dlg.hDC, PHYSICALOFFSETY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 dpi = GetDeviceCaps(prt_dlg.hDC, LOGPIXELSY);
1399
1400 rev_offset = phyw - (dvoff + vr);
1401
1402 prt_top_margin = to_device_units(OPT_PRINT_TOP, dpi, phyw, dvoff, 5);
1403
1404 /* adjust top margin if there is a header */
1405 prt_top_margin += prt_line_height * prt_header_height();
1406
1407 bottom_margin = vr - to_device_units(OPT_PRINT_BOT, dpi, phyw,
1408 rev_offset, 5);
1409
1410 return (bottom_margin - prt_top_margin) / prt_line_height;
1411}
1412
1413 int
1414mch_print_init(prt_settings_T *psettings, char_u *jobname, int forceit)
1415{
1416 static HGLOBAL stored_dm = NULL;
1417 static HGLOBAL stored_devn = NULL;
1418 static int stored_nCopies = 1;
1419 static int stored_nFlags = 0;
1420
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001421 LOGFONTW fLogFont;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 int pifItalic;
1423 int pifBold;
1424 int pifUnderline;
1425
1426 DEVMODE *mem;
1427 DEVNAMES *devname;
1428 int i;
1429
1430 bUserAbort = &(psettings->user_abort);
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02001431 vim_memset(&prt_dlg, 0, sizeof(PRINTDLG));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 prt_dlg.lStructSize = sizeof(PRINTDLG);
1433#ifndef FEAT_GUI
1434 GetConsoleHwnd(); /* get value of s_hwnd */
1435#endif
1436 prt_dlg.hwndOwner = s_hwnd;
1437 prt_dlg.Flags = PD_NOPAGENUMS | PD_NOSELECTION | PD_RETURNDC;
1438 if (!forceit)
1439 {
1440 prt_dlg.hDevMode = stored_dm;
1441 prt_dlg.hDevNames = stored_devn;
1442 prt_dlg.lCustData = stored_nCopies; // work around bug in print dialog
1443#ifndef FEAT_GUI
1444 /*
1445 * Use hook to prevent console window being sent to back
1446 */
1447 prt_dlg.lpfnPrintHook = PrintHookProc;
1448 prt_dlg.Flags |= PD_ENABLEPRINTHOOK;
1449#endif
1450 prt_dlg.Flags |= stored_nFlags;
1451 }
1452
1453 /*
1454 * If bang present, return default printer setup with no dialog
1455 * never show dialog if we are running over telnet
1456 */
1457 if (forceit
1458#ifndef FEAT_GUI
1459 || !term_console
1460#endif
1461 )
1462 {
1463 prt_dlg.Flags |= PD_RETURNDEFAULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 /*
1465 * MSDN suggests setting the first parameter to WINSPOOL for
1466 * NT, but NULL appears to work just as well.
1467 */
1468 if (*p_pdev != NUL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001469 prt_dlg.hDC = CreateDC(NULL, (LPCSTR)p_pdev, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 {
1472 prt_dlg.Flags |= PD_RETURNDEFAULT;
1473 if (PrintDlg(&prt_dlg) == 0)
1474 goto init_fail_dlg;
1475 }
1476 }
1477 else if (PrintDlg(&prt_dlg) == 0)
1478 goto init_fail_dlg;
1479 else
1480 {
1481 /*
1482 * keep the previous driver context
1483 */
1484 stored_dm = prt_dlg.hDevMode;
1485 stored_devn = prt_dlg.hDevNames;
1486 stored_nFlags = prt_dlg.Flags;
1487 stored_nCopies = prt_dlg.nCopies;
1488 }
1489
1490 if (prt_dlg.hDC == NULL)
1491 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001492 emsg(_("E237: Printer selection failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 mch_print_cleanup();
1494 return FALSE;
1495 }
1496
1497 /* Not all printer drivers report the support of color (or grey) in the
1498 * same way. Let's set has_color if there appears to be some way to print
1499 * more than B&W. */
1500 i = GetDeviceCaps(prt_dlg.hDC, NUMCOLORS);
1501 psettings->has_color = (GetDeviceCaps(prt_dlg.hDC, BITSPIXEL) > 1
1502 || GetDeviceCaps(prt_dlg.hDC, PLANES) > 1
1503 || i > 2 || i == -1);
1504
1505 /* Ensure all font styles are baseline aligned */
1506 SetTextAlign(prt_dlg.hDC, TA_BASELINE|TA_LEFT);
1507
1508 /*
1509 * On some windows systems the nCopies parameter is not
1510 * passed back correctly. It must be retrieved from the
1511 * hDevMode struct.
1512 */
1513 mem = (DEVMODE *)GlobalLock(prt_dlg.hDevMode);
1514 if (mem != NULL)
1515 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 if (mem->dmCopies != 1)
1517 stored_nCopies = mem->dmCopies;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 if ((mem->dmFields & DM_DUPLEX) && (mem->dmDuplex & ~DMDUP_SIMPLEX))
1519 psettings->duplex = TRUE;
1520 if ((mem->dmFields & DM_COLOR) && (mem->dmColor & DMCOLOR_COLOR))
1521 psettings->has_color = TRUE;
1522 }
1523 GlobalUnlock(prt_dlg.hDevMode);
1524
1525 devname = (DEVNAMES *)GlobalLock(prt_dlg.hDevNames);
1526 if (devname != 0)
1527 {
1528 char_u *printer_name = (char_u *)devname + devname->wDeviceOffset;
1529 char_u *port_name = (char_u *)devname +devname->wOutputOffset;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001530 char_u *text = (char_u *)_("to %s on %s");
Bram Moolenaarf191d552014-10-09 17:05:56 +02001531 char_u *printer_name_orig = printer_name;
1532 char_u *port_name_orig = port_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533
Bram Moolenaarf191d552014-10-09 17:05:56 +02001534 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
1535 {
1536 char_u *to_free = NULL;
1537 int maxlen;
1538
Bram Moolenaar861d80a2014-10-16 20:35:36 +02001539 acp_to_enc(printer_name, (int)STRLEN(printer_name), &to_free,
1540 &maxlen);
Bram Moolenaarf191d552014-10-09 17:05:56 +02001541 if (to_free != NULL)
1542 printer_name = to_free;
Bram Moolenaar861d80a2014-10-16 20:35:36 +02001543 acp_to_enc(port_name, (int)STRLEN(port_name), &to_free, &maxlen);
Bram Moolenaarf191d552014-10-09 17:05:56 +02001544 if (to_free != NULL)
1545 port_name = to_free;
1546 }
Bram Moolenaare6a91fd2008-07-24 18:51:11 +00001547 prt_name = alloc((unsigned)(STRLEN(printer_name) + STRLEN(port_name)
1548 + STRLEN(text)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 if (prt_name != NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001550 wsprintf((char *)prt_name, (const char *)text,
1551 printer_name, port_name);
Bram Moolenaarf191d552014-10-09 17:05:56 +02001552 if (printer_name != printer_name_orig)
1553 vim_free(printer_name);
1554 if (port_name != port_name_orig)
1555 vim_free(port_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 }
1557 GlobalUnlock(prt_dlg.hDevNames);
1558
1559 /*
1560 * Initialise the font according to 'printfont'
1561 */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02001562 vim_memset(&fLogFont, 0, sizeof(fLogFont));
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001563 if (get_logfont(&fLogFont, p_pfn, prt_dlg.hDC, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001565 semsg(_("E613: Unknown printer font: %s"), p_pfn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 mch_print_cleanup();
1567 return FALSE;
1568 }
1569
1570 for (pifBold = 0; pifBold <= 1; pifBold++)
1571 for (pifItalic = 0; pifItalic <= 1; pifItalic++)
1572 for (pifUnderline = 0; pifUnderline <= 1; pifUnderline++)
1573 {
1574 fLogFont.lfWeight = boldface[pifBold];
1575 fLogFont.lfItalic = pifItalic;
1576 fLogFont.lfUnderline = pifUnderline;
1577 prt_font_handles[pifBold][pifItalic][pifUnderline]
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001578 = CreateFontIndirectW(&fLogFont);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 }
1580
1581 SetBkMode(prt_dlg.hDC, OPAQUE);
1582 SelectObject(prt_dlg.hDC, prt_font_handles[0][0][0]);
1583
1584 /*
1585 * Fill in the settings struct
1586 */
1587 psettings->chars_per_line = prt_get_cpl();
1588 psettings->lines_per_page = prt_get_lpp();
Bram Moolenaar7ddc6422014-09-27 11:18:19 +02001589 if (prt_dlg.Flags & PD_USEDEVMODECOPIESANDCOLLATE)
1590 {
1591 psettings->n_collated_copies = (prt_dlg.Flags & PD_COLLATE)
1592 ? prt_dlg.nCopies : 1;
1593 psettings->n_uncollated_copies = (prt_dlg.Flags & PD_COLLATE)
1594 ? 1 : prt_dlg.nCopies;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595
Bram Moolenaar7ddc6422014-09-27 11:18:19 +02001596 if (psettings->n_collated_copies == 0)
1597 psettings->n_collated_copies = 1;
1598
1599 if (psettings->n_uncollated_copies == 0)
1600 psettings->n_uncollated_copies = 1;
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01001601 }
1602 else
1603 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 psettings->n_collated_copies = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 psettings->n_uncollated_copies = 1;
Bram Moolenaar7ddc6422014-09-27 11:18:19 +02001606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607
1608 psettings->jobname = jobname;
1609
1610 return TRUE;
1611
1612init_fail_dlg:
1613 {
1614 DWORD err = CommDlgExtendedError();
1615
1616 if (err)
1617 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 char_u *buf;
1619
1620 /* I suspect FormatMessage() doesn't work for values returned by
1621 * CommDlgExtendedError(). What does? */
1622 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
1623 FORMAT_MESSAGE_FROM_SYSTEM |
1624 FORMAT_MESSAGE_IGNORE_INSERTS,
1625 NULL, err, 0, (LPTSTR)(&buf), 0, NULL);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001626 semsg(_("E238: Print error: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 buf == NULL ? (char_u *)_("Unknown") : buf);
1628 LocalFree((LPVOID)(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 }
1630 else
1631 msg_clr_eos(); /* Maybe canceled */
1632
1633 mch_print_cleanup();
1634 return FALSE;
1635 }
1636}
1637
1638
1639 int
1640mch_print_begin(prt_settings_T *psettings)
1641{
1642 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 char szBuffer[300];
Bram Moolenaar2290b1f2018-05-13 17:30:45 +02001644 WCHAR *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645
1646 hDlgPrint = CreateDialog(GetModuleHandle(NULL), TEXT("PrintDlgBox"),
1647 prt_dlg.hwndOwner, PrintDlgProc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 SetAbortProc(prt_dlg.hDC, AbortProc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 wsprintf(szBuffer, _("Printing '%s'"), gettail(psettings->jobname));
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001650 vimSetDlgItemText(hDlgPrint, IDC_PRINTTEXT1, (char_u *)szBuffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651
Bram Moolenaar2290b1f2018-05-13 17:30:45 +02001652 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
1653 wp = enc_to_utf16(psettings->jobname, NULL);
1654 if (wp != NULL)
1655 {
1656 DOCINFOW di;
1657
1658 vim_memset(&di, 0, sizeof(di));
1659 di.cbSize = sizeof(di);
1660 di.lpszDocName = wp;
1661 ret = StartDocW(prt_dlg.hDC, &di);
1662 vim_free(wp);
1663 }
1664 else
Bram Moolenaar2290b1f2018-05-13 17:30:45 +02001665 {
1666 DOCINFO di;
1667
1668 vim_memset(&di, 0, sizeof(di));
1669 di.cbSize = sizeof(di);
1670 di.lpszDocName = (LPCSTR)psettings->jobname;
1671 ret = StartDoc(prt_dlg.hDC, &di);
1672 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673
1674#ifdef FEAT_GUI
1675 /* Give focus back to main window (when using MDI). */
1676 SetFocus(s_hwnd);
1677#endif
1678
1679 return (ret > 0);
1680}
1681
1682 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01001683mch_print_end(prt_settings_T *psettings UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684{
1685 EndDoc(prt_dlg.hDC);
1686 if (!*bUserAbort)
1687 SendMessage(hDlgPrint, WM_COMMAND, 0, 0);
1688}
1689
1690 int
1691mch_print_end_page(void)
1692{
1693 return (EndPage(prt_dlg.hDC) > 0);
1694}
1695
1696 int
1697mch_print_begin_page(char_u *msg)
1698{
1699 if (msg != NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001700 vimSetDlgItemText(hDlgPrint, IDC_PROGRESS, msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 return (StartPage(prt_dlg.hDC) > 0);
1702}
1703
1704 int
1705mch_print_blank_page(void)
1706{
1707 return (mch_print_begin_page(NULL) ? (mch_print_end_page()) : FALSE);
1708}
1709
1710static int prt_pos_x = 0;
1711static int prt_pos_y = 0;
1712
1713 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001714mch_print_start_line(int margin, int page_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715{
1716 if (margin)
1717 prt_pos_x = -prt_number_width;
1718 else
1719 prt_pos_x = 0;
1720 prt_pos_y = page_line * prt_line_height
1721 + prt_tm.tmAscent + prt_tm.tmExternalLeading;
1722}
1723
1724 int
1725mch_print_text_out(char_u *p, int len)
1726{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 SIZE sz;
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001728 WCHAR *wp = NULL;
1729 int wlen = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001731 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001732 wp = enc_to_utf16(p, &wlen);
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001733 if (wp != NULL)
1734 {
1735 int ret = FALSE;
1736
1737 TextOutW(prt_dlg.hDC, prt_pos_x + prt_left_margin,
1738 prt_pos_y + prt_top_margin, wp, wlen);
1739 GetTextExtentPoint32W(prt_dlg.hDC, wp, wlen, &sz);
1740 vim_free(wp);
1741 prt_pos_x += (sz.cx - prt_tm.tmOverhang);
1742 /* This is wrong when printing spaces for a TAB. */
1743 if (p[len] != NUL)
1744 {
1745 wlen = MB_PTR2LEN(p + len);
1746 wp = enc_to_utf16(p + len, &wlen);
1747 if (wp != NULL)
1748 {
1749 GetTextExtentPoint32W(prt_dlg.hDC, wp, 1, &sz);
1750 ret = (prt_pos_x + prt_left_margin + sz.cx > prt_right_margin);
1751 vim_free(wp);
1752 }
1753 }
1754 return ret;
1755 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 TextOut(prt_dlg.hDC, prt_pos_x + prt_left_margin,
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001757 prt_pos_y + prt_top_margin,
1758 (LPCSTR)p, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759#ifndef FEAT_PROPORTIONAL_FONTS
1760 prt_pos_x += len * prt_tm.tmAveCharWidth;
1761 return (prt_pos_x + prt_left_margin + prt_tm.tmAveCharWidth
1762 + prt_tm.tmOverhang > prt_right_margin);
1763#else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001764 GetTextExtentPoint32(prt_dlg.hDC, (LPCSTR)p, len, &sz);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 prt_pos_x += (sz.cx - prt_tm.tmOverhang);
1766 /* This is wrong when printing spaces for a TAB. */
1767 if (p[len] == NUL)
1768 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 GetTextExtentPoint32(prt_dlg.hDC, p + len, 1, &sz);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 return (prt_pos_x + prt_left_margin + sz.cx > prt_right_margin);
1771#endif
1772}
1773
1774 void
1775mch_print_set_font(int iBold, int iItalic, int iUnderline)
1776{
1777 SelectObject(prt_dlg.hDC, prt_font_handles[iBold][iItalic][iUnderline]);
1778}
1779
1780 void
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001781mch_print_set_bg(long_u bgcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782{
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001783 SetBkColor(prt_dlg.hDC, GetNearestColor(prt_dlg.hDC,
1784 swap_me((COLORREF)bgcol)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 /*
1786 * With a white background we can draw characters transparent, which is
1787 * good for italic characters that overlap to the next char cell.
1788 */
1789 if (bgcol == 0xffffffUL)
1790 SetBkMode(prt_dlg.hDC, TRANSPARENT);
1791 else
1792 SetBkMode(prt_dlg.hDC, OPAQUE);
1793}
1794
1795 void
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001796mch_print_set_fg(long_u fgcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797{
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001798 SetTextColor(prt_dlg.hDC, GetNearestColor(prt_dlg.hDC,
1799 swap_me((COLORREF)fgcol)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800}
1801
1802#endif /*FEAT_PRINTER && !FEAT_POSTSCRIPT*/
1803
Bram Moolenaar58d98232005-07-23 22:25:46 +00001804
1805
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806#if defined(FEAT_SHORTCUT) || defined(PROTO)
Bram Moolenaar82881492012-11-20 16:53:39 +01001807# ifndef PROTO
1808# include <shlobj.h>
1809# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810
Bram Moolenaardce1e892019-02-10 23:18:53 +01001811typedef enum _FILE_INFO_BY_HANDLE_CLASS_ {
1812 FileBasicInfo_,
1813 FileStandardInfo_,
1814 FileNameInfo_,
1815 FileRenameInfo_,
1816 FileDispositionInfo_,
1817 FileAllocationInfo_,
1818 FileEndOfFileInfo_,
1819 FileStreamInfo_,
1820 FileCompressionInfo_,
1821 FileAttributeTagInfo_,
1822 FileIdBothDirectoryInfo_,
1823 FileIdBothDirectoryRestartInfo_,
1824 FileIoPriorityHintInfo_,
1825 FileRemoteProtocolInfo_,
1826 FileFullDirectoryInfo_,
1827 FileFullDirectoryRestartInfo_,
1828 FileStorageInfo_,
1829 FileAlignmentInfo_,
1830 FileIdInfo_,
1831 FileIdExtdDirectoryInfo_,
1832 FileIdExtdDirectoryRestartInfo_,
1833 FileDispositionInfoEx_,
1834 FileRenameInfoEx_,
1835 MaximumFileInfoByHandleClass_
1836} FILE_INFO_BY_HANDLE_CLASS_;
1837
1838typedef struct _FILE_NAME_INFO_ {
1839 DWORD FileNameLength;
1840 WCHAR FileName[1];
1841} FILE_NAME_INFO_;
1842
1843typedef BOOL (WINAPI *pfnGetFileInformationByHandleEx)(
1844 HANDLE hFile,
1845 FILE_INFO_BY_HANDLE_CLASS_ FileInformationClass,
1846 LPVOID lpFileInformation,
1847 DWORD dwBufferSize);
1848static pfnGetFileInformationByHandleEx pGetFileInformationByHandleEx = NULL;
1849
1850typedef BOOL (WINAPI *pfnGetVolumeInformationByHandleW)(
1851 HANDLE hFile,
1852 LPWSTR lpVolumeNameBuffer,
1853 DWORD nVolumeNameSize,
1854 LPDWORD lpVolumeSerialNumber,
1855 LPDWORD lpMaximumComponentLength,
1856 LPDWORD lpFileSystemFlags,
1857 LPWSTR lpFileSystemNameBuffer,
1858 DWORD nFileSystemNameSize);
1859static pfnGetVolumeInformationByHandleW pGetVolumeInformationByHandleW = NULL;
1860
1861 char_u *
1862resolve_reparse_point(char_u *fname)
1863{
1864 HANDLE h = INVALID_HANDLE_VALUE;
1865 DWORD size;
1866 char_u *rfname = NULL;
1867 FILE_NAME_INFO_ *nameinfo = NULL;
1868 WCHAR buff[MAX_PATH], *volnames = NULL;
1869 HANDLE hv;
1870 DWORD snfile, snfind;
1871 static BOOL loaded = FALSE;
1872
1873 if (pGetFileInformationByHandleEx == NULL ||
1874 pGetVolumeInformationByHandleW == NULL)
1875 {
1876 HMODULE hmod = GetModuleHandle("kernel32.dll");
1877
1878 if (loaded == TRUE)
1879 return NULL;
1880 pGetFileInformationByHandleEx = (pfnGetFileInformationByHandleEx)
1881 GetProcAddress(hmod, "GetFileInformationByHandleEx");
1882 pGetVolumeInformationByHandleW = (pfnGetVolumeInformationByHandleW)
1883 GetProcAddress(hmod, "GetVolumeInformationByHandleW");
1884 loaded = TRUE;
1885 if (pGetFileInformationByHandleEx == NULL ||
1886 pGetVolumeInformationByHandleW == NULL)
1887 return NULL;
1888 }
1889
1890 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
1891 {
1892 WCHAR *p;
1893
1894 p = enc_to_utf16(fname, NULL);
1895 if (p == NULL)
1896 goto fail;
1897
1898 if ((GetFileAttributesW(p) & FILE_ATTRIBUTE_REPARSE_POINT) == 0)
1899 {
1900 vim_free(p);
1901 goto fail;
1902 }
1903
1904 h = CreateFileW(p, 0, 0, NULL, OPEN_EXISTING,
1905 FILE_FLAG_BACKUP_SEMANTICS, NULL);
1906 vim_free(p);
1907 }
1908 else
1909 {
1910 if ((GetFileAttributes((char*) fname) &
1911 FILE_ATTRIBUTE_REPARSE_POINT) == 0)
1912 goto fail;
1913
1914 h = CreateFile((char*) fname, 0, 0, NULL, OPEN_EXISTING,
1915 FILE_FLAG_BACKUP_SEMANTICS, NULL);
1916 }
1917
1918 if (h == INVALID_HANDLE_VALUE)
1919 goto fail;
1920
1921 size = sizeof(FILE_NAME_INFO_) + sizeof(WCHAR) * (MAX_PATH - 1);
1922 nameinfo = (FILE_NAME_INFO_*)alloc(size + sizeof(WCHAR));
1923 if (nameinfo == NULL)
1924 goto fail;
1925
1926 if (!pGetFileInformationByHandleEx(h, FileNameInfo_, nameinfo, size))
1927 goto fail;
1928
1929 nameinfo->FileName[nameinfo->FileNameLength / sizeof(WCHAR)] = 0;
1930
1931 if (!pGetVolumeInformationByHandleW(
1932 h, NULL, 0, &snfile, NULL, NULL, NULL, 0))
1933 goto fail;
1934
1935 hv = FindFirstVolumeW(buff, MAX_PATH);
1936 if (hv == INVALID_HANDLE_VALUE)
1937 goto fail;
1938
1939 do {
1940 GetVolumeInformationW(
1941 buff, NULL, 0, &snfind, NULL, NULL, NULL, 0);
1942 if (snfind == snfile)
1943 break;
1944 } while (FindNextVolumeW(hv, buff, MAX_PATH));
1945
1946 FindVolumeClose(hv);
1947
1948 if (snfind != snfile)
1949 goto fail;
1950
1951 size = 0;
1952 if (!GetVolumePathNamesForVolumeNameW(buff, NULL, 0, &size) &&
1953 GetLastError() != ERROR_MORE_DATA)
1954 goto fail;
1955
1956 volnames = (WCHAR*)alloc(size * sizeof(WCHAR));
1957 if (!GetVolumePathNamesForVolumeNameW(buff, volnames, size,
1958 &size))
1959 goto fail;
1960
1961 wcscpy(buff, volnames);
1962 if (nameinfo->FileName[0] == '\\')
1963 wcscat(buff, nameinfo->FileName + 1);
1964 else
1965 wcscat(buff, nameinfo->FileName);
1966 rfname = utf16_to_enc(buff, NULL);
1967
1968fail:
1969 if (h != INVALID_HANDLE_VALUE)
1970 CloseHandle(h);
1971 if (nameinfo != NULL)
1972 vim_free(nameinfo);
1973 if (volnames != NULL)
1974 vim_free(volnames);
1975
1976 return rfname;
1977}
1978
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979/*
1980 * When "fname" is the name of a shortcut (*.lnk) resolve the file it points
1981 * to and return that name in allocated memory.
1982 * Otherwise NULL is returned.
1983 */
Bram Moolenaardce1e892019-02-10 23:18:53 +01001984 static char_u *
1985resolve_shortcut(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986{
1987 HRESULT hr;
1988 IShellLink *psl = NULL;
1989 IPersistFile *ppf = NULL;
1990 OLECHAR wsz[MAX_PATH];
1991 WIN32_FIND_DATA ffd; // we get those free of charge
Bram Moolenaar604729e2013-08-30 16:44:19 +02001992 CHAR buf[MAX_PATH]; // could have simply reused 'wsz'...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 char_u *rfname = NULL;
1994 int len;
Bram Moolenaar604729e2013-08-30 16:44:19 +02001995 IShellLinkW *pslw = NULL;
1996 WIN32_FIND_DATAW ffdw; // we get those free of charge
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997
1998 /* Check if the file name ends in ".lnk". Avoid calling
1999 * CoCreateInstance(), it's quite slow. */
2000 if (fname == NULL)
2001 return rfname;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002002 len = (int)STRLEN(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 if (len <= 4 || STRNICMP(fname + len - 4, ".lnk", 4) != 0)
2004 return rfname;
2005
2006 CoInitialize(NULL);
2007
Bram Moolenaar604729e2013-08-30 16:44:19 +02002008 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
2009 {
2010 // create a link manager object and request its interface
2011 hr = CoCreateInstance(
2012 &CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
2013 &IID_IShellLinkW, (void**)&pslw);
2014 if (hr == S_OK)
2015 {
2016 WCHAR *p = enc_to_utf16(fname, NULL);
2017
2018 if (p != NULL)
2019 {
2020 // Get a pointer to the IPersistFile interface.
2021 hr = pslw->lpVtbl->QueryInterface(
2022 pslw, &IID_IPersistFile, (void**)&ppf);
2023 if (hr != S_OK)
2024 goto shortcut_errorw;
2025
2026 // "load" the name and resolve the link
2027 hr = ppf->lpVtbl->Load(ppf, p, STGM_READ);
2028 if (hr != S_OK)
2029 goto shortcut_errorw;
2030# if 0 // This makes Vim wait a long time if the target does not exist.
2031 hr = pslw->lpVtbl->Resolve(pslw, NULL, SLR_NO_UI);
2032 if (hr != S_OK)
2033 goto shortcut_errorw;
2034# endif
2035
2036 // Get the path to the link target.
2037 ZeroMemory(wsz, MAX_PATH * sizeof(WCHAR));
2038 hr = pslw->lpVtbl->GetPath(pslw, wsz, MAX_PATH, &ffdw, 0);
2039 if (hr == S_OK && wsz[0] != NUL)
2040 rfname = utf16_to_enc(wsz, NULL);
2041
2042shortcut_errorw:
2043 vim_free(p);
Bram Moolenaarcd981f22014-02-11 17:06:00 +01002044 goto shortcut_end;
Bram Moolenaar604729e2013-08-30 16:44:19 +02002045 }
2046 }
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002047 goto shortcut_end;
Bram Moolenaar604729e2013-08-30 16:44:19 +02002048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 // create a link manager object and request its interface
2050 hr = CoCreateInstance(
2051 &CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
2052 &IID_IShellLink, (void**)&psl);
2053 if (hr != S_OK)
Bram Moolenaar604729e2013-08-30 16:44:19 +02002054 goto shortcut_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055
2056 // Get a pointer to the IPersistFile interface.
2057 hr = psl->lpVtbl->QueryInterface(
2058 psl, &IID_IPersistFile, (void**)&ppf);
2059 if (hr != S_OK)
Bram Moolenaar604729e2013-08-30 16:44:19 +02002060 goto shortcut_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061
2062 // full path string must be in Unicode.
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002063 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)fname, -1, wsz, MAX_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00002065 // "load" the name and resolve the link
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 hr = ppf->lpVtbl->Load(ppf, wsz, STGM_READ);
2067 if (hr != S_OK)
Bram Moolenaar604729e2013-08-30 16:44:19 +02002068 goto shortcut_end;
2069# if 0 // This makes Vim wait a long time if the target doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 hr = psl->lpVtbl->Resolve(psl, NULL, SLR_NO_UI);
2071 if (hr != S_OK)
Bram Moolenaar604729e2013-08-30 16:44:19 +02002072 goto shortcut_end;
2073# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074
2075 // Get the path to the link target.
2076 ZeroMemory(buf, MAX_PATH);
2077 hr = psl->lpVtbl->GetPath(psl, buf, MAX_PATH, &ffd, 0);
2078 if (hr == S_OK && buf[0] != NUL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002079 rfname = vim_strsave((char_u *)buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080
Bram Moolenaar604729e2013-08-30 16:44:19 +02002081shortcut_end:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 // Release all interface pointers (both belong to the same object)
2083 if (ppf != NULL)
2084 ppf->lpVtbl->Release(ppf);
2085 if (psl != NULL)
2086 psl->lpVtbl->Release(psl);
Bram Moolenaar604729e2013-08-30 16:44:19 +02002087 if (pslw != NULL)
2088 pslw->lpVtbl->Release(pslw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089
2090 CoUninitialize();
2091 return rfname;
2092}
Bram Moolenaardce1e892019-02-10 23:18:53 +01002093
2094 char_u *
2095mch_resolve_path(char_u *fname, int reparse_point)
2096{
2097 char_u *path = resolve_shortcut(fname);
2098
2099 if (path == NULL && reparse_point)
2100 path = resolve_reparse_point(fname);
2101 return path;
2102}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103#endif
2104
2105#if (defined(FEAT_EVAL) && !defined(FEAT_GUI)) || defined(PROTO)
2106/*
2107 * Bring ourselves to the foreground. Does work if the OS doesn't allow it.
2108 */
2109 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002110win32_set_foreground(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111{
2112# ifndef FEAT_GUI
2113 GetConsoleHwnd(); /* get value of s_hwnd */
2114# endif
2115 if (s_hwnd != 0)
2116 SetForegroundWindow(s_hwnd);
2117}
2118#endif
2119
2120#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
2121/*
2122 * Client-server code for Vim
2123 *
2124 * Originally written by Paul Moore
2125 */
2126
2127/* In order to handle inter-process messages, we need to have a window. But
2128 * the functions in this module can be called before the main GUI window is
2129 * created (and may also be called in the console version, where there is no
2130 * GUI window at all).
2131 *
2132 * So we create a hidden window, and arrange to destroy it on exit.
2133 */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002134HWND message_window = 0; /* window that's handling messages */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135
2136#define VIM_CLASSNAME "VIM_MESSAGES"
2137#define VIM_CLASSNAME_LEN (sizeof(VIM_CLASSNAME) - 1)
2138
2139/* Communication is via WM_COPYDATA messages. The message type is send in
2140 * the dwData parameter. Types are defined here. */
2141#define COPYDATA_KEYS 0
2142#define COPYDATA_REPLY 1
2143#define COPYDATA_EXPR 10
2144#define COPYDATA_RESULT 11
2145#define COPYDATA_ERROR_RESULT 12
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002146#define COPYDATA_ENCODING 20
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147
2148/* This is a structure containing a server HWND and its name. */
2149struct server_id
2150{
2151 HWND hwnd;
2152 char_u *name;
2153};
2154
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002155/* Last received 'encoding' that the client uses. */
2156static char_u *client_enc = NULL;
2157
2158/*
2159 * Tell the other side what encoding we are using.
2160 * Errors are ignored.
2161 */
2162 static void
2163serverSendEnc(HWND target)
2164{
2165 COPYDATASTRUCT data;
2166
2167 data.dwData = COPYDATA_ENCODING;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002168 data.cbData = (DWORD)STRLEN(p_enc) + 1;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002169 data.lpData = p_enc;
2170 (void)SendMessage(target, WM_COPYDATA, (WPARAM)message_window,
2171 (LPARAM)(&data));
2172}
2173
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174/*
2175 * Clean up on exit. This destroys the hidden message window.
2176 */
2177 static void
2178#ifdef __BORLANDC__
2179 _RTLENTRYF
2180#endif
2181CleanUpMessaging(void)
2182{
2183 if (message_window != 0)
2184 {
2185 DestroyWindow(message_window);
2186 message_window = 0;
2187 }
2188}
2189
2190static int save_reply(HWND server, char_u *reply, int expr);
2191
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002192/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 * The window procedure for the hidden message window.
2194 * It handles callback messages and notifications from servers.
2195 * In order to process these messages, it is necessary to run a
2196 * message loop. Code which may run before the main message loop
2197 * is started (in the GUI) is careful to pump messages when it needs
2198 * to. Features which require message delivery during normal use will
2199 * not work in the console version - this basically means those
2200 * features which allow Vim to act as a server, rather than a client.
2201 */
2202 static LRESULT CALLBACK
2203Messaging_WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
2204{
2205 if (msg == WM_COPYDATA)
2206 {
2207 /* This is a message from another Vim. The dwData member of the
2208 * COPYDATASTRUCT determines the type of message:
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002209 * COPYDATA_ENCODING:
2210 * The encoding that the client uses. Following messages will
2211 * use this encoding, convert if needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 * COPYDATA_KEYS:
2213 * A key sequence. We are a server, and a client wants these keys
2214 * adding to the input queue.
2215 * COPYDATA_REPLY:
2216 * A reply. We are a client, and a server has sent this message
2217 * in response to a request. (server2client())
2218 * COPYDATA_EXPR:
2219 * An expression. We are a server, and a client wants us to
2220 * evaluate this expression.
2221 * COPYDATA_RESULT:
2222 * A reply. We are a client, and a server has sent this message
2223 * in response to a COPYDATA_EXPR.
2224 * COPYDATA_ERROR_RESULT:
2225 * A reply. We are a client, and a server has sent this message
2226 * in response to a COPYDATA_EXPR that failed to evaluate.
2227 */
2228 COPYDATASTRUCT *data = (COPYDATASTRUCT*)lParam;
2229 HWND sender = (HWND)wParam;
2230 COPYDATASTRUCT reply;
2231 char_u *res;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 int retval;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002233 char_u *str;
2234 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235
2236 switch (data->dwData)
2237 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002238 case COPYDATA_ENCODING:
2239 /* Remember the encoding that the client uses. */
2240 vim_free(client_enc);
2241 client_enc = enc_canonize((char_u *)data->lpData);
2242 return 1;
2243
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 case COPYDATA_KEYS:
2245 /* Remember who sent this, for <client> */
2246 clientWindow = sender;
2247
2248 /* Add the received keys to the input buffer. The loop waiting
2249 * for the user to do something should check the input buffer. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002250 str = serverConvert(client_enc, (char_u *)data->lpData, &tofree);
2251 server_to_input_buf(str);
2252 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253
2254# ifdef FEAT_GUI
2255 /* Wake up the main GUI loop. */
2256 if (s_hwnd != 0)
2257 PostMessage(s_hwnd, WM_NULL, 0, 0);
2258# endif
2259 return 1;
2260
2261 case COPYDATA_EXPR:
2262 /* Remember who sent this, for <client> */
2263 clientWindow = sender;
2264
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002265 str = serverConvert(client_enc, (char_u *)data->lpData, &tofree);
2266 res = eval_client_expr_to_string(str);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002267
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 if (res == NULL)
2269 {
Bram Moolenaar15bf76d2017-03-18 16:18:37 +01002270 char *err = _(e_invexprmsg);
2271 size_t len = STRLEN(str) + STRLEN(err) + 5;
2272
Bram Moolenaar1662ce12017-03-19 21:47:50 +01002273 res = alloc((unsigned)len);
Bram Moolenaar15bf76d2017-03-18 16:18:37 +01002274 if (res != NULL)
2275 vim_snprintf((char *)res, len, "%s: \"%s\"", err, str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 reply.dwData = COPYDATA_ERROR_RESULT;
2277 }
2278 else
2279 reply.dwData = COPYDATA_RESULT;
2280 reply.lpData = res;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002281 reply.cbData = (DWORD)STRLEN(res) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002283 serverSendEnc(sender);
Bram Moolenaar5246cd72013-06-16 16:41:47 +02002284 retval = (int)SendMessage(sender, WM_COPYDATA,
2285 (WPARAM)message_window, (LPARAM)(&reply));
Bram Moolenaar15bf76d2017-03-18 16:18:37 +01002286 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 vim_free(res);
2288 return retval;
2289
2290 case COPYDATA_REPLY:
2291 case COPYDATA_RESULT:
2292 case COPYDATA_ERROR_RESULT:
2293 if (data->lpData != NULL)
2294 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002295 str = serverConvert(client_enc, (char_u *)data->lpData,
2296 &tofree);
2297 if (tofree == NULL)
2298 str = vim_strsave(str);
2299 if (save_reply(sender, str,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 (data->dwData == COPYDATA_REPLY ? 0 :
2301 (data->dwData == COPYDATA_RESULT ? 1 :
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002302 2))) == FAIL)
2303 vim_free(str);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002304 else if (data->dwData == COPYDATA_REPLY)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 {
Bram Moolenaar427d51c2013-06-16 16:01:25 +02002306 char_u winstr[30];
2307
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002308 sprintf((char *)winstr, PRINTF_HEX_LONG_U, (long_u)sender);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002309 apply_autocmds(EVENT_REMOTEREPLY, winstr, str,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 TRUE, curbuf);
2311 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 }
2313 return 1;
2314 }
2315
2316 return 0;
2317 }
2318
2319 else if (msg == WM_ACTIVATE && wParam == WA_ACTIVE)
2320 {
2321 /* When the message window is activated (brought to the foreground),
2322 * this actually applies to the text window. */
2323#ifndef FEAT_GUI
2324 GetConsoleHwnd(); /* get value of s_hwnd */
2325#endif
2326 if (s_hwnd != 0)
2327 {
2328 SetForegroundWindow(s_hwnd);
2329 return 0;
2330 }
2331 }
2332
2333 return DefWindowProc(hwnd, msg, wParam, lParam);
2334}
2335
2336/*
2337 * Initialise the message handling process. This involves creating a window
2338 * to handle messages - the window will not be visible.
2339 */
2340 void
2341serverInitMessaging(void)
2342{
2343 WNDCLASS wndclass;
2344 HINSTANCE s_hinst;
2345
2346 /* Clean up on exit */
2347 atexit(CleanUpMessaging);
2348
2349 /* Register a window class - we only really care
2350 * about the window procedure
2351 */
2352 s_hinst = (HINSTANCE)GetModuleHandle(0);
2353 wndclass.style = 0;
2354 wndclass.lpfnWndProc = Messaging_WndProc;
2355 wndclass.cbClsExtra = 0;
2356 wndclass.cbWndExtra = 0;
2357 wndclass.hInstance = s_hinst;
2358 wndclass.hIcon = NULL;
2359 wndclass.hCursor = NULL;
2360 wndclass.hbrBackground = NULL;
2361 wndclass.lpszMenuName = NULL;
2362 wndclass.lpszClassName = VIM_CLASSNAME;
2363 RegisterClass(&wndclass);
2364
2365 /* Create the message window. It will be hidden, so the details don't
2366 * matter. Don't use WS_OVERLAPPEDWINDOW, it will make a shortcut remove
2367 * focus from gvim. */
2368 message_window = CreateWindow(VIM_CLASSNAME, "",
2369 WS_POPUPWINDOW | WS_CAPTION,
2370 CW_USEDEFAULT, CW_USEDEFAULT,
2371 100, 100, NULL, NULL,
2372 s_hinst, NULL);
2373}
2374
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002375/* Used by serverSendToVim() to find an alternate server name. */
2376static char_u *altname_buf_ptr = NULL;
2377
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378/*
2379 * Get the title of the window "hwnd", which is the Vim server name, in
2380 * "name[namelen]" and return the length.
2381 * Returns zero if window "hwnd" is not a Vim server.
2382 */
2383 static int
2384getVimServerName(HWND hwnd, char *name, int namelen)
2385{
2386 int len;
2387 char buffer[VIM_CLASSNAME_LEN + 1];
2388
2389 /* Ignore windows which aren't Vim message windows */
2390 len = GetClassName(hwnd, buffer, sizeof(buffer));
2391 if (len != VIM_CLASSNAME_LEN || STRCMP(buffer, VIM_CLASSNAME) != 0)
2392 return 0;
2393
2394 /* Get the title of the window */
2395 return GetWindowText(hwnd, name, namelen);
2396}
2397
2398 static BOOL CALLBACK
2399enumWindowsGetServer(HWND hwnd, LPARAM lparam)
2400{
2401 struct server_id *id = (struct server_id *)lparam;
2402 char server[MAX_PATH];
2403
2404 /* Get the title of the window */
2405 if (getVimServerName(hwnd, server, sizeof(server)) == 0)
2406 return TRUE;
2407
2408 /* If this is the server we're looking for, return its HWND */
2409 if (STRICMP(server, id->name) == 0)
2410 {
2411 id->hwnd = hwnd;
2412 return FALSE;
2413 }
2414
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002415 /* If we are looking for an alternate server, remember this name. */
2416 if (altname_buf_ptr != NULL
2417 && STRNICMP(server, id->name, STRLEN(id->name)) == 0
2418 && vim_isdigit(server[STRLEN(id->name)]))
2419 {
2420 STRCPY(altname_buf_ptr, server);
2421 altname_buf_ptr = NULL; /* don't use another name */
2422 }
2423
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 /* Otherwise, keep looking */
2425 return TRUE;
2426}
2427
2428 static BOOL CALLBACK
2429enumWindowsGetNames(HWND hwnd, LPARAM lparam)
2430{
2431 garray_T *ga = (garray_T *)lparam;
2432 char server[MAX_PATH];
2433
2434 /* Get the title of the window */
2435 if (getVimServerName(hwnd, server, sizeof(server)) == 0)
2436 return TRUE;
2437
2438 /* Add the name to the list */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002439 ga_concat(ga, (char_u *)server);
2440 ga_concat(ga, (char_u *)"\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 return TRUE;
2442}
2443
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002444struct enum_windows_s
2445{
2446 WNDENUMPROC lpEnumFunc;
2447 LPARAM lParam;
2448};
2449
2450 static BOOL CALLBACK
2451enum_windows_child(HWND hwnd, LPARAM lParam)
2452{
2453 struct enum_windows_s *ew = (struct enum_windows_s *)lParam;
2454
2455 return (ew->lpEnumFunc)(hwnd, ew->lParam);
2456}
2457
2458 static BOOL CALLBACK
2459enum_windows_toplevel(HWND hwnd, LPARAM lParam)
2460{
2461 struct enum_windows_s *ew = (struct enum_windows_s *)lParam;
2462
Bram Moolenaar95ba5c32018-10-07 22:47:07 +02002463 if ((ew->lpEnumFunc)(hwnd, ew->lParam))
2464 return TRUE;
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002465 return EnumChildWindows(hwnd, enum_windows_child, lParam);
2466}
2467
2468/* Enumerate all windows including children. */
2469 static BOOL
2470enum_windows(WNDENUMPROC lpEnumFunc, LPARAM lParam)
2471{
2472 struct enum_windows_s ew;
2473
2474 ew.lpEnumFunc = lpEnumFunc;
2475 ew.lParam = lParam;
2476 return EnumWindows(enum_windows_toplevel, (LPARAM)&ew);
2477}
2478
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 static HWND
2480findServer(char_u *name)
2481{
2482 struct server_id id;
2483
2484 id.name = name;
2485 id.hwnd = 0;
2486
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002487 enum_windows(enumWindowsGetServer, (LPARAM)(&id));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488
2489 return id.hwnd;
2490}
2491
2492 void
2493serverSetName(char_u *name)
2494{
2495 char_u *ok_name;
2496 HWND hwnd = 0;
2497 int i = 0;
2498 char_u *p;
2499
2500 /* Leave enough space for a 9-digit suffix to ensure uniqueness! */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002501 ok_name = alloc((unsigned)STRLEN(name) + 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502
2503 STRCPY(ok_name, name);
2504 p = ok_name + STRLEN(name);
2505
2506 for (;;)
2507 {
2508 /* This is inefficient - we're doing an EnumWindows loop for each
2509 * possible name. It would be better to grab all names in one go,
2510 * and scan the list each time...
2511 */
2512 hwnd = findServer(ok_name);
2513 if (hwnd == 0)
2514 break;
2515
2516 ++i;
2517 if (i >= 1000)
2518 break;
2519
2520 sprintf((char *)p, "%d", i);
2521 }
2522
2523 if (hwnd != 0)
2524 vim_free(ok_name);
2525 else
2526 {
2527 /* Remember the name */
2528 serverName = ok_name;
2529#ifdef FEAT_TITLE
2530 need_maketitle = TRUE; /* update Vim window title later */
2531#endif
2532
2533 /* Update the message window title */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002534 SetWindowText(message_window, (LPCSTR)ok_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535
2536#ifdef FEAT_EVAL
2537 /* Set the servername variable */
2538 set_vim_var_string(VV_SEND_SERVER, serverName, -1);
2539#endif
2540 }
2541}
2542
2543 char_u *
2544serverGetVimNames(void)
2545{
2546 garray_T ga;
2547
2548 ga_init2(&ga, 1, 100);
2549
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002550 enum_windows(enumWindowsGetNames, (LPARAM)(&ga));
Bram Moolenaar269ec652004-07-29 08:43:53 +00002551 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552
2553 return ga.ga_data;
2554}
2555
2556 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002557serverSendReply(
2558 char_u *name, /* Where to send. */
2559 char_u *reply) /* What to send. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560{
2561 HWND target;
2562 COPYDATASTRUCT data;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002563 long_u n = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564
2565 /* The "name" argument is a magic cookie obtained from expand("<client>").
2566 * It should be of the form 0xXXXXX - i.e. a C hex literal, which is the
2567 * value of the client's message window HWND.
2568 */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002569 sscanf((char *)name, SCANF_HEX_LONG_U, &n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 if (n == 0)
2571 return -1;
2572
2573 target = (HWND)n;
2574 if (!IsWindow(target))
2575 return -1;
2576
2577 data.dwData = COPYDATA_REPLY;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002578 data.cbData = (DWORD)STRLEN(reply) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 data.lpData = reply;
2580
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002581 serverSendEnc(target);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 if (SendMessage(target, WM_COPYDATA, (WPARAM)message_window,
2583 (LPARAM)(&data)))
2584 return 0;
2585
2586 return -1;
2587}
2588
2589 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002590serverSendToVim(
2591 char_u *name, /* Where to send. */
2592 char_u *cmd, /* What to send. */
2593 char_u **result, /* Result of eval'ed expression */
2594 void *ptarget, /* HWND of server */
2595 int asExpr, /* Expression or keys? */
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002596 int timeout, /* timeout in seconds or zero */
Bram Moolenaar05540972016-01-30 20:31:25 +01002597 int silent) /* don't complain about no server */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598{
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002599 HWND target;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 COPYDATASTRUCT data;
2601 char_u *retval = NULL;
2602 int retcode = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002603 char_u altname_buf[MAX_PATH];
2604
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01002605 /* Execute locally if no display or target is ourselves */
2606 if (serverName != NULL && STRICMP(name, serverName) == 0)
2607 return sendToLocalVim(cmd, asExpr, result);
2608
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002609 /* If the server name does not end in a digit then we look for an
2610 * alternate name. e.g. when "name" is GVIM the we may find GVIM2. */
2611 if (STRLEN(name) > 1 && !vim_isdigit(name[STRLEN(name) - 1]))
2612 altname_buf_ptr = altname_buf;
2613 altname_buf[0] = NUL;
2614 target = findServer(name);
2615 altname_buf_ptr = NULL;
2616 if (target == 0 && altname_buf[0] != NUL)
2617 /* Use another server name we found. */
2618 target = findServer(altname_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619
2620 if (target == 0)
2621 {
2622 if (!silent)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002623 semsg(_(e_noserver), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 return -1;
2625 }
2626
2627 if (ptarget)
2628 *(HWND *)ptarget = target;
2629
2630 data.dwData = asExpr ? COPYDATA_EXPR : COPYDATA_KEYS;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002631 data.cbData = (DWORD)STRLEN(cmd) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 data.lpData = cmd;
2633
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002634 serverSendEnc(target);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 if (SendMessage(target, WM_COPYDATA, (WPARAM)message_window,
2636 (LPARAM)(&data)) == 0)
2637 return -1;
2638
2639 if (asExpr)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002640 retval = serverGetReply(target, &retcode, TRUE, TRUE, timeout);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641
2642 if (result == NULL)
2643 vim_free(retval);
2644 else
2645 *result = retval; /* Caller assumes responsibility for freeing */
2646
2647 return retcode;
2648}
2649
2650/*
2651 * Bring the server to the foreground.
2652 */
2653 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002654serverForeground(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655{
2656 HWND target = findServer(name);
2657
2658 if (target != 0)
2659 SetForegroundWindow(target);
2660}
2661
2662/* Replies from server need to be stored until the client picks them up via
2663 * remote_read(). So we maintain a list of server-id/reply pairs.
2664 * Note that there could be multiple replies from one server pending if the
2665 * client is slow picking them up.
2666 * We just store the replies in a simple list. When we remove an entry, we
2667 * move list entries down to fill the gap.
2668 * The server ID is simply the HWND.
2669 */
2670typedef struct
2671{
2672 HWND server; /* server window */
2673 char_u *reply; /* reply string */
2674 int expr_result; /* 0 for REPLY, 1 for RESULT 2 for error */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002675} reply_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676
2677static garray_T reply_list = {0, 0, sizeof(reply_T), 5, 0};
2678
2679#define REPLY_ITEM(i) ((reply_T *)(reply_list.ga_data) + (i))
2680#define REPLY_COUNT (reply_list.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681
2682/* Flag which is used to wait for a reply */
2683static int reply_received = 0;
2684
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002685/*
2686 * Store a reply. "reply" must be allocated memory (or NULL).
2687 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 static int
2689save_reply(HWND server, char_u *reply, int expr)
2690{
2691 reply_T *rep;
2692
2693 if (ga_grow(&reply_list, 1) == FAIL)
2694 return FAIL;
2695
2696 rep = REPLY_ITEM(REPLY_COUNT);
2697 rep->server = server;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002698 rep->reply = reply;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 rep->expr_result = expr;
2700 if (rep->reply == NULL)
2701 return FAIL;
2702
2703 ++REPLY_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 reply_received = 1;
2705 return OK;
2706}
2707
2708/*
2709 * Get a reply from server "server".
2710 * When "expr_res" is non NULL, get the result of an expression, otherwise a
2711 * server2client() message.
2712 * When non NULL, point to return code. 0 => OK, -1 => ERROR
2713 * If "remove" is TRUE, consume the message, the caller must free it then.
2714 * if "wait" is TRUE block until a message arrives (or the server exits).
2715 */
2716 char_u *
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002717serverGetReply(HWND server, int *expr_res, int remove, int wait, int timeout)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718{
2719 int i;
2720 char_u *reply;
2721 reply_T *rep;
Bram Moolenaar15e737f2017-03-18 21:22:47 +01002722 int did_process = FALSE;
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002723 time_t start;
2724 time_t now;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725
2726 /* When waiting, loop until the message waiting for is received. */
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002727 time(&start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 for (;;)
2729 {
2730 /* Reset this here, in case a message arrives while we are going
2731 * through the already received messages. */
2732 reply_received = 0;
2733
2734 for (i = 0; i < REPLY_COUNT; ++i)
2735 {
2736 rep = REPLY_ITEM(i);
2737 if (rep->server == server
2738 && ((rep->expr_result != 0) == (expr_res != NULL)))
2739 {
2740 /* Save the values we've found for later */
2741 reply = rep->reply;
2742 if (expr_res != NULL)
2743 *expr_res = rep->expr_result == 1 ? 0 : -1;
2744
2745 if (remove)
2746 {
2747 /* Move the rest of the list down to fill the gap */
2748 mch_memmove(rep, rep + 1,
2749 (REPLY_COUNT - i - 1) * sizeof(reply_T));
2750 --REPLY_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 }
2752
2753 /* Return the reply to the caller, who takes on responsibility
2754 * for freeing it if "remove" is TRUE. */
2755 return reply;
2756 }
2757 }
2758
2759 /* If we got here, we didn't find a reply. Return immediately if the
2760 * "wait" parameter isn't set. */
2761 if (!wait)
Bram Moolenaar15e737f2017-03-18 21:22:47 +01002762 {
2763 /* Process pending messages once. Without this, looping on
2764 * remote_peek() would never get the reply. */
2765 if (!did_process)
2766 {
2767 did_process = TRUE;
2768 serverProcessPendingMessages();
2769 continue;
2770 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 break;
Bram Moolenaar15e737f2017-03-18 21:22:47 +01002772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773
2774 /* We need to wait for a reply. Enter a message loop until the
2775 * "reply_received" flag gets set. */
2776
2777 /* Loop until we receive a reply */
2778 while (reply_received == 0)
2779 {
Bram Moolenaar42205552017-03-18 19:42:22 +01002780#ifdef FEAT_TIMERS
Bram Moolenaard23a8232018-02-10 18:45:26 +01002781 /* TODO: use the return value to decide how long to wait. */
Bram Moolenaar42205552017-03-18 19:42:22 +01002782 check_due_timer();
2783#endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002784 time(&now);
2785 if (timeout > 0 && (now - start) >= timeout)
2786 break;
2787
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 /* Wait for a SendMessage() call to us. This could be the reply
2789 * we are waiting for. Use a timeout of a second, to catch the
2790 * situation that the server died unexpectedly. */
2791 MsgWaitForMultipleObjects(0, NULL, TRUE, 1000, QS_ALLINPUT);
2792
2793 /* If the server has died, give up */
2794 if (!IsWindow(server))
2795 return NULL;
2796
2797 serverProcessPendingMessages();
2798 }
2799 }
2800
2801 return NULL;
2802}
2803
2804/*
2805 * Process any messages in the Windows message queue.
2806 */
2807 void
2808serverProcessPendingMessages(void)
2809{
2810 MSG msg;
2811
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02002812 while (pPeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 {
2814 TranslateMessage(&msg);
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02002815 pDispatchMessage(&msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816 }
2817}
2818
2819#endif /* FEAT_CLIENTSERVER */
2820
2821#if defined(FEAT_GUI) || (defined(FEAT_PRINTER) && !defined(FEAT_POSTSCRIPT)) \
2822 || defined(PROTO)
2823
2824struct charset_pair
2825{
2826 char *name;
2827 BYTE charset;
2828};
2829
2830static struct charset_pair
2831charset_pairs[] =
2832{
2833 {"ANSI", ANSI_CHARSET},
2834 {"CHINESEBIG5", CHINESEBIG5_CHARSET},
2835 {"DEFAULT", DEFAULT_CHARSET},
2836 {"HANGEUL", HANGEUL_CHARSET},
2837 {"OEM", OEM_CHARSET},
2838 {"SHIFTJIS", SHIFTJIS_CHARSET},
2839 {"SYMBOL", SYMBOL_CHARSET},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840 {"ARABIC", ARABIC_CHARSET},
2841 {"BALTIC", BALTIC_CHARSET},
2842 {"EASTEUROPE", EASTEUROPE_CHARSET},
2843 {"GB2312", GB2312_CHARSET},
2844 {"GREEK", GREEK_CHARSET},
2845 {"HEBREW", HEBREW_CHARSET},
2846 {"JOHAB", JOHAB_CHARSET},
2847 {"MAC", MAC_CHARSET},
2848 {"RUSSIAN", RUSSIAN_CHARSET},
2849 {"THAI", THAI_CHARSET},
2850 {"TURKISH", TURKISH_CHARSET},
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002851#ifdef VIETNAMESE_CHARSET
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 {"VIETNAMESE", VIETNAMESE_CHARSET},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853#endif
2854 {NULL, 0}
2855};
2856
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002857struct quality_pair
2858{
2859 char *name;
2860 DWORD quality;
2861};
2862
2863static struct quality_pair
2864quality_pairs[] = {
2865#ifdef CLEARTYPE_QUALITY
2866 {"CLEARTYPE", CLEARTYPE_QUALITY},
2867#endif
2868#ifdef ANTIALIASED_QUALITY
2869 {"ANTIALIASED", ANTIALIASED_QUALITY},
2870#endif
Bram Moolenaar73a733e2016-05-11 21:05:05 +02002871#ifdef NONANTIALIASED_QUALITY
2872 {"NONANTIALIASED", NONANTIALIASED_QUALITY},
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002873#endif
2874#ifdef PROOF_QUALITY
2875 {"PROOF", PROOF_QUALITY},
2876#endif
Bram Moolenaar4c9ce052016-04-04 21:06:19 +02002877#ifdef DRAFT_QUALITY
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002878 {"DRAFT", DRAFT_QUALITY},
2879#endif
2880 {"DEFAULT", DEFAULT_QUALITY},
2881 {NULL, 0}
2882};
2883
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884/*
2885 * Convert a charset ID to a name.
2886 * Return NULL when not recognized.
2887 */
2888 char *
2889charset_id2name(int id)
2890{
2891 struct charset_pair *cp;
2892
2893 for (cp = charset_pairs; cp->name != NULL; ++cp)
2894 if ((BYTE)id == cp->charset)
2895 break;
2896 return cp->name;
2897}
2898
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002899/*
2900 * Convert a quality ID to a name.
2901 * Return NULL when not recognized.
2902 */
2903 char *
2904quality_id2name(DWORD id)
2905{
2906 struct quality_pair *qp;
2907
2908 for (qp = quality_pairs; qp->name != NULL; ++qp)
2909 if (id == qp->quality)
2910 break;
2911 return qp->name;
2912}
2913
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002914static const LOGFONTW s_lfDefault =
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915{
2916 -12, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, DEFAULT_CHARSET,
2917 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
2918 PROOF_QUALITY, FIXED_PITCH | FF_DONTCARE,
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002919 L"Fixedsys" /* see _ReadVimIni */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920};
2921
2922/* Initialise the "current height" to -12 (same as s_lfDefault) just
2923 * in case the user specifies a font in "guifont" with no size before a font
2924 * with an explicit size has been set. This defaults the size to this value
2925 * (-12 equates to roughly 9pt).
2926 */
2927int current_font_height = -12; /* also used in gui_w48.c */
2928
2929/* Convert a string representing a point size into pixels. The string should
2930 * be a positive decimal number, with an optional decimal point (eg, "12", or
2931 * "10.5"). The pixel value is returned, and a pointer to the next unconverted
2932 * character is stored in *end. The flag "vertical" says whether this
2933 * calculation is for a vertical (height) size or a horizontal (width) one.
2934 */
2935 static int
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002936points_to_pixels(WCHAR *str, WCHAR **end, int vertical, long_i pprinter_dc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937{
2938 int pixels;
2939 int points = 0;
2940 int divisor = 0;
2941 HWND hwnd = (HWND)0;
2942 HDC hdc;
2943 HDC printer_dc = (HDC)pprinter_dc;
2944
2945 while (*str != NUL)
2946 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002947 if (*str == L'.' && divisor == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948 {
2949 /* Start keeping a divisor, for later */
2950 divisor = 1;
2951 }
2952 else
2953 {
2954 if (!VIM_ISDIGIT(*str))
2955 break;
2956
2957 points *= 10;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002958 points += *str - L'0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 divisor *= 10;
2960 }
2961 ++str;
2962 }
2963
2964 if (divisor == 0)
2965 divisor = 1;
2966
2967 if (printer_dc == NULL)
2968 {
2969 hwnd = GetDesktopWindow();
2970 hdc = GetWindowDC(hwnd);
2971 }
2972 else
2973 hdc = printer_dc;
2974
2975 pixels = MulDiv(points,
2976 GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX),
2977 72 * divisor);
2978
2979 if (printer_dc == NULL)
2980 ReleaseDC(hwnd, hdc);
2981
2982 *end = str;
2983 return pixels;
2984}
2985
2986 static int CALLBACK
2987font_enumproc(
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002988 ENUMLOGFONTW *elf,
2989 NEWTEXTMETRICW *ntm UNUSED,
2990 DWORD type UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991 LPARAM lparam)
2992{
2993 /* Return value:
2994 * 0 = terminate now (monospace & ANSI)
2995 * 1 = continue, still no luck...
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002996 * 2 = continue, but we have an acceptable LOGFONTW
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997 * (monospace, not ANSI)
2998 * We use these values, as EnumFontFamilies returns 1 if the
2999 * callback function is never called. So, we check the return as
3000 * 0 = perfect, 2 = OK, 1 = no good...
3001 * It's not pretty, but it works!
3002 */
3003
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003004 LOGFONTW *lf = (LOGFONTW *)(lparam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005
3006#ifndef FEAT_PROPORTIONAL_FONTS
3007 /* Ignore non-monospace fonts without further ado */
3008 if ((ntm->tmPitchAndFamily & 1) != 0)
3009 return 1;
3010#endif
3011
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003012 /* Remember this LOGFONTW as a "possible" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 *lf = elf->elfLogFont;
3014
3015 /* Terminate the scan as soon as we find an ANSI font */
3016 if (lf->lfCharSet == ANSI_CHARSET
3017 || lf->lfCharSet == OEM_CHARSET
3018 || lf->lfCharSet == DEFAULT_CHARSET)
3019 return 0;
3020
3021 /* Continue the scan - we have a non-ANSI font */
3022 return 2;
3023}
3024
3025 static int
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003026init_logfont(LOGFONTW *lf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027{
3028 int n;
3029 HWND hwnd = GetDesktopWindow();
3030 HDC hdc = GetWindowDC(hwnd);
3031
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003032 n = EnumFontFamiliesW(hdc,
3033 lf->lfFaceName,
3034 (FONTENUMPROCW)font_enumproc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 (LPARAM)lf);
3036
3037 ReleaseDC(hwnd, hdc);
3038
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003039 /* If we couldn't find a usable font, return failure */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040 if (n == 1)
3041 return FAIL;
3042
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003043 /* Tidy up the rest of the LOGFONTW structure. We set to a basic
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044 * font - get_logfont() sets bold, italic, etc based on the user's
3045 * input.
3046 */
3047 lf->lfHeight = current_font_height;
3048 lf->lfWidth = 0;
3049 lf->lfItalic = FALSE;
3050 lf->lfUnderline = FALSE;
3051 lf->lfStrikeOut = FALSE;
3052 lf->lfWeight = FW_NORMAL;
3053
3054 /* Return success */
3055 return OK;
3056}
3057
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003058/*
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003059 * Compare a UTF-16 string and an ASCII string literally.
3060 * Only works all the code points are inside ASCII range.
3061 */
3062 static int
3063utf16ascncmp(const WCHAR *w, const char *p, size_t n)
3064{
3065 size_t i;
3066
3067 for (i = 0; i < n; i++)
3068 {
3069 if (w[i] == 0 || w[i] != p[i])
3070 return w[i] - p[i];
3071 }
3072 return 0;
3073}
3074
3075/*
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003076 * Get font info from "name" into logfont "lf".
3077 * Return OK for a valid name, FAIL otherwise.
3078 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 int
3080get_logfont(
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003081 LOGFONTW *lf,
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003082 char_u *name,
3083 HDC printer_dc,
3084 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003086 WCHAR *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087 int i;
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003088 int ret = FAIL;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003089 static LOGFONTW *lastlf = NULL;
3090 WCHAR *wname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091
3092 *lf = s_lfDefault;
3093 if (name == NULL)
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003094 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003096 wname = enc_to_utf16(name, NULL);
3097 if (wname == NULL)
3098 return FAIL;
3099
3100 if (wcscmp(wname, L"*") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01003102#if defined(FEAT_GUI_MSWIN)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003103 CHOOSEFONTW cf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 /* if name is "*", bring up std font dialog: */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02003105 vim_memset(&cf, 0, sizeof(cf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 cf.lStructSize = sizeof(cf);
3107 cf.hwndOwner = s_hwnd;
3108 cf.Flags = CF_SCREENFONTS | CF_FIXEDPITCHONLY | CF_INITTOLOGFONTSTRUCT;
3109 if (lastlf != NULL)
3110 *lf = *lastlf;
3111 cf.lpLogFont = lf;
3112 cf.nFontType = 0 ; //REGULAR_FONTTYPE;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003113 if (ChooseFontW(&cf))
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003114 ret = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115#endif
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003116 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 }
3118
3119 /*
3120 * Split name up, it could be <name>:h<height>:w<width> etc.
3121 */
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003122 for (p = wname; *p && *p != L':'; p++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003124 if (p - wname + 1 >= LF_FACESIZE)
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003125 goto theend; /* Name too long */
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003126 lf->lfFaceName[p - wname] = *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 }
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003128 if (p != wname)
3129 lf->lfFaceName[p - wname] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130
3131 /* First set defaults */
3132 lf->lfHeight = -12;
3133 lf->lfWidth = 0;
3134 lf->lfWeight = FW_NORMAL;
3135 lf->lfItalic = FALSE;
3136 lf->lfUnderline = FALSE;
3137 lf->lfStrikeOut = FALSE;
3138
3139 /*
3140 * If the font can't be found, try replacing '_' by ' '.
3141 */
3142 if (init_logfont(lf) == FAIL)
3143 {
3144 int did_replace = FALSE;
3145
3146 for (i = 0; lf->lfFaceName[i]; ++i)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003147 if (lf->lfFaceName[i] == L'_')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003149 lf->lfFaceName[i] = L' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 did_replace = TRUE;
3151 }
3152 if (!did_replace || init_logfont(lf) == FAIL)
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003153 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 }
3155
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003156 while (*p == L':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 p++;
3158
3159 /* Set the values found after ':' */
3160 while (*p)
3161 {
3162 switch (*p++)
3163 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003164 case L'h':
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003165 lf->lfHeight = - points_to_pixels(p, &p, TRUE, (long_i)printer_dc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003167 case L'w':
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003168 lf->lfWidth = points_to_pixels(p, &p, FALSE, (long_i)printer_dc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003170 case L'b':
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 lf->lfWeight = FW_BOLD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003173 case L'i':
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 lf->lfItalic = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003176 case L'u':
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 lf->lfUnderline = TRUE;
3178 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003179 case L's':
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180 lf->lfStrikeOut = TRUE;
3181 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003182 case L'c':
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 {
3184 struct charset_pair *cp;
3185
3186 for (cp = charset_pairs; cp->name != NULL; ++cp)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003187 if (utf16ascncmp(p, cp->name, strlen(cp->name)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 {
3189 lf->lfCharSet = cp->charset;
3190 p += strlen(cp->name);
3191 break;
3192 }
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003193 if (cp->name == NULL && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003195 char_u *s = utf16_to_enc(p, NULL);
3196 semsg(_("E244: Illegal charset name \"%s\" in font name \"%s\""), s, name);
3197 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 break;
3199 }
3200 break;
3201 }
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003202 case L'q':
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003203 {
3204 struct quality_pair *qp;
3205
3206 for (qp = quality_pairs; qp->name != NULL; ++qp)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003207 if (utf16ascncmp(p, qp->name, strlen(qp->name)) == 0)
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003208 {
3209 lf->lfQuality = qp->quality;
3210 p += strlen(qp->name);
3211 break;
3212 }
3213 if (qp->name == NULL && verbose)
3214 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003215 char_u *s = utf16_to_enc(p, NULL);
3216 semsg(_("E244: Illegal quality name \"%s\" in font name \"%s\""), s, name);
3217 vim_free(s);
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003218 break;
3219 }
3220 break;
3221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 default:
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003223 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003224 semsg(_("E245: Illegal char '%c' in font name \"%s\""), p[-1], name);
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003225 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 }
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003227 while (*p == L':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 p++;
3229 }
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003230 ret = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 /* ron: init lastlf */
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003234 if (ret == OK && printer_dc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 {
3236 vim_free(lastlf);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003237 lastlf = (LOGFONTW *)alloc(sizeof(LOGFONTW));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 if (lastlf != NULL)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003239 mch_memmove(lastlf, lf, sizeof(LOGFONTW));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 }
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003241 vim_free(wname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003243 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244}
3245
3246#endif /* defined(FEAT_GUI) || defined(FEAT_PRINTER) */
Bram Moolenaarf12d9832016-01-29 21:11:25 +01003247
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003248#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaarf12d9832016-01-29 21:11:25 +01003249/*
3250 * Initialize the Winsock dll.
3251 */
3252 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003253channel_init_winsock(void)
Bram Moolenaarf12d9832016-01-29 21:11:25 +01003254{
3255 WSADATA wsaData;
3256 int wsaerr;
3257
3258 if (WSInitialized)
3259 return;
3260
3261 wsaerr = WSAStartup(MAKEWORD(2, 2), &wsaData);
3262 if (wsaerr == 0)
3263 WSInitialized = TRUE;
3264}
3265#endif