blob: 80009533c63663d3b655c533f086c41d68f590ea [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 Moolenaar0f873732019-12-05 20:28:46 +0100390 // WinNT and later can use _MAX_PATH wide characters for a pathname, which
391 // means that the maximum pathname is _MAX_PATH * 3 bytes when 'enc' is
392 // UTF-8.
Bram Moolenaard2a203b2013-08-30 16:51:18 +0200393 char szName[_MAX_PATH * 3 + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394
Bram Moolenaar0f873732019-12-05 20:28:46 +0100395 // A name like "d:/foo" and "//server/share" is absolute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396 if ((fname[0] && fname[1] == ':' && (fname[2] == '/' || fname[2] == '\\'))
397 || (fname[0] == fname[1] && (fname[0] == '/' || fname[0] == '\\')))
398 return TRUE;
399
Bram Moolenaar0f873732019-12-05 20:28:46 +0100400 // A name that can't be made absolute probably isn't absolute.
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100401 if (mch_FullName(fname, (char_u *)szName, sizeof(szName) - 1, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402 return FALSE;
403
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100404 return pathcmp((const char *)fname, (const char *)szName, -1) == 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405}
406
407/*
408 * Replace all slashes by backslashes.
409 * This used to be the other way around, but MS-DOS sometimes has problems
410 * with slashes (e.g. in a command name). We can't have mixed slashes and
411 * backslashes, because comparing file names will not work correctly. The
412 * commands that use a file name should try to avoid the need to type a
413 * backslash twice.
414 * When 'shellslash' set do it the other way around.
Bram Moolenaarb4f6a462015-10-13 19:43:17 +0200415 * When the path looks like a URL leave it unmodified.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416 */
417 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100418slash_adjust(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419{
Bram Moolenaarb4f6a462015-10-13 19:43:17 +0200420 if (path_with_url(p))
421 return;
Bram Moolenaar39d21e32017-08-05 23:09:31 +0200422
423 if (*p == '`')
424 {
Bram Moolenaar116a0f82017-08-07 21:17:57 +0200425 size_t len = STRLEN(p);
426
Bram Moolenaar0f873732019-12-05 20:28:46 +0100427 // don't replace backslash in backtick quoted strings
Bram Moolenaar39d21e32017-08-05 23:09:31 +0200428 if (len > 2 && *(p + len - 1) == '`')
429 return;
430 }
431
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +0000432 while (*p)
433 {
434 if (*p == psepcN)
435 *p = psepc;
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100436 MB_PTR_ADV(p);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +0000437 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438}
439
Bram Moolenaar0f873732019-12-05 20:28:46 +0100440// Use 64-bit stat functions if available.
Bram Moolenaar8767f522016-07-01 17:17:39 +0200441#ifdef HAVE_STAT64
442# undef stat
443# undef _stat
444# undef _wstat
445# undef _fstat
446# define stat _stat64
447# define _stat _stat64
448# define _wstat _wstat64
449# define _fstat _fstat64
450#endif
451
Bram Moolenaar9d1685d2014-01-14 12:18:45 +0100452#if (defined(_MSC_VER) && (_MSC_VER >= 1300)) || defined(__MINGW32__)
Bram Moolenaar8962fda2013-09-29 19:05:21 +0200453# define OPEN_OH_ARGTYPE intptr_t
454#else
455# define OPEN_OH_ARGTYPE long
456#endif
457
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200458 static int
Bram Moolenaar8767f522016-07-01 17:17:39 +0200459wstat_symlink_aware(const WCHAR *name, stat_T *stp)
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200460{
Bram Moolenaara12a1612019-01-24 16:39:02 +0100461#if (defined(_MSC_VER) && (_MSC_VER < 1900)) || defined(__MINGW32__)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100462 // Work around for VC12 or earlier (and MinGW). _wstat() can't handle
463 // symlinks properly.
464 // VC9 or earlier: _wstat() doesn't support a symlink at all. It retrieves
465 // status of a symlink itself.
466 // VC10: _wstat() supports a symlink to a normal file, but it doesn't
467 // support a symlink to a directory (always returns an error).
468 // VC11 and VC12: _wstat() doesn't return an error for a symlink to a
469 // directory, but it doesn't set S_IFDIR flag.
470 // MinGW: Same as VC9.
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200471 int n;
472 BOOL is_symlink = FALSE;
473 HANDLE hFind, h;
474 DWORD attr = 0;
475 WIN32_FIND_DATAW findDataW;
476
477 hFind = FindFirstFileW(name, &findDataW);
478 if (hFind != INVALID_HANDLE_VALUE)
479 {
480 attr = findDataW.dwFileAttributes;
481 if ((attr & FILE_ATTRIBUTE_REPARSE_POINT)
482 && (findDataW.dwReserved0 == IO_REPARSE_TAG_SYMLINK))
483 is_symlink = TRUE;
484 FindClose(hFind);
485 }
486 if (is_symlink)
487 {
488 h = CreateFileW(name, FILE_READ_ATTRIBUTES,
489 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
490 OPEN_EXISTING,
491 (attr & FILE_ATTRIBUTE_DIRECTORY)
492 ? FILE_FLAG_BACKUP_SEMANTICS : 0,
493 NULL);
494 if (h != INVALID_HANDLE_VALUE)
495 {
496 int fd;
497
Bram Moolenaar8962fda2013-09-29 19:05:21 +0200498 fd = _open_osfhandle((OPEN_OH_ARGTYPE)h, _O_RDONLY);
Bram Moolenaar8767f522016-07-01 17:17:39 +0200499 n = _fstat(fd, (struct _stat *)stp);
Bram Moolenaarfce7b3d2016-01-19 19:00:32 +0100500 if ((n == 0) && (attr & FILE_ATTRIBUTE_DIRECTORY))
501 stp->st_mode = (stp->st_mode & ~S_IFREG) | S_IFDIR;
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200502 _close(fd);
503 return n;
504 }
505 }
Bram Moolenaara12a1612019-01-24 16:39:02 +0100506#endif
Bram Moolenaar8767f522016-07-01 17:17:39 +0200507 return _wstat(name, (struct _stat *)stp);
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200508}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509
510/*
511 * stat() can't handle a trailing '/' or '\', remove it first.
512 */
513 int
Bram Moolenaar8767f522016-07-01 17:17:39 +0200514vim_stat(const char *name, stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100516 // WinNT and later can use _MAX_PATH wide characters for a pathname, which
517 // means that the maximum pathname is _MAX_PATH * 3 bytes when 'enc' is
518 // UTF-8.
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100519 char_u buf[_MAX_PATH * 3 + 1];
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100520 char_u *p;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200521 WCHAR *wp;
522 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523
Bram Moolenaard2a203b2013-08-30 16:51:18 +0200524 vim_strncpy((char_u *)buf, (char_u *)name, sizeof(buf) - 1);
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100525 p = buf + STRLEN(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526 if (p > buf)
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100527 MB_PTR_BACK(buf, p);
Bram Moolenaarb1cb35f2014-01-10 13:05:20 +0100528
Bram Moolenaar0f873732019-12-05 20:28:46 +0100529 // Remove trailing '\\' except root path.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530 if (p > buf && (*p == '\\' || *p == '/') && p[-1] != ':')
531 *p = NUL;
Bram Moolenaarb1cb35f2014-01-10 13:05:20 +0100532
533 if ((buf[0] == '\\' && buf[1] == '\\') || (buf[0] == '/' && buf[1] == '/'))
534 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100535 // UNC root path must be followed by '\\'.
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100536 p = vim_strpbrk(buf + 2, (char_u *)"\\/");
Bram Moolenaarb1cb35f2014-01-10 13:05:20 +0100537 if (p != NULL)
538 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100539 p = vim_strpbrk(p + 1, (char_u *)"\\/");
Bram Moolenaarb1cb35f2014-01-10 13:05:20 +0100540 if (p == NULL)
541 STRCAT(buf, "\\");
542 }
543 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200545 wp = enc_to_utf16(buf, NULL);
546 if (wp == NULL)
547 return -1;
548
549 n = wstat_symlink_aware(wp, stp);
550 vim_free(wp);
551 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552}
553
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200554#if (defined(FEAT_GUI_MSWIN) && !defined(VIMDLL)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555 void
Bram Moolenaar26e86442020-05-17 14:06:16 +0200556mch_settmode(tmode_T tmode UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100558 // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559}
560
561 int
562mch_get_shellsize(void)
563{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100564 // never used
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 return OK;
566}
567
568 void
569mch_set_shellsize(void)
570{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100571 // never used
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572}
573
574/*
575 * Rows and/or Columns has changed.
576 */
577 void
578mch_new_shellsize(void)
579{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100580 // never used
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581}
582
583#endif
584
585/*
586 * We have no job control, so fake it by starting a new shell.
587 */
588 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100589mch_suspend(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590{
591 suspend_shell();
592}
593
594#if defined(USE_MCH_ERRMSG) || defined(PROTO)
595
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200596# ifdef display_errors
597# undef display_errors
598# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599
600/*
601 * Display the saved error message(s).
602 */
603 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100604display_errors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605{
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200606# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 char *p;
608
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200609# ifdef VIMDLL
610 if (gui.in_use || gui.starting)
611# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200613 if (error_ga.ga_data != NULL)
614 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100615 // avoid putting up a message box with blanks only
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200616 for (p = (char *)error_ga.ga_data; *p; ++p)
617 if (!isspace(*p))
618 {
619 (void)gui_mch_dialog(
Bram Moolenaarc17ef8e2006-03-25 21:48:58 +0000620 gui.starting ? VIM_INFO :
Bram Moolenaarc17ef8e2006-03-25 21:48:58 +0000621 VIM_ERROR,
Bram Moolenaarc17ef8e2006-03-25 21:48:58 +0000622 gui.starting ? (char_u *)_("Message") :
Bram Moolenaarc17ef8e2006-03-25 21:48:58 +0000623 (char_u *)_("Error"),
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100624 (char_u *)p, (char_u *)_("&Ok"),
625 1, NULL, FALSE);
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200626 break;
627 }
628 ga_clear(&error_ga);
629 }
630 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200632# endif
633# if !defined(FEAT_GUI) || defined(VIMDLL)
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +0100634 FlushFileBuffers(GetStdHandle(STD_ERROR_HANDLE));
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200635# endif
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +0100636}
637#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638
639
640/*
641 * Return TRUE if "p" contain a wildcard that can be expanded by
642 * dos_expandpath().
643 */
644 int
645mch_has_exp_wildcard(char_u *p)
646{
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100647 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648 {
649 if (vim_strchr((char_u *)"?*[", *p) != NULL
650 || (*p == '~' && p[1] != NUL))
651 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652 }
653 return FALSE;
654}
655
656/*
657 * Return TRUE if "p" contain a wildcard or a "~1" kind of thing (could be a
658 * shortened file name).
659 */
660 int
661mch_has_wildcard(char_u *p)
662{
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100663 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 {
665 if (vim_strchr((char_u *)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100666#ifdef VIM_BACKTICK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 "?*$[`"
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100668#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669 "?*$["
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100670#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 , *p) != NULL
672 || (*p == '~' && p[1] != NUL))
673 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 }
675 return FALSE;
676}
677
678
679/*
680 * The normal _chdir() does not change the default drive. This one does.
681 * Returning 0 implies success; -1 implies failure.
682 */
683 int
684mch_chdir(char *path)
685{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200686 WCHAR *p;
687 int n;
688
Bram Moolenaar0f873732019-12-05 20:28:46 +0100689 if (path[0] == NUL) // just checking...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 return -1;
691
Bram Moolenaara2974d72009-07-14 16:38:36 +0000692 if (p_verbose >= 5)
693 {
694 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100695 smsg("chdir(%s)", path);
Bram Moolenaara2974d72009-07-14 16:38:36 +0000696 verbose_leave();
697 }
Bram Moolenaar0f873732019-12-05 20:28:46 +0100698 if (isalpha(path[0]) && path[1] == ':') // has a drive name
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100700 // If we can change to the drive, skip that part of the path. If we
701 // can't then the current directory may be invalid, try using chdir()
702 // with the whole path.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 if (_chdrive(TOLOWER_ASC(path[0]) - 'a' + 1) == 0)
704 path += 2;
705 }
706
Bram Moolenaar0f873732019-12-05 20:28:46 +0100707 if (*path == NUL) // drive name only
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 return 0;
709
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200710 p = enc_to_utf16((char_u *)path, NULL);
711 if (p == NULL)
712 return -1;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000713
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200714 n = _wchdir(p);
715 vim_free(p);
716 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717}
718
719
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200720#if defined(FEAT_GUI_MSWIN) && !defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721/*
722 * return non-zero if a character is available
723 */
724 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100725mch_char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100727 // never used
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 return TRUE;
729}
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200730
731# if defined(FEAT_TERMINAL) || defined(PROTO)
732/*
733 * Check for any pending input or messages.
734 */
735 int
736mch_check_messages(void)
737{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100738 // TODO: check for messages
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200739 return TRUE;
740}
741# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742#endif
743
744
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745#if defined(FEAT_LIBCALL) || defined(PROTO)
746/*
747 * Call a DLL routine which takes either a string or int param
748 * and returns an allocated string.
749 * Return OK if it worked, FAIL if not.
750 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751typedef LPTSTR (*MYSTRPROCSTR)(LPTSTR);
752typedef LPTSTR (*MYINTPROCSTR)(int);
753typedef int (*MYSTRPROCINT)(LPTSTR);
754typedef int (*MYINTPROCINT)(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756/*
757 * Check if a pointer points to a valid NUL terminated string.
758 * Return the length of the string, including terminating NUL.
759 * Returns 0 for an invalid pointer, 1 for an empty string.
760 */
761 static size_t
762check_str_len(char_u *str)
763{
764 SYSTEM_INFO si;
765 MEMORY_BASIC_INFORMATION mbi;
766 size_t length = 0;
767 size_t i;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100768 const char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769
Bram Moolenaar0f873732019-12-05 20:28:46 +0100770 // get page size
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 GetSystemInfo(&si);
772
Bram Moolenaar0f873732019-12-05 20:28:46 +0100773 // get memory information
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774 if (VirtualQuery(str, &mbi, sizeof(mbi)))
775 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100776 // pre cast these (typing savers)
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000777 long_u dwStr = (long_u)str;
778 long_u dwBaseAddress = (long_u)mbi.BaseAddress;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779
Bram Moolenaar0f873732019-12-05 20:28:46 +0100780 // get start address of page that str is on
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000781 long_u strPage = dwStr - (dwStr - dwBaseAddress) % si.dwPageSize;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782
Bram Moolenaar0f873732019-12-05 20:28:46 +0100783 // get length from str to end of page
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000784 long_u pageLength = si.dwPageSize - (dwStr - strPage);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785
Bram Moolenaar442b4222010-05-24 21:34:22 +0200786 for (p = str; !IsBadReadPtr(p, (UINT)pageLength);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 p += pageLength, pageLength = si.dwPageSize)
788 for (i = 0; i < pageLength; ++i, ++length)
789 if (p[i] == NUL)
790 return length + 1;
791 }
792
793 return 0;
794}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200796/*
797 * Passed to do_in_runtimepath() to load a vim.ico file.
798 */
799 static void
800mch_icon_load_cb(char_u *fname, void *cookie)
801{
802 HANDLE *h = (HANDLE *)cookie;
803
804 *h = LoadImage(NULL,
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100805 (LPSTR)fname,
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200806 IMAGE_ICON,
807 64,
808 64,
809 LR_LOADFROMFILE | LR_LOADMAP3DCOLORS);
810}
811
812/*
813 * Try loading an icon file from 'runtimepath'.
814 */
815 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100816mch_icon_load(HANDLE *iconp)
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200817{
818 return do_in_runtimepath((char_u *)"bitmaps/vim.ico",
Bram Moolenaar7f8989d2016-03-12 22:11:39 +0100819 0, mch_icon_load_cb, iconp);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200820}
821
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 int
823mch_libcall(
824 char_u *libname,
825 char_u *funcname,
Bram Moolenaar0f873732019-12-05 20:28:46 +0100826 char_u *argstring, // NULL when using a argint
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 int argint,
Bram Moolenaar0f873732019-12-05 20:28:46 +0100828 char_u **string_result,// NULL when using number_result
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 int *number_result)
830{
831 HINSTANCE hinstLib;
832 MYSTRPROCSTR ProcAdd;
833 MYINTPROCSTR ProcAddI;
834 char_u *retval_str = NULL;
835 int retval_int = 0;
836 size_t len;
837
838 BOOL fRunTimeLinkSuccess = FALSE;
839
840 // Get a handle to the DLL module.
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100841 hinstLib = vimLoadLib((char *)libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842
843 // If the handle is valid, try to get the function address.
844 if (hinstLib != NULL)
845 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100846# ifdef HAVE_TRY_EXCEPT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 __try
848 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100849# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 if (argstring != NULL)
851 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100852 // Call with string argument
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100853 ProcAdd = (MYSTRPROCSTR)GetProcAddress(hinstLib, (LPCSTR)funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 if ((fRunTimeLinkSuccess = (ProcAdd != NULL)) != 0)
855 {
856 if (string_result == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100857 retval_int = ((MYSTRPROCINT)ProcAdd)((LPSTR)argstring);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100859 retval_str = (char_u *)(ProcAdd)((LPSTR)argstring);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 }
861 }
862 else
863 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100864 // Call with number argument
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100865 ProcAddI = (MYINTPROCSTR) GetProcAddress(hinstLib, (LPCSTR)funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866 if ((fRunTimeLinkSuccess = (ProcAddI != NULL)) != 0)
867 {
868 if (string_result == NULL)
869 retval_int = ((MYINTPROCINT)ProcAddI)(argint);
870 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100871 retval_str = (char_u *)(ProcAddI)(argint);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 }
873 }
874
875 // Save the string before we free the library.
876 // Assume that a "1" result is an illegal pointer.
877 if (string_result == NULL)
878 *number_result = retval_int;
879 else if (retval_str != NULL
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100880 && (len = check_str_len(retval_str)) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881 {
Bram Moolenaar18a4ba22019-05-24 19:39:03 +0200882 *string_result = alloc(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 if (*string_result != NULL)
884 mch_memmove(*string_result, retval_str, len);
885 }
886
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100887# ifdef HAVE_TRY_EXCEPT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 }
889 __except(EXCEPTION_EXECUTE_HANDLER)
890 {
891 if (GetExceptionCode() == EXCEPTION_STACK_OVERFLOW)
892 RESETSTKOFLW();
893 fRunTimeLinkSuccess = 0;
894 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100895# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896
897 // Free the DLL module.
898 (void)FreeLibrary(hinstLib);
899 }
900
901 if (!fRunTimeLinkSuccess)
902 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100903 semsg(_(e_libcall), funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 return FAIL;
905 }
906
907 return OK;
908}
909#endif
910
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911/*
912 * Debugging helper: expose the MCH_WRITE_DUMP stuff to other modules
913 */
914 void
Bram Moolenaar1266d672017-02-01 13:43:36 +0100915DumpPutS(const char *psz UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100917#ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 if (fdDump)
919 {
920 fputs(psz, fdDump);
921 if (psz[strlen(psz) - 1] != '\n')
922 fputc('\n', fdDump);
923 fflush(fdDump);
924 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100925#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926}
927
928#ifdef _DEBUG
929
930void __cdecl
931Trace(
932 char *pszFormat,
933 ...)
934{
935 CHAR szBuff[2048];
936 va_list args;
937
938 va_start(args, pszFormat);
939 vsprintf(szBuff, pszFormat, args);
940 va_end(args);
941
942 OutputDebugString(szBuff);
943}
944
945#endif //_DEBUG
946
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200947#if !defined(FEAT_GUI) || defined(VIMDLL) || defined(PROTO)
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200948# ifdef FEAT_TITLE
Bram Moolenaar0f873732019-12-05 20:28:46 +0100949extern HWND g_hWnd; // This is in os_win32.c.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950# endif
951
952/*
953 * Showing the printer dialog is tricky since we have no GUI
954 * window to parent it. The following routines are needed to
955 * get the window parenting and Z-order to work properly.
956 */
957 static void
958GetConsoleHwnd(void)
959{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100960 // Skip if it's already set.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 if (s_hwnd != 0)
962 return;
963
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200964# ifdef FEAT_TITLE
Bram Moolenaar0f873732019-12-05 20:28:46 +0100965 // Window handle may have been found by init code (Windows NT only)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 if (g_hWnd != 0)
967 {
968 s_hwnd = g_hWnd;
969 return;
970 }
971# endif
972
Bram Moolenaare1ed53f2019-02-12 23:12:37 +0100973 s_hwnd = GetConsoleWindow();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974}
Bram Moolenaar843ee412004-06-30 16:16:41 +0000975
976/*
977 * Console implementation of ":winpos".
978 */
979 int
980mch_get_winpos(int *x, int *y)
981{
982 RECT rect;
983
984 GetConsoleHwnd();
985 GetWindowRect(s_hwnd, &rect);
986 *x = rect.left;
987 *y = rect.top;
988 return OK;
989}
990
991/*
992 * Console implementation of ":winpos x y".
993 */
994 void
995mch_set_winpos(int x, int y)
996{
997 GetConsoleHwnd();
998 SetWindowPos(s_hwnd, NULL, x, y, 0, 0,
999 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
1000}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001#endif
1002
1003#if (defined(FEAT_PRINTER) && !defined(FEAT_POSTSCRIPT)) || defined(PROTO)
1004
Bram Moolenaar0f873732019-12-05 20:28:46 +01001005//=================================================================
1006// Win32 printer stuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007
1008static HFONT prt_font_handles[2][2][2];
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001009static PRINTDLGW prt_dlg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010static const int boldface[2] = {FW_REGULAR, FW_BOLD};
1011static TEXTMETRIC prt_tm;
1012static int prt_line_height;
1013static int prt_number_width;
1014static int prt_left_margin;
1015static int prt_right_margin;
1016static int prt_top_margin;
1017static char_u szAppName[] = TEXT("VIM");
1018static HWND hDlgPrint;
1019static int *bUserAbort = NULL;
1020static char_u *prt_name = NULL;
1021
Bram Moolenaar0f873732019-12-05 20:28:46 +01001022// Defines which are also in vim.rc.
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001023# define IDC_BOX1 400
1024# define IDC_PRINTTEXT1 401
1025# define IDC_PRINTTEXT2 402
1026# define IDC_PROGRESS 403
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001028 static BOOL
1029vimSetDlgItemText(HWND hDlg, int nIDDlgItem, char_u *s)
1030{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001031 WCHAR *wp;
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001032 BOOL ret;
1033
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001034 wp = enc_to_utf16(s, NULL);
1035 if (wp == NULL)
1036 return FALSE;
1037
1038 ret = SetDlgItemTextW(hDlg, nIDDlgItem, wp);
1039 vim_free(wp);
1040 return ret;
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001041}
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001042
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043/*
1044 * Convert BGR to RGB for Windows GDI calls
1045 */
1046 static COLORREF
1047swap_me(COLORREF colorref)
1048{
1049 int temp;
1050 char *ptr = (char *)&colorref;
1051
1052 temp = *(ptr);
1053 *(ptr ) = *(ptr + 2);
1054 *(ptr + 2) = temp;
1055 return colorref;
1056}
1057
Bram Moolenaar0f873732019-12-05 20:28:46 +01001058// Attempt to make this work for old and new compilers
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001059# if !defined(_WIN64) && (!defined(_MSC_VER) || _MSC_VER < 1300)
1060# define PDP_RETVAL BOOL
1061# else
1062# define PDP_RETVAL INT_PTR
1063# endif
Bram Moolenaared39e1d2008-08-09 17:55:22 +00001064
Bram Moolenaared39e1d2008-08-09 17:55:22 +00001065 static PDP_RETVAL CALLBACK
Bram Moolenaar1266d672017-02-01 13:43:36 +01001066PrintDlgProc(
1067 HWND hDlg,
1068 UINT message,
1069 WPARAM wParam UNUSED,
1070 LPARAM lParam UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001072# ifdef FEAT_GETTEXT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 NONCLIENTMETRICS nm;
1074 static HFONT hfont;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001075# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076
1077 switch (message)
1078 {
1079 case WM_INITDIALOG:
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001080# ifdef FEAT_GETTEXT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 nm.cbSize = sizeof(NONCLIENTMETRICS);
1082 if (SystemParametersInfo(
1083 SPI_GETNONCLIENTMETRICS,
1084 sizeof(NONCLIENTMETRICS),
1085 &nm,
1086 0))
1087 {
1088 char buff[MAX_PATH];
1089 int i;
1090
Bram Moolenaar0f873732019-12-05 20:28:46 +01001091 // Translate the dialog texts
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 hfont = CreateFontIndirect(&nm.lfMessageFont);
1093 for (i = IDC_PRINTTEXT1; i <= IDC_PROGRESS; i++)
1094 {
1095 SendDlgItemMessage(hDlg, i, WM_SETFONT, (WPARAM)hfont, 1);
1096 if (GetDlgItemText(hDlg,i, buff, sizeof(buff)))
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001097 vimSetDlgItemText(hDlg,i, (char_u *)_(buff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 }
1099 SendDlgItemMessage(hDlg, IDCANCEL,
1100 WM_SETFONT, (WPARAM)hfont, 1);
1101 if (GetDlgItemText(hDlg,IDCANCEL, buff, sizeof(buff)))
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001102 vimSetDlgItemText(hDlg,IDCANCEL, (char_u *)_(buff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001104# endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001105 SetWindowText(hDlg, (LPCSTR)szAppName);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 if (prt_name != NULL)
1107 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001108 vimSetDlgItemText(hDlg, IDC_PRINTTEXT2, (char_u *)prt_name);
Bram Moolenaard23a8232018-02-10 18:45:26 +01001109 VIM_CLEAR(prt_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 }
1111 EnableMenuItem(GetSystemMenu(hDlg, FALSE), SC_CLOSE, MF_GRAYED);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001112# if !defined(FEAT_GUI) || defined(VIMDLL)
1113# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001114 if (!gui.in_use)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001115# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001116 BringWindowToTop(s_hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001117# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 return TRUE;
1119
1120 case WM_COMMAND:
1121 *bUserAbort = TRUE;
1122 EnableWindow(GetParent(hDlg), TRUE);
1123 DestroyWindow(hDlg);
1124 hDlgPrint = NULL;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001125# ifdef FEAT_GETTEXT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 DeleteObject(hfont);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001127# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 return TRUE;
1129 }
1130 return FALSE;
1131}
1132
1133 static BOOL CALLBACK
Bram Moolenaar1266d672017-02-01 13:43:36 +01001134AbortProc(HDC hdcPrn UNUSED, int iCode UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135{
1136 MSG msg;
1137
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02001138 while (!*bUserAbort && pPeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 {
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02001140 if (!hDlgPrint || !pIsDialogMessage(hDlgPrint, &msg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 {
1142 TranslateMessage(&msg);
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02001143 pDispatchMessage(&msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 }
1145 }
1146 return !*bUserAbort;
1147}
1148
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001149# if !defined(FEAT_GUI) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150
Bram Moolenaar26fdd7d2011-11-30 13:42:44 +01001151 static UINT_PTR CALLBACK
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152PrintHookProc(
1153 HWND hDlg, // handle to dialog box
1154 UINT uiMsg, // message identifier
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02001155 WPARAM wParam UNUSED, // message parameter
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 LPARAM lParam // message parameter
1157 )
1158{
1159 HWND hwndOwner;
1160 RECT rc, rcDlg, rcOwner;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001161 PRINTDLGW *pPD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162
1163 if (uiMsg == WM_INITDIALOG)
1164 {
1165 // Get the owner window and dialog box rectangles.
1166 if ((hwndOwner = GetParent(hDlg)) == NULL)
1167 hwndOwner = GetDesktopWindow();
1168
1169 GetWindowRect(hwndOwner, &rcOwner);
1170 GetWindowRect(hDlg, &rcDlg);
1171 CopyRect(&rc, &rcOwner);
1172
1173 // Offset the owner and dialog box rectangles so that
1174 // right and bottom values represent the width and
1175 // height, and then offset the owner again to discard
1176 // space taken up by the dialog box.
1177
1178 OffsetRect(&rcDlg, -rcDlg.left, -rcDlg.top);
1179 OffsetRect(&rc, -rc.left, -rc.top);
1180 OffsetRect(&rc, -rcDlg.right, -rcDlg.bottom);
1181
1182 // The new position is the sum of half the remaining
1183 // space and the owner's original position.
1184
1185 SetWindowPos(hDlg,
1186 HWND_TOP,
1187 rcOwner.left + (rc.right / 2),
1188 rcOwner.top + (rc.bottom / 2),
1189 0, 0, // ignores size arguments
1190 SWP_NOSIZE);
1191
Bram Moolenaar0f873732019-12-05 20:28:46 +01001192 // tackle the printdlg copiesctrl problem
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001193 pPD = (PRINTDLGW *)lParam;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 pPD->nCopies = (WORD)pPD->lCustData;
1195 SetDlgItemInt( hDlg, edt3, pPD->nCopies, FALSE );
Bram Moolenaar0f873732019-12-05 20:28:46 +01001196 // Bring the window to top
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 BringWindowToTop(GetParent(hDlg));
1198 SetForegroundWindow(hDlg);
1199 }
1200
1201 return FALSE;
1202}
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001203# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204
1205 void
1206mch_print_cleanup(void)
1207{
1208 int pifItalic;
1209 int pifBold;
1210 int pifUnderline;
1211
1212 for (pifBold = 0; pifBold <= 1; pifBold++)
1213 for (pifItalic = 0; pifItalic <= 1; pifItalic++)
1214 for (pifUnderline = 0; pifUnderline <= 1; pifUnderline++)
1215 DeleteObject(prt_font_handles[pifBold][pifItalic][pifUnderline]);
1216
1217 if (prt_dlg.hDC != NULL)
1218 DeleteDC(prt_dlg.hDC);
1219 if (!*bUserAbort)
1220 SendMessage(hDlgPrint, WM_COMMAND, 0, 0);
1221}
1222
1223 static int
1224to_device_units(int idx, int dpi, int physsize, int offset, int def_number)
1225{
1226 int ret = 0;
1227 int u;
1228 int nr;
1229
1230 u = prt_get_unit(idx);
1231 if (u == PRT_UNIT_NONE)
1232 {
1233 u = PRT_UNIT_PERC;
1234 nr = def_number;
1235 }
1236 else
1237 nr = printer_opts[idx].number;
1238
1239 switch (u)
1240 {
1241 case PRT_UNIT_PERC:
1242 ret = (physsize * nr) / 100;
1243 break;
1244 case PRT_UNIT_INCH:
1245 ret = (nr * dpi);
1246 break;
1247 case PRT_UNIT_MM:
1248 ret = (nr * 10 * dpi) / 254;
1249 break;
1250 case PRT_UNIT_POINT:
1251 ret = (nr * 10 * dpi) / 720;
1252 break;
1253 }
1254
1255 if (ret < offset)
1256 return 0;
1257 else
1258 return ret - offset;
1259}
1260
1261 static int
1262prt_get_cpl(void)
1263{
1264 int hr;
1265 int phyw;
1266 int dvoff;
1267 int rev_offset;
1268 int dpi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269
1270 GetTextMetrics(prt_dlg.hDC, &prt_tm);
1271 prt_line_height = prt_tm.tmHeight + prt_tm.tmExternalLeading;
1272
1273 hr = GetDeviceCaps(prt_dlg.hDC, HORZRES);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 phyw = GetDeviceCaps(prt_dlg.hDC, PHYSICALWIDTH);
1275 dvoff = GetDeviceCaps(prt_dlg.hDC, PHYSICALOFFSETX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 dpi = GetDeviceCaps(prt_dlg.hDC, LOGPIXELSX);
1277
1278 rev_offset = phyw - (dvoff + hr);
1279
1280 prt_left_margin = to_device_units(OPT_PRINT_LEFT, dpi, phyw, dvoff, 10);
1281 if (prt_use_number())
1282 {
1283 prt_number_width = PRINT_NUMBER_WIDTH * prt_tm.tmAveCharWidth;
1284 prt_left_margin += prt_number_width;
1285 }
1286 else
1287 prt_number_width = 0;
1288
1289 prt_right_margin = hr - to_device_units(OPT_PRINT_RIGHT, dpi, phyw,
1290 rev_offset, 5);
1291
1292 return (prt_right_margin - prt_left_margin) / prt_tm.tmAveCharWidth;
1293}
1294
1295 static int
1296prt_get_lpp(void)
1297{
1298 int vr;
1299 int phyw;
1300 int dvoff;
1301 int rev_offset;
1302 int bottom_margin;
1303 int dpi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304
1305 vr = GetDeviceCaps(prt_dlg.hDC, VERTRES);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 phyw = GetDeviceCaps(prt_dlg.hDC, PHYSICALHEIGHT);
1307 dvoff = GetDeviceCaps(prt_dlg.hDC, PHYSICALOFFSETY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 dpi = GetDeviceCaps(prt_dlg.hDC, LOGPIXELSY);
1309
1310 rev_offset = phyw - (dvoff + vr);
1311
1312 prt_top_margin = to_device_units(OPT_PRINT_TOP, dpi, phyw, dvoff, 5);
1313
Bram Moolenaar0f873732019-12-05 20:28:46 +01001314 // adjust top margin if there is a header
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 prt_top_margin += prt_line_height * prt_header_height();
1316
1317 bottom_margin = vr - to_device_units(OPT_PRINT_BOT, dpi, phyw,
1318 rev_offset, 5);
1319
1320 return (bottom_margin - prt_top_margin) / prt_line_height;
1321}
1322
1323 int
1324mch_print_init(prt_settings_T *psettings, char_u *jobname, int forceit)
1325{
1326 static HGLOBAL stored_dm = NULL;
1327 static HGLOBAL stored_devn = NULL;
1328 static int stored_nCopies = 1;
1329 static int stored_nFlags = 0;
1330
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001331 LOGFONTW fLogFont;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 int pifItalic;
1333 int pifBold;
1334 int pifUnderline;
1335
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001336 DEVMODEW *mem;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 DEVNAMES *devname;
1338 int i;
1339
1340 bUserAbort = &(psettings->user_abort);
Bram Moolenaara80faa82020-04-12 19:37:17 +02001341 CLEAR_FIELD(prt_dlg);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001342 prt_dlg.lStructSize = sizeof(PRINTDLGW);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001343# if !defined(FEAT_GUI) || defined(VIMDLL)
1344# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001345 if (!gui.in_use)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001346# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01001347 GetConsoleHwnd(); // get value of s_hwnd
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001348# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 prt_dlg.hwndOwner = s_hwnd;
1350 prt_dlg.Flags = PD_NOPAGENUMS | PD_NOSELECTION | PD_RETURNDC;
1351 if (!forceit)
1352 {
1353 prt_dlg.hDevMode = stored_dm;
1354 prt_dlg.hDevNames = stored_devn;
1355 prt_dlg.lCustData = stored_nCopies; // work around bug in print dialog
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001356# if !defined(FEAT_GUI) || defined(VIMDLL)
1357# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001358 if (!gui.in_use)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001359# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001360 {
1361 /*
1362 * Use hook to prevent console window being sent to back
1363 */
1364 prt_dlg.lpfnPrintHook = PrintHookProc;
1365 prt_dlg.Flags |= PD_ENABLEPRINTHOOK;
1366 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001367# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 prt_dlg.Flags |= stored_nFlags;
1369 }
1370
1371 /*
1372 * If bang present, return default printer setup with no dialog
1373 * never show dialog if we are running over telnet
1374 */
1375 if (forceit
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001376# if !defined(FEAT_GUI) || defined(VIMDLL)
1377# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001378 || (!gui.in_use && !term_console)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001379# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 || !term_console
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001381# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001382# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 )
1384 {
1385 prt_dlg.Flags |= PD_RETURNDEFAULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 /*
1387 * MSDN suggests setting the first parameter to WINSPOOL for
1388 * NT, but NULL appears to work just as well.
1389 */
1390 if (*p_pdev != NUL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001391 prt_dlg.hDC = CreateDC(NULL, (LPCSTR)p_pdev, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 {
1394 prt_dlg.Flags |= PD_RETURNDEFAULT;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001395 if (PrintDlgW(&prt_dlg) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 goto init_fail_dlg;
1397 }
1398 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001399 else if (PrintDlgW(&prt_dlg) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 goto init_fail_dlg;
1401 else
1402 {
1403 /*
1404 * keep the previous driver context
1405 */
1406 stored_dm = prt_dlg.hDevMode;
1407 stored_devn = prt_dlg.hDevNames;
1408 stored_nFlags = prt_dlg.Flags;
1409 stored_nCopies = prt_dlg.nCopies;
1410 }
1411
1412 if (prt_dlg.hDC == NULL)
1413 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001414 emsg(_("E237: Printer selection failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 mch_print_cleanup();
1416 return FALSE;
1417 }
1418
Bram Moolenaar0f873732019-12-05 20:28:46 +01001419 // Not all printer drivers report the support of color (or grey) in the
1420 // same way. Let's set has_color if there appears to be some way to print
1421 // more than B&W.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 i = GetDeviceCaps(prt_dlg.hDC, NUMCOLORS);
1423 psettings->has_color = (GetDeviceCaps(prt_dlg.hDC, BITSPIXEL) > 1
1424 || GetDeviceCaps(prt_dlg.hDC, PLANES) > 1
1425 || i > 2 || i == -1);
1426
Bram Moolenaar0f873732019-12-05 20:28:46 +01001427 // Ensure all font styles are baseline aligned
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 SetTextAlign(prt_dlg.hDC, TA_BASELINE|TA_LEFT);
1429
1430 /*
1431 * On some windows systems the nCopies parameter is not
1432 * passed back correctly. It must be retrieved from the
1433 * hDevMode struct.
1434 */
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001435 mem = (DEVMODEW *)GlobalLock(prt_dlg.hDevMode);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 if (mem != NULL)
1437 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 if (mem->dmCopies != 1)
1439 stored_nCopies = mem->dmCopies;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 if ((mem->dmFields & DM_DUPLEX) && (mem->dmDuplex & ~DMDUP_SIMPLEX))
1441 psettings->duplex = TRUE;
1442 if ((mem->dmFields & DM_COLOR) && (mem->dmColor & DMCOLOR_COLOR))
1443 psettings->has_color = TRUE;
1444 }
1445 GlobalUnlock(prt_dlg.hDevMode);
1446
1447 devname = (DEVNAMES *)GlobalLock(prt_dlg.hDevNames);
1448 if (devname != 0)
1449 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001450 WCHAR *wprinter_name = (WCHAR *)devname + devname->wDeviceOffset;
1451 WCHAR *wport_name = (WCHAR *)devname + devname->wOutputOffset;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001452 char_u *text = (char_u *)_("to %s on %s");
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001453 char_u *printer_name = utf16_to_enc(wprinter_name, NULL);
1454 char_u *port_name = utf16_to_enc(wport_name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001456 if (printer_name != NULL && port_name != NULL)
Bram Moolenaar964b3742019-05-24 18:54:09 +02001457 prt_name = alloc(STRLEN(printer_name)
1458 + STRLEN(port_name) + STRLEN(text));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 if (prt_name != NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001460 wsprintf((char *)prt_name, (const char *)text,
1461 printer_name, port_name);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001462 vim_free(printer_name);
1463 vim_free(port_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 }
1465 GlobalUnlock(prt_dlg.hDevNames);
1466
1467 /*
1468 * Initialise the font according to 'printfont'
1469 */
Bram Moolenaara80faa82020-04-12 19:37:17 +02001470 CLEAR_FIELD(fLogFont);
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001471 if (get_logfont(&fLogFont, p_pfn, prt_dlg.hDC, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001473 semsg(_("E613: Unknown printer font: %s"), p_pfn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 mch_print_cleanup();
1475 return FALSE;
1476 }
1477
1478 for (pifBold = 0; pifBold <= 1; pifBold++)
1479 for (pifItalic = 0; pifItalic <= 1; pifItalic++)
1480 for (pifUnderline = 0; pifUnderline <= 1; pifUnderline++)
1481 {
1482 fLogFont.lfWeight = boldface[pifBold];
1483 fLogFont.lfItalic = pifItalic;
1484 fLogFont.lfUnderline = pifUnderline;
1485 prt_font_handles[pifBold][pifItalic][pifUnderline]
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001486 = CreateFontIndirectW(&fLogFont);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 }
1488
1489 SetBkMode(prt_dlg.hDC, OPAQUE);
1490 SelectObject(prt_dlg.hDC, prt_font_handles[0][0][0]);
1491
1492 /*
1493 * Fill in the settings struct
1494 */
1495 psettings->chars_per_line = prt_get_cpl();
1496 psettings->lines_per_page = prt_get_lpp();
Bram Moolenaar7ddc6422014-09-27 11:18:19 +02001497 if (prt_dlg.Flags & PD_USEDEVMODECOPIESANDCOLLATE)
1498 {
1499 psettings->n_collated_copies = (prt_dlg.Flags & PD_COLLATE)
1500 ? prt_dlg.nCopies : 1;
1501 psettings->n_uncollated_copies = (prt_dlg.Flags & PD_COLLATE)
1502 ? 1 : prt_dlg.nCopies;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503
Bram Moolenaar7ddc6422014-09-27 11:18:19 +02001504 if (psettings->n_collated_copies == 0)
1505 psettings->n_collated_copies = 1;
1506
1507 if (psettings->n_uncollated_copies == 0)
1508 psettings->n_uncollated_copies = 1;
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01001509 }
1510 else
1511 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 psettings->n_collated_copies = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 psettings->n_uncollated_copies = 1;
Bram Moolenaar7ddc6422014-09-27 11:18:19 +02001514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515
1516 psettings->jobname = jobname;
1517
1518 return TRUE;
1519
1520init_fail_dlg:
1521 {
1522 DWORD err = CommDlgExtendedError();
1523
1524 if (err)
1525 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 char_u *buf;
1527
Bram Moolenaar0f873732019-12-05 20:28:46 +01001528 // I suspect FormatMessage() doesn't work for values returned by
1529 // CommDlgExtendedError(). What does?
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
1531 FORMAT_MESSAGE_FROM_SYSTEM |
1532 FORMAT_MESSAGE_IGNORE_INSERTS,
1533 NULL, err, 0, (LPTSTR)(&buf), 0, NULL);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001534 semsg(_("E238: Print error: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 buf == NULL ? (char_u *)_("Unknown") : buf);
1536 LocalFree((LPVOID)(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 }
1538 else
Bram Moolenaar0f873732019-12-05 20:28:46 +01001539 msg_clr_eos(); // Maybe canceled
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540
1541 mch_print_cleanup();
1542 return FALSE;
1543 }
1544}
1545
1546
1547 int
1548mch_print_begin(prt_settings_T *psettings)
1549{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001550 int ret = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 char szBuffer[300];
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001552 WCHAR *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001554 hDlgPrint = CreateDialog(g_hinst, TEXT("PrintDlgBox"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555 prt_dlg.hwndOwner, PrintDlgProc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 SetAbortProc(prt_dlg.hDC, AbortProc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557 wsprintf(szBuffer, _("Printing '%s'"), gettail(psettings->jobname));
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001558 vimSetDlgItemText(hDlgPrint, IDC_PRINTTEXT1, (char_u *)szBuffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001560 wp = enc_to_utf16(psettings->jobname, NULL);
Bram Moolenaar2290b1f2018-05-13 17:30:45 +02001561 if (wp != NULL)
1562 {
1563 DOCINFOW di;
1564
Bram Moolenaara80faa82020-04-12 19:37:17 +02001565 CLEAR_FIELD(di);
Bram Moolenaar2290b1f2018-05-13 17:30:45 +02001566 di.cbSize = sizeof(di);
1567 di.lpszDocName = wp;
1568 ret = StartDocW(prt_dlg.hDC, &di);
1569 vim_free(wp);
1570 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001572# ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01001573 // Give focus back to main window (when using MDI).
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001574# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001575 if (gui.in_use)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001576# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001577 SetFocus(s_hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001578# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579
1580 return (ret > 0);
1581}
1582
1583 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01001584mch_print_end(prt_settings_T *psettings UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585{
1586 EndDoc(prt_dlg.hDC);
1587 if (!*bUserAbort)
1588 SendMessage(hDlgPrint, WM_COMMAND, 0, 0);
1589}
1590
1591 int
1592mch_print_end_page(void)
1593{
1594 return (EndPage(prt_dlg.hDC) > 0);
1595}
1596
1597 int
1598mch_print_begin_page(char_u *msg)
1599{
1600 if (msg != NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001601 vimSetDlgItemText(hDlgPrint, IDC_PROGRESS, msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 return (StartPage(prt_dlg.hDC) > 0);
1603}
1604
1605 int
1606mch_print_blank_page(void)
1607{
1608 return (mch_print_begin_page(NULL) ? (mch_print_end_page()) : FALSE);
1609}
1610
1611static int prt_pos_x = 0;
1612static int prt_pos_y = 0;
1613
1614 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001615mch_print_start_line(int margin, int page_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616{
1617 if (margin)
1618 prt_pos_x = -prt_number_width;
1619 else
1620 prt_pos_x = 0;
1621 prt_pos_y = page_line * prt_line_height
1622 + prt_tm.tmAscent + prt_tm.tmExternalLeading;
1623}
1624
1625 int
1626mch_print_text_out(char_u *p, int len)
1627{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 SIZE sz;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001629 WCHAR *wp;
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001630 int wlen = len;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001631 int ret = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001633 wp = enc_to_utf16(p, &wlen);
1634 if (wp == NULL)
1635 return FALSE;
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001636
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001637 TextOutW(prt_dlg.hDC, prt_pos_x + prt_left_margin,
1638 prt_pos_y + prt_top_margin, wp, wlen);
1639 GetTextExtentPoint32W(prt_dlg.hDC, wp, wlen, &sz);
1640 vim_free(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 prt_pos_x += (sz.cx - prt_tm.tmOverhang);
Bram Moolenaar0f873732019-12-05 20:28:46 +01001642 // This is wrong when printing spaces for a TAB.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001643 if (p[len] != NUL)
1644 {
Bram Moolenaar1614a142019-10-06 22:00:13 +02001645 wlen = mb_ptr2len(p + len);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001646 wp = enc_to_utf16(p + len, &wlen);
1647 if (wp != NULL)
1648 {
1649 GetTextExtentPoint32W(prt_dlg.hDC, wp, 1, &sz);
1650 ret = (prt_pos_x + prt_left_margin + sz.cx > prt_right_margin);
1651 vim_free(wp);
1652 }
1653 }
1654 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655}
1656
1657 void
1658mch_print_set_font(int iBold, int iItalic, int iUnderline)
1659{
1660 SelectObject(prt_dlg.hDC, prt_font_handles[iBold][iItalic][iUnderline]);
1661}
1662
1663 void
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001664mch_print_set_bg(long_u bgcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665{
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001666 SetBkColor(prt_dlg.hDC, GetNearestColor(prt_dlg.hDC,
1667 swap_me((COLORREF)bgcol)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 /*
1669 * With a white background we can draw characters transparent, which is
1670 * good for italic characters that overlap to the next char cell.
1671 */
1672 if (bgcol == 0xffffffUL)
1673 SetBkMode(prt_dlg.hDC, TRANSPARENT);
1674 else
1675 SetBkMode(prt_dlg.hDC, OPAQUE);
1676}
1677
1678 void
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001679mch_print_set_fg(long_u fgcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680{
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001681 SetTextColor(prt_dlg.hDC, GetNearestColor(prt_dlg.hDC,
1682 swap_me((COLORREF)fgcol)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683}
1684
Bram Moolenaar0f873732019-12-05 20:28:46 +01001685#endif // FEAT_PRINTER && !FEAT_POSTSCRIPT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686
Bram Moolenaar58d98232005-07-23 22:25:46 +00001687
1688
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689#if defined(FEAT_SHORTCUT) || defined(PROTO)
Bram Moolenaar82881492012-11-20 16:53:39 +01001690# ifndef PROTO
1691# include <shlobj.h>
1692# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001694typedef BOOL (WINAPI *pfnGetFinalPathNameByHandleW)(
Bram Moolenaardce1e892019-02-10 23:18:53 +01001695 HANDLE hFile,
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001696 LPWSTR lpszFilePath,
1697 DWORD cchFilePath,
1698 DWORD dwFlags);
1699static pfnGetFinalPathNameByHandleW pGetFinalPathNameByHandleW = NULL;
Bram Moolenaardce1e892019-02-10 23:18:53 +01001700
Bram Moolenaar4a792c82019-06-06 12:22:41 +02001701# define is_path_sep(c) ((c) == L'\\' || (c) == L'/')
1702
1703 static int
1704is_reparse_point_included(LPCWSTR fname)
1705{
1706 LPCWSTR p = fname, q;
1707 WCHAR buf[MAX_PATH];
1708 DWORD attr;
1709
1710 if (isalpha(p[0]) && p[1] == L':' && is_path_sep(p[2]))
1711 p += 3;
1712 else if (is_path_sep(p[0]) && is_path_sep(p[1]))
1713 p += 2;
1714
1715 while (*p != L'\0')
1716 {
1717 q = wcspbrk(p, L"\\/");
1718 if (q == NULL)
1719 p = q = fname + wcslen(fname);
1720 else
1721 p = q + 1;
1722 if (q - fname >= MAX_PATH)
1723 return FALSE;
1724 wcsncpy(buf, fname, q - fname);
1725 buf[q - fname] = L'\0';
1726 attr = GetFileAttributesW(buf);
1727 if (attr != INVALID_FILE_ATTRIBUTES
1728 && (attr & FILE_ATTRIBUTE_REPARSE_POINT) != 0)
1729 return TRUE;
1730 }
1731 return FALSE;
1732}
1733
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02001734 static char_u *
Bram Moolenaardce1e892019-02-10 23:18:53 +01001735resolve_reparse_point(char_u *fname)
1736{
1737 HANDLE h = INVALID_HANDLE_VALUE;
1738 DWORD size;
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001739 WCHAR *p, *wp;
Bram Moolenaardce1e892019-02-10 23:18:53 +01001740 char_u *rfname = NULL;
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001741 WCHAR *buff = NULL;
Bram Moolenaardce1e892019-02-10 23:18:53 +01001742 static BOOL loaded = FALSE;
1743
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001744 if (pGetFinalPathNameByHandleW == NULL)
Bram Moolenaardce1e892019-02-10 23:18:53 +01001745 {
1746 HMODULE hmod = GetModuleHandle("kernel32.dll");
1747
1748 if (loaded == TRUE)
1749 return NULL;
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001750 pGetFinalPathNameByHandleW = (pfnGetFinalPathNameByHandleW)
1751 GetProcAddress(hmod, "GetFinalPathNameByHandleW");
Bram Moolenaardce1e892019-02-10 23:18:53 +01001752 loaded = TRUE;
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001753 if (pGetFinalPathNameByHandleW == NULL)
Bram Moolenaardce1e892019-02-10 23:18:53 +01001754 return NULL;
1755 }
1756
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001757 p = enc_to_utf16(fname, NULL);
1758 if (p == NULL)
1759 goto fail;
1760
Bram Moolenaar4a792c82019-06-06 12:22:41 +02001761 if (!is_reparse_point_included(p))
1762 {
1763 vim_free(p);
1764 goto fail;
1765 }
1766
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001767 h = CreateFileW(p, 0, 0, NULL, OPEN_EXISTING,
1768 FILE_FLAG_BACKUP_SEMANTICS, NULL);
1769 vim_free(p);
Bram Moolenaardce1e892019-02-10 23:18:53 +01001770
1771 if (h == INVALID_HANDLE_VALUE)
1772 goto fail;
1773
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001774 size = pGetFinalPathNameByHandleW(h, NULL, 0, 0);
1775 if (size == 0)
1776 goto fail;
1777 buff = ALLOC_MULT(WCHAR, size);
1778 if (buff == NULL)
1779 goto fail;
1780 if (pGetFinalPathNameByHandleW(h, buff, size, 0) == 0)
Bram Moolenaardce1e892019-02-10 23:18:53 +01001781 goto fail;
1782
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001783 if (wcsncmp(buff, L"\\\\?\\UNC\\", 8) == 0)
1784 {
1785 buff[6] = L'\\';
1786 wp = buff + 6;
1787 }
1788 else if (wcsncmp(buff, L"\\\\?\\", 4) == 0)
1789 wp = buff + 4;
Bram Moolenaardce1e892019-02-10 23:18:53 +01001790 else
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001791 wp = buff;
1792
1793 rfname = utf16_to_enc(wp, NULL);
Bram Moolenaardce1e892019-02-10 23:18:53 +01001794
1795fail:
1796 if (h != INVALID_HANDLE_VALUE)
1797 CloseHandle(h);
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001798 if (buff != NULL)
1799 vim_free(buff);
Bram Moolenaardce1e892019-02-10 23:18:53 +01001800
1801 return rfname;
1802}
1803
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804/*
1805 * When "fname" is the name of a shortcut (*.lnk) resolve the file it points
1806 * to and return that name in allocated memory.
1807 * Otherwise NULL is returned.
1808 */
Bram Moolenaardce1e892019-02-10 23:18:53 +01001809 static char_u *
1810resolve_shortcut(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811{
1812 HRESULT hr;
1813 IShellLink *psl = NULL;
1814 IPersistFile *ppf = NULL;
1815 OLECHAR wsz[MAX_PATH];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 char_u *rfname = NULL;
1817 int len;
Bram Moolenaar604729e2013-08-30 16:44:19 +02001818 IShellLinkW *pslw = NULL;
1819 WIN32_FIND_DATAW ffdw; // we get those free of charge
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820
Bram Moolenaar0f873732019-12-05 20:28:46 +01001821 // Check if the file name ends in ".lnk". Avoid calling
1822 // CoCreateInstance(), it's quite slow.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 if (fname == NULL)
1824 return rfname;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001825 len = (int)STRLEN(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 if (len <= 4 || STRNICMP(fname + len - 4, ".lnk", 4) != 0)
1827 return rfname;
1828
1829 CoInitialize(NULL);
1830
1831 // create a link manager object and request its interface
1832 hr = CoCreateInstance(
1833 &CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001834 &IID_IShellLinkW, (void**)&pslw);
1835 if (hr == S_OK)
1836 {
1837 WCHAR *p = enc_to_utf16(fname, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001839 if (p != NULL)
1840 {
1841 // Get a pointer to the IPersistFile interface.
1842 hr = pslw->lpVtbl->QueryInterface(
1843 pslw, &IID_IPersistFile, (void**)&ppf);
1844 if (hr != S_OK)
1845 goto shortcut_errorw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001847 // "load" the name and resolve the link
1848 hr = ppf->lpVtbl->Load(ppf, p, STGM_READ);
1849 if (hr != S_OK)
1850 goto shortcut_errorw;
1851# if 0 // This makes Vim wait a long time if the target does not exist.
1852 hr = pslw->lpVtbl->Resolve(pslw, NULL, SLR_NO_UI);
1853 if (hr != S_OK)
1854 goto shortcut_errorw;
Bram Moolenaar604729e2013-08-30 16:44:19 +02001855# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001857 // Get the path to the link target.
1858 ZeroMemory(wsz, MAX_PATH * sizeof(WCHAR));
1859 hr = pslw->lpVtbl->GetPath(pslw, wsz, MAX_PATH, &ffdw, 0);
1860 if (hr == S_OK && wsz[0] != NUL)
1861 rfname = utf16_to_enc(wsz, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001863shortcut_errorw:
1864 vim_free(p);
1865 }
1866 }
1867
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 // Release all interface pointers (both belong to the same object)
1869 if (ppf != NULL)
1870 ppf->lpVtbl->Release(ppf);
1871 if (psl != NULL)
1872 psl->lpVtbl->Release(psl);
Bram Moolenaar604729e2013-08-30 16:44:19 +02001873 if (pslw != NULL)
1874 pslw->lpVtbl->Release(pslw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875
1876 CoUninitialize();
1877 return rfname;
1878}
Bram Moolenaardce1e892019-02-10 23:18:53 +01001879
1880 char_u *
1881mch_resolve_path(char_u *fname, int reparse_point)
1882{
1883 char_u *path = resolve_shortcut(fname);
1884
1885 if (path == NULL && reparse_point)
1886 path = resolve_reparse_point(fname);
1887 return path;
1888}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889#endif
1890
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001891#if (defined(FEAT_EVAL) && (!defined(FEAT_GUI) || defined(VIMDLL))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892/*
1893 * Bring ourselves to the foreground. Does work if the OS doesn't allow it.
1894 */
1895 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001896win32_set_foreground(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897{
Bram Moolenaar0f873732019-12-05 20:28:46 +01001898 GetConsoleHwnd(); // get value of s_hwnd
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 if (s_hwnd != 0)
1900 SetForegroundWindow(s_hwnd);
1901}
1902#endif
1903
1904#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
1905/*
1906 * Client-server code for Vim
1907 *
1908 * Originally written by Paul Moore
1909 */
1910
Bram Moolenaar0f873732019-12-05 20:28:46 +01001911// In order to handle inter-process messages, we need to have a window. But
1912// the functions in this module can be called before the main GUI window is
1913// created (and may also be called in the console version, where there is no
1914// GUI window at all).
1915//
1916// So we create a hidden window, and arrange to destroy it on exit.
1917HWND message_window = 0; // window that's handling messages
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001919# define VIM_CLASSNAME "VIM_MESSAGES"
1920# define VIM_CLASSNAME_LEN (sizeof(VIM_CLASSNAME) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921
Bram Moolenaar0f873732019-12-05 20:28:46 +01001922// Communication is via WM_COPYDATA messages. The message type is send in
1923// the dwData parameter. Types are defined here.
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001924# define COPYDATA_KEYS 0
1925# define COPYDATA_REPLY 1
1926# define COPYDATA_EXPR 10
1927# define COPYDATA_RESULT 11
1928# define COPYDATA_ERROR_RESULT 12
1929# define COPYDATA_ENCODING 20
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930
Bram Moolenaar0f873732019-12-05 20:28:46 +01001931// This is a structure containing a server HWND and its name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932struct server_id
1933{
1934 HWND hwnd;
1935 char_u *name;
1936};
1937
Bram Moolenaar0f873732019-12-05 20:28:46 +01001938// Last received 'encoding' that the client uses.
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001939static char_u *client_enc = NULL;
1940
1941/*
1942 * Tell the other side what encoding we are using.
1943 * Errors are ignored.
1944 */
1945 static void
1946serverSendEnc(HWND target)
1947{
1948 COPYDATASTRUCT data;
1949
1950 data.dwData = COPYDATA_ENCODING;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001951 data.cbData = (DWORD)STRLEN(p_enc) + 1;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001952 data.lpData = p_enc;
1953 (void)SendMessage(target, WM_COPYDATA, (WPARAM)message_window,
1954 (LPARAM)(&data));
1955}
1956
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957/*
1958 * Clean up on exit. This destroys the hidden message window.
1959 */
1960 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961CleanUpMessaging(void)
1962{
1963 if (message_window != 0)
1964 {
1965 DestroyWindow(message_window);
1966 message_window = 0;
1967 }
1968}
1969
1970static int save_reply(HWND server, char_u *reply, int expr);
1971
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001972/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973 * The window procedure for the hidden message window.
1974 * It handles callback messages and notifications from servers.
1975 * In order to process these messages, it is necessary to run a
1976 * message loop. Code which may run before the main message loop
1977 * is started (in the GUI) is careful to pump messages when it needs
1978 * to. Features which require message delivery during normal use will
1979 * not work in the console version - this basically means those
1980 * features which allow Vim to act as a server, rather than a client.
1981 */
1982 static LRESULT CALLBACK
1983Messaging_WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
1984{
1985 if (msg == WM_COPYDATA)
1986 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001987 // This is a message from another Vim. The dwData member of the
1988 // COPYDATASTRUCT determines the type of message:
1989 // COPYDATA_ENCODING:
1990 // The encoding that the client uses. Following messages will
1991 // use this encoding, convert if needed.
1992 // COPYDATA_KEYS:
1993 // A key sequence. We are a server, and a client wants these keys
1994 // adding to the input queue.
1995 // COPYDATA_REPLY:
1996 // A reply. We are a client, and a server has sent this message
1997 // in response to a request. (server2client())
1998 // COPYDATA_EXPR:
1999 // An expression. We are a server, and a client wants us to
2000 // evaluate this expression.
2001 // COPYDATA_RESULT:
2002 // A reply. We are a client, and a server has sent this message
2003 // in response to a COPYDATA_EXPR.
2004 // COPYDATA_ERROR_RESULT:
2005 // A reply. We are a client, and a server has sent this message
2006 // in response to a COPYDATA_EXPR that failed to evaluate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 COPYDATASTRUCT *data = (COPYDATASTRUCT*)lParam;
2008 HWND sender = (HWND)wParam;
2009 COPYDATASTRUCT reply;
2010 char_u *res;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 int retval;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002012 char_u *str;
2013 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014
2015 switch (data->dwData)
2016 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002017 case COPYDATA_ENCODING:
Bram Moolenaar0f873732019-12-05 20:28:46 +01002018 // Remember the encoding that the client uses.
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002019 vim_free(client_enc);
2020 client_enc = enc_canonize((char_u *)data->lpData);
2021 return 1;
2022
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 case COPYDATA_KEYS:
Bram Moolenaar0f873732019-12-05 20:28:46 +01002024 // Remember who sent this, for <client>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 clientWindow = sender;
2026
Bram Moolenaar0f873732019-12-05 20:28:46 +01002027 // Add the received keys to the input buffer. The loop waiting
2028 // for the user to do something should check the input buffer.
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002029 str = serverConvert(client_enc, (char_u *)data->lpData, &tofree);
2030 server_to_input_buf(str);
2031 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032
2033# ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01002034 // Wake up the main GUI loop.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002035# ifdef VIMDLL
2036 if (gui.in_use)
2037# endif
2038 if (s_hwnd != 0)
2039 PostMessage(s_hwnd, WM_NULL, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040# endif
2041 return 1;
2042
2043 case COPYDATA_EXPR:
Bram Moolenaar0f873732019-12-05 20:28:46 +01002044 // Remember who sent this, for <client>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 clientWindow = sender;
2046
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002047 str = serverConvert(client_enc, (char_u *)data->lpData, &tofree);
2048 res = eval_client_expr_to_string(str);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002049
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 if (res == NULL)
2051 {
Bram Moolenaar15bf76d2017-03-18 16:18:37 +01002052 char *err = _(e_invexprmsg);
2053 size_t len = STRLEN(str) + STRLEN(err) + 5;
2054
Bram Moolenaar964b3742019-05-24 18:54:09 +02002055 res = alloc(len);
Bram Moolenaar15bf76d2017-03-18 16:18:37 +01002056 if (res != NULL)
2057 vim_snprintf((char *)res, len, "%s: \"%s\"", err, str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 reply.dwData = COPYDATA_ERROR_RESULT;
2059 }
2060 else
2061 reply.dwData = COPYDATA_RESULT;
2062 reply.lpData = res;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002063 reply.cbData = (DWORD)STRLEN(res) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002065 serverSendEnc(sender);
Bram Moolenaar5246cd72013-06-16 16:41:47 +02002066 retval = (int)SendMessage(sender, WM_COPYDATA,
2067 (WPARAM)message_window, (LPARAM)(&reply));
Bram Moolenaar15bf76d2017-03-18 16:18:37 +01002068 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 vim_free(res);
2070 return retval;
2071
2072 case COPYDATA_REPLY:
2073 case COPYDATA_RESULT:
2074 case COPYDATA_ERROR_RESULT:
2075 if (data->lpData != NULL)
2076 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002077 str = serverConvert(client_enc, (char_u *)data->lpData,
2078 &tofree);
2079 if (tofree == NULL)
2080 str = vim_strsave(str);
2081 if (save_reply(sender, str,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 (data->dwData == COPYDATA_REPLY ? 0 :
2083 (data->dwData == COPYDATA_RESULT ? 1 :
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002084 2))) == FAIL)
2085 vim_free(str);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002086 else if (data->dwData == COPYDATA_REPLY)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 {
Bram Moolenaar427d51c2013-06-16 16:01:25 +02002088 char_u winstr[30];
2089
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002090 sprintf((char *)winstr, PRINTF_HEX_LONG_U, (long_u)sender);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002091 apply_autocmds(EVENT_REMOTEREPLY, winstr, str,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 TRUE, curbuf);
2093 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 }
2095 return 1;
2096 }
2097
2098 return 0;
2099 }
2100
2101 else if (msg == WM_ACTIVATE && wParam == WA_ACTIVE)
2102 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002103 // When the message window is activated (brought to the foreground),
2104 // this actually applies to the text window.
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002105# if !defined(FEAT_GUI) || defined(VIMDLL)
2106# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002107 if (!gui.in_use)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002108# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002109 GetConsoleHwnd(); // get value of s_hwnd
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002110# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 if (s_hwnd != 0)
2112 {
2113 SetForegroundWindow(s_hwnd);
2114 return 0;
2115 }
2116 }
2117
2118 return DefWindowProc(hwnd, msg, wParam, lParam);
2119}
2120
2121/*
2122 * Initialise the message handling process. This involves creating a window
2123 * to handle messages - the window will not be visible.
2124 */
2125 void
2126serverInitMessaging(void)
2127{
2128 WNDCLASS wndclass;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129
Bram Moolenaar0f873732019-12-05 20:28:46 +01002130 // Clean up on exit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 atexit(CleanUpMessaging);
2132
Bram Moolenaar0f873732019-12-05 20:28:46 +01002133 // Register a window class - we only really care
2134 // about the window procedure
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 wndclass.style = 0;
2136 wndclass.lpfnWndProc = Messaging_WndProc;
2137 wndclass.cbClsExtra = 0;
2138 wndclass.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002139 wndclass.hInstance = g_hinst;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 wndclass.hIcon = NULL;
2141 wndclass.hCursor = NULL;
2142 wndclass.hbrBackground = NULL;
2143 wndclass.lpszMenuName = NULL;
2144 wndclass.lpszClassName = VIM_CLASSNAME;
2145 RegisterClass(&wndclass);
2146
Bram Moolenaar0f873732019-12-05 20:28:46 +01002147 // Create the message window. It will be hidden, so the details don't
2148 // matter. Don't use WS_OVERLAPPEDWINDOW, it will make a shortcut remove
2149 // focus from gvim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 message_window = CreateWindow(VIM_CLASSNAME, "",
2151 WS_POPUPWINDOW | WS_CAPTION,
2152 CW_USEDEFAULT, CW_USEDEFAULT,
2153 100, 100, NULL, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002154 g_hinst, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155}
2156
Bram Moolenaar0f873732019-12-05 20:28:46 +01002157// Used by serverSendToVim() to find an alternate server name.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002158static char_u *altname_buf_ptr = NULL;
2159
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160/*
2161 * Get the title of the window "hwnd", which is the Vim server name, in
2162 * "name[namelen]" and return the length.
2163 * Returns zero if window "hwnd" is not a Vim server.
2164 */
2165 static int
2166getVimServerName(HWND hwnd, char *name, int namelen)
2167{
2168 int len;
2169 char buffer[VIM_CLASSNAME_LEN + 1];
2170
Bram Moolenaar0f873732019-12-05 20:28:46 +01002171 // Ignore windows which aren't Vim message windows
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 len = GetClassName(hwnd, buffer, sizeof(buffer));
2173 if (len != VIM_CLASSNAME_LEN || STRCMP(buffer, VIM_CLASSNAME) != 0)
2174 return 0;
2175
Bram Moolenaar0f873732019-12-05 20:28:46 +01002176 // Get the title of the window
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 return GetWindowText(hwnd, name, namelen);
2178}
2179
2180 static BOOL CALLBACK
2181enumWindowsGetServer(HWND hwnd, LPARAM lparam)
2182{
2183 struct server_id *id = (struct server_id *)lparam;
2184 char server[MAX_PATH];
2185
Bram Moolenaar0f873732019-12-05 20:28:46 +01002186 // Get the title of the window
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 if (getVimServerName(hwnd, server, sizeof(server)) == 0)
2188 return TRUE;
2189
Bram Moolenaar0f873732019-12-05 20:28:46 +01002190 // If this is the server we're looking for, return its HWND
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 if (STRICMP(server, id->name) == 0)
2192 {
2193 id->hwnd = hwnd;
2194 return FALSE;
2195 }
2196
Bram Moolenaar0f873732019-12-05 20:28:46 +01002197 // If we are looking for an alternate server, remember this name.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002198 if (altname_buf_ptr != NULL
2199 && STRNICMP(server, id->name, STRLEN(id->name)) == 0
2200 && vim_isdigit(server[STRLEN(id->name)]))
2201 {
2202 STRCPY(altname_buf_ptr, server);
Bram Moolenaar0f873732019-12-05 20:28:46 +01002203 altname_buf_ptr = NULL; // don't use another name
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002204 }
2205
Bram Moolenaar0f873732019-12-05 20:28:46 +01002206 // Otherwise, keep looking
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207 return TRUE;
2208}
2209
2210 static BOOL CALLBACK
2211enumWindowsGetNames(HWND hwnd, LPARAM lparam)
2212{
2213 garray_T *ga = (garray_T *)lparam;
2214 char server[MAX_PATH];
2215
Bram Moolenaar0f873732019-12-05 20:28:46 +01002216 // Get the title of the window
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 if (getVimServerName(hwnd, server, sizeof(server)) == 0)
2218 return TRUE;
2219
Bram Moolenaar0f873732019-12-05 20:28:46 +01002220 // Add the name to the list
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002221 ga_concat(ga, (char_u *)server);
2222 ga_concat(ga, (char_u *)"\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 return TRUE;
2224}
2225
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002226struct enum_windows_s
2227{
2228 WNDENUMPROC lpEnumFunc;
2229 LPARAM lParam;
2230};
2231
2232 static BOOL CALLBACK
2233enum_windows_child(HWND hwnd, LPARAM lParam)
2234{
2235 struct enum_windows_s *ew = (struct enum_windows_s *)lParam;
2236
2237 return (ew->lpEnumFunc)(hwnd, ew->lParam);
2238}
2239
2240 static BOOL CALLBACK
2241enum_windows_toplevel(HWND hwnd, LPARAM lParam)
2242{
2243 struct enum_windows_s *ew = (struct enum_windows_s *)lParam;
2244
Bram Moolenaar95ba5c32018-10-07 22:47:07 +02002245 if ((ew->lpEnumFunc)(hwnd, ew->lParam))
2246 return TRUE;
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002247 return EnumChildWindows(hwnd, enum_windows_child, lParam);
2248}
2249
Bram Moolenaar0f873732019-12-05 20:28:46 +01002250/*
2251 * Enumerate all windows including children.
2252 */
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002253 static BOOL
2254enum_windows(WNDENUMPROC lpEnumFunc, LPARAM lParam)
2255{
2256 struct enum_windows_s ew;
2257
2258 ew.lpEnumFunc = lpEnumFunc;
2259 ew.lParam = lParam;
2260 return EnumWindows(enum_windows_toplevel, (LPARAM)&ew);
2261}
2262
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 static HWND
2264findServer(char_u *name)
2265{
2266 struct server_id id;
2267
2268 id.name = name;
2269 id.hwnd = 0;
2270
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002271 enum_windows(enumWindowsGetServer, (LPARAM)(&id));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272
2273 return id.hwnd;
2274}
2275
2276 void
2277serverSetName(char_u *name)
2278{
2279 char_u *ok_name;
2280 HWND hwnd = 0;
2281 int i = 0;
2282 char_u *p;
2283
Bram Moolenaar0f873732019-12-05 20:28:46 +01002284 // Leave enough space for a 9-digit suffix to ensure uniqueness!
Bram Moolenaar964b3742019-05-24 18:54:09 +02002285 ok_name = alloc(STRLEN(name) + 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286
2287 STRCPY(ok_name, name);
2288 p = ok_name + STRLEN(name);
2289
2290 for (;;)
2291 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002292 // This is inefficient - we're doing an EnumWindows loop for each
2293 // possible name. It would be better to grab all names in one go,
2294 // and scan the list each time...
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 hwnd = findServer(ok_name);
2296 if (hwnd == 0)
2297 break;
2298
2299 ++i;
2300 if (i >= 1000)
2301 break;
2302
2303 sprintf((char *)p, "%d", i);
2304 }
2305
2306 if (hwnd != 0)
2307 vim_free(ok_name);
2308 else
2309 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002310 // Remember the name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 serverName = ok_name;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002312# ifdef FEAT_TITLE
Bram Moolenaar0f873732019-12-05 20:28:46 +01002313 need_maketitle = TRUE; // update Vim window title later
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002314# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315
Bram Moolenaar0f873732019-12-05 20:28:46 +01002316 // Update the message window title
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002317 SetWindowText(message_window, (LPCSTR)ok_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002319# ifdef FEAT_EVAL
Bram Moolenaar0f873732019-12-05 20:28:46 +01002320 // Set the servername variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 set_vim_var_string(VV_SEND_SERVER, serverName, -1);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002322# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 }
2324}
2325
2326 char_u *
2327serverGetVimNames(void)
2328{
2329 garray_T ga;
2330
2331 ga_init2(&ga, 1, 100);
2332
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002333 enum_windows(enumWindowsGetNames, (LPARAM)(&ga));
Bram Moolenaar269ec652004-07-29 08:43:53 +00002334 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335
2336 return ga.ga_data;
2337}
2338
2339 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002340serverSendReply(
Bram Moolenaar0f873732019-12-05 20:28:46 +01002341 char_u *name, // Where to send.
2342 char_u *reply) // What to send.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343{
2344 HWND target;
2345 COPYDATASTRUCT data;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002346 long_u n = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347
Bram Moolenaar0f873732019-12-05 20:28:46 +01002348 // The "name" argument is a magic cookie obtained from expand("<client>").
2349 // It should be of the form 0xXXXXX - i.e. a C hex literal, which is the
2350 // value of the client's message window HWND.
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002351 sscanf((char *)name, SCANF_HEX_LONG_U, &n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 if (n == 0)
2353 return -1;
2354
2355 target = (HWND)n;
2356 if (!IsWindow(target))
2357 return -1;
2358
2359 data.dwData = COPYDATA_REPLY;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002360 data.cbData = (DWORD)STRLEN(reply) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 data.lpData = reply;
2362
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002363 serverSendEnc(target);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 if (SendMessage(target, WM_COPYDATA, (WPARAM)message_window,
2365 (LPARAM)(&data)))
2366 return 0;
2367
2368 return -1;
2369}
2370
2371 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002372serverSendToVim(
Bram Moolenaar0f873732019-12-05 20:28:46 +01002373 char_u *name, // Where to send.
2374 char_u *cmd, // What to send.
2375 char_u **result, // Result of eval'ed expression
2376 void *ptarget, // HWND of server
2377 int asExpr, // Expression or keys?
2378 int timeout, // timeout in seconds or zero
2379 int silent) // don't complain about no server
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380{
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002381 HWND target;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 COPYDATASTRUCT data;
2383 char_u *retval = NULL;
2384 int retcode = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002385 char_u altname_buf[MAX_PATH];
2386
Bram Moolenaar0f873732019-12-05 20:28:46 +01002387 // Execute locally if no display or target is ourselves
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01002388 if (serverName != NULL && STRICMP(name, serverName) == 0)
2389 return sendToLocalVim(cmd, asExpr, result);
2390
Bram Moolenaar0f873732019-12-05 20:28:46 +01002391 // If the server name does not end in a digit then we look for an
2392 // alternate name. e.g. when "name" is GVIM the we may find GVIM2.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002393 if (STRLEN(name) > 1 && !vim_isdigit(name[STRLEN(name) - 1]))
2394 altname_buf_ptr = altname_buf;
2395 altname_buf[0] = NUL;
2396 target = findServer(name);
2397 altname_buf_ptr = NULL;
2398 if (target == 0 && altname_buf[0] != NUL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01002399 // Use another server name we found.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002400 target = findServer(altname_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401
2402 if (target == 0)
2403 {
2404 if (!silent)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002405 semsg(_(e_noserver), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 return -1;
2407 }
2408
2409 if (ptarget)
2410 *(HWND *)ptarget = target;
2411
2412 data.dwData = asExpr ? COPYDATA_EXPR : COPYDATA_KEYS;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002413 data.cbData = (DWORD)STRLEN(cmd) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 data.lpData = cmd;
2415
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002416 serverSendEnc(target);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 if (SendMessage(target, WM_COPYDATA, (WPARAM)message_window,
2418 (LPARAM)(&data)) == 0)
2419 return -1;
2420
2421 if (asExpr)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002422 retval = serverGetReply(target, &retcode, TRUE, TRUE, timeout);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423
2424 if (result == NULL)
2425 vim_free(retval);
2426 else
Bram Moolenaar0f873732019-12-05 20:28:46 +01002427 *result = retval; // Caller assumes responsibility for freeing
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428
2429 return retcode;
2430}
2431
2432/*
2433 * Bring the server to the foreground.
2434 */
2435 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002436serverForeground(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437{
2438 HWND target = findServer(name);
2439
2440 if (target != 0)
2441 SetForegroundWindow(target);
2442}
2443
Bram Moolenaar0f873732019-12-05 20:28:46 +01002444// Replies from server need to be stored until the client picks them up via
2445// remote_read(). So we maintain a list of server-id/reply pairs.
2446// Note that there could be multiple replies from one server pending if the
2447// client is slow picking them up.
2448// We just store the replies in a simple list. When we remove an entry, we
2449// move list entries down to fill the gap.
2450// The server ID is simply the HWND.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451typedef struct
2452{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002453 HWND server; // server window
2454 char_u *reply; // reply string
2455 int expr_result; // 0 for REPLY, 1 for RESULT 2 for error
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002456} reply_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457
2458static garray_T reply_list = {0, 0, sizeof(reply_T), 5, 0};
2459
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002460# define REPLY_ITEM(i) ((reply_T *)(reply_list.ga_data) + (i))
2461# define REPLY_COUNT (reply_list.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462
Bram Moolenaar0f873732019-12-05 20:28:46 +01002463// Flag which is used to wait for a reply
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464static int reply_received = 0;
2465
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002466/*
2467 * Store a reply. "reply" must be allocated memory (or NULL).
2468 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 static int
2470save_reply(HWND server, char_u *reply, int expr)
2471{
2472 reply_T *rep;
2473
2474 if (ga_grow(&reply_list, 1) == FAIL)
2475 return FAIL;
2476
2477 rep = REPLY_ITEM(REPLY_COUNT);
2478 rep->server = server;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002479 rep->reply = reply;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 rep->expr_result = expr;
2481 if (rep->reply == NULL)
2482 return FAIL;
2483
2484 ++REPLY_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 reply_received = 1;
2486 return OK;
2487}
2488
2489/*
2490 * Get a reply from server "server".
2491 * When "expr_res" is non NULL, get the result of an expression, otherwise a
2492 * server2client() message.
2493 * When non NULL, point to return code. 0 => OK, -1 => ERROR
2494 * If "remove" is TRUE, consume the message, the caller must free it then.
2495 * if "wait" is TRUE block until a message arrives (or the server exits).
2496 */
2497 char_u *
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002498serverGetReply(HWND server, int *expr_res, int remove, int wait, int timeout)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499{
2500 int i;
2501 char_u *reply;
2502 reply_T *rep;
Bram Moolenaar15e737f2017-03-18 21:22:47 +01002503 int did_process = FALSE;
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002504 time_t start;
2505 time_t now;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506
Bram Moolenaar0f873732019-12-05 20:28:46 +01002507 // When waiting, loop until the message waiting for is received.
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002508 time(&start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 for (;;)
2510 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002511 // Reset this here, in case a message arrives while we are going
2512 // through the already received messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 reply_received = 0;
2514
2515 for (i = 0; i < REPLY_COUNT; ++i)
2516 {
2517 rep = REPLY_ITEM(i);
2518 if (rep->server == server
2519 && ((rep->expr_result != 0) == (expr_res != NULL)))
2520 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002521 // Save the values we've found for later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 reply = rep->reply;
2523 if (expr_res != NULL)
2524 *expr_res = rep->expr_result == 1 ? 0 : -1;
2525
2526 if (remove)
2527 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002528 // Move the rest of the list down to fill the gap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 mch_memmove(rep, rep + 1,
2530 (REPLY_COUNT - i - 1) * sizeof(reply_T));
2531 --REPLY_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 }
2533
Bram Moolenaar0f873732019-12-05 20:28:46 +01002534 // Return the reply to the caller, who takes on responsibility
2535 // for freeing it if "remove" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 return reply;
2537 }
2538 }
2539
Bram Moolenaar0f873732019-12-05 20:28:46 +01002540 // If we got here, we didn't find a reply. Return immediately if the
2541 // "wait" parameter isn't set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 if (!wait)
Bram Moolenaar15e737f2017-03-18 21:22:47 +01002543 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002544 // Process pending messages once. Without this, looping on
2545 // remote_peek() would never get the reply.
Bram Moolenaar15e737f2017-03-18 21:22:47 +01002546 if (!did_process)
2547 {
2548 did_process = TRUE;
2549 serverProcessPendingMessages();
2550 continue;
2551 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 break;
Bram Moolenaar15e737f2017-03-18 21:22:47 +01002553 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554
Bram Moolenaar0f873732019-12-05 20:28:46 +01002555 // We need to wait for a reply. Enter a message loop until the
2556 // "reply_received" flag gets set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557
Bram Moolenaar0f873732019-12-05 20:28:46 +01002558 // Loop until we receive a reply
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 while (reply_received == 0)
2560 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002561# ifdef FEAT_TIMERS
Bram Moolenaar0f873732019-12-05 20:28:46 +01002562 // TODO: use the return value to decide how long to wait.
Bram Moolenaar42205552017-03-18 19:42:22 +01002563 check_due_timer();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002564# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002565 time(&now);
2566 if (timeout > 0 && (now - start) >= timeout)
2567 break;
2568
Bram Moolenaar0f873732019-12-05 20:28:46 +01002569 // Wait for a SendMessage() call to us. This could be the reply
2570 // we are waiting for. Use a timeout of a second, to catch the
2571 // situation that the server died unexpectedly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 MsgWaitForMultipleObjects(0, NULL, TRUE, 1000, QS_ALLINPUT);
2573
Bram Moolenaar0f873732019-12-05 20:28:46 +01002574 // If the server has died, give up
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 if (!IsWindow(server))
2576 return NULL;
2577
2578 serverProcessPendingMessages();
2579 }
2580 }
2581
2582 return NULL;
2583}
2584
2585/*
2586 * Process any messages in the Windows message queue.
2587 */
2588 void
2589serverProcessPendingMessages(void)
2590{
2591 MSG msg;
2592
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02002593 while (pPeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 {
2595 TranslateMessage(&msg);
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02002596 pDispatchMessage(&msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 }
2598}
2599
Bram Moolenaar0f873732019-12-05 20:28:46 +01002600#endif // FEAT_CLIENTSERVER
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601
2602#if defined(FEAT_GUI) || (defined(FEAT_PRINTER) && !defined(FEAT_POSTSCRIPT)) \
2603 || defined(PROTO)
2604
2605struct charset_pair
2606{
2607 char *name;
2608 BYTE charset;
2609};
2610
2611static struct charset_pair
2612charset_pairs[] =
2613{
2614 {"ANSI", ANSI_CHARSET},
2615 {"CHINESEBIG5", CHINESEBIG5_CHARSET},
2616 {"DEFAULT", DEFAULT_CHARSET},
2617 {"HANGEUL", HANGEUL_CHARSET},
2618 {"OEM", OEM_CHARSET},
2619 {"SHIFTJIS", SHIFTJIS_CHARSET},
2620 {"SYMBOL", SYMBOL_CHARSET},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 {"ARABIC", ARABIC_CHARSET},
2622 {"BALTIC", BALTIC_CHARSET},
2623 {"EASTEUROPE", EASTEUROPE_CHARSET},
2624 {"GB2312", GB2312_CHARSET},
2625 {"GREEK", GREEK_CHARSET},
2626 {"HEBREW", HEBREW_CHARSET},
2627 {"JOHAB", JOHAB_CHARSET},
2628 {"MAC", MAC_CHARSET},
2629 {"RUSSIAN", RUSSIAN_CHARSET},
2630 {"THAI", THAI_CHARSET},
2631 {"TURKISH", TURKISH_CHARSET},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002632# ifdef VIETNAMESE_CHARSET
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 {"VIETNAMESE", VIETNAMESE_CHARSET},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002634# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 {NULL, 0}
2636};
2637
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002638struct quality_pair
2639{
2640 char *name;
2641 DWORD quality;
2642};
2643
2644static struct quality_pair
2645quality_pairs[] = {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002646# ifdef CLEARTYPE_QUALITY
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002647 {"CLEARTYPE", CLEARTYPE_QUALITY},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002648# endif
2649# ifdef ANTIALIASED_QUALITY
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002650 {"ANTIALIASED", ANTIALIASED_QUALITY},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002651# endif
2652# ifdef NONANTIALIASED_QUALITY
Bram Moolenaar73a733e2016-05-11 21:05:05 +02002653 {"NONANTIALIASED", NONANTIALIASED_QUALITY},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002654# endif
2655# ifdef PROOF_QUALITY
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002656 {"PROOF", PROOF_QUALITY},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002657# endif
2658# ifdef DRAFT_QUALITY
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002659 {"DRAFT", DRAFT_QUALITY},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002660# endif
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002661 {"DEFAULT", DEFAULT_QUALITY},
2662 {NULL, 0}
2663};
2664
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665/*
2666 * Convert a charset ID to a name.
2667 * Return NULL when not recognized.
2668 */
2669 char *
2670charset_id2name(int id)
2671{
2672 struct charset_pair *cp;
2673
2674 for (cp = charset_pairs; cp->name != NULL; ++cp)
2675 if ((BYTE)id == cp->charset)
2676 break;
2677 return cp->name;
2678}
2679
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002680/*
2681 * Convert a quality ID to a name.
2682 * Return NULL when not recognized.
2683 */
2684 char *
2685quality_id2name(DWORD id)
2686{
2687 struct quality_pair *qp;
2688
2689 for (qp = quality_pairs; qp->name != NULL; ++qp)
2690 if (id == qp->quality)
2691 break;
2692 return qp->name;
2693}
2694
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002695static const LOGFONTW s_lfDefault =
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696{
2697 -12, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, DEFAULT_CHARSET,
2698 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
2699 PROOF_QUALITY, FIXED_PITCH | FF_DONTCARE,
Bram Moolenaar0f873732019-12-05 20:28:46 +01002700 L"Fixedsys" // see _ReadVimIni
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701};
2702
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002703// Initialise the "current height" to -12 (same as s_lfDefault) just
2704// in case the user specifies a font in "guifont" with no size before a font
2705// with an explicit size has been set. This defaults the size to this value
2706// (-12 equates to roughly 9pt).
2707int current_font_height = -12; // also used in gui_w32.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708
Bram Moolenaar0f873732019-12-05 20:28:46 +01002709/*
2710 * Convert a string representing a point size into pixels. The string should
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 * be a positive decimal number, with an optional decimal point (eg, "12", or
2712 * "10.5"). The pixel value is returned, and a pointer to the next unconverted
2713 * character is stored in *end. The flag "vertical" says whether this
2714 * calculation is for a vertical (height) size or a horizontal (width) one.
2715 */
2716 static int
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002717points_to_pixels(WCHAR *str, WCHAR **end, int vertical, long_i pprinter_dc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718{
2719 int pixels;
2720 int points = 0;
2721 int divisor = 0;
2722 HWND hwnd = (HWND)0;
2723 HDC hdc;
2724 HDC printer_dc = (HDC)pprinter_dc;
2725
2726 while (*str != NUL)
2727 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002728 if (*str == L'.' && divisor == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002730 // Start keeping a divisor, for later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 divisor = 1;
2732 }
2733 else
2734 {
2735 if (!VIM_ISDIGIT(*str))
2736 break;
2737
2738 points *= 10;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002739 points += *str - L'0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 divisor *= 10;
2741 }
2742 ++str;
2743 }
2744
2745 if (divisor == 0)
2746 divisor = 1;
2747
2748 if (printer_dc == NULL)
2749 {
2750 hwnd = GetDesktopWindow();
2751 hdc = GetWindowDC(hwnd);
2752 }
2753 else
2754 hdc = printer_dc;
2755
2756 pixels = MulDiv(points,
2757 GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX),
2758 72 * divisor);
2759
2760 if (printer_dc == NULL)
2761 ReleaseDC(hwnd, hdc);
2762
2763 *end = str;
2764 return pixels;
2765}
2766
2767 static int CALLBACK
2768font_enumproc(
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002769 ENUMLOGFONTW *elf,
2770 NEWTEXTMETRICW *ntm UNUSED,
2771 DWORD type UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 LPARAM lparam)
2773{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002774 // Return value:
2775 // 0 = terminate now (monospace & ANSI)
2776 // 1 = continue, still no luck...
2777 // 2 = continue, but we have an acceptable LOGFONTW
2778 // (monospace, not ANSI)
2779 // We use these values, as EnumFontFamilies returns 1 if the
2780 // callback function is never called. So, we check the return as
2781 // 0 = perfect, 2 = OK, 1 = no good...
2782 // It's not pretty, but it works!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002784 LOGFONTW *lf = (LOGFONTW *)(lparam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002786# ifndef FEAT_PROPORTIONAL_FONTS
Bram Moolenaar0f873732019-12-05 20:28:46 +01002787 // Ignore non-monospace fonts without further ado
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 if ((ntm->tmPitchAndFamily & 1) != 0)
2789 return 1;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002790# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791
Bram Moolenaar0f873732019-12-05 20:28:46 +01002792 // Remember this LOGFONTW as a "possible"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 *lf = elf->elfLogFont;
2794
Bram Moolenaar0f873732019-12-05 20:28:46 +01002795 // Terminate the scan as soon as we find an ANSI font
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 if (lf->lfCharSet == ANSI_CHARSET
2797 || lf->lfCharSet == OEM_CHARSET
2798 || lf->lfCharSet == DEFAULT_CHARSET)
2799 return 0;
2800
Bram Moolenaar0f873732019-12-05 20:28:46 +01002801 // Continue the scan - we have a non-ANSI font
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 return 2;
2803}
2804
2805 static int
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002806init_logfont(LOGFONTW *lf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807{
2808 int n;
2809 HWND hwnd = GetDesktopWindow();
2810 HDC hdc = GetWindowDC(hwnd);
2811
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002812 n = EnumFontFamiliesW(hdc,
2813 lf->lfFaceName,
2814 (FONTENUMPROCW)font_enumproc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 (LPARAM)lf);
2816
2817 ReleaseDC(hwnd, hdc);
2818
Bram Moolenaar0f873732019-12-05 20:28:46 +01002819 // If we couldn't find a usable font, return failure
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820 if (n == 1)
2821 return FAIL;
2822
Bram Moolenaar0f873732019-12-05 20:28:46 +01002823 // Tidy up the rest of the LOGFONTW structure. We set to a basic
2824 // font - get_logfont() sets bold, italic, etc based on the user's
2825 // input.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 lf->lfHeight = current_font_height;
2827 lf->lfWidth = 0;
2828 lf->lfItalic = FALSE;
2829 lf->lfUnderline = FALSE;
2830 lf->lfStrikeOut = FALSE;
2831 lf->lfWeight = FW_NORMAL;
2832
Bram Moolenaar0f873732019-12-05 20:28:46 +01002833 // Return success
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 return OK;
2835}
2836
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002837/*
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002838 * Compare a UTF-16 string and an ASCII string literally.
2839 * Only works all the code points are inside ASCII range.
2840 */
2841 static int
2842utf16ascncmp(const WCHAR *w, const char *p, size_t n)
2843{
2844 size_t i;
2845
2846 for (i = 0; i < n; i++)
2847 {
2848 if (w[i] == 0 || w[i] != p[i])
2849 return w[i] - p[i];
2850 }
2851 return 0;
2852}
2853
2854/*
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002855 * Get font info from "name" into logfont "lf".
2856 * Return OK for a valid name, FAIL otherwise.
2857 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 int
2859get_logfont(
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002860 LOGFONTW *lf,
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002861 char_u *name,
2862 HDC printer_dc,
2863 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002865 WCHAR *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 int i;
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002867 int ret = FAIL;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002868 static LOGFONTW *lastlf = NULL;
2869 WCHAR *wname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870
2871 *lf = s_lfDefault;
2872 if (name == NULL)
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002873 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002875 wname = enc_to_utf16(name, NULL);
2876 if (wname == NULL)
2877 return FAIL;
2878
2879 if (wcscmp(wname, L"*") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002881# if defined(FEAT_GUI_MSWIN)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002882 CHOOSEFONTW cf;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002883 // if name is "*", bring up std font dialog:
Bram Moolenaara80faa82020-04-12 19:37:17 +02002884 CLEAR_FIELD(cf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 cf.lStructSize = sizeof(cf);
2886 cf.hwndOwner = s_hwnd;
2887 cf.Flags = CF_SCREENFONTS | CF_FIXEDPITCHONLY | CF_INITTOLOGFONTSTRUCT;
2888 if (lastlf != NULL)
2889 *lf = *lastlf;
2890 cf.lpLogFont = lf;
2891 cf.nFontType = 0 ; //REGULAR_FONTTYPE;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002892 if (ChooseFontW(&cf))
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002893 ret = OK;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002894# endif
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002895 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 }
2897
2898 /*
2899 * Split name up, it could be <name>:h<height>:w<width> etc.
2900 */
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002901 for (p = wname; *p && *p != L':'; p++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002903 if (p - wname + 1 >= LF_FACESIZE)
Bram Moolenaar0f873732019-12-05 20:28:46 +01002904 goto theend; // Name too long
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002905 lf->lfFaceName[p - wname] = *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 }
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002907 if (p != wname)
2908 lf->lfFaceName[p - wname] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909
Bram Moolenaar0f873732019-12-05 20:28:46 +01002910 // First set defaults
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 lf->lfHeight = -12;
2912 lf->lfWidth = 0;
2913 lf->lfWeight = FW_NORMAL;
2914 lf->lfItalic = FALSE;
2915 lf->lfUnderline = FALSE;
2916 lf->lfStrikeOut = FALSE;
2917
2918 /*
2919 * If the font can't be found, try replacing '_' by ' '.
2920 */
2921 if (init_logfont(lf) == FAIL)
2922 {
2923 int did_replace = FALSE;
2924
2925 for (i = 0; lf->lfFaceName[i]; ++i)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002926 if (lf->lfFaceName[i] == L'_')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002928 lf->lfFaceName[i] = L' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929 did_replace = TRUE;
2930 }
2931 if (!did_replace || init_logfont(lf) == FAIL)
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002932 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 }
2934
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002935 while (*p == L':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936 p++;
2937
Bram Moolenaar0f873732019-12-05 20:28:46 +01002938 // Set the values found after ':'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 while (*p)
2940 {
2941 switch (*p++)
2942 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002943 case L'h':
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002944 lf->lfHeight = - points_to_pixels(p, &p, TRUE, (long_i)printer_dc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002946 case L'w':
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002947 lf->lfWidth = points_to_pixels(p, &p, FALSE, (long_i)printer_dc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948 break;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02002949 case L'W':
2950 lf->lfWeight = wcstol(p, &p, 10);
2951 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002952 case L'b':
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 lf->lfWeight = FW_BOLD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002955 case L'i':
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 lf->lfItalic = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002958 case L'u':
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 lf->lfUnderline = TRUE;
2960 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002961 case L's':
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962 lf->lfStrikeOut = TRUE;
2963 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002964 case L'c':
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965 {
2966 struct charset_pair *cp;
2967
2968 for (cp = charset_pairs; cp->name != NULL; ++cp)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002969 if (utf16ascncmp(p, cp->name, strlen(cp->name)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 {
2971 lf->lfCharSet = cp->charset;
2972 p += strlen(cp->name);
2973 break;
2974 }
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002975 if (cp->name == NULL && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002977 char_u *s = utf16_to_enc(p, NULL);
2978 semsg(_("E244: Illegal charset name \"%s\" in font name \"%s\""), s, name);
2979 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 break;
2981 }
2982 break;
2983 }
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002984 case L'q':
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002985 {
2986 struct quality_pair *qp;
2987
2988 for (qp = quality_pairs; qp->name != NULL; ++qp)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002989 if (utf16ascncmp(p, qp->name, strlen(qp->name)) == 0)
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002990 {
2991 lf->lfQuality = qp->quality;
2992 p += strlen(qp->name);
2993 break;
2994 }
2995 if (qp->name == NULL && verbose)
2996 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002997 char_u *s = utf16_to_enc(p, NULL);
2998 semsg(_("E244: Illegal quality name \"%s\" in font name \"%s\""), s, name);
2999 vim_free(s);
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003000 break;
3001 }
3002 break;
3003 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004 default:
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003005 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003006 semsg(_("E245: Illegal char '%c' in font name \"%s\""), p[-1], name);
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003007 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008 }
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003009 while (*p == L':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010 p++;
3011 }
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003012 ret = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014theend:
Bram Moolenaar0f873732019-12-05 20:28:46 +01003015 // ron: init lastlf
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003016 if (ret == OK && printer_dc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017 {
3018 vim_free(lastlf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003019 lastlf = ALLOC_ONE(LOGFONTW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 if (lastlf != NULL)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003021 mch_memmove(lastlf, lf, sizeof(LOGFONTW));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 }
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003023 vim_free(wname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003025 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026}
3027
Bram Moolenaar0f873732019-12-05 20:28:46 +01003028#endif // defined(FEAT_GUI) || defined(FEAT_PRINTER)
Bram Moolenaarf12d9832016-01-29 21:11:25 +01003029
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003030#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaarf12d9832016-01-29 21:11:25 +01003031/*
3032 * Initialize the Winsock dll.
3033 */
3034 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003035channel_init_winsock(void)
Bram Moolenaarf12d9832016-01-29 21:11:25 +01003036{
3037 WSADATA wsaData;
3038 int wsaerr;
3039
3040 if (WSInitialized)
3041 return;
3042
3043 wsaerr = WSAStartup(MAKEWORD(2, 2), &wsaData);
3044 if (wsaerr == 0)
3045 WSInitialized = TRUE;
3046}
3047#endif