blob: 9179e9b2d9da3d2c4f1b299319d1ae2a2940412c [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;
Bram Moolenaarb9cdb372019-04-17 18:24:35 +0200101typedef int LPDWORD;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102typedef int LPSTR;
103typedef int LPTSTR;
Bram Moolenaarb9cdb372019-04-17 18:24:35 +0200104typedef int LPVOID;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000105typedef int LPWSTR;
106typedef int LRESULT;
107typedef int MOUSE_EVENT_RECORD;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100108typedef int NEWTEXTMETRICW;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000109typedef int PACL;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200110typedef int PRINTDLGW;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000111typedef int PSECURITY_DESCRIPTOR;
112typedef int PSID;
113typedef int SECURITY_INFORMATION;
114typedef int SHORT;
115typedef int SMALL_RECT;
116typedef int TEXTMETRIC;
117typedef int UINT;
118typedef int WCHAR;
Bram Moolenaarc447d8d2018-12-18 21:56:28 +0100119typedef int WNDENUMPROC;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120typedef int WORD;
121typedef int WPARAM;
122typedef void VOID;
123#endif
124
125/* Record all output and all keyboard & mouse input */
126/* #define MCH_WRITE_DUMP */
127
128#ifdef MCH_WRITE_DUMP
129FILE* fdDump = NULL;
130#endif
131
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200132#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133extern char g_szOrigTitle[];
134#endif
135
136#ifdef FEAT_GUI
137extern HWND s_hwnd;
138#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139static HWND s_hwnd = 0; /* console window handle, set by GetConsoleHwnd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140#endif
141
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100142#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100143int WSInitialized = FALSE; /* WinSock is initialized */
144#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145
146/* Don't generate prototypes here, because some systems do have these
147 * functions. */
148#if defined(__GNUC__) && !defined(PROTO)
149# ifndef __MINGW32__
150int _stricoll(char *a, char *b)
151{
152 // the ANSI-ish correct way is to use strxfrm():
153 char a_buff[512], b_buff[512]; // file names, so this is enough on Win32
154 strxfrm(a_buff, a, 512);
155 strxfrm(b_buff, b, 512);
156 return strcoll(a_buff, b_buff);
157}
158
159char * _fullpath(char *buf, char *fname, int len)
160{
161 LPTSTR toss;
162
163 return (char *)GetFullPathName(fname, len, buf, &toss);
164}
165# endif
166
Bram Moolenaaraf62ff32013-03-19 14:48:29 +0100167# if !defined(__MINGW32__) || (__GNUC__ < 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168int _chdrive(int drive)
169{
170 char temp [3] = "-:";
171 temp[0] = drive + 'A' - 1;
172 return !SetCurrentDirectory(temp);
173}
Bram Moolenaaraf62ff32013-03-19 14:48:29 +0100174# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175#else
176# ifdef __BORLANDC__
177/* being a more ANSI compliant compiler, BorlandC doesn't define _stricoll:
178 * but it does in BC 5.02! */
179# if __BORLANDC__ < 0x502
180int _stricoll(char *a, char *b)
181{
182# if 1
183 // this is fast but not correct:
184 return stricmp(a, b);
185# else
186 // the ANSI-ish correct way is to use strxfrm():
187 char a_buff[512], b_buff[512]; // file names, so this is enough on Win32
188 strxfrm(a_buff, a, 512);
189 strxfrm(b_buff, b, 512);
190 return strcoll(a_buff, b_buff);
191# endif
192}
193# endif
194# endif
195#endif
196
197
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200198#ifndef PROTO
199/*
200 * Save the instance handle of the exe/dll.
201 */
202 void
203SaveInst(HINSTANCE hInst)
204{
205 g_hinst = hInst;
206}
207#endif
208
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209#if defined(FEAT_GUI_MSWIN) || defined(PROTO)
210/*
211 * GUI version of mch_exit().
212 * Shut down and exit with status `r'
213 * Careful: mch_exit() may be called before mch_init()!
214 */
215 void
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200216mch_exit_g(int r)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217{
Bram Moolenaar955f1982017-02-05 15:10:51 +0100218 exiting = TRUE;
219
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220 display_errors();
221
222 ml_close_all(TRUE); /* remove all memfiles */
223
224# ifdef FEAT_OLE
225 UninitOLE();
226# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100227# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228 if (WSInitialized)
229 {
230 WSInitialized = FALSE;
231 WSACleanup();
232 }
233# endif
234#ifdef DYNAMIC_GETTEXT
235 dyn_libintl_end();
236#endif
237
238 if (gui.in_use)
239 gui_exit(r);
Bram Moolenaar85c79d32007-02-20 01:59:20 +0000240
241#ifdef EXITFREE
242 free_all_mem();
243#endif
244
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245 exit(r);
246}
247
248#endif /* FEAT_GUI_MSWIN */
249
250
251/*
252 * Init the tables for toupper() and tolower().
253 */
254 void
255mch_early_init(void)
256{
257 int i;
258
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259 PlatformId();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260
261 /* Init the tables for toupper() and tolower() */
262 for (i = 0; i < 256; ++i)
263 toupper_tab[i] = tolower_tab[i] = i;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100264 CharUpperBuff((LPSTR)toupper_tab, 256);
265 CharLowerBuff((LPSTR)tolower_tab, 256);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266}
267
268
269/*
270 * Return TRUE if the input comes from a terminal, FALSE otherwise.
271 */
272 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100273mch_input_isatty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274{
275#ifdef FEAT_GUI_MSWIN
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200276# ifdef VIMDLL
277 if (gui.in_use)
278# endif
279 return TRUE; /* GUI always has a tty */
280#endif
281#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282 if (isatty(read_cmd_fd))
283 return TRUE;
284 return FALSE;
285#endif
286}
287
288#ifdef FEAT_TITLE
289/*
290 * mch_settitle(): set titlebar of our window
291 */
292 void
293mch_settitle(
294 char_u *title,
295 char_u *icon)
296{
297# ifdef FEAT_GUI_MSWIN
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200298# ifdef VIMDLL
299 if (gui.in_use)
300# endif
301 {
302 gui_mch_settitle(title, icon);
303 return;
304 }
305# endif
306# if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307 if (title != NULL)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000308 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200309 WCHAR *wp = enc_to_utf16(title, NULL);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000310
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200311 if (wp == NULL)
312 return;
313
314 SetConsoleTitleW(wp);
315 vim_free(wp);
316 return;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000317 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000318# endif
319}
320
321
322/*
323 * Restore the window/icon title.
324 * which is one of:
Bram Moolenaar40385db2018-08-07 22:31:44 +0200325 * SAVE_RESTORE_TITLE: Just restore title
326 * SAVE_RESTORE_ICON: Just restore icon (which we don't have)
327 * SAVE_RESTORE_BOTH: Restore title and icon (which we don't have)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328 */
329 void
Bram Moolenaar1266d672017-02-01 13:43:36 +0100330mch_restore_title(int which UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331{
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200332#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
333# ifdef VIMDLL
334 if (!gui.in_use)
335# endif
336 SetConsoleTitle(g_szOrigTitle);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337#endif
338}
339
340
341/*
342 * Return TRUE if we can restore the title (we can)
343 */
344 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100345mch_can_restore_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346{
347 return TRUE;
348}
349
350
351/*
352 * Return TRUE if we can restore the icon title (we can't)
353 */
354 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100355mch_can_restore_icon(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000356{
357 return FALSE;
358}
359#endif /* FEAT_TITLE */
360
361
362/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000363 * Get absolute file name into buffer "buf" of length "len" bytes,
364 * turning all '/'s into '\\'s and getting the correct case of each component
365 * of the file name. Append a (back)slash to a directory name.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366 * When 'shellslash' set do it the other way around.
367 * Return OK or FAIL.
368 */
369 int
370mch_FullName(
371 char_u *fname,
372 char_u *buf,
373 int len,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100374 int force UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375{
376 int nResult = FAIL;
377
378#ifdef __BORLANDC__
379 if (*fname == NUL) /* Borland behaves badly here - make it consistent */
380 nResult = mch_dirname(buf, len);
381 else
382#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200384 WCHAR *wname;
385 WCHAR wbuf[MAX_PATH];
386 char_u *cname = NULL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000387
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200388 wname = enc_to_utf16(fname, NULL);
389 if (wname != NULL && _wfullpath(wbuf, wname, MAX_PATH) != NULL)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000390 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200391 cname = utf16_to_enc((short_u *)wbuf, NULL);
392 if (cname != NULL)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000393 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200394 vim_strncpy(buf, cname, len - 1);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000395 nResult = OK;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200396 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000397 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200398 vim_free(wname);
399 vim_free(cname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401
402#ifdef USE_FNAME_CASE
403 fname_case(buf, len);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000404#else
405 slash_adjust(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406#endif
407
408 return nResult;
409}
410
411
412/*
413 * Return TRUE if "fname" does not depend on the current directory.
414 */
415 int
416mch_isFullName(char_u *fname)
417{
Bram Moolenaard2a203b2013-08-30 16:51:18 +0200418 /* WinNT and later can use _MAX_PATH wide characters for a pathname, which
419 * means that the maximum pathname is _MAX_PATH * 3 bytes when 'enc' is
420 * UTF-8. */
421 char szName[_MAX_PATH * 3 + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422
423 /* A name like "d:/foo" and "//server/share" is absolute */
424 if ((fname[0] && fname[1] == ':' && (fname[2] == '/' || fname[2] == '\\'))
425 || (fname[0] == fname[1] && (fname[0] == '/' || fname[0] == '\\')))
426 return TRUE;
427
428 /* A name that can't be made absolute probably isn't absolute. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100429 if (mch_FullName(fname, (char_u *)szName, sizeof(szName) - 1, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430 return FALSE;
431
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100432 return pathcmp((const char *)fname, (const char *)szName, -1) == 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000433}
434
435/*
436 * Replace all slashes by backslashes.
437 * This used to be the other way around, but MS-DOS sometimes has problems
438 * with slashes (e.g. in a command name). We can't have mixed slashes and
439 * backslashes, because comparing file names will not work correctly. The
440 * commands that use a file name should try to avoid the need to type a
441 * backslash twice.
442 * When 'shellslash' set do it the other way around.
Bram Moolenaarb4f6a462015-10-13 19:43:17 +0200443 * When the path looks like a URL leave it unmodified.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444 */
445 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100446slash_adjust(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000447{
Bram Moolenaarb4f6a462015-10-13 19:43:17 +0200448 if (path_with_url(p))
449 return;
Bram Moolenaar39d21e32017-08-05 23:09:31 +0200450
451 if (*p == '`')
452 {
Bram Moolenaar116a0f82017-08-07 21:17:57 +0200453 size_t len = STRLEN(p);
454
Bram Moolenaar39d21e32017-08-05 23:09:31 +0200455 /* don't replace backslash in backtick quoted strings */
Bram Moolenaar39d21e32017-08-05 23:09:31 +0200456 if (len > 2 && *(p + len - 1) == '`')
457 return;
458 }
459
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +0000460 while (*p)
461 {
462 if (*p == psepcN)
463 *p = psepc;
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100464 MB_PTR_ADV(p);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +0000465 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466}
467
Bram Moolenaar8767f522016-07-01 17:17:39 +0200468/* Use 64-bit stat functions if available. */
469#ifdef HAVE_STAT64
470# undef stat
471# undef _stat
472# undef _wstat
473# undef _fstat
474# define stat _stat64
475# define _stat _stat64
476# define _wstat _wstat64
477# define _fstat _fstat64
478#endif
479
Bram Moolenaar9d1685d2014-01-14 12:18:45 +0100480#if (defined(_MSC_VER) && (_MSC_VER >= 1300)) || defined(__MINGW32__)
Bram Moolenaar8962fda2013-09-29 19:05:21 +0200481# define OPEN_OH_ARGTYPE intptr_t
482#else
483# define OPEN_OH_ARGTYPE long
484#endif
485
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200486 static int
Bram Moolenaar8767f522016-07-01 17:17:39 +0200487wstat_symlink_aware(const WCHAR *name, stat_T *stp)
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200488{
Bram Moolenaara12a1612019-01-24 16:39:02 +0100489#if (defined(_MSC_VER) && (_MSC_VER < 1900)) || defined(__MINGW32__)
Bram Moolenaarfce7b3d2016-01-19 19:00:32 +0100490 /* Work around for VC12 or earlier (and MinGW). _wstat() can't handle
491 * symlinks properly.
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200492 * VC9 or earlier: _wstat() doesn't support a symlink at all. It retrieves
493 * status of a symlink itself.
494 * VC10: _wstat() supports a symlink to a normal file, but it doesn't
Bram Moolenaarfce7b3d2016-01-19 19:00:32 +0100495 * support a symlink to a directory (always returns an error).
496 * VC11 and VC12: _wstat() doesn't return an error for a symlink to a
497 * directory, but it doesn't set S_IFDIR flag.
498 * MinGW: Same as VC9. */
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200499 int n;
500 BOOL is_symlink = FALSE;
501 HANDLE hFind, h;
502 DWORD attr = 0;
503 WIN32_FIND_DATAW findDataW;
504
505 hFind = FindFirstFileW(name, &findDataW);
506 if (hFind != INVALID_HANDLE_VALUE)
507 {
508 attr = findDataW.dwFileAttributes;
509 if ((attr & FILE_ATTRIBUTE_REPARSE_POINT)
510 && (findDataW.dwReserved0 == IO_REPARSE_TAG_SYMLINK))
511 is_symlink = TRUE;
512 FindClose(hFind);
513 }
514 if (is_symlink)
515 {
516 h = CreateFileW(name, FILE_READ_ATTRIBUTES,
517 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
518 OPEN_EXISTING,
519 (attr & FILE_ATTRIBUTE_DIRECTORY)
520 ? FILE_FLAG_BACKUP_SEMANTICS : 0,
521 NULL);
522 if (h != INVALID_HANDLE_VALUE)
523 {
524 int fd;
525
Bram Moolenaar8962fda2013-09-29 19:05:21 +0200526 fd = _open_osfhandle((OPEN_OH_ARGTYPE)h, _O_RDONLY);
Bram Moolenaar8767f522016-07-01 17:17:39 +0200527 n = _fstat(fd, (struct _stat *)stp);
Bram Moolenaarfce7b3d2016-01-19 19:00:32 +0100528 if ((n == 0) && (attr & FILE_ATTRIBUTE_DIRECTORY))
529 stp->st_mode = (stp->st_mode & ~S_IFREG) | S_IFDIR;
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200530 _close(fd);
531 return n;
532 }
533 }
Bram Moolenaara12a1612019-01-24 16:39:02 +0100534#endif
Bram Moolenaar8767f522016-07-01 17:17:39 +0200535 return _wstat(name, (struct _stat *)stp);
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200536}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537
538/*
539 * stat() can't handle a trailing '/' or '\', remove it first.
540 */
541 int
Bram Moolenaar8767f522016-07-01 17:17:39 +0200542vim_stat(const char *name, stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543{
Bram Moolenaard2a203b2013-08-30 16:51:18 +0200544 /* WinNT and later can use _MAX_PATH wide characters for a pathname, which
545 * means that the maximum pathname is _MAX_PATH * 3 bytes when 'enc' is
546 * UTF-8. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100547 char_u buf[_MAX_PATH * 3 + 1];
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100548 char_u *p;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200549 WCHAR *wp;
550 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551
Bram Moolenaard2a203b2013-08-30 16:51:18 +0200552 vim_strncpy((char_u *)buf, (char_u *)name, sizeof(buf) - 1);
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100553 p = buf + STRLEN(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554 if (p > buf)
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100555 MB_PTR_BACK(buf, p);
Bram Moolenaarb1cb35f2014-01-10 13:05:20 +0100556
557 /* Remove trailing '\\' except root path. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558 if (p > buf && (*p == '\\' || *p == '/') && p[-1] != ':')
559 *p = NUL;
Bram Moolenaarb1cb35f2014-01-10 13:05:20 +0100560
561 if ((buf[0] == '\\' && buf[1] == '\\') || (buf[0] == '/' && buf[1] == '/'))
562 {
563 /* UNC root path must be followed by '\\'. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100564 p = vim_strpbrk(buf + 2, (char_u *)"\\/");
Bram Moolenaarb1cb35f2014-01-10 13:05:20 +0100565 if (p != NULL)
566 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100567 p = vim_strpbrk(p + 1, (char_u *)"\\/");
Bram Moolenaarb1cb35f2014-01-10 13:05:20 +0100568 if (p == NULL)
569 STRCAT(buf, "\\");
570 }
571 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200573 wp = enc_to_utf16(buf, NULL);
574 if (wp == NULL)
575 return -1;
576
577 n = wstat_symlink_aware(wp, stp);
578 vim_free(wp);
579 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580}
581
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200582#if (defined(FEAT_GUI_MSWIN) && !defined(VIMDLL)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 void
Bram Moolenaar1266d672017-02-01 13:43:36 +0100584mch_settmode(int tmode UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585{
586 /* nothing to do */
587}
588
589 int
590mch_get_shellsize(void)
591{
592 /* never used */
593 return OK;
594}
595
596 void
597mch_set_shellsize(void)
598{
599 /* never used */
600}
601
602/*
603 * Rows and/or Columns has changed.
604 */
605 void
606mch_new_shellsize(void)
607{
608 /* never used */
609}
610
611#endif
612
613/*
614 * We have no job control, so fake it by starting a new shell.
615 */
616 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100617mch_suspend(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618{
619 suspend_shell();
620}
621
622#if defined(USE_MCH_ERRMSG) || defined(PROTO)
623
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200624# ifdef display_errors
625# undef display_errors
626# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627
628/*
629 * Display the saved error message(s).
630 */
631 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100632display_errors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633{
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200634# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635 char *p;
636
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200637# ifdef VIMDLL
638 if (gui.in_use || gui.starting)
639# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200641 if (error_ga.ga_data != NULL)
642 {
643 /* avoid putting up a message box with blanks only */
644 for (p = (char *)error_ga.ga_data; *p; ++p)
645 if (!isspace(*p))
646 {
647 (void)gui_mch_dialog(
Bram Moolenaarc17ef8e2006-03-25 21:48:58 +0000648 gui.starting ? VIM_INFO :
Bram Moolenaarc17ef8e2006-03-25 21:48:58 +0000649 VIM_ERROR,
Bram Moolenaarc17ef8e2006-03-25 21:48:58 +0000650 gui.starting ? (char_u *)_("Message") :
Bram Moolenaarc17ef8e2006-03-25 21:48:58 +0000651 (char_u *)_("Error"),
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100652 (char_u *)p, (char_u *)_("&Ok"),
653 1, NULL, FALSE);
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200654 break;
655 }
656 ga_clear(&error_ga);
657 }
658 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200660# endif
661# if !defined(FEAT_GUI) || defined(VIMDLL)
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +0100662 FlushFileBuffers(GetStdHandle(STD_ERROR_HANDLE));
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200663# endif
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +0100664}
665#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666
667
668/*
669 * Return TRUE if "p" contain a wildcard that can be expanded by
670 * dos_expandpath().
671 */
672 int
673mch_has_exp_wildcard(char_u *p)
674{
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100675 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 {
677 if (vim_strchr((char_u *)"?*[", *p) != NULL
678 || (*p == '~' && p[1] != NUL))
679 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 }
681 return FALSE;
682}
683
684/*
685 * Return TRUE if "p" contain a wildcard or a "~1" kind of thing (could be a
686 * shortened file name).
687 */
688 int
689mch_has_wildcard(char_u *p)
690{
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100691 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 {
693 if (vim_strchr((char_u *)
694# ifdef VIM_BACKTICK
695 "?*$[`"
696# else
697 "?*$["
698# endif
699 , *p) != NULL
700 || (*p == '~' && p[1] != NUL))
701 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 }
703 return FALSE;
704}
705
706
707/*
708 * The normal _chdir() does not change the default drive. This one does.
709 * Returning 0 implies success; -1 implies failure.
710 */
711 int
712mch_chdir(char *path)
713{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200714 WCHAR *p;
715 int n;
716
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 if (path[0] == NUL) /* just checking... */
718 return -1;
719
Bram Moolenaara2974d72009-07-14 16:38:36 +0000720 if (p_verbose >= 5)
721 {
722 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100723 smsg("chdir(%s)", path);
Bram Moolenaara2974d72009-07-14 16:38:36 +0000724 verbose_leave();
725 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726 if (isalpha(path[0]) && path[1] == ':') /* has a drive name */
727 {
728 /* If we can change to the drive, skip that part of the path. If we
729 * can't then the current directory may be invalid, try using chdir()
730 * with the whole path. */
731 if (_chdrive(TOLOWER_ASC(path[0]) - 'a' + 1) == 0)
732 path += 2;
733 }
734
735 if (*path == NUL) /* drive name only */
736 return 0;
737
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200738 p = enc_to_utf16((char_u *)path, NULL);
739 if (p == NULL)
740 return -1;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000741
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200742 n = _wchdir(p);
743 vim_free(p);
744 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745}
746
747
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200748#if defined(FEAT_GUI_MSWIN) && !defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749/*
750 * return non-zero if a character is available
751 */
752 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100753mch_char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754{
755 /* never used */
756 return TRUE;
757}
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200758
759# if defined(FEAT_TERMINAL) || defined(PROTO)
760/*
761 * Check for any pending input or messages.
762 */
763 int
764mch_check_messages(void)
765{
766 /* TODO: check for messages */
767 return TRUE;
768}
769# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770#endif
771
772
773/*
774 * set screen mode, always fails.
775 */
776 int
Bram Moolenaar1266d672017-02-01 13:43:36 +0100777mch_screenmode(char_u *arg UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100779 emsg(_(e_screenmode));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 return FAIL;
781}
782
783
784#if defined(FEAT_LIBCALL) || defined(PROTO)
785/*
786 * Call a DLL routine which takes either a string or int param
787 * and returns an allocated string.
788 * Return OK if it worked, FAIL if not.
789 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790typedef LPTSTR (*MYSTRPROCSTR)(LPTSTR);
791typedef LPTSTR (*MYINTPROCSTR)(int);
792typedef int (*MYSTRPROCINT)(LPTSTR);
793typedef int (*MYINTPROCINT)(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795/*
796 * Check if a pointer points to a valid NUL terminated string.
797 * Return the length of the string, including terminating NUL.
798 * Returns 0 for an invalid pointer, 1 for an empty string.
799 */
800 static size_t
801check_str_len(char_u *str)
802{
803 SYSTEM_INFO si;
804 MEMORY_BASIC_INFORMATION mbi;
805 size_t length = 0;
806 size_t i;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100807 const char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808
809 /* get page size */
810 GetSystemInfo(&si);
811
812 /* get memory information */
813 if (VirtualQuery(str, &mbi, sizeof(mbi)))
814 {
815 /* pre cast these (typing savers) */
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000816 long_u dwStr = (long_u)str;
817 long_u dwBaseAddress = (long_u)mbi.BaseAddress;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818
819 /* get start address of page that str is on */
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000820 long_u strPage = dwStr - (dwStr - dwBaseAddress) % si.dwPageSize;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821
822 /* get length from str to end of page */
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000823 long_u pageLength = si.dwPageSize - (dwStr - strPage);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824
Bram Moolenaar442b4222010-05-24 21:34:22 +0200825 for (p = str; !IsBadReadPtr(p, (UINT)pageLength);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 p += pageLength, pageLength = si.dwPageSize)
827 for (i = 0; i < pageLength; ++i, ++length)
828 if (p[i] == NUL)
829 return length + 1;
830 }
831
832 return 0;
833}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200835/*
836 * Passed to do_in_runtimepath() to load a vim.ico file.
837 */
838 static void
839mch_icon_load_cb(char_u *fname, void *cookie)
840{
841 HANDLE *h = (HANDLE *)cookie;
842
843 *h = LoadImage(NULL,
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100844 (LPSTR)fname,
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200845 IMAGE_ICON,
846 64,
847 64,
848 LR_LOADFROMFILE | LR_LOADMAP3DCOLORS);
849}
850
851/*
852 * Try loading an icon file from 'runtimepath'.
853 */
854 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100855mch_icon_load(HANDLE *iconp)
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200856{
857 return do_in_runtimepath((char_u *)"bitmaps/vim.ico",
Bram Moolenaar7f8989d2016-03-12 22:11:39 +0100858 0, mch_icon_load_cb, iconp);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200859}
860
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 int
862mch_libcall(
863 char_u *libname,
864 char_u *funcname,
865 char_u *argstring, /* NULL when using a argint */
866 int argint,
867 char_u **string_result,/* NULL when using number_result */
868 int *number_result)
869{
870 HINSTANCE hinstLib;
871 MYSTRPROCSTR ProcAdd;
872 MYINTPROCSTR ProcAddI;
873 char_u *retval_str = NULL;
874 int retval_int = 0;
875 size_t len;
876
877 BOOL fRunTimeLinkSuccess = FALSE;
878
879 // Get a handle to the DLL module.
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100880 hinstLib = vimLoadLib((char *)libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881
882 // If the handle is valid, try to get the function address.
883 if (hinstLib != NULL)
884 {
885#ifdef HAVE_TRY_EXCEPT
886 __try
887 {
888#endif
889 if (argstring != NULL)
890 {
891 /* Call with string argument */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100892 ProcAdd = (MYSTRPROCSTR)GetProcAddress(hinstLib, (LPCSTR)funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 if ((fRunTimeLinkSuccess = (ProcAdd != NULL)) != 0)
894 {
895 if (string_result == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100896 retval_int = ((MYSTRPROCINT)ProcAdd)((LPSTR)argstring);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100898 retval_str = (char_u *)(ProcAdd)((LPSTR)argstring);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 }
900 }
901 else
902 {
903 /* Call with number argument */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100904 ProcAddI = (MYINTPROCSTR) GetProcAddress(hinstLib, (LPCSTR)funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 if ((fRunTimeLinkSuccess = (ProcAddI != NULL)) != 0)
906 {
907 if (string_result == NULL)
908 retval_int = ((MYINTPROCINT)ProcAddI)(argint);
909 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100910 retval_str = (char_u *)(ProcAddI)(argint);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 }
912 }
913
914 // Save the string before we free the library.
915 // Assume that a "1" result is an illegal pointer.
916 if (string_result == NULL)
917 *number_result = retval_int;
918 else if (retval_str != NULL
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100919 && (len = check_str_len(retval_str)) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 {
921 *string_result = lalloc((long_u)len, TRUE);
922 if (*string_result != NULL)
923 mch_memmove(*string_result, retval_str, len);
924 }
925
926#ifdef HAVE_TRY_EXCEPT
927 }
928 __except(EXCEPTION_EXECUTE_HANDLER)
929 {
930 if (GetExceptionCode() == EXCEPTION_STACK_OVERFLOW)
931 RESETSTKOFLW();
932 fRunTimeLinkSuccess = 0;
933 }
934#endif
935
936 // Free the DLL module.
937 (void)FreeLibrary(hinstLib);
938 }
939
940 if (!fRunTimeLinkSuccess)
941 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100942 semsg(_(e_libcall), funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 return FAIL;
944 }
945
946 return OK;
947}
948#endif
949
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950/*
951 * Debugging helper: expose the MCH_WRITE_DUMP stuff to other modules
952 */
953 void
Bram Moolenaar1266d672017-02-01 13:43:36 +0100954DumpPutS(const char *psz UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955{
956# ifdef MCH_WRITE_DUMP
957 if (fdDump)
958 {
959 fputs(psz, fdDump);
960 if (psz[strlen(psz) - 1] != '\n')
961 fputc('\n', fdDump);
962 fflush(fdDump);
963 }
964# endif
965}
966
967#ifdef _DEBUG
968
969void __cdecl
970Trace(
971 char *pszFormat,
972 ...)
973{
974 CHAR szBuff[2048];
975 va_list args;
976
977 va_start(args, pszFormat);
978 vsprintf(szBuff, pszFormat, args);
979 va_end(args);
980
981 OutputDebugString(szBuff);
982}
983
984#endif //_DEBUG
985
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200986#if !defined(FEAT_GUI) || defined(VIMDLL) || defined(PROTO)
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200987# ifdef FEAT_TITLE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988extern HWND g_hWnd; /* This is in os_win32.c. */
989# endif
990
991/*
992 * Showing the printer dialog is tricky since we have no GUI
993 * window to parent it. The following routines are needed to
994 * get the window parenting and Z-order to work properly.
995 */
996 static void
997GetConsoleHwnd(void)
998{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 /* Skip if it's already set. */
1000 if (s_hwnd != 0)
1001 return;
1002
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001003# ifdef FEAT_TITLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 /* Window handle may have been found by init code (Windows NT only) */
1005 if (g_hWnd != 0)
1006 {
1007 s_hwnd = g_hWnd;
1008 return;
1009 }
1010# endif
1011
Bram Moolenaare1ed53f2019-02-12 23:12:37 +01001012 s_hwnd = GetConsoleWindow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013}
Bram Moolenaar843ee412004-06-30 16:16:41 +00001014
1015/*
1016 * Console implementation of ":winpos".
1017 */
1018 int
1019mch_get_winpos(int *x, int *y)
1020{
1021 RECT rect;
1022
1023 GetConsoleHwnd();
1024 GetWindowRect(s_hwnd, &rect);
1025 *x = rect.left;
1026 *y = rect.top;
1027 return OK;
1028}
1029
1030/*
1031 * Console implementation of ":winpos x y".
1032 */
1033 void
1034mch_set_winpos(int x, int y)
1035{
1036 GetConsoleHwnd();
1037 SetWindowPos(s_hwnd, NULL, x, y, 0, 0,
1038 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
1039}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040#endif
1041
1042#if (defined(FEAT_PRINTER) && !defined(FEAT_POSTSCRIPT)) || defined(PROTO)
1043
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044/*=================================================================
1045 * Win32 printer stuff
1046 */
1047
1048static HFONT prt_font_handles[2][2][2];
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001049static PRINTDLGW prt_dlg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050static const int boldface[2] = {FW_REGULAR, FW_BOLD};
1051static TEXTMETRIC prt_tm;
1052static int prt_line_height;
1053static int prt_number_width;
1054static int prt_left_margin;
1055static int prt_right_margin;
1056static int prt_top_margin;
1057static char_u szAppName[] = TEXT("VIM");
1058static HWND hDlgPrint;
1059static int *bUserAbort = NULL;
1060static char_u *prt_name = NULL;
1061
1062/* Defines which are also in vim.rc. */
1063#define IDC_BOX1 400
1064#define IDC_PRINTTEXT1 401
1065#define IDC_PRINTTEXT2 402
1066#define IDC_PROGRESS 403
1067
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001068 static BOOL
1069vimSetDlgItemText(HWND hDlg, int nIDDlgItem, char_u *s)
1070{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001071 WCHAR *wp;
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001072 BOOL ret;
1073
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001074 wp = enc_to_utf16(s, NULL);
1075 if (wp == NULL)
1076 return FALSE;
1077
1078 ret = SetDlgItemTextW(hDlg, nIDDlgItem, wp);
1079 vim_free(wp);
1080 return ret;
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001081}
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001082
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083/*
1084 * Convert BGR to RGB for Windows GDI calls
1085 */
1086 static COLORREF
1087swap_me(COLORREF colorref)
1088{
1089 int temp;
1090 char *ptr = (char *)&colorref;
1091
1092 temp = *(ptr);
1093 *(ptr ) = *(ptr + 2);
1094 *(ptr + 2) = temp;
1095 return colorref;
1096}
1097
Bram Moolenaared39e1d2008-08-09 17:55:22 +00001098/* Attempt to make this work for old and new compilers */
Bram Moolenaar9f733d12011-09-21 20:09:42 +02001099#if !defined(_WIN64) && (!defined(_MSC_VER) || _MSC_VER < 1300)
Bram Moolenaared39e1d2008-08-09 17:55:22 +00001100# define PDP_RETVAL BOOL
1101#else
1102# define PDP_RETVAL INT_PTR
1103#endif
1104
Bram Moolenaared39e1d2008-08-09 17:55:22 +00001105 static PDP_RETVAL CALLBACK
Bram Moolenaar1266d672017-02-01 13:43:36 +01001106PrintDlgProc(
1107 HWND hDlg,
1108 UINT message,
1109 WPARAM wParam UNUSED,
1110 LPARAM lParam UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111{
1112#ifdef FEAT_GETTEXT
1113 NONCLIENTMETRICS nm;
1114 static HFONT hfont;
1115#endif
1116
1117 switch (message)
1118 {
1119 case WM_INITDIALOG:
1120#ifdef FEAT_GETTEXT
1121 nm.cbSize = sizeof(NONCLIENTMETRICS);
1122 if (SystemParametersInfo(
1123 SPI_GETNONCLIENTMETRICS,
1124 sizeof(NONCLIENTMETRICS),
1125 &nm,
1126 0))
1127 {
1128 char buff[MAX_PATH];
1129 int i;
1130
1131 /* Translate the dialog texts */
1132 hfont = CreateFontIndirect(&nm.lfMessageFont);
1133 for (i = IDC_PRINTTEXT1; i <= IDC_PROGRESS; i++)
1134 {
1135 SendDlgItemMessage(hDlg, i, WM_SETFONT, (WPARAM)hfont, 1);
1136 if (GetDlgItemText(hDlg,i, buff, sizeof(buff)))
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001137 vimSetDlgItemText(hDlg,i, (char_u *)_(buff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 }
1139 SendDlgItemMessage(hDlg, IDCANCEL,
1140 WM_SETFONT, (WPARAM)hfont, 1);
1141 if (GetDlgItemText(hDlg,IDCANCEL, buff, sizeof(buff)))
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001142 vimSetDlgItemText(hDlg,IDCANCEL, (char_u *)_(buff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 }
1144#endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001145 SetWindowText(hDlg, (LPCSTR)szAppName);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 if (prt_name != NULL)
1147 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001148 vimSetDlgItemText(hDlg, IDC_PRINTTEXT2, (char_u *)prt_name);
Bram Moolenaard23a8232018-02-10 18:45:26 +01001149 VIM_CLEAR(prt_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 }
1151 EnableMenuItem(GetSystemMenu(hDlg, FALSE), SC_CLOSE, MF_GRAYED);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001152#if !defined(FEAT_GUI) || defined(VIMDLL)
1153# ifdef VIMDLL
1154 if (!gui.in_use)
1155# endif
1156 BringWindowToTop(s_hwnd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157#endif
1158 return TRUE;
1159
1160 case WM_COMMAND:
1161 *bUserAbort = TRUE;
1162 EnableWindow(GetParent(hDlg), TRUE);
1163 DestroyWindow(hDlg);
1164 hDlgPrint = NULL;
1165#ifdef FEAT_GETTEXT
1166 DeleteObject(hfont);
1167#endif
1168 return TRUE;
1169 }
1170 return FALSE;
1171}
1172
1173 static BOOL CALLBACK
Bram Moolenaar1266d672017-02-01 13:43:36 +01001174AbortProc(HDC hdcPrn UNUSED, int iCode UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175{
1176 MSG msg;
1177
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02001178 while (!*bUserAbort && pPeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 {
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02001180 if (!hDlgPrint || !pIsDialogMessage(hDlgPrint, &msg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 {
1182 TranslateMessage(&msg);
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02001183 pDispatchMessage(&msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 }
1185 }
1186 return !*bUserAbort;
1187}
1188
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001189#if !defined(FEAT_GUI) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190
Bram Moolenaar26fdd7d2011-11-30 13:42:44 +01001191 static UINT_PTR CALLBACK
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192PrintHookProc(
1193 HWND hDlg, // handle to dialog box
1194 UINT uiMsg, // message identifier
1195 WPARAM wParam, // message parameter
1196 LPARAM lParam // message parameter
1197 )
1198{
1199 HWND hwndOwner;
1200 RECT rc, rcDlg, rcOwner;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001201 PRINTDLGW *pPD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202
1203 if (uiMsg == WM_INITDIALOG)
1204 {
1205 // Get the owner window and dialog box rectangles.
1206 if ((hwndOwner = GetParent(hDlg)) == NULL)
1207 hwndOwner = GetDesktopWindow();
1208
1209 GetWindowRect(hwndOwner, &rcOwner);
1210 GetWindowRect(hDlg, &rcDlg);
1211 CopyRect(&rc, &rcOwner);
1212
1213 // Offset the owner and dialog box rectangles so that
1214 // right and bottom values represent the width and
1215 // height, and then offset the owner again to discard
1216 // space taken up by the dialog box.
1217
1218 OffsetRect(&rcDlg, -rcDlg.left, -rcDlg.top);
1219 OffsetRect(&rc, -rc.left, -rc.top);
1220 OffsetRect(&rc, -rcDlg.right, -rcDlg.bottom);
1221
1222 // The new position is the sum of half the remaining
1223 // space and the owner's original position.
1224
1225 SetWindowPos(hDlg,
1226 HWND_TOP,
1227 rcOwner.left + (rc.right / 2),
1228 rcOwner.top + (rc.bottom / 2),
1229 0, 0, // ignores size arguments
1230 SWP_NOSIZE);
1231
1232 /* tackle the printdlg copiesctrl problem */
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001233 pPD = (PRINTDLGW *)lParam;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234 pPD->nCopies = (WORD)pPD->lCustData;
1235 SetDlgItemInt( hDlg, edt3, pPD->nCopies, FALSE );
1236 /* Bring the window to top */
1237 BringWindowToTop(GetParent(hDlg));
1238 SetForegroundWindow(hDlg);
1239 }
1240
1241 return FALSE;
1242}
1243#endif
1244
1245 void
1246mch_print_cleanup(void)
1247{
1248 int pifItalic;
1249 int pifBold;
1250 int pifUnderline;
1251
1252 for (pifBold = 0; pifBold <= 1; pifBold++)
1253 for (pifItalic = 0; pifItalic <= 1; pifItalic++)
1254 for (pifUnderline = 0; pifUnderline <= 1; pifUnderline++)
1255 DeleteObject(prt_font_handles[pifBold][pifItalic][pifUnderline]);
1256
1257 if (prt_dlg.hDC != NULL)
1258 DeleteDC(prt_dlg.hDC);
1259 if (!*bUserAbort)
1260 SendMessage(hDlgPrint, WM_COMMAND, 0, 0);
1261}
1262
1263 static int
1264to_device_units(int idx, int dpi, int physsize, int offset, int def_number)
1265{
1266 int ret = 0;
1267 int u;
1268 int nr;
1269
1270 u = prt_get_unit(idx);
1271 if (u == PRT_UNIT_NONE)
1272 {
1273 u = PRT_UNIT_PERC;
1274 nr = def_number;
1275 }
1276 else
1277 nr = printer_opts[idx].number;
1278
1279 switch (u)
1280 {
1281 case PRT_UNIT_PERC:
1282 ret = (physsize * nr) / 100;
1283 break;
1284 case PRT_UNIT_INCH:
1285 ret = (nr * dpi);
1286 break;
1287 case PRT_UNIT_MM:
1288 ret = (nr * 10 * dpi) / 254;
1289 break;
1290 case PRT_UNIT_POINT:
1291 ret = (nr * 10 * dpi) / 720;
1292 break;
1293 }
1294
1295 if (ret < offset)
1296 return 0;
1297 else
1298 return ret - offset;
1299}
1300
1301 static int
1302prt_get_cpl(void)
1303{
1304 int hr;
1305 int phyw;
1306 int dvoff;
1307 int rev_offset;
1308 int dpi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309
1310 GetTextMetrics(prt_dlg.hDC, &prt_tm);
1311 prt_line_height = prt_tm.tmHeight + prt_tm.tmExternalLeading;
1312
1313 hr = GetDeviceCaps(prt_dlg.hDC, HORZRES);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 phyw = GetDeviceCaps(prt_dlg.hDC, PHYSICALWIDTH);
1315 dvoff = GetDeviceCaps(prt_dlg.hDC, PHYSICALOFFSETX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 dpi = GetDeviceCaps(prt_dlg.hDC, LOGPIXELSX);
1317
1318 rev_offset = phyw - (dvoff + hr);
1319
1320 prt_left_margin = to_device_units(OPT_PRINT_LEFT, dpi, phyw, dvoff, 10);
1321 if (prt_use_number())
1322 {
1323 prt_number_width = PRINT_NUMBER_WIDTH * prt_tm.tmAveCharWidth;
1324 prt_left_margin += prt_number_width;
1325 }
1326 else
1327 prt_number_width = 0;
1328
1329 prt_right_margin = hr - to_device_units(OPT_PRINT_RIGHT, dpi, phyw,
1330 rev_offset, 5);
1331
1332 return (prt_right_margin - prt_left_margin) / prt_tm.tmAveCharWidth;
1333}
1334
1335 static int
1336prt_get_lpp(void)
1337{
1338 int vr;
1339 int phyw;
1340 int dvoff;
1341 int rev_offset;
1342 int bottom_margin;
1343 int dpi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344
1345 vr = GetDeviceCaps(prt_dlg.hDC, VERTRES);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 phyw = GetDeviceCaps(prt_dlg.hDC, PHYSICALHEIGHT);
1347 dvoff = GetDeviceCaps(prt_dlg.hDC, PHYSICALOFFSETY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 dpi = GetDeviceCaps(prt_dlg.hDC, LOGPIXELSY);
1349
1350 rev_offset = phyw - (dvoff + vr);
1351
1352 prt_top_margin = to_device_units(OPT_PRINT_TOP, dpi, phyw, dvoff, 5);
1353
1354 /* adjust top margin if there is a header */
1355 prt_top_margin += prt_line_height * prt_header_height();
1356
1357 bottom_margin = vr - to_device_units(OPT_PRINT_BOT, dpi, phyw,
1358 rev_offset, 5);
1359
1360 return (bottom_margin - prt_top_margin) / prt_line_height;
1361}
1362
1363 int
1364mch_print_init(prt_settings_T *psettings, char_u *jobname, int forceit)
1365{
1366 static HGLOBAL stored_dm = NULL;
1367 static HGLOBAL stored_devn = NULL;
1368 static int stored_nCopies = 1;
1369 static int stored_nFlags = 0;
1370
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001371 LOGFONTW fLogFont;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 int pifItalic;
1373 int pifBold;
1374 int pifUnderline;
1375
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001376 DEVMODEW *mem;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 DEVNAMES *devname;
1378 int i;
1379
1380 bUserAbort = &(psettings->user_abort);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001381 vim_memset(&prt_dlg, 0, sizeof(PRINTDLGW));
1382 prt_dlg.lStructSize = sizeof(PRINTDLGW);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001383#if !defined(FEAT_GUI) || defined(VIMDLL)
1384# ifdef VIMDLL
1385 if (!gui.in_use)
1386# endif
1387 GetConsoleHwnd(); /* get value of s_hwnd */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388#endif
1389 prt_dlg.hwndOwner = s_hwnd;
1390 prt_dlg.Flags = PD_NOPAGENUMS | PD_NOSELECTION | PD_RETURNDC;
1391 if (!forceit)
1392 {
1393 prt_dlg.hDevMode = stored_dm;
1394 prt_dlg.hDevNames = stored_devn;
1395 prt_dlg.lCustData = stored_nCopies; // work around bug in print dialog
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001396#if !defined(FEAT_GUI) || defined(VIMDLL)
1397# ifdef VIMDLL
1398 if (!gui.in_use)
1399# endif
1400 {
1401 /*
1402 * Use hook to prevent console window being sent to back
1403 */
1404 prt_dlg.lpfnPrintHook = PrintHookProc;
1405 prt_dlg.Flags |= PD_ENABLEPRINTHOOK;
1406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407#endif
1408 prt_dlg.Flags |= stored_nFlags;
1409 }
1410
1411 /*
1412 * If bang present, return default printer setup with no dialog
1413 * never show dialog if we are running over telnet
1414 */
1415 if (forceit
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001416#if !defined(FEAT_GUI) || defined(VIMDLL)
1417# ifdef VIMDLL
1418 || (!gui.in_use && !term_console)
1419# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 || !term_console
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001421# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422#endif
1423 )
1424 {
1425 prt_dlg.Flags |= PD_RETURNDEFAULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 /*
1427 * MSDN suggests setting the first parameter to WINSPOOL for
1428 * NT, but NULL appears to work just as well.
1429 */
1430 if (*p_pdev != NUL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001431 prt_dlg.hDC = CreateDC(NULL, (LPCSTR)p_pdev, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 {
1434 prt_dlg.Flags |= PD_RETURNDEFAULT;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001435 if (PrintDlgW(&prt_dlg) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 goto init_fail_dlg;
1437 }
1438 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001439 else if (PrintDlgW(&prt_dlg) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 goto init_fail_dlg;
1441 else
1442 {
1443 /*
1444 * keep the previous driver context
1445 */
1446 stored_dm = prt_dlg.hDevMode;
1447 stored_devn = prt_dlg.hDevNames;
1448 stored_nFlags = prt_dlg.Flags;
1449 stored_nCopies = prt_dlg.nCopies;
1450 }
1451
1452 if (prt_dlg.hDC == NULL)
1453 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001454 emsg(_("E237: Printer selection failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 mch_print_cleanup();
1456 return FALSE;
1457 }
1458
1459 /* Not all printer drivers report the support of color (or grey) in the
1460 * same way. Let's set has_color if there appears to be some way to print
1461 * more than B&W. */
1462 i = GetDeviceCaps(prt_dlg.hDC, NUMCOLORS);
1463 psettings->has_color = (GetDeviceCaps(prt_dlg.hDC, BITSPIXEL) > 1
1464 || GetDeviceCaps(prt_dlg.hDC, PLANES) > 1
1465 || i > 2 || i == -1);
1466
1467 /* Ensure all font styles are baseline aligned */
1468 SetTextAlign(prt_dlg.hDC, TA_BASELINE|TA_LEFT);
1469
1470 /*
1471 * On some windows systems the nCopies parameter is not
1472 * passed back correctly. It must be retrieved from the
1473 * hDevMode struct.
1474 */
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001475 mem = (DEVMODEW *)GlobalLock(prt_dlg.hDevMode);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 if (mem != NULL)
1477 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478 if (mem->dmCopies != 1)
1479 stored_nCopies = mem->dmCopies;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 if ((mem->dmFields & DM_DUPLEX) && (mem->dmDuplex & ~DMDUP_SIMPLEX))
1481 psettings->duplex = TRUE;
1482 if ((mem->dmFields & DM_COLOR) && (mem->dmColor & DMCOLOR_COLOR))
1483 psettings->has_color = TRUE;
1484 }
1485 GlobalUnlock(prt_dlg.hDevMode);
1486
1487 devname = (DEVNAMES *)GlobalLock(prt_dlg.hDevNames);
1488 if (devname != 0)
1489 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001490 WCHAR *wprinter_name = (WCHAR *)devname + devname->wDeviceOffset;
1491 WCHAR *wport_name = (WCHAR *)devname + devname->wOutputOffset;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001492 char_u *text = (char_u *)_("to %s on %s");
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001493 char_u *printer_name = utf16_to_enc(wprinter_name, NULL);
1494 char_u *port_name = utf16_to_enc(wport_name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001496 if (printer_name != NULL && port_name != NULL)
1497 prt_name = alloc((unsigned)(STRLEN(printer_name)
1498 + STRLEN(port_name) + STRLEN(text)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 if (prt_name != NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001500 wsprintf((char *)prt_name, (const char *)text,
1501 printer_name, port_name);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001502 vim_free(printer_name);
1503 vim_free(port_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504 }
1505 GlobalUnlock(prt_dlg.hDevNames);
1506
1507 /*
1508 * Initialise the font according to 'printfont'
1509 */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02001510 vim_memset(&fLogFont, 0, sizeof(fLogFont));
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001511 if (get_logfont(&fLogFont, p_pfn, prt_dlg.hDC, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001513 semsg(_("E613: Unknown printer font: %s"), p_pfn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 mch_print_cleanup();
1515 return FALSE;
1516 }
1517
1518 for (pifBold = 0; pifBold <= 1; pifBold++)
1519 for (pifItalic = 0; pifItalic <= 1; pifItalic++)
1520 for (pifUnderline = 0; pifUnderline <= 1; pifUnderline++)
1521 {
1522 fLogFont.lfWeight = boldface[pifBold];
1523 fLogFont.lfItalic = pifItalic;
1524 fLogFont.lfUnderline = pifUnderline;
1525 prt_font_handles[pifBold][pifItalic][pifUnderline]
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001526 = CreateFontIndirectW(&fLogFont);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 }
1528
1529 SetBkMode(prt_dlg.hDC, OPAQUE);
1530 SelectObject(prt_dlg.hDC, prt_font_handles[0][0][0]);
1531
1532 /*
1533 * Fill in the settings struct
1534 */
1535 psettings->chars_per_line = prt_get_cpl();
1536 psettings->lines_per_page = prt_get_lpp();
Bram Moolenaar7ddc6422014-09-27 11:18:19 +02001537 if (prt_dlg.Flags & PD_USEDEVMODECOPIESANDCOLLATE)
1538 {
1539 psettings->n_collated_copies = (prt_dlg.Flags & PD_COLLATE)
1540 ? prt_dlg.nCopies : 1;
1541 psettings->n_uncollated_copies = (prt_dlg.Flags & PD_COLLATE)
1542 ? 1 : prt_dlg.nCopies;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543
Bram Moolenaar7ddc6422014-09-27 11:18:19 +02001544 if (psettings->n_collated_copies == 0)
1545 psettings->n_collated_copies = 1;
1546
1547 if (psettings->n_uncollated_copies == 0)
1548 psettings->n_uncollated_copies = 1;
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01001549 }
1550 else
1551 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 psettings->n_collated_copies = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 psettings->n_uncollated_copies = 1;
Bram Moolenaar7ddc6422014-09-27 11:18:19 +02001554 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555
1556 psettings->jobname = jobname;
1557
1558 return TRUE;
1559
1560init_fail_dlg:
1561 {
1562 DWORD err = CommDlgExtendedError();
1563
1564 if (err)
1565 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 char_u *buf;
1567
1568 /* I suspect FormatMessage() doesn't work for values returned by
1569 * CommDlgExtendedError(). What does? */
1570 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
1571 FORMAT_MESSAGE_FROM_SYSTEM |
1572 FORMAT_MESSAGE_IGNORE_INSERTS,
1573 NULL, err, 0, (LPTSTR)(&buf), 0, NULL);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001574 semsg(_("E238: Print error: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 buf == NULL ? (char_u *)_("Unknown") : buf);
1576 LocalFree((LPVOID)(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 }
1578 else
1579 msg_clr_eos(); /* Maybe canceled */
1580
1581 mch_print_cleanup();
1582 return FALSE;
1583 }
1584}
1585
1586
1587 int
1588mch_print_begin(prt_settings_T *psettings)
1589{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001590 int ret = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 char szBuffer[300];
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001592 WCHAR *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001594 hDlgPrint = CreateDialog(g_hinst, TEXT("PrintDlgBox"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 prt_dlg.hwndOwner, PrintDlgProc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 SetAbortProc(prt_dlg.hDC, AbortProc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 wsprintf(szBuffer, _("Printing '%s'"), gettail(psettings->jobname));
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001598 vimSetDlgItemText(hDlgPrint, IDC_PRINTTEXT1, (char_u *)szBuffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001600 wp = enc_to_utf16(psettings->jobname, NULL);
Bram Moolenaar2290b1f2018-05-13 17:30:45 +02001601 if (wp != NULL)
1602 {
1603 DOCINFOW di;
1604
1605 vim_memset(&di, 0, sizeof(di));
1606 di.cbSize = sizeof(di);
1607 di.lpszDocName = wp;
1608 ret = StartDocW(prt_dlg.hDC, &di);
1609 vim_free(wp);
1610 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611
1612#ifdef FEAT_GUI
1613 /* Give focus back to main window (when using MDI). */
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001614# ifdef VIMDLL
1615 if (gui.in_use)
1616# endif
1617 SetFocus(s_hwnd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618#endif
1619
1620 return (ret > 0);
1621}
1622
1623 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01001624mch_print_end(prt_settings_T *psettings UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625{
1626 EndDoc(prt_dlg.hDC);
1627 if (!*bUserAbort)
1628 SendMessage(hDlgPrint, WM_COMMAND, 0, 0);
1629}
1630
1631 int
1632mch_print_end_page(void)
1633{
1634 return (EndPage(prt_dlg.hDC) > 0);
1635}
1636
1637 int
1638mch_print_begin_page(char_u *msg)
1639{
1640 if (msg != NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001641 vimSetDlgItemText(hDlgPrint, IDC_PROGRESS, msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642 return (StartPage(prt_dlg.hDC) > 0);
1643}
1644
1645 int
1646mch_print_blank_page(void)
1647{
1648 return (mch_print_begin_page(NULL) ? (mch_print_end_page()) : FALSE);
1649}
1650
1651static int prt_pos_x = 0;
1652static int prt_pos_y = 0;
1653
1654 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001655mch_print_start_line(int margin, int page_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656{
1657 if (margin)
1658 prt_pos_x = -prt_number_width;
1659 else
1660 prt_pos_x = 0;
1661 prt_pos_y = page_line * prt_line_height
1662 + prt_tm.tmAscent + prt_tm.tmExternalLeading;
1663}
1664
1665 int
1666mch_print_text_out(char_u *p, int len)
1667{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 SIZE sz;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001669 WCHAR *wp;
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001670 int wlen = len;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001671 int ret = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001673 wp = enc_to_utf16(p, &wlen);
1674 if (wp == NULL)
1675 return FALSE;
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001676
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001677 TextOutW(prt_dlg.hDC, prt_pos_x + prt_left_margin,
1678 prt_pos_y + prt_top_margin, wp, wlen);
1679 GetTextExtentPoint32W(prt_dlg.hDC, wp, wlen, &sz);
1680 vim_free(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 prt_pos_x += (sz.cx - prt_tm.tmOverhang);
1682 /* This is wrong when printing spaces for a TAB. */
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001683 if (p[len] != NUL)
1684 {
1685 wlen = MB_PTR2LEN(p + len);
1686 wp = enc_to_utf16(p + len, &wlen);
1687 if (wp != NULL)
1688 {
1689 GetTextExtentPoint32W(prt_dlg.hDC, wp, 1, &sz);
1690 ret = (prt_pos_x + prt_left_margin + sz.cx > prt_right_margin);
1691 vim_free(wp);
1692 }
1693 }
1694 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695}
1696
1697 void
1698mch_print_set_font(int iBold, int iItalic, int iUnderline)
1699{
1700 SelectObject(prt_dlg.hDC, prt_font_handles[iBold][iItalic][iUnderline]);
1701}
1702
1703 void
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001704mch_print_set_bg(long_u bgcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705{
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001706 SetBkColor(prt_dlg.hDC, GetNearestColor(prt_dlg.hDC,
1707 swap_me((COLORREF)bgcol)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 /*
1709 * With a white background we can draw characters transparent, which is
1710 * good for italic characters that overlap to the next char cell.
1711 */
1712 if (bgcol == 0xffffffUL)
1713 SetBkMode(prt_dlg.hDC, TRANSPARENT);
1714 else
1715 SetBkMode(prt_dlg.hDC, OPAQUE);
1716}
1717
1718 void
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001719mch_print_set_fg(long_u fgcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720{
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001721 SetTextColor(prt_dlg.hDC, GetNearestColor(prt_dlg.hDC,
1722 swap_me((COLORREF)fgcol)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723}
1724
1725#endif /*FEAT_PRINTER && !FEAT_POSTSCRIPT*/
1726
Bram Moolenaar58d98232005-07-23 22:25:46 +00001727
1728
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729#if defined(FEAT_SHORTCUT) || defined(PROTO)
Bram Moolenaar82881492012-11-20 16:53:39 +01001730# ifndef PROTO
1731# include <shlobj.h>
1732# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733
Bram Moolenaardce1e892019-02-10 23:18:53 +01001734typedef enum _FILE_INFO_BY_HANDLE_CLASS_ {
1735 FileBasicInfo_,
1736 FileStandardInfo_,
1737 FileNameInfo_,
1738 FileRenameInfo_,
1739 FileDispositionInfo_,
1740 FileAllocationInfo_,
1741 FileEndOfFileInfo_,
1742 FileStreamInfo_,
1743 FileCompressionInfo_,
1744 FileAttributeTagInfo_,
1745 FileIdBothDirectoryInfo_,
1746 FileIdBothDirectoryRestartInfo_,
1747 FileIoPriorityHintInfo_,
1748 FileRemoteProtocolInfo_,
1749 FileFullDirectoryInfo_,
1750 FileFullDirectoryRestartInfo_,
1751 FileStorageInfo_,
1752 FileAlignmentInfo_,
1753 FileIdInfo_,
1754 FileIdExtdDirectoryInfo_,
1755 FileIdExtdDirectoryRestartInfo_,
1756 FileDispositionInfoEx_,
1757 FileRenameInfoEx_,
1758 MaximumFileInfoByHandleClass_
1759} FILE_INFO_BY_HANDLE_CLASS_;
1760
1761typedef struct _FILE_NAME_INFO_ {
1762 DWORD FileNameLength;
1763 WCHAR FileName[1];
1764} FILE_NAME_INFO_;
1765
1766typedef BOOL (WINAPI *pfnGetFileInformationByHandleEx)(
1767 HANDLE hFile,
1768 FILE_INFO_BY_HANDLE_CLASS_ FileInformationClass,
1769 LPVOID lpFileInformation,
1770 DWORD dwBufferSize);
1771static pfnGetFileInformationByHandleEx pGetFileInformationByHandleEx = NULL;
1772
1773typedef BOOL (WINAPI *pfnGetVolumeInformationByHandleW)(
1774 HANDLE hFile,
1775 LPWSTR lpVolumeNameBuffer,
1776 DWORD nVolumeNameSize,
1777 LPDWORD lpVolumeSerialNumber,
1778 LPDWORD lpMaximumComponentLength,
1779 LPDWORD lpFileSystemFlags,
1780 LPWSTR lpFileSystemNameBuffer,
1781 DWORD nFileSystemNameSize);
1782static pfnGetVolumeInformationByHandleW pGetVolumeInformationByHandleW = NULL;
1783
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02001784 static char_u *
Bram Moolenaardce1e892019-02-10 23:18:53 +01001785resolve_reparse_point(char_u *fname)
1786{
1787 HANDLE h = INVALID_HANDLE_VALUE;
1788 DWORD size;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001789 WCHAR *p;
Bram Moolenaardce1e892019-02-10 23:18:53 +01001790 char_u *rfname = NULL;
1791 FILE_NAME_INFO_ *nameinfo = NULL;
1792 WCHAR buff[MAX_PATH], *volnames = NULL;
1793 HANDLE hv;
1794 DWORD snfile, snfind;
1795 static BOOL loaded = FALSE;
1796
1797 if (pGetFileInformationByHandleEx == NULL ||
1798 pGetVolumeInformationByHandleW == NULL)
1799 {
1800 HMODULE hmod = GetModuleHandle("kernel32.dll");
1801
1802 if (loaded == TRUE)
1803 return NULL;
1804 pGetFileInformationByHandleEx = (pfnGetFileInformationByHandleEx)
1805 GetProcAddress(hmod, "GetFileInformationByHandleEx");
1806 pGetVolumeInformationByHandleW = (pfnGetVolumeInformationByHandleW)
1807 GetProcAddress(hmod, "GetVolumeInformationByHandleW");
1808 loaded = TRUE;
1809 if (pGetFileInformationByHandleEx == NULL ||
1810 pGetVolumeInformationByHandleW == NULL)
1811 return NULL;
1812 }
1813
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001814 p = enc_to_utf16(fname, NULL);
1815 if (p == NULL)
1816 goto fail;
1817
1818 if ((GetFileAttributesW(p) & FILE_ATTRIBUTE_REPARSE_POINT) == 0)
Bram Moolenaardce1e892019-02-10 23:18:53 +01001819 {
Bram Moolenaardce1e892019-02-10 23:18:53 +01001820 vim_free(p);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001821 goto fail;
Bram Moolenaardce1e892019-02-10 23:18:53 +01001822 }
Bram Moolenaardce1e892019-02-10 23:18:53 +01001823
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001824 h = CreateFileW(p, 0, 0, NULL, OPEN_EXISTING,
1825 FILE_FLAG_BACKUP_SEMANTICS, NULL);
1826 vim_free(p);
Bram Moolenaardce1e892019-02-10 23:18:53 +01001827
1828 if (h == INVALID_HANDLE_VALUE)
1829 goto fail;
1830
1831 size = sizeof(FILE_NAME_INFO_) + sizeof(WCHAR) * (MAX_PATH - 1);
1832 nameinfo = (FILE_NAME_INFO_*)alloc(size + sizeof(WCHAR));
1833 if (nameinfo == NULL)
1834 goto fail;
1835
1836 if (!pGetFileInformationByHandleEx(h, FileNameInfo_, nameinfo, size))
1837 goto fail;
1838
1839 nameinfo->FileName[nameinfo->FileNameLength / sizeof(WCHAR)] = 0;
1840
1841 if (!pGetVolumeInformationByHandleW(
1842 h, NULL, 0, &snfile, NULL, NULL, NULL, 0))
1843 goto fail;
1844
1845 hv = FindFirstVolumeW(buff, MAX_PATH);
1846 if (hv == INVALID_HANDLE_VALUE)
1847 goto fail;
1848
1849 do {
1850 GetVolumeInformationW(
1851 buff, NULL, 0, &snfind, NULL, NULL, NULL, 0);
1852 if (snfind == snfile)
1853 break;
1854 } while (FindNextVolumeW(hv, buff, MAX_PATH));
1855
1856 FindVolumeClose(hv);
1857
1858 if (snfind != snfile)
1859 goto fail;
1860
1861 size = 0;
1862 if (!GetVolumePathNamesForVolumeNameW(buff, NULL, 0, &size) &&
1863 GetLastError() != ERROR_MORE_DATA)
1864 goto fail;
1865
1866 volnames = (WCHAR*)alloc(size * sizeof(WCHAR));
1867 if (!GetVolumePathNamesForVolumeNameW(buff, volnames, size,
1868 &size))
1869 goto fail;
1870
1871 wcscpy(buff, volnames);
1872 if (nameinfo->FileName[0] == '\\')
1873 wcscat(buff, nameinfo->FileName + 1);
1874 else
1875 wcscat(buff, nameinfo->FileName);
1876 rfname = utf16_to_enc(buff, NULL);
1877
1878fail:
1879 if (h != INVALID_HANDLE_VALUE)
1880 CloseHandle(h);
1881 if (nameinfo != NULL)
1882 vim_free(nameinfo);
1883 if (volnames != NULL)
1884 vim_free(volnames);
1885
1886 return rfname;
1887}
1888
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889/*
1890 * When "fname" is the name of a shortcut (*.lnk) resolve the file it points
1891 * to and return that name in allocated memory.
1892 * Otherwise NULL is returned.
1893 */
Bram Moolenaardce1e892019-02-10 23:18:53 +01001894 static char_u *
1895resolve_shortcut(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896{
1897 HRESULT hr;
1898 IShellLink *psl = NULL;
1899 IPersistFile *ppf = NULL;
1900 OLECHAR wsz[MAX_PATH];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 char_u *rfname = NULL;
1902 int len;
Bram Moolenaar604729e2013-08-30 16:44:19 +02001903 IShellLinkW *pslw = NULL;
1904 WIN32_FIND_DATAW ffdw; // we get those free of charge
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905
1906 /* Check if the file name ends in ".lnk". Avoid calling
1907 * CoCreateInstance(), it's quite slow. */
1908 if (fname == NULL)
1909 return rfname;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001910 len = (int)STRLEN(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 if (len <= 4 || STRNICMP(fname + len - 4, ".lnk", 4) != 0)
1912 return rfname;
1913
1914 CoInitialize(NULL);
1915
1916 // create a link manager object and request its interface
1917 hr = CoCreateInstance(
1918 &CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001919 &IID_IShellLinkW, (void**)&pslw);
1920 if (hr == S_OK)
1921 {
1922 WCHAR *p = enc_to_utf16(fname, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001924 if (p != NULL)
1925 {
1926 // Get a pointer to the IPersistFile interface.
1927 hr = pslw->lpVtbl->QueryInterface(
1928 pslw, &IID_IPersistFile, (void**)&ppf);
1929 if (hr != S_OK)
1930 goto shortcut_errorw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001932 // "load" the name and resolve the link
1933 hr = ppf->lpVtbl->Load(ppf, p, STGM_READ);
1934 if (hr != S_OK)
1935 goto shortcut_errorw;
1936# if 0 // This makes Vim wait a long time if the target does not exist.
1937 hr = pslw->lpVtbl->Resolve(pslw, NULL, SLR_NO_UI);
1938 if (hr != S_OK)
1939 goto shortcut_errorw;
Bram Moolenaar604729e2013-08-30 16:44:19 +02001940# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001942 // Get the path to the link target.
1943 ZeroMemory(wsz, MAX_PATH * sizeof(WCHAR));
1944 hr = pslw->lpVtbl->GetPath(pslw, wsz, MAX_PATH, &ffdw, 0);
1945 if (hr == S_OK && wsz[0] != NUL)
1946 rfname = utf16_to_enc(wsz, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001948shortcut_errorw:
1949 vim_free(p);
1950 }
1951 }
1952
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 // Release all interface pointers (both belong to the same object)
1954 if (ppf != NULL)
1955 ppf->lpVtbl->Release(ppf);
1956 if (psl != NULL)
1957 psl->lpVtbl->Release(psl);
Bram Moolenaar604729e2013-08-30 16:44:19 +02001958 if (pslw != NULL)
1959 pslw->lpVtbl->Release(pslw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960
1961 CoUninitialize();
1962 return rfname;
1963}
Bram Moolenaardce1e892019-02-10 23:18:53 +01001964
1965 char_u *
1966mch_resolve_path(char_u *fname, int reparse_point)
1967{
1968 char_u *path = resolve_shortcut(fname);
1969
1970 if (path == NULL && reparse_point)
1971 path = resolve_reparse_point(fname);
1972 return path;
1973}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974#endif
1975
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001976#if (defined(FEAT_EVAL) && (!defined(FEAT_GUI) || defined(VIMDLL))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977/*
1978 * Bring ourselves to the foreground. Does work if the OS doesn't allow it.
1979 */
1980 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001981win32_set_foreground(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 GetConsoleHwnd(); /* get value of s_hwnd */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 if (s_hwnd != 0)
1985 SetForegroundWindow(s_hwnd);
1986}
1987#endif
1988
1989#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
1990/*
1991 * Client-server code for Vim
1992 *
1993 * Originally written by Paul Moore
1994 */
1995
1996/* In order to handle inter-process messages, we need to have a window. But
1997 * the functions in this module can be called before the main GUI window is
1998 * created (and may also be called in the console version, where there is no
1999 * GUI window at all).
2000 *
2001 * So we create a hidden window, and arrange to destroy it on exit.
2002 */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002003HWND message_window = 0; /* window that's handling messages */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004
2005#define VIM_CLASSNAME "VIM_MESSAGES"
2006#define VIM_CLASSNAME_LEN (sizeof(VIM_CLASSNAME) - 1)
2007
2008/* Communication is via WM_COPYDATA messages. The message type is send in
2009 * the dwData parameter. Types are defined here. */
2010#define COPYDATA_KEYS 0
2011#define COPYDATA_REPLY 1
2012#define COPYDATA_EXPR 10
2013#define COPYDATA_RESULT 11
2014#define COPYDATA_ERROR_RESULT 12
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002015#define COPYDATA_ENCODING 20
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016
2017/* This is a structure containing a server HWND and its name. */
2018struct server_id
2019{
2020 HWND hwnd;
2021 char_u *name;
2022};
2023
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002024/* Last received 'encoding' that the client uses. */
2025static char_u *client_enc = NULL;
2026
2027/*
2028 * Tell the other side what encoding we are using.
2029 * Errors are ignored.
2030 */
2031 static void
2032serverSendEnc(HWND target)
2033{
2034 COPYDATASTRUCT data;
2035
2036 data.dwData = COPYDATA_ENCODING;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002037 data.cbData = (DWORD)STRLEN(p_enc) + 1;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002038 data.lpData = p_enc;
2039 (void)SendMessage(target, WM_COPYDATA, (WPARAM)message_window,
2040 (LPARAM)(&data));
2041}
2042
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043/*
2044 * Clean up on exit. This destroys the hidden message window.
2045 */
2046 static void
2047#ifdef __BORLANDC__
2048 _RTLENTRYF
2049#endif
2050CleanUpMessaging(void)
2051{
2052 if (message_window != 0)
2053 {
2054 DestroyWindow(message_window);
2055 message_window = 0;
2056 }
2057}
2058
2059static int save_reply(HWND server, char_u *reply, int expr);
2060
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002061/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 * The window procedure for the hidden message window.
2063 * It handles callback messages and notifications from servers.
2064 * In order to process these messages, it is necessary to run a
2065 * message loop. Code which may run before the main message loop
2066 * is started (in the GUI) is careful to pump messages when it needs
2067 * to. Features which require message delivery during normal use will
2068 * not work in the console version - this basically means those
2069 * features which allow Vim to act as a server, rather than a client.
2070 */
2071 static LRESULT CALLBACK
2072Messaging_WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
2073{
2074 if (msg == WM_COPYDATA)
2075 {
2076 /* This is a message from another Vim. The dwData member of the
2077 * COPYDATASTRUCT determines the type of message:
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002078 * COPYDATA_ENCODING:
2079 * The encoding that the client uses. Following messages will
2080 * use this encoding, convert if needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 * COPYDATA_KEYS:
2082 * A key sequence. We are a server, and a client wants these keys
2083 * adding to the input queue.
2084 * COPYDATA_REPLY:
2085 * A reply. We are a client, and a server has sent this message
2086 * in response to a request. (server2client())
2087 * COPYDATA_EXPR:
2088 * An expression. We are a server, and a client wants us to
2089 * evaluate this expression.
2090 * COPYDATA_RESULT:
2091 * A reply. We are a client, and a server has sent this message
2092 * in response to a COPYDATA_EXPR.
2093 * COPYDATA_ERROR_RESULT:
2094 * A reply. We are a client, and a server has sent this message
2095 * in response to a COPYDATA_EXPR that failed to evaluate.
2096 */
2097 COPYDATASTRUCT *data = (COPYDATASTRUCT*)lParam;
2098 HWND sender = (HWND)wParam;
2099 COPYDATASTRUCT reply;
2100 char_u *res;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 int retval;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002102 char_u *str;
2103 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104
2105 switch (data->dwData)
2106 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002107 case COPYDATA_ENCODING:
2108 /* Remember the encoding that the client uses. */
2109 vim_free(client_enc);
2110 client_enc = enc_canonize((char_u *)data->lpData);
2111 return 1;
2112
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 case COPYDATA_KEYS:
2114 /* Remember who sent this, for <client> */
2115 clientWindow = sender;
2116
2117 /* Add the received keys to the input buffer. The loop waiting
2118 * for the user to do something should check the input buffer. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002119 str = serverConvert(client_enc, (char_u *)data->lpData, &tofree);
2120 server_to_input_buf(str);
2121 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122
2123# ifdef FEAT_GUI
2124 /* Wake up the main GUI loop. */
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002125# ifdef VIMDLL
2126 if (gui.in_use)
2127# endif
2128 if (s_hwnd != 0)
2129 PostMessage(s_hwnd, WM_NULL, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130# endif
2131 return 1;
2132
2133 case COPYDATA_EXPR:
2134 /* Remember who sent this, for <client> */
2135 clientWindow = sender;
2136
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002137 str = serverConvert(client_enc, (char_u *)data->lpData, &tofree);
2138 res = eval_client_expr_to_string(str);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002139
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 if (res == NULL)
2141 {
Bram Moolenaar15bf76d2017-03-18 16:18:37 +01002142 char *err = _(e_invexprmsg);
2143 size_t len = STRLEN(str) + STRLEN(err) + 5;
2144
Bram Moolenaar1662ce12017-03-19 21:47:50 +01002145 res = alloc((unsigned)len);
Bram Moolenaar15bf76d2017-03-18 16:18:37 +01002146 if (res != NULL)
2147 vim_snprintf((char *)res, len, "%s: \"%s\"", err, str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 reply.dwData = COPYDATA_ERROR_RESULT;
2149 }
2150 else
2151 reply.dwData = COPYDATA_RESULT;
2152 reply.lpData = res;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002153 reply.cbData = (DWORD)STRLEN(res) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002155 serverSendEnc(sender);
Bram Moolenaar5246cd72013-06-16 16:41:47 +02002156 retval = (int)SendMessage(sender, WM_COPYDATA,
2157 (WPARAM)message_window, (LPARAM)(&reply));
Bram Moolenaar15bf76d2017-03-18 16:18:37 +01002158 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 vim_free(res);
2160 return retval;
2161
2162 case COPYDATA_REPLY:
2163 case COPYDATA_RESULT:
2164 case COPYDATA_ERROR_RESULT:
2165 if (data->lpData != NULL)
2166 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002167 str = serverConvert(client_enc, (char_u *)data->lpData,
2168 &tofree);
2169 if (tofree == NULL)
2170 str = vim_strsave(str);
2171 if (save_reply(sender, str,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 (data->dwData == COPYDATA_REPLY ? 0 :
2173 (data->dwData == COPYDATA_RESULT ? 1 :
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002174 2))) == FAIL)
2175 vim_free(str);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002176 else if (data->dwData == COPYDATA_REPLY)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 {
Bram Moolenaar427d51c2013-06-16 16:01:25 +02002178 char_u winstr[30];
2179
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002180 sprintf((char *)winstr, PRINTF_HEX_LONG_U, (long_u)sender);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002181 apply_autocmds(EVENT_REMOTEREPLY, winstr, str,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 TRUE, curbuf);
2183 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 }
2185 return 1;
2186 }
2187
2188 return 0;
2189 }
2190
2191 else if (msg == WM_ACTIVATE && wParam == WA_ACTIVE)
2192 {
2193 /* When the message window is activated (brought to the foreground),
2194 * this actually applies to the text window. */
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002195#if !defined(FEAT_GUI) || defined(VIMDLL)
2196# ifdef VIMDLL
2197 if (!gui.in_use)
2198# endif
2199 GetConsoleHwnd(); /* get value of s_hwnd */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200#endif
2201 if (s_hwnd != 0)
2202 {
2203 SetForegroundWindow(s_hwnd);
2204 return 0;
2205 }
2206 }
2207
2208 return DefWindowProc(hwnd, msg, wParam, lParam);
2209}
2210
2211/*
2212 * Initialise the message handling process. This involves creating a window
2213 * to handle messages - the window will not be visible.
2214 */
2215 void
2216serverInitMessaging(void)
2217{
2218 WNDCLASS wndclass;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219
2220 /* Clean up on exit */
2221 atexit(CleanUpMessaging);
2222
2223 /* Register a window class - we only really care
2224 * about the window procedure
2225 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 wndclass.style = 0;
2227 wndclass.lpfnWndProc = Messaging_WndProc;
2228 wndclass.cbClsExtra = 0;
2229 wndclass.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002230 wndclass.hInstance = g_hinst;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 wndclass.hIcon = NULL;
2232 wndclass.hCursor = NULL;
2233 wndclass.hbrBackground = NULL;
2234 wndclass.lpszMenuName = NULL;
2235 wndclass.lpszClassName = VIM_CLASSNAME;
2236 RegisterClass(&wndclass);
2237
2238 /* Create the message window. It will be hidden, so the details don't
2239 * matter. Don't use WS_OVERLAPPEDWINDOW, it will make a shortcut remove
2240 * focus from gvim. */
2241 message_window = CreateWindow(VIM_CLASSNAME, "",
2242 WS_POPUPWINDOW | WS_CAPTION,
2243 CW_USEDEFAULT, CW_USEDEFAULT,
2244 100, 100, NULL, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002245 g_hinst, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246}
2247
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002248/* Used by serverSendToVim() to find an alternate server name. */
2249static char_u *altname_buf_ptr = NULL;
2250
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251/*
2252 * Get the title of the window "hwnd", which is the Vim server name, in
2253 * "name[namelen]" and return the length.
2254 * Returns zero if window "hwnd" is not a Vim server.
2255 */
2256 static int
2257getVimServerName(HWND hwnd, char *name, int namelen)
2258{
2259 int len;
2260 char buffer[VIM_CLASSNAME_LEN + 1];
2261
2262 /* Ignore windows which aren't Vim message windows */
2263 len = GetClassName(hwnd, buffer, sizeof(buffer));
2264 if (len != VIM_CLASSNAME_LEN || STRCMP(buffer, VIM_CLASSNAME) != 0)
2265 return 0;
2266
2267 /* Get the title of the window */
2268 return GetWindowText(hwnd, name, namelen);
2269}
2270
2271 static BOOL CALLBACK
2272enumWindowsGetServer(HWND hwnd, LPARAM lparam)
2273{
2274 struct server_id *id = (struct server_id *)lparam;
2275 char server[MAX_PATH];
2276
2277 /* Get the title of the window */
2278 if (getVimServerName(hwnd, server, sizeof(server)) == 0)
2279 return TRUE;
2280
2281 /* If this is the server we're looking for, return its HWND */
2282 if (STRICMP(server, id->name) == 0)
2283 {
2284 id->hwnd = hwnd;
2285 return FALSE;
2286 }
2287
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002288 /* If we are looking for an alternate server, remember this name. */
2289 if (altname_buf_ptr != NULL
2290 && STRNICMP(server, id->name, STRLEN(id->name)) == 0
2291 && vim_isdigit(server[STRLEN(id->name)]))
2292 {
2293 STRCPY(altname_buf_ptr, server);
2294 altname_buf_ptr = NULL; /* don't use another name */
2295 }
2296
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 /* Otherwise, keep looking */
2298 return TRUE;
2299}
2300
2301 static BOOL CALLBACK
2302enumWindowsGetNames(HWND hwnd, LPARAM lparam)
2303{
2304 garray_T *ga = (garray_T *)lparam;
2305 char server[MAX_PATH];
2306
2307 /* Get the title of the window */
2308 if (getVimServerName(hwnd, server, sizeof(server)) == 0)
2309 return TRUE;
2310
2311 /* Add the name to the list */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002312 ga_concat(ga, (char_u *)server);
2313 ga_concat(ga, (char_u *)"\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 return TRUE;
2315}
2316
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002317struct enum_windows_s
2318{
2319 WNDENUMPROC lpEnumFunc;
2320 LPARAM lParam;
2321};
2322
2323 static BOOL CALLBACK
2324enum_windows_child(HWND hwnd, LPARAM lParam)
2325{
2326 struct enum_windows_s *ew = (struct enum_windows_s *)lParam;
2327
2328 return (ew->lpEnumFunc)(hwnd, ew->lParam);
2329}
2330
2331 static BOOL CALLBACK
2332enum_windows_toplevel(HWND hwnd, LPARAM lParam)
2333{
2334 struct enum_windows_s *ew = (struct enum_windows_s *)lParam;
2335
Bram Moolenaar95ba5c32018-10-07 22:47:07 +02002336 if ((ew->lpEnumFunc)(hwnd, ew->lParam))
2337 return TRUE;
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002338 return EnumChildWindows(hwnd, enum_windows_child, lParam);
2339}
2340
2341/* Enumerate all windows including children. */
2342 static BOOL
2343enum_windows(WNDENUMPROC lpEnumFunc, LPARAM lParam)
2344{
2345 struct enum_windows_s ew;
2346
2347 ew.lpEnumFunc = lpEnumFunc;
2348 ew.lParam = lParam;
2349 return EnumWindows(enum_windows_toplevel, (LPARAM)&ew);
2350}
2351
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 static HWND
2353findServer(char_u *name)
2354{
2355 struct server_id id;
2356
2357 id.name = name;
2358 id.hwnd = 0;
2359
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002360 enum_windows(enumWindowsGetServer, (LPARAM)(&id));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361
2362 return id.hwnd;
2363}
2364
2365 void
2366serverSetName(char_u *name)
2367{
2368 char_u *ok_name;
2369 HWND hwnd = 0;
2370 int i = 0;
2371 char_u *p;
2372
2373 /* Leave enough space for a 9-digit suffix to ensure uniqueness! */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002374 ok_name = alloc((unsigned)STRLEN(name) + 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375
2376 STRCPY(ok_name, name);
2377 p = ok_name + STRLEN(name);
2378
2379 for (;;)
2380 {
2381 /* This is inefficient - we're doing an EnumWindows loop for each
2382 * possible name. It would be better to grab all names in one go,
2383 * and scan the list each time...
2384 */
2385 hwnd = findServer(ok_name);
2386 if (hwnd == 0)
2387 break;
2388
2389 ++i;
2390 if (i >= 1000)
2391 break;
2392
2393 sprintf((char *)p, "%d", i);
2394 }
2395
2396 if (hwnd != 0)
2397 vim_free(ok_name);
2398 else
2399 {
2400 /* Remember the name */
2401 serverName = ok_name;
2402#ifdef FEAT_TITLE
2403 need_maketitle = TRUE; /* update Vim window title later */
2404#endif
2405
2406 /* Update the message window title */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002407 SetWindowText(message_window, (LPCSTR)ok_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408
2409#ifdef FEAT_EVAL
2410 /* Set the servername variable */
2411 set_vim_var_string(VV_SEND_SERVER, serverName, -1);
2412#endif
2413 }
2414}
2415
2416 char_u *
2417serverGetVimNames(void)
2418{
2419 garray_T ga;
2420
2421 ga_init2(&ga, 1, 100);
2422
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002423 enum_windows(enumWindowsGetNames, (LPARAM)(&ga));
Bram Moolenaar269ec652004-07-29 08:43:53 +00002424 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425
2426 return ga.ga_data;
2427}
2428
2429 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002430serverSendReply(
2431 char_u *name, /* Where to send. */
2432 char_u *reply) /* What to send. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433{
2434 HWND target;
2435 COPYDATASTRUCT data;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002436 long_u n = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437
2438 /* The "name" argument is a magic cookie obtained from expand("<client>").
2439 * It should be of the form 0xXXXXX - i.e. a C hex literal, which is the
2440 * value of the client's message window HWND.
2441 */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002442 sscanf((char *)name, SCANF_HEX_LONG_U, &n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 if (n == 0)
2444 return -1;
2445
2446 target = (HWND)n;
2447 if (!IsWindow(target))
2448 return -1;
2449
2450 data.dwData = COPYDATA_REPLY;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002451 data.cbData = (DWORD)STRLEN(reply) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 data.lpData = reply;
2453
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002454 serverSendEnc(target);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 if (SendMessage(target, WM_COPYDATA, (WPARAM)message_window,
2456 (LPARAM)(&data)))
2457 return 0;
2458
2459 return -1;
2460}
2461
2462 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002463serverSendToVim(
2464 char_u *name, /* Where to send. */
2465 char_u *cmd, /* What to send. */
2466 char_u **result, /* Result of eval'ed expression */
2467 void *ptarget, /* HWND of server */
2468 int asExpr, /* Expression or keys? */
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002469 int timeout, /* timeout in seconds or zero */
Bram Moolenaar05540972016-01-30 20:31:25 +01002470 int silent) /* don't complain about no server */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471{
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002472 HWND target;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 COPYDATASTRUCT data;
2474 char_u *retval = NULL;
2475 int retcode = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002476 char_u altname_buf[MAX_PATH];
2477
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01002478 /* Execute locally if no display or target is ourselves */
2479 if (serverName != NULL && STRICMP(name, serverName) == 0)
2480 return sendToLocalVim(cmd, asExpr, result);
2481
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002482 /* If the server name does not end in a digit then we look for an
2483 * alternate name. e.g. when "name" is GVIM the we may find GVIM2. */
2484 if (STRLEN(name) > 1 && !vim_isdigit(name[STRLEN(name) - 1]))
2485 altname_buf_ptr = altname_buf;
2486 altname_buf[0] = NUL;
2487 target = findServer(name);
2488 altname_buf_ptr = NULL;
2489 if (target == 0 && altname_buf[0] != NUL)
2490 /* Use another server name we found. */
2491 target = findServer(altname_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492
2493 if (target == 0)
2494 {
2495 if (!silent)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002496 semsg(_(e_noserver), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 return -1;
2498 }
2499
2500 if (ptarget)
2501 *(HWND *)ptarget = target;
2502
2503 data.dwData = asExpr ? COPYDATA_EXPR : COPYDATA_KEYS;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002504 data.cbData = (DWORD)STRLEN(cmd) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 data.lpData = cmd;
2506
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002507 serverSendEnc(target);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 if (SendMessage(target, WM_COPYDATA, (WPARAM)message_window,
2509 (LPARAM)(&data)) == 0)
2510 return -1;
2511
2512 if (asExpr)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002513 retval = serverGetReply(target, &retcode, TRUE, TRUE, timeout);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514
2515 if (result == NULL)
2516 vim_free(retval);
2517 else
2518 *result = retval; /* Caller assumes responsibility for freeing */
2519
2520 return retcode;
2521}
2522
2523/*
2524 * Bring the server to the foreground.
2525 */
2526 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002527serverForeground(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528{
2529 HWND target = findServer(name);
2530
2531 if (target != 0)
2532 SetForegroundWindow(target);
2533}
2534
2535/* Replies from server need to be stored until the client picks them up via
2536 * remote_read(). So we maintain a list of server-id/reply pairs.
2537 * Note that there could be multiple replies from one server pending if the
2538 * client is slow picking them up.
2539 * We just store the replies in a simple list. When we remove an entry, we
2540 * move list entries down to fill the gap.
2541 * The server ID is simply the HWND.
2542 */
2543typedef struct
2544{
2545 HWND server; /* server window */
2546 char_u *reply; /* reply string */
2547 int expr_result; /* 0 for REPLY, 1 for RESULT 2 for error */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002548} reply_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549
2550static garray_T reply_list = {0, 0, sizeof(reply_T), 5, 0};
2551
2552#define REPLY_ITEM(i) ((reply_T *)(reply_list.ga_data) + (i))
2553#define REPLY_COUNT (reply_list.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554
2555/* Flag which is used to wait for a reply */
2556static int reply_received = 0;
2557
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002558/*
2559 * Store a reply. "reply" must be allocated memory (or NULL).
2560 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 static int
2562save_reply(HWND server, char_u *reply, int expr)
2563{
2564 reply_T *rep;
2565
2566 if (ga_grow(&reply_list, 1) == FAIL)
2567 return FAIL;
2568
2569 rep = REPLY_ITEM(REPLY_COUNT);
2570 rep->server = server;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002571 rep->reply = reply;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 rep->expr_result = expr;
2573 if (rep->reply == NULL)
2574 return FAIL;
2575
2576 ++REPLY_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 reply_received = 1;
2578 return OK;
2579}
2580
2581/*
2582 * Get a reply from server "server".
2583 * When "expr_res" is non NULL, get the result of an expression, otherwise a
2584 * server2client() message.
2585 * When non NULL, point to return code. 0 => OK, -1 => ERROR
2586 * If "remove" is TRUE, consume the message, the caller must free it then.
2587 * if "wait" is TRUE block until a message arrives (or the server exits).
2588 */
2589 char_u *
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002590serverGetReply(HWND server, int *expr_res, int remove, int wait, int timeout)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591{
2592 int i;
2593 char_u *reply;
2594 reply_T *rep;
Bram Moolenaar15e737f2017-03-18 21:22:47 +01002595 int did_process = FALSE;
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002596 time_t start;
2597 time_t now;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598
2599 /* When waiting, loop until the message waiting for is received. */
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002600 time(&start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 for (;;)
2602 {
2603 /* Reset this here, in case a message arrives while we are going
2604 * through the already received messages. */
2605 reply_received = 0;
2606
2607 for (i = 0; i < REPLY_COUNT; ++i)
2608 {
2609 rep = REPLY_ITEM(i);
2610 if (rep->server == server
2611 && ((rep->expr_result != 0) == (expr_res != NULL)))
2612 {
2613 /* Save the values we've found for later */
2614 reply = rep->reply;
2615 if (expr_res != NULL)
2616 *expr_res = rep->expr_result == 1 ? 0 : -1;
2617
2618 if (remove)
2619 {
2620 /* Move the rest of the list down to fill the gap */
2621 mch_memmove(rep, rep + 1,
2622 (REPLY_COUNT - i - 1) * sizeof(reply_T));
2623 --REPLY_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 }
2625
2626 /* Return the reply to the caller, who takes on responsibility
2627 * for freeing it if "remove" is TRUE. */
2628 return reply;
2629 }
2630 }
2631
2632 /* If we got here, we didn't find a reply. Return immediately if the
2633 * "wait" parameter isn't set. */
2634 if (!wait)
Bram Moolenaar15e737f2017-03-18 21:22:47 +01002635 {
2636 /* Process pending messages once. Without this, looping on
2637 * remote_peek() would never get the reply. */
2638 if (!did_process)
2639 {
2640 did_process = TRUE;
2641 serverProcessPendingMessages();
2642 continue;
2643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 break;
Bram Moolenaar15e737f2017-03-18 21:22:47 +01002645 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646
2647 /* We need to wait for a reply. Enter a message loop until the
2648 * "reply_received" flag gets set. */
2649
2650 /* Loop until we receive a reply */
2651 while (reply_received == 0)
2652 {
Bram Moolenaar42205552017-03-18 19:42:22 +01002653#ifdef FEAT_TIMERS
Bram Moolenaard23a8232018-02-10 18:45:26 +01002654 /* TODO: use the return value to decide how long to wait. */
Bram Moolenaar42205552017-03-18 19:42:22 +01002655 check_due_timer();
2656#endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002657 time(&now);
2658 if (timeout > 0 && (now - start) >= timeout)
2659 break;
2660
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 /* Wait for a SendMessage() call to us. This could be the reply
2662 * we are waiting for. Use a timeout of a second, to catch the
2663 * situation that the server died unexpectedly. */
2664 MsgWaitForMultipleObjects(0, NULL, TRUE, 1000, QS_ALLINPUT);
2665
2666 /* If the server has died, give up */
2667 if (!IsWindow(server))
2668 return NULL;
2669
2670 serverProcessPendingMessages();
2671 }
2672 }
2673
2674 return NULL;
2675}
2676
2677/*
2678 * Process any messages in the Windows message queue.
2679 */
2680 void
2681serverProcessPendingMessages(void)
2682{
2683 MSG msg;
2684
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02002685 while (pPeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 {
2687 TranslateMessage(&msg);
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02002688 pDispatchMessage(&msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 }
2690}
2691
2692#endif /* FEAT_CLIENTSERVER */
2693
2694#if defined(FEAT_GUI) || (defined(FEAT_PRINTER) && !defined(FEAT_POSTSCRIPT)) \
2695 || defined(PROTO)
2696
2697struct charset_pair
2698{
2699 char *name;
2700 BYTE charset;
2701};
2702
2703static struct charset_pair
2704charset_pairs[] =
2705{
2706 {"ANSI", ANSI_CHARSET},
2707 {"CHINESEBIG5", CHINESEBIG5_CHARSET},
2708 {"DEFAULT", DEFAULT_CHARSET},
2709 {"HANGEUL", HANGEUL_CHARSET},
2710 {"OEM", OEM_CHARSET},
2711 {"SHIFTJIS", SHIFTJIS_CHARSET},
2712 {"SYMBOL", SYMBOL_CHARSET},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 {"ARABIC", ARABIC_CHARSET},
2714 {"BALTIC", BALTIC_CHARSET},
2715 {"EASTEUROPE", EASTEUROPE_CHARSET},
2716 {"GB2312", GB2312_CHARSET},
2717 {"GREEK", GREEK_CHARSET},
2718 {"HEBREW", HEBREW_CHARSET},
2719 {"JOHAB", JOHAB_CHARSET},
2720 {"MAC", MAC_CHARSET},
2721 {"RUSSIAN", RUSSIAN_CHARSET},
2722 {"THAI", THAI_CHARSET},
2723 {"TURKISH", TURKISH_CHARSET},
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002724#ifdef VIETNAMESE_CHARSET
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 {"VIETNAMESE", VIETNAMESE_CHARSET},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726#endif
2727 {NULL, 0}
2728};
2729
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002730struct quality_pair
2731{
2732 char *name;
2733 DWORD quality;
2734};
2735
2736static struct quality_pair
2737quality_pairs[] = {
2738#ifdef CLEARTYPE_QUALITY
2739 {"CLEARTYPE", CLEARTYPE_QUALITY},
2740#endif
2741#ifdef ANTIALIASED_QUALITY
2742 {"ANTIALIASED", ANTIALIASED_QUALITY},
2743#endif
Bram Moolenaar73a733e2016-05-11 21:05:05 +02002744#ifdef NONANTIALIASED_QUALITY
2745 {"NONANTIALIASED", NONANTIALIASED_QUALITY},
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002746#endif
2747#ifdef PROOF_QUALITY
2748 {"PROOF", PROOF_QUALITY},
2749#endif
Bram Moolenaar4c9ce052016-04-04 21:06:19 +02002750#ifdef DRAFT_QUALITY
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002751 {"DRAFT", DRAFT_QUALITY},
2752#endif
2753 {"DEFAULT", DEFAULT_QUALITY},
2754 {NULL, 0}
2755};
2756
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757/*
2758 * Convert a charset ID to a name.
2759 * Return NULL when not recognized.
2760 */
2761 char *
2762charset_id2name(int id)
2763{
2764 struct charset_pair *cp;
2765
2766 for (cp = charset_pairs; cp->name != NULL; ++cp)
2767 if ((BYTE)id == cp->charset)
2768 break;
2769 return cp->name;
2770}
2771
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002772/*
2773 * Convert a quality ID to a name.
2774 * Return NULL when not recognized.
2775 */
2776 char *
2777quality_id2name(DWORD id)
2778{
2779 struct quality_pair *qp;
2780
2781 for (qp = quality_pairs; qp->name != NULL; ++qp)
2782 if (id == qp->quality)
2783 break;
2784 return qp->name;
2785}
2786
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002787static const LOGFONTW s_lfDefault =
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788{
2789 -12, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, DEFAULT_CHARSET,
2790 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
2791 PROOF_QUALITY, FIXED_PITCH | FF_DONTCARE,
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002792 L"Fixedsys" /* see _ReadVimIni */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793};
2794
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002795// Initialise the "current height" to -12 (same as s_lfDefault) just
2796// in case the user specifies a font in "guifont" with no size before a font
2797// with an explicit size has been set. This defaults the size to this value
2798// (-12 equates to roughly 9pt).
2799int current_font_height = -12; // also used in gui_w32.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800
2801/* Convert a string representing a point size into pixels. The string should
2802 * be a positive decimal number, with an optional decimal point (eg, "12", or
2803 * "10.5"). The pixel value is returned, and a pointer to the next unconverted
2804 * character is stored in *end. The flag "vertical" says whether this
2805 * calculation is for a vertical (height) size or a horizontal (width) one.
2806 */
2807 static int
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002808points_to_pixels(WCHAR *str, WCHAR **end, int vertical, long_i pprinter_dc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809{
2810 int pixels;
2811 int points = 0;
2812 int divisor = 0;
2813 HWND hwnd = (HWND)0;
2814 HDC hdc;
2815 HDC printer_dc = (HDC)pprinter_dc;
2816
2817 while (*str != NUL)
2818 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002819 if (*str == L'.' && divisor == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820 {
2821 /* Start keeping a divisor, for later */
2822 divisor = 1;
2823 }
2824 else
2825 {
2826 if (!VIM_ISDIGIT(*str))
2827 break;
2828
2829 points *= 10;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002830 points += *str - L'0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 divisor *= 10;
2832 }
2833 ++str;
2834 }
2835
2836 if (divisor == 0)
2837 divisor = 1;
2838
2839 if (printer_dc == NULL)
2840 {
2841 hwnd = GetDesktopWindow();
2842 hdc = GetWindowDC(hwnd);
2843 }
2844 else
2845 hdc = printer_dc;
2846
2847 pixels = MulDiv(points,
2848 GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX),
2849 72 * divisor);
2850
2851 if (printer_dc == NULL)
2852 ReleaseDC(hwnd, hdc);
2853
2854 *end = str;
2855 return pixels;
2856}
2857
2858 static int CALLBACK
2859font_enumproc(
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002860 ENUMLOGFONTW *elf,
2861 NEWTEXTMETRICW *ntm UNUSED,
2862 DWORD type UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 LPARAM lparam)
2864{
2865 /* Return value:
2866 * 0 = terminate now (monospace & ANSI)
2867 * 1 = continue, still no luck...
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002868 * 2 = continue, but we have an acceptable LOGFONTW
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 * (monospace, not ANSI)
2870 * We use these values, as EnumFontFamilies returns 1 if the
2871 * callback function is never called. So, we check the return as
2872 * 0 = perfect, 2 = OK, 1 = no good...
2873 * It's not pretty, but it works!
2874 */
2875
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002876 LOGFONTW *lf = (LOGFONTW *)(lparam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877
2878#ifndef FEAT_PROPORTIONAL_FONTS
2879 /* Ignore non-monospace fonts without further ado */
2880 if ((ntm->tmPitchAndFamily & 1) != 0)
2881 return 1;
2882#endif
2883
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002884 /* Remember this LOGFONTW as a "possible" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 *lf = elf->elfLogFont;
2886
2887 /* Terminate the scan as soon as we find an ANSI font */
2888 if (lf->lfCharSet == ANSI_CHARSET
2889 || lf->lfCharSet == OEM_CHARSET
2890 || lf->lfCharSet == DEFAULT_CHARSET)
2891 return 0;
2892
2893 /* Continue the scan - we have a non-ANSI font */
2894 return 2;
2895}
2896
2897 static int
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002898init_logfont(LOGFONTW *lf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899{
2900 int n;
2901 HWND hwnd = GetDesktopWindow();
2902 HDC hdc = GetWindowDC(hwnd);
2903
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002904 n = EnumFontFamiliesW(hdc,
2905 lf->lfFaceName,
2906 (FONTENUMPROCW)font_enumproc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 (LPARAM)lf);
2908
2909 ReleaseDC(hwnd, hdc);
2910
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002911 /* If we couldn't find a usable font, return failure */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 if (n == 1)
2913 return FAIL;
2914
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002915 /* Tidy up the rest of the LOGFONTW structure. We set to a basic
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 * font - get_logfont() sets bold, italic, etc based on the user's
2917 * input.
2918 */
2919 lf->lfHeight = current_font_height;
2920 lf->lfWidth = 0;
2921 lf->lfItalic = FALSE;
2922 lf->lfUnderline = FALSE;
2923 lf->lfStrikeOut = FALSE;
2924 lf->lfWeight = FW_NORMAL;
2925
2926 /* Return success */
2927 return OK;
2928}
2929
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002930/*
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002931 * Compare a UTF-16 string and an ASCII string literally.
2932 * Only works all the code points are inside ASCII range.
2933 */
2934 static int
2935utf16ascncmp(const WCHAR *w, const char *p, size_t n)
2936{
2937 size_t i;
2938
2939 for (i = 0; i < n; i++)
2940 {
2941 if (w[i] == 0 || w[i] != p[i])
2942 return w[i] - p[i];
2943 }
2944 return 0;
2945}
2946
2947/*
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002948 * Get font info from "name" into logfont "lf".
2949 * Return OK for a valid name, FAIL otherwise.
2950 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 int
2952get_logfont(
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002953 LOGFONTW *lf,
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002954 char_u *name,
2955 HDC printer_dc,
2956 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002958 WCHAR *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 int i;
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002960 int ret = FAIL;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002961 static LOGFONTW *lastlf = NULL;
2962 WCHAR *wname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963
2964 *lf = s_lfDefault;
2965 if (name == NULL)
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002966 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002968 wname = enc_to_utf16(name, NULL);
2969 if (wname == NULL)
2970 return FAIL;
2971
2972 if (wcscmp(wname, L"*") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01002974#if defined(FEAT_GUI_MSWIN)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002975 CHOOSEFONTW cf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 /* if name is "*", bring up std font dialog: */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02002977 vim_memset(&cf, 0, sizeof(cf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 cf.lStructSize = sizeof(cf);
2979 cf.hwndOwner = s_hwnd;
2980 cf.Flags = CF_SCREENFONTS | CF_FIXEDPITCHONLY | CF_INITTOLOGFONTSTRUCT;
2981 if (lastlf != NULL)
2982 *lf = *lastlf;
2983 cf.lpLogFont = lf;
2984 cf.nFontType = 0 ; //REGULAR_FONTTYPE;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002985 if (ChooseFontW(&cf))
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002986 ret = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987#endif
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002988 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989 }
2990
2991 /*
2992 * Split name up, it could be <name>:h<height>:w<width> etc.
2993 */
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002994 for (p = wname; *p && *p != L':'; p++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002996 if (p - wname + 1 >= LF_FACESIZE)
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002997 goto theend; /* Name too long */
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002998 lf->lfFaceName[p - wname] = *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999 }
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003000 if (p != wname)
3001 lf->lfFaceName[p - wname] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002
3003 /* First set defaults */
3004 lf->lfHeight = -12;
3005 lf->lfWidth = 0;
3006 lf->lfWeight = FW_NORMAL;
3007 lf->lfItalic = FALSE;
3008 lf->lfUnderline = FALSE;
3009 lf->lfStrikeOut = FALSE;
3010
3011 /*
3012 * If the font can't be found, try replacing '_' by ' '.
3013 */
3014 if (init_logfont(lf) == FAIL)
3015 {
3016 int did_replace = FALSE;
3017
3018 for (i = 0; lf->lfFaceName[i]; ++i)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003019 if (lf->lfFaceName[i] == L'_')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003021 lf->lfFaceName[i] = L' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 did_replace = TRUE;
3023 }
3024 if (!did_replace || init_logfont(lf) == FAIL)
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003025 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 }
3027
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003028 while (*p == L':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029 p++;
3030
3031 /* Set the values found after ':' */
3032 while (*p)
3033 {
3034 switch (*p++)
3035 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003036 case L'h':
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003037 lf->lfHeight = - points_to_pixels(p, &p, TRUE, (long_i)printer_dc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003039 case L'w':
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003040 lf->lfWidth = points_to_pixels(p, &p, FALSE, (long_i)printer_dc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 break;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003042 case L'W':
3043 lf->lfWeight = wcstol(p, &p, 10);
3044 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003045 case L'b':
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046 lf->lfWeight = FW_BOLD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003048 case L'i':
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049 lf->lfItalic = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003051 case L'u':
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052 lf->lfUnderline = TRUE;
3053 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003054 case L's':
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 lf->lfStrikeOut = TRUE;
3056 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003057 case L'c':
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058 {
3059 struct charset_pair *cp;
3060
3061 for (cp = charset_pairs; cp->name != NULL; ++cp)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003062 if (utf16ascncmp(p, cp->name, strlen(cp->name)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063 {
3064 lf->lfCharSet = cp->charset;
3065 p += strlen(cp->name);
3066 break;
3067 }
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003068 if (cp->name == NULL && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003070 char_u *s = utf16_to_enc(p, NULL);
3071 semsg(_("E244: Illegal charset name \"%s\" in font name \"%s\""), s, name);
3072 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073 break;
3074 }
3075 break;
3076 }
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003077 case L'q':
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003078 {
3079 struct quality_pair *qp;
3080
3081 for (qp = quality_pairs; qp->name != NULL; ++qp)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003082 if (utf16ascncmp(p, qp->name, strlen(qp->name)) == 0)
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003083 {
3084 lf->lfQuality = qp->quality;
3085 p += strlen(qp->name);
3086 break;
3087 }
3088 if (qp->name == NULL && verbose)
3089 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003090 char_u *s = utf16_to_enc(p, NULL);
3091 semsg(_("E244: Illegal quality name \"%s\" in font name \"%s\""), s, name);
3092 vim_free(s);
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003093 break;
3094 }
3095 break;
3096 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 default:
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003098 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003099 semsg(_("E245: Illegal char '%c' in font name \"%s\""), p[-1], name);
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003100 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 }
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003102 while (*p == L':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 p++;
3104 }
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003105 ret = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 /* ron: init lastlf */
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003109 if (ret == OK && printer_dc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 {
3111 vim_free(lastlf);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003112 lastlf = (LOGFONTW *)alloc(sizeof(LOGFONTW));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 if (lastlf != NULL)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003114 mch_memmove(lastlf, lf, sizeof(LOGFONTW));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 }
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003116 vim_free(wname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003118 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119}
3120
3121#endif /* defined(FEAT_GUI) || defined(FEAT_PRINTER) */
Bram Moolenaarf12d9832016-01-29 21:11:25 +01003122
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003123#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaarf12d9832016-01-29 21:11:25 +01003124/*
3125 * Initialize the Winsock dll.
3126 */
3127 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003128channel_init_winsock(void)
Bram Moolenaarf12d9832016-01-29 21:11:25 +01003129{
3130 WSADATA wsaData;
3131 int wsaerr;
3132
3133 if (WSInitialized)
3134 return;
3135
3136 wsaerr = WSAStartup(MAKEWORD(2, 2), &wsaData);
3137 if (wsaerr == 0)
3138 WSInitialized = TRUE;
3139}
3140#endif