blob: fb9a3beb3c856e937d84e3a209ca86679ca8d32f [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{
518 DWORD attr = 0;
519 int idx;
520 WCHAR *p, *end, *wname;
521 // The buffer size is arbitrarily chosen to be "big enough" (TM), the
522 // ceiling should be around 16k.
523 char_u buf[4096];
524 DWORD buf_len = sizeof(buf);
525 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 {
563 if ((*p++ == L'\0'))
564 ++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 Moolenaar071d4272004-06-13 20:20:40 +0000667 char *p;
668
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 Moolenaarafde13b2019-04-28 19:46:49 +0200676 for (p = (char *)error_ga.ga_data; *p; ++p)
677 if (!isspace(*p))
678 {
679 (void)gui_mch_dialog(
Bram Moolenaarc17ef8e2006-03-25 21:48:58 +0000680 gui.starting ? VIM_INFO :
Bram Moolenaarc17ef8e2006-03-25 21:48:58 +0000681 VIM_ERROR,
Bram Moolenaarc17ef8e2006-03-25 21:48:58 +0000682 gui.starting ? (char_u *)_("Message") :
Bram Moolenaarc17ef8e2006-03-25 21:48:58 +0000683 (char_u *)_("Error"),
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100684 (char_u *)p, (char_u *)_("&Ok"),
685 1, NULL, FALSE);
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200686 break;
687 }
688 ga_clear(&error_ga);
689 }
690 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200692# endif
693# if !defined(FEAT_GUI) || defined(VIMDLL)
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +0100694 FlushFileBuffers(GetStdHandle(STD_ERROR_HANDLE));
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200695# endif
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +0100696}
697#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698
699
700/*
701 * Return TRUE if "p" contain a wildcard that can be expanded by
702 * dos_expandpath().
703 */
704 int
705mch_has_exp_wildcard(char_u *p)
706{
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100707 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 {
709 if (vim_strchr((char_u *)"?*[", *p) != NULL
710 || (*p == '~' && p[1] != NUL))
711 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 }
713 return FALSE;
714}
715
716/*
717 * Return TRUE if "p" contain a wildcard or a "~1" kind of thing (could be a
718 * shortened file name).
719 */
720 int
721mch_has_wildcard(char_u *p)
722{
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100723 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 {
725 if (vim_strchr((char_u *)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100726#ifdef VIM_BACKTICK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727 "?*$[`"
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100728#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 "?*$["
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100730#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 , *p) != NULL
732 || (*p == '~' && p[1] != NUL))
733 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 }
735 return FALSE;
736}
737
738
739/*
740 * The normal _chdir() does not change the default drive. This one does.
741 * Returning 0 implies success; -1 implies failure.
742 */
743 int
744mch_chdir(char *path)
745{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200746 WCHAR *p;
747 int n;
748
Bram Moolenaar0f873732019-12-05 20:28:46 +0100749 if (path[0] == NUL) // just checking...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 return -1;
751
Bram Moolenaara2974d72009-07-14 16:38:36 +0000752 if (p_verbose >= 5)
753 {
754 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100755 smsg("chdir(%s)", path);
Bram Moolenaara2974d72009-07-14 16:38:36 +0000756 verbose_leave();
757 }
Bram Moolenaar0f873732019-12-05 20:28:46 +0100758 if (isalpha(path[0]) && path[1] == ':') // has a drive name
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100760 // If we can change to the drive, skip that part of the path. If we
761 // can't then the current directory may be invalid, try using chdir()
762 // with the whole path.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763 if (_chdrive(TOLOWER_ASC(path[0]) - 'a' + 1) == 0)
764 path += 2;
765 }
766
Bram Moolenaar0f873732019-12-05 20:28:46 +0100767 if (*path == NUL) // drive name only
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 return 0;
769
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200770 p = enc_to_utf16((char_u *)path, NULL);
771 if (p == NULL)
772 return -1;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000773
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200774 n = _wchdir(p);
775 vim_free(p);
776 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777}
778
779
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200780#if defined(FEAT_GUI_MSWIN) && !defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781/*
782 * return non-zero if a character is available
783 */
784 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100785mch_char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100787 // never used
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788 return TRUE;
789}
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200790
791# if defined(FEAT_TERMINAL) || defined(PROTO)
792/*
793 * Check for any pending input or messages.
794 */
795 int
796mch_check_messages(void)
797{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100798 // TODO: check for messages
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200799 return TRUE;
800}
801# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802#endif
803
804
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805#if defined(FEAT_LIBCALL) || defined(PROTO)
806/*
807 * Call a DLL routine which takes either a string or int param
808 * and returns an allocated string.
809 * Return OK if it worked, FAIL if not.
810 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811typedef LPTSTR (*MYSTRPROCSTR)(LPTSTR);
812typedef LPTSTR (*MYINTPROCSTR)(int);
813typedef int (*MYSTRPROCINT)(LPTSTR);
814typedef int (*MYINTPROCINT)(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816/*
817 * Check if a pointer points to a valid NUL terminated string.
818 * Return the length of the string, including terminating NUL.
819 * Returns 0 for an invalid pointer, 1 for an empty string.
820 */
821 static size_t
822check_str_len(char_u *str)
823{
824 SYSTEM_INFO si;
825 MEMORY_BASIC_INFORMATION mbi;
826 size_t length = 0;
827 size_t i;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100828 const char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829
Bram Moolenaar0f873732019-12-05 20:28:46 +0100830 // get page size
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 GetSystemInfo(&si);
832
Bram Moolenaar0f873732019-12-05 20:28:46 +0100833 // get memory information
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 if (VirtualQuery(str, &mbi, sizeof(mbi)))
835 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100836 // pre cast these (typing savers)
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000837 long_u dwStr = (long_u)str;
838 long_u dwBaseAddress = (long_u)mbi.BaseAddress;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839
Bram Moolenaar0f873732019-12-05 20:28:46 +0100840 // get start address of page that str is on
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000841 long_u strPage = dwStr - (dwStr - dwBaseAddress) % si.dwPageSize;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842
Bram Moolenaar0f873732019-12-05 20:28:46 +0100843 // get length from str to end of page
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000844 long_u pageLength = si.dwPageSize - (dwStr - strPage);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845
Bram Moolenaar442b4222010-05-24 21:34:22 +0200846 for (p = str; !IsBadReadPtr(p, (UINT)pageLength);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 p += pageLength, pageLength = si.dwPageSize)
848 for (i = 0; i < pageLength; ++i, ++length)
849 if (p[i] == NUL)
850 return length + 1;
851 }
852
853 return 0;
854}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200856/*
857 * Passed to do_in_runtimepath() to load a vim.ico file.
858 */
859 static void
860mch_icon_load_cb(char_u *fname, void *cookie)
861{
862 HANDLE *h = (HANDLE *)cookie;
863
864 *h = LoadImage(NULL,
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100865 (LPSTR)fname,
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200866 IMAGE_ICON,
867 64,
868 64,
869 LR_LOADFROMFILE | LR_LOADMAP3DCOLORS);
870}
871
872/*
873 * Try loading an icon file from 'runtimepath'.
874 */
875 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100876mch_icon_load(HANDLE *iconp)
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200877{
878 return do_in_runtimepath((char_u *)"bitmaps/vim.ico",
Bram Moolenaar7f8989d2016-03-12 22:11:39 +0100879 0, mch_icon_load_cb, iconp);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200880}
881
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 int
883mch_libcall(
884 char_u *libname,
885 char_u *funcname,
Bram Moolenaar0f873732019-12-05 20:28:46 +0100886 char_u *argstring, // NULL when using a argint
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 int argint,
Bram Moolenaar0f873732019-12-05 20:28:46 +0100888 char_u **string_result,// NULL when using number_result
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 int *number_result)
890{
891 HINSTANCE hinstLib;
892 MYSTRPROCSTR ProcAdd;
893 MYINTPROCSTR ProcAddI;
894 char_u *retval_str = NULL;
895 int retval_int = 0;
896 size_t len;
897
898 BOOL fRunTimeLinkSuccess = FALSE;
899
900 // Get a handle to the DLL module.
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100901 hinstLib = vimLoadLib((char *)libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902
903 // If the handle is valid, try to get the function address.
904 if (hinstLib != NULL)
905 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100906# ifdef HAVE_TRY_EXCEPT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907 __try
908 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100909# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910 if (argstring != NULL)
911 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100912 // Call with string argument
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100913 ProcAdd = (MYSTRPROCSTR)GetProcAddress(hinstLib, (LPCSTR)funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 if ((fRunTimeLinkSuccess = (ProcAdd != NULL)) != 0)
915 {
916 if (string_result == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100917 retval_int = ((MYSTRPROCINT)ProcAdd)((LPSTR)argstring);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100919 retval_str = (char_u *)(ProcAdd)((LPSTR)argstring);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 }
921 }
922 else
923 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100924 // Call with number argument
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100925 ProcAddI = (MYINTPROCSTR) GetProcAddress(hinstLib, (LPCSTR)funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 if ((fRunTimeLinkSuccess = (ProcAddI != NULL)) != 0)
927 {
928 if (string_result == NULL)
929 retval_int = ((MYINTPROCINT)ProcAddI)(argint);
930 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100931 retval_str = (char_u *)(ProcAddI)(argint);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932 }
933 }
934
935 // Save the string before we free the library.
936 // Assume that a "1" result is an illegal pointer.
937 if (string_result == NULL)
938 *number_result = retval_int;
939 else if (retval_str != NULL
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100940 && (len = check_str_len(retval_str)) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 {
Bram Moolenaar18a4ba22019-05-24 19:39:03 +0200942 *string_result = alloc(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 if (*string_result != NULL)
944 mch_memmove(*string_result, retval_str, len);
945 }
946
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100947# ifdef HAVE_TRY_EXCEPT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948 }
949 __except(EXCEPTION_EXECUTE_HANDLER)
950 {
951 if (GetExceptionCode() == EXCEPTION_STACK_OVERFLOW)
K.Takatac351dc12022-01-24 11:24:08 +0000952 _resetstkoflw();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 fRunTimeLinkSuccess = 0;
954 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100955# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956
957 // Free the DLL module.
958 (void)FreeLibrary(hinstLib);
959 }
960
961 if (!fRunTimeLinkSuccess)
962 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000963 semsg(_(e_library_call_failed_for_str), funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 return FAIL;
965 }
966
967 return OK;
968}
969#endif
970
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971/*
972 * Debugging helper: expose the MCH_WRITE_DUMP stuff to other modules
973 */
974 void
Bram Moolenaar1266d672017-02-01 13:43:36 +0100975DumpPutS(const char *psz UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100977#ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 if (fdDump)
979 {
980 fputs(psz, fdDump);
981 if (psz[strlen(psz) - 1] != '\n')
982 fputc('\n', fdDump);
983 fflush(fdDump);
984 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100985#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986}
987
988#ifdef _DEBUG
989
990void __cdecl
991Trace(
992 char *pszFormat,
993 ...)
994{
995 CHAR szBuff[2048];
996 va_list args;
997
998 va_start(args, pszFormat);
999 vsprintf(szBuff, pszFormat, args);
1000 va_end(args);
1001
1002 OutputDebugString(szBuff);
1003}
1004
1005#endif //_DEBUG
1006
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001007#if !defined(FEAT_GUI) || defined(VIMDLL) || defined(PROTO)
Bram Moolenaar0f873732019-12-05 20:28:46 +01001008extern HWND g_hWnd; // This is in os_win32.c.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009
1010/*
1011 * Showing the printer dialog is tricky since we have no GUI
1012 * window to parent it. The following routines are needed to
1013 * get the window parenting and Z-order to work properly.
1014 */
1015 static void
1016GetConsoleHwnd(void)
1017{
Bram Moolenaar0f873732019-12-05 20:28:46 +01001018 // Skip if it's already set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 if (s_hwnd != 0)
1020 return;
1021
Bram Moolenaar0f873732019-12-05 20:28:46 +01001022 // Window handle may have been found by init code (Windows NT only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 if (g_hWnd != 0)
1024 {
1025 s_hwnd = g_hWnd;
1026 return;
1027 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028
Bram Moolenaare1ed53f2019-02-12 23:12:37 +01001029 s_hwnd = GetConsoleWindow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030}
Bram Moolenaar843ee412004-06-30 16:16:41 +00001031
1032/*
1033 * Console implementation of ":winpos".
1034 */
1035 int
1036mch_get_winpos(int *x, int *y)
1037{
1038 RECT rect;
1039
1040 GetConsoleHwnd();
1041 GetWindowRect(s_hwnd, &rect);
1042 *x = rect.left;
1043 *y = rect.top;
1044 return OK;
1045}
1046
1047/*
1048 * Console implementation of ":winpos x y".
1049 */
1050 void
1051mch_set_winpos(int x, int y)
1052{
1053 GetConsoleHwnd();
1054 SetWindowPos(s_hwnd, NULL, x, y, 0, 0,
1055 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
1056}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057#endif
1058
1059#if (defined(FEAT_PRINTER) && !defined(FEAT_POSTSCRIPT)) || defined(PROTO)
1060
Bram Moolenaar0f873732019-12-05 20:28:46 +01001061//=================================================================
1062// Win32 printer stuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063
1064static HFONT prt_font_handles[2][2][2];
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001065static PRINTDLGW prt_dlg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066static const int boldface[2] = {FW_REGULAR, FW_BOLD};
1067static TEXTMETRIC prt_tm;
1068static int prt_line_height;
1069static int prt_number_width;
1070static int prt_left_margin;
1071static int prt_right_margin;
1072static int prt_top_margin;
1073static char_u szAppName[] = TEXT("VIM");
1074static HWND hDlgPrint;
1075static int *bUserAbort = NULL;
1076static char_u *prt_name = NULL;
1077
Bram Moolenaar0f873732019-12-05 20:28:46 +01001078// Defines which are also in vim.rc.
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001079# define IDC_BOX1 400
1080# define IDC_PRINTTEXT1 401
1081# define IDC_PRINTTEXT2 402
1082# define IDC_PROGRESS 403
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001084 static BOOL
1085vimSetDlgItemText(HWND hDlg, int nIDDlgItem, char_u *s)
1086{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001087 WCHAR *wp;
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001088 BOOL ret;
1089
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001090 wp = enc_to_utf16(s, NULL);
1091 if (wp == NULL)
1092 return FALSE;
1093
1094 ret = SetDlgItemTextW(hDlg, nIDDlgItem, wp);
1095 vim_free(wp);
1096 return ret;
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001097}
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001098
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099/*
1100 * Convert BGR to RGB for Windows GDI calls
1101 */
1102 static COLORREF
1103swap_me(COLORREF colorref)
1104{
1105 int temp;
1106 char *ptr = (char *)&colorref;
1107
1108 temp = *(ptr);
1109 *(ptr ) = *(ptr + 2);
1110 *(ptr + 2) = temp;
1111 return colorref;
1112}
1113
K.Takatac351dc12022-01-24 11:24:08 +00001114 static INT_PTR CALLBACK
Bram Moolenaar1266d672017-02-01 13:43:36 +01001115PrintDlgProc(
1116 HWND hDlg,
1117 UINT message,
1118 WPARAM wParam UNUSED,
1119 LPARAM lParam UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001121# ifdef FEAT_GETTEXT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 NONCLIENTMETRICS nm;
1123 static HFONT hfont;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001124# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125
1126 switch (message)
1127 {
1128 case WM_INITDIALOG:
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001129# ifdef FEAT_GETTEXT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 nm.cbSize = sizeof(NONCLIENTMETRICS);
1131 if (SystemParametersInfo(
1132 SPI_GETNONCLIENTMETRICS,
1133 sizeof(NONCLIENTMETRICS),
1134 &nm,
1135 0))
1136 {
1137 char buff[MAX_PATH];
1138 int i;
1139
Bram Moolenaar0f873732019-12-05 20:28:46 +01001140 // Translate the dialog texts
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 hfont = CreateFontIndirect(&nm.lfMessageFont);
1142 for (i = IDC_PRINTTEXT1; i <= IDC_PROGRESS; i++)
1143 {
1144 SendDlgItemMessage(hDlg, i, WM_SETFONT, (WPARAM)hfont, 1);
1145 if (GetDlgItemText(hDlg,i, buff, sizeof(buff)))
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001146 vimSetDlgItemText(hDlg,i, (char_u *)_(buff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 }
1148 SendDlgItemMessage(hDlg, IDCANCEL,
1149 WM_SETFONT, (WPARAM)hfont, 1);
1150 if (GetDlgItemText(hDlg,IDCANCEL, buff, sizeof(buff)))
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001151 vimSetDlgItemText(hDlg,IDCANCEL, (char_u *)_(buff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001153# endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001154 SetWindowText(hDlg, (LPCSTR)szAppName);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 if (prt_name != NULL)
1156 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001157 vimSetDlgItemText(hDlg, IDC_PRINTTEXT2, (char_u *)prt_name);
Bram Moolenaard23a8232018-02-10 18:45:26 +01001158 VIM_CLEAR(prt_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 }
1160 EnableMenuItem(GetSystemMenu(hDlg, FALSE), SC_CLOSE, MF_GRAYED);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001161# if !defined(FEAT_GUI) || defined(VIMDLL)
1162# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001163 if (!gui.in_use)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001164# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001165 BringWindowToTop(s_hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001166# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 return TRUE;
1168
1169 case WM_COMMAND:
1170 *bUserAbort = TRUE;
1171 EnableWindow(GetParent(hDlg), TRUE);
1172 DestroyWindow(hDlg);
1173 hDlgPrint = NULL;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001174# ifdef FEAT_GETTEXT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 DeleteObject(hfont);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001176# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 return TRUE;
1178 }
1179 return FALSE;
1180}
1181
1182 static BOOL CALLBACK
Bram Moolenaar1266d672017-02-01 13:43:36 +01001183AbortProc(HDC hdcPrn UNUSED, int iCode UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184{
1185 MSG msg;
1186
K.Takatab7057bd2022-01-21 11:37:07 +00001187 while (!*bUserAbort && PeekMessageW(&msg, NULL, 0, 0, PM_REMOVE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 {
K.Takatab7057bd2022-01-21 11:37:07 +00001189 if (!hDlgPrint || !IsDialogMessageW(hDlgPrint, &msg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 {
1191 TranslateMessage(&msg);
K.Takatab7057bd2022-01-21 11:37:07 +00001192 DispatchMessageW(&msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 }
1194 }
1195 return !*bUserAbort;
1196}
1197
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001198# if !defined(FEAT_GUI) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199
Bram Moolenaar26fdd7d2011-11-30 13:42:44 +01001200 static UINT_PTR CALLBACK
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201PrintHookProc(
1202 HWND hDlg, // handle to dialog box
1203 UINT uiMsg, // message identifier
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02001204 WPARAM wParam UNUSED, // message parameter
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 LPARAM lParam // message parameter
1206 )
1207{
1208 HWND hwndOwner;
1209 RECT rc, rcDlg, rcOwner;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001210 PRINTDLGW *pPD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211
1212 if (uiMsg == WM_INITDIALOG)
1213 {
1214 // Get the owner window and dialog box rectangles.
1215 if ((hwndOwner = GetParent(hDlg)) == NULL)
1216 hwndOwner = GetDesktopWindow();
1217
1218 GetWindowRect(hwndOwner, &rcOwner);
1219 GetWindowRect(hDlg, &rcDlg);
1220 CopyRect(&rc, &rcOwner);
1221
1222 // Offset the owner and dialog box rectangles so that
1223 // right and bottom values represent the width and
1224 // height, and then offset the owner again to discard
1225 // space taken up by the dialog box.
1226
1227 OffsetRect(&rcDlg, -rcDlg.left, -rcDlg.top);
1228 OffsetRect(&rc, -rc.left, -rc.top);
1229 OffsetRect(&rc, -rcDlg.right, -rcDlg.bottom);
1230
1231 // The new position is the sum of half the remaining
1232 // space and the owner's original position.
1233
1234 SetWindowPos(hDlg,
1235 HWND_TOP,
1236 rcOwner.left + (rc.right / 2),
1237 rcOwner.top + (rc.bottom / 2),
1238 0, 0, // ignores size arguments
1239 SWP_NOSIZE);
1240
Bram Moolenaar0f873732019-12-05 20:28:46 +01001241 // tackle the printdlg copiesctrl problem
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001242 pPD = (PRINTDLGW *)lParam;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 pPD->nCopies = (WORD)pPD->lCustData;
1244 SetDlgItemInt( hDlg, edt3, pPD->nCopies, FALSE );
Bram Moolenaar0f873732019-12-05 20:28:46 +01001245 // Bring the window to top
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 BringWindowToTop(GetParent(hDlg));
1247 SetForegroundWindow(hDlg);
1248 }
1249
1250 return FALSE;
1251}
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001252# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253
1254 void
1255mch_print_cleanup(void)
1256{
1257 int pifItalic;
1258 int pifBold;
1259 int pifUnderline;
1260
1261 for (pifBold = 0; pifBold <= 1; pifBold++)
1262 for (pifItalic = 0; pifItalic <= 1; pifItalic++)
1263 for (pifUnderline = 0; pifUnderline <= 1; pifUnderline++)
1264 DeleteObject(prt_font_handles[pifBold][pifItalic][pifUnderline]);
1265
1266 if (prt_dlg.hDC != NULL)
1267 DeleteDC(prt_dlg.hDC);
1268 if (!*bUserAbort)
1269 SendMessage(hDlgPrint, WM_COMMAND, 0, 0);
1270}
1271
1272 static int
1273to_device_units(int idx, int dpi, int physsize, int offset, int def_number)
1274{
1275 int ret = 0;
1276 int u;
1277 int nr;
1278
1279 u = prt_get_unit(idx);
1280 if (u == PRT_UNIT_NONE)
1281 {
1282 u = PRT_UNIT_PERC;
1283 nr = def_number;
1284 }
1285 else
1286 nr = printer_opts[idx].number;
1287
1288 switch (u)
1289 {
1290 case PRT_UNIT_PERC:
1291 ret = (physsize * nr) / 100;
1292 break;
1293 case PRT_UNIT_INCH:
1294 ret = (nr * dpi);
1295 break;
1296 case PRT_UNIT_MM:
1297 ret = (nr * 10 * dpi) / 254;
1298 break;
1299 case PRT_UNIT_POINT:
1300 ret = (nr * 10 * dpi) / 720;
1301 break;
1302 }
1303
1304 if (ret < offset)
1305 return 0;
1306 else
1307 return ret - offset;
1308}
1309
1310 static int
1311prt_get_cpl(void)
1312{
1313 int hr;
1314 int phyw;
1315 int dvoff;
1316 int rev_offset;
1317 int dpi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318
1319 GetTextMetrics(prt_dlg.hDC, &prt_tm);
1320 prt_line_height = prt_tm.tmHeight + prt_tm.tmExternalLeading;
1321
1322 hr = GetDeviceCaps(prt_dlg.hDC, HORZRES);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 phyw = GetDeviceCaps(prt_dlg.hDC, PHYSICALWIDTH);
1324 dvoff = GetDeviceCaps(prt_dlg.hDC, PHYSICALOFFSETX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 dpi = GetDeviceCaps(prt_dlg.hDC, LOGPIXELSX);
1326
1327 rev_offset = phyw - (dvoff + hr);
1328
1329 prt_left_margin = to_device_units(OPT_PRINT_LEFT, dpi, phyw, dvoff, 10);
1330 if (prt_use_number())
1331 {
1332 prt_number_width = PRINT_NUMBER_WIDTH * prt_tm.tmAveCharWidth;
1333 prt_left_margin += prt_number_width;
1334 }
1335 else
1336 prt_number_width = 0;
1337
1338 prt_right_margin = hr - to_device_units(OPT_PRINT_RIGHT, dpi, phyw,
1339 rev_offset, 5);
1340
1341 return (prt_right_margin - prt_left_margin) / prt_tm.tmAveCharWidth;
1342}
1343
1344 static int
1345prt_get_lpp(void)
1346{
1347 int vr;
1348 int phyw;
1349 int dvoff;
1350 int rev_offset;
1351 int bottom_margin;
1352 int dpi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353
1354 vr = GetDeviceCaps(prt_dlg.hDC, VERTRES);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 phyw = GetDeviceCaps(prt_dlg.hDC, PHYSICALHEIGHT);
1356 dvoff = GetDeviceCaps(prt_dlg.hDC, PHYSICALOFFSETY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 dpi = GetDeviceCaps(prt_dlg.hDC, LOGPIXELSY);
1358
1359 rev_offset = phyw - (dvoff + vr);
1360
1361 prt_top_margin = to_device_units(OPT_PRINT_TOP, dpi, phyw, dvoff, 5);
1362
Bram Moolenaar0f873732019-12-05 20:28:46 +01001363 // adjust top margin if there is a header
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 prt_top_margin += prt_line_height * prt_header_height();
1365
1366 bottom_margin = vr - to_device_units(OPT_PRINT_BOT, dpi, phyw,
1367 rev_offset, 5);
1368
1369 return (bottom_margin - prt_top_margin) / prt_line_height;
1370}
1371
1372 int
1373mch_print_init(prt_settings_T *psettings, char_u *jobname, int forceit)
1374{
1375 static HGLOBAL stored_dm = NULL;
1376 static HGLOBAL stored_devn = NULL;
1377 static int stored_nCopies = 1;
1378 static int stored_nFlags = 0;
1379
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001380 LOGFONTW fLogFont;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 int pifItalic;
1382 int pifBold;
1383 int pifUnderline;
1384
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001385 DEVMODEW *mem;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 DEVNAMES *devname;
1387 int i;
1388
1389 bUserAbort = &(psettings->user_abort);
Bram Moolenaara80faa82020-04-12 19:37:17 +02001390 CLEAR_FIELD(prt_dlg);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001391 prt_dlg.lStructSize = sizeof(PRINTDLGW);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001392# if !defined(FEAT_GUI) || defined(VIMDLL)
1393# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001394 if (!gui.in_use)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001395# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01001396 GetConsoleHwnd(); // get value of s_hwnd
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001397# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 prt_dlg.hwndOwner = s_hwnd;
1399 prt_dlg.Flags = PD_NOPAGENUMS | PD_NOSELECTION | PD_RETURNDC;
1400 if (!forceit)
1401 {
1402 prt_dlg.hDevMode = stored_dm;
1403 prt_dlg.hDevNames = stored_devn;
1404 prt_dlg.lCustData = stored_nCopies; // work around bug in print dialog
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001405# if !defined(FEAT_GUI) || defined(VIMDLL)
1406# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001407 if (!gui.in_use)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001408# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001409 {
1410 /*
1411 * Use hook to prevent console window being sent to back
1412 */
1413 prt_dlg.lpfnPrintHook = PrintHookProc;
1414 prt_dlg.Flags |= PD_ENABLEPRINTHOOK;
1415 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001416# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 prt_dlg.Flags |= stored_nFlags;
1418 }
1419
1420 /*
1421 * If bang present, return default printer setup with no dialog
1422 * never show dialog if we are running over telnet
1423 */
1424 if (forceit
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001425# if !defined(FEAT_GUI) || defined(VIMDLL)
1426# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001427 || (!gui.in_use && !term_console)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001428# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 || !term_console
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001430# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001431# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 )
1433 {
1434 prt_dlg.Flags |= PD_RETURNDEFAULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435 /*
1436 * MSDN suggests setting the first parameter to WINSPOOL for
1437 * NT, but NULL appears to work just as well.
1438 */
1439 if (*p_pdev != NUL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001440 prt_dlg.hDC = CreateDC(NULL, (LPCSTR)p_pdev, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 {
1443 prt_dlg.Flags |= PD_RETURNDEFAULT;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001444 if (PrintDlgW(&prt_dlg) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 goto init_fail_dlg;
1446 }
1447 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001448 else if (PrintDlgW(&prt_dlg) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 goto init_fail_dlg;
1450 else
1451 {
1452 /*
1453 * keep the previous driver context
1454 */
1455 stored_dm = prt_dlg.hDevMode;
1456 stored_devn = prt_dlg.hDevNames;
1457 stored_nFlags = prt_dlg.Flags;
1458 stored_nCopies = prt_dlg.nCopies;
1459 }
1460
1461 if (prt_dlg.hDC == NULL)
1462 {
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00001463 emsg(_(e_printer_selection_failed));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 mch_print_cleanup();
1465 return FALSE;
1466 }
1467
Bram Moolenaar0f873732019-12-05 20:28:46 +01001468 // Not all printer drivers report the support of color (or grey) in the
1469 // same way. Let's set has_color if there appears to be some way to print
1470 // more than B&W.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 i = GetDeviceCaps(prt_dlg.hDC, NUMCOLORS);
1472 psettings->has_color = (GetDeviceCaps(prt_dlg.hDC, BITSPIXEL) > 1
1473 || GetDeviceCaps(prt_dlg.hDC, PLANES) > 1
1474 || i > 2 || i == -1);
1475
Bram Moolenaar0f873732019-12-05 20:28:46 +01001476 // Ensure all font styles are baseline aligned
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 SetTextAlign(prt_dlg.hDC, TA_BASELINE|TA_LEFT);
1478
1479 /*
1480 * On some windows systems the nCopies parameter is not
1481 * passed back correctly. It must be retrieved from the
1482 * hDevMode struct.
1483 */
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001484 mem = (DEVMODEW *)GlobalLock(prt_dlg.hDevMode);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 if (mem != NULL)
1486 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 if (mem->dmCopies != 1)
1488 stored_nCopies = mem->dmCopies;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489 if ((mem->dmFields & DM_DUPLEX) && (mem->dmDuplex & ~DMDUP_SIMPLEX))
1490 psettings->duplex = TRUE;
1491 if ((mem->dmFields & DM_COLOR) && (mem->dmColor & DMCOLOR_COLOR))
1492 psettings->has_color = TRUE;
1493 }
1494 GlobalUnlock(prt_dlg.hDevMode);
1495
1496 devname = (DEVNAMES *)GlobalLock(prt_dlg.hDevNames);
1497 if (devname != 0)
1498 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001499 WCHAR *wprinter_name = (WCHAR *)devname + devname->wDeviceOffset;
1500 WCHAR *wport_name = (WCHAR *)devname + devname->wOutputOffset;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001501 char_u *text = (char_u *)_("to %s on %s");
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001502 char_u *printer_name = utf16_to_enc(wprinter_name, NULL);
1503 char_u *port_name = utf16_to_enc(wport_name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001505 if (printer_name != NULL && port_name != NULL)
Bram Moolenaar964b3742019-05-24 18:54:09 +02001506 prt_name = alloc(STRLEN(printer_name)
1507 + STRLEN(port_name) + STRLEN(text));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 if (prt_name != NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001509 wsprintf((char *)prt_name, (const char *)text,
1510 printer_name, port_name);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001511 vim_free(printer_name);
1512 vim_free(port_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 }
1514 GlobalUnlock(prt_dlg.hDevNames);
1515
1516 /*
1517 * Initialise the font according to 'printfont'
1518 */
Bram Moolenaara80faa82020-04-12 19:37:17 +02001519 CLEAR_FIELD(fLogFont);
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001520 if (get_logfont(&fLogFont, p_pfn, prt_dlg.hDC, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 {
Bram Moolenaard88be5b2022-01-04 19:57:55 +00001522 semsg(_(e_unknown_printer_font_str), p_pfn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 mch_print_cleanup();
1524 return FALSE;
1525 }
1526
1527 for (pifBold = 0; pifBold <= 1; pifBold++)
1528 for (pifItalic = 0; pifItalic <= 1; pifItalic++)
1529 for (pifUnderline = 0; pifUnderline <= 1; pifUnderline++)
1530 {
1531 fLogFont.lfWeight = boldface[pifBold];
1532 fLogFont.lfItalic = pifItalic;
1533 fLogFont.lfUnderline = pifUnderline;
1534 prt_font_handles[pifBold][pifItalic][pifUnderline]
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001535 = CreateFontIndirectW(&fLogFont);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 }
1537
1538 SetBkMode(prt_dlg.hDC, OPAQUE);
1539 SelectObject(prt_dlg.hDC, prt_font_handles[0][0][0]);
1540
1541 /*
1542 * Fill in the settings struct
1543 */
1544 psettings->chars_per_line = prt_get_cpl();
1545 psettings->lines_per_page = prt_get_lpp();
Bram Moolenaar7ddc6422014-09-27 11:18:19 +02001546 if (prt_dlg.Flags & PD_USEDEVMODECOPIESANDCOLLATE)
1547 {
1548 psettings->n_collated_copies = (prt_dlg.Flags & PD_COLLATE)
1549 ? prt_dlg.nCopies : 1;
1550 psettings->n_uncollated_copies = (prt_dlg.Flags & PD_COLLATE)
1551 ? 1 : prt_dlg.nCopies;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552
Bram Moolenaar7ddc6422014-09-27 11:18:19 +02001553 if (psettings->n_collated_copies == 0)
1554 psettings->n_collated_copies = 1;
1555
1556 if (psettings->n_uncollated_copies == 0)
1557 psettings->n_uncollated_copies = 1;
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01001558 }
1559 else
1560 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 psettings->n_collated_copies = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 psettings->n_uncollated_copies = 1;
Bram Moolenaar7ddc6422014-09-27 11:18:19 +02001563 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564
1565 psettings->jobname = jobname;
1566
1567 return TRUE;
1568
1569init_fail_dlg:
1570 {
1571 DWORD err = CommDlgExtendedError();
1572
1573 if (err)
1574 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 char_u *buf;
1576
Bram Moolenaar0f873732019-12-05 20:28:46 +01001577 // I suspect FormatMessage() doesn't work for values returned by
1578 // CommDlgExtendedError(). What does?
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
1580 FORMAT_MESSAGE_FROM_SYSTEM |
1581 FORMAT_MESSAGE_IGNORE_INSERTS,
1582 NULL, err, 0, (LPTSTR)(&buf), 0, NULL);
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00001583 semsg(_(e_print_error_str),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 buf == NULL ? (char_u *)_("Unknown") : buf);
1585 LocalFree((LPVOID)(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 }
1587 else
Bram Moolenaar0f873732019-12-05 20:28:46 +01001588 msg_clr_eos(); // Maybe canceled
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589
1590 mch_print_cleanup();
1591 return FALSE;
1592 }
1593}
1594
1595
1596 int
1597mch_print_begin(prt_settings_T *psettings)
1598{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001599 int ret = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 char szBuffer[300];
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001601 WCHAR *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001603 hDlgPrint = CreateDialog(g_hinst, TEXT("PrintDlgBox"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 prt_dlg.hwndOwner, PrintDlgProc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 SetAbortProc(prt_dlg.hDC, AbortProc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 wsprintf(szBuffer, _("Printing '%s'"), gettail(psettings->jobname));
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001607 vimSetDlgItemText(hDlgPrint, IDC_PRINTTEXT1, (char_u *)szBuffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001609 wp = enc_to_utf16(psettings->jobname, NULL);
Bram Moolenaar2290b1f2018-05-13 17:30:45 +02001610 if (wp != NULL)
1611 {
1612 DOCINFOW di;
1613
Bram Moolenaara80faa82020-04-12 19:37:17 +02001614 CLEAR_FIELD(di);
Bram Moolenaar2290b1f2018-05-13 17:30:45 +02001615 di.cbSize = sizeof(di);
1616 di.lpszDocName = wp;
1617 ret = StartDocW(prt_dlg.hDC, &di);
1618 vim_free(wp);
1619 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001621# ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01001622 // Give focus back to main window (when using MDI).
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001623# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001624 if (gui.in_use)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001625# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001626 SetFocus(s_hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001627# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628
1629 return (ret > 0);
1630}
1631
1632 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01001633mch_print_end(prt_settings_T *psettings UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634{
1635 EndDoc(prt_dlg.hDC);
1636 if (!*bUserAbort)
1637 SendMessage(hDlgPrint, WM_COMMAND, 0, 0);
1638}
1639
1640 int
1641mch_print_end_page(void)
1642{
1643 return (EndPage(prt_dlg.hDC) > 0);
1644}
1645
1646 int
1647mch_print_begin_page(char_u *msg)
1648{
1649 if (msg != NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001650 vimSetDlgItemText(hDlgPrint, IDC_PROGRESS, msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 return (StartPage(prt_dlg.hDC) > 0);
1652}
1653
1654 int
1655mch_print_blank_page(void)
1656{
1657 return (mch_print_begin_page(NULL) ? (mch_print_end_page()) : FALSE);
1658}
1659
1660static int prt_pos_x = 0;
1661static int prt_pos_y = 0;
1662
1663 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001664mch_print_start_line(int margin, int page_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665{
1666 if (margin)
1667 prt_pos_x = -prt_number_width;
1668 else
1669 prt_pos_x = 0;
1670 prt_pos_y = page_line * prt_line_height
1671 + prt_tm.tmAscent + prt_tm.tmExternalLeading;
1672}
1673
1674 int
1675mch_print_text_out(char_u *p, int len)
1676{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 SIZE sz;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001678 WCHAR *wp;
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001679 int wlen = len;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001680 int ret = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001682 wp = enc_to_utf16(p, &wlen);
1683 if (wp == NULL)
1684 return FALSE;
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001685
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001686 TextOutW(prt_dlg.hDC, prt_pos_x + prt_left_margin,
1687 prt_pos_y + prt_top_margin, wp, wlen);
1688 GetTextExtentPoint32W(prt_dlg.hDC, wp, wlen, &sz);
1689 vim_free(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 prt_pos_x += (sz.cx - prt_tm.tmOverhang);
Bram Moolenaar0f873732019-12-05 20:28:46 +01001691 // This is wrong when printing spaces for a TAB.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001692 if (p[len] != NUL)
1693 {
Bram Moolenaar1614a142019-10-06 22:00:13 +02001694 wlen = mb_ptr2len(p + len);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001695 wp = enc_to_utf16(p + len, &wlen);
1696 if (wp != NULL)
1697 {
1698 GetTextExtentPoint32W(prt_dlg.hDC, wp, 1, &sz);
1699 ret = (prt_pos_x + prt_left_margin + sz.cx > prt_right_margin);
1700 vim_free(wp);
1701 }
1702 }
1703 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704}
1705
1706 void
1707mch_print_set_font(int iBold, int iItalic, int iUnderline)
1708{
1709 SelectObject(prt_dlg.hDC, prt_font_handles[iBold][iItalic][iUnderline]);
1710}
1711
1712 void
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001713mch_print_set_bg(long_u bgcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714{
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001715 SetBkColor(prt_dlg.hDC, GetNearestColor(prt_dlg.hDC,
1716 swap_me((COLORREF)bgcol)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 /*
1718 * With a white background we can draw characters transparent, which is
1719 * good for italic characters that overlap to the next char cell.
1720 */
1721 if (bgcol == 0xffffffUL)
1722 SetBkMode(prt_dlg.hDC, TRANSPARENT);
1723 else
1724 SetBkMode(prt_dlg.hDC, OPAQUE);
1725}
1726
1727 void
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001728mch_print_set_fg(long_u fgcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729{
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001730 SetTextColor(prt_dlg.hDC, GetNearestColor(prt_dlg.hDC,
1731 swap_me((COLORREF)fgcol)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732}
1733
Bram Moolenaar0f873732019-12-05 20:28:46 +01001734#endif // FEAT_PRINTER && !FEAT_POSTSCRIPT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735
Bram Moolenaar58d98232005-07-23 22:25:46 +00001736
1737
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738#if defined(FEAT_SHORTCUT) || defined(PROTO)
Bram Moolenaar82881492012-11-20 16:53:39 +01001739# ifndef PROTO
1740# include <shlobj.h>
1741# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001743typedef BOOL (WINAPI *pfnGetFinalPathNameByHandleW)(
Bram Moolenaardce1e892019-02-10 23:18:53 +01001744 HANDLE hFile,
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001745 LPWSTR lpszFilePath,
1746 DWORD cchFilePath,
1747 DWORD dwFlags);
1748static pfnGetFinalPathNameByHandleW pGetFinalPathNameByHandleW = NULL;
Bram Moolenaardce1e892019-02-10 23:18:53 +01001749
Bram Moolenaar4a792c82019-06-06 12:22:41 +02001750# define is_path_sep(c) ((c) == L'\\' || (c) == L'/')
1751
1752 static int
1753is_reparse_point_included(LPCWSTR fname)
1754{
1755 LPCWSTR p = fname, q;
1756 WCHAR buf[MAX_PATH];
1757 DWORD attr;
1758
1759 if (isalpha(p[0]) && p[1] == L':' && is_path_sep(p[2]))
1760 p += 3;
1761 else if (is_path_sep(p[0]) && is_path_sep(p[1]))
1762 p += 2;
1763
1764 while (*p != L'\0')
1765 {
1766 q = wcspbrk(p, L"\\/");
1767 if (q == NULL)
1768 p = q = fname + wcslen(fname);
1769 else
1770 p = q + 1;
1771 if (q - fname >= MAX_PATH)
1772 return FALSE;
1773 wcsncpy(buf, fname, q - fname);
1774 buf[q - fname] = L'\0';
1775 attr = GetFileAttributesW(buf);
1776 if (attr != INVALID_FILE_ATTRIBUTES
1777 && (attr & FILE_ATTRIBUTE_REPARSE_POINT) != 0)
1778 return TRUE;
1779 }
1780 return FALSE;
1781}
1782
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02001783 static char_u *
Bram Moolenaardce1e892019-02-10 23:18:53 +01001784resolve_reparse_point(char_u *fname)
1785{
1786 HANDLE h = INVALID_HANDLE_VALUE;
1787 DWORD size;
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001788 WCHAR *p, *wp;
Bram Moolenaardce1e892019-02-10 23:18:53 +01001789 char_u *rfname = NULL;
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001790 WCHAR *buff = NULL;
Bram Moolenaardce1e892019-02-10 23:18:53 +01001791 static BOOL loaded = FALSE;
1792
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001793 if (pGetFinalPathNameByHandleW == NULL)
Bram Moolenaardce1e892019-02-10 23:18:53 +01001794 {
1795 HMODULE hmod = GetModuleHandle("kernel32.dll");
1796
1797 if (loaded == TRUE)
1798 return NULL;
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001799 pGetFinalPathNameByHandleW = (pfnGetFinalPathNameByHandleW)
1800 GetProcAddress(hmod, "GetFinalPathNameByHandleW");
Bram Moolenaardce1e892019-02-10 23:18:53 +01001801 loaded = TRUE;
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001802 if (pGetFinalPathNameByHandleW == NULL)
Bram Moolenaardce1e892019-02-10 23:18:53 +01001803 return NULL;
1804 }
1805
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001806 p = enc_to_utf16(fname, NULL);
1807 if (p == NULL)
1808 goto fail;
1809
Bram Moolenaar4a792c82019-06-06 12:22:41 +02001810 if (!is_reparse_point_included(p))
1811 {
1812 vim_free(p);
1813 goto fail;
1814 }
1815
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001816 h = CreateFileW(p, 0, 0, NULL, OPEN_EXISTING,
1817 FILE_FLAG_BACKUP_SEMANTICS, NULL);
1818 vim_free(p);
Bram Moolenaardce1e892019-02-10 23:18:53 +01001819
1820 if (h == INVALID_HANDLE_VALUE)
1821 goto fail;
1822
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001823 size = pGetFinalPathNameByHandleW(h, NULL, 0, 0);
1824 if (size == 0)
1825 goto fail;
1826 buff = ALLOC_MULT(WCHAR, size);
1827 if (buff == NULL)
1828 goto fail;
1829 if (pGetFinalPathNameByHandleW(h, buff, size, 0) == 0)
Bram Moolenaardce1e892019-02-10 23:18:53 +01001830 goto fail;
1831
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001832 if (wcsncmp(buff, L"\\\\?\\UNC\\", 8) == 0)
1833 {
1834 buff[6] = L'\\';
1835 wp = buff + 6;
1836 }
1837 else if (wcsncmp(buff, L"\\\\?\\", 4) == 0)
1838 wp = buff + 4;
Bram Moolenaardce1e892019-02-10 23:18:53 +01001839 else
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001840 wp = buff;
1841
1842 rfname = utf16_to_enc(wp, NULL);
Bram Moolenaardce1e892019-02-10 23:18:53 +01001843
1844fail:
1845 if (h != INVALID_HANDLE_VALUE)
1846 CloseHandle(h);
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001847 if (buff != NULL)
1848 vim_free(buff);
Bram Moolenaardce1e892019-02-10 23:18:53 +01001849
1850 return rfname;
1851}
1852
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853/*
1854 * When "fname" is the name of a shortcut (*.lnk) resolve the file it points
1855 * to and return that name in allocated memory.
1856 * Otherwise NULL is returned.
1857 */
Bram Moolenaardce1e892019-02-10 23:18:53 +01001858 static char_u *
1859resolve_shortcut(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860{
1861 HRESULT hr;
1862 IShellLink *psl = NULL;
1863 IPersistFile *ppf = NULL;
1864 OLECHAR wsz[MAX_PATH];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 char_u *rfname = NULL;
1866 int len;
Bram Moolenaar604729e2013-08-30 16:44:19 +02001867 IShellLinkW *pslw = NULL;
1868 WIN32_FIND_DATAW ffdw; // we get those free of charge
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869
Bram Moolenaar0f873732019-12-05 20:28:46 +01001870 // Check if the file name ends in ".lnk". Avoid calling
1871 // CoCreateInstance(), it's quite slow.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 if (fname == NULL)
1873 return rfname;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001874 len = (int)STRLEN(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 if (len <= 4 || STRNICMP(fname + len - 4, ".lnk", 4) != 0)
1876 return rfname;
1877
1878 CoInitialize(NULL);
1879
1880 // create a link manager object and request its interface
1881 hr = CoCreateInstance(
1882 &CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001883 &IID_IShellLinkW, (void**)&pslw);
1884 if (hr == S_OK)
1885 {
1886 WCHAR *p = enc_to_utf16(fname, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001888 if (p != NULL)
1889 {
1890 // Get a pointer to the IPersistFile interface.
1891 hr = pslw->lpVtbl->QueryInterface(
1892 pslw, &IID_IPersistFile, (void**)&ppf);
1893 if (hr != S_OK)
1894 goto shortcut_errorw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001896 // "load" the name and resolve the link
1897 hr = ppf->lpVtbl->Load(ppf, p, STGM_READ);
1898 if (hr != S_OK)
1899 goto shortcut_errorw;
1900# if 0 // This makes Vim wait a long time if the target does not exist.
1901 hr = pslw->lpVtbl->Resolve(pslw, NULL, SLR_NO_UI);
1902 if (hr != S_OK)
1903 goto shortcut_errorw;
Bram Moolenaar604729e2013-08-30 16:44:19 +02001904# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001906 // Get the path to the link target.
1907 ZeroMemory(wsz, MAX_PATH * sizeof(WCHAR));
1908 hr = pslw->lpVtbl->GetPath(pslw, wsz, MAX_PATH, &ffdw, 0);
1909 if (hr == S_OK && wsz[0] != NUL)
1910 rfname = utf16_to_enc(wsz, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001912shortcut_errorw:
1913 vim_free(p);
1914 }
1915 }
1916
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 // Release all interface pointers (both belong to the same object)
1918 if (ppf != NULL)
1919 ppf->lpVtbl->Release(ppf);
1920 if (psl != NULL)
1921 psl->lpVtbl->Release(psl);
Bram Moolenaar604729e2013-08-30 16:44:19 +02001922 if (pslw != NULL)
1923 pslw->lpVtbl->Release(pslw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924
1925 CoUninitialize();
1926 return rfname;
1927}
Bram Moolenaardce1e892019-02-10 23:18:53 +01001928
1929 char_u *
1930mch_resolve_path(char_u *fname, int reparse_point)
1931{
1932 char_u *path = resolve_shortcut(fname);
1933
1934 if (path == NULL && reparse_point)
1935 path = resolve_reparse_point(fname);
1936 return path;
1937}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938#endif
1939
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001940#if (defined(FEAT_EVAL) && (!defined(FEAT_GUI) || defined(VIMDLL))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941/*
1942 * Bring ourselves to the foreground. Does work if the OS doesn't allow it.
1943 */
1944 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001945win32_set_foreground(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946{
Bram Moolenaar0f873732019-12-05 20:28:46 +01001947 GetConsoleHwnd(); // get value of s_hwnd
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 if (s_hwnd != 0)
1949 SetForegroundWindow(s_hwnd);
1950}
1951#endif
1952
1953#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
1954/*
1955 * Client-server code for Vim
1956 *
1957 * Originally written by Paul Moore
1958 */
1959
Bram Moolenaar0f873732019-12-05 20:28:46 +01001960// In order to handle inter-process messages, we need to have a window. But
1961// the functions in this module can be called before the main GUI window is
1962// created (and may also be called in the console version, where there is no
1963// GUI window at all).
1964//
1965// So we create a hidden window, and arrange to destroy it on exit.
1966HWND message_window = 0; // window that's handling messages
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001968# define VIM_CLASSNAME "VIM_MESSAGES"
1969# define VIM_CLASSNAME_LEN (sizeof(VIM_CLASSNAME) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001971// Communication is via WM_COPYDATA messages. The message type is sent in
Bram Moolenaar0f873732019-12-05 20:28:46 +01001972// the dwData parameter. Types are defined here.
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001973# define COPYDATA_KEYS 0
1974# define COPYDATA_REPLY 1
1975# define COPYDATA_EXPR 10
1976# define COPYDATA_RESULT 11
1977# define COPYDATA_ERROR_RESULT 12
1978# define COPYDATA_ENCODING 20
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979
Bram Moolenaar0f873732019-12-05 20:28:46 +01001980// This is a structure containing a server HWND and its name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981struct server_id
1982{
1983 HWND hwnd;
1984 char_u *name;
1985};
1986
Bram Moolenaar0f873732019-12-05 20:28:46 +01001987// Last received 'encoding' that the client uses.
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001988static char_u *client_enc = NULL;
1989
1990/*
1991 * Tell the other side what encoding we are using.
1992 * Errors are ignored.
1993 */
1994 static void
1995serverSendEnc(HWND target)
1996{
1997 COPYDATASTRUCT data;
1998
1999 data.dwData = COPYDATA_ENCODING;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002000 data.cbData = (DWORD)STRLEN(p_enc) + 1;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002001 data.lpData = p_enc;
2002 (void)SendMessage(target, WM_COPYDATA, (WPARAM)message_window,
2003 (LPARAM)(&data));
2004}
2005
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006/*
2007 * Clean up on exit. This destroys the hidden message window.
2008 */
2009 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010CleanUpMessaging(void)
2011{
2012 if (message_window != 0)
2013 {
2014 DestroyWindow(message_window);
2015 message_window = 0;
2016 }
2017}
2018
2019static int save_reply(HWND server, char_u *reply, int expr);
2020
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002021/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 * The window procedure for the hidden message window.
2023 * It handles callback messages and notifications from servers.
2024 * In order to process these messages, it is necessary to run a
2025 * message loop. Code which may run before the main message loop
2026 * is started (in the GUI) is careful to pump messages when it needs
2027 * to. Features which require message delivery during normal use will
2028 * not work in the console version - this basically means those
2029 * features which allow Vim to act as a server, rather than a client.
2030 */
2031 static LRESULT CALLBACK
2032Messaging_WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
2033{
2034 if (msg == WM_COPYDATA)
2035 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002036 // This is a message from another Vim. The dwData member of the
2037 // COPYDATASTRUCT determines the type of message:
2038 // COPYDATA_ENCODING:
2039 // The encoding that the client uses. Following messages will
2040 // use this encoding, convert if needed.
2041 // COPYDATA_KEYS:
2042 // A key sequence. We are a server, and a client wants these keys
2043 // adding to the input queue.
2044 // COPYDATA_REPLY:
2045 // A reply. We are a client, and a server has sent this message
2046 // in response to a request. (server2client())
2047 // COPYDATA_EXPR:
2048 // An expression. We are a server, and a client wants us to
2049 // evaluate this expression.
2050 // COPYDATA_RESULT:
2051 // A reply. We are a client, and a server has sent this message
2052 // in response to a COPYDATA_EXPR.
2053 // COPYDATA_ERROR_RESULT:
2054 // A reply. We are a client, and a server has sent this message
2055 // in response to a COPYDATA_EXPR that failed to evaluate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 COPYDATASTRUCT *data = (COPYDATASTRUCT*)lParam;
2057 HWND sender = (HWND)wParam;
2058 COPYDATASTRUCT reply;
2059 char_u *res;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 int retval;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002061 char_u *str;
2062 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063
2064 switch (data->dwData)
2065 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002066 case COPYDATA_ENCODING:
Bram Moolenaar0f873732019-12-05 20:28:46 +01002067 // Remember the encoding that the client uses.
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002068 vim_free(client_enc);
2069 client_enc = enc_canonize((char_u *)data->lpData);
2070 return 1;
2071
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 case COPYDATA_KEYS:
Bram Moolenaar0f873732019-12-05 20:28:46 +01002073 // Remember who sent this, for <client>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 clientWindow = sender;
2075
Bram Moolenaar0f873732019-12-05 20:28:46 +01002076 // Add the received keys to the input buffer. The loop waiting
2077 // for the user to do something should check the input buffer.
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002078 str = serverConvert(client_enc, (char_u *)data->lpData, &tofree);
2079 server_to_input_buf(str);
2080 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081
2082# ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01002083 // Wake up the main GUI loop.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002084# ifdef VIMDLL
2085 if (gui.in_use)
2086# endif
2087 if (s_hwnd != 0)
2088 PostMessage(s_hwnd, WM_NULL, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089# endif
2090 return 1;
2091
2092 case COPYDATA_EXPR:
Bram Moolenaar0f873732019-12-05 20:28:46 +01002093 // Remember who sent this, for <client>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 clientWindow = sender;
2095
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002096 str = serverConvert(client_enc, (char_u *)data->lpData, &tofree);
2097 res = eval_client_expr_to_string(str);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002098
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 if (res == NULL)
2100 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00002101 char *err = _(e_invalid_expression_received);
Bram Moolenaar15bf76d2017-03-18 16:18:37 +01002102 size_t len = STRLEN(str) + STRLEN(err) + 5;
2103
Bram Moolenaar964b3742019-05-24 18:54:09 +02002104 res = alloc(len);
Bram Moolenaar15bf76d2017-03-18 16:18:37 +01002105 if (res != NULL)
2106 vim_snprintf((char *)res, len, "%s: \"%s\"", err, str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 reply.dwData = COPYDATA_ERROR_RESULT;
2108 }
2109 else
2110 reply.dwData = COPYDATA_RESULT;
2111 reply.lpData = res;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002112 reply.cbData = (DWORD)STRLEN(res) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002114 serverSendEnc(sender);
Bram Moolenaar5246cd72013-06-16 16:41:47 +02002115 retval = (int)SendMessage(sender, WM_COPYDATA,
2116 (WPARAM)message_window, (LPARAM)(&reply));
Bram Moolenaar15bf76d2017-03-18 16:18:37 +01002117 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 vim_free(res);
2119 return retval;
2120
2121 case COPYDATA_REPLY:
2122 case COPYDATA_RESULT:
2123 case COPYDATA_ERROR_RESULT:
2124 if (data->lpData != NULL)
2125 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002126 str = serverConvert(client_enc, (char_u *)data->lpData,
2127 &tofree);
2128 if (tofree == NULL)
2129 str = vim_strsave(str);
2130 if (save_reply(sender, str,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 (data->dwData == COPYDATA_REPLY ? 0 :
2132 (data->dwData == COPYDATA_RESULT ? 1 :
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002133 2))) == FAIL)
2134 vim_free(str);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002135 else if (data->dwData == COPYDATA_REPLY)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 {
Bram Moolenaar427d51c2013-06-16 16:01:25 +02002137 char_u winstr[30];
2138
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002139 sprintf((char *)winstr, PRINTF_HEX_LONG_U, (long_u)sender);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002140 apply_autocmds(EVENT_REMOTEREPLY, winstr, str,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 TRUE, curbuf);
2142 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 }
2144 return 1;
2145 }
2146
2147 return 0;
2148 }
2149
2150 else if (msg == WM_ACTIVATE && wParam == WA_ACTIVE)
2151 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002152 // When the message window is activated (brought to the foreground),
2153 // this actually applies to the text window.
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002154# if !defined(FEAT_GUI) || defined(VIMDLL)
2155# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002156 if (!gui.in_use)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002157# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002158 GetConsoleHwnd(); // get value of s_hwnd
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002159# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 if (s_hwnd != 0)
2161 {
2162 SetForegroundWindow(s_hwnd);
2163 return 0;
2164 }
2165 }
2166
2167 return DefWindowProc(hwnd, msg, wParam, lParam);
2168}
2169
2170/*
2171 * Initialise the message handling process. This involves creating a window
2172 * to handle messages - the window will not be visible.
2173 */
2174 void
2175serverInitMessaging(void)
2176{
2177 WNDCLASS wndclass;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178
Bram Moolenaar0f873732019-12-05 20:28:46 +01002179 // Clean up on exit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 atexit(CleanUpMessaging);
2181
Bram Moolenaar0f873732019-12-05 20:28:46 +01002182 // Register a window class - we only really care
2183 // about the window procedure
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 wndclass.style = 0;
2185 wndclass.lpfnWndProc = Messaging_WndProc;
2186 wndclass.cbClsExtra = 0;
2187 wndclass.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002188 wndclass.hInstance = g_hinst;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 wndclass.hIcon = NULL;
2190 wndclass.hCursor = NULL;
2191 wndclass.hbrBackground = NULL;
2192 wndclass.lpszMenuName = NULL;
2193 wndclass.lpszClassName = VIM_CLASSNAME;
2194 RegisterClass(&wndclass);
2195
Bram Moolenaar0f873732019-12-05 20:28:46 +01002196 // Create the message window. It will be hidden, so the details don't
2197 // matter. Don't use WS_OVERLAPPEDWINDOW, it will make a shortcut remove
2198 // focus from gvim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 message_window = CreateWindow(VIM_CLASSNAME, "",
2200 WS_POPUPWINDOW | WS_CAPTION,
2201 CW_USEDEFAULT, CW_USEDEFAULT,
2202 100, 100, NULL, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002203 g_hinst, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204}
2205
Bram Moolenaar0f873732019-12-05 20:28:46 +01002206// Used by serverSendToVim() to find an alternate server name.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002207static char_u *altname_buf_ptr = NULL;
2208
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209/*
2210 * Get the title of the window "hwnd", which is the Vim server name, in
2211 * "name[namelen]" and return the length.
2212 * Returns zero if window "hwnd" is not a Vim server.
2213 */
2214 static int
2215getVimServerName(HWND hwnd, char *name, int namelen)
2216{
2217 int len;
2218 char buffer[VIM_CLASSNAME_LEN + 1];
2219
Bram Moolenaar0f873732019-12-05 20:28:46 +01002220 // Ignore windows which aren't Vim message windows
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 len = GetClassName(hwnd, buffer, sizeof(buffer));
2222 if (len != VIM_CLASSNAME_LEN || STRCMP(buffer, VIM_CLASSNAME) != 0)
2223 return 0;
2224
Bram Moolenaar0f873732019-12-05 20:28:46 +01002225 // Get the title of the window
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 return GetWindowText(hwnd, name, namelen);
2227}
2228
2229 static BOOL CALLBACK
2230enumWindowsGetServer(HWND hwnd, LPARAM lparam)
2231{
2232 struct server_id *id = (struct server_id *)lparam;
2233 char server[MAX_PATH];
2234
Bram Moolenaar0f873732019-12-05 20:28:46 +01002235 // Get the title of the window
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 if (getVimServerName(hwnd, server, sizeof(server)) == 0)
2237 return TRUE;
2238
Bram Moolenaar0f873732019-12-05 20:28:46 +01002239 // If this is the server we're looking for, return its HWND
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 if (STRICMP(server, id->name) == 0)
2241 {
2242 id->hwnd = hwnd;
2243 return FALSE;
2244 }
2245
Bram Moolenaar0f873732019-12-05 20:28:46 +01002246 // If we are looking for an alternate server, remember this name.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002247 if (altname_buf_ptr != NULL
2248 && STRNICMP(server, id->name, STRLEN(id->name)) == 0
2249 && vim_isdigit(server[STRLEN(id->name)]))
2250 {
2251 STRCPY(altname_buf_ptr, server);
Bram Moolenaar0f873732019-12-05 20:28:46 +01002252 altname_buf_ptr = NULL; // don't use another name
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002253 }
2254
Bram Moolenaar0f873732019-12-05 20:28:46 +01002255 // Otherwise, keep looking
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 return TRUE;
2257}
2258
2259 static BOOL CALLBACK
2260enumWindowsGetNames(HWND hwnd, LPARAM lparam)
2261{
2262 garray_T *ga = (garray_T *)lparam;
2263 char server[MAX_PATH];
2264
Bram Moolenaar0f873732019-12-05 20:28:46 +01002265 // Get the title of the window
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 if (getVimServerName(hwnd, server, sizeof(server)) == 0)
2267 return TRUE;
2268
Bram Moolenaar0f873732019-12-05 20:28:46 +01002269 // Add the name to the list
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002270 ga_concat(ga, (char_u *)server);
2271 ga_concat(ga, (char_u *)"\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 return TRUE;
2273}
2274
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002275struct enum_windows_s
2276{
2277 WNDENUMPROC lpEnumFunc;
2278 LPARAM lParam;
2279};
2280
2281 static BOOL CALLBACK
2282enum_windows_child(HWND hwnd, LPARAM lParam)
2283{
2284 struct enum_windows_s *ew = (struct enum_windows_s *)lParam;
2285
2286 return (ew->lpEnumFunc)(hwnd, ew->lParam);
2287}
2288
2289 static BOOL CALLBACK
2290enum_windows_toplevel(HWND hwnd, LPARAM lParam)
2291{
2292 struct enum_windows_s *ew = (struct enum_windows_s *)lParam;
2293
Bram Moolenaar95ba5c32018-10-07 22:47:07 +02002294 if ((ew->lpEnumFunc)(hwnd, ew->lParam))
2295 return TRUE;
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002296 return EnumChildWindows(hwnd, enum_windows_child, lParam);
2297}
2298
Bram Moolenaar0f873732019-12-05 20:28:46 +01002299/*
2300 * Enumerate all windows including children.
2301 */
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002302 static BOOL
2303enum_windows(WNDENUMPROC lpEnumFunc, LPARAM lParam)
2304{
2305 struct enum_windows_s ew;
2306
2307 ew.lpEnumFunc = lpEnumFunc;
2308 ew.lParam = lParam;
2309 return EnumWindows(enum_windows_toplevel, (LPARAM)&ew);
2310}
2311
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 static HWND
2313findServer(char_u *name)
2314{
2315 struct server_id id;
2316
2317 id.name = name;
2318 id.hwnd = 0;
2319
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002320 enum_windows(enumWindowsGetServer, (LPARAM)(&id));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321
2322 return id.hwnd;
2323}
2324
2325 void
2326serverSetName(char_u *name)
2327{
2328 char_u *ok_name;
2329 HWND hwnd = 0;
2330 int i = 0;
2331 char_u *p;
2332
Bram Moolenaar0f873732019-12-05 20:28:46 +01002333 // Leave enough space for a 9-digit suffix to ensure uniqueness!
Bram Moolenaar964b3742019-05-24 18:54:09 +02002334 ok_name = alloc(STRLEN(name) + 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335
2336 STRCPY(ok_name, name);
2337 p = ok_name + STRLEN(name);
2338
2339 for (;;)
2340 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002341 // This is inefficient - we're doing an EnumWindows loop for each
2342 // possible name. It would be better to grab all names in one go,
2343 // and scan the list each time...
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 hwnd = findServer(ok_name);
2345 if (hwnd == 0)
2346 break;
2347
2348 ++i;
2349 if (i >= 1000)
2350 break;
2351
2352 sprintf((char *)p, "%d", i);
2353 }
2354
2355 if (hwnd != 0)
2356 vim_free(ok_name);
2357 else
2358 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002359 // Remember the name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 serverName = ok_name;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002361 need_maketitle = TRUE; // update Vim window title later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362
Bram Moolenaar0f873732019-12-05 20:28:46 +01002363 // Update the message window title
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002364 SetWindowText(message_window, (LPCSTR)ok_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002366# ifdef FEAT_EVAL
Bram Moolenaar0f873732019-12-05 20:28:46 +01002367 // Set the servername variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 set_vim_var_string(VV_SEND_SERVER, serverName, -1);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002369# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 }
2371}
2372
2373 char_u *
2374serverGetVimNames(void)
2375{
2376 garray_T ga;
2377
2378 ga_init2(&ga, 1, 100);
2379
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002380 enum_windows(enumWindowsGetNames, (LPARAM)(&ga));
Bram Moolenaar269ec652004-07-29 08:43:53 +00002381 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382
2383 return ga.ga_data;
2384}
2385
2386 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002387serverSendReply(
Bram Moolenaar0f873732019-12-05 20:28:46 +01002388 char_u *name, // Where to send.
2389 char_u *reply) // What to send.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390{
2391 HWND target;
2392 COPYDATASTRUCT data;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002393 long_u n = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394
Bram Moolenaar0f873732019-12-05 20:28:46 +01002395 // The "name" argument is a magic cookie obtained from expand("<client>").
2396 // It should be of the form 0xXXXXX - i.e. a C hex literal, which is the
2397 // value of the client's message window HWND.
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002398 sscanf((char *)name, SCANF_HEX_LONG_U, &n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 if (n == 0)
2400 return -1;
2401
2402 target = (HWND)n;
2403 if (!IsWindow(target))
2404 return -1;
2405
2406 data.dwData = COPYDATA_REPLY;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002407 data.cbData = (DWORD)STRLEN(reply) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 data.lpData = reply;
2409
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002410 serverSendEnc(target);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 if (SendMessage(target, WM_COPYDATA, (WPARAM)message_window,
2412 (LPARAM)(&data)))
2413 return 0;
2414
2415 return -1;
2416}
2417
2418 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002419serverSendToVim(
Bram Moolenaar0f873732019-12-05 20:28:46 +01002420 char_u *name, // Where to send.
2421 char_u *cmd, // What to send.
2422 char_u **result, // Result of eval'ed expression
2423 void *ptarget, // HWND of server
2424 int asExpr, // Expression or keys?
2425 int timeout, // timeout in seconds or zero
2426 int silent) // don't complain about no server
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427{
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002428 HWND target;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 COPYDATASTRUCT data;
2430 char_u *retval = NULL;
2431 int retcode = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002432 char_u altname_buf[MAX_PATH];
2433
Bram Moolenaar0f873732019-12-05 20:28:46 +01002434 // Execute locally if no display or target is ourselves
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01002435 if (serverName != NULL && STRICMP(name, serverName) == 0)
2436 return sendToLocalVim(cmd, asExpr, result);
2437
Bram Moolenaar0f873732019-12-05 20:28:46 +01002438 // If the server name does not end in a digit then we look for an
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00002439 // alternate name. e.g. when "name" is GVIM then we may find GVIM2.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002440 if (STRLEN(name) > 1 && !vim_isdigit(name[STRLEN(name) - 1]))
2441 altname_buf_ptr = altname_buf;
2442 altname_buf[0] = NUL;
2443 target = findServer(name);
2444 altname_buf_ptr = NULL;
2445 if (target == 0 && altname_buf[0] != NUL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01002446 // Use another server name we found.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002447 target = findServer(altname_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448
2449 if (target == 0)
2450 {
2451 if (!silent)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002452 semsg(_(e_no_registered_server_named_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 return -1;
2454 }
2455
2456 if (ptarget)
2457 *(HWND *)ptarget = target;
2458
2459 data.dwData = asExpr ? COPYDATA_EXPR : COPYDATA_KEYS;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002460 data.cbData = (DWORD)STRLEN(cmd) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 data.lpData = cmd;
2462
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002463 serverSendEnc(target);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 if (SendMessage(target, WM_COPYDATA, (WPARAM)message_window,
2465 (LPARAM)(&data)) == 0)
2466 return -1;
2467
2468 if (asExpr)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002469 retval = serverGetReply(target, &retcode, TRUE, TRUE, timeout);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470
2471 if (result == NULL)
2472 vim_free(retval);
2473 else
Bram Moolenaar0f873732019-12-05 20:28:46 +01002474 *result = retval; // Caller assumes responsibility for freeing
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475
2476 return retcode;
2477}
2478
2479/*
2480 * Bring the server to the foreground.
2481 */
2482 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002483serverForeground(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484{
2485 HWND target = findServer(name);
2486
2487 if (target != 0)
2488 SetForegroundWindow(target);
2489}
2490
Bram Moolenaar0f873732019-12-05 20:28:46 +01002491// Replies from server need to be stored until the client picks them up via
2492// remote_read(). So we maintain a list of server-id/reply pairs.
2493// Note that there could be multiple replies from one server pending if the
2494// client is slow picking them up.
2495// We just store the replies in a simple list. When we remove an entry, we
2496// move list entries down to fill the gap.
2497// The server ID is simply the HWND.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498typedef struct
2499{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002500 HWND server; // server window
2501 char_u *reply; // reply string
2502 int expr_result; // 0 for REPLY, 1 for RESULT 2 for error
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002503} reply_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504
2505static garray_T reply_list = {0, 0, sizeof(reply_T), 5, 0};
2506
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002507# define REPLY_ITEM(i) ((reply_T *)(reply_list.ga_data) + (i))
2508# define REPLY_COUNT (reply_list.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509
Bram Moolenaar0f873732019-12-05 20:28:46 +01002510// Flag which is used to wait for a reply
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511static int reply_received = 0;
2512
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002513/*
2514 * Store a reply. "reply" must be allocated memory (or NULL).
2515 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 static int
2517save_reply(HWND server, char_u *reply, int expr)
2518{
2519 reply_T *rep;
2520
2521 if (ga_grow(&reply_list, 1) == FAIL)
2522 return FAIL;
2523
2524 rep = REPLY_ITEM(REPLY_COUNT);
2525 rep->server = server;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002526 rep->reply = reply;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 rep->expr_result = expr;
2528 if (rep->reply == NULL)
2529 return FAIL;
2530
2531 ++REPLY_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 reply_received = 1;
2533 return OK;
2534}
2535
2536/*
2537 * Get a reply from server "server".
2538 * When "expr_res" is non NULL, get the result of an expression, otherwise a
2539 * server2client() message.
2540 * When non NULL, point to return code. 0 => OK, -1 => ERROR
2541 * If "remove" is TRUE, consume the message, the caller must free it then.
2542 * if "wait" is TRUE block until a message arrives (or the server exits).
2543 */
2544 char_u *
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002545serverGetReply(HWND server, int *expr_res, int remove, int wait, int timeout)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546{
2547 int i;
2548 char_u *reply;
2549 reply_T *rep;
Bram Moolenaar15e737f2017-03-18 21:22:47 +01002550 int did_process = FALSE;
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002551 time_t start;
2552 time_t now;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553
Bram Moolenaar0f873732019-12-05 20:28:46 +01002554 // When waiting, loop until the message waiting for is received.
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002555 time(&start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 for (;;)
2557 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002558 // Reset this here, in case a message arrives while we are going
2559 // through the already received messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 reply_received = 0;
2561
2562 for (i = 0; i < REPLY_COUNT; ++i)
2563 {
2564 rep = REPLY_ITEM(i);
2565 if (rep->server == server
2566 && ((rep->expr_result != 0) == (expr_res != NULL)))
2567 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002568 // Save the values we've found for later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 reply = rep->reply;
2570 if (expr_res != NULL)
2571 *expr_res = rep->expr_result == 1 ? 0 : -1;
2572
2573 if (remove)
2574 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002575 // Move the rest of the list down to fill the gap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 mch_memmove(rep, rep + 1,
2577 (REPLY_COUNT - i - 1) * sizeof(reply_T));
2578 --REPLY_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 }
2580
Bram Moolenaar0f873732019-12-05 20:28:46 +01002581 // Return the reply to the caller, who takes on responsibility
2582 // for freeing it if "remove" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 return reply;
2584 }
2585 }
2586
Bram Moolenaar0f873732019-12-05 20:28:46 +01002587 // If we got here, we didn't find a reply. Return immediately if the
2588 // "wait" parameter isn't set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 if (!wait)
Bram Moolenaar15e737f2017-03-18 21:22:47 +01002590 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002591 // Process pending messages once. Without this, looping on
2592 // remote_peek() would never get the reply.
Bram Moolenaar15e737f2017-03-18 21:22:47 +01002593 if (!did_process)
2594 {
2595 did_process = TRUE;
2596 serverProcessPendingMessages();
2597 continue;
2598 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 break;
Bram Moolenaar15e737f2017-03-18 21:22:47 +01002600 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601
Bram Moolenaar0f873732019-12-05 20:28:46 +01002602 // We need to wait for a reply. Enter a message loop until the
2603 // "reply_received" flag gets set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604
Bram Moolenaar0f873732019-12-05 20:28:46 +01002605 // Loop until we receive a reply
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 while (reply_received == 0)
2607 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002608# ifdef FEAT_TIMERS
Bram Moolenaar0f873732019-12-05 20:28:46 +01002609 // TODO: use the return value to decide how long to wait.
Bram Moolenaar42205552017-03-18 19:42:22 +01002610 check_due_timer();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002611# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002612 time(&now);
2613 if (timeout > 0 && (now - start) >= timeout)
2614 break;
2615
Bram Moolenaar0f873732019-12-05 20:28:46 +01002616 // Wait for a SendMessage() call to us. This could be the reply
2617 // we are waiting for. Use a timeout of a second, to catch the
2618 // situation that the server died unexpectedly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 MsgWaitForMultipleObjects(0, NULL, TRUE, 1000, QS_ALLINPUT);
2620
Bram Moolenaar0f873732019-12-05 20:28:46 +01002621 // If the server has died, give up
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 if (!IsWindow(server))
2623 return NULL;
2624
2625 serverProcessPendingMessages();
2626 }
2627 }
2628
2629 return NULL;
2630}
2631
2632/*
2633 * Process any messages in the Windows message queue.
2634 */
2635 void
2636serverProcessPendingMessages(void)
2637{
2638 MSG msg;
2639
K.Takatab7057bd2022-01-21 11:37:07 +00002640 while (PeekMessageW(&msg, NULL, 0, 0, PM_REMOVE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 {
2642 TranslateMessage(&msg);
K.Takatab7057bd2022-01-21 11:37:07 +00002643 DispatchMessageW(&msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 }
2645}
2646
Bram Moolenaar0f873732019-12-05 20:28:46 +01002647#endif // FEAT_CLIENTSERVER
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648
2649#if defined(FEAT_GUI) || (defined(FEAT_PRINTER) && !defined(FEAT_POSTSCRIPT)) \
2650 || defined(PROTO)
2651
2652struct charset_pair
2653{
2654 char *name;
2655 BYTE charset;
2656};
2657
2658static struct charset_pair
2659charset_pairs[] =
2660{
2661 {"ANSI", ANSI_CHARSET},
2662 {"CHINESEBIG5", CHINESEBIG5_CHARSET},
2663 {"DEFAULT", DEFAULT_CHARSET},
2664 {"HANGEUL", HANGEUL_CHARSET},
2665 {"OEM", OEM_CHARSET},
2666 {"SHIFTJIS", SHIFTJIS_CHARSET},
2667 {"SYMBOL", SYMBOL_CHARSET},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 {"ARABIC", ARABIC_CHARSET},
2669 {"BALTIC", BALTIC_CHARSET},
2670 {"EASTEUROPE", EASTEUROPE_CHARSET},
2671 {"GB2312", GB2312_CHARSET},
2672 {"GREEK", GREEK_CHARSET},
2673 {"HEBREW", HEBREW_CHARSET},
2674 {"JOHAB", JOHAB_CHARSET},
2675 {"MAC", MAC_CHARSET},
2676 {"RUSSIAN", RUSSIAN_CHARSET},
2677 {"THAI", THAI_CHARSET},
2678 {"TURKISH", TURKISH_CHARSET},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002679# ifdef VIETNAMESE_CHARSET
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 {"VIETNAMESE", VIETNAMESE_CHARSET},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002681# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 {NULL, 0}
2683};
2684
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002685struct quality_pair
2686{
2687 char *name;
2688 DWORD quality;
2689};
2690
2691static struct quality_pair
2692quality_pairs[] = {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002693# ifdef CLEARTYPE_QUALITY
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002694 {"CLEARTYPE", CLEARTYPE_QUALITY},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002695# endif
2696# ifdef ANTIALIASED_QUALITY
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002697 {"ANTIALIASED", ANTIALIASED_QUALITY},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002698# endif
2699# ifdef NONANTIALIASED_QUALITY
Bram Moolenaar73a733e2016-05-11 21:05:05 +02002700 {"NONANTIALIASED", NONANTIALIASED_QUALITY},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002701# endif
2702# ifdef PROOF_QUALITY
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002703 {"PROOF", PROOF_QUALITY},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002704# endif
2705# ifdef DRAFT_QUALITY
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002706 {"DRAFT", DRAFT_QUALITY},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002707# endif
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002708 {"DEFAULT", DEFAULT_QUALITY},
2709 {NULL, 0}
2710};
2711
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712/*
2713 * Convert a charset ID to a name.
2714 * Return NULL when not recognized.
2715 */
2716 char *
2717charset_id2name(int id)
2718{
2719 struct charset_pair *cp;
2720
2721 for (cp = charset_pairs; cp->name != NULL; ++cp)
2722 if ((BYTE)id == cp->charset)
2723 break;
2724 return cp->name;
2725}
2726
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002727/*
2728 * Convert a quality ID to a name.
2729 * Return NULL when not recognized.
2730 */
2731 char *
2732quality_id2name(DWORD id)
2733{
2734 struct quality_pair *qp;
2735
2736 for (qp = quality_pairs; qp->name != NULL; ++qp)
2737 if (id == qp->quality)
2738 break;
2739 return qp->name;
2740}
2741
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002742static const LOGFONTW s_lfDefault =
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743{
2744 -12, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, DEFAULT_CHARSET,
2745 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
2746 PROOF_QUALITY, FIXED_PITCH | FF_DONTCARE,
Bram Moolenaar0f873732019-12-05 20:28:46 +01002747 L"Fixedsys" // see _ReadVimIni
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748};
2749
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002750// Initialise the "current height" to -12 (same as s_lfDefault) just
2751// in case the user specifies a font in "guifont" with no size before a font
2752// with an explicit size has been set. This defaults the size to this value
2753// (-12 equates to roughly 9pt).
2754int current_font_height = -12; // also used in gui_w32.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755
Bram Moolenaar0f873732019-12-05 20:28:46 +01002756/*
2757 * Convert a string representing a point size into pixels. The string should
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 * be a positive decimal number, with an optional decimal point (eg, "12", or
2759 * "10.5"). The pixel value is returned, and a pointer to the next unconverted
2760 * character is stored in *end. The flag "vertical" says whether this
2761 * calculation is for a vertical (height) size or a horizontal (width) one.
2762 */
2763 static int
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002764points_to_pixels(WCHAR *str, WCHAR **end, int vertical, long_i pprinter_dc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765{
2766 int pixels;
2767 int points = 0;
2768 int divisor = 0;
2769 HWND hwnd = (HWND)0;
2770 HDC hdc;
2771 HDC printer_dc = (HDC)pprinter_dc;
2772
2773 while (*str != NUL)
2774 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002775 if (*str == L'.' && divisor == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002777 // Start keeping a divisor, for later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 divisor = 1;
2779 }
2780 else
2781 {
2782 if (!VIM_ISDIGIT(*str))
2783 break;
2784
2785 points *= 10;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002786 points += *str - L'0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787 divisor *= 10;
2788 }
2789 ++str;
2790 }
2791
2792 if (divisor == 0)
2793 divisor = 1;
2794
2795 if (printer_dc == NULL)
2796 {
2797 hwnd = GetDesktopWindow();
2798 hdc = GetWindowDC(hwnd);
2799 }
2800 else
2801 hdc = printer_dc;
2802
2803 pixels = MulDiv(points,
2804 GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX),
2805 72 * divisor);
2806
2807 if (printer_dc == NULL)
2808 ReleaseDC(hwnd, hdc);
2809
2810 *end = str;
2811 return pixels;
2812}
2813
2814 static int CALLBACK
2815font_enumproc(
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002816 ENUMLOGFONTW *elf,
2817 NEWTEXTMETRICW *ntm UNUSED,
2818 DWORD type UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 LPARAM lparam)
2820{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002821 // Return value:
2822 // 0 = terminate now (monospace & ANSI)
2823 // 1 = continue, still no luck...
2824 // 2 = continue, but we have an acceptable LOGFONTW
2825 // (monospace, not ANSI)
2826 // We use these values, as EnumFontFamilies returns 1 if the
2827 // callback function is never called. So, we check the return as
2828 // 0 = perfect, 2 = OK, 1 = no good...
2829 // It's not pretty, but it works!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002831 LOGFONTW *lf = (LOGFONTW *)(lparam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002833# ifndef FEAT_PROPORTIONAL_FONTS
Bram Moolenaar0f873732019-12-05 20:28:46 +01002834 // Ignore non-monospace fonts without further ado
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 if ((ntm->tmPitchAndFamily & 1) != 0)
2836 return 1;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002837# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838
Bram Moolenaar0f873732019-12-05 20:28:46 +01002839 // Remember this LOGFONTW as a "possible"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840 *lf = elf->elfLogFont;
2841
Bram Moolenaar0f873732019-12-05 20:28:46 +01002842 // Terminate the scan as soon as we find an ANSI font
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 if (lf->lfCharSet == ANSI_CHARSET
2844 || lf->lfCharSet == OEM_CHARSET
2845 || lf->lfCharSet == DEFAULT_CHARSET)
2846 return 0;
2847
Bram Moolenaar0f873732019-12-05 20:28:46 +01002848 // Continue the scan - we have a non-ANSI font
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 return 2;
2850}
2851
2852 static int
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002853init_logfont(LOGFONTW *lf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854{
2855 int n;
2856 HWND hwnd = GetDesktopWindow();
2857 HDC hdc = GetWindowDC(hwnd);
2858
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002859 n = EnumFontFamiliesW(hdc,
2860 lf->lfFaceName,
2861 (FONTENUMPROCW)font_enumproc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862 (LPARAM)lf);
2863
2864 ReleaseDC(hwnd, hdc);
2865
Bram Moolenaar0f873732019-12-05 20:28:46 +01002866 // If we couldn't find a usable font, return failure
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 if (n == 1)
2868 return FAIL;
2869
Bram Moolenaar0f873732019-12-05 20:28:46 +01002870 // Tidy up the rest of the LOGFONTW structure. We set to a basic
2871 // font - get_logfont() sets bold, italic, etc based on the user's
2872 // input.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873 lf->lfHeight = current_font_height;
2874 lf->lfWidth = 0;
2875 lf->lfItalic = FALSE;
2876 lf->lfUnderline = FALSE;
2877 lf->lfStrikeOut = FALSE;
2878 lf->lfWeight = FW_NORMAL;
2879
Bram Moolenaar0f873732019-12-05 20:28:46 +01002880 // Return success
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 return OK;
2882}
2883
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002884/*
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002885 * Compare a UTF-16 string and an ASCII string literally.
2886 * Only works all the code points are inside ASCII range.
2887 */
2888 static int
2889utf16ascncmp(const WCHAR *w, const char *p, size_t n)
2890{
2891 size_t i;
2892
2893 for (i = 0; i < n; i++)
2894 {
2895 if (w[i] == 0 || w[i] != p[i])
2896 return w[i] - p[i];
2897 }
2898 return 0;
2899}
2900
2901/*
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002902 * Get font info from "name" into logfont "lf".
2903 * Return OK for a valid name, FAIL otherwise.
2904 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 int
2906get_logfont(
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002907 LOGFONTW *lf,
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002908 char_u *name,
2909 HDC printer_dc,
2910 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002912 WCHAR *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 int i;
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002914 int ret = FAIL;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002915 static LOGFONTW *lastlf = NULL;
2916 WCHAR *wname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917
2918 *lf = s_lfDefault;
2919 if (name == NULL)
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002920 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002922 wname = enc_to_utf16(name, NULL);
2923 if (wname == NULL)
2924 return FAIL;
2925
2926 if (wcscmp(wname, L"*") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002928# if defined(FEAT_GUI_MSWIN)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002929 CHOOSEFONTW cf;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002930 // if name is "*", bring up std font dialog:
Bram Moolenaara80faa82020-04-12 19:37:17 +02002931 CLEAR_FIELD(cf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 cf.lStructSize = sizeof(cf);
2933 cf.hwndOwner = s_hwnd;
2934 cf.Flags = CF_SCREENFONTS | CF_FIXEDPITCHONLY | CF_INITTOLOGFONTSTRUCT;
2935 if (lastlf != NULL)
2936 *lf = *lastlf;
2937 cf.lpLogFont = lf;
2938 cf.nFontType = 0 ; //REGULAR_FONTTYPE;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002939 if (ChooseFontW(&cf))
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002940 ret = OK;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002941# endif
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002942 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 }
2944
2945 /*
2946 * Split name up, it could be <name>:h<height>:w<width> etc.
2947 */
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002948 for (p = wname; *p && *p != L':'; p++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002950 if (p - wname + 1 >= LF_FACESIZE)
Bram Moolenaar0f873732019-12-05 20:28:46 +01002951 goto theend; // Name too long
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002952 lf->lfFaceName[p - wname] = *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 }
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002954 if (p != wname)
2955 lf->lfFaceName[p - wname] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956
Bram Moolenaar0f873732019-12-05 20:28:46 +01002957 // First set defaults
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958 lf->lfHeight = -12;
2959 lf->lfWidth = 0;
2960 lf->lfWeight = FW_NORMAL;
2961 lf->lfItalic = FALSE;
2962 lf->lfUnderline = FALSE;
2963 lf->lfStrikeOut = FALSE;
2964
2965 /*
2966 * If the font can't be found, try replacing '_' by ' '.
2967 */
2968 if (init_logfont(lf) == FAIL)
2969 {
2970 int did_replace = FALSE;
2971
2972 for (i = 0; lf->lfFaceName[i]; ++i)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002973 if (lf->lfFaceName[i] == L'_')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002975 lf->lfFaceName[i] = L' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 did_replace = TRUE;
2977 }
2978 if (!did_replace || init_logfont(lf) == FAIL)
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002979 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 }
2981
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002982 while (*p == L':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 p++;
2984
Bram Moolenaar0f873732019-12-05 20:28:46 +01002985 // Set the values found after ':'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986 while (*p)
2987 {
2988 switch (*p++)
2989 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002990 case L'h':
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002991 lf->lfHeight = - points_to_pixels(p, &p, TRUE, (long_i)printer_dc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002993 case L'w':
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002994 lf->lfWidth = points_to_pixels(p, &p, FALSE, (long_i)printer_dc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 break;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02002996 case L'W':
2997 lf->lfWeight = wcstol(p, &p, 10);
2998 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002999 case L'b':
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000 lf->lfWeight = FW_BOLD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003002 case L'i':
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003 lf->lfItalic = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003005 case L'u':
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006 lf->lfUnderline = TRUE;
3007 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003008 case L's':
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009 lf->lfStrikeOut = TRUE;
3010 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003011 case L'c':
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 {
3013 struct charset_pair *cp;
3014
3015 for (cp = charset_pairs; cp->name != NULL; ++cp)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003016 if (utf16ascncmp(p, cp->name, strlen(cp->name)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017 {
3018 lf->lfCharSet = cp->charset;
3019 p += strlen(cp->name);
3020 break;
3021 }
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003022 if (cp->name == NULL && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003024 char_u *s = utf16_to_enc(p, NULL);
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00003025
3026 semsg(_(e_illegal_str_name_str_in_font_name_str),
3027 "charset", s, name);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003028 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029 break;
3030 }
3031 break;
3032 }
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003033 case L'q':
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003034 {
3035 struct quality_pair *qp;
3036
3037 for (qp = quality_pairs; qp->name != NULL; ++qp)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003038 if (utf16ascncmp(p, qp->name, strlen(qp->name)) == 0)
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003039 {
3040 lf->lfQuality = qp->quality;
3041 p += strlen(qp->name);
3042 break;
3043 }
3044 if (qp->name == NULL && verbose)
3045 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003046 char_u *s = utf16_to_enc(p, NULL);
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00003047 semsg(_(e_illegal_str_name_str_in_font_name_str),
3048 "quality", s, name);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003049 vim_free(s);
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003050 break;
3051 }
3052 break;
3053 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054 default:
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003055 if (verbose)
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00003056 semsg(_(e_illegal_char_nr_in_font_name_str), p[-1], name);
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003057 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058 }
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003059 while (*p == L':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 p++;
3061 }
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003062 ret = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064theend:
Bram Moolenaar0f873732019-12-05 20:28:46 +01003065 // ron: init lastlf
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003066 if (ret == OK && printer_dc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067 {
3068 vim_free(lastlf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003069 lastlf = ALLOC_ONE(LOGFONTW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 if (lastlf != NULL)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003071 mch_memmove(lastlf, lf, sizeof(LOGFONTW));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 }
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003073 vim_free(wname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003075 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076}
3077
Bram Moolenaar0f873732019-12-05 20:28:46 +01003078#endif // defined(FEAT_GUI) || defined(FEAT_PRINTER)
Bram Moolenaarf12d9832016-01-29 21:11:25 +01003079
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003080#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaarf12d9832016-01-29 21:11:25 +01003081/*
3082 * Initialize the Winsock dll.
3083 */
3084 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003085channel_init_winsock(void)
Bram Moolenaarf12d9832016-01-29 21:11:25 +01003086{
3087 WSADATA wsaData;
3088 int wsaerr;
3089
3090 if (WSInitialized)
3091 return;
3092
3093 wsaerr = WSAStartup(MAKEWORD(2, 2), &wsaData);
3094 if (wsaerr == 0)
3095 WSInitialized = TRUE;
3096}
3097#endif