blob: c7cfd27613570eca721e68400506e449e38dbebc [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 Moolenaar1266d672017-02-01 13:43:36 +0100556mch_settmode(int 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
745/*
746 * set screen mode, always fails.
747 */
748 int
Bram Moolenaar1266d672017-02-01 13:43:36 +0100749mch_screenmode(char_u *arg UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100751 emsg(_(e_screenmode));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 return FAIL;
753}
754
755
756#if defined(FEAT_LIBCALL) || defined(PROTO)
757/*
758 * Call a DLL routine which takes either a string or int param
759 * and returns an allocated string.
760 * Return OK if it worked, FAIL if not.
761 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762typedef LPTSTR (*MYSTRPROCSTR)(LPTSTR);
763typedef LPTSTR (*MYINTPROCSTR)(int);
764typedef int (*MYSTRPROCINT)(LPTSTR);
765typedef int (*MYINTPROCINT)(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767/*
768 * Check if a pointer points to a valid NUL terminated string.
769 * Return the length of the string, including terminating NUL.
770 * Returns 0 for an invalid pointer, 1 for an empty string.
771 */
772 static size_t
773check_str_len(char_u *str)
774{
775 SYSTEM_INFO si;
776 MEMORY_BASIC_INFORMATION mbi;
777 size_t length = 0;
778 size_t i;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100779 const char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780
Bram Moolenaar0f873732019-12-05 20:28:46 +0100781 // get page size
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 GetSystemInfo(&si);
783
Bram Moolenaar0f873732019-12-05 20:28:46 +0100784 // get memory information
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 if (VirtualQuery(str, &mbi, sizeof(mbi)))
786 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100787 // pre cast these (typing savers)
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000788 long_u dwStr = (long_u)str;
789 long_u dwBaseAddress = (long_u)mbi.BaseAddress;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790
Bram Moolenaar0f873732019-12-05 20:28:46 +0100791 // get start address of page that str is on
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000792 long_u strPage = dwStr - (dwStr - dwBaseAddress) % si.dwPageSize;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793
Bram Moolenaar0f873732019-12-05 20:28:46 +0100794 // get length from str to end of page
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000795 long_u pageLength = si.dwPageSize - (dwStr - strPage);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796
Bram Moolenaar442b4222010-05-24 21:34:22 +0200797 for (p = str; !IsBadReadPtr(p, (UINT)pageLength);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 p += pageLength, pageLength = si.dwPageSize)
799 for (i = 0; i < pageLength; ++i, ++length)
800 if (p[i] == NUL)
801 return length + 1;
802 }
803
804 return 0;
805}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200807/*
808 * Passed to do_in_runtimepath() to load a vim.ico file.
809 */
810 static void
811mch_icon_load_cb(char_u *fname, void *cookie)
812{
813 HANDLE *h = (HANDLE *)cookie;
814
815 *h = LoadImage(NULL,
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100816 (LPSTR)fname,
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200817 IMAGE_ICON,
818 64,
819 64,
820 LR_LOADFROMFILE | LR_LOADMAP3DCOLORS);
821}
822
823/*
824 * Try loading an icon file from 'runtimepath'.
825 */
826 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100827mch_icon_load(HANDLE *iconp)
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200828{
829 return do_in_runtimepath((char_u *)"bitmaps/vim.ico",
Bram Moolenaar7f8989d2016-03-12 22:11:39 +0100830 0, mch_icon_load_cb, iconp);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200831}
832
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 int
834mch_libcall(
835 char_u *libname,
836 char_u *funcname,
Bram Moolenaar0f873732019-12-05 20:28:46 +0100837 char_u *argstring, // NULL when using a argint
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 int argint,
Bram Moolenaar0f873732019-12-05 20:28:46 +0100839 char_u **string_result,// NULL when using number_result
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 int *number_result)
841{
842 HINSTANCE hinstLib;
843 MYSTRPROCSTR ProcAdd;
844 MYINTPROCSTR ProcAddI;
845 char_u *retval_str = NULL;
846 int retval_int = 0;
847 size_t len;
848
849 BOOL fRunTimeLinkSuccess = FALSE;
850
851 // Get a handle to the DLL module.
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100852 hinstLib = vimLoadLib((char *)libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853
854 // If the handle is valid, try to get the function address.
855 if (hinstLib != NULL)
856 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100857# ifdef HAVE_TRY_EXCEPT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 __try
859 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100860# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 if (argstring != NULL)
862 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100863 // Call with string argument
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100864 ProcAdd = (MYSTRPROCSTR)GetProcAddress(hinstLib, (LPCSTR)funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865 if ((fRunTimeLinkSuccess = (ProcAdd != NULL)) != 0)
866 {
867 if (string_result == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100868 retval_int = ((MYSTRPROCINT)ProcAdd)((LPSTR)argstring);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100870 retval_str = (char_u *)(ProcAdd)((LPSTR)argstring);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871 }
872 }
873 else
874 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100875 // Call with number argument
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100876 ProcAddI = (MYINTPROCSTR) GetProcAddress(hinstLib, (LPCSTR)funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877 if ((fRunTimeLinkSuccess = (ProcAddI != NULL)) != 0)
878 {
879 if (string_result == NULL)
880 retval_int = ((MYINTPROCINT)ProcAddI)(argint);
881 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100882 retval_str = (char_u *)(ProcAddI)(argint);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 }
884 }
885
886 // Save the string before we free the library.
887 // Assume that a "1" result is an illegal pointer.
888 if (string_result == NULL)
889 *number_result = retval_int;
890 else if (retval_str != NULL
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100891 && (len = check_str_len(retval_str)) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 {
Bram Moolenaar18a4ba22019-05-24 19:39:03 +0200893 *string_result = alloc(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 if (*string_result != NULL)
895 mch_memmove(*string_result, retval_str, len);
896 }
897
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100898# ifdef HAVE_TRY_EXCEPT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 }
900 __except(EXCEPTION_EXECUTE_HANDLER)
901 {
902 if (GetExceptionCode() == EXCEPTION_STACK_OVERFLOW)
903 RESETSTKOFLW();
904 fRunTimeLinkSuccess = 0;
905 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100906# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907
908 // Free the DLL module.
909 (void)FreeLibrary(hinstLib);
910 }
911
912 if (!fRunTimeLinkSuccess)
913 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100914 semsg(_(e_libcall), funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 return FAIL;
916 }
917
918 return OK;
919}
920#endif
921
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922/*
923 * Debugging helper: expose the MCH_WRITE_DUMP stuff to other modules
924 */
925 void
Bram Moolenaar1266d672017-02-01 13:43:36 +0100926DumpPutS(const char *psz UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100928#ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 if (fdDump)
930 {
931 fputs(psz, fdDump);
932 if (psz[strlen(psz) - 1] != '\n')
933 fputc('\n', fdDump);
934 fflush(fdDump);
935 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100936#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937}
938
939#ifdef _DEBUG
940
941void __cdecl
942Trace(
943 char *pszFormat,
944 ...)
945{
946 CHAR szBuff[2048];
947 va_list args;
948
949 va_start(args, pszFormat);
950 vsprintf(szBuff, pszFormat, args);
951 va_end(args);
952
953 OutputDebugString(szBuff);
954}
955
956#endif //_DEBUG
957
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200958#if !defined(FEAT_GUI) || defined(VIMDLL) || defined(PROTO)
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200959# ifdef FEAT_TITLE
Bram Moolenaar0f873732019-12-05 20:28:46 +0100960extern HWND g_hWnd; // This is in os_win32.c.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961# endif
962
963/*
964 * Showing the printer dialog is tricky since we have no GUI
965 * window to parent it. The following routines are needed to
966 * get the window parenting and Z-order to work properly.
967 */
968 static void
969GetConsoleHwnd(void)
970{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100971 // Skip if it's already set.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972 if (s_hwnd != 0)
973 return;
974
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200975# ifdef FEAT_TITLE
Bram Moolenaar0f873732019-12-05 20:28:46 +0100976 // Window handle may have been found by init code (Windows NT only)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 if (g_hWnd != 0)
978 {
979 s_hwnd = g_hWnd;
980 return;
981 }
982# endif
983
Bram Moolenaare1ed53f2019-02-12 23:12:37 +0100984 s_hwnd = GetConsoleWindow();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985}
Bram Moolenaar843ee412004-06-30 16:16:41 +0000986
987/*
988 * Console implementation of ":winpos".
989 */
990 int
991mch_get_winpos(int *x, int *y)
992{
993 RECT rect;
994
995 GetConsoleHwnd();
996 GetWindowRect(s_hwnd, &rect);
997 *x = rect.left;
998 *y = rect.top;
999 return OK;
1000}
1001
1002/*
1003 * Console implementation of ":winpos x y".
1004 */
1005 void
1006mch_set_winpos(int x, int y)
1007{
1008 GetConsoleHwnd();
1009 SetWindowPos(s_hwnd, NULL, x, y, 0, 0,
1010 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
1011}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012#endif
1013
1014#if (defined(FEAT_PRINTER) && !defined(FEAT_POSTSCRIPT)) || defined(PROTO)
1015
Bram Moolenaar0f873732019-12-05 20:28:46 +01001016//=================================================================
1017// Win32 printer stuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018
1019static HFONT prt_font_handles[2][2][2];
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001020static PRINTDLGW prt_dlg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021static const int boldface[2] = {FW_REGULAR, FW_BOLD};
1022static TEXTMETRIC prt_tm;
1023static int prt_line_height;
1024static int prt_number_width;
1025static int prt_left_margin;
1026static int prt_right_margin;
1027static int prt_top_margin;
1028static char_u szAppName[] = TEXT("VIM");
1029static HWND hDlgPrint;
1030static int *bUserAbort = NULL;
1031static char_u *prt_name = NULL;
1032
Bram Moolenaar0f873732019-12-05 20:28:46 +01001033// Defines which are also in vim.rc.
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001034# define IDC_BOX1 400
1035# define IDC_PRINTTEXT1 401
1036# define IDC_PRINTTEXT2 402
1037# define IDC_PROGRESS 403
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001039 static BOOL
1040vimSetDlgItemText(HWND hDlg, int nIDDlgItem, char_u *s)
1041{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001042 WCHAR *wp;
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001043 BOOL ret;
1044
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001045 wp = enc_to_utf16(s, NULL);
1046 if (wp == NULL)
1047 return FALSE;
1048
1049 ret = SetDlgItemTextW(hDlg, nIDDlgItem, wp);
1050 vim_free(wp);
1051 return ret;
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001052}
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001053
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054/*
1055 * Convert BGR to RGB for Windows GDI calls
1056 */
1057 static COLORREF
1058swap_me(COLORREF colorref)
1059{
1060 int temp;
1061 char *ptr = (char *)&colorref;
1062
1063 temp = *(ptr);
1064 *(ptr ) = *(ptr + 2);
1065 *(ptr + 2) = temp;
1066 return colorref;
1067}
1068
Bram Moolenaar0f873732019-12-05 20:28:46 +01001069// Attempt to make this work for old and new compilers
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001070# if !defined(_WIN64) && (!defined(_MSC_VER) || _MSC_VER < 1300)
1071# define PDP_RETVAL BOOL
1072# else
1073# define PDP_RETVAL INT_PTR
1074# endif
Bram Moolenaared39e1d2008-08-09 17:55:22 +00001075
Bram Moolenaared39e1d2008-08-09 17:55:22 +00001076 static PDP_RETVAL CALLBACK
Bram Moolenaar1266d672017-02-01 13:43:36 +01001077PrintDlgProc(
1078 HWND hDlg,
1079 UINT message,
1080 WPARAM wParam UNUSED,
1081 LPARAM lParam UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001083# ifdef FEAT_GETTEXT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 NONCLIENTMETRICS nm;
1085 static HFONT hfont;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001086# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087
1088 switch (message)
1089 {
1090 case WM_INITDIALOG:
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001091# ifdef FEAT_GETTEXT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 nm.cbSize = sizeof(NONCLIENTMETRICS);
1093 if (SystemParametersInfo(
1094 SPI_GETNONCLIENTMETRICS,
1095 sizeof(NONCLIENTMETRICS),
1096 &nm,
1097 0))
1098 {
1099 char buff[MAX_PATH];
1100 int i;
1101
Bram Moolenaar0f873732019-12-05 20:28:46 +01001102 // Translate the dialog texts
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 hfont = CreateFontIndirect(&nm.lfMessageFont);
1104 for (i = IDC_PRINTTEXT1; i <= IDC_PROGRESS; i++)
1105 {
1106 SendDlgItemMessage(hDlg, i, WM_SETFONT, (WPARAM)hfont, 1);
1107 if (GetDlgItemText(hDlg,i, buff, sizeof(buff)))
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001108 vimSetDlgItemText(hDlg,i, (char_u *)_(buff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 }
1110 SendDlgItemMessage(hDlg, IDCANCEL,
1111 WM_SETFONT, (WPARAM)hfont, 1);
1112 if (GetDlgItemText(hDlg,IDCANCEL, buff, sizeof(buff)))
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001113 vimSetDlgItemText(hDlg,IDCANCEL, (char_u *)_(buff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001115# endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001116 SetWindowText(hDlg, (LPCSTR)szAppName);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 if (prt_name != NULL)
1118 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001119 vimSetDlgItemText(hDlg, IDC_PRINTTEXT2, (char_u *)prt_name);
Bram Moolenaard23a8232018-02-10 18:45:26 +01001120 VIM_CLEAR(prt_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 }
1122 EnableMenuItem(GetSystemMenu(hDlg, FALSE), SC_CLOSE, MF_GRAYED);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001123# if !defined(FEAT_GUI) || defined(VIMDLL)
1124# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001125 if (!gui.in_use)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001126# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001127 BringWindowToTop(s_hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001128# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 return TRUE;
1130
1131 case WM_COMMAND:
1132 *bUserAbort = TRUE;
1133 EnableWindow(GetParent(hDlg), TRUE);
1134 DestroyWindow(hDlg);
1135 hDlgPrint = NULL;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001136# ifdef FEAT_GETTEXT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 DeleteObject(hfont);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001138# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 return TRUE;
1140 }
1141 return FALSE;
1142}
1143
1144 static BOOL CALLBACK
Bram Moolenaar1266d672017-02-01 13:43:36 +01001145AbortProc(HDC hdcPrn UNUSED, int iCode UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146{
1147 MSG msg;
1148
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02001149 while (!*bUserAbort && pPeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 {
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02001151 if (!hDlgPrint || !pIsDialogMessage(hDlgPrint, &msg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 {
1153 TranslateMessage(&msg);
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02001154 pDispatchMessage(&msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 }
1156 }
1157 return !*bUserAbort;
1158}
1159
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001160# if !defined(FEAT_GUI) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161
Bram Moolenaar26fdd7d2011-11-30 13:42:44 +01001162 static UINT_PTR CALLBACK
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163PrintHookProc(
1164 HWND hDlg, // handle to dialog box
1165 UINT uiMsg, // message identifier
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02001166 WPARAM wParam UNUSED, // message parameter
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 LPARAM lParam // message parameter
1168 )
1169{
1170 HWND hwndOwner;
1171 RECT rc, rcDlg, rcOwner;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001172 PRINTDLGW *pPD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173
1174 if (uiMsg == WM_INITDIALOG)
1175 {
1176 // Get the owner window and dialog box rectangles.
1177 if ((hwndOwner = GetParent(hDlg)) == NULL)
1178 hwndOwner = GetDesktopWindow();
1179
1180 GetWindowRect(hwndOwner, &rcOwner);
1181 GetWindowRect(hDlg, &rcDlg);
1182 CopyRect(&rc, &rcOwner);
1183
1184 // Offset the owner and dialog box rectangles so that
1185 // right and bottom values represent the width and
1186 // height, and then offset the owner again to discard
1187 // space taken up by the dialog box.
1188
1189 OffsetRect(&rcDlg, -rcDlg.left, -rcDlg.top);
1190 OffsetRect(&rc, -rc.left, -rc.top);
1191 OffsetRect(&rc, -rcDlg.right, -rcDlg.bottom);
1192
1193 // The new position is the sum of half the remaining
1194 // space and the owner's original position.
1195
1196 SetWindowPos(hDlg,
1197 HWND_TOP,
1198 rcOwner.left + (rc.right / 2),
1199 rcOwner.top + (rc.bottom / 2),
1200 0, 0, // ignores size arguments
1201 SWP_NOSIZE);
1202
Bram Moolenaar0f873732019-12-05 20:28:46 +01001203 // tackle the printdlg copiesctrl problem
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001204 pPD = (PRINTDLGW *)lParam;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 pPD->nCopies = (WORD)pPD->lCustData;
1206 SetDlgItemInt( hDlg, edt3, pPD->nCopies, FALSE );
Bram Moolenaar0f873732019-12-05 20:28:46 +01001207 // Bring the window to top
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 BringWindowToTop(GetParent(hDlg));
1209 SetForegroundWindow(hDlg);
1210 }
1211
1212 return FALSE;
1213}
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001214# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215
1216 void
1217mch_print_cleanup(void)
1218{
1219 int pifItalic;
1220 int pifBold;
1221 int pifUnderline;
1222
1223 for (pifBold = 0; pifBold <= 1; pifBold++)
1224 for (pifItalic = 0; pifItalic <= 1; pifItalic++)
1225 for (pifUnderline = 0; pifUnderline <= 1; pifUnderline++)
1226 DeleteObject(prt_font_handles[pifBold][pifItalic][pifUnderline]);
1227
1228 if (prt_dlg.hDC != NULL)
1229 DeleteDC(prt_dlg.hDC);
1230 if (!*bUserAbort)
1231 SendMessage(hDlgPrint, WM_COMMAND, 0, 0);
1232}
1233
1234 static int
1235to_device_units(int idx, int dpi, int physsize, int offset, int def_number)
1236{
1237 int ret = 0;
1238 int u;
1239 int nr;
1240
1241 u = prt_get_unit(idx);
1242 if (u == PRT_UNIT_NONE)
1243 {
1244 u = PRT_UNIT_PERC;
1245 nr = def_number;
1246 }
1247 else
1248 nr = printer_opts[idx].number;
1249
1250 switch (u)
1251 {
1252 case PRT_UNIT_PERC:
1253 ret = (physsize * nr) / 100;
1254 break;
1255 case PRT_UNIT_INCH:
1256 ret = (nr * dpi);
1257 break;
1258 case PRT_UNIT_MM:
1259 ret = (nr * 10 * dpi) / 254;
1260 break;
1261 case PRT_UNIT_POINT:
1262 ret = (nr * 10 * dpi) / 720;
1263 break;
1264 }
1265
1266 if (ret < offset)
1267 return 0;
1268 else
1269 return ret - offset;
1270}
1271
1272 static int
1273prt_get_cpl(void)
1274{
1275 int hr;
1276 int phyw;
1277 int dvoff;
1278 int rev_offset;
1279 int dpi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280
1281 GetTextMetrics(prt_dlg.hDC, &prt_tm);
1282 prt_line_height = prt_tm.tmHeight + prt_tm.tmExternalLeading;
1283
1284 hr = GetDeviceCaps(prt_dlg.hDC, HORZRES);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 phyw = GetDeviceCaps(prt_dlg.hDC, PHYSICALWIDTH);
1286 dvoff = GetDeviceCaps(prt_dlg.hDC, PHYSICALOFFSETX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 dpi = GetDeviceCaps(prt_dlg.hDC, LOGPIXELSX);
1288
1289 rev_offset = phyw - (dvoff + hr);
1290
1291 prt_left_margin = to_device_units(OPT_PRINT_LEFT, dpi, phyw, dvoff, 10);
1292 if (prt_use_number())
1293 {
1294 prt_number_width = PRINT_NUMBER_WIDTH * prt_tm.tmAveCharWidth;
1295 prt_left_margin += prt_number_width;
1296 }
1297 else
1298 prt_number_width = 0;
1299
1300 prt_right_margin = hr - to_device_units(OPT_PRINT_RIGHT, dpi, phyw,
1301 rev_offset, 5);
1302
1303 return (prt_right_margin - prt_left_margin) / prt_tm.tmAveCharWidth;
1304}
1305
1306 static int
1307prt_get_lpp(void)
1308{
1309 int vr;
1310 int phyw;
1311 int dvoff;
1312 int rev_offset;
1313 int bottom_margin;
1314 int dpi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315
1316 vr = GetDeviceCaps(prt_dlg.hDC, VERTRES);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 phyw = GetDeviceCaps(prt_dlg.hDC, PHYSICALHEIGHT);
1318 dvoff = GetDeviceCaps(prt_dlg.hDC, PHYSICALOFFSETY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 dpi = GetDeviceCaps(prt_dlg.hDC, LOGPIXELSY);
1320
1321 rev_offset = phyw - (dvoff + vr);
1322
1323 prt_top_margin = to_device_units(OPT_PRINT_TOP, dpi, phyw, dvoff, 5);
1324
Bram Moolenaar0f873732019-12-05 20:28:46 +01001325 // adjust top margin if there is a header
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 prt_top_margin += prt_line_height * prt_header_height();
1327
1328 bottom_margin = vr - to_device_units(OPT_PRINT_BOT, dpi, phyw,
1329 rev_offset, 5);
1330
1331 return (bottom_margin - prt_top_margin) / prt_line_height;
1332}
1333
1334 int
1335mch_print_init(prt_settings_T *psettings, char_u *jobname, int forceit)
1336{
1337 static HGLOBAL stored_dm = NULL;
1338 static HGLOBAL stored_devn = NULL;
1339 static int stored_nCopies = 1;
1340 static int stored_nFlags = 0;
1341
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001342 LOGFONTW fLogFont;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 int pifItalic;
1344 int pifBold;
1345 int pifUnderline;
1346
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001347 DEVMODEW *mem;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 DEVNAMES *devname;
1349 int i;
1350
1351 bUserAbort = &(psettings->user_abort);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001352 vim_memset(&prt_dlg, 0, sizeof(PRINTDLGW));
1353 prt_dlg.lStructSize = sizeof(PRINTDLGW);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001354# if !defined(FEAT_GUI) || defined(VIMDLL)
1355# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001356 if (!gui.in_use)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001357# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01001358 GetConsoleHwnd(); // get value of s_hwnd
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001359# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 prt_dlg.hwndOwner = s_hwnd;
1361 prt_dlg.Flags = PD_NOPAGENUMS | PD_NOSELECTION | PD_RETURNDC;
1362 if (!forceit)
1363 {
1364 prt_dlg.hDevMode = stored_dm;
1365 prt_dlg.hDevNames = stored_devn;
1366 prt_dlg.lCustData = stored_nCopies; // work around bug in print dialog
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001367# if !defined(FEAT_GUI) || defined(VIMDLL)
1368# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001369 if (!gui.in_use)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001370# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001371 {
1372 /*
1373 * Use hook to prevent console window being sent to back
1374 */
1375 prt_dlg.lpfnPrintHook = PrintHookProc;
1376 prt_dlg.Flags |= PD_ENABLEPRINTHOOK;
1377 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001378# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 prt_dlg.Flags |= stored_nFlags;
1380 }
1381
1382 /*
1383 * If bang present, return default printer setup with no dialog
1384 * never show dialog if we are running over telnet
1385 */
1386 if (forceit
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001387# if !defined(FEAT_GUI) || defined(VIMDLL)
1388# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001389 || (!gui.in_use && !term_console)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001390# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 || !term_console
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001392# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001393# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 )
1395 {
1396 prt_dlg.Flags |= PD_RETURNDEFAULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 /*
1398 * MSDN suggests setting the first parameter to WINSPOOL for
1399 * NT, but NULL appears to work just as well.
1400 */
1401 if (*p_pdev != NUL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001402 prt_dlg.hDC = CreateDC(NULL, (LPCSTR)p_pdev, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 {
1405 prt_dlg.Flags |= PD_RETURNDEFAULT;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001406 if (PrintDlgW(&prt_dlg) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 goto init_fail_dlg;
1408 }
1409 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001410 else if (PrintDlgW(&prt_dlg) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 goto init_fail_dlg;
1412 else
1413 {
1414 /*
1415 * keep the previous driver context
1416 */
1417 stored_dm = prt_dlg.hDevMode;
1418 stored_devn = prt_dlg.hDevNames;
1419 stored_nFlags = prt_dlg.Flags;
1420 stored_nCopies = prt_dlg.nCopies;
1421 }
1422
1423 if (prt_dlg.hDC == NULL)
1424 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001425 emsg(_("E237: Printer selection failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 mch_print_cleanup();
1427 return FALSE;
1428 }
1429
Bram Moolenaar0f873732019-12-05 20:28:46 +01001430 // Not all printer drivers report the support of color (or grey) in the
1431 // same way. Let's set has_color if there appears to be some way to print
1432 // more than B&W.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 i = GetDeviceCaps(prt_dlg.hDC, NUMCOLORS);
1434 psettings->has_color = (GetDeviceCaps(prt_dlg.hDC, BITSPIXEL) > 1
1435 || GetDeviceCaps(prt_dlg.hDC, PLANES) > 1
1436 || i > 2 || i == -1);
1437
Bram Moolenaar0f873732019-12-05 20:28:46 +01001438 // Ensure all font styles are baseline aligned
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439 SetTextAlign(prt_dlg.hDC, TA_BASELINE|TA_LEFT);
1440
1441 /*
1442 * On some windows systems the nCopies parameter is not
1443 * passed back correctly. It must be retrieved from the
1444 * hDevMode struct.
1445 */
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001446 mem = (DEVMODEW *)GlobalLock(prt_dlg.hDevMode);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 if (mem != NULL)
1448 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 if (mem->dmCopies != 1)
1450 stored_nCopies = mem->dmCopies;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 if ((mem->dmFields & DM_DUPLEX) && (mem->dmDuplex & ~DMDUP_SIMPLEX))
1452 psettings->duplex = TRUE;
1453 if ((mem->dmFields & DM_COLOR) && (mem->dmColor & DMCOLOR_COLOR))
1454 psettings->has_color = TRUE;
1455 }
1456 GlobalUnlock(prt_dlg.hDevMode);
1457
1458 devname = (DEVNAMES *)GlobalLock(prt_dlg.hDevNames);
1459 if (devname != 0)
1460 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001461 WCHAR *wprinter_name = (WCHAR *)devname + devname->wDeviceOffset;
1462 WCHAR *wport_name = (WCHAR *)devname + devname->wOutputOffset;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001463 char_u *text = (char_u *)_("to %s on %s");
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001464 char_u *printer_name = utf16_to_enc(wprinter_name, NULL);
1465 char_u *port_name = utf16_to_enc(wport_name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001467 if (printer_name != NULL && port_name != NULL)
Bram Moolenaar964b3742019-05-24 18:54:09 +02001468 prt_name = alloc(STRLEN(printer_name)
1469 + STRLEN(port_name) + STRLEN(text));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 if (prt_name != NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001471 wsprintf((char *)prt_name, (const char *)text,
1472 printer_name, port_name);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001473 vim_free(printer_name);
1474 vim_free(port_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 }
1476 GlobalUnlock(prt_dlg.hDevNames);
1477
1478 /*
1479 * Initialise the font according to 'printfont'
1480 */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02001481 vim_memset(&fLogFont, 0, sizeof(fLogFont));
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001482 if (get_logfont(&fLogFont, p_pfn, prt_dlg.hDC, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001484 semsg(_("E613: Unknown printer font: %s"), p_pfn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 mch_print_cleanup();
1486 return FALSE;
1487 }
1488
1489 for (pifBold = 0; pifBold <= 1; pifBold++)
1490 for (pifItalic = 0; pifItalic <= 1; pifItalic++)
1491 for (pifUnderline = 0; pifUnderline <= 1; pifUnderline++)
1492 {
1493 fLogFont.lfWeight = boldface[pifBold];
1494 fLogFont.lfItalic = pifItalic;
1495 fLogFont.lfUnderline = pifUnderline;
1496 prt_font_handles[pifBold][pifItalic][pifUnderline]
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001497 = CreateFontIndirectW(&fLogFont);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498 }
1499
1500 SetBkMode(prt_dlg.hDC, OPAQUE);
1501 SelectObject(prt_dlg.hDC, prt_font_handles[0][0][0]);
1502
1503 /*
1504 * Fill in the settings struct
1505 */
1506 psettings->chars_per_line = prt_get_cpl();
1507 psettings->lines_per_page = prt_get_lpp();
Bram Moolenaar7ddc6422014-09-27 11:18:19 +02001508 if (prt_dlg.Flags & PD_USEDEVMODECOPIESANDCOLLATE)
1509 {
1510 psettings->n_collated_copies = (prt_dlg.Flags & PD_COLLATE)
1511 ? prt_dlg.nCopies : 1;
1512 psettings->n_uncollated_copies = (prt_dlg.Flags & PD_COLLATE)
1513 ? 1 : prt_dlg.nCopies;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514
Bram Moolenaar7ddc6422014-09-27 11:18:19 +02001515 if (psettings->n_collated_copies == 0)
1516 psettings->n_collated_copies = 1;
1517
1518 if (psettings->n_uncollated_copies == 0)
1519 psettings->n_uncollated_copies = 1;
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01001520 }
1521 else
1522 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 psettings->n_collated_copies = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 psettings->n_uncollated_copies = 1;
Bram Moolenaar7ddc6422014-09-27 11:18:19 +02001525 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526
1527 psettings->jobname = jobname;
1528
1529 return TRUE;
1530
1531init_fail_dlg:
1532 {
1533 DWORD err = CommDlgExtendedError();
1534
1535 if (err)
1536 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 char_u *buf;
1538
Bram Moolenaar0f873732019-12-05 20:28:46 +01001539 // I suspect FormatMessage() doesn't work for values returned by
1540 // CommDlgExtendedError(). What does?
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
1542 FORMAT_MESSAGE_FROM_SYSTEM |
1543 FORMAT_MESSAGE_IGNORE_INSERTS,
1544 NULL, err, 0, (LPTSTR)(&buf), 0, NULL);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001545 semsg(_("E238: Print error: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 buf == NULL ? (char_u *)_("Unknown") : buf);
1547 LocalFree((LPVOID)(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 }
1549 else
Bram Moolenaar0f873732019-12-05 20:28:46 +01001550 msg_clr_eos(); // Maybe canceled
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551
1552 mch_print_cleanup();
1553 return FALSE;
1554 }
1555}
1556
1557
1558 int
1559mch_print_begin(prt_settings_T *psettings)
1560{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001561 int ret = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 char szBuffer[300];
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001563 WCHAR *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001565 hDlgPrint = CreateDialog(g_hinst, TEXT("PrintDlgBox"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 prt_dlg.hwndOwner, PrintDlgProc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 SetAbortProc(prt_dlg.hDC, AbortProc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 wsprintf(szBuffer, _("Printing '%s'"), gettail(psettings->jobname));
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001569 vimSetDlgItemText(hDlgPrint, IDC_PRINTTEXT1, (char_u *)szBuffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001571 wp = enc_to_utf16(psettings->jobname, NULL);
Bram Moolenaar2290b1f2018-05-13 17:30:45 +02001572 if (wp != NULL)
1573 {
1574 DOCINFOW di;
1575
1576 vim_memset(&di, 0, sizeof(di));
1577 di.cbSize = sizeof(di);
1578 di.lpszDocName = wp;
1579 ret = StartDocW(prt_dlg.hDC, &di);
1580 vim_free(wp);
1581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001583# ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01001584 // Give focus back to main window (when using MDI).
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001585# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001586 if (gui.in_use)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001587# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001588 SetFocus(s_hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001589# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590
1591 return (ret > 0);
1592}
1593
1594 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01001595mch_print_end(prt_settings_T *psettings UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596{
1597 EndDoc(prt_dlg.hDC);
1598 if (!*bUserAbort)
1599 SendMessage(hDlgPrint, WM_COMMAND, 0, 0);
1600}
1601
1602 int
1603mch_print_end_page(void)
1604{
1605 return (EndPage(prt_dlg.hDC) > 0);
1606}
1607
1608 int
1609mch_print_begin_page(char_u *msg)
1610{
1611 if (msg != NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001612 vimSetDlgItemText(hDlgPrint, IDC_PROGRESS, msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 return (StartPage(prt_dlg.hDC) > 0);
1614}
1615
1616 int
1617mch_print_blank_page(void)
1618{
1619 return (mch_print_begin_page(NULL) ? (mch_print_end_page()) : FALSE);
1620}
1621
1622static int prt_pos_x = 0;
1623static int prt_pos_y = 0;
1624
1625 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001626mch_print_start_line(int margin, int page_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627{
1628 if (margin)
1629 prt_pos_x = -prt_number_width;
1630 else
1631 prt_pos_x = 0;
1632 prt_pos_y = page_line * prt_line_height
1633 + prt_tm.tmAscent + prt_tm.tmExternalLeading;
1634}
1635
1636 int
1637mch_print_text_out(char_u *p, int len)
1638{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 SIZE sz;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001640 WCHAR *wp;
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001641 int wlen = len;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001642 int ret = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001644 wp = enc_to_utf16(p, &wlen);
1645 if (wp == NULL)
1646 return FALSE;
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001647
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001648 TextOutW(prt_dlg.hDC, prt_pos_x + prt_left_margin,
1649 prt_pos_y + prt_top_margin, wp, wlen);
1650 GetTextExtentPoint32W(prt_dlg.hDC, wp, wlen, &sz);
1651 vim_free(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 prt_pos_x += (sz.cx - prt_tm.tmOverhang);
Bram Moolenaar0f873732019-12-05 20:28:46 +01001653 // This is wrong when printing spaces for a TAB.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001654 if (p[len] != NUL)
1655 {
Bram Moolenaar1614a142019-10-06 22:00:13 +02001656 wlen = mb_ptr2len(p + len);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001657 wp = enc_to_utf16(p + len, &wlen);
1658 if (wp != NULL)
1659 {
1660 GetTextExtentPoint32W(prt_dlg.hDC, wp, 1, &sz);
1661 ret = (prt_pos_x + prt_left_margin + sz.cx > prt_right_margin);
1662 vim_free(wp);
1663 }
1664 }
1665 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666}
1667
1668 void
1669mch_print_set_font(int iBold, int iItalic, int iUnderline)
1670{
1671 SelectObject(prt_dlg.hDC, prt_font_handles[iBold][iItalic][iUnderline]);
1672}
1673
1674 void
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001675mch_print_set_bg(long_u bgcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676{
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001677 SetBkColor(prt_dlg.hDC, GetNearestColor(prt_dlg.hDC,
1678 swap_me((COLORREF)bgcol)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 /*
1680 * With a white background we can draw characters transparent, which is
1681 * good for italic characters that overlap to the next char cell.
1682 */
1683 if (bgcol == 0xffffffUL)
1684 SetBkMode(prt_dlg.hDC, TRANSPARENT);
1685 else
1686 SetBkMode(prt_dlg.hDC, OPAQUE);
1687}
1688
1689 void
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001690mch_print_set_fg(long_u fgcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691{
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001692 SetTextColor(prt_dlg.hDC, GetNearestColor(prt_dlg.hDC,
1693 swap_me((COLORREF)fgcol)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694}
1695
Bram Moolenaar0f873732019-12-05 20:28:46 +01001696#endif // FEAT_PRINTER && !FEAT_POSTSCRIPT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697
Bram Moolenaar58d98232005-07-23 22:25:46 +00001698
1699
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700#if defined(FEAT_SHORTCUT) || defined(PROTO)
Bram Moolenaar82881492012-11-20 16:53:39 +01001701# ifndef PROTO
1702# include <shlobj.h>
1703# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001705typedef BOOL (WINAPI *pfnGetFinalPathNameByHandleW)(
Bram Moolenaardce1e892019-02-10 23:18:53 +01001706 HANDLE hFile,
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001707 LPWSTR lpszFilePath,
1708 DWORD cchFilePath,
1709 DWORD dwFlags);
1710static pfnGetFinalPathNameByHandleW pGetFinalPathNameByHandleW = NULL;
Bram Moolenaardce1e892019-02-10 23:18:53 +01001711
Bram Moolenaar4a792c82019-06-06 12:22:41 +02001712# define is_path_sep(c) ((c) == L'\\' || (c) == L'/')
1713
1714 static int
1715is_reparse_point_included(LPCWSTR fname)
1716{
1717 LPCWSTR p = fname, q;
1718 WCHAR buf[MAX_PATH];
1719 DWORD attr;
1720
1721 if (isalpha(p[0]) && p[1] == L':' && is_path_sep(p[2]))
1722 p += 3;
1723 else if (is_path_sep(p[0]) && is_path_sep(p[1]))
1724 p += 2;
1725
1726 while (*p != L'\0')
1727 {
1728 q = wcspbrk(p, L"\\/");
1729 if (q == NULL)
1730 p = q = fname + wcslen(fname);
1731 else
1732 p = q + 1;
1733 if (q - fname >= MAX_PATH)
1734 return FALSE;
1735 wcsncpy(buf, fname, q - fname);
1736 buf[q - fname] = L'\0';
1737 attr = GetFileAttributesW(buf);
1738 if (attr != INVALID_FILE_ATTRIBUTES
1739 && (attr & FILE_ATTRIBUTE_REPARSE_POINT) != 0)
1740 return TRUE;
1741 }
1742 return FALSE;
1743}
1744
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02001745 static char_u *
Bram Moolenaardce1e892019-02-10 23:18:53 +01001746resolve_reparse_point(char_u *fname)
1747{
1748 HANDLE h = INVALID_HANDLE_VALUE;
1749 DWORD size;
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001750 WCHAR *p, *wp;
Bram Moolenaardce1e892019-02-10 23:18:53 +01001751 char_u *rfname = NULL;
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001752 WCHAR *buff = NULL;
Bram Moolenaardce1e892019-02-10 23:18:53 +01001753 static BOOL loaded = FALSE;
1754
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001755 if (pGetFinalPathNameByHandleW == NULL)
Bram Moolenaardce1e892019-02-10 23:18:53 +01001756 {
1757 HMODULE hmod = GetModuleHandle("kernel32.dll");
1758
1759 if (loaded == TRUE)
1760 return NULL;
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001761 pGetFinalPathNameByHandleW = (pfnGetFinalPathNameByHandleW)
1762 GetProcAddress(hmod, "GetFinalPathNameByHandleW");
Bram Moolenaardce1e892019-02-10 23:18:53 +01001763 loaded = TRUE;
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001764 if (pGetFinalPathNameByHandleW == NULL)
Bram Moolenaardce1e892019-02-10 23:18:53 +01001765 return NULL;
1766 }
1767
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001768 p = enc_to_utf16(fname, NULL);
1769 if (p == NULL)
1770 goto fail;
1771
Bram Moolenaar4a792c82019-06-06 12:22:41 +02001772 if (!is_reparse_point_included(p))
1773 {
1774 vim_free(p);
1775 goto fail;
1776 }
1777
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001778 h = CreateFileW(p, 0, 0, NULL, OPEN_EXISTING,
1779 FILE_FLAG_BACKUP_SEMANTICS, NULL);
1780 vim_free(p);
Bram Moolenaardce1e892019-02-10 23:18:53 +01001781
1782 if (h == INVALID_HANDLE_VALUE)
1783 goto fail;
1784
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001785 size = pGetFinalPathNameByHandleW(h, NULL, 0, 0);
1786 if (size == 0)
1787 goto fail;
1788 buff = ALLOC_MULT(WCHAR, size);
1789 if (buff == NULL)
1790 goto fail;
1791 if (pGetFinalPathNameByHandleW(h, buff, size, 0) == 0)
Bram Moolenaardce1e892019-02-10 23:18:53 +01001792 goto fail;
1793
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001794 if (wcsncmp(buff, L"\\\\?\\UNC\\", 8) == 0)
1795 {
1796 buff[6] = L'\\';
1797 wp = buff + 6;
1798 }
1799 else if (wcsncmp(buff, L"\\\\?\\", 4) == 0)
1800 wp = buff + 4;
Bram Moolenaardce1e892019-02-10 23:18:53 +01001801 else
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001802 wp = buff;
1803
1804 rfname = utf16_to_enc(wp, NULL);
Bram Moolenaardce1e892019-02-10 23:18:53 +01001805
1806fail:
1807 if (h != INVALID_HANDLE_VALUE)
1808 CloseHandle(h);
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001809 if (buff != NULL)
1810 vim_free(buff);
Bram Moolenaardce1e892019-02-10 23:18:53 +01001811
1812 return rfname;
1813}
1814
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815/*
1816 * When "fname" is the name of a shortcut (*.lnk) resolve the file it points
1817 * to and return that name in allocated memory.
1818 * Otherwise NULL is returned.
1819 */
Bram Moolenaardce1e892019-02-10 23:18:53 +01001820 static char_u *
1821resolve_shortcut(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822{
1823 HRESULT hr;
1824 IShellLink *psl = NULL;
1825 IPersistFile *ppf = NULL;
1826 OLECHAR wsz[MAX_PATH];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 char_u *rfname = NULL;
1828 int len;
Bram Moolenaar604729e2013-08-30 16:44:19 +02001829 IShellLinkW *pslw = NULL;
1830 WIN32_FIND_DATAW ffdw; // we get those free of charge
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831
Bram Moolenaar0f873732019-12-05 20:28:46 +01001832 // Check if the file name ends in ".lnk". Avoid calling
1833 // CoCreateInstance(), it's quite slow.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 if (fname == NULL)
1835 return rfname;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001836 len = (int)STRLEN(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 if (len <= 4 || STRNICMP(fname + len - 4, ".lnk", 4) != 0)
1838 return rfname;
1839
1840 CoInitialize(NULL);
1841
1842 // create a link manager object and request its interface
1843 hr = CoCreateInstance(
1844 &CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001845 &IID_IShellLinkW, (void**)&pslw);
1846 if (hr == S_OK)
1847 {
1848 WCHAR *p = enc_to_utf16(fname, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001850 if (p != NULL)
1851 {
1852 // Get a pointer to the IPersistFile interface.
1853 hr = pslw->lpVtbl->QueryInterface(
1854 pslw, &IID_IPersistFile, (void**)&ppf);
1855 if (hr != S_OK)
1856 goto shortcut_errorw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001858 // "load" the name and resolve the link
1859 hr = ppf->lpVtbl->Load(ppf, p, STGM_READ);
1860 if (hr != S_OK)
1861 goto shortcut_errorw;
1862# if 0 // This makes Vim wait a long time if the target does not exist.
1863 hr = pslw->lpVtbl->Resolve(pslw, NULL, SLR_NO_UI);
1864 if (hr != S_OK)
1865 goto shortcut_errorw;
Bram Moolenaar604729e2013-08-30 16:44:19 +02001866# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001868 // Get the path to the link target.
1869 ZeroMemory(wsz, MAX_PATH * sizeof(WCHAR));
1870 hr = pslw->lpVtbl->GetPath(pslw, wsz, MAX_PATH, &ffdw, 0);
1871 if (hr == S_OK && wsz[0] != NUL)
1872 rfname = utf16_to_enc(wsz, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001874shortcut_errorw:
1875 vim_free(p);
1876 }
1877 }
1878
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 // Release all interface pointers (both belong to the same object)
1880 if (ppf != NULL)
1881 ppf->lpVtbl->Release(ppf);
1882 if (psl != NULL)
1883 psl->lpVtbl->Release(psl);
Bram Moolenaar604729e2013-08-30 16:44:19 +02001884 if (pslw != NULL)
1885 pslw->lpVtbl->Release(pslw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886
1887 CoUninitialize();
1888 return rfname;
1889}
Bram Moolenaardce1e892019-02-10 23:18:53 +01001890
1891 char_u *
1892mch_resolve_path(char_u *fname, int reparse_point)
1893{
1894 char_u *path = resolve_shortcut(fname);
1895
1896 if (path == NULL && reparse_point)
1897 path = resolve_reparse_point(fname);
1898 return path;
1899}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900#endif
1901
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001902#if (defined(FEAT_EVAL) && (!defined(FEAT_GUI) || defined(VIMDLL))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903/*
1904 * Bring ourselves to the foreground. Does work if the OS doesn't allow it.
1905 */
1906 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001907win32_set_foreground(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908{
Bram Moolenaar0f873732019-12-05 20:28:46 +01001909 GetConsoleHwnd(); // get value of s_hwnd
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 if (s_hwnd != 0)
1911 SetForegroundWindow(s_hwnd);
1912}
1913#endif
1914
1915#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
1916/*
1917 * Client-server code for Vim
1918 *
1919 * Originally written by Paul Moore
1920 */
1921
Bram Moolenaar0f873732019-12-05 20:28:46 +01001922// In order to handle inter-process messages, we need to have a window. But
1923// the functions in this module can be called before the main GUI window is
1924// created (and may also be called in the console version, where there is no
1925// GUI window at all).
1926//
1927// So we create a hidden window, and arrange to destroy it on exit.
1928HWND message_window = 0; // window that's handling messages
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001930# define VIM_CLASSNAME "VIM_MESSAGES"
1931# define VIM_CLASSNAME_LEN (sizeof(VIM_CLASSNAME) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932
Bram Moolenaar0f873732019-12-05 20:28:46 +01001933// Communication is via WM_COPYDATA messages. The message type is send in
1934// the dwData parameter. Types are defined here.
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001935# define COPYDATA_KEYS 0
1936# define COPYDATA_REPLY 1
1937# define COPYDATA_EXPR 10
1938# define COPYDATA_RESULT 11
1939# define COPYDATA_ERROR_RESULT 12
1940# define COPYDATA_ENCODING 20
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941
Bram Moolenaar0f873732019-12-05 20:28:46 +01001942// This is a structure containing a server HWND and its name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943struct server_id
1944{
1945 HWND hwnd;
1946 char_u *name;
1947};
1948
Bram Moolenaar0f873732019-12-05 20:28:46 +01001949// Last received 'encoding' that the client uses.
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001950static char_u *client_enc = NULL;
1951
1952/*
1953 * Tell the other side what encoding we are using.
1954 * Errors are ignored.
1955 */
1956 static void
1957serverSendEnc(HWND target)
1958{
1959 COPYDATASTRUCT data;
1960
1961 data.dwData = COPYDATA_ENCODING;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001962 data.cbData = (DWORD)STRLEN(p_enc) + 1;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001963 data.lpData = p_enc;
1964 (void)SendMessage(target, WM_COPYDATA, (WPARAM)message_window,
1965 (LPARAM)(&data));
1966}
1967
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968/*
1969 * Clean up on exit. This destroys the hidden message window.
1970 */
1971 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972CleanUpMessaging(void)
1973{
1974 if (message_window != 0)
1975 {
1976 DestroyWindow(message_window);
1977 message_window = 0;
1978 }
1979}
1980
1981static int save_reply(HWND server, char_u *reply, int expr);
1982
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001983/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 * The window procedure for the hidden message window.
1985 * It handles callback messages and notifications from servers.
1986 * In order to process these messages, it is necessary to run a
1987 * message loop. Code which may run before the main message loop
1988 * is started (in the GUI) is careful to pump messages when it needs
1989 * to. Features which require message delivery during normal use will
1990 * not work in the console version - this basically means those
1991 * features which allow Vim to act as a server, rather than a client.
1992 */
1993 static LRESULT CALLBACK
1994Messaging_WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
1995{
1996 if (msg == WM_COPYDATA)
1997 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001998 // This is a message from another Vim. The dwData member of the
1999 // COPYDATASTRUCT determines the type of message:
2000 // COPYDATA_ENCODING:
2001 // The encoding that the client uses. Following messages will
2002 // use this encoding, convert if needed.
2003 // COPYDATA_KEYS:
2004 // A key sequence. We are a server, and a client wants these keys
2005 // adding to the input queue.
2006 // COPYDATA_REPLY:
2007 // A reply. We are a client, and a server has sent this message
2008 // in response to a request. (server2client())
2009 // COPYDATA_EXPR:
2010 // An expression. We are a server, and a client wants us to
2011 // evaluate this expression.
2012 // COPYDATA_RESULT:
2013 // A reply. We are a client, and a server has sent this message
2014 // in response to a COPYDATA_EXPR.
2015 // COPYDATA_ERROR_RESULT:
2016 // A reply. We are a client, and a server has sent this message
2017 // in response to a COPYDATA_EXPR that failed to evaluate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 COPYDATASTRUCT *data = (COPYDATASTRUCT*)lParam;
2019 HWND sender = (HWND)wParam;
2020 COPYDATASTRUCT reply;
2021 char_u *res;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 int retval;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002023 char_u *str;
2024 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025
2026 switch (data->dwData)
2027 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002028 case COPYDATA_ENCODING:
Bram Moolenaar0f873732019-12-05 20:28:46 +01002029 // Remember the encoding that the client uses.
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002030 vim_free(client_enc);
2031 client_enc = enc_canonize((char_u *)data->lpData);
2032 return 1;
2033
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 case COPYDATA_KEYS:
Bram Moolenaar0f873732019-12-05 20:28:46 +01002035 // Remember who sent this, for <client>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 clientWindow = sender;
2037
Bram Moolenaar0f873732019-12-05 20:28:46 +01002038 // Add the received keys to the input buffer. The loop waiting
2039 // for the user to do something should check the input buffer.
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002040 str = serverConvert(client_enc, (char_u *)data->lpData, &tofree);
2041 server_to_input_buf(str);
2042 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043
2044# ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01002045 // Wake up the main GUI loop.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002046# ifdef VIMDLL
2047 if (gui.in_use)
2048# endif
2049 if (s_hwnd != 0)
2050 PostMessage(s_hwnd, WM_NULL, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051# endif
2052 return 1;
2053
2054 case COPYDATA_EXPR:
Bram Moolenaar0f873732019-12-05 20:28:46 +01002055 // Remember who sent this, for <client>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 clientWindow = sender;
2057
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002058 str = serverConvert(client_enc, (char_u *)data->lpData, &tofree);
2059 res = eval_client_expr_to_string(str);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002060
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061 if (res == NULL)
2062 {
Bram Moolenaar15bf76d2017-03-18 16:18:37 +01002063 char *err = _(e_invexprmsg);
2064 size_t len = STRLEN(str) + STRLEN(err) + 5;
2065
Bram Moolenaar964b3742019-05-24 18:54:09 +02002066 res = alloc(len);
Bram Moolenaar15bf76d2017-03-18 16:18:37 +01002067 if (res != NULL)
2068 vim_snprintf((char *)res, len, "%s: \"%s\"", err, str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 reply.dwData = COPYDATA_ERROR_RESULT;
2070 }
2071 else
2072 reply.dwData = COPYDATA_RESULT;
2073 reply.lpData = res;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002074 reply.cbData = (DWORD)STRLEN(res) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002076 serverSendEnc(sender);
Bram Moolenaar5246cd72013-06-16 16:41:47 +02002077 retval = (int)SendMessage(sender, WM_COPYDATA,
2078 (WPARAM)message_window, (LPARAM)(&reply));
Bram Moolenaar15bf76d2017-03-18 16:18:37 +01002079 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 vim_free(res);
2081 return retval;
2082
2083 case COPYDATA_REPLY:
2084 case COPYDATA_RESULT:
2085 case COPYDATA_ERROR_RESULT:
2086 if (data->lpData != NULL)
2087 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002088 str = serverConvert(client_enc, (char_u *)data->lpData,
2089 &tofree);
2090 if (tofree == NULL)
2091 str = vim_strsave(str);
2092 if (save_reply(sender, str,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 (data->dwData == COPYDATA_REPLY ? 0 :
2094 (data->dwData == COPYDATA_RESULT ? 1 :
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002095 2))) == FAIL)
2096 vim_free(str);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002097 else if (data->dwData == COPYDATA_REPLY)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 {
Bram Moolenaar427d51c2013-06-16 16:01:25 +02002099 char_u winstr[30];
2100
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002101 sprintf((char *)winstr, PRINTF_HEX_LONG_U, (long_u)sender);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002102 apply_autocmds(EVENT_REMOTEREPLY, winstr, str,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 TRUE, curbuf);
2104 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 }
2106 return 1;
2107 }
2108
2109 return 0;
2110 }
2111
2112 else if (msg == WM_ACTIVATE && wParam == WA_ACTIVE)
2113 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002114 // When the message window is activated (brought to the foreground),
2115 // this actually applies to the text window.
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002116# if !defined(FEAT_GUI) || defined(VIMDLL)
2117# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002118 if (!gui.in_use)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002119# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002120 GetConsoleHwnd(); // get value of s_hwnd
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002121# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 if (s_hwnd != 0)
2123 {
2124 SetForegroundWindow(s_hwnd);
2125 return 0;
2126 }
2127 }
2128
2129 return DefWindowProc(hwnd, msg, wParam, lParam);
2130}
2131
2132/*
2133 * Initialise the message handling process. This involves creating a window
2134 * to handle messages - the window will not be visible.
2135 */
2136 void
2137serverInitMessaging(void)
2138{
2139 WNDCLASS wndclass;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140
Bram Moolenaar0f873732019-12-05 20:28:46 +01002141 // Clean up on exit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 atexit(CleanUpMessaging);
2143
Bram Moolenaar0f873732019-12-05 20:28:46 +01002144 // Register a window class - we only really care
2145 // about the window procedure
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 wndclass.style = 0;
2147 wndclass.lpfnWndProc = Messaging_WndProc;
2148 wndclass.cbClsExtra = 0;
2149 wndclass.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002150 wndclass.hInstance = g_hinst;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 wndclass.hIcon = NULL;
2152 wndclass.hCursor = NULL;
2153 wndclass.hbrBackground = NULL;
2154 wndclass.lpszMenuName = NULL;
2155 wndclass.lpszClassName = VIM_CLASSNAME;
2156 RegisterClass(&wndclass);
2157
Bram Moolenaar0f873732019-12-05 20:28:46 +01002158 // Create the message window. It will be hidden, so the details don't
2159 // matter. Don't use WS_OVERLAPPEDWINDOW, it will make a shortcut remove
2160 // focus from gvim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 message_window = CreateWindow(VIM_CLASSNAME, "",
2162 WS_POPUPWINDOW | WS_CAPTION,
2163 CW_USEDEFAULT, CW_USEDEFAULT,
2164 100, 100, NULL, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002165 g_hinst, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166}
2167
Bram Moolenaar0f873732019-12-05 20:28:46 +01002168// Used by serverSendToVim() to find an alternate server name.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002169static char_u *altname_buf_ptr = NULL;
2170
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171/*
2172 * Get the title of the window "hwnd", which is the Vim server name, in
2173 * "name[namelen]" and return the length.
2174 * Returns zero if window "hwnd" is not a Vim server.
2175 */
2176 static int
2177getVimServerName(HWND hwnd, char *name, int namelen)
2178{
2179 int len;
2180 char buffer[VIM_CLASSNAME_LEN + 1];
2181
Bram Moolenaar0f873732019-12-05 20:28:46 +01002182 // Ignore windows which aren't Vim message windows
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 len = GetClassName(hwnd, buffer, sizeof(buffer));
2184 if (len != VIM_CLASSNAME_LEN || STRCMP(buffer, VIM_CLASSNAME) != 0)
2185 return 0;
2186
Bram Moolenaar0f873732019-12-05 20:28:46 +01002187 // Get the title of the window
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 return GetWindowText(hwnd, name, namelen);
2189}
2190
2191 static BOOL CALLBACK
2192enumWindowsGetServer(HWND hwnd, LPARAM lparam)
2193{
2194 struct server_id *id = (struct server_id *)lparam;
2195 char server[MAX_PATH];
2196
Bram Moolenaar0f873732019-12-05 20:28:46 +01002197 // Get the title of the window
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 if (getVimServerName(hwnd, server, sizeof(server)) == 0)
2199 return TRUE;
2200
Bram Moolenaar0f873732019-12-05 20:28:46 +01002201 // If this is the server we're looking for, return its HWND
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 if (STRICMP(server, id->name) == 0)
2203 {
2204 id->hwnd = hwnd;
2205 return FALSE;
2206 }
2207
Bram Moolenaar0f873732019-12-05 20:28:46 +01002208 // If we are looking for an alternate server, remember this name.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002209 if (altname_buf_ptr != NULL
2210 && STRNICMP(server, id->name, STRLEN(id->name)) == 0
2211 && vim_isdigit(server[STRLEN(id->name)]))
2212 {
2213 STRCPY(altname_buf_ptr, server);
Bram Moolenaar0f873732019-12-05 20:28:46 +01002214 altname_buf_ptr = NULL; // don't use another name
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002215 }
2216
Bram Moolenaar0f873732019-12-05 20:28:46 +01002217 // Otherwise, keep looking
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 return TRUE;
2219}
2220
2221 static BOOL CALLBACK
2222enumWindowsGetNames(HWND hwnd, LPARAM lparam)
2223{
2224 garray_T *ga = (garray_T *)lparam;
2225 char server[MAX_PATH];
2226
Bram Moolenaar0f873732019-12-05 20:28:46 +01002227 // Get the title of the window
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 if (getVimServerName(hwnd, server, sizeof(server)) == 0)
2229 return TRUE;
2230
Bram Moolenaar0f873732019-12-05 20:28:46 +01002231 // Add the name to the list
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002232 ga_concat(ga, (char_u *)server);
2233 ga_concat(ga, (char_u *)"\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 return TRUE;
2235}
2236
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002237struct enum_windows_s
2238{
2239 WNDENUMPROC lpEnumFunc;
2240 LPARAM lParam;
2241};
2242
2243 static BOOL CALLBACK
2244enum_windows_child(HWND hwnd, LPARAM lParam)
2245{
2246 struct enum_windows_s *ew = (struct enum_windows_s *)lParam;
2247
2248 return (ew->lpEnumFunc)(hwnd, ew->lParam);
2249}
2250
2251 static BOOL CALLBACK
2252enum_windows_toplevel(HWND hwnd, LPARAM lParam)
2253{
2254 struct enum_windows_s *ew = (struct enum_windows_s *)lParam;
2255
Bram Moolenaar95ba5c32018-10-07 22:47:07 +02002256 if ((ew->lpEnumFunc)(hwnd, ew->lParam))
2257 return TRUE;
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002258 return EnumChildWindows(hwnd, enum_windows_child, lParam);
2259}
2260
Bram Moolenaar0f873732019-12-05 20:28:46 +01002261/*
2262 * Enumerate all windows including children.
2263 */
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002264 static BOOL
2265enum_windows(WNDENUMPROC lpEnumFunc, LPARAM lParam)
2266{
2267 struct enum_windows_s ew;
2268
2269 ew.lpEnumFunc = lpEnumFunc;
2270 ew.lParam = lParam;
2271 return EnumWindows(enum_windows_toplevel, (LPARAM)&ew);
2272}
2273
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 static HWND
2275findServer(char_u *name)
2276{
2277 struct server_id id;
2278
2279 id.name = name;
2280 id.hwnd = 0;
2281
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002282 enum_windows(enumWindowsGetServer, (LPARAM)(&id));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283
2284 return id.hwnd;
2285}
2286
2287 void
2288serverSetName(char_u *name)
2289{
2290 char_u *ok_name;
2291 HWND hwnd = 0;
2292 int i = 0;
2293 char_u *p;
2294
Bram Moolenaar0f873732019-12-05 20:28:46 +01002295 // Leave enough space for a 9-digit suffix to ensure uniqueness!
Bram Moolenaar964b3742019-05-24 18:54:09 +02002296 ok_name = alloc(STRLEN(name) + 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297
2298 STRCPY(ok_name, name);
2299 p = ok_name + STRLEN(name);
2300
2301 for (;;)
2302 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002303 // This is inefficient - we're doing an EnumWindows loop for each
2304 // possible name. It would be better to grab all names in one go,
2305 // and scan the list each time...
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 hwnd = findServer(ok_name);
2307 if (hwnd == 0)
2308 break;
2309
2310 ++i;
2311 if (i >= 1000)
2312 break;
2313
2314 sprintf((char *)p, "%d", i);
2315 }
2316
2317 if (hwnd != 0)
2318 vim_free(ok_name);
2319 else
2320 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002321 // Remember the name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 serverName = ok_name;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002323# ifdef FEAT_TITLE
Bram Moolenaar0f873732019-12-05 20:28:46 +01002324 need_maketitle = TRUE; // update Vim window title later
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002325# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326
Bram Moolenaar0f873732019-12-05 20:28:46 +01002327 // Update the message window title
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002328 SetWindowText(message_window, (LPCSTR)ok_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002330# ifdef FEAT_EVAL
Bram Moolenaar0f873732019-12-05 20:28:46 +01002331 // Set the servername variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 set_vim_var_string(VV_SEND_SERVER, serverName, -1);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002333# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 }
2335}
2336
2337 char_u *
2338serverGetVimNames(void)
2339{
2340 garray_T ga;
2341
2342 ga_init2(&ga, 1, 100);
2343
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002344 enum_windows(enumWindowsGetNames, (LPARAM)(&ga));
Bram Moolenaar269ec652004-07-29 08:43:53 +00002345 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346
2347 return ga.ga_data;
2348}
2349
2350 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002351serverSendReply(
Bram Moolenaar0f873732019-12-05 20:28:46 +01002352 char_u *name, // Where to send.
2353 char_u *reply) // What to send.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354{
2355 HWND target;
2356 COPYDATASTRUCT data;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002357 long_u n = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358
Bram Moolenaar0f873732019-12-05 20:28:46 +01002359 // The "name" argument is a magic cookie obtained from expand("<client>").
2360 // It should be of the form 0xXXXXX - i.e. a C hex literal, which is the
2361 // value of the client's message window HWND.
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002362 sscanf((char *)name, SCANF_HEX_LONG_U, &n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 if (n == 0)
2364 return -1;
2365
2366 target = (HWND)n;
2367 if (!IsWindow(target))
2368 return -1;
2369
2370 data.dwData = COPYDATA_REPLY;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002371 data.cbData = (DWORD)STRLEN(reply) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 data.lpData = reply;
2373
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002374 serverSendEnc(target);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 if (SendMessage(target, WM_COPYDATA, (WPARAM)message_window,
2376 (LPARAM)(&data)))
2377 return 0;
2378
2379 return -1;
2380}
2381
2382 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002383serverSendToVim(
Bram Moolenaar0f873732019-12-05 20:28:46 +01002384 char_u *name, // Where to send.
2385 char_u *cmd, // What to send.
2386 char_u **result, // Result of eval'ed expression
2387 void *ptarget, // HWND of server
2388 int asExpr, // Expression or keys?
2389 int timeout, // timeout in seconds or zero
2390 int silent) // don't complain about no server
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391{
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002392 HWND target;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 COPYDATASTRUCT data;
2394 char_u *retval = NULL;
2395 int retcode = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002396 char_u altname_buf[MAX_PATH];
2397
Bram Moolenaar0f873732019-12-05 20:28:46 +01002398 // Execute locally if no display or target is ourselves
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01002399 if (serverName != NULL && STRICMP(name, serverName) == 0)
2400 return sendToLocalVim(cmd, asExpr, result);
2401
Bram Moolenaar0f873732019-12-05 20:28:46 +01002402 // If the server name does not end in a digit then we look for an
2403 // alternate name. e.g. when "name" is GVIM the we may find GVIM2.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002404 if (STRLEN(name) > 1 && !vim_isdigit(name[STRLEN(name) - 1]))
2405 altname_buf_ptr = altname_buf;
2406 altname_buf[0] = NUL;
2407 target = findServer(name);
2408 altname_buf_ptr = NULL;
2409 if (target == 0 && altname_buf[0] != NUL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01002410 // Use another server name we found.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002411 target = findServer(altname_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412
2413 if (target == 0)
2414 {
2415 if (!silent)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002416 semsg(_(e_noserver), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 return -1;
2418 }
2419
2420 if (ptarget)
2421 *(HWND *)ptarget = target;
2422
2423 data.dwData = asExpr ? COPYDATA_EXPR : COPYDATA_KEYS;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002424 data.cbData = (DWORD)STRLEN(cmd) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 data.lpData = cmd;
2426
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002427 serverSendEnc(target);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 if (SendMessage(target, WM_COPYDATA, (WPARAM)message_window,
2429 (LPARAM)(&data)) == 0)
2430 return -1;
2431
2432 if (asExpr)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002433 retval = serverGetReply(target, &retcode, TRUE, TRUE, timeout);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434
2435 if (result == NULL)
2436 vim_free(retval);
2437 else
Bram Moolenaar0f873732019-12-05 20:28:46 +01002438 *result = retval; // Caller assumes responsibility for freeing
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439
2440 return retcode;
2441}
2442
2443/*
2444 * Bring the server to the foreground.
2445 */
2446 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002447serverForeground(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448{
2449 HWND target = findServer(name);
2450
2451 if (target != 0)
2452 SetForegroundWindow(target);
2453}
2454
Bram Moolenaar0f873732019-12-05 20:28:46 +01002455// Replies from server need to be stored until the client picks them up via
2456// remote_read(). So we maintain a list of server-id/reply pairs.
2457// Note that there could be multiple replies from one server pending if the
2458// client is slow picking them up.
2459// We just store the replies in a simple list. When we remove an entry, we
2460// move list entries down to fill the gap.
2461// The server ID is simply the HWND.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462typedef struct
2463{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002464 HWND server; // server window
2465 char_u *reply; // reply string
2466 int expr_result; // 0 for REPLY, 1 for RESULT 2 for error
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002467} reply_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468
2469static garray_T reply_list = {0, 0, sizeof(reply_T), 5, 0};
2470
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002471# define REPLY_ITEM(i) ((reply_T *)(reply_list.ga_data) + (i))
2472# define REPLY_COUNT (reply_list.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473
Bram Moolenaar0f873732019-12-05 20:28:46 +01002474// Flag which is used to wait for a reply
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475static int reply_received = 0;
2476
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002477/*
2478 * Store a reply. "reply" must be allocated memory (or NULL).
2479 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 static int
2481save_reply(HWND server, char_u *reply, int expr)
2482{
2483 reply_T *rep;
2484
2485 if (ga_grow(&reply_list, 1) == FAIL)
2486 return FAIL;
2487
2488 rep = REPLY_ITEM(REPLY_COUNT);
2489 rep->server = server;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002490 rep->reply = reply;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 rep->expr_result = expr;
2492 if (rep->reply == NULL)
2493 return FAIL;
2494
2495 ++REPLY_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496 reply_received = 1;
2497 return OK;
2498}
2499
2500/*
2501 * Get a reply from server "server".
2502 * When "expr_res" is non NULL, get the result of an expression, otherwise a
2503 * server2client() message.
2504 * When non NULL, point to return code. 0 => OK, -1 => ERROR
2505 * If "remove" is TRUE, consume the message, the caller must free it then.
2506 * if "wait" is TRUE block until a message arrives (or the server exits).
2507 */
2508 char_u *
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002509serverGetReply(HWND server, int *expr_res, int remove, int wait, int timeout)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510{
2511 int i;
2512 char_u *reply;
2513 reply_T *rep;
Bram Moolenaar15e737f2017-03-18 21:22:47 +01002514 int did_process = FALSE;
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002515 time_t start;
2516 time_t now;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517
Bram Moolenaar0f873732019-12-05 20:28:46 +01002518 // When waiting, loop until the message waiting for is received.
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002519 time(&start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 for (;;)
2521 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002522 // Reset this here, in case a message arrives while we are going
2523 // through the already received messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 reply_received = 0;
2525
2526 for (i = 0; i < REPLY_COUNT; ++i)
2527 {
2528 rep = REPLY_ITEM(i);
2529 if (rep->server == server
2530 && ((rep->expr_result != 0) == (expr_res != NULL)))
2531 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002532 // Save the values we've found for later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 reply = rep->reply;
2534 if (expr_res != NULL)
2535 *expr_res = rep->expr_result == 1 ? 0 : -1;
2536
2537 if (remove)
2538 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002539 // Move the rest of the list down to fill the gap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 mch_memmove(rep, rep + 1,
2541 (REPLY_COUNT - i - 1) * sizeof(reply_T));
2542 --REPLY_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 }
2544
Bram Moolenaar0f873732019-12-05 20:28:46 +01002545 // Return the reply to the caller, who takes on responsibility
2546 // for freeing it if "remove" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 return reply;
2548 }
2549 }
2550
Bram Moolenaar0f873732019-12-05 20:28:46 +01002551 // If we got here, we didn't find a reply. Return immediately if the
2552 // "wait" parameter isn't set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 if (!wait)
Bram Moolenaar15e737f2017-03-18 21:22:47 +01002554 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002555 // Process pending messages once. Without this, looping on
2556 // remote_peek() would never get the reply.
Bram Moolenaar15e737f2017-03-18 21:22:47 +01002557 if (!did_process)
2558 {
2559 did_process = TRUE;
2560 serverProcessPendingMessages();
2561 continue;
2562 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 break;
Bram Moolenaar15e737f2017-03-18 21:22:47 +01002564 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565
Bram Moolenaar0f873732019-12-05 20:28:46 +01002566 // We need to wait for a reply. Enter a message loop until the
2567 // "reply_received" flag gets set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568
Bram Moolenaar0f873732019-12-05 20:28:46 +01002569 // Loop until we receive a reply
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 while (reply_received == 0)
2571 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002572# ifdef FEAT_TIMERS
Bram Moolenaar0f873732019-12-05 20:28:46 +01002573 // TODO: use the return value to decide how long to wait.
Bram Moolenaar42205552017-03-18 19:42:22 +01002574 check_due_timer();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002575# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002576 time(&now);
2577 if (timeout > 0 && (now - start) >= timeout)
2578 break;
2579
Bram Moolenaar0f873732019-12-05 20:28:46 +01002580 // Wait for a SendMessage() call to us. This could be the reply
2581 // we are waiting for. Use a timeout of a second, to catch the
2582 // situation that the server died unexpectedly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 MsgWaitForMultipleObjects(0, NULL, TRUE, 1000, QS_ALLINPUT);
2584
Bram Moolenaar0f873732019-12-05 20:28:46 +01002585 // If the server has died, give up
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 if (!IsWindow(server))
2587 return NULL;
2588
2589 serverProcessPendingMessages();
2590 }
2591 }
2592
2593 return NULL;
2594}
2595
2596/*
2597 * Process any messages in the Windows message queue.
2598 */
2599 void
2600serverProcessPendingMessages(void)
2601{
2602 MSG msg;
2603
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02002604 while (pPeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 {
2606 TranslateMessage(&msg);
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02002607 pDispatchMessage(&msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 }
2609}
2610
Bram Moolenaar0f873732019-12-05 20:28:46 +01002611#endif // FEAT_CLIENTSERVER
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612
2613#if defined(FEAT_GUI) || (defined(FEAT_PRINTER) && !defined(FEAT_POSTSCRIPT)) \
2614 || defined(PROTO)
2615
2616struct charset_pair
2617{
2618 char *name;
2619 BYTE charset;
2620};
2621
2622static struct charset_pair
2623charset_pairs[] =
2624{
2625 {"ANSI", ANSI_CHARSET},
2626 {"CHINESEBIG5", CHINESEBIG5_CHARSET},
2627 {"DEFAULT", DEFAULT_CHARSET},
2628 {"HANGEUL", HANGEUL_CHARSET},
2629 {"OEM", OEM_CHARSET},
2630 {"SHIFTJIS", SHIFTJIS_CHARSET},
2631 {"SYMBOL", SYMBOL_CHARSET},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 {"ARABIC", ARABIC_CHARSET},
2633 {"BALTIC", BALTIC_CHARSET},
2634 {"EASTEUROPE", EASTEUROPE_CHARSET},
2635 {"GB2312", GB2312_CHARSET},
2636 {"GREEK", GREEK_CHARSET},
2637 {"HEBREW", HEBREW_CHARSET},
2638 {"JOHAB", JOHAB_CHARSET},
2639 {"MAC", MAC_CHARSET},
2640 {"RUSSIAN", RUSSIAN_CHARSET},
2641 {"THAI", THAI_CHARSET},
2642 {"TURKISH", TURKISH_CHARSET},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002643# ifdef VIETNAMESE_CHARSET
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 {"VIETNAMESE", VIETNAMESE_CHARSET},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002645# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 {NULL, 0}
2647};
2648
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002649struct quality_pair
2650{
2651 char *name;
2652 DWORD quality;
2653};
2654
2655static struct quality_pair
2656quality_pairs[] = {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002657# ifdef CLEARTYPE_QUALITY
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002658 {"CLEARTYPE", CLEARTYPE_QUALITY},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002659# endif
2660# ifdef ANTIALIASED_QUALITY
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002661 {"ANTIALIASED", ANTIALIASED_QUALITY},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002662# endif
2663# ifdef NONANTIALIASED_QUALITY
Bram Moolenaar73a733e2016-05-11 21:05:05 +02002664 {"NONANTIALIASED", NONANTIALIASED_QUALITY},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002665# endif
2666# ifdef PROOF_QUALITY
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002667 {"PROOF", PROOF_QUALITY},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002668# endif
2669# ifdef DRAFT_QUALITY
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002670 {"DRAFT", DRAFT_QUALITY},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002671# endif
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002672 {"DEFAULT", DEFAULT_QUALITY},
2673 {NULL, 0}
2674};
2675
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676/*
2677 * Convert a charset ID to a name.
2678 * Return NULL when not recognized.
2679 */
2680 char *
2681charset_id2name(int id)
2682{
2683 struct charset_pair *cp;
2684
2685 for (cp = charset_pairs; cp->name != NULL; ++cp)
2686 if ((BYTE)id == cp->charset)
2687 break;
2688 return cp->name;
2689}
2690
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002691/*
2692 * Convert a quality ID to a name.
2693 * Return NULL when not recognized.
2694 */
2695 char *
2696quality_id2name(DWORD id)
2697{
2698 struct quality_pair *qp;
2699
2700 for (qp = quality_pairs; qp->name != NULL; ++qp)
2701 if (id == qp->quality)
2702 break;
2703 return qp->name;
2704}
2705
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002706static const LOGFONTW s_lfDefault =
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707{
2708 -12, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, DEFAULT_CHARSET,
2709 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
2710 PROOF_QUALITY, FIXED_PITCH | FF_DONTCARE,
Bram Moolenaar0f873732019-12-05 20:28:46 +01002711 L"Fixedsys" // see _ReadVimIni
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712};
2713
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002714// Initialise the "current height" to -12 (same as s_lfDefault) just
2715// in case the user specifies a font in "guifont" with no size before a font
2716// with an explicit size has been set. This defaults the size to this value
2717// (-12 equates to roughly 9pt).
2718int current_font_height = -12; // also used in gui_w32.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719
Bram Moolenaar0f873732019-12-05 20:28:46 +01002720/*
2721 * Convert a string representing a point size into pixels. The string should
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 * be a positive decimal number, with an optional decimal point (eg, "12", or
2723 * "10.5"). The pixel value is returned, and a pointer to the next unconverted
2724 * character is stored in *end. The flag "vertical" says whether this
2725 * calculation is for a vertical (height) size or a horizontal (width) one.
2726 */
2727 static int
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002728points_to_pixels(WCHAR *str, WCHAR **end, int vertical, long_i pprinter_dc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729{
2730 int pixels;
2731 int points = 0;
2732 int divisor = 0;
2733 HWND hwnd = (HWND)0;
2734 HDC hdc;
2735 HDC printer_dc = (HDC)pprinter_dc;
2736
2737 while (*str != NUL)
2738 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002739 if (*str == L'.' && divisor == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002741 // Start keeping a divisor, for later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 divisor = 1;
2743 }
2744 else
2745 {
2746 if (!VIM_ISDIGIT(*str))
2747 break;
2748
2749 points *= 10;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002750 points += *str - L'0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 divisor *= 10;
2752 }
2753 ++str;
2754 }
2755
2756 if (divisor == 0)
2757 divisor = 1;
2758
2759 if (printer_dc == NULL)
2760 {
2761 hwnd = GetDesktopWindow();
2762 hdc = GetWindowDC(hwnd);
2763 }
2764 else
2765 hdc = printer_dc;
2766
2767 pixels = MulDiv(points,
2768 GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX),
2769 72 * divisor);
2770
2771 if (printer_dc == NULL)
2772 ReleaseDC(hwnd, hdc);
2773
2774 *end = str;
2775 return pixels;
2776}
2777
2778 static int CALLBACK
2779font_enumproc(
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002780 ENUMLOGFONTW *elf,
2781 NEWTEXTMETRICW *ntm UNUSED,
2782 DWORD type UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 LPARAM lparam)
2784{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002785 // Return value:
2786 // 0 = terminate now (monospace & ANSI)
2787 // 1 = continue, still no luck...
2788 // 2 = continue, but we have an acceptable LOGFONTW
2789 // (monospace, not ANSI)
2790 // We use these values, as EnumFontFamilies returns 1 if the
2791 // callback function is never called. So, we check the return as
2792 // 0 = perfect, 2 = OK, 1 = no good...
2793 // It's not pretty, but it works!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002795 LOGFONTW *lf = (LOGFONTW *)(lparam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002797# ifndef FEAT_PROPORTIONAL_FONTS
Bram Moolenaar0f873732019-12-05 20:28:46 +01002798 // Ignore non-monospace fonts without further ado
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 if ((ntm->tmPitchAndFamily & 1) != 0)
2800 return 1;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002801# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802
Bram Moolenaar0f873732019-12-05 20:28:46 +01002803 // Remember this LOGFONTW as a "possible"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804 *lf = elf->elfLogFont;
2805
Bram Moolenaar0f873732019-12-05 20:28:46 +01002806 // Terminate the scan as soon as we find an ANSI font
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 if (lf->lfCharSet == ANSI_CHARSET
2808 || lf->lfCharSet == OEM_CHARSET
2809 || lf->lfCharSet == DEFAULT_CHARSET)
2810 return 0;
2811
Bram Moolenaar0f873732019-12-05 20:28:46 +01002812 // Continue the scan - we have a non-ANSI font
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 return 2;
2814}
2815
2816 static int
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002817init_logfont(LOGFONTW *lf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818{
2819 int n;
2820 HWND hwnd = GetDesktopWindow();
2821 HDC hdc = GetWindowDC(hwnd);
2822
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002823 n = EnumFontFamiliesW(hdc,
2824 lf->lfFaceName,
2825 (FONTENUMPROCW)font_enumproc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 (LPARAM)lf);
2827
2828 ReleaseDC(hwnd, hdc);
2829
Bram Moolenaar0f873732019-12-05 20:28:46 +01002830 // If we couldn't find a usable font, return failure
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 if (n == 1)
2832 return FAIL;
2833
Bram Moolenaar0f873732019-12-05 20:28:46 +01002834 // Tidy up the rest of the LOGFONTW structure. We set to a basic
2835 // font - get_logfont() sets bold, italic, etc based on the user's
2836 // input.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837 lf->lfHeight = current_font_height;
2838 lf->lfWidth = 0;
2839 lf->lfItalic = FALSE;
2840 lf->lfUnderline = FALSE;
2841 lf->lfStrikeOut = FALSE;
2842 lf->lfWeight = FW_NORMAL;
2843
Bram Moolenaar0f873732019-12-05 20:28:46 +01002844 // Return success
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 return OK;
2846}
2847
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002848/*
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002849 * Compare a UTF-16 string and an ASCII string literally.
2850 * Only works all the code points are inside ASCII range.
2851 */
2852 static int
2853utf16ascncmp(const WCHAR *w, const char *p, size_t n)
2854{
2855 size_t i;
2856
2857 for (i = 0; i < n; i++)
2858 {
2859 if (w[i] == 0 || w[i] != p[i])
2860 return w[i] - p[i];
2861 }
2862 return 0;
2863}
2864
2865/*
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002866 * Get font info from "name" into logfont "lf".
2867 * Return OK for a valid name, FAIL otherwise.
2868 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 int
2870get_logfont(
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002871 LOGFONTW *lf,
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002872 char_u *name,
2873 HDC printer_dc,
2874 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002876 WCHAR *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 int i;
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002878 int ret = FAIL;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002879 static LOGFONTW *lastlf = NULL;
2880 WCHAR *wname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881
2882 *lf = s_lfDefault;
2883 if (name == NULL)
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002884 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002886 wname = enc_to_utf16(name, NULL);
2887 if (wname == NULL)
2888 return FAIL;
2889
2890 if (wcscmp(wname, L"*") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002892# if defined(FEAT_GUI_MSWIN)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002893 CHOOSEFONTW cf;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002894 // if name is "*", bring up std font dialog:
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02002895 vim_memset(&cf, 0, sizeof(cf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 cf.lStructSize = sizeof(cf);
2897 cf.hwndOwner = s_hwnd;
2898 cf.Flags = CF_SCREENFONTS | CF_FIXEDPITCHONLY | CF_INITTOLOGFONTSTRUCT;
2899 if (lastlf != NULL)
2900 *lf = *lastlf;
2901 cf.lpLogFont = lf;
2902 cf.nFontType = 0 ; //REGULAR_FONTTYPE;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002903 if (ChooseFontW(&cf))
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002904 ret = OK;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002905# endif
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002906 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 }
2908
2909 /*
2910 * Split name up, it could be <name>:h<height>:w<width> etc.
2911 */
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002912 for (p = wname; *p && *p != L':'; p++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002914 if (p - wname + 1 >= LF_FACESIZE)
Bram Moolenaar0f873732019-12-05 20:28:46 +01002915 goto theend; // Name too long
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002916 lf->lfFaceName[p - wname] = *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 }
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002918 if (p != wname)
2919 lf->lfFaceName[p - wname] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920
Bram Moolenaar0f873732019-12-05 20:28:46 +01002921 // First set defaults
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 lf->lfHeight = -12;
2923 lf->lfWidth = 0;
2924 lf->lfWeight = FW_NORMAL;
2925 lf->lfItalic = FALSE;
2926 lf->lfUnderline = FALSE;
2927 lf->lfStrikeOut = FALSE;
2928
2929 /*
2930 * If the font can't be found, try replacing '_' by ' '.
2931 */
2932 if (init_logfont(lf) == FAIL)
2933 {
2934 int did_replace = FALSE;
2935
2936 for (i = 0; lf->lfFaceName[i]; ++i)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002937 if (lf->lfFaceName[i] == L'_')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002939 lf->lfFaceName[i] = L' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 did_replace = TRUE;
2941 }
2942 if (!did_replace || init_logfont(lf) == FAIL)
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002943 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944 }
2945
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002946 while (*p == L':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 p++;
2948
Bram Moolenaar0f873732019-12-05 20:28:46 +01002949 // Set the values found after ':'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 while (*p)
2951 {
2952 switch (*p++)
2953 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002954 case L'h':
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002955 lf->lfHeight = - points_to_pixels(p, &p, TRUE, (long_i)printer_dc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002957 case L'w':
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002958 lf->lfWidth = points_to_pixels(p, &p, FALSE, (long_i)printer_dc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 break;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02002960 case L'W':
2961 lf->lfWeight = wcstol(p, &p, 10);
2962 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002963 case L'b':
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964 lf->lfWeight = FW_BOLD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002966 case L'i':
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967 lf->lfItalic = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002969 case L'u':
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 lf->lfUnderline = TRUE;
2971 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002972 case L's':
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 lf->lfStrikeOut = TRUE;
2974 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002975 case L'c':
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 {
2977 struct charset_pair *cp;
2978
2979 for (cp = charset_pairs; cp->name != NULL; ++cp)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002980 if (utf16ascncmp(p, cp->name, strlen(cp->name)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 {
2982 lf->lfCharSet = cp->charset;
2983 p += strlen(cp->name);
2984 break;
2985 }
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002986 if (cp->name == NULL && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002988 char_u *s = utf16_to_enc(p, NULL);
2989 semsg(_("E244: Illegal charset name \"%s\" in font name \"%s\""), s, name);
2990 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991 break;
2992 }
2993 break;
2994 }
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002995 case L'q':
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002996 {
2997 struct quality_pair *qp;
2998
2999 for (qp = quality_pairs; qp->name != NULL; ++qp)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003000 if (utf16ascncmp(p, qp->name, strlen(qp->name)) == 0)
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003001 {
3002 lf->lfQuality = qp->quality;
3003 p += strlen(qp->name);
3004 break;
3005 }
3006 if (qp->name == NULL && verbose)
3007 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003008 char_u *s = utf16_to_enc(p, NULL);
3009 semsg(_("E244: Illegal quality name \"%s\" in font name \"%s\""), s, name);
3010 vim_free(s);
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003011 break;
3012 }
3013 break;
3014 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015 default:
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003016 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003017 semsg(_("E245: Illegal char '%c' in font name \"%s\""), p[-1], name);
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003018 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019 }
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003020 while (*p == L':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 p++;
3022 }
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003023 ret = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025theend:
Bram Moolenaar0f873732019-12-05 20:28:46 +01003026 // ron: init lastlf
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003027 if (ret == OK && printer_dc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028 {
3029 vim_free(lastlf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003030 lastlf = ALLOC_ONE(LOGFONTW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031 if (lastlf != NULL)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003032 mch_memmove(lastlf, lf, sizeof(LOGFONTW));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 }
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01003034 vim_free(wname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003036 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037}
3038
Bram Moolenaar0f873732019-12-05 20:28:46 +01003039#endif // defined(FEAT_GUI) || defined(FEAT_PRINTER)
Bram Moolenaarf12d9832016-01-29 21:11:25 +01003040
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003041#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaarf12d9832016-01-29 21:11:25 +01003042/*
3043 * Initialize the Winsock dll.
3044 */
3045 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003046channel_init_winsock(void)
Bram Moolenaarf12d9832016-01-29 21:11:25 +01003047{
3048 WSADATA wsaData;
3049 int wsaerr;
3050
3051 if (WSInitialized)
3052 return;
3053
3054 wsaerr = WSAStartup(MAKEWORD(2, 2), &wsaData);
3055 if (wsaerr == 0)
3056 WSInitialized = TRUE;
3057}
3058#endif