blob: 256da045800e88d38e95004506247465b0521fb8 [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
K.Takataf9f2a332022-06-17 20:05:40 +01001974// Timeout for sending a message to another Vim instance. Normally this works
1975// instantly, but it may hang when the other Vim instance is halted.
1976# define SENDMESSAGE_TIMEOUT (5 * 1000)
1977
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001978// Communication is via WM_COPYDATA messages. The message type is sent in
Bram Moolenaar0f873732019-12-05 20:28:46 +01001979// the dwData parameter. Types are defined here.
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001980# define COPYDATA_KEYS 0
1981# define COPYDATA_REPLY 1
1982# define COPYDATA_EXPR 10
1983# define COPYDATA_RESULT 11
1984# define COPYDATA_ERROR_RESULT 12
1985# define COPYDATA_ENCODING 20
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986
Bram Moolenaar0f873732019-12-05 20:28:46 +01001987// This is a structure containing a server HWND and its name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988struct server_id
1989{
1990 HWND hwnd;
1991 char_u *name;
1992};
1993
Bram Moolenaar0f873732019-12-05 20:28:46 +01001994// Last received 'encoding' that the client uses.
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001995static char_u *client_enc = NULL;
1996
1997/*
1998 * Tell the other side what encoding we are using.
K.Takataf9f2a332022-06-17 20:05:40 +01001999 * Return -1 if timeout happens. Other errors are ignored.
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002000 */
K.Takataf9f2a332022-06-17 20:05:40 +01002001 static int
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002002serverSendEnc(HWND target)
2003{
2004 COPYDATASTRUCT data;
2005
2006 data.dwData = COPYDATA_ENCODING;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002007 data.cbData = (DWORD)STRLEN(p_enc) + 1;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002008 data.lpData = p_enc;
K.Takataf9f2a332022-06-17 20:05:40 +01002009 if (SendMessageTimeout(target, WM_COPYDATA,
2010 (WPARAM)message_window, (LPARAM)&data,
2011 SMTO_ABORTIFHUNG, SENDMESSAGE_TIMEOUT, NULL) == 0)
2012 return -1;
2013 return 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002014}
2015
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016/*
2017 * Clean up on exit. This destroys the hidden message window.
2018 */
2019 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020CleanUpMessaging(void)
2021{
2022 if (message_window != 0)
2023 {
2024 DestroyWindow(message_window);
2025 message_window = 0;
2026 }
2027}
2028
2029static int save_reply(HWND server, char_u *reply, int expr);
2030
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002031/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 * The window procedure for the hidden message window.
2033 * It handles callback messages and notifications from servers.
2034 * In order to process these messages, it is necessary to run a
2035 * message loop. Code which may run before the main message loop
2036 * is started (in the GUI) is careful to pump messages when it needs
2037 * to. Features which require message delivery during normal use will
2038 * not work in the console version - this basically means those
2039 * features which allow Vim to act as a server, rather than a client.
2040 */
2041 static LRESULT CALLBACK
2042Messaging_WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
2043{
2044 if (msg == WM_COPYDATA)
2045 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002046 // This is a message from another Vim. The dwData member of the
2047 // COPYDATASTRUCT determines the type of message:
2048 // COPYDATA_ENCODING:
2049 // The encoding that the client uses. Following messages will
2050 // use this encoding, convert if needed.
2051 // COPYDATA_KEYS:
2052 // A key sequence. We are a server, and a client wants these keys
2053 // adding to the input queue.
2054 // COPYDATA_REPLY:
2055 // A reply. We are a client, and a server has sent this message
2056 // in response to a request. (server2client())
2057 // COPYDATA_EXPR:
2058 // An expression. We are a server, and a client wants us to
2059 // evaluate this expression.
2060 // COPYDATA_RESULT:
2061 // A reply. We are a client, and a server has sent this message
2062 // in response to a COPYDATA_EXPR.
2063 // COPYDATA_ERROR_RESULT:
2064 // A reply. We are a client, and a server has sent this message
2065 // in response to a COPYDATA_EXPR that failed to evaluate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 COPYDATASTRUCT *data = (COPYDATASTRUCT*)lParam;
2067 HWND sender = (HWND)wParam;
2068 COPYDATASTRUCT reply;
2069 char_u *res;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 int retval;
K.Takataf9f2a332022-06-17 20:05:40 +01002071 DWORD_PTR dwret = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002072 char_u *str;
2073 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074
2075 switch (data->dwData)
2076 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002077 case COPYDATA_ENCODING:
Bram Moolenaar0f873732019-12-05 20:28:46 +01002078 // Remember the encoding that the client uses.
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002079 vim_free(client_enc);
2080 client_enc = enc_canonize((char_u *)data->lpData);
2081 return 1;
2082
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 case COPYDATA_KEYS:
Bram Moolenaar0f873732019-12-05 20:28:46 +01002084 // Remember who sent this, for <client>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 clientWindow = sender;
2086
Bram Moolenaar0f873732019-12-05 20:28:46 +01002087 // Add the received keys to the input buffer. The loop waiting
2088 // for the user to do something should check the input buffer.
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002089 str = serverConvert(client_enc, (char_u *)data->lpData, &tofree);
2090 server_to_input_buf(str);
2091 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092
2093# ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01002094 // Wake up the main GUI loop.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002095# ifdef VIMDLL
2096 if (gui.in_use)
2097# endif
2098 if (s_hwnd != 0)
2099 PostMessage(s_hwnd, WM_NULL, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100# endif
2101 return 1;
2102
2103 case COPYDATA_EXPR:
Bram Moolenaar0f873732019-12-05 20:28:46 +01002104 // Remember who sent this, for <client>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 clientWindow = sender;
2106
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002107 str = serverConvert(client_enc, (char_u *)data->lpData, &tofree);
2108 res = eval_client_expr_to_string(str);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002109
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 if (res == NULL)
2111 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00002112 char *err = _(e_invalid_expression_received);
Bram Moolenaar15bf76d2017-03-18 16:18:37 +01002113 size_t len = STRLEN(str) + STRLEN(err) + 5;
2114
Bram Moolenaar964b3742019-05-24 18:54:09 +02002115 res = alloc(len);
Bram Moolenaar15bf76d2017-03-18 16:18:37 +01002116 if (res != NULL)
2117 vim_snprintf((char *)res, len, "%s: \"%s\"", err, str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 reply.dwData = COPYDATA_ERROR_RESULT;
2119 }
2120 else
2121 reply.dwData = COPYDATA_RESULT;
2122 reply.lpData = res;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002123 reply.cbData = (DWORD)STRLEN(res) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124
K.Takataf9f2a332022-06-17 20:05:40 +01002125 if (serverSendEnc(sender) < 0)
2126 retval = -1;
2127 else
2128 {
2129 if (SendMessageTimeout(sender, WM_COPYDATA,
2130 (WPARAM)message_window, (LPARAM)&reply,
2131 SMTO_ABORTIFHUNG, SENDMESSAGE_TIMEOUT, &dwret) == 0)
2132 retval = -1;
2133 else
2134 retval = (int)dwret;
2135 }
Bram Moolenaar15bf76d2017-03-18 16:18:37 +01002136 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 vim_free(res);
2138 return retval;
2139
2140 case COPYDATA_REPLY:
2141 case COPYDATA_RESULT:
2142 case COPYDATA_ERROR_RESULT:
2143 if (data->lpData != NULL)
2144 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002145 str = serverConvert(client_enc, (char_u *)data->lpData,
2146 &tofree);
2147 if (tofree == NULL)
2148 str = vim_strsave(str);
2149 if (save_reply(sender, str,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 (data->dwData == COPYDATA_REPLY ? 0 :
2151 (data->dwData == COPYDATA_RESULT ? 1 :
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002152 2))) == FAIL)
2153 vim_free(str);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002154 else if (data->dwData == COPYDATA_REPLY)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 {
Bram Moolenaar427d51c2013-06-16 16:01:25 +02002156 char_u winstr[30];
2157
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002158 sprintf((char *)winstr, PRINTF_HEX_LONG_U, (long_u)sender);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002159 apply_autocmds(EVENT_REMOTEREPLY, winstr, str,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 TRUE, curbuf);
2161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 }
2163 return 1;
2164 }
2165
2166 return 0;
2167 }
2168
2169 else if (msg == WM_ACTIVATE && wParam == WA_ACTIVE)
2170 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002171 // When the message window is activated (brought to the foreground),
2172 // this actually applies to the text window.
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002173# if !defined(FEAT_GUI) || defined(VIMDLL)
2174# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002175 if (!gui.in_use)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002176# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002177 GetConsoleHwnd(); // get value of s_hwnd
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002178# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 if (s_hwnd != 0)
2180 {
2181 SetForegroundWindow(s_hwnd);
2182 return 0;
2183 }
2184 }
2185
2186 return DefWindowProc(hwnd, msg, wParam, lParam);
2187}
2188
2189/*
2190 * Initialise the message handling process. This involves creating a window
2191 * to handle messages - the window will not be visible.
2192 */
2193 void
2194serverInitMessaging(void)
2195{
2196 WNDCLASS wndclass;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197
Bram Moolenaar0f873732019-12-05 20:28:46 +01002198 // Clean up on exit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 atexit(CleanUpMessaging);
2200
Bram Moolenaar0f873732019-12-05 20:28:46 +01002201 // Register a window class - we only really care
2202 // about the window procedure
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 wndclass.style = 0;
2204 wndclass.lpfnWndProc = Messaging_WndProc;
2205 wndclass.cbClsExtra = 0;
2206 wndclass.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002207 wndclass.hInstance = g_hinst;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 wndclass.hIcon = NULL;
2209 wndclass.hCursor = NULL;
2210 wndclass.hbrBackground = NULL;
2211 wndclass.lpszMenuName = NULL;
2212 wndclass.lpszClassName = VIM_CLASSNAME;
2213 RegisterClass(&wndclass);
2214
Bram Moolenaar0f873732019-12-05 20:28:46 +01002215 // Create the message window. It will be hidden, so the details don't
2216 // matter. Don't use WS_OVERLAPPEDWINDOW, it will make a shortcut remove
2217 // focus from gvim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 message_window = CreateWindow(VIM_CLASSNAME, "",
2219 WS_POPUPWINDOW | WS_CAPTION,
2220 CW_USEDEFAULT, CW_USEDEFAULT,
2221 100, 100, NULL, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002222 g_hinst, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223}
2224
Bram Moolenaar0f873732019-12-05 20:28:46 +01002225// Used by serverSendToVim() to find an alternate server name.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002226static char_u *altname_buf_ptr = NULL;
2227
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228/*
2229 * Get the title of the window "hwnd", which is the Vim server name, in
2230 * "name[namelen]" and return the length.
2231 * Returns zero if window "hwnd" is not a Vim server.
2232 */
2233 static int
2234getVimServerName(HWND hwnd, char *name, int namelen)
2235{
2236 int len;
2237 char buffer[VIM_CLASSNAME_LEN + 1];
2238
Bram Moolenaar0f873732019-12-05 20:28:46 +01002239 // Ignore windows which aren't Vim message windows
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 len = GetClassName(hwnd, buffer, sizeof(buffer));
2241 if (len != VIM_CLASSNAME_LEN || STRCMP(buffer, VIM_CLASSNAME) != 0)
2242 return 0;
2243
Bram Moolenaar0f873732019-12-05 20:28:46 +01002244 // Get the title of the window
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 return GetWindowText(hwnd, name, namelen);
2246}
2247
2248 static BOOL CALLBACK
2249enumWindowsGetServer(HWND hwnd, LPARAM lparam)
2250{
2251 struct server_id *id = (struct server_id *)lparam;
2252 char server[MAX_PATH];
2253
Bram Moolenaar0f873732019-12-05 20:28:46 +01002254 // Get the title of the window
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 if (getVimServerName(hwnd, server, sizeof(server)) == 0)
2256 return TRUE;
2257
Bram Moolenaar0f873732019-12-05 20:28:46 +01002258 // If this is the server we're looking for, return its HWND
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 if (STRICMP(server, id->name) == 0)
2260 {
2261 id->hwnd = hwnd;
2262 return FALSE;
2263 }
2264
Bram Moolenaar0f873732019-12-05 20:28:46 +01002265 // If we are looking for an alternate server, remember this name.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002266 if (altname_buf_ptr != NULL
2267 && STRNICMP(server, id->name, STRLEN(id->name)) == 0
2268 && vim_isdigit(server[STRLEN(id->name)]))
2269 {
2270 STRCPY(altname_buf_ptr, server);
Bram Moolenaar0f873732019-12-05 20:28:46 +01002271 altname_buf_ptr = NULL; // don't use another name
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002272 }
2273
Bram Moolenaar0f873732019-12-05 20:28:46 +01002274 // Otherwise, keep looking
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 return TRUE;
2276}
2277
2278 static BOOL CALLBACK
2279enumWindowsGetNames(HWND hwnd, LPARAM lparam)
2280{
2281 garray_T *ga = (garray_T *)lparam;
2282 char server[MAX_PATH];
2283
Bram Moolenaar0f873732019-12-05 20:28:46 +01002284 // Get the title of the window
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 if (getVimServerName(hwnd, server, sizeof(server)) == 0)
2286 return TRUE;
2287
Bram Moolenaar0f873732019-12-05 20:28:46 +01002288 // Add the name to the list
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002289 ga_concat(ga, (char_u *)server);
2290 ga_concat(ga, (char_u *)"\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 return TRUE;
2292}
2293
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002294struct enum_windows_s
2295{
2296 WNDENUMPROC lpEnumFunc;
2297 LPARAM lParam;
2298};
2299
2300 static BOOL CALLBACK
2301enum_windows_child(HWND hwnd, LPARAM lParam)
2302{
2303 struct enum_windows_s *ew = (struct enum_windows_s *)lParam;
2304
2305 return (ew->lpEnumFunc)(hwnd, ew->lParam);
2306}
2307
2308 static BOOL CALLBACK
2309enum_windows_toplevel(HWND hwnd, LPARAM lParam)
2310{
2311 struct enum_windows_s *ew = (struct enum_windows_s *)lParam;
2312
Bram Moolenaar95ba5c32018-10-07 22:47:07 +02002313 if ((ew->lpEnumFunc)(hwnd, ew->lParam))
2314 return TRUE;
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002315 return EnumChildWindows(hwnd, enum_windows_child, lParam);
2316}
2317
Bram Moolenaar0f873732019-12-05 20:28:46 +01002318/*
2319 * Enumerate all windows including children.
2320 */
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002321 static BOOL
2322enum_windows(WNDENUMPROC lpEnumFunc, LPARAM lParam)
2323{
2324 struct enum_windows_s ew;
2325
2326 ew.lpEnumFunc = lpEnumFunc;
2327 ew.lParam = lParam;
2328 return EnumWindows(enum_windows_toplevel, (LPARAM)&ew);
2329}
2330
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 static HWND
2332findServer(char_u *name)
2333{
2334 struct server_id id;
2335
2336 id.name = name;
2337 id.hwnd = 0;
2338
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002339 enum_windows(enumWindowsGetServer, (LPARAM)(&id));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340
2341 return id.hwnd;
2342}
2343
2344 void
2345serverSetName(char_u *name)
2346{
2347 char_u *ok_name;
2348 HWND hwnd = 0;
2349 int i = 0;
2350 char_u *p;
2351
Bram Moolenaar0f873732019-12-05 20:28:46 +01002352 // Leave enough space for a 9-digit suffix to ensure uniqueness!
Bram Moolenaar964b3742019-05-24 18:54:09 +02002353 ok_name = alloc(STRLEN(name) + 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354
2355 STRCPY(ok_name, name);
2356 p = ok_name + STRLEN(name);
2357
2358 for (;;)
2359 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002360 // This is inefficient - we're doing an EnumWindows loop for each
2361 // possible name. It would be better to grab all names in one go,
2362 // and scan the list each time...
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 hwnd = findServer(ok_name);
2364 if (hwnd == 0)
2365 break;
2366
2367 ++i;
2368 if (i >= 1000)
2369 break;
2370
2371 sprintf((char *)p, "%d", i);
2372 }
2373
2374 if (hwnd != 0)
2375 vim_free(ok_name);
2376 else
2377 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002378 // Remember the name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 serverName = ok_name;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002380 need_maketitle = TRUE; // update Vim window title later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381
Bram Moolenaar0f873732019-12-05 20:28:46 +01002382 // Update the message window title
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002383 SetWindowText(message_window, (LPCSTR)ok_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002385# ifdef FEAT_EVAL
Bram Moolenaar0f873732019-12-05 20:28:46 +01002386 // Set the servername variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 set_vim_var_string(VV_SEND_SERVER, serverName, -1);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002388# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 }
2390}
2391
2392 char_u *
2393serverGetVimNames(void)
2394{
2395 garray_T ga;
2396
2397 ga_init2(&ga, 1, 100);
2398
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002399 enum_windows(enumWindowsGetNames, (LPARAM)(&ga));
Bram Moolenaar269ec652004-07-29 08:43:53 +00002400 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401
2402 return ga.ga_data;
2403}
2404
2405 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002406serverSendReply(
Bram Moolenaar0f873732019-12-05 20:28:46 +01002407 char_u *name, // Where to send.
2408 char_u *reply) // What to send.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409{
2410 HWND target;
2411 COPYDATASTRUCT data;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002412 long_u n = 0;
K.Takataf9f2a332022-06-17 20:05:40 +01002413 DWORD_PTR dwret = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414
Bram Moolenaar0f873732019-12-05 20:28:46 +01002415 // The "name" argument is a magic cookie obtained from expand("<client>").
2416 // It should be of the form 0xXXXXX - i.e. a C hex literal, which is the
2417 // value of the client's message window HWND.
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002418 sscanf((char *)name, SCANF_HEX_LONG_U, &n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 if (n == 0)
2420 return -1;
2421
2422 target = (HWND)n;
2423 if (!IsWindow(target))
2424 return -1;
2425
2426 data.dwData = COPYDATA_REPLY;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002427 data.cbData = (DWORD)STRLEN(reply) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 data.lpData = reply;
2429
K.Takataf9f2a332022-06-17 20:05:40 +01002430 if (serverSendEnc(target) < 0)
2431 return -1;
2432 if (SendMessageTimeout(target, WM_COPYDATA,
2433 (WPARAM)message_window, (LPARAM)&data,
2434 SMTO_ABORTIFHUNG, SENDMESSAGE_TIMEOUT, &dwret) == 0)
2435 return -1;
2436 return dwret ? 0 : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437}
2438
2439 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002440serverSendToVim(
Bram Moolenaar0f873732019-12-05 20:28:46 +01002441 char_u *name, // Where to send.
2442 char_u *cmd, // What to send.
2443 char_u **result, // Result of eval'ed expression
2444 void *ptarget, // HWND of server
2445 int asExpr, // Expression or keys?
2446 int timeout, // timeout in seconds or zero
2447 int silent) // don't complain about no server
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448{
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002449 HWND target;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 COPYDATASTRUCT data;
2451 char_u *retval = NULL;
2452 int retcode = 0;
K.Takataf9f2a332022-06-17 20:05:40 +01002453 DWORD_PTR dwret = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002454 char_u altname_buf[MAX_PATH];
2455
Bram Moolenaar0f873732019-12-05 20:28:46 +01002456 // Execute locally if no display or target is ourselves
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01002457 if (serverName != NULL && STRICMP(name, serverName) == 0)
2458 return sendToLocalVim(cmd, asExpr, result);
2459
Bram Moolenaar0f873732019-12-05 20:28:46 +01002460 // If the server name does not end in a digit then we look for an
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00002461 // alternate name. e.g. when "name" is GVIM then we may find GVIM2.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002462 if (STRLEN(name) > 1 && !vim_isdigit(name[STRLEN(name) - 1]))
2463 altname_buf_ptr = altname_buf;
2464 altname_buf[0] = NUL;
2465 target = findServer(name);
2466 altname_buf_ptr = NULL;
2467 if (target == 0 && altname_buf[0] != NUL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01002468 // Use another server name we found.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002469 target = findServer(altname_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470
2471 if (target == 0)
2472 {
2473 if (!silent)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002474 semsg(_(e_no_registered_server_named_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 return -1;
2476 }
2477
2478 if (ptarget)
2479 *(HWND *)ptarget = target;
2480
2481 data.dwData = asExpr ? COPYDATA_EXPR : COPYDATA_KEYS;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002482 data.cbData = (DWORD)STRLEN(cmd) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 data.lpData = cmd;
2484
K.Takataf9f2a332022-06-17 20:05:40 +01002485 if (serverSendEnc(target) < 0)
2486 return -1;
2487 if (SendMessageTimeout(target, WM_COPYDATA,
2488 (WPARAM)message_window, (LPARAM)&data,
2489 SMTO_ABORTIFHUNG, SENDMESSAGE_TIMEOUT, &dwret) == 0)
2490 return -1;
2491 if (dwret == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 return -1;
2493
2494 if (asExpr)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002495 retval = serverGetReply(target, &retcode, TRUE, TRUE, timeout);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496
2497 if (result == NULL)
2498 vim_free(retval);
2499 else
Bram Moolenaar0f873732019-12-05 20:28:46 +01002500 *result = retval; // Caller assumes responsibility for freeing
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501
2502 return retcode;
2503}
2504
2505/*
2506 * Bring the server to the foreground.
2507 */
2508 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002509serverForeground(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510{
2511 HWND target = findServer(name);
2512
2513 if (target != 0)
2514 SetForegroundWindow(target);
2515}
2516
Bram Moolenaar0f873732019-12-05 20:28:46 +01002517// Replies from server need to be stored until the client picks them up via
2518// remote_read(). So we maintain a list of server-id/reply pairs.
2519// Note that there could be multiple replies from one server pending if the
2520// client is slow picking them up.
2521// We just store the replies in a simple list. When we remove an entry, we
2522// move list entries down to fill the gap.
2523// The server ID is simply the HWND.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524typedef struct
2525{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002526 HWND server; // server window
2527 char_u *reply; // reply string
2528 int expr_result; // 0 for REPLY, 1 for RESULT 2 for error
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002529} reply_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530
2531static garray_T reply_list = {0, 0, sizeof(reply_T), 5, 0};
2532
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002533# define REPLY_ITEM(i) ((reply_T *)(reply_list.ga_data) + (i))
2534# define REPLY_COUNT (reply_list.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535
Bram Moolenaar0f873732019-12-05 20:28:46 +01002536// Flag which is used to wait for a reply
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537static int reply_received = 0;
2538
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002539/*
2540 * Store a reply. "reply" must be allocated memory (or NULL).
2541 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 static int
2543save_reply(HWND server, char_u *reply, int expr)
2544{
2545 reply_T *rep;
2546
2547 if (ga_grow(&reply_list, 1) == FAIL)
2548 return FAIL;
2549
2550 rep = REPLY_ITEM(REPLY_COUNT);
2551 rep->server = server;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002552 rep->reply = reply;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 rep->expr_result = expr;
2554 if (rep->reply == NULL)
2555 return FAIL;
2556
2557 ++REPLY_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 reply_received = 1;
2559 return OK;
2560}
2561
2562/*
2563 * Get a reply from server "server".
2564 * When "expr_res" is non NULL, get the result of an expression, otherwise a
2565 * server2client() message.
2566 * When non NULL, point to return code. 0 => OK, -1 => ERROR
2567 * If "remove" is TRUE, consume the message, the caller must free it then.
2568 * if "wait" is TRUE block until a message arrives (or the server exits).
2569 */
2570 char_u *
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002571serverGetReply(HWND server, int *expr_res, int remove, int wait, int timeout)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572{
2573 int i;
2574 char_u *reply;
2575 reply_T *rep;
Bram Moolenaar15e737f2017-03-18 21:22:47 +01002576 int did_process = FALSE;
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002577 time_t start;
2578 time_t now;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579
Bram Moolenaar0f873732019-12-05 20:28:46 +01002580 // When waiting, loop until the message waiting for is received.
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002581 time(&start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 for (;;)
2583 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002584 // Reset this here, in case a message arrives while we are going
2585 // through the already received messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 reply_received = 0;
2587
2588 for (i = 0; i < REPLY_COUNT; ++i)
2589 {
2590 rep = REPLY_ITEM(i);
2591 if (rep->server == server
2592 && ((rep->expr_result != 0) == (expr_res != NULL)))
2593 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002594 // Save the values we've found for later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 reply = rep->reply;
2596 if (expr_res != NULL)
2597 *expr_res = rep->expr_result == 1 ? 0 : -1;
2598
2599 if (remove)
2600 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002601 // Move the rest of the list down to fill the gap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 mch_memmove(rep, rep + 1,
2603 (REPLY_COUNT - i - 1) * sizeof(reply_T));
2604 --REPLY_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 }
2606
Bram Moolenaar0f873732019-12-05 20:28:46 +01002607 // Return the reply to the caller, who takes on responsibility
2608 // for freeing it if "remove" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 return reply;
2610 }
2611 }
2612
Bram Moolenaar0f873732019-12-05 20:28:46 +01002613 // If we got here, we didn't find a reply. Return immediately if the
2614 // "wait" parameter isn't set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 if (!wait)
Bram Moolenaar15e737f2017-03-18 21:22:47 +01002616 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002617 // Process pending messages once. Without this, looping on
2618 // remote_peek() would never get the reply.
Bram Moolenaar15e737f2017-03-18 21:22:47 +01002619 if (!did_process)
2620 {
2621 did_process = TRUE;
2622 serverProcessPendingMessages();
2623 continue;
2624 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 break;
Bram Moolenaar15e737f2017-03-18 21:22:47 +01002626 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627
Bram Moolenaar0f873732019-12-05 20:28:46 +01002628 // We need to wait for a reply. Enter a message loop until the
2629 // "reply_received" flag gets set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630
Bram Moolenaar0f873732019-12-05 20:28:46 +01002631 // Loop until we receive a reply
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 while (reply_received == 0)
2633 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002634# ifdef FEAT_TIMERS
Bram Moolenaar0f873732019-12-05 20:28:46 +01002635 // TODO: use the return value to decide how long to wait.
Bram Moolenaar42205552017-03-18 19:42:22 +01002636 check_due_timer();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002637# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002638 time(&now);
2639 if (timeout > 0 && (now - start) >= timeout)
2640 break;
2641
Bram Moolenaar0f873732019-12-05 20:28:46 +01002642 // Wait for a SendMessage() call to us. This could be the reply
2643 // we are waiting for. Use a timeout of a second, to catch the
2644 // situation that the server died unexpectedly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 MsgWaitForMultipleObjects(0, NULL, TRUE, 1000, QS_ALLINPUT);
2646
Bram Moolenaar0f873732019-12-05 20:28:46 +01002647 // If the server has died, give up
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 if (!IsWindow(server))
2649 return NULL;
2650
2651 serverProcessPendingMessages();
2652 }
2653 }
2654
2655 return NULL;
2656}
2657
2658/*
2659 * Process any messages in the Windows message queue.
2660 */
2661 void
2662serverProcessPendingMessages(void)
2663{
2664 MSG msg;
2665
K.Takatab7057bd2022-01-21 11:37:07 +00002666 while (PeekMessageW(&msg, NULL, 0, 0, PM_REMOVE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 {
2668 TranslateMessage(&msg);
K.Takatab7057bd2022-01-21 11:37:07 +00002669 DispatchMessageW(&msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 }
2671}
2672
Bram Moolenaar0f873732019-12-05 20:28:46 +01002673#endif // FEAT_CLIENTSERVER
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674
2675#if defined(FEAT_GUI) || (defined(FEAT_PRINTER) && !defined(FEAT_POSTSCRIPT)) \
2676 || defined(PROTO)
2677
2678struct charset_pair
2679{
2680 char *name;
2681 BYTE charset;
2682};
2683
2684static struct charset_pair
2685charset_pairs[] =
2686{
2687 {"ANSI", ANSI_CHARSET},
2688 {"CHINESEBIG5", CHINESEBIG5_CHARSET},
2689 {"DEFAULT", DEFAULT_CHARSET},
2690 {"HANGEUL", HANGEUL_CHARSET},
2691 {"OEM", OEM_CHARSET},
2692 {"SHIFTJIS", SHIFTJIS_CHARSET},
2693 {"SYMBOL", SYMBOL_CHARSET},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 {"ARABIC", ARABIC_CHARSET},
2695 {"BALTIC", BALTIC_CHARSET},
2696 {"EASTEUROPE", EASTEUROPE_CHARSET},
2697 {"GB2312", GB2312_CHARSET},
2698 {"GREEK", GREEK_CHARSET},
2699 {"HEBREW", HEBREW_CHARSET},
2700 {"JOHAB", JOHAB_CHARSET},
2701 {"MAC", MAC_CHARSET},
2702 {"RUSSIAN", RUSSIAN_CHARSET},
2703 {"THAI", THAI_CHARSET},
2704 {"TURKISH", TURKISH_CHARSET},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002705# ifdef VIETNAMESE_CHARSET
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 {"VIETNAMESE", VIETNAMESE_CHARSET},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002707# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 {NULL, 0}
2709};
2710
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002711struct quality_pair
2712{
2713 char *name;
2714 DWORD quality;
2715};
2716
2717static struct quality_pair
2718quality_pairs[] = {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002719# ifdef CLEARTYPE_QUALITY
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002720 {"CLEARTYPE", CLEARTYPE_QUALITY},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002721# endif
2722# ifdef ANTIALIASED_QUALITY
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002723 {"ANTIALIASED", ANTIALIASED_QUALITY},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002724# endif
2725# ifdef NONANTIALIASED_QUALITY
Bram Moolenaar73a733e2016-05-11 21:05:05 +02002726 {"NONANTIALIASED", NONANTIALIASED_QUALITY},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002727# endif
2728# ifdef PROOF_QUALITY
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002729 {"PROOF", PROOF_QUALITY},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002730# endif
2731# ifdef DRAFT_QUALITY
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002732 {"DRAFT", DRAFT_QUALITY},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002733# endif
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002734 {"DEFAULT", DEFAULT_QUALITY},
2735 {NULL, 0}
2736};
2737
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738/*
2739 * Convert a charset ID to a name.
2740 * Return NULL when not recognized.
2741 */
2742 char *
2743charset_id2name(int id)
2744{
2745 struct charset_pair *cp;
2746
2747 for (cp = charset_pairs; cp->name != NULL; ++cp)
2748 if ((BYTE)id == cp->charset)
2749 break;
2750 return cp->name;
2751}
2752
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002753/*
2754 * Convert a quality ID to a name.
2755 * Return NULL when not recognized.
2756 */
2757 char *
2758quality_id2name(DWORD id)
2759{
2760 struct quality_pair *qp;
2761
2762 for (qp = quality_pairs; qp->name != NULL; ++qp)
2763 if (id == qp->quality)
2764 break;
2765 return qp->name;
2766}
2767
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002768static const LOGFONTW s_lfDefault =
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769{
2770 -12, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, DEFAULT_CHARSET,
2771 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
2772 PROOF_QUALITY, FIXED_PITCH | FF_DONTCARE,
Bram Moolenaar0f873732019-12-05 20:28:46 +01002773 L"Fixedsys" // see _ReadVimIni
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774};
2775
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002776// Initialise the "current height" to -12 (same as s_lfDefault) just
2777// in case the user specifies a font in "guifont" with no size before a font
2778// with an explicit size has been set. This defaults the size to this value
2779// (-12 equates to roughly 9pt).
2780int current_font_height = -12; // also used in gui_w32.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781
Bram Moolenaar0f873732019-12-05 20:28:46 +01002782/*
2783 * Convert a string representing a point size into pixels. The string should
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 * be a positive decimal number, with an optional decimal point (eg, "12", or
2785 * "10.5"). The pixel value is returned, and a pointer to the next unconverted
2786 * character is stored in *end. The flag "vertical" says whether this
2787 * calculation is for a vertical (height) size or a horizontal (width) one.
2788 */
2789 static int
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002790points_to_pixels(WCHAR *str, WCHAR **end, int vertical, long_i pprinter_dc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791{
2792 int pixels;
2793 int points = 0;
2794 int divisor = 0;
2795 HWND hwnd = (HWND)0;
2796 HDC hdc;
2797 HDC printer_dc = (HDC)pprinter_dc;
2798
2799 while (*str != NUL)
2800 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002801 if (*str == L'.' && divisor == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002803 // Start keeping a divisor, for later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 divisor = 1;
2805 }
2806 else
2807 {
2808 if (!VIM_ISDIGIT(*str))
2809 break;
2810
2811 points *= 10;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002812 points += *str - L'0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 divisor *= 10;
2814 }
2815 ++str;
2816 }
2817
2818 if (divisor == 0)
2819 divisor = 1;
2820
2821 if (printer_dc == NULL)
2822 {
2823 hwnd = GetDesktopWindow();
2824 hdc = GetWindowDC(hwnd);
2825 }
2826 else
2827 hdc = printer_dc;
2828
2829 pixels = MulDiv(points,
2830 GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX),
2831 72 * divisor);
2832
2833 if (printer_dc == NULL)
2834 ReleaseDC(hwnd, hdc);
2835
2836 *end = str;
2837 return pixels;
2838}
2839
2840 static int CALLBACK
2841font_enumproc(
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002842 ENUMLOGFONTW *elf,
2843 NEWTEXTMETRICW *ntm UNUSED,
2844 DWORD type UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 LPARAM lparam)
2846{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002847 // Return value:
2848 // 0 = terminate now (monospace & ANSI)
2849 // 1 = continue, still no luck...
2850 // 2 = continue, but we have an acceptable LOGFONTW
2851 // (monospace, not ANSI)
2852 // We use these values, as EnumFontFamilies returns 1 if the
2853 // callback function is never called. So, we check the return as
2854 // 0 = perfect, 2 = OK, 1 = no good...
2855 // It's not pretty, but it works!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002857 LOGFONTW *lf = (LOGFONTW *)(lparam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002859# ifndef FEAT_PROPORTIONAL_FONTS
Bram Moolenaar0f873732019-12-05 20:28:46 +01002860 // Ignore non-monospace fonts without further ado
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 if ((ntm->tmPitchAndFamily & 1) != 0)
2862 return 1;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002863# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864
Bram Moolenaar0f873732019-12-05 20:28:46 +01002865 // Remember this LOGFONTW as a "possible"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 *lf = elf->elfLogFont;
2867
Bram Moolenaar0f873732019-12-05 20:28:46 +01002868 // Terminate the scan as soon as we find an ANSI font
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 if (lf->lfCharSet == ANSI_CHARSET
2870 || lf->lfCharSet == OEM_CHARSET
2871 || lf->lfCharSet == DEFAULT_CHARSET)
2872 return 0;
2873
Bram Moolenaar0f873732019-12-05 20:28:46 +01002874 // Continue the scan - we have a non-ANSI font
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 return 2;
2876}
2877
2878 static int
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002879init_logfont(LOGFONTW *lf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880{
2881 int n;
2882 HWND hwnd = GetDesktopWindow();
2883 HDC hdc = GetWindowDC(hwnd);
2884
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002885 n = EnumFontFamiliesW(hdc,
2886 lf->lfFaceName,
2887 (FONTENUMPROCW)font_enumproc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 (LPARAM)lf);
2889
2890 ReleaseDC(hwnd, hdc);
2891
Bram Moolenaar0f873732019-12-05 20:28:46 +01002892 // If we couldn't find a usable font, return failure
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 if (n == 1)
2894 return FAIL;
2895
Bram Moolenaar0f873732019-12-05 20:28:46 +01002896 // Tidy up the rest of the LOGFONTW structure. We set to a basic
2897 // font - get_logfont() sets bold, italic, etc based on the user's
2898 // input.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 lf->lfHeight = current_font_height;
2900 lf->lfWidth = 0;
2901 lf->lfItalic = FALSE;
2902 lf->lfUnderline = FALSE;
2903 lf->lfStrikeOut = FALSE;
2904 lf->lfWeight = FW_NORMAL;
2905
Bram Moolenaar0f873732019-12-05 20:28:46 +01002906 // Return success
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 return OK;
2908}
2909
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002910/*
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002911 * Compare a UTF-16 string and an ASCII string literally.
2912 * Only works all the code points are inside ASCII range.
2913 */
2914 static int
2915utf16ascncmp(const WCHAR *w, const char *p, size_t n)
2916{
2917 size_t i;
2918
2919 for (i = 0; i < n; i++)
2920 {
2921 if (w[i] == 0 || w[i] != p[i])
2922 return w[i] - p[i];
2923 }
2924 return 0;
2925}
2926
2927/*
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002928 * Get font info from "name" into logfont "lf".
2929 * Return OK for a valid name, FAIL otherwise.
2930 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 int
2932get_logfont(
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002933 LOGFONTW *lf,
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002934 char_u *name,
2935 HDC printer_dc,
2936 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002938 WCHAR *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 int i;
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002940 int ret = FAIL;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002941 static LOGFONTW *lastlf = NULL;
2942 WCHAR *wname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943
2944 *lf = s_lfDefault;
2945 if (name == NULL)
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002946 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002948 wname = enc_to_utf16(name, NULL);
2949 if (wname == NULL)
2950 return FAIL;
2951
2952 if (wcscmp(wname, L"*") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002954# if defined(FEAT_GUI_MSWIN)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002955 CHOOSEFONTW cf;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002956 // if name is "*", bring up std font dialog:
Bram Moolenaara80faa82020-04-12 19:37:17 +02002957 CLEAR_FIELD(cf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958 cf.lStructSize = sizeof(cf);
2959 cf.hwndOwner = s_hwnd;
2960 cf.Flags = CF_SCREENFONTS | CF_FIXEDPITCHONLY | CF_INITTOLOGFONTSTRUCT;
2961 if (lastlf != NULL)
2962 *lf = *lastlf;
2963 cf.lpLogFont = lf;
2964 cf.nFontType = 0 ; //REGULAR_FONTTYPE;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002965 if (ChooseFontW(&cf))
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002966 ret = OK;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002967# endif
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002968 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969 }
2970
2971 /*
2972 * Split name up, it could be <name>:h<height>:w<width> etc.
2973 */
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002974 for (p = wname; *p && *p != L':'; p++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002976 if (p - wname + 1 >= LF_FACESIZE)
Bram Moolenaar0f873732019-12-05 20:28:46 +01002977 goto theend; // Name too long
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002978 lf->lfFaceName[p - wname] = *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 }
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002980 if (p != wname)
2981 lf->lfFaceName[p - wname] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982
Bram Moolenaar0f873732019-12-05 20:28:46 +01002983 // First set defaults
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984 lf->lfHeight = -12;
2985 lf->lfWidth = 0;
2986 lf->lfWeight = FW_NORMAL;
2987 lf->lfItalic = FALSE;
2988 lf->lfUnderline = FALSE;
2989 lf->lfStrikeOut = FALSE;
2990
2991 /*
2992 * If the font can't be found, try replacing '_' by ' '.
2993 */
2994 if (init_logfont(lf) == FAIL)
2995 {
2996 int did_replace = FALSE;
2997
2998 for (i = 0; lf->lfFaceName[i]; ++i)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002999 if (lf->lfFaceName[i] == L'_')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003001 lf->lfFaceName[i] = L' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002 did_replace = TRUE;
3003 }
3004 if (!did_replace || init_logfont(lf) == FAIL)
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003005 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006 }
3007
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003008 while (*p == L':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009 p++;
3010
Bram Moolenaar0f873732019-12-05 20:28:46 +01003011 // Set the values found after ':'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 while (*p)
3013 {
3014 switch (*p++)
3015 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003016 case L'h':
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003017 lf->lfHeight = - points_to_pixels(p, &p, TRUE, (long_i)printer_dc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003019 case L'w':
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003020 lf->lfWidth = points_to_pixels(p, &p, FALSE, (long_i)printer_dc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 break;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02003022 case L'W':
3023 lf->lfWeight = wcstol(p, &p, 10);
3024 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003025 case L'b':
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 lf->lfWeight = FW_BOLD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003028 case L'i':
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029 lf->lfItalic = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003031 case L'u':
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 lf->lfUnderline = TRUE;
3033 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003034 case L's':
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 lf->lfStrikeOut = TRUE;
3036 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003037 case L'c':
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 {
3039 struct charset_pair *cp;
3040
3041 for (cp = charset_pairs; cp->name != NULL; ++cp)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003042 if (utf16ascncmp(p, cp->name, strlen(cp->name)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043 {
3044 lf->lfCharSet = cp->charset;
3045 p += strlen(cp->name);
3046 break;
3047 }
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003048 if (cp->name == NULL && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003050 char_u *s = utf16_to_enc(p, NULL);
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00003051
3052 semsg(_(e_illegal_str_name_str_in_font_name_str),
3053 "charset", s, name);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003054 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 break;
3056 }
3057 break;
3058 }
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003059 case L'q':
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003060 {
3061 struct quality_pair *qp;
3062
3063 for (qp = quality_pairs; qp->name != NULL; ++qp)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003064 if (utf16ascncmp(p, qp->name, strlen(qp->name)) == 0)
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003065 {
3066 lf->lfQuality = qp->quality;
3067 p += strlen(qp->name);
3068 break;
3069 }
3070 if (qp->name == NULL && verbose)
3071 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003072 char_u *s = utf16_to_enc(p, NULL);
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00003073 semsg(_(e_illegal_str_name_str_in_font_name_str),
3074 "quality", s, name);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003075 vim_free(s);
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003076 break;
3077 }
3078 break;
3079 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080 default:
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003081 if (verbose)
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00003082 semsg(_(e_illegal_char_nr_in_font_name_str), p[-1], name);
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003083 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 }
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003085 while (*p == L':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 p++;
3087 }
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003088 ret = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090theend:
Bram Moolenaar0f873732019-12-05 20:28:46 +01003091 // ron: init lastlf
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003092 if (ret == OK && printer_dc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 {
3094 vim_free(lastlf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003095 lastlf = ALLOC_ONE(LOGFONTW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 if (lastlf != NULL)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003097 mch_memmove(lastlf, lf, sizeof(LOGFONTW));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 }
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003099 vim_free(wname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003101 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102}
3103
Bram Moolenaar0f873732019-12-05 20:28:46 +01003104#endif // defined(FEAT_GUI) || defined(FEAT_PRINTER)
Bram Moolenaarf12d9832016-01-29 21:11:25 +01003105
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003106#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaarf12d9832016-01-29 21:11:25 +01003107/*
3108 * Initialize the Winsock dll.
3109 */
3110 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003111channel_init_winsock(void)
Bram Moolenaarf12d9832016-01-29 21:11:25 +01003112{
3113 WSADATA wsaData;
3114 int wsaerr;
3115
3116 if (WSInitialized)
3117 return;
3118
3119 wsaerr = WSAStartup(MAKEWORD(2, 2), &wsaData);
3120 if (wsaerr == 0)
3121 WSInitialized = TRUE;
3122}
3123#endif