blob: 448b2bf29c7ffdec3442831b447e62470946f1cb [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;
95typedef int KEY_EVENT_RECORD;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +010096typedef int LOGFONTW;
Bram Moolenaar071d4272004-06-13 20:20:40 +000097typedef int LPARAM;
98typedef int LPBOOL;
99typedef int LPCSTR;
100typedef int LPCWSTR;
Bram Moolenaarb9cdb372019-04-17 18:24:35 +0200101typedef int LPDWORD;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102typedef int LPSTR;
103typedef int LPTSTR;
Bram Moolenaarb9cdb372019-04-17 18:24:35 +0200104typedef int LPVOID;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000105typedef int LPWSTR;
106typedef int LRESULT;
107typedef int MOUSE_EVENT_RECORD;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100108typedef int NEWTEXTMETRICW;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000109typedef int PACL;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200110typedef int PRINTDLGW;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000111typedef int PSECURITY_DESCRIPTOR;
112typedef int PSID;
113typedef int SECURITY_INFORMATION;
114typedef int SHORT;
115typedef int SMALL_RECT;
116typedef int TEXTMETRIC;
117typedef int UINT;
118typedef int WCHAR;
Bram Moolenaarc447d8d2018-12-18 21:56:28 +0100119typedef int WNDENUMPROC;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120typedef int WORD;
121typedef int WPARAM;
122typedef void VOID;
123#endif
124
Bram Moolenaar0f873732019-12-05 20:28:46 +0100125// Record all output and all keyboard & mouse input
126// #define MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127
128#ifdef MCH_WRITE_DUMP
129FILE* fdDump = NULL;
130#endif
131
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200132#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133extern char g_szOrigTitle[];
134#endif
135
136#ifdef FEAT_GUI
137extern HWND s_hwnd;
138#else
Bram Moolenaar0f873732019-12-05 20:28:46 +0100139static HWND s_hwnd = 0; // console window handle, set by GetConsoleHwnd()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140#endif
141
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100142#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar0f873732019-12-05 20:28:46 +0100143int WSInitialized = FALSE; // WinSock is initialized
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100144#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145
Bram Moolenaar0f873732019-12-05 20:28:46 +0100146// Don't generate prototypes here, because some systems do have these
147// functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148#if defined(__GNUC__) && !defined(PROTO)
149# ifndef __MINGW32__
150int _stricoll(char *a, char *b)
151{
152 // the ANSI-ish correct way is to use strxfrm():
153 char a_buff[512], b_buff[512]; // file names, so this is enough on Win32
154 strxfrm(a_buff, a, 512);
155 strxfrm(b_buff, b, 512);
156 return strcoll(a_buff, b_buff);
157}
158
159char * _fullpath(char *buf, char *fname, int len)
160{
161 LPTSTR toss;
162
163 return (char *)GetFullPathName(fname, len, buf, &toss);
164}
165# endif
166
Bram Moolenaaraf62ff32013-03-19 14:48:29 +0100167# if !defined(__MINGW32__) || (__GNUC__ < 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168int _chdrive(int drive)
169{
170 char temp [3] = "-:";
171 temp[0] = drive + 'A' - 1;
172 return !SetCurrentDirectory(temp);
173}
Bram Moolenaaraf62ff32013-03-19 14:48:29 +0100174# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175#endif
176
177
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200178#ifndef PROTO
179/*
180 * Save the instance handle of the exe/dll.
181 */
182 void
183SaveInst(HINSTANCE hInst)
184{
185 g_hinst = hInst;
186}
187#endif
188
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189#if defined(FEAT_GUI_MSWIN) || defined(PROTO)
190/*
191 * GUI version of mch_exit().
192 * Shut down and exit with status `r'
193 * Careful: mch_exit() may be called before mch_init()!
194 */
195 void
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200196mch_exit_g(int r)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197{
Bram Moolenaar955f1982017-02-05 15:10:51 +0100198 exiting = TRUE;
199
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200 display_errors();
201
Bram Moolenaar0f873732019-12-05 20:28:46 +0100202 ml_close_all(TRUE); // remove all memfiles
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203
204# ifdef FEAT_OLE
205 UninitOLE();
206# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100207# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208 if (WSInitialized)
209 {
210 WSInitialized = FALSE;
211 WSACleanup();
212 }
213# endif
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100214# ifdef DYNAMIC_GETTEXT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215 dyn_libintl_end();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100216# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217
218 if (gui.in_use)
219 gui_exit(r);
Bram Moolenaar85c79d32007-02-20 01:59:20 +0000220
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100221# ifdef EXITFREE
Bram Moolenaar85c79d32007-02-20 01:59:20 +0000222 free_all_mem();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100223# endif
Bram Moolenaar85c79d32007-02-20 01:59:20 +0000224
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225 exit(r);
226}
227
Bram Moolenaar0f873732019-12-05 20:28:46 +0100228#endif // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229
230
231/*
232 * Init the tables for toupper() and tolower().
233 */
234 void
235mch_early_init(void)
236{
237 int i;
238
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239 PlatformId();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240
Bram Moolenaar0f873732019-12-05 20:28:46 +0100241 // Init the tables for toupper() and tolower()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242 for (i = 0; i < 256; ++i)
243 toupper_tab[i] = tolower_tab[i] = i;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100244 CharUpperBuff((LPSTR)toupper_tab, 256);
245 CharLowerBuff((LPSTR)tolower_tab, 256);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246}
247
248
249/*
250 * Return TRUE if the input comes from a terminal, FALSE otherwise.
251 */
252 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100253mch_input_isatty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254{
255#ifdef FEAT_GUI_MSWIN
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200256# ifdef VIMDLL
257 if (gui.in_use)
258# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +0100259 return TRUE; // GUI always has a tty
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200260#endif
261#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262 if (isatty(read_cmd_fd))
263 return TRUE;
264 return FALSE;
265#endif
266}
267
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268/*
269 * mch_settitle(): set titlebar of our window
270 */
271 void
272mch_settitle(
273 char_u *title,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +0200274 char_u *icon UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275{
Bram Moolenaar651fca82021-11-29 20:39:38 +0000276#ifdef FEAT_GUI_MSWIN
277# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200278 if (gui.in_use)
Bram Moolenaar651fca82021-11-29 20:39:38 +0000279# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200280 {
281 gui_mch_settitle(title, icon);
282 return;
283 }
Bram Moolenaar651fca82021-11-29 20:39:38 +0000284#endif
285#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000286 if (title != NULL)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000287 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200288 WCHAR *wp = enc_to_utf16(title, NULL);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000289
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200290 if (wp == NULL)
291 return;
292
293 SetConsoleTitleW(wp);
294 vim_free(wp);
295 return;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000296 }
Bram Moolenaar651fca82021-11-29 20:39:38 +0000297#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000298}
299
300
301/*
302 * Restore the window/icon title.
303 * which is one of:
Bram Moolenaar40385db2018-08-07 22:31:44 +0200304 * SAVE_RESTORE_TITLE: Just restore title
305 * SAVE_RESTORE_ICON: Just restore icon (which we don't have)
306 * SAVE_RESTORE_BOTH: Restore title and icon (which we don't have)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307 */
308 void
Bram Moolenaar1266d672017-02-01 13:43:36 +0100309mch_restore_title(int which UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000310{
Bram Moolenaar651fca82021-11-29 20:39:38 +0000311#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
312# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200313 if (!gui.in_use)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100314# endif
Bram Moolenaar651fca82021-11-29 20:39:38 +0000315 SetConsoleTitle(g_szOrigTitle);
316#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317}
318
319
320/*
321 * Return TRUE if we can restore the title (we can)
322 */
323 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100324mch_can_restore_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325{
326 return TRUE;
327}
328
329
330/*
331 * Return TRUE if we can restore the icon title (we can't)
332 */
333 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100334mch_can_restore_icon(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335{
336 return FALSE;
337}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338
339
340/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000341 * Get absolute file name into buffer "buf" of length "len" bytes,
342 * turning all '/'s into '\\'s and getting the correct case of each component
343 * of the file name. Append a (back)slash to a directory name.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344 * When 'shellslash' set do it the other way around.
345 * Return OK or FAIL.
346 */
347 int
348mch_FullName(
349 char_u *fname,
350 char_u *buf,
351 int len,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100352 int force UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353{
354 int nResult = FAIL;
Bram Moolenaareae1b912019-05-09 15:12:55 +0200355 WCHAR *wname;
356 WCHAR wbuf[MAX_PATH];
357 char_u *cname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358
Bram Moolenaareae1b912019-05-09 15:12:55 +0200359 wname = enc_to_utf16(fname, NULL);
360 if (wname != NULL && _wfullpath(wbuf, wname, MAX_PATH) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361 {
Bram Moolenaareae1b912019-05-09 15:12:55 +0200362 cname = utf16_to_enc((short_u *)wbuf, NULL);
363 if (cname != NULL)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000364 {
Bram Moolenaareae1b912019-05-09 15:12:55 +0200365 vim_strncpy(buf, cname, len - 1);
366 nResult = OK;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368 }
Bram Moolenaareae1b912019-05-09 15:12:55 +0200369 vim_free(wname);
370 vim_free(cname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371
372#ifdef USE_FNAME_CASE
373 fname_case(buf, len);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000374#else
375 slash_adjust(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376#endif
377
378 return nResult;
379}
380
381
382/*
383 * Return TRUE if "fname" does not depend on the current directory.
384 */
385 int
386mch_isFullName(char_u *fname)
387{
Bram Moolenaar0ea74212020-12-11 20:10:50 +0100388 // A name like "d:/foo" and "//server/share" is absolute. "d:foo" is not.
389 // Another way to check is to use mch_FullName() and see if the result is
390 // the same as the name or mch_FullName() fails. However, this has quite a
391 // bit of overhead, so let's not do that.
Yegappan Lakshmanan6df0f272021-12-16 13:06:10 +0000392 if (*fname == NUL)
Yegappan Lakshmanan5a664fe2021-12-29 18:16:21 +0000393 return FALSE;
Bram Moolenaar0ea74212020-12-11 20:10:50 +0100394 return ((ASCII_ISALPHA(fname[0]) && fname[1] == ':'
395 && (fname[2] == '/' || fname[2] == '\\'))
396 || (fname[0] == fname[1] && (fname[0] == '/' || fname[0] == '\\')));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397}
398
399/*
400 * Replace all slashes by backslashes.
401 * This used to be the other way around, but MS-DOS sometimes has problems
402 * with slashes (e.g. in a command name). We can't have mixed slashes and
403 * backslashes, because comparing file names will not work correctly. The
404 * commands that use a file name should try to avoid the need to type a
405 * backslash twice.
406 * When 'shellslash' set do it the other way around.
Bram Moolenaarb4f6a462015-10-13 19:43:17 +0200407 * When the path looks like a URL leave it unmodified.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408 */
409 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100410slash_adjust(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411{
Bram Moolenaarb4f6a462015-10-13 19:43:17 +0200412 if (path_with_url(p))
413 return;
Bram Moolenaar39d21e32017-08-05 23:09:31 +0200414
415 if (*p == '`')
416 {
Bram Moolenaar116a0f82017-08-07 21:17:57 +0200417 size_t len = STRLEN(p);
418
Bram Moolenaar0f873732019-12-05 20:28:46 +0100419 // don't replace backslash in backtick quoted strings
Bram Moolenaar39d21e32017-08-05 23:09:31 +0200420 if (len > 2 && *(p + len - 1) == '`')
421 return;
422 }
423
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +0000424 while (*p)
425 {
426 if (*p == psepcN)
427 *p = psepc;
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100428 MB_PTR_ADV(p);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +0000429 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430}
431
K.Takatac351dc12022-01-24 11:24:08 +0000432// Use 64-bit stat functions.
433#undef stat
434#undef _stat
435#undef _wstat
436#undef _fstat
437#define stat _stat64
438#define _stat _stat64
439#define _wstat _wstat64
440#define _fstat _fstat64
Bram Moolenaar8962fda2013-09-29 19:05:21 +0200441
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200442 static int
LemonBoy40fd7e62022-05-05 20:18:16 +0100443read_reparse_point(const WCHAR *name, char_u *buf, DWORD *buf_len)
444{
445 HANDLE h;
446 BOOL ok;
447
448 h = CreateFileW(name, FILE_READ_ATTRIBUTES,
449 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
450 OPEN_EXISTING,
451 FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT,
452 NULL);
453 if (h == INVALID_HANDLE_VALUE)
454 return FAIL;
455
456 ok = DeviceIoControl(h, FSCTL_GET_REPARSE_POINT, NULL, 0, buf, *buf_len,
457 buf_len, NULL);
458 CloseHandle(h);
459
460 return ok ? OK : FAIL;
461}
462
463 static int
Bram Moolenaar8767f522016-07-01 17:17:39 +0200464wstat_symlink_aware(const WCHAR *name, stat_T *stp)
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200465{
Bram Moolenaara12a1612019-01-24 16:39:02 +0100466#if (defined(_MSC_VER) && (_MSC_VER < 1900)) || defined(__MINGW32__)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100467 // Work around for VC12 or earlier (and MinGW). _wstat() can't handle
468 // symlinks properly.
469 // VC9 or earlier: _wstat() doesn't support a symlink at all. It retrieves
470 // status of a symlink itself.
471 // VC10: _wstat() supports a symlink to a normal file, but it doesn't
472 // support a symlink to a directory (always returns an error).
473 // VC11 and VC12: _wstat() doesn't return an error for a symlink to a
474 // directory, but it doesn't set S_IFDIR flag.
475 // MinGW: Same as VC9.
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200476 int n;
477 BOOL is_symlink = FALSE;
478 HANDLE hFind, h;
479 DWORD attr = 0;
480 WIN32_FIND_DATAW findDataW;
481
482 hFind = FindFirstFileW(name, &findDataW);
483 if (hFind != INVALID_HANDLE_VALUE)
484 {
485 attr = findDataW.dwFileAttributes;
486 if ((attr & FILE_ATTRIBUTE_REPARSE_POINT)
487 && (findDataW.dwReserved0 == IO_REPARSE_TAG_SYMLINK))
488 is_symlink = TRUE;
489 FindClose(hFind);
490 }
491 if (is_symlink)
492 {
493 h = CreateFileW(name, FILE_READ_ATTRIBUTES,
494 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
495 OPEN_EXISTING,
496 (attr & FILE_ATTRIBUTE_DIRECTORY)
497 ? FILE_FLAG_BACKUP_SEMANTICS : 0,
498 NULL);
499 if (h != INVALID_HANDLE_VALUE)
500 {
501 int fd;
502
K.Takatac351dc12022-01-24 11:24:08 +0000503 fd = _open_osfhandle((intptr_t)h, _O_RDONLY);
Bram Moolenaar8767f522016-07-01 17:17:39 +0200504 n = _fstat(fd, (struct _stat *)stp);
Bram Moolenaarfce7b3d2016-01-19 19:00:32 +0100505 if ((n == 0) && (attr & FILE_ATTRIBUTE_DIRECTORY))
506 stp->st_mode = (stp->st_mode & ~S_IFREG) | S_IFDIR;
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200507 _close(fd);
508 return n;
509 }
510 }
Bram Moolenaara12a1612019-01-24 16:39:02 +0100511#endif
Bram Moolenaar8767f522016-07-01 17:17:39 +0200512 return _wstat(name, (struct _stat *)stp);
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200513}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514
LemonBoy40fd7e62022-05-05 20:18:16 +0100515 char_u *
516resolve_appexeclink(char_u *fname)
517{
Bram Moolenaar9f1983d2022-05-12 20:35:35 +0100518 DWORD attr = 0;
519 int idx;
520 WCHAR *p, *end, *wname;
LemonBoy40fd7e62022-05-05 20:18:16 +0100521 // The buffer size is arbitrarily chosen to be "big enough" (TM), the
522 // ceiling should be around 16k.
Bram Moolenaar9f1983d2022-05-12 20:35:35 +0100523 char_u buf[4096];
524 DWORD buf_len = sizeof(buf);
LemonBoy40fd7e62022-05-05 20:18:16 +0100525 REPARSE_DATA_BUFFER *rb = (REPARSE_DATA_BUFFER *)buf;
526
527 wname = enc_to_utf16(fname, NULL);
528 if (wname == NULL)
529 return NULL;
530
531 attr = GetFileAttributesW(wname);
532 if (attr == INVALID_FILE_ATTRIBUTES ||
533 (attr & FILE_ATTRIBUTE_REPARSE_POINT) == 0)
534 {
535 vim_free(wname);
536 return NULL;
537 }
538
539 // The applinks are similar to symlinks but with a huge difference: they can
540 // only be executed, any other I/O operation on them is bound to fail with
541 // ERROR_FILE_NOT_FOUND even though the file exists.
542 if (read_reparse_point(wname, buf, &buf_len) == FAIL)
543 {
544 vim_free(wname);
545 return NULL;
546 }
547 vim_free(wname);
548
549 if (rb->ReparseTag != IO_REPARSE_TAG_APPEXECLINK)
550 return NULL;
551
552 // The (undocumented) reparse buffer contains a set of N null-terminated
553 // Unicode strings, the application path is stored in the third one.
554 if (rb->AppExecLinkReparseBuffer.StringCount < 3)
555 return NULL;
556
557 p = rb->AppExecLinkReparseBuffer.StringList;
558 end = p + rb->ReparseDataLength / sizeof(WCHAR);
559 for (idx = 0; p < end
560 && idx < (int)rb->AppExecLinkReparseBuffer.StringCount
561 && idx != 2; )
562 {
Yegappan Lakshmananebb01bd2022-06-08 15:14:09 +0100563 if (*p++ == L'\0')
LemonBoy40fd7e62022-05-05 20:18:16 +0100564 ++idx;
565 }
566
567 return utf16_to_enc(p, NULL);
568}
569
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570/*
571 * stat() can't handle a trailing '/' or '\', remove it first.
572 */
573 int
Bram Moolenaar8767f522016-07-01 17:17:39 +0200574vim_stat(const char *name, stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100576 // WinNT and later can use _MAX_PATH wide characters for a pathname, which
577 // means that the maximum pathname is _MAX_PATH * 3 bytes when 'enc' is
578 // UTF-8.
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100579 char_u buf[_MAX_PATH * 3 + 1];
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100580 char_u *p;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200581 WCHAR *wp;
582 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583
Bram Moolenaard2a203b2013-08-30 16:51:18 +0200584 vim_strncpy((char_u *)buf, (char_u *)name, sizeof(buf) - 1);
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100585 p = buf + STRLEN(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 if (p > buf)
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100587 MB_PTR_BACK(buf, p);
Bram Moolenaarb1cb35f2014-01-10 13:05:20 +0100588
Bram Moolenaar0f873732019-12-05 20:28:46 +0100589 // Remove trailing '\\' except root path.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590 if (p > buf && (*p == '\\' || *p == '/') && p[-1] != ':')
591 *p = NUL;
Bram Moolenaarb1cb35f2014-01-10 13:05:20 +0100592
593 if ((buf[0] == '\\' && buf[1] == '\\') || (buf[0] == '/' && buf[1] == '/'))
594 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100595 // UNC root path must be followed by '\\'.
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100596 p = vim_strpbrk(buf + 2, (char_u *)"\\/");
Bram Moolenaarb1cb35f2014-01-10 13:05:20 +0100597 if (p != NULL)
598 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100599 p = vim_strpbrk(p + 1, (char_u *)"\\/");
Bram Moolenaarb1cb35f2014-01-10 13:05:20 +0100600 if (p == NULL)
601 STRCAT(buf, "\\");
602 }
603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200605 wp = enc_to_utf16(buf, NULL);
606 if (wp == NULL)
607 return -1;
608
609 n = wstat_symlink_aware(wp, stp);
610 vim_free(wp);
611 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612}
613
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200614#if (defined(FEAT_GUI_MSWIN) && !defined(VIMDLL)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615 void
Bram Moolenaar26e86442020-05-17 14:06:16 +0200616mch_settmode(tmode_T tmode UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100618 // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619}
620
621 int
622mch_get_shellsize(void)
623{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100624 // never used
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625 return OK;
626}
627
628 void
629mch_set_shellsize(void)
630{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100631 // never used
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632}
633
634/*
635 * Rows and/or Columns has changed.
636 */
637 void
638mch_new_shellsize(void)
639{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100640 // never used
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641}
642
643#endif
644
645/*
646 * We have no job control, so fake it by starting a new shell.
647 */
648 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100649mch_suspend(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650{
651 suspend_shell();
652}
653
654#if defined(USE_MCH_ERRMSG) || defined(PROTO)
655
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200656# ifdef display_errors
657# undef display_errors
658# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659
660/*
661 * Display the saved error message(s).
662 */
663 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100664display_errors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665{
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200666# ifdef FEAT_GUI
Bram Moolenaar819ab822022-06-13 22:34:14 +0100667 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200669# ifdef VIMDLL
670 if (gui.in_use || gui.starting)
671# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200673 if (error_ga.ga_data != NULL)
674 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100675 // avoid putting up a message box with blanks only
Bram Moolenaar819ab822022-06-13 22:34:14 +0100676 for (p = (char_u *)error_ga.ga_data; *p; ++p)
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200677 if (!isspace(*p))
678 {
Bram Moolenaar2d12c252022-06-13 21:42:45 +0100679 // Only use a dialog when not using --gui-dialog-file:
680 // write text to a file.
681 if (!gui_dialog_log((char_u *)"Errors", p))
682 (void)gui_mch_dialog(
Bram Moolenaarc17ef8e2006-03-25 21:48:58 +0000683 gui.starting ? VIM_INFO :
Bram Moolenaarc17ef8e2006-03-25 21:48:58 +0000684 VIM_ERROR,
Bram Moolenaarc17ef8e2006-03-25 21:48:58 +0000685 gui.starting ? (char_u *)_("Message") :
Bram Moolenaarc17ef8e2006-03-25 21:48:58 +0000686 (char_u *)_("Error"),
Bram Moolenaar819ab822022-06-13 22:34:14 +0100687 p, (char_u *)_("&Ok"),
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100688 1, NULL, FALSE);
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200689 break;
690 }
691 ga_clear(&error_ga);
692 }
693 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200695# endif
696# if !defined(FEAT_GUI) || defined(VIMDLL)
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +0100697 FlushFileBuffers(GetStdHandle(STD_ERROR_HANDLE));
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200698# endif
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +0100699}
700#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701
702
703/*
704 * Return TRUE if "p" contain a wildcard that can be expanded by
705 * dos_expandpath().
706 */
707 int
708mch_has_exp_wildcard(char_u *p)
709{
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100710 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 {
712 if (vim_strchr((char_u *)"?*[", *p) != NULL
713 || (*p == '~' && p[1] != NUL))
714 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 }
716 return FALSE;
717}
718
719/*
720 * Return TRUE if "p" contain a wildcard or a "~1" kind of thing (could be a
721 * shortened file name).
722 */
723 int
724mch_has_wildcard(char_u *p)
725{
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100726 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727 {
728 if (vim_strchr((char_u *)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100729#ifdef VIM_BACKTICK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730 "?*$[`"
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100731#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 "?*$["
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100733#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 , *p) != NULL
735 || (*p == '~' && p[1] != NUL))
736 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 }
738 return FALSE;
739}
740
741
742/*
743 * The normal _chdir() does not change the default drive. This one does.
744 * Returning 0 implies success; -1 implies failure.
745 */
746 int
747mch_chdir(char *path)
748{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200749 WCHAR *p;
750 int n;
751
Bram Moolenaar0f873732019-12-05 20:28:46 +0100752 if (path[0] == NUL) // just checking...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 return -1;
754
Bram Moolenaara2974d72009-07-14 16:38:36 +0000755 if (p_verbose >= 5)
756 {
757 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100758 smsg("chdir(%s)", path);
Bram Moolenaara2974d72009-07-14 16:38:36 +0000759 verbose_leave();
760 }
Bram Moolenaar0f873732019-12-05 20:28:46 +0100761 if (isalpha(path[0]) && path[1] == ':') // has a drive name
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100763 // If we can change to the drive, skip that part of the path. If we
764 // can't then the current directory may be invalid, try using chdir()
765 // with the whole path.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 if (_chdrive(TOLOWER_ASC(path[0]) - 'a' + 1) == 0)
767 path += 2;
768 }
769
Bram Moolenaar0f873732019-12-05 20:28:46 +0100770 if (*path == NUL) // drive name only
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 return 0;
772
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200773 p = enc_to_utf16((char_u *)path, NULL);
774 if (p == NULL)
775 return -1;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000776
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200777 n = _wchdir(p);
778 vim_free(p);
779 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780}
781
782
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200783#if defined(FEAT_GUI_MSWIN) && !defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784/*
785 * return non-zero if a character is available
786 */
787 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100788mch_char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100790 // never used
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 return TRUE;
792}
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200793
794# if defined(FEAT_TERMINAL) || defined(PROTO)
795/*
796 * Check for any pending input or messages.
797 */
798 int
799mch_check_messages(void)
800{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100801 // TODO: check for messages
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200802 return TRUE;
803}
804# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805#endif
806
807
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808#if defined(FEAT_LIBCALL) || defined(PROTO)
809/*
810 * Call a DLL routine which takes either a string or int param
811 * and returns an allocated string.
812 * Return OK if it worked, FAIL if not.
813 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814typedef LPTSTR (*MYSTRPROCSTR)(LPTSTR);
815typedef LPTSTR (*MYINTPROCSTR)(int);
816typedef int (*MYSTRPROCINT)(LPTSTR);
817typedef int (*MYINTPROCINT)(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819/*
820 * Check if a pointer points to a valid NUL terminated string.
821 * Return the length of the string, including terminating NUL.
822 * Returns 0 for an invalid pointer, 1 for an empty string.
823 */
824 static size_t
825check_str_len(char_u *str)
826{
827 SYSTEM_INFO si;
828 MEMORY_BASIC_INFORMATION mbi;
829 size_t length = 0;
830 size_t i;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100831 const char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832
Bram Moolenaar0f873732019-12-05 20:28:46 +0100833 // get page size
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 GetSystemInfo(&si);
835
Bram Moolenaar0f873732019-12-05 20:28:46 +0100836 // get memory information
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 if (VirtualQuery(str, &mbi, sizeof(mbi)))
838 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100839 // pre cast these (typing savers)
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000840 long_u dwStr = (long_u)str;
841 long_u dwBaseAddress = (long_u)mbi.BaseAddress;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842
Bram Moolenaar0f873732019-12-05 20:28:46 +0100843 // get start address of page that str is on
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000844 long_u strPage = dwStr - (dwStr - dwBaseAddress) % si.dwPageSize;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845
Bram Moolenaar0f873732019-12-05 20:28:46 +0100846 // get length from str to end of page
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000847 long_u pageLength = si.dwPageSize - (dwStr - strPage);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848
Bram Moolenaar442b4222010-05-24 21:34:22 +0200849 for (p = str; !IsBadReadPtr(p, (UINT)pageLength);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 p += pageLength, pageLength = si.dwPageSize)
851 for (i = 0; i < pageLength; ++i, ++length)
852 if (p[i] == NUL)
853 return length + 1;
854 }
855
856 return 0;
857}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200859/*
860 * Passed to do_in_runtimepath() to load a vim.ico file.
861 */
862 static void
863mch_icon_load_cb(char_u *fname, void *cookie)
864{
865 HANDLE *h = (HANDLE *)cookie;
866
867 *h = LoadImage(NULL,
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100868 (LPSTR)fname,
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200869 IMAGE_ICON,
870 64,
871 64,
872 LR_LOADFROMFILE | LR_LOADMAP3DCOLORS);
873}
874
875/*
876 * Try loading an icon file from 'runtimepath'.
877 */
878 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100879mch_icon_load(HANDLE *iconp)
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200880{
881 return do_in_runtimepath((char_u *)"bitmaps/vim.ico",
Bram Moolenaar7f8989d2016-03-12 22:11:39 +0100882 0, mch_icon_load_cb, iconp);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200883}
884
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 int
886mch_libcall(
887 char_u *libname,
888 char_u *funcname,
Bram Moolenaar0f873732019-12-05 20:28:46 +0100889 char_u *argstring, // NULL when using a argint
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 int argint,
Bram Moolenaar0f873732019-12-05 20:28:46 +0100891 char_u **string_result,// NULL when using number_result
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 int *number_result)
893{
894 HINSTANCE hinstLib;
895 MYSTRPROCSTR ProcAdd;
896 MYINTPROCSTR ProcAddI;
897 char_u *retval_str = NULL;
898 int retval_int = 0;
899 size_t len;
900
901 BOOL fRunTimeLinkSuccess = FALSE;
902
903 // Get a handle to the DLL module.
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100904 hinstLib = vimLoadLib((char *)libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905
906 // If the handle is valid, try to get the function address.
907 if (hinstLib != NULL)
908 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100909# ifdef HAVE_TRY_EXCEPT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910 __try
911 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100912# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 if (argstring != NULL)
914 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100915 // Call with string argument
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100916 ProcAdd = (MYSTRPROCSTR)GetProcAddress(hinstLib, (LPCSTR)funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 if ((fRunTimeLinkSuccess = (ProcAdd != NULL)) != 0)
918 {
919 if (string_result == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100920 retval_int = ((MYSTRPROCINT)ProcAdd)((LPSTR)argstring);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100922 retval_str = (char_u *)(ProcAdd)((LPSTR)argstring);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 }
924 }
925 else
926 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100927 // Call with number argument
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100928 ProcAddI = (MYINTPROCSTR) GetProcAddress(hinstLib, (LPCSTR)funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 if ((fRunTimeLinkSuccess = (ProcAddI != NULL)) != 0)
930 {
931 if (string_result == NULL)
932 retval_int = ((MYINTPROCINT)ProcAddI)(argint);
933 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100934 retval_str = (char_u *)(ProcAddI)(argint);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935 }
936 }
937
938 // Save the string before we free the library.
939 // Assume that a "1" result is an illegal pointer.
940 if (string_result == NULL)
941 *number_result = retval_int;
942 else if (retval_str != NULL
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100943 && (len = check_str_len(retval_str)) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 {
Bram Moolenaar18a4ba22019-05-24 19:39:03 +0200945 *string_result = alloc(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 if (*string_result != NULL)
947 mch_memmove(*string_result, retval_str, len);
948 }
949
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100950# ifdef HAVE_TRY_EXCEPT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 }
952 __except(EXCEPTION_EXECUTE_HANDLER)
953 {
954 if (GetExceptionCode() == EXCEPTION_STACK_OVERFLOW)
K.Takatac351dc12022-01-24 11:24:08 +0000955 _resetstkoflw();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 fRunTimeLinkSuccess = 0;
957 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100958# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959
960 // Free the DLL module.
961 (void)FreeLibrary(hinstLib);
962 }
963
964 if (!fRunTimeLinkSuccess)
965 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000966 semsg(_(e_library_call_failed_for_str), funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 return FAIL;
968 }
969
970 return OK;
971}
972#endif
973
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974/*
975 * Debugging helper: expose the MCH_WRITE_DUMP stuff to other modules
976 */
977 void
Bram Moolenaar1266d672017-02-01 13:43:36 +0100978DumpPutS(const char *psz UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100980#ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981 if (fdDump)
982 {
983 fputs(psz, fdDump);
984 if (psz[strlen(psz) - 1] != '\n')
985 fputc('\n', fdDump);
986 fflush(fdDump);
987 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100988#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989}
990
991#ifdef _DEBUG
992
993void __cdecl
994Trace(
995 char *pszFormat,
996 ...)
997{
998 CHAR szBuff[2048];
999 va_list args;
1000
1001 va_start(args, pszFormat);
1002 vsprintf(szBuff, pszFormat, args);
1003 va_end(args);
1004
1005 OutputDebugString(szBuff);
1006}
1007
1008#endif //_DEBUG
1009
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001010#if !defined(FEAT_GUI) || defined(VIMDLL) || defined(PROTO)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001011extern HWND g_hWnd; // This is in os_win32.c.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012
1013/*
1014 * Showing the printer dialog is tricky since we have no GUI
1015 * window to parent it. The following routines are needed to
1016 * get the window parenting and Z-order to work properly.
1017 */
1018 static void
1019GetConsoleHwnd(void)
1020{
Bram Moolenaar0f873732019-12-05 20:28:46 +01001021 // Skip if it's already set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 if (s_hwnd != 0)
1023 return;
1024
Bram Moolenaar0f873732019-12-05 20:28:46 +01001025 // Window handle may have been found by init code (Windows NT only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 if (g_hWnd != 0)
1027 {
1028 s_hwnd = g_hWnd;
1029 return;
1030 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031
Bram Moolenaare1ed53f2019-02-12 23:12:37 +01001032 s_hwnd = GetConsoleWindow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033}
Bram Moolenaar843ee412004-06-30 16:16:41 +00001034
1035/*
1036 * Console implementation of ":winpos".
1037 */
1038 int
1039mch_get_winpos(int *x, int *y)
1040{
1041 RECT rect;
1042
1043 GetConsoleHwnd();
1044 GetWindowRect(s_hwnd, &rect);
1045 *x = rect.left;
1046 *y = rect.top;
1047 return OK;
1048}
1049
1050/*
1051 * Console implementation of ":winpos x y".
1052 */
1053 void
1054mch_set_winpos(int x, int y)
1055{
1056 GetConsoleHwnd();
1057 SetWindowPos(s_hwnd, NULL, x, y, 0, 0,
1058 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
1059}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060#endif
1061
1062#if (defined(FEAT_PRINTER) && !defined(FEAT_POSTSCRIPT)) || defined(PROTO)
1063
Bram Moolenaar0f873732019-12-05 20:28:46 +01001064//=================================================================
1065// Win32 printer stuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066
1067static HFONT prt_font_handles[2][2][2];
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001068static PRINTDLGW prt_dlg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069static const int boldface[2] = {FW_REGULAR, FW_BOLD};
1070static TEXTMETRIC prt_tm;
1071static int prt_line_height;
1072static int prt_number_width;
1073static int prt_left_margin;
1074static int prt_right_margin;
1075static int prt_top_margin;
1076static char_u szAppName[] = TEXT("VIM");
1077static HWND hDlgPrint;
1078static int *bUserAbort = NULL;
1079static char_u *prt_name = NULL;
1080
Bram Moolenaar0f873732019-12-05 20:28:46 +01001081// Defines which are also in vim.rc.
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001082# define IDC_BOX1 400
1083# define IDC_PRINTTEXT1 401
1084# define IDC_PRINTTEXT2 402
1085# define IDC_PROGRESS 403
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001087 static BOOL
1088vimSetDlgItemText(HWND hDlg, int nIDDlgItem, char_u *s)
1089{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001090 WCHAR *wp;
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001091 BOOL ret;
1092
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001093 wp = enc_to_utf16(s, NULL);
1094 if (wp == NULL)
1095 return FALSE;
1096
1097 ret = SetDlgItemTextW(hDlg, nIDDlgItem, wp);
1098 vim_free(wp);
1099 return ret;
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001100}
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001101
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102/*
1103 * Convert BGR to RGB for Windows GDI calls
1104 */
1105 static COLORREF
1106swap_me(COLORREF colorref)
1107{
1108 int temp;
1109 char *ptr = (char *)&colorref;
1110
1111 temp = *(ptr);
1112 *(ptr ) = *(ptr + 2);
1113 *(ptr + 2) = temp;
1114 return colorref;
1115}
1116
K.Takatac351dc12022-01-24 11:24:08 +00001117 static INT_PTR CALLBACK
Bram Moolenaar1266d672017-02-01 13:43:36 +01001118PrintDlgProc(
1119 HWND hDlg,
1120 UINT message,
1121 WPARAM wParam UNUSED,
1122 LPARAM lParam UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001124# ifdef FEAT_GETTEXT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 NONCLIENTMETRICS nm;
1126 static HFONT hfont;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001127# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128
1129 switch (message)
1130 {
1131 case WM_INITDIALOG:
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001132# ifdef FEAT_GETTEXT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 nm.cbSize = sizeof(NONCLIENTMETRICS);
1134 if (SystemParametersInfo(
1135 SPI_GETNONCLIENTMETRICS,
1136 sizeof(NONCLIENTMETRICS),
1137 &nm,
1138 0))
1139 {
1140 char buff[MAX_PATH];
1141 int i;
1142
Bram Moolenaar0f873732019-12-05 20:28:46 +01001143 // Translate the dialog texts
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 hfont = CreateFontIndirect(&nm.lfMessageFont);
1145 for (i = IDC_PRINTTEXT1; i <= IDC_PROGRESS; i++)
1146 {
1147 SendDlgItemMessage(hDlg, i, WM_SETFONT, (WPARAM)hfont, 1);
1148 if (GetDlgItemText(hDlg,i, buff, sizeof(buff)))
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001149 vimSetDlgItemText(hDlg,i, (char_u *)_(buff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 }
1151 SendDlgItemMessage(hDlg, IDCANCEL,
1152 WM_SETFONT, (WPARAM)hfont, 1);
1153 if (GetDlgItemText(hDlg,IDCANCEL, buff, sizeof(buff)))
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001154 vimSetDlgItemText(hDlg,IDCANCEL, (char_u *)_(buff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001156# endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001157 SetWindowText(hDlg, (LPCSTR)szAppName);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 if (prt_name != NULL)
1159 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001160 vimSetDlgItemText(hDlg, IDC_PRINTTEXT2, (char_u *)prt_name);
Bram Moolenaard23a8232018-02-10 18:45:26 +01001161 VIM_CLEAR(prt_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 }
1163 EnableMenuItem(GetSystemMenu(hDlg, FALSE), SC_CLOSE, MF_GRAYED);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001164# if !defined(FEAT_GUI) || defined(VIMDLL)
1165# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001166 if (!gui.in_use)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001167# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001168 BringWindowToTop(s_hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001169# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 return TRUE;
1171
1172 case WM_COMMAND:
1173 *bUserAbort = TRUE;
1174 EnableWindow(GetParent(hDlg), TRUE);
1175 DestroyWindow(hDlg);
1176 hDlgPrint = NULL;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001177# ifdef FEAT_GETTEXT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 DeleteObject(hfont);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001179# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 return TRUE;
1181 }
1182 return FALSE;
1183}
1184
1185 static BOOL CALLBACK
Bram Moolenaar1266d672017-02-01 13:43:36 +01001186AbortProc(HDC hdcPrn UNUSED, int iCode UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187{
1188 MSG msg;
1189
K.Takatab7057bd2022-01-21 11:37:07 +00001190 while (!*bUserAbort && PeekMessageW(&msg, NULL, 0, 0, PM_REMOVE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 {
K.Takatab7057bd2022-01-21 11:37:07 +00001192 if (!hDlgPrint || !IsDialogMessageW(hDlgPrint, &msg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 {
1194 TranslateMessage(&msg);
K.Takatab7057bd2022-01-21 11:37:07 +00001195 DispatchMessageW(&msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 }
1197 }
1198 return !*bUserAbort;
1199}
1200
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001201# if !defined(FEAT_GUI) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202
Bram Moolenaar26fdd7d2011-11-30 13:42:44 +01001203 static UINT_PTR CALLBACK
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204PrintHookProc(
1205 HWND hDlg, // handle to dialog box
1206 UINT uiMsg, // message identifier
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02001207 WPARAM wParam UNUSED, // message parameter
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 LPARAM lParam // message parameter
1209 )
1210{
1211 HWND hwndOwner;
1212 RECT rc, rcDlg, rcOwner;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001213 PRINTDLGW *pPD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214
1215 if (uiMsg == WM_INITDIALOG)
1216 {
1217 // Get the owner window and dialog box rectangles.
1218 if ((hwndOwner = GetParent(hDlg)) == NULL)
1219 hwndOwner = GetDesktopWindow();
1220
1221 GetWindowRect(hwndOwner, &rcOwner);
1222 GetWindowRect(hDlg, &rcDlg);
1223 CopyRect(&rc, &rcOwner);
1224
1225 // Offset the owner and dialog box rectangles so that
1226 // right and bottom values represent the width and
1227 // height, and then offset the owner again to discard
1228 // space taken up by the dialog box.
1229
1230 OffsetRect(&rcDlg, -rcDlg.left, -rcDlg.top);
1231 OffsetRect(&rc, -rc.left, -rc.top);
1232 OffsetRect(&rc, -rcDlg.right, -rcDlg.bottom);
1233
1234 // The new position is the sum of half the remaining
1235 // space and the owner's original position.
1236
1237 SetWindowPos(hDlg,
1238 HWND_TOP,
1239 rcOwner.left + (rc.right / 2),
1240 rcOwner.top + (rc.bottom / 2),
1241 0, 0, // ignores size arguments
1242 SWP_NOSIZE);
1243
Bram Moolenaar0f873732019-12-05 20:28:46 +01001244 // tackle the printdlg copiesctrl problem
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001245 pPD = (PRINTDLGW *)lParam;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 pPD->nCopies = (WORD)pPD->lCustData;
1247 SetDlgItemInt( hDlg, edt3, pPD->nCopies, FALSE );
Bram Moolenaar0f873732019-12-05 20:28:46 +01001248 // Bring the window to top
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 BringWindowToTop(GetParent(hDlg));
1250 SetForegroundWindow(hDlg);
1251 }
1252
1253 return FALSE;
1254}
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001255# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256
1257 void
1258mch_print_cleanup(void)
1259{
1260 int pifItalic;
1261 int pifBold;
1262 int pifUnderline;
1263
1264 for (pifBold = 0; pifBold <= 1; pifBold++)
1265 for (pifItalic = 0; pifItalic <= 1; pifItalic++)
1266 for (pifUnderline = 0; pifUnderline <= 1; pifUnderline++)
1267 DeleteObject(prt_font_handles[pifBold][pifItalic][pifUnderline]);
1268
1269 if (prt_dlg.hDC != NULL)
1270 DeleteDC(prt_dlg.hDC);
1271 if (!*bUserAbort)
1272 SendMessage(hDlgPrint, WM_COMMAND, 0, 0);
1273}
1274
1275 static int
1276to_device_units(int idx, int dpi, int physsize, int offset, int def_number)
1277{
1278 int ret = 0;
1279 int u;
1280 int nr;
1281
1282 u = prt_get_unit(idx);
1283 if (u == PRT_UNIT_NONE)
1284 {
1285 u = PRT_UNIT_PERC;
1286 nr = def_number;
1287 }
1288 else
1289 nr = printer_opts[idx].number;
1290
1291 switch (u)
1292 {
1293 case PRT_UNIT_PERC:
1294 ret = (physsize * nr) / 100;
1295 break;
1296 case PRT_UNIT_INCH:
1297 ret = (nr * dpi);
1298 break;
1299 case PRT_UNIT_MM:
1300 ret = (nr * 10 * dpi) / 254;
1301 break;
1302 case PRT_UNIT_POINT:
1303 ret = (nr * 10 * dpi) / 720;
1304 break;
1305 }
1306
1307 if (ret < offset)
1308 return 0;
1309 else
1310 return ret - offset;
1311}
1312
1313 static int
1314prt_get_cpl(void)
1315{
1316 int hr;
1317 int phyw;
1318 int dvoff;
1319 int rev_offset;
1320 int dpi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321
1322 GetTextMetrics(prt_dlg.hDC, &prt_tm);
1323 prt_line_height = prt_tm.tmHeight + prt_tm.tmExternalLeading;
1324
1325 hr = GetDeviceCaps(prt_dlg.hDC, HORZRES);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 phyw = GetDeviceCaps(prt_dlg.hDC, PHYSICALWIDTH);
1327 dvoff = GetDeviceCaps(prt_dlg.hDC, PHYSICALOFFSETX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 dpi = GetDeviceCaps(prt_dlg.hDC, LOGPIXELSX);
1329
1330 rev_offset = phyw - (dvoff + hr);
1331
1332 prt_left_margin = to_device_units(OPT_PRINT_LEFT, dpi, phyw, dvoff, 10);
1333 if (prt_use_number())
1334 {
1335 prt_number_width = PRINT_NUMBER_WIDTH * prt_tm.tmAveCharWidth;
1336 prt_left_margin += prt_number_width;
1337 }
1338 else
1339 prt_number_width = 0;
1340
1341 prt_right_margin = hr - to_device_units(OPT_PRINT_RIGHT, dpi, phyw,
1342 rev_offset, 5);
1343
1344 return (prt_right_margin - prt_left_margin) / prt_tm.tmAveCharWidth;
1345}
1346
1347 static int
1348prt_get_lpp(void)
1349{
1350 int vr;
1351 int phyw;
1352 int dvoff;
1353 int rev_offset;
1354 int bottom_margin;
1355 int dpi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356
1357 vr = GetDeviceCaps(prt_dlg.hDC, VERTRES);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 phyw = GetDeviceCaps(prt_dlg.hDC, PHYSICALHEIGHT);
1359 dvoff = GetDeviceCaps(prt_dlg.hDC, PHYSICALOFFSETY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 dpi = GetDeviceCaps(prt_dlg.hDC, LOGPIXELSY);
1361
1362 rev_offset = phyw - (dvoff + vr);
1363
1364 prt_top_margin = to_device_units(OPT_PRINT_TOP, dpi, phyw, dvoff, 5);
1365
Bram Moolenaar0f873732019-12-05 20:28:46 +01001366 // adjust top margin if there is a header
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 prt_top_margin += prt_line_height * prt_header_height();
1368
1369 bottom_margin = vr - to_device_units(OPT_PRINT_BOT, dpi, phyw,
1370 rev_offset, 5);
1371
1372 return (bottom_margin - prt_top_margin) / prt_line_height;
1373}
1374
1375 int
1376mch_print_init(prt_settings_T *psettings, char_u *jobname, int forceit)
1377{
1378 static HGLOBAL stored_dm = NULL;
1379 static HGLOBAL stored_devn = NULL;
1380 static int stored_nCopies = 1;
1381 static int stored_nFlags = 0;
1382
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001383 LOGFONTW fLogFont;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 int pifItalic;
1385 int pifBold;
1386 int pifUnderline;
1387
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001388 DEVMODEW *mem;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 DEVNAMES *devname;
1390 int i;
1391
1392 bUserAbort = &(psettings->user_abort);
Bram Moolenaara80faa82020-04-12 19:37:17 +02001393 CLEAR_FIELD(prt_dlg);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001394 prt_dlg.lStructSize = sizeof(PRINTDLGW);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001395# if !defined(FEAT_GUI) || defined(VIMDLL)
1396# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001397 if (!gui.in_use)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001398# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01001399 GetConsoleHwnd(); // get value of s_hwnd
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001400# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 prt_dlg.hwndOwner = s_hwnd;
1402 prt_dlg.Flags = PD_NOPAGENUMS | PD_NOSELECTION | PD_RETURNDC;
1403 if (!forceit)
1404 {
1405 prt_dlg.hDevMode = stored_dm;
1406 prt_dlg.hDevNames = stored_devn;
1407 prt_dlg.lCustData = stored_nCopies; // work around bug in print dialog
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001408# if !defined(FEAT_GUI) || defined(VIMDLL)
1409# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001410 if (!gui.in_use)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001411# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001412 {
1413 /*
1414 * Use hook to prevent console window being sent to back
1415 */
1416 prt_dlg.lpfnPrintHook = PrintHookProc;
1417 prt_dlg.Flags |= PD_ENABLEPRINTHOOK;
1418 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001419# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 prt_dlg.Flags |= stored_nFlags;
1421 }
1422
1423 /*
1424 * If bang present, return default printer setup with no dialog
1425 * never show dialog if we are running over telnet
1426 */
1427 if (forceit
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001428# if !defined(FEAT_GUI) || defined(VIMDLL)
1429# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001430 || (!gui.in_use && !term_console)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001431# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 || !term_console
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001433# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001434# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435 )
1436 {
1437 prt_dlg.Flags |= PD_RETURNDEFAULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 /*
1439 * MSDN suggests setting the first parameter to WINSPOOL for
1440 * NT, but NULL appears to work just as well.
1441 */
1442 if (*p_pdev != NUL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001443 prt_dlg.hDC = CreateDC(NULL, (LPCSTR)p_pdev, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 {
1446 prt_dlg.Flags |= PD_RETURNDEFAULT;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001447 if (PrintDlgW(&prt_dlg) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 goto init_fail_dlg;
1449 }
1450 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001451 else if (PrintDlgW(&prt_dlg) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 goto init_fail_dlg;
1453 else
1454 {
1455 /*
1456 * keep the previous driver context
1457 */
1458 stored_dm = prt_dlg.hDevMode;
1459 stored_devn = prt_dlg.hDevNames;
1460 stored_nFlags = prt_dlg.Flags;
1461 stored_nCopies = prt_dlg.nCopies;
1462 }
1463
1464 if (prt_dlg.hDC == NULL)
1465 {
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00001466 emsg(_(e_printer_selection_failed));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 mch_print_cleanup();
1468 return FALSE;
1469 }
1470
Bram Moolenaar0f873732019-12-05 20:28:46 +01001471 // Not all printer drivers report the support of color (or grey) in the
1472 // same way. Let's set has_color if there appears to be some way to print
1473 // more than B&W.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 i = GetDeviceCaps(prt_dlg.hDC, NUMCOLORS);
1475 psettings->has_color = (GetDeviceCaps(prt_dlg.hDC, BITSPIXEL) > 1
1476 || GetDeviceCaps(prt_dlg.hDC, PLANES) > 1
1477 || i > 2 || i == -1);
1478
Bram Moolenaar0f873732019-12-05 20:28:46 +01001479 // Ensure all font styles are baseline aligned
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 SetTextAlign(prt_dlg.hDC, TA_BASELINE|TA_LEFT);
1481
1482 /*
1483 * On some windows systems the nCopies parameter is not
1484 * passed back correctly. It must be retrieved from the
1485 * hDevMode struct.
1486 */
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001487 mem = (DEVMODEW *)GlobalLock(prt_dlg.hDevMode);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 if (mem != NULL)
1489 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 if (mem->dmCopies != 1)
1491 stored_nCopies = mem->dmCopies;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 if ((mem->dmFields & DM_DUPLEX) && (mem->dmDuplex & ~DMDUP_SIMPLEX))
1493 psettings->duplex = TRUE;
1494 if ((mem->dmFields & DM_COLOR) && (mem->dmColor & DMCOLOR_COLOR))
1495 psettings->has_color = TRUE;
1496 }
1497 GlobalUnlock(prt_dlg.hDevMode);
1498
1499 devname = (DEVNAMES *)GlobalLock(prt_dlg.hDevNames);
1500 if (devname != 0)
1501 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001502 WCHAR *wprinter_name = (WCHAR *)devname + devname->wDeviceOffset;
1503 WCHAR *wport_name = (WCHAR *)devname + devname->wOutputOffset;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001504 char_u *text = (char_u *)_("to %s on %s");
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001505 char_u *printer_name = utf16_to_enc(wprinter_name, NULL);
1506 char_u *port_name = utf16_to_enc(wport_name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001508 if (printer_name != NULL && port_name != NULL)
Bram Moolenaar964b3742019-05-24 18:54:09 +02001509 prt_name = alloc(STRLEN(printer_name)
1510 + STRLEN(port_name) + STRLEN(text));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 if (prt_name != NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001512 wsprintf((char *)prt_name, (const char *)text,
1513 printer_name, port_name);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001514 vim_free(printer_name);
1515 vim_free(port_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 }
1517 GlobalUnlock(prt_dlg.hDevNames);
1518
1519 /*
1520 * Initialise the font according to 'printfont'
1521 */
Bram Moolenaara80faa82020-04-12 19:37:17 +02001522 CLEAR_FIELD(fLogFont);
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001523 if (get_logfont(&fLogFont, p_pfn, prt_dlg.hDC, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 {
Bram Moolenaard88be5b2022-01-04 19:57:55 +00001525 semsg(_(e_unknown_printer_font_str), p_pfn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 mch_print_cleanup();
1527 return FALSE;
1528 }
1529
1530 for (pifBold = 0; pifBold <= 1; pifBold++)
1531 for (pifItalic = 0; pifItalic <= 1; pifItalic++)
1532 for (pifUnderline = 0; pifUnderline <= 1; pifUnderline++)
1533 {
1534 fLogFont.lfWeight = boldface[pifBold];
1535 fLogFont.lfItalic = pifItalic;
1536 fLogFont.lfUnderline = pifUnderline;
1537 prt_font_handles[pifBold][pifItalic][pifUnderline]
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001538 = CreateFontIndirectW(&fLogFont);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 }
1540
1541 SetBkMode(prt_dlg.hDC, OPAQUE);
1542 SelectObject(prt_dlg.hDC, prt_font_handles[0][0][0]);
1543
1544 /*
1545 * Fill in the settings struct
1546 */
1547 psettings->chars_per_line = prt_get_cpl();
1548 psettings->lines_per_page = prt_get_lpp();
Bram Moolenaar7ddc6422014-09-27 11:18:19 +02001549 if (prt_dlg.Flags & PD_USEDEVMODECOPIESANDCOLLATE)
1550 {
1551 psettings->n_collated_copies = (prt_dlg.Flags & PD_COLLATE)
1552 ? prt_dlg.nCopies : 1;
1553 psettings->n_uncollated_copies = (prt_dlg.Flags & PD_COLLATE)
1554 ? 1 : prt_dlg.nCopies;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555
Bram Moolenaar7ddc6422014-09-27 11:18:19 +02001556 if (psettings->n_collated_copies == 0)
1557 psettings->n_collated_copies = 1;
1558
1559 if (psettings->n_uncollated_copies == 0)
1560 psettings->n_uncollated_copies = 1;
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01001561 }
1562 else
1563 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564 psettings->n_collated_copies = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 psettings->n_uncollated_copies = 1;
Bram Moolenaar7ddc6422014-09-27 11:18:19 +02001566 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567
1568 psettings->jobname = jobname;
1569
1570 return TRUE;
1571
1572init_fail_dlg:
1573 {
1574 DWORD err = CommDlgExtendedError();
1575
1576 if (err)
1577 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 char_u *buf;
1579
Bram Moolenaar0f873732019-12-05 20:28:46 +01001580 // I suspect FormatMessage() doesn't work for values returned by
1581 // CommDlgExtendedError(). What does?
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
1583 FORMAT_MESSAGE_FROM_SYSTEM |
1584 FORMAT_MESSAGE_IGNORE_INSERTS,
1585 NULL, err, 0, (LPTSTR)(&buf), 0, NULL);
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00001586 semsg(_(e_print_error_str),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 buf == NULL ? (char_u *)_("Unknown") : buf);
1588 LocalFree((LPVOID)(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 }
1590 else
Bram Moolenaar0f873732019-12-05 20:28:46 +01001591 msg_clr_eos(); // Maybe canceled
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592
1593 mch_print_cleanup();
1594 return FALSE;
1595 }
1596}
1597
1598
1599 int
1600mch_print_begin(prt_settings_T *psettings)
1601{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001602 int ret = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 char szBuffer[300];
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001604 WCHAR *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001606 hDlgPrint = CreateDialog(g_hinst, TEXT("PrintDlgBox"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 prt_dlg.hwndOwner, PrintDlgProc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 SetAbortProc(prt_dlg.hDC, AbortProc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 wsprintf(szBuffer, _("Printing '%s'"), gettail(psettings->jobname));
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001610 vimSetDlgItemText(hDlgPrint, IDC_PRINTTEXT1, (char_u *)szBuffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001612 wp = enc_to_utf16(psettings->jobname, NULL);
Bram Moolenaar2290b1f2018-05-13 17:30:45 +02001613 if (wp != NULL)
1614 {
1615 DOCINFOW di;
1616
Bram Moolenaara80faa82020-04-12 19:37:17 +02001617 CLEAR_FIELD(di);
Bram Moolenaar2290b1f2018-05-13 17:30:45 +02001618 di.cbSize = sizeof(di);
1619 di.lpszDocName = wp;
1620 ret = StartDocW(prt_dlg.hDC, &di);
1621 vim_free(wp);
1622 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001624# ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01001625 // Give focus back to main window (when using MDI).
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001626# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001627 if (gui.in_use)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001628# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001629 SetFocus(s_hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001630# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631
1632 return (ret > 0);
1633}
1634
1635 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01001636mch_print_end(prt_settings_T *psettings UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637{
1638 EndDoc(prt_dlg.hDC);
1639 if (!*bUserAbort)
1640 SendMessage(hDlgPrint, WM_COMMAND, 0, 0);
1641}
1642
1643 int
1644mch_print_end_page(void)
1645{
1646 return (EndPage(prt_dlg.hDC) > 0);
1647}
1648
1649 int
1650mch_print_begin_page(char_u *msg)
1651{
1652 if (msg != NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001653 vimSetDlgItemText(hDlgPrint, IDC_PROGRESS, msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 return (StartPage(prt_dlg.hDC) > 0);
1655}
1656
1657 int
1658mch_print_blank_page(void)
1659{
1660 return (mch_print_begin_page(NULL) ? (mch_print_end_page()) : FALSE);
1661}
1662
1663static int prt_pos_x = 0;
1664static int prt_pos_y = 0;
1665
1666 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001667mch_print_start_line(int margin, int page_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668{
1669 if (margin)
1670 prt_pos_x = -prt_number_width;
1671 else
1672 prt_pos_x = 0;
1673 prt_pos_y = page_line * prt_line_height
1674 + prt_tm.tmAscent + prt_tm.tmExternalLeading;
1675}
1676
1677 int
1678mch_print_text_out(char_u *p, int len)
1679{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 SIZE sz;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001681 WCHAR *wp;
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001682 int wlen = len;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001683 int ret = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001685 wp = enc_to_utf16(p, &wlen);
1686 if (wp == NULL)
1687 return FALSE;
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001688
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001689 TextOutW(prt_dlg.hDC, prt_pos_x + prt_left_margin,
1690 prt_pos_y + prt_top_margin, wp, wlen);
1691 GetTextExtentPoint32W(prt_dlg.hDC, wp, wlen, &sz);
1692 vim_free(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 prt_pos_x += (sz.cx - prt_tm.tmOverhang);
Bram Moolenaar0f873732019-12-05 20:28:46 +01001694 // This is wrong when printing spaces for a TAB.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001695 if (p[len] != NUL)
1696 {
Bram Moolenaar1614a142019-10-06 22:00:13 +02001697 wlen = mb_ptr2len(p + len);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001698 wp = enc_to_utf16(p + len, &wlen);
1699 if (wp != NULL)
1700 {
1701 GetTextExtentPoint32W(prt_dlg.hDC, wp, 1, &sz);
1702 ret = (prt_pos_x + prt_left_margin + sz.cx > prt_right_margin);
1703 vim_free(wp);
1704 }
1705 }
1706 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707}
1708
1709 void
1710mch_print_set_font(int iBold, int iItalic, int iUnderline)
1711{
1712 SelectObject(prt_dlg.hDC, prt_font_handles[iBold][iItalic][iUnderline]);
1713}
1714
1715 void
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001716mch_print_set_bg(long_u bgcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717{
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001718 SetBkColor(prt_dlg.hDC, GetNearestColor(prt_dlg.hDC,
1719 swap_me((COLORREF)bgcol)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 /*
1721 * With a white background we can draw characters transparent, which is
1722 * good for italic characters that overlap to the next char cell.
1723 */
1724 if (bgcol == 0xffffffUL)
1725 SetBkMode(prt_dlg.hDC, TRANSPARENT);
1726 else
1727 SetBkMode(prt_dlg.hDC, OPAQUE);
1728}
1729
1730 void
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001731mch_print_set_fg(long_u fgcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732{
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001733 SetTextColor(prt_dlg.hDC, GetNearestColor(prt_dlg.hDC,
1734 swap_me((COLORREF)fgcol)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735}
1736
Bram Moolenaar0f873732019-12-05 20:28:46 +01001737#endif // FEAT_PRINTER && !FEAT_POSTSCRIPT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738
Bram Moolenaar58d98232005-07-23 22:25:46 +00001739
1740
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741#if defined(FEAT_SHORTCUT) || defined(PROTO)
Bram Moolenaar82881492012-11-20 16:53:39 +01001742# ifndef PROTO
1743# include <shlobj.h>
1744# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001746typedef BOOL (WINAPI *pfnGetFinalPathNameByHandleW)(
Bram Moolenaardce1e892019-02-10 23:18:53 +01001747 HANDLE hFile,
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001748 LPWSTR lpszFilePath,
1749 DWORD cchFilePath,
1750 DWORD dwFlags);
1751static pfnGetFinalPathNameByHandleW pGetFinalPathNameByHandleW = NULL;
Bram Moolenaardce1e892019-02-10 23:18:53 +01001752
Bram Moolenaar4a792c82019-06-06 12:22:41 +02001753# define is_path_sep(c) ((c) == L'\\' || (c) == L'/')
1754
1755 static int
1756is_reparse_point_included(LPCWSTR fname)
1757{
1758 LPCWSTR p = fname, q;
1759 WCHAR buf[MAX_PATH];
1760 DWORD attr;
1761
1762 if (isalpha(p[0]) && p[1] == L':' && is_path_sep(p[2]))
1763 p += 3;
1764 else if (is_path_sep(p[0]) && is_path_sep(p[1]))
1765 p += 2;
1766
1767 while (*p != L'\0')
1768 {
1769 q = wcspbrk(p, L"\\/");
1770 if (q == NULL)
1771 p = q = fname + wcslen(fname);
1772 else
1773 p = q + 1;
1774 if (q - fname >= MAX_PATH)
1775 return FALSE;
1776 wcsncpy(buf, fname, q - fname);
1777 buf[q - fname] = L'\0';
1778 attr = GetFileAttributesW(buf);
1779 if (attr != INVALID_FILE_ATTRIBUTES
1780 && (attr & FILE_ATTRIBUTE_REPARSE_POINT) != 0)
1781 return TRUE;
1782 }
1783 return FALSE;
1784}
1785
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02001786 static char_u *
Bram Moolenaardce1e892019-02-10 23:18:53 +01001787resolve_reparse_point(char_u *fname)
1788{
1789 HANDLE h = INVALID_HANDLE_VALUE;
1790 DWORD size;
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001791 WCHAR *p, *wp;
Bram Moolenaardce1e892019-02-10 23:18:53 +01001792 char_u *rfname = NULL;
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001793 WCHAR *buff = NULL;
Bram Moolenaardce1e892019-02-10 23:18:53 +01001794 static BOOL loaded = FALSE;
1795
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001796 if (pGetFinalPathNameByHandleW == NULL)
Bram Moolenaardce1e892019-02-10 23:18:53 +01001797 {
1798 HMODULE hmod = GetModuleHandle("kernel32.dll");
1799
1800 if (loaded == TRUE)
1801 return NULL;
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001802 pGetFinalPathNameByHandleW = (pfnGetFinalPathNameByHandleW)
1803 GetProcAddress(hmod, "GetFinalPathNameByHandleW");
Bram Moolenaardce1e892019-02-10 23:18:53 +01001804 loaded = TRUE;
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001805 if (pGetFinalPathNameByHandleW == NULL)
Bram Moolenaardce1e892019-02-10 23:18:53 +01001806 return NULL;
1807 }
1808
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001809 p = enc_to_utf16(fname, NULL);
1810 if (p == NULL)
1811 goto fail;
1812
Bram Moolenaar4a792c82019-06-06 12:22:41 +02001813 if (!is_reparse_point_included(p))
1814 {
1815 vim_free(p);
1816 goto fail;
1817 }
1818
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001819 h = CreateFileW(p, 0, 0, NULL, OPEN_EXISTING,
1820 FILE_FLAG_BACKUP_SEMANTICS, NULL);
1821 vim_free(p);
Bram Moolenaardce1e892019-02-10 23:18:53 +01001822
1823 if (h == INVALID_HANDLE_VALUE)
1824 goto fail;
1825
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001826 size = pGetFinalPathNameByHandleW(h, NULL, 0, 0);
1827 if (size == 0)
1828 goto fail;
1829 buff = ALLOC_MULT(WCHAR, size);
1830 if (buff == NULL)
1831 goto fail;
1832 if (pGetFinalPathNameByHandleW(h, buff, size, 0) == 0)
Bram Moolenaardce1e892019-02-10 23:18:53 +01001833 goto fail;
1834
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001835 if (wcsncmp(buff, L"\\\\?\\UNC\\", 8) == 0)
1836 {
1837 buff[6] = L'\\';
1838 wp = buff + 6;
1839 }
1840 else if (wcsncmp(buff, L"\\\\?\\", 4) == 0)
1841 wp = buff + 4;
Bram Moolenaardce1e892019-02-10 23:18:53 +01001842 else
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001843 wp = buff;
1844
1845 rfname = utf16_to_enc(wp, NULL);
Bram Moolenaardce1e892019-02-10 23:18:53 +01001846
1847fail:
1848 if (h != INVALID_HANDLE_VALUE)
1849 CloseHandle(h);
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001850 if (buff != NULL)
1851 vim_free(buff);
Bram Moolenaardce1e892019-02-10 23:18:53 +01001852
1853 return rfname;
1854}
1855
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856/*
1857 * When "fname" is the name of a shortcut (*.lnk) resolve the file it points
1858 * to and return that name in allocated memory.
1859 * Otherwise NULL is returned.
1860 */
Bram Moolenaardce1e892019-02-10 23:18:53 +01001861 static char_u *
1862resolve_shortcut(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863{
1864 HRESULT hr;
1865 IShellLink *psl = NULL;
1866 IPersistFile *ppf = NULL;
1867 OLECHAR wsz[MAX_PATH];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 char_u *rfname = NULL;
1869 int len;
Bram Moolenaar604729e2013-08-30 16:44:19 +02001870 IShellLinkW *pslw = NULL;
1871 WIN32_FIND_DATAW ffdw; // we get those free of charge
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872
Bram Moolenaar0f873732019-12-05 20:28:46 +01001873 // Check if the file name ends in ".lnk". Avoid calling
1874 // CoCreateInstance(), it's quite slow.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 if (fname == NULL)
1876 return rfname;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001877 len = (int)STRLEN(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 if (len <= 4 || STRNICMP(fname + len - 4, ".lnk", 4) != 0)
1879 return rfname;
1880
1881 CoInitialize(NULL);
1882
1883 // create a link manager object and request its interface
1884 hr = CoCreateInstance(
1885 &CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001886 &IID_IShellLinkW, (void**)&pslw);
1887 if (hr == S_OK)
1888 {
1889 WCHAR *p = enc_to_utf16(fname, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001891 if (p != NULL)
1892 {
1893 // Get a pointer to the IPersistFile interface.
1894 hr = pslw->lpVtbl->QueryInterface(
1895 pslw, &IID_IPersistFile, (void**)&ppf);
1896 if (hr != S_OK)
1897 goto shortcut_errorw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001899 // "load" the name and resolve the link
1900 hr = ppf->lpVtbl->Load(ppf, p, STGM_READ);
1901 if (hr != S_OK)
1902 goto shortcut_errorw;
1903# if 0 // This makes Vim wait a long time if the target does not exist.
1904 hr = pslw->lpVtbl->Resolve(pslw, NULL, SLR_NO_UI);
1905 if (hr != S_OK)
1906 goto shortcut_errorw;
Bram Moolenaar604729e2013-08-30 16:44:19 +02001907# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001909 // Get the path to the link target.
1910 ZeroMemory(wsz, MAX_PATH * sizeof(WCHAR));
1911 hr = pslw->lpVtbl->GetPath(pslw, wsz, MAX_PATH, &ffdw, 0);
1912 if (hr == S_OK && wsz[0] != NUL)
1913 rfname = utf16_to_enc(wsz, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001915shortcut_errorw:
1916 vim_free(p);
1917 }
1918 }
1919
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 // Release all interface pointers (both belong to the same object)
1921 if (ppf != NULL)
1922 ppf->lpVtbl->Release(ppf);
1923 if (psl != NULL)
1924 psl->lpVtbl->Release(psl);
Bram Moolenaar604729e2013-08-30 16:44:19 +02001925 if (pslw != NULL)
1926 pslw->lpVtbl->Release(pslw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927
1928 CoUninitialize();
1929 return rfname;
1930}
Bram Moolenaardce1e892019-02-10 23:18:53 +01001931
1932 char_u *
1933mch_resolve_path(char_u *fname, int reparse_point)
1934{
1935 char_u *path = resolve_shortcut(fname);
1936
1937 if (path == NULL && reparse_point)
1938 path = resolve_reparse_point(fname);
1939 return path;
1940}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941#endif
1942
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001943#if (defined(FEAT_EVAL) && (!defined(FEAT_GUI) || defined(VIMDLL))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944/*
1945 * Bring ourselves to the foreground. Does work if the OS doesn't allow it.
1946 */
1947 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001948win32_set_foreground(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949{
Bram Moolenaar0f873732019-12-05 20:28:46 +01001950 GetConsoleHwnd(); // get value of s_hwnd
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 if (s_hwnd != 0)
1952 SetForegroundWindow(s_hwnd);
1953}
1954#endif
1955
1956#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
1957/*
1958 * Client-server code for Vim
1959 *
1960 * Originally written by Paul Moore
1961 */
1962
Bram Moolenaar0f873732019-12-05 20:28:46 +01001963// In order to handle inter-process messages, we need to have a window. But
1964// the functions in this module can be called before the main GUI window is
1965// created (and may also be called in the console version, where there is no
1966// GUI window at all).
1967//
1968// So we create a hidden window, and arrange to destroy it on exit.
1969HWND message_window = 0; // window that's handling messages
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001971# define VIM_CLASSNAME "VIM_MESSAGES"
1972# define VIM_CLASSNAME_LEN (sizeof(VIM_CLASSNAME) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001974// Communication is via WM_COPYDATA messages. The message type is sent in
Bram Moolenaar0f873732019-12-05 20:28:46 +01001975// the dwData parameter. Types are defined here.
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001976# define COPYDATA_KEYS 0
1977# define COPYDATA_REPLY 1
1978# define COPYDATA_EXPR 10
1979# define COPYDATA_RESULT 11
1980# define COPYDATA_ERROR_RESULT 12
1981# define COPYDATA_ENCODING 20
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982
Bram Moolenaar0f873732019-12-05 20:28:46 +01001983// This is a structure containing a server HWND and its name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984struct server_id
1985{
1986 HWND hwnd;
1987 char_u *name;
1988};
1989
Bram Moolenaar0f873732019-12-05 20:28:46 +01001990// Last received 'encoding' that the client uses.
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001991static char_u *client_enc = NULL;
1992
1993/*
1994 * Tell the other side what encoding we are using.
1995 * Errors are ignored.
1996 */
1997 static void
1998serverSendEnc(HWND target)
1999{
2000 COPYDATASTRUCT data;
2001
2002 data.dwData = COPYDATA_ENCODING;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002003 data.cbData = (DWORD)STRLEN(p_enc) + 1;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002004 data.lpData = p_enc;
2005 (void)SendMessage(target, WM_COPYDATA, (WPARAM)message_window,
2006 (LPARAM)(&data));
2007}
2008
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009/*
2010 * Clean up on exit. This destroys the hidden message window.
2011 */
2012 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013CleanUpMessaging(void)
2014{
2015 if (message_window != 0)
2016 {
2017 DestroyWindow(message_window);
2018 message_window = 0;
2019 }
2020}
2021
2022static int save_reply(HWND server, char_u *reply, int expr);
2023
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002024/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 * The window procedure for the hidden message window.
2026 * It handles callback messages and notifications from servers.
2027 * In order to process these messages, it is necessary to run a
2028 * message loop. Code which may run before the main message loop
2029 * is started (in the GUI) is careful to pump messages when it needs
2030 * to. Features which require message delivery during normal use will
2031 * not work in the console version - this basically means those
2032 * features which allow Vim to act as a server, rather than a client.
2033 */
2034 static LRESULT CALLBACK
2035Messaging_WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
2036{
2037 if (msg == WM_COPYDATA)
2038 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002039 // This is a message from another Vim. The dwData member of the
2040 // COPYDATASTRUCT determines the type of message:
2041 // COPYDATA_ENCODING:
2042 // The encoding that the client uses. Following messages will
2043 // use this encoding, convert if needed.
2044 // COPYDATA_KEYS:
2045 // A key sequence. We are a server, and a client wants these keys
2046 // adding to the input queue.
2047 // COPYDATA_REPLY:
2048 // A reply. We are a client, and a server has sent this message
2049 // in response to a request. (server2client())
2050 // COPYDATA_EXPR:
2051 // An expression. We are a server, and a client wants us to
2052 // evaluate this expression.
2053 // COPYDATA_RESULT:
2054 // A reply. We are a client, and a server has sent this message
2055 // in response to a COPYDATA_EXPR.
2056 // COPYDATA_ERROR_RESULT:
2057 // A reply. We are a client, and a server has sent this message
2058 // in response to a COPYDATA_EXPR that failed to evaluate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 COPYDATASTRUCT *data = (COPYDATASTRUCT*)lParam;
2060 HWND sender = (HWND)wParam;
2061 COPYDATASTRUCT reply;
2062 char_u *res;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 int retval;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002064 char_u *str;
2065 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066
2067 switch (data->dwData)
2068 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002069 case COPYDATA_ENCODING:
Bram Moolenaar0f873732019-12-05 20:28:46 +01002070 // Remember the encoding that the client uses.
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002071 vim_free(client_enc);
2072 client_enc = enc_canonize((char_u *)data->lpData);
2073 return 1;
2074
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 case COPYDATA_KEYS:
Bram Moolenaar0f873732019-12-05 20:28:46 +01002076 // Remember who sent this, for <client>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 clientWindow = sender;
2078
Bram Moolenaar0f873732019-12-05 20:28:46 +01002079 // Add the received keys to the input buffer. The loop waiting
2080 // for the user to do something should check the input buffer.
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002081 str = serverConvert(client_enc, (char_u *)data->lpData, &tofree);
2082 server_to_input_buf(str);
2083 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084
2085# ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01002086 // Wake up the main GUI loop.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002087# ifdef VIMDLL
2088 if (gui.in_use)
2089# endif
2090 if (s_hwnd != 0)
2091 PostMessage(s_hwnd, WM_NULL, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092# endif
2093 return 1;
2094
2095 case COPYDATA_EXPR:
Bram Moolenaar0f873732019-12-05 20:28:46 +01002096 // Remember who sent this, for <client>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097 clientWindow = sender;
2098
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002099 str = serverConvert(client_enc, (char_u *)data->lpData, &tofree);
2100 res = eval_client_expr_to_string(str);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002101
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 if (res == NULL)
2103 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00002104 char *err = _(e_invalid_expression_received);
Bram Moolenaar15bf76d2017-03-18 16:18:37 +01002105 size_t len = STRLEN(str) + STRLEN(err) + 5;
2106
Bram Moolenaar964b3742019-05-24 18:54:09 +02002107 res = alloc(len);
Bram Moolenaar15bf76d2017-03-18 16:18:37 +01002108 if (res != NULL)
2109 vim_snprintf((char *)res, len, "%s: \"%s\"", err, str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 reply.dwData = COPYDATA_ERROR_RESULT;
2111 }
2112 else
2113 reply.dwData = COPYDATA_RESULT;
2114 reply.lpData = res;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002115 reply.cbData = (DWORD)STRLEN(res) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002117 serverSendEnc(sender);
Bram Moolenaar5246cd72013-06-16 16:41:47 +02002118 retval = (int)SendMessage(sender, WM_COPYDATA,
2119 (WPARAM)message_window, (LPARAM)(&reply));
Bram Moolenaar15bf76d2017-03-18 16:18:37 +01002120 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 vim_free(res);
2122 return retval;
2123
2124 case COPYDATA_REPLY:
2125 case COPYDATA_RESULT:
2126 case COPYDATA_ERROR_RESULT:
2127 if (data->lpData != NULL)
2128 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002129 str = serverConvert(client_enc, (char_u *)data->lpData,
2130 &tofree);
2131 if (tofree == NULL)
2132 str = vim_strsave(str);
2133 if (save_reply(sender, str,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 (data->dwData == COPYDATA_REPLY ? 0 :
2135 (data->dwData == COPYDATA_RESULT ? 1 :
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002136 2))) == FAIL)
2137 vim_free(str);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002138 else if (data->dwData == COPYDATA_REPLY)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 {
Bram Moolenaar427d51c2013-06-16 16:01:25 +02002140 char_u winstr[30];
2141
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002142 sprintf((char *)winstr, PRINTF_HEX_LONG_U, (long_u)sender);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002143 apply_autocmds(EVENT_REMOTEREPLY, winstr, str,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144 TRUE, curbuf);
2145 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 }
2147 return 1;
2148 }
2149
2150 return 0;
2151 }
2152
2153 else if (msg == WM_ACTIVATE && wParam == WA_ACTIVE)
2154 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002155 // When the message window is activated (brought to the foreground),
2156 // this actually applies to the text window.
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002157# if !defined(FEAT_GUI) || defined(VIMDLL)
2158# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002159 if (!gui.in_use)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002160# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002161 GetConsoleHwnd(); // get value of s_hwnd
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002162# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 if (s_hwnd != 0)
2164 {
2165 SetForegroundWindow(s_hwnd);
2166 return 0;
2167 }
2168 }
2169
2170 return DefWindowProc(hwnd, msg, wParam, lParam);
2171}
2172
2173/*
2174 * Initialise the message handling process. This involves creating a window
2175 * to handle messages - the window will not be visible.
2176 */
2177 void
2178serverInitMessaging(void)
2179{
2180 WNDCLASS wndclass;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181
Bram Moolenaar0f873732019-12-05 20:28:46 +01002182 // Clean up on exit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 atexit(CleanUpMessaging);
2184
Bram Moolenaar0f873732019-12-05 20:28:46 +01002185 // Register a window class - we only really care
2186 // about the window procedure
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 wndclass.style = 0;
2188 wndclass.lpfnWndProc = Messaging_WndProc;
2189 wndclass.cbClsExtra = 0;
2190 wndclass.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002191 wndclass.hInstance = g_hinst;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 wndclass.hIcon = NULL;
2193 wndclass.hCursor = NULL;
2194 wndclass.hbrBackground = NULL;
2195 wndclass.lpszMenuName = NULL;
2196 wndclass.lpszClassName = VIM_CLASSNAME;
2197 RegisterClass(&wndclass);
2198
Bram Moolenaar0f873732019-12-05 20:28:46 +01002199 // Create the message window. It will be hidden, so the details don't
2200 // matter. Don't use WS_OVERLAPPEDWINDOW, it will make a shortcut remove
2201 // focus from gvim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 message_window = CreateWindow(VIM_CLASSNAME, "",
2203 WS_POPUPWINDOW | WS_CAPTION,
2204 CW_USEDEFAULT, CW_USEDEFAULT,
2205 100, 100, NULL, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002206 g_hinst, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207}
2208
Bram Moolenaar0f873732019-12-05 20:28:46 +01002209// Used by serverSendToVim() to find an alternate server name.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002210static char_u *altname_buf_ptr = NULL;
2211
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212/*
2213 * Get the title of the window "hwnd", which is the Vim server name, in
2214 * "name[namelen]" and return the length.
2215 * Returns zero if window "hwnd" is not a Vim server.
2216 */
2217 static int
2218getVimServerName(HWND hwnd, char *name, int namelen)
2219{
2220 int len;
2221 char buffer[VIM_CLASSNAME_LEN + 1];
2222
Bram Moolenaar0f873732019-12-05 20:28:46 +01002223 // Ignore windows which aren't Vim message windows
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 len = GetClassName(hwnd, buffer, sizeof(buffer));
2225 if (len != VIM_CLASSNAME_LEN || STRCMP(buffer, VIM_CLASSNAME) != 0)
2226 return 0;
2227
Bram Moolenaar0f873732019-12-05 20:28:46 +01002228 // Get the title of the window
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 return GetWindowText(hwnd, name, namelen);
2230}
2231
2232 static BOOL CALLBACK
2233enumWindowsGetServer(HWND hwnd, LPARAM lparam)
2234{
2235 struct server_id *id = (struct server_id *)lparam;
2236 char server[MAX_PATH];
2237
Bram Moolenaar0f873732019-12-05 20:28:46 +01002238 // Get the title of the window
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 if (getVimServerName(hwnd, server, sizeof(server)) == 0)
2240 return TRUE;
2241
Bram Moolenaar0f873732019-12-05 20:28:46 +01002242 // If this is the server we're looking for, return its HWND
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 if (STRICMP(server, id->name) == 0)
2244 {
2245 id->hwnd = hwnd;
2246 return FALSE;
2247 }
2248
Bram Moolenaar0f873732019-12-05 20:28:46 +01002249 // If we are looking for an alternate server, remember this name.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002250 if (altname_buf_ptr != NULL
2251 && STRNICMP(server, id->name, STRLEN(id->name)) == 0
2252 && vim_isdigit(server[STRLEN(id->name)]))
2253 {
2254 STRCPY(altname_buf_ptr, server);
Bram Moolenaar0f873732019-12-05 20:28:46 +01002255 altname_buf_ptr = NULL; // don't use another name
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002256 }
2257
Bram Moolenaar0f873732019-12-05 20:28:46 +01002258 // Otherwise, keep looking
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 return TRUE;
2260}
2261
2262 static BOOL CALLBACK
2263enumWindowsGetNames(HWND hwnd, LPARAM lparam)
2264{
2265 garray_T *ga = (garray_T *)lparam;
2266 char server[MAX_PATH];
2267
Bram Moolenaar0f873732019-12-05 20:28:46 +01002268 // Get the title of the window
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 if (getVimServerName(hwnd, server, sizeof(server)) == 0)
2270 return TRUE;
2271
Bram Moolenaar0f873732019-12-05 20:28:46 +01002272 // Add the name to the list
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002273 ga_concat(ga, (char_u *)server);
2274 ga_concat(ga, (char_u *)"\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 return TRUE;
2276}
2277
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002278struct enum_windows_s
2279{
2280 WNDENUMPROC lpEnumFunc;
2281 LPARAM lParam;
2282};
2283
2284 static BOOL CALLBACK
2285enum_windows_child(HWND hwnd, LPARAM lParam)
2286{
2287 struct enum_windows_s *ew = (struct enum_windows_s *)lParam;
2288
2289 return (ew->lpEnumFunc)(hwnd, ew->lParam);
2290}
2291
2292 static BOOL CALLBACK
2293enum_windows_toplevel(HWND hwnd, LPARAM lParam)
2294{
2295 struct enum_windows_s *ew = (struct enum_windows_s *)lParam;
2296
Bram Moolenaar95ba5c32018-10-07 22:47:07 +02002297 if ((ew->lpEnumFunc)(hwnd, ew->lParam))
2298 return TRUE;
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002299 return EnumChildWindows(hwnd, enum_windows_child, lParam);
2300}
2301
Bram Moolenaar0f873732019-12-05 20:28:46 +01002302/*
2303 * Enumerate all windows including children.
2304 */
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002305 static BOOL
2306enum_windows(WNDENUMPROC lpEnumFunc, LPARAM lParam)
2307{
2308 struct enum_windows_s ew;
2309
2310 ew.lpEnumFunc = lpEnumFunc;
2311 ew.lParam = lParam;
2312 return EnumWindows(enum_windows_toplevel, (LPARAM)&ew);
2313}
2314
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 static HWND
2316findServer(char_u *name)
2317{
2318 struct server_id id;
2319
2320 id.name = name;
2321 id.hwnd = 0;
2322
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002323 enum_windows(enumWindowsGetServer, (LPARAM)(&id));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324
2325 return id.hwnd;
2326}
2327
2328 void
2329serverSetName(char_u *name)
2330{
2331 char_u *ok_name;
2332 HWND hwnd = 0;
2333 int i = 0;
2334 char_u *p;
2335
Bram Moolenaar0f873732019-12-05 20:28:46 +01002336 // Leave enough space for a 9-digit suffix to ensure uniqueness!
Bram Moolenaar964b3742019-05-24 18:54:09 +02002337 ok_name = alloc(STRLEN(name) + 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338
2339 STRCPY(ok_name, name);
2340 p = ok_name + STRLEN(name);
2341
2342 for (;;)
2343 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002344 // This is inefficient - we're doing an EnumWindows loop for each
2345 // possible name. It would be better to grab all names in one go,
2346 // and scan the list each time...
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 hwnd = findServer(ok_name);
2348 if (hwnd == 0)
2349 break;
2350
2351 ++i;
2352 if (i >= 1000)
2353 break;
2354
2355 sprintf((char *)p, "%d", i);
2356 }
2357
2358 if (hwnd != 0)
2359 vim_free(ok_name);
2360 else
2361 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002362 // Remember the name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 serverName = ok_name;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002364 need_maketitle = TRUE; // update Vim window title later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365
Bram Moolenaar0f873732019-12-05 20:28:46 +01002366 // Update the message window title
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002367 SetWindowText(message_window, (LPCSTR)ok_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002369# ifdef FEAT_EVAL
Bram Moolenaar0f873732019-12-05 20:28:46 +01002370 // Set the servername variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 set_vim_var_string(VV_SEND_SERVER, serverName, -1);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002372# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 }
2374}
2375
2376 char_u *
2377serverGetVimNames(void)
2378{
2379 garray_T ga;
2380
2381 ga_init2(&ga, 1, 100);
2382
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002383 enum_windows(enumWindowsGetNames, (LPARAM)(&ga));
Bram Moolenaar269ec652004-07-29 08:43:53 +00002384 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385
2386 return ga.ga_data;
2387}
2388
2389 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002390serverSendReply(
Bram Moolenaar0f873732019-12-05 20:28:46 +01002391 char_u *name, // Where to send.
2392 char_u *reply) // What to send.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393{
2394 HWND target;
2395 COPYDATASTRUCT data;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002396 long_u n = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397
Bram Moolenaar0f873732019-12-05 20:28:46 +01002398 // The "name" argument is a magic cookie obtained from expand("<client>").
2399 // It should be of the form 0xXXXXX - i.e. a C hex literal, which is the
2400 // value of the client's message window HWND.
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002401 sscanf((char *)name, SCANF_HEX_LONG_U, &n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 if (n == 0)
2403 return -1;
2404
2405 target = (HWND)n;
2406 if (!IsWindow(target))
2407 return -1;
2408
2409 data.dwData = COPYDATA_REPLY;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002410 data.cbData = (DWORD)STRLEN(reply) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 data.lpData = reply;
2412
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002413 serverSendEnc(target);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 if (SendMessage(target, WM_COPYDATA, (WPARAM)message_window,
2415 (LPARAM)(&data)))
2416 return 0;
2417
2418 return -1;
2419}
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;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002435 char_u altname_buf[MAX_PATH];
2436
Bram Moolenaar0f873732019-12-05 20:28:46 +01002437 // Execute locally if no display or target is ourselves
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01002438 if (serverName != NULL && STRICMP(name, serverName) == 0)
2439 return sendToLocalVim(cmd, asExpr, result);
2440
Bram Moolenaar0f873732019-12-05 20:28:46 +01002441 // If the server name does not end in a digit then we look for an
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00002442 // alternate name. e.g. when "name" is GVIM then we may find GVIM2.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002443 if (STRLEN(name) > 1 && !vim_isdigit(name[STRLEN(name) - 1]))
2444 altname_buf_ptr = altname_buf;
2445 altname_buf[0] = NUL;
2446 target = findServer(name);
2447 altname_buf_ptr = NULL;
2448 if (target == 0 && altname_buf[0] != NUL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01002449 // Use another server name we found.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002450 target = findServer(altname_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451
2452 if (target == 0)
2453 {
2454 if (!silent)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002455 semsg(_(e_no_registered_server_named_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 return -1;
2457 }
2458
2459 if (ptarget)
2460 *(HWND *)ptarget = target;
2461
2462 data.dwData = asExpr ? COPYDATA_EXPR : COPYDATA_KEYS;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002463 data.cbData = (DWORD)STRLEN(cmd) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 data.lpData = cmd;
2465
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002466 serverSendEnc(target);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 if (SendMessage(target, WM_COPYDATA, (WPARAM)message_window,
2468 (LPARAM)(&data)) == 0)
2469 return -1;
2470
2471 if (asExpr)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002472 retval = serverGetReply(target, &retcode, TRUE, TRUE, timeout);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473
2474 if (result == NULL)
2475 vim_free(retval);
2476 else
Bram Moolenaar0f873732019-12-05 20:28:46 +01002477 *result = retval; // Caller assumes responsibility for freeing
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478
2479 return retcode;
2480}
2481
2482/*
2483 * Bring the server to the foreground.
2484 */
2485 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002486serverForeground(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487{
2488 HWND target = findServer(name);
2489
2490 if (target != 0)
2491 SetForegroundWindow(target);
2492}
2493
Bram Moolenaar0f873732019-12-05 20:28:46 +01002494// Replies from server need to be stored until the client picks them up via
2495// remote_read(). So we maintain a list of server-id/reply pairs.
2496// Note that there could be multiple replies from one server pending if the
2497// client is slow picking them up.
2498// We just store the replies in a simple list. When we remove an entry, we
2499// move list entries down to fill the gap.
2500// The server ID is simply the HWND.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501typedef struct
2502{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002503 HWND server; // server window
2504 char_u *reply; // reply string
2505 int expr_result; // 0 for REPLY, 1 for RESULT 2 for error
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002506} reply_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507
2508static garray_T reply_list = {0, 0, sizeof(reply_T), 5, 0};
2509
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002510# define REPLY_ITEM(i) ((reply_T *)(reply_list.ga_data) + (i))
2511# define REPLY_COUNT (reply_list.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512
Bram Moolenaar0f873732019-12-05 20:28:46 +01002513// Flag which is used to wait for a reply
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514static int reply_received = 0;
2515
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002516/*
2517 * Store a reply. "reply" must be allocated memory (or NULL).
2518 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 static int
2520save_reply(HWND server, char_u *reply, int expr)
2521{
2522 reply_T *rep;
2523
2524 if (ga_grow(&reply_list, 1) == FAIL)
2525 return FAIL;
2526
2527 rep = REPLY_ITEM(REPLY_COUNT);
2528 rep->server = server;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002529 rep->reply = reply;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 rep->expr_result = expr;
2531 if (rep->reply == NULL)
2532 return FAIL;
2533
2534 ++REPLY_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 reply_received = 1;
2536 return OK;
2537}
2538
2539/*
2540 * Get a reply from server "server".
2541 * When "expr_res" is non NULL, get the result of an expression, otherwise a
2542 * server2client() message.
2543 * When non NULL, point to return code. 0 => OK, -1 => ERROR
2544 * If "remove" is TRUE, consume the message, the caller must free it then.
2545 * if "wait" is TRUE block until a message arrives (or the server exits).
2546 */
2547 char_u *
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002548serverGetReply(HWND server, int *expr_res, int remove, int wait, int timeout)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549{
2550 int i;
2551 char_u *reply;
2552 reply_T *rep;
Bram Moolenaar15e737f2017-03-18 21:22:47 +01002553 int did_process = FALSE;
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002554 time_t start;
2555 time_t now;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556
Bram Moolenaar0f873732019-12-05 20:28:46 +01002557 // When waiting, loop until the message waiting for is received.
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002558 time(&start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 for (;;)
2560 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002561 // Reset this here, in case a message arrives while we are going
2562 // through the already received messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 reply_received = 0;
2564
2565 for (i = 0; i < REPLY_COUNT; ++i)
2566 {
2567 rep = REPLY_ITEM(i);
2568 if (rep->server == server
2569 && ((rep->expr_result != 0) == (expr_res != NULL)))
2570 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002571 // Save the values we've found for later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 reply = rep->reply;
2573 if (expr_res != NULL)
2574 *expr_res = rep->expr_result == 1 ? 0 : -1;
2575
2576 if (remove)
2577 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002578 // Move the rest of the list down to fill the gap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 mch_memmove(rep, rep + 1,
2580 (REPLY_COUNT - i - 1) * sizeof(reply_T));
2581 --REPLY_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 }
2583
Bram Moolenaar0f873732019-12-05 20:28:46 +01002584 // Return the reply to the caller, who takes on responsibility
2585 // for freeing it if "remove" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 return reply;
2587 }
2588 }
2589
Bram Moolenaar0f873732019-12-05 20:28:46 +01002590 // If we got here, we didn't find a reply. Return immediately if the
2591 // "wait" parameter isn't set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 if (!wait)
Bram Moolenaar15e737f2017-03-18 21:22:47 +01002593 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002594 // Process pending messages once. Without this, looping on
2595 // remote_peek() would never get the reply.
Bram Moolenaar15e737f2017-03-18 21:22:47 +01002596 if (!did_process)
2597 {
2598 did_process = TRUE;
2599 serverProcessPendingMessages();
2600 continue;
2601 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 break;
Bram Moolenaar15e737f2017-03-18 21:22:47 +01002603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604
Bram Moolenaar0f873732019-12-05 20:28:46 +01002605 // We need to wait for a reply. Enter a message loop until the
2606 // "reply_received" flag gets set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607
Bram Moolenaar0f873732019-12-05 20:28:46 +01002608 // Loop until we receive a reply
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 while (reply_received == 0)
2610 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002611# ifdef FEAT_TIMERS
Bram Moolenaar0f873732019-12-05 20:28:46 +01002612 // TODO: use the return value to decide how long to wait.
Bram Moolenaar42205552017-03-18 19:42:22 +01002613 check_due_timer();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002614# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002615 time(&now);
2616 if (timeout > 0 && (now - start) >= timeout)
2617 break;
2618
Bram Moolenaar0f873732019-12-05 20:28:46 +01002619 // Wait for a SendMessage() call to us. This could be the reply
2620 // we are waiting for. Use a timeout of a second, to catch the
2621 // situation that the server died unexpectedly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 MsgWaitForMultipleObjects(0, NULL, TRUE, 1000, QS_ALLINPUT);
2623
Bram Moolenaar0f873732019-12-05 20:28:46 +01002624 // If the server has died, give up
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 if (!IsWindow(server))
2626 return NULL;
2627
2628 serverProcessPendingMessages();
2629 }
2630 }
2631
2632 return NULL;
2633}
2634
2635/*
2636 * Process any messages in the Windows message queue.
2637 */
2638 void
2639serverProcessPendingMessages(void)
2640{
2641 MSG msg;
2642
K.Takatab7057bd2022-01-21 11:37:07 +00002643 while (PeekMessageW(&msg, NULL, 0, 0, PM_REMOVE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 {
2645 TranslateMessage(&msg);
K.Takatab7057bd2022-01-21 11:37:07 +00002646 DispatchMessageW(&msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 }
2648}
2649
Bram Moolenaar0f873732019-12-05 20:28:46 +01002650#endif // FEAT_CLIENTSERVER
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651
2652#if defined(FEAT_GUI) || (defined(FEAT_PRINTER) && !defined(FEAT_POSTSCRIPT)) \
2653 || defined(PROTO)
2654
2655struct charset_pair
2656{
2657 char *name;
2658 BYTE charset;
2659};
2660
2661static struct charset_pair
2662charset_pairs[] =
2663{
2664 {"ANSI", ANSI_CHARSET},
2665 {"CHINESEBIG5", CHINESEBIG5_CHARSET},
2666 {"DEFAULT", DEFAULT_CHARSET},
2667 {"HANGEUL", HANGEUL_CHARSET},
2668 {"OEM", OEM_CHARSET},
2669 {"SHIFTJIS", SHIFTJIS_CHARSET},
2670 {"SYMBOL", SYMBOL_CHARSET},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 {"ARABIC", ARABIC_CHARSET},
2672 {"BALTIC", BALTIC_CHARSET},
2673 {"EASTEUROPE", EASTEUROPE_CHARSET},
2674 {"GB2312", GB2312_CHARSET},
2675 {"GREEK", GREEK_CHARSET},
2676 {"HEBREW", HEBREW_CHARSET},
2677 {"JOHAB", JOHAB_CHARSET},
2678 {"MAC", MAC_CHARSET},
2679 {"RUSSIAN", RUSSIAN_CHARSET},
2680 {"THAI", THAI_CHARSET},
2681 {"TURKISH", TURKISH_CHARSET},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002682# ifdef VIETNAMESE_CHARSET
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 {"VIETNAMESE", VIETNAMESE_CHARSET},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002684# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 {NULL, 0}
2686};
2687
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002688struct quality_pair
2689{
2690 char *name;
2691 DWORD quality;
2692};
2693
2694static struct quality_pair
2695quality_pairs[] = {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002696# ifdef CLEARTYPE_QUALITY
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002697 {"CLEARTYPE", CLEARTYPE_QUALITY},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002698# endif
2699# ifdef ANTIALIASED_QUALITY
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002700 {"ANTIALIASED", ANTIALIASED_QUALITY},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002701# endif
2702# ifdef NONANTIALIASED_QUALITY
Bram Moolenaar73a733e2016-05-11 21:05:05 +02002703 {"NONANTIALIASED", NONANTIALIASED_QUALITY},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002704# endif
2705# ifdef PROOF_QUALITY
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002706 {"PROOF", PROOF_QUALITY},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002707# endif
2708# ifdef DRAFT_QUALITY
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002709 {"DRAFT", DRAFT_QUALITY},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002710# endif
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002711 {"DEFAULT", DEFAULT_QUALITY},
2712 {NULL, 0}
2713};
2714
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715/*
2716 * Convert a charset ID to a name.
2717 * Return NULL when not recognized.
2718 */
2719 char *
2720charset_id2name(int id)
2721{
2722 struct charset_pair *cp;
2723
2724 for (cp = charset_pairs; cp->name != NULL; ++cp)
2725 if ((BYTE)id == cp->charset)
2726 break;
2727 return cp->name;
2728}
2729
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002730/*
2731 * Convert a quality ID to a name.
2732 * Return NULL when not recognized.
2733 */
2734 char *
2735quality_id2name(DWORD id)
2736{
2737 struct quality_pair *qp;
2738
2739 for (qp = quality_pairs; qp->name != NULL; ++qp)
2740 if (id == qp->quality)
2741 break;
2742 return qp->name;
2743}
2744
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002745static const LOGFONTW s_lfDefault =
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746{
2747 -12, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, DEFAULT_CHARSET,
2748 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
2749 PROOF_QUALITY, FIXED_PITCH | FF_DONTCARE,
Bram Moolenaar0f873732019-12-05 20:28:46 +01002750 L"Fixedsys" // see _ReadVimIni
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751};
2752
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002753// Initialise the "current height" to -12 (same as s_lfDefault) just
2754// in case the user specifies a font in "guifont" with no size before a font
2755// with an explicit size has been set. This defaults the size to this value
2756// (-12 equates to roughly 9pt).
2757int current_font_height = -12; // also used in gui_w32.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758
Bram Moolenaar0f873732019-12-05 20:28:46 +01002759/*
2760 * Convert a string representing a point size into pixels. The string should
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 * be a positive decimal number, with an optional decimal point (eg, "12", or
2762 * "10.5"). The pixel value is returned, and a pointer to the next unconverted
2763 * character is stored in *end. The flag "vertical" says whether this
2764 * calculation is for a vertical (height) size or a horizontal (width) one.
2765 */
2766 static int
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002767points_to_pixels(WCHAR *str, WCHAR **end, int vertical, long_i pprinter_dc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768{
2769 int pixels;
2770 int points = 0;
2771 int divisor = 0;
2772 HWND hwnd = (HWND)0;
2773 HDC hdc;
2774 HDC printer_dc = (HDC)pprinter_dc;
2775
2776 while (*str != NUL)
2777 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002778 if (*str == L'.' && divisor == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002780 // Start keeping a divisor, for later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 divisor = 1;
2782 }
2783 else
2784 {
2785 if (!VIM_ISDIGIT(*str))
2786 break;
2787
2788 points *= 10;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002789 points += *str - L'0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790 divisor *= 10;
2791 }
2792 ++str;
2793 }
2794
2795 if (divisor == 0)
2796 divisor = 1;
2797
2798 if (printer_dc == NULL)
2799 {
2800 hwnd = GetDesktopWindow();
2801 hdc = GetWindowDC(hwnd);
2802 }
2803 else
2804 hdc = printer_dc;
2805
2806 pixels = MulDiv(points,
2807 GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX),
2808 72 * divisor);
2809
2810 if (printer_dc == NULL)
2811 ReleaseDC(hwnd, hdc);
2812
2813 *end = str;
2814 return pixels;
2815}
2816
2817 static int CALLBACK
2818font_enumproc(
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002819 ENUMLOGFONTW *elf,
2820 NEWTEXTMETRICW *ntm UNUSED,
2821 DWORD type UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 LPARAM lparam)
2823{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002824 // Return value:
2825 // 0 = terminate now (monospace & ANSI)
2826 // 1 = continue, still no luck...
2827 // 2 = continue, but we have an acceptable LOGFONTW
2828 // (monospace, not ANSI)
2829 // We use these values, as EnumFontFamilies returns 1 if the
2830 // callback function is never called. So, we check the return as
2831 // 0 = perfect, 2 = OK, 1 = no good...
2832 // It's not pretty, but it works!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002834 LOGFONTW *lf = (LOGFONTW *)(lparam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002836# ifndef FEAT_PROPORTIONAL_FONTS
Bram Moolenaar0f873732019-12-05 20:28:46 +01002837 // Ignore non-monospace fonts without further ado
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838 if ((ntm->tmPitchAndFamily & 1) != 0)
2839 return 1;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002840# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841
Bram Moolenaar0f873732019-12-05 20:28:46 +01002842 // Remember this LOGFONTW as a "possible"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 *lf = elf->elfLogFont;
2844
Bram Moolenaar0f873732019-12-05 20:28:46 +01002845 // Terminate the scan as soon as we find an ANSI font
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846 if (lf->lfCharSet == ANSI_CHARSET
2847 || lf->lfCharSet == OEM_CHARSET
2848 || lf->lfCharSet == DEFAULT_CHARSET)
2849 return 0;
2850
Bram Moolenaar0f873732019-12-05 20:28:46 +01002851 // Continue the scan - we have a non-ANSI font
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 return 2;
2853}
2854
2855 static int
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002856init_logfont(LOGFONTW *lf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857{
2858 int n;
2859 HWND hwnd = GetDesktopWindow();
2860 HDC hdc = GetWindowDC(hwnd);
2861
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002862 n = EnumFontFamiliesW(hdc,
2863 lf->lfFaceName,
2864 (FONTENUMPROCW)font_enumproc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 (LPARAM)lf);
2866
2867 ReleaseDC(hwnd, hdc);
2868
Bram Moolenaar0f873732019-12-05 20:28:46 +01002869 // If we couldn't find a usable font, return failure
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 if (n == 1)
2871 return FAIL;
2872
Bram Moolenaar0f873732019-12-05 20:28:46 +01002873 // Tidy up the rest of the LOGFONTW structure. We set to a basic
2874 // font - get_logfont() sets bold, italic, etc based on the user's
2875 // input.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 lf->lfHeight = current_font_height;
2877 lf->lfWidth = 0;
2878 lf->lfItalic = FALSE;
2879 lf->lfUnderline = FALSE;
2880 lf->lfStrikeOut = FALSE;
2881 lf->lfWeight = FW_NORMAL;
2882
Bram Moolenaar0f873732019-12-05 20:28:46 +01002883 // Return success
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 return OK;
2885}
2886
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002887/*
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002888 * Compare a UTF-16 string and an ASCII string literally.
2889 * Only works all the code points are inside ASCII range.
2890 */
2891 static int
2892utf16ascncmp(const WCHAR *w, const char *p, size_t n)
2893{
2894 size_t i;
2895
2896 for (i = 0; i < n; i++)
2897 {
2898 if (w[i] == 0 || w[i] != p[i])
2899 return w[i] - p[i];
2900 }
2901 return 0;
2902}
2903
2904/*
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002905 * Get font info from "name" into logfont "lf".
2906 * Return OK for a valid name, FAIL otherwise.
2907 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 int
2909get_logfont(
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002910 LOGFONTW *lf,
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002911 char_u *name,
2912 HDC printer_dc,
2913 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002915 WCHAR *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 int i;
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002917 int ret = FAIL;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002918 static LOGFONTW *lastlf = NULL;
2919 WCHAR *wname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920
2921 *lf = s_lfDefault;
2922 if (name == NULL)
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002923 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002925 wname = enc_to_utf16(name, NULL);
2926 if (wname == NULL)
2927 return FAIL;
2928
2929 if (wcscmp(wname, L"*") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002931# if defined(FEAT_GUI_MSWIN)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002932 CHOOSEFONTW cf;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002933 // if name is "*", bring up std font dialog:
Bram Moolenaara80faa82020-04-12 19:37:17 +02002934 CLEAR_FIELD(cf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 cf.lStructSize = sizeof(cf);
2936 cf.hwndOwner = s_hwnd;
2937 cf.Flags = CF_SCREENFONTS | CF_FIXEDPITCHONLY | CF_INITTOLOGFONTSTRUCT;
2938 if (lastlf != NULL)
2939 *lf = *lastlf;
2940 cf.lpLogFont = lf;
2941 cf.nFontType = 0 ; //REGULAR_FONTTYPE;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002942 if (ChooseFontW(&cf))
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002943 ret = OK;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002944# endif
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002945 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 }
2947
2948 /*
2949 * Split name up, it could be <name>:h<height>:w<width> etc.
2950 */
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002951 for (p = wname; *p && *p != L':'; p++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002953 if (p - wname + 1 >= LF_FACESIZE)
Bram Moolenaar0f873732019-12-05 20:28:46 +01002954 goto theend; // Name too long
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002955 lf->lfFaceName[p - wname] = *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 }
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002957 if (p != wname)
2958 lf->lfFaceName[p - wname] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959
Bram Moolenaar0f873732019-12-05 20:28:46 +01002960 // First set defaults
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 lf->lfHeight = -12;
2962 lf->lfWidth = 0;
2963 lf->lfWeight = FW_NORMAL;
2964 lf->lfItalic = FALSE;
2965 lf->lfUnderline = FALSE;
2966 lf->lfStrikeOut = FALSE;
2967
2968 /*
2969 * If the font can't be found, try replacing '_' by ' '.
2970 */
2971 if (init_logfont(lf) == FAIL)
2972 {
2973 int did_replace = FALSE;
2974
2975 for (i = 0; lf->lfFaceName[i]; ++i)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002976 if (lf->lfFaceName[i] == L'_')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002978 lf->lfFaceName[i] = L' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 did_replace = TRUE;
2980 }
2981 if (!did_replace || init_logfont(lf) == FAIL)
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002982 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 }
2984
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002985 while (*p == L':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986 p++;
2987
Bram Moolenaar0f873732019-12-05 20:28:46 +01002988 // Set the values found after ':'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989 while (*p)
2990 {
2991 switch (*p++)
2992 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002993 case L'h':
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002994 lf->lfHeight = - points_to_pixels(p, &p, TRUE, (long_i)printer_dc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002996 case L'w':
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002997 lf->lfWidth = points_to_pixels(p, &p, FALSE, (long_i)printer_dc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998 break;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02002999 case L'W':
3000 lf->lfWeight = wcstol(p, &p, 10);
3001 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003002 case L'b':
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003 lf->lfWeight = FW_BOLD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003005 case L'i':
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006 lf->lfItalic = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003008 case L'u':
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009 lf->lfUnderline = TRUE;
3010 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003011 case L's':
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 lf->lfStrikeOut = TRUE;
3013 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003014 case L'c':
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015 {
3016 struct charset_pair *cp;
3017
3018 for (cp = charset_pairs; cp->name != NULL; ++cp)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003019 if (utf16ascncmp(p, cp->name, strlen(cp->name)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 {
3021 lf->lfCharSet = cp->charset;
3022 p += strlen(cp->name);
3023 break;
3024 }
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003025 if (cp->name == NULL && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003027 char_u *s = utf16_to_enc(p, NULL);
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00003028
3029 semsg(_(e_illegal_str_name_str_in_font_name_str),
3030 "charset", s, name);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003031 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 break;
3033 }
3034 break;
3035 }
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003036 case L'q':
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003037 {
3038 struct quality_pair *qp;
3039
3040 for (qp = quality_pairs; qp->name != NULL; ++qp)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003041 if (utf16ascncmp(p, qp->name, strlen(qp->name)) == 0)
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003042 {
3043 lf->lfQuality = qp->quality;
3044 p += strlen(qp->name);
3045 break;
3046 }
3047 if (qp->name == NULL && verbose)
3048 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003049 char_u *s = utf16_to_enc(p, NULL);
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00003050 semsg(_(e_illegal_str_name_str_in_font_name_str),
3051 "quality", s, name);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003052 vim_free(s);
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003053 break;
3054 }
3055 break;
3056 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057 default:
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003058 if (verbose)
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00003059 semsg(_(e_illegal_char_nr_in_font_name_str), p[-1], name);
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003060 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061 }
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003062 while (*p == L':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063 p++;
3064 }
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003065 ret = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067theend:
Bram Moolenaar0f873732019-12-05 20:28:46 +01003068 // ron: init lastlf
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003069 if (ret == OK && printer_dc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 {
3071 vim_free(lastlf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003072 lastlf = ALLOC_ONE(LOGFONTW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073 if (lastlf != NULL)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003074 mch_memmove(lastlf, lf, sizeof(LOGFONTW));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 }
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003076 vim_free(wname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003078 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079}
3080
Bram Moolenaar0f873732019-12-05 20:28:46 +01003081#endif // defined(FEAT_GUI) || defined(FEAT_PRINTER)
Bram Moolenaarf12d9832016-01-29 21:11:25 +01003082
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003083#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaarf12d9832016-01-29 21:11:25 +01003084/*
3085 * Initialize the Winsock dll.
3086 */
3087 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003088channel_init_winsock(void)
Bram Moolenaarf12d9832016-01-29 21:11:25 +01003089{
3090 WSADATA wsaData;
3091 int wsaerr;
3092
3093 if (WSInitialized)
3094 return;
3095
3096 wsaerr = WSAStartup(MAKEWORD(2, 2), &wsaData);
3097 if (wsaerr == 0)
3098 WSInitialized = TRUE;
3099}
3100#endif