blob: 9d0e1a08a4d497763569d915657f036f012f8671 [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>
K.Takata56f587b2024-06-19 19:56:03 +020021
22// cproto fails on missing include files
Bram Moolenaar82881492012-11-20 16:53:39 +010023#ifndef PROTO
24# include <process.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000025# include <direct.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000026
Bram Moolenaar651fca82021-11-29 20:39:38 +000027# if !defined(FEAT_GUI_MSWIN)
Bram Moolenaar82881492012-11-20 16:53:39 +010028# include <shellapi.h>
29# endif
30
31# if defined(FEAT_PRINTER) && !defined(FEAT_POSTSCRIPT)
32# include <dlgs.h>
Bram Moolenaarcea912a2016-10-12 14:20:24 +020033# include <winspool.h>
Bram Moolenaar82881492012-11-20 16:53:39 +010034# include <commdlg.h>
Bram Moolenaarb04a98f2016-12-01 20:32:29 +010035# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +010036#endif // PROTO
Bram Moolenaar071d4272004-06-13 20:20:40 +000037
Bram Moolenaar071d4272004-06-13 20:20:40 +000038/*
39 * When generating prototypes for Win32 on Unix, these lines make the syntax
40 * errors disappear. They do not need to be correct.
41 */
42#ifdef PROTO
Bram Moolenaar912bc4a2019-12-01 18:58:11 +010043# define WINAPI
44# define WINBASEAPI
Bram Moolenaar071d4272004-06-13 20:20:40 +000045typedef int BOOL;
46typedef int CALLBACK;
47typedef int COLORREF;
48typedef int CONSOLE_CURSOR_INFO;
49typedef int COORD;
50typedef int DWORD;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +010051typedef int ENUMLOGFONTW;
Bram Moolenaar071d4272004-06-13 20:20:40 +000052typedef int HANDLE;
53typedef int HDC;
54typedef int HFONT;
55typedef int HICON;
56typedef int HWND;
57typedef int INPUT_RECORD;
Bram Moolenaard21e5bd2022-06-27 22:52:43 +010058typedef int INT_PTR;
Bram Moolenaar071d4272004-06-13 20:20:40 +000059typedef int KEY_EVENT_RECORD;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +010060typedef int LOGFONTW;
Bram Moolenaar071d4272004-06-13 20:20:40 +000061typedef int LPARAM;
62typedef int LPBOOL;
63typedef int LPCSTR;
64typedef int LPCWSTR;
Bram Moolenaarb9cdb372019-04-17 18:24:35 +020065typedef int LPDWORD;
Bram Moolenaar071d4272004-06-13 20:20:40 +000066typedef int LPSTR;
67typedef int LPTSTR;
Bram Moolenaarb9cdb372019-04-17 18:24:35 +020068typedef int LPVOID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000069typedef int LPWSTR;
70typedef int LRESULT;
71typedef int MOUSE_EVENT_RECORD;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +010072typedef int NEWTEXTMETRICW;
Bram Moolenaar071d4272004-06-13 20:20:40 +000073typedef int PACL;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +020074typedef int PRINTDLGW;
Bram Moolenaar071d4272004-06-13 20:20:40 +000075typedef int PSECURITY_DESCRIPTOR;
76typedef int PSID;
77typedef int SECURITY_INFORMATION;
78typedef int SHORT;
79typedef int SMALL_RECT;
80typedef int TEXTMETRIC;
81typedef int UINT;
82typedef int WCHAR;
Bram Moolenaarc447d8d2018-12-18 21:56:28 +010083typedef int WNDENUMPROC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000084typedef int WORD;
85typedef int WPARAM;
86typedef void VOID;
87#endif
88
Bram Moolenaar0f873732019-12-05 20:28:46 +010089// Record all output and all keyboard & mouse input
90// #define MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +000091
92#ifdef MCH_WRITE_DUMP
93FILE* fdDump = NULL;
94#endif
95
Bram Moolenaarafde13b2019-04-28 19:46:49 +020096#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000097extern char g_szOrigTitle[];
98#endif
99
100#ifdef FEAT_GUI
101extern HWND s_hwnd;
102#else
Bram Moolenaar0f873732019-12-05 20:28:46 +0100103static HWND s_hwnd = 0; // console window handle, set by GetConsoleHwnd()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104#endif
105
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100106#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar0f873732019-12-05 20:28:46 +0100107int WSInitialized = FALSE; // WinSock is initialized
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100108#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000109
Bram Moolenaar071d4272004-06-13 20:20:40 +0000110
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200111#ifndef PROTO
112/*
113 * Save the instance handle of the exe/dll.
114 */
115 void
116SaveInst(HINSTANCE hInst)
117{
118 g_hinst = hInst;
119}
120#endif
121
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122#if defined(FEAT_GUI_MSWIN) || defined(PROTO)
123/*
124 * GUI version of mch_exit().
125 * Shut down and exit with status `r'
126 * Careful: mch_exit() may be called before mch_init()!
127 */
128 void
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200129mch_exit_g(int r)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130{
Bram Moolenaar955f1982017-02-05 15:10:51 +0100131 exiting = TRUE;
132
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133 display_errors();
134
Bram Moolenaar0f873732019-12-05 20:28:46 +0100135 ml_close_all(TRUE); // remove all memfiles
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136
137# ifdef FEAT_OLE
138 UninitOLE();
139# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100140# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141 if (WSInitialized)
142 {
143 WSInitialized = FALSE;
144 WSACleanup();
145 }
146# endif
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100147# ifdef DYNAMIC_GETTEXT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148 dyn_libintl_end();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100149# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150
151 if (gui.in_use)
152 gui_exit(r);
Bram Moolenaar85c79d32007-02-20 01:59:20 +0000153
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100154# ifdef EXITFREE
Bram Moolenaar85c79d32007-02-20 01:59:20 +0000155 free_all_mem();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100156# endif
Bram Moolenaar85c79d32007-02-20 01:59:20 +0000157
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158 exit(r);
159}
160
Bram Moolenaar0f873732019-12-05 20:28:46 +0100161#endif // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162
163
164/*
165 * Init the tables for toupper() and tolower().
166 */
167 void
168mch_early_init(void)
169{
170 int i;
171
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172 PlatformId();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173
Bram Moolenaar0f873732019-12-05 20:28:46 +0100174 // Init the tables for toupper() and tolower()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175 for (i = 0; i < 256; ++i)
176 toupper_tab[i] = tolower_tab[i] = i;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100177 CharUpperBuff((LPSTR)toupper_tab, 256);
178 CharLowerBuff((LPSTR)tolower_tab, 256);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179}
180
181
182/*
183 * Return TRUE if the input comes from a terminal, FALSE otherwise.
184 */
185 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100186mch_input_isatty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187{
188#ifdef FEAT_GUI_MSWIN
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200189# ifdef VIMDLL
190 if (gui.in_use)
191# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +0100192 return TRUE; // GUI always has a tty
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200193#endif
194#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195 if (isatty(read_cmd_fd))
196 return TRUE;
197 return FALSE;
198#endif
199}
200
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201/*
202 * mch_settitle(): set titlebar of our window
203 */
204 void
205mch_settitle(
206 char_u *title,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +0200207 char_u *icon UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208{
Bram Moolenaar651fca82021-11-29 20:39:38 +0000209#ifdef FEAT_GUI_MSWIN
210# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200211 if (gui.in_use)
Bram Moolenaar651fca82021-11-29 20:39:38 +0000212# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200213 {
214 gui_mch_settitle(title, icon);
215 return;
216 }
Bram Moolenaar651fca82021-11-29 20:39:38 +0000217#endif
218#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219 if (title != NULL)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000220 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200221 WCHAR *wp = enc_to_utf16(title, NULL);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000222
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200223 if (wp == NULL)
224 return;
225
226 SetConsoleTitleW(wp);
227 vim_free(wp);
228 return;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000229 }
Bram Moolenaar651fca82021-11-29 20:39:38 +0000230#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231}
232
233
234/*
235 * Restore the window/icon title.
236 * which is one of:
Bram Moolenaar40385db2018-08-07 22:31:44 +0200237 * SAVE_RESTORE_TITLE: Just restore title
238 * SAVE_RESTORE_ICON: Just restore icon (which we don't have)
239 * SAVE_RESTORE_BOTH: Restore title and icon (which we don't have)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240 */
241 void
Bram Moolenaar1266d672017-02-01 13:43:36 +0100242mch_restore_title(int which UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243{
Bram Moolenaar651fca82021-11-29 20:39:38 +0000244#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
245# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200246 if (!gui.in_use)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100247# endif
Bram Moolenaar651fca82021-11-29 20:39:38 +0000248 SetConsoleTitle(g_szOrigTitle);
249#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000250}
251
252
253/*
254 * Return TRUE if we can restore the title (we can)
255 */
256 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100257mch_can_restore_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258{
259 return TRUE;
260}
261
262
263/*
264 * Return TRUE if we can restore the icon title (we can't)
265 */
266 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100267mch_can_restore_icon(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268{
269 return FALSE;
270}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000271
272
273/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000274 * Get absolute file name into buffer "buf" of length "len" bytes,
275 * turning all '/'s into '\\'s and getting the correct case of each component
276 * of the file name. Append a (back)slash to a directory name.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277 * When 'shellslash' set do it the other way around.
278 * Return OK or FAIL.
279 */
280 int
281mch_FullName(
282 char_u *fname,
283 char_u *buf,
284 int len,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100285 int force UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000286{
287 int nResult = FAIL;
Bram Moolenaareae1b912019-05-09 15:12:55 +0200288 WCHAR *wname;
289 WCHAR wbuf[MAX_PATH];
290 char_u *cname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291
Bram Moolenaareae1b912019-05-09 15:12:55 +0200292 wname = enc_to_utf16(fname, NULL);
293 if (wname != NULL && _wfullpath(wbuf, wname, MAX_PATH) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000294 {
Bram Moolenaareae1b912019-05-09 15:12:55 +0200295 cname = utf16_to_enc((short_u *)wbuf, NULL);
296 if (cname != NULL)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000297 {
Bram Moolenaareae1b912019-05-09 15:12:55 +0200298 vim_strncpy(buf, cname, len - 1);
299 nResult = OK;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000300 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301 }
Bram Moolenaareae1b912019-05-09 15:12:55 +0200302 vim_free(wname);
303 vim_free(cname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304
305#ifdef USE_FNAME_CASE
306 fname_case(buf, len);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000307#else
308 slash_adjust(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309#endif
310
311 return nResult;
312}
313
314
315/*
316 * Return TRUE if "fname" does not depend on the current directory.
317 */
318 int
319mch_isFullName(char_u *fname)
320{
Bram Moolenaar0ea74212020-12-11 20:10:50 +0100321 // A name like "d:/foo" and "//server/share" is absolute. "d:foo" is not.
322 // Another way to check is to use mch_FullName() and see if the result is
323 // the same as the name or mch_FullName() fails. However, this has quite a
324 // bit of overhead, so let's not do that.
Yegappan Lakshmanan6df0f272021-12-16 13:06:10 +0000325 if (*fname == NUL)
Yegappan Lakshmanan5a664fe2021-12-29 18:16:21 +0000326 return FALSE;
Bram Moolenaar0ea74212020-12-11 20:10:50 +0100327 return ((ASCII_ISALPHA(fname[0]) && fname[1] == ':'
328 && (fname[2] == '/' || fname[2] == '\\'))
329 || (fname[0] == fname[1] && (fname[0] == '/' || fname[0] == '\\')));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330}
331
332/*
333 * Replace all slashes by backslashes.
334 * This used to be the other way around, but MS-DOS sometimes has problems
335 * with slashes (e.g. in a command name). We can't have mixed slashes and
336 * backslashes, because comparing file names will not work correctly. The
337 * commands that use a file name should try to avoid the need to type a
338 * backslash twice.
339 * When 'shellslash' set do it the other way around.
Bram Moolenaarb4f6a462015-10-13 19:43:17 +0200340 * When the path looks like a URL leave it unmodified.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341 */
342 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100343slash_adjust(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344{
Bram Moolenaarb4f6a462015-10-13 19:43:17 +0200345 if (path_with_url(p))
346 return;
Bram Moolenaar39d21e32017-08-05 23:09:31 +0200347
348 if (*p == '`')
349 {
Bram Moolenaar116a0f82017-08-07 21:17:57 +0200350 size_t len = STRLEN(p);
351
Bram Moolenaar0f873732019-12-05 20:28:46 +0100352 // don't replace backslash in backtick quoted strings
Bram Moolenaar39d21e32017-08-05 23:09:31 +0200353 if (len > 2 && *(p + len - 1) == '`')
354 return;
355 }
356
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +0000357 while (*p)
358 {
359 if (*p == psepcN)
360 *p = psepc;
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100361 MB_PTR_ADV(p);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +0000362 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363}
364
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200365 static int
LemonBoy40fd7e62022-05-05 20:18:16 +0100366read_reparse_point(const WCHAR *name, char_u *buf, DWORD *buf_len)
367{
368 HANDLE h;
369 BOOL ok;
370
371 h = CreateFileW(name, FILE_READ_ATTRIBUTES,
372 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
373 OPEN_EXISTING,
374 FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT,
375 NULL);
376 if (h == INVALID_HANDLE_VALUE)
377 return FAIL;
378
379 ok = DeviceIoControl(h, FSCTL_GET_REPARSE_POINT, NULL, 0, buf, *buf_len,
380 buf_len, NULL);
381 CloseHandle(h);
382
383 return ok ? OK : FAIL;
384}
385
LemonBoy40fd7e62022-05-05 20:18:16 +0100386 char_u *
387resolve_appexeclink(char_u *fname)
388{
Bram Moolenaar9f1983d2022-05-12 20:35:35 +0100389 DWORD attr = 0;
390 int idx;
391 WCHAR *p, *end, *wname;
LemonBoy40fd7e62022-05-05 20:18:16 +0100392 // The buffer size is arbitrarily chosen to be "big enough" (TM), the
393 // ceiling should be around 16k.
Bram Moolenaar9f1983d2022-05-12 20:35:35 +0100394 char_u buf[4096];
395 DWORD buf_len = sizeof(buf);
LemonBoy40fd7e62022-05-05 20:18:16 +0100396 REPARSE_DATA_BUFFER *rb = (REPARSE_DATA_BUFFER *)buf;
397
398 wname = enc_to_utf16(fname, NULL);
399 if (wname == NULL)
400 return NULL;
401
402 attr = GetFileAttributesW(wname);
403 if (attr == INVALID_FILE_ATTRIBUTES ||
404 (attr & FILE_ATTRIBUTE_REPARSE_POINT) == 0)
405 {
406 vim_free(wname);
407 return NULL;
408 }
409
410 // The applinks are similar to symlinks but with a huge difference: they can
411 // only be executed, any other I/O operation on them is bound to fail with
412 // ERROR_FILE_NOT_FOUND even though the file exists.
413 if (read_reparse_point(wname, buf, &buf_len) == FAIL)
414 {
415 vim_free(wname);
416 return NULL;
417 }
418 vim_free(wname);
419
420 if (rb->ReparseTag != IO_REPARSE_TAG_APPEXECLINK)
421 return NULL;
422
423 // The (undocumented) reparse buffer contains a set of N null-terminated
424 // Unicode strings, the application path is stored in the third one.
425 if (rb->AppExecLinkReparseBuffer.StringCount < 3)
426 return NULL;
427
428 p = rb->AppExecLinkReparseBuffer.StringList;
429 end = p + rb->ReparseDataLength / sizeof(WCHAR);
430 for (idx = 0; p < end
431 && idx < (int)rb->AppExecLinkReparseBuffer.StringCount
432 && idx != 2; )
433 {
Yegappan Lakshmananebb01bd2022-06-08 15:14:09 +0100434 if (*p++ == L'\0')
LemonBoy40fd7e62022-05-05 20:18:16 +0100435 ++idx;
436 }
437
438 return utf16_to_enc(p, NULL);
439}
440
LemonBoy23c5ebe2024-06-18 20:43:51 +0200441// Use 64-bit stat functions.
442#undef stat
443#undef _stat
444#undef _wstat
445#undef _fstat
446#define stat _stat64
447#define _stat _stat64
448#define _wstat _wstat64
449#define _fstat _fstat64
450
451/*
452 * Implements lstat() and stat() that can handle symlinks properly.
453 */
454 static int
455mswin_stat_impl(const WCHAR *name, stat_T *stp, const int resolve)
456{
457 int n;
458 int fd;
459 BOOL is_symlink = FALSE;
460 HANDLE hFind, h;
461 DWORD attr = 0;
462 DWORD flag = 0;
463 WIN32_FIND_DATAW findDataW;
464
465#ifdef _UCRT
466 if (resolve)
467 // Universal CRT can handle symlinks properly.
468 return _wstat(name, stp);
469#endif
470
471 hFind = FindFirstFileW(name, &findDataW);
472 if (hFind != INVALID_HANDLE_VALUE)
473 {
474 attr = findDataW.dwFileAttributes;
475 if ((attr & FILE_ATTRIBUTE_REPARSE_POINT)
476 && (findDataW.dwReserved0 == IO_REPARSE_TAG_SYMLINK))
477 is_symlink = TRUE;
478 FindClose(hFind);
479 }
480
481 // Use the plain old stat() whenever it's possible.
482 if (!is_symlink)
483 return _wstat(name, stp);
484
485 if (!resolve && is_symlink)
486 flag = FILE_FLAG_OPEN_REPARSE_POINT;
487 if (attr & FILE_ATTRIBUTE_DIRECTORY)
488 flag |= FILE_FLAG_BACKUP_SEMANTICS;
489
490 h = CreateFileW(name, FILE_READ_ATTRIBUTES,
491 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, flag,
492 NULL);
493 if (h == INVALID_HANDLE_VALUE)
494 return -1;
495
496 fd = _open_osfhandle((intptr_t)h, _O_RDONLY);
497 n = _fstat(fd, (struct _stat *)stp);
498 if ((n == 0) && (attr & FILE_ATTRIBUTE_DIRECTORY))
499 stp->st_mode = (stp->st_mode & ~S_IFMT) | S_IFDIR;
500 _close(fd);
501
502 return n;
503}
504
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505/*
506 * stat() can't handle a trailing '/' or '\', remove it first.
LemonBoy23c5ebe2024-06-18 20:43:51 +0200507 * When 'resolve' is true behave as lstat() wrt symlinks.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000508 */
LemonBoy23c5ebe2024-06-18 20:43:51 +0200509 static int
510stat_impl(const char *name, stat_T *stp, const int resolve)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100512 // WinNT and later can use _MAX_PATH wide characters for a pathname, which
513 // means that the maximum pathname is _MAX_PATH * 3 bytes when 'enc' is
514 // UTF-8.
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100515 char_u buf[_MAX_PATH * 3 + 1];
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100516 char_u *p;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200517 WCHAR *wp;
518 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519
Bram Moolenaard2a203b2013-08-30 16:51:18 +0200520 vim_strncpy((char_u *)buf, (char_u *)name, sizeof(buf) - 1);
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100521 p = buf + STRLEN(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522 if (p > buf)
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100523 MB_PTR_BACK(buf, p);
Bram Moolenaarb1cb35f2014-01-10 13:05:20 +0100524
Bram Moolenaar0f873732019-12-05 20:28:46 +0100525 // Remove trailing '\\' except root path.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526 if (p > buf && (*p == '\\' || *p == '/') && p[-1] != ':')
527 *p = NUL;
Bram Moolenaarb1cb35f2014-01-10 13:05:20 +0100528
529 if ((buf[0] == '\\' && buf[1] == '\\') || (buf[0] == '/' && buf[1] == '/'))
530 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100531 // UNC root path must be followed by '\\'.
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100532 p = vim_strpbrk(buf + 2, (char_u *)"\\/");
Bram Moolenaarb1cb35f2014-01-10 13:05:20 +0100533 if (p != NULL)
534 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100535 p = vim_strpbrk(p + 1, (char_u *)"\\/");
Bram Moolenaarb1cb35f2014-01-10 13:05:20 +0100536 if (p == NULL)
537 STRCAT(buf, "\\");
538 }
539 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200541 wp = enc_to_utf16(buf, NULL);
542 if (wp == NULL)
543 return -1;
544
LemonBoy23c5ebe2024-06-18 20:43:51 +0200545 n = mswin_stat_impl(wp, stp, resolve);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200546 vim_free(wp);
547 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548}
549
LemonBoy23c5ebe2024-06-18 20:43:51 +0200550 int
551vim_lstat(const char *name, stat_T *stp)
552{
553 return stat_impl(name, stp, FALSE);
554}
555
556 int
557vim_stat(const char *name, stat_T *stp)
558{
559 return stat_impl(name, stp, TRUE);
560}
561
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200562#if (defined(FEAT_GUI_MSWIN) && !defined(VIMDLL)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563 void
Bram Moolenaar26e86442020-05-17 14:06:16 +0200564mch_settmode(tmode_T tmode UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100566 // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567}
568
569 int
570mch_get_shellsize(void)
571{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100572 // never used
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 return OK;
574}
575
576 void
577mch_set_shellsize(void)
578{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100579 // never used
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580}
581
582/*
583 * Rows and/or Columns has changed.
584 */
585 void
586mch_new_shellsize(void)
587{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100588 // never used
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589}
590
591#endif
592
593/*
594 * We have no job control, so fake it by starting a new shell.
595 */
596 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100597mch_suspend(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598{
599 suspend_shell();
600}
601
602#if defined(USE_MCH_ERRMSG) || defined(PROTO)
603
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200604# ifdef display_errors
605# undef display_errors
606# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607
608/*
609 * Display the saved error message(s).
610 */
611 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100612display_errors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613{
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200614# ifdef FEAT_GUI
Bram Moolenaar819ab822022-06-13 22:34:14 +0100615 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200617# ifdef VIMDLL
618 if (gui.in_use || gui.starting)
619# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200621 if (error_ga.ga_data != NULL)
622 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100623 // avoid putting up a message box with blanks only
Bram Moolenaar819ab822022-06-13 22:34:14 +0100624 for (p = (char_u *)error_ga.ga_data; *p; ++p)
Keith Thompson184f71c2024-01-04 21:19:04 +0100625 if (!SAFE_isspace(*p))
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200626 {
Bram Moolenaar2d12c252022-06-13 21:42:45 +0100627 // Only use a dialog when not using --gui-dialog-file:
628 // write text to a file.
629 if (!gui_dialog_log((char_u *)"Errors", p))
630 (void)gui_mch_dialog(
Bram Moolenaarc17ef8e2006-03-25 21:48:58 +0000631 gui.starting ? VIM_INFO :
Bram Moolenaarc17ef8e2006-03-25 21:48:58 +0000632 VIM_ERROR,
Bram Moolenaarc17ef8e2006-03-25 21:48:58 +0000633 gui.starting ? (char_u *)_("Message") :
Bram Moolenaarc17ef8e2006-03-25 21:48:58 +0000634 (char_u *)_("Error"),
Bram Moolenaar819ab822022-06-13 22:34:14 +0100635 p, (char_u *)_("&Ok"),
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100636 1, NULL, FALSE);
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200637 break;
638 }
639 ga_clear(&error_ga);
640 }
641 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200643# endif
644# if !defined(FEAT_GUI) || defined(VIMDLL)
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +0100645 FlushFileBuffers(GetStdHandle(STD_ERROR_HANDLE));
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200646# endif
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +0100647}
648#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649
650
651/*
652 * Return TRUE if "p" contain a wildcard that can be expanded by
653 * dos_expandpath().
654 */
655 int
656mch_has_exp_wildcard(char_u *p)
657{
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100658 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 {
660 if (vim_strchr((char_u *)"?*[", *p) != NULL
661 || (*p == '~' && p[1] != NUL))
662 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 }
664 return FALSE;
665}
666
667/*
668 * Return TRUE if "p" contain a wildcard or a "~1" kind of thing (could be a
669 * shortened file name).
670 */
671 int
672mch_has_wildcard(char_u *p)
673{
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100674 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 {
676 if (vim_strchr((char_u *)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100677#ifdef VIM_BACKTICK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 "?*$[`"
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100679#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 "?*$["
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100681#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 , *p) != NULL
683 || (*p == '~' && p[1] != NUL))
684 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 }
686 return FALSE;
687}
688
689
690/*
691 * The normal _chdir() does not change the default drive. This one does.
692 * Returning 0 implies success; -1 implies failure.
693 */
694 int
695mch_chdir(char *path)
696{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200697 WCHAR *p;
698 int n;
699
Bram Moolenaar0f873732019-12-05 20:28:46 +0100700 if (path[0] == NUL) // just checking...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 return -1;
702
Bram Moolenaara2974d72009-07-14 16:38:36 +0000703 if (p_verbose >= 5)
704 {
705 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100706 smsg("chdir(%s)", path);
Bram Moolenaara2974d72009-07-14 16:38:36 +0000707 verbose_leave();
708 }
Keith Thompson184f71c2024-01-04 21:19:04 +0100709 if (SAFE_isalpha(path[0]) && path[1] == ':') // has a drive name
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100711 // If we can change to the drive, skip that part of the path. If we
712 // can't then the current directory may be invalid, try using chdir()
713 // with the whole path.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 if (_chdrive(TOLOWER_ASC(path[0]) - 'a' + 1) == 0)
715 path += 2;
716 }
717
Bram Moolenaar0f873732019-12-05 20:28:46 +0100718 if (*path == NUL) // drive name only
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719 return 0;
720
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200721 p = enc_to_utf16((char_u *)path, NULL);
722 if (p == NULL)
723 return -1;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000724
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200725 n = _wchdir(p);
726 vim_free(p);
727 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728}
729
730
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200731#if defined(FEAT_GUI_MSWIN) && !defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732/*
733 * return non-zero if a character is available
734 */
735 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100736mch_char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100738 // never used
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 return TRUE;
740}
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200741
742# if defined(FEAT_TERMINAL) || defined(PROTO)
743/*
744 * Check for any pending input or messages.
745 */
746 int
747mch_check_messages(void)
748{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100749 // TODO: check for messages
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200750 return TRUE;
751}
752# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753#endif
754
755
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756#if defined(FEAT_LIBCALL) || defined(PROTO)
757/*
758 * Call a DLL routine which takes either a string or int param
759 * and returns an allocated string.
760 * Return OK if it worked, FAIL if not.
761 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762typedef LPTSTR (*MYSTRPROCSTR)(LPTSTR);
763typedef LPTSTR (*MYINTPROCSTR)(int);
764typedef int (*MYSTRPROCINT)(LPTSTR);
765typedef int (*MYINTPROCINT)(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767/*
768 * Check if a pointer points to a valid NUL terminated string.
769 * Return the length of the string, including terminating NUL.
770 * Returns 0 for an invalid pointer, 1 for an empty string.
771 */
772 static size_t
773check_str_len(char_u *str)
774{
775 SYSTEM_INFO si;
776 MEMORY_BASIC_INFORMATION mbi;
777 size_t length = 0;
778 size_t i;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100779 const char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780
Bram Moolenaar0f873732019-12-05 20:28:46 +0100781 // get page size
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 GetSystemInfo(&si);
783
Bram Moolenaar0f873732019-12-05 20:28:46 +0100784 // get memory information
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000785 if (!VirtualQuery(str, &mbi, sizeof(mbi)))
786 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000788 // pre cast these (typing savers)
789 long_u dwStr = (long_u)str;
790 long_u dwBaseAddress = (long_u)mbi.BaseAddress;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000792 // get start address of page that str is on
793 long_u strPage = dwStr - (dwStr - dwBaseAddress) % si.dwPageSize;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000795 // get length from str to end of page
796 long_u pageLength = si.dwPageSize - (dwStr - strPage);
797
798 for (p = str; !IsBadReadPtr(p, (UINT)pageLength);
799 p += pageLength, pageLength = si.dwPageSize)
800 for (i = 0; i < pageLength; ++i, ++length)
801 if (p[i] == NUL)
802 return length + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803
804 return 0;
805}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200807/*
808 * Passed to do_in_runtimepath() to load a vim.ico file.
809 */
810 static void
811mch_icon_load_cb(char_u *fname, void *cookie)
812{
813 HANDLE *h = (HANDLE *)cookie;
814
815 *h = LoadImage(NULL,
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100816 (LPSTR)fname,
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200817 IMAGE_ICON,
818 64,
819 64,
820 LR_LOADFROMFILE | LR_LOADMAP3DCOLORS);
821}
822
823/*
824 * Try loading an icon file from 'runtimepath'.
825 */
826 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100827mch_icon_load(HANDLE *iconp)
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200828{
829 return do_in_runtimepath((char_u *)"bitmaps/vim.ico",
Bram Moolenaar7f8989d2016-03-12 22:11:39 +0100830 0, mch_icon_load_cb, iconp);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200831}
832
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 int
834mch_libcall(
835 char_u *libname,
836 char_u *funcname,
Bram Moolenaar0f873732019-12-05 20:28:46 +0100837 char_u *argstring, // NULL when using a argint
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 int argint,
Bram Moolenaar0f873732019-12-05 20:28:46 +0100839 char_u **string_result,// NULL when using number_result
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 int *number_result)
841{
842 HINSTANCE hinstLib;
843 MYSTRPROCSTR ProcAdd;
844 MYINTPROCSTR ProcAddI;
845 char_u *retval_str = NULL;
846 int retval_int = 0;
847 size_t len;
848
849 BOOL fRunTimeLinkSuccess = FALSE;
850
851 // Get a handle to the DLL module.
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100852 hinstLib = vimLoadLib((char *)libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853
854 // If the handle is valid, try to get the function address.
855 if (hinstLib != NULL)
856 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100857# ifdef HAVE_TRY_EXCEPT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 __try
859 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100860# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 if (argstring != NULL)
862 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100863 // Call with string argument
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100864 ProcAdd = (MYSTRPROCSTR)GetProcAddress(hinstLib, (LPCSTR)funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865 if ((fRunTimeLinkSuccess = (ProcAdd != NULL)) != 0)
866 {
867 if (string_result == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100868 retval_int = ((MYSTRPROCINT)ProcAdd)((LPSTR)argstring);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100870 retval_str = (char_u *)(ProcAdd)((LPSTR)argstring);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871 }
872 }
873 else
874 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100875 // Call with number argument
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100876 ProcAddI = (MYINTPROCSTR) GetProcAddress(hinstLib, (LPCSTR)funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877 if ((fRunTimeLinkSuccess = (ProcAddI != NULL)) != 0)
878 {
879 if (string_result == NULL)
880 retval_int = ((MYINTPROCINT)ProcAddI)(argint);
881 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100882 retval_str = (char_u *)(ProcAddI)(argint);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 }
884 }
885
886 // Save the string before we free the library.
887 // Assume that a "1" result is an illegal pointer.
888 if (string_result == NULL)
889 *number_result = retval_int;
890 else if (retval_str != NULL
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100891 && (len = check_str_len(retval_str)) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 {
Bram Moolenaar18a4ba22019-05-24 19:39:03 +0200893 *string_result = alloc(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 if (*string_result != NULL)
895 mch_memmove(*string_result, retval_str, len);
896 }
897
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100898# ifdef HAVE_TRY_EXCEPT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 }
900 __except(EXCEPTION_EXECUTE_HANDLER)
901 {
902 if (GetExceptionCode() == EXCEPTION_STACK_OVERFLOW)
K.Takatac351dc12022-01-24 11:24:08 +0000903 _resetstkoflw();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 fRunTimeLinkSuccess = 0;
905 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100906# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907
908 // Free the DLL module.
909 (void)FreeLibrary(hinstLib);
910 }
911
912 if (!fRunTimeLinkSuccess)
913 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000914 semsg(_(e_library_call_failed_for_str), funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 return FAIL;
916 }
917
918 return OK;
919}
920#endif
921
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922/*
923 * Debugging helper: expose the MCH_WRITE_DUMP stuff to other modules
924 */
925 void
Bram Moolenaar1266d672017-02-01 13:43:36 +0100926DumpPutS(const char *psz UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100928#ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 if (fdDump)
930 {
931 fputs(psz, fdDump);
932 if (psz[strlen(psz) - 1] != '\n')
933 fputc('\n', fdDump);
934 fflush(fdDump);
935 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100936#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937}
938
939#ifdef _DEBUG
940
941void __cdecl
942Trace(
943 char *pszFormat,
944 ...)
945{
946 CHAR szBuff[2048];
947 va_list args;
948
949 va_start(args, pszFormat);
950 vsprintf(szBuff, pszFormat, args);
951 va_end(args);
952
953 OutputDebugString(szBuff);
954}
955
956#endif //_DEBUG
957
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200958#if !defined(FEAT_GUI) || defined(VIMDLL) || defined(PROTO)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100959extern HWND g_hWnd; // This is in os_win32.c.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960
961/*
962 * Showing the printer dialog is tricky since we have no GUI
963 * window to parent it. The following routines are needed to
964 * get the window parenting and Z-order to work properly.
965 */
966 static void
967GetConsoleHwnd(void)
968{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100969 // Skip if it's already set.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 if (s_hwnd != 0)
971 return;
972
Bram Moolenaar0f873732019-12-05 20:28:46 +0100973 // Window handle may have been found by init code (Windows NT only)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 if (g_hWnd != 0)
975 {
976 s_hwnd = g_hWnd;
977 return;
978 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979
Bram Moolenaare1ed53f2019-02-12 23:12:37 +0100980 s_hwnd = GetConsoleWindow();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981}
Bram Moolenaar843ee412004-06-30 16:16:41 +0000982
983/*
984 * Console implementation of ":winpos".
985 */
986 int
987mch_get_winpos(int *x, int *y)
988{
989 RECT rect;
990
991 GetConsoleHwnd();
992 GetWindowRect(s_hwnd, &rect);
993 *x = rect.left;
994 *y = rect.top;
995 return OK;
996}
997
998/*
999 * Console implementation of ":winpos x y".
1000 */
1001 void
1002mch_set_winpos(int x, int y)
1003{
1004 GetConsoleHwnd();
1005 SetWindowPos(s_hwnd, NULL, x, y, 0, 0,
1006 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
1007}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008#endif
1009
1010#if (defined(FEAT_PRINTER) && !defined(FEAT_POSTSCRIPT)) || defined(PROTO)
1011
Bram Moolenaar0f873732019-12-05 20:28:46 +01001012//=================================================================
1013// Win32 printer stuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014
1015static HFONT prt_font_handles[2][2][2];
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001016static PRINTDLGW prt_dlg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017static const int boldface[2] = {FW_REGULAR, FW_BOLD};
1018static TEXTMETRIC prt_tm;
1019static int prt_line_height;
1020static int prt_number_width;
1021static int prt_left_margin;
1022static int prt_right_margin;
1023static int prt_top_margin;
1024static char_u szAppName[] = TEXT("VIM");
1025static HWND hDlgPrint;
1026static int *bUserAbort = NULL;
1027static char_u *prt_name = NULL;
1028
Bram Moolenaar0f873732019-12-05 20:28:46 +01001029// Defines which are also in vim.rc.
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001030# define IDC_BOX1 400
1031# define IDC_PRINTTEXT1 401
1032# define IDC_PRINTTEXT2 402
1033# define IDC_PROGRESS 403
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001035 static BOOL
1036vimSetDlgItemText(HWND hDlg, int nIDDlgItem, char_u *s)
1037{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001038 WCHAR *wp;
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001039 BOOL ret;
1040
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001041 wp = enc_to_utf16(s, NULL);
1042 if (wp == NULL)
1043 return FALSE;
1044
1045 ret = SetDlgItemTextW(hDlg, nIDDlgItem, wp);
1046 vim_free(wp);
1047 return ret;
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001048}
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001049
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050/*
1051 * Convert BGR to RGB for Windows GDI calls
1052 */
1053 static COLORREF
1054swap_me(COLORREF colorref)
1055{
1056 int temp;
1057 char *ptr = (char *)&colorref;
1058
1059 temp = *(ptr);
1060 *(ptr ) = *(ptr + 2);
1061 *(ptr + 2) = temp;
1062 return colorref;
1063}
1064
K.Takatac351dc12022-01-24 11:24:08 +00001065 static INT_PTR CALLBACK
Bram Moolenaar1266d672017-02-01 13:43:36 +01001066PrintDlgProc(
1067 HWND hDlg,
1068 UINT message,
1069 WPARAM wParam UNUSED,
1070 LPARAM lParam UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001072# ifdef FEAT_GETTEXT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 NONCLIENTMETRICS nm;
1074 static HFONT hfont;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001075# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076
1077 switch (message)
1078 {
1079 case WM_INITDIALOG:
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001080# ifdef FEAT_GETTEXT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 nm.cbSize = sizeof(NONCLIENTMETRICS);
1082 if (SystemParametersInfo(
1083 SPI_GETNONCLIENTMETRICS,
1084 sizeof(NONCLIENTMETRICS),
1085 &nm,
1086 0))
1087 {
1088 char buff[MAX_PATH];
1089 int i;
1090
Bram Moolenaar0f873732019-12-05 20:28:46 +01001091 // Translate the dialog texts
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 hfont = CreateFontIndirect(&nm.lfMessageFont);
1093 for (i = IDC_PRINTTEXT1; i <= IDC_PROGRESS; i++)
1094 {
1095 SendDlgItemMessage(hDlg, i, WM_SETFONT, (WPARAM)hfont, 1);
1096 if (GetDlgItemText(hDlg,i, buff, sizeof(buff)))
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001097 vimSetDlgItemText(hDlg,i, (char_u *)_(buff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 }
1099 SendDlgItemMessage(hDlg, IDCANCEL,
1100 WM_SETFONT, (WPARAM)hfont, 1);
1101 if (GetDlgItemText(hDlg,IDCANCEL, buff, sizeof(buff)))
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001102 vimSetDlgItemText(hDlg,IDCANCEL, (char_u *)_(buff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001104# endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001105 SetWindowText(hDlg, (LPCSTR)szAppName);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 if (prt_name != NULL)
1107 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001108 vimSetDlgItemText(hDlg, IDC_PRINTTEXT2, (char_u *)prt_name);
Bram Moolenaard23a8232018-02-10 18:45:26 +01001109 VIM_CLEAR(prt_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 }
1111 EnableMenuItem(GetSystemMenu(hDlg, FALSE), SC_CLOSE, MF_GRAYED);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001112# if !defined(FEAT_GUI) || defined(VIMDLL)
1113# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001114 if (!gui.in_use)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001115# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001116 BringWindowToTop(s_hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001117# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 return TRUE;
1119
1120 case WM_COMMAND:
1121 *bUserAbort = TRUE;
1122 EnableWindow(GetParent(hDlg), TRUE);
1123 DestroyWindow(hDlg);
1124 hDlgPrint = NULL;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001125# ifdef FEAT_GETTEXT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 DeleteObject(hfont);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001127# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 return TRUE;
1129 }
1130 return FALSE;
1131}
1132
1133 static BOOL CALLBACK
Bram Moolenaar1266d672017-02-01 13:43:36 +01001134AbortProc(HDC hdcPrn UNUSED, int iCode UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135{
1136 MSG msg;
1137
K.Takatab7057bd2022-01-21 11:37:07 +00001138 while (!*bUserAbort && PeekMessageW(&msg, NULL, 0, 0, PM_REMOVE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 {
K.Takatab7057bd2022-01-21 11:37:07 +00001140 if (!hDlgPrint || !IsDialogMessageW(hDlgPrint, &msg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 {
1142 TranslateMessage(&msg);
K.Takatab7057bd2022-01-21 11:37:07 +00001143 DispatchMessageW(&msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 }
1145 }
1146 return !*bUserAbort;
1147}
1148
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001149# if !defined(FEAT_GUI) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150
Bram Moolenaar26fdd7d2011-11-30 13:42:44 +01001151 static UINT_PTR CALLBACK
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152PrintHookProc(
1153 HWND hDlg, // handle to dialog box
1154 UINT uiMsg, // message identifier
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02001155 WPARAM wParam UNUSED, // message parameter
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 LPARAM lParam // message parameter
1157 )
1158{
1159 HWND hwndOwner;
1160 RECT rc, rcDlg, rcOwner;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001161 PRINTDLGW *pPD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001163 if (uiMsg != WM_INITDIALOG)
1164 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001166 // Get the owner window and dialog box rectangles.
1167 if ((hwndOwner = GetParent(hDlg)) == NULL)
1168 hwndOwner = GetDesktopWindow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001170 GetWindowRect(hwndOwner, &rcOwner);
1171 GetWindowRect(hDlg, &rcDlg);
1172 CopyRect(&rc, &rcOwner);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001174 // Offset the owner and dialog box rectangles so that
1175 // right and bottom values represent the width and
1176 // height, and then offset the owner again to discard
1177 // space taken up by the dialog box.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001179 OffsetRect(&rcDlg, -rcDlg.left, -rcDlg.top);
1180 OffsetRect(&rc, -rc.left, -rc.top);
1181 OffsetRect(&rc, -rcDlg.right, -rcDlg.bottom);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001183 // The new position is the sum of half the remaining
1184 // space and the owner's original position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001186 SetWindowPos(hDlg,
1187 HWND_TOP,
1188 rcOwner.left + (rc.right / 2),
1189 rcOwner.top + (rc.bottom / 2),
1190 0, 0, // ignores size arguments
1191 SWP_NOSIZE);
1192
1193 // tackle the printdlg copiesctrl problem
1194 pPD = (PRINTDLGW *)lParam;
1195 pPD->nCopies = (WORD)pPD->lCustData;
1196 SetDlgItemInt( hDlg, edt3, pPD->nCopies, FALSE );
1197 // Bring the window to top
1198 BringWindowToTop(GetParent(hDlg));
1199 SetForegroundWindow(hDlg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200
1201 return FALSE;
1202}
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001203# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204
1205 void
1206mch_print_cleanup(void)
1207{
1208 int pifItalic;
1209 int pifBold;
1210 int pifUnderline;
1211
1212 for (pifBold = 0; pifBold <= 1; pifBold++)
1213 for (pifItalic = 0; pifItalic <= 1; pifItalic++)
1214 for (pifUnderline = 0; pifUnderline <= 1; pifUnderline++)
1215 DeleteObject(prt_font_handles[pifBold][pifItalic][pifUnderline]);
1216
1217 if (prt_dlg.hDC != NULL)
1218 DeleteDC(prt_dlg.hDC);
1219 if (!*bUserAbort)
1220 SendMessage(hDlgPrint, WM_COMMAND, 0, 0);
1221}
1222
1223 static int
1224to_device_units(int idx, int dpi, int physsize, int offset, int def_number)
1225{
1226 int ret = 0;
1227 int u;
1228 int nr;
1229
1230 u = prt_get_unit(idx);
1231 if (u == PRT_UNIT_NONE)
1232 {
1233 u = PRT_UNIT_PERC;
1234 nr = def_number;
1235 }
1236 else
1237 nr = printer_opts[idx].number;
1238
1239 switch (u)
1240 {
1241 case PRT_UNIT_PERC:
1242 ret = (physsize * nr) / 100;
1243 break;
1244 case PRT_UNIT_INCH:
1245 ret = (nr * dpi);
1246 break;
1247 case PRT_UNIT_MM:
1248 ret = (nr * 10 * dpi) / 254;
1249 break;
1250 case PRT_UNIT_POINT:
1251 ret = (nr * 10 * dpi) / 720;
1252 break;
1253 }
1254
1255 if (ret < offset)
1256 return 0;
1257 else
1258 return ret - offset;
1259}
1260
1261 static int
1262prt_get_cpl(void)
1263{
1264 int hr;
1265 int phyw;
1266 int dvoff;
1267 int rev_offset;
1268 int dpi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269
1270 GetTextMetrics(prt_dlg.hDC, &prt_tm);
1271 prt_line_height = prt_tm.tmHeight + prt_tm.tmExternalLeading;
1272
1273 hr = GetDeviceCaps(prt_dlg.hDC, HORZRES);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 phyw = GetDeviceCaps(prt_dlg.hDC, PHYSICALWIDTH);
1275 dvoff = GetDeviceCaps(prt_dlg.hDC, PHYSICALOFFSETX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 dpi = GetDeviceCaps(prt_dlg.hDC, LOGPIXELSX);
1277
1278 rev_offset = phyw - (dvoff + hr);
1279
1280 prt_left_margin = to_device_units(OPT_PRINT_LEFT, dpi, phyw, dvoff, 10);
1281 if (prt_use_number())
1282 {
1283 prt_number_width = PRINT_NUMBER_WIDTH * prt_tm.tmAveCharWidth;
1284 prt_left_margin += prt_number_width;
1285 }
1286 else
1287 prt_number_width = 0;
1288
1289 prt_right_margin = hr - to_device_units(OPT_PRINT_RIGHT, dpi, phyw,
1290 rev_offset, 5);
1291
1292 return (prt_right_margin - prt_left_margin) / prt_tm.tmAveCharWidth;
1293}
1294
1295 static int
1296prt_get_lpp(void)
1297{
1298 int vr;
1299 int phyw;
1300 int dvoff;
1301 int rev_offset;
1302 int bottom_margin;
1303 int dpi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304
1305 vr = GetDeviceCaps(prt_dlg.hDC, VERTRES);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 phyw = GetDeviceCaps(prt_dlg.hDC, PHYSICALHEIGHT);
1307 dvoff = GetDeviceCaps(prt_dlg.hDC, PHYSICALOFFSETY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 dpi = GetDeviceCaps(prt_dlg.hDC, LOGPIXELSY);
1309
1310 rev_offset = phyw - (dvoff + vr);
1311
1312 prt_top_margin = to_device_units(OPT_PRINT_TOP, dpi, phyw, dvoff, 5);
1313
Bram Moolenaar0f873732019-12-05 20:28:46 +01001314 // adjust top margin if there is a header
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 prt_top_margin += prt_line_height * prt_header_height();
1316
1317 bottom_margin = vr - to_device_units(OPT_PRINT_BOT, dpi, phyw,
1318 rev_offset, 5);
1319
1320 return (bottom_margin - prt_top_margin) / prt_line_height;
1321}
1322
1323 int
1324mch_print_init(prt_settings_T *psettings, char_u *jobname, int forceit)
1325{
1326 static HGLOBAL stored_dm = NULL;
1327 static HGLOBAL stored_devn = NULL;
1328 static int stored_nCopies = 1;
1329 static int stored_nFlags = 0;
1330
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001331 LOGFONTW fLogFont;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 int pifItalic;
1333 int pifBold;
1334 int pifUnderline;
1335
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001336 DEVMODEW *mem;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 DEVNAMES *devname;
1338 int i;
Bram Moolenaarb391e1f2023-01-16 19:51:03 +00001339 DWORD err;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340
1341 bUserAbort = &(psettings->user_abort);
Bram Moolenaara80faa82020-04-12 19:37:17 +02001342 CLEAR_FIELD(prt_dlg);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001343 prt_dlg.lStructSize = sizeof(PRINTDLGW);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001344# if !defined(FEAT_GUI) || defined(VIMDLL)
1345# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001346 if (!gui.in_use)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001347# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01001348 GetConsoleHwnd(); // get value of s_hwnd
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001349# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 prt_dlg.hwndOwner = s_hwnd;
1351 prt_dlg.Flags = PD_NOPAGENUMS | PD_NOSELECTION | PD_RETURNDC;
1352 if (!forceit)
1353 {
1354 prt_dlg.hDevMode = stored_dm;
1355 prt_dlg.hDevNames = stored_devn;
1356 prt_dlg.lCustData = stored_nCopies; // work around bug in print dialog
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001357# if !defined(FEAT_GUI) || defined(VIMDLL)
1358# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001359 if (!gui.in_use)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001360# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001361 {
1362 /*
1363 * Use hook to prevent console window being sent to back
1364 */
1365 prt_dlg.lpfnPrintHook = PrintHookProc;
1366 prt_dlg.Flags |= PD_ENABLEPRINTHOOK;
1367 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001368# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 prt_dlg.Flags |= stored_nFlags;
1370 }
1371
1372 /*
1373 * If bang present, return default printer setup with no dialog
1374 * never show dialog if we are running over telnet
1375 */
1376 if (forceit
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001377# if !defined(FEAT_GUI) || defined(VIMDLL)
1378# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001379 || (!gui.in_use && !term_console)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001380# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 || !term_console
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001382# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001383# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 )
1385 {
1386 prt_dlg.Flags |= PD_RETURNDEFAULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 /*
1388 * MSDN suggests setting the first parameter to WINSPOOL for
1389 * NT, but NULL appears to work just as well.
1390 */
1391 if (*p_pdev != NUL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001392 prt_dlg.hDC = CreateDC(NULL, (LPCSTR)p_pdev, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 {
1395 prt_dlg.Flags |= PD_RETURNDEFAULT;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001396 if (PrintDlgW(&prt_dlg) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 goto init_fail_dlg;
1398 }
1399 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001400 else if (PrintDlgW(&prt_dlg) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 goto init_fail_dlg;
1402 else
1403 {
1404 /*
1405 * keep the previous driver context
1406 */
1407 stored_dm = prt_dlg.hDevMode;
1408 stored_devn = prt_dlg.hDevNames;
1409 stored_nFlags = prt_dlg.Flags;
1410 stored_nCopies = prt_dlg.nCopies;
1411 }
1412
1413 if (prt_dlg.hDC == NULL)
1414 {
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00001415 emsg(_(e_printer_selection_failed));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 mch_print_cleanup();
1417 return FALSE;
1418 }
1419
Bram Moolenaar0f873732019-12-05 20:28:46 +01001420 // Not all printer drivers report the support of color (or grey) in the
1421 // same way. Let's set has_color if there appears to be some way to print
1422 // more than B&W.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 i = GetDeviceCaps(prt_dlg.hDC, NUMCOLORS);
1424 psettings->has_color = (GetDeviceCaps(prt_dlg.hDC, BITSPIXEL) > 1
1425 || GetDeviceCaps(prt_dlg.hDC, PLANES) > 1
1426 || i > 2 || i == -1);
1427
Bram Moolenaar0f873732019-12-05 20:28:46 +01001428 // Ensure all font styles are baseline aligned
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 SetTextAlign(prt_dlg.hDC, TA_BASELINE|TA_LEFT);
1430
1431 /*
1432 * On some windows systems the nCopies parameter is not
1433 * passed back correctly. It must be retrieved from the
1434 * hDevMode struct.
1435 */
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001436 mem = (DEVMODEW *)GlobalLock(prt_dlg.hDevMode);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 if (mem != NULL)
1438 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439 if (mem->dmCopies != 1)
1440 stored_nCopies = mem->dmCopies;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 if ((mem->dmFields & DM_DUPLEX) && (mem->dmDuplex & ~DMDUP_SIMPLEX))
1442 psettings->duplex = TRUE;
1443 if ((mem->dmFields & DM_COLOR) && (mem->dmColor & DMCOLOR_COLOR))
1444 psettings->has_color = TRUE;
1445 }
1446 GlobalUnlock(prt_dlg.hDevMode);
1447
1448 devname = (DEVNAMES *)GlobalLock(prt_dlg.hDevNames);
1449 if (devname != 0)
1450 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001451 WCHAR *wprinter_name = (WCHAR *)devname + devname->wDeviceOffset;
1452 WCHAR *wport_name = (WCHAR *)devname + devname->wOutputOffset;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001453 char_u *text = (char_u *)_("to %s on %s");
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001454 char_u *printer_name = utf16_to_enc(wprinter_name, NULL);
1455 char_u *port_name = utf16_to_enc(wport_name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001457 if (printer_name != NULL && port_name != NULL)
Bram Moolenaar964b3742019-05-24 18:54:09 +02001458 prt_name = alloc(STRLEN(printer_name)
1459 + STRLEN(port_name) + STRLEN(text));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 if (prt_name != NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001461 wsprintf((char *)prt_name, (const char *)text,
1462 printer_name, port_name);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001463 vim_free(printer_name);
1464 vim_free(port_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 }
1466 GlobalUnlock(prt_dlg.hDevNames);
1467
1468 /*
1469 * Initialise the font according to 'printfont'
1470 */
Bram Moolenaara80faa82020-04-12 19:37:17 +02001471 CLEAR_FIELD(fLogFont);
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001472 if (get_logfont(&fLogFont, p_pfn, prt_dlg.hDC, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 {
Bram Moolenaard88be5b2022-01-04 19:57:55 +00001474 semsg(_(e_unknown_printer_font_str), p_pfn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 mch_print_cleanup();
1476 return FALSE;
1477 }
1478
1479 for (pifBold = 0; pifBold <= 1; pifBold++)
1480 for (pifItalic = 0; pifItalic <= 1; pifItalic++)
1481 for (pifUnderline = 0; pifUnderline <= 1; pifUnderline++)
1482 {
1483 fLogFont.lfWeight = boldface[pifBold];
1484 fLogFont.lfItalic = pifItalic;
1485 fLogFont.lfUnderline = pifUnderline;
1486 prt_font_handles[pifBold][pifItalic][pifUnderline]
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001487 = CreateFontIndirectW(&fLogFont);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 }
1489
1490 SetBkMode(prt_dlg.hDC, OPAQUE);
1491 SelectObject(prt_dlg.hDC, prt_font_handles[0][0][0]);
1492
1493 /*
1494 * Fill in the settings struct
1495 */
1496 psettings->chars_per_line = prt_get_cpl();
1497 psettings->lines_per_page = prt_get_lpp();
Bram Moolenaar7ddc6422014-09-27 11:18:19 +02001498 if (prt_dlg.Flags & PD_USEDEVMODECOPIESANDCOLLATE)
1499 {
1500 psettings->n_collated_copies = (prt_dlg.Flags & PD_COLLATE)
1501 ? prt_dlg.nCopies : 1;
1502 psettings->n_uncollated_copies = (prt_dlg.Flags & PD_COLLATE)
1503 ? 1 : prt_dlg.nCopies;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504
Bram Moolenaar7ddc6422014-09-27 11:18:19 +02001505 if (psettings->n_collated_copies == 0)
1506 psettings->n_collated_copies = 1;
1507
1508 if (psettings->n_uncollated_copies == 0)
1509 psettings->n_uncollated_copies = 1;
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01001510 }
1511 else
1512 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 psettings->n_collated_copies = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 psettings->n_uncollated_copies = 1;
Bram Moolenaar7ddc6422014-09-27 11:18:19 +02001515 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516
1517 psettings->jobname = jobname;
1518
1519 return TRUE;
1520
1521init_fail_dlg:
Bram Moolenaarb391e1f2023-01-16 19:51:03 +00001522 err = CommDlgExtendedError();
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001523 if (err)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001525 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001527 // I suspect FormatMessage() doesn't work for values returned by
1528 // CommDlgExtendedError(). What does?
1529 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
1530 FORMAT_MESSAGE_FROM_SYSTEM |
1531 FORMAT_MESSAGE_IGNORE_INSERTS,
1532 NULL, err, 0, (LPTSTR)(&buf), 0, NULL);
1533 semsg(_(e_print_error_str),
1534 buf == NULL ? (char_u *)_("Unknown") : buf);
1535 LocalFree((LPVOID)(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001537 else
1538 msg_clr_eos(); // Maybe canceled
1539
1540 mch_print_cleanup();
1541 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542}
1543
1544
1545 int
1546mch_print_begin(prt_settings_T *psettings)
1547{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001548 int ret = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 char szBuffer[300];
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001550 WCHAR *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001552 hDlgPrint = CreateDialog(g_hinst, TEXT("PrintDlgBox"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 prt_dlg.hwndOwner, PrintDlgProc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554 SetAbortProc(prt_dlg.hDC, AbortProc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555 wsprintf(szBuffer, _("Printing '%s'"), gettail(psettings->jobname));
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001556 vimSetDlgItemText(hDlgPrint, IDC_PRINTTEXT1, (char_u *)szBuffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001558 wp = enc_to_utf16(psettings->jobname, NULL);
Bram Moolenaar2290b1f2018-05-13 17:30:45 +02001559 if (wp != NULL)
1560 {
1561 DOCINFOW di;
1562
Bram Moolenaara80faa82020-04-12 19:37:17 +02001563 CLEAR_FIELD(di);
Bram Moolenaar2290b1f2018-05-13 17:30:45 +02001564 di.cbSize = sizeof(di);
1565 di.lpszDocName = wp;
1566 ret = StartDocW(prt_dlg.hDC, &di);
1567 vim_free(wp);
1568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001570# ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01001571 // Give focus back to main window (when using MDI).
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001572# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001573 if (gui.in_use)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001574# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001575 SetFocus(s_hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001576# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577
1578 return (ret > 0);
1579}
1580
1581 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01001582mch_print_end(prt_settings_T *psettings UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583{
1584 EndDoc(prt_dlg.hDC);
1585 if (!*bUserAbort)
1586 SendMessage(hDlgPrint, WM_COMMAND, 0, 0);
1587}
1588
1589 int
1590mch_print_end_page(void)
1591{
1592 return (EndPage(prt_dlg.hDC) > 0);
1593}
1594
1595 int
1596mch_print_begin_page(char_u *msg)
1597{
1598 if (msg != NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001599 vimSetDlgItemText(hDlgPrint, IDC_PROGRESS, msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 return (StartPage(prt_dlg.hDC) > 0);
1601}
1602
1603 int
1604mch_print_blank_page(void)
1605{
1606 return (mch_print_begin_page(NULL) ? (mch_print_end_page()) : FALSE);
1607}
1608
1609static int prt_pos_x = 0;
1610static int prt_pos_y = 0;
1611
1612 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001613mch_print_start_line(int margin, int page_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614{
1615 if (margin)
1616 prt_pos_x = -prt_number_width;
1617 else
1618 prt_pos_x = 0;
1619 prt_pos_y = page_line * prt_line_height
1620 + prt_tm.tmAscent + prt_tm.tmExternalLeading;
1621}
1622
1623 int
1624mch_print_text_out(char_u *p, int len)
1625{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 SIZE sz;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001627 WCHAR *wp;
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001628 int wlen = len;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001629 int ret = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001631 wp = enc_to_utf16(p, &wlen);
1632 if (wp == NULL)
1633 return FALSE;
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001634
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001635 TextOutW(prt_dlg.hDC, prt_pos_x + prt_left_margin,
1636 prt_pos_y + prt_top_margin, wp, wlen);
1637 GetTextExtentPoint32W(prt_dlg.hDC, wp, wlen, &sz);
1638 vim_free(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 prt_pos_x += (sz.cx - prt_tm.tmOverhang);
Bram Moolenaar0f873732019-12-05 20:28:46 +01001640 // This is wrong when printing spaces for a TAB.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001641 if (p[len] != NUL)
1642 {
Bram Moolenaar1614a142019-10-06 22:00:13 +02001643 wlen = mb_ptr2len(p + len);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001644 wp = enc_to_utf16(p + len, &wlen);
1645 if (wp != NULL)
1646 {
1647 GetTextExtentPoint32W(prt_dlg.hDC, wp, 1, &sz);
1648 ret = (prt_pos_x + prt_left_margin + sz.cx > prt_right_margin);
1649 vim_free(wp);
1650 }
1651 }
1652 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653}
1654
1655 void
1656mch_print_set_font(int iBold, int iItalic, int iUnderline)
1657{
1658 SelectObject(prt_dlg.hDC, prt_font_handles[iBold][iItalic][iUnderline]);
1659}
1660
1661 void
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001662mch_print_set_bg(long_u bgcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663{
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001664 SetBkColor(prt_dlg.hDC, GetNearestColor(prt_dlg.hDC,
1665 swap_me((COLORREF)bgcol)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 /*
1667 * With a white background we can draw characters transparent, which is
1668 * good for italic characters that overlap to the next char cell.
1669 */
1670 if (bgcol == 0xffffffUL)
1671 SetBkMode(prt_dlg.hDC, TRANSPARENT);
1672 else
1673 SetBkMode(prt_dlg.hDC, OPAQUE);
1674}
1675
1676 void
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001677mch_print_set_fg(long_u fgcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678{
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001679 SetTextColor(prt_dlg.hDC, GetNearestColor(prt_dlg.hDC,
1680 swap_me((COLORREF)fgcol)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681}
1682
Bram Moolenaar0f873732019-12-05 20:28:46 +01001683#endif // FEAT_PRINTER && !FEAT_POSTSCRIPT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684
Bram Moolenaar58d98232005-07-23 22:25:46 +00001685
1686
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687#if defined(FEAT_SHORTCUT) || defined(PROTO)
Bram Moolenaar82881492012-11-20 16:53:39 +01001688# ifndef PROTO
1689# include <shlobj.h>
1690# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691
Bram Moolenaar4a792c82019-06-06 12:22:41 +02001692# define is_path_sep(c) ((c) == L'\\' || (c) == L'/')
1693
1694 static int
1695is_reparse_point_included(LPCWSTR fname)
1696{
1697 LPCWSTR p = fname, q;
1698 WCHAR buf[MAX_PATH];
1699 DWORD attr;
1700
Keith Thompson184f71c2024-01-04 21:19:04 +01001701 if (SAFE_isalpha(p[0]) && p[1] == L':' && is_path_sep(p[2]))
Bram Moolenaar4a792c82019-06-06 12:22:41 +02001702 p += 3;
1703 else if (is_path_sep(p[0]) && is_path_sep(p[1]))
1704 p += 2;
1705
1706 while (*p != L'\0')
1707 {
1708 q = wcspbrk(p, L"\\/");
1709 if (q == NULL)
1710 p = q = fname + wcslen(fname);
1711 else
1712 p = q + 1;
1713 if (q - fname >= MAX_PATH)
1714 return FALSE;
1715 wcsncpy(buf, fname, q - fname);
1716 buf[q - fname] = L'\0';
1717 attr = GetFileAttributesW(buf);
1718 if (attr != INVALID_FILE_ATTRIBUTES
1719 && (attr & FILE_ATTRIBUTE_REPARSE_POINT) != 0)
1720 return TRUE;
1721 }
1722 return FALSE;
1723}
1724
AmberArrf5d0f542023-08-20 20:03:45 +02001725/*
1726 * Return the resolved file path, NULL if "fname" is an AppExecLink reparse
1727 * point, already fully resolved, or it doesn't exists.
1728 */
1729 char_u *
Bram Moolenaardce1e892019-02-10 23:18:53 +01001730resolve_reparse_point(char_u *fname)
1731{
1732 HANDLE h = INVALID_HANDLE_VALUE;
1733 DWORD size;
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001734 WCHAR *p, *wp;
Bram Moolenaardce1e892019-02-10 23:18:53 +01001735 char_u *rfname = NULL;
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001736 WCHAR *buff = NULL;
Bram Moolenaardce1e892019-02-10 23:18:53 +01001737
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001738 p = enc_to_utf16(fname, NULL);
1739 if (p == NULL)
1740 goto fail;
1741
Bram Moolenaar4a792c82019-06-06 12:22:41 +02001742 if (!is_reparse_point_included(p))
1743 {
1744 vim_free(p);
1745 goto fail;
1746 }
1747
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001748 h = CreateFileW(p, 0, 0, NULL, OPEN_EXISTING,
1749 FILE_FLAG_BACKUP_SEMANTICS, NULL);
1750 vim_free(p);
Bram Moolenaardce1e892019-02-10 23:18:53 +01001751
1752 if (h == INVALID_HANDLE_VALUE)
1753 goto fail;
1754
K.Takata27b53be2022-09-18 12:25:49 +01001755 size = GetFinalPathNameByHandleW(h, NULL, 0, 0);
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001756 if (size == 0)
1757 goto fail;
1758 buff = ALLOC_MULT(WCHAR, size);
1759 if (buff == NULL)
1760 goto fail;
K.Takata27b53be2022-09-18 12:25:49 +01001761 if (GetFinalPathNameByHandleW(h, buff, size, 0) == 0)
Bram Moolenaardce1e892019-02-10 23:18:53 +01001762 goto fail;
1763
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001764 if (wcsncmp(buff, L"\\\\?\\UNC\\", 8) == 0)
1765 {
1766 buff[6] = L'\\';
1767 wp = buff + 6;
1768 }
1769 else if (wcsncmp(buff, L"\\\\?\\", 4) == 0)
1770 wp = buff + 4;
Bram Moolenaardce1e892019-02-10 23:18:53 +01001771 else
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001772 wp = buff;
1773
1774 rfname = utf16_to_enc(wp, NULL);
Bram Moolenaardce1e892019-02-10 23:18:53 +01001775
1776fail:
1777 if (h != INVALID_HANDLE_VALUE)
1778 CloseHandle(h);
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001779 if (buff != NULL)
1780 vim_free(buff);
Bram Moolenaardce1e892019-02-10 23:18:53 +01001781
1782 return rfname;
1783}
1784
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785/*
1786 * When "fname" is the name of a shortcut (*.lnk) resolve the file it points
1787 * to and return that name in allocated memory.
1788 * Otherwise NULL is returned.
1789 */
Bram Moolenaardce1e892019-02-10 23:18:53 +01001790 static char_u *
1791resolve_shortcut(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792{
1793 HRESULT hr;
1794 IShellLink *psl = NULL;
1795 IPersistFile *ppf = NULL;
1796 OLECHAR wsz[MAX_PATH];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 char_u *rfname = NULL;
1798 int len;
Bram Moolenaar604729e2013-08-30 16:44:19 +02001799 IShellLinkW *pslw = NULL;
1800 WIN32_FIND_DATAW ffdw; // we get those free of charge
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801
Bram Moolenaar0f873732019-12-05 20:28:46 +01001802 // Check if the file name ends in ".lnk". Avoid calling
1803 // CoCreateInstance(), it's quite slow.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 if (fname == NULL)
1805 return rfname;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001806 len = (int)STRLEN(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 if (len <= 4 || STRNICMP(fname + len - 4, ".lnk", 4) != 0)
1808 return rfname;
1809
1810 CoInitialize(NULL);
1811
1812 // create a link manager object and request its interface
1813 hr = CoCreateInstance(
1814 &CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001815 &IID_IShellLinkW, (void**)&pslw);
1816 if (hr == S_OK)
1817 {
1818 WCHAR *p = enc_to_utf16(fname, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001820 if (p != NULL)
1821 {
1822 // Get a pointer to the IPersistFile interface.
1823 hr = pslw->lpVtbl->QueryInterface(
1824 pslw, &IID_IPersistFile, (void**)&ppf);
1825 if (hr != S_OK)
1826 goto shortcut_errorw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001828 // "load" the name and resolve the link
1829 hr = ppf->lpVtbl->Load(ppf, p, STGM_READ);
1830 if (hr != S_OK)
1831 goto shortcut_errorw;
1832# if 0 // This makes Vim wait a long time if the target does not exist.
1833 hr = pslw->lpVtbl->Resolve(pslw, NULL, SLR_NO_UI);
1834 if (hr != S_OK)
1835 goto shortcut_errorw;
Bram Moolenaar604729e2013-08-30 16:44:19 +02001836# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001838 // Get the path to the link target.
1839 ZeroMemory(wsz, MAX_PATH * sizeof(WCHAR));
1840 hr = pslw->lpVtbl->GetPath(pslw, wsz, MAX_PATH, &ffdw, 0);
1841 if (hr == S_OK && wsz[0] != NUL)
1842 rfname = utf16_to_enc(wsz, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001844shortcut_errorw:
1845 vim_free(p);
1846 }
1847 }
1848
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 // Release all interface pointers (both belong to the same object)
1850 if (ppf != NULL)
1851 ppf->lpVtbl->Release(ppf);
1852 if (psl != NULL)
1853 psl->lpVtbl->Release(psl);
Bram Moolenaar604729e2013-08-30 16:44:19 +02001854 if (pslw != NULL)
1855 pslw->lpVtbl->Release(pslw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856
1857 CoUninitialize();
1858 return rfname;
1859}
Bram Moolenaardce1e892019-02-10 23:18:53 +01001860
1861 char_u *
1862mch_resolve_path(char_u *fname, int reparse_point)
1863{
1864 char_u *path = resolve_shortcut(fname);
1865
1866 if (path == NULL && reparse_point)
1867 path = resolve_reparse_point(fname);
1868 return path;
1869}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870#endif
1871
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001872#if (defined(FEAT_EVAL) && (!defined(FEAT_GUI) || defined(VIMDLL))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873/*
1874 * Bring ourselves to the foreground. Does work if the OS doesn't allow it.
1875 */
1876 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001877win32_set_foreground(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878{
Bram Moolenaar0f873732019-12-05 20:28:46 +01001879 GetConsoleHwnd(); // get value of s_hwnd
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 if (s_hwnd != 0)
1881 SetForegroundWindow(s_hwnd);
1882}
1883#endif
1884
1885#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
1886/*
1887 * Client-server code for Vim
1888 *
1889 * Originally written by Paul Moore
1890 */
1891
Bram Moolenaar0f873732019-12-05 20:28:46 +01001892// In order to handle inter-process messages, we need to have a window. But
1893// the functions in this module can be called before the main GUI window is
1894// created (and may also be called in the console version, where there is no
1895// GUI window at all).
1896//
1897// So we create a hidden window, and arrange to destroy it on exit.
1898HWND message_window = 0; // window that's handling messages
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001900# define VIM_CLASSNAME "VIM_MESSAGES"
1901# define VIM_CLASSNAME_LEN (sizeof(VIM_CLASSNAME) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902
K.Takataf9f2a332022-06-17 20:05:40 +01001903// Timeout for sending a message to another Vim instance. Normally this works
1904// instantly, but it may hang when the other Vim instance is halted.
1905# define SENDMESSAGE_TIMEOUT (5 * 1000)
1906
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001907// Communication is via WM_COPYDATA messages. The message type is sent in
Bram Moolenaar0f873732019-12-05 20:28:46 +01001908// the dwData parameter. Types are defined here.
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001909# define COPYDATA_KEYS 0
1910# define COPYDATA_REPLY 1
1911# define COPYDATA_EXPR 10
1912# define COPYDATA_RESULT 11
1913# define COPYDATA_ERROR_RESULT 12
1914# define COPYDATA_ENCODING 20
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915
Bram Moolenaar0f873732019-12-05 20:28:46 +01001916// This is a structure containing a server HWND and its name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917struct server_id
1918{
1919 HWND hwnd;
1920 char_u *name;
1921};
1922
Bram Moolenaar0f873732019-12-05 20:28:46 +01001923// Last received 'encoding' that the client uses.
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001924static char_u *client_enc = NULL;
1925
1926/*
1927 * Tell the other side what encoding we are using.
K.Takataf9f2a332022-06-17 20:05:40 +01001928 * Return -1 if timeout happens. Other errors are ignored.
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001929 */
K.Takataf9f2a332022-06-17 20:05:40 +01001930 static int
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001931serverSendEnc(HWND target)
1932{
1933 COPYDATASTRUCT data;
1934
1935 data.dwData = COPYDATA_ENCODING;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001936 data.cbData = (DWORD)STRLEN(p_enc) + 1;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001937 data.lpData = p_enc;
K.Takataf9f2a332022-06-17 20:05:40 +01001938 if (SendMessageTimeout(target, WM_COPYDATA,
1939 (WPARAM)message_window, (LPARAM)&data,
1940 SMTO_ABORTIFHUNG, SENDMESSAGE_TIMEOUT, NULL) == 0)
1941 return -1;
1942 return 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001943}
1944
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945/*
1946 * Clean up on exit. This destroys the hidden message window.
1947 */
1948 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949CleanUpMessaging(void)
1950{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001951 if (message_window == 0)
1952 return;
1953
1954 DestroyWindow(message_window);
1955 message_window = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956}
1957
1958static int save_reply(HWND server, char_u *reply, int expr);
1959
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001960/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 * The window procedure for the hidden message window.
1962 * It handles callback messages and notifications from servers.
1963 * In order to process these messages, it is necessary to run a
1964 * message loop. Code which may run before the main message loop
1965 * is started (in the GUI) is careful to pump messages when it needs
1966 * to. Features which require message delivery during normal use will
1967 * not work in the console version - this basically means those
1968 * features which allow Vim to act as a server, rather than a client.
1969 */
1970 static LRESULT CALLBACK
1971Messaging_WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
1972{
1973 if (msg == WM_COPYDATA)
1974 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001975 // This is a message from another Vim. The dwData member of the
1976 // COPYDATASTRUCT determines the type of message:
1977 // COPYDATA_ENCODING:
1978 // The encoding that the client uses. Following messages will
1979 // use this encoding, convert if needed.
1980 // COPYDATA_KEYS:
1981 // A key sequence. We are a server, and a client wants these keys
1982 // adding to the input queue.
1983 // COPYDATA_REPLY:
1984 // A reply. We are a client, and a server has sent this message
1985 // in response to a request. (server2client())
1986 // COPYDATA_EXPR:
1987 // An expression. We are a server, and a client wants us to
1988 // evaluate this expression.
1989 // COPYDATA_RESULT:
1990 // A reply. We are a client, and a server has sent this message
1991 // in response to a COPYDATA_EXPR.
1992 // COPYDATA_ERROR_RESULT:
1993 // A reply. We are a client, and a server has sent this message
1994 // in response to a COPYDATA_EXPR that failed to evaluate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 COPYDATASTRUCT *data = (COPYDATASTRUCT*)lParam;
1996 HWND sender = (HWND)wParam;
1997 COPYDATASTRUCT reply;
1998 char_u *res;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 int retval;
K.Takataf9f2a332022-06-17 20:05:40 +01002000 DWORD_PTR dwret = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002001 char_u *str;
2002 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003
2004 switch (data->dwData)
2005 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002006 case COPYDATA_ENCODING:
Bram Moolenaar0f873732019-12-05 20:28:46 +01002007 // Remember the encoding that the client uses.
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002008 vim_free(client_enc);
2009 client_enc = enc_canonize((char_u *)data->lpData);
2010 return 1;
2011
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 case COPYDATA_KEYS:
Bram Moolenaar0f873732019-12-05 20:28:46 +01002013 // Remember who sent this, for <client>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 clientWindow = sender;
2015
Bram Moolenaar0f873732019-12-05 20:28:46 +01002016 // Add the received keys to the input buffer. The loop waiting
2017 // for the user to do something should check the input buffer.
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002018 str = serverConvert(client_enc, (char_u *)data->lpData, &tofree);
2019 server_to_input_buf(str);
2020 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021
2022# ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01002023 // Wake up the main GUI loop.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002024# ifdef VIMDLL
2025 if (gui.in_use)
2026# endif
2027 if (s_hwnd != 0)
2028 PostMessage(s_hwnd, WM_NULL, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029# endif
2030 return 1;
2031
2032 case COPYDATA_EXPR:
Bram Moolenaar0f873732019-12-05 20:28:46 +01002033 // Remember who sent this, for <client>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 clientWindow = sender;
2035
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002036 str = serverConvert(client_enc, (char_u *)data->lpData, &tofree);
2037 res = eval_client_expr_to_string(str);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002038
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 if (res == NULL)
2040 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00002041 char *err = _(e_invalid_expression_received);
Bram Moolenaar15bf76d2017-03-18 16:18:37 +01002042 size_t len = STRLEN(str) + STRLEN(err) + 5;
2043
Bram Moolenaar964b3742019-05-24 18:54:09 +02002044 res = alloc(len);
Bram Moolenaar15bf76d2017-03-18 16:18:37 +01002045 if (res != NULL)
2046 vim_snprintf((char *)res, len, "%s: \"%s\"", err, str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 reply.dwData = COPYDATA_ERROR_RESULT;
2048 }
2049 else
2050 reply.dwData = COPYDATA_RESULT;
2051 reply.lpData = res;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002052 reply.cbData = (DWORD)STRLEN(res) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053
K.Takataf9f2a332022-06-17 20:05:40 +01002054 if (serverSendEnc(sender) < 0)
2055 retval = -1;
2056 else
2057 {
2058 if (SendMessageTimeout(sender, WM_COPYDATA,
2059 (WPARAM)message_window, (LPARAM)&reply,
2060 SMTO_ABORTIFHUNG, SENDMESSAGE_TIMEOUT, &dwret) == 0)
2061 retval = -1;
2062 else
2063 retval = (int)dwret;
2064 }
Bram Moolenaar15bf76d2017-03-18 16:18:37 +01002065 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 vim_free(res);
2067 return retval;
2068
2069 case COPYDATA_REPLY:
2070 case COPYDATA_RESULT:
2071 case COPYDATA_ERROR_RESULT:
2072 if (data->lpData != NULL)
2073 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002074 str = serverConvert(client_enc, (char_u *)data->lpData,
2075 &tofree);
2076 if (tofree == NULL)
2077 str = vim_strsave(str);
2078 if (save_reply(sender, str,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 (data->dwData == COPYDATA_REPLY ? 0 :
2080 (data->dwData == COPYDATA_RESULT ? 1 :
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002081 2))) == FAIL)
2082 vim_free(str);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002083 else if (data->dwData == COPYDATA_REPLY)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 {
Bram Moolenaar427d51c2013-06-16 16:01:25 +02002085 char_u winstr[30];
2086
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002087 sprintf((char *)winstr, PRINTF_HEX_LONG_U, (long_u)sender);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002088 apply_autocmds(EVENT_REMOTEREPLY, winstr, str,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089 TRUE, curbuf);
2090 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 }
2092 return 1;
2093 }
2094
2095 return 0;
2096 }
2097
2098 else if (msg == WM_ACTIVATE && wParam == WA_ACTIVE)
2099 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002100 // When the message window is activated (brought to the foreground),
2101 // this actually applies to the text window.
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002102# if !defined(FEAT_GUI) || defined(VIMDLL)
2103# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002104 if (!gui.in_use)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002105# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002106 GetConsoleHwnd(); // get value of s_hwnd
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002107# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 if (s_hwnd != 0)
2109 {
2110 SetForegroundWindow(s_hwnd);
2111 return 0;
2112 }
2113 }
2114
2115 return DefWindowProc(hwnd, msg, wParam, lParam);
2116}
2117
2118/*
2119 * Initialise the message handling process. This involves creating a window
2120 * to handle messages - the window will not be visible.
2121 */
2122 void
2123serverInitMessaging(void)
2124{
2125 WNDCLASS wndclass;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126
Bram Moolenaar0f873732019-12-05 20:28:46 +01002127 // Clean up on exit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 atexit(CleanUpMessaging);
2129
Bram Moolenaar0f873732019-12-05 20:28:46 +01002130 // Register a window class - we only really care
2131 // about the window procedure
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 wndclass.style = 0;
2133 wndclass.lpfnWndProc = Messaging_WndProc;
2134 wndclass.cbClsExtra = 0;
2135 wndclass.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002136 wndclass.hInstance = g_hinst;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 wndclass.hIcon = NULL;
2138 wndclass.hCursor = NULL;
2139 wndclass.hbrBackground = NULL;
2140 wndclass.lpszMenuName = NULL;
2141 wndclass.lpszClassName = VIM_CLASSNAME;
2142 RegisterClass(&wndclass);
2143
Bram Moolenaar0f873732019-12-05 20:28:46 +01002144 // Create the message window. It will be hidden, so the details don't
2145 // matter. Don't use WS_OVERLAPPEDWINDOW, it will make a shortcut remove
2146 // focus from gvim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 message_window = CreateWindow(VIM_CLASSNAME, "",
2148 WS_POPUPWINDOW | WS_CAPTION,
2149 CW_USEDEFAULT, CW_USEDEFAULT,
2150 100, 100, NULL, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002151 g_hinst, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152}
2153
Bram Moolenaar0f873732019-12-05 20:28:46 +01002154// Used by serverSendToVim() to find an alternate server name.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002155static char_u *altname_buf_ptr = NULL;
2156
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157/*
2158 * Get the title of the window "hwnd", which is the Vim server name, in
2159 * "name[namelen]" and return the length.
2160 * Returns zero if window "hwnd" is not a Vim server.
2161 */
2162 static int
2163getVimServerName(HWND hwnd, char *name, int namelen)
2164{
2165 int len;
2166 char buffer[VIM_CLASSNAME_LEN + 1];
2167
Bram Moolenaar0f873732019-12-05 20:28:46 +01002168 // Ignore windows which aren't Vim message windows
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 len = GetClassName(hwnd, buffer, sizeof(buffer));
2170 if (len != VIM_CLASSNAME_LEN || STRCMP(buffer, VIM_CLASSNAME) != 0)
2171 return 0;
2172
Bram Moolenaar0f873732019-12-05 20:28:46 +01002173 // Get the title of the window
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 return GetWindowText(hwnd, name, namelen);
2175}
2176
2177 static BOOL CALLBACK
2178enumWindowsGetServer(HWND hwnd, LPARAM lparam)
2179{
2180 struct server_id *id = (struct server_id *)lparam;
2181 char server[MAX_PATH];
2182
Bram Moolenaar0f873732019-12-05 20:28:46 +01002183 // Get the title of the window
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 if (getVimServerName(hwnd, server, sizeof(server)) == 0)
2185 return TRUE;
2186
Bram Moolenaar0f873732019-12-05 20:28:46 +01002187 // If this is the server we're looking for, return its HWND
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 if (STRICMP(server, id->name) == 0)
2189 {
2190 id->hwnd = hwnd;
2191 return FALSE;
2192 }
2193
Bram Moolenaar0f873732019-12-05 20:28:46 +01002194 // If we are looking for an alternate server, remember this name.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002195 if (altname_buf_ptr != NULL
2196 && STRNICMP(server, id->name, STRLEN(id->name)) == 0
2197 && vim_isdigit(server[STRLEN(id->name)]))
2198 {
2199 STRCPY(altname_buf_ptr, server);
Bram Moolenaar0f873732019-12-05 20:28:46 +01002200 altname_buf_ptr = NULL; // don't use another name
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002201 }
2202
Bram Moolenaar0f873732019-12-05 20:28:46 +01002203 // Otherwise, keep looking
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 return TRUE;
2205}
2206
2207 static BOOL CALLBACK
2208enumWindowsGetNames(HWND hwnd, LPARAM lparam)
2209{
2210 garray_T *ga = (garray_T *)lparam;
2211 char server[MAX_PATH];
2212
Bram Moolenaar0f873732019-12-05 20:28:46 +01002213 // Get the title of the window
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 if (getVimServerName(hwnd, server, sizeof(server)) == 0)
2215 return TRUE;
2216
Bram Moolenaar0f873732019-12-05 20:28:46 +01002217 // Add the name to the list
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002218 ga_concat(ga, (char_u *)server);
2219 ga_concat(ga, (char_u *)"\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 return TRUE;
2221}
2222
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002223struct enum_windows_s
2224{
2225 WNDENUMPROC lpEnumFunc;
2226 LPARAM lParam;
2227};
2228
2229 static BOOL CALLBACK
2230enum_windows_child(HWND hwnd, LPARAM lParam)
2231{
2232 struct enum_windows_s *ew = (struct enum_windows_s *)lParam;
2233
2234 return (ew->lpEnumFunc)(hwnd, ew->lParam);
2235}
2236
2237 static BOOL CALLBACK
2238enum_windows_toplevel(HWND hwnd, LPARAM lParam)
2239{
2240 struct enum_windows_s *ew = (struct enum_windows_s *)lParam;
2241
Bram Moolenaar95ba5c32018-10-07 22:47:07 +02002242 if ((ew->lpEnumFunc)(hwnd, ew->lParam))
2243 return TRUE;
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002244 return EnumChildWindows(hwnd, enum_windows_child, lParam);
2245}
2246
Bram Moolenaar0f873732019-12-05 20:28:46 +01002247/*
2248 * Enumerate all windows including children.
2249 */
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002250 static BOOL
2251enum_windows(WNDENUMPROC lpEnumFunc, LPARAM lParam)
2252{
2253 struct enum_windows_s ew;
2254
2255 ew.lpEnumFunc = lpEnumFunc;
2256 ew.lParam = lParam;
2257 return EnumWindows(enum_windows_toplevel, (LPARAM)&ew);
2258}
2259
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 static HWND
2261findServer(char_u *name)
2262{
2263 struct server_id id;
2264
2265 id.name = name;
2266 id.hwnd = 0;
2267
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002268 enum_windows(enumWindowsGetServer, (LPARAM)(&id));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269
2270 return id.hwnd;
2271}
2272
2273 void
2274serverSetName(char_u *name)
2275{
2276 char_u *ok_name;
2277 HWND hwnd = 0;
2278 int i = 0;
2279 char_u *p;
2280
Bram Moolenaar0f873732019-12-05 20:28:46 +01002281 // Leave enough space for a 9-digit suffix to ensure uniqueness!
Bram Moolenaar964b3742019-05-24 18:54:09 +02002282 ok_name = alloc(STRLEN(name) + 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283
2284 STRCPY(ok_name, name);
2285 p = ok_name + STRLEN(name);
2286
2287 for (;;)
2288 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002289 // This is inefficient - we're doing an EnumWindows loop for each
2290 // possible name. It would be better to grab all names in one go,
2291 // and scan the list each time...
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 hwnd = findServer(ok_name);
2293 if (hwnd == 0)
2294 break;
2295
2296 ++i;
2297 if (i >= 1000)
2298 break;
2299
2300 sprintf((char *)p, "%d", i);
2301 }
2302
2303 if (hwnd != 0)
2304 vim_free(ok_name);
2305 else
2306 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002307 // Remember the name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 serverName = ok_name;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002309 need_maketitle = TRUE; // update Vim window title later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310
Bram Moolenaar0f873732019-12-05 20:28:46 +01002311 // Update the message window title
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002312 SetWindowText(message_window, (LPCSTR)ok_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002314# ifdef FEAT_EVAL
Bram Moolenaar0f873732019-12-05 20:28:46 +01002315 // Set the servername variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 set_vim_var_string(VV_SEND_SERVER, serverName, -1);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002317# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 }
2319}
2320
2321 char_u *
2322serverGetVimNames(void)
2323{
2324 garray_T ga;
2325
2326 ga_init2(&ga, 1, 100);
2327
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002328 enum_windows(enumWindowsGetNames, (LPARAM)(&ga));
Bram Moolenaar269ec652004-07-29 08:43:53 +00002329 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330
2331 return ga.ga_data;
2332}
2333
2334 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002335serverSendReply(
Bram Moolenaar0f873732019-12-05 20:28:46 +01002336 char_u *name, // Where to send.
2337 char_u *reply) // What to send.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338{
2339 HWND target;
2340 COPYDATASTRUCT data;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002341 long_u n = 0;
K.Takataf9f2a332022-06-17 20:05:40 +01002342 DWORD_PTR dwret = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343
Bram Moolenaar0f873732019-12-05 20:28:46 +01002344 // The "name" argument is a magic cookie obtained from expand("<client>").
2345 // It should be of the form 0xXXXXX - i.e. a C hex literal, which is the
2346 // value of the client's message window HWND.
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002347 sscanf((char *)name, SCANF_HEX_LONG_U, &n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 if (n == 0)
2349 return -1;
2350
2351 target = (HWND)n;
2352 if (!IsWindow(target))
2353 return -1;
2354
2355 data.dwData = COPYDATA_REPLY;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002356 data.cbData = (DWORD)STRLEN(reply) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 data.lpData = reply;
2358
K.Takataf9f2a332022-06-17 20:05:40 +01002359 if (serverSendEnc(target) < 0)
2360 return -1;
2361 if (SendMessageTimeout(target, WM_COPYDATA,
2362 (WPARAM)message_window, (LPARAM)&data,
2363 SMTO_ABORTIFHUNG, SENDMESSAGE_TIMEOUT, &dwret) == 0)
2364 return -1;
2365 return dwret ? 0 : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366}
2367
2368 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002369serverSendToVim(
Bram Moolenaar0f873732019-12-05 20:28:46 +01002370 char_u *name, // Where to send.
2371 char_u *cmd, // What to send.
2372 char_u **result, // Result of eval'ed expression
2373 void *ptarget, // HWND of server
2374 int asExpr, // Expression or keys?
2375 int timeout, // timeout in seconds or zero
2376 int silent) // don't complain about no server
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377{
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002378 HWND target;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 COPYDATASTRUCT data;
2380 char_u *retval = NULL;
2381 int retcode = 0;
K.Takataf9f2a332022-06-17 20:05:40 +01002382 DWORD_PTR dwret = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002383 char_u altname_buf[MAX_PATH];
2384
Bram Moolenaar0f873732019-12-05 20:28:46 +01002385 // Execute locally if no display or target is ourselves
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01002386 if (serverName != NULL && STRICMP(name, serverName) == 0)
2387 return sendToLocalVim(cmd, asExpr, result);
2388
Bram Moolenaar0f873732019-12-05 20:28:46 +01002389 // If the server name does not end in a digit then we look for an
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00002390 // alternate name. e.g. when "name" is GVIM then we may find GVIM2.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002391 if (STRLEN(name) > 1 && !vim_isdigit(name[STRLEN(name) - 1]))
2392 altname_buf_ptr = altname_buf;
2393 altname_buf[0] = NUL;
2394 target = findServer(name);
2395 altname_buf_ptr = NULL;
2396 if (target == 0 && altname_buf[0] != NUL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01002397 // Use another server name we found.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002398 target = findServer(altname_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399
2400 if (target == 0)
2401 {
2402 if (!silent)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002403 semsg(_(e_no_registered_server_named_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 return -1;
2405 }
2406
2407 if (ptarget)
2408 *(HWND *)ptarget = target;
2409
2410 data.dwData = asExpr ? COPYDATA_EXPR : COPYDATA_KEYS;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002411 data.cbData = (DWORD)STRLEN(cmd) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 data.lpData = cmd;
2413
K.Takataf9f2a332022-06-17 20:05:40 +01002414 if (serverSendEnc(target) < 0)
2415 return -1;
2416 if (SendMessageTimeout(target, WM_COPYDATA,
2417 (WPARAM)message_window, (LPARAM)&data,
2418 SMTO_ABORTIFHUNG, SENDMESSAGE_TIMEOUT, &dwret) == 0)
2419 return -1;
2420 if (dwret == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 return -1;
2422
2423 if (asExpr)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002424 retval = serverGetReply(target, &retcode, TRUE, TRUE, timeout);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425
2426 if (result == NULL)
2427 vim_free(retval);
2428 else
Bram Moolenaar0f873732019-12-05 20:28:46 +01002429 *result = retval; // Caller assumes responsibility for freeing
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430
2431 return retcode;
2432}
2433
2434/*
2435 * Bring the server to the foreground.
2436 */
2437 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002438serverForeground(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439{
2440 HWND target = findServer(name);
2441
2442 if (target != 0)
2443 SetForegroundWindow(target);
2444}
2445
Bram Moolenaar0f873732019-12-05 20:28:46 +01002446// Replies from server need to be stored until the client picks them up via
2447// remote_read(). So we maintain a list of server-id/reply pairs.
2448// Note that there could be multiple replies from one server pending if the
2449// client is slow picking them up.
2450// We just store the replies in a simple list. When we remove an entry, we
2451// move list entries down to fill the gap.
2452// The server ID is simply the HWND.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453typedef struct
2454{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002455 HWND server; // server window
2456 char_u *reply; // reply string
2457 int expr_result; // 0 for REPLY, 1 for RESULT 2 for error
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002458} reply_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459
2460static garray_T reply_list = {0, 0, sizeof(reply_T), 5, 0};
2461
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002462# define REPLY_ITEM(i) ((reply_T *)(reply_list.ga_data) + (i))
2463# define REPLY_COUNT (reply_list.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464
Bram Moolenaar0f873732019-12-05 20:28:46 +01002465// Flag which is used to wait for a reply
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466static int reply_received = 0;
2467
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002468/*
2469 * Store a reply. "reply" must be allocated memory (or NULL).
2470 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 static int
2472save_reply(HWND server, char_u *reply, int expr)
2473{
2474 reply_T *rep;
2475
2476 if (ga_grow(&reply_list, 1) == FAIL)
2477 return FAIL;
2478
2479 rep = REPLY_ITEM(REPLY_COUNT);
2480 rep->server = server;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002481 rep->reply = reply;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 rep->expr_result = expr;
2483 if (rep->reply == NULL)
2484 return FAIL;
2485
2486 ++REPLY_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 reply_received = 1;
2488 return OK;
2489}
2490
2491/*
2492 * Get a reply from server "server".
2493 * When "expr_res" is non NULL, get the result of an expression, otherwise a
2494 * server2client() message.
2495 * When non NULL, point to return code. 0 => OK, -1 => ERROR
2496 * If "remove" is TRUE, consume the message, the caller must free it then.
2497 * if "wait" is TRUE block until a message arrives (or the server exits).
2498 */
2499 char_u *
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002500serverGetReply(HWND server, int *expr_res, int remove, int wait, int timeout)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501{
2502 int i;
2503 char_u *reply;
2504 reply_T *rep;
Bram Moolenaar15e737f2017-03-18 21:22:47 +01002505 int did_process = FALSE;
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002506 time_t start;
2507 time_t now;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508
Bram Moolenaar0f873732019-12-05 20:28:46 +01002509 // When waiting, loop until the message waiting for is received.
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002510 time(&start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 for (;;)
2512 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002513 // Reset this here, in case a message arrives while we are going
2514 // through the already received messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 reply_received = 0;
2516
2517 for (i = 0; i < REPLY_COUNT; ++i)
2518 {
2519 rep = REPLY_ITEM(i);
2520 if (rep->server == server
2521 && ((rep->expr_result != 0) == (expr_res != NULL)))
2522 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002523 // Save the values we've found for later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 reply = rep->reply;
2525 if (expr_res != NULL)
2526 *expr_res = rep->expr_result == 1 ? 0 : -1;
2527
2528 if (remove)
2529 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002530 // Move the rest of the list down to fill the gap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 mch_memmove(rep, rep + 1,
2532 (REPLY_COUNT - i - 1) * sizeof(reply_T));
2533 --REPLY_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 }
2535
Bram Moolenaar0f873732019-12-05 20:28:46 +01002536 // Return the reply to the caller, who takes on responsibility
2537 // for freeing it if "remove" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 return reply;
2539 }
2540 }
2541
Bram Moolenaar0f873732019-12-05 20:28:46 +01002542 // If we got here, we didn't find a reply. Return immediately if the
2543 // "wait" parameter isn't set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 if (!wait)
Bram Moolenaar15e737f2017-03-18 21:22:47 +01002545 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002546 // Process pending messages once. Without this, looping on
2547 // remote_peek() would never get the reply.
Bram Moolenaar15e737f2017-03-18 21:22:47 +01002548 if (!did_process)
2549 {
2550 did_process = TRUE;
2551 serverProcessPendingMessages();
2552 continue;
2553 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 break;
Bram Moolenaar15e737f2017-03-18 21:22:47 +01002555 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556
Bram Moolenaar0f873732019-12-05 20:28:46 +01002557 // We need to wait for a reply. Enter a message loop until the
2558 // "reply_received" flag gets set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559
Bram Moolenaar0f873732019-12-05 20:28:46 +01002560 // Loop until we receive a reply
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 while (reply_received == 0)
2562 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002563# ifdef FEAT_TIMERS
Bram Moolenaar0f873732019-12-05 20:28:46 +01002564 // TODO: use the return value to decide how long to wait.
Bram Moolenaar42205552017-03-18 19:42:22 +01002565 check_due_timer();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002566# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002567 time(&now);
2568 if (timeout > 0 && (now - start) >= timeout)
2569 break;
2570
Bram Moolenaar0f873732019-12-05 20:28:46 +01002571 // Wait for a SendMessage() call to us. This could be the reply
2572 // we are waiting for. Use a timeout of a second, to catch the
2573 // situation that the server died unexpectedly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 MsgWaitForMultipleObjects(0, NULL, TRUE, 1000, QS_ALLINPUT);
2575
Bram Moolenaar0f873732019-12-05 20:28:46 +01002576 // If the server has died, give up
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 if (!IsWindow(server))
2578 return NULL;
2579
2580 serverProcessPendingMessages();
2581 }
2582 }
2583
2584 return NULL;
2585}
2586
2587/*
2588 * Process any messages in the Windows message queue.
2589 */
2590 void
2591serverProcessPendingMessages(void)
2592{
2593 MSG msg;
2594
K.Takatab7057bd2022-01-21 11:37:07 +00002595 while (PeekMessageW(&msg, NULL, 0, 0, PM_REMOVE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 {
2597 TranslateMessage(&msg);
K.Takatab7057bd2022-01-21 11:37:07 +00002598 DispatchMessageW(&msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 }
2600}
2601
Bram Moolenaar0f873732019-12-05 20:28:46 +01002602#endif // FEAT_CLIENTSERVER
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603
2604#if defined(FEAT_GUI) || (defined(FEAT_PRINTER) && !defined(FEAT_POSTSCRIPT)) \
2605 || defined(PROTO)
2606
2607struct charset_pair
2608{
2609 char *name;
2610 BYTE charset;
2611};
2612
2613static struct charset_pair
2614charset_pairs[] =
2615{
2616 {"ANSI", ANSI_CHARSET},
2617 {"CHINESEBIG5", CHINESEBIG5_CHARSET},
2618 {"DEFAULT", DEFAULT_CHARSET},
2619 {"HANGEUL", HANGEUL_CHARSET},
2620 {"OEM", OEM_CHARSET},
2621 {"SHIFTJIS", SHIFTJIS_CHARSET},
2622 {"SYMBOL", SYMBOL_CHARSET},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 {"ARABIC", ARABIC_CHARSET},
2624 {"BALTIC", BALTIC_CHARSET},
2625 {"EASTEUROPE", EASTEUROPE_CHARSET},
2626 {"GB2312", GB2312_CHARSET},
2627 {"GREEK", GREEK_CHARSET},
2628 {"HEBREW", HEBREW_CHARSET},
2629 {"JOHAB", JOHAB_CHARSET},
2630 {"MAC", MAC_CHARSET},
2631 {"RUSSIAN", RUSSIAN_CHARSET},
2632 {"THAI", THAI_CHARSET},
2633 {"TURKISH", TURKISH_CHARSET},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002634# ifdef VIETNAMESE_CHARSET
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 {"VIETNAMESE", VIETNAMESE_CHARSET},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002636# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 {NULL, 0}
2638};
2639
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002640struct quality_pair
2641{
2642 char *name;
2643 DWORD quality;
2644};
2645
2646static struct quality_pair
2647quality_pairs[] = {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002648# ifdef CLEARTYPE_QUALITY
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002649 {"CLEARTYPE", CLEARTYPE_QUALITY},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002650# endif
2651# ifdef ANTIALIASED_QUALITY
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002652 {"ANTIALIASED", ANTIALIASED_QUALITY},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002653# endif
2654# ifdef NONANTIALIASED_QUALITY
Bram Moolenaar73a733e2016-05-11 21:05:05 +02002655 {"NONANTIALIASED", NONANTIALIASED_QUALITY},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002656# endif
2657# ifdef PROOF_QUALITY
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002658 {"PROOF", PROOF_QUALITY},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002659# endif
2660# ifdef DRAFT_QUALITY
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002661 {"DRAFT", DRAFT_QUALITY},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002662# endif
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002663 {"DEFAULT", DEFAULT_QUALITY},
2664 {NULL, 0}
2665};
2666
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667/*
2668 * Convert a charset ID to a name.
2669 * Return NULL when not recognized.
2670 */
2671 char *
2672charset_id2name(int id)
2673{
2674 struct charset_pair *cp;
2675
2676 for (cp = charset_pairs; cp->name != NULL; ++cp)
2677 if ((BYTE)id == cp->charset)
2678 break;
2679 return cp->name;
2680}
2681
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002682/*
2683 * Convert a quality ID to a name.
2684 * Return NULL when not recognized.
2685 */
2686 char *
2687quality_id2name(DWORD id)
2688{
2689 struct quality_pair *qp;
2690
2691 for (qp = quality_pairs; qp->name != NULL; ++qp)
2692 if (id == qp->quality)
2693 break;
2694 return qp->name;
2695}
2696
Ken Takatad8cb1dd2024-01-12 18:09:43 +01002697// The default font height in 100% scaling (96dpi).
2698// (-12 in 96dpi equates to roughly 9pt)
2699#define DEFAULT_FONT_HEIGHT (-12)
2700
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002701static const LOGFONTW s_lfDefault =
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702{
Ken Takatad8cb1dd2024-01-12 18:09:43 +01002703 DEFAULT_FONT_HEIGHT,
2704 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, DEFAULT_CHARSET,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
2706 PROOF_QUALITY, FIXED_PITCH | FF_DONTCARE,
Ken Takatad8cb1dd2024-01-12 18:09:43 +01002707 L"" // Default font name will be set later based on current language.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708};
2709
Ken Takatad8cb1dd2024-01-12 18:09:43 +01002710// This will be initialized when set_default_logfont() is called first time.
2711// The value will be based on the system DPI.
2712int current_font_height = 0; // also used in gui_w32.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713
Bram Moolenaar0f873732019-12-05 20:28:46 +01002714/*
2715 * Convert a string representing a point size into pixels. The string should
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 * be a positive decimal number, with an optional decimal point (eg, "12", or
2717 * "10.5"). The pixel value is returned, and a pointer to the next unconverted
2718 * character is stored in *end. The flag "vertical" says whether this
2719 * calculation is for a vertical (height) size or a horizontal (width) one.
2720 */
2721 static int
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002722points_to_pixels(WCHAR *str, WCHAR **end, int vertical, long_i pprinter_dc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723{
2724 int pixels;
2725 int points = 0;
2726 int divisor = 0;
2727 HWND hwnd = (HWND)0;
2728 HDC hdc;
2729 HDC printer_dc = (HDC)pprinter_dc;
2730
2731 while (*str != NUL)
2732 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002733 if (*str == L'.' && divisor == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002735 // Start keeping a divisor, for later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 divisor = 1;
2737 }
2738 else
2739 {
2740 if (!VIM_ISDIGIT(*str))
2741 break;
2742
2743 points *= 10;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002744 points += *str - L'0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 divisor *= 10;
2746 }
2747 ++str;
2748 }
2749
2750 if (divisor == 0)
2751 divisor = 1;
2752
2753 if (printer_dc == NULL)
2754 {
2755 hwnd = GetDesktopWindow();
2756 hdc = GetWindowDC(hwnd);
2757 }
2758 else
2759 hdc = printer_dc;
2760
2761 pixels = MulDiv(points,
2762 GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX),
2763 72 * divisor);
2764
2765 if (printer_dc == NULL)
2766 ReleaseDC(hwnd, hdc);
2767
2768 *end = str;
2769 return pixels;
2770}
2771
Yee Cheng Chin290b8872023-10-05 20:54:21 +02002772/*
2773 * Convert pixel into point size. This is a reverse of points_to_pixels.
2774 */
2775 static double
2776pixels_to_points(int pixels, int vertical, long_i pprinter_dc)
2777{
2778 double points = 0;
2779 HWND hwnd = (HWND)0;
2780 HDC hdc;
2781 HDC printer_dc = (HDC)pprinter_dc;
2782
2783 if (printer_dc == NULL)
2784 {
2785 hwnd = GetDesktopWindow();
2786 hdc = GetWindowDC(hwnd);
2787 }
2788 else
2789 hdc = printer_dc;
2790
2791 points = pixels * 72.0 / GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX);
2792 if (printer_dc == NULL)
2793 ReleaseDC(hwnd, hdc);
2794
2795 return points;
2796}
2797
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 static int CALLBACK
2799font_enumproc(
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002800 ENUMLOGFONTW *elf,
2801 NEWTEXTMETRICW *ntm UNUSED,
2802 DWORD type UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 LPARAM lparam)
2804{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002805 // Return value:
2806 // 0 = terminate now (monospace & ANSI)
2807 // 1 = continue, still no luck...
2808 // 2 = continue, but we have an acceptable LOGFONTW
2809 // (monospace, not ANSI)
2810 // We use these values, as EnumFontFamilies returns 1 if the
2811 // callback function is never called. So, we check the return as
2812 // 0 = perfect, 2 = OK, 1 = no good...
2813 // It's not pretty, but it works!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002815 LOGFONTW *lf = (LOGFONTW *)(lparam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002817# ifndef FEAT_PROPORTIONAL_FONTS
Bram Moolenaar0f873732019-12-05 20:28:46 +01002818 // Ignore non-monospace fonts without further ado
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 if ((ntm->tmPitchAndFamily & 1) != 0)
2820 return 1;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002821# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822
Bram Moolenaar0f873732019-12-05 20:28:46 +01002823 // Remember this LOGFONTW as a "possible"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 *lf = elf->elfLogFont;
2825
Bram Moolenaar0f873732019-12-05 20:28:46 +01002826 // Terminate the scan as soon as we find an ANSI font
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 if (lf->lfCharSet == ANSI_CHARSET
2828 || lf->lfCharSet == OEM_CHARSET
2829 || lf->lfCharSet == DEFAULT_CHARSET)
2830 return 0;
2831
Bram Moolenaar0f873732019-12-05 20:28:46 +01002832 // Continue the scan - we have a non-ANSI font
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 return 2;
2834}
2835
2836 static int
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002837init_logfont(LOGFONTW *lf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838{
2839 int n;
2840 HWND hwnd = GetDesktopWindow();
2841 HDC hdc = GetWindowDC(hwnd);
2842
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002843 n = EnumFontFamiliesW(hdc,
2844 lf->lfFaceName,
2845 (FONTENUMPROCW)font_enumproc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846 (LPARAM)lf);
2847
2848 ReleaseDC(hwnd, hdc);
2849
Bram Moolenaar0f873732019-12-05 20:28:46 +01002850 // If we couldn't find a usable font, return failure
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 if (n == 1)
2852 return FAIL;
2853
Bram Moolenaar0f873732019-12-05 20:28:46 +01002854 // Tidy up the rest of the LOGFONTW structure. We set to a basic
2855 // font - get_logfont() sets bold, italic, etc based on the user's
2856 // input.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 lf->lfHeight = current_font_height;
2858 lf->lfWidth = 0;
2859 lf->lfItalic = FALSE;
2860 lf->lfUnderline = FALSE;
2861 lf->lfStrikeOut = FALSE;
2862 lf->lfWeight = FW_NORMAL;
2863
Bram Moolenaar0f873732019-12-05 20:28:46 +01002864 // Return success
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 return OK;
2866}
2867
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002868/*
Yee Cheng Chin290b8872023-10-05 20:54:21 +02002869 * Call back for EnumFontFamiliesW in expand_font_enumproc.
2870 *
2871 */
2872 static int CALLBACK
2873expand_font_enumproc(
2874 ENUMLOGFONTW *elf,
2875 NEWTEXTMETRICW *ntm UNUSED,
2876 DWORD type UNUSED,
2877 LPARAM lparam)
2878{
2879 LOGFONTW *lf = (LOGFONTW*)elf;
2880
2881# ifndef FEAT_PROPORTIONAL_FONTS
2882 // Ignore non-monospace fonts without further ado
2883 if ((ntm->tmPitchAndFamily & 1) != 0)
2884 return 1;
2885# endif
2886
2887 // Filter only on ANSI. Otherwise will see a lot of random fonts that we
2888 // usually don't want.
2889 if (lf->lfCharSet != ANSI_CHARSET)
2890 return 1;
2891
2892 int (*add_match)(char_u *) = (int (*)(char_u *))lparam;
2893
2894 WCHAR *faceNameW = lf->lfFaceName;
2895 char_u *faceName = utf16_to_enc(faceNameW, NULL);
2896 if (!faceName)
2897 return 0;
2898
2899 add_match(faceName);
2900 vim_free(faceName);
2901
2902 return 1;
2903}
2904
2905/*
2906 * Cmdline expansion for setting 'guifont'. Will enumerate through all
2907 * monospace fonts for completion. If used after ':', will expand to possible
2908 * font configuration options like font sizes.
2909 *
2910 * This function has "gui" in its name because in some platforms (GTK) font
2911 * handling is done by the GUI code, whereas in Windows it's part of the
2912 * platform code.
2913 */
2914 void
2915gui_mch_expand_font(optexpand_T *args, void *param UNUSED, int (*add_match)(char_u *val))
2916{
2917 expand_T *xp = args->oe_xp;
2918 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
2919 {
2920 char buf[30];
2921
2922 // Always fill in with the current font size as first option for
2923 // convenience. We simply round to the closest integer for simplicity.
2924 int font_height = (int)round(
2925 pixels_to_points(-current_font_height, TRUE, (long_i)NULL));
2926 vim_snprintf(buf, ARRAY_LENGTH(buf), "h%d", font_height);
2927 add_match((char_u *)buf);
2928
2929 // Note: Keep this in sync with get_logfont(). Don't include 'c' and
2930 // 'q' as we fill in all the values below.
2931 static char *(p_gfn_win_opt_values[]) = {
2932 "h" , "w" , "W" , "b" , "i" , "u" , "s"};
2933 for (size_t i = 0; i < ARRAY_LENGTH(p_gfn_win_opt_values); i++)
2934 add_match((char_u *)p_gfn_win_opt_values[i]);
2935
2936 struct charset_pair *cp;
2937 for (cp = charset_pairs; cp->name != NULL; ++cp)
2938 {
2939 vim_snprintf(buf, ARRAY_LENGTH(buf), "c%s", cp->name);
2940 add_match((char_u *)buf);
2941 }
2942 struct quality_pair *qp;
2943 for (qp = quality_pairs; qp->name != NULL; ++qp)
2944 {
2945 vim_snprintf(buf, ARRAY_LENGTH(buf), "q%s", qp->name);
2946 add_match((char_u *)buf);
2947 }
2948 return;
2949 }
2950
2951 HWND hwnd = GetDesktopWindow();
2952 HDC hdc = GetWindowDC(hwnd);
2953
2954 EnumFontFamiliesW(hdc,
2955 NULL,
2956 (FONTENUMPROCW)expand_font_enumproc,
2957 (LPARAM)add_match);
2958
2959 ReleaseDC(hwnd, hdc);
2960}
2961
2962/*
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002963 * Compare a UTF-16 string and an ASCII string literally.
2964 * Only works all the code points are inside ASCII range.
2965 */
2966 static int
2967utf16ascncmp(const WCHAR *w, const char *p, size_t n)
2968{
2969 size_t i;
2970
2971 for (i = 0; i < n; i++)
2972 {
2973 if (w[i] == 0 || w[i] != p[i])
2974 return w[i] - p[i];
2975 }
2976 return 0;
2977}
2978
2979/*
Ken Takatad8cb1dd2024-01-12 18:09:43 +01002980 * Equivalent of GetDpiForSystem().
2981 */
2982 UINT WINAPI
2983vimGetDpiForSystem(void)
2984{
2985 HWND hwnd = GetDesktopWindow();
2986 HDC hdc = GetWindowDC(hwnd);
2987 UINT dpi = GetDeviceCaps(hdc, LOGPIXELSY);
2988 ReleaseDC(hwnd, hdc);
2989 return dpi;
2990}
2991
2992/*
2993 * Set default logfont based on current language.
2994 */
2995 static void
2996set_default_logfont(LOGFONTW *lf)
2997{
2998 // Default font name for current language on MS-Windows.
2999 // If not translated, falls back to "Consolas".
3000 // This must be a fixed-pitch font.
3001 const char *defaultfontname = N_("DefaultFontNameForWindows");
3002 char *fontname = _(defaultfontname);
3003
3004 if (strcmp(fontname, defaultfontname) == 0)
3005 fontname = "Consolas";
3006
3007 *lf = s_lfDefault;
3008 lf->lfHeight = DEFAULT_FONT_HEIGHT * (int)vimGetDpiForSystem() / 96;
3009 if (current_font_height == 0)
3010 current_font_height = lf->lfHeight;
3011
3012 WCHAR *wfontname = enc_to_utf16((char_u*)fontname, NULL);
3013 if (wfontname != NULL)
3014 {
3015 wcscpy_s(lf->lfFaceName, LF_FACESIZE, wfontname);
3016 vim_free(wfontname);
3017 }
3018}
3019
3020/*
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003021 * Get font info from "name" into logfont "lf".
3022 * Return OK for a valid name, FAIL otherwise.
3023 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024 int
3025get_logfont(
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003026 LOGFONTW *lf,
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003027 char_u *name,
3028 HDC printer_dc,
3029 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003031 WCHAR *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 int i;
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003033 int ret = FAIL;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003034 static LOGFONTW *lastlf = NULL;
3035 WCHAR *wname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036
Ken Takatad8cb1dd2024-01-12 18:09:43 +01003037 set_default_logfont(lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 if (name == NULL)
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003039 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003041 wname = enc_to_utf16(name, NULL);
3042 if (wname == NULL)
3043 return FAIL;
3044
3045 if (wcscmp(wname, L"*") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003047# if defined(FEAT_GUI_MSWIN)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003048 CHOOSEFONTW cf;
Bram Moolenaar0f873732019-12-05 20:28:46 +01003049 // if name is "*", bring up std font dialog:
Bram Moolenaara80faa82020-04-12 19:37:17 +02003050 CLEAR_FIELD(cf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051 cf.lStructSize = sizeof(cf);
3052 cf.hwndOwner = s_hwnd;
3053 cf.Flags = CF_SCREENFONTS | CF_FIXEDPITCHONLY | CF_INITTOLOGFONTSTRUCT;
3054 if (lastlf != NULL)
3055 *lf = *lastlf;
3056 cf.lpLogFont = lf;
3057 cf.nFontType = 0 ; //REGULAR_FONTTYPE;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003058 if (ChooseFontW(&cf))
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003059 ret = OK;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003060# endif
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003061 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062 }
3063
3064 /*
3065 * Split name up, it could be <name>:h<height>:w<width> etc.
3066 */
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003067 for (p = wname; *p && *p != L':'; p++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003069 if (p - wname + 1 >= LF_FACESIZE)
Bram Moolenaar0f873732019-12-05 20:28:46 +01003070 goto theend; // Name too long
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003071 lf->lfFaceName[p - wname] = *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 }
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003073 if (p != wname)
3074 lf->lfFaceName[p - wname] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075
Bram Moolenaar0f873732019-12-05 20:28:46 +01003076 // First set defaults
Ken Takatad8cb1dd2024-01-12 18:09:43 +01003077 lf->lfHeight = DEFAULT_FONT_HEIGHT * (int)vimGetDpiForSystem() / 96;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 lf->lfWidth = 0;
3079 lf->lfWeight = FW_NORMAL;
3080 lf->lfItalic = FALSE;
3081 lf->lfUnderline = FALSE;
3082 lf->lfStrikeOut = FALSE;
3083
3084 /*
3085 * If the font can't be found, try replacing '_' by ' '.
3086 */
3087 if (init_logfont(lf) == FAIL)
3088 {
3089 int did_replace = FALSE;
3090
3091 for (i = 0; lf->lfFaceName[i]; ++i)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003092 if (lf->lfFaceName[i] == L'_')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003094 lf->lfFaceName[i] = L' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095 did_replace = TRUE;
3096 }
3097 if (!did_replace || init_logfont(lf) == FAIL)
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003098 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099 }
3100
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003101 while (*p == L':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 p++;
3103
Bram Moolenaar0f873732019-12-05 20:28:46 +01003104 // Set the values found after ':'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 while (*p)
3106 {
3107 switch (*p++)
3108 {
Yee Cheng Chin290b8872023-10-05 20:54:21 +02003109 // Note: Keep this in sync with gui_mch_expand_font().
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003110 case L'h':
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003111 lf->lfHeight = - points_to_pixels(p, &p, TRUE, (long_i)printer_dc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003113 case L'w':
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003114 lf->lfWidth = points_to_pixels(p, &p, FALSE, (long_i)printer_dc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 break;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003116 case L'W':
3117 lf->lfWeight = wcstol(p, &p, 10);
3118 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003119 case L'b':
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 lf->lfWeight = FW_BOLD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003122 case L'i':
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 lf->lfItalic = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003125 case L'u':
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 lf->lfUnderline = TRUE;
3127 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003128 case L's':
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129 lf->lfStrikeOut = TRUE;
3130 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003131 case L'c':
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 {
3133 struct charset_pair *cp;
3134
3135 for (cp = charset_pairs; cp->name != NULL; ++cp)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003136 if (utf16ascncmp(p, cp->name, strlen(cp->name)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137 {
3138 lf->lfCharSet = cp->charset;
3139 p += strlen(cp->name);
3140 break;
3141 }
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003142 if (cp->name == NULL && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003144 char_u *s = utf16_to_enc(p, NULL);
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00003145
3146 semsg(_(e_illegal_str_name_str_in_font_name_str),
3147 "charset", s, name);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003148 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 break;
3150 }
3151 break;
3152 }
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003153 case L'q':
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003154 {
3155 struct quality_pair *qp;
3156
3157 for (qp = quality_pairs; qp->name != NULL; ++qp)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003158 if (utf16ascncmp(p, qp->name, strlen(qp->name)) == 0)
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003159 {
3160 lf->lfQuality = qp->quality;
3161 p += strlen(qp->name);
3162 break;
3163 }
3164 if (qp->name == NULL && verbose)
3165 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003166 char_u *s = utf16_to_enc(p, NULL);
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00003167 semsg(_(e_illegal_str_name_str_in_font_name_str),
3168 "quality", s, name);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003169 vim_free(s);
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003170 break;
3171 }
3172 break;
3173 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 default:
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003175 if (verbose)
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00003176 semsg(_(e_illegal_char_nr_in_font_name_str), p[-1], name);
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003177 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 }
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003179 while (*p == L':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180 p++;
3181 }
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003182 ret = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184theend:
Bram Moolenaar0f873732019-12-05 20:28:46 +01003185 // ron: init lastlf
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003186 if (ret == OK && printer_dc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 {
3188 vim_free(lastlf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003189 lastlf = ALLOC_ONE(LOGFONTW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 if (lastlf != NULL)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003191 mch_memmove(lastlf, lf, sizeof(LOGFONTW));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 }
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003193 vim_free(wname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003195 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196}
3197
Bram Moolenaar0f873732019-12-05 20:28:46 +01003198#endif // defined(FEAT_GUI) || defined(FEAT_PRINTER)
Bram Moolenaarf12d9832016-01-29 21:11:25 +01003199
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003200#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaarf12d9832016-01-29 21:11:25 +01003201/*
3202 * Initialize the Winsock dll.
3203 */
3204 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003205channel_init_winsock(void)
Bram Moolenaarf12d9832016-01-29 21:11:25 +01003206{
3207 WSADATA wsaData;
3208 int wsaerr;
3209
3210 if (WSInitialized)
3211 return;
3212
3213 wsaerr = WSAStartup(MAKEWORD(2, 2), &wsaData);
3214 if (wsaerr == 0)
3215 WSInitialized = TRUE;
3216}
3217#endif