blob: 21b7db31f3daebba5963201186592721dd37d23e [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * os_mswin.c
12 *
Bram Moolenaarcf7164a2016-02-20 13:55:06 +010013 * Routines for Win32.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014 */
15
Bram Moolenaar071d4272004-06-13 20:20:40 +000016#include "vim.h"
17
Bram Moolenaar071d4272004-06-13 20:20:40 +000018#include <sys/types.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000019#include <signal.h>
20#include <limits.h>
Bram Moolenaar82881492012-11-20 16:53:39 +010021#ifndef PROTO
22# include <process.h>
23#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024
25#undef chdir
26#ifdef __GNUC__
27# ifndef __MINGW32__
28# include <dirent.h>
29# endif
30#else
31# include <direct.h>
32#endif
33
Bram Moolenaar82881492012-11-20 16:53:39 +010034#ifndef PROTO
Bram Moolenaar651fca82021-11-29 20:39:38 +000035# if !defined(FEAT_GUI_MSWIN)
Bram Moolenaar82881492012-11-20 16:53:39 +010036# include <shellapi.h>
37# endif
38
39# if defined(FEAT_PRINTER) && !defined(FEAT_POSTSCRIPT)
40# include <dlgs.h>
Bram Moolenaarcea912a2016-10-12 14:20:24 +020041# include <winspool.h>
Bram Moolenaar82881492012-11-20 16:53:39 +010042# include <commdlg.h>
Bram Moolenaarb04a98f2016-12-01 20:32:29 +010043# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000044
Bram Moolenaar0f873732019-12-05 20:28:46 +010045#endif // PROTO
Bram Moolenaar071d4272004-06-13 20:20:40 +000046
47#ifdef __MINGW32__
48# ifndef FROM_LEFT_1ST_BUTTON_PRESSED
49# define FROM_LEFT_1ST_BUTTON_PRESSED 0x0001
50# endif
51# ifndef RIGHTMOST_BUTTON_PRESSED
52# define RIGHTMOST_BUTTON_PRESSED 0x0002
53# endif
54# ifndef FROM_LEFT_2ND_BUTTON_PRESSED
55# define FROM_LEFT_2ND_BUTTON_PRESSED 0x0004
56# endif
57# ifndef FROM_LEFT_3RD_BUTTON_PRESSED
58# define FROM_LEFT_3RD_BUTTON_PRESSED 0x0008
59# endif
60# ifndef FROM_LEFT_4TH_BUTTON_PRESSED
61# define FROM_LEFT_4TH_BUTTON_PRESSED 0x0010
62# endif
63
64/*
65 * EventFlags
66 */
67# ifndef MOUSE_MOVED
68# define MOUSE_MOVED 0x0001
69# endif
70# ifndef DOUBLE_CLICK
71# define DOUBLE_CLICK 0x0002
72# endif
73#endif
74
75/*
76 * When generating prototypes for Win32 on Unix, these lines make the syntax
77 * errors disappear. They do not need to be correct.
78 */
79#ifdef PROTO
Bram Moolenaar912bc4a2019-12-01 18:58:11 +010080# define WINAPI
81# define WINBASEAPI
Bram Moolenaar071d4272004-06-13 20:20:40 +000082typedef int BOOL;
83typedef int CALLBACK;
84typedef int COLORREF;
85typedef int CONSOLE_CURSOR_INFO;
86typedef int COORD;
87typedef int DWORD;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +010088typedef int ENUMLOGFONTW;
Bram Moolenaar071d4272004-06-13 20:20:40 +000089typedef int HANDLE;
90typedef int HDC;
91typedef int HFONT;
92typedef int HICON;
93typedef int HWND;
94typedef int INPUT_RECORD;
Bram Moolenaard21e5bd2022-06-27 22:52:43 +010095typedef int INT_PTR;
Bram Moolenaar071d4272004-06-13 20:20:40 +000096typedef int KEY_EVENT_RECORD;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +010097typedef int LOGFONTW;
Bram Moolenaar071d4272004-06-13 20:20:40 +000098typedef int LPARAM;
99typedef int LPBOOL;
100typedef int LPCSTR;
101typedef int LPCWSTR;
Bram Moolenaarb9cdb372019-04-17 18:24:35 +0200102typedef int LPDWORD;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000103typedef int LPSTR;
104typedef int LPTSTR;
Bram Moolenaarb9cdb372019-04-17 18:24:35 +0200105typedef int LPVOID;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106typedef int LPWSTR;
107typedef int LRESULT;
108typedef int MOUSE_EVENT_RECORD;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100109typedef int NEWTEXTMETRICW;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000110typedef int PACL;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200111typedef int PRINTDLGW;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112typedef int PSECURITY_DESCRIPTOR;
113typedef int PSID;
114typedef int SECURITY_INFORMATION;
115typedef int SHORT;
116typedef int SMALL_RECT;
117typedef int TEXTMETRIC;
118typedef int UINT;
119typedef int WCHAR;
Bram Moolenaarc447d8d2018-12-18 21:56:28 +0100120typedef int WNDENUMPROC;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121typedef int WORD;
122typedef int WPARAM;
123typedef void VOID;
124#endif
125
Bram Moolenaar0f873732019-12-05 20:28:46 +0100126// Record all output and all keyboard & mouse input
127// #define MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128
129#ifdef MCH_WRITE_DUMP
130FILE* fdDump = NULL;
131#endif
132
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200133#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134extern char g_szOrigTitle[];
135#endif
136
137#ifdef FEAT_GUI
138extern HWND s_hwnd;
139#else
Bram Moolenaar0f873732019-12-05 20:28:46 +0100140static HWND s_hwnd = 0; // console window handle, set by GetConsoleHwnd()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141#endif
142
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100143#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar0f873732019-12-05 20:28:46 +0100144int WSInitialized = FALSE; // WinSock is initialized
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100145#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146
Bram Moolenaar0f873732019-12-05 20:28:46 +0100147// Don't generate prototypes here, because some systems do have these
148// functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149#if defined(__GNUC__) && !defined(PROTO)
150# ifndef __MINGW32__
151int _stricoll(char *a, char *b)
152{
153 // the ANSI-ish correct way is to use strxfrm():
154 char a_buff[512], b_buff[512]; // file names, so this is enough on Win32
155 strxfrm(a_buff, a, 512);
156 strxfrm(b_buff, b, 512);
157 return strcoll(a_buff, b_buff);
158}
159
160char * _fullpath(char *buf, char *fname, int len)
161{
162 LPTSTR toss;
163
164 return (char *)GetFullPathName(fname, len, buf, &toss);
165}
166# endif
167
Bram Moolenaaraf62ff32013-03-19 14:48:29 +0100168# if !defined(__MINGW32__) || (__GNUC__ < 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169int _chdrive(int drive)
170{
171 char temp [3] = "-:";
172 temp[0] = drive + 'A' - 1;
173 return !SetCurrentDirectory(temp);
174}
Bram Moolenaaraf62ff32013-03-19 14:48:29 +0100175# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176#endif
177
178
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200179#ifndef PROTO
180/*
181 * Save the instance handle of the exe/dll.
182 */
183 void
184SaveInst(HINSTANCE hInst)
185{
186 g_hinst = hInst;
187}
188#endif
189
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190#if defined(FEAT_GUI_MSWIN) || defined(PROTO)
191/*
192 * GUI version of mch_exit().
193 * Shut down and exit with status `r'
194 * Careful: mch_exit() may be called before mch_init()!
195 */
196 void
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200197mch_exit_g(int r)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198{
Bram Moolenaar955f1982017-02-05 15:10:51 +0100199 exiting = TRUE;
200
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201 display_errors();
202
Bram Moolenaar0f873732019-12-05 20:28:46 +0100203 ml_close_all(TRUE); // remove all memfiles
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204
205# ifdef FEAT_OLE
206 UninitOLE();
207# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100208# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209 if (WSInitialized)
210 {
211 WSInitialized = FALSE;
212 WSACleanup();
213 }
214# endif
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100215# ifdef DYNAMIC_GETTEXT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216 dyn_libintl_end();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100217# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218
219 if (gui.in_use)
220 gui_exit(r);
Bram Moolenaar85c79d32007-02-20 01:59:20 +0000221
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100222# ifdef EXITFREE
Bram Moolenaar85c79d32007-02-20 01:59:20 +0000223 free_all_mem();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100224# endif
Bram Moolenaar85c79d32007-02-20 01:59:20 +0000225
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226 exit(r);
227}
228
Bram Moolenaar0f873732019-12-05 20:28:46 +0100229#endif // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230
231
232/*
233 * Init the tables for toupper() and tolower().
234 */
235 void
236mch_early_init(void)
237{
238 int i;
239
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240 PlatformId();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241
Bram Moolenaar0f873732019-12-05 20:28:46 +0100242 // Init the tables for toupper() and tolower()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243 for (i = 0; i < 256; ++i)
244 toupper_tab[i] = tolower_tab[i] = i;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100245 CharUpperBuff((LPSTR)toupper_tab, 256);
246 CharLowerBuff((LPSTR)tolower_tab, 256);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000247}
248
249
250/*
251 * Return TRUE if the input comes from a terminal, FALSE otherwise.
252 */
253 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100254mch_input_isatty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000255{
256#ifdef FEAT_GUI_MSWIN
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200257# ifdef VIMDLL
258 if (gui.in_use)
259# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +0100260 return TRUE; // GUI always has a tty
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200261#endif
262#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263 if (isatty(read_cmd_fd))
264 return TRUE;
265 return FALSE;
266#endif
267}
268
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269/*
270 * mch_settitle(): set titlebar of our window
271 */
272 void
273mch_settitle(
274 char_u *title,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +0200275 char_u *icon UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000276{
Bram Moolenaar651fca82021-11-29 20:39:38 +0000277#ifdef FEAT_GUI_MSWIN
278# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200279 if (gui.in_use)
Bram Moolenaar651fca82021-11-29 20:39:38 +0000280# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200281 {
282 gui_mch_settitle(title, icon);
283 return;
284 }
Bram Moolenaar651fca82021-11-29 20:39:38 +0000285#endif
286#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000287 if (title != NULL)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000288 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200289 WCHAR *wp = enc_to_utf16(title, NULL);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000290
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200291 if (wp == NULL)
292 return;
293
294 SetConsoleTitleW(wp);
295 vim_free(wp);
296 return;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000297 }
Bram Moolenaar651fca82021-11-29 20:39:38 +0000298#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000299}
300
301
302/*
303 * Restore the window/icon title.
304 * which is one of:
Bram Moolenaar40385db2018-08-07 22:31:44 +0200305 * SAVE_RESTORE_TITLE: Just restore title
306 * SAVE_RESTORE_ICON: Just restore icon (which we don't have)
307 * SAVE_RESTORE_BOTH: Restore title and icon (which we don't have)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000308 */
309 void
Bram Moolenaar1266d672017-02-01 13:43:36 +0100310mch_restore_title(int which UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311{
Bram Moolenaar651fca82021-11-29 20:39:38 +0000312#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
313# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200314 if (!gui.in_use)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100315# endif
Bram Moolenaar651fca82021-11-29 20:39:38 +0000316 SetConsoleTitle(g_szOrigTitle);
317#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000318}
319
320
321/*
322 * Return TRUE if we can restore the title (we can)
323 */
324 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100325mch_can_restore_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326{
327 return TRUE;
328}
329
330
331/*
332 * Return TRUE if we can restore the icon title (we can't)
333 */
334 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100335mch_can_restore_icon(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336{
337 return FALSE;
338}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339
340
341/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000342 * Get absolute file name into buffer "buf" of length "len" bytes,
343 * turning all '/'s into '\\'s and getting the correct case of each component
344 * of the file name. Append a (back)slash to a directory name.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345 * When 'shellslash' set do it the other way around.
346 * Return OK or FAIL.
347 */
348 int
349mch_FullName(
350 char_u *fname,
351 char_u *buf,
352 int len,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100353 int force UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354{
355 int nResult = FAIL;
Bram Moolenaareae1b912019-05-09 15:12:55 +0200356 WCHAR *wname;
357 WCHAR wbuf[MAX_PATH];
358 char_u *cname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359
Bram Moolenaareae1b912019-05-09 15:12:55 +0200360 wname = enc_to_utf16(fname, NULL);
361 if (wname != NULL && _wfullpath(wbuf, wname, MAX_PATH) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362 {
Bram Moolenaareae1b912019-05-09 15:12:55 +0200363 cname = utf16_to_enc((short_u *)wbuf, NULL);
364 if (cname != NULL)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000365 {
Bram Moolenaareae1b912019-05-09 15:12:55 +0200366 vim_strncpy(buf, cname, len - 1);
367 nResult = OK;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369 }
Bram Moolenaareae1b912019-05-09 15:12:55 +0200370 vim_free(wname);
371 vim_free(cname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372
373#ifdef USE_FNAME_CASE
374 fname_case(buf, len);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000375#else
376 slash_adjust(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377#endif
378
379 return nResult;
380}
381
382
383/*
384 * Return TRUE if "fname" does not depend on the current directory.
385 */
386 int
387mch_isFullName(char_u *fname)
388{
Bram Moolenaar0ea74212020-12-11 20:10:50 +0100389 // A name like "d:/foo" and "//server/share" is absolute. "d:foo" is not.
390 // Another way to check is to use mch_FullName() and see if the result is
391 // the same as the name or mch_FullName() fails. However, this has quite a
392 // bit of overhead, so let's not do that.
Yegappan Lakshmanan6df0f272021-12-16 13:06:10 +0000393 if (*fname == NUL)
Yegappan Lakshmanan5a664fe2021-12-29 18:16:21 +0000394 return FALSE;
Bram Moolenaar0ea74212020-12-11 20:10:50 +0100395 return ((ASCII_ISALPHA(fname[0]) && fname[1] == ':'
396 && (fname[2] == '/' || fname[2] == '\\'))
397 || (fname[0] == fname[1] && (fname[0] == '/' || fname[0] == '\\')));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398}
399
400/*
401 * Replace all slashes by backslashes.
402 * This used to be the other way around, but MS-DOS sometimes has problems
403 * with slashes (e.g. in a command name). We can't have mixed slashes and
404 * backslashes, because comparing file names will not work correctly. The
405 * commands that use a file name should try to avoid the need to type a
406 * backslash twice.
407 * When 'shellslash' set do it the other way around.
Bram Moolenaarb4f6a462015-10-13 19:43:17 +0200408 * When the path looks like a URL leave it unmodified.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409 */
410 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100411slash_adjust(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412{
Bram Moolenaarb4f6a462015-10-13 19:43:17 +0200413 if (path_with_url(p))
414 return;
Bram Moolenaar39d21e32017-08-05 23:09:31 +0200415
416 if (*p == '`')
417 {
Bram Moolenaar116a0f82017-08-07 21:17:57 +0200418 size_t len = STRLEN(p);
419
Bram Moolenaar0f873732019-12-05 20:28:46 +0100420 // don't replace backslash in backtick quoted strings
Bram Moolenaar39d21e32017-08-05 23:09:31 +0200421 if (len > 2 && *(p + len - 1) == '`')
422 return;
423 }
424
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +0000425 while (*p)
426 {
427 if (*p == psepcN)
428 *p = psepc;
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100429 MB_PTR_ADV(p);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +0000430 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431}
432
K.Takatac351dc12022-01-24 11:24:08 +0000433// Use 64-bit stat functions.
434#undef stat
435#undef _stat
436#undef _wstat
437#undef _fstat
438#define stat _stat64
439#define _stat _stat64
440#define _wstat _wstat64
441#define _fstat _fstat64
Bram Moolenaar8962fda2013-09-29 19:05:21 +0200442
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200443 static int
LemonBoy40fd7e62022-05-05 20:18:16 +0100444read_reparse_point(const WCHAR *name, char_u *buf, DWORD *buf_len)
445{
446 HANDLE h;
447 BOOL ok;
448
449 h = CreateFileW(name, FILE_READ_ATTRIBUTES,
450 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
451 OPEN_EXISTING,
452 FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT,
453 NULL);
454 if (h == INVALID_HANDLE_VALUE)
455 return FAIL;
456
457 ok = DeviceIoControl(h, FSCTL_GET_REPARSE_POINT, NULL, 0, buf, *buf_len,
458 buf_len, NULL);
459 CloseHandle(h);
460
461 return ok ? OK : FAIL;
462}
463
464 static int
Bram Moolenaar8767f522016-07-01 17:17:39 +0200465wstat_symlink_aware(const WCHAR *name, stat_T *stp)
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200466{
Bram Moolenaara12a1612019-01-24 16:39:02 +0100467#if (defined(_MSC_VER) && (_MSC_VER < 1900)) || defined(__MINGW32__)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100468 // Work around for VC12 or earlier (and MinGW). _wstat() can't handle
469 // symlinks properly.
470 // VC9 or earlier: _wstat() doesn't support a symlink at all. It retrieves
471 // status of a symlink itself.
472 // VC10: _wstat() supports a symlink to a normal file, but it doesn't
473 // support a symlink to a directory (always returns an error).
474 // VC11 and VC12: _wstat() doesn't return an error for a symlink to a
475 // directory, but it doesn't set S_IFDIR flag.
476 // MinGW: Same as VC9.
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200477 int n;
478 BOOL is_symlink = FALSE;
479 HANDLE hFind, h;
480 DWORD attr = 0;
481 WIN32_FIND_DATAW findDataW;
482
483 hFind = FindFirstFileW(name, &findDataW);
484 if (hFind != INVALID_HANDLE_VALUE)
485 {
486 attr = findDataW.dwFileAttributes;
487 if ((attr & FILE_ATTRIBUTE_REPARSE_POINT)
488 && (findDataW.dwReserved0 == IO_REPARSE_TAG_SYMLINK))
489 is_symlink = TRUE;
490 FindClose(hFind);
491 }
492 if (is_symlink)
493 {
494 h = CreateFileW(name, FILE_READ_ATTRIBUTES,
495 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
496 OPEN_EXISTING,
497 (attr & FILE_ATTRIBUTE_DIRECTORY)
498 ? FILE_FLAG_BACKUP_SEMANTICS : 0,
499 NULL);
500 if (h != INVALID_HANDLE_VALUE)
501 {
502 int fd;
503
K.Takatac351dc12022-01-24 11:24:08 +0000504 fd = _open_osfhandle((intptr_t)h, _O_RDONLY);
Bram Moolenaar8767f522016-07-01 17:17:39 +0200505 n = _fstat(fd, (struct _stat *)stp);
Bram Moolenaarfce7b3d2016-01-19 19:00:32 +0100506 if ((n == 0) && (attr & FILE_ATTRIBUTE_DIRECTORY))
507 stp->st_mode = (stp->st_mode & ~S_IFREG) | S_IFDIR;
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200508 _close(fd);
509 return n;
510 }
511 }
Bram Moolenaara12a1612019-01-24 16:39:02 +0100512#endif
Bram Moolenaar8767f522016-07-01 17:17:39 +0200513 return _wstat(name, (struct _stat *)stp);
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200514}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515
LemonBoy40fd7e62022-05-05 20:18:16 +0100516 char_u *
517resolve_appexeclink(char_u *fname)
518{
Bram Moolenaar9f1983d2022-05-12 20:35:35 +0100519 DWORD attr = 0;
520 int idx;
521 WCHAR *p, *end, *wname;
LemonBoy40fd7e62022-05-05 20:18:16 +0100522 // The buffer size is arbitrarily chosen to be "big enough" (TM), the
523 // ceiling should be around 16k.
Bram Moolenaar9f1983d2022-05-12 20:35:35 +0100524 char_u buf[4096];
525 DWORD buf_len = sizeof(buf);
LemonBoy40fd7e62022-05-05 20:18:16 +0100526 REPARSE_DATA_BUFFER *rb = (REPARSE_DATA_BUFFER *)buf;
527
528 wname = enc_to_utf16(fname, NULL);
529 if (wname == NULL)
530 return NULL;
531
532 attr = GetFileAttributesW(wname);
533 if (attr == INVALID_FILE_ATTRIBUTES ||
534 (attr & FILE_ATTRIBUTE_REPARSE_POINT) == 0)
535 {
536 vim_free(wname);
537 return NULL;
538 }
539
540 // The applinks are similar to symlinks but with a huge difference: they can
541 // only be executed, any other I/O operation on them is bound to fail with
542 // ERROR_FILE_NOT_FOUND even though the file exists.
543 if (read_reparse_point(wname, buf, &buf_len) == FAIL)
544 {
545 vim_free(wname);
546 return NULL;
547 }
548 vim_free(wname);
549
550 if (rb->ReparseTag != IO_REPARSE_TAG_APPEXECLINK)
551 return NULL;
552
553 // The (undocumented) reparse buffer contains a set of N null-terminated
554 // Unicode strings, the application path is stored in the third one.
555 if (rb->AppExecLinkReparseBuffer.StringCount < 3)
556 return NULL;
557
558 p = rb->AppExecLinkReparseBuffer.StringList;
559 end = p + rb->ReparseDataLength / sizeof(WCHAR);
560 for (idx = 0; p < end
561 && idx < (int)rb->AppExecLinkReparseBuffer.StringCount
562 && idx != 2; )
563 {
Yegappan Lakshmananebb01bd2022-06-08 15:14:09 +0100564 if (*p++ == L'\0')
LemonBoy40fd7e62022-05-05 20:18:16 +0100565 ++idx;
566 }
567
568 return utf16_to_enc(p, NULL);
569}
570
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571/*
572 * stat() can't handle a trailing '/' or '\', remove it first.
573 */
574 int
Bram Moolenaar8767f522016-07-01 17:17:39 +0200575vim_stat(const char *name, stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100577 // WinNT and later can use _MAX_PATH wide characters for a pathname, which
578 // means that the maximum pathname is _MAX_PATH * 3 bytes when 'enc' is
579 // UTF-8.
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100580 char_u buf[_MAX_PATH * 3 + 1];
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100581 char_u *p;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200582 WCHAR *wp;
583 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584
Bram Moolenaard2a203b2013-08-30 16:51:18 +0200585 vim_strncpy((char_u *)buf, (char_u *)name, sizeof(buf) - 1);
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100586 p = buf + STRLEN(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 if (p > buf)
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100588 MB_PTR_BACK(buf, p);
Bram Moolenaarb1cb35f2014-01-10 13:05:20 +0100589
Bram Moolenaar0f873732019-12-05 20:28:46 +0100590 // Remove trailing '\\' except root path.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591 if (p > buf && (*p == '\\' || *p == '/') && p[-1] != ':')
592 *p = NUL;
Bram Moolenaarb1cb35f2014-01-10 13:05:20 +0100593
594 if ((buf[0] == '\\' && buf[1] == '\\') || (buf[0] == '/' && buf[1] == '/'))
595 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100596 // UNC root path must be followed by '\\'.
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100597 p = vim_strpbrk(buf + 2, (char_u *)"\\/");
Bram Moolenaarb1cb35f2014-01-10 13:05:20 +0100598 if (p != NULL)
599 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100600 p = vim_strpbrk(p + 1, (char_u *)"\\/");
Bram Moolenaarb1cb35f2014-01-10 13:05:20 +0100601 if (p == NULL)
602 STRCAT(buf, "\\");
603 }
604 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200606 wp = enc_to_utf16(buf, NULL);
607 if (wp == NULL)
608 return -1;
609
610 n = wstat_symlink_aware(wp, stp);
611 vim_free(wp);
612 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613}
614
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200615#if (defined(FEAT_GUI_MSWIN) && !defined(VIMDLL)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616 void
Bram Moolenaar26e86442020-05-17 14:06:16 +0200617mch_settmode(tmode_T tmode UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100619 // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620}
621
622 int
623mch_get_shellsize(void)
624{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100625 // never used
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 return OK;
627}
628
629 void
630mch_set_shellsize(void)
631{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100632 // never used
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633}
634
635/*
636 * Rows and/or Columns has changed.
637 */
638 void
639mch_new_shellsize(void)
640{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100641 // never used
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642}
643
644#endif
645
646/*
647 * We have no job control, so fake it by starting a new shell.
648 */
649 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100650mch_suspend(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651{
652 suspend_shell();
653}
654
655#if defined(USE_MCH_ERRMSG) || defined(PROTO)
656
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200657# ifdef display_errors
658# undef display_errors
659# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660
661/*
662 * Display the saved error message(s).
663 */
664 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100665display_errors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666{
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200667# ifdef FEAT_GUI
Bram Moolenaar819ab822022-06-13 22:34:14 +0100668 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200670# ifdef VIMDLL
671 if (gui.in_use || gui.starting)
672# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200674 if (error_ga.ga_data != NULL)
675 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100676 // avoid putting up a message box with blanks only
Bram Moolenaar819ab822022-06-13 22:34:14 +0100677 for (p = (char_u *)error_ga.ga_data; *p; ++p)
Keith Thompson184f71c2024-01-04 21:19:04 +0100678 if (!SAFE_isspace(*p))
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200679 {
Bram Moolenaar2d12c252022-06-13 21:42:45 +0100680 // Only use a dialog when not using --gui-dialog-file:
681 // write text to a file.
682 if (!gui_dialog_log((char_u *)"Errors", p))
683 (void)gui_mch_dialog(
Bram Moolenaarc17ef8e2006-03-25 21:48:58 +0000684 gui.starting ? VIM_INFO :
Bram Moolenaarc17ef8e2006-03-25 21:48:58 +0000685 VIM_ERROR,
Bram Moolenaarc17ef8e2006-03-25 21:48:58 +0000686 gui.starting ? (char_u *)_("Message") :
Bram Moolenaarc17ef8e2006-03-25 21:48:58 +0000687 (char_u *)_("Error"),
Bram Moolenaar819ab822022-06-13 22:34:14 +0100688 p, (char_u *)_("&Ok"),
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100689 1, NULL, FALSE);
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200690 break;
691 }
692 ga_clear(&error_ga);
693 }
694 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200696# endif
697# if !defined(FEAT_GUI) || defined(VIMDLL)
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +0100698 FlushFileBuffers(GetStdHandle(STD_ERROR_HANDLE));
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200699# endif
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +0100700}
701#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702
703
704/*
705 * Return TRUE if "p" contain a wildcard that can be expanded by
706 * dos_expandpath().
707 */
708 int
709mch_has_exp_wildcard(char_u *p)
710{
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100711 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 {
713 if (vim_strchr((char_u *)"?*[", *p) != NULL
714 || (*p == '~' && p[1] != NUL))
715 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 }
717 return FALSE;
718}
719
720/*
721 * Return TRUE if "p" contain a wildcard or a "~1" kind of thing (could be a
722 * shortened file name).
723 */
724 int
725mch_has_wildcard(char_u *p)
726{
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100727 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 {
729 if (vim_strchr((char_u *)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100730#ifdef VIM_BACKTICK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 "?*$[`"
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100732#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733 "?*$["
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100734#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735 , *p) != NULL
736 || (*p == '~' && p[1] != NUL))
737 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738 }
739 return FALSE;
740}
741
742
743/*
744 * The normal _chdir() does not change the default drive. This one does.
745 * Returning 0 implies success; -1 implies failure.
746 */
747 int
748mch_chdir(char *path)
749{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200750 WCHAR *p;
751 int n;
752
Bram Moolenaar0f873732019-12-05 20:28:46 +0100753 if (path[0] == NUL) // just checking...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 return -1;
755
Bram Moolenaara2974d72009-07-14 16:38:36 +0000756 if (p_verbose >= 5)
757 {
758 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100759 smsg("chdir(%s)", path);
Bram Moolenaara2974d72009-07-14 16:38:36 +0000760 verbose_leave();
761 }
Keith Thompson184f71c2024-01-04 21:19:04 +0100762 if (SAFE_isalpha(path[0]) && path[1] == ':') // has a drive name
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100764 // If we can change to the drive, skip that part of the path. If we
765 // can't then the current directory may be invalid, try using chdir()
766 // with the whole path.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 if (_chdrive(TOLOWER_ASC(path[0]) - 'a' + 1) == 0)
768 path += 2;
769 }
770
Bram Moolenaar0f873732019-12-05 20:28:46 +0100771 if (*path == NUL) // drive name only
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 return 0;
773
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200774 p = enc_to_utf16((char_u *)path, NULL);
775 if (p == NULL)
776 return -1;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000777
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200778 n = _wchdir(p);
779 vim_free(p);
780 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781}
782
783
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200784#if defined(FEAT_GUI_MSWIN) && !defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785/*
786 * return non-zero if a character is available
787 */
788 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100789mch_char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100791 // never used
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 return TRUE;
793}
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200794
795# if defined(FEAT_TERMINAL) || defined(PROTO)
796/*
797 * Check for any pending input or messages.
798 */
799 int
800mch_check_messages(void)
801{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100802 // TODO: check for messages
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200803 return TRUE;
804}
805# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806#endif
807
808
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809#if defined(FEAT_LIBCALL) || defined(PROTO)
810/*
811 * Call a DLL routine which takes either a string or int param
812 * and returns an allocated string.
813 * Return OK if it worked, FAIL if not.
814 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815typedef LPTSTR (*MYSTRPROCSTR)(LPTSTR);
816typedef LPTSTR (*MYINTPROCSTR)(int);
817typedef int (*MYSTRPROCINT)(LPTSTR);
818typedef int (*MYINTPROCINT)(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820/*
821 * Check if a pointer points to a valid NUL terminated string.
822 * Return the length of the string, including terminating NUL.
823 * Returns 0 for an invalid pointer, 1 for an empty string.
824 */
825 static size_t
826check_str_len(char_u *str)
827{
828 SYSTEM_INFO si;
829 MEMORY_BASIC_INFORMATION mbi;
830 size_t length = 0;
831 size_t i;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100832 const char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833
Bram Moolenaar0f873732019-12-05 20:28:46 +0100834 // get page size
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 GetSystemInfo(&si);
836
Bram Moolenaar0f873732019-12-05 20:28:46 +0100837 // get memory information
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000838 if (!VirtualQuery(str, &mbi, sizeof(mbi)))
839 return 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000841 // pre cast these (typing savers)
842 long_u dwStr = (long_u)str;
843 long_u dwBaseAddress = (long_u)mbi.BaseAddress;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000845 // get start address of page that str is on
846 long_u strPage = dwStr - (dwStr - dwBaseAddress) % si.dwPageSize;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +0000848 // get length from str to end of page
849 long_u pageLength = si.dwPageSize - (dwStr - strPage);
850
851 for (p = str; !IsBadReadPtr(p, (UINT)pageLength);
852 p += pageLength, pageLength = si.dwPageSize)
853 for (i = 0; i < pageLength; ++i, ++length)
854 if (p[i] == NUL)
855 return length + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856
857 return 0;
858}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200860/*
861 * Passed to do_in_runtimepath() to load a vim.ico file.
862 */
863 static void
864mch_icon_load_cb(char_u *fname, void *cookie)
865{
866 HANDLE *h = (HANDLE *)cookie;
867
868 *h = LoadImage(NULL,
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100869 (LPSTR)fname,
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200870 IMAGE_ICON,
871 64,
872 64,
873 LR_LOADFROMFILE | LR_LOADMAP3DCOLORS);
874}
875
876/*
877 * Try loading an icon file from 'runtimepath'.
878 */
879 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100880mch_icon_load(HANDLE *iconp)
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200881{
882 return do_in_runtimepath((char_u *)"bitmaps/vim.ico",
Bram Moolenaar7f8989d2016-03-12 22:11:39 +0100883 0, mch_icon_load_cb, iconp);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200884}
885
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 int
887mch_libcall(
888 char_u *libname,
889 char_u *funcname,
Bram Moolenaar0f873732019-12-05 20:28:46 +0100890 char_u *argstring, // NULL when using a argint
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 int argint,
Bram Moolenaar0f873732019-12-05 20:28:46 +0100892 char_u **string_result,// NULL when using number_result
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 int *number_result)
894{
895 HINSTANCE hinstLib;
896 MYSTRPROCSTR ProcAdd;
897 MYINTPROCSTR ProcAddI;
898 char_u *retval_str = NULL;
899 int retval_int = 0;
900 size_t len;
901
902 BOOL fRunTimeLinkSuccess = FALSE;
903
904 // Get a handle to the DLL module.
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100905 hinstLib = vimLoadLib((char *)libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906
907 // If the handle is valid, try to get the function address.
908 if (hinstLib != NULL)
909 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100910# ifdef HAVE_TRY_EXCEPT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 __try
912 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100913# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 if (argstring != NULL)
915 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100916 // Call with string argument
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100917 ProcAdd = (MYSTRPROCSTR)GetProcAddress(hinstLib, (LPCSTR)funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 if ((fRunTimeLinkSuccess = (ProcAdd != NULL)) != 0)
919 {
920 if (string_result == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100921 retval_int = ((MYSTRPROCINT)ProcAdd)((LPSTR)argstring);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100923 retval_str = (char_u *)(ProcAdd)((LPSTR)argstring);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 }
925 }
926 else
927 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100928 // Call with number argument
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100929 ProcAddI = (MYINTPROCSTR) GetProcAddress(hinstLib, (LPCSTR)funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 if ((fRunTimeLinkSuccess = (ProcAddI != NULL)) != 0)
931 {
932 if (string_result == NULL)
933 retval_int = ((MYINTPROCINT)ProcAddI)(argint);
934 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100935 retval_str = (char_u *)(ProcAddI)(argint);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 }
937 }
938
939 // Save the string before we free the library.
940 // Assume that a "1" result is an illegal pointer.
941 if (string_result == NULL)
942 *number_result = retval_int;
943 else if (retval_str != NULL
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100944 && (len = check_str_len(retval_str)) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 {
Bram Moolenaar18a4ba22019-05-24 19:39:03 +0200946 *string_result = alloc(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 if (*string_result != NULL)
948 mch_memmove(*string_result, retval_str, len);
949 }
950
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100951# ifdef HAVE_TRY_EXCEPT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 }
953 __except(EXCEPTION_EXECUTE_HANDLER)
954 {
955 if (GetExceptionCode() == EXCEPTION_STACK_OVERFLOW)
K.Takatac351dc12022-01-24 11:24:08 +0000956 _resetstkoflw();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 fRunTimeLinkSuccess = 0;
958 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100959# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960
961 // Free the DLL module.
962 (void)FreeLibrary(hinstLib);
963 }
964
965 if (!fRunTimeLinkSuccess)
966 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000967 semsg(_(e_library_call_failed_for_str), funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 return FAIL;
969 }
970
971 return OK;
972}
973#endif
974
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975/*
976 * Debugging helper: expose the MCH_WRITE_DUMP stuff to other modules
977 */
978 void
Bram Moolenaar1266d672017-02-01 13:43:36 +0100979DumpPutS(const char *psz UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100981#ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982 if (fdDump)
983 {
984 fputs(psz, fdDump);
985 if (psz[strlen(psz) - 1] != '\n')
986 fputc('\n', fdDump);
987 fflush(fdDump);
988 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100989#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990}
991
992#ifdef _DEBUG
993
994void __cdecl
995Trace(
996 char *pszFormat,
997 ...)
998{
999 CHAR szBuff[2048];
1000 va_list args;
1001
1002 va_start(args, pszFormat);
1003 vsprintf(szBuff, pszFormat, args);
1004 va_end(args);
1005
1006 OutputDebugString(szBuff);
1007}
1008
1009#endif //_DEBUG
1010
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001011#if !defined(FEAT_GUI) || defined(VIMDLL) || defined(PROTO)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001012extern HWND g_hWnd; // This is in os_win32.c.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013
1014/*
1015 * Showing the printer dialog is tricky since we have no GUI
1016 * window to parent it. The following routines are needed to
1017 * get the window parenting and Z-order to work properly.
1018 */
1019 static void
1020GetConsoleHwnd(void)
1021{
Bram Moolenaar0f873732019-12-05 20:28:46 +01001022 // Skip if it's already set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 if (s_hwnd != 0)
1024 return;
1025
Bram Moolenaar0f873732019-12-05 20:28:46 +01001026 // Window handle may have been found by init code (Windows NT only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 if (g_hWnd != 0)
1028 {
1029 s_hwnd = g_hWnd;
1030 return;
1031 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032
Bram Moolenaare1ed53f2019-02-12 23:12:37 +01001033 s_hwnd = GetConsoleWindow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034}
Bram Moolenaar843ee412004-06-30 16:16:41 +00001035
1036/*
1037 * Console implementation of ":winpos".
1038 */
1039 int
1040mch_get_winpos(int *x, int *y)
1041{
1042 RECT rect;
1043
1044 GetConsoleHwnd();
1045 GetWindowRect(s_hwnd, &rect);
1046 *x = rect.left;
1047 *y = rect.top;
1048 return OK;
1049}
1050
1051/*
1052 * Console implementation of ":winpos x y".
1053 */
1054 void
1055mch_set_winpos(int x, int y)
1056{
1057 GetConsoleHwnd();
1058 SetWindowPos(s_hwnd, NULL, x, y, 0, 0,
1059 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
1060}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061#endif
1062
1063#if (defined(FEAT_PRINTER) && !defined(FEAT_POSTSCRIPT)) || defined(PROTO)
1064
Bram Moolenaar0f873732019-12-05 20:28:46 +01001065//=================================================================
1066// Win32 printer stuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067
1068static HFONT prt_font_handles[2][2][2];
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001069static PRINTDLGW prt_dlg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070static const int boldface[2] = {FW_REGULAR, FW_BOLD};
1071static TEXTMETRIC prt_tm;
1072static int prt_line_height;
1073static int prt_number_width;
1074static int prt_left_margin;
1075static int prt_right_margin;
1076static int prt_top_margin;
1077static char_u szAppName[] = TEXT("VIM");
1078static HWND hDlgPrint;
1079static int *bUserAbort = NULL;
1080static char_u *prt_name = NULL;
1081
Bram Moolenaar0f873732019-12-05 20:28:46 +01001082// Defines which are also in vim.rc.
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001083# define IDC_BOX1 400
1084# define IDC_PRINTTEXT1 401
1085# define IDC_PRINTTEXT2 402
1086# define IDC_PROGRESS 403
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001088 static BOOL
1089vimSetDlgItemText(HWND hDlg, int nIDDlgItem, char_u *s)
1090{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001091 WCHAR *wp;
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001092 BOOL ret;
1093
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001094 wp = enc_to_utf16(s, NULL);
1095 if (wp == NULL)
1096 return FALSE;
1097
1098 ret = SetDlgItemTextW(hDlg, nIDDlgItem, wp);
1099 vim_free(wp);
1100 return ret;
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001101}
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001102
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103/*
1104 * Convert BGR to RGB for Windows GDI calls
1105 */
1106 static COLORREF
1107swap_me(COLORREF colorref)
1108{
1109 int temp;
1110 char *ptr = (char *)&colorref;
1111
1112 temp = *(ptr);
1113 *(ptr ) = *(ptr + 2);
1114 *(ptr + 2) = temp;
1115 return colorref;
1116}
1117
K.Takatac351dc12022-01-24 11:24:08 +00001118 static INT_PTR CALLBACK
Bram Moolenaar1266d672017-02-01 13:43:36 +01001119PrintDlgProc(
1120 HWND hDlg,
1121 UINT message,
1122 WPARAM wParam UNUSED,
1123 LPARAM lParam UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001125# ifdef FEAT_GETTEXT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 NONCLIENTMETRICS nm;
1127 static HFONT hfont;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001128# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129
1130 switch (message)
1131 {
1132 case WM_INITDIALOG:
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001133# ifdef FEAT_GETTEXT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 nm.cbSize = sizeof(NONCLIENTMETRICS);
1135 if (SystemParametersInfo(
1136 SPI_GETNONCLIENTMETRICS,
1137 sizeof(NONCLIENTMETRICS),
1138 &nm,
1139 0))
1140 {
1141 char buff[MAX_PATH];
1142 int i;
1143
Bram Moolenaar0f873732019-12-05 20:28:46 +01001144 // Translate the dialog texts
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 hfont = CreateFontIndirect(&nm.lfMessageFont);
1146 for (i = IDC_PRINTTEXT1; i <= IDC_PROGRESS; i++)
1147 {
1148 SendDlgItemMessage(hDlg, i, WM_SETFONT, (WPARAM)hfont, 1);
1149 if (GetDlgItemText(hDlg,i, buff, sizeof(buff)))
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001150 vimSetDlgItemText(hDlg,i, (char_u *)_(buff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 }
1152 SendDlgItemMessage(hDlg, IDCANCEL,
1153 WM_SETFONT, (WPARAM)hfont, 1);
1154 if (GetDlgItemText(hDlg,IDCANCEL, buff, sizeof(buff)))
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001155 vimSetDlgItemText(hDlg,IDCANCEL, (char_u *)_(buff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001157# endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001158 SetWindowText(hDlg, (LPCSTR)szAppName);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 if (prt_name != NULL)
1160 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001161 vimSetDlgItemText(hDlg, IDC_PRINTTEXT2, (char_u *)prt_name);
Bram Moolenaard23a8232018-02-10 18:45:26 +01001162 VIM_CLEAR(prt_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 }
1164 EnableMenuItem(GetSystemMenu(hDlg, FALSE), SC_CLOSE, MF_GRAYED);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001165# if !defined(FEAT_GUI) || defined(VIMDLL)
1166# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001167 if (!gui.in_use)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001168# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001169 BringWindowToTop(s_hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001170# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 return TRUE;
1172
1173 case WM_COMMAND:
1174 *bUserAbort = TRUE;
1175 EnableWindow(GetParent(hDlg), TRUE);
1176 DestroyWindow(hDlg);
1177 hDlgPrint = NULL;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001178# ifdef FEAT_GETTEXT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 DeleteObject(hfont);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001180# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 return TRUE;
1182 }
1183 return FALSE;
1184}
1185
1186 static BOOL CALLBACK
Bram Moolenaar1266d672017-02-01 13:43:36 +01001187AbortProc(HDC hdcPrn UNUSED, int iCode UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188{
1189 MSG msg;
1190
K.Takatab7057bd2022-01-21 11:37:07 +00001191 while (!*bUserAbort && PeekMessageW(&msg, NULL, 0, 0, PM_REMOVE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 {
K.Takatab7057bd2022-01-21 11:37:07 +00001193 if (!hDlgPrint || !IsDialogMessageW(hDlgPrint, &msg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 {
1195 TranslateMessage(&msg);
K.Takatab7057bd2022-01-21 11:37:07 +00001196 DispatchMessageW(&msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 }
1198 }
1199 return !*bUserAbort;
1200}
1201
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001202# if !defined(FEAT_GUI) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203
Bram Moolenaar26fdd7d2011-11-30 13:42:44 +01001204 static UINT_PTR CALLBACK
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205PrintHookProc(
1206 HWND hDlg, // handle to dialog box
1207 UINT uiMsg, // message identifier
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02001208 WPARAM wParam UNUSED, // message parameter
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 LPARAM lParam // message parameter
1210 )
1211{
1212 HWND hwndOwner;
1213 RECT rc, rcDlg, rcOwner;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001214 PRINTDLGW *pPD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001216 if (uiMsg != WM_INITDIALOG)
1217 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001219 // Get the owner window and dialog box rectangles.
1220 if ((hwndOwner = GetParent(hDlg)) == NULL)
1221 hwndOwner = GetDesktopWindow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001223 GetWindowRect(hwndOwner, &rcOwner);
1224 GetWindowRect(hDlg, &rcDlg);
1225 CopyRect(&rc, &rcOwner);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001227 // Offset the owner and dialog box rectangles so that
1228 // right and bottom values represent the width and
1229 // height, and then offset the owner again to discard
1230 // space taken up by the dialog box.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001232 OffsetRect(&rcDlg, -rcDlg.left, -rcDlg.top);
1233 OffsetRect(&rc, -rc.left, -rc.top);
1234 OffsetRect(&rc, -rcDlg.right, -rcDlg.bottom);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001236 // The new position is the sum of half the remaining
1237 // space and the owner's original position.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001239 SetWindowPos(hDlg,
1240 HWND_TOP,
1241 rcOwner.left + (rc.right / 2),
1242 rcOwner.top + (rc.bottom / 2),
1243 0, 0, // ignores size arguments
1244 SWP_NOSIZE);
1245
1246 // tackle the printdlg copiesctrl problem
1247 pPD = (PRINTDLGW *)lParam;
1248 pPD->nCopies = (WORD)pPD->lCustData;
1249 SetDlgItemInt( hDlg, edt3, pPD->nCopies, FALSE );
1250 // Bring the window to top
1251 BringWindowToTop(GetParent(hDlg));
1252 SetForegroundWindow(hDlg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253
1254 return FALSE;
1255}
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001256# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257
1258 void
1259mch_print_cleanup(void)
1260{
1261 int pifItalic;
1262 int pifBold;
1263 int pifUnderline;
1264
1265 for (pifBold = 0; pifBold <= 1; pifBold++)
1266 for (pifItalic = 0; pifItalic <= 1; pifItalic++)
1267 for (pifUnderline = 0; pifUnderline <= 1; pifUnderline++)
1268 DeleteObject(prt_font_handles[pifBold][pifItalic][pifUnderline]);
1269
1270 if (prt_dlg.hDC != NULL)
1271 DeleteDC(prt_dlg.hDC);
1272 if (!*bUserAbort)
1273 SendMessage(hDlgPrint, WM_COMMAND, 0, 0);
1274}
1275
1276 static int
1277to_device_units(int idx, int dpi, int physsize, int offset, int def_number)
1278{
1279 int ret = 0;
1280 int u;
1281 int nr;
1282
1283 u = prt_get_unit(idx);
1284 if (u == PRT_UNIT_NONE)
1285 {
1286 u = PRT_UNIT_PERC;
1287 nr = def_number;
1288 }
1289 else
1290 nr = printer_opts[idx].number;
1291
1292 switch (u)
1293 {
1294 case PRT_UNIT_PERC:
1295 ret = (physsize * nr) / 100;
1296 break;
1297 case PRT_UNIT_INCH:
1298 ret = (nr * dpi);
1299 break;
1300 case PRT_UNIT_MM:
1301 ret = (nr * 10 * dpi) / 254;
1302 break;
1303 case PRT_UNIT_POINT:
1304 ret = (nr * 10 * dpi) / 720;
1305 break;
1306 }
1307
1308 if (ret < offset)
1309 return 0;
1310 else
1311 return ret - offset;
1312}
1313
1314 static int
1315prt_get_cpl(void)
1316{
1317 int hr;
1318 int phyw;
1319 int dvoff;
1320 int rev_offset;
1321 int dpi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322
1323 GetTextMetrics(prt_dlg.hDC, &prt_tm);
1324 prt_line_height = prt_tm.tmHeight + prt_tm.tmExternalLeading;
1325
1326 hr = GetDeviceCaps(prt_dlg.hDC, HORZRES);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 phyw = GetDeviceCaps(prt_dlg.hDC, PHYSICALWIDTH);
1328 dvoff = GetDeviceCaps(prt_dlg.hDC, PHYSICALOFFSETX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 dpi = GetDeviceCaps(prt_dlg.hDC, LOGPIXELSX);
1330
1331 rev_offset = phyw - (dvoff + hr);
1332
1333 prt_left_margin = to_device_units(OPT_PRINT_LEFT, dpi, phyw, dvoff, 10);
1334 if (prt_use_number())
1335 {
1336 prt_number_width = PRINT_NUMBER_WIDTH * prt_tm.tmAveCharWidth;
1337 prt_left_margin += prt_number_width;
1338 }
1339 else
1340 prt_number_width = 0;
1341
1342 prt_right_margin = hr - to_device_units(OPT_PRINT_RIGHT, dpi, phyw,
1343 rev_offset, 5);
1344
1345 return (prt_right_margin - prt_left_margin) / prt_tm.tmAveCharWidth;
1346}
1347
1348 static int
1349prt_get_lpp(void)
1350{
1351 int vr;
1352 int phyw;
1353 int dvoff;
1354 int rev_offset;
1355 int bottom_margin;
1356 int dpi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357
1358 vr = GetDeviceCaps(prt_dlg.hDC, VERTRES);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 phyw = GetDeviceCaps(prt_dlg.hDC, PHYSICALHEIGHT);
1360 dvoff = GetDeviceCaps(prt_dlg.hDC, PHYSICALOFFSETY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 dpi = GetDeviceCaps(prt_dlg.hDC, LOGPIXELSY);
1362
1363 rev_offset = phyw - (dvoff + vr);
1364
1365 prt_top_margin = to_device_units(OPT_PRINT_TOP, dpi, phyw, dvoff, 5);
1366
Bram Moolenaar0f873732019-12-05 20:28:46 +01001367 // adjust top margin if there is a header
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 prt_top_margin += prt_line_height * prt_header_height();
1369
1370 bottom_margin = vr - to_device_units(OPT_PRINT_BOT, dpi, phyw,
1371 rev_offset, 5);
1372
1373 return (bottom_margin - prt_top_margin) / prt_line_height;
1374}
1375
1376 int
1377mch_print_init(prt_settings_T *psettings, char_u *jobname, int forceit)
1378{
1379 static HGLOBAL stored_dm = NULL;
1380 static HGLOBAL stored_devn = NULL;
1381 static int stored_nCopies = 1;
1382 static int stored_nFlags = 0;
1383
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001384 LOGFONTW fLogFont;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 int pifItalic;
1386 int pifBold;
1387 int pifUnderline;
1388
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001389 DEVMODEW *mem;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 DEVNAMES *devname;
1391 int i;
Bram Moolenaarb391e1f2023-01-16 19:51:03 +00001392 DWORD err;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393
1394 bUserAbort = &(psettings->user_abort);
Bram Moolenaara80faa82020-04-12 19:37:17 +02001395 CLEAR_FIELD(prt_dlg);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001396 prt_dlg.lStructSize = sizeof(PRINTDLGW);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001397# if !defined(FEAT_GUI) || defined(VIMDLL)
1398# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001399 if (!gui.in_use)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001400# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01001401 GetConsoleHwnd(); // get value of s_hwnd
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001402# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 prt_dlg.hwndOwner = s_hwnd;
1404 prt_dlg.Flags = PD_NOPAGENUMS | PD_NOSELECTION | PD_RETURNDC;
1405 if (!forceit)
1406 {
1407 prt_dlg.hDevMode = stored_dm;
1408 prt_dlg.hDevNames = stored_devn;
1409 prt_dlg.lCustData = stored_nCopies; // work around bug in print dialog
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001410# if !defined(FEAT_GUI) || defined(VIMDLL)
1411# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001412 if (!gui.in_use)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001413# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001414 {
1415 /*
1416 * Use hook to prevent console window being sent to back
1417 */
1418 prt_dlg.lpfnPrintHook = PrintHookProc;
1419 prt_dlg.Flags |= PD_ENABLEPRINTHOOK;
1420 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001421# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 prt_dlg.Flags |= stored_nFlags;
1423 }
1424
1425 /*
1426 * If bang present, return default printer setup with no dialog
1427 * never show dialog if we are running over telnet
1428 */
1429 if (forceit
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001430# if !defined(FEAT_GUI) || defined(VIMDLL)
1431# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001432 || (!gui.in_use && !term_console)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001433# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 || !term_console
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001435# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001436# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 )
1438 {
1439 prt_dlg.Flags |= PD_RETURNDEFAULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 /*
1441 * MSDN suggests setting the first parameter to WINSPOOL for
1442 * NT, but NULL appears to work just as well.
1443 */
1444 if (*p_pdev != NUL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001445 prt_dlg.hDC = CreateDC(NULL, (LPCSTR)p_pdev, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 {
1448 prt_dlg.Flags |= PD_RETURNDEFAULT;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001449 if (PrintDlgW(&prt_dlg) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450 goto init_fail_dlg;
1451 }
1452 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001453 else if (PrintDlgW(&prt_dlg) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 goto init_fail_dlg;
1455 else
1456 {
1457 /*
1458 * keep the previous driver context
1459 */
1460 stored_dm = prt_dlg.hDevMode;
1461 stored_devn = prt_dlg.hDevNames;
1462 stored_nFlags = prt_dlg.Flags;
1463 stored_nCopies = prt_dlg.nCopies;
1464 }
1465
1466 if (prt_dlg.hDC == NULL)
1467 {
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00001468 emsg(_(e_printer_selection_failed));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 mch_print_cleanup();
1470 return FALSE;
1471 }
1472
Bram Moolenaar0f873732019-12-05 20:28:46 +01001473 // Not all printer drivers report the support of color (or grey) in the
1474 // same way. Let's set has_color if there appears to be some way to print
1475 // more than B&W.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 i = GetDeviceCaps(prt_dlg.hDC, NUMCOLORS);
1477 psettings->has_color = (GetDeviceCaps(prt_dlg.hDC, BITSPIXEL) > 1
1478 || GetDeviceCaps(prt_dlg.hDC, PLANES) > 1
1479 || i > 2 || i == -1);
1480
Bram Moolenaar0f873732019-12-05 20:28:46 +01001481 // Ensure all font styles are baseline aligned
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 SetTextAlign(prt_dlg.hDC, TA_BASELINE|TA_LEFT);
1483
1484 /*
1485 * On some windows systems the nCopies parameter is not
1486 * passed back correctly. It must be retrieved from the
1487 * hDevMode struct.
1488 */
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001489 mem = (DEVMODEW *)GlobalLock(prt_dlg.hDevMode);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 if (mem != NULL)
1491 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 if (mem->dmCopies != 1)
1493 stored_nCopies = mem->dmCopies;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494 if ((mem->dmFields & DM_DUPLEX) && (mem->dmDuplex & ~DMDUP_SIMPLEX))
1495 psettings->duplex = TRUE;
1496 if ((mem->dmFields & DM_COLOR) && (mem->dmColor & DMCOLOR_COLOR))
1497 psettings->has_color = TRUE;
1498 }
1499 GlobalUnlock(prt_dlg.hDevMode);
1500
1501 devname = (DEVNAMES *)GlobalLock(prt_dlg.hDevNames);
1502 if (devname != 0)
1503 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001504 WCHAR *wprinter_name = (WCHAR *)devname + devname->wDeviceOffset;
1505 WCHAR *wport_name = (WCHAR *)devname + devname->wOutputOffset;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001506 char_u *text = (char_u *)_("to %s on %s");
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001507 char_u *printer_name = utf16_to_enc(wprinter_name, NULL);
1508 char_u *port_name = utf16_to_enc(wport_name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001510 if (printer_name != NULL && port_name != NULL)
Bram Moolenaar964b3742019-05-24 18:54:09 +02001511 prt_name = alloc(STRLEN(printer_name)
1512 + STRLEN(port_name) + STRLEN(text));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 if (prt_name != NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001514 wsprintf((char *)prt_name, (const char *)text,
1515 printer_name, port_name);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001516 vim_free(printer_name);
1517 vim_free(port_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 }
1519 GlobalUnlock(prt_dlg.hDevNames);
1520
1521 /*
1522 * Initialise the font according to 'printfont'
1523 */
Bram Moolenaara80faa82020-04-12 19:37:17 +02001524 CLEAR_FIELD(fLogFont);
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001525 if (get_logfont(&fLogFont, p_pfn, prt_dlg.hDC, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 {
Bram Moolenaard88be5b2022-01-04 19:57:55 +00001527 semsg(_(e_unknown_printer_font_str), p_pfn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 mch_print_cleanup();
1529 return FALSE;
1530 }
1531
1532 for (pifBold = 0; pifBold <= 1; pifBold++)
1533 for (pifItalic = 0; pifItalic <= 1; pifItalic++)
1534 for (pifUnderline = 0; pifUnderline <= 1; pifUnderline++)
1535 {
1536 fLogFont.lfWeight = boldface[pifBold];
1537 fLogFont.lfItalic = pifItalic;
1538 fLogFont.lfUnderline = pifUnderline;
1539 prt_font_handles[pifBold][pifItalic][pifUnderline]
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001540 = CreateFontIndirectW(&fLogFont);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 }
1542
1543 SetBkMode(prt_dlg.hDC, OPAQUE);
1544 SelectObject(prt_dlg.hDC, prt_font_handles[0][0][0]);
1545
1546 /*
1547 * Fill in the settings struct
1548 */
1549 psettings->chars_per_line = prt_get_cpl();
1550 psettings->lines_per_page = prt_get_lpp();
Bram Moolenaar7ddc6422014-09-27 11:18:19 +02001551 if (prt_dlg.Flags & PD_USEDEVMODECOPIESANDCOLLATE)
1552 {
1553 psettings->n_collated_copies = (prt_dlg.Flags & PD_COLLATE)
1554 ? prt_dlg.nCopies : 1;
1555 psettings->n_uncollated_copies = (prt_dlg.Flags & PD_COLLATE)
1556 ? 1 : prt_dlg.nCopies;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557
Bram Moolenaar7ddc6422014-09-27 11:18:19 +02001558 if (psettings->n_collated_copies == 0)
1559 psettings->n_collated_copies = 1;
1560
1561 if (psettings->n_uncollated_copies == 0)
1562 psettings->n_uncollated_copies = 1;
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01001563 }
1564 else
1565 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 psettings->n_collated_copies = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 psettings->n_uncollated_copies = 1;
Bram Moolenaar7ddc6422014-09-27 11:18:19 +02001568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569
1570 psettings->jobname = jobname;
1571
1572 return TRUE;
1573
1574init_fail_dlg:
Bram Moolenaarb391e1f2023-01-16 19:51:03 +00001575 err = CommDlgExtendedError();
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001576 if (err)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 {
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001578 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001580 // I suspect FormatMessage() doesn't work for values returned by
1581 // CommDlgExtendedError(). What does?
1582 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
1583 FORMAT_MESSAGE_FROM_SYSTEM |
1584 FORMAT_MESSAGE_IGNORE_INSERTS,
1585 NULL, err, 0, (LPTSTR)(&buf), 0, NULL);
1586 semsg(_(e_print_error_str),
1587 buf == NULL ? (char_u *)_("Unknown") : buf);
1588 LocalFree((LPVOID)(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 }
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00001590 else
1591 msg_clr_eos(); // Maybe canceled
1592
1593 mch_print_cleanup();
1594 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595}
1596
1597
1598 int
1599mch_print_begin(prt_settings_T *psettings)
1600{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001601 int ret = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 char szBuffer[300];
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001603 WCHAR *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001605 hDlgPrint = CreateDialog(g_hinst, TEXT("PrintDlgBox"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 prt_dlg.hwndOwner, PrintDlgProc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 SetAbortProc(prt_dlg.hDC, AbortProc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 wsprintf(szBuffer, _("Printing '%s'"), gettail(psettings->jobname));
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001609 vimSetDlgItemText(hDlgPrint, IDC_PRINTTEXT1, (char_u *)szBuffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001611 wp = enc_to_utf16(psettings->jobname, NULL);
Bram Moolenaar2290b1f2018-05-13 17:30:45 +02001612 if (wp != NULL)
1613 {
1614 DOCINFOW di;
1615
Bram Moolenaara80faa82020-04-12 19:37:17 +02001616 CLEAR_FIELD(di);
Bram Moolenaar2290b1f2018-05-13 17:30:45 +02001617 di.cbSize = sizeof(di);
1618 di.lpszDocName = wp;
1619 ret = StartDocW(prt_dlg.hDC, &di);
1620 vim_free(wp);
1621 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001623# ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01001624 // Give focus back to main window (when using MDI).
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001625# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001626 if (gui.in_use)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001627# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001628 SetFocus(s_hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001629# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630
1631 return (ret > 0);
1632}
1633
1634 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01001635mch_print_end(prt_settings_T *psettings UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636{
1637 EndDoc(prt_dlg.hDC);
1638 if (!*bUserAbort)
1639 SendMessage(hDlgPrint, WM_COMMAND, 0, 0);
1640}
1641
1642 int
1643mch_print_end_page(void)
1644{
1645 return (EndPage(prt_dlg.hDC) > 0);
1646}
1647
1648 int
1649mch_print_begin_page(char_u *msg)
1650{
1651 if (msg != NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001652 vimSetDlgItemText(hDlgPrint, IDC_PROGRESS, msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 return (StartPage(prt_dlg.hDC) > 0);
1654}
1655
1656 int
1657mch_print_blank_page(void)
1658{
1659 return (mch_print_begin_page(NULL) ? (mch_print_end_page()) : FALSE);
1660}
1661
1662static int prt_pos_x = 0;
1663static int prt_pos_y = 0;
1664
1665 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001666mch_print_start_line(int margin, int page_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667{
1668 if (margin)
1669 prt_pos_x = -prt_number_width;
1670 else
1671 prt_pos_x = 0;
1672 prt_pos_y = page_line * prt_line_height
1673 + prt_tm.tmAscent + prt_tm.tmExternalLeading;
1674}
1675
1676 int
1677mch_print_text_out(char_u *p, int len)
1678{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 SIZE sz;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001680 WCHAR *wp;
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001681 int wlen = len;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001682 int ret = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001684 wp = enc_to_utf16(p, &wlen);
1685 if (wp == NULL)
1686 return FALSE;
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001687
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001688 TextOutW(prt_dlg.hDC, prt_pos_x + prt_left_margin,
1689 prt_pos_y + prt_top_margin, wp, wlen);
1690 GetTextExtentPoint32W(prt_dlg.hDC, wp, wlen, &sz);
1691 vim_free(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 prt_pos_x += (sz.cx - prt_tm.tmOverhang);
Bram Moolenaar0f873732019-12-05 20:28:46 +01001693 // This is wrong when printing spaces for a TAB.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001694 if (p[len] != NUL)
1695 {
Bram Moolenaar1614a142019-10-06 22:00:13 +02001696 wlen = mb_ptr2len(p + len);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001697 wp = enc_to_utf16(p + len, &wlen);
1698 if (wp != NULL)
1699 {
1700 GetTextExtentPoint32W(prt_dlg.hDC, wp, 1, &sz);
1701 ret = (prt_pos_x + prt_left_margin + sz.cx > prt_right_margin);
1702 vim_free(wp);
1703 }
1704 }
1705 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706}
1707
1708 void
1709mch_print_set_font(int iBold, int iItalic, int iUnderline)
1710{
1711 SelectObject(prt_dlg.hDC, prt_font_handles[iBold][iItalic][iUnderline]);
1712}
1713
1714 void
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001715mch_print_set_bg(long_u bgcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716{
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001717 SetBkColor(prt_dlg.hDC, GetNearestColor(prt_dlg.hDC,
1718 swap_me((COLORREF)bgcol)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 /*
1720 * With a white background we can draw characters transparent, which is
1721 * good for italic characters that overlap to the next char cell.
1722 */
1723 if (bgcol == 0xffffffUL)
1724 SetBkMode(prt_dlg.hDC, TRANSPARENT);
1725 else
1726 SetBkMode(prt_dlg.hDC, OPAQUE);
1727}
1728
1729 void
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001730mch_print_set_fg(long_u fgcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731{
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001732 SetTextColor(prt_dlg.hDC, GetNearestColor(prt_dlg.hDC,
1733 swap_me((COLORREF)fgcol)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734}
1735
Bram Moolenaar0f873732019-12-05 20:28:46 +01001736#endif // FEAT_PRINTER && !FEAT_POSTSCRIPT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737
Bram Moolenaar58d98232005-07-23 22:25:46 +00001738
1739
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740#if defined(FEAT_SHORTCUT) || defined(PROTO)
Bram Moolenaar82881492012-11-20 16:53:39 +01001741# ifndef PROTO
1742# include <shlobj.h>
1743# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744
Bram Moolenaar4a792c82019-06-06 12:22:41 +02001745# define is_path_sep(c) ((c) == L'\\' || (c) == L'/')
1746
1747 static int
1748is_reparse_point_included(LPCWSTR fname)
1749{
1750 LPCWSTR p = fname, q;
1751 WCHAR buf[MAX_PATH];
1752 DWORD attr;
1753
Keith Thompson184f71c2024-01-04 21:19:04 +01001754 if (SAFE_isalpha(p[0]) && p[1] == L':' && is_path_sep(p[2]))
Bram Moolenaar4a792c82019-06-06 12:22:41 +02001755 p += 3;
1756 else if (is_path_sep(p[0]) && is_path_sep(p[1]))
1757 p += 2;
1758
1759 while (*p != L'\0')
1760 {
1761 q = wcspbrk(p, L"\\/");
1762 if (q == NULL)
1763 p = q = fname + wcslen(fname);
1764 else
1765 p = q + 1;
1766 if (q - fname >= MAX_PATH)
1767 return FALSE;
1768 wcsncpy(buf, fname, q - fname);
1769 buf[q - fname] = L'\0';
1770 attr = GetFileAttributesW(buf);
1771 if (attr != INVALID_FILE_ATTRIBUTES
1772 && (attr & FILE_ATTRIBUTE_REPARSE_POINT) != 0)
1773 return TRUE;
1774 }
1775 return FALSE;
1776}
1777
AmberArrf5d0f542023-08-20 20:03:45 +02001778/*
1779 * Return the resolved file path, NULL if "fname" is an AppExecLink reparse
1780 * point, already fully resolved, or it doesn't exists.
1781 */
1782 char_u *
Bram Moolenaardce1e892019-02-10 23:18:53 +01001783resolve_reparse_point(char_u *fname)
1784{
1785 HANDLE h = INVALID_HANDLE_VALUE;
1786 DWORD size;
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001787 WCHAR *p, *wp;
Bram Moolenaardce1e892019-02-10 23:18:53 +01001788 char_u *rfname = NULL;
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001789 WCHAR *buff = NULL;
Bram Moolenaardce1e892019-02-10 23:18:53 +01001790
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001791 p = enc_to_utf16(fname, NULL);
1792 if (p == NULL)
1793 goto fail;
1794
Bram Moolenaar4a792c82019-06-06 12:22:41 +02001795 if (!is_reparse_point_included(p))
1796 {
1797 vim_free(p);
1798 goto fail;
1799 }
1800
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001801 h = CreateFileW(p, 0, 0, NULL, OPEN_EXISTING,
1802 FILE_FLAG_BACKUP_SEMANTICS, NULL);
1803 vim_free(p);
Bram Moolenaardce1e892019-02-10 23:18:53 +01001804
1805 if (h == INVALID_HANDLE_VALUE)
1806 goto fail;
1807
K.Takata27b53be2022-09-18 12:25:49 +01001808 size = GetFinalPathNameByHandleW(h, NULL, 0, 0);
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001809 if (size == 0)
1810 goto fail;
1811 buff = ALLOC_MULT(WCHAR, size);
1812 if (buff == NULL)
1813 goto fail;
K.Takata27b53be2022-09-18 12:25:49 +01001814 if (GetFinalPathNameByHandleW(h, buff, size, 0) == 0)
Bram Moolenaardce1e892019-02-10 23:18:53 +01001815 goto fail;
1816
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001817 if (wcsncmp(buff, L"\\\\?\\UNC\\", 8) == 0)
1818 {
1819 buff[6] = L'\\';
1820 wp = buff + 6;
1821 }
1822 else if (wcsncmp(buff, L"\\\\?\\", 4) == 0)
1823 wp = buff + 4;
Bram Moolenaardce1e892019-02-10 23:18:53 +01001824 else
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001825 wp = buff;
1826
1827 rfname = utf16_to_enc(wp, NULL);
Bram Moolenaardce1e892019-02-10 23:18:53 +01001828
1829fail:
1830 if (h != INVALID_HANDLE_VALUE)
1831 CloseHandle(h);
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001832 if (buff != NULL)
1833 vim_free(buff);
Bram Moolenaardce1e892019-02-10 23:18:53 +01001834
1835 return rfname;
1836}
1837
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838/*
1839 * When "fname" is the name of a shortcut (*.lnk) resolve the file it points
1840 * to and return that name in allocated memory.
1841 * Otherwise NULL is returned.
1842 */
Bram Moolenaardce1e892019-02-10 23:18:53 +01001843 static char_u *
1844resolve_shortcut(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845{
1846 HRESULT hr;
1847 IShellLink *psl = NULL;
1848 IPersistFile *ppf = NULL;
1849 OLECHAR wsz[MAX_PATH];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 char_u *rfname = NULL;
1851 int len;
Bram Moolenaar604729e2013-08-30 16:44:19 +02001852 IShellLinkW *pslw = NULL;
1853 WIN32_FIND_DATAW ffdw; // we get those free of charge
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854
Bram Moolenaar0f873732019-12-05 20:28:46 +01001855 // Check if the file name ends in ".lnk". Avoid calling
1856 // CoCreateInstance(), it's quite slow.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 if (fname == NULL)
1858 return rfname;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001859 len = (int)STRLEN(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 if (len <= 4 || STRNICMP(fname + len - 4, ".lnk", 4) != 0)
1861 return rfname;
1862
1863 CoInitialize(NULL);
1864
1865 // create a link manager object and request its interface
1866 hr = CoCreateInstance(
1867 &CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001868 &IID_IShellLinkW, (void**)&pslw);
1869 if (hr == S_OK)
1870 {
1871 WCHAR *p = enc_to_utf16(fname, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001873 if (p != NULL)
1874 {
1875 // Get a pointer to the IPersistFile interface.
1876 hr = pslw->lpVtbl->QueryInterface(
1877 pslw, &IID_IPersistFile, (void**)&ppf);
1878 if (hr != S_OK)
1879 goto shortcut_errorw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001881 // "load" the name and resolve the link
1882 hr = ppf->lpVtbl->Load(ppf, p, STGM_READ);
1883 if (hr != S_OK)
1884 goto shortcut_errorw;
1885# if 0 // This makes Vim wait a long time if the target does not exist.
1886 hr = pslw->lpVtbl->Resolve(pslw, NULL, SLR_NO_UI);
1887 if (hr != S_OK)
1888 goto shortcut_errorw;
Bram Moolenaar604729e2013-08-30 16:44:19 +02001889# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001891 // Get the path to the link target.
1892 ZeroMemory(wsz, MAX_PATH * sizeof(WCHAR));
1893 hr = pslw->lpVtbl->GetPath(pslw, wsz, MAX_PATH, &ffdw, 0);
1894 if (hr == S_OK && wsz[0] != NUL)
1895 rfname = utf16_to_enc(wsz, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001897shortcut_errorw:
1898 vim_free(p);
1899 }
1900 }
1901
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 // Release all interface pointers (both belong to the same object)
1903 if (ppf != NULL)
1904 ppf->lpVtbl->Release(ppf);
1905 if (psl != NULL)
1906 psl->lpVtbl->Release(psl);
Bram Moolenaar604729e2013-08-30 16:44:19 +02001907 if (pslw != NULL)
1908 pslw->lpVtbl->Release(pslw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909
1910 CoUninitialize();
1911 return rfname;
1912}
Bram Moolenaardce1e892019-02-10 23:18:53 +01001913
1914 char_u *
1915mch_resolve_path(char_u *fname, int reparse_point)
1916{
1917 char_u *path = resolve_shortcut(fname);
1918
1919 if (path == NULL && reparse_point)
1920 path = resolve_reparse_point(fname);
1921 return path;
1922}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923#endif
1924
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001925#if (defined(FEAT_EVAL) && (!defined(FEAT_GUI) || defined(VIMDLL))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926/*
1927 * Bring ourselves to the foreground. Does work if the OS doesn't allow it.
1928 */
1929 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001930win32_set_foreground(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931{
Bram Moolenaar0f873732019-12-05 20:28:46 +01001932 GetConsoleHwnd(); // get value of s_hwnd
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 if (s_hwnd != 0)
1934 SetForegroundWindow(s_hwnd);
1935}
1936#endif
1937
1938#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
1939/*
1940 * Client-server code for Vim
1941 *
1942 * Originally written by Paul Moore
1943 */
1944
Bram Moolenaar0f873732019-12-05 20:28:46 +01001945// In order to handle inter-process messages, we need to have a window. But
1946// the functions in this module can be called before the main GUI window is
1947// created (and may also be called in the console version, where there is no
1948// GUI window at all).
1949//
1950// So we create a hidden window, and arrange to destroy it on exit.
1951HWND message_window = 0; // window that's handling messages
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001953# define VIM_CLASSNAME "VIM_MESSAGES"
1954# define VIM_CLASSNAME_LEN (sizeof(VIM_CLASSNAME) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955
K.Takataf9f2a332022-06-17 20:05:40 +01001956// Timeout for sending a message to another Vim instance. Normally this works
1957// instantly, but it may hang when the other Vim instance is halted.
1958# define SENDMESSAGE_TIMEOUT (5 * 1000)
1959
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001960// Communication is via WM_COPYDATA messages. The message type is sent in
Bram Moolenaar0f873732019-12-05 20:28:46 +01001961// the dwData parameter. Types are defined here.
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001962# define COPYDATA_KEYS 0
1963# define COPYDATA_REPLY 1
1964# define COPYDATA_EXPR 10
1965# define COPYDATA_RESULT 11
1966# define COPYDATA_ERROR_RESULT 12
1967# define COPYDATA_ENCODING 20
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968
Bram Moolenaar0f873732019-12-05 20:28:46 +01001969// This is a structure containing a server HWND and its name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970struct server_id
1971{
1972 HWND hwnd;
1973 char_u *name;
1974};
1975
Bram Moolenaar0f873732019-12-05 20:28:46 +01001976// Last received 'encoding' that the client uses.
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001977static char_u *client_enc = NULL;
1978
1979/*
1980 * Tell the other side what encoding we are using.
K.Takataf9f2a332022-06-17 20:05:40 +01001981 * Return -1 if timeout happens. Other errors are ignored.
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001982 */
K.Takataf9f2a332022-06-17 20:05:40 +01001983 static int
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001984serverSendEnc(HWND target)
1985{
1986 COPYDATASTRUCT data;
1987
1988 data.dwData = COPYDATA_ENCODING;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001989 data.cbData = (DWORD)STRLEN(p_enc) + 1;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001990 data.lpData = p_enc;
K.Takataf9f2a332022-06-17 20:05:40 +01001991 if (SendMessageTimeout(target, WM_COPYDATA,
1992 (WPARAM)message_window, (LPARAM)&data,
1993 SMTO_ABORTIFHUNG, SENDMESSAGE_TIMEOUT, NULL) == 0)
1994 return -1;
1995 return 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001996}
1997
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998/*
1999 * Clean up on exit. This destroys the hidden message window.
2000 */
2001 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002CleanUpMessaging(void)
2003{
Yegappan Lakshmanana41e2212023-01-16 18:19:05 +00002004 if (message_window == 0)
2005 return;
2006
2007 DestroyWindow(message_window);
2008 message_window = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009}
2010
2011static int save_reply(HWND server, char_u *reply, int expr);
2012
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002013/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 * The window procedure for the hidden message window.
2015 * It handles callback messages and notifications from servers.
2016 * In order to process these messages, it is necessary to run a
2017 * message loop. Code which may run before the main message loop
2018 * is started (in the GUI) is careful to pump messages when it needs
2019 * to. Features which require message delivery during normal use will
2020 * not work in the console version - this basically means those
2021 * features which allow Vim to act as a server, rather than a client.
2022 */
2023 static LRESULT CALLBACK
2024Messaging_WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
2025{
2026 if (msg == WM_COPYDATA)
2027 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002028 // This is a message from another Vim. The dwData member of the
2029 // COPYDATASTRUCT determines the type of message:
2030 // COPYDATA_ENCODING:
2031 // The encoding that the client uses. Following messages will
2032 // use this encoding, convert if needed.
2033 // COPYDATA_KEYS:
2034 // A key sequence. We are a server, and a client wants these keys
2035 // adding to the input queue.
2036 // COPYDATA_REPLY:
2037 // A reply. We are a client, and a server has sent this message
2038 // in response to a request. (server2client())
2039 // COPYDATA_EXPR:
2040 // An expression. We are a server, and a client wants us to
2041 // evaluate this expression.
2042 // COPYDATA_RESULT:
2043 // A reply. We are a client, and a server has sent this message
2044 // in response to a COPYDATA_EXPR.
2045 // COPYDATA_ERROR_RESULT:
2046 // A reply. We are a client, and a server has sent this message
2047 // in response to a COPYDATA_EXPR that failed to evaluate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 COPYDATASTRUCT *data = (COPYDATASTRUCT*)lParam;
2049 HWND sender = (HWND)wParam;
2050 COPYDATASTRUCT reply;
2051 char_u *res;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 int retval;
K.Takataf9f2a332022-06-17 20:05:40 +01002053 DWORD_PTR dwret = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002054 char_u *str;
2055 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056
2057 switch (data->dwData)
2058 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002059 case COPYDATA_ENCODING:
Bram Moolenaar0f873732019-12-05 20:28:46 +01002060 // Remember the encoding that the client uses.
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002061 vim_free(client_enc);
2062 client_enc = enc_canonize((char_u *)data->lpData);
2063 return 1;
2064
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065 case COPYDATA_KEYS:
Bram Moolenaar0f873732019-12-05 20:28:46 +01002066 // Remember who sent this, for <client>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 clientWindow = sender;
2068
Bram Moolenaar0f873732019-12-05 20:28:46 +01002069 // Add the received keys to the input buffer. The loop waiting
2070 // for the user to do something should check the input buffer.
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002071 str = serverConvert(client_enc, (char_u *)data->lpData, &tofree);
2072 server_to_input_buf(str);
2073 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074
2075# ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01002076 // Wake up the main GUI loop.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002077# ifdef VIMDLL
2078 if (gui.in_use)
2079# endif
2080 if (s_hwnd != 0)
2081 PostMessage(s_hwnd, WM_NULL, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082# endif
2083 return 1;
2084
2085 case COPYDATA_EXPR:
Bram Moolenaar0f873732019-12-05 20:28:46 +01002086 // Remember who sent this, for <client>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 clientWindow = sender;
2088
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002089 str = serverConvert(client_enc, (char_u *)data->lpData, &tofree);
2090 res = eval_client_expr_to_string(str);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002091
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 if (res == NULL)
2093 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00002094 char *err = _(e_invalid_expression_received);
Bram Moolenaar15bf76d2017-03-18 16:18:37 +01002095 size_t len = STRLEN(str) + STRLEN(err) + 5;
2096
Bram Moolenaar964b3742019-05-24 18:54:09 +02002097 res = alloc(len);
Bram Moolenaar15bf76d2017-03-18 16:18:37 +01002098 if (res != NULL)
2099 vim_snprintf((char *)res, len, "%s: \"%s\"", err, str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 reply.dwData = COPYDATA_ERROR_RESULT;
2101 }
2102 else
2103 reply.dwData = COPYDATA_RESULT;
2104 reply.lpData = res;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002105 reply.cbData = (DWORD)STRLEN(res) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106
K.Takataf9f2a332022-06-17 20:05:40 +01002107 if (serverSendEnc(sender) < 0)
2108 retval = -1;
2109 else
2110 {
2111 if (SendMessageTimeout(sender, WM_COPYDATA,
2112 (WPARAM)message_window, (LPARAM)&reply,
2113 SMTO_ABORTIFHUNG, SENDMESSAGE_TIMEOUT, &dwret) == 0)
2114 retval = -1;
2115 else
2116 retval = (int)dwret;
2117 }
Bram Moolenaar15bf76d2017-03-18 16:18:37 +01002118 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 vim_free(res);
2120 return retval;
2121
2122 case COPYDATA_REPLY:
2123 case COPYDATA_RESULT:
2124 case COPYDATA_ERROR_RESULT:
2125 if (data->lpData != NULL)
2126 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002127 str = serverConvert(client_enc, (char_u *)data->lpData,
2128 &tofree);
2129 if (tofree == NULL)
2130 str = vim_strsave(str);
2131 if (save_reply(sender, str,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 (data->dwData == COPYDATA_REPLY ? 0 :
2133 (data->dwData == COPYDATA_RESULT ? 1 :
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002134 2))) == FAIL)
2135 vim_free(str);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002136 else if (data->dwData == COPYDATA_REPLY)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 {
Bram Moolenaar427d51c2013-06-16 16:01:25 +02002138 char_u winstr[30];
2139
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002140 sprintf((char *)winstr, PRINTF_HEX_LONG_U, (long_u)sender);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002141 apply_autocmds(EVENT_REMOTEREPLY, winstr, str,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 TRUE, curbuf);
2143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144 }
2145 return 1;
2146 }
2147
2148 return 0;
2149 }
2150
2151 else if (msg == WM_ACTIVATE && wParam == WA_ACTIVE)
2152 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002153 // When the message window is activated (brought to the foreground),
2154 // this actually applies to the text window.
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002155# if !defined(FEAT_GUI) || defined(VIMDLL)
2156# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002157 if (!gui.in_use)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002158# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002159 GetConsoleHwnd(); // get value of s_hwnd
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002160# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 if (s_hwnd != 0)
2162 {
2163 SetForegroundWindow(s_hwnd);
2164 return 0;
2165 }
2166 }
2167
2168 return DefWindowProc(hwnd, msg, wParam, lParam);
2169}
2170
2171/*
2172 * Initialise the message handling process. This involves creating a window
2173 * to handle messages - the window will not be visible.
2174 */
2175 void
2176serverInitMessaging(void)
2177{
2178 WNDCLASS wndclass;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179
Bram Moolenaar0f873732019-12-05 20:28:46 +01002180 // Clean up on exit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 atexit(CleanUpMessaging);
2182
Bram Moolenaar0f873732019-12-05 20:28:46 +01002183 // Register a window class - we only really care
2184 // about the window procedure
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 wndclass.style = 0;
2186 wndclass.lpfnWndProc = Messaging_WndProc;
2187 wndclass.cbClsExtra = 0;
2188 wndclass.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002189 wndclass.hInstance = g_hinst;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 wndclass.hIcon = NULL;
2191 wndclass.hCursor = NULL;
2192 wndclass.hbrBackground = NULL;
2193 wndclass.lpszMenuName = NULL;
2194 wndclass.lpszClassName = VIM_CLASSNAME;
2195 RegisterClass(&wndclass);
2196
Bram Moolenaar0f873732019-12-05 20:28:46 +01002197 // Create the message window. It will be hidden, so the details don't
2198 // matter. Don't use WS_OVERLAPPEDWINDOW, it will make a shortcut remove
2199 // focus from gvim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 message_window = CreateWindow(VIM_CLASSNAME, "",
2201 WS_POPUPWINDOW | WS_CAPTION,
2202 CW_USEDEFAULT, CW_USEDEFAULT,
2203 100, 100, NULL, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002204 g_hinst, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205}
2206
Bram Moolenaar0f873732019-12-05 20:28:46 +01002207// Used by serverSendToVim() to find an alternate server name.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002208static char_u *altname_buf_ptr = NULL;
2209
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210/*
2211 * Get the title of the window "hwnd", which is the Vim server name, in
2212 * "name[namelen]" and return the length.
2213 * Returns zero if window "hwnd" is not a Vim server.
2214 */
2215 static int
2216getVimServerName(HWND hwnd, char *name, int namelen)
2217{
2218 int len;
2219 char buffer[VIM_CLASSNAME_LEN + 1];
2220
Bram Moolenaar0f873732019-12-05 20:28:46 +01002221 // Ignore windows which aren't Vim message windows
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 len = GetClassName(hwnd, buffer, sizeof(buffer));
2223 if (len != VIM_CLASSNAME_LEN || STRCMP(buffer, VIM_CLASSNAME) != 0)
2224 return 0;
2225
Bram Moolenaar0f873732019-12-05 20:28:46 +01002226 // Get the title of the window
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 return GetWindowText(hwnd, name, namelen);
2228}
2229
2230 static BOOL CALLBACK
2231enumWindowsGetServer(HWND hwnd, LPARAM lparam)
2232{
2233 struct server_id *id = (struct server_id *)lparam;
2234 char server[MAX_PATH];
2235
Bram Moolenaar0f873732019-12-05 20:28:46 +01002236 // Get the title of the window
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 if (getVimServerName(hwnd, server, sizeof(server)) == 0)
2238 return TRUE;
2239
Bram Moolenaar0f873732019-12-05 20:28:46 +01002240 // If this is the server we're looking for, return its HWND
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 if (STRICMP(server, id->name) == 0)
2242 {
2243 id->hwnd = hwnd;
2244 return FALSE;
2245 }
2246
Bram Moolenaar0f873732019-12-05 20:28:46 +01002247 // If we are looking for an alternate server, remember this name.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002248 if (altname_buf_ptr != NULL
2249 && STRNICMP(server, id->name, STRLEN(id->name)) == 0
2250 && vim_isdigit(server[STRLEN(id->name)]))
2251 {
2252 STRCPY(altname_buf_ptr, server);
Bram Moolenaar0f873732019-12-05 20:28:46 +01002253 altname_buf_ptr = NULL; // don't use another name
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002254 }
2255
Bram Moolenaar0f873732019-12-05 20:28:46 +01002256 // Otherwise, keep looking
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 return TRUE;
2258}
2259
2260 static BOOL CALLBACK
2261enumWindowsGetNames(HWND hwnd, LPARAM lparam)
2262{
2263 garray_T *ga = (garray_T *)lparam;
2264 char server[MAX_PATH];
2265
Bram Moolenaar0f873732019-12-05 20:28:46 +01002266 // Get the title of the window
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 if (getVimServerName(hwnd, server, sizeof(server)) == 0)
2268 return TRUE;
2269
Bram Moolenaar0f873732019-12-05 20:28:46 +01002270 // Add the name to the list
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002271 ga_concat(ga, (char_u *)server);
2272 ga_concat(ga, (char_u *)"\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 return TRUE;
2274}
2275
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002276struct enum_windows_s
2277{
2278 WNDENUMPROC lpEnumFunc;
2279 LPARAM lParam;
2280};
2281
2282 static BOOL CALLBACK
2283enum_windows_child(HWND hwnd, LPARAM lParam)
2284{
2285 struct enum_windows_s *ew = (struct enum_windows_s *)lParam;
2286
2287 return (ew->lpEnumFunc)(hwnd, ew->lParam);
2288}
2289
2290 static BOOL CALLBACK
2291enum_windows_toplevel(HWND hwnd, LPARAM lParam)
2292{
2293 struct enum_windows_s *ew = (struct enum_windows_s *)lParam;
2294
Bram Moolenaar95ba5c32018-10-07 22:47:07 +02002295 if ((ew->lpEnumFunc)(hwnd, ew->lParam))
2296 return TRUE;
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002297 return EnumChildWindows(hwnd, enum_windows_child, lParam);
2298}
2299
Bram Moolenaar0f873732019-12-05 20:28:46 +01002300/*
2301 * Enumerate all windows including children.
2302 */
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002303 static BOOL
2304enum_windows(WNDENUMPROC lpEnumFunc, LPARAM lParam)
2305{
2306 struct enum_windows_s ew;
2307
2308 ew.lpEnumFunc = lpEnumFunc;
2309 ew.lParam = lParam;
2310 return EnumWindows(enum_windows_toplevel, (LPARAM)&ew);
2311}
2312
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 static HWND
2314findServer(char_u *name)
2315{
2316 struct server_id id;
2317
2318 id.name = name;
2319 id.hwnd = 0;
2320
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002321 enum_windows(enumWindowsGetServer, (LPARAM)(&id));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322
2323 return id.hwnd;
2324}
2325
2326 void
2327serverSetName(char_u *name)
2328{
2329 char_u *ok_name;
2330 HWND hwnd = 0;
2331 int i = 0;
2332 char_u *p;
2333
Bram Moolenaar0f873732019-12-05 20:28:46 +01002334 // Leave enough space for a 9-digit suffix to ensure uniqueness!
Bram Moolenaar964b3742019-05-24 18:54:09 +02002335 ok_name = alloc(STRLEN(name) + 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336
2337 STRCPY(ok_name, name);
2338 p = ok_name + STRLEN(name);
2339
2340 for (;;)
2341 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002342 // This is inefficient - we're doing an EnumWindows loop for each
2343 // possible name. It would be better to grab all names in one go,
2344 // and scan the list each time...
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 hwnd = findServer(ok_name);
2346 if (hwnd == 0)
2347 break;
2348
2349 ++i;
2350 if (i >= 1000)
2351 break;
2352
2353 sprintf((char *)p, "%d", i);
2354 }
2355
2356 if (hwnd != 0)
2357 vim_free(ok_name);
2358 else
2359 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002360 // Remember the name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 serverName = ok_name;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002362 need_maketitle = TRUE; // update Vim window title later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363
Bram Moolenaar0f873732019-12-05 20:28:46 +01002364 // Update the message window title
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002365 SetWindowText(message_window, (LPCSTR)ok_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002367# ifdef FEAT_EVAL
Bram Moolenaar0f873732019-12-05 20:28:46 +01002368 // Set the servername variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 set_vim_var_string(VV_SEND_SERVER, serverName, -1);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002370# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 }
2372}
2373
2374 char_u *
2375serverGetVimNames(void)
2376{
2377 garray_T ga;
2378
2379 ga_init2(&ga, 1, 100);
2380
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002381 enum_windows(enumWindowsGetNames, (LPARAM)(&ga));
Bram Moolenaar269ec652004-07-29 08:43:53 +00002382 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383
2384 return ga.ga_data;
2385}
2386
2387 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002388serverSendReply(
Bram Moolenaar0f873732019-12-05 20:28:46 +01002389 char_u *name, // Where to send.
2390 char_u *reply) // What to send.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391{
2392 HWND target;
2393 COPYDATASTRUCT data;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002394 long_u n = 0;
K.Takataf9f2a332022-06-17 20:05:40 +01002395 DWORD_PTR dwret = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396
Bram Moolenaar0f873732019-12-05 20:28:46 +01002397 // The "name" argument is a magic cookie obtained from expand("<client>").
2398 // It should be of the form 0xXXXXX - i.e. a C hex literal, which is the
2399 // value of the client's message window HWND.
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002400 sscanf((char *)name, SCANF_HEX_LONG_U, &n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 if (n == 0)
2402 return -1;
2403
2404 target = (HWND)n;
2405 if (!IsWindow(target))
2406 return -1;
2407
2408 data.dwData = COPYDATA_REPLY;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002409 data.cbData = (DWORD)STRLEN(reply) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 data.lpData = reply;
2411
K.Takataf9f2a332022-06-17 20:05:40 +01002412 if (serverSendEnc(target) < 0)
2413 return -1;
2414 if (SendMessageTimeout(target, WM_COPYDATA,
2415 (WPARAM)message_window, (LPARAM)&data,
2416 SMTO_ABORTIFHUNG, SENDMESSAGE_TIMEOUT, &dwret) == 0)
2417 return -1;
2418 return dwret ? 0 : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419}
2420
2421 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002422serverSendToVim(
Bram Moolenaar0f873732019-12-05 20:28:46 +01002423 char_u *name, // Where to send.
2424 char_u *cmd, // What to send.
2425 char_u **result, // Result of eval'ed expression
2426 void *ptarget, // HWND of server
2427 int asExpr, // Expression or keys?
2428 int timeout, // timeout in seconds or zero
2429 int silent) // don't complain about no server
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430{
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002431 HWND target;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 COPYDATASTRUCT data;
2433 char_u *retval = NULL;
2434 int retcode = 0;
K.Takataf9f2a332022-06-17 20:05:40 +01002435 DWORD_PTR dwret = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002436 char_u altname_buf[MAX_PATH];
2437
Bram Moolenaar0f873732019-12-05 20:28:46 +01002438 // Execute locally if no display or target is ourselves
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01002439 if (serverName != NULL && STRICMP(name, serverName) == 0)
2440 return sendToLocalVim(cmd, asExpr, result);
2441
Bram Moolenaar0f873732019-12-05 20:28:46 +01002442 // If the server name does not end in a digit then we look for an
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00002443 // alternate name. e.g. when "name" is GVIM then we may find GVIM2.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002444 if (STRLEN(name) > 1 && !vim_isdigit(name[STRLEN(name) - 1]))
2445 altname_buf_ptr = altname_buf;
2446 altname_buf[0] = NUL;
2447 target = findServer(name);
2448 altname_buf_ptr = NULL;
2449 if (target == 0 && altname_buf[0] != NUL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01002450 // Use another server name we found.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002451 target = findServer(altname_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452
2453 if (target == 0)
2454 {
2455 if (!silent)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002456 semsg(_(e_no_registered_server_named_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 return -1;
2458 }
2459
2460 if (ptarget)
2461 *(HWND *)ptarget = target;
2462
2463 data.dwData = asExpr ? COPYDATA_EXPR : COPYDATA_KEYS;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002464 data.cbData = (DWORD)STRLEN(cmd) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 data.lpData = cmd;
2466
K.Takataf9f2a332022-06-17 20:05:40 +01002467 if (serverSendEnc(target) < 0)
2468 return -1;
2469 if (SendMessageTimeout(target, WM_COPYDATA,
2470 (WPARAM)message_window, (LPARAM)&data,
2471 SMTO_ABORTIFHUNG, SENDMESSAGE_TIMEOUT, &dwret) == 0)
2472 return -1;
2473 if (dwret == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 return -1;
2475
2476 if (asExpr)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002477 retval = serverGetReply(target, &retcode, TRUE, TRUE, timeout);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478
2479 if (result == NULL)
2480 vim_free(retval);
2481 else
Bram Moolenaar0f873732019-12-05 20:28:46 +01002482 *result = retval; // Caller assumes responsibility for freeing
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483
2484 return retcode;
2485}
2486
2487/*
2488 * Bring the server to the foreground.
2489 */
2490 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002491serverForeground(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492{
2493 HWND target = findServer(name);
2494
2495 if (target != 0)
2496 SetForegroundWindow(target);
2497}
2498
Bram Moolenaar0f873732019-12-05 20:28:46 +01002499// Replies from server need to be stored until the client picks them up via
2500// remote_read(). So we maintain a list of server-id/reply pairs.
2501// Note that there could be multiple replies from one server pending if the
2502// client is slow picking them up.
2503// We just store the replies in a simple list. When we remove an entry, we
2504// move list entries down to fill the gap.
2505// The server ID is simply the HWND.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506typedef struct
2507{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002508 HWND server; // server window
2509 char_u *reply; // reply string
2510 int expr_result; // 0 for REPLY, 1 for RESULT 2 for error
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002511} reply_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512
2513static garray_T reply_list = {0, 0, sizeof(reply_T), 5, 0};
2514
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002515# define REPLY_ITEM(i) ((reply_T *)(reply_list.ga_data) + (i))
2516# define REPLY_COUNT (reply_list.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517
Bram Moolenaar0f873732019-12-05 20:28:46 +01002518// Flag which is used to wait for a reply
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519static int reply_received = 0;
2520
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002521/*
2522 * Store a reply. "reply" must be allocated memory (or NULL).
2523 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 static int
2525save_reply(HWND server, char_u *reply, int expr)
2526{
2527 reply_T *rep;
2528
2529 if (ga_grow(&reply_list, 1) == FAIL)
2530 return FAIL;
2531
2532 rep = REPLY_ITEM(REPLY_COUNT);
2533 rep->server = server;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002534 rep->reply = reply;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 rep->expr_result = expr;
2536 if (rep->reply == NULL)
2537 return FAIL;
2538
2539 ++REPLY_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 reply_received = 1;
2541 return OK;
2542}
2543
2544/*
2545 * Get a reply from server "server".
2546 * When "expr_res" is non NULL, get the result of an expression, otherwise a
2547 * server2client() message.
2548 * When non NULL, point to return code. 0 => OK, -1 => ERROR
2549 * If "remove" is TRUE, consume the message, the caller must free it then.
2550 * if "wait" is TRUE block until a message arrives (or the server exits).
2551 */
2552 char_u *
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002553serverGetReply(HWND server, int *expr_res, int remove, int wait, int timeout)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554{
2555 int i;
2556 char_u *reply;
2557 reply_T *rep;
Bram Moolenaar15e737f2017-03-18 21:22:47 +01002558 int did_process = FALSE;
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002559 time_t start;
2560 time_t now;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561
Bram Moolenaar0f873732019-12-05 20:28:46 +01002562 // When waiting, loop until the message waiting for is received.
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002563 time(&start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 for (;;)
2565 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002566 // Reset this here, in case a message arrives while we are going
2567 // through the already received messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 reply_received = 0;
2569
2570 for (i = 0; i < REPLY_COUNT; ++i)
2571 {
2572 rep = REPLY_ITEM(i);
2573 if (rep->server == server
2574 && ((rep->expr_result != 0) == (expr_res != NULL)))
2575 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002576 // Save the values we've found for later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 reply = rep->reply;
2578 if (expr_res != NULL)
2579 *expr_res = rep->expr_result == 1 ? 0 : -1;
2580
2581 if (remove)
2582 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002583 // Move the rest of the list down to fill the gap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 mch_memmove(rep, rep + 1,
2585 (REPLY_COUNT - i - 1) * sizeof(reply_T));
2586 --REPLY_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 }
2588
Bram Moolenaar0f873732019-12-05 20:28:46 +01002589 // Return the reply to the caller, who takes on responsibility
2590 // for freeing it if "remove" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 return reply;
2592 }
2593 }
2594
Bram Moolenaar0f873732019-12-05 20:28:46 +01002595 // If we got here, we didn't find a reply. Return immediately if the
2596 // "wait" parameter isn't set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 if (!wait)
Bram Moolenaar15e737f2017-03-18 21:22:47 +01002598 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002599 // Process pending messages once. Without this, looping on
2600 // remote_peek() would never get the reply.
Bram Moolenaar15e737f2017-03-18 21:22:47 +01002601 if (!did_process)
2602 {
2603 did_process = TRUE;
2604 serverProcessPendingMessages();
2605 continue;
2606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 break;
Bram Moolenaar15e737f2017-03-18 21:22:47 +01002608 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609
Bram Moolenaar0f873732019-12-05 20:28:46 +01002610 // We need to wait for a reply. Enter a message loop until the
2611 // "reply_received" flag gets set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612
Bram Moolenaar0f873732019-12-05 20:28:46 +01002613 // Loop until we receive a reply
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 while (reply_received == 0)
2615 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002616# ifdef FEAT_TIMERS
Bram Moolenaar0f873732019-12-05 20:28:46 +01002617 // TODO: use the return value to decide how long to wait.
Bram Moolenaar42205552017-03-18 19:42:22 +01002618 check_due_timer();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002619# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002620 time(&now);
2621 if (timeout > 0 && (now - start) >= timeout)
2622 break;
2623
Bram Moolenaar0f873732019-12-05 20:28:46 +01002624 // Wait for a SendMessage() call to us. This could be the reply
2625 // we are waiting for. Use a timeout of a second, to catch the
2626 // situation that the server died unexpectedly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 MsgWaitForMultipleObjects(0, NULL, TRUE, 1000, QS_ALLINPUT);
2628
Bram Moolenaar0f873732019-12-05 20:28:46 +01002629 // If the server has died, give up
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 if (!IsWindow(server))
2631 return NULL;
2632
2633 serverProcessPendingMessages();
2634 }
2635 }
2636
2637 return NULL;
2638}
2639
2640/*
2641 * Process any messages in the Windows message queue.
2642 */
2643 void
2644serverProcessPendingMessages(void)
2645{
2646 MSG msg;
2647
K.Takatab7057bd2022-01-21 11:37:07 +00002648 while (PeekMessageW(&msg, NULL, 0, 0, PM_REMOVE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 {
2650 TranslateMessage(&msg);
K.Takatab7057bd2022-01-21 11:37:07 +00002651 DispatchMessageW(&msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 }
2653}
2654
Bram Moolenaar0f873732019-12-05 20:28:46 +01002655#endif // FEAT_CLIENTSERVER
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656
2657#if defined(FEAT_GUI) || (defined(FEAT_PRINTER) && !defined(FEAT_POSTSCRIPT)) \
2658 || defined(PROTO)
2659
2660struct charset_pair
2661{
2662 char *name;
2663 BYTE charset;
2664};
2665
2666static struct charset_pair
2667charset_pairs[] =
2668{
2669 {"ANSI", ANSI_CHARSET},
2670 {"CHINESEBIG5", CHINESEBIG5_CHARSET},
2671 {"DEFAULT", DEFAULT_CHARSET},
2672 {"HANGEUL", HANGEUL_CHARSET},
2673 {"OEM", OEM_CHARSET},
2674 {"SHIFTJIS", SHIFTJIS_CHARSET},
2675 {"SYMBOL", SYMBOL_CHARSET},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 {"ARABIC", ARABIC_CHARSET},
2677 {"BALTIC", BALTIC_CHARSET},
2678 {"EASTEUROPE", EASTEUROPE_CHARSET},
2679 {"GB2312", GB2312_CHARSET},
2680 {"GREEK", GREEK_CHARSET},
2681 {"HEBREW", HEBREW_CHARSET},
2682 {"JOHAB", JOHAB_CHARSET},
2683 {"MAC", MAC_CHARSET},
2684 {"RUSSIAN", RUSSIAN_CHARSET},
2685 {"THAI", THAI_CHARSET},
2686 {"TURKISH", TURKISH_CHARSET},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002687# ifdef VIETNAMESE_CHARSET
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 {"VIETNAMESE", VIETNAMESE_CHARSET},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002689# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 {NULL, 0}
2691};
2692
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002693struct quality_pair
2694{
2695 char *name;
2696 DWORD quality;
2697};
2698
2699static struct quality_pair
2700quality_pairs[] = {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002701# ifdef CLEARTYPE_QUALITY
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002702 {"CLEARTYPE", CLEARTYPE_QUALITY},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002703# endif
2704# ifdef ANTIALIASED_QUALITY
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002705 {"ANTIALIASED", ANTIALIASED_QUALITY},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002706# endif
2707# ifdef NONANTIALIASED_QUALITY
Bram Moolenaar73a733e2016-05-11 21:05:05 +02002708 {"NONANTIALIASED", NONANTIALIASED_QUALITY},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002709# endif
2710# ifdef PROOF_QUALITY
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002711 {"PROOF", PROOF_QUALITY},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002712# endif
2713# ifdef DRAFT_QUALITY
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002714 {"DRAFT", DRAFT_QUALITY},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002715# endif
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002716 {"DEFAULT", DEFAULT_QUALITY},
2717 {NULL, 0}
2718};
2719
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720/*
2721 * Convert a charset ID to a name.
2722 * Return NULL when not recognized.
2723 */
2724 char *
2725charset_id2name(int id)
2726{
2727 struct charset_pair *cp;
2728
2729 for (cp = charset_pairs; cp->name != NULL; ++cp)
2730 if ((BYTE)id == cp->charset)
2731 break;
2732 return cp->name;
2733}
2734
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002735/*
2736 * Convert a quality ID to a name.
2737 * Return NULL when not recognized.
2738 */
2739 char *
2740quality_id2name(DWORD id)
2741{
2742 struct quality_pair *qp;
2743
2744 for (qp = quality_pairs; qp->name != NULL; ++qp)
2745 if (id == qp->quality)
2746 break;
2747 return qp->name;
2748}
2749
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002750static const LOGFONTW s_lfDefault =
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751{
2752 -12, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, DEFAULT_CHARSET,
2753 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
2754 PROOF_QUALITY, FIXED_PITCH | FF_DONTCARE,
Bram Moolenaar0f873732019-12-05 20:28:46 +01002755 L"Fixedsys" // see _ReadVimIni
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756};
2757
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002758// Initialise the "current height" to -12 (same as s_lfDefault) just
2759// in case the user specifies a font in "guifont" with no size before a font
2760// with an explicit size has been set. This defaults the size to this value
2761// (-12 equates to roughly 9pt).
2762int current_font_height = -12; // also used in gui_w32.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763
Bram Moolenaar0f873732019-12-05 20:28:46 +01002764/*
2765 * Convert a string representing a point size into pixels. The string should
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 * be a positive decimal number, with an optional decimal point (eg, "12", or
2767 * "10.5"). The pixel value is returned, and a pointer to the next unconverted
2768 * character is stored in *end. The flag "vertical" says whether this
2769 * calculation is for a vertical (height) size or a horizontal (width) one.
2770 */
2771 static int
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002772points_to_pixels(WCHAR *str, WCHAR **end, int vertical, long_i pprinter_dc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773{
2774 int pixels;
2775 int points = 0;
2776 int divisor = 0;
2777 HWND hwnd = (HWND)0;
2778 HDC hdc;
2779 HDC printer_dc = (HDC)pprinter_dc;
2780
2781 while (*str != NUL)
2782 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002783 if (*str == L'.' && divisor == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002785 // Start keeping a divisor, for later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 divisor = 1;
2787 }
2788 else
2789 {
2790 if (!VIM_ISDIGIT(*str))
2791 break;
2792
2793 points *= 10;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002794 points += *str - L'0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 divisor *= 10;
2796 }
2797 ++str;
2798 }
2799
2800 if (divisor == 0)
2801 divisor = 1;
2802
2803 if (printer_dc == NULL)
2804 {
2805 hwnd = GetDesktopWindow();
2806 hdc = GetWindowDC(hwnd);
2807 }
2808 else
2809 hdc = printer_dc;
2810
2811 pixels = MulDiv(points,
2812 GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX),
2813 72 * divisor);
2814
2815 if (printer_dc == NULL)
2816 ReleaseDC(hwnd, hdc);
2817
2818 *end = str;
2819 return pixels;
2820}
2821
Yee Cheng Chin290b8872023-10-05 20:54:21 +02002822/*
2823 * Convert pixel into point size. This is a reverse of points_to_pixels.
2824 */
2825 static double
2826pixels_to_points(int pixels, int vertical, long_i pprinter_dc)
2827{
2828 double points = 0;
2829 HWND hwnd = (HWND)0;
2830 HDC hdc;
2831 HDC printer_dc = (HDC)pprinter_dc;
2832
2833 if (printer_dc == NULL)
2834 {
2835 hwnd = GetDesktopWindow();
2836 hdc = GetWindowDC(hwnd);
2837 }
2838 else
2839 hdc = printer_dc;
2840
2841 points = pixels * 72.0 / GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX);
2842 if (printer_dc == NULL)
2843 ReleaseDC(hwnd, hdc);
2844
2845 return points;
2846}
2847
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 static int CALLBACK
2849font_enumproc(
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002850 ENUMLOGFONTW *elf,
2851 NEWTEXTMETRICW *ntm UNUSED,
2852 DWORD type UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 LPARAM lparam)
2854{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002855 // Return value:
2856 // 0 = terminate now (monospace & ANSI)
2857 // 1 = continue, still no luck...
2858 // 2 = continue, but we have an acceptable LOGFONTW
2859 // (monospace, not ANSI)
2860 // We use these values, as EnumFontFamilies returns 1 if the
2861 // callback function is never called. So, we check the return as
2862 // 0 = perfect, 2 = OK, 1 = no good...
2863 // It's not pretty, but it works!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002865 LOGFONTW *lf = (LOGFONTW *)(lparam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002867# ifndef FEAT_PROPORTIONAL_FONTS
Bram Moolenaar0f873732019-12-05 20:28:46 +01002868 // Ignore non-monospace fonts without further ado
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 if ((ntm->tmPitchAndFamily & 1) != 0)
2870 return 1;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002871# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872
Bram Moolenaar0f873732019-12-05 20:28:46 +01002873 // Remember this LOGFONTW as a "possible"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 *lf = elf->elfLogFont;
2875
Bram Moolenaar0f873732019-12-05 20:28:46 +01002876 // Terminate the scan as soon as we find an ANSI font
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 if (lf->lfCharSet == ANSI_CHARSET
2878 || lf->lfCharSet == OEM_CHARSET
2879 || lf->lfCharSet == DEFAULT_CHARSET)
2880 return 0;
2881
Bram Moolenaar0f873732019-12-05 20:28:46 +01002882 // Continue the scan - we have a non-ANSI font
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 return 2;
2884}
2885
2886 static int
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002887init_logfont(LOGFONTW *lf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888{
2889 int n;
2890 HWND hwnd = GetDesktopWindow();
2891 HDC hdc = GetWindowDC(hwnd);
2892
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002893 n = EnumFontFamiliesW(hdc,
2894 lf->lfFaceName,
2895 (FONTENUMPROCW)font_enumproc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 (LPARAM)lf);
2897
2898 ReleaseDC(hwnd, hdc);
2899
Bram Moolenaar0f873732019-12-05 20:28:46 +01002900 // If we couldn't find a usable font, return failure
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 if (n == 1)
2902 return FAIL;
2903
Bram Moolenaar0f873732019-12-05 20:28:46 +01002904 // Tidy up the rest of the LOGFONTW structure. We set to a basic
2905 // font - get_logfont() sets bold, italic, etc based on the user's
2906 // input.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 lf->lfHeight = current_font_height;
2908 lf->lfWidth = 0;
2909 lf->lfItalic = FALSE;
2910 lf->lfUnderline = FALSE;
2911 lf->lfStrikeOut = FALSE;
2912 lf->lfWeight = FW_NORMAL;
2913
Bram Moolenaar0f873732019-12-05 20:28:46 +01002914 // Return success
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 return OK;
2916}
2917
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002918/*
Yee Cheng Chin290b8872023-10-05 20:54:21 +02002919 * Call back for EnumFontFamiliesW in expand_font_enumproc.
2920 *
2921 */
2922 static int CALLBACK
2923expand_font_enumproc(
2924 ENUMLOGFONTW *elf,
2925 NEWTEXTMETRICW *ntm UNUSED,
2926 DWORD type UNUSED,
2927 LPARAM lparam)
2928{
2929 LOGFONTW *lf = (LOGFONTW*)elf;
2930
2931# ifndef FEAT_PROPORTIONAL_FONTS
2932 // Ignore non-monospace fonts without further ado
2933 if ((ntm->tmPitchAndFamily & 1) != 0)
2934 return 1;
2935# endif
2936
2937 // Filter only on ANSI. Otherwise will see a lot of random fonts that we
2938 // usually don't want.
2939 if (lf->lfCharSet != ANSI_CHARSET)
2940 return 1;
2941
2942 int (*add_match)(char_u *) = (int (*)(char_u *))lparam;
2943
2944 WCHAR *faceNameW = lf->lfFaceName;
2945 char_u *faceName = utf16_to_enc(faceNameW, NULL);
2946 if (!faceName)
2947 return 0;
2948
2949 add_match(faceName);
2950 vim_free(faceName);
2951
2952 return 1;
2953}
2954
2955/*
2956 * Cmdline expansion for setting 'guifont'. Will enumerate through all
2957 * monospace fonts for completion. If used after ':', will expand to possible
2958 * font configuration options like font sizes.
2959 *
2960 * This function has "gui" in its name because in some platforms (GTK) font
2961 * handling is done by the GUI code, whereas in Windows it's part of the
2962 * platform code.
2963 */
2964 void
2965gui_mch_expand_font(optexpand_T *args, void *param UNUSED, int (*add_match)(char_u *val))
2966{
2967 expand_T *xp = args->oe_xp;
2968 if (xp->xp_pattern > args->oe_set_arg && *(xp->xp_pattern-1) == ':')
2969 {
2970 char buf[30];
2971
2972 // Always fill in with the current font size as first option for
2973 // convenience. We simply round to the closest integer for simplicity.
2974 int font_height = (int)round(
2975 pixels_to_points(-current_font_height, TRUE, (long_i)NULL));
2976 vim_snprintf(buf, ARRAY_LENGTH(buf), "h%d", font_height);
2977 add_match((char_u *)buf);
2978
2979 // Note: Keep this in sync with get_logfont(). Don't include 'c' and
2980 // 'q' as we fill in all the values below.
2981 static char *(p_gfn_win_opt_values[]) = {
2982 "h" , "w" , "W" , "b" , "i" , "u" , "s"};
2983 for (size_t i = 0; i < ARRAY_LENGTH(p_gfn_win_opt_values); i++)
2984 add_match((char_u *)p_gfn_win_opt_values[i]);
2985
2986 struct charset_pair *cp;
2987 for (cp = charset_pairs; cp->name != NULL; ++cp)
2988 {
2989 vim_snprintf(buf, ARRAY_LENGTH(buf), "c%s", cp->name);
2990 add_match((char_u *)buf);
2991 }
2992 struct quality_pair *qp;
2993 for (qp = quality_pairs; qp->name != NULL; ++qp)
2994 {
2995 vim_snprintf(buf, ARRAY_LENGTH(buf), "q%s", qp->name);
2996 add_match((char_u *)buf);
2997 }
2998 return;
2999 }
3000
3001 HWND hwnd = GetDesktopWindow();
3002 HDC hdc = GetWindowDC(hwnd);
3003
3004 EnumFontFamiliesW(hdc,
3005 NULL,
3006 (FONTENUMPROCW)expand_font_enumproc,
3007 (LPARAM)add_match);
3008
3009 ReleaseDC(hwnd, hdc);
3010}
3011
3012/*
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003013 * Compare a UTF-16 string and an ASCII string literally.
3014 * Only works all the code points are inside ASCII range.
3015 */
3016 static int
3017utf16ascncmp(const WCHAR *w, const char *p, size_t n)
3018{
3019 size_t i;
3020
3021 for (i = 0; i < n; i++)
3022 {
3023 if (w[i] == 0 || w[i] != p[i])
3024 return w[i] - p[i];
3025 }
3026 return 0;
3027}
3028
3029/*
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003030 * Get font info from "name" into logfont "lf".
3031 * Return OK for a valid name, FAIL otherwise.
3032 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 int
3034get_logfont(
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003035 LOGFONTW *lf,
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003036 char_u *name,
3037 HDC printer_dc,
3038 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003040 WCHAR *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 int i;
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003042 int ret = FAIL;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003043 static LOGFONTW *lastlf = NULL;
3044 WCHAR *wname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045
3046 *lf = s_lfDefault;
3047 if (name == NULL)
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003048 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003050 wname = enc_to_utf16(name, NULL);
3051 if (wname == NULL)
3052 return FAIL;
3053
3054 if (wcscmp(wname, L"*") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003056# if defined(FEAT_GUI_MSWIN)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003057 CHOOSEFONTW cf;
Bram Moolenaar0f873732019-12-05 20:28:46 +01003058 // if name is "*", bring up std font dialog:
Bram Moolenaara80faa82020-04-12 19:37:17 +02003059 CLEAR_FIELD(cf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 cf.lStructSize = sizeof(cf);
3061 cf.hwndOwner = s_hwnd;
3062 cf.Flags = CF_SCREENFONTS | CF_FIXEDPITCHONLY | CF_INITTOLOGFONTSTRUCT;
3063 if (lastlf != NULL)
3064 *lf = *lastlf;
3065 cf.lpLogFont = lf;
3066 cf.nFontType = 0 ; //REGULAR_FONTTYPE;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003067 if (ChooseFontW(&cf))
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003068 ret = OK;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01003069# endif
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003070 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 }
3072
3073 /*
3074 * Split name up, it could be <name>:h<height>:w<width> etc.
3075 */
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003076 for (p = wname; *p && *p != L':'; p++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003078 if (p - wname + 1 >= LF_FACESIZE)
Bram Moolenaar0f873732019-12-05 20:28:46 +01003079 goto theend; // Name too long
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003080 lf->lfFaceName[p - wname] = *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 }
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003082 if (p != wname)
3083 lf->lfFaceName[p - wname] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084
Bram Moolenaar0f873732019-12-05 20:28:46 +01003085 // First set defaults
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 lf->lfHeight = -12;
3087 lf->lfWidth = 0;
3088 lf->lfWeight = FW_NORMAL;
3089 lf->lfItalic = FALSE;
3090 lf->lfUnderline = FALSE;
3091 lf->lfStrikeOut = FALSE;
3092
3093 /*
3094 * If the font can't be found, try replacing '_' by ' '.
3095 */
3096 if (init_logfont(lf) == FAIL)
3097 {
3098 int did_replace = FALSE;
3099
3100 for (i = 0; lf->lfFaceName[i]; ++i)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003101 if (lf->lfFaceName[i] == L'_')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003103 lf->lfFaceName[i] = L' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 did_replace = TRUE;
3105 }
3106 if (!did_replace || init_logfont(lf) == FAIL)
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003107 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 }
3109
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003110 while (*p == L':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 p++;
3112
Bram Moolenaar0f873732019-12-05 20:28:46 +01003113 // Set the values found after ':'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 while (*p)
3115 {
3116 switch (*p++)
3117 {
Yee Cheng Chin290b8872023-10-05 20:54:21 +02003118 // Note: Keep this in sync with gui_mch_expand_font().
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003119 case L'h':
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003120 lf->lfHeight = - points_to_pixels(p, &p, TRUE, (long_i)printer_dc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003122 case L'w':
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003123 lf->lfWidth = points_to_pixels(p, &p, FALSE, (long_i)printer_dc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 break;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003125 case L'W':
3126 lf->lfWeight = wcstol(p, &p, 10);
3127 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003128 case L'b':
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129 lf->lfWeight = FW_BOLD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003131 case L'i':
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 lf->lfItalic = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003134 case L'u':
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 lf->lfUnderline = TRUE;
3136 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003137 case L's':
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 lf->lfStrikeOut = TRUE;
3139 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003140 case L'c':
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 {
3142 struct charset_pair *cp;
3143
3144 for (cp = charset_pairs; cp->name != NULL; ++cp)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003145 if (utf16ascncmp(p, cp->name, strlen(cp->name)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 {
3147 lf->lfCharSet = cp->charset;
3148 p += strlen(cp->name);
3149 break;
3150 }
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003151 if (cp->name == NULL && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003153 char_u *s = utf16_to_enc(p, NULL);
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00003154
3155 semsg(_(e_illegal_str_name_str_in_font_name_str),
3156 "charset", s, name);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003157 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 break;
3159 }
3160 break;
3161 }
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003162 case L'q':
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003163 {
3164 struct quality_pair *qp;
3165
3166 for (qp = quality_pairs; qp->name != NULL; ++qp)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003167 if (utf16ascncmp(p, qp->name, strlen(qp->name)) == 0)
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003168 {
3169 lf->lfQuality = qp->quality;
3170 p += strlen(qp->name);
3171 break;
3172 }
3173 if (qp->name == NULL && verbose)
3174 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003175 char_u *s = utf16_to_enc(p, NULL);
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00003176 semsg(_(e_illegal_str_name_str_in_font_name_str),
3177 "quality", s, name);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003178 vim_free(s);
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003179 break;
3180 }
3181 break;
3182 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 default:
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003184 if (verbose)
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00003185 semsg(_(e_illegal_char_nr_in_font_name_str), p[-1], name);
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003186 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 }
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003188 while (*p == L':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 p++;
3190 }
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003191 ret = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193theend:
Bram Moolenaar0f873732019-12-05 20:28:46 +01003194 // ron: init lastlf
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003195 if (ret == OK && printer_dc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 {
3197 vim_free(lastlf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003198 lastlf = ALLOC_ONE(LOGFONTW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 if (lastlf != NULL)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003200 mch_memmove(lastlf, lf, sizeof(LOGFONTW));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 }
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003202 vim_free(wname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003204 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205}
3206
Bram Moolenaar0f873732019-12-05 20:28:46 +01003207#endif // defined(FEAT_GUI) || defined(FEAT_PRINTER)
Bram Moolenaarf12d9832016-01-29 21:11:25 +01003208
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003209#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaarf12d9832016-01-29 21:11:25 +01003210/*
3211 * Initialize the Winsock dll.
3212 */
3213 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003214channel_init_winsock(void)
Bram Moolenaarf12d9832016-01-29 21:11:25 +01003215{
3216 WSADATA wsaData;
3217 int wsaerr;
3218
3219 if (WSInitialized)
3220 return;
3221
3222 wsaerr = WSAStartup(MAKEWORD(2, 2), &wsaData);
3223 if (wsaerr == 0)
3224 WSInitialized = TRUE;
3225}
3226#endif