blob: 67989fe28c482db4f84eb248599d853c82189fca [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#endif
176
177
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200178#ifndef PROTO
179/*
180 * Save the instance handle of the exe/dll.
181 */
182 void
183SaveInst(HINSTANCE hInst)
184{
185 g_hinst = hInst;
186}
187#endif
188
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189#if defined(FEAT_GUI_MSWIN) || defined(PROTO)
190/*
191 * GUI version of mch_exit().
192 * Shut down and exit with status `r'
193 * Careful: mch_exit() may be called before mch_init()!
194 */
195 void
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200196mch_exit_g(int r)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197{
Bram Moolenaar955f1982017-02-05 15:10:51 +0100198 exiting = TRUE;
199
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200 display_errors();
201
202 ml_close_all(TRUE); /* remove all memfiles */
203
204# ifdef FEAT_OLE
205 UninitOLE();
206# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100207# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208 if (WSInitialized)
209 {
210 WSInitialized = FALSE;
211 WSACleanup();
212 }
213# endif
214#ifdef DYNAMIC_GETTEXT
215 dyn_libintl_end();
216#endif
217
218 if (gui.in_use)
219 gui_exit(r);
Bram Moolenaar85c79d32007-02-20 01:59:20 +0000220
221#ifdef EXITFREE
222 free_all_mem();
223#endif
224
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225 exit(r);
226}
227
228#endif /* FEAT_GUI_MSWIN */
229
230
231/*
232 * Init the tables for toupper() and tolower().
233 */
234 void
235mch_early_init(void)
236{
237 int i;
238
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239 PlatformId();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240
241 /* Init the tables for toupper() and tolower() */
242 for (i = 0; i < 256; ++i)
243 toupper_tab[i] = tolower_tab[i] = i;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100244 CharUpperBuff((LPSTR)toupper_tab, 256);
245 CharLowerBuff((LPSTR)tolower_tab, 256);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246}
247
248
249/*
250 * Return TRUE if the input comes from a terminal, FALSE otherwise.
251 */
252 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100253mch_input_isatty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254{
255#ifdef FEAT_GUI_MSWIN
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200256# ifdef VIMDLL
257 if (gui.in_use)
258# endif
259 return TRUE; /* GUI always has a tty */
260#endif
261#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262 if (isatty(read_cmd_fd))
263 return TRUE;
264 return FALSE;
265#endif
266}
267
268#ifdef FEAT_TITLE
269/*
270 * mch_settitle(): set titlebar of our window
271 */
272 void
273mch_settitle(
274 char_u *title,
275 char_u *icon)
276{
277# ifdef FEAT_GUI_MSWIN
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200278# ifdef VIMDLL
279 if (gui.in_use)
280# endif
281 {
282 gui_mch_settitle(title, icon);
283 return;
284 }
285# endif
286# if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000287 if (title != NULL)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000288 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200289 WCHAR *wp = enc_to_utf16(title, NULL);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000290
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200291 if (wp == NULL)
292 return;
293
294 SetConsoleTitleW(wp);
295 vim_free(wp);
296 return;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000297 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000298# endif
299}
300
301
302/*
303 * Restore the window/icon title.
304 * which is one of:
Bram Moolenaar40385db2018-08-07 22:31:44 +0200305 * SAVE_RESTORE_TITLE: Just restore title
306 * SAVE_RESTORE_ICON: Just restore icon (which we don't have)
307 * SAVE_RESTORE_BOTH: Restore title and icon (which we don't have)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000308 */
309 void
Bram Moolenaar1266d672017-02-01 13:43:36 +0100310mch_restore_title(int which UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311{
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200312#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
313# ifdef VIMDLL
314 if (!gui.in_use)
315# endif
316 SetConsoleTitle(g_szOrigTitle);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317#endif
318}
319
320
321/*
322 * Return TRUE if we can restore the title (we can)
323 */
324 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100325mch_can_restore_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326{
327 return TRUE;
328}
329
330
331/*
332 * Return TRUE if we can restore the icon title (we can't)
333 */
334 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100335mch_can_restore_icon(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336{
337 return FALSE;
338}
339#endif /* FEAT_TITLE */
340
341
342/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000343 * Get absolute file name into buffer "buf" of length "len" bytes,
344 * turning all '/'s into '\\'s and getting the correct case of each component
345 * of the file name. Append a (back)slash to a directory name.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346 * When 'shellslash' set do it the other way around.
347 * Return OK or FAIL.
348 */
349 int
350mch_FullName(
351 char_u *fname,
352 char_u *buf,
353 int len,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100354 int force UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355{
356 int nResult = FAIL;
Bram Moolenaareae1b912019-05-09 15:12:55 +0200357 WCHAR *wname;
358 WCHAR wbuf[MAX_PATH];
359 char_u *cname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360
Bram Moolenaareae1b912019-05-09 15:12:55 +0200361 wname = enc_to_utf16(fname, NULL);
362 if (wname != NULL && _wfullpath(wbuf, wname, MAX_PATH) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363 {
Bram Moolenaareae1b912019-05-09 15:12:55 +0200364 cname = utf16_to_enc((short_u *)wbuf, NULL);
365 if (cname != NULL)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000366 {
Bram Moolenaareae1b912019-05-09 15:12:55 +0200367 vim_strncpy(buf, cname, len - 1);
368 nResult = OK;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000369 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370 }
Bram Moolenaareae1b912019-05-09 15:12:55 +0200371 vim_free(wname);
372 vim_free(cname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373
374#ifdef USE_FNAME_CASE
375 fname_case(buf, len);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000376#else
377 slash_adjust(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378#endif
379
380 return nResult;
381}
382
383
384/*
385 * Return TRUE if "fname" does not depend on the current directory.
386 */
387 int
388mch_isFullName(char_u *fname)
389{
Bram Moolenaard2a203b2013-08-30 16:51:18 +0200390 /* WinNT and later can use _MAX_PATH wide characters for a pathname, which
391 * means that the maximum pathname is _MAX_PATH * 3 bytes when 'enc' is
392 * UTF-8. */
393 char szName[_MAX_PATH * 3 + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394
395 /* A name like "d:/foo" and "//server/share" is absolute */
396 if ((fname[0] && fname[1] == ':' && (fname[2] == '/' || fname[2] == '\\'))
397 || (fname[0] == fname[1] && (fname[0] == '/' || fname[0] == '\\')))
398 return TRUE;
399
400 /* A name that can't be made absolute probably isn't absolute. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100401 if (mch_FullName(fname, (char_u *)szName, sizeof(szName) - 1, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402 return FALSE;
403
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100404 return pathcmp((const char *)fname, (const char *)szName, -1) == 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405}
406
407/*
408 * Replace all slashes by backslashes.
409 * This used to be the other way around, but MS-DOS sometimes has problems
410 * with slashes (e.g. in a command name). We can't have mixed slashes and
411 * backslashes, because comparing file names will not work correctly. The
412 * commands that use a file name should try to avoid the need to type a
413 * backslash twice.
414 * When 'shellslash' set do it the other way around.
Bram Moolenaarb4f6a462015-10-13 19:43:17 +0200415 * When the path looks like a URL leave it unmodified.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416 */
417 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100418slash_adjust(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419{
Bram Moolenaarb4f6a462015-10-13 19:43:17 +0200420 if (path_with_url(p))
421 return;
Bram Moolenaar39d21e32017-08-05 23:09:31 +0200422
423 if (*p == '`')
424 {
Bram Moolenaar116a0f82017-08-07 21:17:57 +0200425 size_t len = STRLEN(p);
426
Bram Moolenaar39d21e32017-08-05 23:09:31 +0200427 /* don't replace backslash in backtick quoted strings */
Bram Moolenaar39d21e32017-08-05 23:09:31 +0200428 if (len > 2 && *(p + len - 1) == '`')
429 return;
430 }
431
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +0000432 while (*p)
433 {
434 if (*p == psepcN)
435 *p = psepc;
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100436 MB_PTR_ADV(p);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +0000437 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438}
439
Bram Moolenaar8767f522016-07-01 17:17:39 +0200440/* Use 64-bit stat functions if available. */
441#ifdef HAVE_STAT64
442# undef stat
443# undef _stat
444# undef _wstat
445# undef _fstat
446# define stat _stat64
447# define _stat _stat64
448# define _wstat _wstat64
449# define _fstat _fstat64
450#endif
451
Bram Moolenaar9d1685d2014-01-14 12:18:45 +0100452#if (defined(_MSC_VER) && (_MSC_VER >= 1300)) || defined(__MINGW32__)
Bram Moolenaar8962fda2013-09-29 19:05:21 +0200453# define OPEN_OH_ARGTYPE intptr_t
454#else
455# define OPEN_OH_ARGTYPE long
456#endif
457
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200458 static int
Bram Moolenaar8767f522016-07-01 17:17:39 +0200459wstat_symlink_aware(const WCHAR *name, stat_T *stp)
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200460{
Bram Moolenaara12a1612019-01-24 16:39:02 +0100461#if (defined(_MSC_VER) && (_MSC_VER < 1900)) || defined(__MINGW32__)
Bram Moolenaarfce7b3d2016-01-19 19:00:32 +0100462 /* Work around for VC12 or earlier (and MinGW). _wstat() can't handle
463 * symlinks properly.
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200464 * VC9 or earlier: _wstat() doesn't support a symlink at all. It retrieves
465 * status of a symlink itself.
466 * VC10: _wstat() supports a symlink to a normal file, but it doesn't
Bram Moolenaarfce7b3d2016-01-19 19:00:32 +0100467 * support a symlink to a directory (always returns an error).
468 * VC11 and VC12: _wstat() doesn't return an error for a symlink to a
469 * directory, but it doesn't set S_IFDIR flag.
470 * MinGW: Same as VC9. */
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200471 int n;
472 BOOL is_symlink = FALSE;
473 HANDLE hFind, h;
474 DWORD attr = 0;
475 WIN32_FIND_DATAW findDataW;
476
477 hFind = FindFirstFileW(name, &findDataW);
478 if (hFind != INVALID_HANDLE_VALUE)
479 {
480 attr = findDataW.dwFileAttributes;
481 if ((attr & FILE_ATTRIBUTE_REPARSE_POINT)
482 && (findDataW.dwReserved0 == IO_REPARSE_TAG_SYMLINK))
483 is_symlink = TRUE;
484 FindClose(hFind);
485 }
486 if (is_symlink)
487 {
488 h = CreateFileW(name, FILE_READ_ATTRIBUTES,
489 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
490 OPEN_EXISTING,
491 (attr & FILE_ATTRIBUTE_DIRECTORY)
492 ? FILE_FLAG_BACKUP_SEMANTICS : 0,
493 NULL);
494 if (h != INVALID_HANDLE_VALUE)
495 {
496 int fd;
497
Bram Moolenaar8962fda2013-09-29 19:05:21 +0200498 fd = _open_osfhandle((OPEN_OH_ARGTYPE)h, _O_RDONLY);
Bram Moolenaar8767f522016-07-01 17:17:39 +0200499 n = _fstat(fd, (struct _stat *)stp);
Bram Moolenaarfce7b3d2016-01-19 19:00:32 +0100500 if ((n == 0) && (attr & FILE_ATTRIBUTE_DIRECTORY))
501 stp->st_mode = (stp->st_mode & ~S_IFREG) | S_IFDIR;
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200502 _close(fd);
503 return n;
504 }
505 }
Bram Moolenaara12a1612019-01-24 16:39:02 +0100506#endif
Bram Moolenaar8767f522016-07-01 17:17:39 +0200507 return _wstat(name, (struct _stat *)stp);
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200508}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509
510/*
511 * stat() can't handle a trailing '/' or '\', remove it first.
512 */
513 int
Bram Moolenaar8767f522016-07-01 17:17:39 +0200514vim_stat(const char *name, stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515{
Bram Moolenaard2a203b2013-08-30 16:51:18 +0200516 /* WinNT and later can use _MAX_PATH wide characters for a pathname, which
517 * means that the maximum pathname is _MAX_PATH * 3 bytes when 'enc' is
518 * UTF-8. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100519 char_u buf[_MAX_PATH * 3 + 1];
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100520 char_u *p;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200521 WCHAR *wp;
522 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523
Bram Moolenaard2a203b2013-08-30 16:51:18 +0200524 vim_strncpy((char_u *)buf, (char_u *)name, sizeof(buf) - 1);
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100525 p = buf + STRLEN(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526 if (p > buf)
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100527 MB_PTR_BACK(buf, p);
Bram Moolenaarb1cb35f2014-01-10 13:05:20 +0100528
529 /* Remove trailing '\\' except root path. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530 if (p > buf && (*p == '\\' || *p == '/') && p[-1] != ':')
531 *p = NUL;
Bram Moolenaarb1cb35f2014-01-10 13:05:20 +0100532
533 if ((buf[0] == '\\' && buf[1] == '\\') || (buf[0] == '/' && buf[1] == '/'))
534 {
535 /* UNC root path must be followed by '\\'. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100536 p = vim_strpbrk(buf + 2, (char_u *)"\\/");
Bram Moolenaarb1cb35f2014-01-10 13:05:20 +0100537 if (p != NULL)
538 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100539 p = vim_strpbrk(p + 1, (char_u *)"\\/");
Bram Moolenaarb1cb35f2014-01-10 13:05:20 +0100540 if (p == NULL)
541 STRCAT(buf, "\\");
542 }
543 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200545 wp = enc_to_utf16(buf, NULL);
546 if (wp == NULL)
547 return -1;
548
549 n = wstat_symlink_aware(wp, stp);
550 vim_free(wp);
551 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552}
553
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200554#if (defined(FEAT_GUI_MSWIN) && !defined(VIMDLL)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555 void
Bram Moolenaar1266d672017-02-01 13:43:36 +0100556mch_settmode(int tmode UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557{
558 /* nothing to do */
559}
560
561 int
562mch_get_shellsize(void)
563{
564 /* never used */
565 return OK;
566}
567
568 void
569mch_set_shellsize(void)
570{
571 /* never used */
572}
573
574/*
575 * Rows and/or Columns has changed.
576 */
577 void
578mch_new_shellsize(void)
579{
580 /* never used */
581}
582
583#endif
584
585/*
586 * We have no job control, so fake it by starting a new shell.
587 */
588 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100589mch_suspend(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590{
591 suspend_shell();
592}
593
594#if defined(USE_MCH_ERRMSG) || defined(PROTO)
595
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200596# ifdef display_errors
597# undef display_errors
598# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599
600/*
601 * Display the saved error message(s).
602 */
603 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100604display_errors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605{
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200606# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 char *p;
608
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200609# ifdef VIMDLL
610 if (gui.in_use || gui.starting)
611# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200613 if (error_ga.ga_data != NULL)
614 {
615 /* avoid putting up a message box with blanks only */
616 for (p = (char *)error_ga.ga_data; *p; ++p)
617 if (!isspace(*p))
618 {
619 (void)gui_mch_dialog(
Bram Moolenaarc17ef8e2006-03-25 21:48:58 +0000620 gui.starting ? VIM_INFO :
Bram Moolenaarc17ef8e2006-03-25 21:48:58 +0000621 VIM_ERROR,
Bram Moolenaarc17ef8e2006-03-25 21:48:58 +0000622 gui.starting ? (char_u *)_("Message") :
Bram Moolenaarc17ef8e2006-03-25 21:48:58 +0000623 (char_u *)_("Error"),
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100624 (char_u *)p, (char_u *)_("&Ok"),
625 1, NULL, FALSE);
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200626 break;
627 }
628 ga_clear(&error_ga);
629 }
630 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200632# endif
633# if !defined(FEAT_GUI) || defined(VIMDLL)
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +0100634 FlushFileBuffers(GetStdHandle(STD_ERROR_HANDLE));
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200635# endif
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +0100636}
637#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638
639
640/*
641 * Return TRUE if "p" contain a wildcard that can be expanded by
642 * dos_expandpath().
643 */
644 int
645mch_has_exp_wildcard(char_u *p)
646{
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100647 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648 {
649 if (vim_strchr((char_u *)"?*[", *p) != NULL
650 || (*p == '~' && p[1] != NUL))
651 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652 }
653 return FALSE;
654}
655
656/*
657 * Return TRUE if "p" contain a wildcard or a "~1" kind of thing (could be a
658 * shortened file name).
659 */
660 int
661mch_has_wildcard(char_u *p)
662{
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100663 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 {
665 if (vim_strchr((char_u *)
666# ifdef VIM_BACKTICK
667 "?*$[`"
668# else
669 "?*$["
670# endif
671 , *p) != NULL
672 || (*p == '~' && p[1] != NUL))
673 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 }
675 return FALSE;
676}
677
678
679/*
680 * The normal _chdir() does not change the default drive. This one does.
681 * Returning 0 implies success; -1 implies failure.
682 */
683 int
684mch_chdir(char *path)
685{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200686 WCHAR *p;
687 int n;
688
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 if (path[0] == NUL) /* just checking... */
690 return -1;
691
Bram Moolenaara2974d72009-07-14 16:38:36 +0000692 if (p_verbose >= 5)
693 {
694 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100695 smsg("chdir(%s)", path);
Bram Moolenaara2974d72009-07-14 16:38:36 +0000696 verbose_leave();
697 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 if (isalpha(path[0]) && path[1] == ':') /* has a drive name */
699 {
700 /* If we can change to the drive, skip that part of the path. If we
701 * can't then the current directory may be invalid, try using chdir()
702 * with the whole path. */
703 if (_chdrive(TOLOWER_ASC(path[0]) - 'a' + 1) == 0)
704 path += 2;
705 }
706
707 if (*path == NUL) /* drive name only */
708 return 0;
709
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200710 p = enc_to_utf16((char_u *)path, NULL);
711 if (p == NULL)
712 return -1;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000713
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200714 n = _wchdir(p);
715 vim_free(p);
716 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717}
718
719
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200720#if defined(FEAT_GUI_MSWIN) && !defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721/*
722 * return non-zero if a character is available
723 */
724 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100725mch_char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726{
727 /* never used */
728 return TRUE;
729}
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200730
731# if defined(FEAT_TERMINAL) || defined(PROTO)
732/*
733 * Check for any pending input or messages.
734 */
735 int
736mch_check_messages(void)
737{
738 /* TODO: check for messages */
739 return TRUE;
740}
741# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742#endif
743
744
745/*
746 * set screen mode, always fails.
747 */
748 int
Bram Moolenaar1266d672017-02-01 13:43:36 +0100749mch_screenmode(char_u *arg UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100751 emsg(_(e_screenmode));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 return FAIL;
753}
754
755
756#if defined(FEAT_LIBCALL) || defined(PROTO)
757/*
758 * Call a DLL routine which takes either a string or int param
759 * and returns an allocated string.
760 * Return OK if it worked, FAIL if not.
761 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762typedef LPTSTR (*MYSTRPROCSTR)(LPTSTR);
763typedef LPTSTR (*MYINTPROCSTR)(int);
764typedef int (*MYSTRPROCINT)(LPTSTR);
765typedef int (*MYINTPROCINT)(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767/*
768 * Check if a pointer points to a valid NUL terminated string.
769 * Return the length of the string, including terminating NUL.
770 * Returns 0 for an invalid pointer, 1 for an empty string.
771 */
772 static size_t
773check_str_len(char_u *str)
774{
775 SYSTEM_INFO si;
776 MEMORY_BASIC_INFORMATION mbi;
777 size_t length = 0;
778 size_t i;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100779 const char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780
781 /* get page size */
782 GetSystemInfo(&si);
783
784 /* get memory information */
785 if (VirtualQuery(str, &mbi, sizeof(mbi)))
786 {
787 /* pre cast these (typing savers) */
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000788 long_u dwStr = (long_u)str;
789 long_u dwBaseAddress = (long_u)mbi.BaseAddress;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790
791 /* get start address of page that str is on */
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000792 long_u strPage = dwStr - (dwStr - dwBaseAddress) % si.dwPageSize;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793
794 /* get length from str to end of page */
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000795 long_u pageLength = si.dwPageSize - (dwStr - strPage);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796
Bram Moolenaar442b4222010-05-24 21:34:22 +0200797 for (p = str; !IsBadReadPtr(p, (UINT)pageLength);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 p += pageLength, pageLength = si.dwPageSize)
799 for (i = 0; i < pageLength; ++i, ++length)
800 if (p[i] == NUL)
801 return length + 1;
802 }
803
804 return 0;
805}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200807/*
808 * Passed to do_in_runtimepath() to load a vim.ico file.
809 */
810 static void
811mch_icon_load_cb(char_u *fname, void *cookie)
812{
813 HANDLE *h = (HANDLE *)cookie;
814
815 *h = LoadImage(NULL,
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100816 (LPSTR)fname,
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200817 IMAGE_ICON,
818 64,
819 64,
820 LR_LOADFROMFILE | LR_LOADMAP3DCOLORS);
821}
822
823/*
824 * Try loading an icon file from 'runtimepath'.
825 */
826 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100827mch_icon_load(HANDLE *iconp)
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200828{
829 return do_in_runtimepath((char_u *)"bitmaps/vim.ico",
Bram Moolenaar7f8989d2016-03-12 22:11:39 +0100830 0, mch_icon_load_cb, iconp);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200831}
832
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 int
834mch_libcall(
835 char_u *libname,
836 char_u *funcname,
837 char_u *argstring, /* NULL when using a argint */
838 int argint,
839 char_u **string_result,/* NULL when using number_result */
840 int *number_result)
841{
842 HINSTANCE hinstLib;
843 MYSTRPROCSTR ProcAdd;
844 MYINTPROCSTR ProcAddI;
845 char_u *retval_str = NULL;
846 int retval_int = 0;
847 size_t len;
848
849 BOOL fRunTimeLinkSuccess = FALSE;
850
851 // Get a handle to the DLL module.
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100852 hinstLib = vimLoadLib((char *)libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853
854 // If the handle is valid, try to get the function address.
855 if (hinstLib != NULL)
856 {
857#ifdef HAVE_TRY_EXCEPT
858 __try
859 {
860#endif
861 if (argstring != NULL)
862 {
863 /* Call with string argument */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100864 ProcAdd = (MYSTRPROCSTR)GetProcAddress(hinstLib, (LPCSTR)funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865 if ((fRunTimeLinkSuccess = (ProcAdd != NULL)) != 0)
866 {
867 if (string_result == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100868 retval_int = ((MYSTRPROCINT)ProcAdd)((LPSTR)argstring);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100870 retval_str = (char_u *)(ProcAdd)((LPSTR)argstring);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871 }
872 }
873 else
874 {
875 /* Call with number argument */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100876 ProcAddI = (MYINTPROCSTR) GetProcAddress(hinstLib, (LPCSTR)funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877 if ((fRunTimeLinkSuccess = (ProcAddI != NULL)) != 0)
878 {
879 if (string_result == NULL)
880 retval_int = ((MYINTPROCINT)ProcAddI)(argint);
881 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100882 retval_str = (char_u *)(ProcAddI)(argint);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 }
884 }
885
886 // Save the string before we free the library.
887 // Assume that a "1" result is an illegal pointer.
888 if (string_result == NULL)
889 *number_result = retval_int;
890 else if (retval_str != NULL
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100891 && (len = check_str_len(retval_str)) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 {
893 *string_result = lalloc((long_u)len, TRUE);
894 if (*string_result != NULL)
895 mch_memmove(*string_result, retval_str, len);
896 }
897
898#ifdef HAVE_TRY_EXCEPT
899 }
900 __except(EXCEPTION_EXECUTE_HANDLER)
901 {
902 if (GetExceptionCode() == EXCEPTION_STACK_OVERFLOW)
903 RESETSTKOFLW();
904 fRunTimeLinkSuccess = 0;
905 }
906#endif
907
908 // Free the DLL module.
909 (void)FreeLibrary(hinstLib);
910 }
911
912 if (!fRunTimeLinkSuccess)
913 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100914 semsg(_(e_libcall), funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 return FAIL;
916 }
917
918 return OK;
919}
920#endif
921
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922/*
923 * Debugging helper: expose the MCH_WRITE_DUMP stuff to other modules
924 */
925 void
Bram Moolenaar1266d672017-02-01 13:43:36 +0100926DumpPutS(const char *psz UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927{
928# ifdef MCH_WRITE_DUMP
929 if (fdDump)
930 {
931 fputs(psz, fdDump);
932 if (psz[strlen(psz) - 1] != '\n')
933 fputc('\n', fdDump);
934 fflush(fdDump);
935 }
936# endif
937}
938
939#ifdef _DEBUG
940
941void __cdecl
942Trace(
943 char *pszFormat,
944 ...)
945{
946 CHAR szBuff[2048];
947 va_list args;
948
949 va_start(args, pszFormat);
950 vsprintf(szBuff, pszFormat, args);
951 va_end(args);
952
953 OutputDebugString(szBuff);
954}
955
956#endif //_DEBUG
957
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200958#if !defined(FEAT_GUI) || defined(VIMDLL) || defined(PROTO)
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200959# ifdef FEAT_TITLE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960extern HWND g_hWnd; /* This is in os_win32.c. */
961# endif
962
963/*
964 * Showing the printer dialog is tricky since we have no GUI
965 * window to parent it. The following routines are needed to
966 * get the window parenting and Z-order to work properly.
967 */
968 static void
969GetConsoleHwnd(void)
970{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 /* Skip if it's already set. */
972 if (s_hwnd != 0)
973 return;
974
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200975# ifdef FEAT_TITLE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 /* Window handle may have been found by init code (Windows NT only) */
977 if (g_hWnd != 0)
978 {
979 s_hwnd = g_hWnd;
980 return;
981 }
982# endif
983
Bram Moolenaare1ed53f2019-02-12 23:12:37 +0100984 s_hwnd = GetConsoleWindow();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985}
Bram Moolenaar843ee412004-06-30 16:16:41 +0000986
987/*
988 * Console implementation of ":winpos".
989 */
990 int
991mch_get_winpos(int *x, int *y)
992{
993 RECT rect;
994
995 GetConsoleHwnd();
996 GetWindowRect(s_hwnd, &rect);
997 *x = rect.left;
998 *y = rect.top;
999 return OK;
1000}
1001
1002/*
1003 * Console implementation of ":winpos x y".
1004 */
1005 void
1006mch_set_winpos(int x, int y)
1007{
1008 GetConsoleHwnd();
1009 SetWindowPos(s_hwnd, NULL, x, y, 0, 0,
1010 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
1011}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012#endif
1013
1014#if (defined(FEAT_PRINTER) && !defined(FEAT_POSTSCRIPT)) || defined(PROTO)
1015
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016/*=================================================================
1017 * Win32 printer stuff
1018 */
1019
1020static HFONT prt_font_handles[2][2][2];
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001021static PRINTDLGW prt_dlg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022static const int boldface[2] = {FW_REGULAR, FW_BOLD};
1023static TEXTMETRIC prt_tm;
1024static int prt_line_height;
1025static int prt_number_width;
1026static int prt_left_margin;
1027static int prt_right_margin;
1028static int prt_top_margin;
1029static char_u szAppName[] = TEXT("VIM");
1030static HWND hDlgPrint;
1031static int *bUserAbort = NULL;
1032static char_u *prt_name = NULL;
1033
1034/* Defines which are also in vim.rc. */
1035#define IDC_BOX1 400
1036#define IDC_PRINTTEXT1 401
1037#define IDC_PRINTTEXT2 402
1038#define IDC_PROGRESS 403
1039
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001040 static BOOL
1041vimSetDlgItemText(HWND hDlg, int nIDDlgItem, char_u *s)
1042{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001043 WCHAR *wp;
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001044 BOOL ret;
1045
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001046 wp = enc_to_utf16(s, NULL);
1047 if (wp == NULL)
1048 return FALSE;
1049
1050 ret = SetDlgItemTextW(hDlg, nIDDlgItem, wp);
1051 vim_free(wp);
1052 return ret;
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001053}
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001054
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055/*
1056 * Convert BGR to RGB for Windows GDI calls
1057 */
1058 static COLORREF
1059swap_me(COLORREF colorref)
1060{
1061 int temp;
1062 char *ptr = (char *)&colorref;
1063
1064 temp = *(ptr);
1065 *(ptr ) = *(ptr + 2);
1066 *(ptr + 2) = temp;
1067 return colorref;
1068}
1069
Bram Moolenaared39e1d2008-08-09 17:55:22 +00001070/* Attempt to make this work for old and new compilers */
Bram Moolenaar9f733d12011-09-21 20:09:42 +02001071#if !defined(_WIN64) && (!defined(_MSC_VER) || _MSC_VER < 1300)
Bram Moolenaared39e1d2008-08-09 17:55:22 +00001072# define PDP_RETVAL BOOL
1073#else
1074# define PDP_RETVAL INT_PTR
1075#endif
1076
Bram Moolenaared39e1d2008-08-09 17:55:22 +00001077 static PDP_RETVAL CALLBACK
Bram Moolenaar1266d672017-02-01 13:43:36 +01001078PrintDlgProc(
1079 HWND hDlg,
1080 UINT message,
1081 WPARAM wParam UNUSED,
1082 LPARAM lParam UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083{
1084#ifdef FEAT_GETTEXT
1085 NONCLIENTMETRICS nm;
1086 static HFONT hfont;
1087#endif
1088
1089 switch (message)
1090 {
1091 case WM_INITDIALOG:
1092#ifdef FEAT_GETTEXT
1093 nm.cbSize = sizeof(NONCLIENTMETRICS);
1094 if (SystemParametersInfo(
1095 SPI_GETNONCLIENTMETRICS,
1096 sizeof(NONCLIENTMETRICS),
1097 &nm,
1098 0))
1099 {
1100 char buff[MAX_PATH];
1101 int i;
1102
1103 /* Translate the dialog texts */
1104 hfont = CreateFontIndirect(&nm.lfMessageFont);
1105 for (i = IDC_PRINTTEXT1; i <= IDC_PROGRESS; i++)
1106 {
1107 SendDlgItemMessage(hDlg, i, WM_SETFONT, (WPARAM)hfont, 1);
1108 if (GetDlgItemText(hDlg,i, buff, sizeof(buff)))
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001109 vimSetDlgItemText(hDlg,i, (char_u *)_(buff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 }
1111 SendDlgItemMessage(hDlg, IDCANCEL,
1112 WM_SETFONT, (WPARAM)hfont, 1);
1113 if (GetDlgItemText(hDlg,IDCANCEL, buff, sizeof(buff)))
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001114 vimSetDlgItemText(hDlg,IDCANCEL, (char_u *)_(buff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 }
1116#endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001117 SetWindowText(hDlg, (LPCSTR)szAppName);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 if (prt_name != NULL)
1119 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001120 vimSetDlgItemText(hDlg, IDC_PRINTTEXT2, (char_u *)prt_name);
Bram Moolenaard23a8232018-02-10 18:45:26 +01001121 VIM_CLEAR(prt_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 }
1123 EnableMenuItem(GetSystemMenu(hDlg, FALSE), SC_CLOSE, MF_GRAYED);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001124#if !defined(FEAT_GUI) || defined(VIMDLL)
1125# ifdef VIMDLL
1126 if (!gui.in_use)
1127# endif
1128 BringWindowToTop(s_hwnd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129#endif
1130 return TRUE;
1131
1132 case WM_COMMAND:
1133 *bUserAbort = TRUE;
1134 EnableWindow(GetParent(hDlg), TRUE);
1135 DestroyWindow(hDlg);
1136 hDlgPrint = NULL;
1137#ifdef FEAT_GETTEXT
1138 DeleteObject(hfont);
1139#endif
1140 return TRUE;
1141 }
1142 return FALSE;
1143}
1144
1145 static BOOL CALLBACK
Bram Moolenaar1266d672017-02-01 13:43:36 +01001146AbortProc(HDC hdcPrn UNUSED, int iCode UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147{
1148 MSG msg;
1149
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02001150 while (!*bUserAbort && pPeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 {
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02001152 if (!hDlgPrint || !pIsDialogMessage(hDlgPrint, &msg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 {
1154 TranslateMessage(&msg);
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02001155 pDispatchMessage(&msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 }
1157 }
1158 return !*bUserAbort;
1159}
1160
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001161#if !defined(FEAT_GUI) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162
Bram Moolenaar26fdd7d2011-11-30 13:42:44 +01001163 static UINT_PTR CALLBACK
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164PrintHookProc(
1165 HWND hDlg, // handle to dialog box
1166 UINT uiMsg, // message identifier
1167 WPARAM wParam, // message parameter
1168 LPARAM lParam // message parameter
1169 )
1170{
1171 HWND hwndOwner;
1172 RECT rc, rcDlg, rcOwner;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001173 PRINTDLGW *pPD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174
1175 if (uiMsg == WM_INITDIALOG)
1176 {
1177 // Get the owner window and dialog box rectangles.
1178 if ((hwndOwner = GetParent(hDlg)) == NULL)
1179 hwndOwner = GetDesktopWindow();
1180
1181 GetWindowRect(hwndOwner, &rcOwner);
1182 GetWindowRect(hDlg, &rcDlg);
1183 CopyRect(&rc, &rcOwner);
1184
1185 // Offset the owner and dialog box rectangles so that
1186 // right and bottom values represent the width and
1187 // height, and then offset the owner again to discard
1188 // space taken up by the dialog box.
1189
1190 OffsetRect(&rcDlg, -rcDlg.left, -rcDlg.top);
1191 OffsetRect(&rc, -rc.left, -rc.top);
1192 OffsetRect(&rc, -rcDlg.right, -rcDlg.bottom);
1193
1194 // The new position is the sum of half the remaining
1195 // space and the owner's original position.
1196
1197 SetWindowPos(hDlg,
1198 HWND_TOP,
1199 rcOwner.left + (rc.right / 2),
1200 rcOwner.top + (rc.bottom / 2),
1201 0, 0, // ignores size arguments
1202 SWP_NOSIZE);
1203
1204 /* tackle the printdlg copiesctrl problem */
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001205 pPD = (PRINTDLGW *)lParam;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 pPD->nCopies = (WORD)pPD->lCustData;
1207 SetDlgItemInt( hDlg, edt3, pPD->nCopies, FALSE );
1208 /* Bring the window to top */
1209 BringWindowToTop(GetParent(hDlg));
1210 SetForegroundWindow(hDlg);
1211 }
1212
1213 return FALSE;
1214}
1215#endif
1216
1217 void
1218mch_print_cleanup(void)
1219{
1220 int pifItalic;
1221 int pifBold;
1222 int pifUnderline;
1223
1224 for (pifBold = 0; pifBold <= 1; pifBold++)
1225 for (pifItalic = 0; pifItalic <= 1; pifItalic++)
1226 for (pifUnderline = 0; pifUnderline <= 1; pifUnderline++)
1227 DeleteObject(prt_font_handles[pifBold][pifItalic][pifUnderline]);
1228
1229 if (prt_dlg.hDC != NULL)
1230 DeleteDC(prt_dlg.hDC);
1231 if (!*bUserAbort)
1232 SendMessage(hDlgPrint, WM_COMMAND, 0, 0);
1233}
1234
1235 static int
1236to_device_units(int idx, int dpi, int physsize, int offset, int def_number)
1237{
1238 int ret = 0;
1239 int u;
1240 int nr;
1241
1242 u = prt_get_unit(idx);
1243 if (u == PRT_UNIT_NONE)
1244 {
1245 u = PRT_UNIT_PERC;
1246 nr = def_number;
1247 }
1248 else
1249 nr = printer_opts[idx].number;
1250
1251 switch (u)
1252 {
1253 case PRT_UNIT_PERC:
1254 ret = (physsize * nr) / 100;
1255 break;
1256 case PRT_UNIT_INCH:
1257 ret = (nr * dpi);
1258 break;
1259 case PRT_UNIT_MM:
1260 ret = (nr * 10 * dpi) / 254;
1261 break;
1262 case PRT_UNIT_POINT:
1263 ret = (nr * 10 * dpi) / 720;
1264 break;
1265 }
1266
1267 if (ret < offset)
1268 return 0;
1269 else
1270 return ret - offset;
1271}
1272
1273 static int
1274prt_get_cpl(void)
1275{
1276 int hr;
1277 int phyw;
1278 int dvoff;
1279 int rev_offset;
1280 int dpi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281
1282 GetTextMetrics(prt_dlg.hDC, &prt_tm);
1283 prt_line_height = prt_tm.tmHeight + prt_tm.tmExternalLeading;
1284
1285 hr = GetDeviceCaps(prt_dlg.hDC, HORZRES);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 phyw = GetDeviceCaps(prt_dlg.hDC, PHYSICALWIDTH);
1287 dvoff = GetDeviceCaps(prt_dlg.hDC, PHYSICALOFFSETX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 dpi = GetDeviceCaps(prt_dlg.hDC, LOGPIXELSX);
1289
1290 rev_offset = phyw - (dvoff + hr);
1291
1292 prt_left_margin = to_device_units(OPT_PRINT_LEFT, dpi, phyw, dvoff, 10);
1293 if (prt_use_number())
1294 {
1295 prt_number_width = PRINT_NUMBER_WIDTH * prt_tm.tmAveCharWidth;
1296 prt_left_margin += prt_number_width;
1297 }
1298 else
1299 prt_number_width = 0;
1300
1301 prt_right_margin = hr - to_device_units(OPT_PRINT_RIGHT, dpi, phyw,
1302 rev_offset, 5);
1303
1304 return (prt_right_margin - prt_left_margin) / prt_tm.tmAveCharWidth;
1305}
1306
1307 static int
1308prt_get_lpp(void)
1309{
1310 int vr;
1311 int phyw;
1312 int dvoff;
1313 int rev_offset;
1314 int bottom_margin;
1315 int dpi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316
1317 vr = GetDeviceCaps(prt_dlg.hDC, VERTRES);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 phyw = GetDeviceCaps(prt_dlg.hDC, PHYSICALHEIGHT);
1319 dvoff = GetDeviceCaps(prt_dlg.hDC, PHYSICALOFFSETY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 dpi = GetDeviceCaps(prt_dlg.hDC, LOGPIXELSY);
1321
1322 rev_offset = phyw - (dvoff + vr);
1323
1324 prt_top_margin = to_device_units(OPT_PRINT_TOP, dpi, phyw, dvoff, 5);
1325
1326 /* adjust top margin if there is a header */
1327 prt_top_margin += prt_line_height * prt_header_height();
1328
1329 bottom_margin = vr - to_device_units(OPT_PRINT_BOT, dpi, phyw,
1330 rev_offset, 5);
1331
1332 return (bottom_margin - prt_top_margin) / prt_line_height;
1333}
1334
1335 int
1336mch_print_init(prt_settings_T *psettings, char_u *jobname, int forceit)
1337{
1338 static HGLOBAL stored_dm = NULL;
1339 static HGLOBAL stored_devn = NULL;
1340 static int stored_nCopies = 1;
1341 static int stored_nFlags = 0;
1342
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001343 LOGFONTW fLogFont;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 int pifItalic;
1345 int pifBold;
1346 int pifUnderline;
1347
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001348 DEVMODEW *mem;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 DEVNAMES *devname;
1350 int i;
1351
1352 bUserAbort = &(psettings->user_abort);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001353 vim_memset(&prt_dlg, 0, sizeof(PRINTDLGW));
1354 prt_dlg.lStructSize = sizeof(PRINTDLGW);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001355#if !defined(FEAT_GUI) || defined(VIMDLL)
1356# ifdef VIMDLL
1357 if (!gui.in_use)
1358# endif
1359 GetConsoleHwnd(); /* get value of s_hwnd */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360#endif
1361 prt_dlg.hwndOwner = s_hwnd;
1362 prt_dlg.Flags = PD_NOPAGENUMS | PD_NOSELECTION | PD_RETURNDC;
1363 if (!forceit)
1364 {
1365 prt_dlg.hDevMode = stored_dm;
1366 prt_dlg.hDevNames = stored_devn;
1367 prt_dlg.lCustData = stored_nCopies; // work around bug in print dialog
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001368#if !defined(FEAT_GUI) || defined(VIMDLL)
1369# ifdef VIMDLL
1370 if (!gui.in_use)
1371# endif
1372 {
1373 /*
1374 * Use hook to prevent console window being sent to back
1375 */
1376 prt_dlg.lpfnPrintHook = PrintHookProc;
1377 prt_dlg.Flags |= PD_ENABLEPRINTHOOK;
1378 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379#endif
1380 prt_dlg.Flags |= stored_nFlags;
1381 }
1382
1383 /*
1384 * If bang present, return default printer setup with no dialog
1385 * never show dialog if we are running over telnet
1386 */
1387 if (forceit
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001388#if !defined(FEAT_GUI) || defined(VIMDLL)
1389# ifdef VIMDLL
1390 || (!gui.in_use && !term_console)
1391# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 || !term_console
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001393# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394#endif
1395 )
1396 {
1397 prt_dlg.Flags |= PD_RETURNDEFAULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 /*
1399 * MSDN suggests setting the first parameter to WINSPOOL for
1400 * NT, but NULL appears to work just as well.
1401 */
1402 if (*p_pdev != NUL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001403 prt_dlg.hDC = CreateDC(NULL, (LPCSTR)p_pdev, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 {
1406 prt_dlg.Flags |= PD_RETURNDEFAULT;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001407 if (PrintDlgW(&prt_dlg) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 goto init_fail_dlg;
1409 }
1410 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001411 else if (PrintDlgW(&prt_dlg) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 goto init_fail_dlg;
1413 else
1414 {
1415 /*
1416 * keep the previous driver context
1417 */
1418 stored_dm = prt_dlg.hDevMode;
1419 stored_devn = prt_dlg.hDevNames;
1420 stored_nFlags = prt_dlg.Flags;
1421 stored_nCopies = prt_dlg.nCopies;
1422 }
1423
1424 if (prt_dlg.hDC == NULL)
1425 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001426 emsg(_("E237: Printer selection failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 mch_print_cleanup();
1428 return FALSE;
1429 }
1430
1431 /* Not all printer drivers report the support of color (or grey) in the
1432 * same way. Let's set has_color if there appears to be some way to print
1433 * more than B&W. */
1434 i = GetDeviceCaps(prt_dlg.hDC, NUMCOLORS);
1435 psettings->has_color = (GetDeviceCaps(prt_dlg.hDC, BITSPIXEL) > 1
1436 || GetDeviceCaps(prt_dlg.hDC, PLANES) > 1
1437 || i > 2 || i == -1);
1438
1439 /* Ensure all font styles are baseline aligned */
1440 SetTextAlign(prt_dlg.hDC, TA_BASELINE|TA_LEFT);
1441
1442 /*
1443 * On some windows systems the nCopies parameter is not
1444 * passed back correctly. It must be retrieved from the
1445 * hDevMode struct.
1446 */
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001447 mem = (DEVMODEW *)GlobalLock(prt_dlg.hDevMode);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 if (mem != NULL)
1449 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450 if (mem->dmCopies != 1)
1451 stored_nCopies = mem->dmCopies;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 if ((mem->dmFields & DM_DUPLEX) && (mem->dmDuplex & ~DMDUP_SIMPLEX))
1453 psettings->duplex = TRUE;
1454 if ((mem->dmFields & DM_COLOR) && (mem->dmColor & DMCOLOR_COLOR))
1455 psettings->has_color = TRUE;
1456 }
1457 GlobalUnlock(prt_dlg.hDevMode);
1458
1459 devname = (DEVNAMES *)GlobalLock(prt_dlg.hDevNames);
1460 if (devname != 0)
1461 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001462 WCHAR *wprinter_name = (WCHAR *)devname + devname->wDeviceOffset;
1463 WCHAR *wport_name = (WCHAR *)devname + devname->wOutputOffset;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001464 char_u *text = (char_u *)_("to %s on %s");
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001465 char_u *printer_name = utf16_to_enc(wprinter_name, NULL);
1466 char_u *port_name = utf16_to_enc(wport_name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001468 if (printer_name != NULL && port_name != NULL)
Bram Moolenaar964b3742019-05-24 18:54:09 +02001469 prt_name = alloc(STRLEN(printer_name)
1470 + STRLEN(port_name) + STRLEN(text));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 if (prt_name != NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001472 wsprintf((char *)prt_name, (const char *)text,
1473 printer_name, port_name);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001474 vim_free(printer_name);
1475 vim_free(port_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 }
1477 GlobalUnlock(prt_dlg.hDevNames);
1478
1479 /*
1480 * Initialise the font according to 'printfont'
1481 */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02001482 vim_memset(&fLogFont, 0, sizeof(fLogFont));
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001483 if (get_logfont(&fLogFont, p_pfn, prt_dlg.hDC, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001485 semsg(_("E613: Unknown printer font: %s"), p_pfn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 mch_print_cleanup();
1487 return FALSE;
1488 }
1489
1490 for (pifBold = 0; pifBold <= 1; pifBold++)
1491 for (pifItalic = 0; pifItalic <= 1; pifItalic++)
1492 for (pifUnderline = 0; pifUnderline <= 1; pifUnderline++)
1493 {
1494 fLogFont.lfWeight = boldface[pifBold];
1495 fLogFont.lfItalic = pifItalic;
1496 fLogFont.lfUnderline = pifUnderline;
1497 prt_font_handles[pifBold][pifItalic][pifUnderline]
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001498 = CreateFontIndirectW(&fLogFont);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 }
1500
1501 SetBkMode(prt_dlg.hDC, OPAQUE);
1502 SelectObject(prt_dlg.hDC, prt_font_handles[0][0][0]);
1503
1504 /*
1505 * Fill in the settings struct
1506 */
1507 psettings->chars_per_line = prt_get_cpl();
1508 psettings->lines_per_page = prt_get_lpp();
Bram Moolenaar7ddc6422014-09-27 11:18:19 +02001509 if (prt_dlg.Flags & PD_USEDEVMODECOPIESANDCOLLATE)
1510 {
1511 psettings->n_collated_copies = (prt_dlg.Flags & PD_COLLATE)
1512 ? prt_dlg.nCopies : 1;
1513 psettings->n_uncollated_copies = (prt_dlg.Flags & PD_COLLATE)
1514 ? 1 : prt_dlg.nCopies;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515
Bram Moolenaar7ddc6422014-09-27 11:18:19 +02001516 if (psettings->n_collated_copies == 0)
1517 psettings->n_collated_copies = 1;
1518
1519 if (psettings->n_uncollated_copies == 0)
1520 psettings->n_uncollated_copies = 1;
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01001521 }
1522 else
1523 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 psettings->n_collated_copies = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 psettings->n_uncollated_copies = 1;
Bram Moolenaar7ddc6422014-09-27 11:18:19 +02001526 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527
1528 psettings->jobname = jobname;
1529
1530 return TRUE;
1531
1532init_fail_dlg:
1533 {
1534 DWORD err = CommDlgExtendedError();
1535
1536 if (err)
1537 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 char_u *buf;
1539
1540 /* I suspect FormatMessage() doesn't work for values returned by
1541 * CommDlgExtendedError(). What does? */
1542 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
1543 FORMAT_MESSAGE_FROM_SYSTEM |
1544 FORMAT_MESSAGE_IGNORE_INSERTS,
1545 NULL, err, 0, (LPTSTR)(&buf), 0, NULL);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001546 semsg(_("E238: Print error: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 buf == NULL ? (char_u *)_("Unknown") : buf);
1548 LocalFree((LPVOID)(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 }
1550 else
1551 msg_clr_eos(); /* Maybe canceled */
1552
1553 mch_print_cleanup();
1554 return FALSE;
1555 }
1556}
1557
1558
1559 int
1560mch_print_begin(prt_settings_T *psettings)
1561{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001562 int ret = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 char szBuffer[300];
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001564 WCHAR *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001566 hDlgPrint = CreateDialog(g_hinst, TEXT("PrintDlgBox"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 prt_dlg.hwndOwner, PrintDlgProc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 SetAbortProc(prt_dlg.hDC, AbortProc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 wsprintf(szBuffer, _("Printing '%s'"), gettail(psettings->jobname));
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001570 vimSetDlgItemText(hDlgPrint, IDC_PRINTTEXT1, (char_u *)szBuffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001572 wp = enc_to_utf16(psettings->jobname, NULL);
Bram Moolenaar2290b1f2018-05-13 17:30:45 +02001573 if (wp != NULL)
1574 {
1575 DOCINFOW di;
1576
1577 vim_memset(&di, 0, sizeof(di));
1578 di.cbSize = sizeof(di);
1579 di.lpszDocName = wp;
1580 ret = StartDocW(prt_dlg.hDC, &di);
1581 vim_free(wp);
1582 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583
1584#ifdef FEAT_GUI
1585 /* Give focus back to main window (when using MDI). */
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001586# ifdef VIMDLL
1587 if (gui.in_use)
1588# endif
1589 SetFocus(s_hwnd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590#endif
1591
1592 return (ret > 0);
1593}
1594
1595 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01001596mch_print_end(prt_settings_T *psettings UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597{
1598 EndDoc(prt_dlg.hDC);
1599 if (!*bUserAbort)
1600 SendMessage(hDlgPrint, WM_COMMAND, 0, 0);
1601}
1602
1603 int
1604mch_print_end_page(void)
1605{
1606 return (EndPage(prt_dlg.hDC) > 0);
1607}
1608
1609 int
1610mch_print_begin_page(char_u *msg)
1611{
1612 if (msg != NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001613 vimSetDlgItemText(hDlgPrint, IDC_PROGRESS, msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 return (StartPage(prt_dlg.hDC) > 0);
1615}
1616
1617 int
1618mch_print_blank_page(void)
1619{
1620 return (mch_print_begin_page(NULL) ? (mch_print_end_page()) : FALSE);
1621}
1622
1623static int prt_pos_x = 0;
1624static int prt_pos_y = 0;
1625
1626 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001627mch_print_start_line(int margin, int page_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628{
1629 if (margin)
1630 prt_pos_x = -prt_number_width;
1631 else
1632 prt_pos_x = 0;
1633 prt_pos_y = page_line * prt_line_height
1634 + prt_tm.tmAscent + prt_tm.tmExternalLeading;
1635}
1636
1637 int
1638mch_print_text_out(char_u *p, int len)
1639{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 SIZE sz;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001641 WCHAR *wp;
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001642 int wlen = len;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001643 int ret = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001645 wp = enc_to_utf16(p, &wlen);
1646 if (wp == NULL)
1647 return FALSE;
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001648
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001649 TextOutW(prt_dlg.hDC, prt_pos_x + prt_left_margin,
1650 prt_pos_y + prt_top_margin, wp, wlen);
1651 GetTextExtentPoint32W(prt_dlg.hDC, wp, wlen, &sz);
1652 vim_free(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 prt_pos_x += (sz.cx - prt_tm.tmOverhang);
1654 /* This is wrong when printing spaces for a TAB. */
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001655 if (p[len] != NUL)
1656 {
1657 wlen = MB_PTR2LEN(p + len);
1658 wp = enc_to_utf16(p + len, &wlen);
1659 if (wp != NULL)
1660 {
1661 GetTextExtentPoint32W(prt_dlg.hDC, wp, 1, &sz);
1662 ret = (prt_pos_x + prt_left_margin + sz.cx > prt_right_margin);
1663 vim_free(wp);
1664 }
1665 }
1666 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667}
1668
1669 void
1670mch_print_set_font(int iBold, int iItalic, int iUnderline)
1671{
1672 SelectObject(prt_dlg.hDC, prt_font_handles[iBold][iItalic][iUnderline]);
1673}
1674
1675 void
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001676mch_print_set_bg(long_u bgcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677{
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001678 SetBkColor(prt_dlg.hDC, GetNearestColor(prt_dlg.hDC,
1679 swap_me((COLORREF)bgcol)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 /*
1681 * With a white background we can draw characters transparent, which is
1682 * good for italic characters that overlap to the next char cell.
1683 */
1684 if (bgcol == 0xffffffUL)
1685 SetBkMode(prt_dlg.hDC, TRANSPARENT);
1686 else
1687 SetBkMode(prt_dlg.hDC, OPAQUE);
1688}
1689
1690 void
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001691mch_print_set_fg(long_u fgcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692{
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001693 SetTextColor(prt_dlg.hDC, GetNearestColor(prt_dlg.hDC,
1694 swap_me((COLORREF)fgcol)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695}
1696
1697#endif /*FEAT_PRINTER && !FEAT_POSTSCRIPT*/
1698
Bram Moolenaar58d98232005-07-23 22:25:46 +00001699
1700
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701#if defined(FEAT_SHORTCUT) || defined(PROTO)
Bram Moolenaar82881492012-11-20 16:53:39 +01001702# ifndef PROTO
1703# include <shlobj.h>
1704# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705
Bram Moolenaardce1e892019-02-10 23:18:53 +01001706typedef enum _FILE_INFO_BY_HANDLE_CLASS_ {
1707 FileBasicInfo_,
1708 FileStandardInfo_,
1709 FileNameInfo_,
1710 FileRenameInfo_,
1711 FileDispositionInfo_,
1712 FileAllocationInfo_,
1713 FileEndOfFileInfo_,
1714 FileStreamInfo_,
1715 FileCompressionInfo_,
1716 FileAttributeTagInfo_,
1717 FileIdBothDirectoryInfo_,
1718 FileIdBothDirectoryRestartInfo_,
1719 FileIoPriorityHintInfo_,
1720 FileRemoteProtocolInfo_,
1721 FileFullDirectoryInfo_,
1722 FileFullDirectoryRestartInfo_,
1723 FileStorageInfo_,
1724 FileAlignmentInfo_,
1725 FileIdInfo_,
1726 FileIdExtdDirectoryInfo_,
1727 FileIdExtdDirectoryRestartInfo_,
1728 FileDispositionInfoEx_,
1729 FileRenameInfoEx_,
1730 MaximumFileInfoByHandleClass_
1731} FILE_INFO_BY_HANDLE_CLASS_;
1732
1733typedef struct _FILE_NAME_INFO_ {
1734 DWORD FileNameLength;
1735 WCHAR FileName[1];
1736} FILE_NAME_INFO_;
1737
1738typedef BOOL (WINAPI *pfnGetFileInformationByHandleEx)(
1739 HANDLE hFile,
1740 FILE_INFO_BY_HANDLE_CLASS_ FileInformationClass,
1741 LPVOID lpFileInformation,
1742 DWORD dwBufferSize);
1743static pfnGetFileInformationByHandleEx pGetFileInformationByHandleEx = NULL;
1744
1745typedef BOOL (WINAPI *pfnGetVolumeInformationByHandleW)(
1746 HANDLE hFile,
1747 LPWSTR lpVolumeNameBuffer,
1748 DWORD nVolumeNameSize,
1749 LPDWORD lpVolumeSerialNumber,
1750 LPDWORD lpMaximumComponentLength,
1751 LPDWORD lpFileSystemFlags,
1752 LPWSTR lpFileSystemNameBuffer,
1753 DWORD nFileSystemNameSize);
1754static pfnGetVolumeInformationByHandleW pGetVolumeInformationByHandleW = NULL;
1755
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02001756 static char_u *
Bram Moolenaardce1e892019-02-10 23:18:53 +01001757resolve_reparse_point(char_u *fname)
1758{
1759 HANDLE h = INVALID_HANDLE_VALUE;
1760 DWORD size;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001761 WCHAR *p;
Bram Moolenaardce1e892019-02-10 23:18:53 +01001762 char_u *rfname = NULL;
1763 FILE_NAME_INFO_ *nameinfo = NULL;
1764 WCHAR buff[MAX_PATH], *volnames = NULL;
1765 HANDLE hv;
1766 DWORD snfile, snfind;
1767 static BOOL loaded = FALSE;
1768
1769 if (pGetFileInformationByHandleEx == NULL ||
1770 pGetVolumeInformationByHandleW == NULL)
1771 {
1772 HMODULE hmod = GetModuleHandle("kernel32.dll");
1773
1774 if (loaded == TRUE)
1775 return NULL;
1776 pGetFileInformationByHandleEx = (pfnGetFileInformationByHandleEx)
1777 GetProcAddress(hmod, "GetFileInformationByHandleEx");
1778 pGetVolumeInformationByHandleW = (pfnGetVolumeInformationByHandleW)
1779 GetProcAddress(hmod, "GetVolumeInformationByHandleW");
1780 loaded = TRUE;
1781 if (pGetFileInformationByHandleEx == NULL ||
1782 pGetVolumeInformationByHandleW == NULL)
1783 return NULL;
1784 }
1785
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001786 p = enc_to_utf16(fname, NULL);
1787 if (p == NULL)
1788 goto fail;
1789
1790 if ((GetFileAttributesW(p) & FILE_ATTRIBUTE_REPARSE_POINT) == 0)
Bram Moolenaardce1e892019-02-10 23:18:53 +01001791 {
Bram Moolenaardce1e892019-02-10 23:18:53 +01001792 vim_free(p);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001793 goto fail;
Bram Moolenaardce1e892019-02-10 23:18:53 +01001794 }
Bram Moolenaardce1e892019-02-10 23:18:53 +01001795
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001796 h = CreateFileW(p, 0, 0, NULL, OPEN_EXISTING,
1797 FILE_FLAG_BACKUP_SEMANTICS, NULL);
1798 vim_free(p);
Bram Moolenaardce1e892019-02-10 23:18:53 +01001799
1800 if (h == INVALID_HANDLE_VALUE)
1801 goto fail;
1802
1803 size = sizeof(FILE_NAME_INFO_) + sizeof(WCHAR) * (MAX_PATH - 1);
1804 nameinfo = (FILE_NAME_INFO_*)alloc(size + sizeof(WCHAR));
1805 if (nameinfo == NULL)
1806 goto fail;
1807
1808 if (!pGetFileInformationByHandleEx(h, FileNameInfo_, nameinfo, size))
1809 goto fail;
1810
1811 nameinfo->FileName[nameinfo->FileNameLength / sizeof(WCHAR)] = 0;
1812
1813 if (!pGetVolumeInformationByHandleW(
1814 h, NULL, 0, &snfile, NULL, NULL, NULL, 0))
1815 goto fail;
1816
1817 hv = FindFirstVolumeW(buff, MAX_PATH);
1818 if (hv == INVALID_HANDLE_VALUE)
1819 goto fail;
1820
1821 do {
1822 GetVolumeInformationW(
1823 buff, NULL, 0, &snfind, NULL, NULL, NULL, 0);
1824 if (snfind == snfile)
1825 break;
1826 } while (FindNextVolumeW(hv, buff, MAX_PATH));
1827
1828 FindVolumeClose(hv);
1829
1830 if (snfind != snfile)
1831 goto fail;
1832
1833 size = 0;
1834 if (!GetVolumePathNamesForVolumeNameW(buff, NULL, 0, &size) &&
1835 GetLastError() != ERROR_MORE_DATA)
1836 goto fail;
1837
1838 volnames = (WCHAR*)alloc(size * sizeof(WCHAR));
1839 if (!GetVolumePathNamesForVolumeNameW(buff, volnames, size,
1840 &size))
1841 goto fail;
1842
1843 wcscpy(buff, volnames);
1844 if (nameinfo->FileName[0] == '\\')
1845 wcscat(buff, nameinfo->FileName + 1);
1846 else
1847 wcscat(buff, nameinfo->FileName);
1848 rfname = utf16_to_enc(buff, NULL);
1849
1850fail:
1851 if (h != INVALID_HANDLE_VALUE)
1852 CloseHandle(h);
1853 if (nameinfo != NULL)
1854 vim_free(nameinfo);
1855 if (volnames != NULL)
1856 vim_free(volnames);
1857
1858 return rfname;
1859}
1860
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861/*
1862 * When "fname" is the name of a shortcut (*.lnk) resolve the file it points
1863 * to and return that name in allocated memory.
1864 * Otherwise NULL is returned.
1865 */
Bram Moolenaardce1e892019-02-10 23:18:53 +01001866 static char_u *
1867resolve_shortcut(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868{
1869 HRESULT hr;
1870 IShellLink *psl = NULL;
1871 IPersistFile *ppf = NULL;
1872 OLECHAR wsz[MAX_PATH];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 char_u *rfname = NULL;
1874 int len;
Bram Moolenaar604729e2013-08-30 16:44:19 +02001875 IShellLinkW *pslw = NULL;
1876 WIN32_FIND_DATAW ffdw; // we get those free of charge
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877
1878 /* Check if the file name ends in ".lnk". Avoid calling
1879 * CoCreateInstance(), it's quite slow. */
1880 if (fname == NULL)
1881 return rfname;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001882 len = (int)STRLEN(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 if (len <= 4 || STRNICMP(fname + len - 4, ".lnk", 4) != 0)
1884 return rfname;
1885
1886 CoInitialize(NULL);
1887
1888 // create a link manager object and request its interface
1889 hr = CoCreateInstance(
1890 &CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001891 &IID_IShellLinkW, (void**)&pslw);
1892 if (hr == S_OK)
1893 {
1894 WCHAR *p = enc_to_utf16(fname, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001896 if (p != NULL)
1897 {
1898 // Get a pointer to the IPersistFile interface.
1899 hr = pslw->lpVtbl->QueryInterface(
1900 pslw, &IID_IPersistFile, (void**)&ppf);
1901 if (hr != S_OK)
1902 goto shortcut_errorw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001904 // "load" the name and resolve the link
1905 hr = ppf->lpVtbl->Load(ppf, p, STGM_READ);
1906 if (hr != S_OK)
1907 goto shortcut_errorw;
1908# if 0 // This makes Vim wait a long time if the target does not exist.
1909 hr = pslw->lpVtbl->Resolve(pslw, NULL, SLR_NO_UI);
1910 if (hr != S_OK)
1911 goto shortcut_errorw;
Bram Moolenaar604729e2013-08-30 16:44:19 +02001912# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001914 // Get the path to the link target.
1915 ZeroMemory(wsz, MAX_PATH * sizeof(WCHAR));
1916 hr = pslw->lpVtbl->GetPath(pslw, wsz, MAX_PATH, &ffdw, 0);
1917 if (hr == S_OK && wsz[0] != NUL)
1918 rfname = utf16_to_enc(wsz, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001920shortcut_errorw:
1921 vim_free(p);
1922 }
1923 }
1924
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 // Release all interface pointers (both belong to the same object)
1926 if (ppf != NULL)
1927 ppf->lpVtbl->Release(ppf);
1928 if (psl != NULL)
1929 psl->lpVtbl->Release(psl);
Bram Moolenaar604729e2013-08-30 16:44:19 +02001930 if (pslw != NULL)
1931 pslw->lpVtbl->Release(pslw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932
1933 CoUninitialize();
1934 return rfname;
1935}
Bram Moolenaardce1e892019-02-10 23:18:53 +01001936
1937 char_u *
1938mch_resolve_path(char_u *fname, int reparse_point)
1939{
1940 char_u *path = resolve_shortcut(fname);
1941
1942 if (path == NULL && reparse_point)
1943 path = resolve_reparse_point(fname);
1944 return path;
1945}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946#endif
1947
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001948#if (defined(FEAT_EVAL) && (!defined(FEAT_GUI) || defined(VIMDLL))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949/*
1950 * Bring ourselves to the foreground. Does work if the OS doesn't allow it.
1951 */
1952 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001953win32_set_foreground(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 GetConsoleHwnd(); /* get value of s_hwnd */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 if (s_hwnd != 0)
1957 SetForegroundWindow(s_hwnd);
1958}
1959#endif
1960
1961#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
1962/*
1963 * Client-server code for Vim
1964 *
1965 * Originally written by Paul Moore
1966 */
1967
1968/* In order to handle inter-process messages, we need to have a window. But
1969 * the functions in this module can be called before the main GUI window is
1970 * created (and may also be called in the console version, where there is no
1971 * GUI window at all).
1972 *
1973 * So we create a hidden window, and arrange to destroy it on exit.
1974 */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001975HWND message_window = 0; /* window that's handling messages */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976
1977#define VIM_CLASSNAME "VIM_MESSAGES"
1978#define VIM_CLASSNAME_LEN (sizeof(VIM_CLASSNAME) - 1)
1979
1980/* Communication is via WM_COPYDATA messages. The message type is send in
1981 * the dwData parameter. Types are defined here. */
1982#define COPYDATA_KEYS 0
1983#define COPYDATA_REPLY 1
1984#define COPYDATA_EXPR 10
1985#define COPYDATA_RESULT 11
1986#define COPYDATA_ERROR_RESULT 12
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001987#define COPYDATA_ENCODING 20
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988
1989/* This is a structure containing a server HWND and its name. */
1990struct server_id
1991{
1992 HWND hwnd;
1993 char_u *name;
1994};
1995
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001996/* Last received 'encoding' that the client uses. */
1997static char_u *client_enc = NULL;
1998
1999/*
2000 * Tell the other side what encoding we are using.
2001 * Errors are ignored.
2002 */
2003 static void
2004serverSendEnc(HWND target)
2005{
2006 COPYDATASTRUCT data;
2007
2008 data.dwData = COPYDATA_ENCODING;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002009 data.cbData = (DWORD)STRLEN(p_enc) + 1;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002010 data.lpData = p_enc;
2011 (void)SendMessage(target, WM_COPYDATA, (WPARAM)message_window,
2012 (LPARAM)(&data));
2013}
2014
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015/*
2016 * Clean up on exit. This destroys the hidden message window.
2017 */
2018 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019CleanUpMessaging(void)
2020{
2021 if (message_window != 0)
2022 {
2023 DestroyWindow(message_window);
2024 message_window = 0;
2025 }
2026}
2027
2028static int save_reply(HWND server, char_u *reply, int expr);
2029
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002030/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 * The window procedure for the hidden message window.
2032 * It handles callback messages and notifications from servers.
2033 * In order to process these messages, it is necessary to run a
2034 * message loop. Code which may run before the main message loop
2035 * is started (in the GUI) is careful to pump messages when it needs
2036 * to. Features which require message delivery during normal use will
2037 * not work in the console version - this basically means those
2038 * features which allow Vim to act as a server, rather than a client.
2039 */
2040 static LRESULT CALLBACK
2041Messaging_WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
2042{
2043 if (msg == WM_COPYDATA)
2044 {
2045 /* This is a message from another Vim. The dwData member of the
2046 * COPYDATASTRUCT determines the type of message:
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002047 * COPYDATA_ENCODING:
2048 * The encoding that the client uses. Following messages will
2049 * use this encoding, convert if needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 * COPYDATA_KEYS:
2051 * A key sequence. We are a server, and a client wants these keys
2052 * adding to the input queue.
2053 * COPYDATA_REPLY:
2054 * A reply. We are a client, and a server has sent this message
2055 * in response to a request. (server2client())
2056 * COPYDATA_EXPR:
2057 * An expression. We are a server, and a client wants us to
2058 * evaluate this expression.
2059 * COPYDATA_RESULT:
2060 * A reply. We are a client, and a server has sent this message
2061 * in response to a COPYDATA_EXPR.
2062 * COPYDATA_ERROR_RESULT:
2063 * A reply. We are a client, and a server has sent this message
2064 * in response to a COPYDATA_EXPR that failed to evaluate.
2065 */
2066 COPYDATASTRUCT *data = (COPYDATASTRUCT*)lParam;
2067 HWND sender = (HWND)wParam;
2068 COPYDATASTRUCT reply;
2069 char_u *res;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 int retval;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002071 char_u *str;
2072 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073
2074 switch (data->dwData)
2075 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002076 case COPYDATA_ENCODING:
2077 /* Remember the encoding that the client uses. */
2078 vim_free(client_enc);
2079 client_enc = enc_canonize((char_u *)data->lpData);
2080 return 1;
2081
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 case COPYDATA_KEYS:
2083 /* Remember who sent this, for <client> */
2084 clientWindow = sender;
2085
2086 /* Add the received keys to the input buffer. The loop waiting
2087 * for the user to do something should check the input buffer. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002088 str = serverConvert(client_enc, (char_u *)data->lpData, &tofree);
2089 server_to_input_buf(str);
2090 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091
2092# ifdef FEAT_GUI
2093 /* Wake up the main GUI loop. */
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002094# ifdef VIMDLL
2095 if (gui.in_use)
2096# endif
2097 if (s_hwnd != 0)
2098 PostMessage(s_hwnd, WM_NULL, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099# endif
2100 return 1;
2101
2102 case COPYDATA_EXPR:
2103 /* Remember who sent this, for <client> */
2104 clientWindow = sender;
2105
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002106 str = serverConvert(client_enc, (char_u *)data->lpData, &tofree);
2107 res = eval_client_expr_to_string(str);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002108
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 if (res == NULL)
2110 {
Bram Moolenaar15bf76d2017-03-18 16:18:37 +01002111 char *err = _(e_invexprmsg);
2112 size_t len = STRLEN(str) + STRLEN(err) + 5;
2113
Bram Moolenaar964b3742019-05-24 18:54:09 +02002114 res = alloc(len);
Bram Moolenaar15bf76d2017-03-18 16:18:37 +01002115 if (res != NULL)
2116 vim_snprintf((char *)res, len, "%s: \"%s\"", err, str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 reply.dwData = COPYDATA_ERROR_RESULT;
2118 }
2119 else
2120 reply.dwData = COPYDATA_RESULT;
2121 reply.lpData = res;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002122 reply.cbData = (DWORD)STRLEN(res) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002124 serverSendEnc(sender);
Bram Moolenaar5246cd72013-06-16 16:41:47 +02002125 retval = (int)SendMessage(sender, WM_COPYDATA,
2126 (WPARAM)message_window, (LPARAM)(&reply));
Bram Moolenaar15bf76d2017-03-18 16:18:37 +01002127 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 vim_free(res);
2129 return retval;
2130
2131 case COPYDATA_REPLY:
2132 case COPYDATA_RESULT:
2133 case COPYDATA_ERROR_RESULT:
2134 if (data->lpData != NULL)
2135 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002136 str = serverConvert(client_enc, (char_u *)data->lpData,
2137 &tofree);
2138 if (tofree == NULL)
2139 str = vim_strsave(str);
2140 if (save_reply(sender, str,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 (data->dwData == COPYDATA_REPLY ? 0 :
2142 (data->dwData == COPYDATA_RESULT ? 1 :
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002143 2))) == FAIL)
2144 vim_free(str);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002145 else if (data->dwData == COPYDATA_REPLY)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 {
Bram Moolenaar427d51c2013-06-16 16:01:25 +02002147 char_u winstr[30];
2148
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002149 sprintf((char *)winstr, PRINTF_HEX_LONG_U, (long_u)sender);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002150 apply_autocmds(EVENT_REMOTEREPLY, winstr, str,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 TRUE, curbuf);
2152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 }
2154 return 1;
2155 }
2156
2157 return 0;
2158 }
2159
2160 else if (msg == WM_ACTIVATE && wParam == WA_ACTIVE)
2161 {
2162 /* When the message window is activated (brought to the foreground),
2163 * this actually applies to the text window. */
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002164#if !defined(FEAT_GUI) || defined(VIMDLL)
2165# ifdef VIMDLL
2166 if (!gui.in_use)
2167# endif
2168 GetConsoleHwnd(); /* get value of s_hwnd */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169#endif
2170 if (s_hwnd != 0)
2171 {
2172 SetForegroundWindow(s_hwnd);
2173 return 0;
2174 }
2175 }
2176
2177 return DefWindowProc(hwnd, msg, wParam, lParam);
2178}
2179
2180/*
2181 * Initialise the message handling process. This involves creating a window
2182 * to handle messages - the window will not be visible.
2183 */
2184 void
2185serverInitMessaging(void)
2186{
2187 WNDCLASS wndclass;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188
2189 /* Clean up on exit */
2190 atexit(CleanUpMessaging);
2191
2192 /* Register a window class - we only really care
2193 * about the window procedure
2194 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 wndclass.style = 0;
2196 wndclass.lpfnWndProc = Messaging_WndProc;
2197 wndclass.cbClsExtra = 0;
2198 wndclass.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002199 wndclass.hInstance = g_hinst;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 wndclass.hIcon = NULL;
2201 wndclass.hCursor = NULL;
2202 wndclass.hbrBackground = NULL;
2203 wndclass.lpszMenuName = NULL;
2204 wndclass.lpszClassName = VIM_CLASSNAME;
2205 RegisterClass(&wndclass);
2206
2207 /* Create the message window. It will be hidden, so the details don't
2208 * matter. Don't use WS_OVERLAPPEDWINDOW, it will make a shortcut remove
2209 * focus from gvim. */
2210 message_window = CreateWindow(VIM_CLASSNAME, "",
2211 WS_POPUPWINDOW | WS_CAPTION,
2212 CW_USEDEFAULT, CW_USEDEFAULT,
2213 100, 100, NULL, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002214 g_hinst, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215}
2216
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002217/* Used by serverSendToVim() to find an alternate server name. */
2218static char_u *altname_buf_ptr = NULL;
2219
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220/*
2221 * Get the title of the window "hwnd", which is the Vim server name, in
2222 * "name[namelen]" and return the length.
2223 * Returns zero if window "hwnd" is not a Vim server.
2224 */
2225 static int
2226getVimServerName(HWND hwnd, char *name, int namelen)
2227{
2228 int len;
2229 char buffer[VIM_CLASSNAME_LEN + 1];
2230
2231 /* Ignore windows which aren't Vim message windows */
2232 len = GetClassName(hwnd, buffer, sizeof(buffer));
2233 if (len != VIM_CLASSNAME_LEN || STRCMP(buffer, VIM_CLASSNAME) != 0)
2234 return 0;
2235
2236 /* Get the title of the window */
2237 return GetWindowText(hwnd, name, namelen);
2238}
2239
2240 static BOOL CALLBACK
2241enumWindowsGetServer(HWND hwnd, LPARAM lparam)
2242{
2243 struct server_id *id = (struct server_id *)lparam;
2244 char server[MAX_PATH];
2245
2246 /* Get the title of the window */
2247 if (getVimServerName(hwnd, server, sizeof(server)) == 0)
2248 return TRUE;
2249
2250 /* If this is the server we're looking for, return its HWND */
2251 if (STRICMP(server, id->name) == 0)
2252 {
2253 id->hwnd = hwnd;
2254 return FALSE;
2255 }
2256
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002257 /* If we are looking for an alternate server, remember this name. */
2258 if (altname_buf_ptr != NULL
2259 && STRNICMP(server, id->name, STRLEN(id->name)) == 0
2260 && vim_isdigit(server[STRLEN(id->name)]))
2261 {
2262 STRCPY(altname_buf_ptr, server);
2263 altname_buf_ptr = NULL; /* don't use another name */
2264 }
2265
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 /* Otherwise, keep looking */
2267 return TRUE;
2268}
2269
2270 static BOOL CALLBACK
2271enumWindowsGetNames(HWND hwnd, LPARAM lparam)
2272{
2273 garray_T *ga = (garray_T *)lparam;
2274 char server[MAX_PATH];
2275
2276 /* Get the title of the window */
2277 if (getVimServerName(hwnd, server, sizeof(server)) == 0)
2278 return TRUE;
2279
2280 /* Add the name to the list */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002281 ga_concat(ga, (char_u *)server);
2282 ga_concat(ga, (char_u *)"\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 return TRUE;
2284}
2285
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002286struct enum_windows_s
2287{
2288 WNDENUMPROC lpEnumFunc;
2289 LPARAM lParam;
2290};
2291
2292 static BOOL CALLBACK
2293enum_windows_child(HWND hwnd, LPARAM lParam)
2294{
2295 struct enum_windows_s *ew = (struct enum_windows_s *)lParam;
2296
2297 return (ew->lpEnumFunc)(hwnd, ew->lParam);
2298}
2299
2300 static BOOL CALLBACK
2301enum_windows_toplevel(HWND hwnd, LPARAM lParam)
2302{
2303 struct enum_windows_s *ew = (struct enum_windows_s *)lParam;
2304
Bram Moolenaar95ba5c32018-10-07 22:47:07 +02002305 if ((ew->lpEnumFunc)(hwnd, ew->lParam))
2306 return TRUE;
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002307 return EnumChildWindows(hwnd, enum_windows_child, lParam);
2308}
2309
2310/* Enumerate all windows including children. */
2311 static BOOL
2312enum_windows(WNDENUMPROC lpEnumFunc, LPARAM lParam)
2313{
2314 struct enum_windows_s ew;
2315
2316 ew.lpEnumFunc = lpEnumFunc;
2317 ew.lParam = lParam;
2318 return EnumWindows(enum_windows_toplevel, (LPARAM)&ew);
2319}
2320
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 static HWND
2322findServer(char_u *name)
2323{
2324 struct server_id id;
2325
2326 id.name = name;
2327 id.hwnd = 0;
2328
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002329 enum_windows(enumWindowsGetServer, (LPARAM)(&id));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330
2331 return id.hwnd;
2332}
2333
2334 void
2335serverSetName(char_u *name)
2336{
2337 char_u *ok_name;
2338 HWND hwnd = 0;
2339 int i = 0;
2340 char_u *p;
2341
2342 /* Leave enough space for a 9-digit suffix to ensure uniqueness! */
Bram Moolenaar964b3742019-05-24 18:54:09 +02002343 ok_name = alloc(STRLEN(name) + 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344
2345 STRCPY(ok_name, name);
2346 p = ok_name + STRLEN(name);
2347
2348 for (;;)
2349 {
2350 /* This is inefficient - we're doing an EnumWindows loop for each
2351 * possible name. It would be better to grab all names in one go,
2352 * and scan the list each time...
2353 */
2354 hwnd = findServer(ok_name);
2355 if (hwnd == 0)
2356 break;
2357
2358 ++i;
2359 if (i >= 1000)
2360 break;
2361
2362 sprintf((char *)p, "%d", i);
2363 }
2364
2365 if (hwnd != 0)
2366 vim_free(ok_name);
2367 else
2368 {
2369 /* Remember the name */
2370 serverName = ok_name;
2371#ifdef FEAT_TITLE
2372 need_maketitle = TRUE; /* update Vim window title later */
2373#endif
2374
2375 /* Update the message window title */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002376 SetWindowText(message_window, (LPCSTR)ok_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377
2378#ifdef FEAT_EVAL
2379 /* Set the servername variable */
2380 set_vim_var_string(VV_SEND_SERVER, serverName, -1);
2381#endif
2382 }
2383}
2384
2385 char_u *
2386serverGetVimNames(void)
2387{
2388 garray_T ga;
2389
2390 ga_init2(&ga, 1, 100);
2391
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002392 enum_windows(enumWindowsGetNames, (LPARAM)(&ga));
Bram Moolenaar269ec652004-07-29 08:43:53 +00002393 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394
2395 return ga.ga_data;
2396}
2397
2398 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002399serverSendReply(
2400 char_u *name, /* Where to send. */
2401 char_u *reply) /* What to send. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402{
2403 HWND target;
2404 COPYDATASTRUCT data;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002405 long_u n = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406
2407 /* The "name" argument is a magic cookie obtained from expand("<client>").
2408 * It should be of the form 0xXXXXX - i.e. a C hex literal, which is the
2409 * value of the client's message window HWND.
2410 */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002411 sscanf((char *)name, SCANF_HEX_LONG_U, &n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 if (n == 0)
2413 return -1;
2414
2415 target = (HWND)n;
2416 if (!IsWindow(target))
2417 return -1;
2418
2419 data.dwData = COPYDATA_REPLY;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002420 data.cbData = (DWORD)STRLEN(reply) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 data.lpData = reply;
2422
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002423 serverSendEnc(target);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 if (SendMessage(target, WM_COPYDATA, (WPARAM)message_window,
2425 (LPARAM)(&data)))
2426 return 0;
2427
2428 return -1;
2429}
2430
2431 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002432serverSendToVim(
2433 char_u *name, /* Where to send. */
2434 char_u *cmd, /* What to send. */
2435 char_u **result, /* Result of eval'ed expression */
2436 void *ptarget, /* HWND of server */
2437 int asExpr, /* Expression or keys? */
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002438 int timeout, /* timeout in seconds or zero */
Bram Moolenaar05540972016-01-30 20:31:25 +01002439 int silent) /* don't complain about no server */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440{
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002441 HWND target;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 COPYDATASTRUCT data;
2443 char_u *retval = NULL;
2444 int retcode = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002445 char_u altname_buf[MAX_PATH];
2446
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01002447 /* Execute locally if no display or target is ourselves */
2448 if (serverName != NULL && STRICMP(name, serverName) == 0)
2449 return sendToLocalVim(cmd, asExpr, result);
2450
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002451 /* If the server name does not end in a digit then we look for an
2452 * alternate name. e.g. when "name" is GVIM the we may find GVIM2. */
2453 if (STRLEN(name) > 1 && !vim_isdigit(name[STRLEN(name) - 1]))
2454 altname_buf_ptr = altname_buf;
2455 altname_buf[0] = NUL;
2456 target = findServer(name);
2457 altname_buf_ptr = NULL;
2458 if (target == 0 && altname_buf[0] != NUL)
2459 /* Use another server name we found. */
2460 target = findServer(altname_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461
2462 if (target == 0)
2463 {
2464 if (!silent)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002465 semsg(_(e_noserver), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 return -1;
2467 }
2468
2469 if (ptarget)
2470 *(HWND *)ptarget = target;
2471
2472 data.dwData = asExpr ? COPYDATA_EXPR : COPYDATA_KEYS;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002473 data.cbData = (DWORD)STRLEN(cmd) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 data.lpData = cmd;
2475
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002476 serverSendEnc(target);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 if (SendMessage(target, WM_COPYDATA, (WPARAM)message_window,
2478 (LPARAM)(&data)) == 0)
2479 return -1;
2480
2481 if (asExpr)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002482 retval = serverGetReply(target, &retcode, TRUE, TRUE, timeout);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483
2484 if (result == NULL)
2485 vim_free(retval);
2486 else
2487 *result = retval; /* Caller assumes responsibility for freeing */
2488
2489 return retcode;
2490}
2491
2492/*
2493 * Bring the server to the foreground.
2494 */
2495 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002496serverForeground(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497{
2498 HWND target = findServer(name);
2499
2500 if (target != 0)
2501 SetForegroundWindow(target);
2502}
2503
2504/* Replies from server need to be stored until the client picks them up via
2505 * remote_read(). So we maintain a list of server-id/reply pairs.
2506 * Note that there could be multiple replies from one server pending if the
2507 * client is slow picking them up.
2508 * We just store the replies in a simple list. When we remove an entry, we
2509 * move list entries down to fill the gap.
2510 * The server ID is simply the HWND.
2511 */
2512typedef struct
2513{
2514 HWND server; /* server window */
2515 char_u *reply; /* reply string */
2516 int expr_result; /* 0 for REPLY, 1 for RESULT 2 for error */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002517} reply_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518
2519static garray_T reply_list = {0, 0, sizeof(reply_T), 5, 0};
2520
2521#define REPLY_ITEM(i) ((reply_T *)(reply_list.ga_data) + (i))
2522#define REPLY_COUNT (reply_list.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523
2524/* Flag which is used to wait for a reply */
2525static int reply_received = 0;
2526
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002527/*
2528 * Store a reply. "reply" must be allocated memory (or NULL).
2529 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 static int
2531save_reply(HWND server, char_u *reply, int expr)
2532{
2533 reply_T *rep;
2534
2535 if (ga_grow(&reply_list, 1) == FAIL)
2536 return FAIL;
2537
2538 rep = REPLY_ITEM(REPLY_COUNT);
2539 rep->server = server;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002540 rep->reply = reply;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 rep->expr_result = expr;
2542 if (rep->reply == NULL)
2543 return FAIL;
2544
2545 ++REPLY_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 reply_received = 1;
2547 return OK;
2548}
2549
2550/*
2551 * Get a reply from server "server".
2552 * When "expr_res" is non NULL, get the result of an expression, otherwise a
2553 * server2client() message.
2554 * When non NULL, point to return code. 0 => OK, -1 => ERROR
2555 * If "remove" is TRUE, consume the message, the caller must free it then.
2556 * if "wait" is TRUE block until a message arrives (or the server exits).
2557 */
2558 char_u *
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002559serverGetReply(HWND server, int *expr_res, int remove, int wait, int timeout)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560{
2561 int i;
2562 char_u *reply;
2563 reply_T *rep;
Bram Moolenaar15e737f2017-03-18 21:22:47 +01002564 int did_process = FALSE;
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002565 time_t start;
2566 time_t now;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567
2568 /* When waiting, loop until the message waiting for is received. */
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002569 time(&start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 for (;;)
2571 {
2572 /* Reset this here, in case a message arrives while we are going
2573 * through the already received messages. */
2574 reply_received = 0;
2575
2576 for (i = 0; i < REPLY_COUNT; ++i)
2577 {
2578 rep = REPLY_ITEM(i);
2579 if (rep->server == server
2580 && ((rep->expr_result != 0) == (expr_res != NULL)))
2581 {
2582 /* Save the values we've found for later */
2583 reply = rep->reply;
2584 if (expr_res != NULL)
2585 *expr_res = rep->expr_result == 1 ? 0 : -1;
2586
2587 if (remove)
2588 {
2589 /* Move the rest of the list down to fill the gap */
2590 mch_memmove(rep, rep + 1,
2591 (REPLY_COUNT - i - 1) * sizeof(reply_T));
2592 --REPLY_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 }
2594
2595 /* Return the reply to the caller, who takes on responsibility
2596 * for freeing it if "remove" is TRUE. */
2597 return reply;
2598 }
2599 }
2600
2601 /* If we got here, we didn't find a reply. Return immediately if the
2602 * "wait" parameter isn't set. */
2603 if (!wait)
Bram Moolenaar15e737f2017-03-18 21:22:47 +01002604 {
2605 /* Process pending messages once. Without this, looping on
2606 * remote_peek() would never get the reply. */
2607 if (!did_process)
2608 {
2609 did_process = TRUE;
2610 serverProcessPendingMessages();
2611 continue;
2612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 break;
Bram Moolenaar15e737f2017-03-18 21:22:47 +01002614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615
2616 /* We need to wait for a reply. Enter a message loop until the
2617 * "reply_received" flag gets set. */
2618
2619 /* Loop until we receive a reply */
2620 while (reply_received == 0)
2621 {
Bram Moolenaar42205552017-03-18 19:42:22 +01002622#ifdef FEAT_TIMERS
Bram Moolenaard23a8232018-02-10 18:45:26 +01002623 /* TODO: use the return value to decide how long to wait. */
Bram Moolenaar42205552017-03-18 19:42:22 +01002624 check_due_timer();
2625#endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002626 time(&now);
2627 if (timeout > 0 && (now - start) >= timeout)
2628 break;
2629
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 /* Wait for a SendMessage() call to us. This could be the reply
2631 * we are waiting for. Use a timeout of a second, to catch the
2632 * situation that the server died unexpectedly. */
2633 MsgWaitForMultipleObjects(0, NULL, TRUE, 1000, QS_ALLINPUT);
2634
2635 /* If the server has died, give up */
2636 if (!IsWindow(server))
2637 return NULL;
2638
2639 serverProcessPendingMessages();
2640 }
2641 }
2642
2643 return NULL;
2644}
2645
2646/*
2647 * Process any messages in the Windows message queue.
2648 */
2649 void
2650serverProcessPendingMessages(void)
2651{
2652 MSG msg;
2653
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02002654 while (pPeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 {
2656 TranslateMessage(&msg);
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02002657 pDispatchMessage(&msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 }
2659}
2660
2661#endif /* FEAT_CLIENTSERVER */
2662
2663#if defined(FEAT_GUI) || (defined(FEAT_PRINTER) && !defined(FEAT_POSTSCRIPT)) \
2664 || defined(PROTO)
2665
2666struct charset_pair
2667{
2668 char *name;
2669 BYTE charset;
2670};
2671
2672static struct charset_pair
2673charset_pairs[] =
2674{
2675 {"ANSI", ANSI_CHARSET},
2676 {"CHINESEBIG5", CHINESEBIG5_CHARSET},
2677 {"DEFAULT", DEFAULT_CHARSET},
2678 {"HANGEUL", HANGEUL_CHARSET},
2679 {"OEM", OEM_CHARSET},
2680 {"SHIFTJIS", SHIFTJIS_CHARSET},
2681 {"SYMBOL", SYMBOL_CHARSET},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 {"ARABIC", ARABIC_CHARSET},
2683 {"BALTIC", BALTIC_CHARSET},
2684 {"EASTEUROPE", EASTEUROPE_CHARSET},
2685 {"GB2312", GB2312_CHARSET},
2686 {"GREEK", GREEK_CHARSET},
2687 {"HEBREW", HEBREW_CHARSET},
2688 {"JOHAB", JOHAB_CHARSET},
2689 {"MAC", MAC_CHARSET},
2690 {"RUSSIAN", RUSSIAN_CHARSET},
2691 {"THAI", THAI_CHARSET},
2692 {"TURKISH", TURKISH_CHARSET},
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002693#ifdef VIETNAMESE_CHARSET
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 {"VIETNAMESE", VIETNAMESE_CHARSET},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695#endif
2696 {NULL, 0}
2697};
2698
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002699struct quality_pair
2700{
2701 char *name;
2702 DWORD quality;
2703};
2704
2705static struct quality_pair
2706quality_pairs[] = {
2707#ifdef CLEARTYPE_QUALITY
2708 {"CLEARTYPE", CLEARTYPE_QUALITY},
2709#endif
2710#ifdef ANTIALIASED_QUALITY
2711 {"ANTIALIASED", ANTIALIASED_QUALITY},
2712#endif
Bram Moolenaar73a733e2016-05-11 21:05:05 +02002713#ifdef NONANTIALIASED_QUALITY
2714 {"NONANTIALIASED", NONANTIALIASED_QUALITY},
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002715#endif
2716#ifdef PROOF_QUALITY
2717 {"PROOF", PROOF_QUALITY},
2718#endif
Bram Moolenaar4c9ce052016-04-04 21:06:19 +02002719#ifdef DRAFT_QUALITY
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002720 {"DRAFT", DRAFT_QUALITY},
2721#endif
2722 {"DEFAULT", DEFAULT_QUALITY},
2723 {NULL, 0}
2724};
2725
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726/*
2727 * Convert a charset ID to a name.
2728 * Return NULL when not recognized.
2729 */
2730 char *
2731charset_id2name(int id)
2732{
2733 struct charset_pair *cp;
2734
2735 for (cp = charset_pairs; cp->name != NULL; ++cp)
2736 if ((BYTE)id == cp->charset)
2737 break;
2738 return cp->name;
2739}
2740
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002741/*
2742 * Convert a quality ID to a name.
2743 * Return NULL when not recognized.
2744 */
2745 char *
2746quality_id2name(DWORD id)
2747{
2748 struct quality_pair *qp;
2749
2750 for (qp = quality_pairs; qp->name != NULL; ++qp)
2751 if (id == qp->quality)
2752 break;
2753 return qp->name;
2754}
2755
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002756static const LOGFONTW s_lfDefault =
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757{
2758 -12, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, DEFAULT_CHARSET,
2759 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
2760 PROOF_QUALITY, FIXED_PITCH | FF_DONTCARE,
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002761 L"Fixedsys" /* see _ReadVimIni */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762};
2763
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002764// Initialise the "current height" to -12 (same as s_lfDefault) just
2765// in case the user specifies a font in "guifont" with no size before a font
2766// with an explicit size has been set. This defaults the size to this value
2767// (-12 equates to roughly 9pt).
2768int current_font_height = -12; // also used in gui_w32.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769
2770/* Convert a string representing a point size into pixels. The string should
2771 * be a positive decimal number, with an optional decimal point (eg, "12", or
2772 * "10.5"). The pixel value is returned, and a pointer to the next unconverted
2773 * character is stored in *end. The flag "vertical" says whether this
2774 * calculation is for a vertical (height) size or a horizontal (width) one.
2775 */
2776 static int
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002777points_to_pixels(WCHAR *str, WCHAR **end, int vertical, long_i pprinter_dc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778{
2779 int pixels;
2780 int points = 0;
2781 int divisor = 0;
2782 HWND hwnd = (HWND)0;
2783 HDC hdc;
2784 HDC printer_dc = (HDC)pprinter_dc;
2785
2786 while (*str != NUL)
2787 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002788 if (*str == L'.' && divisor == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 {
2790 /* Start keeping a divisor, for later */
2791 divisor = 1;
2792 }
2793 else
2794 {
2795 if (!VIM_ISDIGIT(*str))
2796 break;
2797
2798 points *= 10;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002799 points += *str - L'0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 divisor *= 10;
2801 }
2802 ++str;
2803 }
2804
2805 if (divisor == 0)
2806 divisor = 1;
2807
2808 if (printer_dc == NULL)
2809 {
2810 hwnd = GetDesktopWindow();
2811 hdc = GetWindowDC(hwnd);
2812 }
2813 else
2814 hdc = printer_dc;
2815
2816 pixels = MulDiv(points,
2817 GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX),
2818 72 * divisor);
2819
2820 if (printer_dc == NULL)
2821 ReleaseDC(hwnd, hdc);
2822
2823 *end = str;
2824 return pixels;
2825}
2826
2827 static int CALLBACK
2828font_enumproc(
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002829 ENUMLOGFONTW *elf,
2830 NEWTEXTMETRICW *ntm UNUSED,
2831 DWORD type UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832 LPARAM lparam)
2833{
2834 /* Return value:
2835 * 0 = terminate now (monospace & ANSI)
2836 * 1 = continue, still no luck...
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002837 * 2 = continue, but we have an acceptable LOGFONTW
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838 * (monospace, not ANSI)
2839 * We use these values, as EnumFontFamilies returns 1 if the
2840 * callback function is never called. So, we check the return as
2841 * 0 = perfect, 2 = OK, 1 = no good...
2842 * It's not pretty, but it works!
2843 */
2844
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002845 LOGFONTW *lf = (LOGFONTW *)(lparam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846
2847#ifndef FEAT_PROPORTIONAL_FONTS
2848 /* Ignore non-monospace fonts without further ado */
2849 if ((ntm->tmPitchAndFamily & 1) != 0)
2850 return 1;
2851#endif
2852
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002853 /* Remember this LOGFONTW as a "possible" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 *lf = elf->elfLogFont;
2855
2856 /* Terminate the scan as soon as we find an ANSI font */
2857 if (lf->lfCharSet == ANSI_CHARSET
2858 || lf->lfCharSet == OEM_CHARSET
2859 || lf->lfCharSet == DEFAULT_CHARSET)
2860 return 0;
2861
2862 /* Continue the scan - we have a non-ANSI font */
2863 return 2;
2864}
2865
2866 static int
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002867init_logfont(LOGFONTW *lf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868{
2869 int n;
2870 HWND hwnd = GetDesktopWindow();
2871 HDC hdc = GetWindowDC(hwnd);
2872
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002873 n = EnumFontFamiliesW(hdc,
2874 lf->lfFaceName,
2875 (FONTENUMPROCW)font_enumproc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 (LPARAM)lf);
2877
2878 ReleaseDC(hwnd, hdc);
2879
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002880 /* If we couldn't find a usable font, return failure */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 if (n == 1)
2882 return FAIL;
2883
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002884 /* Tidy up the rest of the LOGFONTW structure. We set to a basic
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 * font - get_logfont() sets bold, italic, etc based on the user's
2886 * input.
2887 */
2888 lf->lfHeight = current_font_height;
2889 lf->lfWidth = 0;
2890 lf->lfItalic = FALSE;
2891 lf->lfUnderline = FALSE;
2892 lf->lfStrikeOut = FALSE;
2893 lf->lfWeight = FW_NORMAL;
2894
2895 /* Return success */
2896 return OK;
2897}
2898
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002899/*
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002900 * Compare a UTF-16 string and an ASCII string literally.
2901 * Only works all the code points are inside ASCII range.
2902 */
2903 static int
2904utf16ascncmp(const WCHAR *w, const char *p, size_t n)
2905{
2906 size_t i;
2907
2908 for (i = 0; i < n; i++)
2909 {
2910 if (w[i] == 0 || w[i] != p[i])
2911 return w[i] - p[i];
2912 }
2913 return 0;
2914}
2915
2916/*
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002917 * Get font info from "name" into logfont "lf".
2918 * Return OK for a valid name, FAIL otherwise.
2919 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 int
2921get_logfont(
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002922 LOGFONTW *lf,
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002923 char_u *name,
2924 HDC printer_dc,
2925 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002927 WCHAR *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 int i;
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002929 int ret = FAIL;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002930 static LOGFONTW *lastlf = NULL;
2931 WCHAR *wname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932
2933 *lf = s_lfDefault;
2934 if (name == NULL)
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002935 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002937 wname = enc_to_utf16(name, NULL);
2938 if (wname == NULL)
2939 return FAIL;
2940
2941 if (wcscmp(wname, L"*") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01002943#if defined(FEAT_GUI_MSWIN)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002944 CHOOSEFONTW cf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 /* if name is "*", bring up std font dialog: */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02002946 vim_memset(&cf, 0, sizeof(cf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 cf.lStructSize = sizeof(cf);
2948 cf.hwndOwner = s_hwnd;
2949 cf.Flags = CF_SCREENFONTS | CF_FIXEDPITCHONLY | CF_INITTOLOGFONTSTRUCT;
2950 if (lastlf != NULL)
2951 *lf = *lastlf;
2952 cf.lpLogFont = lf;
2953 cf.nFontType = 0 ; //REGULAR_FONTTYPE;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002954 if (ChooseFontW(&cf))
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002955 ret = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956#endif
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002957 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958 }
2959
2960 /*
2961 * Split name up, it could be <name>:h<height>:w<width> etc.
2962 */
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002963 for (p = wname; *p && *p != L':'; p++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002965 if (p - wname + 1 >= LF_FACESIZE)
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002966 goto theend; /* Name too long */
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002967 lf->lfFaceName[p - wname] = *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 }
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002969 if (p != wname)
2970 lf->lfFaceName[p - wname] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971
2972 /* First set defaults */
2973 lf->lfHeight = -12;
2974 lf->lfWidth = 0;
2975 lf->lfWeight = FW_NORMAL;
2976 lf->lfItalic = FALSE;
2977 lf->lfUnderline = FALSE;
2978 lf->lfStrikeOut = FALSE;
2979
2980 /*
2981 * If the font can't be found, try replacing '_' by ' '.
2982 */
2983 if (init_logfont(lf) == FAIL)
2984 {
2985 int did_replace = FALSE;
2986
2987 for (i = 0; lf->lfFaceName[i]; ++i)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002988 if (lf->lfFaceName[i] == L'_')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002990 lf->lfFaceName[i] = L' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991 did_replace = TRUE;
2992 }
2993 if (!did_replace || init_logfont(lf) == FAIL)
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002994 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 }
2996
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002997 while (*p == L':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998 p++;
2999
3000 /* Set the values found after ':' */
3001 while (*p)
3002 {
3003 switch (*p++)
3004 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003005 case L'h':
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003006 lf->lfHeight = - points_to_pixels(p, &p, TRUE, (long_i)printer_dc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003008 case L'w':
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003009 lf->lfWidth = points_to_pixels(p, &p, FALSE, (long_i)printer_dc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010 break;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003011 case L'W':
3012 lf->lfWeight = wcstol(p, &p, 10);
3013 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003014 case L'b':
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015 lf->lfWeight = FW_BOLD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003017 case L'i':
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018 lf->lfItalic = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003020 case L'u':
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 lf->lfUnderline = TRUE;
3022 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003023 case L's':
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024 lf->lfStrikeOut = TRUE;
3025 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003026 case L'c':
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027 {
3028 struct charset_pair *cp;
3029
3030 for (cp = charset_pairs; cp->name != NULL; ++cp)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003031 if (utf16ascncmp(p, cp->name, strlen(cp->name)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 {
3033 lf->lfCharSet = cp->charset;
3034 p += strlen(cp->name);
3035 break;
3036 }
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003037 if (cp->name == NULL && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003039 char_u *s = utf16_to_enc(p, NULL);
3040 semsg(_("E244: Illegal charset name \"%s\" in font name \"%s\""), s, name);
3041 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 break;
3043 }
3044 break;
3045 }
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003046 case L'q':
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003047 {
3048 struct quality_pair *qp;
3049
3050 for (qp = quality_pairs; qp->name != NULL; ++qp)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003051 if (utf16ascncmp(p, qp->name, strlen(qp->name)) == 0)
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003052 {
3053 lf->lfQuality = qp->quality;
3054 p += strlen(qp->name);
3055 break;
3056 }
3057 if (qp->name == NULL && verbose)
3058 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003059 char_u *s = utf16_to_enc(p, NULL);
3060 semsg(_("E244: Illegal quality name \"%s\" in font name \"%s\""), s, name);
3061 vim_free(s);
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003062 break;
3063 }
3064 break;
3065 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 default:
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003067 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003068 semsg(_("E245: Illegal char '%c' in font name \"%s\""), p[-1], name);
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003069 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 }
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003071 while (*p == L':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 p++;
3073 }
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003074 ret = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 /* ron: init lastlf */
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003078 if (ret == OK && printer_dc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 {
3080 vim_free(lastlf);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003081 lastlf = (LOGFONTW *)alloc(sizeof(LOGFONTW));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082 if (lastlf != NULL)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003083 mch_memmove(lastlf, lf, sizeof(LOGFONTW));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 }
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003085 vim_free(wname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003087 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088}
3089
3090#endif /* defined(FEAT_GUI) || defined(FEAT_PRINTER) */
Bram Moolenaarf12d9832016-01-29 21:11:25 +01003091
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003092#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaarf12d9832016-01-29 21:11:25 +01003093/*
3094 * Initialize the Winsock dll.
3095 */
3096 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003097channel_init_winsock(void)
Bram Moolenaarf12d9832016-01-29 21:11:25 +01003098{
3099 WSADATA wsaData;
3100 int wsaerr;
3101
3102 if (WSInitialized)
3103 return;
3104
3105 wsaerr = WSAStartup(MAKEWORD(2, 2), &wsaData);
3106 if (wsaerr == 0)
3107 WSInitialized = TRUE;
3108}
3109#endif