blob: f4fc0894d9a702e013c3f7a7fd17ee745282eecf [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
Bram Moolenaar0f873732019-12-05 20:28:46 +0100432// Use 64-bit stat functions if available.
Bram Moolenaar8767f522016-07-01 17:17:39 +0200433#ifdef HAVE_STAT64
434# undef stat
435# undef _stat
436# undef _wstat
437# undef _fstat
438# define stat _stat64
439# define _stat _stat64
440# define _wstat _wstat64
441# define _fstat _fstat64
442#endif
443
Bram Moolenaar9d1685d2014-01-14 12:18:45 +0100444#if (defined(_MSC_VER) && (_MSC_VER >= 1300)) || defined(__MINGW32__)
Bram Moolenaar8962fda2013-09-29 19:05:21 +0200445# define OPEN_OH_ARGTYPE intptr_t
446#else
447# define OPEN_OH_ARGTYPE long
448#endif
449
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200450 static int
Bram Moolenaar8767f522016-07-01 17:17:39 +0200451wstat_symlink_aware(const WCHAR *name, stat_T *stp)
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200452{
Bram Moolenaara12a1612019-01-24 16:39:02 +0100453#if (defined(_MSC_VER) && (_MSC_VER < 1900)) || defined(__MINGW32__)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100454 // Work around for VC12 or earlier (and MinGW). _wstat() can't handle
455 // symlinks properly.
456 // VC9 or earlier: _wstat() doesn't support a symlink at all. It retrieves
457 // status of a symlink itself.
458 // VC10: _wstat() supports a symlink to a normal file, but it doesn't
459 // support a symlink to a directory (always returns an error).
460 // VC11 and VC12: _wstat() doesn't return an error for a symlink to a
461 // directory, but it doesn't set S_IFDIR flag.
462 // MinGW: Same as VC9.
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200463 int n;
464 BOOL is_symlink = FALSE;
465 HANDLE hFind, h;
466 DWORD attr = 0;
467 WIN32_FIND_DATAW findDataW;
468
469 hFind = FindFirstFileW(name, &findDataW);
470 if (hFind != INVALID_HANDLE_VALUE)
471 {
472 attr = findDataW.dwFileAttributes;
473 if ((attr & FILE_ATTRIBUTE_REPARSE_POINT)
474 && (findDataW.dwReserved0 == IO_REPARSE_TAG_SYMLINK))
475 is_symlink = TRUE;
476 FindClose(hFind);
477 }
478 if (is_symlink)
479 {
480 h = CreateFileW(name, FILE_READ_ATTRIBUTES,
481 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
482 OPEN_EXISTING,
483 (attr & FILE_ATTRIBUTE_DIRECTORY)
484 ? FILE_FLAG_BACKUP_SEMANTICS : 0,
485 NULL);
486 if (h != INVALID_HANDLE_VALUE)
487 {
488 int fd;
489
Bram Moolenaar8962fda2013-09-29 19:05:21 +0200490 fd = _open_osfhandle((OPEN_OH_ARGTYPE)h, _O_RDONLY);
Bram Moolenaar8767f522016-07-01 17:17:39 +0200491 n = _fstat(fd, (struct _stat *)stp);
Bram Moolenaarfce7b3d2016-01-19 19:00:32 +0100492 if ((n == 0) && (attr & FILE_ATTRIBUTE_DIRECTORY))
493 stp->st_mode = (stp->st_mode & ~S_IFREG) | S_IFDIR;
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200494 _close(fd);
495 return n;
496 }
497 }
Bram Moolenaara12a1612019-01-24 16:39:02 +0100498#endif
Bram Moolenaar8767f522016-07-01 17:17:39 +0200499 return _wstat(name, (struct _stat *)stp);
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200500}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501
502/*
503 * stat() can't handle a trailing '/' or '\', remove it first.
504 */
505 int
Bram Moolenaar8767f522016-07-01 17:17:39 +0200506vim_stat(const char *name, stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100508 // WinNT and later can use _MAX_PATH wide characters for a pathname, which
509 // means that the maximum pathname is _MAX_PATH * 3 bytes when 'enc' is
510 // UTF-8.
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100511 char_u buf[_MAX_PATH * 3 + 1];
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100512 char_u *p;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200513 WCHAR *wp;
514 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515
Bram Moolenaard2a203b2013-08-30 16:51:18 +0200516 vim_strncpy((char_u *)buf, (char_u *)name, sizeof(buf) - 1);
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100517 p = buf + STRLEN(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518 if (p > buf)
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100519 MB_PTR_BACK(buf, p);
Bram Moolenaarb1cb35f2014-01-10 13:05:20 +0100520
Bram Moolenaar0f873732019-12-05 20:28:46 +0100521 // Remove trailing '\\' except root path.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522 if (p > buf && (*p == '\\' || *p == '/') && p[-1] != ':')
523 *p = NUL;
Bram Moolenaarb1cb35f2014-01-10 13:05:20 +0100524
525 if ((buf[0] == '\\' && buf[1] == '\\') || (buf[0] == '/' && buf[1] == '/'))
526 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100527 // UNC root path must be followed by '\\'.
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100528 p = vim_strpbrk(buf + 2, (char_u *)"\\/");
Bram Moolenaarb1cb35f2014-01-10 13:05:20 +0100529 if (p != NULL)
530 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100531 p = vim_strpbrk(p + 1, (char_u *)"\\/");
Bram Moolenaarb1cb35f2014-01-10 13:05:20 +0100532 if (p == NULL)
533 STRCAT(buf, "\\");
534 }
535 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200537 wp = enc_to_utf16(buf, NULL);
538 if (wp == NULL)
539 return -1;
540
541 n = wstat_symlink_aware(wp, stp);
542 vim_free(wp);
543 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544}
545
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200546#if (defined(FEAT_GUI_MSWIN) && !defined(VIMDLL)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 void
Bram Moolenaar26e86442020-05-17 14:06:16 +0200548mch_settmode(tmode_T tmode UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100550 // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551}
552
553 int
554mch_get_shellsize(void)
555{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100556 // never used
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557 return OK;
558}
559
560 void
561mch_set_shellsize(void)
562{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100563 // never used
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564}
565
566/*
567 * Rows and/or Columns has changed.
568 */
569 void
570mch_new_shellsize(void)
571{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100572 // never used
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573}
574
575#endif
576
577/*
578 * We have no job control, so fake it by starting a new shell.
579 */
580 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100581mch_suspend(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582{
583 suspend_shell();
584}
585
586#if defined(USE_MCH_ERRMSG) || defined(PROTO)
587
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200588# ifdef display_errors
589# undef display_errors
590# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591
592/*
593 * Display the saved error message(s).
594 */
595 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100596display_errors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597{
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200598# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599 char *p;
600
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200601# ifdef VIMDLL
602 if (gui.in_use || gui.starting)
603# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200605 if (error_ga.ga_data != NULL)
606 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100607 // avoid putting up a message box with blanks only
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200608 for (p = (char *)error_ga.ga_data; *p; ++p)
609 if (!isspace(*p))
610 {
611 (void)gui_mch_dialog(
Bram Moolenaarc17ef8e2006-03-25 21:48:58 +0000612 gui.starting ? VIM_INFO :
Bram Moolenaarc17ef8e2006-03-25 21:48:58 +0000613 VIM_ERROR,
Bram Moolenaarc17ef8e2006-03-25 21:48:58 +0000614 gui.starting ? (char_u *)_("Message") :
Bram Moolenaarc17ef8e2006-03-25 21:48:58 +0000615 (char_u *)_("Error"),
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100616 (char_u *)p, (char_u *)_("&Ok"),
617 1, NULL, FALSE);
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200618 break;
619 }
620 ga_clear(&error_ga);
621 }
622 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200624# endif
625# if !defined(FEAT_GUI) || defined(VIMDLL)
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +0100626 FlushFileBuffers(GetStdHandle(STD_ERROR_HANDLE));
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200627# endif
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +0100628}
629#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630
631
632/*
633 * Return TRUE if "p" contain a wildcard that can be expanded by
634 * dos_expandpath().
635 */
636 int
637mch_has_exp_wildcard(char_u *p)
638{
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100639 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 {
641 if (vim_strchr((char_u *)"?*[", *p) != NULL
642 || (*p == '~' && p[1] != NUL))
643 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 }
645 return FALSE;
646}
647
648/*
649 * Return TRUE if "p" contain a wildcard or a "~1" kind of thing (could be a
650 * shortened file name).
651 */
652 int
653mch_has_wildcard(char_u *p)
654{
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100655 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 {
657 if (vim_strchr((char_u *)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100658#ifdef VIM_BACKTICK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 "?*$[`"
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100660#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 "?*$["
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100662#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 , *p) != NULL
664 || (*p == '~' && p[1] != NUL))
665 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 }
667 return FALSE;
668}
669
670
671/*
672 * The normal _chdir() does not change the default drive. This one does.
673 * Returning 0 implies success; -1 implies failure.
674 */
675 int
676mch_chdir(char *path)
677{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200678 WCHAR *p;
679 int n;
680
Bram Moolenaar0f873732019-12-05 20:28:46 +0100681 if (path[0] == NUL) // just checking...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 return -1;
683
Bram Moolenaara2974d72009-07-14 16:38:36 +0000684 if (p_verbose >= 5)
685 {
686 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100687 smsg("chdir(%s)", path);
Bram Moolenaara2974d72009-07-14 16:38:36 +0000688 verbose_leave();
689 }
Bram Moolenaar0f873732019-12-05 20:28:46 +0100690 if (isalpha(path[0]) && path[1] == ':') // has a drive name
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100692 // If we can change to the drive, skip that part of the path. If we
693 // can't then the current directory may be invalid, try using chdir()
694 // with the whole path.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695 if (_chdrive(TOLOWER_ASC(path[0]) - 'a' + 1) == 0)
696 path += 2;
697 }
698
Bram Moolenaar0f873732019-12-05 20:28:46 +0100699 if (*path == NUL) // drive name only
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 return 0;
701
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200702 p = enc_to_utf16((char_u *)path, NULL);
703 if (p == NULL)
704 return -1;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000705
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200706 n = _wchdir(p);
707 vim_free(p);
708 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709}
710
711
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200712#if defined(FEAT_GUI_MSWIN) && !defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713/*
714 * return non-zero if a character is available
715 */
716 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100717mch_char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100719 // never used
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 return TRUE;
721}
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200722
723# if defined(FEAT_TERMINAL) || defined(PROTO)
724/*
725 * Check for any pending input or messages.
726 */
727 int
728mch_check_messages(void)
729{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100730 // TODO: check for messages
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200731 return TRUE;
732}
733# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734#endif
735
736
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737#if defined(FEAT_LIBCALL) || defined(PROTO)
738/*
739 * Call a DLL routine which takes either a string or int param
740 * and returns an allocated string.
741 * Return OK if it worked, FAIL if not.
742 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743typedef LPTSTR (*MYSTRPROCSTR)(LPTSTR);
744typedef LPTSTR (*MYINTPROCSTR)(int);
745typedef int (*MYSTRPROCINT)(LPTSTR);
746typedef int (*MYINTPROCINT)(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748/*
749 * Check if a pointer points to a valid NUL terminated string.
750 * Return the length of the string, including terminating NUL.
751 * Returns 0 for an invalid pointer, 1 for an empty string.
752 */
753 static size_t
754check_str_len(char_u *str)
755{
756 SYSTEM_INFO si;
757 MEMORY_BASIC_INFORMATION mbi;
758 size_t length = 0;
759 size_t i;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100760 const char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761
Bram Moolenaar0f873732019-12-05 20:28:46 +0100762 // get page size
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763 GetSystemInfo(&si);
764
Bram Moolenaar0f873732019-12-05 20:28:46 +0100765 // get memory information
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 if (VirtualQuery(str, &mbi, sizeof(mbi)))
767 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100768 // pre cast these (typing savers)
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000769 long_u dwStr = (long_u)str;
770 long_u dwBaseAddress = (long_u)mbi.BaseAddress;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771
Bram Moolenaar0f873732019-12-05 20:28:46 +0100772 // get start address of page that str is on
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000773 long_u strPage = dwStr - (dwStr - dwBaseAddress) % si.dwPageSize;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774
Bram Moolenaar0f873732019-12-05 20:28:46 +0100775 // get length from str to end of page
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000776 long_u pageLength = si.dwPageSize - (dwStr - strPage);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777
Bram Moolenaar442b4222010-05-24 21:34:22 +0200778 for (p = str; !IsBadReadPtr(p, (UINT)pageLength);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 p += pageLength, pageLength = si.dwPageSize)
780 for (i = 0; i < pageLength; ++i, ++length)
781 if (p[i] == NUL)
782 return length + 1;
783 }
784
785 return 0;
786}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200788/*
789 * Passed to do_in_runtimepath() to load a vim.ico file.
790 */
791 static void
792mch_icon_load_cb(char_u *fname, void *cookie)
793{
794 HANDLE *h = (HANDLE *)cookie;
795
796 *h = LoadImage(NULL,
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100797 (LPSTR)fname,
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200798 IMAGE_ICON,
799 64,
800 64,
801 LR_LOADFROMFILE | LR_LOADMAP3DCOLORS);
802}
803
804/*
805 * Try loading an icon file from 'runtimepath'.
806 */
807 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100808mch_icon_load(HANDLE *iconp)
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200809{
810 return do_in_runtimepath((char_u *)"bitmaps/vim.ico",
Bram Moolenaar7f8989d2016-03-12 22:11:39 +0100811 0, mch_icon_load_cb, iconp);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200812}
813
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 int
815mch_libcall(
816 char_u *libname,
817 char_u *funcname,
Bram Moolenaar0f873732019-12-05 20:28:46 +0100818 char_u *argstring, // NULL when using a argint
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 int argint,
Bram Moolenaar0f873732019-12-05 20:28:46 +0100820 char_u **string_result,// NULL when using number_result
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 int *number_result)
822{
823 HINSTANCE hinstLib;
824 MYSTRPROCSTR ProcAdd;
825 MYINTPROCSTR ProcAddI;
826 char_u *retval_str = NULL;
827 int retval_int = 0;
828 size_t len;
829
830 BOOL fRunTimeLinkSuccess = FALSE;
831
832 // Get a handle to the DLL module.
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100833 hinstLib = vimLoadLib((char *)libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834
835 // If the handle is valid, try to get the function address.
836 if (hinstLib != NULL)
837 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100838# ifdef HAVE_TRY_EXCEPT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 __try
840 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100841# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 if (argstring != NULL)
843 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100844 // Call with string argument
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100845 ProcAdd = (MYSTRPROCSTR)GetProcAddress(hinstLib, (LPCSTR)funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 if ((fRunTimeLinkSuccess = (ProcAdd != NULL)) != 0)
847 {
848 if (string_result == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100849 retval_int = ((MYSTRPROCINT)ProcAdd)((LPSTR)argstring);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100851 retval_str = (char_u *)(ProcAdd)((LPSTR)argstring);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 }
853 }
854 else
855 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100856 // Call with number argument
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100857 ProcAddI = (MYINTPROCSTR) GetProcAddress(hinstLib, (LPCSTR)funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 if ((fRunTimeLinkSuccess = (ProcAddI != NULL)) != 0)
859 {
860 if (string_result == NULL)
861 retval_int = ((MYINTPROCINT)ProcAddI)(argint);
862 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100863 retval_str = (char_u *)(ProcAddI)(argint);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 }
865 }
866
867 // Save the string before we free the library.
868 // Assume that a "1" result is an illegal pointer.
869 if (string_result == NULL)
870 *number_result = retval_int;
871 else if (retval_str != NULL
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100872 && (len = check_str_len(retval_str)) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 {
Bram Moolenaar18a4ba22019-05-24 19:39:03 +0200874 *string_result = alloc(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 if (*string_result != NULL)
876 mch_memmove(*string_result, retval_str, len);
877 }
878
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100879# ifdef HAVE_TRY_EXCEPT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 }
881 __except(EXCEPTION_EXECUTE_HANDLER)
882 {
883 if (GetExceptionCode() == EXCEPTION_STACK_OVERFLOW)
884 RESETSTKOFLW();
885 fRunTimeLinkSuccess = 0;
886 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100887# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888
889 // Free the DLL module.
890 (void)FreeLibrary(hinstLib);
891 }
892
893 if (!fRunTimeLinkSuccess)
894 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000895 semsg(_(e_library_call_failed_for_str), funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 return FAIL;
897 }
898
899 return OK;
900}
901#endif
902
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903/*
904 * Debugging helper: expose the MCH_WRITE_DUMP stuff to other modules
905 */
906 void
Bram Moolenaar1266d672017-02-01 13:43:36 +0100907DumpPutS(const char *psz UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100909#ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910 if (fdDump)
911 {
912 fputs(psz, fdDump);
913 if (psz[strlen(psz) - 1] != '\n')
914 fputc('\n', fdDump);
915 fflush(fdDump);
916 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100917#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918}
919
920#ifdef _DEBUG
921
922void __cdecl
923Trace(
924 char *pszFormat,
925 ...)
926{
927 CHAR szBuff[2048];
928 va_list args;
929
930 va_start(args, pszFormat);
931 vsprintf(szBuff, pszFormat, args);
932 va_end(args);
933
934 OutputDebugString(szBuff);
935}
936
937#endif //_DEBUG
938
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200939#if !defined(FEAT_GUI) || defined(VIMDLL) || defined(PROTO)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100940extern HWND g_hWnd; // This is in os_win32.c.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941
942/*
943 * Showing the printer dialog is tricky since we have no GUI
944 * window to parent it. The following routines are needed to
945 * get the window parenting and Z-order to work properly.
946 */
947 static void
948GetConsoleHwnd(void)
949{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100950 // Skip if it's already set.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 if (s_hwnd != 0)
952 return;
953
Bram Moolenaar0f873732019-12-05 20:28:46 +0100954 // Window handle may have been found by init code (Windows NT only)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 if (g_hWnd != 0)
956 {
957 s_hwnd = g_hWnd;
958 return;
959 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960
Bram Moolenaare1ed53f2019-02-12 23:12:37 +0100961 s_hwnd = GetConsoleWindow();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962}
Bram Moolenaar843ee412004-06-30 16:16:41 +0000963
964/*
965 * Console implementation of ":winpos".
966 */
967 int
968mch_get_winpos(int *x, int *y)
969{
970 RECT rect;
971
972 GetConsoleHwnd();
973 GetWindowRect(s_hwnd, &rect);
974 *x = rect.left;
975 *y = rect.top;
976 return OK;
977}
978
979/*
980 * Console implementation of ":winpos x y".
981 */
982 void
983mch_set_winpos(int x, int y)
984{
985 GetConsoleHwnd();
986 SetWindowPos(s_hwnd, NULL, x, y, 0, 0,
987 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
988}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989#endif
990
991#if (defined(FEAT_PRINTER) && !defined(FEAT_POSTSCRIPT)) || defined(PROTO)
992
Bram Moolenaar0f873732019-12-05 20:28:46 +0100993//=================================================================
994// Win32 printer stuff
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995
996static HFONT prt_font_handles[2][2][2];
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200997static PRINTDLGW prt_dlg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998static const int boldface[2] = {FW_REGULAR, FW_BOLD};
999static TEXTMETRIC prt_tm;
1000static int prt_line_height;
1001static int prt_number_width;
1002static int prt_left_margin;
1003static int prt_right_margin;
1004static int prt_top_margin;
1005static char_u szAppName[] = TEXT("VIM");
1006static HWND hDlgPrint;
1007static int *bUserAbort = NULL;
1008static char_u *prt_name = NULL;
1009
Bram Moolenaar0f873732019-12-05 20:28:46 +01001010// Defines which are also in vim.rc.
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001011# define IDC_BOX1 400
1012# define IDC_PRINTTEXT1 401
1013# define IDC_PRINTTEXT2 402
1014# define IDC_PROGRESS 403
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001016 static BOOL
1017vimSetDlgItemText(HWND hDlg, int nIDDlgItem, char_u *s)
1018{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001019 WCHAR *wp;
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001020 BOOL ret;
1021
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001022 wp = enc_to_utf16(s, NULL);
1023 if (wp == NULL)
1024 return FALSE;
1025
1026 ret = SetDlgItemTextW(hDlg, nIDDlgItem, wp);
1027 vim_free(wp);
1028 return ret;
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001029}
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001030
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031/*
1032 * Convert BGR to RGB for Windows GDI calls
1033 */
1034 static COLORREF
1035swap_me(COLORREF colorref)
1036{
1037 int temp;
1038 char *ptr = (char *)&colorref;
1039
1040 temp = *(ptr);
1041 *(ptr ) = *(ptr + 2);
1042 *(ptr + 2) = temp;
1043 return colorref;
1044}
1045
Bram Moolenaar0f873732019-12-05 20:28:46 +01001046// Attempt to make this work for old and new compilers
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001047# if !defined(_WIN64) && (!defined(_MSC_VER) || _MSC_VER < 1300)
1048# define PDP_RETVAL BOOL
1049# else
1050# define PDP_RETVAL INT_PTR
1051# endif
Bram Moolenaared39e1d2008-08-09 17:55:22 +00001052
Bram Moolenaared39e1d2008-08-09 17:55:22 +00001053 static PDP_RETVAL CALLBACK
Bram Moolenaar1266d672017-02-01 13:43:36 +01001054PrintDlgProc(
1055 HWND hDlg,
1056 UINT message,
1057 WPARAM wParam UNUSED,
1058 LPARAM lParam UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001060# ifdef FEAT_GETTEXT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 NONCLIENTMETRICS nm;
1062 static HFONT hfont;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001063# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064
1065 switch (message)
1066 {
1067 case WM_INITDIALOG:
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001068# ifdef FEAT_GETTEXT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 nm.cbSize = sizeof(NONCLIENTMETRICS);
1070 if (SystemParametersInfo(
1071 SPI_GETNONCLIENTMETRICS,
1072 sizeof(NONCLIENTMETRICS),
1073 &nm,
1074 0))
1075 {
1076 char buff[MAX_PATH];
1077 int i;
1078
Bram Moolenaar0f873732019-12-05 20:28:46 +01001079 // Translate the dialog texts
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 hfont = CreateFontIndirect(&nm.lfMessageFont);
1081 for (i = IDC_PRINTTEXT1; i <= IDC_PROGRESS; i++)
1082 {
1083 SendDlgItemMessage(hDlg, i, WM_SETFONT, (WPARAM)hfont, 1);
1084 if (GetDlgItemText(hDlg,i, buff, sizeof(buff)))
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001085 vimSetDlgItemText(hDlg,i, (char_u *)_(buff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 }
1087 SendDlgItemMessage(hDlg, IDCANCEL,
1088 WM_SETFONT, (WPARAM)hfont, 1);
1089 if (GetDlgItemText(hDlg,IDCANCEL, buff, sizeof(buff)))
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001090 vimSetDlgItemText(hDlg,IDCANCEL, (char_u *)_(buff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001092# endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001093 SetWindowText(hDlg, (LPCSTR)szAppName);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 if (prt_name != NULL)
1095 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001096 vimSetDlgItemText(hDlg, IDC_PRINTTEXT2, (char_u *)prt_name);
Bram Moolenaard23a8232018-02-10 18:45:26 +01001097 VIM_CLEAR(prt_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 }
1099 EnableMenuItem(GetSystemMenu(hDlg, FALSE), SC_CLOSE, MF_GRAYED);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001100# if !defined(FEAT_GUI) || defined(VIMDLL)
1101# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001102 if (!gui.in_use)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001103# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001104 BringWindowToTop(s_hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001105# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 return TRUE;
1107
1108 case WM_COMMAND:
1109 *bUserAbort = TRUE;
1110 EnableWindow(GetParent(hDlg), TRUE);
1111 DestroyWindow(hDlg);
1112 hDlgPrint = NULL;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001113# ifdef FEAT_GETTEXT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 DeleteObject(hfont);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001115# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 return TRUE;
1117 }
1118 return FALSE;
1119}
1120
1121 static BOOL CALLBACK
Bram Moolenaar1266d672017-02-01 13:43:36 +01001122AbortProc(HDC hdcPrn UNUSED, int iCode UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123{
1124 MSG msg;
1125
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02001126 while (!*bUserAbort && pPeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 {
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02001128 if (!hDlgPrint || !pIsDialogMessage(hDlgPrint, &msg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 {
1130 TranslateMessage(&msg);
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02001131 pDispatchMessage(&msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 }
1133 }
1134 return !*bUserAbort;
1135}
1136
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001137# if !defined(FEAT_GUI) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138
Bram Moolenaar26fdd7d2011-11-30 13:42:44 +01001139 static UINT_PTR CALLBACK
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140PrintHookProc(
1141 HWND hDlg, // handle to dialog box
1142 UINT uiMsg, // message identifier
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02001143 WPARAM wParam UNUSED, // message parameter
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 LPARAM lParam // message parameter
1145 )
1146{
1147 HWND hwndOwner;
1148 RECT rc, rcDlg, rcOwner;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001149 PRINTDLGW *pPD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150
1151 if (uiMsg == WM_INITDIALOG)
1152 {
1153 // Get the owner window and dialog box rectangles.
1154 if ((hwndOwner = GetParent(hDlg)) == NULL)
1155 hwndOwner = GetDesktopWindow();
1156
1157 GetWindowRect(hwndOwner, &rcOwner);
1158 GetWindowRect(hDlg, &rcDlg);
1159 CopyRect(&rc, &rcOwner);
1160
1161 // Offset the owner and dialog box rectangles so that
1162 // right and bottom values represent the width and
1163 // height, and then offset the owner again to discard
1164 // space taken up by the dialog box.
1165
1166 OffsetRect(&rcDlg, -rcDlg.left, -rcDlg.top);
1167 OffsetRect(&rc, -rc.left, -rc.top);
1168 OffsetRect(&rc, -rcDlg.right, -rcDlg.bottom);
1169
1170 // The new position is the sum of half the remaining
1171 // space and the owner's original position.
1172
1173 SetWindowPos(hDlg,
1174 HWND_TOP,
1175 rcOwner.left + (rc.right / 2),
1176 rcOwner.top + (rc.bottom / 2),
1177 0, 0, // ignores size arguments
1178 SWP_NOSIZE);
1179
Bram Moolenaar0f873732019-12-05 20:28:46 +01001180 // tackle the printdlg copiesctrl problem
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001181 pPD = (PRINTDLGW *)lParam;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 pPD->nCopies = (WORD)pPD->lCustData;
1183 SetDlgItemInt( hDlg, edt3, pPD->nCopies, FALSE );
Bram Moolenaar0f873732019-12-05 20:28:46 +01001184 // Bring the window to top
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 BringWindowToTop(GetParent(hDlg));
1186 SetForegroundWindow(hDlg);
1187 }
1188
1189 return FALSE;
1190}
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001191# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192
1193 void
1194mch_print_cleanup(void)
1195{
1196 int pifItalic;
1197 int pifBold;
1198 int pifUnderline;
1199
1200 for (pifBold = 0; pifBold <= 1; pifBold++)
1201 for (pifItalic = 0; pifItalic <= 1; pifItalic++)
1202 for (pifUnderline = 0; pifUnderline <= 1; pifUnderline++)
1203 DeleteObject(prt_font_handles[pifBold][pifItalic][pifUnderline]);
1204
1205 if (prt_dlg.hDC != NULL)
1206 DeleteDC(prt_dlg.hDC);
1207 if (!*bUserAbort)
1208 SendMessage(hDlgPrint, WM_COMMAND, 0, 0);
1209}
1210
1211 static int
1212to_device_units(int idx, int dpi, int physsize, int offset, int def_number)
1213{
1214 int ret = 0;
1215 int u;
1216 int nr;
1217
1218 u = prt_get_unit(idx);
1219 if (u == PRT_UNIT_NONE)
1220 {
1221 u = PRT_UNIT_PERC;
1222 nr = def_number;
1223 }
1224 else
1225 nr = printer_opts[idx].number;
1226
1227 switch (u)
1228 {
1229 case PRT_UNIT_PERC:
1230 ret = (physsize * nr) / 100;
1231 break;
1232 case PRT_UNIT_INCH:
1233 ret = (nr * dpi);
1234 break;
1235 case PRT_UNIT_MM:
1236 ret = (nr * 10 * dpi) / 254;
1237 break;
1238 case PRT_UNIT_POINT:
1239 ret = (nr * 10 * dpi) / 720;
1240 break;
1241 }
1242
1243 if (ret < offset)
1244 return 0;
1245 else
1246 return ret - offset;
1247}
1248
1249 static int
1250prt_get_cpl(void)
1251{
1252 int hr;
1253 int phyw;
1254 int dvoff;
1255 int rev_offset;
1256 int dpi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257
1258 GetTextMetrics(prt_dlg.hDC, &prt_tm);
1259 prt_line_height = prt_tm.tmHeight + prt_tm.tmExternalLeading;
1260
1261 hr = GetDeviceCaps(prt_dlg.hDC, HORZRES);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 phyw = GetDeviceCaps(prt_dlg.hDC, PHYSICALWIDTH);
1263 dvoff = GetDeviceCaps(prt_dlg.hDC, PHYSICALOFFSETX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 dpi = GetDeviceCaps(prt_dlg.hDC, LOGPIXELSX);
1265
1266 rev_offset = phyw - (dvoff + hr);
1267
1268 prt_left_margin = to_device_units(OPT_PRINT_LEFT, dpi, phyw, dvoff, 10);
1269 if (prt_use_number())
1270 {
1271 prt_number_width = PRINT_NUMBER_WIDTH * prt_tm.tmAveCharWidth;
1272 prt_left_margin += prt_number_width;
1273 }
1274 else
1275 prt_number_width = 0;
1276
1277 prt_right_margin = hr - to_device_units(OPT_PRINT_RIGHT, dpi, phyw,
1278 rev_offset, 5);
1279
1280 return (prt_right_margin - prt_left_margin) / prt_tm.tmAveCharWidth;
1281}
1282
1283 static int
1284prt_get_lpp(void)
1285{
1286 int vr;
1287 int phyw;
1288 int dvoff;
1289 int rev_offset;
1290 int bottom_margin;
1291 int dpi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292
1293 vr = GetDeviceCaps(prt_dlg.hDC, VERTRES);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 phyw = GetDeviceCaps(prt_dlg.hDC, PHYSICALHEIGHT);
1295 dvoff = GetDeviceCaps(prt_dlg.hDC, PHYSICALOFFSETY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 dpi = GetDeviceCaps(prt_dlg.hDC, LOGPIXELSY);
1297
1298 rev_offset = phyw - (dvoff + vr);
1299
1300 prt_top_margin = to_device_units(OPT_PRINT_TOP, dpi, phyw, dvoff, 5);
1301
Bram Moolenaar0f873732019-12-05 20:28:46 +01001302 // adjust top margin if there is a header
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 prt_top_margin += prt_line_height * prt_header_height();
1304
1305 bottom_margin = vr - to_device_units(OPT_PRINT_BOT, dpi, phyw,
1306 rev_offset, 5);
1307
1308 return (bottom_margin - prt_top_margin) / prt_line_height;
1309}
1310
1311 int
1312mch_print_init(prt_settings_T *psettings, char_u *jobname, int forceit)
1313{
1314 static HGLOBAL stored_dm = NULL;
1315 static HGLOBAL stored_devn = NULL;
1316 static int stored_nCopies = 1;
1317 static int stored_nFlags = 0;
1318
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001319 LOGFONTW fLogFont;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 int pifItalic;
1321 int pifBold;
1322 int pifUnderline;
1323
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001324 DEVMODEW *mem;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 DEVNAMES *devname;
1326 int i;
1327
1328 bUserAbort = &(psettings->user_abort);
Bram Moolenaara80faa82020-04-12 19:37:17 +02001329 CLEAR_FIELD(prt_dlg);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001330 prt_dlg.lStructSize = sizeof(PRINTDLGW);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001331# if !defined(FEAT_GUI) || defined(VIMDLL)
1332# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001333 if (!gui.in_use)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001334# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01001335 GetConsoleHwnd(); // get value of s_hwnd
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001336# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 prt_dlg.hwndOwner = s_hwnd;
1338 prt_dlg.Flags = PD_NOPAGENUMS | PD_NOSELECTION | PD_RETURNDC;
1339 if (!forceit)
1340 {
1341 prt_dlg.hDevMode = stored_dm;
1342 prt_dlg.hDevNames = stored_devn;
1343 prt_dlg.lCustData = stored_nCopies; // work around bug in print dialog
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001344# if !defined(FEAT_GUI) || defined(VIMDLL)
1345# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001346 if (!gui.in_use)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001347# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001348 {
1349 /*
1350 * Use hook to prevent console window being sent to back
1351 */
1352 prt_dlg.lpfnPrintHook = PrintHookProc;
1353 prt_dlg.Flags |= PD_ENABLEPRINTHOOK;
1354 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001355# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 prt_dlg.Flags |= stored_nFlags;
1357 }
1358
1359 /*
1360 * If bang present, return default printer setup with no dialog
1361 * never show dialog if we are running over telnet
1362 */
1363 if (forceit
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001364# if !defined(FEAT_GUI) || defined(VIMDLL)
1365# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001366 || (!gui.in_use && !term_console)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001367# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 || !term_console
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001369# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001370# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 )
1372 {
1373 prt_dlg.Flags |= PD_RETURNDEFAULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 /*
1375 * MSDN suggests setting the first parameter to WINSPOOL for
1376 * NT, but NULL appears to work just as well.
1377 */
1378 if (*p_pdev != NUL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001379 prt_dlg.hDC = CreateDC(NULL, (LPCSTR)p_pdev, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 {
1382 prt_dlg.Flags |= PD_RETURNDEFAULT;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001383 if (PrintDlgW(&prt_dlg) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 goto init_fail_dlg;
1385 }
1386 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001387 else if (PrintDlgW(&prt_dlg) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 goto init_fail_dlg;
1389 else
1390 {
1391 /*
1392 * keep the previous driver context
1393 */
1394 stored_dm = prt_dlg.hDevMode;
1395 stored_devn = prt_dlg.hDevNames;
1396 stored_nFlags = prt_dlg.Flags;
1397 stored_nCopies = prt_dlg.nCopies;
1398 }
1399
1400 if (prt_dlg.hDC == NULL)
1401 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001402 emsg(_("E237: Printer selection failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 mch_print_cleanup();
1404 return FALSE;
1405 }
1406
Bram Moolenaar0f873732019-12-05 20:28:46 +01001407 // Not all printer drivers report the support of color (or grey) in the
1408 // same way. Let's set has_color if there appears to be some way to print
1409 // more than B&W.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 i = GetDeviceCaps(prt_dlg.hDC, NUMCOLORS);
1411 psettings->has_color = (GetDeviceCaps(prt_dlg.hDC, BITSPIXEL) > 1
1412 || GetDeviceCaps(prt_dlg.hDC, PLANES) > 1
1413 || i > 2 || i == -1);
1414
Bram Moolenaar0f873732019-12-05 20:28:46 +01001415 // Ensure all font styles are baseline aligned
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 SetTextAlign(prt_dlg.hDC, TA_BASELINE|TA_LEFT);
1417
1418 /*
1419 * On some windows systems the nCopies parameter is not
1420 * passed back correctly. It must be retrieved from the
1421 * hDevMode struct.
1422 */
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001423 mem = (DEVMODEW *)GlobalLock(prt_dlg.hDevMode);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 if (mem != NULL)
1425 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 if (mem->dmCopies != 1)
1427 stored_nCopies = mem->dmCopies;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 if ((mem->dmFields & DM_DUPLEX) && (mem->dmDuplex & ~DMDUP_SIMPLEX))
1429 psettings->duplex = TRUE;
1430 if ((mem->dmFields & DM_COLOR) && (mem->dmColor & DMCOLOR_COLOR))
1431 psettings->has_color = TRUE;
1432 }
1433 GlobalUnlock(prt_dlg.hDevMode);
1434
1435 devname = (DEVNAMES *)GlobalLock(prt_dlg.hDevNames);
1436 if (devname != 0)
1437 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001438 WCHAR *wprinter_name = (WCHAR *)devname + devname->wDeviceOffset;
1439 WCHAR *wport_name = (WCHAR *)devname + devname->wOutputOffset;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001440 char_u *text = (char_u *)_("to %s on %s");
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001441 char_u *printer_name = utf16_to_enc(wprinter_name, NULL);
1442 char_u *port_name = utf16_to_enc(wport_name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001444 if (printer_name != NULL && port_name != NULL)
Bram Moolenaar964b3742019-05-24 18:54:09 +02001445 prt_name = alloc(STRLEN(printer_name)
1446 + STRLEN(port_name) + STRLEN(text));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 if (prt_name != NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001448 wsprintf((char *)prt_name, (const char *)text,
1449 printer_name, port_name);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001450 vim_free(printer_name);
1451 vim_free(port_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 }
1453 GlobalUnlock(prt_dlg.hDevNames);
1454
1455 /*
1456 * Initialise the font according to 'printfont'
1457 */
Bram Moolenaara80faa82020-04-12 19:37:17 +02001458 CLEAR_FIELD(fLogFont);
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001459 if (get_logfont(&fLogFont, p_pfn, prt_dlg.hDC, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001461 semsg(_("E613: Unknown printer font: %s"), p_pfn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 mch_print_cleanup();
1463 return FALSE;
1464 }
1465
1466 for (pifBold = 0; pifBold <= 1; pifBold++)
1467 for (pifItalic = 0; pifItalic <= 1; pifItalic++)
1468 for (pifUnderline = 0; pifUnderline <= 1; pifUnderline++)
1469 {
1470 fLogFont.lfWeight = boldface[pifBold];
1471 fLogFont.lfItalic = pifItalic;
1472 fLogFont.lfUnderline = pifUnderline;
1473 prt_font_handles[pifBold][pifItalic][pifUnderline]
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001474 = CreateFontIndirectW(&fLogFont);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 }
1476
1477 SetBkMode(prt_dlg.hDC, OPAQUE);
1478 SelectObject(prt_dlg.hDC, prt_font_handles[0][0][0]);
1479
1480 /*
1481 * Fill in the settings struct
1482 */
1483 psettings->chars_per_line = prt_get_cpl();
1484 psettings->lines_per_page = prt_get_lpp();
Bram Moolenaar7ddc6422014-09-27 11:18:19 +02001485 if (prt_dlg.Flags & PD_USEDEVMODECOPIESANDCOLLATE)
1486 {
1487 psettings->n_collated_copies = (prt_dlg.Flags & PD_COLLATE)
1488 ? prt_dlg.nCopies : 1;
1489 psettings->n_uncollated_copies = (prt_dlg.Flags & PD_COLLATE)
1490 ? 1 : prt_dlg.nCopies;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491
Bram Moolenaar7ddc6422014-09-27 11:18:19 +02001492 if (psettings->n_collated_copies == 0)
1493 psettings->n_collated_copies = 1;
1494
1495 if (psettings->n_uncollated_copies == 0)
1496 psettings->n_uncollated_copies = 1;
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01001497 }
1498 else
1499 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 psettings->n_collated_copies = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 psettings->n_uncollated_copies = 1;
Bram Moolenaar7ddc6422014-09-27 11:18:19 +02001502 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503
1504 psettings->jobname = jobname;
1505
1506 return TRUE;
1507
1508init_fail_dlg:
1509 {
1510 DWORD err = CommDlgExtendedError();
1511
1512 if (err)
1513 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 char_u *buf;
1515
Bram Moolenaar0f873732019-12-05 20:28:46 +01001516 // I suspect FormatMessage() doesn't work for values returned by
1517 // CommDlgExtendedError(). What does?
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
1519 FORMAT_MESSAGE_FROM_SYSTEM |
1520 FORMAT_MESSAGE_IGNORE_INSERTS,
1521 NULL, err, 0, (LPTSTR)(&buf), 0, NULL);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001522 semsg(_("E238: Print error: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 buf == NULL ? (char_u *)_("Unknown") : buf);
1524 LocalFree((LPVOID)(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 }
1526 else
Bram Moolenaar0f873732019-12-05 20:28:46 +01001527 msg_clr_eos(); // Maybe canceled
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528
1529 mch_print_cleanup();
1530 return FALSE;
1531 }
1532}
1533
1534
1535 int
1536mch_print_begin(prt_settings_T *psettings)
1537{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001538 int ret = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 char szBuffer[300];
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001540 WCHAR *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001542 hDlgPrint = CreateDialog(g_hinst, TEXT("PrintDlgBox"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 prt_dlg.hwndOwner, PrintDlgProc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 SetAbortProc(prt_dlg.hDC, AbortProc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 wsprintf(szBuffer, _("Printing '%s'"), gettail(psettings->jobname));
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001546 vimSetDlgItemText(hDlgPrint, IDC_PRINTTEXT1, (char_u *)szBuffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001548 wp = enc_to_utf16(psettings->jobname, NULL);
Bram Moolenaar2290b1f2018-05-13 17:30:45 +02001549 if (wp != NULL)
1550 {
1551 DOCINFOW di;
1552
Bram Moolenaara80faa82020-04-12 19:37:17 +02001553 CLEAR_FIELD(di);
Bram Moolenaar2290b1f2018-05-13 17:30:45 +02001554 di.cbSize = sizeof(di);
1555 di.lpszDocName = wp;
1556 ret = StartDocW(prt_dlg.hDC, &di);
1557 vim_free(wp);
1558 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001560# ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01001561 // Give focus back to main window (when using MDI).
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001562# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001563 if (gui.in_use)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001564# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001565 SetFocus(s_hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001566# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567
1568 return (ret > 0);
1569}
1570
1571 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01001572mch_print_end(prt_settings_T *psettings UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573{
1574 EndDoc(prt_dlg.hDC);
1575 if (!*bUserAbort)
1576 SendMessage(hDlgPrint, WM_COMMAND, 0, 0);
1577}
1578
1579 int
1580mch_print_end_page(void)
1581{
1582 return (EndPage(prt_dlg.hDC) > 0);
1583}
1584
1585 int
1586mch_print_begin_page(char_u *msg)
1587{
1588 if (msg != NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001589 vimSetDlgItemText(hDlgPrint, IDC_PROGRESS, msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 return (StartPage(prt_dlg.hDC) > 0);
1591}
1592
1593 int
1594mch_print_blank_page(void)
1595{
1596 return (mch_print_begin_page(NULL) ? (mch_print_end_page()) : FALSE);
1597}
1598
1599static int prt_pos_x = 0;
1600static int prt_pos_y = 0;
1601
1602 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001603mch_print_start_line(int margin, int page_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604{
1605 if (margin)
1606 prt_pos_x = -prt_number_width;
1607 else
1608 prt_pos_x = 0;
1609 prt_pos_y = page_line * prt_line_height
1610 + prt_tm.tmAscent + prt_tm.tmExternalLeading;
1611}
1612
1613 int
1614mch_print_text_out(char_u *p, int len)
1615{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 SIZE sz;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001617 WCHAR *wp;
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001618 int wlen = len;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001619 int ret = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001621 wp = enc_to_utf16(p, &wlen);
1622 if (wp == NULL)
1623 return FALSE;
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001624
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001625 TextOutW(prt_dlg.hDC, prt_pos_x + prt_left_margin,
1626 prt_pos_y + prt_top_margin, wp, wlen);
1627 GetTextExtentPoint32W(prt_dlg.hDC, wp, wlen, &sz);
1628 vim_free(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 prt_pos_x += (sz.cx - prt_tm.tmOverhang);
Bram Moolenaar0f873732019-12-05 20:28:46 +01001630 // This is wrong when printing spaces for a TAB.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001631 if (p[len] != NUL)
1632 {
Bram Moolenaar1614a142019-10-06 22:00:13 +02001633 wlen = mb_ptr2len(p + len);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001634 wp = enc_to_utf16(p + len, &wlen);
1635 if (wp != NULL)
1636 {
1637 GetTextExtentPoint32W(prt_dlg.hDC, wp, 1, &sz);
1638 ret = (prt_pos_x + prt_left_margin + sz.cx > prt_right_margin);
1639 vim_free(wp);
1640 }
1641 }
1642 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643}
1644
1645 void
1646mch_print_set_font(int iBold, int iItalic, int iUnderline)
1647{
1648 SelectObject(prt_dlg.hDC, prt_font_handles[iBold][iItalic][iUnderline]);
1649}
1650
1651 void
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001652mch_print_set_bg(long_u bgcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653{
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001654 SetBkColor(prt_dlg.hDC, GetNearestColor(prt_dlg.hDC,
1655 swap_me((COLORREF)bgcol)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 /*
1657 * With a white background we can draw characters transparent, which is
1658 * good for italic characters that overlap to the next char cell.
1659 */
1660 if (bgcol == 0xffffffUL)
1661 SetBkMode(prt_dlg.hDC, TRANSPARENT);
1662 else
1663 SetBkMode(prt_dlg.hDC, OPAQUE);
1664}
1665
1666 void
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001667mch_print_set_fg(long_u fgcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668{
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001669 SetTextColor(prt_dlg.hDC, GetNearestColor(prt_dlg.hDC,
1670 swap_me((COLORREF)fgcol)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671}
1672
Bram Moolenaar0f873732019-12-05 20:28:46 +01001673#endif // FEAT_PRINTER && !FEAT_POSTSCRIPT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674
Bram Moolenaar58d98232005-07-23 22:25:46 +00001675
1676
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677#if defined(FEAT_SHORTCUT) || defined(PROTO)
Bram Moolenaar82881492012-11-20 16:53:39 +01001678# ifndef PROTO
1679# include <shlobj.h>
1680# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001682typedef BOOL (WINAPI *pfnGetFinalPathNameByHandleW)(
Bram Moolenaardce1e892019-02-10 23:18:53 +01001683 HANDLE hFile,
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001684 LPWSTR lpszFilePath,
1685 DWORD cchFilePath,
1686 DWORD dwFlags);
1687static pfnGetFinalPathNameByHandleW pGetFinalPathNameByHandleW = NULL;
Bram Moolenaardce1e892019-02-10 23:18:53 +01001688
Bram Moolenaar4a792c82019-06-06 12:22:41 +02001689# define is_path_sep(c) ((c) == L'\\' || (c) == L'/')
1690
1691 static int
1692is_reparse_point_included(LPCWSTR fname)
1693{
1694 LPCWSTR p = fname, q;
1695 WCHAR buf[MAX_PATH];
1696 DWORD attr;
1697
1698 if (isalpha(p[0]) && p[1] == L':' && is_path_sep(p[2]))
1699 p += 3;
1700 else if (is_path_sep(p[0]) && is_path_sep(p[1]))
1701 p += 2;
1702
1703 while (*p != L'\0')
1704 {
1705 q = wcspbrk(p, L"\\/");
1706 if (q == NULL)
1707 p = q = fname + wcslen(fname);
1708 else
1709 p = q + 1;
1710 if (q - fname >= MAX_PATH)
1711 return FALSE;
1712 wcsncpy(buf, fname, q - fname);
1713 buf[q - fname] = L'\0';
1714 attr = GetFileAttributesW(buf);
1715 if (attr != INVALID_FILE_ATTRIBUTES
1716 && (attr & FILE_ATTRIBUTE_REPARSE_POINT) != 0)
1717 return TRUE;
1718 }
1719 return FALSE;
1720}
1721
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02001722 static char_u *
Bram Moolenaardce1e892019-02-10 23:18:53 +01001723resolve_reparse_point(char_u *fname)
1724{
1725 HANDLE h = INVALID_HANDLE_VALUE;
1726 DWORD size;
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001727 WCHAR *p, *wp;
Bram Moolenaardce1e892019-02-10 23:18:53 +01001728 char_u *rfname = NULL;
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001729 WCHAR *buff = NULL;
Bram Moolenaardce1e892019-02-10 23:18:53 +01001730 static BOOL loaded = FALSE;
1731
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001732 if (pGetFinalPathNameByHandleW == NULL)
Bram Moolenaardce1e892019-02-10 23:18:53 +01001733 {
1734 HMODULE hmod = GetModuleHandle("kernel32.dll");
1735
1736 if (loaded == TRUE)
1737 return NULL;
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001738 pGetFinalPathNameByHandleW = (pfnGetFinalPathNameByHandleW)
1739 GetProcAddress(hmod, "GetFinalPathNameByHandleW");
Bram Moolenaardce1e892019-02-10 23:18:53 +01001740 loaded = TRUE;
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001741 if (pGetFinalPathNameByHandleW == NULL)
Bram Moolenaardce1e892019-02-10 23:18:53 +01001742 return NULL;
1743 }
1744
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001745 p = enc_to_utf16(fname, NULL);
1746 if (p == NULL)
1747 goto fail;
1748
Bram Moolenaar4a792c82019-06-06 12:22:41 +02001749 if (!is_reparse_point_included(p))
1750 {
1751 vim_free(p);
1752 goto fail;
1753 }
1754
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001755 h = CreateFileW(p, 0, 0, NULL, OPEN_EXISTING,
1756 FILE_FLAG_BACKUP_SEMANTICS, NULL);
1757 vim_free(p);
Bram Moolenaardce1e892019-02-10 23:18:53 +01001758
1759 if (h == INVALID_HANDLE_VALUE)
1760 goto fail;
1761
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001762 size = pGetFinalPathNameByHandleW(h, NULL, 0, 0);
1763 if (size == 0)
1764 goto fail;
1765 buff = ALLOC_MULT(WCHAR, size);
1766 if (buff == NULL)
1767 goto fail;
1768 if (pGetFinalPathNameByHandleW(h, buff, size, 0) == 0)
Bram Moolenaardce1e892019-02-10 23:18:53 +01001769 goto fail;
1770
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001771 if (wcsncmp(buff, L"\\\\?\\UNC\\", 8) == 0)
1772 {
1773 buff[6] = L'\\';
1774 wp = buff + 6;
1775 }
1776 else if (wcsncmp(buff, L"\\\\?\\", 4) == 0)
1777 wp = buff + 4;
Bram Moolenaardce1e892019-02-10 23:18:53 +01001778 else
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001779 wp = buff;
1780
1781 rfname = utf16_to_enc(wp, NULL);
Bram Moolenaardce1e892019-02-10 23:18:53 +01001782
1783fail:
1784 if (h != INVALID_HANDLE_VALUE)
1785 CloseHandle(h);
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001786 if (buff != NULL)
1787 vim_free(buff);
Bram Moolenaardce1e892019-02-10 23:18:53 +01001788
1789 return rfname;
1790}
1791
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792/*
1793 * When "fname" is the name of a shortcut (*.lnk) resolve the file it points
1794 * to and return that name in allocated memory.
1795 * Otherwise NULL is returned.
1796 */
Bram Moolenaardce1e892019-02-10 23:18:53 +01001797 static char_u *
1798resolve_shortcut(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799{
1800 HRESULT hr;
1801 IShellLink *psl = NULL;
1802 IPersistFile *ppf = NULL;
1803 OLECHAR wsz[MAX_PATH];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 char_u *rfname = NULL;
1805 int len;
Bram Moolenaar604729e2013-08-30 16:44:19 +02001806 IShellLinkW *pslw = NULL;
1807 WIN32_FIND_DATAW ffdw; // we get those free of charge
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808
Bram Moolenaar0f873732019-12-05 20:28:46 +01001809 // Check if the file name ends in ".lnk". Avoid calling
1810 // CoCreateInstance(), it's quite slow.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 if (fname == NULL)
1812 return rfname;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001813 len = (int)STRLEN(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 if (len <= 4 || STRNICMP(fname + len - 4, ".lnk", 4) != 0)
1815 return rfname;
1816
1817 CoInitialize(NULL);
1818
1819 // create a link manager object and request its interface
1820 hr = CoCreateInstance(
1821 &CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001822 &IID_IShellLinkW, (void**)&pslw);
1823 if (hr == S_OK)
1824 {
1825 WCHAR *p = enc_to_utf16(fname, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001827 if (p != NULL)
1828 {
1829 // Get a pointer to the IPersistFile interface.
1830 hr = pslw->lpVtbl->QueryInterface(
1831 pslw, &IID_IPersistFile, (void**)&ppf);
1832 if (hr != S_OK)
1833 goto shortcut_errorw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001835 // "load" the name and resolve the link
1836 hr = ppf->lpVtbl->Load(ppf, p, STGM_READ);
1837 if (hr != S_OK)
1838 goto shortcut_errorw;
1839# if 0 // This makes Vim wait a long time if the target does not exist.
1840 hr = pslw->lpVtbl->Resolve(pslw, NULL, SLR_NO_UI);
1841 if (hr != S_OK)
1842 goto shortcut_errorw;
Bram Moolenaar604729e2013-08-30 16:44:19 +02001843# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001845 // Get the path to the link target.
1846 ZeroMemory(wsz, MAX_PATH * sizeof(WCHAR));
1847 hr = pslw->lpVtbl->GetPath(pslw, wsz, MAX_PATH, &ffdw, 0);
1848 if (hr == S_OK && wsz[0] != NUL)
1849 rfname = utf16_to_enc(wsz, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001851shortcut_errorw:
1852 vim_free(p);
1853 }
1854 }
1855
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 // Release all interface pointers (both belong to the same object)
1857 if (ppf != NULL)
1858 ppf->lpVtbl->Release(ppf);
1859 if (psl != NULL)
1860 psl->lpVtbl->Release(psl);
Bram Moolenaar604729e2013-08-30 16:44:19 +02001861 if (pslw != NULL)
1862 pslw->lpVtbl->Release(pslw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863
1864 CoUninitialize();
1865 return rfname;
1866}
Bram Moolenaardce1e892019-02-10 23:18:53 +01001867
1868 char_u *
1869mch_resolve_path(char_u *fname, int reparse_point)
1870{
1871 char_u *path = resolve_shortcut(fname);
1872
1873 if (path == NULL && reparse_point)
1874 path = resolve_reparse_point(fname);
1875 return path;
1876}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877#endif
1878
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001879#if (defined(FEAT_EVAL) && (!defined(FEAT_GUI) || defined(VIMDLL))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880/*
1881 * Bring ourselves to the foreground. Does work if the OS doesn't allow it.
1882 */
1883 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001884win32_set_foreground(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885{
Bram Moolenaar0f873732019-12-05 20:28:46 +01001886 GetConsoleHwnd(); // get value of s_hwnd
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 if (s_hwnd != 0)
1888 SetForegroundWindow(s_hwnd);
1889}
1890#endif
1891
1892#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
1893/*
1894 * Client-server code for Vim
1895 *
1896 * Originally written by Paul Moore
1897 */
1898
Bram Moolenaar0f873732019-12-05 20:28:46 +01001899// In order to handle inter-process messages, we need to have a window. But
1900// the functions in this module can be called before the main GUI window is
1901// created (and may also be called in the console version, where there is no
1902// GUI window at all).
1903//
1904// So we create a hidden window, and arrange to destroy it on exit.
1905HWND message_window = 0; // window that's handling messages
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001907# define VIM_CLASSNAME "VIM_MESSAGES"
1908# define VIM_CLASSNAME_LEN (sizeof(VIM_CLASSNAME) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001910// Communication is via WM_COPYDATA messages. The message type is sent in
Bram Moolenaar0f873732019-12-05 20:28:46 +01001911// the dwData parameter. Types are defined here.
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001912# define COPYDATA_KEYS 0
1913# define COPYDATA_REPLY 1
1914# define COPYDATA_EXPR 10
1915# define COPYDATA_RESULT 11
1916# define COPYDATA_ERROR_RESULT 12
1917# define COPYDATA_ENCODING 20
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918
Bram Moolenaar0f873732019-12-05 20:28:46 +01001919// This is a structure containing a server HWND and its name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920struct server_id
1921{
1922 HWND hwnd;
1923 char_u *name;
1924};
1925
Bram Moolenaar0f873732019-12-05 20:28:46 +01001926// Last received 'encoding' that the client uses.
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001927static char_u *client_enc = NULL;
1928
1929/*
1930 * Tell the other side what encoding we are using.
1931 * Errors are ignored.
1932 */
1933 static void
1934serverSendEnc(HWND target)
1935{
1936 COPYDATASTRUCT data;
1937
1938 data.dwData = COPYDATA_ENCODING;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001939 data.cbData = (DWORD)STRLEN(p_enc) + 1;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001940 data.lpData = p_enc;
1941 (void)SendMessage(target, WM_COPYDATA, (WPARAM)message_window,
1942 (LPARAM)(&data));
1943}
1944
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945/*
1946 * Clean up on exit. This destroys the hidden message window.
1947 */
1948 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949CleanUpMessaging(void)
1950{
1951 if (message_window != 0)
1952 {
1953 DestroyWindow(message_window);
1954 message_window = 0;
1955 }
1956}
1957
1958static int save_reply(HWND server, char_u *reply, int expr);
1959
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001960/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 * The window procedure for the hidden message window.
1962 * It handles callback messages and notifications from servers.
1963 * In order to process these messages, it is necessary to run a
1964 * message loop. Code which may run before the main message loop
1965 * is started (in the GUI) is careful to pump messages when it needs
1966 * to. Features which require message delivery during normal use will
1967 * not work in the console version - this basically means those
1968 * features which allow Vim to act as a server, rather than a client.
1969 */
1970 static LRESULT CALLBACK
1971Messaging_WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
1972{
1973 if (msg == WM_COPYDATA)
1974 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001975 // This is a message from another Vim. The dwData member of the
1976 // COPYDATASTRUCT determines the type of message:
1977 // COPYDATA_ENCODING:
1978 // The encoding that the client uses. Following messages will
1979 // use this encoding, convert if needed.
1980 // COPYDATA_KEYS:
1981 // A key sequence. We are a server, and a client wants these keys
1982 // adding to the input queue.
1983 // COPYDATA_REPLY:
1984 // A reply. We are a client, and a server has sent this message
1985 // in response to a request. (server2client())
1986 // COPYDATA_EXPR:
1987 // An expression. We are a server, and a client wants us to
1988 // evaluate this expression.
1989 // COPYDATA_RESULT:
1990 // A reply. We are a client, and a server has sent this message
1991 // in response to a COPYDATA_EXPR.
1992 // COPYDATA_ERROR_RESULT:
1993 // A reply. We are a client, and a server has sent this message
1994 // in response to a COPYDATA_EXPR that failed to evaluate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 COPYDATASTRUCT *data = (COPYDATASTRUCT*)lParam;
1996 HWND sender = (HWND)wParam;
1997 COPYDATASTRUCT reply;
1998 char_u *res;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 int retval;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002000 char_u *str;
2001 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002
2003 switch (data->dwData)
2004 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002005 case COPYDATA_ENCODING:
Bram Moolenaar0f873732019-12-05 20:28:46 +01002006 // Remember the encoding that the client uses.
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002007 vim_free(client_enc);
2008 client_enc = enc_canonize((char_u *)data->lpData);
2009 return 1;
2010
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 case COPYDATA_KEYS:
Bram Moolenaar0f873732019-12-05 20:28:46 +01002012 // Remember who sent this, for <client>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 clientWindow = sender;
2014
Bram Moolenaar0f873732019-12-05 20:28:46 +01002015 // Add the received keys to the input buffer. The loop waiting
2016 // for the user to do something should check the input buffer.
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002017 str = serverConvert(client_enc, (char_u *)data->lpData, &tofree);
2018 server_to_input_buf(str);
2019 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020
2021# ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01002022 // Wake up the main GUI loop.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002023# ifdef VIMDLL
2024 if (gui.in_use)
2025# endif
2026 if (s_hwnd != 0)
2027 PostMessage(s_hwnd, WM_NULL, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028# endif
2029 return 1;
2030
2031 case COPYDATA_EXPR:
Bram Moolenaar0f873732019-12-05 20:28:46 +01002032 // Remember who sent this, for <client>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 clientWindow = sender;
2034
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002035 str = serverConvert(client_enc, (char_u *)data->lpData, &tofree);
2036 res = eval_client_expr_to_string(str);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002037
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 if (res == NULL)
2039 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00002040 char *err = _(e_invalid_expression_received);
Bram Moolenaar15bf76d2017-03-18 16:18:37 +01002041 size_t len = STRLEN(str) + STRLEN(err) + 5;
2042
Bram Moolenaar964b3742019-05-24 18:54:09 +02002043 res = alloc(len);
Bram Moolenaar15bf76d2017-03-18 16:18:37 +01002044 if (res != NULL)
2045 vim_snprintf((char *)res, len, "%s: \"%s\"", err, str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 reply.dwData = COPYDATA_ERROR_RESULT;
2047 }
2048 else
2049 reply.dwData = COPYDATA_RESULT;
2050 reply.lpData = res;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002051 reply.cbData = (DWORD)STRLEN(res) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002053 serverSendEnc(sender);
Bram Moolenaar5246cd72013-06-16 16:41:47 +02002054 retval = (int)SendMessage(sender, WM_COPYDATA,
2055 (WPARAM)message_window, (LPARAM)(&reply));
Bram Moolenaar15bf76d2017-03-18 16:18:37 +01002056 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 vim_free(res);
2058 return retval;
2059
2060 case COPYDATA_REPLY:
2061 case COPYDATA_RESULT:
2062 case COPYDATA_ERROR_RESULT:
2063 if (data->lpData != NULL)
2064 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002065 str = serverConvert(client_enc, (char_u *)data->lpData,
2066 &tofree);
2067 if (tofree == NULL)
2068 str = vim_strsave(str);
2069 if (save_reply(sender, str,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 (data->dwData == COPYDATA_REPLY ? 0 :
2071 (data->dwData == COPYDATA_RESULT ? 1 :
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002072 2))) == FAIL)
2073 vim_free(str);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002074 else if (data->dwData == COPYDATA_REPLY)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 {
Bram Moolenaar427d51c2013-06-16 16:01:25 +02002076 char_u winstr[30];
2077
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002078 sprintf((char *)winstr, PRINTF_HEX_LONG_U, (long_u)sender);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002079 apply_autocmds(EVENT_REMOTEREPLY, winstr, str,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 TRUE, curbuf);
2081 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 }
2083 return 1;
2084 }
2085
2086 return 0;
2087 }
2088
2089 else if (msg == WM_ACTIVATE && wParam == WA_ACTIVE)
2090 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002091 // When the message window is activated (brought to the foreground),
2092 // this actually applies to the text window.
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002093# if !defined(FEAT_GUI) || defined(VIMDLL)
2094# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002095 if (!gui.in_use)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002096# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002097 GetConsoleHwnd(); // get value of s_hwnd
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002098# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 if (s_hwnd != 0)
2100 {
2101 SetForegroundWindow(s_hwnd);
2102 return 0;
2103 }
2104 }
2105
2106 return DefWindowProc(hwnd, msg, wParam, lParam);
2107}
2108
2109/*
2110 * Initialise the message handling process. This involves creating a window
2111 * to handle messages - the window will not be visible.
2112 */
2113 void
2114serverInitMessaging(void)
2115{
2116 WNDCLASS wndclass;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117
Bram Moolenaar0f873732019-12-05 20:28:46 +01002118 // Clean up on exit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 atexit(CleanUpMessaging);
2120
Bram Moolenaar0f873732019-12-05 20:28:46 +01002121 // Register a window class - we only really care
2122 // about the window procedure
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 wndclass.style = 0;
2124 wndclass.lpfnWndProc = Messaging_WndProc;
2125 wndclass.cbClsExtra = 0;
2126 wndclass.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002127 wndclass.hInstance = g_hinst;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 wndclass.hIcon = NULL;
2129 wndclass.hCursor = NULL;
2130 wndclass.hbrBackground = NULL;
2131 wndclass.lpszMenuName = NULL;
2132 wndclass.lpszClassName = VIM_CLASSNAME;
2133 RegisterClass(&wndclass);
2134
Bram Moolenaar0f873732019-12-05 20:28:46 +01002135 // Create the message window. It will be hidden, so the details don't
2136 // matter. Don't use WS_OVERLAPPEDWINDOW, it will make a shortcut remove
2137 // focus from gvim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 message_window = CreateWindow(VIM_CLASSNAME, "",
2139 WS_POPUPWINDOW | WS_CAPTION,
2140 CW_USEDEFAULT, CW_USEDEFAULT,
2141 100, 100, NULL, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002142 g_hinst, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143}
2144
Bram Moolenaar0f873732019-12-05 20:28:46 +01002145// Used by serverSendToVim() to find an alternate server name.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002146static char_u *altname_buf_ptr = NULL;
2147
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148/*
2149 * Get the title of the window "hwnd", which is the Vim server name, in
2150 * "name[namelen]" and return the length.
2151 * Returns zero if window "hwnd" is not a Vim server.
2152 */
2153 static int
2154getVimServerName(HWND hwnd, char *name, int namelen)
2155{
2156 int len;
2157 char buffer[VIM_CLASSNAME_LEN + 1];
2158
Bram Moolenaar0f873732019-12-05 20:28:46 +01002159 // Ignore windows which aren't Vim message windows
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 len = GetClassName(hwnd, buffer, sizeof(buffer));
2161 if (len != VIM_CLASSNAME_LEN || STRCMP(buffer, VIM_CLASSNAME) != 0)
2162 return 0;
2163
Bram Moolenaar0f873732019-12-05 20:28:46 +01002164 // Get the title of the window
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 return GetWindowText(hwnd, name, namelen);
2166}
2167
2168 static BOOL CALLBACK
2169enumWindowsGetServer(HWND hwnd, LPARAM lparam)
2170{
2171 struct server_id *id = (struct server_id *)lparam;
2172 char server[MAX_PATH];
2173
Bram Moolenaar0f873732019-12-05 20:28:46 +01002174 // Get the title of the window
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 if (getVimServerName(hwnd, server, sizeof(server)) == 0)
2176 return TRUE;
2177
Bram Moolenaar0f873732019-12-05 20:28:46 +01002178 // If this is the server we're looking for, return its HWND
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 if (STRICMP(server, id->name) == 0)
2180 {
2181 id->hwnd = hwnd;
2182 return FALSE;
2183 }
2184
Bram Moolenaar0f873732019-12-05 20:28:46 +01002185 // If we are looking for an alternate server, remember this name.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002186 if (altname_buf_ptr != NULL
2187 && STRNICMP(server, id->name, STRLEN(id->name)) == 0
2188 && vim_isdigit(server[STRLEN(id->name)]))
2189 {
2190 STRCPY(altname_buf_ptr, server);
Bram Moolenaar0f873732019-12-05 20:28:46 +01002191 altname_buf_ptr = NULL; // don't use another name
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002192 }
2193
Bram Moolenaar0f873732019-12-05 20:28:46 +01002194 // Otherwise, keep looking
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 return TRUE;
2196}
2197
2198 static BOOL CALLBACK
2199enumWindowsGetNames(HWND hwnd, LPARAM lparam)
2200{
2201 garray_T *ga = (garray_T *)lparam;
2202 char server[MAX_PATH];
2203
Bram Moolenaar0f873732019-12-05 20:28:46 +01002204 // Get the title of the window
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 if (getVimServerName(hwnd, server, sizeof(server)) == 0)
2206 return TRUE;
2207
Bram Moolenaar0f873732019-12-05 20:28:46 +01002208 // Add the name to the list
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002209 ga_concat(ga, (char_u *)server);
2210 ga_concat(ga, (char_u *)"\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 return TRUE;
2212}
2213
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002214struct enum_windows_s
2215{
2216 WNDENUMPROC lpEnumFunc;
2217 LPARAM lParam;
2218};
2219
2220 static BOOL CALLBACK
2221enum_windows_child(HWND hwnd, LPARAM lParam)
2222{
2223 struct enum_windows_s *ew = (struct enum_windows_s *)lParam;
2224
2225 return (ew->lpEnumFunc)(hwnd, ew->lParam);
2226}
2227
2228 static BOOL CALLBACK
2229enum_windows_toplevel(HWND hwnd, LPARAM lParam)
2230{
2231 struct enum_windows_s *ew = (struct enum_windows_s *)lParam;
2232
Bram Moolenaar95ba5c32018-10-07 22:47:07 +02002233 if ((ew->lpEnumFunc)(hwnd, ew->lParam))
2234 return TRUE;
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002235 return EnumChildWindows(hwnd, enum_windows_child, lParam);
2236}
2237
Bram Moolenaar0f873732019-12-05 20:28:46 +01002238/*
2239 * Enumerate all windows including children.
2240 */
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002241 static BOOL
2242enum_windows(WNDENUMPROC lpEnumFunc, LPARAM lParam)
2243{
2244 struct enum_windows_s ew;
2245
2246 ew.lpEnumFunc = lpEnumFunc;
2247 ew.lParam = lParam;
2248 return EnumWindows(enum_windows_toplevel, (LPARAM)&ew);
2249}
2250
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 static HWND
2252findServer(char_u *name)
2253{
2254 struct server_id id;
2255
2256 id.name = name;
2257 id.hwnd = 0;
2258
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002259 enum_windows(enumWindowsGetServer, (LPARAM)(&id));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260
2261 return id.hwnd;
2262}
2263
2264 void
2265serverSetName(char_u *name)
2266{
2267 char_u *ok_name;
2268 HWND hwnd = 0;
2269 int i = 0;
2270 char_u *p;
2271
Bram Moolenaar0f873732019-12-05 20:28:46 +01002272 // Leave enough space for a 9-digit suffix to ensure uniqueness!
Bram Moolenaar964b3742019-05-24 18:54:09 +02002273 ok_name = alloc(STRLEN(name) + 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274
2275 STRCPY(ok_name, name);
2276 p = ok_name + STRLEN(name);
2277
2278 for (;;)
2279 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002280 // This is inefficient - we're doing an EnumWindows loop for each
2281 // possible name. It would be better to grab all names in one go,
2282 // and scan the list each time...
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 hwnd = findServer(ok_name);
2284 if (hwnd == 0)
2285 break;
2286
2287 ++i;
2288 if (i >= 1000)
2289 break;
2290
2291 sprintf((char *)p, "%d", i);
2292 }
2293
2294 if (hwnd != 0)
2295 vim_free(ok_name);
2296 else
2297 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002298 // Remember the name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 serverName = ok_name;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002300 need_maketitle = TRUE; // update Vim window title later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301
Bram Moolenaar0f873732019-12-05 20:28:46 +01002302 // Update the message window title
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002303 SetWindowText(message_window, (LPCSTR)ok_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002305# ifdef FEAT_EVAL
Bram Moolenaar0f873732019-12-05 20:28:46 +01002306 // Set the servername variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 set_vim_var_string(VV_SEND_SERVER, serverName, -1);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002308# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 }
2310}
2311
2312 char_u *
2313serverGetVimNames(void)
2314{
2315 garray_T ga;
2316
2317 ga_init2(&ga, 1, 100);
2318
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002319 enum_windows(enumWindowsGetNames, (LPARAM)(&ga));
Bram Moolenaar269ec652004-07-29 08:43:53 +00002320 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321
2322 return ga.ga_data;
2323}
2324
2325 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002326serverSendReply(
Bram Moolenaar0f873732019-12-05 20:28:46 +01002327 char_u *name, // Where to send.
2328 char_u *reply) // What to send.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329{
2330 HWND target;
2331 COPYDATASTRUCT data;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002332 long_u n = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333
Bram Moolenaar0f873732019-12-05 20:28:46 +01002334 // The "name" argument is a magic cookie obtained from expand("<client>").
2335 // It should be of the form 0xXXXXX - i.e. a C hex literal, which is the
2336 // value of the client's message window HWND.
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002337 sscanf((char *)name, SCANF_HEX_LONG_U, &n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 if (n == 0)
2339 return -1;
2340
2341 target = (HWND)n;
2342 if (!IsWindow(target))
2343 return -1;
2344
2345 data.dwData = COPYDATA_REPLY;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002346 data.cbData = (DWORD)STRLEN(reply) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 data.lpData = reply;
2348
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002349 serverSendEnc(target);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 if (SendMessage(target, WM_COPYDATA, (WPARAM)message_window,
2351 (LPARAM)(&data)))
2352 return 0;
2353
2354 return -1;
2355}
2356
2357 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002358serverSendToVim(
Bram Moolenaar0f873732019-12-05 20:28:46 +01002359 char_u *name, // Where to send.
2360 char_u *cmd, // What to send.
2361 char_u **result, // Result of eval'ed expression
2362 void *ptarget, // HWND of server
2363 int asExpr, // Expression or keys?
2364 int timeout, // timeout in seconds or zero
2365 int silent) // don't complain about no server
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366{
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002367 HWND target;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 COPYDATASTRUCT data;
2369 char_u *retval = NULL;
2370 int retcode = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002371 char_u altname_buf[MAX_PATH];
2372
Bram Moolenaar0f873732019-12-05 20:28:46 +01002373 // Execute locally if no display or target is ourselves
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01002374 if (serverName != NULL && STRICMP(name, serverName) == 0)
2375 return sendToLocalVim(cmd, asExpr, result);
2376
Bram Moolenaar0f873732019-12-05 20:28:46 +01002377 // If the server name does not end in a digit then we look for an
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00002378 // alternate name. e.g. when "name" is GVIM then we may find GVIM2.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002379 if (STRLEN(name) > 1 && !vim_isdigit(name[STRLEN(name) - 1]))
2380 altname_buf_ptr = altname_buf;
2381 altname_buf[0] = NUL;
2382 target = findServer(name);
2383 altname_buf_ptr = NULL;
2384 if (target == 0 && altname_buf[0] != NUL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01002385 // Use another server name we found.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002386 target = findServer(altname_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387
2388 if (target == 0)
2389 {
2390 if (!silent)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002391 semsg(_(e_no_registered_server_named_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 return -1;
2393 }
2394
2395 if (ptarget)
2396 *(HWND *)ptarget = target;
2397
2398 data.dwData = asExpr ? COPYDATA_EXPR : COPYDATA_KEYS;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002399 data.cbData = (DWORD)STRLEN(cmd) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 data.lpData = cmd;
2401
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002402 serverSendEnc(target);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 if (SendMessage(target, WM_COPYDATA, (WPARAM)message_window,
2404 (LPARAM)(&data)) == 0)
2405 return -1;
2406
2407 if (asExpr)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002408 retval = serverGetReply(target, &retcode, TRUE, TRUE, timeout);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409
2410 if (result == NULL)
2411 vim_free(retval);
2412 else
Bram Moolenaar0f873732019-12-05 20:28:46 +01002413 *result = retval; // Caller assumes responsibility for freeing
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414
2415 return retcode;
2416}
2417
2418/*
2419 * Bring the server to the foreground.
2420 */
2421 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002422serverForeground(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423{
2424 HWND target = findServer(name);
2425
2426 if (target != 0)
2427 SetForegroundWindow(target);
2428}
2429
Bram Moolenaar0f873732019-12-05 20:28:46 +01002430// Replies from server need to be stored until the client picks them up via
2431// remote_read(). So we maintain a list of server-id/reply pairs.
2432// Note that there could be multiple replies from one server pending if the
2433// client is slow picking them up.
2434// We just store the replies in a simple list. When we remove an entry, we
2435// move list entries down to fill the gap.
2436// The server ID is simply the HWND.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437typedef struct
2438{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002439 HWND server; // server window
2440 char_u *reply; // reply string
2441 int expr_result; // 0 for REPLY, 1 for RESULT 2 for error
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002442} reply_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443
2444static garray_T reply_list = {0, 0, sizeof(reply_T), 5, 0};
2445
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002446# define REPLY_ITEM(i) ((reply_T *)(reply_list.ga_data) + (i))
2447# define REPLY_COUNT (reply_list.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448
Bram Moolenaar0f873732019-12-05 20:28:46 +01002449// Flag which is used to wait for a reply
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450static int reply_received = 0;
2451
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002452/*
2453 * Store a reply. "reply" must be allocated memory (or NULL).
2454 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 static int
2456save_reply(HWND server, char_u *reply, int expr)
2457{
2458 reply_T *rep;
2459
2460 if (ga_grow(&reply_list, 1) == FAIL)
2461 return FAIL;
2462
2463 rep = REPLY_ITEM(REPLY_COUNT);
2464 rep->server = server;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002465 rep->reply = reply;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 rep->expr_result = expr;
2467 if (rep->reply == NULL)
2468 return FAIL;
2469
2470 ++REPLY_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 reply_received = 1;
2472 return OK;
2473}
2474
2475/*
2476 * Get a reply from server "server".
2477 * When "expr_res" is non NULL, get the result of an expression, otherwise a
2478 * server2client() message.
2479 * When non NULL, point to return code. 0 => OK, -1 => ERROR
2480 * If "remove" is TRUE, consume the message, the caller must free it then.
2481 * if "wait" is TRUE block until a message arrives (or the server exits).
2482 */
2483 char_u *
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002484serverGetReply(HWND server, int *expr_res, int remove, int wait, int timeout)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485{
2486 int i;
2487 char_u *reply;
2488 reply_T *rep;
Bram Moolenaar15e737f2017-03-18 21:22:47 +01002489 int did_process = FALSE;
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002490 time_t start;
2491 time_t now;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492
Bram Moolenaar0f873732019-12-05 20:28:46 +01002493 // When waiting, loop until the message waiting for is received.
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002494 time(&start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 for (;;)
2496 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002497 // Reset this here, in case a message arrives while we are going
2498 // through the already received messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 reply_received = 0;
2500
2501 for (i = 0; i < REPLY_COUNT; ++i)
2502 {
2503 rep = REPLY_ITEM(i);
2504 if (rep->server == server
2505 && ((rep->expr_result != 0) == (expr_res != NULL)))
2506 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002507 // Save the values we've found for later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 reply = rep->reply;
2509 if (expr_res != NULL)
2510 *expr_res = rep->expr_result == 1 ? 0 : -1;
2511
2512 if (remove)
2513 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002514 // Move the rest of the list down to fill the gap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 mch_memmove(rep, rep + 1,
2516 (REPLY_COUNT - i - 1) * sizeof(reply_T));
2517 --REPLY_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 }
2519
Bram Moolenaar0f873732019-12-05 20:28:46 +01002520 // Return the reply to the caller, who takes on responsibility
2521 // for freeing it if "remove" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 return reply;
2523 }
2524 }
2525
Bram Moolenaar0f873732019-12-05 20:28:46 +01002526 // If we got here, we didn't find a reply. Return immediately if the
2527 // "wait" parameter isn't set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 if (!wait)
Bram Moolenaar15e737f2017-03-18 21:22:47 +01002529 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002530 // Process pending messages once. Without this, looping on
2531 // remote_peek() would never get the reply.
Bram Moolenaar15e737f2017-03-18 21:22:47 +01002532 if (!did_process)
2533 {
2534 did_process = TRUE;
2535 serverProcessPendingMessages();
2536 continue;
2537 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 break;
Bram Moolenaar15e737f2017-03-18 21:22:47 +01002539 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540
Bram Moolenaar0f873732019-12-05 20:28:46 +01002541 // We need to wait for a reply. Enter a message loop until the
2542 // "reply_received" flag gets set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543
Bram Moolenaar0f873732019-12-05 20:28:46 +01002544 // Loop until we receive a reply
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 while (reply_received == 0)
2546 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002547# ifdef FEAT_TIMERS
Bram Moolenaar0f873732019-12-05 20:28:46 +01002548 // TODO: use the return value to decide how long to wait.
Bram Moolenaar42205552017-03-18 19:42:22 +01002549 check_due_timer();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002550# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002551 time(&now);
2552 if (timeout > 0 && (now - start) >= timeout)
2553 break;
2554
Bram Moolenaar0f873732019-12-05 20:28:46 +01002555 // Wait for a SendMessage() call to us. This could be the reply
2556 // we are waiting for. Use a timeout of a second, to catch the
2557 // situation that the server died unexpectedly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 MsgWaitForMultipleObjects(0, NULL, TRUE, 1000, QS_ALLINPUT);
2559
Bram Moolenaar0f873732019-12-05 20:28:46 +01002560 // If the server has died, give up
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 if (!IsWindow(server))
2562 return NULL;
2563
2564 serverProcessPendingMessages();
2565 }
2566 }
2567
2568 return NULL;
2569}
2570
2571/*
2572 * Process any messages in the Windows message queue.
2573 */
2574 void
2575serverProcessPendingMessages(void)
2576{
2577 MSG msg;
2578
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02002579 while (pPeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 {
2581 TranslateMessage(&msg);
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02002582 pDispatchMessage(&msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 }
2584}
2585
Bram Moolenaar0f873732019-12-05 20:28:46 +01002586#endif // FEAT_CLIENTSERVER
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587
2588#if defined(FEAT_GUI) || (defined(FEAT_PRINTER) && !defined(FEAT_POSTSCRIPT)) \
2589 || defined(PROTO)
2590
2591struct charset_pair
2592{
2593 char *name;
2594 BYTE charset;
2595};
2596
2597static struct charset_pair
2598charset_pairs[] =
2599{
2600 {"ANSI", ANSI_CHARSET},
2601 {"CHINESEBIG5", CHINESEBIG5_CHARSET},
2602 {"DEFAULT", DEFAULT_CHARSET},
2603 {"HANGEUL", HANGEUL_CHARSET},
2604 {"OEM", OEM_CHARSET},
2605 {"SHIFTJIS", SHIFTJIS_CHARSET},
2606 {"SYMBOL", SYMBOL_CHARSET},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 {"ARABIC", ARABIC_CHARSET},
2608 {"BALTIC", BALTIC_CHARSET},
2609 {"EASTEUROPE", EASTEUROPE_CHARSET},
2610 {"GB2312", GB2312_CHARSET},
2611 {"GREEK", GREEK_CHARSET},
2612 {"HEBREW", HEBREW_CHARSET},
2613 {"JOHAB", JOHAB_CHARSET},
2614 {"MAC", MAC_CHARSET},
2615 {"RUSSIAN", RUSSIAN_CHARSET},
2616 {"THAI", THAI_CHARSET},
2617 {"TURKISH", TURKISH_CHARSET},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002618# ifdef VIETNAMESE_CHARSET
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 {"VIETNAMESE", VIETNAMESE_CHARSET},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002620# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 {NULL, 0}
2622};
2623
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002624struct quality_pair
2625{
2626 char *name;
2627 DWORD quality;
2628};
2629
2630static struct quality_pair
2631quality_pairs[] = {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002632# ifdef CLEARTYPE_QUALITY
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002633 {"CLEARTYPE", CLEARTYPE_QUALITY},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002634# endif
2635# ifdef ANTIALIASED_QUALITY
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002636 {"ANTIALIASED", ANTIALIASED_QUALITY},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002637# endif
2638# ifdef NONANTIALIASED_QUALITY
Bram Moolenaar73a733e2016-05-11 21:05:05 +02002639 {"NONANTIALIASED", NONANTIALIASED_QUALITY},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002640# endif
2641# ifdef PROOF_QUALITY
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002642 {"PROOF", PROOF_QUALITY},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002643# endif
2644# ifdef DRAFT_QUALITY
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002645 {"DRAFT", DRAFT_QUALITY},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002646# endif
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002647 {"DEFAULT", DEFAULT_QUALITY},
2648 {NULL, 0}
2649};
2650
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651/*
2652 * Convert a charset ID to a name.
2653 * Return NULL when not recognized.
2654 */
2655 char *
2656charset_id2name(int id)
2657{
2658 struct charset_pair *cp;
2659
2660 for (cp = charset_pairs; cp->name != NULL; ++cp)
2661 if ((BYTE)id == cp->charset)
2662 break;
2663 return cp->name;
2664}
2665
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002666/*
2667 * Convert a quality ID to a name.
2668 * Return NULL when not recognized.
2669 */
2670 char *
2671quality_id2name(DWORD id)
2672{
2673 struct quality_pair *qp;
2674
2675 for (qp = quality_pairs; qp->name != NULL; ++qp)
2676 if (id == qp->quality)
2677 break;
2678 return qp->name;
2679}
2680
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002681static const LOGFONTW s_lfDefault =
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682{
2683 -12, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, DEFAULT_CHARSET,
2684 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
2685 PROOF_QUALITY, FIXED_PITCH | FF_DONTCARE,
Bram Moolenaar0f873732019-12-05 20:28:46 +01002686 L"Fixedsys" // see _ReadVimIni
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687};
2688
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002689// Initialise the "current height" to -12 (same as s_lfDefault) just
2690// in case the user specifies a font in "guifont" with no size before a font
2691// with an explicit size has been set. This defaults the size to this value
2692// (-12 equates to roughly 9pt).
2693int current_font_height = -12; // also used in gui_w32.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694
Bram Moolenaar0f873732019-12-05 20:28:46 +01002695/*
2696 * Convert a string representing a point size into pixels. The string should
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 * be a positive decimal number, with an optional decimal point (eg, "12", or
2698 * "10.5"). The pixel value is returned, and a pointer to the next unconverted
2699 * character is stored in *end. The flag "vertical" says whether this
2700 * calculation is for a vertical (height) size or a horizontal (width) one.
2701 */
2702 static int
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002703points_to_pixels(WCHAR *str, WCHAR **end, int vertical, long_i pprinter_dc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704{
2705 int pixels;
2706 int points = 0;
2707 int divisor = 0;
2708 HWND hwnd = (HWND)0;
2709 HDC hdc;
2710 HDC printer_dc = (HDC)pprinter_dc;
2711
2712 while (*str != NUL)
2713 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002714 if (*str == L'.' && divisor == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002716 // Start keeping a divisor, for later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 divisor = 1;
2718 }
2719 else
2720 {
2721 if (!VIM_ISDIGIT(*str))
2722 break;
2723
2724 points *= 10;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002725 points += *str - L'0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 divisor *= 10;
2727 }
2728 ++str;
2729 }
2730
2731 if (divisor == 0)
2732 divisor = 1;
2733
2734 if (printer_dc == NULL)
2735 {
2736 hwnd = GetDesktopWindow();
2737 hdc = GetWindowDC(hwnd);
2738 }
2739 else
2740 hdc = printer_dc;
2741
2742 pixels = MulDiv(points,
2743 GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX),
2744 72 * divisor);
2745
2746 if (printer_dc == NULL)
2747 ReleaseDC(hwnd, hdc);
2748
2749 *end = str;
2750 return pixels;
2751}
2752
2753 static int CALLBACK
2754font_enumproc(
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002755 ENUMLOGFONTW *elf,
2756 NEWTEXTMETRICW *ntm UNUSED,
2757 DWORD type UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 LPARAM lparam)
2759{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002760 // Return value:
2761 // 0 = terminate now (monospace & ANSI)
2762 // 1 = continue, still no luck...
2763 // 2 = continue, but we have an acceptable LOGFONTW
2764 // (monospace, not ANSI)
2765 // We use these values, as EnumFontFamilies returns 1 if the
2766 // callback function is never called. So, we check the return as
2767 // 0 = perfect, 2 = OK, 1 = no good...
2768 // It's not pretty, but it works!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002770 LOGFONTW *lf = (LOGFONTW *)(lparam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002772# ifndef FEAT_PROPORTIONAL_FONTS
Bram Moolenaar0f873732019-12-05 20:28:46 +01002773 // Ignore non-monospace fonts without further ado
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 if ((ntm->tmPitchAndFamily & 1) != 0)
2775 return 1;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002776# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777
Bram Moolenaar0f873732019-12-05 20:28:46 +01002778 // Remember this LOGFONTW as a "possible"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 *lf = elf->elfLogFont;
2780
Bram Moolenaar0f873732019-12-05 20:28:46 +01002781 // Terminate the scan as soon as we find an ANSI font
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 if (lf->lfCharSet == ANSI_CHARSET
2783 || lf->lfCharSet == OEM_CHARSET
2784 || lf->lfCharSet == DEFAULT_CHARSET)
2785 return 0;
2786
Bram Moolenaar0f873732019-12-05 20:28:46 +01002787 // Continue the scan - we have a non-ANSI font
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 return 2;
2789}
2790
2791 static int
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002792init_logfont(LOGFONTW *lf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793{
2794 int n;
2795 HWND hwnd = GetDesktopWindow();
2796 HDC hdc = GetWindowDC(hwnd);
2797
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002798 n = EnumFontFamiliesW(hdc,
2799 lf->lfFaceName,
2800 (FONTENUMPROCW)font_enumproc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 (LPARAM)lf);
2802
2803 ReleaseDC(hwnd, hdc);
2804
Bram Moolenaar0f873732019-12-05 20:28:46 +01002805 // If we couldn't find a usable font, return failure
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 if (n == 1)
2807 return FAIL;
2808
Bram Moolenaar0f873732019-12-05 20:28:46 +01002809 // Tidy up the rest of the LOGFONTW structure. We set to a basic
2810 // font - get_logfont() sets bold, italic, etc based on the user's
2811 // input.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 lf->lfHeight = current_font_height;
2813 lf->lfWidth = 0;
2814 lf->lfItalic = FALSE;
2815 lf->lfUnderline = FALSE;
2816 lf->lfStrikeOut = FALSE;
2817 lf->lfWeight = FW_NORMAL;
2818
Bram Moolenaar0f873732019-12-05 20:28:46 +01002819 // Return success
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820 return OK;
2821}
2822
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002823/*
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002824 * Compare a UTF-16 string and an ASCII string literally.
2825 * Only works all the code points are inside ASCII range.
2826 */
2827 static int
2828utf16ascncmp(const WCHAR *w, const char *p, size_t n)
2829{
2830 size_t i;
2831
2832 for (i = 0; i < n; i++)
2833 {
2834 if (w[i] == 0 || w[i] != p[i])
2835 return w[i] - p[i];
2836 }
2837 return 0;
2838}
2839
2840/*
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002841 * Get font info from "name" into logfont "lf".
2842 * Return OK for a valid name, FAIL otherwise.
2843 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 int
2845get_logfont(
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002846 LOGFONTW *lf,
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002847 char_u *name,
2848 HDC printer_dc,
2849 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002851 WCHAR *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 int i;
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002853 int ret = FAIL;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002854 static LOGFONTW *lastlf = NULL;
2855 WCHAR *wname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856
2857 *lf = s_lfDefault;
2858 if (name == NULL)
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002859 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002861 wname = enc_to_utf16(name, NULL);
2862 if (wname == NULL)
2863 return FAIL;
2864
2865 if (wcscmp(wname, L"*") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002867# if defined(FEAT_GUI_MSWIN)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002868 CHOOSEFONTW cf;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002869 // if name is "*", bring up std font dialog:
Bram Moolenaara80faa82020-04-12 19:37:17 +02002870 CLEAR_FIELD(cf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 cf.lStructSize = sizeof(cf);
2872 cf.hwndOwner = s_hwnd;
2873 cf.Flags = CF_SCREENFONTS | CF_FIXEDPITCHONLY | CF_INITTOLOGFONTSTRUCT;
2874 if (lastlf != NULL)
2875 *lf = *lastlf;
2876 cf.lpLogFont = lf;
2877 cf.nFontType = 0 ; //REGULAR_FONTTYPE;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002878 if (ChooseFontW(&cf))
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002879 ret = OK;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002880# endif
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002881 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 }
2883
2884 /*
2885 * Split name up, it could be <name>:h<height>:w<width> etc.
2886 */
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002887 for (p = wname; *p && *p != L':'; p++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002889 if (p - wname + 1 >= LF_FACESIZE)
Bram Moolenaar0f873732019-12-05 20:28:46 +01002890 goto theend; // Name too long
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002891 lf->lfFaceName[p - wname] = *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892 }
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002893 if (p != wname)
2894 lf->lfFaceName[p - wname] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895
Bram Moolenaar0f873732019-12-05 20:28:46 +01002896 // First set defaults
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 lf->lfHeight = -12;
2898 lf->lfWidth = 0;
2899 lf->lfWeight = FW_NORMAL;
2900 lf->lfItalic = FALSE;
2901 lf->lfUnderline = FALSE;
2902 lf->lfStrikeOut = FALSE;
2903
2904 /*
2905 * If the font can't be found, try replacing '_' by ' '.
2906 */
2907 if (init_logfont(lf) == FAIL)
2908 {
2909 int did_replace = FALSE;
2910
2911 for (i = 0; lf->lfFaceName[i]; ++i)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002912 if (lf->lfFaceName[i] == L'_')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002914 lf->lfFaceName[i] = L' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 did_replace = TRUE;
2916 }
2917 if (!did_replace || init_logfont(lf) == FAIL)
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002918 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 }
2920
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002921 while (*p == L':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 p++;
2923
Bram Moolenaar0f873732019-12-05 20:28:46 +01002924 // Set the values found after ':'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925 while (*p)
2926 {
2927 switch (*p++)
2928 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002929 case L'h':
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002930 lf->lfHeight = - points_to_pixels(p, &p, TRUE, (long_i)printer_dc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002932 case L'w':
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002933 lf->lfWidth = points_to_pixels(p, &p, FALSE, (long_i)printer_dc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934 break;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02002935 case L'W':
2936 lf->lfWeight = wcstol(p, &p, 10);
2937 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002938 case L'b':
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 lf->lfWeight = FW_BOLD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002941 case L'i':
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 lf->lfItalic = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002944 case L'u':
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 lf->lfUnderline = TRUE;
2946 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002947 case L's':
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948 lf->lfStrikeOut = TRUE;
2949 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002950 case L'c':
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 {
2952 struct charset_pair *cp;
2953
2954 for (cp = charset_pairs; cp->name != NULL; ++cp)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002955 if (utf16ascncmp(p, cp->name, strlen(cp->name)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 {
2957 lf->lfCharSet = cp->charset;
2958 p += strlen(cp->name);
2959 break;
2960 }
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002961 if (cp->name == NULL && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002963 char_u *s = utf16_to_enc(p, NULL);
2964 semsg(_("E244: Illegal charset name \"%s\" in font name \"%s\""), s, name);
2965 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966 break;
2967 }
2968 break;
2969 }
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002970 case L'q':
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002971 {
2972 struct quality_pair *qp;
2973
2974 for (qp = quality_pairs; qp->name != NULL; ++qp)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002975 if (utf16ascncmp(p, qp->name, strlen(qp->name)) == 0)
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002976 {
2977 lf->lfQuality = qp->quality;
2978 p += strlen(qp->name);
2979 break;
2980 }
2981 if (qp->name == NULL && verbose)
2982 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002983 char_u *s = utf16_to_enc(p, NULL);
2984 semsg(_("E244: Illegal quality name \"%s\" in font name \"%s\""), s, name);
2985 vim_free(s);
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002986 break;
2987 }
2988 break;
2989 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990 default:
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002991 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002992 semsg(_("E245: Illegal char '%c' in font name \"%s\""), p[-1], name);
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002993 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994 }
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002995 while (*p == L':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996 p++;
2997 }
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002998 ret = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000theend:
Bram Moolenaar0f873732019-12-05 20:28:46 +01003001 // ron: init lastlf
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003002 if (ret == OK && printer_dc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003 {
3004 vim_free(lastlf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003005 lastlf = ALLOC_ONE(LOGFONTW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006 if (lastlf != NULL)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003007 mch_memmove(lastlf, lf, sizeof(LOGFONTW));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008 }
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003009 vim_free(wname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003011 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012}
3013
Bram Moolenaar0f873732019-12-05 20:28:46 +01003014#endif // defined(FEAT_GUI) || defined(FEAT_PRINTER)
Bram Moolenaarf12d9832016-01-29 21:11:25 +01003015
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003016#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaarf12d9832016-01-29 21:11:25 +01003017/*
3018 * Initialize the Winsock dll.
3019 */
3020 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003021channel_init_winsock(void)
Bram Moolenaarf12d9832016-01-29 21:11:25 +01003022{
3023 WSADATA wsaData;
3024 int wsaerr;
3025
3026 if (WSInitialized)
3027 return;
3028
3029 wsaerr = WSAStartup(MAKEWORD(2, 2), &wsaData);
3030 if (wsaerr == 0)
3031 WSInitialized = TRUE;
3032}
3033#endif