blob: 485ee20afc084d1b0cf18c9b52012d2ef695e8d1 [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
LemonBoy9987fe82024-07-04 13:20:49 +0200833/*
834 * Fill the buffer 'buf' with 'len' random bytes.
835 * Returns FAIL if the OS PRNG is not available or something went wrong.
836 */
837 int
838mch_get_random(char_u *buf, int len)
839{
840 static int initialized = NOTDONE;
841 static HINSTANCE hInstLib;
842 static BOOL (WINAPI *pProcessPrng)(PUCHAR, ULONG);
843
844 if (initialized == NOTDONE)
845 {
846 hInstLib = vimLoadLib("bcryptprimitives.dll");
847 if (hInstLib != NULL)
848 pProcessPrng = (void *)GetProcAddress(hInstLib, "ProcessPrng");
849 if (hInstLib == NULL || pProcessPrng == NULL)
850 {
851 FreeLibrary(hInstLib);
852 initialized = FAIL;
853 }
854 else
855 initialized = OK;
856 }
857
858 if (initialized == FAIL)
859 return FAIL;
860
861 // According to the documentation this call cannot fail.
862 pProcessPrng(buf, len);
863
864 return OK;
865}
866
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867 int
868mch_libcall(
869 char_u *libname,
870 char_u *funcname,
Bram Moolenaar0f873732019-12-05 20:28:46 +0100871 char_u *argstring, // NULL when using a argint
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 int argint,
Bram Moolenaar0f873732019-12-05 20:28:46 +0100873 char_u **string_result,// NULL when using number_result
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 int *number_result)
875{
876 HINSTANCE hinstLib;
877 MYSTRPROCSTR ProcAdd;
878 MYINTPROCSTR ProcAddI;
879 char_u *retval_str = NULL;
880 int retval_int = 0;
881 size_t len;
882
883 BOOL fRunTimeLinkSuccess = FALSE;
884
885 // Get a handle to the DLL module.
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100886 hinstLib = vimLoadLib((char *)libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887
888 // If the handle is valid, try to get the function address.
889 if (hinstLib != NULL)
890 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100891# ifdef HAVE_TRY_EXCEPT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 __try
893 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100894# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 if (argstring != NULL)
896 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100897 // Call with string argument
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100898 ProcAdd = (MYSTRPROCSTR)GetProcAddress(hinstLib, (LPCSTR)funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 if ((fRunTimeLinkSuccess = (ProcAdd != NULL)) != 0)
900 {
901 if (string_result == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100902 retval_int = ((MYSTRPROCINT)ProcAdd)((LPSTR)argstring);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100904 retval_str = (char_u *)(ProcAdd)((LPSTR)argstring);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 }
906 }
907 else
908 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100909 // Call with number argument
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100910 ProcAddI = (MYINTPROCSTR) GetProcAddress(hinstLib, (LPCSTR)funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 if ((fRunTimeLinkSuccess = (ProcAddI != NULL)) != 0)
912 {
913 if (string_result == NULL)
914 retval_int = ((MYINTPROCINT)ProcAddI)(argint);
915 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100916 retval_str = (char_u *)(ProcAddI)(argint);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 }
918 }
919
920 // Save the string before we free the library.
921 // Assume that a "1" result is an illegal pointer.
922 if (string_result == NULL)
923 *number_result = retval_int;
924 else if (retval_str != NULL
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100925 && (len = check_str_len(retval_str)) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 {
Bram Moolenaar18a4ba22019-05-24 19:39:03 +0200927 *string_result = alloc(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 if (*string_result != NULL)
929 mch_memmove(*string_result, retval_str, len);
930 }
931
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100932# ifdef HAVE_TRY_EXCEPT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 }
934 __except(EXCEPTION_EXECUTE_HANDLER)
935 {
936 if (GetExceptionCode() == EXCEPTION_STACK_OVERFLOW)
K.Takatac351dc12022-01-24 11:24:08 +0000937 _resetstkoflw();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 fRunTimeLinkSuccess = 0;
939 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100940# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941
942 // Free the DLL module.
943 (void)FreeLibrary(hinstLib);
944 }
945
946 if (!fRunTimeLinkSuccess)
947 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000948 semsg(_(e_library_call_failed_for_str), funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 return FAIL;
950 }
951
952 return OK;
953}
954#endif
955
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956/*
957 * Debugging helper: expose the MCH_WRITE_DUMP stuff to other modules
958 */
959 void
Bram Moolenaar1266d672017-02-01 13:43:36 +0100960DumpPutS(const char *psz UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100962#ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963 if (fdDump)
964 {
965 fputs(psz, fdDump);
966 if (psz[strlen(psz) - 1] != '\n')
967 fputc('\n', fdDump);
968 fflush(fdDump);
969 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100970#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971}
972
973#ifdef _DEBUG
974
975void __cdecl
976Trace(
977 char *pszFormat,
978 ...)
979{
980 CHAR szBuff[2048];
981 va_list args;
982
983 va_start(args, pszFormat);
984 vsprintf(szBuff, pszFormat, args);
985 va_end(args);
986
987 OutputDebugString(szBuff);
988}
989
990#endif //_DEBUG
991
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200992#if !defined(FEAT_GUI) || defined(VIMDLL) || defined(PROTO)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100993extern HWND g_hWnd; // This is in os_win32.c.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994
995/*
996 * Showing the printer dialog is tricky since we have no GUI
997 * window to parent it. The following routines are needed to
998 * get the window parenting and Z-order to work properly.
999 */
1000 static void
1001GetConsoleHwnd(void)
1002{
Bram Moolenaar0f873732019-12-05 20:28:46 +01001003 // Skip if it's already set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 if (s_hwnd != 0)
1005 return;
1006
Bram Moolenaar0f873732019-12-05 20:28:46 +01001007 // Window handle may have been found by init code (Windows NT only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 if (g_hWnd != 0)
1009 {
1010 s_hwnd = g_hWnd;
1011 return;
1012 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013
Bram Moolenaare1ed53f2019-02-12 23:12:37 +01001014 s_hwnd = GetConsoleWindow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015}
Bram Moolenaar843ee412004-06-30 16:16:41 +00001016
1017/*
1018 * Console implementation of ":winpos".
1019 */
1020 int
1021mch_get_winpos(int *x, int *y)
1022{
1023 RECT rect;
1024
1025 GetConsoleHwnd();
1026 GetWindowRect(s_hwnd, &rect);
1027 *x = rect.left;
1028 *y = rect.top;
1029 return OK;
1030}
1031
1032/*
1033 * Console implementation of ":winpos x y".
1034 */
1035 void
1036mch_set_winpos(int x, int y)
1037{
1038 GetConsoleHwnd();
1039 SetWindowPos(s_hwnd, NULL, x, y, 0, 0,
1040 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
1041}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042#endif
1043
1044#if (defined(FEAT_PRINTER) && !defined(FEAT_POSTSCRIPT)) || defined(PROTO)
1045
Bram Moolenaar0f873732019-12-05 20:28:46 +01001046//=================================================================
1047// Win32 printer stuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048
1049static HFONT prt_font_handles[2][2][2];
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001050static PRINTDLGW prt_dlg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051static const int boldface[2] = {FW_REGULAR, FW_BOLD};
1052static TEXTMETRIC prt_tm;
1053static int prt_line_height;
1054static int prt_number_width;
1055static int prt_left_margin;
1056static int prt_right_margin;
1057static int prt_top_margin;
1058static char_u szAppName[] = TEXT("VIM");
1059static HWND hDlgPrint;
1060static int *bUserAbort = NULL;
1061static char_u *prt_name = NULL;
1062
Bram Moolenaar0f873732019-12-05 20:28:46 +01001063// Defines which are also in vim.rc.
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001064# define IDC_BOX1 400
1065# define IDC_PRINTTEXT1 401
1066# define IDC_PRINTTEXT2 402
1067# define IDC_PROGRESS 403
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001069 static BOOL
1070vimSetDlgItemText(HWND hDlg, int nIDDlgItem, char_u *s)
1071{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001072 WCHAR *wp;
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001073 BOOL ret;
1074
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001075 wp = enc_to_utf16(s, NULL);
1076 if (wp == NULL)
1077 return FALSE;
1078
1079 ret = SetDlgItemTextW(hDlg, nIDDlgItem, wp);
1080 vim_free(wp);
1081 return ret;
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001082}
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001083
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084/*
1085 * Convert BGR to RGB for Windows GDI calls
1086 */
1087 static COLORREF
1088swap_me(COLORREF colorref)
1089{
1090 int temp;
1091 char *ptr = (char *)&colorref;
1092
1093 temp = *(ptr);
1094 *(ptr ) = *(ptr + 2);
1095 *(ptr + 2) = temp;
1096 return colorref;
1097}
1098
K.Takatac351dc12022-01-24 11:24:08 +00001099 static INT_PTR CALLBACK
Bram Moolenaar1266d672017-02-01 13:43:36 +01001100PrintDlgProc(
1101 HWND hDlg,
1102 UINT message,
1103 WPARAM wParam UNUSED,
1104 LPARAM lParam UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001106# ifdef FEAT_GETTEXT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 NONCLIENTMETRICS nm;
1108 static HFONT hfont;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001109# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110
1111 switch (message)
1112 {
1113 case WM_INITDIALOG:
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001114# ifdef FEAT_GETTEXT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 nm.cbSize = sizeof(NONCLIENTMETRICS);
1116 if (SystemParametersInfo(
1117 SPI_GETNONCLIENTMETRICS,
1118 sizeof(NONCLIENTMETRICS),
1119 &nm,
1120 0))
1121 {
1122 char buff[MAX_PATH];
1123 int i;
1124
Bram Moolenaar0f873732019-12-05 20:28:46 +01001125 // Translate the dialog texts
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 hfont = CreateFontIndirect(&nm.lfMessageFont);
1127 for (i = IDC_PRINTTEXT1; i <= IDC_PROGRESS; i++)
1128 {
1129 SendDlgItemMessage(hDlg, i, WM_SETFONT, (WPARAM)hfont, 1);
1130 if (GetDlgItemText(hDlg,i, buff, sizeof(buff)))
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001131 vimSetDlgItemText(hDlg,i, (char_u *)_(buff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 }
1133 SendDlgItemMessage(hDlg, IDCANCEL,
1134 WM_SETFONT, (WPARAM)hfont, 1);
1135 if (GetDlgItemText(hDlg,IDCANCEL, buff, sizeof(buff)))
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001136 vimSetDlgItemText(hDlg,IDCANCEL, (char_u *)_(buff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001138# endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001139 SetWindowText(hDlg, (LPCSTR)szAppName);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 if (prt_name != NULL)
1141 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001142 vimSetDlgItemText(hDlg, IDC_PRINTTEXT2, (char_u *)prt_name);
Bram Moolenaard23a8232018-02-10 18:45:26 +01001143 VIM_CLEAR(prt_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 }
1145 EnableMenuItem(GetSystemMenu(hDlg, FALSE), SC_CLOSE, MF_GRAYED);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001146# if !defined(FEAT_GUI) || defined(VIMDLL)
1147# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001148 if (!gui.in_use)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001149# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001150 BringWindowToTop(s_hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001151# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 return TRUE;
1153
1154 case WM_COMMAND:
1155 *bUserAbort = TRUE;
1156 EnableWindow(GetParent(hDlg), TRUE);
1157 DestroyWindow(hDlg);
1158 hDlgPrint = NULL;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001159# ifdef FEAT_GETTEXT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 DeleteObject(hfont);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001161# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 return TRUE;
1163 }
1164 return FALSE;
1165}
1166
1167 static BOOL CALLBACK
Bram Moolenaar1266d672017-02-01 13:43:36 +01001168AbortProc(HDC hdcPrn UNUSED, int iCode UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169{
1170 MSG msg;
1171
K.Takatab7057bd2022-01-21 11:37:07 +00001172 while (!*bUserAbort && PeekMessageW(&msg, NULL, 0, 0, PM_REMOVE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 {
K.Takatab7057bd2022-01-21 11:37:07 +00001174 if (!hDlgPrint || !IsDialogMessageW(hDlgPrint, &msg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 {
1176 TranslateMessage(&msg);
K.Takatab7057bd2022-01-21 11:37:07 +00001177 DispatchMessageW(&msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 }
1179 }
1180 return !*bUserAbort;
1181}
1182
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001183# if !defined(FEAT_GUI) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184
Bram Moolenaar26fdd7d2011-11-30 13:42:44 +01001185 static UINT_PTR CALLBACK
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186PrintHookProc(
1187 HWND hDlg, // handle to dialog box
1188 UINT uiMsg, // message identifier
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02001189 WPARAM wParam UNUSED, // message parameter
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 LPARAM lParam // message parameter
1191 )
1192{
1193 HWND hwndOwner;
1194 RECT rc, rcDlg, rcOwner;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001195 PRINTDLGW *pPD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001197 if (uiMsg != WM_INITDIALOG)
1198 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001200 // Get the owner window and dialog box rectangles.
1201 if ((hwndOwner = GetParent(hDlg)) == NULL)
1202 hwndOwner = GetDesktopWindow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001204 GetWindowRect(hwndOwner, &rcOwner);
1205 GetWindowRect(hDlg, &rcDlg);
1206 CopyRect(&rc, &rcOwner);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001208 // Offset the owner and dialog box rectangles so that
1209 // right and bottom values represent the width and
1210 // height, and then offset the owner again to discard
1211 // space taken up by the dialog box.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001213 OffsetRect(&rcDlg, -rcDlg.left, -rcDlg.top);
1214 OffsetRect(&rc, -rc.left, -rc.top);
1215 OffsetRect(&rc, -rcDlg.right, -rcDlg.bottom);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001217 // The new position is the sum of half the remaining
1218 // space and the owner's original position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001220 SetWindowPos(hDlg,
1221 HWND_TOP,
1222 rcOwner.left + (rc.right / 2),
1223 rcOwner.top + (rc.bottom / 2),
1224 0, 0, // ignores size arguments
1225 SWP_NOSIZE);
1226
1227 // tackle the printdlg copiesctrl problem
1228 pPD = (PRINTDLGW *)lParam;
1229 pPD->nCopies = (WORD)pPD->lCustData;
1230 SetDlgItemInt( hDlg, edt3, pPD->nCopies, FALSE );
1231 // Bring the window to top
1232 BringWindowToTop(GetParent(hDlg));
1233 SetForegroundWindow(hDlg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234
1235 return FALSE;
1236}
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001237# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238
1239 void
1240mch_print_cleanup(void)
1241{
1242 int pifItalic;
1243 int pifBold;
1244 int pifUnderline;
1245
1246 for (pifBold = 0; pifBold <= 1; pifBold++)
1247 for (pifItalic = 0; pifItalic <= 1; pifItalic++)
1248 for (pifUnderline = 0; pifUnderline <= 1; pifUnderline++)
1249 DeleteObject(prt_font_handles[pifBold][pifItalic][pifUnderline]);
1250
1251 if (prt_dlg.hDC != NULL)
1252 DeleteDC(prt_dlg.hDC);
1253 if (!*bUserAbort)
1254 SendMessage(hDlgPrint, WM_COMMAND, 0, 0);
1255}
1256
1257 static int
1258to_device_units(int idx, int dpi, int physsize, int offset, int def_number)
1259{
1260 int ret = 0;
1261 int u;
1262 int nr;
1263
1264 u = prt_get_unit(idx);
1265 if (u == PRT_UNIT_NONE)
1266 {
1267 u = PRT_UNIT_PERC;
1268 nr = def_number;
1269 }
1270 else
1271 nr = printer_opts[idx].number;
1272
1273 switch (u)
1274 {
1275 case PRT_UNIT_PERC:
1276 ret = (physsize * nr) / 100;
1277 break;
1278 case PRT_UNIT_INCH:
1279 ret = (nr * dpi);
1280 break;
1281 case PRT_UNIT_MM:
1282 ret = (nr * 10 * dpi) / 254;
1283 break;
1284 case PRT_UNIT_POINT:
1285 ret = (nr * 10 * dpi) / 720;
1286 break;
1287 }
1288
1289 if (ret < offset)
1290 return 0;
1291 else
1292 return ret - offset;
1293}
1294
1295 static int
1296prt_get_cpl(void)
1297{
1298 int hr;
1299 int phyw;
1300 int dvoff;
1301 int rev_offset;
1302 int dpi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303
1304 GetTextMetrics(prt_dlg.hDC, &prt_tm);
1305 prt_line_height = prt_tm.tmHeight + prt_tm.tmExternalLeading;
1306
1307 hr = GetDeviceCaps(prt_dlg.hDC, HORZRES);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 phyw = GetDeviceCaps(prt_dlg.hDC, PHYSICALWIDTH);
1309 dvoff = GetDeviceCaps(prt_dlg.hDC, PHYSICALOFFSETX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 dpi = GetDeviceCaps(prt_dlg.hDC, LOGPIXELSX);
1311
1312 rev_offset = phyw - (dvoff + hr);
1313
1314 prt_left_margin = to_device_units(OPT_PRINT_LEFT, dpi, phyw, dvoff, 10);
1315 if (prt_use_number())
1316 {
1317 prt_number_width = PRINT_NUMBER_WIDTH * prt_tm.tmAveCharWidth;
1318 prt_left_margin += prt_number_width;
1319 }
1320 else
1321 prt_number_width = 0;
1322
1323 prt_right_margin = hr - to_device_units(OPT_PRINT_RIGHT, dpi, phyw,
1324 rev_offset, 5);
1325
1326 return (prt_right_margin - prt_left_margin) / prt_tm.tmAveCharWidth;
1327}
1328
1329 static int
1330prt_get_lpp(void)
1331{
1332 int vr;
1333 int phyw;
1334 int dvoff;
1335 int rev_offset;
1336 int bottom_margin;
1337 int dpi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338
1339 vr = GetDeviceCaps(prt_dlg.hDC, VERTRES);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 phyw = GetDeviceCaps(prt_dlg.hDC, PHYSICALHEIGHT);
1341 dvoff = GetDeviceCaps(prt_dlg.hDC, PHYSICALOFFSETY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 dpi = GetDeviceCaps(prt_dlg.hDC, LOGPIXELSY);
1343
1344 rev_offset = phyw - (dvoff + vr);
1345
1346 prt_top_margin = to_device_units(OPT_PRINT_TOP, dpi, phyw, dvoff, 5);
1347
Bram Moolenaar0f873732019-12-05 20:28:46 +01001348 // adjust top margin if there is a header
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 prt_top_margin += prt_line_height * prt_header_height();
1350
1351 bottom_margin = vr - to_device_units(OPT_PRINT_BOT, dpi, phyw,
1352 rev_offset, 5);
1353
1354 return (bottom_margin - prt_top_margin) / prt_line_height;
1355}
1356
1357 int
1358mch_print_init(prt_settings_T *psettings, char_u *jobname, int forceit)
1359{
1360 static HGLOBAL stored_dm = NULL;
1361 static HGLOBAL stored_devn = NULL;
1362 static int stored_nCopies = 1;
1363 static int stored_nFlags = 0;
1364
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001365 LOGFONTW fLogFont;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 int pifItalic;
1367 int pifBold;
1368 int pifUnderline;
1369
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001370 DEVMODEW *mem;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 DEVNAMES *devname;
1372 int i;
Bram Moolenaarb391e1f2023-01-16 19:51:03 +00001373 DWORD err;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374
1375 bUserAbort = &(psettings->user_abort);
Bram Moolenaara80faa82020-04-12 19:37:17 +02001376 CLEAR_FIELD(prt_dlg);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001377 prt_dlg.lStructSize = sizeof(PRINTDLGW);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001378# if !defined(FEAT_GUI) || defined(VIMDLL)
1379# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001380 if (!gui.in_use)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001381# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01001382 GetConsoleHwnd(); // get value of s_hwnd
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001383# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 prt_dlg.hwndOwner = s_hwnd;
1385 prt_dlg.Flags = PD_NOPAGENUMS | PD_NOSELECTION | PD_RETURNDC;
1386 if (!forceit)
1387 {
1388 prt_dlg.hDevMode = stored_dm;
1389 prt_dlg.hDevNames = stored_devn;
1390 prt_dlg.lCustData = stored_nCopies; // work around bug in print dialog
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001391# if !defined(FEAT_GUI) || defined(VIMDLL)
1392# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001393 if (!gui.in_use)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001394# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001395 {
1396 /*
1397 * Use hook to prevent console window being sent to back
1398 */
1399 prt_dlg.lpfnPrintHook = PrintHookProc;
1400 prt_dlg.Flags |= PD_ENABLEPRINTHOOK;
1401 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001402# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 prt_dlg.Flags |= stored_nFlags;
1404 }
1405
1406 /*
1407 * If bang present, return default printer setup with no dialog
1408 * never show dialog if we are running over telnet
1409 */
1410 if (forceit
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001411# if !defined(FEAT_GUI) || defined(VIMDLL)
1412# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001413 || (!gui.in_use && !term_console)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001414# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 || !term_console
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001416# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001417# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 )
1419 {
1420 prt_dlg.Flags |= PD_RETURNDEFAULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 /*
1422 * MSDN suggests setting the first parameter to WINSPOOL for
1423 * NT, but NULL appears to work just as well.
1424 */
1425 if (*p_pdev != NUL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001426 prt_dlg.hDC = CreateDC(NULL, (LPCSTR)p_pdev, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 {
1429 prt_dlg.Flags |= PD_RETURNDEFAULT;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001430 if (PrintDlgW(&prt_dlg) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431 goto init_fail_dlg;
1432 }
1433 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001434 else if (PrintDlgW(&prt_dlg) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435 goto init_fail_dlg;
1436 else
1437 {
1438 /*
1439 * keep the previous driver context
1440 */
1441 stored_dm = prt_dlg.hDevMode;
1442 stored_devn = prt_dlg.hDevNames;
1443 stored_nFlags = prt_dlg.Flags;
1444 stored_nCopies = prt_dlg.nCopies;
1445 }
1446
1447 if (prt_dlg.hDC == NULL)
1448 {
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00001449 emsg(_(e_printer_selection_failed));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450 mch_print_cleanup();
1451 return FALSE;
1452 }
1453
Bram Moolenaar0f873732019-12-05 20:28:46 +01001454 // Not all printer drivers report the support of color (or grey) in the
1455 // same way. Let's set has_color if there appears to be some way to print
1456 // more than B&W.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 i = GetDeviceCaps(prt_dlg.hDC, NUMCOLORS);
1458 psettings->has_color = (GetDeviceCaps(prt_dlg.hDC, BITSPIXEL) > 1
1459 || GetDeviceCaps(prt_dlg.hDC, PLANES) > 1
1460 || i > 2 || i == -1);
1461
Bram Moolenaar0f873732019-12-05 20:28:46 +01001462 // Ensure all font styles are baseline aligned
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 SetTextAlign(prt_dlg.hDC, TA_BASELINE|TA_LEFT);
1464
1465 /*
1466 * On some windows systems the nCopies parameter is not
1467 * passed back correctly. It must be retrieved from the
1468 * hDevMode struct.
1469 */
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001470 mem = (DEVMODEW *)GlobalLock(prt_dlg.hDevMode);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 if (mem != NULL)
1472 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 if (mem->dmCopies != 1)
1474 stored_nCopies = mem->dmCopies;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 if ((mem->dmFields & DM_DUPLEX) && (mem->dmDuplex & ~DMDUP_SIMPLEX))
1476 psettings->duplex = TRUE;
1477 if ((mem->dmFields & DM_COLOR) && (mem->dmColor & DMCOLOR_COLOR))
1478 psettings->has_color = TRUE;
1479 }
1480 GlobalUnlock(prt_dlg.hDevMode);
1481
1482 devname = (DEVNAMES *)GlobalLock(prt_dlg.hDevNames);
1483 if (devname != 0)
1484 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001485 WCHAR *wprinter_name = (WCHAR *)devname + devname->wDeviceOffset;
1486 WCHAR *wport_name = (WCHAR *)devname + devname->wOutputOffset;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001487 char_u *text = (char_u *)_("to %s on %s");
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001488 char_u *printer_name = utf16_to_enc(wprinter_name, NULL);
1489 char_u *port_name = utf16_to_enc(wport_name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001491 if (printer_name != NULL && port_name != NULL)
Bram Moolenaar964b3742019-05-24 18:54:09 +02001492 prt_name = alloc(STRLEN(printer_name)
1493 + STRLEN(port_name) + STRLEN(text));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494 if (prt_name != NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001495 wsprintf((char *)prt_name, (const char *)text,
1496 printer_name, port_name);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001497 vim_free(printer_name);
1498 vim_free(port_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 }
1500 GlobalUnlock(prt_dlg.hDevNames);
1501
1502 /*
1503 * Initialise the font according to 'printfont'
1504 */
Bram Moolenaara80faa82020-04-12 19:37:17 +02001505 CLEAR_FIELD(fLogFont);
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001506 if (get_logfont(&fLogFont, p_pfn, prt_dlg.hDC, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 {
Bram Moolenaard88be5b2022-01-04 19:57:55 +00001508 semsg(_(e_unknown_printer_font_str), p_pfn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 mch_print_cleanup();
1510 return FALSE;
1511 }
1512
1513 for (pifBold = 0; pifBold <= 1; pifBold++)
1514 for (pifItalic = 0; pifItalic <= 1; pifItalic++)
1515 for (pifUnderline = 0; pifUnderline <= 1; pifUnderline++)
1516 {
1517 fLogFont.lfWeight = boldface[pifBold];
1518 fLogFont.lfItalic = pifItalic;
1519 fLogFont.lfUnderline = pifUnderline;
1520 prt_font_handles[pifBold][pifItalic][pifUnderline]
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001521 = CreateFontIndirectW(&fLogFont);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522 }
1523
1524 SetBkMode(prt_dlg.hDC, OPAQUE);
1525 SelectObject(prt_dlg.hDC, prt_font_handles[0][0][0]);
1526
1527 /*
1528 * Fill in the settings struct
1529 */
1530 psettings->chars_per_line = prt_get_cpl();
1531 psettings->lines_per_page = prt_get_lpp();
Bram Moolenaar7ddc6422014-09-27 11:18:19 +02001532 if (prt_dlg.Flags & PD_USEDEVMODECOPIESANDCOLLATE)
1533 {
1534 psettings->n_collated_copies = (prt_dlg.Flags & PD_COLLATE)
1535 ? prt_dlg.nCopies : 1;
1536 psettings->n_uncollated_copies = (prt_dlg.Flags & PD_COLLATE)
1537 ? 1 : prt_dlg.nCopies;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538
Bram Moolenaar7ddc6422014-09-27 11:18:19 +02001539 if (psettings->n_collated_copies == 0)
1540 psettings->n_collated_copies = 1;
1541
1542 if (psettings->n_uncollated_copies == 0)
1543 psettings->n_uncollated_copies = 1;
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01001544 }
1545 else
1546 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 psettings->n_collated_copies = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 psettings->n_uncollated_copies = 1;
Bram Moolenaar7ddc6422014-09-27 11:18:19 +02001549 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550
1551 psettings->jobname = jobname;
1552
1553 return TRUE;
1554
1555init_fail_dlg:
Bram Moolenaarb391e1f2023-01-16 19:51:03 +00001556 err = CommDlgExtendedError();
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001557 if (err)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001559 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001561 // I suspect FormatMessage() doesn't work for values returned by
1562 // CommDlgExtendedError(). What does?
1563 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
1564 FORMAT_MESSAGE_FROM_SYSTEM |
1565 FORMAT_MESSAGE_IGNORE_INSERTS,
1566 NULL, err, 0, (LPTSTR)(&buf), 0, NULL);
1567 semsg(_(e_print_error_str),
1568 buf == NULL ? (char_u *)_("Unknown") : buf);
1569 LocalFree((LPVOID)(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001571 else
1572 msg_clr_eos(); // Maybe canceled
1573
1574 mch_print_cleanup();
1575 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576}
1577
1578
1579 int
1580mch_print_begin(prt_settings_T *psettings)
1581{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001582 int ret = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 char szBuffer[300];
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001584 WCHAR *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001586 hDlgPrint = CreateDialog(g_hinst, TEXT("PrintDlgBox"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 prt_dlg.hwndOwner, PrintDlgProc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588 SetAbortProc(prt_dlg.hDC, AbortProc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 wsprintf(szBuffer, _("Printing '%s'"), gettail(psettings->jobname));
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001590 vimSetDlgItemText(hDlgPrint, IDC_PRINTTEXT1, (char_u *)szBuffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001592 wp = enc_to_utf16(psettings->jobname, NULL);
Bram Moolenaar2290b1f2018-05-13 17:30:45 +02001593 if (wp != NULL)
1594 {
1595 DOCINFOW di;
1596
Bram Moolenaara80faa82020-04-12 19:37:17 +02001597 CLEAR_FIELD(di);
Bram Moolenaar2290b1f2018-05-13 17:30:45 +02001598 di.cbSize = sizeof(di);
1599 di.lpszDocName = wp;
1600 ret = StartDocW(prt_dlg.hDC, &di);
1601 vim_free(wp);
1602 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001604# ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01001605 // Give focus back to main window (when using MDI).
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001606# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001607 if (gui.in_use)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001608# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001609 SetFocus(s_hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001610# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611
1612 return (ret > 0);
1613}
1614
1615 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01001616mch_print_end(prt_settings_T *psettings UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617{
1618 EndDoc(prt_dlg.hDC);
1619 if (!*bUserAbort)
1620 SendMessage(hDlgPrint, WM_COMMAND, 0, 0);
1621}
1622
1623 int
1624mch_print_end_page(void)
1625{
1626 return (EndPage(prt_dlg.hDC) > 0);
1627}
1628
1629 int
1630mch_print_begin_page(char_u *msg)
1631{
1632 if (msg != NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001633 vimSetDlgItemText(hDlgPrint, IDC_PROGRESS, msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 return (StartPage(prt_dlg.hDC) > 0);
1635}
1636
1637 int
1638mch_print_blank_page(void)
1639{
1640 return (mch_print_begin_page(NULL) ? (mch_print_end_page()) : FALSE);
1641}
1642
1643static int prt_pos_x = 0;
1644static int prt_pos_y = 0;
1645
1646 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001647mch_print_start_line(int margin, int page_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648{
1649 if (margin)
1650 prt_pos_x = -prt_number_width;
1651 else
1652 prt_pos_x = 0;
1653 prt_pos_y = page_line * prt_line_height
1654 + prt_tm.tmAscent + prt_tm.tmExternalLeading;
1655}
1656
1657 int
1658mch_print_text_out(char_u *p, int len)
1659{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 SIZE sz;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001661 WCHAR *wp;
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001662 int wlen = len;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001663 int ret = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001665 wp = enc_to_utf16(p, &wlen);
1666 if (wp == NULL)
1667 return FALSE;
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001668
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001669 TextOutW(prt_dlg.hDC, prt_pos_x + prt_left_margin,
1670 prt_pos_y + prt_top_margin, wp, wlen);
1671 GetTextExtentPoint32W(prt_dlg.hDC, wp, wlen, &sz);
1672 vim_free(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 prt_pos_x += (sz.cx - prt_tm.tmOverhang);
Bram Moolenaar0f873732019-12-05 20:28:46 +01001674 // This is wrong when printing spaces for a TAB.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001675 if (p[len] != NUL)
1676 {
Bram Moolenaar1614a142019-10-06 22:00:13 +02001677 wlen = mb_ptr2len(p + len);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001678 wp = enc_to_utf16(p + len, &wlen);
1679 if (wp != NULL)
1680 {
1681 GetTextExtentPoint32W(prt_dlg.hDC, wp, 1, &sz);
1682 ret = (prt_pos_x + prt_left_margin + sz.cx > prt_right_margin);
1683 vim_free(wp);
1684 }
1685 }
1686 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687}
1688
1689 void
1690mch_print_set_font(int iBold, int iItalic, int iUnderline)
1691{
1692 SelectObject(prt_dlg.hDC, prt_font_handles[iBold][iItalic][iUnderline]);
1693}
1694
1695 void
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001696mch_print_set_bg(long_u bgcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697{
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001698 SetBkColor(prt_dlg.hDC, GetNearestColor(prt_dlg.hDC,
1699 swap_me((COLORREF)bgcol)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 /*
1701 * With a white background we can draw characters transparent, which is
1702 * good for italic characters that overlap to the next char cell.
1703 */
1704 if (bgcol == 0xffffffUL)
1705 SetBkMode(prt_dlg.hDC, TRANSPARENT);
1706 else
1707 SetBkMode(prt_dlg.hDC, OPAQUE);
1708}
1709
1710 void
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001711mch_print_set_fg(long_u fgcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712{
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001713 SetTextColor(prt_dlg.hDC, GetNearestColor(prt_dlg.hDC,
1714 swap_me((COLORREF)fgcol)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715}
1716
Bram Moolenaar0f873732019-12-05 20:28:46 +01001717#endif // FEAT_PRINTER && !FEAT_POSTSCRIPT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718
Bram Moolenaar58d98232005-07-23 22:25:46 +00001719
1720
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721#if defined(FEAT_SHORTCUT) || defined(PROTO)
Bram Moolenaar82881492012-11-20 16:53:39 +01001722# ifndef PROTO
1723# include <shlobj.h>
1724# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725
Bram Moolenaar4a792c82019-06-06 12:22:41 +02001726# define is_path_sep(c) ((c) == L'\\' || (c) == L'/')
1727
1728 static int
1729is_reparse_point_included(LPCWSTR fname)
1730{
1731 LPCWSTR p = fname, q;
1732 WCHAR buf[MAX_PATH];
1733 DWORD attr;
1734
Keith Thompson184f71c2024-01-04 21:19:04 +01001735 if (SAFE_isalpha(p[0]) && p[1] == L':' && is_path_sep(p[2]))
Bram Moolenaar4a792c82019-06-06 12:22:41 +02001736 p += 3;
1737 else if (is_path_sep(p[0]) && is_path_sep(p[1]))
1738 p += 2;
1739
1740 while (*p != L'\0')
1741 {
1742 q = wcspbrk(p, L"\\/");
1743 if (q == NULL)
1744 p = q = fname + wcslen(fname);
1745 else
1746 p = q + 1;
1747 if (q - fname >= MAX_PATH)
1748 return FALSE;
1749 wcsncpy(buf, fname, q - fname);
1750 buf[q - fname] = L'\0';
1751 attr = GetFileAttributesW(buf);
1752 if (attr != INVALID_FILE_ATTRIBUTES
1753 && (attr & FILE_ATTRIBUTE_REPARSE_POINT) != 0)
1754 return TRUE;
1755 }
1756 return FALSE;
1757}
1758
AmberArrf5d0f542023-08-20 20:03:45 +02001759/*
1760 * Return the resolved file path, NULL if "fname" is an AppExecLink reparse
1761 * point, already fully resolved, or it doesn't exists.
1762 */
1763 char_u *
Bram Moolenaardce1e892019-02-10 23:18:53 +01001764resolve_reparse_point(char_u *fname)
1765{
1766 HANDLE h = INVALID_HANDLE_VALUE;
1767 DWORD size;
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001768 WCHAR *p, *wp;
Bram Moolenaardce1e892019-02-10 23:18:53 +01001769 char_u *rfname = NULL;
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001770 WCHAR *buff = NULL;
Bram Moolenaardce1e892019-02-10 23:18:53 +01001771
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001772 p = enc_to_utf16(fname, NULL);
1773 if (p == NULL)
1774 goto fail;
1775
Bram Moolenaar4a792c82019-06-06 12:22:41 +02001776 if (!is_reparse_point_included(p))
1777 {
1778 vim_free(p);
1779 goto fail;
1780 }
1781
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001782 h = CreateFileW(p, 0, 0, NULL, OPEN_EXISTING,
1783 FILE_FLAG_BACKUP_SEMANTICS, NULL);
1784 vim_free(p);
Bram Moolenaardce1e892019-02-10 23:18:53 +01001785
1786 if (h == INVALID_HANDLE_VALUE)
1787 goto fail;
1788
K.Takata27b53be2022-09-18 12:25:49 +01001789 size = GetFinalPathNameByHandleW(h, NULL, 0, 0);
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001790 if (size == 0)
1791 goto fail;
1792 buff = ALLOC_MULT(WCHAR, size);
1793 if (buff == NULL)
1794 goto fail;
K.Takata27b53be2022-09-18 12:25:49 +01001795 if (GetFinalPathNameByHandleW(h, buff, size, 0) == 0)
Bram Moolenaardce1e892019-02-10 23:18:53 +01001796 goto fail;
1797
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001798 if (wcsncmp(buff, L"\\\\?\\UNC\\", 8) == 0)
1799 {
1800 buff[6] = L'\\';
1801 wp = buff + 6;
1802 }
1803 else if (wcsncmp(buff, L"\\\\?\\", 4) == 0)
1804 wp = buff + 4;
Bram Moolenaardce1e892019-02-10 23:18:53 +01001805 else
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001806 wp = buff;
1807
1808 rfname = utf16_to_enc(wp, NULL);
Bram Moolenaardce1e892019-02-10 23:18:53 +01001809
1810fail:
1811 if (h != INVALID_HANDLE_VALUE)
1812 CloseHandle(h);
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001813 if (buff != NULL)
1814 vim_free(buff);
Bram Moolenaardce1e892019-02-10 23:18:53 +01001815
1816 return rfname;
1817}
1818
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819/*
1820 * When "fname" is the name of a shortcut (*.lnk) resolve the file it points
1821 * to and return that name in allocated memory.
1822 * Otherwise NULL is returned.
1823 */
Bram Moolenaardce1e892019-02-10 23:18:53 +01001824 static char_u *
1825resolve_shortcut(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826{
1827 HRESULT hr;
1828 IShellLink *psl = NULL;
1829 IPersistFile *ppf = NULL;
1830 OLECHAR wsz[MAX_PATH];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 char_u *rfname = NULL;
1832 int len;
Bram Moolenaar604729e2013-08-30 16:44:19 +02001833 IShellLinkW *pslw = NULL;
1834 WIN32_FIND_DATAW ffdw; // we get those free of charge
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835
Bram Moolenaar0f873732019-12-05 20:28:46 +01001836 // Check if the file name ends in ".lnk". Avoid calling
1837 // CoCreateInstance(), it's quite slow.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 if (fname == NULL)
1839 return rfname;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001840 len = (int)STRLEN(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 if (len <= 4 || STRNICMP(fname + len - 4, ".lnk", 4) != 0)
1842 return rfname;
1843
1844 CoInitialize(NULL);
1845
1846 // create a link manager object and request its interface
1847 hr = CoCreateInstance(
1848 &CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001849 &IID_IShellLinkW, (void**)&pslw);
1850 if (hr == S_OK)
1851 {
1852 WCHAR *p = enc_to_utf16(fname, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001854 if (p != NULL)
1855 {
1856 // Get a pointer to the IPersistFile interface.
1857 hr = pslw->lpVtbl->QueryInterface(
1858 pslw, &IID_IPersistFile, (void**)&ppf);
1859 if (hr != S_OK)
1860 goto shortcut_errorw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001862 // "load" the name and resolve the link
1863 hr = ppf->lpVtbl->Load(ppf, p, STGM_READ);
1864 if (hr != S_OK)
1865 goto shortcut_errorw;
1866# if 0 // This makes Vim wait a long time if the target does not exist.
1867 hr = pslw->lpVtbl->Resolve(pslw, NULL, SLR_NO_UI);
1868 if (hr != S_OK)
1869 goto shortcut_errorw;
Bram Moolenaar604729e2013-08-30 16:44:19 +02001870# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001872 // Get the path to the link target.
1873 ZeroMemory(wsz, MAX_PATH * sizeof(WCHAR));
1874 hr = pslw->lpVtbl->GetPath(pslw, wsz, MAX_PATH, &ffdw, 0);
1875 if (hr == S_OK && wsz[0] != NUL)
1876 rfname = utf16_to_enc(wsz, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001878shortcut_errorw:
1879 vim_free(p);
1880 }
1881 }
1882
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 // Release all interface pointers (both belong to the same object)
1884 if (ppf != NULL)
1885 ppf->lpVtbl->Release(ppf);
1886 if (psl != NULL)
1887 psl->lpVtbl->Release(psl);
Bram Moolenaar604729e2013-08-30 16:44:19 +02001888 if (pslw != NULL)
1889 pslw->lpVtbl->Release(pslw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890
1891 CoUninitialize();
1892 return rfname;
1893}
Bram Moolenaardce1e892019-02-10 23:18:53 +01001894
1895 char_u *
1896mch_resolve_path(char_u *fname, int reparse_point)
1897{
1898 char_u *path = resolve_shortcut(fname);
1899
1900 if (path == NULL && reparse_point)
1901 path = resolve_reparse_point(fname);
1902 return path;
1903}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904#endif
1905
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001906#if (defined(FEAT_EVAL) && (!defined(FEAT_GUI) || defined(VIMDLL))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907/*
1908 * Bring ourselves to the foreground. Does work if the OS doesn't allow it.
1909 */
1910 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001911win32_set_foreground(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912{
Bram Moolenaar0f873732019-12-05 20:28:46 +01001913 GetConsoleHwnd(); // get value of s_hwnd
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 if (s_hwnd != 0)
1915 SetForegroundWindow(s_hwnd);
1916}
1917#endif
1918
1919#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
1920/*
1921 * Client-server code for Vim
1922 *
1923 * Originally written by Paul Moore
1924 */
1925
Bram Moolenaar0f873732019-12-05 20:28:46 +01001926// In order to handle inter-process messages, we need to have a window. But
1927// the functions in this module can be called before the main GUI window is
1928// created (and may also be called in the console version, where there is no
1929// GUI window at all).
1930//
1931// So we create a hidden window, and arrange to destroy it on exit.
1932HWND message_window = 0; // window that's handling messages
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001934# define VIM_CLASSNAME "VIM_MESSAGES"
1935# define VIM_CLASSNAME_LEN (sizeof(VIM_CLASSNAME) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936
K.Takataf9f2a332022-06-17 20:05:40 +01001937// Timeout for sending a message to another Vim instance. Normally this works
1938// instantly, but it may hang when the other Vim instance is halted.
1939# define SENDMESSAGE_TIMEOUT (5 * 1000)
1940
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001941// Communication is via WM_COPYDATA messages. The message type is sent in
Bram Moolenaar0f873732019-12-05 20:28:46 +01001942// the dwData parameter. Types are defined here.
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001943# define COPYDATA_KEYS 0
1944# define COPYDATA_REPLY 1
1945# define COPYDATA_EXPR 10
1946# define COPYDATA_RESULT 11
1947# define COPYDATA_ERROR_RESULT 12
1948# define COPYDATA_ENCODING 20
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949
Bram Moolenaar0f873732019-12-05 20:28:46 +01001950// This is a structure containing a server HWND and its name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951struct server_id
1952{
1953 HWND hwnd;
1954 char_u *name;
1955};
1956
Bram Moolenaar0f873732019-12-05 20:28:46 +01001957// Last received 'encoding' that the client uses.
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001958static char_u *client_enc = NULL;
1959
1960/*
1961 * Tell the other side what encoding we are using.
K.Takataf9f2a332022-06-17 20:05:40 +01001962 * Return -1 if timeout happens. Other errors are ignored.
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001963 */
K.Takataf9f2a332022-06-17 20:05:40 +01001964 static int
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001965serverSendEnc(HWND target)
1966{
1967 COPYDATASTRUCT data;
1968
1969 data.dwData = COPYDATA_ENCODING;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001970 data.cbData = (DWORD)STRLEN(p_enc) + 1;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001971 data.lpData = p_enc;
K.Takataf9f2a332022-06-17 20:05:40 +01001972 if (SendMessageTimeout(target, WM_COPYDATA,
1973 (WPARAM)message_window, (LPARAM)&data,
1974 SMTO_ABORTIFHUNG, SENDMESSAGE_TIMEOUT, NULL) == 0)
1975 return -1;
1976 return 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001977}
1978
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979/*
1980 * Clean up on exit. This destroys the hidden message window.
1981 */
1982 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983CleanUpMessaging(void)
1984{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001985 if (message_window == 0)
1986 return;
1987
1988 DestroyWindow(message_window);
1989 message_window = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990}
1991
1992static int save_reply(HWND server, char_u *reply, int expr);
1993
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001994/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 * The window procedure for the hidden message window.
1996 * It handles callback messages and notifications from servers.
1997 * In order to process these messages, it is necessary to run a
1998 * message loop. Code which may run before the main message loop
1999 * is started (in the GUI) is careful to pump messages when it needs
2000 * to. Features which require message delivery during normal use will
2001 * not work in the console version - this basically means those
2002 * features which allow Vim to act as a server, rather than a client.
2003 */
2004 static LRESULT CALLBACK
2005Messaging_WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
2006{
2007 if (msg == WM_COPYDATA)
2008 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002009 // This is a message from another Vim. The dwData member of the
2010 // COPYDATASTRUCT determines the type of message:
2011 // COPYDATA_ENCODING:
2012 // The encoding that the client uses. Following messages will
2013 // use this encoding, convert if needed.
2014 // COPYDATA_KEYS:
2015 // A key sequence. We are a server, and a client wants these keys
2016 // adding to the input queue.
2017 // COPYDATA_REPLY:
2018 // A reply. We are a client, and a server has sent this message
2019 // in response to a request. (server2client())
2020 // COPYDATA_EXPR:
2021 // An expression. We are a server, and a client wants us to
2022 // evaluate this expression.
2023 // COPYDATA_RESULT:
2024 // A reply. We are a client, and a server has sent this message
2025 // in response to a COPYDATA_EXPR.
2026 // COPYDATA_ERROR_RESULT:
2027 // A reply. We are a client, and a server has sent this message
2028 // in response to a COPYDATA_EXPR that failed to evaluate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 COPYDATASTRUCT *data = (COPYDATASTRUCT*)lParam;
2030 HWND sender = (HWND)wParam;
2031 COPYDATASTRUCT reply;
2032 char_u *res;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 int retval;
K.Takataf9f2a332022-06-17 20:05:40 +01002034 DWORD_PTR dwret = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002035 char_u *str;
2036 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037
2038 switch (data->dwData)
2039 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002040 case COPYDATA_ENCODING:
Bram Moolenaar0f873732019-12-05 20:28:46 +01002041 // Remember the encoding that the client uses.
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002042 vim_free(client_enc);
2043 client_enc = enc_canonize((char_u *)data->lpData);
2044 return 1;
2045
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 case COPYDATA_KEYS:
Bram Moolenaar0f873732019-12-05 20:28:46 +01002047 // Remember who sent this, for <client>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 clientWindow = sender;
2049
Bram Moolenaar0f873732019-12-05 20:28:46 +01002050 // Add the received keys to the input buffer. The loop waiting
2051 // for the user to do something should check the input buffer.
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002052 str = serverConvert(client_enc, (char_u *)data->lpData, &tofree);
2053 server_to_input_buf(str);
2054 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055
2056# ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01002057 // Wake up the main GUI loop.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002058# ifdef VIMDLL
2059 if (gui.in_use)
2060# endif
2061 if (s_hwnd != 0)
2062 PostMessage(s_hwnd, WM_NULL, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063# endif
2064 return 1;
2065
2066 case COPYDATA_EXPR:
Bram Moolenaar0f873732019-12-05 20:28:46 +01002067 // Remember who sent this, for <client>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 clientWindow = sender;
2069
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002070 str = serverConvert(client_enc, (char_u *)data->lpData, &tofree);
2071 res = eval_client_expr_to_string(str);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002072
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 if (res == NULL)
2074 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00002075 char *err = _(e_invalid_expression_received);
Bram Moolenaar15bf76d2017-03-18 16:18:37 +01002076 size_t len = STRLEN(str) + STRLEN(err) + 5;
2077
Bram Moolenaar964b3742019-05-24 18:54:09 +02002078 res = alloc(len);
Bram Moolenaar15bf76d2017-03-18 16:18:37 +01002079 if (res != NULL)
2080 vim_snprintf((char *)res, len, "%s: \"%s\"", err, str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 reply.dwData = COPYDATA_ERROR_RESULT;
2082 }
2083 else
2084 reply.dwData = COPYDATA_RESULT;
2085 reply.lpData = res;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002086 reply.cbData = (DWORD)STRLEN(res) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087
K.Takataf9f2a332022-06-17 20:05:40 +01002088 if (serverSendEnc(sender) < 0)
2089 retval = -1;
2090 else
2091 {
2092 if (SendMessageTimeout(sender, WM_COPYDATA,
2093 (WPARAM)message_window, (LPARAM)&reply,
2094 SMTO_ABORTIFHUNG, SENDMESSAGE_TIMEOUT, &dwret) == 0)
2095 retval = -1;
2096 else
2097 retval = (int)dwret;
2098 }
Bram Moolenaar15bf76d2017-03-18 16:18:37 +01002099 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 vim_free(res);
2101 return retval;
2102
2103 case COPYDATA_REPLY:
2104 case COPYDATA_RESULT:
2105 case COPYDATA_ERROR_RESULT:
2106 if (data->lpData != NULL)
2107 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002108 str = serverConvert(client_enc, (char_u *)data->lpData,
2109 &tofree);
2110 if (tofree == NULL)
2111 str = vim_strsave(str);
2112 if (save_reply(sender, str,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 (data->dwData == COPYDATA_REPLY ? 0 :
2114 (data->dwData == COPYDATA_RESULT ? 1 :
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002115 2))) == FAIL)
2116 vim_free(str);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002117 else if (data->dwData == COPYDATA_REPLY)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 {
Bram Moolenaar427d51c2013-06-16 16:01:25 +02002119 char_u winstr[30];
2120
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002121 sprintf((char *)winstr, PRINTF_HEX_LONG_U, (long_u)sender);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002122 apply_autocmds(EVENT_REMOTEREPLY, winstr, str,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 TRUE, curbuf);
2124 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 }
2126 return 1;
2127 }
2128
2129 return 0;
2130 }
2131
2132 else if (msg == WM_ACTIVATE && wParam == WA_ACTIVE)
2133 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002134 // When the message window is activated (brought to the foreground),
2135 // this actually applies to the text window.
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002136# if !defined(FEAT_GUI) || defined(VIMDLL)
2137# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002138 if (!gui.in_use)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002139# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002140 GetConsoleHwnd(); // get value of s_hwnd
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002141# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 if (s_hwnd != 0)
2143 {
2144 SetForegroundWindow(s_hwnd);
2145 return 0;
2146 }
2147 }
2148
2149 return DefWindowProc(hwnd, msg, wParam, lParam);
2150}
2151
2152/*
2153 * Initialise the message handling process. This involves creating a window
2154 * to handle messages - the window will not be visible.
2155 */
2156 void
2157serverInitMessaging(void)
2158{
2159 WNDCLASS wndclass;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160
Bram Moolenaar0f873732019-12-05 20:28:46 +01002161 // Clean up on exit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 atexit(CleanUpMessaging);
2163
Bram Moolenaar0f873732019-12-05 20:28:46 +01002164 // Register a window class - we only really care
2165 // about the window procedure
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 wndclass.style = 0;
2167 wndclass.lpfnWndProc = Messaging_WndProc;
2168 wndclass.cbClsExtra = 0;
2169 wndclass.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002170 wndclass.hInstance = g_hinst;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 wndclass.hIcon = NULL;
2172 wndclass.hCursor = NULL;
2173 wndclass.hbrBackground = NULL;
2174 wndclass.lpszMenuName = NULL;
2175 wndclass.lpszClassName = VIM_CLASSNAME;
2176 RegisterClass(&wndclass);
2177
Bram Moolenaar0f873732019-12-05 20:28:46 +01002178 // Create the message window. It will be hidden, so the details don't
2179 // matter. Don't use WS_OVERLAPPEDWINDOW, it will make a shortcut remove
2180 // focus from gvim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 message_window = CreateWindow(VIM_CLASSNAME, "",
2182 WS_POPUPWINDOW | WS_CAPTION,
2183 CW_USEDEFAULT, CW_USEDEFAULT,
2184 100, 100, NULL, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002185 g_hinst, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186}
2187
Bram Moolenaar0f873732019-12-05 20:28:46 +01002188// Used by serverSendToVim() to find an alternate server name.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002189static char_u *altname_buf_ptr = NULL;
2190
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191/*
2192 * Get the title of the window "hwnd", which is the Vim server name, in
2193 * "name[namelen]" and return the length.
2194 * Returns zero if window "hwnd" is not a Vim server.
2195 */
2196 static int
2197getVimServerName(HWND hwnd, char *name, int namelen)
2198{
2199 int len;
2200 char buffer[VIM_CLASSNAME_LEN + 1];
2201
Bram Moolenaar0f873732019-12-05 20:28:46 +01002202 // Ignore windows which aren't Vim message windows
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 len = GetClassName(hwnd, buffer, sizeof(buffer));
2204 if (len != VIM_CLASSNAME_LEN || STRCMP(buffer, VIM_CLASSNAME) != 0)
2205 return 0;
2206
Bram Moolenaar0f873732019-12-05 20:28:46 +01002207 // Get the title of the window
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 return GetWindowText(hwnd, name, namelen);
2209}
2210
2211 static BOOL CALLBACK
2212enumWindowsGetServer(HWND hwnd, LPARAM lparam)
2213{
2214 struct server_id *id = (struct server_id *)lparam;
2215 char server[MAX_PATH];
2216
Bram Moolenaar0f873732019-12-05 20:28:46 +01002217 // Get the title of the window
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 if (getVimServerName(hwnd, server, sizeof(server)) == 0)
2219 return TRUE;
2220
Bram Moolenaar0f873732019-12-05 20:28:46 +01002221 // If this is the server we're looking for, return its HWND
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 if (STRICMP(server, id->name) == 0)
2223 {
2224 id->hwnd = hwnd;
2225 return FALSE;
2226 }
2227
Bram Moolenaar0f873732019-12-05 20:28:46 +01002228 // If we are looking for an alternate server, remember this name.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002229 if (altname_buf_ptr != NULL
2230 && STRNICMP(server, id->name, STRLEN(id->name)) == 0
2231 && vim_isdigit(server[STRLEN(id->name)]))
2232 {
2233 STRCPY(altname_buf_ptr, server);
Bram Moolenaar0f873732019-12-05 20:28:46 +01002234 altname_buf_ptr = NULL; // don't use another name
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002235 }
2236
Bram Moolenaar0f873732019-12-05 20:28:46 +01002237 // Otherwise, keep looking
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 return TRUE;
2239}
2240
2241 static BOOL CALLBACK
2242enumWindowsGetNames(HWND hwnd, LPARAM lparam)
2243{
2244 garray_T *ga = (garray_T *)lparam;
2245 char server[MAX_PATH];
2246
Bram Moolenaar0f873732019-12-05 20:28:46 +01002247 // Get the title of the window
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 if (getVimServerName(hwnd, server, sizeof(server)) == 0)
2249 return TRUE;
2250
Bram Moolenaar0f873732019-12-05 20:28:46 +01002251 // Add the name to the list
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002252 ga_concat(ga, (char_u *)server);
2253 ga_concat(ga, (char_u *)"\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 return TRUE;
2255}
2256
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002257struct enum_windows_s
2258{
2259 WNDENUMPROC lpEnumFunc;
2260 LPARAM lParam;
2261};
2262
2263 static BOOL CALLBACK
2264enum_windows_child(HWND hwnd, LPARAM lParam)
2265{
2266 struct enum_windows_s *ew = (struct enum_windows_s *)lParam;
2267
2268 return (ew->lpEnumFunc)(hwnd, ew->lParam);
2269}
2270
2271 static BOOL CALLBACK
2272enum_windows_toplevel(HWND hwnd, LPARAM lParam)
2273{
2274 struct enum_windows_s *ew = (struct enum_windows_s *)lParam;
2275
Bram Moolenaar95ba5c32018-10-07 22:47:07 +02002276 if ((ew->lpEnumFunc)(hwnd, ew->lParam))
2277 return TRUE;
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002278 return EnumChildWindows(hwnd, enum_windows_child, lParam);
2279}
2280
Bram Moolenaar0f873732019-12-05 20:28:46 +01002281/*
2282 * Enumerate all windows including children.
2283 */
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002284 static BOOL
2285enum_windows(WNDENUMPROC lpEnumFunc, LPARAM lParam)
2286{
2287 struct enum_windows_s ew;
2288
2289 ew.lpEnumFunc = lpEnumFunc;
2290 ew.lParam = lParam;
2291 return EnumWindows(enum_windows_toplevel, (LPARAM)&ew);
2292}
2293
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 static HWND
2295findServer(char_u *name)
2296{
2297 struct server_id id;
2298
2299 id.name = name;
2300 id.hwnd = 0;
2301
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002302 enum_windows(enumWindowsGetServer, (LPARAM)(&id));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303
2304 return id.hwnd;
2305}
2306
2307 void
2308serverSetName(char_u *name)
2309{
2310 char_u *ok_name;
2311 HWND hwnd = 0;
2312 int i = 0;
2313 char_u *p;
2314
Bram Moolenaar0f873732019-12-05 20:28:46 +01002315 // Leave enough space for a 9-digit suffix to ensure uniqueness!
Bram Moolenaar964b3742019-05-24 18:54:09 +02002316 ok_name = alloc(STRLEN(name) + 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317
2318 STRCPY(ok_name, name);
2319 p = ok_name + STRLEN(name);
2320
2321 for (;;)
2322 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002323 // This is inefficient - we're doing an EnumWindows loop for each
2324 // possible name. It would be better to grab all names in one go,
2325 // and scan the list each time...
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 hwnd = findServer(ok_name);
2327 if (hwnd == 0)
2328 break;
2329
2330 ++i;
2331 if (i >= 1000)
2332 break;
2333
2334 sprintf((char *)p, "%d", i);
2335 }
2336
2337 if (hwnd != 0)
2338 vim_free(ok_name);
2339 else
2340 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002341 // Remember the name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 serverName = ok_name;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002343 need_maketitle = TRUE; // update Vim window title later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344
Bram Moolenaar0f873732019-12-05 20:28:46 +01002345 // Update the message window title
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002346 SetWindowText(message_window, (LPCSTR)ok_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002348# ifdef FEAT_EVAL
Bram Moolenaar0f873732019-12-05 20:28:46 +01002349 // Set the servername variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 set_vim_var_string(VV_SEND_SERVER, serverName, -1);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002351# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 }
2353}
2354
2355 char_u *
2356serverGetVimNames(void)
2357{
2358 garray_T ga;
2359
2360 ga_init2(&ga, 1, 100);
2361
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002362 enum_windows(enumWindowsGetNames, (LPARAM)(&ga));
Bram Moolenaar269ec652004-07-29 08:43:53 +00002363 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364
2365 return ga.ga_data;
2366}
2367
2368 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002369serverSendReply(
Bram Moolenaar0f873732019-12-05 20:28:46 +01002370 char_u *name, // Where to send.
2371 char_u *reply) // What to send.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372{
2373 HWND target;
2374 COPYDATASTRUCT data;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002375 long_u n = 0;
K.Takataf9f2a332022-06-17 20:05:40 +01002376 DWORD_PTR dwret = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377
Bram Moolenaar0f873732019-12-05 20:28:46 +01002378 // The "name" argument is a magic cookie obtained from expand("<client>").
2379 // It should be of the form 0xXXXXX - i.e. a C hex literal, which is the
2380 // value of the client's message window HWND.
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002381 sscanf((char *)name, SCANF_HEX_LONG_U, &n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 if (n == 0)
2383 return -1;
2384
2385 target = (HWND)n;
2386 if (!IsWindow(target))
2387 return -1;
2388
2389 data.dwData = COPYDATA_REPLY;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002390 data.cbData = (DWORD)STRLEN(reply) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 data.lpData = reply;
2392
K.Takataf9f2a332022-06-17 20:05:40 +01002393 if (serverSendEnc(target) < 0)
2394 return -1;
2395 if (SendMessageTimeout(target, WM_COPYDATA,
2396 (WPARAM)message_window, (LPARAM)&data,
2397 SMTO_ABORTIFHUNG, SENDMESSAGE_TIMEOUT, &dwret) == 0)
2398 return -1;
2399 return dwret ? 0 : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400}
2401
2402 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002403serverSendToVim(
Bram Moolenaar0f873732019-12-05 20:28:46 +01002404 char_u *name, // Where to send.
2405 char_u *cmd, // What to send.
2406 char_u **result, // Result of eval'ed expression
2407 void *ptarget, // HWND of server
2408 int asExpr, // Expression or keys?
2409 int timeout, // timeout in seconds or zero
2410 int silent) // don't complain about no server
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411{
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002412 HWND target;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 COPYDATASTRUCT data;
2414 char_u *retval = NULL;
2415 int retcode = 0;
K.Takataf9f2a332022-06-17 20:05:40 +01002416 DWORD_PTR dwret = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002417 char_u altname_buf[MAX_PATH];
2418
Bram Moolenaar0f873732019-12-05 20:28:46 +01002419 // Execute locally if no display or target is ourselves
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01002420 if (serverName != NULL && STRICMP(name, serverName) == 0)
2421 return sendToLocalVim(cmd, asExpr, result);
2422
Bram Moolenaar0f873732019-12-05 20:28:46 +01002423 // If the server name does not end in a digit then we look for an
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00002424 // alternate name. e.g. when "name" is GVIM then we may find GVIM2.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002425 if (STRLEN(name) > 1 && !vim_isdigit(name[STRLEN(name) - 1]))
2426 altname_buf_ptr = altname_buf;
2427 altname_buf[0] = NUL;
2428 target = findServer(name);
2429 altname_buf_ptr = NULL;
2430 if (target == 0 && altname_buf[0] != NUL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01002431 // Use another server name we found.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002432 target = findServer(altname_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433
2434 if (target == 0)
2435 {
2436 if (!silent)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002437 semsg(_(e_no_registered_server_named_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 return -1;
2439 }
2440
2441 if (ptarget)
2442 *(HWND *)ptarget = target;
2443
2444 data.dwData = asExpr ? COPYDATA_EXPR : COPYDATA_KEYS;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002445 data.cbData = (DWORD)STRLEN(cmd) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 data.lpData = cmd;
2447
K.Takataf9f2a332022-06-17 20:05:40 +01002448 if (serverSendEnc(target) < 0)
2449 return -1;
2450 if (SendMessageTimeout(target, WM_COPYDATA,
2451 (WPARAM)message_window, (LPARAM)&data,
2452 SMTO_ABORTIFHUNG, SENDMESSAGE_TIMEOUT, &dwret) == 0)
2453 return -1;
2454 if (dwret == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 return -1;
2456
2457 if (asExpr)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002458 retval = serverGetReply(target, &retcode, TRUE, TRUE, timeout);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459
2460 if (result == NULL)
2461 vim_free(retval);
2462 else
Bram Moolenaar0f873732019-12-05 20:28:46 +01002463 *result = retval; // Caller assumes responsibility for freeing
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464
2465 return retcode;
2466}
2467
2468/*
2469 * Bring the server to the foreground.
2470 */
2471 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002472serverForeground(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473{
2474 HWND target = findServer(name);
2475
2476 if (target != 0)
2477 SetForegroundWindow(target);
2478}
2479
Bram Moolenaar0f873732019-12-05 20:28:46 +01002480// Replies from server need to be stored until the client picks them up via
2481// remote_read(). So we maintain a list of server-id/reply pairs.
2482// Note that there could be multiple replies from one server pending if the
2483// client is slow picking them up.
2484// We just store the replies in a simple list. When we remove an entry, we
2485// move list entries down to fill the gap.
2486// The server ID is simply the HWND.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487typedef struct
2488{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002489 HWND server; // server window
2490 char_u *reply; // reply string
2491 int expr_result; // 0 for REPLY, 1 for RESULT 2 for error
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002492} reply_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493
2494static garray_T reply_list = {0, 0, sizeof(reply_T), 5, 0};
2495
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002496# define REPLY_ITEM(i) ((reply_T *)(reply_list.ga_data) + (i))
2497# define REPLY_COUNT (reply_list.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498
Bram Moolenaar0f873732019-12-05 20:28:46 +01002499// Flag which is used to wait for a reply
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500static int reply_received = 0;
2501
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002502/*
2503 * Store a reply. "reply" must be allocated memory (or NULL).
2504 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 static int
2506save_reply(HWND server, char_u *reply, int expr)
2507{
2508 reply_T *rep;
2509
2510 if (ga_grow(&reply_list, 1) == FAIL)
2511 return FAIL;
2512
2513 rep = REPLY_ITEM(REPLY_COUNT);
2514 rep->server = server;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002515 rep->reply = reply;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 rep->expr_result = expr;
2517 if (rep->reply == NULL)
2518 return FAIL;
2519
2520 ++REPLY_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 reply_received = 1;
2522 return OK;
2523}
2524
2525/*
2526 * Get a reply from server "server".
2527 * When "expr_res" is non NULL, get the result of an expression, otherwise a
2528 * server2client() message.
2529 * When non NULL, point to return code. 0 => OK, -1 => ERROR
2530 * If "remove" is TRUE, consume the message, the caller must free it then.
2531 * if "wait" is TRUE block until a message arrives (or the server exits).
2532 */
2533 char_u *
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002534serverGetReply(HWND server, int *expr_res, int remove, int wait, int timeout)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535{
2536 int i;
2537 char_u *reply;
2538 reply_T *rep;
Bram Moolenaar15e737f2017-03-18 21:22:47 +01002539 int did_process = FALSE;
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002540 time_t start;
2541 time_t now;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542
Bram Moolenaar0f873732019-12-05 20:28:46 +01002543 // When waiting, loop until the message waiting for is received.
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002544 time(&start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 for (;;)
2546 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002547 // Reset this here, in case a message arrives while we are going
2548 // through the already received messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 reply_received = 0;
2550
2551 for (i = 0; i < REPLY_COUNT; ++i)
2552 {
2553 rep = REPLY_ITEM(i);
2554 if (rep->server == server
2555 && ((rep->expr_result != 0) == (expr_res != NULL)))
2556 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002557 // Save the values we've found for later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 reply = rep->reply;
2559 if (expr_res != NULL)
2560 *expr_res = rep->expr_result == 1 ? 0 : -1;
2561
2562 if (remove)
2563 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002564 // Move the rest of the list down to fill the gap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 mch_memmove(rep, rep + 1,
2566 (REPLY_COUNT - i - 1) * sizeof(reply_T));
2567 --REPLY_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 }
2569
Bram Moolenaar0f873732019-12-05 20:28:46 +01002570 // Return the reply to the caller, who takes on responsibility
2571 // for freeing it if "remove" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 return reply;
2573 }
2574 }
2575
Bram Moolenaar0f873732019-12-05 20:28:46 +01002576 // If we got here, we didn't find a reply. Return immediately if the
2577 // "wait" parameter isn't set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 if (!wait)
Bram Moolenaar15e737f2017-03-18 21:22:47 +01002579 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002580 // Process pending messages once. Without this, looping on
2581 // remote_peek() would never get the reply.
Bram Moolenaar15e737f2017-03-18 21:22:47 +01002582 if (!did_process)
2583 {
2584 did_process = TRUE;
2585 serverProcessPendingMessages();
2586 continue;
2587 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 break;
Bram Moolenaar15e737f2017-03-18 21:22:47 +01002589 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590
Bram Moolenaar0f873732019-12-05 20:28:46 +01002591 // We need to wait for a reply. Enter a message loop until the
2592 // "reply_received" flag gets set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593
Bram Moolenaar0f873732019-12-05 20:28:46 +01002594 // Loop until we receive a reply
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 while (reply_received == 0)
2596 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002597# ifdef FEAT_TIMERS
Bram Moolenaar0f873732019-12-05 20:28:46 +01002598 // TODO: use the return value to decide how long to wait.
Bram Moolenaar42205552017-03-18 19:42:22 +01002599 check_due_timer();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002600# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002601 time(&now);
2602 if (timeout > 0 && (now - start) >= timeout)
2603 break;
2604
Bram Moolenaar0f873732019-12-05 20:28:46 +01002605 // Wait for a SendMessage() call to us. This could be the reply
2606 // we are waiting for. Use a timeout of a second, to catch the
2607 // situation that the server died unexpectedly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 MsgWaitForMultipleObjects(0, NULL, TRUE, 1000, QS_ALLINPUT);
2609
Bram Moolenaar0f873732019-12-05 20:28:46 +01002610 // If the server has died, give up
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 if (!IsWindow(server))
2612 return NULL;
2613
2614 serverProcessPendingMessages();
2615 }
2616 }
2617
2618 return NULL;
2619}
2620
2621/*
2622 * Process any messages in the Windows message queue.
2623 */
2624 void
2625serverProcessPendingMessages(void)
2626{
2627 MSG msg;
2628
K.Takatab7057bd2022-01-21 11:37:07 +00002629 while (PeekMessageW(&msg, NULL, 0, 0, PM_REMOVE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 {
2631 TranslateMessage(&msg);
K.Takatab7057bd2022-01-21 11:37:07 +00002632 DispatchMessageW(&msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 }
2634}
2635
Bram Moolenaar0f873732019-12-05 20:28:46 +01002636#endif // FEAT_CLIENTSERVER
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637
2638#if defined(FEAT_GUI) || (defined(FEAT_PRINTER) && !defined(FEAT_POSTSCRIPT)) \
2639 || defined(PROTO)
2640
2641struct charset_pair
2642{
2643 char *name;
2644 BYTE charset;
2645};
2646
2647static struct charset_pair
2648charset_pairs[] =
2649{
2650 {"ANSI", ANSI_CHARSET},
2651 {"CHINESEBIG5", CHINESEBIG5_CHARSET},
2652 {"DEFAULT", DEFAULT_CHARSET},
2653 {"HANGEUL", HANGEUL_CHARSET},
2654 {"OEM", OEM_CHARSET},
2655 {"SHIFTJIS", SHIFTJIS_CHARSET},
2656 {"SYMBOL", SYMBOL_CHARSET},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 {"ARABIC", ARABIC_CHARSET},
2658 {"BALTIC", BALTIC_CHARSET},
2659 {"EASTEUROPE", EASTEUROPE_CHARSET},
2660 {"GB2312", GB2312_CHARSET},
2661 {"GREEK", GREEK_CHARSET},
2662 {"HEBREW", HEBREW_CHARSET},
2663 {"JOHAB", JOHAB_CHARSET},
2664 {"MAC", MAC_CHARSET},
2665 {"RUSSIAN", RUSSIAN_CHARSET},
2666 {"THAI", THAI_CHARSET},
2667 {"TURKISH", TURKISH_CHARSET},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002668# ifdef VIETNAMESE_CHARSET
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 {"VIETNAMESE", VIETNAMESE_CHARSET},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002670# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 {NULL, 0}
2672};
2673
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002674struct quality_pair
2675{
2676 char *name;
2677 DWORD quality;
2678};
2679
2680static struct quality_pair
2681quality_pairs[] = {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002682# ifdef CLEARTYPE_QUALITY
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002683 {"CLEARTYPE", CLEARTYPE_QUALITY},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002684# endif
2685# ifdef ANTIALIASED_QUALITY
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002686 {"ANTIALIASED", ANTIALIASED_QUALITY},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002687# endif
2688# ifdef NONANTIALIASED_QUALITY
Bram Moolenaar73a733e2016-05-11 21:05:05 +02002689 {"NONANTIALIASED", NONANTIALIASED_QUALITY},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002690# endif
2691# ifdef PROOF_QUALITY
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002692 {"PROOF", PROOF_QUALITY},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002693# endif
2694# ifdef DRAFT_QUALITY
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002695 {"DRAFT", DRAFT_QUALITY},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002696# endif
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002697 {"DEFAULT", DEFAULT_QUALITY},
2698 {NULL, 0}
2699};
2700
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701/*
2702 * Convert a charset ID to a name.
2703 * Return NULL when not recognized.
2704 */
2705 char *
2706charset_id2name(int id)
2707{
2708 struct charset_pair *cp;
2709
2710 for (cp = charset_pairs; cp->name != NULL; ++cp)
2711 if ((BYTE)id == cp->charset)
2712 break;
2713 return cp->name;
2714}
2715
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002716/*
2717 * Convert a quality ID to a name.
2718 * Return NULL when not recognized.
2719 */
2720 char *
2721quality_id2name(DWORD id)
2722{
2723 struct quality_pair *qp;
2724
2725 for (qp = quality_pairs; qp->name != NULL; ++qp)
2726 if (id == qp->quality)
2727 break;
2728 return qp->name;
2729}
2730
Ken Takatad8cb1dd2024-01-12 18:09:43 +01002731// The default font height in 100% scaling (96dpi).
matveytad3b6a32024-12-08 10:26:51 +01002732// (-16 in 96dpi equates to roughly 12pt)
2733#define DEFAULT_FONT_HEIGHT (-16)
Ken Takatad8cb1dd2024-01-12 18:09:43 +01002734
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002735static const LOGFONTW s_lfDefault =
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736{
Ken Takatad8cb1dd2024-01-12 18:09:43 +01002737 DEFAULT_FONT_HEIGHT,
2738 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, DEFAULT_CHARSET,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
2740 PROOF_QUALITY, FIXED_PITCH | FF_DONTCARE,
Ken Takatad8cb1dd2024-01-12 18:09:43 +01002741 L"" // Default font name will be set later based on current language.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742};
2743
Ken Takatad8cb1dd2024-01-12 18:09:43 +01002744// This will be initialized when set_default_logfont() is called first time.
2745// The value will be based on the system DPI.
2746int current_font_height = 0; // also used in gui_w32.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747
Bram Moolenaar0f873732019-12-05 20:28:46 +01002748/*
2749 * Convert a string representing a point size into pixels. The string should
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 * be a positive decimal number, with an optional decimal point (eg, "12", or
2751 * "10.5"). The pixel value is returned, and a pointer to the next unconverted
2752 * character is stored in *end. The flag "vertical" says whether this
2753 * calculation is for a vertical (height) size or a horizontal (width) one.
2754 */
2755 static int
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002756points_to_pixels(WCHAR *str, WCHAR **end, int vertical, long_i pprinter_dc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757{
2758 int pixels;
2759 int points = 0;
2760 int divisor = 0;
2761 HWND hwnd = (HWND)0;
2762 HDC hdc;
2763 HDC printer_dc = (HDC)pprinter_dc;
2764
2765 while (*str != NUL)
2766 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002767 if (*str == L'.' && divisor == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002769 // Start keeping a divisor, for later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 divisor = 1;
2771 }
2772 else
2773 {
2774 if (!VIM_ISDIGIT(*str))
2775 break;
2776
2777 points *= 10;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002778 points += *str - L'0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 divisor *= 10;
2780 }
2781 ++str;
2782 }
2783
2784 if (divisor == 0)
2785 divisor = 1;
2786
2787 if (printer_dc == NULL)
2788 {
2789 hwnd = GetDesktopWindow();
2790 hdc = GetWindowDC(hwnd);
2791 }
2792 else
2793 hdc = printer_dc;
2794
2795 pixels = MulDiv(points,
2796 GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX),
2797 72 * divisor);
2798
2799 if (printer_dc == NULL)
2800 ReleaseDC(hwnd, hdc);
2801
2802 *end = str;
2803 return pixels;
2804}
2805
Yee Cheng Chin290b8872023-10-05 20:54:21 +02002806/*
2807 * Convert pixel into point size. This is a reverse of points_to_pixels.
2808 */
2809 static double
2810pixels_to_points(int pixels, int vertical, long_i pprinter_dc)
2811{
2812 double points = 0;
2813 HWND hwnd = (HWND)0;
2814 HDC hdc;
2815 HDC printer_dc = (HDC)pprinter_dc;
2816
2817 if (printer_dc == NULL)
2818 {
2819 hwnd = GetDesktopWindow();
2820 hdc = GetWindowDC(hwnd);
2821 }
2822 else
2823 hdc = printer_dc;
2824
2825 points = pixels * 72.0 / GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX);
2826 if (printer_dc == NULL)
2827 ReleaseDC(hwnd, hdc);
2828
2829 return points;
2830}
2831
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832 static int CALLBACK
2833font_enumproc(
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002834 ENUMLOGFONTW *elf,
2835 NEWTEXTMETRICW *ntm UNUSED,
2836 DWORD type UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837 LPARAM lparam)
2838{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002839 // Return value:
2840 // 0 = terminate now (monospace & ANSI)
2841 // 1 = continue, still no luck...
2842 // 2 = continue, but we have an acceptable LOGFONTW
2843 // (monospace, not ANSI)
2844 // We use these values, as EnumFontFamilies returns 1 if the
2845 // callback function is never called. So, we check the return as
2846 // 0 = perfect, 2 = OK, 1 = no good...
2847 // It's not pretty, but it works!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002849 LOGFONTW *lf = (LOGFONTW *)(lparam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002851# ifndef FEAT_PROPORTIONAL_FONTS
Bram Moolenaar0f873732019-12-05 20:28:46 +01002852 // Ignore non-monospace fonts without further ado
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 if ((ntm->tmPitchAndFamily & 1) != 0)
2854 return 1;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002855# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856
Bram Moolenaar0f873732019-12-05 20:28:46 +01002857 // Remember this LOGFONTW as a "possible"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 *lf = elf->elfLogFont;
2859
Bram Moolenaar0f873732019-12-05 20:28:46 +01002860 // Terminate the scan as soon as we find an ANSI font
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 if (lf->lfCharSet == ANSI_CHARSET
2862 || lf->lfCharSet == OEM_CHARSET
2863 || lf->lfCharSet == DEFAULT_CHARSET)
2864 return 0;
2865
Bram Moolenaar0f873732019-12-05 20:28:46 +01002866 // Continue the scan - we have a non-ANSI font
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 return 2;
2868}
2869
2870 static int
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002871init_logfont(LOGFONTW *lf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872{
2873 int n;
2874 HWND hwnd = GetDesktopWindow();
2875 HDC hdc = GetWindowDC(hwnd);
2876
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002877 n = EnumFontFamiliesW(hdc,
2878 lf->lfFaceName,
2879 (FONTENUMPROCW)font_enumproc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880 (LPARAM)lf);
2881
2882 ReleaseDC(hwnd, hdc);
2883
Bram Moolenaar0f873732019-12-05 20:28:46 +01002884 // If we couldn't find a usable font, return failure
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 if (n == 1)
2886 return FAIL;
2887
Bram Moolenaar0f873732019-12-05 20:28:46 +01002888 // Tidy up the rest of the LOGFONTW structure. We set to a basic
2889 // font - get_logfont() sets bold, italic, etc based on the user's
2890 // input.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 lf->lfHeight = current_font_height;
2892 lf->lfWidth = 0;
2893 lf->lfItalic = FALSE;
2894 lf->lfUnderline = FALSE;
2895 lf->lfStrikeOut = FALSE;
2896 lf->lfWeight = FW_NORMAL;
2897
Bram Moolenaar0f873732019-12-05 20:28:46 +01002898 // Return success
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 return OK;
2900}
2901
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002902/*
Yee Cheng Chin290b8872023-10-05 20:54:21 +02002903 * Call back for EnumFontFamiliesW in expand_font_enumproc.
2904 *
2905 */
2906 static int CALLBACK
2907expand_font_enumproc(
2908 ENUMLOGFONTW *elf,
2909 NEWTEXTMETRICW *ntm UNUSED,
2910 DWORD type UNUSED,
2911 LPARAM lparam)
2912{
2913 LOGFONTW *lf = (LOGFONTW*)elf;
2914
2915# ifndef FEAT_PROPORTIONAL_FONTS
2916 // Ignore non-monospace fonts without further ado
2917 if ((ntm->tmPitchAndFamily & 1) != 0)
2918 return 1;
2919# endif
2920
2921 // Filter only on ANSI. Otherwise will see a lot of random fonts that we
2922 // usually don't want.
2923 if (lf->lfCharSet != ANSI_CHARSET)
zeertzjqd9be94c2024-07-14 10:20:20 +02002924 return 1;
Yee Cheng Chin290b8872023-10-05 20:54:21 +02002925
2926 int (*add_match)(char_u *) = (int (*)(char_u *))lparam;
2927
2928 WCHAR *faceNameW = lf->lfFaceName;
2929 char_u *faceName = utf16_to_enc(faceNameW, NULL);
2930 if (!faceName)
2931 return 0;
2932
2933 add_match(faceName);
2934 vim_free(faceName);
2935
2936 return 1;
2937}
2938
2939/*
2940 * Cmdline expansion for setting 'guifont'. Will enumerate through all
2941 * monospace fonts for completion. If used after ':', will expand to possible
2942 * font configuration options like font sizes.
2943 *
2944 * This function has "gui" in its name because in some platforms (GTK) font
2945 * handling is done by the GUI code, whereas in Windows it's part of the
2946 * platform code.
2947 */
2948 void
2949gui_mch_expand_font(optexpand_T *args, void *param UNUSED, int (*add_match)(char_u *val))
2950{
2951 expand_T *xp = args->oe_xp;
2952 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
2953 {
2954 char buf[30];
2955
2956 // Always fill in with the current font size as first option for
2957 // convenience. We simply round to the closest integer for simplicity.
zeertzjqd9be94c2024-07-14 10:20:20 +02002958 int font_height = (int)round(
Yee Cheng Chin290b8872023-10-05 20:54:21 +02002959 pixels_to_points(-current_font_height, TRUE, (long_i)NULL));
2960 vim_snprintf(buf, ARRAY_LENGTH(buf), "h%d", font_height);
2961 add_match((char_u *)buf);
2962
2963 // Note: Keep this in sync with get_logfont(). Don't include 'c' and
2964 // 'q' as we fill in all the values below.
2965 static char *(p_gfn_win_opt_values[]) = {
2966 "h" , "w" , "W" , "b" , "i" , "u" , "s"};
2967 for (size_t i = 0; i < ARRAY_LENGTH(p_gfn_win_opt_values); i++)
2968 add_match((char_u *)p_gfn_win_opt_values[i]);
2969
2970 struct charset_pair *cp;
2971 for (cp = charset_pairs; cp->name != NULL; ++cp)
2972 {
2973 vim_snprintf(buf, ARRAY_LENGTH(buf), "c%s", cp->name);
2974 add_match((char_u *)buf);
2975 }
2976 struct quality_pair *qp;
2977 for (qp = quality_pairs; qp->name != NULL; ++qp)
2978 {
2979 vim_snprintf(buf, ARRAY_LENGTH(buf), "q%s", qp->name);
2980 add_match((char_u *)buf);
2981 }
2982 return;
2983 }
2984
2985 HWND hwnd = GetDesktopWindow();
2986 HDC hdc = GetWindowDC(hwnd);
2987
2988 EnumFontFamiliesW(hdc,
2989 NULL,
2990 (FONTENUMPROCW)expand_font_enumproc,
2991 (LPARAM)add_match);
2992
2993 ReleaseDC(hwnd, hdc);
2994}
2995
2996/*
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002997 * Compare a UTF-16 string and an ASCII string literally.
2998 * Only works all the code points are inside ASCII range.
2999 */
3000 static int
3001utf16ascncmp(const WCHAR *w, const char *p, size_t n)
3002{
3003 size_t i;
3004
3005 for (i = 0; i < n; i++)
3006 {
3007 if (w[i] == 0 || w[i] != p[i])
3008 return w[i] - p[i];
3009 }
3010 return 0;
3011}
3012
3013/*
Ken Takatad8cb1dd2024-01-12 18:09:43 +01003014 * Equivalent of GetDpiForSystem().
3015 */
3016 UINT WINAPI
3017vimGetDpiForSystem(void)
3018{
3019 HWND hwnd = GetDesktopWindow();
3020 HDC hdc = GetWindowDC(hwnd);
3021 UINT dpi = GetDeviceCaps(hdc, LOGPIXELSY);
3022 ReleaseDC(hwnd, hdc);
3023 return dpi;
3024}
3025
3026/*
3027 * Set default logfont based on current language.
3028 */
3029 static void
3030set_default_logfont(LOGFONTW *lf)
3031{
3032 // Default font name for current language on MS-Windows.
3033 // If not translated, falls back to "Consolas".
3034 // This must be a fixed-pitch font.
3035 const char *defaultfontname = N_("DefaultFontNameForWindows");
3036 char *fontname = _(defaultfontname);
3037
3038 if (strcmp(fontname, defaultfontname) == 0)
3039 fontname = "Consolas";
3040
3041 *lf = s_lfDefault;
3042 lf->lfHeight = DEFAULT_FONT_HEIGHT * (int)vimGetDpiForSystem() / 96;
3043 if (current_font_height == 0)
3044 current_font_height = lf->lfHeight;
3045
3046 WCHAR *wfontname = enc_to_utf16((char_u*)fontname, NULL);
3047 if (wfontname != NULL)
3048 {
3049 wcscpy_s(lf->lfFaceName, LF_FACESIZE, wfontname);
3050 vim_free(wfontname);
3051 }
3052}
3053
3054/*
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003055 * Get font info from "name" into logfont "lf".
3056 * Return OK for a valid name, FAIL otherwise.
3057 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058 int
3059get_logfont(
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003060 LOGFONTW *lf,
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003061 char_u *name,
3062 HDC printer_dc,
3063 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003065 WCHAR *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 int i;
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003067 int ret = FAIL;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003068 static LOGFONTW *lastlf = NULL;
3069 WCHAR *wname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070
Ken Takatad8cb1dd2024-01-12 18:09:43 +01003071 set_default_logfont(lf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 if (name == NULL)
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003073 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003075 wname = enc_to_utf16(name, NULL);
3076 if (wname == NULL)
3077 return FAIL;
3078
3079 if (wcscmp(wname, L"*") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003081# if defined(FEAT_GUI_MSWIN)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003082 CHOOSEFONTW cf;
Bram Moolenaar0f873732019-12-05 20:28:46 +01003083 // if name is "*", bring up std font dialog:
Bram Moolenaara80faa82020-04-12 19:37:17 +02003084 CLEAR_FIELD(cf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 cf.lStructSize = sizeof(cf);
3086 cf.hwndOwner = s_hwnd;
3087 cf.Flags = CF_SCREENFONTS | CF_FIXEDPITCHONLY | CF_INITTOLOGFONTSTRUCT;
3088 if (lastlf != NULL)
3089 *lf = *lastlf;
3090 cf.lpLogFont = lf;
3091 cf.nFontType = 0 ; //REGULAR_FONTTYPE;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003092 if (ChooseFontW(&cf))
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003093 ret = OK;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003094# endif
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003095 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 }
3097
3098 /*
3099 * Split name up, it could be <name>:h<height>:w<width> etc.
3100 */
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003101 for (p = wname; *p && *p != L':'; p++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003103 if (p - wname + 1 >= LF_FACESIZE)
Bram Moolenaar0f873732019-12-05 20:28:46 +01003104 goto theend; // Name too long
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003105 lf->lfFaceName[p - wname] = *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 }
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003107 if (p != wname)
3108 lf->lfFaceName[p - wname] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109
Bram Moolenaar0f873732019-12-05 20:28:46 +01003110 // First set defaults
Ken Takatad8cb1dd2024-01-12 18:09:43 +01003111 lf->lfHeight = DEFAULT_FONT_HEIGHT * (int)vimGetDpiForSystem() / 96;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 lf->lfWidth = 0;
3113 lf->lfWeight = FW_NORMAL;
3114 lf->lfItalic = FALSE;
3115 lf->lfUnderline = FALSE;
3116 lf->lfStrikeOut = FALSE;
3117
3118 /*
3119 * If the font can't be found, try replacing '_' by ' '.
3120 */
3121 if (init_logfont(lf) == FAIL)
3122 {
3123 int did_replace = FALSE;
3124
3125 for (i = 0; lf->lfFaceName[i]; ++i)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003126 if (lf->lfFaceName[i] == L'_')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003128 lf->lfFaceName[i] = L' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129 did_replace = TRUE;
3130 }
3131 if (!did_replace || init_logfont(lf) == FAIL)
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003132 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 }
3134
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003135 while (*p == L':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 p++;
3137
Bram Moolenaar0f873732019-12-05 20:28:46 +01003138 // Set the values found after ':'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 while (*p)
3140 {
3141 switch (*p++)
3142 {
Yee Cheng Chin290b8872023-10-05 20:54:21 +02003143 // Note: Keep this in sync with gui_mch_expand_font().
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003144 case L'h':
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003145 lf->lfHeight = - points_to_pixels(p, &p, TRUE, (long_i)printer_dc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003147 case L'w':
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003148 lf->lfWidth = points_to_pixels(p, &p, FALSE, (long_i)printer_dc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 break;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003150 case L'W':
3151 lf->lfWeight = wcstol(p, &p, 10);
3152 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003153 case L'b':
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 lf->lfWeight = FW_BOLD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003156 case L'i':
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 lf->lfItalic = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003159 case L'u':
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 lf->lfUnderline = TRUE;
3161 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003162 case L's':
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 lf->lfStrikeOut = TRUE;
3164 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003165 case L'c':
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 {
3167 struct charset_pair *cp;
3168
3169 for (cp = charset_pairs; cp->name != NULL; ++cp)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003170 if (utf16ascncmp(p, cp->name, strlen(cp->name)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 {
3172 lf->lfCharSet = cp->charset;
3173 p += strlen(cp->name);
3174 break;
3175 }
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003176 if (cp->name == NULL && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003178 char_u *s = utf16_to_enc(p, NULL);
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00003179
3180 semsg(_(e_illegal_str_name_str_in_font_name_str),
3181 "charset", s, name);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003182 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 break;
3184 }
3185 break;
3186 }
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003187 case L'q':
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003188 {
3189 struct quality_pair *qp;
3190
3191 for (qp = quality_pairs; qp->name != NULL; ++qp)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003192 if (utf16ascncmp(p, qp->name, strlen(qp->name)) == 0)
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003193 {
3194 lf->lfQuality = qp->quality;
3195 p += strlen(qp->name);
3196 break;
3197 }
3198 if (qp->name == NULL && verbose)
3199 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003200 char_u *s = utf16_to_enc(p, NULL);
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00003201 semsg(_(e_illegal_str_name_str_in_font_name_str),
3202 "quality", s, name);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003203 vim_free(s);
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003204 break;
3205 }
3206 break;
3207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 default:
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003209 if (verbose)
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00003210 semsg(_(e_illegal_char_nr_in_font_name_str), p[-1], name);
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003211 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212 }
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003213 while (*p == L':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 p++;
3215 }
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003216 ret = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218theend:
Bram Moolenaar0f873732019-12-05 20:28:46 +01003219 // ron: init lastlf
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003220 if (ret == OK && printer_dc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 {
3222 vim_free(lastlf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003223 lastlf = ALLOC_ONE(LOGFONTW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224 if (lastlf != NULL)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003225 mch_memmove(lastlf, lf, sizeof(LOGFONTW));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 }
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003227 vim_free(wname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003229 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230}
3231
Bram Moolenaar0f873732019-12-05 20:28:46 +01003232#endif // defined(FEAT_GUI) || defined(FEAT_PRINTER)
Bram Moolenaarf12d9832016-01-29 21:11:25 +01003233
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003234#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaarf12d9832016-01-29 21:11:25 +01003235/*
3236 * Initialize the Winsock dll.
3237 */
3238 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003239channel_init_winsock(void)
Bram Moolenaarf12d9832016-01-29 21:11:25 +01003240{
3241 WSADATA wsaData;
3242 int wsaerr;
3243
3244 if (WSInitialized)
3245 return;
3246
3247 wsaerr = WSAStartup(MAKEWORD(2, 2), &wsaData);
3248 if (wsaerr == 0)
3249 WSInitialized = TRUE;
3250}
3251#endif