blob: 360c456e5cc924f3c46b046fadfa890b337147ae [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.
392 return ((ASCII_ISALPHA(fname[0]) && fname[1] == ':'
393 && (fname[2] == '/' || fname[2] == '\\'))
394 || (fname[0] == fname[1] && (fname[0] == '/' || fname[0] == '\\')));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395}
396
397/*
398 * Replace all slashes by backslashes.
399 * This used to be the other way around, but MS-DOS sometimes has problems
400 * with slashes (e.g. in a command name). We can't have mixed slashes and
401 * backslashes, because comparing file names will not work correctly. The
402 * commands that use a file name should try to avoid the need to type a
403 * backslash twice.
404 * When 'shellslash' set do it the other way around.
Bram Moolenaarb4f6a462015-10-13 19:43:17 +0200405 * When the path looks like a URL leave it unmodified.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406 */
407 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100408slash_adjust(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409{
Bram Moolenaarb4f6a462015-10-13 19:43:17 +0200410 if (path_with_url(p))
411 return;
Bram Moolenaar39d21e32017-08-05 23:09:31 +0200412
413 if (*p == '`')
414 {
Bram Moolenaar116a0f82017-08-07 21:17:57 +0200415 size_t len = STRLEN(p);
416
Bram Moolenaar0f873732019-12-05 20:28:46 +0100417 // don't replace backslash in backtick quoted strings
Bram Moolenaar39d21e32017-08-05 23:09:31 +0200418 if (len > 2 && *(p + len - 1) == '`')
419 return;
420 }
421
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +0000422 while (*p)
423 {
424 if (*p == psepcN)
425 *p = psepc;
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100426 MB_PTR_ADV(p);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +0000427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428}
429
Bram Moolenaar0f873732019-12-05 20:28:46 +0100430// Use 64-bit stat functions if available.
Bram Moolenaar8767f522016-07-01 17:17:39 +0200431#ifdef HAVE_STAT64
432# undef stat
433# undef _stat
434# undef _wstat
435# undef _fstat
436# define stat _stat64
437# define _stat _stat64
438# define _wstat _wstat64
439# define _fstat _fstat64
440#endif
441
Bram Moolenaar9d1685d2014-01-14 12:18:45 +0100442#if (defined(_MSC_VER) && (_MSC_VER >= 1300)) || defined(__MINGW32__)
Bram Moolenaar8962fda2013-09-29 19:05:21 +0200443# define OPEN_OH_ARGTYPE intptr_t
444#else
445# define OPEN_OH_ARGTYPE long
446#endif
447
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200448 static int
Bram Moolenaar8767f522016-07-01 17:17:39 +0200449wstat_symlink_aware(const WCHAR *name, stat_T *stp)
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200450{
Bram Moolenaara12a1612019-01-24 16:39:02 +0100451#if (defined(_MSC_VER) && (_MSC_VER < 1900)) || defined(__MINGW32__)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100452 // Work around for VC12 or earlier (and MinGW). _wstat() can't handle
453 // symlinks properly.
454 // VC9 or earlier: _wstat() doesn't support a symlink at all. It retrieves
455 // status of a symlink itself.
456 // VC10: _wstat() supports a symlink to a normal file, but it doesn't
457 // support a symlink to a directory (always returns an error).
458 // VC11 and VC12: _wstat() doesn't return an error for a symlink to a
459 // directory, but it doesn't set S_IFDIR flag.
460 // MinGW: Same as VC9.
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200461 int n;
462 BOOL is_symlink = FALSE;
463 HANDLE hFind, h;
464 DWORD attr = 0;
465 WIN32_FIND_DATAW findDataW;
466
467 hFind = FindFirstFileW(name, &findDataW);
468 if (hFind != INVALID_HANDLE_VALUE)
469 {
470 attr = findDataW.dwFileAttributes;
471 if ((attr & FILE_ATTRIBUTE_REPARSE_POINT)
472 && (findDataW.dwReserved0 == IO_REPARSE_TAG_SYMLINK))
473 is_symlink = TRUE;
474 FindClose(hFind);
475 }
476 if (is_symlink)
477 {
478 h = CreateFileW(name, FILE_READ_ATTRIBUTES,
479 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
480 OPEN_EXISTING,
481 (attr & FILE_ATTRIBUTE_DIRECTORY)
482 ? FILE_FLAG_BACKUP_SEMANTICS : 0,
483 NULL);
484 if (h != INVALID_HANDLE_VALUE)
485 {
486 int fd;
487
Bram Moolenaar8962fda2013-09-29 19:05:21 +0200488 fd = _open_osfhandle((OPEN_OH_ARGTYPE)h, _O_RDONLY);
Bram Moolenaar8767f522016-07-01 17:17:39 +0200489 n = _fstat(fd, (struct _stat *)stp);
Bram Moolenaarfce7b3d2016-01-19 19:00:32 +0100490 if ((n == 0) && (attr & FILE_ATTRIBUTE_DIRECTORY))
491 stp->st_mode = (stp->st_mode & ~S_IFREG) | S_IFDIR;
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200492 _close(fd);
493 return n;
494 }
495 }
Bram Moolenaara12a1612019-01-24 16:39:02 +0100496#endif
Bram Moolenaar8767f522016-07-01 17:17:39 +0200497 return _wstat(name, (struct _stat *)stp);
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200498}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499
500/*
501 * stat() can't handle a trailing '/' or '\', remove it first.
502 */
503 int
Bram Moolenaar8767f522016-07-01 17:17:39 +0200504vim_stat(const char *name, stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100506 // WinNT and later can use _MAX_PATH wide characters for a pathname, which
507 // means that the maximum pathname is _MAX_PATH * 3 bytes when 'enc' is
508 // UTF-8.
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100509 char_u buf[_MAX_PATH * 3 + 1];
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100510 char_u *p;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200511 WCHAR *wp;
512 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513
Bram Moolenaard2a203b2013-08-30 16:51:18 +0200514 vim_strncpy((char_u *)buf, (char_u *)name, sizeof(buf) - 1);
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100515 p = buf + STRLEN(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516 if (p > buf)
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100517 MB_PTR_BACK(buf, p);
Bram Moolenaarb1cb35f2014-01-10 13:05:20 +0100518
Bram Moolenaar0f873732019-12-05 20:28:46 +0100519 // Remove trailing '\\' except root path.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520 if (p > buf && (*p == '\\' || *p == '/') && p[-1] != ':')
521 *p = NUL;
Bram Moolenaarb1cb35f2014-01-10 13:05:20 +0100522
523 if ((buf[0] == '\\' && buf[1] == '\\') || (buf[0] == '/' && buf[1] == '/'))
524 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100525 // UNC root path must be followed by '\\'.
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100526 p = vim_strpbrk(buf + 2, (char_u *)"\\/");
Bram Moolenaarb1cb35f2014-01-10 13:05:20 +0100527 if (p != NULL)
528 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100529 p = vim_strpbrk(p + 1, (char_u *)"\\/");
Bram Moolenaarb1cb35f2014-01-10 13:05:20 +0100530 if (p == NULL)
531 STRCAT(buf, "\\");
532 }
533 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200535 wp = enc_to_utf16(buf, NULL);
536 if (wp == NULL)
537 return -1;
538
539 n = wstat_symlink_aware(wp, stp);
540 vim_free(wp);
541 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542}
543
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200544#if (defined(FEAT_GUI_MSWIN) && !defined(VIMDLL)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 void
Bram Moolenaar26e86442020-05-17 14:06:16 +0200546mch_settmode(tmode_T tmode UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100548 // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549}
550
551 int
552mch_get_shellsize(void)
553{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100554 // never used
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555 return OK;
556}
557
558 void
559mch_set_shellsize(void)
560{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100561 // never used
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562}
563
564/*
565 * Rows and/or Columns has changed.
566 */
567 void
568mch_new_shellsize(void)
569{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100570 // never used
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571}
572
573#endif
574
575/*
576 * We have no job control, so fake it by starting a new shell.
577 */
578 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100579mch_suspend(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580{
581 suspend_shell();
582}
583
584#if defined(USE_MCH_ERRMSG) || defined(PROTO)
585
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200586# ifdef display_errors
587# undef display_errors
588# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589
590/*
591 * Display the saved error message(s).
592 */
593 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100594display_errors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595{
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200596# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597 char *p;
598
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200599# ifdef VIMDLL
600 if (gui.in_use || gui.starting)
601# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200603 if (error_ga.ga_data != NULL)
604 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100605 // avoid putting up a message box with blanks only
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200606 for (p = (char *)error_ga.ga_data; *p; ++p)
607 if (!isspace(*p))
608 {
609 (void)gui_mch_dialog(
Bram Moolenaarc17ef8e2006-03-25 21:48:58 +0000610 gui.starting ? VIM_INFO :
Bram Moolenaarc17ef8e2006-03-25 21:48:58 +0000611 VIM_ERROR,
Bram Moolenaarc17ef8e2006-03-25 21:48:58 +0000612 gui.starting ? (char_u *)_("Message") :
Bram Moolenaarc17ef8e2006-03-25 21:48:58 +0000613 (char_u *)_("Error"),
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100614 (char_u *)p, (char_u *)_("&Ok"),
615 1, NULL, FALSE);
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200616 break;
617 }
618 ga_clear(&error_ga);
619 }
620 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200622# endif
623# if !defined(FEAT_GUI) || defined(VIMDLL)
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +0100624 FlushFileBuffers(GetStdHandle(STD_ERROR_HANDLE));
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200625# endif
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +0100626}
627#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628
629
630/*
631 * Return TRUE if "p" contain a wildcard that can be expanded by
632 * dos_expandpath().
633 */
634 int
635mch_has_exp_wildcard(char_u *p)
636{
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100637 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 {
639 if (vim_strchr((char_u *)"?*[", *p) != NULL
640 || (*p == '~' && p[1] != NUL))
641 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 }
643 return FALSE;
644}
645
646/*
647 * Return TRUE if "p" contain a wildcard or a "~1" kind of thing (could be a
648 * shortened file name).
649 */
650 int
651mch_has_wildcard(char_u *p)
652{
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100653 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 {
655 if (vim_strchr((char_u *)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100656#ifdef VIM_BACKTICK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657 "?*$[`"
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100658#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 "?*$["
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100660#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 , *p) != NULL
662 || (*p == '~' && p[1] != NUL))
663 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 }
665 return FALSE;
666}
667
668
669/*
670 * The normal _chdir() does not change the default drive. This one does.
671 * Returning 0 implies success; -1 implies failure.
672 */
673 int
674mch_chdir(char *path)
675{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200676 WCHAR *p;
677 int n;
678
Bram Moolenaar0f873732019-12-05 20:28:46 +0100679 if (path[0] == NUL) // just checking...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 return -1;
681
Bram Moolenaara2974d72009-07-14 16:38:36 +0000682 if (p_verbose >= 5)
683 {
684 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100685 smsg("chdir(%s)", path);
Bram Moolenaara2974d72009-07-14 16:38:36 +0000686 verbose_leave();
687 }
Bram Moolenaar0f873732019-12-05 20:28:46 +0100688 if (isalpha(path[0]) && path[1] == ':') // has a drive name
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100690 // If we can change to the drive, skip that part of the path. If we
691 // can't then the current directory may be invalid, try using chdir()
692 // with the whole path.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 if (_chdrive(TOLOWER_ASC(path[0]) - 'a' + 1) == 0)
694 path += 2;
695 }
696
Bram Moolenaar0f873732019-12-05 20:28:46 +0100697 if (*path == NUL) // drive name only
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 return 0;
699
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200700 p = enc_to_utf16((char_u *)path, NULL);
701 if (p == NULL)
702 return -1;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000703
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200704 n = _wchdir(p);
705 vim_free(p);
706 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707}
708
709
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200710#if defined(FEAT_GUI_MSWIN) && !defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711/*
712 * return non-zero if a character is available
713 */
714 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100715mch_char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100717 // never used
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718 return TRUE;
719}
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200720
721# if defined(FEAT_TERMINAL) || defined(PROTO)
722/*
723 * Check for any pending input or messages.
724 */
725 int
726mch_check_messages(void)
727{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100728 // TODO: check for messages
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200729 return TRUE;
730}
731# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732#endif
733
734
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735#if defined(FEAT_LIBCALL) || defined(PROTO)
736/*
737 * Call a DLL routine which takes either a string or int param
738 * and returns an allocated string.
739 * Return OK if it worked, FAIL if not.
740 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741typedef LPTSTR (*MYSTRPROCSTR)(LPTSTR);
742typedef LPTSTR (*MYINTPROCSTR)(int);
743typedef int (*MYSTRPROCINT)(LPTSTR);
744typedef int (*MYINTPROCINT)(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746/*
747 * Check if a pointer points to a valid NUL terminated string.
748 * Return the length of the string, including terminating NUL.
749 * Returns 0 for an invalid pointer, 1 for an empty string.
750 */
751 static size_t
752check_str_len(char_u *str)
753{
754 SYSTEM_INFO si;
755 MEMORY_BASIC_INFORMATION mbi;
756 size_t length = 0;
757 size_t i;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100758 const char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759
Bram Moolenaar0f873732019-12-05 20:28:46 +0100760 // get page size
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 GetSystemInfo(&si);
762
Bram Moolenaar0f873732019-12-05 20:28:46 +0100763 // get memory information
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764 if (VirtualQuery(str, &mbi, sizeof(mbi)))
765 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100766 // pre cast these (typing savers)
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000767 long_u dwStr = (long_u)str;
768 long_u dwBaseAddress = (long_u)mbi.BaseAddress;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769
Bram Moolenaar0f873732019-12-05 20:28:46 +0100770 // get start address of page that str is on
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000771 long_u strPage = dwStr - (dwStr - dwBaseAddress) % si.dwPageSize;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772
Bram Moolenaar0f873732019-12-05 20:28:46 +0100773 // get length from str to end of page
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000774 long_u pageLength = si.dwPageSize - (dwStr - strPage);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775
Bram Moolenaar442b4222010-05-24 21:34:22 +0200776 for (p = str; !IsBadReadPtr(p, (UINT)pageLength);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 p += pageLength, pageLength = si.dwPageSize)
778 for (i = 0; i < pageLength; ++i, ++length)
779 if (p[i] == NUL)
780 return length + 1;
781 }
782
783 return 0;
784}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200786/*
787 * Passed to do_in_runtimepath() to load a vim.ico file.
788 */
789 static void
790mch_icon_load_cb(char_u *fname, void *cookie)
791{
792 HANDLE *h = (HANDLE *)cookie;
793
794 *h = LoadImage(NULL,
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100795 (LPSTR)fname,
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200796 IMAGE_ICON,
797 64,
798 64,
799 LR_LOADFROMFILE | LR_LOADMAP3DCOLORS);
800}
801
802/*
803 * Try loading an icon file from 'runtimepath'.
804 */
805 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100806mch_icon_load(HANDLE *iconp)
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200807{
808 return do_in_runtimepath((char_u *)"bitmaps/vim.ico",
Bram Moolenaar7f8989d2016-03-12 22:11:39 +0100809 0, mch_icon_load_cb, iconp);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200810}
811
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812 int
813mch_libcall(
814 char_u *libname,
815 char_u *funcname,
Bram Moolenaar0f873732019-12-05 20:28:46 +0100816 char_u *argstring, // NULL when using a argint
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 int argint,
Bram Moolenaar0f873732019-12-05 20:28:46 +0100818 char_u **string_result,// NULL when using number_result
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 int *number_result)
820{
821 HINSTANCE hinstLib;
822 MYSTRPROCSTR ProcAdd;
823 MYINTPROCSTR ProcAddI;
824 char_u *retval_str = NULL;
825 int retval_int = 0;
826 size_t len;
827
828 BOOL fRunTimeLinkSuccess = FALSE;
829
830 // Get a handle to the DLL module.
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100831 hinstLib = vimLoadLib((char *)libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832
833 // If the handle is valid, try to get the function address.
834 if (hinstLib != NULL)
835 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100836# ifdef HAVE_TRY_EXCEPT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 __try
838 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100839# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 if (argstring != NULL)
841 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100842 // Call with string argument
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100843 ProcAdd = (MYSTRPROCSTR)GetProcAddress(hinstLib, (LPCSTR)funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844 if ((fRunTimeLinkSuccess = (ProcAdd != NULL)) != 0)
845 {
846 if (string_result == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100847 retval_int = ((MYSTRPROCINT)ProcAdd)((LPSTR)argstring);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100849 retval_str = (char_u *)(ProcAdd)((LPSTR)argstring);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 }
851 }
852 else
853 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100854 // Call with number argument
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100855 ProcAddI = (MYINTPROCSTR) GetProcAddress(hinstLib, (LPCSTR)funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856 if ((fRunTimeLinkSuccess = (ProcAddI != NULL)) != 0)
857 {
858 if (string_result == NULL)
859 retval_int = ((MYINTPROCINT)ProcAddI)(argint);
860 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100861 retval_str = (char_u *)(ProcAddI)(argint);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862 }
863 }
864
865 // Save the string before we free the library.
866 // Assume that a "1" result is an illegal pointer.
867 if (string_result == NULL)
868 *number_result = retval_int;
869 else if (retval_str != NULL
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100870 && (len = check_str_len(retval_str)) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871 {
Bram Moolenaar18a4ba22019-05-24 19:39:03 +0200872 *string_result = alloc(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 if (*string_result != NULL)
874 mch_memmove(*string_result, retval_str, len);
875 }
876
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100877# ifdef HAVE_TRY_EXCEPT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 }
879 __except(EXCEPTION_EXECUTE_HANDLER)
880 {
881 if (GetExceptionCode() == EXCEPTION_STACK_OVERFLOW)
882 RESETSTKOFLW();
883 fRunTimeLinkSuccess = 0;
884 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100885# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886
887 // Free the DLL module.
888 (void)FreeLibrary(hinstLib);
889 }
890
891 if (!fRunTimeLinkSuccess)
892 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100893 semsg(_(e_libcall), funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 return FAIL;
895 }
896
897 return OK;
898}
899#endif
900
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901/*
902 * Debugging helper: expose the MCH_WRITE_DUMP stuff to other modules
903 */
904 void
Bram Moolenaar1266d672017-02-01 13:43:36 +0100905DumpPutS(const char *psz UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100907#ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 if (fdDump)
909 {
910 fputs(psz, fdDump);
911 if (psz[strlen(psz) - 1] != '\n')
912 fputc('\n', fdDump);
913 fflush(fdDump);
914 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100915#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916}
917
918#ifdef _DEBUG
919
920void __cdecl
921Trace(
922 char *pszFormat,
923 ...)
924{
925 CHAR szBuff[2048];
926 va_list args;
927
928 va_start(args, pszFormat);
929 vsprintf(szBuff, pszFormat, args);
930 va_end(args);
931
932 OutputDebugString(szBuff);
933}
934
935#endif //_DEBUG
936
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200937#if !defined(FEAT_GUI) || defined(VIMDLL) || defined(PROTO)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100938extern HWND g_hWnd; // This is in os_win32.c.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939
940/*
941 * Showing the printer dialog is tricky since we have no GUI
942 * window to parent it. The following routines are needed to
943 * get the window parenting and Z-order to work properly.
944 */
945 static void
946GetConsoleHwnd(void)
947{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100948 // Skip if it's already set.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 if (s_hwnd != 0)
950 return;
951
Bram Moolenaar0f873732019-12-05 20:28:46 +0100952 // Window handle may have been found by init code (Windows NT only)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 if (g_hWnd != 0)
954 {
955 s_hwnd = g_hWnd;
956 return;
957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958
Bram Moolenaare1ed53f2019-02-12 23:12:37 +0100959 s_hwnd = GetConsoleWindow();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960}
Bram Moolenaar843ee412004-06-30 16:16:41 +0000961
962/*
963 * Console implementation of ":winpos".
964 */
965 int
966mch_get_winpos(int *x, int *y)
967{
968 RECT rect;
969
970 GetConsoleHwnd();
971 GetWindowRect(s_hwnd, &rect);
972 *x = rect.left;
973 *y = rect.top;
974 return OK;
975}
976
977/*
978 * Console implementation of ":winpos x y".
979 */
980 void
981mch_set_winpos(int x, int y)
982{
983 GetConsoleHwnd();
984 SetWindowPos(s_hwnd, NULL, x, y, 0, 0,
985 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
986}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987#endif
988
989#if (defined(FEAT_PRINTER) && !defined(FEAT_POSTSCRIPT)) || defined(PROTO)
990
Bram Moolenaar0f873732019-12-05 20:28:46 +0100991//=================================================================
992// Win32 printer stuff
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993
994static HFONT prt_font_handles[2][2][2];
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200995static PRINTDLGW prt_dlg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996static const int boldface[2] = {FW_REGULAR, FW_BOLD};
997static TEXTMETRIC prt_tm;
998static int prt_line_height;
999static int prt_number_width;
1000static int prt_left_margin;
1001static int prt_right_margin;
1002static int prt_top_margin;
1003static char_u szAppName[] = TEXT("VIM");
1004static HWND hDlgPrint;
1005static int *bUserAbort = NULL;
1006static char_u *prt_name = NULL;
1007
Bram Moolenaar0f873732019-12-05 20:28:46 +01001008// Defines which are also in vim.rc.
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001009# define IDC_BOX1 400
1010# define IDC_PRINTTEXT1 401
1011# define IDC_PRINTTEXT2 402
1012# define IDC_PROGRESS 403
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001014 static BOOL
1015vimSetDlgItemText(HWND hDlg, int nIDDlgItem, char_u *s)
1016{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001017 WCHAR *wp;
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001018 BOOL ret;
1019
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001020 wp = enc_to_utf16(s, NULL);
1021 if (wp == NULL)
1022 return FALSE;
1023
1024 ret = SetDlgItemTextW(hDlg, nIDDlgItem, wp);
1025 vim_free(wp);
1026 return ret;
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001027}
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001028
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029/*
1030 * Convert BGR to RGB for Windows GDI calls
1031 */
1032 static COLORREF
1033swap_me(COLORREF colorref)
1034{
1035 int temp;
1036 char *ptr = (char *)&colorref;
1037
1038 temp = *(ptr);
1039 *(ptr ) = *(ptr + 2);
1040 *(ptr + 2) = temp;
1041 return colorref;
1042}
1043
Bram Moolenaar0f873732019-12-05 20:28:46 +01001044// Attempt to make this work for old and new compilers
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001045# if !defined(_WIN64) && (!defined(_MSC_VER) || _MSC_VER < 1300)
1046# define PDP_RETVAL BOOL
1047# else
1048# define PDP_RETVAL INT_PTR
1049# endif
Bram Moolenaared39e1d2008-08-09 17:55:22 +00001050
Bram Moolenaared39e1d2008-08-09 17:55:22 +00001051 static PDP_RETVAL CALLBACK
Bram Moolenaar1266d672017-02-01 13:43:36 +01001052PrintDlgProc(
1053 HWND hDlg,
1054 UINT message,
1055 WPARAM wParam UNUSED,
1056 LPARAM lParam UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001058# ifdef FEAT_GETTEXT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 NONCLIENTMETRICS nm;
1060 static HFONT hfont;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001061# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062
1063 switch (message)
1064 {
1065 case WM_INITDIALOG:
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001066# ifdef FEAT_GETTEXT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 nm.cbSize = sizeof(NONCLIENTMETRICS);
1068 if (SystemParametersInfo(
1069 SPI_GETNONCLIENTMETRICS,
1070 sizeof(NONCLIENTMETRICS),
1071 &nm,
1072 0))
1073 {
1074 char buff[MAX_PATH];
1075 int i;
1076
Bram Moolenaar0f873732019-12-05 20:28:46 +01001077 // Translate the dialog texts
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 hfont = CreateFontIndirect(&nm.lfMessageFont);
1079 for (i = IDC_PRINTTEXT1; i <= IDC_PROGRESS; i++)
1080 {
1081 SendDlgItemMessage(hDlg, i, WM_SETFONT, (WPARAM)hfont, 1);
1082 if (GetDlgItemText(hDlg,i, buff, sizeof(buff)))
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001083 vimSetDlgItemText(hDlg,i, (char_u *)_(buff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 }
1085 SendDlgItemMessage(hDlg, IDCANCEL,
1086 WM_SETFONT, (WPARAM)hfont, 1);
1087 if (GetDlgItemText(hDlg,IDCANCEL, buff, sizeof(buff)))
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001088 vimSetDlgItemText(hDlg,IDCANCEL, (char_u *)_(buff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001090# endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001091 SetWindowText(hDlg, (LPCSTR)szAppName);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 if (prt_name != NULL)
1093 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001094 vimSetDlgItemText(hDlg, IDC_PRINTTEXT2, (char_u *)prt_name);
Bram Moolenaard23a8232018-02-10 18:45:26 +01001095 VIM_CLEAR(prt_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 }
1097 EnableMenuItem(GetSystemMenu(hDlg, FALSE), SC_CLOSE, MF_GRAYED);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001098# if !defined(FEAT_GUI) || defined(VIMDLL)
1099# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001100 if (!gui.in_use)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001101# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001102 BringWindowToTop(s_hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001103# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 return TRUE;
1105
1106 case WM_COMMAND:
1107 *bUserAbort = TRUE;
1108 EnableWindow(GetParent(hDlg), TRUE);
1109 DestroyWindow(hDlg);
1110 hDlgPrint = NULL;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001111# ifdef FEAT_GETTEXT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 DeleteObject(hfont);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001113# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 return TRUE;
1115 }
1116 return FALSE;
1117}
1118
1119 static BOOL CALLBACK
Bram Moolenaar1266d672017-02-01 13:43:36 +01001120AbortProc(HDC hdcPrn UNUSED, int iCode UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121{
1122 MSG msg;
1123
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02001124 while (!*bUserAbort && pPeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 {
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02001126 if (!hDlgPrint || !pIsDialogMessage(hDlgPrint, &msg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 {
1128 TranslateMessage(&msg);
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02001129 pDispatchMessage(&msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 }
1131 }
1132 return !*bUserAbort;
1133}
1134
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001135# if !defined(FEAT_GUI) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136
Bram Moolenaar26fdd7d2011-11-30 13:42:44 +01001137 static UINT_PTR CALLBACK
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138PrintHookProc(
1139 HWND hDlg, // handle to dialog box
1140 UINT uiMsg, // message identifier
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02001141 WPARAM wParam UNUSED, // message parameter
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 LPARAM lParam // message parameter
1143 )
1144{
1145 HWND hwndOwner;
1146 RECT rc, rcDlg, rcOwner;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001147 PRINTDLGW *pPD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148
1149 if (uiMsg == WM_INITDIALOG)
1150 {
1151 // Get the owner window and dialog box rectangles.
1152 if ((hwndOwner = GetParent(hDlg)) == NULL)
1153 hwndOwner = GetDesktopWindow();
1154
1155 GetWindowRect(hwndOwner, &rcOwner);
1156 GetWindowRect(hDlg, &rcDlg);
1157 CopyRect(&rc, &rcOwner);
1158
1159 // Offset the owner and dialog box rectangles so that
1160 // right and bottom values represent the width and
1161 // height, and then offset the owner again to discard
1162 // space taken up by the dialog box.
1163
1164 OffsetRect(&rcDlg, -rcDlg.left, -rcDlg.top);
1165 OffsetRect(&rc, -rc.left, -rc.top);
1166 OffsetRect(&rc, -rcDlg.right, -rcDlg.bottom);
1167
1168 // The new position is the sum of half the remaining
1169 // space and the owner's original position.
1170
1171 SetWindowPos(hDlg,
1172 HWND_TOP,
1173 rcOwner.left + (rc.right / 2),
1174 rcOwner.top + (rc.bottom / 2),
1175 0, 0, // ignores size arguments
1176 SWP_NOSIZE);
1177
Bram Moolenaar0f873732019-12-05 20:28:46 +01001178 // tackle the printdlg copiesctrl problem
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001179 pPD = (PRINTDLGW *)lParam;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 pPD->nCopies = (WORD)pPD->lCustData;
1181 SetDlgItemInt( hDlg, edt3, pPD->nCopies, FALSE );
Bram Moolenaar0f873732019-12-05 20:28:46 +01001182 // Bring the window to top
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 BringWindowToTop(GetParent(hDlg));
1184 SetForegroundWindow(hDlg);
1185 }
1186
1187 return FALSE;
1188}
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001189# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190
1191 void
1192mch_print_cleanup(void)
1193{
1194 int pifItalic;
1195 int pifBold;
1196 int pifUnderline;
1197
1198 for (pifBold = 0; pifBold <= 1; pifBold++)
1199 for (pifItalic = 0; pifItalic <= 1; pifItalic++)
1200 for (pifUnderline = 0; pifUnderline <= 1; pifUnderline++)
1201 DeleteObject(prt_font_handles[pifBold][pifItalic][pifUnderline]);
1202
1203 if (prt_dlg.hDC != NULL)
1204 DeleteDC(prt_dlg.hDC);
1205 if (!*bUserAbort)
1206 SendMessage(hDlgPrint, WM_COMMAND, 0, 0);
1207}
1208
1209 static int
1210to_device_units(int idx, int dpi, int physsize, int offset, int def_number)
1211{
1212 int ret = 0;
1213 int u;
1214 int nr;
1215
1216 u = prt_get_unit(idx);
1217 if (u == PRT_UNIT_NONE)
1218 {
1219 u = PRT_UNIT_PERC;
1220 nr = def_number;
1221 }
1222 else
1223 nr = printer_opts[idx].number;
1224
1225 switch (u)
1226 {
1227 case PRT_UNIT_PERC:
1228 ret = (physsize * nr) / 100;
1229 break;
1230 case PRT_UNIT_INCH:
1231 ret = (nr * dpi);
1232 break;
1233 case PRT_UNIT_MM:
1234 ret = (nr * 10 * dpi) / 254;
1235 break;
1236 case PRT_UNIT_POINT:
1237 ret = (nr * 10 * dpi) / 720;
1238 break;
1239 }
1240
1241 if (ret < offset)
1242 return 0;
1243 else
1244 return ret - offset;
1245}
1246
1247 static int
1248prt_get_cpl(void)
1249{
1250 int hr;
1251 int phyw;
1252 int dvoff;
1253 int rev_offset;
1254 int dpi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255
1256 GetTextMetrics(prt_dlg.hDC, &prt_tm);
1257 prt_line_height = prt_tm.tmHeight + prt_tm.tmExternalLeading;
1258
1259 hr = GetDeviceCaps(prt_dlg.hDC, HORZRES);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 phyw = GetDeviceCaps(prt_dlg.hDC, PHYSICALWIDTH);
1261 dvoff = GetDeviceCaps(prt_dlg.hDC, PHYSICALOFFSETX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 dpi = GetDeviceCaps(prt_dlg.hDC, LOGPIXELSX);
1263
1264 rev_offset = phyw - (dvoff + hr);
1265
1266 prt_left_margin = to_device_units(OPT_PRINT_LEFT, dpi, phyw, dvoff, 10);
1267 if (prt_use_number())
1268 {
1269 prt_number_width = PRINT_NUMBER_WIDTH * prt_tm.tmAveCharWidth;
1270 prt_left_margin += prt_number_width;
1271 }
1272 else
1273 prt_number_width = 0;
1274
1275 prt_right_margin = hr - to_device_units(OPT_PRINT_RIGHT, dpi, phyw,
1276 rev_offset, 5);
1277
1278 return (prt_right_margin - prt_left_margin) / prt_tm.tmAveCharWidth;
1279}
1280
1281 static int
1282prt_get_lpp(void)
1283{
1284 int vr;
1285 int phyw;
1286 int dvoff;
1287 int rev_offset;
1288 int bottom_margin;
1289 int dpi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290
1291 vr = GetDeviceCaps(prt_dlg.hDC, VERTRES);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 phyw = GetDeviceCaps(prt_dlg.hDC, PHYSICALHEIGHT);
1293 dvoff = GetDeviceCaps(prt_dlg.hDC, PHYSICALOFFSETY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 dpi = GetDeviceCaps(prt_dlg.hDC, LOGPIXELSY);
1295
1296 rev_offset = phyw - (dvoff + vr);
1297
1298 prt_top_margin = to_device_units(OPT_PRINT_TOP, dpi, phyw, dvoff, 5);
1299
Bram Moolenaar0f873732019-12-05 20:28:46 +01001300 // adjust top margin if there is a header
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 prt_top_margin += prt_line_height * prt_header_height();
1302
1303 bottom_margin = vr - to_device_units(OPT_PRINT_BOT, dpi, phyw,
1304 rev_offset, 5);
1305
1306 return (bottom_margin - prt_top_margin) / prt_line_height;
1307}
1308
1309 int
1310mch_print_init(prt_settings_T *psettings, char_u *jobname, int forceit)
1311{
1312 static HGLOBAL stored_dm = NULL;
1313 static HGLOBAL stored_devn = NULL;
1314 static int stored_nCopies = 1;
1315 static int stored_nFlags = 0;
1316
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001317 LOGFONTW fLogFont;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 int pifItalic;
1319 int pifBold;
1320 int pifUnderline;
1321
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001322 DEVMODEW *mem;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 DEVNAMES *devname;
1324 int i;
1325
1326 bUserAbort = &(psettings->user_abort);
Bram Moolenaara80faa82020-04-12 19:37:17 +02001327 CLEAR_FIELD(prt_dlg);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001328 prt_dlg.lStructSize = sizeof(PRINTDLGW);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001329# if !defined(FEAT_GUI) || defined(VIMDLL)
1330# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001331 if (!gui.in_use)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001332# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01001333 GetConsoleHwnd(); // get value of s_hwnd
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001334# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 prt_dlg.hwndOwner = s_hwnd;
1336 prt_dlg.Flags = PD_NOPAGENUMS | PD_NOSELECTION | PD_RETURNDC;
1337 if (!forceit)
1338 {
1339 prt_dlg.hDevMode = stored_dm;
1340 prt_dlg.hDevNames = stored_devn;
1341 prt_dlg.lCustData = stored_nCopies; // work around bug in print dialog
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001342# if !defined(FEAT_GUI) || defined(VIMDLL)
1343# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001344 if (!gui.in_use)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001345# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001346 {
1347 /*
1348 * Use hook to prevent console window being sent to back
1349 */
1350 prt_dlg.lpfnPrintHook = PrintHookProc;
1351 prt_dlg.Flags |= PD_ENABLEPRINTHOOK;
1352 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001353# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 prt_dlg.Flags |= stored_nFlags;
1355 }
1356
1357 /*
1358 * If bang present, return default printer setup with no dialog
1359 * never show dialog if we are running over telnet
1360 */
1361 if (forceit
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001362# if !defined(FEAT_GUI) || defined(VIMDLL)
1363# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001364 || (!gui.in_use && !term_console)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001365# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 || !term_console
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001367# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001368# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 )
1370 {
1371 prt_dlg.Flags |= PD_RETURNDEFAULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 /*
1373 * MSDN suggests setting the first parameter to WINSPOOL for
1374 * NT, but NULL appears to work just as well.
1375 */
1376 if (*p_pdev != NUL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001377 prt_dlg.hDC = CreateDC(NULL, (LPCSTR)p_pdev, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 {
1380 prt_dlg.Flags |= PD_RETURNDEFAULT;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001381 if (PrintDlgW(&prt_dlg) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 goto init_fail_dlg;
1383 }
1384 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001385 else if (PrintDlgW(&prt_dlg) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 goto init_fail_dlg;
1387 else
1388 {
1389 /*
1390 * keep the previous driver context
1391 */
1392 stored_dm = prt_dlg.hDevMode;
1393 stored_devn = prt_dlg.hDevNames;
1394 stored_nFlags = prt_dlg.Flags;
1395 stored_nCopies = prt_dlg.nCopies;
1396 }
1397
1398 if (prt_dlg.hDC == NULL)
1399 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001400 emsg(_("E237: Printer selection failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 mch_print_cleanup();
1402 return FALSE;
1403 }
1404
Bram Moolenaar0f873732019-12-05 20:28:46 +01001405 // Not all printer drivers report the support of color (or grey) in the
1406 // same way. Let's set has_color if there appears to be some way to print
1407 // more than B&W.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 i = GetDeviceCaps(prt_dlg.hDC, NUMCOLORS);
1409 psettings->has_color = (GetDeviceCaps(prt_dlg.hDC, BITSPIXEL) > 1
1410 || GetDeviceCaps(prt_dlg.hDC, PLANES) > 1
1411 || i > 2 || i == -1);
1412
Bram Moolenaar0f873732019-12-05 20:28:46 +01001413 // Ensure all font styles are baseline aligned
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 SetTextAlign(prt_dlg.hDC, TA_BASELINE|TA_LEFT);
1415
1416 /*
1417 * On some windows systems the nCopies parameter is not
1418 * passed back correctly. It must be retrieved from the
1419 * hDevMode struct.
1420 */
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001421 mem = (DEVMODEW *)GlobalLock(prt_dlg.hDevMode);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 if (mem != NULL)
1423 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 if (mem->dmCopies != 1)
1425 stored_nCopies = mem->dmCopies;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 if ((mem->dmFields & DM_DUPLEX) && (mem->dmDuplex & ~DMDUP_SIMPLEX))
1427 psettings->duplex = TRUE;
1428 if ((mem->dmFields & DM_COLOR) && (mem->dmColor & DMCOLOR_COLOR))
1429 psettings->has_color = TRUE;
1430 }
1431 GlobalUnlock(prt_dlg.hDevMode);
1432
1433 devname = (DEVNAMES *)GlobalLock(prt_dlg.hDevNames);
1434 if (devname != 0)
1435 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001436 WCHAR *wprinter_name = (WCHAR *)devname + devname->wDeviceOffset;
1437 WCHAR *wport_name = (WCHAR *)devname + devname->wOutputOffset;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001438 char_u *text = (char_u *)_("to %s on %s");
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001439 char_u *printer_name = utf16_to_enc(wprinter_name, NULL);
1440 char_u *port_name = utf16_to_enc(wport_name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001442 if (printer_name != NULL && port_name != NULL)
Bram Moolenaar964b3742019-05-24 18:54:09 +02001443 prt_name = alloc(STRLEN(printer_name)
1444 + STRLEN(port_name) + STRLEN(text));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 if (prt_name != NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001446 wsprintf((char *)prt_name, (const char *)text,
1447 printer_name, port_name);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001448 vim_free(printer_name);
1449 vim_free(port_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450 }
1451 GlobalUnlock(prt_dlg.hDevNames);
1452
1453 /*
1454 * Initialise the font according to 'printfont'
1455 */
Bram Moolenaara80faa82020-04-12 19:37:17 +02001456 CLEAR_FIELD(fLogFont);
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001457 if (get_logfont(&fLogFont, p_pfn, prt_dlg.hDC, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001459 semsg(_("E613: Unknown printer font: %s"), p_pfn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 mch_print_cleanup();
1461 return FALSE;
1462 }
1463
1464 for (pifBold = 0; pifBold <= 1; pifBold++)
1465 for (pifItalic = 0; pifItalic <= 1; pifItalic++)
1466 for (pifUnderline = 0; pifUnderline <= 1; pifUnderline++)
1467 {
1468 fLogFont.lfWeight = boldface[pifBold];
1469 fLogFont.lfItalic = pifItalic;
1470 fLogFont.lfUnderline = pifUnderline;
1471 prt_font_handles[pifBold][pifItalic][pifUnderline]
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001472 = CreateFontIndirectW(&fLogFont);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 }
1474
1475 SetBkMode(prt_dlg.hDC, OPAQUE);
1476 SelectObject(prt_dlg.hDC, prt_font_handles[0][0][0]);
1477
1478 /*
1479 * Fill in the settings struct
1480 */
1481 psettings->chars_per_line = prt_get_cpl();
1482 psettings->lines_per_page = prt_get_lpp();
Bram Moolenaar7ddc6422014-09-27 11:18:19 +02001483 if (prt_dlg.Flags & PD_USEDEVMODECOPIESANDCOLLATE)
1484 {
1485 psettings->n_collated_copies = (prt_dlg.Flags & PD_COLLATE)
1486 ? prt_dlg.nCopies : 1;
1487 psettings->n_uncollated_copies = (prt_dlg.Flags & PD_COLLATE)
1488 ? 1 : prt_dlg.nCopies;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489
Bram Moolenaar7ddc6422014-09-27 11:18:19 +02001490 if (psettings->n_collated_copies == 0)
1491 psettings->n_collated_copies = 1;
1492
1493 if (psettings->n_uncollated_copies == 0)
1494 psettings->n_uncollated_copies = 1;
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01001495 }
1496 else
1497 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498 psettings->n_collated_copies = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 psettings->n_uncollated_copies = 1;
Bram Moolenaar7ddc6422014-09-27 11:18:19 +02001500 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501
1502 psettings->jobname = jobname;
1503
1504 return TRUE;
1505
1506init_fail_dlg:
1507 {
1508 DWORD err = CommDlgExtendedError();
1509
1510 if (err)
1511 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 char_u *buf;
1513
Bram Moolenaar0f873732019-12-05 20:28:46 +01001514 // I suspect FormatMessage() doesn't work for values returned by
1515 // CommDlgExtendedError(). What does?
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
1517 FORMAT_MESSAGE_FROM_SYSTEM |
1518 FORMAT_MESSAGE_IGNORE_INSERTS,
1519 NULL, err, 0, (LPTSTR)(&buf), 0, NULL);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001520 semsg(_("E238: Print error: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 buf == NULL ? (char_u *)_("Unknown") : buf);
1522 LocalFree((LPVOID)(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 }
1524 else
Bram Moolenaar0f873732019-12-05 20:28:46 +01001525 msg_clr_eos(); // Maybe canceled
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526
1527 mch_print_cleanup();
1528 return FALSE;
1529 }
1530}
1531
1532
1533 int
1534mch_print_begin(prt_settings_T *psettings)
1535{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001536 int ret = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 char szBuffer[300];
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001538 WCHAR *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001540 hDlgPrint = CreateDialog(g_hinst, TEXT("PrintDlgBox"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 prt_dlg.hwndOwner, PrintDlgProc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 SetAbortProc(prt_dlg.hDC, AbortProc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 wsprintf(szBuffer, _("Printing '%s'"), gettail(psettings->jobname));
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001544 vimSetDlgItemText(hDlgPrint, IDC_PRINTTEXT1, (char_u *)szBuffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001546 wp = enc_to_utf16(psettings->jobname, NULL);
Bram Moolenaar2290b1f2018-05-13 17:30:45 +02001547 if (wp != NULL)
1548 {
1549 DOCINFOW di;
1550
Bram Moolenaara80faa82020-04-12 19:37:17 +02001551 CLEAR_FIELD(di);
Bram Moolenaar2290b1f2018-05-13 17:30:45 +02001552 di.cbSize = sizeof(di);
1553 di.lpszDocName = wp;
1554 ret = StartDocW(prt_dlg.hDC, &di);
1555 vim_free(wp);
1556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001558# ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01001559 // Give focus back to main window (when using MDI).
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001560# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001561 if (gui.in_use)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001562# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001563 SetFocus(s_hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001564# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565
1566 return (ret > 0);
1567}
1568
1569 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01001570mch_print_end(prt_settings_T *psettings UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571{
1572 EndDoc(prt_dlg.hDC);
1573 if (!*bUserAbort)
1574 SendMessage(hDlgPrint, WM_COMMAND, 0, 0);
1575}
1576
1577 int
1578mch_print_end_page(void)
1579{
1580 return (EndPage(prt_dlg.hDC) > 0);
1581}
1582
1583 int
1584mch_print_begin_page(char_u *msg)
1585{
1586 if (msg != NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001587 vimSetDlgItemText(hDlgPrint, IDC_PROGRESS, msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588 return (StartPage(prt_dlg.hDC) > 0);
1589}
1590
1591 int
1592mch_print_blank_page(void)
1593{
1594 return (mch_print_begin_page(NULL) ? (mch_print_end_page()) : FALSE);
1595}
1596
1597static int prt_pos_x = 0;
1598static int prt_pos_y = 0;
1599
1600 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001601mch_print_start_line(int margin, int page_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602{
1603 if (margin)
1604 prt_pos_x = -prt_number_width;
1605 else
1606 prt_pos_x = 0;
1607 prt_pos_y = page_line * prt_line_height
1608 + prt_tm.tmAscent + prt_tm.tmExternalLeading;
1609}
1610
1611 int
1612mch_print_text_out(char_u *p, int len)
1613{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 SIZE sz;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001615 WCHAR *wp;
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001616 int wlen = len;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001617 int ret = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001619 wp = enc_to_utf16(p, &wlen);
1620 if (wp == NULL)
1621 return FALSE;
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001622
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001623 TextOutW(prt_dlg.hDC, prt_pos_x + prt_left_margin,
1624 prt_pos_y + prt_top_margin, wp, wlen);
1625 GetTextExtentPoint32W(prt_dlg.hDC, wp, wlen, &sz);
1626 vim_free(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 prt_pos_x += (sz.cx - prt_tm.tmOverhang);
Bram Moolenaar0f873732019-12-05 20:28:46 +01001628 // This is wrong when printing spaces for a TAB.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001629 if (p[len] != NUL)
1630 {
Bram Moolenaar1614a142019-10-06 22:00:13 +02001631 wlen = mb_ptr2len(p + len);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001632 wp = enc_to_utf16(p + len, &wlen);
1633 if (wp != NULL)
1634 {
1635 GetTextExtentPoint32W(prt_dlg.hDC, wp, 1, &sz);
1636 ret = (prt_pos_x + prt_left_margin + sz.cx > prt_right_margin);
1637 vim_free(wp);
1638 }
1639 }
1640 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641}
1642
1643 void
1644mch_print_set_font(int iBold, int iItalic, int iUnderline)
1645{
1646 SelectObject(prt_dlg.hDC, prt_font_handles[iBold][iItalic][iUnderline]);
1647}
1648
1649 void
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001650mch_print_set_bg(long_u bgcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651{
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001652 SetBkColor(prt_dlg.hDC, GetNearestColor(prt_dlg.hDC,
1653 swap_me((COLORREF)bgcol)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 /*
1655 * With a white background we can draw characters transparent, which is
1656 * good for italic characters that overlap to the next char cell.
1657 */
1658 if (bgcol == 0xffffffUL)
1659 SetBkMode(prt_dlg.hDC, TRANSPARENT);
1660 else
1661 SetBkMode(prt_dlg.hDC, OPAQUE);
1662}
1663
1664 void
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001665mch_print_set_fg(long_u fgcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666{
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001667 SetTextColor(prt_dlg.hDC, GetNearestColor(prt_dlg.hDC,
1668 swap_me((COLORREF)fgcol)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669}
1670
Bram Moolenaar0f873732019-12-05 20:28:46 +01001671#endif // FEAT_PRINTER && !FEAT_POSTSCRIPT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672
Bram Moolenaar58d98232005-07-23 22:25:46 +00001673
1674
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675#if defined(FEAT_SHORTCUT) || defined(PROTO)
Bram Moolenaar82881492012-11-20 16:53:39 +01001676# ifndef PROTO
1677# include <shlobj.h>
1678# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001680typedef BOOL (WINAPI *pfnGetFinalPathNameByHandleW)(
Bram Moolenaardce1e892019-02-10 23:18:53 +01001681 HANDLE hFile,
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001682 LPWSTR lpszFilePath,
1683 DWORD cchFilePath,
1684 DWORD dwFlags);
1685static pfnGetFinalPathNameByHandleW pGetFinalPathNameByHandleW = NULL;
Bram Moolenaardce1e892019-02-10 23:18:53 +01001686
Bram Moolenaar4a792c82019-06-06 12:22:41 +02001687# define is_path_sep(c) ((c) == L'\\' || (c) == L'/')
1688
1689 static int
1690is_reparse_point_included(LPCWSTR fname)
1691{
1692 LPCWSTR p = fname, q;
1693 WCHAR buf[MAX_PATH];
1694 DWORD attr;
1695
1696 if (isalpha(p[0]) && p[1] == L':' && is_path_sep(p[2]))
1697 p += 3;
1698 else if (is_path_sep(p[0]) && is_path_sep(p[1]))
1699 p += 2;
1700
1701 while (*p != L'\0')
1702 {
1703 q = wcspbrk(p, L"\\/");
1704 if (q == NULL)
1705 p = q = fname + wcslen(fname);
1706 else
1707 p = q + 1;
1708 if (q - fname >= MAX_PATH)
1709 return FALSE;
1710 wcsncpy(buf, fname, q - fname);
1711 buf[q - fname] = L'\0';
1712 attr = GetFileAttributesW(buf);
1713 if (attr != INVALID_FILE_ATTRIBUTES
1714 && (attr & FILE_ATTRIBUTE_REPARSE_POINT) != 0)
1715 return TRUE;
1716 }
1717 return FALSE;
1718}
1719
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02001720 static char_u *
Bram Moolenaardce1e892019-02-10 23:18:53 +01001721resolve_reparse_point(char_u *fname)
1722{
1723 HANDLE h = INVALID_HANDLE_VALUE;
1724 DWORD size;
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001725 WCHAR *p, *wp;
Bram Moolenaardce1e892019-02-10 23:18:53 +01001726 char_u *rfname = NULL;
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001727 WCHAR *buff = NULL;
Bram Moolenaardce1e892019-02-10 23:18:53 +01001728 static BOOL loaded = FALSE;
1729
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001730 if (pGetFinalPathNameByHandleW == NULL)
Bram Moolenaardce1e892019-02-10 23:18:53 +01001731 {
1732 HMODULE hmod = GetModuleHandle("kernel32.dll");
1733
1734 if (loaded == TRUE)
1735 return NULL;
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001736 pGetFinalPathNameByHandleW = (pfnGetFinalPathNameByHandleW)
1737 GetProcAddress(hmod, "GetFinalPathNameByHandleW");
Bram Moolenaardce1e892019-02-10 23:18:53 +01001738 loaded = TRUE;
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001739 if (pGetFinalPathNameByHandleW == NULL)
Bram Moolenaardce1e892019-02-10 23:18:53 +01001740 return NULL;
1741 }
1742
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001743 p = enc_to_utf16(fname, NULL);
1744 if (p == NULL)
1745 goto fail;
1746
Bram Moolenaar4a792c82019-06-06 12:22:41 +02001747 if (!is_reparse_point_included(p))
1748 {
1749 vim_free(p);
1750 goto fail;
1751 }
1752
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001753 h = CreateFileW(p, 0, 0, NULL, OPEN_EXISTING,
1754 FILE_FLAG_BACKUP_SEMANTICS, NULL);
1755 vim_free(p);
Bram Moolenaardce1e892019-02-10 23:18:53 +01001756
1757 if (h == INVALID_HANDLE_VALUE)
1758 goto fail;
1759
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001760 size = pGetFinalPathNameByHandleW(h, NULL, 0, 0);
1761 if (size == 0)
1762 goto fail;
1763 buff = ALLOC_MULT(WCHAR, size);
1764 if (buff == NULL)
1765 goto fail;
1766 if (pGetFinalPathNameByHandleW(h, buff, size, 0) == 0)
Bram Moolenaardce1e892019-02-10 23:18:53 +01001767 goto fail;
1768
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001769 if (wcsncmp(buff, L"\\\\?\\UNC\\", 8) == 0)
1770 {
1771 buff[6] = L'\\';
1772 wp = buff + 6;
1773 }
1774 else if (wcsncmp(buff, L"\\\\?\\", 4) == 0)
1775 wp = buff + 4;
Bram Moolenaardce1e892019-02-10 23:18:53 +01001776 else
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001777 wp = buff;
1778
1779 rfname = utf16_to_enc(wp, NULL);
Bram Moolenaardce1e892019-02-10 23:18:53 +01001780
1781fail:
1782 if (h != INVALID_HANDLE_VALUE)
1783 CloseHandle(h);
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001784 if (buff != NULL)
1785 vim_free(buff);
Bram Moolenaardce1e892019-02-10 23:18:53 +01001786
1787 return rfname;
1788}
1789
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790/*
1791 * When "fname" is the name of a shortcut (*.lnk) resolve the file it points
1792 * to and return that name in allocated memory.
1793 * Otherwise NULL is returned.
1794 */
Bram Moolenaardce1e892019-02-10 23:18:53 +01001795 static char_u *
1796resolve_shortcut(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797{
1798 HRESULT hr;
1799 IShellLink *psl = NULL;
1800 IPersistFile *ppf = NULL;
1801 OLECHAR wsz[MAX_PATH];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 char_u *rfname = NULL;
1803 int len;
Bram Moolenaar604729e2013-08-30 16:44:19 +02001804 IShellLinkW *pslw = NULL;
1805 WIN32_FIND_DATAW ffdw; // we get those free of charge
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806
Bram Moolenaar0f873732019-12-05 20:28:46 +01001807 // Check if the file name ends in ".lnk". Avoid calling
1808 // CoCreateInstance(), it's quite slow.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 if (fname == NULL)
1810 return rfname;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001811 len = (int)STRLEN(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 if (len <= 4 || STRNICMP(fname + len - 4, ".lnk", 4) != 0)
1813 return rfname;
1814
1815 CoInitialize(NULL);
1816
1817 // create a link manager object and request its interface
1818 hr = CoCreateInstance(
1819 &CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001820 &IID_IShellLinkW, (void**)&pslw);
1821 if (hr == S_OK)
1822 {
1823 WCHAR *p = enc_to_utf16(fname, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001825 if (p != NULL)
1826 {
1827 // Get a pointer to the IPersistFile interface.
1828 hr = pslw->lpVtbl->QueryInterface(
1829 pslw, &IID_IPersistFile, (void**)&ppf);
1830 if (hr != S_OK)
1831 goto shortcut_errorw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001833 // "load" the name and resolve the link
1834 hr = ppf->lpVtbl->Load(ppf, p, STGM_READ);
1835 if (hr != S_OK)
1836 goto shortcut_errorw;
1837# if 0 // This makes Vim wait a long time if the target does not exist.
1838 hr = pslw->lpVtbl->Resolve(pslw, NULL, SLR_NO_UI);
1839 if (hr != S_OK)
1840 goto shortcut_errorw;
Bram Moolenaar604729e2013-08-30 16:44:19 +02001841# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001843 // Get the path to the link target.
1844 ZeroMemory(wsz, MAX_PATH * sizeof(WCHAR));
1845 hr = pslw->lpVtbl->GetPath(pslw, wsz, MAX_PATH, &ffdw, 0);
1846 if (hr == S_OK && wsz[0] != NUL)
1847 rfname = utf16_to_enc(wsz, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001849shortcut_errorw:
1850 vim_free(p);
1851 }
1852 }
1853
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 // Release all interface pointers (both belong to the same object)
1855 if (ppf != NULL)
1856 ppf->lpVtbl->Release(ppf);
1857 if (psl != NULL)
1858 psl->lpVtbl->Release(psl);
Bram Moolenaar604729e2013-08-30 16:44:19 +02001859 if (pslw != NULL)
1860 pslw->lpVtbl->Release(pslw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861
1862 CoUninitialize();
1863 return rfname;
1864}
Bram Moolenaardce1e892019-02-10 23:18:53 +01001865
1866 char_u *
1867mch_resolve_path(char_u *fname, int reparse_point)
1868{
1869 char_u *path = resolve_shortcut(fname);
1870
1871 if (path == NULL && reparse_point)
1872 path = resolve_reparse_point(fname);
1873 return path;
1874}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875#endif
1876
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001877#if (defined(FEAT_EVAL) && (!defined(FEAT_GUI) || defined(VIMDLL))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878/*
1879 * Bring ourselves to the foreground. Does work if the OS doesn't allow it.
1880 */
1881 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001882win32_set_foreground(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883{
Bram Moolenaar0f873732019-12-05 20:28:46 +01001884 GetConsoleHwnd(); // get value of s_hwnd
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 if (s_hwnd != 0)
1886 SetForegroundWindow(s_hwnd);
1887}
1888#endif
1889
1890#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
1891/*
1892 * Client-server code for Vim
1893 *
1894 * Originally written by Paul Moore
1895 */
1896
Bram Moolenaar0f873732019-12-05 20:28:46 +01001897// In order to handle inter-process messages, we need to have a window. But
1898// the functions in this module can be called before the main GUI window is
1899// created (and may also be called in the console version, where there is no
1900// GUI window at all).
1901//
1902// So we create a hidden window, and arrange to destroy it on exit.
1903HWND message_window = 0; // window that's handling messages
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001905# define VIM_CLASSNAME "VIM_MESSAGES"
1906# define VIM_CLASSNAME_LEN (sizeof(VIM_CLASSNAME) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907
Bram Moolenaar0f873732019-12-05 20:28:46 +01001908// Communication is via WM_COPYDATA messages. The message type is send in
1909// the dwData parameter. Types are defined here.
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001910# define COPYDATA_KEYS 0
1911# define COPYDATA_REPLY 1
1912# define COPYDATA_EXPR 10
1913# define COPYDATA_RESULT 11
1914# define COPYDATA_ERROR_RESULT 12
1915# define COPYDATA_ENCODING 20
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916
Bram Moolenaar0f873732019-12-05 20:28:46 +01001917// This is a structure containing a server HWND and its name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918struct server_id
1919{
1920 HWND hwnd;
1921 char_u *name;
1922};
1923
Bram Moolenaar0f873732019-12-05 20:28:46 +01001924// Last received 'encoding' that the client uses.
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001925static char_u *client_enc = NULL;
1926
1927/*
1928 * Tell the other side what encoding we are using.
1929 * Errors are ignored.
1930 */
1931 static void
1932serverSendEnc(HWND target)
1933{
1934 COPYDATASTRUCT data;
1935
1936 data.dwData = COPYDATA_ENCODING;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001937 data.cbData = (DWORD)STRLEN(p_enc) + 1;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001938 data.lpData = p_enc;
1939 (void)SendMessage(target, WM_COPYDATA, (WPARAM)message_window,
1940 (LPARAM)(&data));
1941}
1942
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943/*
1944 * Clean up on exit. This destroys the hidden message window.
1945 */
1946 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947CleanUpMessaging(void)
1948{
1949 if (message_window != 0)
1950 {
1951 DestroyWindow(message_window);
1952 message_window = 0;
1953 }
1954}
1955
1956static int save_reply(HWND server, char_u *reply, int expr);
1957
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001958/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 * The window procedure for the hidden message window.
1960 * It handles callback messages and notifications from servers.
1961 * In order to process these messages, it is necessary to run a
1962 * message loop. Code which may run before the main message loop
1963 * is started (in the GUI) is careful to pump messages when it needs
1964 * to. Features which require message delivery during normal use will
1965 * not work in the console version - this basically means those
1966 * features which allow Vim to act as a server, rather than a client.
1967 */
1968 static LRESULT CALLBACK
1969Messaging_WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
1970{
1971 if (msg == WM_COPYDATA)
1972 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001973 // This is a message from another Vim. The dwData member of the
1974 // COPYDATASTRUCT determines the type of message:
1975 // COPYDATA_ENCODING:
1976 // The encoding that the client uses. Following messages will
1977 // use this encoding, convert if needed.
1978 // COPYDATA_KEYS:
1979 // A key sequence. We are a server, and a client wants these keys
1980 // adding to the input queue.
1981 // COPYDATA_REPLY:
1982 // A reply. We are a client, and a server has sent this message
1983 // in response to a request. (server2client())
1984 // COPYDATA_EXPR:
1985 // An expression. We are a server, and a client wants us to
1986 // evaluate this expression.
1987 // COPYDATA_RESULT:
1988 // A reply. We are a client, and a server has sent this message
1989 // in response to a COPYDATA_EXPR.
1990 // COPYDATA_ERROR_RESULT:
1991 // A reply. We are a client, and a server has sent this message
1992 // in response to a COPYDATA_EXPR that failed to evaluate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 COPYDATASTRUCT *data = (COPYDATASTRUCT*)lParam;
1994 HWND sender = (HWND)wParam;
1995 COPYDATASTRUCT reply;
1996 char_u *res;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 int retval;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001998 char_u *str;
1999 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000
2001 switch (data->dwData)
2002 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002003 case COPYDATA_ENCODING:
Bram Moolenaar0f873732019-12-05 20:28:46 +01002004 // Remember the encoding that the client uses.
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002005 vim_free(client_enc);
2006 client_enc = enc_canonize((char_u *)data->lpData);
2007 return 1;
2008
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 case COPYDATA_KEYS:
Bram Moolenaar0f873732019-12-05 20:28:46 +01002010 // Remember who sent this, for <client>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 clientWindow = sender;
2012
Bram Moolenaar0f873732019-12-05 20:28:46 +01002013 // Add the received keys to the input buffer. The loop waiting
2014 // for the user to do something should check the input buffer.
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002015 str = serverConvert(client_enc, (char_u *)data->lpData, &tofree);
2016 server_to_input_buf(str);
2017 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018
2019# ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01002020 // Wake up the main GUI loop.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002021# ifdef VIMDLL
2022 if (gui.in_use)
2023# endif
2024 if (s_hwnd != 0)
2025 PostMessage(s_hwnd, WM_NULL, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026# endif
2027 return 1;
2028
2029 case COPYDATA_EXPR:
Bram Moolenaar0f873732019-12-05 20:28:46 +01002030 // Remember who sent this, for <client>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 clientWindow = sender;
2032
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002033 str = serverConvert(client_enc, (char_u *)data->lpData, &tofree);
2034 res = eval_client_expr_to_string(str);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002035
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 if (res == NULL)
2037 {
Bram Moolenaar15bf76d2017-03-18 16:18:37 +01002038 char *err = _(e_invexprmsg);
2039 size_t len = STRLEN(str) + STRLEN(err) + 5;
2040
Bram Moolenaar964b3742019-05-24 18:54:09 +02002041 res = alloc(len);
Bram Moolenaar15bf76d2017-03-18 16:18:37 +01002042 if (res != NULL)
2043 vim_snprintf((char *)res, len, "%s: \"%s\"", err, str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 reply.dwData = COPYDATA_ERROR_RESULT;
2045 }
2046 else
2047 reply.dwData = COPYDATA_RESULT;
2048 reply.lpData = res;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002049 reply.cbData = (DWORD)STRLEN(res) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002051 serverSendEnc(sender);
Bram Moolenaar5246cd72013-06-16 16:41:47 +02002052 retval = (int)SendMessage(sender, WM_COPYDATA,
2053 (WPARAM)message_window, (LPARAM)(&reply));
Bram Moolenaar15bf76d2017-03-18 16:18:37 +01002054 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 vim_free(res);
2056 return retval;
2057
2058 case COPYDATA_REPLY:
2059 case COPYDATA_RESULT:
2060 case COPYDATA_ERROR_RESULT:
2061 if (data->lpData != NULL)
2062 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002063 str = serverConvert(client_enc, (char_u *)data->lpData,
2064 &tofree);
2065 if (tofree == NULL)
2066 str = vim_strsave(str);
2067 if (save_reply(sender, str,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 (data->dwData == COPYDATA_REPLY ? 0 :
2069 (data->dwData == COPYDATA_RESULT ? 1 :
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002070 2))) == FAIL)
2071 vim_free(str);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002072 else if (data->dwData == COPYDATA_REPLY)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 {
Bram Moolenaar427d51c2013-06-16 16:01:25 +02002074 char_u winstr[30];
2075
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002076 sprintf((char *)winstr, PRINTF_HEX_LONG_U, (long_u)sender);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002077 apply_autocmds(EVENT_REMOTEREPLY, winstr, str,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 TRUE, curbuf);
2079 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 }
2081 return 1;
2082 }
2083
2084 return 0;
2085 }
2086
2087 else if (msg == WM_ACTIVATE && wParam == WA_ACTIVE)
2088 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002089 // When the message window is activated (brought to the foreground),
2090 // this actually applies to the text window.
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002091# if !defined(FEAT_GUI) || defined(VIMDLL)
2092# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002093 if (!gui.in_use)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002094# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002095 GetConsoleHwnd(); // get value of s_hwnd
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002096# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097 if (s_hwnd != 0)
2098 {
2099 SetForegroundWindow(s_hwnd);
2100 return 0;
2101 }
2102 }
2103
2104 return DefWindowProc(hwnd, msg, wParam, lParam);
2105}
2106
2107/*
2108 * Initialise the message handling process. This involves creating a window
2109 * to handle messages - the window will not be visible.
2110 */
2111 void
2112serverInitMessaging(void)
2113{
2114 WNDCLASS wndclass;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115
Bram Moolenaar0f873732019-12-05 20:28:46 +01002116 // Clean up on exit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 atexit(CleanUpMessaging);
2118
Bram Moolenaar0f873732019-12-05 20:28:46 +01002119 // Register a window class - we only really care
2120 // about the window procedure
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 wndclass.style = 0;
2122 wndclass.lpfnWndProc = Messaging_WndProc;
2123 wndclass.cbClsExtra = 0;
2124 wndclass.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002125 wndclass.hInstance = g_hinst;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 wndclass.hIcon = NULL;
2127 wndclass.hCursor = NULL;
2128 wndclass.hbrBackground = NULL;
2129 wndclass.lpszMenuName = NULL;
2130 wndclass.lpszClassName = VIM_CLASSNAME;
2131 RegisterClass(&wndclass);
2132
Bram Moolenaar0f873732019-12-05 20:28:46 +01002133 // Create the message window. It will be hidden, so the details don't
2134 // matter. Don't use WS_OVERLAPPEDWINDOW, it will make a shortcut remove
2135 // focus from gvim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 message_window = CreateWindow(VIM_CLASSNAME, "",
2137 WS_POPUPWINDOW | WS_CAPTION,
2138 CW_USEDEFAULT, CW_USEDEFAULT,
2139 100, 100, NULL, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002140 g_hinst, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141}
2142
Bram Moolenaar0f873732019-12-05 20:28:46 +01002143// Used by serverSendToVim() to find an alternate server name.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002144static char_u *altname_buf_ptr = NULL;
2145
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146/*
2147 * Get the title of the window "hwnd", which is the Vim server name, in
2148 * "name[namelen]" and return the length.
2149 * Returns zero if window "hwnd" is not a Vim server.
2150 */
2151 static int
2152getVimServerName(HWND hwnd, char *name, int namelen)
2153{
2154 int len;
2155 char buffer[VIM_CLASSNAME_LEN + 1];
2156
Bram Moolenaar0f873732019-12-05 20:28:46 +01002157 // Ignore windows which aren't Vim message windows
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 len = GetClassName(hwnd, buffer, sizeof(buffer));
2159 if (len != VIM_CLASSNAME_LEN || STRCMP(buffer, VIM_CLASSNAME) != 0)
2160 return 0;
2161
Bram Moolenaar0f873732019-12-05 20:28:46 +01002162 // Get the title of the window
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 return GetWindowText(hwnd, name, namelen);
2164}
2165
2166 static BOOL CALLBACK
2167enumWindowsGetServer(HWND hwnd, LPARAM lparam)
2168{
2169 struct server_id *id = (struct server_id *)lparam;
2170 char server[MAX_PATH];
2171
Bram Moolenaar0f873732019-12-05 20:28:46 +01002172 // Get the title of the window
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 if (getVimServerName(hwnd, server, sizeof(server)) == 0)
2174 return TRUE;
2175
Bram Moolenaar0f873732019-12-05 20:28:46 +01002176 // If this is the server we're looking for, return its HWND
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 if (STRICMP(server, id->name) == 0)
2178 {
2179 id->hwnd = hwnd;
2180 return FALSE;
2181 }
2182
Bram Moolenaar0f873732019-12-05 20:28:46 +01002183 // If we are looking for an alternate server, remember this name.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002184 if (altname_buf_ptr != NULL
2185 && STRNICMP(server, id->name, STRLEN(id->name)) == 0
2186 && vim_isdigit(server[STRLEN(id->name)]))
2187 {
2188 STRCPY(altname_buf_ptr, server);
Bram Moolenaar0f873732019-12-05 20:28:46 +01002189 altname_buf_ptr = NULL; // don't use another name
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002190 }
2191
Bram Moolenaar0f873732019-12-05 20:28:46 +01002192 // Otherwise, keep looking
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 return TRUE;
2194}
2195
2196 static BOOL CALLBACK
2197enumWindowsGetNames(HWND hwnd, LPARAM lparam)
2198{
2199 garray_T *ga = (garray_T *)lparam;
2200 char server[MAX_PATH];
2201
Bram Moolenaar0f873732019-12-05 20:28:46 +01002202 // Get the title of the window
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 if (getVimServerName(hwnd, server, sizeof(server)) == 0)
2204 return TRUE;
2205
Bram Moolenaar0f873732019-12-05 20:28:46 +01002206 // Add the name to the list
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002207 ga_concat(ga, (char_u *)server);
2208 ga_concat(ga, (char_u *)"\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 return TRUE;
2210}
2211
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002212struct enum_windows_s
2213{
2214 WNDENUMPROC lpEnumFunc;
2215 LPARAM lParam;
2216};
2217
2218 static BOOL CALLBACK
2219enum_windows_child(HWND hwnd, LPARAM lParam)
2220{
2221 struct enum_windows_s *ew = (struct enum_windows_s *)lParam;
2222
2223 return (ew->lpEnumFunc)(hwnd, ew->lParam);
2224}
2225
2226 static BOOL CALLBACK
2227enum_windows_toplevel(HWND hwnd, LPARAM lParam)
2228{
2229 struct enum_windows_s *ew = (struct enum_windows_s *)lParam;
2230
Bram Moolenaar95ba5c32018-10-07 22:47:07 +02002231 if ((ew->lpEnumFunc)(hwnd, ew->lParam))
2232 return TRUE;
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002233 return EnumChildWindows(hwnd, enum_windows_child, lParam);
2234}
2235
Bram Moolenaar0f873732019-12-05 20:28:46 +01002236/*
2237 * Enumerate all windows including children.
2238 */
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002239 static BOOL
2240enum_windows(WNDENUMPROC lpEnumFunc, LPARAM lParam)
2241{
2242 struct enum_windows_s ew;
2243
2244 ew.lpEnumFunc = lpEnumFunc;
2245 ew.lParam = lParam;
2246 return EnumWindows(enum_windows_toplevel, (LPARAM)&ew);
2247}
2248
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 static HWND
2250findServer(char_u *name)
2251{
2252 struct server_id id;
2253
2254 id.name = name;
2255 id.hwnd = 0;
2256
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002257 enum_windows(enumWindowsGetServer, (LPARAM)(&id));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258
2259 return id.hwnd;
2260}
2261
2262 void
2263serverSetName(char_u *name)
2264{
2265 char_u *ok_name;
2266 HWND hwnd = 0;
2267 int i = 0;
2268 char_u *p;
2269
Bram Moolenaar0f873732019-12-05 20:28:46 +01002270 // Leave enough space for a 9-digit suffix to ensure uniqueness!
Bram Moolenaar964b3742019-05-24 18:54:09 +02002271 ok_name = alloc(STRLEN(name) + 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272
2273 STRCPY(ok_name, name);
2274 p = ok_name + STRLEN(name);
2275
2276 for (;;)
2277 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002278 // This is inefficient - we're doing an EnumWindows loop for each
2279 // possible name. It would be better to grab all names in one go,
2280 // and scan the list each time...
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 hwnd = findServer(ok_name);
2282 if (hwnd == 0)
2283 break;
2284
2285 ++i;
2286 if (i >= 1000)
2287 break;
2288
2289 sprintf((char *)p, "%d", i);
2290 }
2291
2292 if (hwnd != 0)
2293 vim_free(ok_name);
2294 else
2295 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002296 // Remember the name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 serverName = ok_name;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002298 need_maketitle = TRUE; // update Vim window title later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299
Bram Moolenaar0f873732019-12-05 20:28:46 +01002300 // Update the message window title
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002301 SetWindowText(message_window, (LPCSTR)ok_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002303# ifdef FEAT_EVAL
Bram Moolenaar0f873732019-12-05 20:28:46 +01002304 // Set the servername variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 set_vim_var_string(VV_SEND_SERVER, serverName, -1);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002306# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 }
2308}
2309
2310 char_u *
2311serverGetVimNames(void)
2312{
2313 garray_T ga;
2314
2315 ga_init2(&ga, 1, 100);
2316
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002317 enum_windows(enumWindowsGetNames, (LPARAM)(&ga));
Bram Moolenaar269ec652004-07-29 08:43:53 +00002318 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319
2320 return ga.ga_data;
2321}
2322
2323 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002324serverSendReply(
Bram Moolenaar0f873732019-12-05 20:28:46 +01002325 char_u *name, // Where to send.
2326 char_u *reply) // What to send.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327{
2328 HWND target;
2329 COPYDATASTRUCT data;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002330 long_u n = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331
Bram Moolenaar0f873732019-12-05 20:28:46 +01002332 // The "name" argument is a magic cookie obtained from expand("<client>").
2333 // It should be of the form 0xXXXXX - i.e. a C hex literal, which is the
2334 // value of the client's message window HWND.
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002335 sscanf((char *)name, SCANF_HEX_LONG_U, &n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 if (n == 0)
2337 return -1;
2338
2339 target = (HWND)n;
2340 if (!IsWindow(target))
2341 return -1;
2342
2343 data.dwData = COPYDATA_REPLY;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002344 data.cbData = (DWORD)STRLEN(reply) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 data.lpData = reply;
2346
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002347 serverSendEnc(target);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 if (SendMessage(target, WM_COPYDATA, (WPARAM)message_window,
2349 (LPARAM)(&data)))
2350 return 0;
2351
2352 return -1;
2353}
2354
2355 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002356serverSendToVim(
Bram Moolenaar0f873732019-12-05 20:28:46 +01002357 char_u *name, // Where to send.
2358 char_u *cmd, // What to send.
2359 char_u **result, // Result of eval'ed expression
2360 void *ptarget, // HWND of server
2361 int asExpr, // Expression or keys?
2362 int timeout, // timeout in seconds or zero
2363 int silent) // don't complain about no server
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364{
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002365 HWND target;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 COPYDATASTRUCT data;
2367 char_u *retval = NULL;
2368 int retcode = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002369 char_u altname_buf[MAX_PATH];
2370
Bram Moolenaar0f873732019-12-05 20:28:46 +01002371 // Execute locally if no display or target is ourselves
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01002372 if (serverName != NULL && STRICMP(name, serverName) == 0)
2373 return sendToLocalVim(cmd, asExpr, result);
2374
Bram Moolenaar0f873732019-12-05 20:28:46 +01002375 // If the server name does not end in a digit then we look for an
2376 // alternate name. e.g. when "name" is GVIM the we may find GVIM2.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002377 if (STRLEN(name) > 1 && !vim_isdigit(name[STRLEN(name) - 1]))
2378 altname_buf_ptr = altname_buf;
2379 altname_buf[0] = NUL;
2380 target = findServer(name);
2381 altname_buf_ptr = NULL;
2382 if (target == 0 && altname_buf[0] != NUL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01002383 // Use another server name we found.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002384 target = findServer(altname_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385
2386 if (target == 0)
2387 {
2388 if (!silent)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002389 semsg(_(e_noserver), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 return -1;
2391 }
2392
2393 if (ptarget)
2394 *(HWND *)ptarget = target;
2395
2396 data.dwData = asExpr ? COPYDATA_EXPR : COPYDATA_KEYS;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002397 data.cbData = (DWORD)STRLEN(cmd) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 data.lpData = cmd;
2399
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002400 serverSendEnc(target);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 if (SendMessage(target, WM_COPYDATA, (WPARAM)message_window,
2402 (LPARAM)(&data)) == 0)
2403 return -1;
2404
2405 if (asExpr)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002406 retval = serverGetReply(target, &retcode, TRUE, TRUE, timeout);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407
2408 if (result == NULL)
2409 vim_free(retval);
2410 else
Bram Moolenaar0f873732019-12-05 20:28:46 +01002411 *result = retval; // Caller assumes responsibility for freeing
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412
2413 return retcode;
2414}
2415
2416/*
2417 * Bring the server to the foreground.
2418 */
2419 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002420serverForeground(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421{
2422 HWND target = findServer(name);
2423
2424 if (target != 0)
2425 SetForegroundWindow(target);
2426}
2427
Bram Moolenaar0f873732019-12-05 20:28:46 +01002428// Replies from server need to be stored until the client picks them up via
2429// remote_read(). So we maintain a list of server-id/reply pairs.
2430// Note that there could be multiple replies from one server pending if the
2431// client is slow picking them up.
2432// We just store the replies in a simple list. When we remove an entry, we
2433// move list entries down to fill the gap.
2434// The server ID is simply the HWND.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435typedef struct
2436{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002437 HWND server; // server window
2438 char_u *reply; // reply string
2439 int expr_result; // 0 for REPLY, 1 for RESULT 2 for error
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002440} reply_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441
2442static garray_T reply_list = {0, 0, sizeof(reply_T), 5, 0};
2443
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002444# define REPLY_ITEM(i) ((reply_T *)(reply_list.ga_data) + (i))
2445# define REPLY_COUNT (reply_list.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446
Bram Moolenaar0f873732019-12-05 20:28:46 +01002447// Flag which is used to wait for a reply
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448static int reply_received = 0;
2449
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002450/*
2451 * Store a reply. "reply" must be allocated memory (or NULL).
2452 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 static int
2454save_reply(HWND server, char_u *reply, int expr)
2455{
2456 reply_T *rep;
2457
2458 if (ga_grow(&reply_list, 1) == FAIL)
2459 return FAIL;
2460
2461 rep = REPLY_ITEM(REPLY_COUNT);
2462 rep->server = server;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002463 rep->reply = reply;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 rep->expr_result = expr;
2465 if (rep->reply == NULL)
2466 return FAIL;
2467
2468 ++REPLY_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 reply_received = 1;
2470 return OK;
2471}
2472
2473/*
2474 * Get a reply from server "server".
2475 * When "expr_res" is non NULL, get the result of an expression, otherwise a
2476 * server2client() message.
2477 * When non NULL, point to return code. 0 => OK, -1 => ERROR
2478 * If "remove" is TRUE, consume the message, the caller must free it then.
2479 * if "wait" is TRUE block until a message arrives (or the server exits).
2480 */
2481 char_u *
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002482serverGetReply(HWND server, int *expr_res, int remove, int wait, int timeout)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483{
2484 int i;
2485 char_u *reply;
2486 reply_T *rep;
Bram Moolenaar15e737f2017-03-18 21:22:47 +01002487 int did_process = FALSE;
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002488 time_t start;
2489 time_t now;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490
Bram Moolenaar0f873732019-12-05 20:28:46 +01002491 // When waiting, loop until the message waiting for is received.
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002492 time(&start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 for (;;)
2494 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002495 // Reset this here, in case a message arrives while we are going
2496 // through the already received messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 reply_received = 0;
2498
2499 for (i = 0; i < REPLY_COUNT; ++i)
2500 {
2501 rep = REPLY_ITEM(i);
2502 if (rep->server == server
2503 && ((rep->expr_result != 0) == (expr_res != NULL)))
2504 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002505 // Save the values we've found for later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 reply = rep->reply;
2507 if (expr_res != NULL)
2508 *expr_res = rep->expr_result == 1 ? 0 : -1;
2509
2510 if (remove)
2511 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002512 // Move the rest of the list down to fill the gap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 mch_memmove(rep, rep + 1,
2514 (REPLY_COUNT - i - 1) * sizeof(reply_T));
2515 --REPLY_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 }
2517
Bram Moolenaar0f873732019-12-05 20:28:46 +01002518 // Return the reply to the caller, who takes on responsibility
2519 // for freeing it if "remove" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 return reply;
2521 }
2522 }
2523
Bram Moolenaar0f873732019-12-05 20:28:46 +01002524 // If we got here, we didn't find a reply. Return immediately if the
2525 // "wait" parameter isn't set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 if (!wait)
Bram Moolenaar15e737f2017-03-18 21:22:47 +01002527 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002528 // Process pending messages once. Without this, looping on
2529 // remote_peek() would never get the reply.
Bram Moolenaar15e737f2017-03-18 21:22:47 +01002530 if (!did_process)
2531 {
2532 did_process = TRUE;
2533 serverProcessPendingMessages();
2534 continue;
2535 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 break;
Bram Moolenaar15e737f2017-03-18 21:22:47 +01002537 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538
Bram Moolenaar0f873732019-12-05 20:28:46 +01002539 // We need to wait for a reply. Enter a message loop until the
2540 // "reply_received" flag gets set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541
Bram Moolenaar0f873732019-12-05 20:28:46 +01002542 // Loop until we receive a reply
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 while (reply_received == 0)
2544 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002545# ifdef FEAT_TIMERS
Bram Moolenaar0f873732019-12-05 20:28:46 +01002546 // TODO: use the return value to decide how long to wait.
Bram Moolenaar42205552017-03-18 19:42:22 +01002547 check_due_timer();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002548# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002549 time(&now);
2550 if (timeout > 0 && (now - start) >= timeout)
2551 break;
2552
Bram Moolenaar0f873732019-12-05 20:28:46 +01002553 // Wait for a SendMessage() call to us. This could be the reply
2554 // we are waiting for. Use a timeout of a second, to catch the
2555 // situation that the server died unexpectedly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 MsgWaitForMultipleObjects(0, NULL, TRUE, 1000, QS_ALLINPUT);
2557
Bram Moolenaar0f873732019-12-05 20:28:46 +01002558 // If the server has died, give up
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 if (!IsWindow(server))
2560 return NULL;
2561
2562 serverProcessPendingMessages();
2563 }
2564 }
2565
2566 return NULL;
2567}
2568
2569/*
2570 * Process any messages in the Windows message queue.
2571 */
2572 void
2573serverProcessPendingMessages(void)
2574{
2575 MSG msg;
2576
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02002577 while (pPeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 {
2579 TranslateMessage(&msg);
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02002580 pDispatchMessage(&msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 }
2582}
2583
Bram Moolenaar0f873732019-12-05 20:28:46 +01002584#endif // FEAT_CLIENTSERVER
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585
2586#if defined(FEAT_GUI) || (defined(FEAT_PRINTER) && !defined(FEAT_POSTSCRIPT)) \
2587 || defined(PROTO)
2588
2589struct charset_pair
2590{
2591 char *name;
2592 BYTE charset;
2593};
2594
2595static struct charset_pair
2596charset_pairs[] =
2597{
2598 {"ANSI", ANSI_CHARSET},
2599 {"CHINESEBIG5", CHINESEBIG5_CHARSET},
2600 {"DEFAULT", DEFAULT_CHARSET},
2601 {"HANGEUL", HANGEUL_CHARSET},
2602 {"OEM", OEM_CHARSET},
2603 {"SHIFTJIS", SHIFTJIS_CHARSET},
2604 {"SYMBOL", SYMBOL_CHARSET},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 {"ARABIC", ARABIC_CHARSET},
2606 {"BALTIC", BALTIC_CHARSET},
2607 {"EASTEUROPE", EASTEUROPE_CHARSET},
2608 {"GB2312", GB2312_CHARSET},
2609 {"GREEK", GREEK_CHARSET},
2610 {"HEBREW", HEBREW_CHARSET},
2611 {"JOHAB", JOHAB_CHARSET},
2612 {"MAC", MAC_CHARSET},
2613 {"RUSSIAN", RUSSIAN_CHARSET},
2614 {"THAI", THAI_CHARSET},
2615 {"TURKISH", TURKISH_CHARSET},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002616# ifdef VIETNAMESE_CHARSET
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 {"VIETNAMESE", VIETNAMESE_CHARSET},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002618# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 {NULL, 0}
2620};
2621
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002622struct quality_pair
2623{
2624 char *name;
2625 DWORD quality;
2626};
2627
2628static struct quality_pair
2629quality_pairs[] = {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002630# ifdef CLEARTYPE_QUALITY
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002631 {"CLEARTYPE", CLEARTYPE_QUALITY},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002632# endif
2633# ifdef ANTIALIASED_QUALITY
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002634 {"ANTIALIASED", ANTIALIASED_QUALITY},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002635# endif
2636# ifdef NONANTIALIASED_QUALITY
Bram Moolenaar73a733e2016-05-11 21:05:05 +02002637 {"NONANTIALIASED", NONANTIALIASED_QUALITY},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002638# endif
2639# ifdef PROOF_QUALITY
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002640 {"PROOF", PROOF_QUALITY},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002641# endif
2642# ifdef DRAFT_QUALITY
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002643 {"DRAFT", DRAFT_QUALITY},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002644# endif
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002645 {"DEFAULT", DEFAULT_QUALITY},
2646 {NULL, 0}
2647};
2648
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649/*
2650 * Convert a charset ID to a name.
2651 * Return NULL when not recognized.
2652 */
2653 char *
2654charset_id2name(int id)
2655{
2656 struct charset_pair *cp;
2657
2658 for (cp = charset_pairs; cp->name != NULL; ++cp)
2659 if ((BYTE)id == cp->charset)
2660 break;
2661 return cp->name;
2662}
2663
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002664/*
2665 * Convert a quality ID to a name.
2666 * Return NULL when not recognized.
2667 */
2668 char *
2669quality_id2name(DWORD id)
2670{
2671 struct quality_pair *qp;
2672
2673 for (qp = quality_pairs; qp->name != NULL; ++qp)
2674 if (id == qp->quality)
2675 break;
2676 return qp->name;
2677}
2678
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002679static const LOGFONTW s_lfDefault =
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680{
2681 -12, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, DEFAULT_CHARSET,
2682 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
2683 PROOF_QUALITY, FIXED_PITCH | FF_DONTCARE,
Bram Moolenaar0f873732019-12-05 20:28:46 +01002684 L"Fixedsys" // see _ReadVimIni
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685};
2686
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002687// Initialise the "current height" to -12 (same as s_lfDefault) just
2688// in case the user specifies a font in "guifont" with no size before a font
2689// with an explicit size has been set. This defaults the size to this value
2690// (-12 equates to roughly 9pt).
2691int current_font_height = -12; // also used in gui_w32.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692
Bram Moolenaar0f873732019-12-05 20:28:46 +01002693/*
2694 * Convert a string representing a point size into pixels. The string should
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 * be a positive decimal number, with an optional decimal point (eg, "12", or
2696 * "10.5"). The pixel value is returned, and a pointer to the next unconverted
2697 * character is stored in *end. The flag "vertical" says whether this
2698 * calculation is for a vertical (height) size or a horizontal (width) one.
2699 */
2700 static int
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002701points_to_pixels(WCHAR *str, WCHAR **end, int vertical, long_i pprinter_dc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702{
2703 int pixels;
2704 int points = 0;
2705 int divisor = 0;
2706 HWND hwnd = (HWND)0;
2707 HDC hdc;
2708 HDC printer_dc = (HDC)pprinter_dc;
2709
2710 while (*str != NUL)
2711 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002712 if (*str == L'.' && divisor == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002714 // Start keeping a divisor, for later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 divisor = 1;
2716 }
2717 else
2718 {
2719 if (!VIM_ISDIGIT(*str))
2720 break;
2721
2722 points *= 10;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002723 points += *str - L'0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 divisor *= 10;
2725 }
2726 ++str;
2727 }
2728
2729 if (divisor == 0)
2730 divisor = 1;
2731
2732 if (printer_dc == NULL)
2733 {
2734 hwnd = GetDesktopWindow();
2735 hdc = GetWindowDC(hwnd);
2736 }
2737 else
2738 hdc = printer_dc;
2739
2740 pixels = MulDiv(points,
2741 GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX),
2742 72 * divisor);
2743
2744 if (printer_dc == NULL)
2745 ReleaseDC(hwnd, hdc);
2746
2747 *end = str;
2748 return pixels;
2749}
2750
2751 static int CALLBACK
2752font_enumproc(
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002753 ENUMLOGFONTW *elf,
2754 NEWTEXTMETRICW *ntm UNUSED,
2755 DWORD type UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 LPARAM lparam)
2757{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002758 // Return value:
2759 // 0 = terminate now (monospace & ANSI)
2760 // 1 = continue, still no luck...
2761 // 2 = continue, but we have an acceptable LOGFONTW
2762 // (monospace, not ANSI)
2763 // We use these values, as EnumFontFamilies returns 1 if the
2764 // callback function is never called. So, we check the return as
2765 // 0 = perfect, 2 = OK, 1 = no good...
2766 // It's not pretty, but it works!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002768 LOGFONTW *lf = (LOGFONTW *)(lparam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002770# ifndef FEAT_PROPORTIONAL_FONTS
Bram Moolenaar0f873732019-12-05 20:28:46 +01002771 // Ignore non-monospace fonts without further ado
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 if ((ntm->tmPitchAndFamily & 1) != 0)
2773 return 1;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002774# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775
Bram Moolenaar0f873732019-12-05 20:28:46 +01002776 // Remember this LOGFONTW as a "possible"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777 *lf = elf->elfLogFont;
2778
Bram Moolenaar0f873732019-12-05 20:28:46 +01002779 // Terminate the scan as soon as we find an ANSI font
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 if (lf->lfCharSet == ANSI_CHARSET
2781 || lf->lfCharSet == OEM_CHARSET
2782 || lf->lfCharSet == DEFAULT_CHARSET)
2783 return 0;
2784
Bram Moolenaar0f873732019-12-05 20:28:46 +01002785 // Continue the scan - we have a non-ANSI font
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 return 2;
2787}
2788
2789 static int
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002790init_logfont(LOGFONTW *lf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791{
2792 int n;
2793 HWND hwnd = GetDesktopWindow();
2794 HDC hdc = GetWindowDC(hwnd);
2795
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002796 n = EnumFontFamiliesW(hdc,
2797 lf->lfFaceName,
2798 (FONTENUMPROCW)font_enumproc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 (LPARAM)lf);
2800
2801 ReleaseDC(hwnd, hdc);
2802
Bram Moolenaar0f873732019-12-05 20:28:46 +01002803 // If we couldn't find a usable font, return failure
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 if (n == 1)
2805 return FAIL;
2806
Bram Moolenaar0f873732019-12-05 20:28:46 +01002807 // Tidy up the rest of the LOGFONTW structure. We set to a basic
2808 // font - get_logfont() sets bold, italic, etc based on the user's
2809 // input.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 lf->lfHeight = current_font_height;
2811 lf->lfWidth = 0;
2812 lf->lfItalic = FALSE;
2813 lf->lfUnderline = FALSE;
2814 lf->lfStrikeOut = FALSE;
2815 lf->lfWeight = FW_NORMAL;
2816
Bram Moolenaar0f873732019-12-05 20:28:46 +01002817 // Return success
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 return OK;
2819}
2820
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002821/*
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002822 * Compare a UTF-16 string and an ASCII string literally.
2823 * Only works all the code points are inside ASCII range.
2824 */
2825 static int
2826utf16ascncmp(const WCHAR *w, const char *p, size_t n)
2827{
2828 size_t i;
2829
2830 for (i = 0; i < n; i++)
2831 {
2832 if (w[i] == 0 || w[i] != p[i])
2833 return w[i] - p[i];
2834 }
2835 return 0;
2836}
2837
2838/*
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002839 * Get font info from "name" into logfont "lf".
2840 * Return OK for a valid name, FAIL otherwise.
2841 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 int
2843get_logfont(
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002844 LOGFONTW *lf,
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002845 char_u *name,
2846 HDC printer_dc,
2847 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002849 WCHAR *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 int i;
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002851 int ret = FAIL;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002852 static LOGFONTW *lastlf = NULL;
2853 WCHAR *wname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854
2855 *lf = s_lfDefault;
2856 if (name == NULL)
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002857 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002859 wname = enc_to_utf16(name, NULL);
2860 if (wname == NULL)
2861 return FAIL;
2862
2863 if (wcscmp(wname, L"*") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002865# if defined(FEAT_GUI_MSWIN)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002866 CHOOSEFONTW cf;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002867 // if name is "*", bring up std font dialog:
Bram Moolenaara80faa82020-04-12 19:37:17 +02002868 CLEAR_FIELD(cf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 cf.lStructSize = sizeof(cf);
2870 cf.hwndOwner = s_hwnd;
2871 cf.Flags = CF_SCREENFONTS | CF_FIXEDPITCHONLY | CF_INITTOLOGFONTSTRUCT;
2872 if (lastlf != NULL)
2873 *lf = *lastlf;
2874 cf.lpLogFont = lf;
2875 cf.nFontType = 0 ; //REGULAR_FONTTYPE;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002876 if (ChooseFontW(&cf))
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002877 ret = OK;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002878# endif
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002879 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880 }
2881
2882 /*
2883 * Split name up, it could be <name>:h<height>:w<width> etc.
2884 */
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002885 for (p = wname; *p && *p != L':'; p++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002887 if (p - wname + 1 >= LF_FACESIZE)
Bram Moolenaar0f873732019-12-05 20:28:46 +01002888 goto theend; // Name too long
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002889 lf->lfFaceName[p - wname] = *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 }
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002891 if (p != wname)
2892 lf->lfFaceName[p - wname] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893
Bram Moolenaar0f873732019-12-05 20:28:46 +01002894 // First set defaults
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 lf->lfHeight = -12;
2896 lf->lfWidth = 0;
2897 lf->lfWeight = FW_NORMAL;
2898 lf->lfItalic = FALSE;
2899 lf->lfUnderline = FALSE;
2900 lf->lfStrikeOut = FALSE;
2901
2902 /*
2903 * If the font can't be found, try replacing '_' by ' '.
2904 */
2905 if (init_logfont(lf) == FAIL)
2906 {
2907 int did_replace = FALSE;
2908
2909 for (i = 0; lf->lfFaceName[i]; ++i)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002910 if (lf->lfFaceName[i] == L'_')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002912 lf->lfFaceName[i] = L' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 did_replace = TRUE;
2914 }
2915 if (!did_replace || init_logfont(lf) == FAIL)
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002916 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 }
2918
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002919 while (*p == L':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 p++;
2921
Bram Moolenaar0f873732019-12-05 20:28:46 +01002922 // Set the values found after ':'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923 while (*p)
2924 {
2925 switch (*p++)
2926 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002927 case L'h':
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002928 lf->lfHeight = - points_to_pixels(p, &p, TRUE, (long_i)printer_dc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002930 case L'w':
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002931 lf->lfWidth = points_to_pixels(p, &p, FALSE, (long_i)printer_dc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 break;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02002933 case L'W':
2934 lf->lfWeight = wcstol(p, &p, 10);
2935 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002936 case L'b':
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 lf->lfWeight = FW_BOLD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002939 case L'i':
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 lf->lfItalic = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002942 case L'u':
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 lf->lfUnderline = TRUE;
2944 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002945 case L's':
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 lf->lfStrikeOut = TRUE;
2947 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002948 case L'c':
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 {
2950 struct charset_pair *cp;
2951
2952 for (cp = charset_pairs; cp->name != NULL; ++cp)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002953 if (utf16ascncmp(p, cp->name, strlen(cp->name)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 {
2955 lf->lfCharSet = cp->charset;
2956 p += strlen(cp->name);
2957 break;
2958 }
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002959 if (cp->name == NULL && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002961 char_u *s = utf16_to_enc(p, NULL);
2962 semsg(_("E244: Illegal charset name \"%s\" in font name \"%s\""), s, name);
2963 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964 break;
2965 }
2966 break;
2967 }
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002968 case L'q':
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002969 {
2970 struct quality_pair *qp;
2971
2972 for (qp = quality_pairs; qp->name != NULL; ++qp)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002973 if (utf16ascncmp(p, qp->name, strlen(qp->name)) == 0)
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002974 {
2975 lf->lfQuality = qp->quality;
2976 p += strlen(qp->name);
2977 break;
2978 }
2979 if (qp->name == NULL && verbose)
2980 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002981 char_u *s = utf16_to_enc(p, NULL);
2982 semsg(_("E244: Illegal quality name \"%s\" in font name \"%s\""), s, name);
2983 vim_free(s);
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002984 break;
2985 }
2986 break;
2987 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988 default:
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002989 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002990 semsg(_("E245: Illegal char '%c' in font name \"%s\""), p[-1], name);
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002991 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992 }
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002993 while (*p == L':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994 p++;
2995 }
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002996 ret = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998theend:
Bram Moolenaar0f873732019-12-05 20:28:46 +01002999 // ron: init lastlf
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003000 if (ret == OK && printer_dc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 {
3002 vim_free(lastlf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003003 lastlf = ALLOC_ONE(LOGFONTW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004 if (lastlf != NULL)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003005 mch_memmove(lastlf, lf, sizeof(LOGFONTW));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006 }
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003007 vim_free(wname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003009 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010}
3011
Bram Moolenaar0f873732019-12-05 20:28:46 +01003012#endif // defined(FEAT_GUI) || defined(FEAT_PRINTER)
Bram Moolenaarf12d9832016-01-29 21:11:25 +01003013
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003014#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaarf12d9832016-01-29 21:11:25 +01003015/*
3016 * Initialize the Winsock dll.
3017 */
3018 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003019channel_init_winsock(void)
Bram Moolenaarf12d9832016-01-29 21:11:25 +01003020{
3021 WSADATA wsaData;
3022 int wsaerr;
3023
3024 if (WSInitialized)
3025 return;
3026
3027 wsaerr = WSAStartup(MAKEWORD(2, 2), &wsaData);
3028 if (wsaerr == 0)
3029 WSInitialized = TRUE;
3030}
3031#endif