blob: 096ac1bc5b6e4ac054ea86dc08a61ea6939fcb9f [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 Moolenaar4f974752019-02-17 17:44:42 +010035# if defined(FEAT_TITLE) && !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
268#ifdef FEAT_TITLE
269/*
270 * mch_settitle(): set titlebar of our window
271 */
272 void
273mch_settitle(
274 char_u *title,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +0200275 char_u *icon UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000276{
277# ifdef FEAT_GUI_MSWIN
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200278# ifdef VIMDLL
279 if (gui.in_use)
280# endif
281 {
282 gui_mch_settitle(title, icon);
283 return;
284 }
285# endif
286# if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000287 if (title != NULL)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000288 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200289 WCHAR *wp = enc_to_utf16(title, NULL);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000290
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200291 if (wp == NULL)
292 return;
293
294 SetConsoleTitleW(wp);
295 vim_free(wp);
296 return;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000297 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000298# endif
299}
300
301
302/*
303 * Restore the window/icon title.
304 * which is one of:
Bram Moolenaar40385db2018-08-07 22:31:44 +0200305 * SAVE_RESTORE_TITLE: Just restore title
306 * SAVE_RESTORE_ICON: Just restore icon (which we don't have)
307 * SAVE_RESTORE_BOTH: Restore title and icon (which we don't have)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000308 */
309 void
Bram Moolenaar1266d672017-02-01 13:43:36 +0100310mch_restore_title(int which UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100312# if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
313# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200314 if (!gui.in_use)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100315# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200316 SetConsoleTitle(g_szOrigTitle);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100317# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000318}
319
320
321/*
322 * Return TRUE if we can restore the title (we can)
323 */
324 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100325mch_can_restore_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326{
327 return TRUE;
328}
329
330
331/*
332 * Return TRUE if we can restore the icon title (we can't)
333 */
334 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100335mch_can_restore_icon(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336{
337 return FALSE;
338}
Bram Moolenaar0f873732019-12-05 20:28:46 +0100339#endif // FEAT_TITLE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340
341
342/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000343 * Get absolute file name into buffer "buf" of length "len" bytes,
344 * turning all '/'s into '\\'s and getting the correct case of each component
345 * of the file name. Append a (back)slash to a directory name.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346 * When 'shellslash' set do it the other way around.
347 * Return OK or FAIL.
348 */
349 int
350mch_FullName(
351 char_u *fname,
352 char_u *buf,
353 int len,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100354 int force UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355{
356 int nResult = FAIL;
Bram Moolenaareae1b912019-05-09 15:12:55 +0200357 WCHAR *wname;
358 WCHAR wbuf[MAX_PATH];
359 char_u *cname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360
Bram Moolenaareae1b912019-05-09 15:12:55 +0200361 wname = enc_to_utf16(fname, NULL);
362 if (wname != NULL && _wfullpath(wbuf, wname, MAX_PATH) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363 {
Bram Moolenaareae1b912019-05-09 15:12:55 +0200364 cname = utf16_to_enc((short_u *)wbuf, NULL);
365 if (cname != NULL)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000366 {
Bram Moolenaareae1b912019-05-09 15:12:55 +0200367 vim_strncpy(buf, cname, len - 1);
368 nResult = OK;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000369 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370 }
Bram Moolenaareae1b912019-05-09 15:12:55 +0200371 vim_free(wname);
372 vim_free(cname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373
374#ifdef USE_FNAME_CASE
375 fname_case(buf, len);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000376#else
377 slash_adjust(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378#endif
379
380 return nResult;
381}
382
383
384/*
385 * Return TRUE if "fname" does not depend on the current directory.
386 */
387 int
388mch_isFullName(char_u *fname)
389{
Bram Moolenaar0ea74212020-12-11 20:10:50 +0100390 // A name like "d:/foo" and "//server/share" is absolute. "d:foo" is not.
391 // Another way to check is to use mch_FullName() and see if the result is
392 // the same as the name or mch_FullName() fails. However, this has quite a
393 // bit of overhead, so let's not do that.
394 return ((ASCII_ISALPHA(fname[0]) && fname[1] == ':'
395 && (fname[2] == '/' || fname[2] == '\\'))
396 || (fname[0] == fname[1] && (fname[0] == '/' || fname[0] == '\\')));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397}
398
399/*
400 * Replace all slashes by backslashes.
401 * This used to be the other way around, but MS-DOS sometimes has problems
402 * with slashes (e.g. in a command name). We can't have mixed slashes and
403 * backslashes, because comparing file names will not work correctly. The
404 * commands that use a file name should try to avoid the need to type a
405 * backslash twice.
406 * When 'shellslash' set do it the other way around.
Bram Moolenaarb4f6a462015-10-13 19:43:17 +0200407 * When the path looks like a URL leave it unmodified.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408 */
409 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100410slash_adjust(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411{
Bram Moolenaarb4f6a462015-10-13 19:43:17 +0200412 if (path_with_url(p))
413 return;
Bram Moolenaar39d21e32017-08-05 23:09:31 +0200414
415 if (*p == '`')
416 {
Bram Moolenaar116a0f82017-08-07 21:17:57 +0200417 size_t len = STRLEN(p);
418
Bram Moolenaar0f873732019-12-05 20:28:46 +0100419 // don't replace backslash in backtick quoted strings
Bram Moolenaar39d21e32017-08-05 23:09:31 +0200420 if (len > 2 && *(p + len - 1) == '`')
421 return;
422 }
423
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +0000424 while (*p)
425 {
426 if (*p == psepcN)
427 *p = psepc;
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100428 MB_PTR_ADV(p);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +0000429 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430}
431
Bram Moolenaar0f873732019-12-05 20:28:46 +0100432// Use 64-bit stat functions if available.
Bram Moolenaar8767f522016-07-01 17:17:39 +0200433#ifdef HAVE_STAT64
434# undef stat
435# undef _stat
436# undef _wstat
437# undef _fstat
438# define stat _stat64
439# define _stat _stat64
440# define _wstat _wstat64
441# define _fstat _fstat64
442#endif
443
Bram Moolenaar9d1685d2014-01-14 12:18:45 +0100444#if (defined(_MSC_VER) && (_MSC_VER >= 1300)) || defined(__MINGW32__)
Bram Moolenaar8962fda2013-09-29 19:05:21 +0200445# define OPEN_OH_ARGTYPE intptr_t
446#else
447# define OPEN_OH_ARGTYPE long
448#endif
449
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200450 static int
Bram Moolenaar8767f522016-07-01 17:17:39 +0200451wstat_symlink_aware(const WCHAR *name, stat_T *stp)
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200452{
Bram Moolenaara12a1612019-01-24 16:39:02 +0100453#if (defined(_MSC_VER) && (_MSC_VER < 1900)) || defined(__MINGW32__)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100454 // Work around for VC12 or earlier (and MinGW). _wstat() can't handle
455 // symlinks properly.
456 // VC9 or earlier: _wstat() doesn't support a symlink at all. It retrieves
457 // status of a symlink itself.
458 // VC10: _wstat() supports a symlink to a normal file, but it doesn't
459 // support a symlink to a directory (always returns an error).
460 // VC11 and VC12: _wstat() doesn't return an error for a symlink to a
461 // directory, but it doesn't set S_IFDIR flag.
462 // MinGW: Same as VC9.
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200463 int n;
464 BOOL is_symlink = FALSE;
465 HANDLE hFind, h;
466 DWORD attr = 0;
467 WIN32_FIND_DATAW findDataW;
468
469 hFind = FindFirstFileW(name, &findDataW);
470 if (hFind != INVALID_HANDLE_VALUE)
471 {
472 attr = findDataW.dwFileAttributes;
473 if ((attr & FILE_ATTRIBUTE_REPARSE_POINT)
474 && (findDataW.dwReserved0 == IO_REPARSE_TAG_SYMLINK))
475 is_symlink = TRUE;
476 FindClose(hFind);
477 }
478 if (is_symlink)
479 {
480 h = CreateFileW(name, FILE_READ_ATTRIBUTES,
481 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
482 OPEN_EXISTING,
483 (attr & FILE_ATTRIBUTE_DIRECTORY)
484 ? FILE_FLAG_BACKUP_SEMANTICS : 0,
485 NULL);
486 if (h != INVALID_HANDLE_VALUE)
487 {
488 int fd;
489
Bram Moolenaar8962fda2013-09-29 19:05:21 +0200490 fd = _open_osfhandle((OPEN_OH_ARGTYPE)h, _O_RDONLY);
Bram Moolenaar8767f522016-07-01 17:17:39 +0200491 n = _fstat(fd, (struct _stat *)stp);
Bram Moolenaarfce7b3d2016-01-19 19:00:32 +0100492 if ((n == 0) && (attr & FILE_ATTRIBUTE_DIRECTORY))
493 stp->st_mode = (stp->st_mode & ~S_IFREG) | S_IFDIR;
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200494 _close(fd);
495 return n;
496 }
497 }
Bram Moolenaara12a1612019-01-24 16:39:02 +0100498#endif
Bram Moolenaar8767f522016-07-01 17:17:39 +0200499 return _wstat(name, (struct _stat *)stp);
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200500}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501
502/*
503 * stat() can't handle a trailing '/' or '\', remove it first.
504 */
505 int
Bram Moolenaar8767f522016-07-01 17:17:39 +0200506vim_stat(const char *name, stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100508 // WinNT and later can use _MAX_PATH wide characters for a pathname, which
509 // means that the maximum pathname is _MAX_PATH * 3 bytes when 'enc' is
510 // UTF-8.
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100511 char_u buf[_MAX_PATH * 3 + 1];
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100512 char_u *p;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200513 WCHAR *wp;
514 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515
Bram Moolenaard2a203b2013-08-30 16:51:18 +0200516 vim_strncpy((char_u *)buf, (char_u *)name, sizeof(buf) - 1);
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100517 p = buf + STRLEN(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518 if (p > buf)
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100519 MB_PTR_BACK(buf, p);
Bram Moolenaarb1cb35f2014-01-10 13:05:20 +0100520
Bram Moolenaar0f873732019-12-05 20:28:46 +0100521 // Remove trailing '\\' except root path.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522 if (p > buf && (*p == '\\' || *p == '/') && p[-1] != ':')
523 *p = NUL;
Bram Moolenaarb1cb35f2014-01-10 13:05:20 +0100524
525 if ((buf[0] == '\\' && buf[1] == '\\') || (buf[0] == '/' && buf[1] == '/'))
526 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100527 // UNC root path must be followed by '\\'.
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100528 p = vim_strpbrk(buf + 2, (char_u *)"\\/");
Bram Moolenaarb1cb35f2014-01-10 13:05:20 +0100529 if (p != NULL)
530 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100531 p = vim_strpbrk(p + 1, (char_u *)"\\/");
Bram Moolenaarb1cb35f2014-01-10 13:05:20 +0100532 if (p == NULL)
533 STRCAT(buf, "\\");
534 }
535 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200537 wp = enc_to_utf16(buf, NULL);
538 if (wp == NULL)
539 return -1;
540
541 n = wstat_symlink_aware(wp, stp);
542 vim_free(wp);
543 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544}
545
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200546#if (defined(FEAT_GUI_MSWIN) && !defined(VIMDLL)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 void
Bram Moolenaar26e86442020-05-17 14:06:16 +0200548mch_settmode(tmode_T tmode UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100550 // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551}
552
553 int
554mch_get_shellsize(void)
555{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100556 // never used
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557 return OK;
558}
559
560 void
561mch_set_shellsize(void)
562{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100563 // never used
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564}
565
566/*
567 * Rows and/or Columns has changed.
568 */
569 void
570mch_new_shellsize(void)
571{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100572 // never used
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573}
574
575#endif
576
577/*
578 * We have no job control, so fake it by starting a new shell.
579 */
580 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100581mch_suspend(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582{
583 suspend_shell();
584}
585
586#if defined(USE_MCH_ERRMSG) || defined(PROTO)
587
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200588# ifdef display_errors
589# undef display_errors
590# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591
592/*
593 * Display the saved error message(s).
594 */
595 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100596display_errors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597{
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200598# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599 char *p;
600
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200601# ifdef VIMDLL
602 if (gui.in_use || gui.starting)
603# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200605 if (error_ga.ga_data != NULL)
606 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100607 // avoid putting up a message box with blanks only
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200608 for (p = (char *)error_ga.ga_data; *p; ++p)
609 if (!isspace(*p))
610 {
611 (void)gui_mch_dialog(
Bram Moolenaarc17ef8e2006-03-25 21:48:58 +0000612 gui.starting ? VIM_INFO :
Bram Moolenaarc17ef8e2006-03-25 21:48:58 +0000613 VIM_ERROR,
Bram Moolenaarc17ef8e2006-03-25 21:48:58 +0000614 gui.starting ? (char_u *)_("Message") :
Bram Moolenaarc17ef8e2006-03-25 21:48:58 +0000615 (char_u *)_("Error"),
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100616 (char_u *)p, (char_u *)_("&Ok"),
617 1, NULL, FALSE);
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200618 break;
619 }
620 ga_clear(&error_ga);
621 }
622 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200624# endif
625# if !defined(FEAT_GUI) || defined(VIMDLL)
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +0100626 FlushFileBuffers(GetStdHandle(STD_ERROR_HANDLE));
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200627# endif
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +0100628}
629#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630
631
632/*
633 * Return TRUE if "p" contain a wildcard that can be expanded by
634 * dos_expandpath().
635 */
636 int
637mch_has_exp_wildcard(char_u *p)
638{
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100639 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 {
641 if (vim_strchr((char_u *)"?*[", *p) != NULL
642 || (*p == '~' && p[1] != NUL))
643 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 }
645 return FALSE;
646}
647
648/*
649 * Return TRUE if "p" contain a wildcard or a "~1" kind of thing (could be a
650 * shortened file name).
651 */
652 int
653mch_has_wildcard(char_u *p)
654{
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100655 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 {
657 if (vim_strchr((char_u *)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100658#ifdef VIM_BACKTICK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 "?*$[`"
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100660#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 "?*$["
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100662#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 , *p) != NULL
664 || (*p == '~' && p[1] != NUL))
665 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 }
667 return FALSE;
668}
669
670
671/*
672 * The normal _chdir() does not change the default drive. This one does.
673 * Returning 0 implies success; -1 implies failure.
674 */
675 int
676mch_chdir(char *path)
677{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200678 WCHAR *p;
679 int n;
680
Bram Moolenaar0f873732019-12-05 20:28:46 +0100681 if (path[0] == NUL) // just checking...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 return -1;
683
Bram Moolenaara2974d72009-07-14 16:38:36 +0000684 if (p_verbose >= 5)
685 {
686 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100687 smsg("chdir(%s)", path);
Bram Moolenaara2974d72009-07-14 16:38:36 +0000688 verbose_leave();
689 }
Bram Moolenaar0f873732019-12-05 20:28:46 +0100690 if (isalpha(path[0]) && path[1] == ':') // has a drive name
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100692 // If we can change to the drive, skip that part of the path. If we
693 // can't then the current directory may be invalid, try using chdir()
694 // with the whole path.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695 if (_chdrive(TOLOWER_ASC(path[0]) - 'a' + 1) == 0)
696 path += 2;
697 }
698
Bram Moolenaar0f873732019-12-05 20:28:46 +0100699 if (*path == NUL) // drive name only
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 return 0;
701
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200702 p = enc_to_utf16((char_u *)path, NULL);
703 if (p == NULL)
704 return -1;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000705
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200706 n = _wchdir(p);
707 vim_free(p);
708 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709}
710
711
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200712#if defined(FEAT_GUI_MSWIN) && !defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713/*
714 * return non-zero if a character is available
715 */
716 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100717mch_char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100719 // never used
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 return TRUE;
721}
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200722
723# if defined(FEAT_TERMINAL) || defined(PROTO)
724/*
725 * Check for any pending input or messages.
726 */
727 int
728mch_check_messages(void)
729{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100730 // TODO: check for messages
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200731 return TRUE;
732}
733# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734#endif
735
736
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737#if defined(FEAT_LIBCALL) || defined(PROTO)
738/*
739 * Call a DLL routine which takes either a string or int param
740 * and returns an allocated string.
741 * Return OK if it worked, FAIL if not.
742 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743typedef LPTSTR (*MYSTRPROCSTR)(LPTSTR);
744typedef LPTSTR (*MYINTPROCSTR)(int);
745typedef int (*MYSTRPROCINT)(LPTSTR);
746typedef int (*MYINTPROCINT)(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748/*
749 * Check if a pointer points to a valid NUL terminated string.
750 * Return the length of the string, including terminating NUL.
751 * Returns 0 for an invalid pointer, 1 for an empty string.
752 */
753 static size_t
754check_str_len(char_u *str)
755{
756 SYSTEM_INFO si;
757 MEMORY_BASIC_INFORMATION mbi;
758 size_t length = 0;
759 size_t i;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100760 const char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761
Bram Moolenaar0f873732019-12-05 20:28:46 +0100762 // get page size
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763 GetSystemInfo(&si);
764
Bram Moolenaar0f873732019-12-05 20:28:46 +0100765 // get memory information
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 if (VirtualQuery(str, &mbi, sizeof(mbi)))
767 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100768 // pre cast these (typing savers)
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000769 long_u dwStr = (long_u)str;
770 long_u dwBaseAddress = (long_u)mbi.BaseAddress;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771
Bram Moolenaar0f873732019-12-05 20:28:46 +0100772 // get start address of page that str is on
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000773 long_u strPage = dwStr - (dwStr - dwBaseAddress) % si.dwPageSize;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774
Bram Moolenaar0f873732019-12-05 20:28:46 +0100775 // get length from str to end of page
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000776 long_u pageLength = si.dwPageSize - (dwStr - strPage);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777
Bram Moolenaar442b4222010-05-24 21:34:22 +0200778 for (p = str; !IsBadReadPtr(p, (UINT)pageLength);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 p += pageLength, pageLength = si.dwPageSize)
780 for (i = 0; i < pageLength; ++i, ++length)
781 if (p[i] == NUL)
782 return length + 1;
783 }
784
785 return 0;
786}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200788/*
789 * Passed to do_in_runtimepath() to load a vim.ico file.
790 */
791 static void
792mch_icon_load_cb(char_u *fname, void *cookie)
793{
794 HANDLE *h = (HANDLE *)cookie;
795
796 *h = LoadImage(NULL,
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100797 (LPSTR)fname,
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200798 IMAGE_ICON,
799 64,
800 64,
801 LR_LOADFROMFILE | LR_LOADMAP3DCOLORS);
802}
803
804/*
805 * Try loading an icon file from 'runtimepath'.
806 */
807 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100808mch_icon_load(HANDLE *iconp)
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200809{
810 return do_in_runtimepath((char_u *)"bitmaps/vim.ico",
Bram Moolenaar7f8989d2016-03-12 22:11:39 +0100811 0, mch_icon_load_cb, iconp);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200812}
813
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 int
815mch_libcall(
816 char_u *libname,
817 char_u *funcname,
Bram Moolenaar0f873732019-12-05 20:28:46 +0100818 char_u *argstring, // NULL when using a argint
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 int argint,
Bram Moolenaar0f873732019-12-05 20:28:46 +0100820 char_u **string_result,// NULL when using number_result
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 int *number_result)
822{
823 HINSTANCE hinstLib;
824 MYSTRPROCSTR ProcAdd;
825 MYINTPROCSTR ProcAddI;
826 char_u *retval_str = NULL;
827 int retval_int = 0;
828 size_t len;
829
830 BOOL fRunTimeLinkSuccess = FALSE;
831
832 // Get a handle to the DLL module.
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100833 hinstLib = vimLoadLib((char *)libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834
835 // If the handle is valid, try to get the function address.
836 if (hinstLib != NULL)
837 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100838# ifdef HAVE_TRY_EXCEPT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 __try
840 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100841# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 if (argstring != NULL)
843 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100844 // Call with string argument
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100845 ProcAdd = (MYSTRPROCSTR)GetProcAddress(hinstLib, (LPCSTR)funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 if ((fRunTimeLinkSuccess = (ProcAdd != NULL)) != 0)
847 {
848 if (string_result == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100849 retval_int = ((MYSTRPROCINT)ProcAdd)((LPSTR)argstring);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100851 retval_str = (char_u *)(ProcAdd)((LPSTR)argstring);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 }
853 }
854 else
855 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100856 // Call with number argument
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100857 ProcAddI = (MYINTPROCSTR) GetProcAddress(hinstLib, (LPCSTR)funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 if ((fRunTimeLinkSuccess = (ProcAddI != NULL)) != 0)
859 {
860 if (string_result == NULL)
861 retval_int = ((MYINTPROCINT)ProcAddI)(argint);
862 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100863 retval_str = (char_u *)(ProcAddI)(argint);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 }
865 }
866
867 // Save the string before we free the library.
868 // Assume that a "1" result is an illegal pointer.
869 if (string_result == NULL)
870 *number_result = retval_int;
871 else if (retval_str != NULL
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100872 && (len = check_str_len(retval_str)) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 {
Bram Moolenaar18a4ba22019-05-24 19:39:03 +0200874 *string_result = alloc(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 if (*string_result != NULL)
876 mch_memmove(*string_result, retval_str, len);
877 }
878
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100879# ifdef HAVE_TRY_EXCEPT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 }
881 __except(EXCEPTION_EXECUTE_HANDLER)
882 {
883 if (GetExceptionCode() == EXCEPTION_STACK_OVERFLOW)
884 RESETSTKOFLW();
885 fRunTimeLinkSuccess = 0;
886 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100887# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888
889 // Free the DLL module.
890 (void)FreeLibrary(hinstLib);
891 }
892
893 if (!fRunTimeLinkSuccess)
894 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100895 semsg(_(e_libcall), funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 return FAIL;
897 }
898
899 return OK;
900}
901#endif
902
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903/*
904 * Debugging helper: expose the MCH_WRITE_DUMP stuff to other modules
905 */
906 void
Bram Moolenaar1266d672017-02-01 13:43:36 +0100907DumpPutS(const char *psz UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100909#ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910 if (fdDump)
911 {
912 fputs(psz, fdDump);
913 if (psz[strlen(psz) - 1] != '\n')
914 fputc('\n', fdDump);
915 fflush(fdDump);
916 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100917#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918}
919
920#ifdef _DEBUG
921
922void __cdecl
923Trace(
924 char *pszFormat,
925 ...)
926{
927 CHAR szBuff[2048];
928 va_list args;
929
930 va_start(args, pszFormat);
931 vsprintf(szBuff, pszFormat, args);
932 va_end(args);
933
934 OutputDebugString(szBuff);
935}
936
937#endif //_DEBUG
938
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200939#if !defined(FEAT_GUI) || defined(VIMDLL) || defined(PROTO)
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200940# ifdef FEAT_TITLE
Bram Moolenaar0f873732019-12-05 20:28:46 +0100941extern HWND g_hWnd; // This is in os_win32.c.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942# endif
943
944/*
945 * Showing the printer dialog is tricky since we have no GUI
946 * window to parent it. The following routines are needed to
947 * get the window parenting and Z-order to work properly.
948 */
949 static void
950GetConsoleHwnd(void)
951{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100952 // Skip if it's already set.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 if (s_hwnd != 0)
954 return;
955
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200956# ifdef FEAT_TITLE
Bram Moolenaar0f873732019-12-05 20:28:46 +0100957 // Window handle may have been found by init code (Windows NT only)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 if (g_hWnd != 0)
959 {
960 s_hwnd = g_hWnd;
961 return;
962 }
963# endif
964
Bram Moolenaare1ed53f2019-02-12 23:12:37 +0100965 s_hwnd = GetConsoleWindow();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966}
Bram Moolenaar843ee412004-06-30 16:16:41 +0000967
968/*
969 * Console implementation of ":winpos".
970 */
971 int
972mch_get_winpos(int *x, int *y)
973{
974 RECT rect;
975
976 GetConsoleHwnd();
977 GetWindowRect(s_hwnd, &rect);
978 *x = rect.left;
979 *y = rect.top;
980 return OK;
981}
982
983/*
984 * Console implementation of ":winpos x y".
985 */
986 void
987mch_set_winpos(int x, int y)
988{
989 GetConsoleHwnd();
990 SetWindowPos(s_hwnd, NULL, x, y, 0, 0,
991 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
992}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993#endif
994
995#if (defined(FEAT_PRINTER) && !defined(FEAT_POSTSCRIPT)) || defined(PROTO)
996
Bram Moolenaar0f873732019-12-05 20:28:46 +0100997//=================================================================
998// Win32 printer stuff
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999
1000static HFONT prt_font_handles[2][2][2];
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001001static PRINTDLGW prt_dlg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002static const int boldface[2] = {FW_REGULAR, FW_BOLD};
1003static TEXTMETRIC prt_tm;
1004static int prt_line_height;
1005static int prt_number_width;
1006static int prt_left_margin;
1007static int prt_right_margin;
1008static int prt_top_margin;
1009static char_u szAppName[] = TEXT("VIM");
1010static HWND hDlgPrint;
1011static int *bUserAbort = NULL;
1012static char_u *prt_name = NULL;
1013
Bram Moolenaar0f873732019-12-05 20:28:46 +01001014// Defines which are also in vim.rc.
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001015# define IDC_BOX1 400
1016# define IDC_PRINTTEXT1 401
1017# define IDC_PRINTTEXT2 402
1018# define IDC_PROGRESS 403
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001020 static BOOL
1021vimSetDlgItemText(HWND hDlg, int nIDDlgItem, char_u *s)
1022{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001023 WCHAR *wp;
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001024 BOOL ret;
1025
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001026 wp = enc_to_utf16(s, NULL);
1027 if (wp == NULL)
1028 return FALSE;
1029
1030 ret = SetDlgItemTextW(hDlg, nIDDlgItem, wp);
1031 vim_free(wp);
1032 return ret;
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001033}
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001034
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035/*
1036 * Convert BGR to RGB for Windows GDI calls
1037 */
1038 static COLORREF
1039swap_me(COLORREF colorref)
1040{
1041 int temp;
1042 char *ptr = (char *)&colorref;
1043
1044 temp = *(ptr);
1045 *(ptr ) = *(ptr + 2);
1046 *(ptr + 2) = temp;
1047 return colorref;
1048}
1049
Bram Moolenaar0f873732019-12-05 20:28:46 +01001050// Attempt to make this work for old and new compilers
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001051# if !defined(_WIN64) && (!defined(_MSC_VER) || _MSC_VER < 1300)
1052# define PDP_RETVAL BOOL
1053# else
1054# define PDP_RETVAL INT_PTR
1055# endif
Bram Moolenaared39e1d2008-08-09 17:55:22 +00001056
Bram Moolenaared39e1d2008-08-09 17:55:22 +00001057 static PDP_RETVAL CALLBACK
Bram Moolenaar1266d672017-02-01 13:43:36 +01001058PrintDlgProc(
1059 HWND hDlg,
1060 UINT message,
1061 WPARAM wParam UNUSED,
1062 LPARAM lParam UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001064# ifdef FEAT_GETTEXT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 NONCLIENTMETRICS nm;
1066 static HFONT hfont;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001067# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068
1069 switch (message)
1070 {
1071 case WM_INITDIALOG:
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001072# ifdef FEAT_GETTEXT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 nm.cbSize = sizeof(NONCLIENTMETRICS);
1074 if (SystemParametersInfo(
1075 SPI_GETNONCLIENTMETRICS,
1076 sizeof(NONCLIENTMETRICS),
1077 &nm,
1078 0))
1079 {
1080 char buff[MAX_PATH];
1081 int i;
1082
Bram Moolenaar0f873732019-12-05 20:28:46 +01001083 // Translate the dialog texts
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 hfont = CreateFontIndirect(&nm.lfMessageFont);
1085 for (i = IDC_PRINTTEXT1; i <= IDC_PROGRESS; i++)
1086 {
1087 SendDlgItemMessage(hDlg, i, WM_SETFONT, (WPARAM)hfont, 1);
1088 if (GetDlgItemText(hDlg,i, buff, sizeof(buff)))
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001089 vimSetDlgItemText(hDlg,i, (char_u *)_(buff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 }
1091 SendDlgItemMessage(hDlg, IDCANCEL,
1092 WM_SETFONT, (WPARAM)hfont, 1);
1093 if (GetDlgItemText(hDlg,IDCANCEL, buff, sizeof(buff)))
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001094 vimSetDlgItemText(hDlg,IDCANCEL, (char_u *)_(buff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001096# endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001097 SetWindowText(hDlg, (LPCSTR)szAppName);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 if (prt_name != NULL)
1099 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001100 vimSetDlgItemText(hDlg, IDC_PRINTTEXT2, (char_u *)prt_name);
Bram Moolenaard23a8232018-02-10 18:45:26 +01001101 VIM_CLEAR(prt_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 }
1103 EnableMenuItem(GetSystemMenu(hDlg, FALSE), SC_CLOSE, MF_GRAYED);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001104# if !defined(FEAT_GUI) || defined(VIMDLL)
1105# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001106 if (!gui.in_use)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001107# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001108 BringWindowToTop(s_hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001109# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 return TRUE;
1111
1112 case WM_COMMAND:
1113 *bUserAbort = TRUE;
1114 EnableWindow(GetParent(hDlg), TRUE);
1115 DestroyWindow(hDlg);
1116 hDlgPrint = NULL;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001117# ifdef FEAT_GETTEXT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 DeleteObject(hfont);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001119# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 return TRUE;
1121 }
1122 return FALSE;
1123}
1124
1125 static BOOL CALLBACK
Bram Moolenaar1266d672017-02-01 13:43:36 +01001126AbortProc(HDC hdcPrn UNUSED, int iCode UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127{
1128 MSG msg;
1129
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02001130 while (!*bUserAbort && pPeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 {
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02001132 if (!hDlgPrint || !pIsDialogMessage(hDlgPrint, &msg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 {
1134 TranslateMessage(&msg);
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02001135 pDispatchMessage(&msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 }
1137 }
1138 return !*bUserAbort;
1139}
1140
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001141# if !defined(FEAT_GUI) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142
Bram Moolenaar26fdd7d2011-11-30 13:42:44 +01001143 static UINT_PTR CALLBACK
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144PrintHookProc(
1145 HWND hDlg, // handle to dialog box
1146 UINT uiMsg, // message identifier
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02001147 WPARAM wParam UNUSED, // message parameter
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 LPARAM lParam // message parameter
1149 )
1150{
1151 HWND hwndOwner;
1152 RECT rc, rcDlg, rcOwner;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001153 PRINTDLGW *pPD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154
1155 if (uiMsg == WM_INITDIALOG)
1156 {
1157 // Get the owner window and dialog box rectangles.
1158 if ((hwndOwner = GetParent(hDlg)) == NULL)
1159 hwndOwner = GetDesktopWindow();
1160
1161 GetWindowRect(hwndOwner, &rcOwner);
1162 GetWindowRect(hDlg, &rcDlg);
1163 CopyRect(&rc, &rcOwner);
1164
1165 // Offset the owner and dialog box rectangles so that
1166 // right and bottom values represent the width and
1167 // height, and then offset the owner again to discard
1168 // space taken up by the dialog box.
1169
1170 OffsetRect(&rcDlg, -rcDlg.left, -rcDlg.top);
1171 OffsetRect(&rc, -rc.left, -rc.top);
1172 OffsetRect(&rc, -rcDlg.right, -rcDlg.bottom);
1173
1174 // The new position is the sum of half the remaining
1175 // space and the owner's original position.
1176
1177 SetWindowPos(hDlg,
1178 HWND_TOP,
1179 rcOwner.left + (rc.right / 2),
1180 rcOwner.top + (rc.bottom / 2),
1181 0, 0, // ignores size arguments
1182 SWP_NOSIZE);
1183
Bram Moolenaar0f873732019-12-05 20:28:46 +01001184 // tackle the printdlg copiesctrl problem
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001185 pPD = (PRINTDLGW *)lParam;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 pPD->nCopies = (WORD)pPD->lCustData;
1187 SetDlgItemInt( hDlg, edt3, pPD->nCopies, FALSE );
Bram Moolenaar0f873732019-12-05 20:28:46 +01001188 // Bring the window to top
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 BringWindowToTop(GetParent(hDlg));
1190 SetForegroundWindow(hDlg);
1191 }
1192
1193 return FALSE;
1194}
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001195# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196
1197 void
1198mch_print_cleanup(void)
1199{
1200 int pifItalic;
1201 int pifBold;
1202 int pifUnderline;
1203
1204 for (pifBold = 0; pifBold <= 1; pifBold++)
1205 for (pifItalic = 0; pifItalic <= 1; pifItalic++)
1206 for (pifUnderline = 0; pifUnderline <= 1; pifUnderline++)
1207 DeleteObject(prt_font_handles[pifBold][pifItalic][pifUnderline]);
1208
1209 if (prt_dlg.hDC != NULL)
1210 DeleteDC(prt_dlg.hDC);
1211 if (!*bUserAbort)
1212 SendMessage(hDlgPrint, WM_COMMAND, 0, 0);
1213}
1214
1215 static int
1216to_device_units(int idx, int dpi, int physsize, int offset, int def_number)
1217{
1218 int ret = 0;
1219 int u;
1220 int nr;
1221
1222 u = prt_get_unit(idx);
1223 if (u == PRT_UNIT_NONE)
1224 {
1225 u = PRT_UNIT_PERC;
1226 nr = def_number;
1227 }
1228 else
1229 nr = printer_opts[idx].number;
1230
1231 switch (u)
1232 {
1233 case PRT_UNIT_PERC:
1234 ret = (physsize * nr) / 100;
1235 break;
1236 case PRT_UNIT_INCH:
1237 ret = (nr * dpi);
1238 break;
1239 case PRT_UNIT_MM:
1240 ret = (nr * 10 * dpi) / 254;
1241 break;
1242 case PRT_UNIT_POINT:
1243 ret = (nr * 10 * dpi) / 720;
1244 break;
1245 }
1246
1247 if (ret < offset)
1248 return 0;
1249 else
1250 return ret - offset;
1251}
1252
1253 static int
1254prt_get_cpl(void)
1255{
1256 int hr;
1257 int phyw;
1258 int dvoff;
1259 int rev_offset;
1260 int dpi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261
1262 GetTextMetrics(prt_dlg.hDC, &prt_tm);
1263 prt_line_height = prt_tm.tmHeight + prt_tm.tmExternalLeading;
1264
1265 hr = GetDeviceCaps(prt_dlg.hDC, HORZRES);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 phyw = GetDeviceCaps(prt_dlg.hDC, PHYSICALWIDTH);
1267 dvoff = GetDeviceCaps(prt_dlg.hDC, PHYSICALOFFSETX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 dpi = GetDeviceCaps(prt_dlg.hDC, LOGPIXELSX);
1269
1270 rev_offset = phyw - (dvoff + hr);
1271
1272 prt_left_margin = to_device_units(OPT_PRINT_LEFT, dpi, phyw, dvoff, 10);
1273 if (prt_use_number())
1274 {
1275 prt_number_width = PRINT_NUMBER_WIDTH * prt_tm.tmAveCharWidth;
1276 prt_left_margin += prt_number_width;
1277 }
1278 else
1279 prt_number_width = 0;
1280
1281 prt_right_margin = hr - to_device_units(OPT_PRINT_RIGHT, dpi, phyw,
1282 rev_offset, 5);
1283
1284 return (prt_right_margin - prt_left_margin) / prt_tm.tmAveCharWidth;
1285}
1286
1287 static int
1288prt_get_lpp(void)
1289{
1290 int vr;
1291 int phyw;
1292 int dvoff;
1293 int rev_offset;
1294 int bottom_margin;
1295 int dpi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296
1297 vr = GetDeviceCaps(prt_dlg.hDC, VERTRES);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 phyw = GetDeviceCaps(prt_dlg.hDC, PHYSICALHEIGHT);
1299 dvoff = GetDeviceCaps(prt_dlg.hDC, PHYSICALOFFSETY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 dpi = GetDeviceCaps(prt_dlg.hDC, LOGPIXELSY);
1301
1302 rev_offset = phyw - (dvoff + vr);
1303
1304 prt_top_margin = to_device_units(OPT_PRINT_TOP, dpi, phyw, dvoff, 5);
1305
Bram Moolenaar0f873732019-12-05 20:28:46 +01001306 // adjust top margin if there is a header
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 prt_top_margin += prt_line_height * prt_header_height();
1308
1309 bottom_margin = vr - to_device_units(OPT_PRINT_BOT, dpi, phyw,
1310 rev_offset, 5);
1311
1312 return (bottom_margin - prt_top_margin) / prt_line_height;
1313}
1314
1315 int
1316mch_print_init(prt_settings_T *psettings, char_u *jobname, int forceit)
1317{
1318 static HGLOBAL stored_dm = NULL;
1319 static HGLOBAL stored_devn = NULL;
1320 static int stored_nCopies = 1;
1321 static int stored_nFlags = 0;
1322
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001323 LOGFONTW fLogFont;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 int pifItalic;
1325 int pifBold;
1326 int pifUnderline;
1327
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001328 DEVMODEW *mem;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 DEVNAMES *devname;
1330 int i;
1331
1332 bUserAbort = &(psettings->user_abort);
Bram Moolenaara80faa82020-04-12 19:37:17 +02001333 CLEAR_FIELD(prt_dlg);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001334 prt_dlg.lStructSize = sizeof(PRINTDLGW);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001335# if !defined(FEAT_GUI) || defined(VIMDLL)
1336# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001337 if (!gui.in_use)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001338# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01001339 GetConsoleHwnd(); // get value of s_hwnd
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001340# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 prt_dlg.hwndOwner = s_hwnd;
1342 prt_dlg.Flags = PD_NOPAGENUMS | PD_NOSELECTION | PD_RETURNDC;
1343 if (!forceit)
1344 {
1345 prt_dlg.hDevMode = stored_dm;
1346 prt_dlg.hDevNames = stored_devn;
1347 prt_dlg.lCustData = stored_nCopies; // work around bug in print dialog
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001348# if !defined(FEAT_GUI) || defined(VIMDLL)
1349# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001350 if (!gui.in_use)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001351# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001352 {
1353 /*
1354 * Use hook to prevent console window being sent to back
1355 */
1356 prt_dlg.lpfnPrintHook = PrintHookProc;
1357 prt_dlg.Flags |= PD_ENABLEPRINTHOOK;
1358 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001359# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 prt_dlg.Flags |= stored_nFlags;
1361 }
1362
1363 /*
1364 * If bang present, return default printer setup with no dialog
1365 * never show dialog if we are running over telnet
1366 */
1367 if (forceit
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001368# if !defined(FEAT_GUI) || defined(VIMDLL)
1369# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001370 || (!gui.in_use && !term_console)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001371# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 || !term_console
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001373# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001374# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 )
1376 {
1377 prt_dlg.Flags |= PD_RETURNDEFAULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 /*
1379 * MSDN suggests setting the first parameter to WINSPOOL for
1380 * NT, but NULL appears to work just as well.
1381 */
1382 if (*p_pdev != NUL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001383 prt_dlg.hDC = CreateDC(NULL, (LPCSTR)p_pdev, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 {
1386 prt_dlg.Flags |= PD_RETURNDEFAULT;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001387 if (PrintDlgW(&prt_dlg) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 goto init_fail_dlg;
1389 }
1390 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001391 else if (PrintDlgW(&prt_dlg) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 goto init_fail_dlg;
1393 else
1394 {
1395 /*
1396 * keep the previous driver context
1397 */
1398 stored_dm = prt_dlg.hDevMode;
1399 stored_devn = prt_dlg.hDevNames;
1400 stored_nFlags = prt_dlg.Flags;
1401 stored_nCopies = prt_dlg.nCopies;
1402 }
1403
1404 if (prt_dlg.hDC == NULL)
1405 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001406 emsg(_("E237: Printer selection failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 mch_print_cleanup();
1408 return FALSE;
1409 }
1410
Bram Moolenaar0f873732019-12-05 20:28:46 +01001411 // Not all printer drivers report the support of color (or grey) in the
1412 // same way. Let's set has_color if there appears to be some way to print
1413 // more than B&W.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 i = GetDeviceCaps(prt_dlg.hDC, NUMCOLORS);
1415 psettings->has_color = (GetDeviceCaps(prt_dlg.hDC, BITSPIXEL) > 1
1416 || GetDeviceCaps(prt_dlg.hDC, PLANES) > 1
1417 || i > 2 || i == -1);
1418
Bram Moolenaar0f873732019-12-05 20:28:46 +01001419 // Ensure all font styles are baseline aligned
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 SetTextAlign(prt_dlg.hDC, TA_BASELINE|TA_LEFT);
1421
1422 /*
1423 * On some windows systems the nCopies parameter is not
1424 * passed back correctly. It must be retrieved from the
1425 * hDevMode struct.
1426 */
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001427 mem = (DEVMODEW *)GlobalLock(prt_dlg.hDevMode);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 if (mem != NULL)
1429 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 if (mem->dmCopies != 1)
1431 stored_nCopies = mem->dmCopies;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 if ((mem->dmFields & DM_DUPLEX) && (mem->dmDuplex & ~DMDUP_SIMPLEX))
1433 psettings->duplex = TRUE;
1434 if ((mem->dmFields & DM_COLOR) && (mem->dmColor & DMCOLOR_COLOR))
1435 psettings->has_color = TRUE;
1436 }
1437 GlobalUnlock(prt_dlg.hDevMode);
1438
1439 devname = (DEVNAMES *)GlobalLock(prt_dlg.hDevNames);
1440 if (devname != 0)
1441 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001442 WCHAR *wprinter_name = (WCHAR *)devname + devname->wDeviceOffset;
1443 WCHAR *wport_name = (WCHAR *)devname + devname->wOutputOffset;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001444 char_u *text = (char_u *)_("to %s on %s");
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001445 char_u *printer_name = utf16_to_enc(wprinter_name, NULL);
1446 char_u *port_name = utf16_to_enc(wport_name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001448 if (printer_name != NULL && port_name != NULL)
Bram Moolenaar964b3742019-05-24 18:54:09 +02001449 prt_name = alloc(STRLEN(printer_name)
1450 + STRLEN(port_name) + STRLEN(text));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 if (prt_name != NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001452 wsprintf((char *)prt_name, (const char *)text,
1453 printer_name, port_name);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001454 vim_free(printer_name);
1455 vim_free(port_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 }
1457 GlobalUnlock(prt_dlg.hDevNames);
1458
1459 /*
1460 * Initialise the font according to 'printfont'
1461 */
Bram Moolenaara80faa82020-04-12 19:37:17 +02001462 CLEAR_FIELD(fLogFont);
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001463 if (get_logfont(&fLogFont, p_pfn, prt_dlg.hDC, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001465 semsg(_("E613: Unknown printer font: %s"), p_pfn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 mch_print_cleanup();
1467 return FALSE;
1468 }
1469
1470 for (pifBold = 0; pifBold <= 1; pifBold++)
1471 for (pifItalic = 0; pifItalic <= 1; pifItalic++)
1472 for (pifUnderline = 0; pifUnderline <= 1; pifUnderline++)
1473 {
1474 fLogFont.lfWeight = boldface[pifBold];
1475 fLogFont.lfItalic = pifItalic;
1476 fLogFont.lfUnderline = pifUnderline;
1477 prt_font_handles[pifBold][pifItalic][pifUnderline]
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001478 = CreateFontIndirectW(&fLogFont);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 }
1480
1481 SetBkMode(prt_dlg.hDC, OPAQUE);
1482 SelectObject(prt_dlg.hDC, prt_font_handles[0][0][0]);
1483
1484 /*
1485 * Fill in the settings struct
1486 */
1487 psettings->chars_per_line = prt_get_cpl();
1488 psettings->lines_per_page = prt_get_lpp();
Bram Moolenaar7ddc6422014-09-27 11:18:19 +02001489 if (prt_dlg.Flags & PD_USEDEVMODECOPIESANDCOLLATE)
1490 {
1491 psettings->n_collated_copies = (prt_dlg.Flags & PD_COLLATE)
1492 ? prt_dlg.nCopies : 1;
1493 psettings->n_uncollated_copies = (prt_dlg.Flags & PD_COLLATE)
1494 ? 1 : prt_dlg.nCopies;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495
Bram Moolenaar7ddc6422014-09-27 11:18:19 +02001496 if (psettings->n_collated_copies == 0)
1497 psettings->n_collated_copies = 1;
1498
1499 if (psettings->n_uncollated_copies == 0)
1500 psettings->n_uncollated_copies = 1;
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01001501 }
1502 else
1503 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504 psettings->n_collated_copies = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 psettings->n_uncollated_copies = 1;
Bram Moolenaar7ddc6422014-09-27 11:18:19 +02001506 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507
1508 psettings->jobname = jobname;
1509
1510 return TRUE;
1511
1512init_fail_dlg:
1513 {
1514 DWORD err = CommDlgExtendedError();
1515
1516 if (err)
1517 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 char_u *buf;
1519
Bram Moolenaar0f873732019-12-05 20:28:46 +01001520 // I suspect FormatMessage() doesn't work for values returned by
1521 // CommDlgExtendedError(). What does?
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
1523 FORMAT_MESSAGE_FROM_SYSTEM |
1524 FORMAT_MESSAGE_IGNORE_INSERTS,
1525 NULL, err, 0, (LPTSTR)(&buf), 0, NULL);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001526 semsg(_("E238: Print error: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 buf == NULL ? (char_u *)_("Unknown") : buf);
1528 LocalFree((LPVOID)(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 }
1530 else
Bram Moolenaar0f873732019-12-05 20:28:46 +01001531 msg_clr_eos(); // Maybe canceled
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532
1533 mch_print_cleanup();
1534 return FALSE;
1535 }
1536}
1537
1538
1539 int
1540mch_print_begin(prt_settings_T *psettings)
1541{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001542 int ret = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 char szBuffer[300];
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001544 WCHAR *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001546 hDlgPrint = CreateDialog(g_hinst, TEXT("PrintDlgBox"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 prt_dlg.hwndOwner, PrintDlgProc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 SetAbortProc(prt_dlg.hDC, AbortProc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 wsprintf(szBuffer, _("Printing '%s'"), gettail(psettings->jobname));
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001550 vimSetDlgItemText(hDlgPrint, IDC_PRINTTEXT1, (char_u *)szBuffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001552 wp = enc_to_utf16(psettings->jobname, NULL);
Bram Moolenaar2290b1f2018-05-13 17:30:45 +02001553 if (wp != NULL)
1554 {
1555 DOCINFOW di;
1556
Bram Moolenaara80faa82020-04-12 19:37:17 +02001557 CLEAR_FIELD(di);
Bram Moolenaar2290b1f2018-05-13 17:30:45 +02001558 di.cbSize = sizeof(di);
1559 di.lpszDocName = wp;
1560 ret = StartDocW(prt_dlg.hDC, &di);
1561 vim_free(wp);
1562 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001564# ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01001565 // Give focus back to main window (when using MDI).
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001566# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001567 if (gui.in_use)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001568# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001569 SetFocus(s_hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001570# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571
1572 return (ret > 0);
1573}
1574
1575 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01001576mch_print_end(prt_settings_T *psettings UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577{
1578 EndDoc(prt_dlg.hDC);
1579 if (!*bUserAbort)
1580 SendMessage(hDlgPrint, WM_COMMAND, 0, 0);
1581}
1582
1583 int
1584mch_print_end_page(void)
1585{
1586 return (EndPage(prt_dlg.hDC) > 0);
1587}
1588
1589 int
1590mch_print_begin_page(char_u *msg)
1591{
1592 if (msg != NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001593 vimSetDlgItemText(hDlgPrint, IDC_PROGRESS, msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 return (StartPage(prt_dlg.hDC) > 0);
1595}
1596
1597 int
1598mch_print_blank_page(void)
1599{
1600 return (mch_print_begin_page(NULL) ? (mch_print_end_page()) : FALSE);
1601}
1602
1603static int prt_pos_x = 0;
1604static int prt_pos_y = 0;
1605
1606 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001607mch_print_start_line(int margin, int page_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608{
1609 if (margin)
1610 prt_pos_x = -prt_number_width;
1611 else
1612 prt_pos_x = 0;
1613 prt_pos_y = page_line * prt_line_height
1614 + prt_tm.tmAscent + prt_tm.tmExternalLeading;
1615}
1616
1617 int
1618mch_print_text_out(char_u *p, int len)
1619{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 SIZE sz;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001621 WCHAR *wp;
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001622 int wlen = len;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001623 int ret = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001625 wp = enc_to_utf16(p, &wlen);
1626 if (wp == NULL)
1627 return FALSE;
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001628
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001629 TextOutW(prt_dlg.hDC, prt_pos_x + prt_left_margin,
1630 prt_pos_y + prt_top_margin, wp, wlen);
1631 GetTextExtentPoint32W(prt_dlg.hDC, wp, wlen, &sz);
1632 vim_free(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 prt_pos_x += (sz.cx - prt_tm.tmOverhang);
Bram Moolenaar0f873732019-12-05 20:28:46 +01001634 // This is wrong when printing spaces for a TAB.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001635 if (p[len] != NUL)
1636 {
Bram Moolenaar1614a142019-10-06 22:00:13 +02001637 wlen = mb_ptr2len(p + len);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001638 wp = enc_to_utf16(p + len, &wlen);
1639 if (wp != NULL)
1640 {
1641 GetTextExtentPoint32W(prt_dlg.hDC, wp, 1, &sz);
1642 ret = (prt_pos_x + prt_left_margin + sz.cx > prt_right_margin);
1643 vim_free(wp);
1644 }
1645 }
1646 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647}
1648
1649 void
1650mch_print_set_font(int iBold, int iItalic, int iUnderline)
1651{
1652 SelectObject(prt_dlg.hDC, prt_font_handles[iBold][iItalic][iUnderline]);
1653}
1654
1655 void
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001656mch_print_set_bg(long_u bgcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657{
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001658 SetBkColor(prt_dlg.hDC, GetNearestColor(prt_dlg.hDC,
1659 swap_me((COLORREF)bgcol)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 /*
1661 * With a white background we can draw characters transparent, which is
1662 * good for italic characters that overlap to the next char cell.
1663 */
1664 if (bgcol == 0xffffffUL)
1665 SetBkMode(prt_dlg.hDC, TRANSPARENT);
1666 else
1667 SetBkMode(prt_dlg.hDC, OPAQUE);
1668}
1669
1670 void
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001671mch_print_set_fg(long_u fgcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672{
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001673 SetTextColor(prt_dlg.hDC, GetNearestColor(prt_dlg.hDC,
1674 swap_me((COLORREF)fgcol)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675}
1676
Bram Moolenaar0f873732019-12-05 20:28:46 +01001677#endif // FEAT_PRINTER && !FEAT_POSTSCRIPT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678
Bram Moolenaar58d98232005-07-23 22:25:46 +00001679
1680
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681#if defined(FEAT_SHORTCUT) || defined(PROTO)
Bram Moolenaar82881492012-11-20 16:53:39 +01001682# ifndef PROTO
1683# include <shlobj.h>
1684# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001686typedef BOOL (WINAPI *pfnGetFinalPathNameByHandleW)(
Bram Moolenaardce1e892019-02-10 23:18:53 +01001687 HANDLE hFile,
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001688 LPWSTR lpszFilePath,
1689 DWORD cchFilePath,
1690 DWORD dwFlags);
1691static pfnGetFinalPathNameByHandleW pGetFinalPathNameByHandleW = NULL;
Bram Moolenaardce1e892019-02-10 23:18:53 +01001692
Bram Moolenaar4a792c82019-06-06 12:22:41 +02001693# define is_path_sep(c) ((c) == L'\\' || (c) == L'/')
1694
1695 static int
1696is_reparse_point_included(LPCWSTR fname)
1697{
1698 LPCWSTR p = fname, q;
1699 WCHAR buf[MAX_PATH];
1700 DWORD attr;
1701
1702 if (isalpha(p[0]) && p[1] == L':' && is_path_sep(p[2]))
1703 p += 3;
1704 else if (is_path_sep(p[0]) && is_path_sep(p[1]))
1705 p += 2;
1706
1707 while (*p != L'\0')
1708 {
1709 q = wcspbrk(p, L"\\/");
1710 if (q == NULL)
1711 p = q = fname + wcslen(fname);
1712 else
1713 p = q + 1;
1714 if (q - fname >= MAX_PATH)
1715 return FALSE;
1716 wcsncpy(buf, fname, q - fname);
1717 buf[q - fname] = L'\0';
1718 attr = GetFileAttributesW(buf);
1719 if (attr != INVALID_FILE_ATTRIBUTES
1720 && (attr & FILE_ATTRIBUTE_REPARSE_POINT) != 0)
1721 return TRUE;
1722 }
1723 return FALSE;
1724}
1725
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02001726 static char_u *
Bram Moolenaardce1e892019-02-10 23:18:53 +01001727resolve_reparse_point(char_u *fname)
1728{
1729 HANDLE h = INVALID_HANDLE_VALUE;
1730 DWORD size;
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001731 WCHAR *p, *wp;
Bram Moolenaardce1e892019-02-10 23:18:53 +01001732 char_u *rfname = NULL;
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001733 WCHAR *buff = NULL;
Bram Moolenaardce1e892019-02-10 23:18:53 +01001734 static BOOL loaded = FALSE;
1735
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001736 if (pGetFinalPathNameByHandleW == NULL)
Bram Moolenaardce1e892019-02-10 23:18:53 +01001737 {
1738 HMODULE hmod = GetModuleHandle("kernel32.dll");
1739
1740 if (loaded == TRUE)
1741 return NULL;
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001742 pGetFinalPathNameByHandleW = (pfnGetFinalPathNameByHandleW)
1743 GetProcAddress(hmod, "GetFinalPathNameByHandleW");
Bram Moolenaardce1e892019-02-10 23:18:53 +01001744 loaded = TRUE;
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001745 if (pGetFinalPathNameByHandleW == NULL)
Bram Moolenaardce1e892019-02-10 23:18:53 +01001746 return NULL;
1747 }
1748
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001749 p = enc_to_utf16(fname, NULL);
1750 if (p == NULL)
1751 goto fail;
1752
Bram Moolenaar4a792c82019-06-06 12:22:41 +02001753 if (!is_reparse_point_included(p))
1754 {
1755 vim_free(p);
1756 goto fail;
1757 }
1758
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001759 h = CreateFileW(p, 0, 0, NULL, OPEN_EXISTING,
1760 FILE_FLAG_BACKUP_SEMANTICS, NULL);
1761 vim_free(p);
Bram Moolenaardce1e892019-02-10 23:18:53 +01001762
1763 if (h == INVALID_HANDLE_VALUE)
1764 goto fail;
1765
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001766 size = pGetFinalPathNameByHandleW(h, NULL, 0, 0);
1767 if (size == 0)
1768 goto fail;
1769 buff = ALLOC_MULT(WCHAR, size);
1770 if (buff == NULL)
1771 goto fail;
1772 if (pGetFinalPathNameByHandleW(h, buff, size, 0) == 0)
Bram Moolenaardce1e892019-02-10 23:18:53 +01001773 goto fail;
1774
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001775 if (wcsncmp(buff, L"\\\\?\\UNC\\", 8) == 0)
1776 {
1777 buff[6] = L'\\';
1778 wp = buff + 6;
1779 }
1780 else if (wcsncmp(buff, L"\\\\?\\", 4) == 0)
1781 wp = buff + 4;
Bram Moolenaardce1e892019-02-10 23:18:53 +01001782 else
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001783 wp = buff;
1784
1785 rfname = utf16_to_enc(wp, NULL);
Bram Moolenaardce1e892019-02-10 23:18:53 +01001786
1787fail:
1788 if (h != INVALID_HANDLE_VALUE)
1789 CloseHandle(h);
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001790 if (buff != NULL)
1791 vim_free(buff);
Bram Moolenaardce1e892019-02-10 23:18:53 +01001792
1793 return rfname;
1794}
1795
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796/*
1797 * When "fname" is the name of a shortcut (*.lnk) resolve the file it points
1798 * to and return that name in allocated memory.
1799 * Otherwise NULL is returned.
1800 */
Bram Moolenaardce1e892019-02-10 23:18:53 +01001801 static char_u *
1802resolve_shortcut(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803{
1804 HRESULT hr;
1805 IShellLink *psl = NULL;
1806 IPersistFile *ppf = NULL;
1807 OLECHAR wsz[MAX_PATH];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 char_u *rfname = NULL;
1809 int len;
Bram Moolenaar604729e2013-08-30 16:44:19 +02001810 IShellLinkW *pslw = NULL;
1811 WIN32_FIND_DATAW ffdw; // we get those free of charge
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812
Bram Moolenaar0f873732019-12-05 20:28:46 +01001813 // Check if the file name ends in ".lnk". Avoid calling
1814 // CoCreateInstance(), it's quite slow.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 if (fname == NULL)
1816 return rfname;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001817 len = (int)STRLEN(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 if (len <= 4 || STRNICMP(fname + len - 4, ".lnk", 4) != 0)
1819 return rfname;
1820
1821 CoInitialize(NULL);
1822
1823 // create a link manager object and request its interface
1824 hr = CoCreateInstance(
1825 &CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001826 &IID_IShellLinkW, (void**)&pslw);
1827 if (hr == S_OK)
1828 {
1829 WCHAR *p = enc_to_utf16(fname, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001831 if (p != NULL)
1832 {
1833 // Get a pointer to the IPersistFile interface.
1834 hr = pslw->lpVtbl->QueryInterface(
1835 pslw, &IID_IPersistFile, (void**)&ppf);
1836 if (hr != S_OK)
1837 goto shortcut_errorw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001839 // "load" the name and resolve the link
1840 hr = ppf->lpVtbl->Load(ppf, p, STGM_READ);
1841 if (hr != S_OK)
1842 goto shortcut_errorw;
1843# if 0 // This makes Vim wait a long time if the target does not exist.
1844 hr = pslw->lpVtbl->Resolve(pslw, NULL, SLR_NO_UI);
1845 if (hr != S_OK)
1846 goto shortcut_errorw;
Bram Moolenaar604729e2013-08-30 16:44:19 +02001847# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001849 // Get the path to the link target.
1850 ZeroMemory(wsz, MAX_PATH * sizeof(WCHAR));
1851 hr = pslw->lpVtbl->GetPath(pslw, wsz, MAX_PATH, &ffdw, 0);
1852 if (hr == S_OK && wsz[0] != NUL)
1853 rfname = utf16_to_enc(wsz, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001855shortcut_errorw:
1856 vim_free(p);
1857 }
1858 }
1859
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 // Release all interface pointers (both belong to the same object)
1861 if (ppf != NULL)
1862 ppf->lpVtbl->Release(ppf);
1863 if (psl != NULL)
1864 psl->lpVtbl->Release(psl);
Bram Moolenaar604729e2013-08-30 16:44:19 +02001865 if (pslw != NULL)
1866 pslw->lpVtbl->Release(pslw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867
1868 CoUninitialize();
1869 return rfname;
1870}
Bram Moolenaardce1e892019-02-10 23:18:53 +01001871
1872 char_u *
1873mch_resolve_path(char_u *fname, int reparse_point)
1874{
1875 char_u *path = resolve_shortcut(fname);
1876
1877 if (path == NULL && reparse_point)
1878 path = resolve_reparse_point(fname);
1879 return path;
1880}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881#endif
1882
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001883#if (defined(FEAT_EVAL) && (!defined(FEAT_GUI) || defined(VIMDLL))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884/*
1885 * Bring ourselves to the foreground. Does work if the OS doesn't allow it.
1886 */
1887 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001888win32_set_foreground(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889{
Bram Moolenaar0f873732019-12-05 20:28:46 +01001890 GetConsoleHwnd(); // get value of s_hwnd
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 if (s_hwnd != 0)
1892 SetForegroundWindow(s_hwnd);
1893}
1894#endif
1895
1896#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
1897/*
1898 * Client-server code for Vim
1899 *
1900 * Originally written by Paul Moore
1901 */
1902
Bram Moolenaar0f873732019-12-05 20:28:46 +01001903// In order to handle inter-process messages, we need to have a window. But
1904// the functions in this module can be called before the main GUI window is
1905// created (and may also be called in the console version, where there is no
1906// GUI window at all).
1907//
1908// So we create a hidden window, and arrange to destroy it on exit.
1909HWND message_window = 0; // window that's handling messages
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001911# define VIM_CLASSNAME "VIM_MESSAGES"
1912# define VIM_CLASSNAME_LEN (sizeof(VIM_CLASSNAME) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913
Bram Moolenaar0f873732019-12-05 20:28:46 +01001914// Communication is via WM_COPYDATA messages. The message type is send in
1915// the dwData parameter. Types are defined here.
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001916# define COPYDATA_KEYS 0
1917# define COPYDATA_REPLY 1
1918# define COPYDATA_EXPR 10
1919# define COPYDATA_RESULT 11
1920# define COPYDATA_ERROR_RESULT 12
1921# define COPYDATA_ENCODING 20
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922
Bram Moolenaar0f873732019-12-05 20:28:46 +01001923// This is a structure containing a server HWND and its name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924struct server_id
1925{
1926 HWND hwnd;
1927 char_u *name;
1928};
1929
Bram Moolenaar0f873732019-12-05 20:28:46 +01001930// Last received 'encoding' that the client uses.
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001931static char_u *client_enc = NULL;
1932
1933/*
1934 * Tell the other side what encoding we are using.
1935 * Errors are ignored.
1936 */
1937 static void
1938serverSendEnc(HWND target)
1939{
1940 COPYDATASTRUCT data;
1941
1942 data.dwData = COPYDATA_ENCODING;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001943 data.cbData = (DWORD)STRLEN(p_enc) + 1;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001944 data.lpData = p_enc;
1945 (void)SendMessage(target, WM_COPYDATA, (WPARAM)message_window,
1946 (LPARAM)(&data));
1947}
1948
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949/*
1950 * Clean up on exit. This destroys the hidden message window.
1951 */
1952 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953CleanUpMessaging(void)
1954{
1955 if (message_window != 0)
1956 {
1957 DestroyWindow(message_window);
1958 message_window = 0;
1959 }
1960}
1961
1962static int save_reply(HWND server, char_u *reply, int expr);
1963
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001964/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 * The window procedure for the hidden message window.
1966 * It handles callback messages and notifications from servers.
1967 * In order to process these messages, it is necessary to run a
1968 * message loop. Code which may run before the main message loop
1969 * is started (in the GUI) is careful to pump messages when it needs
1970 * to. Features which require message delivery during normal use will
1971 * not work in the console version - this basically means those
1972 * features which allow Vim to act as a server, rather than a client.
1973 */
1974 static LRESULT CALLBACK
1975Messaging_WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
1976{
1977 if (msg == WM_COPYDATA)
1978 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001979 // This is a message from another Vim. The dwData member of the
1980 // COPYDATASTRUCT determines the type of message:
1981 // COPYDATA_ENCODING:
1982 // The encoding that the client uses. Following messages will
1983 // use this encoding, convert if needed.
1984 // COPYDATA_KEYS:
1985 // A key sequence. We are a server, and a client wants these keys
1986 // adding to the input queue.
1987 // COPYDATA_REPLY:
1988 // A reply. We are a client, and a server has sent this message
1989 // in response to a request. (server2client())
1990 // COPYDATA_EXPR:
1991 // An expression. We are a server, and a client wants us to
1992 // evaluate this expression.
1993 // COPYDATA_RESULT:
1994 // A reply. We are a client, and a server has sent this message
1995 // in response to a COPYDATA_EXPR.
1996 // COPYDATA_ERROR_RESULT:
1997 // A reply. We are a client, and a server has sent this message
1998 // in response to a COPYDATA_EXPR that failed to evaluate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 COPYDATASTRUCT *data = (COPYDATASTRUCT*)lParam;
2000 HWND sender = (HWND)wParam;
2001 COPYDATASTRUCT reply;
2002 char_u *res;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 int retval;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002004 char_u *str;
2005 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006
2007 switch (data->dwData)
2008 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002009 case COPYDATA_ENCODING:
Bram Moolenaar0f873732019-12-05 20:28:46 +01002010 // Remember the encoding that the client uses.
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002011 vim_free(client_enc);
2012 client_enc = enc_canonize((char_u *)data->lpData);
2013 return 1;
2014
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 case COPYDATA_KEYS:
Bram Moolenaar0f873732019-12-05 20:28:46 +01002016 // Remember who sent this, for <client>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 clientWindow = sender;
2018
Bram Moolenaar0f873732019-12-05 20:28:46 +01002019 // Add the received keys to the input buffer. The loop waiting
2020 // for the user to do something should check the input buffer.
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002021 str = serverConvert(client_enc, (char_u *)data->lpData, &tofree);
2022 server_to_input_buf(str);
2023 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024
2025# ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01002026 // Wake up the main GUI loop.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002027# ifdef VIMDLL
2028 if (gui.in_use)
2029# endif
2030 if (s_hwnd != 0)
2031 PostMessage(s_hwnd, WM_NULL, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032# endif
2033 return 1;
2034
2035 case COPYDATA_EXPR:
Bram Moolenaar0f873732019-12-05 20:28:46 +01002036 // Remember who sent this, for <client>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 clientWindow = sender;
2038
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002039 str = serverConvert(client_enc, (char_u *)data->lpData, &tofree);
2040 res = eval_client_expr_to_string(str);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002041
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 if (res == NULL)
2043 {
Bram Moolenaar15bf76d2017-03-18 16:18:37 +01002044 char *err = _(e_invexprmsg);
2045 size_t len = STRLEN(str) + STRLEN(err) + 5;
2046
Bram Moolenaar964b3742019-05-24 18:54:09 +02002047 res = alloc(len);
Bram Moolenaar15bf76d2017-03-18 16:18:37 +01002048 if (res != NULL)
2049 vim_snprintf((char *)res, len, "%s: \"%s\"", err, str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 reply.dwData = COPYDATA_ERROR_RESULT;
2051 }
2052 else
2053 reply.dwData = COPYDATA_RESULT;
2054 reply.lpData = res;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002055 reply.cbData = (DWORD)STRLEN(res) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002057 serverSendEnc(sender);
Bram Moolenaar5246cd72013-06-16 16:41:47 +02002058 retval = (int)SendMessage(sender, WM_COPYDATA,
2059 (WPARAM)message_window, (LPARAM)(&reply));
Bram Moolenaar15bf76d2017-03-18 16:18:37 +01002060 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061 vim_free(res);
2062 return retval;
2063
2064 case COPYDATA_REPLY:
2065 case COPYDATA_RESULT:
2066 case COPYDATA_ERROR_RESULT:
2067 if (data->lpData != NULL)
2068 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002069 str = serverConvert(client_enc, (char_u *)data->lpData,
2070 &tofree);
2071 if (tofree == NULL)
2072 str = vim_strsave(str);
2073 if (save_reply(sender, str,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 (data->dwData == COPYDATA_REPLY ? 0 :
2075 (data->dwData == COPYDATA_RESULT ? 1 :
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002076 2))) == FAIL)
2077 vim_free(str);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002078 else if (data->dwData == COPYDATA_REPLY)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 {
Bram Moolenaar427d51c2013-06-16 16:01:25 +02002080 char_u winstr[30];
2081
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002082 sprintf((char *)winstr, PRINTF_HEX_LONG_U, (long_u)sender);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002083 apply_autocmds(EVENT_REMOTEREPLY, winstr, str,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 TRUE, curbuf);
2085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 }
2087 return 1;
2088 }
2089
2090 return 0;
2091 }
2092
2093 else if (msg == WM_ACTIVATE && wParam == WA_ACTIVE)
2094 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002095 // When the message window is activated (brought to the foreground),
2096 // this actually applies to the text window.
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002097# if !defined(FEAT_GUI) || defined(VIMDLL)
2098# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002099 if (!gui.in_use)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002100# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002101 GetConsoleHwnd(); // get value of s_hwnd
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002102# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 if (s_hwnd != 0)
2104 {
2105 SetForegroundWindow(s_hwnd);
2106 return 0;
2107 }
2108 }
2109
2110 return DefWindowProc(hwnd, msg, wParam, lParam);
2111}
2112
2113/*
2114 * Initialise the message handling process. This involves creating a window
2115 * to handle messages - the window will not be visible.
2116 */
2117 void
2118serverInitMessaging(void)
2119{
2120 WNDCLASS wndclass;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121
Bram Moolenaar0f873732019-12-05 20:28:46 +01002122 // Clean up on exit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 atexit(CleanUpMessaging);
2124
Bram Moolenaar0f873732019-12-05 20:28:46 +01002125 // Register a window class - we only really care
2126 // about the window procedure
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127 wndclass.style = 0;
2128 wndclass.lpfnWndProc = Messaging_WndProc;
2129 wndclass.cbClsExtra = 0;
2130 wndclass.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002131 wndclass.hInstance = g_hinst;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 wndclass.hIcon = NULL;
2133 wndclass.hCursor = NULL;
2134 wndclass.hbrBackground = NULL;
2135 wndclass.lpszMenuName = NULL;
2136 wndclass.lpszClassName = VIM_CLASSNAME;
2137 RegisterClass(&wndclass);
2138
Bram Moolenaar0f873732019-12-05 20:28:46 +01002139 // Create the message window. It will be hidden, so the details don't
2140 // matter. Don't use WS_OVERLAPPEDWINDOW, it will make a shortcut remove
2141 // focus from gvim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 message_window = CreateWindow(VIM_CLASSNAME, "",
2143 WS_POPUPWINDOW | WS_CAPTION,
2144 CW_USEDEFAULT, CW_USEDEFAULT,
2145 100, 100, NULL, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002146 g_hinst, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147}
2148
Bram Moolenaar0f873732019-12-05 20:28:46 +01002149// Used by serverSendToVim() to find an alternate server name.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002150static char_u *altname_buf_ptr = NULL;
2151
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152/*
2153 * Get the title of the window "hwnd", which is the Vim server name, in
2154 * "name[namelen]" and return the length.
2155 * Returns zero if window "hwnd" is not a Vim server.
2156 */
2157 static int
2158getVimServerName(HWND hwnd, char *name, int namelen)
2159{
2160 int len;
2161 char buffer[VIM_CLASSNAME_LEN + 1];
2162
Bram Moolenaar0f873732019-12-05 20:28:46 +01002163 // Ignore windows which aren't Vim message windows
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 len = GetClassName(hwnd, buffer, sizeof(buffer));
2165 if (len != VIM_CLASSNAME_LEN || STRCMP(buffer, VIM_CLASSNAME) != 0)
2166 return 0;
2167
Bram Moolenaar0f873732019-12-05 20:28:46 +01002168 // Get the title of the window
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 return GetWindowText(hwnd, name, namelen);
2170}
2171
2172 static BOOL CALLBACK
2173enumWindowsGetServer(HWND hwnd, LPARAM lparam)
2174{
2175 struct server_id *id = (struct server_id *)lparam;
2176 char server[MAX_PATH];
2177
Bram Moolenaar0f873732019-12-05 20:28:46 +01002178 // Get the title of the window
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 if (getVimServerName(hwnd, server, sizeof(server)) == 0)
2180 return TRUE;
2181
Bram Moolenaar0f873732019-12-05 20:28:46 +01002182 // If this is the server we're looking for, return its HWND
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 if (STRICMP(server, id->name) == 0)
2184 {
2185 id->hwnd = hwnd;
2186 return FALSE;
2187 }
2188
Bram Moolenaar0f873732019-12-05 20:28:46 +01002189 // If we are looking for an alternate server, remember this name.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002190 if (altname_buf_ptr != NULL
2191 && STRNICMP(server, id->name, STRLEN(id->name)) == 0
2192 && vim_isdigit(server[STRLEN(id->name)]))
2193 {
2194 STRCPY(altname_buf_ptr, server);
Bram Moolenaar0f873732019-12-05 20:28:46 +01002195 altname_buf_ptr = NULL; // don't use another name
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002196 }
2197
Bram Moolenaar0f873732019-12-05 20:28:46 +01002198 // Otherwise, keep looking
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 return TRUE;
2200}
2201
2202 static BOOL CALLBACK
2203enumWindowsGetNames(HWND hwnd, LPARAM lparam)
2204{
2205 garray_T *ga = (garray_T *)lparam;
2206 char server[MAX_PATH];
2207
Bram Moolenaar0f873732019-12-05 20:28:46 +01002208 // Get the title of the window
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 if (getVimServerName(hwnd, server, sizeof(server)) == 0)
2210 return TRUE;
2211
Bram Moolenaar0f873732019-12-05 20:28:46 +01002212 // Add the name to the list
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002213 ga_concat(ga, (char_u *)server);
2214 ga_concat(ga, (char_u *)"\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 return TRUE;
2216}
2217
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002218struct enum_windows_s
2219{
2220 WNDENUMPROC lpEnumFunc;
2221 LPARAM lParam;
2222};
2223
2224 static BOOL CALLBACK
2225enum_windows_child(HWND hwnd, LPARAM lParam)
2226{
2227 struct enum_windows_s *ew = (struct enum_windows_s *)lParam;
2228
2229 return (ew->lpEnumFunc)(hwnd, ew->lParam);
2230}
2231
2232 static BOOL CALLBACK
2233enum_windows_toplevel(HWND hwnd, LPARAM lParam)
2234{
2235 struct enum_windows_s *ew = (struct enum_windows_s *)lParam;
2236
Bram Moolenaar95ba5c32018-10-07 22:47:07 +02002237 if ((ew->lpEnumFunc)(hwnd, ew->lParam))
2238 return TRUE;
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002239 return EnumChildWindows(hwnd, enum_windows_child, lParam);
2240}
2241
Bram Moolenaar0f873732019-12-05 20:28:46 +01002242/*
2243 * Enumerate all windows including children.
2244 */
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002245 static BOOL
2246enum_windows(WNDENUMPROC lpEnumFunc, LPARAM lParam)
2247{
2248 struct enum_windows_s ew;
2249
2250 ew.lpEnumFunc = lpEnumFunc;
2251 ew.lParam = lParam;
2252 return EnumWindows(enum_windows_toplevel, (LPARAM)&ew);
2253}
2254
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 static HWND
2256findServer(char_u *name)
2257{
2258 struct server_id id;
2259
2260 id.name = name;
2261 id.hwnd = 0;
2262
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002263 enum_windows(enumWindowsGetServer, (LPARAM)(&id));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264
2265 return id.hwnd;
2266}
2267
2268 void
2269serverSetName(char_u *name)
2270{
2271 char_u *ok_name;
2272 HWND hwnd = 0;
2273 int i = 0;
2274 char_u *p;
2275
Bram Moolenaar0f873732019-12-05 20:28:46 +01002276 // Leave enough space for a 9-digit suffix to ensure uniqueness!
Bram Moolenaar964b3742019-05-24 18:54:09 +02002277 ok_name = alloc(STRLEN(name) + 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278
2279 STRCPY(ok_name, name);
2280 p = ok_name + STRLEN(name);
2281
2282 for (;;)
2283 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002284 // This is inefficient - we're doing an EnumWindows loop for each
2285 // possible name. It would be better to grab all names in one go,
2286 // and scan the list each time...
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 hwnd = findServer(ok_name);
2288 if (hwnd == 0)
2289 break;
2290
2291 ++i;
2292 if (i >= 1000)
2293 break;
2294
2295 sprintf((char *)p, "%d", i);
2296 }
2297
2298 if (hwnd != 0)
2299 vim_free(ok_name);
2300 else
2301 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002302 // Remember the name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 serverName = ok_name;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002304# ifdef FEAT_TITLE
Bram Moolenaar0f873732019-12-05 20:28:46 +01002305 need_maketitle = TRUE; // update Vim window title later
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002306# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307
Bram Moolenaar0f873732019-12-05 20:28:46 +01002308 // Update the message window title
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002309 SetWindowText(message_window, (LPCSTR)ok_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002311# ifdef FEAT_EVAL
Bram Moolenaar0f873732019-12-05 20:28:46 +01002312 // Set the servername variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 set_vim_var_string(VV_SEND_SERVER, serverName, -1);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002314# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 }
2316}
2317
2318 char_u *
2319serverGetVimNames(void)
2320{
2321 garray_T ga;
2322
2323 ga_init2(&ga, 1, 100);
2324
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002325 enum_windows(enumWindowsGetNames, (LPARAM)(&ga));
Bram Moolenaar269ec652004-07-29 08:43:53 +00002326 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327
2328 return ga.ga_data;
2329}
2330
2331 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002332serverSendReply(
Bram Moolenaar0f873732019-12-05 20:28:46 +01002333 char_u *name, // Where to send.
2334 char_u *reply) // What to send.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335{
2336 HWND target;
2337 COPYDATASTRUCT data;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002338 long_u n = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339
Bram Moolenaar0f873732019-12-05 20:28:46 +01002340 // The "name" argument is a magic cookie obtained from expand("<client>").
2341 // It should be of the form 0xXXXXX - i.e. a C hex literal, which is the
2342 // value of the client's message window HWND.
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002343 sscanf((char *)name, SCANF_HEX_LONG_U, &n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 if (n == 0)
2345 return -1;
2346
2347 target = (HWND)n;
2348 if (!IsWindow(target))
2349 return -1;
2350
2351 data.dwData = COPYDATA_REPLY;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002352 data.cbData = (DWORD)STRLEN(reply) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 data.lpData = reply;
2354
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002355 serverSendEnc(target);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 if (SendMessage(target, WM_COPYDATA, (WPARAM)message_window,
2357 (LPARAM)(&data)))
2358 return 0;
2359
2360 return -1;
2361}
2362
2363 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002364serverSendToVim(
Bram Moolenaar0f873732019-12-05 20:28:46 +01002365 char_u *name, // Where to send.
2366 char_u *cmd, // What to send.
2367 char_u **result, // Result of eval'ed expression
2368 void *ptarget, // HWND of server
2369 int asExpr, // Expression or keys?
2370 int timeout, // timeout in seconds or zero
2371 int silent) // don't complain about no server
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372{
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002373 HWND target;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 COPYDATASTRUCT data;
2375 char_u *retval = NULL;
2376 int retcode = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002377 char_u altname_buf[MAX_PATH];
2378
Bram Moolenaar0f873732019-12-05 20:28:46 +01002379 // Execute locally if no display or target is ourselves
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01002380 if (serverName != NULL && STRICMP(name, serverName) == 0)
2381 return sendToLocalVim(cmd, asExpr, result);
2382
Bram Moolenaar0f873732019-12-05 20:28:46 +01002383 // If the server name does not end in a digit then we look for an
2384 // alternate name. e.g. when "name" is GVIM the we may find GVIM2.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002385 if (STRLEN(name) > 1 && !vim_isdigit(name[STRLEN(name) - 1]))
2386 altname_buf_ptr = altname_buf;
2387 altname_buf[0] = NUL;
2388 target = findServer(name);
2389 altname_buf_ptr = NULL;
2390 if (target == 0 && altname_buf[0] != NUL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01002391 // Use another server name we found.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002392 target = findServer(altname_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393
2394 if (target == 0)
2395 {
2396 if (!silent)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002397 semsg(_(e_noserver), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 return -1;
2399 }
2400
2401 if (ptarget)
2402 *(HWND *)ptarget = target;
2403
2404 data.dwData = asExpr ? COPYDATA_EXPR : COPYDATA_KEYS;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002405 data.cbData = (DWORD)STRLEN(cmd) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 data.lpData = cmd;
2407
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002408 serverSendEnc(target);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 if (SendMessage(target, WM_COPYDATA, (WPARAM)message_window,
2410 (LPARAM)(&data)) == 0)
2411 return -1;
2412
2413 if (asExpr)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002414 retval = serverGetReply(target, &retcode, TRUE, TRUE, timeout);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415
2416 if (result == NULL)
2417 vim_free(retval);
2418 else
Bram Moolenaar0f873732019-12-05 20:28:46 +01002419 *result = retval; // Caller assumes responsibility for freeing
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420
2421 return retcode;
2422}
2423
2424/*
2425 * Bring the server to the foreground.
2426 */
2427 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002428serverForeground(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429{
2430 HWND target = findServer(name);
2431
2432 if (target != 0)
2433 SetForegroundWindow(target);
2434}
2435
Bram Moolenaar0f873732019-12-05 20:28:46 +01002436// Replies from server need to be stored until the client picks them up via
2437// remote_read(). So we maintain a list of server-id/reply pairs.
2438// Note that there could be multiple replies from one server pending if the
2439// client is slow picking them up.
2440// We just store the replies in a simple list. When we remove an entry, we
2441// move list entries down to fill the gap.
2442// The server ID is simply the HWND.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443typedef struct
2444{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002445 HWND server; // server window
2446 char_u *reply; // reply string
2447 int expr_result; // 0 for REPLY, 1 for RESULT 2 for error
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002448} reply_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449
2450static garray_T reply_list = {0, 0, sizeof(reply_T), 5, 0};
2451
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002452# define REPLY_ITEM(i) ((reply_T *)(reply_list.ga_data) + (i))
2453# define REPLY_COUNT (reply_list.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454
Bram Moolenaar0f873732019-12-05 20:28:46 +01002455// Flag which is used to wait for a reply
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456static int reply_received = 0;
2457
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002458/*
2459 * Store a reply. "reply" must be allocated memory (or NULL).
2460 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 static int
2462save_reply(HWND server, char_u *reply, int expr)
2463{
2464 reply_T *rep;
2465
2466 if (ga_grow(&reply_list, 1) == FAIL)
2467 return FAIL;
2468
2469 rep = REPLY_ITEM(REPLY_COUNT);
2470 rep->server = server;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002471 rep->reply = reply;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 rep->expr_result = expr;
2473 if (rep->reply == NULL)
2474 return FAIL;
2475
2476 ++REPLY_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 reply_received = 1;
2478 return OK;
2479}
2480
2481/*
2482 * Get a reply from server "server".
2483 * When "expr_res" is non NULL, get the result of an expression, otherwise a
2484 * server2client() message.
2485 * When non NULL, point to return code. 0 => OK, -1 => ERROR
2486 * If "remove" is TRUE, consume the message, the caller must free it then.
2487 * if "wait" is TRUE block until a message arrives (or the server exits).
2488 */
2489 char_u *
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002490serverGetReply(HWND server, int *expr_res, int remove, int wait, int timeout)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491{
2492 int i;
2493 char_u *reply;
2494 reply_T *rep;
Bram Moolenaar15e737f2017-03-18 21:22:47 +01002495 int did_process = FALSE;
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002496 time_t start;
2497 time_t now;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498
Bram Moolenaar0f873732019-12-05 20:28:46 +01002499 // When waiting, loop until the message waiting for is received.
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002500 time(&start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 for (;;)
2502 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002503 // Reset this here, in case a message arrives while we are going
2504 // through the already received messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 reply_received = 0;
2506
2507 for (i = 0; i < REPLY_COUNT; ++i)
2508 {
2509 rep = REPLY_ITEM(i);
2510 if (rep->server == server
2511 && ((rep->expr_result != 0) == (expr_res != NULL)))
2512 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002513 // Save the values we've found for later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 reply = rep->reply;
2515 if (expr_res != NULL)
2516 *expr_res = rep->expr_result == 1 ? 0 : -1;
2517
2518 if (remove)
2519 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002520 // Move the rest of the list down to fill the gap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 mch_memmove(rep, rep + 1,
2522 (REPLY_COUNT - i - 1) * sizeof(reply_T));
2523 --REPLY_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 }
2525
Bram Moolenaar0f873732019-12-05 20:28:46 +01002526 // Return the reply to the caller, who takes on responsibility
2527 // for freeing it if "remove" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 return reply;
2529 }
2530 }
2531
Bram Moolenaar0f873732019-12-05 20:28:46 +01002532 // If we got here, we didn't find a reply. Return immediately if the
2533 // "wait" parameter isn't set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 if (!wait)
Bram Moolenaar15e737f2017-03-18 21:22:47 +01002535 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002536 // Process pending messages once. Without this, looping on
2537 // remote_peek() would never get the reply.
Bram Moolenaar15e737f2017-03-18 21:22:47 +01002538 if (!did_process)
2539 {
2540 did_process = TRUE;
2541 serverProcessPendingMessages();
2542 continue;
2543 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 break;
Bram Moolenaar15e737f2017-03-18 21:22:47 +01002545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546
Bram Moolenaar0f873732019-12-05 20:28:46 +01002547 // We need to wait for a reply. Enter a message loop until the
2548 // "reply_received" flag gets set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549
Bram Moolenaar0f873732019-12-05 20:28:46 +01002550 // Loop until we receive a reply
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 while (reply_received == 0)
2552 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002553# ifdef FEAT_TIMERS
Bram Moolenaar0f873732019-12-05 20:28:46 +01002554 // TODO: use the return value to decide how long to wait.
Bram Moolenaar42205552017-03-18 19:42:22 +01002555 check_due_timer();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002556# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002557 time(&now);
2558 if (timeout > 0 && (now - start) >= timeout)
2559 break;
2560
Bram Moolenaar0f873732019-12-05 20:28:46 +01002561 // Wait for a SendMessage() call to us. This could be the reply
2562 // we are waiting for. Use a timeout of a second, to catch the
2563 // situation that the server died unexpectedly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 MsgWaitForMultipleObjects(0, NULL, TRUE, 1000, QS_ALLINPUT);
2565
Bram Moolenaar0f873732019-12-05 20:28:46 +01002566 // If the server has died, give up
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 if (!IsWindow(server))
2568 return NULL;
2569
2570 serverProcessPendingMessages();
2571 }
2572 }
2573
2574 return NULL;
2575}
2576
2577/*
2578 * Process any messages in the Windows message queue.
2579 */
2580 void
2581serverProcessPendingMessages(void)
2582{
2583 MSG msg;
2584
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02002585 while (pPeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 {
2587 TranslateMessage(&msg);
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02002588 pDispatchMessage(&msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 }
2590}
2591
Bram Moolenaar0f873732019-12-05 20:28:46 +01002592#endif // FEAT_CLIENTSERVER
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593
2594#if defined(FEAT_GUI) || (defined(FEAT_PRINTER) && !defined(FEAT_POSTSCRIPT)) \
2595 || defined(PROTO)
2596
2597struct charset_pair
2598{
2599 char *name;
2600 BYTE charset;
2601};
2602
2603static struct charset_pair
2604charset_pairs[] =
2605{
2606 {"ANSI", ANSI_CHARSET},
2607 {"CHINESEBIG5", CHINESEBIG5_CHARSET},
2608 {"DEFAULT", DEFAULT_CHARSET},
2609 {"HANGEUL", HANGEUL_CHARSET},
2610 {"OEM", OEM_CHARSET},
2611 {"SHIFTJIS", SHIFTJIS_CHARSET},
2612 {"SYMBOL", SYMBOL_CHARSET},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 {"ARABIC", ARABIC_CHARSET},
2614 {"BALTIC", BALTIC_CHARSET},
2615 {"EASTEUROPE", EASTEUROPE_CHARSET},
2616 {"GB2312", GB2312_CHARSET},
2617 {"GREEK", GREEK_CHARSET},
2618 {"HEBREW", HEBREW_CHARSET},
2619 {"JOHAB", JOHAB_CHARSET},
2620 {"MAC", MAC_CHARSET},
2621 {"RUSSIAN", RUSSIAN_CHARSET},
2622 {"THAI", THAI_CHARSET},
2623 {"TURKISH", TURKISH_CHARSET},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002624# ifdef VIETNAMESE_CHARSET
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 {"VIETNAMESE", VIETNAMESE_CHARSET},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002626# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 {NULL, 0}
2628};
2629
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002630struct quality_pair
2631{
2632 char *name;
2633 DWORD quality;
2634};
2635
2636static struct quality_pair
2637quality_pairs[] = {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002638# ifdef CLEARTYPE_QUALITY
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002639 {"CLEARTYPE", CLEARTYPE_QUALITY},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002640# endif
2641# ifdef ANTIALIASED_QUALITY
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002642 {"ANTIALIASED", ANTIALIASED_QUALITY},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002643# endif
2644# ifdef NONANTIALIASED_QUALITY
Bram Moolenaar73a733e2016-05-11 21:05:05 +02002645 {"NONANTIALIASED", NONANTIALIASED_QUALITY},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002646# endif
2647# ifdef PROOF_QUALITY
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002648 {"PROOF", PROOF_QUALITY},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002649# endif
2650# ifdef DRAFT_QUALITY
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002651 {"DRAFT", DRAFT_QUALITY},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002652# endif
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002653 {"DEFAULT", DEFAULT_QUALITY},
2654 {NULL, 0}
2655};
2656
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657/*
2658 * Convert a charset ID to a name.
2659 * Return NULL when not recognized.
2660 */
2661 char *
2662charset_id2name(int id)
2663{
2664 struct charset_pair *cp;
2665
2666 for (cp = charset_pairs; cp->name != NULL; ++cp)
2667 if ((BYTE)id == cp->charset)
2668 break;
2669 return cp->name;
2670}
2671
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002672/*
2673 * Convert a quality ID to a name.
2674 * Return NULL when not recognized.
2675 */
2676 char *
2677quality_id2name(DWORD id)
2678{
2679 struct quality_pair *qp;
2680
2681 for (qp = quality_pairs; qp->name != NULL; ++qp)
2682 if (id == qp->quality)
2683 break;
2684 return qp->name;
2685}
2686
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002687static const LOGFONTW s_lfDefault =
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688{
2689 -12, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, DEFAULT_CHARSET,
2690 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
2691 PROOF_QUALITY, FIXED_PITCH | FF_DONTCARE,
Bram Moolenaar0f873732019-12-05 20:28:46 +01002692 L"Fixedsys" // see _ReadVimIni
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693};
2694
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002695// Initialise the "current height" to -12 (same as s_lfDefault) just
2696// in case the user specifies a font in "guifont" with no size before a font
2697// with an explicit size has been set. This defaults the size to this value
2698// (-12 equates to roughly 9pt).
2699int current_font_height = -12; // also used in gui_w32.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700
Bram Moolenaar0f873732019-12-05 20:28:46 +01002701/*
2702 * Convert a string representing a point size into pixels. The string should
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 * be a positive decimal number, with an optional decimal point (eg, "12", or
2704 * "10.5"). The pixel value is returned, and a pointer to the next unconverted
2705 * character is stored in *end. The flag "vertical" says whether this
2706 * calculation is for a vertical (height) size or a horizontal (width) one.
2707 */
2708 static int
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002709points_to_pixels(WCHAR *str, WCHAR **end, int vertical, long_i pprinter_dc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710{
2711 int pixels;
2712 int points = 0;
2713 int divisor = 0;
2714 HWND hwnd = (HWND)0;
2715 HDC hdc;
2716 HDC printer_dc = (HDC)pprinter_dc;
2717
2718 while (*str != NUL)
2719 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002720 if (*str == L'.' && divisor == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002722 // Start keeping a divisor, for later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 divisor = 1;
2724 }
2725 else
2726 {
2727 if (!VIM_ISDIGIT(*str))
2728 break;
2729
2730 points *= 10;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002731 points += *str - L'0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732 divisor *= 10;
2733 }
2734 ++str;
2735 }
2736
2737 if (divisor == 0)
2738 divisor = 1;
2739
2740 if (printer_dc == NULL)
2741 {
2742 hwnd = GetDesktopWindow();
2743 hdc = GetWindowDC(hwnd);
2744 }
2745 else
2746 hdc = printer_dc;
2747
2748 pixels = MulDiv(points,
2749 GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX),
2750 72 * divisor);
2751
2752 if (printer_dc == NULL)
2753 ReleaseDC(hwnd, hdc);
2754
2755 *end = str;
2756 return pixels;
2757}
2758
2759 static int CALLBACK
2760font_enumproc(
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002761 ENUMLOGFONTW *elf,
2762 NEWTEXTMETRICW *ntm UNUSED,
2763 DWORD type UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 LPARAM lparam)
2765{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002766 // Return value:
2767 // 0 = terminate now (monospace & ANSI)
2768 // 1 = continue, still no luck...
2769 // 2 = continue, but we have an acceptable LOGFONTW
2770 // (monospace, not ANSI)
2771 // We use these values, as EnumFontFamilies returns 1 if the
2772 // callback function is never called. So, we check the return as
2773 // 0 = perfect, 2 = OK, 1 = no good...
2774 // It's not pretty, but it works!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002776 LOGFONTW *lf = (LOGFONTW *)(lparam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002778# ifndef FEAT_PROPORTIONAL_FONTS
Bram Moolenaar0f873732019-12-05 20:28:46 +01002779 // Ignore non-monospace fonts without further ado
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 if ((ntm->tmPitchAndFamily & 1) != 0)
2781 return 1;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002782# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783
Bram Moolenaar0f873732019-12-05 20:28:46 +01002784 // Remember this LOGFONTW as a "possible"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 *lf = elf->elfLogFont;
2786
Bram Moolenaar0f873732019-12-05 20:28:46 +01002787 // Terminate the scan as soon as we find an ANSI font
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 if (lf->lfCharSet == ANSI_CHARSET
2789 || lf->lfCharSet == OEM_CHARSET
2790 || lf->lfCharSet == DEFAULT_CHARSET)
2791 return 0;
2792
Bram Moolenaar0f873732019-12-05 20:28:46 +01002793 // Continue the scan - we have a non-ANSI font
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794 return 2;
2795}
2796
2797 static int
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002798init_logfont(LOGFONTW *lf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799{
2800 int n;
2801 HWND hwnd = GetDesktopWindow();
2802 HDC hdc = GetWindowDC(hwnd);
2803
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002804 n = EnumFontFamiliesW(hdc,
2805 lf->lfFaceName,
2806 (FONTENUMPROCW)font_enumproc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 (LPARAM)lf);
2808
2809 ReleaseDC(hwnd, hdc);
2810
Bram Moolenaar0f873732019-12-05 20:28:46 +01002811 // If we couldn't find a usable font, return failure
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 if (n == 1)
2813 return FAIL;
2814
Bram Moolenaar0f873732019-12-05 20:28:46 +01002815 // Tidy up the rest of the LOGFONTW structure. We set to a basic
2816 // font - get_logfont() sets bold, italic, etc based on the user's
2817 // input.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 lf->lfHeight = current_font_height;
2819 lf->lfWidth = 0;
2820 lf->lfItalic = FALSE;
2821 lf->lfUnderline = FALSE;
2822 lf->lfStrikeOut = FALSE;
2823 lf->lfWeight = FW_NORMAL;
2824
Bram Moolenaar0f873732019-12-05 20:28:46 +01002825 // Return success
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 return OK;
2827}
2828
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002829/*
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002830 * Compare a UTF-16 string and an ASCII string literally.
2831 * Only works all the code points are inside ASCII range.
2832 */
2833 static int
2834utf16ascncmp(const WCHAR *w, const char *p, size_t n)
2835{
2836 size_t i;
2837
2838 for (i = 0; i < n; i++)
2839 {
2840 if (w[i] == 0 || w[i] != p[i])
2841 return w[i] - p[i];
2842 }
2843 return 0;
2844}
2845
2846/*
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002847 * Get font info from "name" into logfont "lf".
2848 * Return OK for a valid name, FAIL otherwise.
2849 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 int
2851get_logfont(
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002852 LOGFONTW *lf,
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002853 char_u *name,
2854 HDC printer_dc,
2855 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002857 WCHAR *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 int i;
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002859 int ret = FAIL;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002860 static LOGFONTW *lastlf = NULL;
2861 WCHAR *wname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862
2863 *lf = s_lfDefault;
2864 if (name == NULL)
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002865 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002867 wname = enc_to_utf16(name, NULL);
2868 if (wname == NULL)
2869 return FAIL;
2870
2871 if (wcscmp(wname, L"*") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002873# if defined(FEAT_GUI_MSWIN)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002874 CHOOSEFONTW cf;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002875 // if name is "*", bring up std font dialog:
Bram Moolenaara80faa82020-04-12 19:37:17 +02002876 CLEAR_FIELD(cf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 cf.lStructSize = sizeof(cf);
2878 cf.hwndOwner = s_hwnd;
2879 cf.Flags = CF_SCREENFONTS | CF_FIXEDPITCHONLY | CF_INITTOLOGFONTSTRUCT;
2880 if (lastlf != NULL)
2881 *lf = *lastlf;
2882 cf.lpLogFont = lf;
2883 cf.nFontType = 0 ; //REGULAR_FONTTYPE;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002884 if (ChooseFontW(&cf))
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002885 ret = OK;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002886# endif
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002887 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 }
2889
2890 /*
2891 * Split name up, it could be <name>:h<height>:w<width> etc.
2892 */
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002893 for (p = wname; *p && *p != L':'; p++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002895 if (p - wname + 1 >= LF_FACESIZE)
Bram Moolenaar0f873732019-12-05 20:28:46 +01002896 goto theend; // Name too long
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002897 lf->lfFaceName[p - wname] = *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 }
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002899 if (p != wname)
2900 lf->lfFaceName[p - wname] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901
Bram Moolenaar0f873732019-12-05 20:28:46 +01002902 // First set defaults
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 lf->lfHeight = -12;
2904 lf->lfWidth = 0;
2905 lf->lfWeight = FW_NORMAL;
2906 lf->lfItalic = FALSE;
2907 lf->lfUnderline = FALSE;
2908 lf->lfStrikeOut = FALSE;
2909
2910 /*
2911 * If the font can't be found, try replacing '_' by ' '.
2912 */
2913 if (init_logfont(lf) == FAIL)
2914 {
2915 int did_replace = FALSE;
2916
2917 for (i = 0; lf->lfFaceName[i]; ++i)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002918 if (lf->lfFaceName[i] == L'_')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002920 lf->lfFaceName[i] = L' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 did_replace = TRUE;
2922 }
2923 if (!did_replace || init_logfont(lf) == FAIL)
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002924 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925 }
2926
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002927 while (*p == L':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 p++;
2929
Bram Moolenaar0f873732019-12-05 20:28:46 +01002930 // Set the values found after ':'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 while (*p)
2932 {
2933 switch (*p++)
2934 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002935 case L'h':
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002936 lf->lfHeight = - points_to_pixels(p, &p, TRUE, (long_i)printer_dc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002938 case L'w':
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002939 lf->lfWidth = points_to_pixels(p, &p, FALSE, (long_i)printer_dc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 break;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02002941 case L'W':
2942 lf->lfWeight = wcstol(p, &p, 10);
2943 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002944 case L'b':
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 lf->lfWeight = FW_BOLD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002947 case L'i':
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948 lf->lfItalic = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002950 case L'u':
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 lf->lfUnderline = TRUE;
2952 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002953 case L's':
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 lf->lfStrikeOut = TRUE;
2955 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002956 case L'c':
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 {
2958 struct charset_pair *cp;
2959
2960 for (cp = charset_pairs; cp->name != NULL; ++cp)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002961 if (utf16ascncmp(p, cp->name, strlen(cp->name)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962 {
2963 lf->lfCharSet = cp->charset;
2964 p += strlen(cp->name);
2965 break;
2966 }
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002967 if (cp->name == NULL && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002969 char_u *s = utf16_to_enc(p, NULL);
2970 semsg(_("E244: Illegal charset name \"%s\" in font name \"%s\""), s, name);
2971 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972 break;
2973 }
2974 break;
2975 }
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002976 case L'q':
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002977 {
2978 struct quality_pair *qp;
2979
2980 for (qp = quality_pairs; qp->name != NULL; ++qp)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002981 if (utf16ascncmp(p, qp->name, strlen(qp->name)) == 0)
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002982 {
2983 lf->lfQuality = qp->quality;
2984 p += strlen(qp->name);
2985 break;
2986 }
2987 if (qp->name == NULL && verbose)
2988 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002989 char_u *s = utf16_to_enc(p, NULL);
2990 semsg(_("E244: Illegal quality name \"%s\" in font name \"%s\""), s, name);
2991 vim_free(s);
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002992 break;
2993 }
2994 break;
2995 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996 default:
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002997 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002998 semsg(_("E245: Illegal char '%c' in font name \"%s\""), p[-1], name);
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002999 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000 }
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003001 while (*p == L':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002 p++;
3003 }
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003004 ret = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006theend:
Bram Moolenaar0f873732019-12-05 20:28:46 +01003007 // ron: init lastlf
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003008 if (ret == OK && printer_dc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009 {
3010 vim_free(lastlf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003011 lastlf = ALLOC_ONE(LOGFONTW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 if (lastlf != NULL)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003013 mch_memmove(lastlf, lf, sizeof(LOGFONTW));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014 }
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003015 vim_free(wname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003017 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018}
3019
Bram Moolenaar0f873732019-12-05 20:28:46 +01003020#endif // defined(FEAT_GUI) || defined(FEAT_PRINTER)
Bram Moolenaarf12d9832016-01-29 21:11:25 +01003021
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003022#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaarf12d9832016-01-29 21:11:25 +01003023/*
3024 * Initialize the Winsock dll.
3025 */
3026 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003027channel_init_winsock(void)
Bram Moolenaarf12d9832016-01-29 21:11:25 +01003028{
3029 WSADATA wsaData;
3030 int wsaerr;
3031
3032 if (WSInitialized)
3033 return;
3034
3035 wsaerr = WSAStartup(MAKEWORD(2, 2), &wsaData);
3036 if (wsaerr == 0)
3037 WSInitialized = TRUE;
3038}
3039#endif