blob: 149883b41e5012b14f75b0a7024c31981b30386c [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 Moolenaar651fca82021-11-29 20:39:38 +000035# if !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 Moolenaar0f873732019-12-05 20:28:46 +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
Bram Moolenaar912bc4a2019-12-01 18:58:11 +010080# define WINAPI
81# define WINBASEAPI
Bram Moolenaar071d4272004-06-13 20:20:40 +000082typedef 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;
Bram Moolenaard21e5bd2022-06-27 22:52:43 +010095typedef int INT_PTR;
Bram Moolenaar071d4272004-06-13 20:20:40 +000096typedef int KEY_EVENT_RECORD;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +010097typedef int LOGFONTW;
Bram Moolenaar071d4272004-06-13 20:20:40 +000098typedef int LPARAM;
99typedef int LPBOOL;
100typedef int LPCSTR;
101typedef int LPCWSTR;
Bram Moolenaarb9cdb372019-04-17 18:24:35 +0200102typedef int LPDWORD;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000103typedef int LPSTR;
104typedef int LPTSTR;
Bram Moolenaarb9cdb372019-04-17 18:24:35 +0200105typedef int LPVOID;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106typedef int LPWSTR;
107typedef int LRESULT;
108typedef int MOUSE_EVENT_RECORD;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100109typedef int NEWTEXTMETRICW;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000110typedef int PACL;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200111typedef int PRINTDLGW;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112typedef int PSECURITY_DESCRIPTOR;
113typedef int PSID;
114typedef int SECURITY_INFORMATION;
115typedef int SHORT;
116typedef int SMALL_RECT;
117typedef int TEXTMETRIC;
118typedef int UINT;
119typedef int WCHAR;
Bram Moolenaarc447d8d2018-12-18 21:56:28 +0100120typedef int WNDENUMPROC;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121typedef int WORD;
122typedef int WPARAM;
123typedef void VOID;
124#endif
125
Bram Moolenaar0f873732019-12-05 20:28:46 +0100126// Record all output and all keyboard & mouse input
127// #define MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128
129#ifdef MCH_WRITE_DUMP
130FILE* fdDump = NULL;
131#endif
132
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200133#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134extern char g_szOrigTitle[];
135#endif
136
137#ifdef FEAT_GUI
138extern HWND s_hwnd;
139#else
Bram Moolenaar0f873732019-12-05 20:28:46 +0100140static HWND s_hwnd = 0; // console window handle, set by GetConsoleHwnd()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141#endif
142
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100143#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar0f873732019-12-05 20:28:46 +0100144int WSInitialized = FALSE; // WinSock is initialized
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100145#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146
Bram Moolenaar0f873732019-12-05 20:28:46 +0100147// Don't generate prototypes here, because some systems do have these
148// functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149#if defined(__GNUC__) && !defined(PROTO)
150# ifndef __MINGW32__
151int _stricoll(char *a, char *b)
152{
153 // the ANSI-ish correct way is to use strxfrm():
154 char a_buff[512], b_buff[512]; // file names, so this is enough on Win32
155 strxfrm(a_buff, a, 512);
156 strxfrm(b_buff, b, 512);
157 return strcoll(a_buff, b_buff);
158}
159
160char * _fullpath(char *buf, char *fname, int len)
161{
162 LPTSTR toss;
163
164 return (char *)GetFullPathName(fname, len, buf, &toss);
165}
166# endif
167
Bram Moolenaaraf62ff32013-03-19 14:48:29 +0100168# if !defined(__MINGW32__) || (__GNUC__ < 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169int _chdrive(int drive)
170{
171 char temp [3] = "-:";
172 temp[0] = drive + 'A' - 1;
173 return !SetCurrentDirectory(temp);
174}
Bram Moolenaaraf62ff32013-03-19 14:48:29 +0100175# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176#endif
177
178
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200179#ifndef PROTO
180/*
181 * Save the instance handle of the exe/dll.
182 */
183 void
184SaveInst(HINSTANCE hInst)
185{
186 g_hinst = hInst;
187}
188#endif
189
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190#if defined(FEAT_GUI_MSWIN) || defined(PROTO)
191/*
192 * GUI version of mch_exit().
193 * Shut down and exit with status `r'
194 * Careful: mch_exit() may be called before mch_init()!
195 */
196 void
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200197mch_exit_g(int r)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198{
Bram Moolenaar955f1982017-02-05 15:10:51 +0100199 exiting = TRUE;
200
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201 display_errors();
202
Bram Moolenaar0f873732019-12-05 20:28:46 +0100203 ml_close_all(TRUE); // remove all memfiles
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204
205# ifdef FEAT_OLE
206 UninitOLE();
207# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100208# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209 if (WSInitialized)
210 {
211 WSInitialized = FALSE;
212 WSACleanup();
213 }
214# endif
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100215# ifdef DYNAMIC_GETTEXT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216 dyn_libintl_end();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100217# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218
219 if (gui.in_use)
220 gui_exit(r);
Bram Moolenaar85c79d32007-02-20 01:59:20 +0000221
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100222# ifdef EXITFREE
Bram Moolenaar85c79d32007-02-20 01:59:20 +0000223 free_all_mem();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100224# endif
Bram Moolenaar85c79d32007-02-20 01:59:20 +0000225
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226 exit(r);
227}
228
Bram Moolenaar0f873732019-12-05 20:28:46 +0100229#endif // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230
231
232/*
233 * Init the tables for toupper() and tolower().
234 */
235 void
236mch_early_init(void)
237{
238 int i;
239
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240 PlatformId();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241
Bram Moolenaar0f873732019-12-05 20:28:46 +0100242 // Init the tables for toupper() and tolower()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243 for (i = 0; i < 256; ++i)
244 toupper_tab[i] = tolower_tab[i] = i;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100245 CharUpperBuff((LPSTR)toupper_tab, 256);
246 CharLowerBuff((LPSTR)tolower_tab, 256);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000247}
248
249
250/*
251 * Return TRUE if the input comes from a terminal, FALSE otherwise.
252 */
253 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100254mch_input_isatty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000255{
256#ifdef FEAT_GUI_MSWIN
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200257# ifdef VIMDLL
258 if (gui.in_use)
259# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +0100260 return TRUE; // GUI always has a tty
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200261#endif
262#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263 if (isatty(read_cmd_fd))
264 return TRUE;
265 return FALSE;
266#endif
267}
268
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269/*
270 * mch_settitle(): set titlebar of our window
271 */
272 void
273mch_settitle(
274 char_u *title,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +0200275 char_u *icon UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000276{
Bram Moolenaar651fca82021-11-29 20:39:38 +0000277#ifdef FEAT_GUI_MSWIN
278# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200279 if (gui.in_use)
Bram Moolenaar651fca82021-11-29 20:39:38 +0000280# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200281 {
282 gui_mch_settitle(title, icon);
283 return;
284 }
Bram Moolenaar651fca82021-11-29 20:39:38 +0000285#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 Moolenaar651fca82021-11-29 20:39:38 +0000298#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000299}
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 Moolenaar651fca82021-11-29 20:39:38 +0000312#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
313# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200314 if (!gui.in_use)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100315# endif
Bram Moolenaar651fca82021-11-29 20:39:38 +0000316 SetConsoleTitle(g_szOrigTitle);
317#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000318}
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}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339
340
341/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000342 * Get absolute file name into buffer "buf" of length "len" bytes,
343 * turning all '/'s into '\\'s and getting the correct case of each component
344 * of the file name. Append a (back)slash to a directory name.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345 * When 'shellslash' set do it the other way around.
346 * Return OK or FAIL.
347 */
348 int
349mch_FullName(
350 char_u *fname,
351 char_u *buf,
352 int len,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100353 int force UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354{
355 int nResult = FAIL;
Bram Moolenaareae1b912019-05-09 15:12:55 +0200356 WCHAR *wname;
357 WCHAR wbuf[MAX_PATH];
358 char_u *cname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359
Bram Moolenaareae1b912019-05-09 15:12:55 +0200360 wname = enc_to_utf16(fname, NULL);
361 if (wname != NULL && _wfullpath(wbuf, wname, MAX_PATH) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362 {
Bram Moolenaareae1b912019-05-09 15:12:55 +0200363 cname = utf16_to_enc((short_u *)wbuf, NULL);
364 if (cname != NULL)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000365 {
Bram Moolenaareae1b912019-05-09 15:12:55 +0200366 vim_strncpy(buf, cname, len - 1);
367 nResult = OK;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369 }
Bram Moolenaareae1b912019-05-09 15:12:55 +0200370 vim_free(wname);
371 vim_free(cname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372
373#ifdef USE_FNAME_CASE
374 fname_case(buf, len);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000375#else
376 slash_adjust(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377#endif
378
379 return nResult;
380}
381
382
383/*
384 * Return TRUE if "fname" does not depend on the current directory.
385 */
386 int
387mch_isFullName(char_u *fname)
388{
Bram Moolenaar0ea74212020-12-11 20:10:50 +0100389 // A name like "d:/foo" and "//server/share" is absolute. "d:foo" is not.
390 // Another way to check is to use mch_FullName() and see if the result is
391 // the same as the name or mch_FullName() fails. However, this has quite a
392 // bit of overhead, so let's not do that.
Yegappan Lakshmanan6df0f272021-12-16 13:06:10 +0000393 if (*fname == NUL)
Yegappan Lakshmanan5a664fe2021-12-29 18:16:21 +0000394 return FALSE;
Bram Moolenaar0ea74212020-12-11 20:10:50 +0100395 return ((ASCII_ISALPHA(fname[0]) && fname[1] == ':'
396 && (fname[2] == '/' || fname[2] == '\\'))
397 || (fname[0] == fname[1] && (fname[0] == '/' || fname[0] == '\\')));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398}
399
400/*
401 * Replace all slashes by backslashes.
402 * This used to be the other way around, but MS-DOS sometimes has problems
403 * with slashes (e.g. in a command name). We can't have mixed slashes and
404 * backslashes, because comparing file names will not work correctly. The
405 * commands that use a file name should try to avoid the need to type a
406 * backslash twice.
407 * When 'shellslash' set do it the other way around.
Bram Moolenaarb4f6a462015-10-13 19:43:17 +0200408 * When the path looks like a URL leave it unmodified.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409 */
410 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100411slash_adjust(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412{
Bram Moolenaarb4f6a462015-10-13 19:43:17 +0200413 if (path_with_url(p))
414 return;
Bram Moolenaar39d21e32017-08-05 23:09:31 +0200415
416 if (*p == '`')
417 {
Bram Moolenaar116a0f82017-08-07 21:17:57 +0200418 size_t len = STRLEN(p);
419
Bram Moolenaar0f873732019-12-05 20:28:46 +0100420 // don't replace backslash in backtick quoted strings
Bram Moolenaar39d21e32017-08-05 23:09:31 +0200421 if (len > 2 && *(p + len - 1) == '`')
422 return;
423 }
424
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +0000425 while (*p)
426 {
427 if (*p == psepcN)
428 *p = psepc;
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100429 MB_PTR_ADV(p);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +0000430 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431}
432
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200433 static int
LemonBoy40fd7e62022-05-05 20:18:16 +0100434read_reparse_point(const WCHAR *name, char_u *buf, DWORD *buf_len)
435{
436 HANDLE h;
437 BOOL ok;
438
439 h = CreateFileW(name, FILE_READ_ATTRIBUTES,
440 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
441 OPEN_EXISTING,
442 FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT,
443 NULL);
444 if (h == INVALID_HANDLE_VALUE)
445 return FAIL;
446
447 ok = DeviceIoControl(h, FSCTL_GET_REPARSE_POINT, NULL, 0, buf, *buf_len,
448 buf_len, NULL);
449 CloseHandle(h);
450
451 return ok ? OK : FAIL;
452}
453
LemonBoy40fd7e62022-05-05 20:18:16 +0100454 char_u *
455resolve_appexeclink(char_u *fname)
456{
Bram Moolenaar9f1983d2022-05-12 20:35:35 +0100457 DWORD attr = 0;
458 int idx;
459 WCHAR *p, *end, *wname;
LemonBoy40fd7e62022-05-05 20:18:16 +0100460 // The buffer size is arbitrarily chosen to be "big enough" (TM), the
461 // ceiling should be around 16k.
Bram Moolenaar9f1983d2022-05-12 20:35:35 +0100462 char_u buf[4096];
463 DWORD buf_len = sizeof(buf);
LemonBoy40fd7e62022-05-05 20:18:16 +0100464 REPARSE_DATA_BUFFER *rb = (REPARSE_DATA_BUFFER *)buf;
465
466 wname = enc_to_utf16(fname, NULL);
467 if (wname == NULL)
468 return NULL;
469
470 attr = GetFileAttributesW(wname);
471 if (attr == INVALID_FILE_ATTRIBUTES ||
472 (attr & FILE_ATTRIBUTE_REPARSE_POINT) == 0)
473 {
474 vim_free(wname);
475 return NULL;
476 }
477
478 // The applinks are similar to symlinks but with a huge difference: they can
479 // only be executed, any other I/O operation on them is bound to fail with
480 // ERROR_FILE_NOT_FOUND even though the file exists.
481 if (read_reparse_point(wname, buf, &buf_len) == FAIL)
482 {
483 vim_free(wname);
484 return NULL;
485 }
486 vim_free(wname);
487
488 if (rb->ReparseTag != IO_REPARSE_TAG_APPEXECLINK)
489 return NULL;
490
491 // The (undocumented) reparse buffer contains a set of N null-terminated
492 // Unicode strings, the application path is stored in the third one.
493 if (rb->AppExecLinkReparseBuffer.StringCount < 3)
494 return NULL;
495
496 p = rb->AppExecLinkReparseBuffer.StringList;
497 end = p + rb->ReparseDataLength / sizeof(WCHAR);
498 for (idx = 0; p < end
499 && idx < (int)rb->AppExecLinkReparseBuffer.StringCount
500 && idx != 2; )
501 {
Yegappan Lakshmananebb01bd2022-06-08 15:14:09 +0100502 if (*p++ == L'\0')
LemonBoy40fd7e62022-05-05 20:18:16 +0100503 ++idx;
504 }
505
506 return utf16_to_enc(p, NULL);
507}
508
LemonBoy23c5ebe2024-06-18 20:43:51 +0200509// Use 64-bit stat functions.
510#undef stat
511#undef _stat
512#undef _wstat
513#undef _fstat
514#define stat _stat64
515#define _stat _stat64
516#define _wstat _wstat64
517#define _fstat _fstat64
518
519/*
520 * Implements lstat() and stat() that can handle symlinks properly.
521 */
522 static int
523mswin_stat_impl(const WCHAR *name, stat_T *stp, const int resolve)
524{
525 int n;
526 int fd;
527 BOOL is_symlink = FALSE;
528 HANDLE hFind, h;
529 DWORD attr = 0;
530 DWORD flag = 0;
531 WIN32_FIND_DATAW findDataW;
532
533#ifdef _UCRT
534 if (resolve)
535 // Universal CRT can handle symlinks properly.
536 return _wstat(name, stp);
537#endif
538
539 hFind = FindFirstFileW(name, &findDataW);
540 if (hFind != INVALID_HANDLE_VALUE)
541 {
542 attr = findDataW.dwFileAttributes;
543 if ((attr & FILE_ATTRIBUTE_REPARSE_POINT)
544 && (findDataW.dwReserved0 == IO_REPARSE_TAG_SYMLINK))
545 is_symlink = TRUE;
546 FindClose(hFind);
547 }
548
549 // Use the plain old stat() whenever it's possible.
550 if (!is_symlink)
551 return _wstat(name, stp);
552
553 if (!resolve && is_symlink)
554 flag = FILE_FLAG_OPEN_REPARSE_POINT;
555 if (attr & FILE_ATTRIBUTE_DIRECTORY)
556 flag |= FILE_FLAG_BACKUP_SEMANTICS;
557
558 h = CreateFileW(name, FILE_READ_ATTRIBUTES,
559 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, flag,
560 NULL);
561 if (h == INVALID_HANDLE_VALUE)
562 return -1;
563
564 fd = _open_osfhandle((intptr_t)h, _O_RDONLY);
565 n = _fstat(fd, (struct _stat *)stp);
566 if ((n == 0) && (attr & FILE_ATTRIBUTE_DIRECTORY))
567 stp->st_mode = (stp->st_mode & ~S_IFMT) | S_IFDIR;
568 _close(fd);
569
570 return n;
571}
572
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573/*
574 * stat() can't handle a trailing '/' or '\', remove it first.
LemonBoy23c5ebe2024-06-18 20:43:51 +0200575 * When 'resolve' is true behave as lstat() wrt symlinks.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576 */
LemonBoy23c5ebe2024-06-18 20:43:51 +0200577 static int
578stat_impl(const char *name, stat_T *stp, const int resolve)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100580 // WinNT and later can use _MAX_PATH wide characters for a pathname, which
581 // means that the maximum pathname is _MAX_PATH * 3 bytes when 'enc' is
582 // UTF-8.
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100583 char_u buf[_MAX_PATH * 3 + 1];
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100584 char_u *p;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200585 WCHAR *wp;
586 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587
Bram Moolenaard2a203b2013-08-30 16:51:18 +0200588 vim_strncpy((char_u *)buf, (char_u *)name, sizeof(buf) - 1);
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100589 p = buf + STRLEN(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590 if (p > buf)
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100591 MB_PTR_BACK(buf, p);
Bram Moolenaarb1cb35f2014-01-10 13:05:20 +0100592
Bram Moolenaar0f873732019-12-05 20:28:46 +0100593 // Remove trailing '\\' except root path.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594 if (p > buf && (*p == '\\' || *p == '/') && p[-1] != ':')
595 *p = NUL;
Bram Moolenaarb1cb35f2014-01-10 13:05:20 +0100596
597 if ((buf[0] == '\\' && buf[1] == '\\') || (buf[0] == '/' && buf[1] == '/'))
598 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100599 // UNC root path must be followed by '\\'.
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100600 p = vim_strpbrk(buf + 2, (char_u *)"\\/");
Bram Moolenaarb1cb35f2014-01-10 13:05:20 +0100601 if (p != NULL)
602 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100603 p = vim_strpbrk(p + 1, (char_u *)"\\/");
Bram Moolenaarb1cb35f2014-01-10 13:05:20 +0100604 if (p == NULL)
605 STRCAT(buf, "\\");
606 }
607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200609 wp = enc_to_utf16(buf, NULL);
610 if (wp == NULL)
611 return -1;
612
LemonBoy23c5ebe2024-06-18 20:43:51 +0200613 n = mswin_stat_impl(wp, stp, resolve);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200614 vim_free(wp);
615 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616}
617
LemonBoy23c5ebe2024-06-18 20:43:51 +0200618 int
619vim_lstat(const char *name, stat_T *stp)
620{
621 return stat_impl(name, stp, FALSE);
622}
623
624 int
625vim_stat(const char *name, stat_T *stp)
626{
627 return stat_impl(name, stp, TRUE);
628}
629
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200630#if (defined(FEAT_GUI_MSWIN) && !defined(VIMDLL)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631 void
Bram Moolenaar26e86442020-05-17 14:06:16 +0200632mch_settmode(tmode_T tmode UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100634 // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635}
636
637 int
638mch_get_shellsize(void)
639{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100640 // never used
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 return OK;
642}
643
644 void
645mch_set_shellsize(void)
646{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100647 // never used
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648}
649
650/*
651 * Rows and/or Columns has changed.
652 */
653 void
654mch_new_shellsize(void)
655{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100656 // never used
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657}
658
659#endif
660
661/*
662 * We have no job control, so fake it by starting a new shell.
663 */
664 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100665mch_suspend(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666{
667 suspend_shell();
668}
669
670#if defined(USE_MCH_ERRMSG) || defined(PROTO)
671
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200672# ifdef display_errors
673# undef display_errors
674# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675
676/*
677 * Display the saved error message(s).
678 */
679 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100680display_errors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681{
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200682# ifdef FEAT_GUI
Bram Moolenaar819ab822022-06-13 22:34:14 +0100683 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200685# ifdef VIMDLL
686 if (gui.in_use || gui.starting)
687# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200689 if (error_ga.ga_data != NULL)
690 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100691 // avoid putting up a message box with blanks only
Bram Moolenaar819ab822022-06-13 22:34:14 +0100692 for (p = (char_u *)error_ga.ga_data; *p; ++p)
Keith Thompson184f71c2024-01-04 21:19:04 +0100693 if (!SAFE_isspace(*p))
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200694 {
Bram Moolenaar2d12c252022-06-13 21:42:45 +0100695 // Only use a dialog when not using --gui-dialog-file:
696 // write text to a file.
697 if (!gui_dialog_log((char_u *)"Errors", p))
698 (void)gui_mch_dialog(
Bram Moolenaarc17ef8e2006-03-25 21:48:58 +0000699 gui.starting ? VIM_INFO :
Bram Moolenaarc17ef8e2006-03-25 21:48:58 +0000700 VIM_ERROR,
Bram Moolenaarc17ef8e2006-03-25 21:48:58 +0000701 gui.starting ? (char_u *)_("Message") :
Bram Moolenaarc17ef8e2006-03-25 21:48:58 +0000702 (char_u *)_("Error"),
Bram Moolenaar819ab822022-06-13 22:34:14 +0100703 p, (char_u *)_("&Ok"),
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100704 1, NULL, FALSE);
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200705 break;
706 }
707 ga_clear(&error_ga);
708 }
709 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200711# endif
712# if !defined(FEAT_GUI) || defined(VIMDLL)
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +0100713 FlushFileBuffers(GetStdHandle(STD_ERROR_HANDLE));
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200714# endif
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +0100715}
716#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717
718
719/*
720 * Return TRUE if "p" contain a wildcard that can be expanded by
721 * dos_expandpath().
722 */
723 int
724mch_has_exp_wildcard(char_u *p)
725{
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100726 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727 {
728 if (vim_strchr((char_u *)"?*[", *p) != NULL
729 || (*p == '~' && p[1] != NUL))
730 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 }
732 return FALSE;
733}
734
735/*
736 * Return TRUE if "p" contain a wildcard or a "~1" kind of thing (could be a
737 * shortened file name).
738 */
739 int
740mch_has_wildcard(char_u *p)
741{
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100742 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 {
744 if (vim_strchr((char_u *)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100745#ifdef VIM_BACKTICK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 "?*$[`"
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100747#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 "?*$["
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100749#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 , *p) != NULL
751 || (*p == '~' && p[1] != NUL))
752 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 }
754 return FALSE;
755}
756
757
758/*
759 * The normal _chdir() does not change the default drive. This one does.
760 * Returning 0 implies success; -1 implies failure.
761 */
762 int
763mch_chdir(char *path)
764{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200765 WCHAR *p;
766 int n;
767
Bram Moolenaar0f873732019-12-05 20:28:46 +0100768 if (path[0] == NUL) // just checking...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 return -1;
770
Bram Moolenaara2974d72009-07-14 16:38:36 +0000771 if (p_verbose >= 5)
772 {
773 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100774 smsg("chdir(%s)", path);
Bram Moolenaara2974d72009-07-14 16:38:36 +0000775 verbose_leave();
776 }
Keith Thompson184f71c2024-01-04 21:19:04 +0100777 if (SAFE_isalpha(path[0]) && path[1] == ':') // has a drive name
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100779 // If we can change to the drive, skip that part of the path. If we
780 // can't then the current directory may be invalid, try using chdir()
781 // with the whole path.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 if (_chdrive(TOLOWER_ASC(path[0]) - 'a' + 1) == 0)
783 path += 2;
784 }
785
Bram Moolenaar0f873732019-12-05 20:28:46 +0100786 if (*path == NUL) // drive name only
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 return 0;
788
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200789 p = enc_to_utf16((char_u *)path, NULL);
790 if (p == NULL)
791 return -1;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000792
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200793 n = _wchdir(p);
794 vim_free(p);
795 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796}
797
798
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200799#if defined(FEAT_GUI_MSWIN) && !defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800/*
801 * return non-zero if a character is available
802 */
803 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100804mch_char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100806 // never used
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 return TRUE;
808}
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200809
810# if defined(FEAT_TERMINAL) || defined(PROTO)
811/*
812 * Check for any pending input or messages.
813 */
814 int
815mch_check_messages(void)
816{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100817 // TODO: check for messages
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200818 return TRUE;
819}
820# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821#endif
822
823
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824#if defined(FEAT_LIBCALL) || defined(PROTO)
825/*
826 * Call a DLL routine which takes either a string or int param
827 * and returns an allocated string.
828 * Return OK if it worked, FAIL if not.
829 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830typedef LPTSTR (*MYSTRPROCSTR)(LPTSTR);
831typedef LPTSTR (*MYINTPROCSTR)(int);
832typedef int (*MYSTRPROCINT)(LPTSTR);
833typedef int (*MYINTPROCINT)(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835/*
836 * Check if a pointer points to a valid NUL terminated string.
837 * Return the length of the string, including terminating NUL.
838 * Returns 0 for an invalid pointer, 1 for an empty string.
839 */
840 static size_t
841check_str_len(char_u *str)
842{
843 SYSTEM_INFO si;
844 MEMORY_BASIC_INFORMATION mbi;
845 size_t length = 0;
846 size_t i;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100847 const char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848
Bram Moolenaar0f873732019-12-05 20:28:46 +0100849 // get page size
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 GetSystemInfo(&si);
851
Bram Moolenaar0f873732019-12-05 20:28:46 +0100852 // get memory information
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000853 if (!VirtualQuery(str, &mbi, sizeof(mbi)))
854 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000856 // pre cast these (typing savers)
857 long_u dwStr = (long_u)str;
858 long_u dwBaseAddress = (long_u)mbi.BaseAddress;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000860 // get start address of page that str is on
861 long_u strPage = dwStr - (dwStr - dwBaseAddress) % si.dwPageSize;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000863 // get length from str to end of page
864 long_u pageLength = si.dwPageSize - (dwStr - strPage);
865
866 for (p = str; !IsBadReadPtr(p, (UINT)pageLength);
867 p += pageLength, pageLength = si.dwPageSize)
868 for (i = 0; i < pageLength; ++i, ++length)
869 if (p[i] == NUL)
870 return length + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871
872 return 0;
873}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200875/*
876 * Passed to do_in_runtimepath() to load a vim.ico file.
877 */
878 static void
879mch_icon_load_cb(char_u *fname, void *cookie)
880{
881 HANDLE *h = (HANDLE *)cookie;
882
883 *h = LoadImage(NULL,
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100884 (LPSTR)fname,
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200885 IMAGE_ICON,
886 64,
887 64,
888 LR_LOADFROMFILE | LR_LOADMAP3DCOLORS);
889}
890
891/*
892 * Try loading an icon file from 'runtimepath'.
893 */
894 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100895mch_icon_load(HANDLE *iconp)
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200896{
897 return do_in_runtimepath((char_u *)"bitmaps/vim.ico",
Bram Moolenaar7f8989d2016-03-12 22:11:39 +0100898 0, mch_icon_load_cb, iconp);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200899}
900
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 int
902mch_libcall(
903 char_u *libname,
904 char_u *funcname,
Bram Moolenaar0f873732019-12-05 20:28:46 +0100905 char_u *argstring, // NULL when using a argint
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 int argint,
Bram Moolenaar0f873732019-12-05 20:28:46 +0100907 char_u **string_result,// NULL when using number_result
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 int *number_result)
909{
910 HINSTANCE hinstLib;
911 MYSTRPROCSTR ProcAdd;
912 MYINTPROCSTR ProcAddI;
913 char_u *retval_str = NULL;
914 int retval_int = 0;
915 size_t len;
916
917 BOOL fRunTimeLinkSuccess = FALSE;
918
919 // Get a handle to the DLL module.
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100920 hinstLib = vimLoadLib((char *)libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921
922 // If the handle is valid, try to get the function address.
923 if (hinstLib != NULL)
924 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100925# ifdef HAVE_TRY_EXCEPT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 __try
927 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100928# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 if (argstring != NULL)
930 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100931 // Call with string argument
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100932 ProcAdd = (MYSTRPROCSTR)GetProcAddress(hinstLib, (LPCSTR)funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 if ((fRunTimeLinkSuccess = (ProcAdd != NULL)) != 0)
934 {
935 if (string_result == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100936 retval_int = ((MYSTRPROCINT)ProcAdd)((LPSTR)argstring);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100938 retval_str = (char_u *)(ProcAdd)((LPSTR)argstring);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 }
940 }
941 else
942 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100943 // Call with number argument
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100944 ProcAddI = (MYINTPROCSTR) GetProcAddress(hinstLib, (LPCSTR)funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 if ((fRunTimeLinkSuccess = (ProcAddI != NULL)) != 0)
946 {
947 if (string_result == NULL)
948 retval_int = ((MYINTPROCINT)ProcAddI)(argint);
949 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100950 retval_str = (char_u *)(ProcAddI)(argint);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 }
952 }
953
954 // Save the string before we free the library.
955 // Assume that a "1" result is an illegal pointer.
956 if (string_result == NULL)
957 *number_result = retval_int;
958 else if (retval_str != NULL
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100959 && (len = check_str_len(retval_str)) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 {
Bram Moolenaar18a4ba22019-05-24 19:39:03 +0200961 *string_result = alloc(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 if (*string_result != NULL)
963 mch_memmove(*string_result, retval_str, len);
964 }
965
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100966# ifdef HAVE_TRY_EXCEPT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 }
968 __except(EXCEPTION_EXECUTE_HANDLER)
969 {
970 if (GetExceptionCode() == EXCEPTION_STACK_OVERFLOW)
K.Takatac351dc12022-01-24 11:24:08 +0000971 _resetstkoflw();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972 fRunTimeLinkSuccess = 0;
973 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100974# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975
976 // Free the DLL module.
977 (void)FreeLibrary(hinstLib);
978 }
979
980 if (!fRunTimeLinkSuccess)
981 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000982 semsg(_(e_library_call_failed_for_str), funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 return FAIL;
984 }
985
986 return OK;
987}
988#endif
989
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990/*
991 * Debugging helper: expose the MCH_WRITE_DUMP stuff to other modules
992 */
993 void
Bram Moolenaar1266d672017-02-01 13:43:36 +0100994DumpPutS(const char *psz UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100996#ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 if (fdDump)
998 {
999 fputs(psz, fdDump);
1000 if (psz[strlen(psz) - 1] != '\n')
1001 fputc('\n', fdDump);
1002 fflush(fdDump);
1003 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001004#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005}
1006
1007#ifdef _DEBUG
1008
1009void __cdecl
1010Trace(
1011 char *pszFormat,
1012 ...)
1013{
1014 CHAR szBuff[2048];
1015 va_list args;
1016
1017 va_start(args, pszFormat);
1018 vsprintf(szBuff, pszFormat, args);
1019 va_end(args);
1020
1021 OutputDebugString(szBuff);
1022}
1023
1024#endif //_DEBUG
1025
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001026#if !defined(FEAT_GUI) || defined(VIMDLL) || defined(PROTO)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001027extern HWND g_hWnd; // This is in os_win32.c.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028
1029/*
1030 * Showing the printer dialog is tricky since we have no GUI
1031 * window to parent it. The following routines are needed to
1032 * get the window parenting and Z-order to work properly.
1033 */
1034 static void
1035GetConsoleHwnd(void)
1036{
Bram Moolenaar0f873732019-12-05 20:28:46 +01001037 // Skip if it's already set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 if (s_hwnd != 0)
1039 return;
1040
Bram Moolenaar0f873732019-12-05 20:28:46 +01001041 // Window handle may have been found by init code (Windows NT only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 if (g_hWnd != 0)
1043 {
1044 s_hwnd = g_hWnd;
1045 return;
1046 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047
Bram Moolenaare1ed53f2019-02-12 23:12:37 +01001048 s_hwnd = GetConsoleWindow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049}
Bram Moolenaar843ee412004-06-30 16:16:41 +00001050
1051/*
1052 * Console implementation of ":winpos".
1053 */
1054 int
1055mch_get_winpos(int *x, int *y)
1056{
1057 RECT rect;
1058
1059 GetConsoleHwnd();
1060 GetWindowRect(s_hwnd, &rect);
1061 *x = rect.left;
1062 *y = rect.top;
1063 return OK;
1064}
1065
1066/*
1067 * Console implementation of ":winpos x y".
1068 */
1069 void
1070mch_set_winpos(int x, int y)
1071{
1072 GetConsoleHwnd();
1073 SetWindowPos(s_hwnd, NULL, x, y, 0, 0,
1074 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
1075}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076#endif
1077
1078#if (defined(FEAT_PRINTER) && !defined(FEAT_POSTSCRIPT)) || defined(PROTO)
1079
Bram Moolenaar0f873732019-12-05 20:28:46 +01001080//=================================================================
1081// Win32 printer stuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082
1083static HFONT prt_font_handles[2][2][2];
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001084static PRINTDLGW prt_dlg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085static const int boldface[2] = {FW_REGULAR, FW_BOLD};
1086static TEXTMETRIC prt_tm;
1087static int prt_line_height;
1088static int prt_number_width;
1089static int prt_left_margin;
1090static int prt_right_margin;
1091static int prt_top_margin;
1092static char_u szAppName[] = TEXT("VIM");
1093static HWND hDlgPrint;
1094static int *bUserAbort = NULL;
1095static char_u *prt_name = NULL;
1096
Bram Moolenaar0f873732019-12-05 20:28:46 +01001097// Defines which are also in vim.rc.
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001098# define IDC_BOX1 400
1099# define IDC_PRINTTEXT1 401
1100# define IDC_PRINTTEXT2 402
1101# define IDC_PROGRESS 403
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001103 static BOOL
1104vimSetDlgItemText(HWND hDlg, int nIDDlgItem, char_u *s)
1105{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001106 WCHAR *wp;
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001107 BOOL ret;
1108
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001109 wp = enc_to_utf16(s, NULL);
1110 if (wp == NULL)
1111 return FALSE;
1112
1113 ret = SetDlgItemTextW(hDlg, nIDDlgItem, wp);
1114 vim_free(wp);
1115 return ret;
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001116}
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001117
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118/*
1119 * Convert BGR to RGB for Windows GDI calls
1120 */
1121 static COLORREF
1122swap_me(COLORREF colorref)
1123{
1124 int temp;
1125 char *ptr = (char *)&colorref;
1126
1127 temp = *(ptr);
1128 *(ptr ) = *(ptr + 2);
1129 *(ptr + 2) = temp;
1130 return colorref;
1131}
1132
K.Takatac351dc12022-01-24 11:24:08 +00001133 static INT_PTR CALLBACK
Bram Moolenaar1266d672017-02-01 13:43:36 +01001134PrintDlgProc(
1135 HWND hDlg,
1136 UINT message,
1137 WPARAM wParam UNUSED,
1138 LPARAM lParam UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001140# ifdef FEAT_GETTEXT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 NONCLIENTMETRICS nm;
1142 static HFONT hfont;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001143# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144
1145 switch (message)
1146 {
1147 case WM_INITDIALOG:
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001148# ifdef FEAT_GETTEXT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 nm.cbSize = sizeof(NONCLIENTMETRICS);
1150 if (SystemParametersInfo(
1151 SPI_GETNONCLIENTMETRICS,
1152 sizeof(NONCLIENTMETRICS),
1153 &nm,
1154 0))
1155 {
1156 char buff[MAX_PATH];
1157 int i;
1158
Bram Moolenaar0f873732019-12-05 20:28:46 +01001159 // Translate the dialog texts
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 hfont = CreateFontIndirect(&nm.lfMessageFont);
1161 for (i = IDC_PRINTTEXT1; i <= IDC_PROGRESS; i++)
1162 {
1163 SendDlgItemMessage(hDlg, i, WM_SETFONT, (WPARAM)hfont, 1);
1164 if (GetDlgItemText(hDlg,i, buff, sizeof(buff)))
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001165 vimSetDlgItemText(hDlg,i, (char_u *)_(buff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 }
1167 SendDlgItemMessage(hDlg, IDCANCEL,
1168 WM_SETFONT, (WPARAM)hfont, 1);
1169 if (GetDlgItemText(hDlg,IDCANCEL, buff, sizeof(buff)))
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001170 vimSetDlgItemText(hDlg,IDCANCEL, (char_u *)_(buff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001172# endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001173 SetWindowText(hDlg, (LPCSTR)szAppName);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 if (prt_name != NULL)
1175 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001176 vimSetDlgItemText(hDlg, IDC_PRINTTEXT2, (char_u *)prt_name);
Bram Moolenaard23a8232018-02-10 18:45:26 +01001177 VIM_CLEAR(prt_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 }
1179 EnableMenuItem(GetSystemMenu(hDlg, FALSE), SC_CLOSE, MF_GRAYED);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001180# if !defined(FEAT_GUI) || defined(VIMDLL)
1181# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001182 if (!gui.in_use)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001183# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001184 BringWindowToTop(s_hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001185# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 return TRUE;
1187
1188 case WM_COMMAND:
1189 *bUserAbort = TRUE;
1190 EnableWindow(GetParent(hDlg), TRUE);
1191 DestroyWindow(hDlg);
1192 hDlgPrint = NULL;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001193# ifdef FEAT_GETTEXT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 DeleteObject(hfont);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001195# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 return TRUE;
1197 }
1198 return FALSE;
1199}
1200
1201 static BOOL CALLBACK
Bram Moolenaar1266d672017-02-01 13:43:36 +01001202AbortProc(HDC hdcPrn UNUSED, int iCode UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203{
1204 MSG msg;
1205
K.Takatab7057bd2022-01-21 11:37:07 +00001206 while (!*bUserAbort && PeekMessageW(&msg, NULL, 0, 0, PM_REMOVE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 {
K.Takatab7057bd2022-01-21 11:37:07 +00001208 if (!hDlgPrint || !IsDialogMessageW(hDlgPrint, &msg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 {
1210 TranslateMessage(&msg);
K.Takatab7057bd2022-01-21 11:37:07 +00001211 DispatchMessageW(&msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 }
1213 }
1214 return !*bUserAbort;
1215}
1216
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001217# if !defined(FEAT_GUI) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218
Bram Moolenaar26fdd7d2011-11-30 13:42:44 +01001219 static UINT_PTR CALLBACK
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220PrintHookProc(
1221 HWND hDlg, // handle to dialog box
1222 UINT uiMsg, // message identifier
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02001223 WPARAM wParam UNUSED, // message parameter
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 LPARAM lParam // message parameter
1225 )
1226{
1227 HWND hwndOwner;
1228 RECT rc, rcDlg, rcOwner;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001229 PRINTDLGW *pPD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001231 if (uiMsg != WM_INITDIALOG)
1232 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001234 // Get the owner window and dialog box rectangles.
1235 if ((hwndOwner = GetParent(hDlg)) == NULL)
1236 hwndOwner = GetDesktopWindow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001238 GetWindowRect(hwndOwner, &rcOwner);
1239 GetWindowRect(hDlg, &rcDlg);
1240 CopyRect(&rc, &rcOwner);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001242 // Offset the owner and dialog box rectangles so that
1243 // right and bottom values represent the width and
1244 // height, and then offset the owner again to discard
1245 // space taken up by the dialog box.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001247 OffsetRect(&rcDlg, -rcDlg.left, -rcDlg.top);
1248 OffsetRect(&rc, -rc.left, -rc.top);
1249 OffsetRect(&rc, -rcDlg.right, -rcDlg.bottom);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001251 // The new position is the sum of half the remaining
1252 // space and the owner's original position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001254 SetWindowPos(hDlg,
1255 HWND_TOP,
1256 rcOwner.left + (rc.right / 2),
1257 rcOwner.top + (rc.bottom / 2),
1258 0, 0, // ignores size arguments
1259 SWP_NOSIZE);
1260
1261 // tackle the printdlg copiesctrl problem
1262 pPD = (PRINTDLGW *)lParam;
1263 pPD->nCopies = (WORD)pPD->lCustData;
1264 SetDlgItemInt( hDlg, edt3, pPD->nCopies, FALSE );
1265 // Bring the window to top
1266 BringWindowToTop(GetParent(hDlg));
1267 SetForegroundWindow(hDlg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268
1269 return FALSE;
1270}
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001271# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272
1273 void
1274mch_print_cleanup(void)
1275{
1276 int pifItalic;
1277 int pifBold;
1278 int pifUnderline;
1279
1280 for (pifBold = 0; pifBold <= 1; pifBold++)
1281 for (pifItalic = 0; pifItalic <= 1; pifItalic++)
1282 for (pifUnderline = 0; pifUnderline <= 1; pifUnderline++)
1283 DeleteObject(prt_font_handles[pifBold][pifItalic][pifUnderline]);
1284
1285 if (prt_dlg.hDC != NULL)
1286 DeleteDC(prt_dlg.hDC);
1287 if (!*bUserAbort)
1288 SendMessage(hDlgPrint, WM_COMMAND, 0, 0);
1289}
1290
1291 static int
1292to_device_units(int idx, int dpi, int physsize, int offset, int def_number)
1293{
1294 int ret = 0;
1295 int u;
1296 int nr;
1297
1298 u = prt_get_unit(idx);
1299 if (u == PRT_UNIT_NONE)
1300 {
1301 u = PRT_UNIT_PERC;
1302 nr = def_number;
1303 }
1304 else
1305 nr = printer_opts[idx].number;
1306
1307 switch (u)
1308 {
1309 case PRT_UNIT_PERC:
1310 ret = (physsize * nr) / 100;
1311 break;
1312 case PRT_UNIT_INCH:
1313 ret = (nr * dpi);
1314 break;
1315 case PRT_UNIT_MM:
1316 ret = (nr * 10 * dpi) / 254;
1317 break;
1318 case PRT_UNIT_POINT:
1319 ret = (nr * 10 * dpi) / 720;
1320 break;
1321 }
1322
1323 if (ret < offset)
1324 return 0;
1325 else
1326 return ret - offset;
1327}
1328
1329 static int
1330prt_get_cpl(void)
1331{
1332 int hr;
1333 int phyw;
1334 int dvoff;
1335 int rev_offset;
1336 int dpi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337
1338 GetTextMetrics(prt_dlg.hDC, &prt_tm);
1339 prt_line_height = prt_tm.tmHeight + prt_tm.tmExternalLeading;
1340
1341 hr = GetDeviceCaps(prt_dlg.hDC, HORZRES);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 phyw = GetDeviceCaps(prt_dlg.hDC, PHYSICALWIDTH);
1343 dvoff = GetDeviceCaps(prt_dlg.hDC, PHYSICALOFFSETX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 dpi = GetDeviceCaps(prt_dlg.hDC, LOGPIXELSX);
1345
1346 rev_offset = phyw - (dvoff + hr);
1347
1348 prt_left_margin = to_device_units(OPT_PRINT_LEFT, dpi, phyw, dvoff, 10);
1349 if (prt_use_number())
1350 {
1351 prt_number_width = PRINT_NUMBER_WIDTH * prt_tm.tmAveCharWidth;
1352 prt_left_margin += prt_number_width;
1353 }
1354 else
1355 prt_number_width = 0;
1356
1357 prt_right_margin = hr - to_device_units(OPT_PRINT_RIGHT, dpi, phyw,
1358 rev_offset, 5);
1359
1360 return (prt_right_margin - prt_left_margin) / prt_tm.tmAveCharWidth;
1361}
1362
1363 static int
1364prt_get_lpp(void)
1365{
1366 int vr;
1367 int phyw;
1368 int dvoff;
1369 int rev_offset;
1370 int bottom_margin;
1371 int dpi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372
1373 vr = GetDeviceCaps(prt_dlg.hDC, VERTRES);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 phyw = GetDeviceCaps(prt_dlg.hDC, PHYSICALHEIGHT);
1375 dvoff = GetDeviceCaps(prt_dlg.hDC, PHYSICALOFFSETY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 dpi = GetDeviceCaps(prt_dlg.hDC, LOGPIXELSY);
1377
1378 rev_offset = phyw - (dvoff + vr);
1379
1380 prt_top_margin = to_device_units(OPT_PRINT_TOP, dpi, phyw, dvoff, 5);
1381
Bram Moolenaar0f873732019-12-05 20:28:46 +01001382 // adjust top margin if there is a header
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 prt_top_margin += prt_line_height * prt_header_height();
1384
1385 bottom_margin = vr - to_device_units(OPT_PRINT_BOT, dpi, phyw,
1386 rev_offset, 5);
1387
1388 return (bottom_margin - prt_top_margin) / prt_line_height;
1389}
1390
1391 int
1392mch_print_init(prt_settings_T *psettings, char_u *jobname, int forceit)
1393{
1394 static HGLOBAL stored_dm = NULL;
1395 static HGLOBAL stored_devn = NULL;
1396 static int stored_nCopies = 1;
1397 static int stored_nFlags = 0;
1398
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001399 LOGFONTW fLogFont;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 int pifItalic;
1401 int pifBold;
1402 int pifUnderline;
1403
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001404 DEVMODEW *mem;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 DEVNAMES *devname;
1406 int i;
Bram Moolenaarb391e1f2023-01-16 19:51:03 +00001407 DWORD err;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408
1409 bUserAbort = &(psettings->user_abort);
Bram Moolenaara80faa82020-04-12 19:37:17 +02001410 CLEAR_FIELD(prt_dlg);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001411 prt_dlg.lStructSize = sizeof(PRINTDLGW);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001412# if !defined(FEAT_GUI) || defined(VIMDLL)
1413# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001414 if (!gui.in_use)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001415# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01001416 GetConsoleHwnd(); // get value of s_hwnd
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001417# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 prt_dlg.hwndOwner = s_hwnd;
1419 prt_dlg.Flags = PD_NOPAGENUMS | PD_NOSELECTION | PD_RETURNDC;
1420 if (!forceit)
1421 {
1422 prt_dlg.hDevMode = stored_dm;
1423 prt_dlg.hDevNames = stored_devn;
1424 prt_dlg.lCustData = stored_nCopies; // work around bug in print dialog
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001425# if !defined(FEAT_GUI) || defined(VIMDLL)
1426# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001427 if (!gui.in_use)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001428# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001429 {
1430 /*
1431 * Use hook to prevent console window being sent to back
1432 */
1433 prt_dlg.lpfnPrintHook = PrintHookProc;
1434 prt_dlg.Flags |= PD_ENABLEPRINTHOOK;
1435 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001436# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 prt_dlg.Flags |= stored_nFlags;
1438 }
1439
1440 /*
1441 * If bang present, return default printer setup with no dialog
1442 * never show dialog if we are running over telnet
1443 */
1444 if (forceit
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001445# if !defined(FEAT_GUI) || defined(VIMDLL)
1446# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001447 || (!gui.in_use && !term_console)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001448# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 || !term_console
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001450# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001451# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 )
1453 {
1454 prt_dlg.Flags |= PD_RETURNDEFAULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 /*
1456 * MSDN suggests setting the first parameter to WINSPOOL for
1457 * NT, but NULL appears to work just as well.
1458 */
1459 if (*p_pdev != NUL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001460 prt_dlg.hDC = CreateDC(NULL, (LPCSTR)p_pdev, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 {
1463 prt_dlg.Flags |= PD_RETURNDEFAULT;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001464 if (PrintDlgW(&prt_dlg) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 goto init_fail_dlg;
1466 }
1467 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001468 else if (PrintDlgW(&prt_dlg) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 goto init_fail_dlg;
1470 else
1471 {
1472 /*
1473 * keep the previous driver context
1474 */
1475 stored_dm = prt_dlg.hDevMode;
1476 stored_devn = prt_dlg.hDevNames;
1477 stored_nFlags = prt_dlg.Flags;
1478 stored_nCopies = prt_dlg.nCopies;
1479 }
1480
1481 if (prt_dlg.hDC == NULL)
1482 {
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00001483 emsg(_(e_printer_selection_failed));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 mch_print_cleanup();
1485 return FALSE;
1486 }
1487
Bram Moolenaar0f873732019-12-05 20:28:46 +01001488 // Not all printer drivers report the support of color (or grey) in the
1489 // same way. Let's set has_color if there appears to be some way to print
1490 // more than B&W.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 i = GetDeviceCaps(prt_dlg.hDC, NUMCOLORS);
1492 psettings->has_color = (GetDeviceCaps(prt_dlg.hDC, BITSPIXEL) > 1
1493 || GetDeviceCaps(prt_dlg.hDC, PLANES) > 1
1494 || i > 2 || i == -1);
1495
Bram Moolenaar0f873732019-12-05 20:28:46 +01001496 // Ensure all font styles are baseline aligned
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 SetTextAlign(prt_dlg.hDC, TA_BASELINE|TA_LEFT);
1498
1499 /*
1500 * On some windows systems the nCopies parameter is not
1501 * passed back correctly. It must be retrieved from the
1502 * hDevMode struct.
1503 */
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001504 mem = (DEVMODEW *)GlobalLock(prt_dlg.hDevMode);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 if (mem != NULL)
1506 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 if (mem->dmCopies != 1)
1508 stored_nCopies = mem->dmCopies;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 if ((mem->dmFields & DM_DUPLEX) && (mem->dmDuplex & ~DMDUP_SIMPLEX))
1510 psettings->duplex = TRUE;
1511 if ((mem->dmFields & DM_COLOR) && (mem->dmColor & DMCOLOR_COLOR))
1512 psettings->has_color = TRUE;
1513 }
1514 GlobalUnlock(prt_dlg.hDevMode);
1515
1516 devname = (DEVNAMES *)GlobalLock(prt_dlg.hDevNames);
1517 if (devname != 0)
1518 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001519 WCHAR *wprinter_name = (WCHAR *)devname + devname->wDeviceOffset;
1520 WCHAR *wport_name = (WCHAR *)devname + devname->wOutputOffset;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001521 char_u *text = (char_u *)_("to %s on %s");
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001522 char_u *printer_name = utf16_to_enc(wprinter_name, NULL);
1523 char_u *port_name = utf16_to_enc(wport_name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001525 if (printer_name != NULL && port_name != NULL)
Bram Moolenaar964b3742019-05-24 18:54:09 +02001526 prt_name = alloc(STRLEN(printer_name)
1527 + STRLEN(port_name) + STRLEN(text));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 if (prt_name != NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001529 wsprintf((char *)prt_name, (const char *)text,
1530 printer_name, port_name);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001531 vim_free(printer_name);
1532 vim_free(port_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 }
1534 GlobalUnlock(prt_dlg.hDevNames);
1535
1536 /*
1537 * Initialise the font according to 'printfont'
1538 */
Bram Moolenaara80faa82020-04-12 19:37:17 +02001539 CLEAR_FIELD(fLogFont);
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001540 if (get_logfont(&fLogFont, p_pfn, prt_dlg.hDC, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 {
Bram Moolenaard88be5b2022-01-04 19:57:55 +00001542 semsg(_(e_unknown_printer_font_str), p_pfn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 mch_print_cleanup();
1544 return FALSE;
1545 }
1546
1547 for (pifBold = 0; pifBold <= 1; pifBold++)
1548 for (pifItalic = 0; pifItalic <= 1; pifItalic++)
1549 for (pifUnderline = 0; pifUnderline <= 1; pifUnderline++)
1550 {
1551 fLogFont.lfWeight = boldface[pifBold];
1552 fLogFont.lfItalic = pifItalic;
1553 fLogFont.lfUnderline = pifUnderline;
1554 prt_font_handles[pifBold][pifItalic][pifUnderline]
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001555 = CreateFontIndirectW(&fLogFont);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 }
1557
1558 SetBkMode(prt_dlg.hDC, OPAQUE);
1559 SelectObject(prt_dlg.hDC, prt_font_handles[0][0][0]);
1560
1561 /*
1562 * Fill in the settings struct
1563 */
1564 psettings->chars_per_line = prt_get_cpl();
1565 psettings->lines_per_page = prt_get_lpp();
Bram Moolenaar7ddc6422014-09-27 11:18:19 +02001566 if (prt_dlg.Flags & PD_USEDEVMODECOPIESANDCOLLATE)
1567 {
1568 psettings->n_collated_copies = (prt_dlg.Flags & PD_COLLATE)
1569 ? prt_dlg.nCopies : 1;
1570 psettings->n_uncollated_copies = (prt_dlg.Flags & PD_COLLATE)
1571 ? 1 : prt_dlg.nCopies;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572
Bram Moolenaar7ddc6422014-09-27 11:18:19 +02001573 if (psettings->n_collated_copies == 0)
1574 psettings->n_collated_copies = 1;
1575
1576 if (psettings->n_uncollated_copies == 0)
1577 psettings->n_uncollated_copies = 1;
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01001578 }
1579 else
1580 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 psettings->n_collated_copies = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 psettings->n_uncollated_copies = 1;
Bram Moolenaar7ddc6422014-09-27 11:18:19 +02001583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584
1585 psettings->jobname = jobname;
1586
1587 return TRUE;
1588
1589init_fail_dlg:
Bram Moolenaarb391e1f2023-01-16 19:51:03 +00001590 err = CommDlgExtendedError();
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001591 if (err)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001593 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001595 // I suspect FormatMessage() doesn't work for values returned by
1596 // CommDlgExtendedError(). What does?
1597 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
1598 FORMAT_MESSAGE_FROM_SYSTEM |
1599 FORMAT_MESSAGE_IGNORE_INSERTS,
1600 NULL, err, 0, (LPTSTR)(&buf), 0, NULL);
1601 semsg(_(e_print_error_str),
1602 buf == NULL ? (char_u *)_("Unknown") : buf);
1603 LocalFree((LPVOID)(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001605 else
1606 msg_clr_eos(); // Maybe canceled
1607
1608 mch_print_cleanup();
1609 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610}
1611
1612
1613 int
1614mch_print_begin(prt_settings_T *psettings)
1615{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001616 int ret = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617 char szBuffer[300];
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001618 WCHAR *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001620 hDlgPrint = CreateDialog(g_hinst, TEXT("PrintDlgBox"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 prt_dlg.hwndOwner, PrintDlgProc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 SetAbortProc(prt_dlg.hDC, AbortProc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 wsprintf(szBuffer, _("Printing '%s'"), gettail(psettings->jobname));
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001624 vimSetDlgItemText(hDlgPrint, IDC_PRINTTEXT1, (char_u *)szBuffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001626 wp = enc_to_utf16(psettings->jobname, NULL);
Bram Moolenaar2290b1f2018-05-13 17:30:45 +02001627 if (wp != NULL)
1628 {
1629 DOCINFOW di;
1630
Bram Moolenaara80faa82020-04-12 19:37:17 +02001631 CLEAR_FIELD(di);
Bram Moolenaar2290b1f2018-05-13 17:30:45 +02001632 di.cbSize = sizeof(di);
1633 di.lpszDocName = wp;
1634 ret = StartDocW(prt_dlg.hDC, &di);
1635 vim_free(wp);
1636 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001638# ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01001639 // Give focus back to main window (when using MDI).
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001640# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001641 if (gui.in_use)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001642# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001643 SetFocus(s_hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001644# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645
1646 return (ret > 0);
1647}
1648
1649 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01001650mch_print_end(prt_settings_T *psettings UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651{
1652 EndDoc(prt_dlg.hDC);
1653 if (!*bUserAbort)
1654 SendMessage(hDlgPrint, WM_COMMAND, 0, 0);
1655}
1656
1657 int
1658mch_print_end_page(void)
1659{
1660 return (EndPage(prt_dlg.hDC) > 0);
1661}
1662
1663 int
1664mch_print_begin_page(char_u *msg)
1665{
1666 if (msg != NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001667 vimSetDlgItemText(hDlgPrint, IDC_PROGRESS, msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 return (StartPage(prt_dlg.hDC) > 0);
1669}
1670
1671 int
1672mch_print_blank_page(void)
1673{
1674 return (mch_print_begin_page(NULL) ? (mch_print_end_page()) : FALSE);
1675}
1676
1677static int prt_pos_x = 0;
1678static int prt_pos_y = 0;
1679
1680 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001681mch_print_start_line(int margin, int page_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682{
1683 if (margin)
1684 prt_pos_x = -prt_number_width;
1685 else
1686 prt_pos_x = 0;
1687 prt_pos_y = page_line * prt_line_height
1688 + prt_tm.tmAscent + prt_tm.tmExternalLeading;
1689}
1690
1691 int
1692mch_print_text_out(char_u *p, int len)
1693{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 SIZE sz;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001695 WCHAR *wp;
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001696 int wlen = len;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001697 int ret = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001699 wp = enc_to_utf16(p, &wlen);
1700 if (wp == NULL)
1701 return FALSE;
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001702
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001703 TextOutW(prt_dlg.hDC, prt_pos_x + prt_left_margin,
1704 prt_pos_y + prt_top_margin, wp, wlen);
1705 GetTextExtentPoint32W(prt_dlg.hDC, wp, wlen, &sz);
1706 vim_free(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 prt_pos_x += (sz.cx - prt_tm.tmOverhang);
Bram Moolenaar0f873732019-12-05 20:28:46 +01001708 // This is wrong when printing spaces for a TAB.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001709 if (p[len] != NUL)
1710 {
Bram Moolenaar1614a142019-10-06 22:00:13 +02001711 wlen = mb_ptr2len(p + len);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001712 wp = enc_to_utf16(p + len, &wlen);
1713 if (wp != NULL)
1714 {
1715 GetTextExtentPoint32W(prt_dlg.hDC, wp, 1, &sz);
1716 ret = (prt_pos_x + prt_left_margin + sz.cx > prt_right_margin);
1717 vim_free(wp);
1718 }
1719 }
1720 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721}
1722
1723 void
1724mch_print_set_font(int iBold, int iItalic, int iUnderline)
1725{
1726 SelectObject(prt_dlg.hDC, prt_font_handles[iBold][iItalic][iUnderline]);
1727}
1728
1729 void
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001730mch_print_set_bg(long_u bgcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731{
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001732 SetBkColor(prt_dlg.hDC, GetNearestColor(prt_dlg.hDC,
1733 swap_me((COLORREF)bgcol)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 /*
1735 * With a white background we can draw characters transparent, which is
1736 * good for italic characters that overlap to the next char cell.
1737 */
1738 if (bgcol == 0xffffffUL)
1739 SetBkMode(prt_dlg.hDC, TRANSPARENT);
1740 else
1741 SetBkMode(prt_dlg.hDC, OPAQUE);
1742}
1743
1744 void
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001745mch_print_set_fg(long_u fgcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746{
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001747 SetTextColor(prt_dlg.hDC, GetNearestColor(prt_dlg.hDC,
1748 swap_me((COLORREF)fgcol)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749}
1750
Bram Moolenaar0f873732019-12-05 20:28:46 +01001751#endif // FEAT_PRINTER && !FEAT_POSTSCRIPT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752
Bram Moolenaar58d98232005-07-23 22:25:46 +00001753
1754
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755#if defined(FEAT_SHORTCUT) || defined(PROTO)
Bram Moolenaar82881492012-11-20 16:53:39 +01001756# ifndef PROTO
1757# include <shlobj.h>
1758# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759
Bram Moolenaar4a792c82019-06-06 12:22:41 +02001760# define is_path_sep(c) ((c) == L'\\' || (c) == L'/')
1761
1762 static int
1763is_reparse_point_included(LPCWSTR fname)
1764{
1765 LPCWSTR p = fname, q;
1766 WCHAR buf[MAX_PATH];
1767 DWORD attr;
1768
Keith Thompson184f71c2024-01-04 21:19:04 +01001769 if (SAFE_isalpha(p[0]) && p[1] == L':' && is_path_sep(p[2]))
Bram Moolenaar4a792c82019-06-06 12:22:41 +02001770 p += 3;
1771 else if (is_path_sep(p[0]) && is_path_sep(p[1]))
1772 p += 2;
1773
1774 while (*p != L'\0')
1775 {
1776 q = wcspbrk(p, L"\\/");
1777 if (q == NULL)
1778 p = q = fname + wcslen(fname);
1779 else
1780 p = q + 1;
1781 if (q - fname >= MAX_PATH)
1782 return FALSE;
1783 wcsncpy(buf, fname, q - fname);
1784 buf[q - fname] = L'\0';
1785 attr = GetFileAttributesW(buf);
1786 if (attr != INVALID_FILE_ATTRIBUTES
1787 && (attr & FILE_ATTRIBUTE_REPARSE_POINT) != 0)
1788 return TRUE;
1789 }
1790 return FALSE;
1791}
1792
AmberArrf5d0f542023-08-20 20:03:45 +02001793/*
1794 * Return the resolved file path, NULL if "fname" is an AppExecLink reparse
1795 * point, already fully resolved, or it doesn't exists.
1796 */
1797 char_u *
Bram Moolenaardce1e892019-02-10 23:18:53 +01001798resolve_reparse_point(char_u *fname)
1799{
1800 HANDLE h = INVALID_HANDLE_VALUE;
1801 DWORD size;
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001802 WCHAR *p, *wp;
Bram Moolenaardce1e892019-02-10 23:18:53 +01001803 char_u *rfname = NULL;
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001804 WCHAR *buff = NULL;
Bram Moolenaardce1e892019-02-10 23:18:53 +01001805
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001806 p = enc_to_utf16(fname, NULL);
1807 if (p == NULL)
1808 goto fail;
1809
Bram Moolenaar4a792c82019-06-06 12:22:41 +02001810 if (!is_reparse_point_included(p))
1811 {
1812 vim_free(p);
1813 goto fail;
1814 }
1815
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001816 h = CreateFileW(p, 0, 0, NULL, OPEN_EXISTING,
1817 FILE_FLAG_BACKUP_SEMANTICS, NULL);
1818 vim_free(p);
Bram Moolenaardce1e892019-02-10 23:18:53 +01001819
1820 if (h == INVALID_HANDLE_VALUE)
1821 goto fail;
1822
K.Takata27b53be2022-09-18 12:25:49 +01001823 size = GetFinalPathNameByHandleW(h, NULL, 0, 0);
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001824 if (size == 0)
1825 goto fail;
1826 buff = ALLOC_MULT(WCHAR, size);
1827 if (buff == NULL)
1828 goto fail;
K.Takata27b53be2022-09-18 12:25:49 +01001829 if (GetFinalPathNameByHandleW(h, buff, size, 0) == 0)
Bram Moolenaardce1e892019-02-10 23:18:53 +01001830 goto fail;
1831
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001832 if (wcsncmp(buff, L"\\\\?\\UNC\\", 8) == 0)
1833 {
1834 buff[6] = L'\\';
1835 wp = buff + 6;
1836 }
1837 else if (wcsncmp(buff, L"\\\\?\\", 4) == 0)
1838 wp = buff + 4;
Bram Moolenaardce1e892019-02-10 23:18:53 +01001839 else
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001840 wp = buff;
1841
1842 rfname = utf16_to_enc(wp, NULL);
Bram Moolenaardce1e892019-02-10 23:18:53 +01001843
1844fail:
1845 if (h != INVALID_HANDLE_VALUE)
1846 CloseHandle(h);
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001847 if (buff != NULL)
1848 vim_free(buff);
Bram Moolenaardce1e892019-02-10 23:18:53 +01001849
1850 return rfname;
1851}
1852
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853/*
1854 * When "fname" is the name of a shortcut (*.lnk) resolve the file it points
1855 * to and return that name in allocated memory.
1856 * Otherwise NULL is returned.
1857 */
Bram Moolenaardce1e892019-02-10 23:18:53 +01001858 static char_u *
1859resolve_shortcut(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860{
1861 HRESULT hr;
1862 IShellLink *psl = NULL;
1863 IPersistFile *ppf = NULL;
1864 OLECHAR wsz[MAX_PATH];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 char_u *rfname = NULL;
1866 int len;
Bram Moolenaar604729e2013-08-30 16:44:19 +02001867 IShellLinkW *pslw = NULL;
1868 WIN32_FIND_DATAW ffdw; // we get those free of charge
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869
Bram Moolenaar0f873732019-12-05 20:28:46 +01001870 // Check if the file name ends in ".lnk". Avoid calling
1871 // CoCreateInstance(), it's quite slow.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 if (fname == NULL)
1873 return rfname;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001874 len = (int)STRLEN(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 if (len <= 4 || STRNICMP(fname + len - 4, ".lnk", 4) != 0)
1876 return rfname;
1877
1878 CoInitialize(NULL);
1879
1880 // create a link manager object and request its interface
1881 hr = CoCreateInstance(
1882 &CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001883 &IID_IShellLinkW, (void**)&pslw);
1884 if (hr == S_OK)
1885 {
1886 WCHAR *p = enc_to_utf16(fname, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001888 if (p != NULL)
1889 {
1890 // Get a pointer to the IPersistFile interface.
1891 hr = pslw->lpVtbl->QueryInterface(
1892 pslw, &IID_IPersistFile, (void**)&ppf);
1893 if (hr != S_OK)
1894 goto shortcut_errorw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001896 // "load" the name and resolve the link
1897 hr = ppf->lpVtbl->Load(ppf, p, STGM_READ);
1898 if (hr != S_OK)
1899 goto shortcut_errorw;
1900# if 0 // This makes Vim wait a long time if the target does not exist.
1901 hr = pslw->lpVtbl->Resolve(pslw, NULL, SLR_NO_UI);
1902 if (hr != S_OK)
1903 goto shortcut_errorw;
Bram Moolenaar604729e2013-08-30 16:44:19 +02001904# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001906 // Get the path to the link target.
1907 ZeroMemory(wsz, MAX_PATH * sizeof(WCHAR));
1908 hr = pslw->lpVtbl->GetPath(pslw, wsz, MAX_PATH, &ffdw, 0);
1909 if (hr == S_OK && wsz[0] != NUL)
1910 rfname = utf16_to_enc(wsz, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001912shortcut_errorw:
1913 vim_free(p);
1914 }
1915 }
1916
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 // Release all interface pointers (both belong to the same object)
1918 if (ppf != NULL)
1919 ppf->lpVtbl->Release(ppf);
1920 if (psl != NULL)
1921 psl->lpVtbl->Release(psl);
Bram Moolenaar604729e2013-08-30 16:44:19 +02001922 if (pslw != NULL)
1923 pslw->lpVtbl->Release(pslw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924
1925 CoUninitialize();
1926 return rfname;
1927}
Bram Moolenaardce1e892019-02-10 23:18:53 +01001928
1929 char_u *
1930mch_resolve_path(char_u *fname, int reparse_point)
1931{
1932 char_u *path = resolve_shortcut(fname);
1933
1934 if (path == NULL && reparse_point)
1935 path = resolve_reparse_point(fname);
1936 return path;
1937}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938#endif
1939
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001940#if (defined(FEAT_EVAL) && (!defined(FEAT_GUI) || defined(VIMDLL))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941/*
1942 * Bring ourselves to the foreground. Does work if the OS doesn't allow it.
1943 */
1944 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001945win32_set_foreground(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946{
Bram Moolenaar0f873732019-12-05 20:28:46 +01001947 GetConsoleHwnd(); // get value of s_hwnd
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 if (s_hwnd != 0)
1949 SetForegroundWindow(s_hwnd);
1950}
1951#endif
1952
1953#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
1954/*
1955 * Client-server code for Vim
1956 *
1957 * Originally written by Paul Moore
1958 */
1959
Bram Moolenaar0f873732019-12-05 20:28:46 +01001960// In order to handle inter-process messages, we need to have a window. But
1961// the functions in this module can be called before the main GUI window is
1962// created (and may also be called in the console version, where there is no
1963// GUI window at all).
1964//
1965// So we create a hidden window, and arrange to destroy it on exit.
1966HWND message_window = 0; // window that's handling messages
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001968# define VIM_CLASSNAME "VIM_MESSAGES"
1969# define VIM_CLASSNAME_LEN (sizeof(VIM_CLASSNAME) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970
K.Takataf9f2a332022-06-17 20:05:40 +01001971// Timeout for sending a message to another Vim instance. Normally this works
1972// instantly, but it may hang when the other Vim instance is halted.
1973# define SENDMESSAGE_TIMEOUT (5 * 1000)
1974
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001975// Communication is via WM_COPYDATA messages. The message type is sent in
Bram Moolenaar0f873732019-12-05 20:28:46 +01001976// the dwData parameter. Types are defined here.
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001977# define COPYDATA_KEYS 0
1978# define COPYDATA_REPLY 1
1979# define COPYDATA_EXPR 10
1980# define COPYDATA_RESULT 11
1981# define COPYDATA_ERROR_RESULT 12
1982# define COPYDATA_ENCODING 20
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983
Bram Moolenaar0f873732019-12-05 20:28:46 +01001984// This is a structure containing a server HWND and its name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985struct server_id
1986{
1987 HWND hwnd;
1988 char_u *name;
1989};
1990
Bram Moolenaar0f873732019-12-05 20:28:46 +01001991// Last received 'encoding' that the client uses.
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001992static char_u *client_enc = NULL;
1993
1994/*
1995 * Tell the other side what encoding we are using.
K.Takataf9f2a332022-06-17 20:05:40 +01001996 * Return -1 if timeout happens. Other errors are ignored.
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001997 */
K.Takataf9f2a332022-06-17 20:05:40 +01001998 static int
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001999serverSendEnc(HWND target)
2000{
2001 COPYDATASTRUCT data;
2002
2003 data.dwData = COPYDATA_ENCODING;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002004 data.cbData = (DWORD)STRLEN(p_enc) + 1;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002005 data.lpData = p_enc;
K.Takataf9f2a332022-06-17 20:05:40 +01002006 if (SendMessageTimeout(target, WM_COPYDATA,
2007 (WPARAM)message_window, (LPARAM)&data,
2008 SMTO_ABORTIFHUNG, SENDMESSAGE_TIMEOUT, NULL) == 0)
2009 return -1;
2010 return 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002011}
2012
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013/*
2014 * Clean up on exit. This destroys the hidden message window.
2015 */
2016 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017CleanUpMessaging(void)
2018{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002019 if (message_window == 0)
2020 return;
2021
2022 DestroyWindow(message_window);
2023 message_window = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024}
2025
2026static int save_reply(HWND server, char_u *reply, int expr);
2027
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002028/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 * The window procedure for the hidden message window.
2030 * It handles callback messages and notifications from servers.
2031 * In order to process these messages, it is necessary to run a
2032 * message loop. Code which may run before the main message loop
2033 * is started (in the GUI) is careful to pump messages when it needs
2034 * to. Features which require message delivery during normal use will
2035 * not work in the console version - this basically means those
2036 * features which allow Vim to act as a server, rather than a client.
2037 */
2038 static LRESULT CALLBACK
2039Messaging_WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
2040{
2041 if (msg == WM_COPYDATA)
2042 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002043 // This is a message from another Vim. The dwData member of the
2044 // COPYDATASTRUCT determines the type of message:
2045 // COPYDATA_ENCODING:
2046 // The encoding that the client uses. Following messages will
2047 // use this encoding, convert if needed.
2048 // COPYDATA_KEYS:
2049 // A key sequence. We are a server, and a client wants these keys
2050 // adding to the input queue.
2051 // COPYDATA_REPLY:
2052 // A reply. We are a client, and a server has sent this message
2053 // in response to a request. (server2client())
2054 // COPYDATA_EXPR:
2055 // An expression. We are a server, and a client wants us to
2056 // evaluate this expression.
2057 // COPYDATA_RESULT:
2058 // A reply. We are a client, and a server has sent this message
2059 // in response to a COPYDATA_EXPR.
2060 // COPYDATA_ERROR_RESULT:
2061 // A reply. We are a client, and a server has sent this message
2062 // in response to a COPYDATA_EXPR that failed to evaluate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 COPYDATASTRUCT *data = (COPYDATASTRUCT*)lParam;
2064 HWND sender = (HWND)wParam;
2065 COPYDATASTRUCT reply;
2066 char_u *res;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 int retval;
K.Takataf9f2a332022-06-17 20:05:40 +01002068 DWORD_PTR dwret = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002069 char_u *str;
2070 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071
2072 switch (data->dwData)
2073 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002074 case COPYDATA_ENCODING:
Bram Moolenaar0f873732019-12-05 20:28:46 +01002075 // Remember the encoding that the client uses.
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002076 vim_free(client_enc);
2077 client_enc = enc_canonize((char_u *)data->lpData);
2078 return 1;
2079
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 case COPYDATA_KEYS:
Bram Moolenaar0f873732019-12-05 20:28:46 +01002081 // Remember who sent this, for <client>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 clientWindow = sender;
2083
Bram Moolenaar0f873732019-12-05 20:28:46 +01002084 // Add the received keys to the input buffer. The loop waiting
2085 // for the user to do something should check the input buffer.
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002086 str = serverConvert(client_enc, (char_u *)data->lpData, &tofree);
2087 server_to_input_buf(str);
2088 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089
2090# ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01002091 // Wake up the main GUI loop.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002092# ifdef VIMDLL
2093 if (gui.in_use)
2094# endif
2095 if (s_hwnd != 0)
2096 PostMessage(s_hwnd, WM_NULL, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097# endif
2098 return 1;
2099
2100 case COPYDATA_EXPR:
Bram Moolenaar0f873732019-12-05 20:28:46 +01002101 // Remember who sent this, for <client>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 clientWindow = sender;
2103
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002104 str = serverConvert(client_enc, (char_u *)data->lpData, &tofree);
2105 res = eval_client_expr_to_string(str);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002106
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 if (res == NULL)
2108 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00002109 char *err = _(e_invalid_expression_received);
Bram Moolenaar15bf76d2017-03-18 16:18:37 +01002110 size_t len = STRLEN(str) + STRLEN(err) + 5;
2111
Bram Moolenaar964b3742019-05-24 18:54:09 +02002112 res = alloc(len);
Bram Moolenaar15bf76d2017-03-18 16:18:37 +01002113 if (res != NULL)
2114 vim_snprintf((char *)res, len, "%s: \"%s\"", err, str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 reply.dwData = COPYDATA_ERROR_RESULT;
2116 }
2117 else
2118 reply.dwData = COPYDATA_RESULT;
2119 reply.lpData = res;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002120 reply.cbData = (DWORD)STRLEN(res) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121
K.Takataf9f2a332022-06-17 20:05:40 +01002122 if (serverSendEnc(sender) < 0)
2123 retval = -1;
2124 else
2125 {
2126 if (SendMessageTimeout(sender, WM_COPYDATA,
2127 (WPARAM)message_window, (LPARAM)&reply,
2128 SMTO_ABORTIFHUNG, SENDMESSAGE_TIMEOUT, &dwret) == 0)
2129 retval = -1;
2130 else
2131 retval = (int)dwret;
2132 }
Bram Moolenaar15bf76d2017-03-18 16:18:37 +01002133 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 vim_free(res);
2135 return retval;
2136
2137 case COPYDATA_REPLY:
2138 case COPYDATA_RESULT:
2139 case COPYDATA_ERROR_RESULT:
2140 if (data->lpData != NULL)
2141 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002142 str = serverConvert(client_enc, (char_u *)data->lpData,
2143 &tofree);
2144 if (tofree == NULL)
2145 str = vim_strsave(str);
2146 if (save_reply(sender, str,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 (data->dwData == COPYDATA_REPLY ? 0 :
2148 (data->dwData == COPYDATA_RESULT ? 1 :
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002149 2))) == FAIL)
2150 vim_free(str);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002151 else if (data->dwData == COPYDATA_REPLY)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 {
Bram Moolenaar427d51c2013-06-16 16:01:25 +02002153 char_u winstr[30];
2154
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002155 sprintf((char *)winstr, PRINTF_HEX_LONG_U, (long_u)sender);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002156 apply_autocmds(EVENT_REMOTEREPLY, winstr, str,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 TRUE, curbuf);
2158 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 }
2160 return 1;
2161 }
2162
2163 return 0;
2164 }
2165
2166 else if (msg == WM_ACTIVATE && wParam == WA_ACTIVE)
2167 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002168 // When the message window is activated (brought to the foreground),
2169 // this actually applies to the text window.
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002170# if !defined(FEAT_GUI) || defined(VIMDLL)
2171# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002172 if (!gui.in_use)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002173# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002174 GetConsoleHwnd(); // get value of s_hwnd
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002175# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 if (s_hwnd != 0)
2177 {
2178 SetForegroundWindow(s_hwnd);
2179 return 0;
2180 }
2181 }
2182
2183 return DefWindowProc(hwnd, msg, wParam, lParam);
2184}
2185
2186/*
2187 * Initialise the message handling process. This involves creating a window
2188 * to handle messages - the window will not be visible.
2189 */
2190 void
2191serverInitMessaging(void)
2192{
2193 WNDCLASS wndclass;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194
Bram Moolenaar0f873732019-12-05 20:28:46 +01002195 // Clean up on exit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196 atexit(CleanUpMessaging);
2197
Bram Moolenaar0f873732019-12-05 20:28:46 +01002198 // Register a window class - we only really care
2199 // about the window procedure
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 wndclass.style = 0;
2201 wndclass.lpfnWndProc = Messaging_WndProc;
2202 wndclass.cbClsExtra = 0;
2203 wndclass.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002204 wndclass.hInstance = g_hinst;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 wndclass.hIcon = NULL;
2206 wndclass.hCursor = NULL;
2207 wndclass.hbrBackground = NULL;
2208 wndclass.lpszMenuName = NULL;
2209 wndclass.lpszClassName = VIM_CLASSNAME;
2210 RegisterClass(&wndclass);
2211
Bram Moolenaar0f873732019-12-05 20:28:46 +01002212 // Create the message window. It will be hidden, so the details don't
2213 // matter. Don't use WS_OVERLAPPEDWINDOW, it will make a shortcut remove
2214 // focus from gvim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 message_window = CreateWindow(VIM_CLASSNAME, "",
2216 WS_POPUPWINDOW | WS_CAPTION,
2217 CW_USEDEFAULT, CW_USEDEFAULT,
2218 100, 100, NULL, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002219 g_hinst, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220}
2221
Bram Moolenaar0f873732019-12-05 20:28:46 +01002222// Used by serverSendToVim() to find an alternate server name.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002223static char_u *altname_buf_ptr = NULL;
2224
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225/*
2226 * Get the title of the window "hwnd", which is the Vim server name, in
2227 * "name[namelen]" and return the length.
2228 * Returns zero if window "hwnd" is not a Vim server.
2229 */
2230 static int
2231getVimServerName(HWND hwnd, char *name, int namelen)
2232{
2233 int len;
2234 char buffer[VIM_CLASSNAME_LEN + 1];
2235
Bram Moolenaar0f873732019-12-05 20:28:46 +01002236 // Ignore windows which aren't Vim message windows
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 len = GetClassName(hwnd, buffer, sizeof(buffer));
2238 if (len != VIM_CLASSNAME_LEN || STRCMP(buffer, VIM_CLASSNAME) != 0)
2239 return 0;
2240
Bram Moolenaar0f873732019-12-05 20:28:46 +01002241 // Get the title of the window
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 return GetWindowText(hwnd, name, namelen);
2243}
2244
2245 static BOOL CALLBACK
2246enumWindowsGetServer(HWND hwnd, LPARAM lparam)
2247{
2248 struct server_id *id = (struct server_id *)lparam;
2249 char server[MAX_PATH];
2250
Bram Moolenaar0f873732019-12-05 20:28:46 +01002251 // Get the title of the window
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 if (getVimServerName(hwnd, server, sizeof(server)) == 0)
2253 return TRUE;
2254
Bram Moolenaar0f873732019-12-05 20:28:46 +01002255 // If this is the server we're looking for, return its HWND
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 if (STRICMP(server, id->name) == 0)
2257 {
2258 id->hwnd = hwnd;
2259 return FALSE;
2260 }
2261
Bram Moolenaar0f873732019-12-05 20:28:46 +01002262 // If we are looking for an alternate server, remember this name.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002263 if (altname_buf_ptr != NULL
2264 && STRNICMP(server, id->name, STRLEN(id->name)) == 0
2265 && vim_isdigit(server[STRLEN(id->name)]))
2266 {
2267 STRCPY(altname_buf_ptr, server);
Bram Moolenaar0f873732019-12-05 20:28:46 +01002268 altname_buf_ptr = NULL; // don't use another name
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002269 }
2270
Bram Moolenaar0f873732019-12-05 20:28:46 +01002271 // Otherwise, keep looking
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 return TRUE;
2273}
2274
2275 static BOOL CALLBACK
2276enumWindowsGetNames(HWND hwnd, LPARAM lparam)
2277{
2278 garray_T *ga = (garray_T *)lparam;
2279 char server[MAX_PATH];
2280
Bram Moolenaar0f873732019-12-05 20:28:46 +01002281 // Get the title of the window
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 if (getVimServerName(hwnd, server, sizeof(server)) == 0)
2283 return TRUE;
2284
Bram Moolenaar0f873732019-12-05 20:28:46 +01002285 // Add the name to the list
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002286 ga_concat(ga, (char_u *)server);
2287 ga_concat(ga, (char_u *)"\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 return TRUE;
2289}
2290
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002291struct enum_windows_s
2292{
2293 WNDENUMPROC lpEnumFunc;
2294 LPARAM lParam;
2295};
2296
2297 static BOOL CALLBACK
2298enum_windows_child(HWND hwnd, LPARAM lParam)
2299{
2300 struct enum_windows_s *ew = (struct enum_windows_s *)lParam;
2301
2302 return (ew->lpEnumFunc)(hwnd, ew->lParam);
2303}
2304
2305 static BOOL CALLBACK
2306enum_windows_toplevel(HWND hwnd, LPARAM lParam)
2307{
2308 struct enum_windows_s *ew = (struct enum_windows_s *)lParam;
2309
Bram Moolenaar95ba5c32018-10-07 22:47:07 +02002310 if ((ew->lpEnumFunc)(hwnd, ew->lParam))
2311 return TRUE;
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002312 return EnumChildWindows(hwnd, enum_windows_child, lParam);
2313}
2314
Bram Moolenaar0f873732019-12-05 20:28:46 +01002315/*
2316 * Enumerate all windows including children.
2317 */
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002318 static BOOL
2319enum_windows(WNDENUMPROC lpEnumFunc, LPARAM lParam)
2320{
2321 struct enum_windows_s ew;
2322
2323 ew.lpEnumFunc = lpEnumFunc;
2324 ew.lParam = lParam;
2325 return EnumWindows(enum_windows_toplevel, (LPARAM)&ew);
2326}
2327
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 static HWND
2329findServer(char_u *name)
2330{
2331 struct server_id id;
2332
2333 id.name = name;
2334 id.hwnd = 0;
2335
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002336 enum_windows(enumWindowsGetServer, (LPARAM)(&id));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337
2338 return id.hwnd;
2339}
2340
2341 void
2342serverSetName(char_u *name)
2343{
2344 char_u *ok_name;
2345 HWND hwnd = 0;
2346 int i = 0;
2347 char_u *p;
2348
Bram Moolenaar0f873732019-12-05 20:28:46 +01002349 // Leave enough space for a 9-digit suffix to ensure uniqueness!
Bram Moolenaar964b3742019-05-24 18:54:09 +02002350 ok_name = alloc(STRLEN(name) + 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351
2352 STRCPY(ok_name, name);
2353 p = ok_name + STRLEN(name);
2354
2355 for (;;)
2356 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002357 // This is inefficient - we're doing an EnumWindows loop for each
2358 // possible name. It would be better to grab all names in one go,
2359 // and scan the list each time...
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 hwnd = findServer(ok_name);
2361 if (hwnd == 0)
2362 break;
2363
2364 ++i;
2365 if (i >= 1000)
2366 break;
2367
2368 sprintf((char *)p, "%d", i);
2369 }
2370
2371 if (hwnd != 0)
2372 vim_free(ok_name);
2373 else
2374 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002375 // Remember the name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 serverName = ok_name;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002377 need_maketitle = TRUE; // update Vim window title later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378
Bram Moolenaar0f873732019-12-05 20:28:46 +01002379 // Update the message window title
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002380 SetWindowText(message_window, (LPCSTR)ok_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002382# ifdef FEAT_EVAL
Bram Moolenaar0f873732019-12-05 20:28:46 +01002383 // Set the servername variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 set_vim_var_string(VV_SEND_SERVER, serverName, -1);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002385# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 }
2387}
2388
2389 char_u *
2390serverGetVimNames(void)
2391{
2392 garray_T ga;
2393
2394 ga_init2(&ga, 1, 100);
2395
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002396 enum_windows(enumWindowsGetNames, (LPARAM)(&ga));
Bram Moolenaar269ec652004-07-29 08:43:53 +00002397 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398
2399 return ga.ga_data;
2400}
2401
2402 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002403serverSendReply(
Bram Moolenaar0f873732019-12-05 20:28:46 +01002404 char_u *name, // Where to send.
2405 char_u *reply) // What to send.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406{
2407 HWND target;
2408 COPYDATASTRUCT data;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002409 long_u n = 0;
K.Takataf9f2a332022-06-17 20:05:40 +01002410 DWORD_PTR dwret = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411
Bram Moolenaar0f873732019-12-05 20:28:46 +01002412 // The "name" argument is a magic cookie obtained from expand("<client>").
2413 // It should be of the form 0xXXXXX - i.e. a C hex literal, which is the
2414 // value of the client's message window HWND.
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002415 sscanf((char *)name, SCANF_HEX_LONG_U, &n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 if (n == 0)
2417 return -1;
2418
2419 target = (HWND)n;
2420 if (!IsWindow(target))
2421 return -1;
2422
2423 data.dwData = COPYDATA_REPLY;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002424 data.cbData = (DWORD)STRLEN(reply) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 data.lpData = reply;
2426
K.Takataf9f2a332022-06-17 20:05:40 +01002427 if (serverSendEnc(target) < 0)
2428 return -1;
2429 if (SendMessageTimeout(target, WM_COPYDATA,
2430 (WPARAM)message_window, (LPARAM)&data,
2431 SMTO_ABORTIFHUNG, SENDMESSAGE_TIMEOUT, &dwret) == 0)
2432 return -1;
2433 return dwret ? 0 : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434}
2435
2436 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002437serverSendToVim(
Bram Moolenaar0f873732019-12-05 20:28:46 +01002438 char_u *name, // Where to send.
2439 char_u *cmd, // What to send.
2440 char_u **result, // Result of eval'ed expression
2441 void *ptarget, // HWND of server
2442 int asExpr, // Expression or keys?
2443 int timeout, // timeout in seconds or zero
2444 int silent) // don't complain about no server
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445{
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002446 HWND target;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 COPYDATASTRUCT data;
2448 char_u *retval = NULL;
2449 int retcode = 0;
K.Takataf9f2a332022-06-17 20:05:40 +01002450 DWORD_PTR dwret = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002451 char_u altname_buf[MAX_PATH];
2452
Bram Moolenaar0f873732019-12-05 20:28:46 +01002453 // Execute locally if no display or target is ourselves
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01002454 if (serverName != NULL && STRICMP(name, serverName) == 0)
2455 return sendToLocalVim(cmd, asExpr, result);
2456
Bram Moolenaar0f873732019-12-05 20:28:46 +01002457 // If the server name does not end in a digit then we look for an
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00002458 // alternate name. e.g. when "name" is GVIM then we may find GVIM2.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002459 if (STRLEN(name) > 1 && !vim_isdigit(name[STRLEN(name) - 1]))
2460 altname_buf_ptr = altname_buf;
2461 altname_buf[0] = NUL;
2462 target = findServer(name);
2463 altname_buf_ptr = NULL;
2464 if (target == 0 && altname_buf[0] != NUL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01002465 // Use another server name we found.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002466 target = findServer(altname_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467
2468 if (target == 0)
2469 {
2470 if (!silent)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002471 semsg(_(e_no_registered_server_named_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 return -1;
2473 }
2474
2475 if (ptarget)
2476 *(HWND *)ptarget = target;
2477
2478 data.dwData = asExpr ? COPYDATA_EXPR : COPYDATA_KEYS;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002479 data.cbData = (DWORD)STRLEN(cmd) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 data.lpData = cmd;
2481
K.Takataf9f2a332022-06-17 20:05:40 +01002482 if (serverSendEnc(target) < 0)
2483 return -1;
2484 if (SendMessageTimeout(target, WM_COPYDATA,
2485 (WPARAM)message_window, (LPARAM)&data,
2486 SMTO_ABORTIFHUNG, SENDMESSAGE_TIMEOUT, &dwret) == 0)
2487 return -1;
2488 if (dwret == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 return -1;
2490
2491 if (asExpr)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002492 retval = serverGetReply(target, &retcode, TRUE, TRUE, timeout);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493
2494 if (result == NULL)
2495 vim_free(retval);
2496 else
Bram Moolenaar0f873732019-12-05 20:28:46 +01002497 *result = retval; // Caller assumes responsibility for freeing
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498
2499 return retcode;
2500}
2501
2502/*
2503 * Bring the server to the foreground.
2504 */
2505 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002506serverForeground(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507{
2508 HWND target = findServer(name);
2509
2510 if (target != 0)
2511 SetForegroundWindow(target);
2512}
2513
Bram Moolenaar0f873732019-12-05 20:28:46 +01002514// Replies from server need to be stored until the client picks them up via
2515// remote_read(). So we maintain a list of server-id/reply pairs.
2516// Note that there could be multiple replies from one server pending if the
2517// client is slow picking them up.
2518// We just store the replies in a simple list. When we remove an entry, we
2519// move list entries down to fill the gap.
2520// The server ID is simply the HWND.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521typedef struct
2522{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002523 HWND server; // server window
2524 char_u *reply; // reply string
2525 int expr_result; // 0 for REPLY, 1 for RESULT 2 for error
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002526} reply_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527
2528static garray_T reply_list = {0, 0, sizeof(reply_T), 5, 0};
2529
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002530# define REPLY_ITEM(i) ((reply_T *)(reply_list.ga_data) + (i))
2531# define REPLY_COUNT (reply_list.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532
Bram Moolenaar0f873732019-12-05 20:28:46 +01002533// Flag which is used to wait for a reply
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534static int reply_received = 0;
2535
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002536/*
2537 * Store a reply. "reply" must be allocated memory (or NULL).
2538 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 static int
2540save_reply(HWND server, char_u *reply, int expr)
2541{
2542 reply_T *rep;
2543
2544 if (ga_grow(&reply_list, 1) == FAIL)
2545 return FAIL;
2546
2547 rep = REPLY_ITEM(REPLY_COUNT);
2548 rep->server = server;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002549 rep->reply = reply;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 rep->expr_result = expr;
2551 if (rep->reply == NULL)
2552 return FAIL;
2553
2554 ++REPLY_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 reply_received = 1;
2556 return OK;
2557}
2558
2559/*
2560 * Get a reply from server "server".
2561 * When "expr_res" is non NULL, get the result of an expression, otherwise a
2562 * server2client() message.
2563 * When non NULL, point to return code. 0 => OK, -1 => ERROR
2564 * If "remove" is TRUE, consume the message, the caller must free it then.
2565 * if "wait" is TRUE block until a message arrives (or the server exits).
2566 */
2567 char_u *
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002568serverGetReply(HWND server, int *expr_res, int remove, int wait, int timeout)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569{
2570 int i;
2571 char_u *reply;
2572 reply_T *rep;
Bram Moolenaar15e737f2017-03-18 21:22:47 +01002573 int did_process = FALSE;
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002574 time_t start;
2575 time_t now;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576
Bram Moolenaar0f873732019-12-05 20:28:46 +01002577 // When waiting, loop until the message waiting for is received.
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002578 time(&start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 for (;;)
2580 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002581 // Reset this here, in case a message arrives while we are going
2582 // through the already received messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 reply_received = 0;
2584
2585 for (i = 0; i < REPLY_COUNT; ++i)
2586 {
2587 rep = REPLY_ITEM(i);
2588 if (rep->server == server
2589 && ((rep->expr_result != 0) == (expr_res != NULL)))
2590 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002591 // Save the values we've found for later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 reply = rep->reply;
2593 if (expr_res != NULL)
2594 *expr_res = rep->expr_result == 1 ? 0 : -1;
2595
2596 if (remove)
2597 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002598 // Move the rest of the list down to fill the gap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 mch_memmove(rep, rep + 1,
2600 (REPLY_COUNT - i - 1) * sizeof(reply_T));
2601 --REPLY_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 }
2603
Bram Moolenaar0f873732019-12-05 20:28:46 +01002604 // Return the reply to the caller, who takes on responsibility
2605 // for freeing it if "remove" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 return reply;
2607 }
2608 }
2609
Bram Moolenaar0f873732019-12-05 20:28:46 +01002610 // If we got here, we didn't find a reply. Return immediately if the
2611 // "wait" parameter isn't set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 if (!wait)
Bram Moolenaar15e737f2017-03-18 21:22:47 +01002613 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002614 // Process pending messages once. Without this, looping on
2615 // remote_peek() would never get the reply.
Bram Moolenaar15e737f2017-03-18 21:22:47 +01002616 if (!did_process)
2617 {
2618 did_process = TRUE;
2619 serverProcessPendingMessages();
2620 continue;
2621 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 break;
Bram Moolenaar15e737f2017-03-18 21:22:47 +01002623 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624
Bram Moolenaar0f873732019-12-05 20:28:46 +01002625 // We need to wait for a reply. Enter a message loop until the
2626 // "reply_received" flag gets set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627
Bram Moolenaar0f873732019-12-05 20:28:46 +01002628 // Loop until we receive a reply
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 while (reply_received == 0)
2630 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002631# ifdef FEAT_TIMERS
Bram Moolenaar0f873732019-12-05 20:28:46 +01002632 // TODO: use the return value to decide how long to wait.
Bram Moolenaar42205552017-03-18 19:42:22 +01002633 check_due_timer();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002634# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002635 time(&now);
2636 if (timeout > 0 && (now - start) >= timeout)
2637 break;
2638
Bram Moolenaar0f873732019-12-05 20:28:46 +01002639 // Wait for a SendMessage() call to us. This could be the reply
2640 // we are waiting for. Use a timeout of a second, to catch the
2641 // situation that the server died unexpectedly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 MsgWaitForMultipleObjects(0, NULL, TRUE, 1000, QS_ALLINPUT);
2643
Bram Moolenaar0f873732019-12-05 20:28:46 +01002644 // If the server has died, give up
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 if (!IsWindow(server))
2646 return NULL;
2647
2648 serverProcessPendingMessages();
2649 }
2650 }
2651
2652 return NULL;
2653}
2654
2655/*
2656 * Process any messages in the Windows message queue.
2657 */
2658 void
2659serverProcessPendingMessages(void)
2660{
2661 MSG msg;
2662
K.Takatab7057bd2022-01-21 11:37:07 +00002663 while (PeekMessageW(&msg, NULL, 0, 0, PM_REMOVE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 {
2665 TranslateMessage(&msg);
K.Takatab7057bd2022-01-21 11:37:07 +00002666 DispatchMessageW(&msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 }
2668}
2669
Bram Moolenaar0f873732019-12-05 20:28:46 +01002670#endif // FEAT_CLIENTSERVER
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671
2672#if defined(FEAT_GUI) || (defined(FEAT_PRINTER) && !defined(FEAT_POSTSCRIPT)) \
2673 || defined(PROTO)
2674
2675struct charset_pair
2676{
2677 char *name;
2678 BYTE charset;
2679};
2680
2681static struct charset_pair
2682charset_pairs[] =
2683{
2684 {"ANSI", ANSI_CHARSET},
2685 {"CHINESEBIG5", CHINESEBIG5_CHARSET},
2686 {"DEFAULT", DEFAULT_CHARSET},
2687 {"HANGEUL", HANGEUL_CHARSET},
2688 {"OEM", OEM_CHARSET},
2689 {"SHIFTJIS", SHIFTJIS_CHARSET},
2690 {"SYMBOL", SYMBOL_CHARSET},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 {"ARABIC", ARABIC_CHARSET},
2692 {"BALTIC", BALTIC_CHARSET},
2693 {"EASTEUROPE", EASTEUROPE_CHARSET},
2694 {"GB2312", GB2312_CHARSET},
2695 {"GREEK", GREEK_CHARSET},
2696 {"HEBREW", HEBREW_CHARSET},
2697 {"JOHAB", JOHAB_CHARSET},
2698 {"MAC", MAC_CHARSET},
2699 {"RUSSIAN", RUSSIAN_CHARSET},
2700 {"THAI", THAI_CHARSET},
2701 {"TURKISH", TURKISH_CHARSET},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002702# ifdef VIETNAMESE_CHARSET
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 {"VIETNAMESE", VIETNAMESE_CHARSET},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002704# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 {NULL, 0}
2706};
2707
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002708struct quality_pair
2709{
2710 char *name;
2711 DWORD quality;
2712};
2713
2714static struct quality_pair
2715quality_pairs[] = {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002716# ifdef CLEARTYPE_QUALITY
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002717 {"CLEARTYPE", CLEARTYPE_QUALITY},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002718# endif
2719# ifdef ANTIALIASED_QUALITY
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002720 {"ANTIALIASED", ANTIALIASED_QUALITY},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002721# endif
2722# ifdef NONANTIALIASED_QUALITY
Bram Moolenaar73a733e2016-05-11 21:05:05 +02002723 {"NONANTIALIASED", NONANTIALIASED_QUALITY},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002724# endif
2725# ifdef PROOF_QUALITY
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002726 {"PROOF", PROOF_QUALITY},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002727# endif
2728# ifdef DRAFT_QUALITY
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002729 {"DRAFT", DRAFT_QUALITY},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002730# endif
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002731 {"DEFAULT", DEFAULT_QUALITY},
2732 {NULL, 0}
2733};
2734
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735/*
2736 * Convert a charset ID to a name.
2737 * Return NULL when not recognized.
2738 */
2739 char *
2740charset_id2name(int id)
2741{
2742 struct charset_pair *cp;
2743
2744 for (cp = charset_pairs; cp->name != NULL; ++cp)
2745 if ((BYTE)id == cp->charset)
2746 break;
2747 return cp->name;
2748}
2749
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002750/*
2751 * Convert a quality ID to a name.
2752 * Return NULL when not recognized.
2753 */
2754 char *
2755quality_id2name(DWORD id)
2756{
2757 struct quality_pair *qp;
2758
2759 for (qp = quality_pairs; qp->name != NULL; ++qp)
2760 if (id == qp->quality)
2761 break;
2762 return qp->name;
2763}
2764
Ken Takatad8cb1dd2024-01-12 18:09:43 +01002765// The default font height in 100% scaling (96dpi).
2766// (-12 in 96dpi equates to roughly 9pt)
2767#define DEFAULT_FONT_HEIGHT (-12)
2768
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002769static const LOGFONTW s_lfDefault =
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770{
Ken Takatad8cb1dd2024-01-12 18:09:43 +01002771 DEFAULT_FONT_HEIGHT,
2772 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, DEFAULT_CHARSET,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
2774 PROOF_QUALITY, FIXED_PITCH | FF_DONTCARE,
Ken Takatad8cb1dd2024-01-12 18:09:43 +01002775 L"" // Default font name will be set later based on current language.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776};
2777
Ken Takatad8cb1dd2024-01-12 18:09:43 +01002778// This will be initialized when set_default_logfont() is called first time.
2779// The value will be based on the system DPI.
2780int current_font_height = 0; // also used in gui_w32.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781
Bram Moolenaar0f873732019-12-05 20:28:46 +01002782/*
2783 * Convert a string representing a point size into pixels. The string should
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 * be a positive decimal number, with an optional decimal point (eg, "12", or
2785 * "10.5"). The pixel value is returned, and a pointer to the next unconverted
2786 * character is stored in *end. The flag "vertical" says whether this
2787 * calculation is for a vertical (height) size or a horizontal (width) one.
2788 */
2789 static int
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002790points_to_pixels(WCHAR *str, WCHAR **end, int vertical, long_i pprinter_dc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791{
2792 int pixels;
2793 int points = 0;
2794 int divisor = 0;
2795 HWND hwnd = (HWND)0;
2796 HDC hdc;
2797 HDC printer_dc = (HDC)pprinter_dc;
2798
2799 while (*str != NUL)
2800 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002801 if (*str == L'.' && divisor == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002803 // Start keeping a divisor, for later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 divisor = 1;
2805 }
2806 else
2807 {
2808 if (!VIM_ISDIGIT(*str))
2809 break;
2810
2811 points *= 10;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002812 points += *str - L'0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 divisor *= 10;
2814 }
2815 ++str;
2816 }
2817
2818 if (divisor == 0)
2819 divisor = 1;
2820
2821 if (printer_dc == NULL)
2822 {
2823 hwnd = GetDesktopWindow();
2824 hdc = GetWindowDC(hwnd);
2825 }
2826 else
2827 hdc = printer_dc;
2828
2829 pixels = MulDiv(points,
2830 GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX),
2831 72 * divisor);
2832
2833 if (printer_dc == NULL)
2834 ReleaseDC(hwnd, hdc);
2835
2836 *end = str;
2837 return pixels;
2838}
2839
Yee Cheng Chin290b8872023-10-05 20:54:21 +02002840/*
2841 * Convert pixel into point size. This is a reverse of points_to_pixels.
2842 */
2843 static double
2844pixels_to_points(int pixels, int vertical, long_i pprinter_dc)
2845{
2846 double points = 0;
2847 HWND hwnd = (HWND)0;
2848 HDC hdc;
2849 HDC printer_dc = (HDC)pprinter_dc;
2850
2851 if (printer_dc == NULL)
2852 {
2853 hwnd = GetDesktopWindow();
2854 hdc = GetWindowDC(hwnd);
2855 }
2856 else
2857 hdc = printer_dc;
2858
2859 points = pixels * 72.0 / GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX);
2860 if (printer_dc == NULL)
2861 ReleaseDC(hwnd, hdc);
2862
2863 return points;
2864}
2865
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 static int CALLBACK
2867font_enumproc(
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002868 ENUMLOGFONTW *elf,
2869 NEWTEXTMETRICW *ntm UNUSED,
2870 DWORD type UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 LPARAM lparam)
2872{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002873 // Return value:
2874 // 0 = terminate now (monospace & ANSI)
2875 // 1 = continue, still no luck...
2876 // 2 = continue, but we have an acceptable LOGFONTW
2877 // (monospace, not ANSI)
2878 // We use these values, as EnumFontFamilies returns 1 if the
2879 // callback function is never called. So, we check the return as
2880 // 0 = perfect, 2 = OK, 1 = no good...
2881 // It's not pretty, but it works!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002883 LOGFONTW *lf = (LOGFONTW *)(lparam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002885# ifndef FEAT_PROPORTIONAL_FONTS
Bram Moolenaar0f873732019-12-05 20:28:46 +01002886 // Ignore non-monospace fonts without further ado
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 if ((ntm->tmPitchAndFamily & 1) != 0)
2888 return 1;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002889# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890
Bram Moolenaar0f873732019-12-05 20:28:46 +01002891 // Remember this LOGFONTW as a "possible"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892 *lf = elf->elfLogFont;
2893
Bram Moolenaar0f873732019-12-05 20:28:46 +01002894 // Terminate the scan as soon as we find an ANSI font
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 if (lf->lfCharSet == ANSI_CHARSET
2896 || lf->lfCharSet == OEM_CHARSET
2897 || lf->lfCharSet == DEFAULT_CHARSET)
2898 return 0;
2899
Bram Moolenaar0f873732019-12-05 20:28:46 +01002900 // Continue the scan - we have a non-ANSI font
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 return 2;
2902}
2903
2904 static int
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002905init_logfont(LOGFONTW *lf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906{
2907 int n;
2908 HWND hwnd = GetDesktopWindow();
2909 HDC hdc = GetWindowDC(hwnd);
2910
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002911 n = EnumFontFamiliesW(hdc,
2912 lf->lfFaceName,
2913 (FONTENUMPROCW)font_enumproc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 (LPARAM)lf);
2915
2916 ReleaseDC(hwnd, hdc);
2917
Bram Moolenaar0f873732019-12-05 20:28:46 +01002918 // If we couldn't find a usable font, return failure
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 if (n == 1)
2920 return FAIL;
2921
Bram Moolenaar0f873732019-12-05 20:28:46 +01002922 // Tidy up the rest of the LOGFONTW structure. We set to a basic
2923 // font - get_logfont() sets bold, italic, etc based on the user's
2924 // input.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925 lf->lfHeight = current_font_height;
2926 lf->lfWidth = 0;
2927 lf->lfItalic = FALSE;
2928 lf->lfUnderline = FALSE;
2929 lf->lfStrikeOut = FALSE;
2930 lf->lfWeight = FW_NORMAL;
2931
Bram Moolenaar0f873732019-12-05 20:28:46 +01002932 // Return success
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 return OK;
2934}
2935
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002936/*
Yee Cheng Chin290b8872023-10-05 20:54:21 +02002937 * Call back for EnumFontFamiliesW in expand_font_enumproc.
2938 *
2939 */
2940 static int CALLBACK
2941expand_font_enumproc(
2942 ENUMLOGFONTW *elf,
2943 NEWTEXTMETRICW *ntm UNUSED,
2944 DWORD type UNUSED,
2945 LPARAM lparam)
2946{
2947 LOGFONTW *lf = (LOGFONTW*)elf;
2948
2949# ifndef FEAT_PROPORTIONAL_FONTS
2950 // Ignore non-monospace fonts without further ado
2951 if ((ntm->tmPitchAndFamily & 1) != 0)
2952 return 1;
2953# endif
2954
2955 // Filter only on ANSI. Otherwise will see a lot of random fonts that we
2956 // usually don't want.
2957 if (lf->lfCharSet != ANSI_CHARSET)
2958 return 1;
2959
2960 int (*add_match)(char_u *) = (int (*)(char_u *))lparam;
2961
2962 WCHAR *faceNameW = lf->lfFaceName;
2963 char_u *faceName = utf16_to_enc(faceNameW, NULL);
2964 if (!faceName)
2965 return 0;
2966
2967 add_match(faceName);
2968 vim_free(faceName);
2969
2970 return 1;
2971}
2972
2973/*
2974 * Cmdline expansion for setting 'guifont'. Will enumerate through all
2975 * monospace fonts for completion. If used after ':', will expand to possible
2976 * font configuration options like font sizes.
2977 *
2978 * This function has "gui" in its name because in some platforms (GTK) font
2979 * handling is done by the GUI code, whereas in Windows it's part of the
2980 * platform code.
2981 */
2982 void
2983gui_mch_expand_font(optexpand_T *args, void *param UNUSED, int (*add_match)(char_u *val))
2984{
2985 expand_T *xp = args->oe_xp;
2986 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
2987 {
2988 char buf[30];
2989
2990 // Always fill in with the current font size as first option for
2991 // convenience. We simply round to the closest integer for simplicity.
2992 int font_height = (int)round(
2993 pixels_to_points(-current_font_height, TRUE, (long_i)NULL));
2994 vim_snprintf(buf, ARRAY_LENGTH(buf), "h%d", font_height);
2995 add_match((char_u *)buf);
2996
2997 // Note: Keep this in sync with get_logfont(). Don't include 'c' and
2998 // 'q' as we fill in all the values below.
2999 static char *(p_gfn_win_opt_values[]) = {
3000 "h" , "w" , "W" , "b" , "i" , "u" , "s"};
3001 for (size_t i = 0; i < ARRAY_LENGTH(p_gfn_win_opt_values); i++)
3002 add_match((char_u *)p_gfn_win_opt_values[i]);
3003
3004 struct charset_pair *cp;
3005 for (cp = charset_pairs; cp->name != NULL; ++cp)
3006 {
3007 vim_snprintf(buf, ARRAY_LENGTH(buf), "c%s", cp->name);
3008 add_match((char_u *)buf);
3009 }
3010 struct quality_pair *qp;
3011 for (qp = quality_pairs; qp->name != NULL; ++qp)
3012 {
3013 vim_snprintf(buf, ARRAY_LENGTH(buf), "q%s", qp->name);
3014 add_match((char_u *)buf);
3015 }
3016 return;
3017 }
3018
3019 HWND hwnd = GetDesktopWindow();
3020 HDC hdc = GetWindowDC(hwnd);
3021
3022 EnumFontFamiliesW(hdc,
3023 NULL,
3024 (FONTENUMPROCW)expand_font_enumproc,
3025 (LPARAM)add_match);
3026
3027 ReleaseDC(hwnd, hdc);
3028}
3029
3030/*
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003031 * Compare a UTF-16 string and an ASCII string literally.
3032 * Only works all the code points are inside ASCII range.
3033 */
3034 static int
3035utf16ascncmp(const WCHAR *w, const char *p, size_t n)
3036{
3037 size_t i;
3038
3039 for (i = 0; i < n; i++)
3040 {
3041 if (w[i] == 0 || w[i] != p[i])
3042 return w[i] - p[i];
3043 }
3044 return 0;
3045}
3046
3047/*
Ken Takatad8cb1dd2024-01-12 18:09:43 +01003048 * Equivalent of GetDpiForSystem().
3049 */
3050 UINT WINAPI
3051vimGetDpiForSystem(void)
3052{
3053 HWND hwnd = GetDesktopWindow();
3054 HDC hdc = GetWindowDC(hwnd);
3055 UINT dpi = GetDeviceCaps(hdc, LOGPIXELSY);
3056 ReleaseDC(hwnd, hdc);
3057 return dpi;
3058}
3059
3060/*
3061 * Set default logfont based on current language.
3062 */
3063 static void
3064set_default_logfont(LOGFONTW *lf)
3065{
3066 // Default font name for current language on MS-Windows.
3067 // If not translated, falls back to "Consolas".
3068 // This must be a fixed-pitch font.
3069 const char *defaultfontname = N_("DefaultFontNameForWindows");
3070 char *fontname = _(defaultfontname);
3071
3072 if (strcmp(fontname, defaultfontname) == 0)
3073 fontname = "Consolas";
3074
3075 *lf = s_lfDefault;
3076 lf->lfHeight = DEFAULT_FONT_HEIGHT * (int)vimGetDpiForSystem() / 96;
3077 if (current_font_height == 0)
3078 current_font_height = lf->lfHeight;
3079
3080 WCHAR *wfontname = enc_to_utf16((char_u*)fontname, NULL);
3081 if (wfontname != NULL)
3082 {
3083 wcscpy_s(lf->lfFaceName, LF_FACESIZE, wfontname);
3084 vim_free(wfontname);
3085 }
3086}
3087
3088/*
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003089 * Get font info from "name" into logfont "lf".
3090 * Return OK for a valid name, FAIL otherwise.
3091 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 int
3093get_logfont(
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003094 LOGFONTW *lf,
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003095 char_u *name,
3096 HDC printer_dc,
3097 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003099 WCHAR *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 int i;
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003101 int ret = FAIL;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003102 static LOGFONTW *lastlf = NULL;
3103 WCHAR *wname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104
Ken Takatad8cb1dd2024-01-12 18:09:43 +01003105 set_default_logfont(lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 if (name == NULL)
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003107 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003109 wname = enc_to_utf16(name, NULL);
3110 if (wname == NULL)
3111 return FAIL;
3112
3113 if (wcscmp(wname, L"*") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003115# if defined(FEAT_GUI_MSWIN)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003116 CHOOSEFONTW cf;
Bram Moolenaar0f873732019-12-05 20:28:46 +01003117 // if name is "*", bring up std font dialog:
Bram Moolenaara80faa82020-04-12 19:37:17 +02003118 CLEAR_FIELD(cf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 cf.lStructSize = sizeof(cf);
3120 cf.hwndOwner = s_hwnd;
3121 cf.Flags = CF_SCREENFONTS | CF_FIXEDPITCHONLY | CF_INITTOLOGFONTSTRUCT;
3122 if (lastlf != NULL)
3123 *lf = *lastlf;
3124 cf.lpLogFont = lf;
3125 cf.nFontType = 0 ; //REGULAR_FONTTYPE;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003126 if (ChooseFontW(&cf))
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003127 ret = OK;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003128# endif
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003129 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 }
3131
3132 /*
3133 * Split name up, it could be <name>:h<height>:w<width> etc.
3134 */
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003135 for (p = wname; *p && *p != L':'; p++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003137 if (p - wname + 1 >= LF_FACESIZE)
Bram Moolenaar0f873732019-12-05 20:28:46 +01003138 goto theend; // Name too long
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003139 lf->lfFaceName[p - wname] = *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 }
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003141 if (p != wname)
3142 lf->lfFaceName[p - wname] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143
Bram Moolenaar0f873732019-12-05 20:28:46 +01003144 // First set defaults
Ken Takatad8cb1dd2024-01-12 18:09:43 +01003145 lf->lfHeight = DEFAULT_FONT_HEIGHT * (int)vimGetDpiForSystem() / 96;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 lf->lfWidth = 0;
3147 lf->lfWeight = FW_NORMAL;
3148 lf->lfItalic = FALSE;
3149 lf->lfUnderline = FALSE;
3150 lf->lfStrikeOut = FALSE;
3151
3152 /*
3153 * If the font can't be found, try replacing '_' by ' '.
3154 */
3155 if (init_logfont(lf) == FAIL)
3156 {
3157 int did_replace = FALSE;
3158
3159 for (i = 0; lf->lfFaceName[i]; ++i)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003160 if (lf->lfFaceName[i] == L'_')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003162 lf->lfFaceName[i] = L' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 did_replace = TRUE;
3164 }
3165 if (!did_replace || init_logfont(lf) == FAIL)
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003166 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 }
3168
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003169 while (*p == L':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 p++;
3171
Bram Moolenaar0f873732019-12-05 20:28:46 +01003172 // Set the values found after ':'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 while (*p)
3174 {
3175 switch (*p++)
3176 {
Yee Cheng Chin290b8872023-10-05 20:54:21 +02003177 // Note: Keep this in sync with gui_mch_expand_font().
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003178 case L'h':
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003179 lf->lfHeight = - points_to_pixels(p, &p, TRUE, (long_i)printer_dc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003181 case L'w':
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003182 lf->lfWidth = points_to_pixels(p, &p, FALSE, (long_i)printer_dc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 break;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003184 case L'W':
3185 lf->lfWeight = wcstol(p, &p, 10);
3186 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003187 case L'b':
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 lf->lfWeight = FW_BOLD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003190 case L'i':
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 lf->lfItalic = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003193 case L'u':
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 lf->lfUnderline = TRUE;
3195 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003196 case L's':
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197 lf->lfStrikeOut = TRUE;
3198 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003199 case L'c':
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 {
3201 struct charset_pair *cp;
3202
3203 for (cp = charset_pairs; cp->name != NULL; ++cp)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003204 if (utf16ascncmp(p, cp->name, strlen(cp->name)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 {
3206 lf->lfCharSet = cp->charset;
3207 p += strlen(cp->name);
3208 break;
3209 }
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003210 if (cp->name == NULL && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003212 char_u *s = utf16_to_enc(p, NULL);
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00003213
3214 semsg(_(e_illegal_str_name_str_in_font_name_str),
3215 "charset", s, name);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003216 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 break;
3218 }
3219 break;
3220 }
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003221 case L'q':
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003222 {
3223 struct quality_pair *qp;
3224
3225 for (qp = quality_pairs; qp->name != NULL; ++qp)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003226 if (utf16ascncmp(p, qp->name, strlen(qp->name)) == 0)
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003227 {
3228 lf->lfQuality = qp->quality;
3229 p += strlen(qp->name);
3230 break;
3231 }
3232 if (qp->name == NULL && verbose)
3233 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003234 char_u *s = utf16_to_enc(p, NULL);
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00003235 semsg(_(e_illegal_str_name_str_in_font_name_str),
3236 "quality", s, name);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003237 vim_free(s);
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003238 break;
3239 }
3240 break;
3241 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 default:
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003243 if (verbose)
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00003244 semsg(_(e_illegal_char_nr_in_font_name_str), p[-1], name);
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003245 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 }
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003247 while (*p == L':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 p++;
3249 }
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003250 ret = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252theend:
Bram Moolenaar0f873732019-12-05 20:28:46 +01003253 // ron: init lastlf
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003254 if (ret == OK && printer_dc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 {
3256 vim_free(lastlf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003257 lastlf = ALLOC_ONE(LOGFONTW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 if (lastlf != NULL)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003259 mch_memmove(lastlf, lf, sizeof(LOGFONTW));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260 }
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003261 vim_free(wname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003263 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264}
3265
Bram Moolenaar0f873732019-12-05 20:28:46 +01003266#endif // defined(FEAT_GUI) || defined(FEAT_PRINTER)
Bram Moolenaarf12d9832016-01-29 21:11:25 +01003267
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003268#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaarf12d9832016-01-29 21:11:25 +01003269/*
3270 * Initialize the Winsock dll.
3271 */
3272 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003273channel_init_winsock(void)
Bram Moolenaarf12d9832016-01-29 21:11:25 +01003274{
3275 WSADATA wsaData;
3276 int wsaerr;
3277
3278 if (WSInitialized)
3279 return;
3280
3281 wsaerr = WSAStartup(MAKEWORD(2, 2), &wsaData);
3282 if (wsaerr == 0)
3283 WSInitialized = TRUE;
3284}
3285#endif