blob: 57ac5828ca6a6101979e51487ee9244d103890ee [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * os_mswin.c
12 *
Bram Moolenaarcf7164a2016-02-20 13:55:06 +010013 * Routines for Win32.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014 */
15
Bram Moolenaar071d4272004-06-13 20:20:40 +000016#include "vim.h"
17
Bram Moolenaar071d4272004-06-13 20:20:40 +000018#include <sys/types.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000019#include <signal.h>
20#include <limits.h>
Bram Moolenaar82881492012-11-20 16:53:39 +010021#ifndef PROTO
22# include <process.h>
23#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024
25#undef chdir
26#ifdef __GNUC__
27# ifndef __MINGW32__
28# include <dirent.h>
29# endif
30#else
31# include <direct.h>
32#endif
33
Bram Moolenaar82881492012-11-20 16:53:39 +010034#ifndef PROTO
Bram Moolenaar651fca82021-11-29 20:39:38 +000035# if !defined(FEAT_GUI_MSWIN)
Bram Moolenaar82881492012-11-20 16:53:39 +010036# include <shellapi.h>
37# endif
38
39# if defined(FEAT_PRINTER) && !defined(FEAT_POSTSCRIPT)
40# include <dlgs.h>
Bram Moolenaarcea912a2016-10-12 14:20:24 +020041# include <winspool.h>
Bram Moolenaar82881492012-11-20 16:53:39 +010042# include <commdlg.h>
Bram Moolenaarb04a98f2016-12-01 20:32:29 +010043# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000044
Bram Moolenaar0f873732019-12-05 20:28:46 +010045#endif // PROTO
Bram Moolenaar071d4272004-06-13 20:20:40 +000046
47#ifdef __MINGW32__
48# ifndef FROM_LEFT_1ST_BUTTON_PRESSED
49# define FROM_LEFT_1ST_BUTTON_PRESSED 0x0001
50# endif
51# ifndef RIGHTMOST_BUTTON_PRESSED
52# define RIGHTMOST_BUTTON_PRESSED 0x0002
53# endif
54# ifndef FROM_LEFT_2ND_BUTTON_PRESSED
55# define FROM_LEFT_2ND_BUTTON_PRESSED 0x0004
56# endif
57# ifndef FROM_LEFT_3RD_BUTTON_PRESSED
58# define FROM_LEFT_3RD_BUTTON_PRESSED 0x0008
59# endif
60# ifndef FROM_LEFT_4TH_BUTTON_PRESSED
61# define FROM_LEFT_4TH_BUTTON_PRESSED 0x0010
62# endif
63
64/*
65 * EventFlags
66 */
67# ifndef MOUSE_MOVED
68# define MOUSE_MOVED 0x0001
69# endif
70# ifndef DOUBLE_CLICK
71# define DOUBLE_CLICK 0x0002
72# endif
73#endif
74
75/*
76 * When generating prototypes for Win32 on Unix, these lines make the syntax
77 * errors disappear. They do not need to be correct.
78 */
79#ifdef PROTO
Bram Moolenaar912bc4a2019-12-01 18:58:11 +010080# define WINAPI
81# define WINBASEAPI
Bram Moolenaar071d4272004-06-13 20:20:40 +000082typedef int BOOL;
83typedef int CALLBACK;
84typedef int COLORREF;
85typedef int CONSOLE_CURSOR_INFO;
86typedef int COORD;
87typedef int DWORD;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +010088typedef int ENUMLOGFONTW;
Bram Moolenaar071d4272004-06-13 20:20:40 +000089typedef int HANDLE;
90typedef int HDC;
91typedef int HFONT;
92typedef int HICON;
93typedef int HWND;
94typedef int INPUT_RECORD;
95typedef int KEY_EVENT_RECORD;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +010096typedef int LOGFONTW;
Bram Moolenaar071d4272004-06-13 20:20:40 +000097typedef int LPARAM;
98typedef int LPBOOL;
99typedef int LPCSTR;
100typedef int LPCWSTR;
Bram Moolenaarb9cdb372019-04-17 18:24:35 +0200101typedef int LPDWORD;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102typedef int LPSTR;
103typedef int LPTSTR;
Bram Moolenaarb9cdb372019-04-17 18:24:35 +0200104typedef int LPVOID;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000105typedef int LPWSTR;
106typedef int LRESULT;
107typedef int MOUSE_EVENT_RECORD;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +0100108typedef int NEWTEXTMETRICW;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000109typedef int PACL;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200110typedef int PRINTDLGW;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000111typedef int PSECURITY_DESCRIPTOR;
112typedef int PSID;
113typedef int SECURITY_INFORMATION;
114typedef int SHORT;
115typedef int SMALL_RECT;
116typedef int TEXTMETRIC;
117typedef int UINT;
118typedef int WCHAR;
Bram Moolenaarc447d8d2018-12-18 21:56:28 +0100119typedef int WNDENUMPROC;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120typedef int WORD;
121typedef int WPARAM;
122typedef void VOID;
123#endif
124
Bram Moolenaar0f873732019-12-05 20:28:46 +0100125// Record all output and all keyboard & mouse input
126// #define MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127
128#ifdef MCH_WRITE_DUMP
129FILE* fdDump = NULL;
130#endif
131
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200132#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133extern char g_szOrigTitle[];
134#endif
135
136#ifdef FEAT_GUI
137extern HWND s_hwnd;
138#else
Bram Moolenaar0f873732019-12-05 20:28:46 +0100139static HWND s_hwnd = 0; // console window handle, set by GetConsoleHwnd()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140#endif
141
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100142#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar0f873732019-12-05 20:28:46 +0100143int WSInitialized = FALSE; // WinSock is initialized
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100144#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145
Bram Moolenaar0f873732019-12-05 20:28:46 +0100146// Don't generate prototypes here, because some systems do have these
147// functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148#if defined(__GNUC__) && !defined(PROTO)
149# ifndef __MINGW32__
150int _stricoll(char *a, char *b)
151{
152 // the ANSI-ish correct way is to use strxfrm():
153 char a_buff[512], b_buff[512]; // file names, so this is enough on Win32
154 strxfrm(a_buff, a, 512);
155 strxfrm(b_buff, b, 512);
156 return strcoll(a_buff, b_buff);
157}
158
159char * _fullpath(char *buf, char *fname, int len)
160{
161 LPTSTR toss;
162
163 return (char *)GetFullPathName(fname, len, buf, &toss);
164}
165# endif
166
Bram Moolenaaraf62ff32013-03-19 14:48:29 +0100167# if !defined(__MINGW32__) || (__GNUC__ < 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168int _chdrive(int drive)
169{
170 char temp [3] = "-:";
171 temp[0] = drive + 'A' - 1;
172 return !SetCurrentDirectory(temp);
173}
Bram Moolenaaraf62ff32013-03-19 14:48:29 +0100174# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175#endif
176
177
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200178#ifndef PROTO
179/*
180 * Save the instance handle of the exe/dll.
181 */
182 void
183SaveInst(HINSTANCE hInst)
184{
185 g_hinst = hInst;
186}
187#endif
188
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189#if defined(FEAT_GUI_MSWIN) || defined(PROTO)
190/*
191 * GUI version of mch_exit().
192 * Shut down and exit with status `r'
193 * Careful: mch_exit() may be called before mch_init()!
194 */
195 void
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200196mch_exit_g(int r)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197{
Bram Moolenaar955f1982017-02-05 15:10:51 +0100198 exiting = TRUE;
199
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200 display_errors();
201
Bram Moolenaar0f873732019-12-05 20:28:46 +0100202 ml_close_all(TRUE); // remove all memfiles
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203
204# ifdef FEAT_OLE
205 UninitOLE();
206# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100207# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208 if (WSInitialized)
209 {
210 WSInitialized = FALSE;
211 WSACleanup();
212 }
213# endif
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100214# ifdef DYNAMIC_GETTEXT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215 dyn_libintl_end();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100216# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217
218 if (gui.in_use)
219 gui_exit(r);
Bram Moolenaar85c79d32007-02-20 01:59:20 +0000220
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100221# ifdef EXITFREE
Bram Moolenaar85c79d32007-02-20 01:59:20 +0000222 free_all_mem();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100223# endif
Bram Moolenaar85c79d32007-02-20 01:59:20 +0000224
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225 exit(r);
226}
227
Bram Moolenaar0f873732019-12-05 20:28:46 +0100228#endif // FEAT_GUI_MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229
230
231/*
232 * Init the tables for toupper() and tolower().
233 */
234 void
235mch_early_init(void)
236{
237 int i;
238
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239 PlatformId();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240
Bram Moolenaar0f873732019-12-05 20:28:46 +0100241 // Init the tables for toupper() and tolower()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242 for (i = 0; i < 256; ++i)
243 toupper_tab[i] = tolower_tab[i] = i;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100244 CharUpperBuff((LPSTR)toupper_tab, 256);
245 CharLowerBuff((LPSTR)tolower_tab, 256);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246}
247
248
249/*
250 * Return TRUE if the input comes from a terminal, FALSE otherwise.
251 */
252 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100253mch_input_isatty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254{
255#ifdef FEAT_GUI_MSWIN
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200256# ifdef VIMDLL
257 if (gui.in_use)
258# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +0100259 return TRUE; // GUI always has a tty
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200260#endif
261#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262 if (isatty(read_cmd_fd))
263 return TRUE;
264 return FALSE;
265#endif
266}
267
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268/*
269 * mch_settitle(): set titlebar of our window
270 */
271 void
272mch_settitle(
273 char_u *title,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +0200274 char_u *icon UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275{
Bram Moolenaar651fca82021-11-29 20:39:38 +0000276#ifdef FEAT_GUI_MSWIN
277# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200278 if (gui.in_use)
Bram Moolenaar651fca82021-11-29 20:39:38 +0000279# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200280 {
281 gui_mch_settitle(title, icon);
282 return;
283 }
Bram Moolenaar651fca82021-11-29 20:39:38 +0000284#endif
285#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000286 if (title != NULL)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000287 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200288 WCHAR *wp = enc_to_utf16(title, NULL);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000289
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200290 if (wp == NULL)
291 return;
292
293 SetConsoleTitleW(wp);
294 vim_free(wp);
295 return;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000296 }
Bram Moolenaar651fca82021-11-29 20:39:38 +0000297#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000298}
299
300
301/*
302 * Restore the window/icon title.
303 * which is one of:
Bram Moolenaar40385db2018-08-07 22:31:44 +0200304 * SAVE_RESTORE_TITLE: Just restore title
305 * SAVE_RESTORE_ICON: Just restore icon (which we don't have)
306 * SAVE_RESTORE_BOTH: Restore title and icon (which we don't have)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307 */
308 void
Bram Moolenaar1266d672017-02-01 13:43:36 +0100309mch_restore_title(int which UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000310{
Bram Moolenaar651fca82021-11-29 20:39:38 +0000311#if !defined(FEAT_GUI_MSWIN) || defined(VIMDLL)
312# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200313 if (!gui.in_use)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100314# endif
Bram Moolenaar651fca82021-11-29 20:39:38 +0000315 SetConsoleTitle(g_szOrigTitle);
316#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317}
318
319
320/*
321 * Return TRUE if we can restore the title (we can)
322 */
323 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100324mch_can_restore_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325{
326 return TRUE;
327}
328
329
330/*
331 * Return TRUE if we can restore the icon title (we can't)
332 */
333 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100334mch_can_restore_icon(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335{
336 return FALSE;
337}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338
339
340/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000341 * Get absolute file name into buffer "buf" of length "len" bytes,
342 * turning all '/'s into '\\'s and getting the correct case of each component
343 * of the file name. Append a (back)slash to a directory name.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344 * When 'shellslash' set do it the other way around.
345 * Return OK or FAIL.
346 */
347 int
348mch_FullName(
349 char_u *fname,
350 char_u *buf,
351 int len,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100352 int force UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353{
354 int nResult = FAIL;
Bram Moolenaareae1b912019-05-09 15:12:55 +0200355 WCHAR *wname;
356 WCHAR wbuf[MAX_PATH];
357 char_u *cname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358
Bram Moolenaareae1b912019-05-09 15:12:55 +0200359 wname = enc_to_utf16(fname, NULL);
360 if (wname != NULL && _wfullpath(wbuf, wname, MAX_PATH) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361 {
Bram Moolenaareae1b912019-05-09 15:12:55 +0200362 cname = utf16_to_enc((short_u *)wbuf, NULL);
363 if (cname != NULL)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000364 {
Bram Moolenaareae1b912019-05-09 15:12:55 +0200365 vim_strncpy(buf, cname, len - 1);
366 nResult = OK;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368 }
Bram Moolenaareae1b912019-05-09 15:12:55 +0200369 vim_free(wname);
370 vim_free(cname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371
372#ifdef USE_FNAME_CASE
373 fname_case(buf, len);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000374#else
375 slash_adjust(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376#endif
377
378 return nResult;
379}
380
381
382/*
383 * Return TRUE if "fname" does not depend on the current directory.
384 */
385 int
386mch_isFullName(char_u *fname)
387{
Bram Moolenaar0ea74212020-12-11 20:10:50 +0100388 // A name like "d:/foo" and "//server/share" is absolute. "d:foo" is not.
389 // Another way to check is to use mch_FullName() and see if the result is
390 // the same as the name or mch_FullName() fails. However, this has quite a
391 // bit of overhead, so let's not do that.
Yegappan Lakshmanan6df0f272021-12-16 13:06:10 +0000392 if (*fname == NUL)
Yegappan Lakshmanan5a664fe2021-12-29 18:16:21 +0000393 return FALSE;
Bram Moolenaar0ea74212020-12-11 20:10:50 +0100394 return ((ASCII_ISALPHA(fname[0]) && fname[1] == ':'
395 && (fname[2] == '/' || fname[2] == '\\'))
396 || (fname[0] == fname[1] && (fname[0] == '/' || fname[0] == '\\')));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397}
398
399/*
400 * Replace all slashes by backslashes.
401 * This used to be the other way around, but MS-DOS sometimes has problems
402 * with slashes (e.g. in a command name). We can't have mixed slashes and
403 * backslashes, because comparing file names will not work correctly. The
404 * commands that use a file name should try to avoid the need to type a
405 * backslash twice.
406 * When 'shellslash' set do it the other way around.
Bram Moolenaarb4f6a462015-10-13 19:43:17 +0200407 * When the path looks like a URL leave it unmodified.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408 */
409 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100410slash_adjust(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411{
Bram Moolenaarb4f6a462015-10-13 19:43:17 +0200412 if (path_with_url(p))
413 return;
Bram Moolenaar39d21e32017-08-05 23:09:31 +0200414
415 if (*p == '`')
416 {
Bram Moolenaar116a0f82017-08-07 21:17:57 +0200417 size_t len = STRLEN(p);
418
Bram Moolenaar0f873732019-12-05 20:28:46 +0100419 // don't replace backslash in backtick quoted strings
Bram Moolenaar39d21e32017-08-05 23:09:31 +0200420 if (len > 2 && *(p + len - 1) == '`')
421 return;
422 }
423
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +0000424 while (*p)
425 {
426 if (*p == psepcN)
427 *p = psepc;
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100428 MB_PTR_ADV(p);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +0000429 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430}
431
K.Takatac351dc12022-01-24 11:24:08 +0000432// Use 64-bit stat functions.
433#undef stat
434#undef _stat
435#undef _wstat
436#undef _fstat
437#define stat _stat64
438#define _stat _stat64
439#define _wstat _wstat64
440#define _fstat _fstat64
Bram Moolenaar8962fda2013-09-29 19:05:21 +0200441
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200442 static int
Bram Moolenaar8767f522016-07-01 17:17:39 +0200443wstat_symlink_aware(const WCHAR *name, stat_T *stp)
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200444{
Bram Moolenaara12a1612019-01-24 16:39:02 +0100445#if (defined(_MSC_VER) && (_MSC_VER < 1900)) || defined(__MINGW32__)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100446 // Work around for VC12 or earlier (and MinGW). _wstat() can't handle
447 // symlinks properly.
448 // VC9 or earlier: _wstat() doesn't support a symlink at all. It retrieves
449 // status of a symlink itself.
450 // VC10: _wstat() supports a symlink to a normal file, but it doesn't
451 // support a symlink to a directory (always returns an error).
452 // VC11 and VC12: _wstat() doesn't return an error for a symlink to a
453 // directory, but it doesn't set S_IFDIR flag.
454 // MinGW: Same as VC9.
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200455 int n;
456 BOOL is_symlink = FALSE;
457 HANDLE hFind, h;
458 DWORD attr = 0;
459 WIN32_FIND_DATAW findDataW;
460
461 hFind = FindFirstFileW(name, &findDataW);
462 if (hFind != INVALID_HANDLE_VALUE)
463 {
464 attr = findDataW.dwFileAttributes;
465 if ((attr & FILE_ATTRIBUTE_REPARSE_POINT)
466 && (findDataW.dwReserved0 == IO_REPARSE_TAG_SYMLINK))
467 is_symlink = TRUE;
468 FindClose(hFind);
469 }
470 if (is_symlink)
471 {
472 h = CreateFileW(name, FILE_READ_ATTRIBUTES,
473 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
474 OPEN_EXISTING,
475 (attr & FILE_ATTRIBUTE_DIRECTORY)
476 ? FILE_FLAG_BACKUP_SEMANTICS : 0,
477 NULL);
478 if (h != INVALID_HANDLE_VALUE)
479 {
480 int fd;
481
K.Takatac351dc12022-01-24 11:24:08 +0000482 fd = _open_osfhandle((intptr_t)h, _O_RDONLY);
Bram Moolenaar8767f522016-07-01 17:17:39 +0200483 n = _fstat(fd, (struct _stat *)stp);
Bram Moolenaarfce7b3d2016-01-19 19:00:32 +0100484 if ((n == 0) && (attr & FILE_ATTRIBUTE_DIRECTORY))
485 stp->st_mode = (stp->st_mode & ~S_IFREG) | S_IFDIR;
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200486 _close(fd);
487 return n;
488 }
489 }
Bram Moolenaara12a1612019-01-24 16:39:02 +0100490#endif
Bram Moolenaar8767f522016-07-01 17:17:39 +0200491 return _wstat(name, (struct _stat *)stp);
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200492}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493
494/*
495 * stat() can't handle a trailing '/' or '\', remove it first.
496 */
497 int
Bram Moolenaar8767f522016-07-01 17:17:39 +0200498vim_stat(const char *name, stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100500 // WinNT and later can use _MAX_PATH wide characters for a pathname, which
501 // means that the maximum pathname is _MAX_PATH * 3 bytes when 'enc' is
502 // UTF-8.
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100503 char_u buf[_MAX_PATH * 3 + 1];
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100504 char_u *p;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200505 WCHAR *wp;
506 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507
Bram Moolenaard2a203b2013-08-30 16:51:18 +0200508 vim_strncpy((char_u *)buf, (char_u *)name, sizeof(buf) - 1);
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100509 p = buf + STRLEN(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510 if (p > buf)
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100511 MB_PTR_BACK(buf, p);
Bram Moolenaarb1cb35f2014-01-10 13:05:20 +0100512
Bram Moolenaar0f873732019-12-05 20:28:46 +0100513 // Remove trailing '\\' except root path.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514 if (p > buf && (*p == '\\' || *p == '/') && p[-1] != ':')
515 *p = NUL;
Bram Moolenaarb1cb35f2014-01-10 13:05:20 +0100516
517 if ((buf[0] == '\\' && buf[1] == '\\') || (buf[0] == '/' && buf[1] == '/'))
518 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100519 // UNC root path must be followed by '\\'.
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100520 p = vim_strpbrk(buf + 2, (char_u *)"\\/");
Bram Moolenaarb1cb35f2014-01-10 13:05:20 +0100521 if (p != NULL)
522 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100523 p = vim_strpbrk(p + 1, (char_u *)"\\/");
Bram Moolenaarb1cb35f2014-01-10 13:05:20 +0100524 if (p == NULL)
525 STRCAT(buf, "\\");
526 }
527 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200529 wp = enc_to_utf16(buf, NULL);
530 if (wp == NULL)
531 return -1;
532
533 n = wstat_symlink_aware(wp, stp);
534 vim_free(wp);
535 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536}
537
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200538#if (defined(FEAT_GUI_MSWIN) && !defined(VIMDLL)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539 void
Bram Moolenaar26e86442020-05-17 14:06:16 +0200540mch_settmode(tmode_T tmode UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100542 // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543}
544
545 int
546mch_get_shellsize(void)
547{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100548 // never used
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549 return OK;
550}
551
552 void
553mch_set_shellsize(void)
554{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100555 // never used
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556}
557
558/*
559 * Rows and/or Columns has changed.
560 */
561 void
562mch_new_shellsize(void)
563{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100564 // never used
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565}
566
567#endif
568
569/*
570 * We have no job control, so fake it by starting a new shell.
571 */
572 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100573mch_suspend(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574{
575 suspend_shell();
576}
577
578#if defined(USE_MCH_ERRMSG) || defined(PROTO)
579
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200580# ifdef display_errors
581# undef display_errors
582# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583
584/*
585 * Display the saved error message(s).
586 */
587 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100588display_errors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589{
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200590# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591 char *p;
592
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200593# ifdef VIMDLL
594 if (gui.in_use || gui.starting)
595# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200597 if (error_ga.ga_data != NULL)
598 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100599 // avoid putting up a message box with blanks only
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200600 for (p = (char *)error_ga.ga_data; *p; ++p)
601 if (!isspace(*p))
602 {
603 (void)gui_mch_dialog(
Bram Moolenaarc17ef8e2006-03-25 21:48:58 +0000604 gui.starting ? VIM_INFO :
Bram Moolenaarc17ef8e2006-03-25 21:48:58 +0000605 VIM_ERROR,
Bram Moolenaarc17ef8e2006-03-25 21:48:58 +0000606 gui.starting ? (char_u *)_("Message") :
Bram Moolenaarc17ef8e2006-03-25 21:48:58 +0000607 (char_u *)_("Error"),
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100608 (char_u *)p, (char_u *)_("&Ok"),
609 1, NULL, FALSE);
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200610 break;
611 }
612 ga_clear(&error_ga);
613 }
614 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200616# endif
617# if !defined(FEAT_GUI) || defined(VIMDLL)
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +0100618 FlushFileBuffers(GetStdHandle(STD_ERROR_HANDLE));
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200619# endif
Bram Moolenaar9b5c1fc2019-02-14 14:08:04 +0100620}
621#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622
623
624/*
625 * Return TRUE if "p" contain a wildcard that can be expanded by
626 * dos_expandpath().
627 */
628 int
629mch_has_exp_wildcard(char_u *p)
630{
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100631 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 {
633 if (vim_strchr((char_u *)"?*[", *p) != NULL
634 || (*p == '~' && p[1] != NUL))
635 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 }
637 return FALSE;
638}
639
640/*
641 * Return TRUE if "p" contain a wildcard or a "~1" kind of thing (could be a
642 * shortened file name).
643 */
644 int
645mch_has_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 *)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100650#ifdef VIM_BACKTICK
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 "?*$[`"
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100652#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 "?*$["
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100654#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655 , *p) != NULL
656 || (*p == '~' && p[1] != NUL))
657 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 }
659 return FALSE;
660}
661
662
663/*
664 * The normal _chdir() does not change the default drive. This one does.
665 * Returning 0 implies success; -1 implies failure.
666 */
667 int
668mch_chdir(char *path)
669{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200670 WCHAR *p;
671 int n;
672
Bram Moolenaar0f873732019-12-05 20:28:46 +0100673 if (path[0] == NUL) // just checking...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 return -1;
675
Bram Moolenaara2974d72009-07-14 16:38:36 +0000676 if (p_verbose >= 5)
677 {
678 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100679 smsg("chdir(%s)", path);
Bram Moolenaara2974d72009-07-14 16:38:36 +0000680 verbose_leave();
681 }
Bram Moolenaar0f873732019-12-05 20:28:46 +0100682 if (isalpha(path[0]) && path[1] == ':') // has a drive name
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100684 // If we can change to the drive, skip that part of the path. If we
685 // can't then the current directory may be invalid, try using chdir()
686 // with the whole path.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 if (_chdrive(TOLOWER_ASC(path[0]) - 'a' + 1) == 0)
688 path += 2;
689 }
690
Bram Moolenaar0f873732019-12-05 20:28:46 +0100691 if (*path == NUL) // drive name only
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 return 0;
693
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200694 p = enc_to_utf16((char_u *)path, NULL);
695 if (p == NULL)
696 return -1;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000697
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200698 n = _wchdir(p);
699 vim_free(p);
700 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701}
702
703
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200704#if defined(FEAT_GUI_MSWIN) && !defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705/*
706 * return non-zero if a character is available
707 */
708 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100709mch_char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100711 // never used
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 return TRUE;
713}
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200714
715# if defined(FEAT_TERMINAL) || defined(PROTO)
716/*
717 * Check for any pending input or messages.
718 */
719 int
720mch_check_messages(void)
721{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100722 // TODO: check for messages
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200723 return TRUE;
724}
725# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726#endif
727
728
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729#if defined(FEAT_LIBCALL) || defined(PROTO)
730/*
731 * Call a DLL routine which takes either a string or int param
732 * and returns an allocated string.
733 * Return OK if it worked, FAIL if not.
734 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735typedef LPTSTR (*MYSTRPROCSTR)(LPTSTR);
736typedef LPTSTR (*MYINTPROCSTR)(int);
737typedef int (*MYSTRPROCINT)(LPTSTR);
738typedef int (*MYINTPROCINT)(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740/*
741 * Check if a pointer points to a valid NUL terminated string.
742 * Return the length of the string, including terminating NUL.
743 * Returns 0 for an invalid pointer, 1 for an empty string.
744 */
745 static size_t
746check_str_len(char_u *str)
747{
748 SYSTEM_INFO si;
749 MEMORY_BASIC_INFORMATION mbi;
750 size_t length = 0;
751 size_t i;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100752 const char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753
Bram Moolenaar0f873732019-12-05 20:28:46 +0100754 // get page size
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 GetSystemInfo(&si);
756
Bram Moolenaar0f873732019-12-05 20:28:46 +0100757 // get memory information
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 if (VirtualQuery(str, &mbi, sizeof(mbi)))
759 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100760 // pre cast these (typing savers)
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000761 long_u dwStr = (long_u)str;
762 long_u dwBaseAddress = (long_u)mbi.BaseAddress;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763
Bram Moolenaar0f873732019-12-05 20:28:46 +0100764 // get start address of page that str is on
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000765 long_u strPage = dwStr - (dwStr - dwBaseAddress) % si.dwPageSize;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766
Bram Moolenaar0f873732019-12-05 20:28:46 +0100767 // get length from str to end of page
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000768 long_u pageLength = si.dwPageSize - (dwStr - strPage);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769
Bram Moolenaar442b4222010-05-24 21:34:22 +0200770 for (p = str; !IsBadReadPtr(p, (UINT)pageLength);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 p += pageLength, pageLength = si.dwPageSize)
772 for (i = 0; i < pageLength; ++i, ++length)
773 if (p[i] == NUL)
774 return length + 1;
775 }
776
777 return 0;
778}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200780/*
781 * Passed to do_in_runtimepath() to load a vim.ico file.
782 */
783 static void
784mch_icon_load_cb(char_u *fname, void *cookie)
785{
786 HANDLE *h = (HANDLE *)cookie;
787
788 *h = LoadImage(NULL,
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100789 (LPSTR)fname,
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200790 IMAGE_ICON,
791 64,
792 64,
793 LR_LOADFROMFILE | LR_LOADMAP3DCOLORS);
794}
795
796/*
797 * Try loading an icon file from 'runtimepath'.
798 */
799 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100800mch_icon_load(HANDLE *iconp)
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200801{
802 return do_in_runtimepath((char_u *)"bitmaps/vim.ico",
Bram Moolenaar7f8989d2016-03-12 22:11:39 +0100803 0, mch_icon_load_cb, iconp);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200804}
805
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 int
807mch_libcall(
808 char_u *libname,
809 char_u *funcname,
Bram Moolenaar0f873732019-12-05 20:28:46 +0100810 char_u *argstring, // NULL when using a argint
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 int argint,
Bram Moolenaar0f873732019-12-05 20:28:46 +0100812 char_u **string_result,// NULL when using number_result
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813 int *number_result)
814{
815 HINSTANCE hinstLib;
816 MYSTRPROCSTR ProcAdd;
817 MYINTPROCSTR ProcAddI;
818 char_u *retval_str = NULL;
819 int retval_int = 0;
820 size_t len;
821
822 BOOL fRunTimeLinkSuccess = FALSE;
823
824 // Get a handle to the DLL module.
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100825 hinstLib = vimLoadLib((char *)libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826
827 // If the handle is valid, try to get the function address.
828 if (hinstLib != NULL)
829 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100830# ifdef HAVE_TRY_EXCEPT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 __try
832 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100833# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 if (argstring != NULL)
835 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100836 // Call with string argument
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100837 ProcAdd = (MYSTRPROCSTR)GetProcAddress(hinstLib, (LPCSTR)funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 if ((fRunTimeLinkSuccess = (ProcAdd != NULL)) != 0)
839 {
840 if (string_result == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100841 retval_int = ((MYSTRPROCINT)ProcAdd)((LPSTR)argstring);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100843 retval_str = (char_u *)(ProcAdd)((LPSTR)argstring);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844 }
845 }
846 else
847 {
Bram Moolenaar0f873732019-12-05 20:28:46 +0100848 // Call with number argument
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100849 ProcAddI = (MYINTPROCSTR) GetProcAddress(hinstLib, (LPCSTR)funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 if ((fRunTimeLinkSuccess = (ProcAddI != NULL)) != 0)
851 {
852 if (string_result == NULL)
853 retval_int = ((MYINTPROCINT)ProcAddI)(argint);
854 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100855 retval_str = (char_u *)(ProcAddI)(argint);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856 }
857 }
858
859 // Save the string before we free the library.
860 // Assume that a "1" result is an illegal pointer.
861 if (string_result == NULL)
862 *number_result = retval_int;
863 else if (retval_str != NULL
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100864 && (len = check_str_len(retval_str)) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865 {
Bram Moolenaar18a4ba22019-05-24 19:39:03 +0200866 *string_result = alloc(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867 if (*string_result != NULL)
868 mch_memmove(*string_result, retval_str, len);
869 }
870
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100871# ifdef HAVE_TRY_EXCEPT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 }
873 __except(EXCEPTION_EXECUTE_HANDLER)
874 {
875 if (GetExceptionCode() == EXCEPTION_STACK_OVERFLOW)
K.Takatac351dc12022-01-24 11:24:08 +0000876 _resetstkoflw();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877 fRunTimeLinkSuccess = 0;
878 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100879# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880
881 // Free the DLL module.
882 (void)FreeLibrary(hinstLib);
883 }
884
885 if (!fRunTimeLinkSuccess)
886 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000887 semsg(_(e_library_call_failed_for_str), funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 return FAIL;
889 }
890
891 return OK;
892}
893#endif
894
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895/*
896 * Debugging helper: expose the MCH_WRITE_DUMP stuff to other modules
897 */
898 void
Bram Moolenaar1266d672017-02-01 13:43:36 +0100899DumpPutS(const char *psz UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100901#ifdef MCH_WRITE_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 if (fdDump)
903 {
904 fputs(psz, fdDump);
905 if (psz[strlen(psz) - 1] != '\n')
906 fputc('\n', fdDump);
907 fflush(fdDump);
908 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +0100909#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910}
911
912#ifdef _DEBUG
913
914void __cdecl
915Trace(
916 char *pszFormat,
917 ...)
918{
919 CHAR szBuff[2048];
920 va_list args;
921
922 va_start(args, pszFormat);
923 vsprintf(szBuff, pszFormat, args);
924 va_end(args);
925
926 OutputDebugString(szBuff);
927}
928
929#endif //_DEBUG
930
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200931#if !defined(FEAT_GUI) || defined(VIMDLL) || defined(PROTO)
Bram Moolenaar0f873732019-12-05 20:28:46 +0100932extern HWND g_hWnd; // This is in os_win32.c.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933
934/*
935 * Showing the printer dialog is tricky since we have no GUI
936 * window to parent it. The following routines are needed to
937 * get the window parenting and Z-order to work properly.
938 */
939 static void
940GetConsoleHwnd(void)
941{
Bram Moolenaar0f873732019-12-05 20:28:46 +0100942 // Skip if it's already set.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 if (s_hwnd != 0)
944 return;
945
Bram Moolenaar0f873732019-12-05 20:28:46 +0100946 // Window handle may have been found by init code (Windows NT only)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 if (g_hWnd != 0)
948 {
949 s_hwnd = g_hWnd;
950 return;
951 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952
Bram Moolenaare1ed53f2019-02-12 23:12:37 +0100953 s_hwnd = GetConsoleWindow();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954}
Bram Moolenaar843ee412004-06-30 16:16:41 +0000955
956/*
957 * Console implementation of ":winpos".
958 */
959 int
960mch_get_winpos(int *x, int *y)
961{
962 RECT rect;
963
964 GetConsoleHwnd();
965 GetWindowRect(s_hwnd, &rect);
966 *x = rect.left;
967 *y = rect.top;
968 return OK;
969}
970
971/*
972 * Console implementation of ":winpos x y".
973 */
974 void
975mch_set_winpos(int x, int y)
976{
977 GetConsoleHwnd();
978 SetWindowPos(s_hwnd, NULL, x, y, 0, 0,
979 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
980}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981#endif
982
983#if (defined(FEAT_PRINTER) && !defined(FEAT_POSTSCRIPT)) || defined(PROTO)
984
Bram Moolenaar0f873732019-12-05 20:28:46 +0100985//=================================================================
986// Win32 printer stuff
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987
988static HFONT prt_font_handles[2][2][2];
Bram Moolenaar0eb035c2019-04-02 22:15:55 +0200989static PRINTDLGW prt_dlg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990static const int boldface[2] = {FW_REGULAR, FW_BOLD};
991static TEXTMETRIC prt_tm;
992static int prt_line_height;
993static int prt_number_width;
994static int prt_left_margin;
995static int prt_right_margin;
996static int prt_top_margin;
997static char_u szAppName[] = TEXT("VIM");
998static HWND hDlgPrint;
999static int *bUserAbort = NULL;
1000static char_u *prt_name = NULL;
1001
Bram Moolenaar0f873732019-12-05 20:28:46 +01001002// Defines which are also in vim.rc.
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001003# define IDC_BOX1 400
1004# define IDC_PRINTTEXT1 401
1005# define IDC_PRINTTEXT2 402
1006# define IDC_PROGRESS 403
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001008 static BOOL
1009vimSetDlgItemText(HWND hDlg, int nIDDlgItem, char_u *s)
1010{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001011 WCHAR *wp;
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001012 BOOL ret;
1013
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001014 wp = enc_to_utf16(s, NULL);
1015 if (wp == NULL)
1016 return FALSE;
1017
1018 ret = SetDlgItemTextW(hDlg, nIDDlgItem, wp);
1019 vim_free(wp);
1020 return ret;
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001021}
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001022
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023/*
1024 * Convert BGR to RGB for Windows GDI calls
1025 */
1026 static COLORREF
1027swap_me(COLORREF colorref)
1028{
1029 int temp;
1030 char *ptr = (char *)&colorref;
1031
1032 temp = *(ptr);
1033 *(ptr ) = *(ptr + 2);
1034 *(ptr + 2) = temp;
1035 return colorref;
1036}
1037
K.Takatac351dc12022-01-24 11:24:08 +00001038 static INT_PTR CALLBACK
Bram Moolenaar1266d672017-02-01 13:43:36 +01001039PrintDlgProc(
1040 HWND hDlg,
1041 UINT message,
1042 WPARAM wParam UNUSED,
1043 LPARAM lParam UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044{
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001045# ifdef FEAT_GETTEXT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 NONCLIENTMETRICS nm;
1047 static HFONT hfont;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001048# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049
1050 switch (message)
1051 {
1052 case WM_INITDIALOG:
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001053# ifdef FEAT_GETTEXT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 nm.cbSize = sizeof(NONCLIENTMETRICS);
1055 if (SystemParametersInfo(
1056 SPI_GETNONCLIENTMETRICS,
1057 sizeof(NONCLIENTMETRICS),
1058 &nm,
1059 0))
1060 {
1061 char buff[MAX_PATH];
1062 int i;
1063
Bram Moolenaar0f873732019-12-05 20:28:46 +01001064 // Translate the dialog texts
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 hfont = CreateFontIndirect(&nm.lfMessageFont);
1066 for (i = IDC_PRINTTEXT1; i <= IDC_PROGRESS; i++)
1067 {
1068 SendDlgItemMessage(hDlg, i, WM_SETFONT, (WPARAM)hfont, 1);
1069 if (GetDlgItemText(hDlg,i, buff, sizeof(buff)))
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001070 vimSetDlgItemText(hDlg,i, (char_u *)_(buff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 }
1072 SendDlgItemMessage(hDlg, IDCANCEL,
1073 WM_SETFONT, (WPARAM)hfont, 1);
1074 if (GetDlgItemText(hDlg,IDCANCEL, buff, sizeof(buff)))
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001075 vimSetDlgItemText(hDlg,IDCANCEL, (char_u *)_(buff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001077# endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001078 SetWindowText(hDlg, (LPCSTR)szAppName);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 if (prt_name != NULL)
1080 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001081 vimSetDlgItemText(hDlg, IDC_PRINTTEXT2, (char_u *)prt_name);
Bram Moolenaard23a8232018-02-10 18:45:26 +01001082 VIM_CLEAR(prt_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 }
1084 EnableMenuItem(GetSystemMenu(hDlg, FALSE), SC_CLOSE, MF_GRAYED);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001085# if !defined(FEAT_GUI) || defined(VIMDLL)
1086# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001087 if (!gui.in_use)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001088# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001089 BringWindowToTop(s_hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001090# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 return TRUE;
1092
1093 case WM_COMMAND:
1094 *bUserAbort = TRUE;
1095 EnableWindow(GetParent(hDlg), TRUE);
1096 DestroyWindow(hDlg);
1097 hDlgPrint = NULL;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001098# ifdef FEAT_GETTEXT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 DeleteObject(hfont);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001100# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 return TRUE;
1102 }
1103 return FALSE;
1104}
1105
1106 static BOOL CALLBACK
Bram Moolenaar1266d672017-02-01 13:43:36 +01001107AbortProc(HDC hdcPrn UNUSED, int iCode UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108{
1109 MSG msg;
1110
K.Takatab7057bd2022-01-21 11:37:07 +00001111 while (!*bUserAbort && PeekMessageW(&msg, NULL, 0, 0, PM_REMOVE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 {
K.Takatab7057bd2022-01-21 11:37:07 +00001113 if (!hDlgPrint || !IsDialogMessageW(hDlgPrint, &msg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 {
1115 TranslateMessage(&msg);
K.Takatab7057bd2022-01-21 11:37:07 +00001116 DispatchMessageW(&msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 }
1118 }
1119 return !*bUserAbort;
1120}
1121
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001122# if !defined(FEAT_GUI) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123
Bram Moolenaar26fdd7d2011-11-30 13:42:44 +01001124 static UINT_PTR CALLBACK
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125PrintHookProc(
1126 HWND hDlg, // handle to dialog box
1127 UINT uiMsg, // message identifier
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02001128 WPARAM wParam UNUSED, // message parameter
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 LPARAM lParam // message parameter
1130 )
1131{
1132 HWND hwndOwner;
1133 RECT rc, rcDlg, rcOwner;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001134 PRINTDLGW *pPD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135
1136 if (uiMsg == WM_INITDIALOG)
1137 {
1138 // Get the owner window and dialog box rectangles.
1139 if ((hwndOwner = GetParent(hDlg)) == NULL)
1140 hwndOwner = GetDesktopWindow();
1141
1142 GetWindowRect(hwndOwner, &rcOwner);
1143 GetWindowRect(hDlg, &rcDlg);
1144 CopyRect(&rc, &rcOwner);
1145
1146 // Offset the owner and dialog box rectangles so that
1147 // right and bottom values represent the width and
1148 // height, and then offset the owner again to discard
1149 // space taken up by the dialog box.
1150
1151 OffsetRect(&rcDlg, -rcDlg.left, -rcDlg.top);
1152 OffsetRect(&rc, -rc.left, -rc.top);
1153 OffsetRect(&rc, -rcDlg.right, -rcDlg.bottom);
1154
1155 // The new position is the sum of half the remaining
1156 // space and the owner's original position.
1157
1158 SetWindowPos(hDlg,
1159 HWND_TOP,
1160 rcOwner.left + (rc.right / 2),
1161 rcOwner.top + (rc.bottom / 2),
1162 0, 0, // ignores size arguments
1163 SWP_NOSIZE);
1164
Bram Moolenaar0f873732019-12-05 20:28:46 +01001165 // tackle the printdlg copiesctrl problem
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001166 pPD = (PRINTDLGW *)lParam;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 pPD->nCopies = (WORD)pPD->lCustData;
1168 SetDlgItemInt( hDlg, edt3, pPD->nCopies, FALSE );
Bram Moolenaar0f873732019-12-05 20:28:46 +01001169 // Bring the window to top
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 BringWindowToTop(GetParent(hDlg));
1171 SetForegroundWindow(hDlg);
1172 }
1173
1174 return FALSE;
1175}
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001176# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177
1178 void
1179mch_print_cleanup(void)
1180{
1181 int pifItalic;
1182 int pifBold;
1183 int pifUnderline;
1184
1185 for (pifBold = 0; pifBold <= 1; pifBold++)
1186 for (pifItalic = 0; pifItalic <= 1; pifItalic++)
1187 for (pifUnderline = 0; pifUnderline <= 1; pifUnderline++)
1188 DeleteObject(prt_font_handles[pifBold][pifItalic][pifUnderline]);
1189
1190 if (prt_dlg.hDC != NULL)
1191 DeleteDC(prt_dlg.hDC);
1192 if (!*bUserAbort)
1193 SendMessage(hDlgPrint, WM_COMMAND, 0, 0);
1194}
1195
1196 static int
1197to_device_units(int idx, int dpi, int physsize, int offset, int def_number)
1198{
1199 int ret = 0;
1200 int u;
1201 int nr;
1202
1203 u = prt_get_unit(idx);
1204 if (u == PRT_UNIT_NONE)
1205 {
1206 u = PRT_UNIT_PERC;
1207 nr = def_number;
1208 }
1209 else
1210 nr = printer_opts[idx].number;
1211
1212 switch (u)
1213 {
1214 case PRT_UNIT_PERC:
1215 ret = (physsize * nr) / 100;
1216 break;
1217 case PRT_UNIT_INCH:
1218 ret = (nr * dpi);
1219 break;
1220 case PRT_UNIT_MM:
1221 ret = (nr * 10 * dpi) / 254;
1222 break;
1223 case PRT_UNIT_POINT:
1224 ret = (nr * 10 * dpi) / 720;
1225 break;
1226 }
1227
1228 if (ret < offset)
1229 return 0;
1230 else
1231 return ret - offset;
1232}
1233
1234 static int
1235prt_get_cpl(void)
1236{
1237 int hr;
1238 int phyw;
1239 int dvoff;
1240 int rev_offset;
1241 int dpi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242
1243 GetTextMetrics(prt_dlg.hDC, &prt_tm);
1244 prt_line_height = prt_tm.tmHeight + prt_tm.tmExternalLeading;
1245
1246 hr = GetDeviceCaps(prt_dlg.hDC, HORZRES);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 phyw = GetDeviceCaps(prt_dlg.hDC, PHYSICALWIDTH);
1248 dvoff = GetDeviceCaps(prt_dlg.hDC, PHYSICALOFFSETX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 dpi = GetDeviceCaps(prt_dlg.hDC, LOGPIXELSX);
1250
1251 rev_offset = phyw - (dvoff + hr);
1252
1253 prt_left_margin = to_device_units(OPT_PRINT_LEFT, dpi, phyw, dvoff, 10);
1254 if (prt_use_number())
1255 {
1256 prt_number_width = PRINT_NUMBER_WIDTH * prt_tm.tmAveCharWidth;
1257 prt_left_margin += prt_number_width;
1258 }
1259 else
1260 prt_number_width = 0;
1261
1262 prt_right_margin = hr - to_device_units(OPT_PRINT_RIGHT, dpi, phyw,
1263 rev_offset, 5);
1264
1265 return (prt_right_margin - prt_left_margin) / prt_tm.tmAveCharWidth;
1266}
1267
1268 static int
1269prt_get_lpp(void)
1270{
1271 int vr;
1272 int phyw;
1273 int dvoff;
1274 int rev_offset;
1275 int bottom_margin;
1276 int dpi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277
1278 vr = GetDeviceCaps(prt_dlg.hDC, VERTRES);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 phyw = GetDeviceCaps(prt_dlg.hDC, PHYSICALHEIGHT);
1280 dvoff = GetDeviceCaps(prt_dlg.hDC, PHYSICALOFFSETY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 dpi = GetDeviceCaps(prt_dlg.hDC, LOGPIXELSY);
1282
1283 rev_offset = phyw - (dvoff + vr);
1284
1285 prt_top_margin = to_device_units(OPT_PRINT_TOP, dpi, phyw, dvoff, 5);
1286
Bram Moolenaar0f873732019-12-05 20:28:46 +01001287 // adjust top margin if there is a header
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 prt_top_margin += prt_line_height * prt_header_height();
1289
1290 bottom_margin = vr - to_device_units(OPT_PRINT_BOT, dpi, phyw,
1291 rev_offset, 5);
1292
1293 return (bottom_margin - prt_top_margin) / prt_line_height;
1294}
1295
1296 int
1297mch_print_init(prt_settings_T *psettings, char_u *jobname, int forceit)
1298{
1299 static HGLOBAL stored_dm = NULL;
1300 static HGLOBAL stored_devn = NULL;
1301 static int stored_nCopies = 1;
1302 static int stored_nFlags = 0;
1303
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001304 LOGFONTW fLogFont;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 int pifItalic;
1306 int pifBold;
1307 int pifUnderline;
1308
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001309 DEVMODEW *mem;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 DEVNAMES *devname;
1311 int i;
1312
1313 bUserAbort = &(psettings->user_abort);
Bram Moolenaara80faa82020-04-12 19:37:17 +02001314 CLEAR_FIELD(prt_dlg);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001315 prt_dlg.lStructSize = sizeof(PRINTDLGW);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001316# if !defined(FEAT_GUI) || defined(VIMDLL)
1317# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001318 if (!gui.in_use)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001319# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01001320 GetConsoleHwnd(); // get value of s_hwnd
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001321# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 prt_dlg.hwndOwner = s_hwnd;
1323 prt_dlg.Flags = PD_NOPAGENUMS | PD_NOSELECTION | PD_RETURNDC;
1324 if (!forceit)
1325 {
1326 prt_dlg.hDevMode = stored_dm;
1327 prt_dlg.hDevNames = stored_devn;
1328 prt_dlg.lCustData = stored_nCopies; // work around bug in print dialog
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001329# if !defined(FEAT_GUI) || defined(VIMDLL)
1330# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001331 if (!gui.in_use)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001332# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001333 {
1334 /*
1335 * Use hook to prevent console window being sent to back
1336 */
1337 prt_dlg.lpfnPrintHook = PrintHookProc;
1338 prt_dlg.Flags |= PD_ENABLEPRINTHOOK;
1339 }
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001340# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 prt_dlg.Flags |= stored_nFlags;
1342 }
1343
1344 /*
1345 * If bang present, return default printer setup with no dialog
1346 * never show dialog if we are running over telnet
1347 */
1348 if (forceit
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001349# if !defined(FEAT_GUI) || defined(VIMDLL)
1350# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001351 || (!gui.in_use && !term_console)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001352# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 || !term_console
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001354# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001355# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 )
1357 {
1358 prt_dlg.Flags |= PD_RETURNDEFAULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 /*
1360 * MSDN suggests setting the first parameter to WINSPOOL for
1361 * NT, but NULL appears to work just as well.
1362 */
1363 if (*p_pdev != NUL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001364 prt_dlg.hDC = CreateDC(NULL, (LPCSTR)p_pdev, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 {
1367 prt_dlg.Flags |= PD_RETURNDEFAULT;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001368 if (PrintDlgW(&prt_dlg) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 goto init_fail_dlg;
1370 }
1371 }
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001372 else if (PrintDlgW(&prt_dlg) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 goto init_fail_dlg;
1374 else
1375 {
1376 /*
1377 * keep the previous driver context
1378 */
1379 stored_dm = prt_dlg.hDevMode;
1380 stored_devn = prt_dlg.hDevNames;
1381 stored_nFlags = prt_dlg.Flags;
1382 stored_nCopies = prt_dlg.nCopies;
1383 }
1384
1385 if (prt_dlg.hDC == NULL)
1386 {
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00001387 emsg(_(e_printer_selection_failed));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 mch_print_cleanup();
1389 return FALSE;
1390 }
1391
Bram Moolenaar0f873732019-12-05 20:28:46 +01001392 // Not all printer drivers report the support of color (or grey) in the
1393 // same way. Let's set has_color if there appears to be some way to print
1394 // more than B&W.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 i = GetDeviceCaps(prt_dlg.hDC, NUMCOLORS);
1396 psettings->has_color = (GetDeviceCaps(prt_dlg.hDC, BITSPIXEL) > 1
1397 || GetDeviceCaps(prt_dlg.hDC, PLANES) > 1
1398 || i > 2 || i == -1);
1399
Bram Moolenaar0f873732019-12-05 20:28:46 +01001400 // Ensure all font styles are baseline aligned
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 SetTextAlign(prt_dlg.hDC, TA_BASELINE|TA_LEFT);
1402
1403 /*
1404 * On some windows systems the nCopies parameter is not
1405 * passed back correctly. It must be retrieved from the
1406 * hDevMode struct.
1407 */
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001408 mem = (DEVMODEW *)GlobalLock(prt_dlg.hDevMode);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 if (mem != NULL)
1410 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 if (mem->dmCopies != 1)
1412 stored_nCopies = mem->dmCopies;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 if ((mem->dmFields & DM_DUPLEX) && (mem->dmDuplex & ~DMDUP_SIMPLEX))
1414 psettings->duplex = TRUE;
1415 if ((mem->dmFields & DM_COLOR) && (mem->dmColor & DMCOLOR_COLOR))
1416 psettings->has_color = TRUE;
1417 }
1418 GlobalUnlock(prt_dlg.hDevMode);
1419
1420 devname = (DEVNAMES *)GlobalLock(prt_dlg.hDevNames);
1421 if (devname != 0)
1422 {
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001423 WCHAR *wprinter_name = (WCHAR *)devname + devname->wDeviceOffset;
1424 WCHAR *wport_name = (WCHAR *)devname + devname->wOutputOffset;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001425 char_u *text = (char_u *)_("to %s on %s");
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001426 char_u *printer_name = utf16_to_enc(wprinter_name, NULL);
1427 char_u *port_name = utf16_to_enc(wport_name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001429 if (printer_name != NULL && port_name != NULL)
Bram Moolenaar964b3742019-05-24 18:54:09 +02001430 prt_name = alloc(STRLEN(printer_name)
1431 + STRLEN(port_name) + STRLEN(text));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 if (prt_name != NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001433 wsprintf((char *)prt_name, (const char *)text,
1434 printer_name, port_name);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001435 vim_free(printer_name);
1436 vim_free(port_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 }
1438 GlobalUnlock(prt_dlg.hDevNames);
1439
1440 /*
1441 * Initialise the font according to 'printfont'
1442 */
Bram Moolenaara80faa82020-04-12 19:37:17 +02001443 CLEAR_FIELD(fLogFont);
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001444 if (get_logfont(&fLogFont, p_pfn, prt_dlg.hDC, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 {
Bram Moolenaard88be5b2022-01-04 19:57:55 +00001446 semsg(_(e_unknown_printer_font_str), p_pfn);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 mch_print_cleanup();
1448 return FALSE;
1449 }
1450
1451 for (pifBold = 0; pifBold <= 1; pifBold++)
1452 for (pifItalic = 0; pifItalic <= 1; pifItalic++)
1453 for (pifUnderline = 0; pifUnderline <= 1; pifUnderline++)
1454 {
1455 fLogFont.lfWeight = boldface[pifBold];
1456 fLogFont.lfItalic = pifItalic;
1457 fLogFont.lfUnderline = pifUnderline;
1458 prt_font_handles[pifBold][pifItalic][pifUnderline]
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01001459 = CreateFontIndirectW(&fLogFont);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 }
1461
1462 SetBkMode(prt_dlg.hDC, OPAQUE);
1463 SelectObject(prt_dlg.hDC, prt_font_handles[0][0][0]);
1464
1465 /*
1466 * Fill in the settings struct
1467 */
1468 psettings->chars_per_line = prt_get_cpl();
1469 psettings->lines_per_page = prt_get_lpp();
Bram Moolenaar7ddc6422014-09-27 11:18:19 +02001470 if (prt_dlg.Flags & PD_USEDEVMODECOPIESANDCOLLATE)
1471 {
1472 psettings->n_collated_copies = (prt_dlg.Flags & PD_COLLATE)
1473 ? prt_dlg.nCopies : 1;
1474 psettings->n_uncollated_copies = (prt_dlg.Flags & PD_COLLATE)
1475 ? 1 : prt_dlg.nCopies;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476
Bram Moolenaar7ddc6422014-09-27 11:18:19 +02001477 if (psettings->n_collated_copies == 0)
1478 psettings->n_collated_copies = 1;
1479
1480 if (psettings->n_uncollated_copies == 0)
1481 psettings->n_uncollated_copies = 1;
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01001482 }
1483 else
1484 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 psettings->n_collated_copies = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 psettings->n_uncollated_copies = 1;
Bram Moolenaar7ddc6422014-09-27 11:18:19 +02001487 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488
1489 psettings->jobname = jobname;
1490
1491 return TRUE;
1492
1493init_fail_dlg:
1494 {
1495 DWORD err = CommDlgExtendedError();
1496
1497 if (err)
1498 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 char_u *buf;
1500
Bram Moolenaar0f873732019-12-05 20:28:46 +01001501 // I suspect FormatMessage() doesn't work for values returned by
1502 // CommDlgExtendedError(). What does?
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
1504 FORMAT_MESSAGE_FROM_SYSTEM |
1505 FORMAT_MESSAGE_IGNORE_INSERTS,
1506 NULL, err, 0, (LPTSTR)(&buf), 0, NULL);
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00001507 semsg(_(e_print_error_str),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 buf == NULL ? (char_u *)_("Unknown") : buf);
1509 LocalFree((LPVOID)(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 }
1511 else
Bram Moolenaar0f873732019-12-05 20:28:46 +01001512 msg_clr_eos(); // Maybe canceled
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513
1514 mch_print_cleanup();
1515 return FALSE;
1516 }
1517}
1518
1519
1520 int
1521mch_print_begin(prt_settings_T *psettings)
1522{
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001523 int ret = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 char szBuffer[300];
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001525 WCHAR *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001527 hDlgPrint = CreateDialog(g_hinst, TEXT("PrintDlgBox"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 prt_dlg.hwndOwner, PrintDlgProc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 SetAbortProc(prt_dlg.hDC, AbortProc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530 wsprintf(szBuffer, _("Printing '%s'"), gettail(psettings->jobname));
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001531 vimSetDlgItemText(hDlgPrint, IDC_PRINTTEXT1, (char_u *)szBuffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001533 wp = enc_to_utf16(psettings->jobname, NULL);
Bram Moolenaar2290b1f2018-05-13 17:30:45 +02001534 if (wp != NULL)
1535 {
1536 DOCINFOW di;
1537
Bram Moolenaara80faa82020-04-12 19:37:17 +02001538 CLEAR_FIELD(di);
Bram Moolenaar2290b1f2018-05-13 17:30:45 +02001539 di.cbSize = sizeof(di);
1540 di.lpszDocName = wp;
1541 ret = StartDocW(prt_dlg.hDC, &di);
1542 vim_free(wp);
1543 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001545# ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01001546 // Give focus back to main window (when using MDI).
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001547# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001548 if (gui.in_use)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001549# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001550 SetFocus(s_hwnd);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001551# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552
1553 return (ret > 0);
1554}
1555
1556 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01001557mch_print_end(prt_settings_T *psettings UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558{
1559 EndDoc(prt_dlg.hDC);
1560 if (!*bUserAbort)
1561 SendMessage(hDlgPrint, WM_COMMAND, 0, 0);
1562}
1563
1564 int
1565mch_print_end_page(void)
1566{
1567 return (EndPage(prt_dlg.hDC) > 0);
1568}
1569
1570 int
1571mch_print_begin_page(char_u *msg)
1572{
1573 if (msg != NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001574 vimSetDlgItemText(hDlgPrint, IDC_PROGRESS, msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 return (StartPage(prt_dlg.hDC) > 0);
1576}
1577
1578 int
1579mch_print_blank_page(void)
1580{
1581 return (mch_print_begin_page(NULL) ? (mch_print_end_page()) : FALSE);
1582}
1583
1584static int prt_pos_x = 0;
1585static int prt_pos_y = 0;
1586
1587 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001588mch_print_start_line(int margin, int page_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589{
1590 if (margin)
1591 prt_pos_x = -prt_number_width;
1592 else
1593 prt_pos_x = 0;
1594 prt_pos_y = page_line * prt_line_height
1595 + prt_tm.tmAscent + prt_tm.tmExternalLeading;
1596}
1597
1598 int
1599mch_print_text_out(char_u *p, int len)
1600{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 SIZE sz;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001602 WCHAR *wp;
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001603 int wlen = len;
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001604 int ret = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001606 wp = enc_to_utf16(p, &wlen);
1607 if (wp == NULL)
1608 return FALSE;
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001609
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001610 TextOutW(prt_dlg.hDC, prt_pos_x + prt_left_margin,
1611 prt_pos_y + prt_top_margin, wp, wlen);
1612 GetTextExtentPoint32W(prt_dlg.hDC, wp, wlen, &sz);
1613 vim_free(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 prt_pos_x += (sz.cx - prt_tm.tmOverhang);
Bram Moolenaar0f873732019-12-05 20:28:46 +01001615 // This is wrong when printing spaces for a TAB.
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001616 if (p[len] != NUL)
1617 {
Bram Moolenaar1614a142019-10-06 22:00:13 +02001618 wlen = mb_ptr2len(p + len);
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001619 wp = enc_to_utf16(p + len, &wlen);
1620 if (wp != NULL)
1621 {
1622 GetTextExtentPoint32W(prt_dlg.hDC, wp, 1, &sz);
1623 ret = (prt_pos_x + prt_left_margin + sz.cx > prt_right_margin);
1624 vim_free(wp);
1625 }
1626 }
1627 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628}
1629
1630 void
1631mch_print_set_font(int iBold, int iItalic, int iUnderline)
1632{
1633 SelectObject(prt_dlg.hDC, prt_font_handles[iBold][iItalic][iUnderline]);
1634}
1635
1636 void
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001637mch_print_set_bg(long_u bgcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638{
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001639 SetBkColor(prt_dlg.hDC, GetNearestColor(prt_dlg.hDC,
1640 swap_me((COLORREF)bgcol)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 /*
1642 * With a white background we can draw characters transparent, which is
1643 * good for italic characters that overlap to the next char cell.
1644 */
1645 if (bgcol == 0xffffffUL)
1646 SetBkMode(prt_dlg.hDC, TRANSPARENT);
1647 else
1648 SetBkMode(prt_dlg.hDC, OPAQUE);
1649}
1650
1651 void
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001652mch_print_set_fg(long_u fgcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653{
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001654 SetTextColor(prt_dlg.hDC, GetNearestColor(prt_dlg.hDC,
1655 swap_me((COLORREF)fgcol)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656}
1657
Bram Moolenaar0f873732019-12-05 20:28:46 +01001658#endif // FEAT_PRINTER && !FEAT_POSTSCRIPT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659
Bram Moolenaar58d98232005-07-23 22:25:46 +00001660
1661
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662#if defined(FEAT_SHORTCUT) || defined(PROTO)
Bram Moolenaar82881492012-11-20 16:53:39 +01001663# ifndef PROTO
1664# include <shlobj.h>
1665# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001667typedef BOOL (WINAPI *pfnGetFinalPathNameByHandleW)(
Bram Moolenaardce1e892019-02-10 23:18:53 +01001668 HANDLE hFile,
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001669 LPWSTR lpszFilePath,
1670 DWORD cchFilePath,
1671 DWORD dwFlags);
1672static pfnGetFinalPathNameByHandleW pGetFinalPathNameByHandleW = NULL;
Bram Moolenaardce1e892019-02-10 23:18:53 +01001673
Bram Moolenaar4a792c82019-06-06 12:22:41 +02001674# define is_path_sep(c) ((c) == L'\\' || (c) == L'/')
1675
1676 static int
1677is_reparse_point_included(LPCWSTR fname)
1678{
1679 LPCWSTR p = fname, q;
1680 WCHAR buf[MAX_PATH];
1681 DWORD attr;
1682
1683 if (isalpha(p[0]) && p[1] == L':' && is_path_sep(p[2]))
1684 p += 3;
1685 else if (is_path_sep(p[0]) && is_path_sep(p[1]))
1686 p += 2;
1687
1688 while (*p != L'\0')
1689 {
1690 q = wcspbrk(p, L"\\/");
1691 if (q == NULL)
1692 p = q = fname + wcslen(fname);
1693 else
1694 p = q + 1;
1695 if (q - fname >= MAX_PATH)
1696 return FALSE;
1697 wcsncpy(buf, fname, q - fname);
1698 buf[q - fname] = L'\0';
1699 attr = GetFileAttributesW(buf);
1700 if (attr != INVALID_FILE_ATTRIBUTES
1701 && (attr & FILE_ATTRIBUTE_REPARSE_POINT) != 0)
1702 return TRUE;
1703 }
1704 return FALSE;
1705}
1706
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02001707 static char_u *
Bram Moolenaardce1e892019-02-10 23:18:53 +01001708resolve_reparse_point(char_u *fname)
1709{
1710 HANDLE h = INVALID_HANDLE_VALUE;
1711 DWORD size;
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001712 WCHAR *p, *wp;
Bram Moolenaardce1e892019-02-10 23:18:53 +01001713 char_u *rfname = NULL;
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001714 WCHAR *buff = NULL;
Bram Moolenaardce1e892019-02-10 23:18:53 +01001715 static BOOL loaded = FALSE;
1716
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001717 if (pGetFinalPathNameByHandleW == NULL)
Bram Moolenaardce1e892019-02-10 23:18:53 +01001718 {
1719 HMODULE hmod = GetModuleHandle("kernel32.dll");
1720
1721 if (loaded == TRUE)
1722 return NULL;
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001723 pGetFinalPathNameByHandleW = (pfnGetFinalPathNameByHandleW)
1724 GetProcAddress(hmod, "GetFinalPathNameByHandleW");
Bram Moolenaardce1e892019-02-10 23:18:53 +01001725 loaded = TRUE;
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001726 if (pGetFinalPathNameByHandleW == NULL)
Bram Moolenaardce1e892019-02-10 23:18:53 +01001727 return NULL;
1728 }
1729
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001730 p = enc_to_utf16(fname, NULL);
1731 if (p == NULL)
1732 goto fail;
1733
Bram Moolenaar4a792c82019-06-06 12:22:41 +02001734 if (!is_reparse_point_included(p))
1735 {
1736 vim_free(p);
1737 goto fail;
1738 }
1739
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001740 h = CreateFileW(p, 0, 0, NULL, OPEN_EXISTING,
1741 FILE_FLAG_BACKUP_SEMANTICS, NULL);
1742 vim_free(p);
Bram Moolenaardce1e892019-02-10 23:18:53 +01001743
1744 if (h == INVALID_HANDLE_VALUE)
1745 goto fail;
1746
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001747 size = pGetFinalPathNameByHandleW(h, NULL, 0, 0);
1748 if (size == 0)
1749 goto fail;
1750 buff = ALLOC_MULT(WCHAR, size);
1751 if (buff == NULL)
1752 goto fail;
1753 if (pGetFinalPathNameByHandleW(h, buff, size, 0) == 0)
Bram Moolenaardce1e892019-02-10 23:18:53 +01001754 goto fail;
1755
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001756 if (wcsncmp(buff, L"\\\\?\\UNC\\", 8) == 0)
1757 {
1758 buff[6] = L'\\';
1759 wp = buff + 6;
1760 }
1761 else if (wcsncmp(buff, L"\\\\?\\", 4) == 0)
1762 wp = buff + 4;
Bram Moolenaardce1e892019-02-10 23:18:53 +01001763 else
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001764 wp = buff;
1765
1766 rfname = utf16_to_enc(wp, NULL);
Bram Moolenaardce1e892019-02-10 23:18:53 +01001767
1768fail:
1769 if (h != INVALID_HANDLE_VALUE)
1770 CloseHandle(h);
Bram Moolenaar3f9bdeb2019-08-01 13:55:37 +02001771 if (buff != NULL)
1772 vim_free(buff);
Bram Moolenaardce1e892019-02-10 23:18:53 +01001773
1774 return rfname;
1775}
1776
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777/*
1778 * When "fname" is the name of a shortcut (*.lnk) resolve the file it points
1779 * to and return that name in allocated memory.
1780 * Otherwise NULL is returned.
1781 */
Bram Moolenaardce1e892019-02-10 23:18:53 +01001782 static char_u *
1783resolve_shortcut(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784{
1785 HRESULT hr;
1786 IShellLink *psl = NULL;
1787 IPersistFile *ppf = NULL;
1788 OLECHAR wsz[MAX_PATH];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 char_u *rfname = NULL;
1790 int len;
Bram Moolenaar604729e2013-08-30 16:44:19 +02001791 IShellLinkW *pslw = NULL;
1792 WIN32_FIND_DATAW ffdw; // we get those free of charge
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793
Bram Moolenaar0f873732019-12-05 20:28:46 +01001794 // Check if the file name ends in ".lnk". Avoid calling
1795 // CoCreateInstance(), it's quite slow.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 if (fname == NULL)
1797 return rfname;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001798 len = (int)STRLEN(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799 if (len <= 4 || STRNICMP(fname + len - 4, ".lnk", 4) != 0)
1800 return rfname;
1801
1802 CoInitialize(NULL);
1803
1804 // create a link manager object and request its interface
1805 hr = CoCreateInstance(
1806 &CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001807 &IID_IShellLinkW, (void**)&pslw);
1808 if (hr == S_OK)
1809 {
1810 WCHAR *p = enc_to_utf16(fname, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001812 if (p != NULL)
1813 {
1814 // Get a pointer to the IPersistFile interface.
1815 hr = pslw->lpVtbl->QueryInterface(
1816 pslw, &IID_IPersistFile, (void**)&ppf);
1817 if (hr != S_OK)
1818 goto shortcut_errorw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001820 // "load" the name and resolve the link
1821 hr = ppf->lpVtbl->Load(ppf, p, STGM_READ);
1822 if (hr != S_OK)
1823 goto shortcut_errorw;
1824# if 0 // This makes Vim wait a long time if the target does not exist.
1825 hr = pslw->lpVtbl->Resolve(pslw, NULL, SLR_NO_UI);
1826 if (hr != S_OK)
1827 goto shortcut_errorw;
Bram Moolenaar604729e2013-08-30 16:44:19 +02001828# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001830 // Get the path to the link target.
1831 ZeroMemory(wsz, MAX_PATH * sizeof(WCHAR));
1832 hr = pslw->lpVtbl->GetPath(pslw, wsz, MAX_PATH, &ffdw, 0);
1833 if (hr == S_OK && wsz[0] != NUL)
1834 rfname = utf16_to_enc(wsz, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835
Bram Moolenaar0eb035c2019-04-02 22:15:55 +02001836shortcut_errorw:
1837 vim_free(p);
1838 }
1839 }
1840
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 // Release all interface pointers (both belong to the same object)
1842 if (ppf != NULL)
1843 ppf->lpVtbl->Release(ppf);
1844 if (psl != NULL)
1845 psl->lpVtbl->Release(psl);
Bram Moolenaar604729e2013-08-30 16:44:19 +02001846 if (pslw != NULL)
1847 pslw->lpVtbl->Release(pslw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848
1849 CoUninitialize();
1850 return rfname;
1851}
Bram Moolenaardce1e892019-02-10 23:18:53 +01001852
1853 char_u *
1854mch_resolve_path(char_u *fname, int reparse_point)
1855{
1856 char_u *path = resolve_shortcut(fname);
1857
1858 if (path == NULL && reparse_point)
1859 path = resolve_reparse_point(fname);
1860 return path;
1861}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862#endif
1863
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001864#if (defined(FEAT_EVAL) && (!defined(FEAT_GUI) || defined(VIMDLL))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865/*
1866 * Bring ourselves to the foreground. Does work if the OS doesn't allow it.
1867 */
1868 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001869win32_set_foreground(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870{
Bram Moolenaar0f873732019-12-05 20:28:46 +01001871 GetConsoleHwnd(); // get value of s_hwnd
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 if (s_hwnd != 0)
1873 SetForegroundWindow(s_hwnd);
1874}
1875#endif
1876
1877#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
1878/*
1879 * Client-server code for Vim
1880 *
1881 * Originally written by Paul Moore
1882 */
1883
Bram Moolenaar0f873732019-12-05 20:28:46 +01001884// In order to handle inter-process messages, we need to have a window. But
1885// the functions in this module can be called before the main GUI window is
1886// created (and may also be called in the console version, where there is no
1887// GUI window at all).
1888//
1889// So we create a hidden window, and arrange to destroy it on exit.
1890HWND message_window = 0; // window that's handling messages
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001892# define VIM_CLASSNAME "VIM_MESSAGES"
1893# define VIM_CLASSNAME_LEN (sizeof(VIM_CLASSNAME) - 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00001895// Communication is via WM_COPYDATA messages. The message type is sent in
Bram Moolenaar0f873732019-12-05 20:28:46 +01001896// the dwData parameter. Types are defined here.
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01001897# define COPYDATA_KEYS 0
1898# define COPYDATA_REPLY 1
1899# define COPYDATA_EXPR 10
1900# define COPYDATA_RESULT 11
1901# define COPYDATA_ERROR_RESULT 12
1902# define COPYDATA_ENCODING 20
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903
Bram Moolenaar0f873732019-12-05 20:28:46 +01001904// This is a structure containing a server HWND and its name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905struct server_id
1906{
1907 HWND hwnd;
1908 char_u *name;
1909};
1910
Bram Moolenaar0f873732019-12-05 20:28:46 +01001911// Last received 'encoding' that the client uses.
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001912static char_u *client_enc = NULL;
1913
1914/*
1915 * Tell the other side what encoding we are using.
1916 * Errors are ignored.
1917 */
1918 static void
1919serverSendEnc(HWND target)
1920{
1921 COPYDATASTRUCT data;
1922
1923 data.dwData = COPYDATA_ENCODING;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001924 data.cbData = (DWORD)STRLEN(p_enc) + 1;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001925 data.lpData = p_enc;
1926 (void)SendMessage(target, WM_COPYDATA, (WPARAM)message_window,
1927 (LPARAM)(&data));
1928}
1929
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930/*
1931 * Clean up on exit. This destroys the hidden message window.
1932 */
1933 static void
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934CleanUpMessaging(void)
1935{
1936 if (message_window != 0)
1937 {
1938 DestroyWindow(message_window);
1939 message_window = 0;
1940 }
1941}
1942
1943static int save_reply(HWND server, char_u *reply, int expr);
1944
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001945/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 * The window procedure for the hidden message window.
1947 * It handles callback messages and notifications from servers.
1948 * In order to process these messages, it is necessary to run a
1949 * message loop. Code which may run before the main message loop
1950 * is started (in the GUI) is careful to pump messages when it needs
1951 * to. Features which require message delivery during normal use will
1952 * not work in the console version - this basically means those
1953 * features which allow Vim to act as a server, rather than a client.
1954 */
1955 static LRESULT CALLBACK
1956Messaging_WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
1957{
1958 if (msg == WM_COPYDATA)
1959 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01001960 // This is a message from another Vim. The dwData member of the
1961 // COPYDATASTRUCT determines the type of message:
1962 // COPYDATA_ENCODING:
1963 // The encoding that the client uses. Following messages will
1964 // use this encoding, convert if needed.
1965 // COPYDATA_KEYS:
1966 // A key sequence. We are a server, and a client wants these keys
1967 // adding to the input queue.
1968 // COPYDATA_REPLY:
1969 // A reply. We are a client, and a server has sent this message
1970 // in response to a request. (server2client())
1971 // COPYDATA_EXPR:
1972 // An expression. We are a server, and a client wants us to
1973 // evaluate this expression.
1974 // COPYDATA_RESULT:
1975 // A reply. We are a client, and a server has sent this message
1976 // in response to a COPYDATA_EXPR.
1977 // COPYDATA_ERROR_RESULT:
1978 // A reply. We are a client, and a server has sent this message
1979 // in response to a COPYDATA_EXPR that failed to evaluate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 COPYDATASTRUCT *data = (COPYDATASTRUCT*)lParam;
1981 HWND sender = (HWND)wParam;
1982 COPYDATASTRUCT reply;
1983 char_u *res;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 int retval;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001985 char_u *str;
1986 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987
1988 switch (data->dwData)
1989 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001990 case COPYDATA_ENCODING:
Bram Moolenaar0f873732019-12-05 20:28:46 +01001991 // Remember the encoding that the client uses.
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001992 vim_free(client_enc);
1993 client_enc = enc_canonize((char_u *)data->lpData);
1994 return 1;
1995
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 case COPYDATA_KEYS:
Bram Moolenaar0f873732019-12-05 20:28:46 +01001997 // Remember who sent this, for <client>
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 clientWindow = sender;
1999
Bram Moolenaar0f873732019-12-05 20:28:46 +01002000 // Add the received keys to the input buffer. The loop waiting
2001 // for the user to do something should check the input buffer.
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002002 str = serverConvert(client_enc, (char_u *)data->lpData, &tofree);
2003 server_to_input_buf(str);
2004 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005
2006# ifdef FEAT_GUI
Bram Moolenaar0f873732019-12-05 20:28:46 +01002007 // Wake up the main GUI loop.
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002008# ifdef VIMDLL
2009 if (gui.in_use)
2010# endif
2011 if (s_hwnd != 0)
2012 PostMessage(s_hwnd, WM_NULL, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013# endif
2014 return 1;
2015
2016 case COPYDATA_EXPR:
Bram Moolenaar0f873732019-12-05 20:28:46 +01002017 // Remember who sent this, for <client>
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 clientWindow = sender;
2019
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002020 str = serverConvert(client_enc, (char_u *)data->lpData, &tofree);
2021 res = eval_client_expr_to_string(str);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002022
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 if (res == NULL)
2024 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00002025 char *err = _(e_invalid_expression_received);
Bram Moolenaar15bf76d2017-03-18 16:18:37 +01002026 size_t len = STRLEN(str) + STRLEN(err) + 5;
2027
Bram Moolenaar964b3742019-05-24 18:54:09 +02002028 res = alloc(len);
Bram Moolenaar15bf76d2017-03-18 16:18:37 +01002029 if (res != NULL)
2030 vim_snprintf((char *)res, len, "%s: \"%s\"", err, str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 reply.dwData = COPYDATA_ERROR_RESULT;
2032 }
2033 else
2034 reply.dwData = COPYDATA_RESULT;
2035 reply.lpData = res;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002036 reply.cbData = (DWORD)STRLEN(res) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002038 serverSendEnc(sender);
Bram Moolenaar5246cd72013-06-16 16:41:47 +02002039 retval = (int)SendMessage(sender, WM_COPYDATA,
2040 (WPARAM)message_window, (LPARAM)(&reply));
Bram Moolenaar15bf76d2017-03-18 16:18:37 +01002041 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 vim_free(res);
2043 return retval;
2044
2045 case COPYDATA_REPLY:
2046 case COPYDATA_RESULT:
2047 case COPYDATA_ERROR_RESULT:
2048 if (data->lpData != NULL)
2049 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002050 str = serverConvert(client_enc, (char_u *)data->lpData,
2051 &tofree);
2052 if (tofree == NULL)
2053 str = vim_strsave(str);
2054 if (save_reply(sender, str,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 (data->dwData == COPYDATA_REPLY ? 0 :
2056 (data->dwData == COPYDATA_RESULT ? 1 :
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002057 2))) == FAIL)
2058 vim_free(str);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002059 else if (data->dwData == COPYDATA_REPLY)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 {
Bram Moolenaar427d51c2013-06-16 16:01:25 +02002061 char_u winstr[30];
2062
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002063 sprintf((char *)winstr, PRINTF_HEX_LONG_U, (long_u)sender);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002064 apply_autocmds(EVENT_REMOTEREPLY, winstr, str,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065 TRUE, curbuf);
2066 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 }
2068 return 1;
2069 }
2070
2071 return 0;
2072 }
2073
2074 else if (msg == WM_ACTIVATE && wParam == WA_ACTIVE)
2075 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002076 // When the message window is activated (brought to the foreground),
2077 // this actually applies to the text window.
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002078# if !defined(FEAT_GUI) || defined(VIMDLL)
2079# ifdef VIMDLL
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002080 if (!gui.in_use)
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002081# endif
Bram Moolenaar0f873732019-12-05 20:28:46 +01002082 GetConsoleHwnd(); // get value of s_hwnd
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002083# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 if (s_hwnd != 0)
2085 {
2086 SetForegroundWindow(s_hwnd);
2087 return 0;
2088 }
2089 }
2090
2091 return DefWindowProc(hwnd, msg, wParam, lParam);
2092}
2093
2094/*
2095 * Initialise the message handling process. This involves creating a window
2096 * to handle messages - the window will not be visible.
2097 */
2098 void
2099serverInitMessaging(void)
2100{
2101 WNDCLASS wndclass;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102
Bram Moolenaar0f873732019-12-05 20:28:46 +01002103 // Clean up on exit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 atexit(CleanUpMessaging);
2105
Bram Moolenaar0f873732019-12-05 20:28:46 +01002106 // Register a window class - we only really care
2107 // about the window procedure
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 wndclass.style = 0;
2109 wndclass.lpfnWndProc = Messaging_WndProc;
2110 wndclass.cbClsExtra = 0;
2111 wndclass.cbWndExtra = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002112 wndclass.hInstance = g_hinst;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 wndclass.hIcon = NULL;
2114 wndclass.hCursor = NULL;
2115 wndclass.hbrBackground = NULL;
2116 wndclass.lpszMenuName = NULL;
2117 wndclass.lpszClassName = VIM_CLASSNAME;
2118 RegisterClass(&wndclass);
2119
Bram Moolenaar0f873732019-12-05 20:28:46 +01002120 // Create the message window. It will be hidden, so the details don't
2121 // matter. Don't use WS_OVERLAPPEDWINDOW, it will make a shortcut remove
2122 // focus from gvim.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 message_window = CreateWindow(VIM_CLASSNAME, "",
2124 WS_POPUPWINDOW | WS_CAPTION,
2125 CW_USEDEFAULT, CW_USEDEFAULT,
2126 100, 100, NULL, NULL,
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002127 g_hinst, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128}
2129
Bram Moolenaar0f873732019-12-05 20:28:46 +01002130// Used by serverSendToVim() to find an alternate server name.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002131static char_u *altname_buf_ptr = NULL;
2132
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133/*
2134 * Get the title of the window "hwnd", which is the Vim server name, in
2135 * "name[namelen]" and return the length.
2136 * Returns zero if window "hwnd" is not a Vim server.
2137 */
2138 static int
2139getVimServerName(HWND hwnd, char *name, int namelen)
2140{
2141 int len;
2142 char buffer[VIM_CLASSNAME_LEN + 1];
2143
Bram Moolenaar0f873732019-12-05 20:28:46 +01002144 // Ignore windows which aren't Vim message windows
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 len = GetClassName(hwnd, buffer, sizeof(buffer));
2146 if (len != VIM_CLASSNAME_LEN || STRCMP(buffer, VIM_CLASSNAME) != 0)
2147 return 0;
2148
Bram Moolenaar0f873732019-12-05 20:28:46 +01002149 // Get the title of the window
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 return GetWindowText(hwnd, name, namelen);
2151}
2152
2153 static BOOL CALLBACK
2154enumWindowsGetServer(HWND hwnd, LPARAM lparam)
2155{
2156 struct server_id *id = (struct server_id *)lparam;
2157 char server[MAX_PATH];
2158
Bram Moolenaar0f873732019-12-05 20:28:46 +01002159 // Get the title of the window
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 if (getVimServerName(hwnd, server, sizeof(server)) == 0)
2161 return TRUE;
2162
Bram Moolenaar0f873732019-12-05 20:28:46 +01002163 // If this is the server we're looking for, return its HWND
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 if (STRICMP(server, id->name) == 0)
2165 {
2166 id->hwnd = hwnd;
2167 return FALSE;
2168 }
2169
Bram Moolenaar0f873732019-12-05 20:28:46 +01002170 // If we are looking for an alternate server, remember this name.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002171 if (altname_buf_ptr != NULL
2172 && STRNICMP(server, id->name, STRLEN(id->name)) == 0
2173 && vim_isdigit(server[STRLEN(id->name)]))
2174 {
2175 STRCPY(altname_buf_ptr, server);
Bram Moolenaar0f873732019-12-05 20:28:46 +01002176 altname_buf_ptr = NULL; // don't use another name
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002177 }
2178
Bram Moolenaar0f873732019-12-05 20:28:46 +01002179 // Otherwise, keep looking
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 return TRUE;
2181}
2182
2183 static BOOL CALLBACK
2184enumWindowsGetNames(HWND hwnd, LPARAM lparam)
2185{
2186 garray_T *ga = (garray_T *)lparam;
2187 char server[MAX_PATH];
2188
Bram Moolenaar0f873732019-12-05 20:28:46 +01002189 // Get the title of the window
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 if (getVimServerName(hwnd, server, sizeof(server)) == 0)
2191 return TRUE;
2192
Bram Moolenaar0f873732019-12-05 20:28:46 +01002193 // Add the name to the list
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002194 ga_concat(ga, (char_u *)server);
2195 ga_concat(ga, (char_u *)"\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196 return TRUE;
2197}
2198
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002199struct enum_windows_s
2200{
2201 WNDENUMPROC lpEnumFunc;
2202 LPARAM lParam;
2203};
2204
2205 static BOOL CALLBACK
2206enum_windows_child(HWND hwnd, LPARAM lParam)
2207{
2208 struct enum_windows_s *ew = (struct enum_windows_s *)lParam;
2209
2210 return (ew->lpEnumFunc)(hwnd, ew->lParam);
2211}
2212
2213 static BOOL CALLBACK
2214enum_windows_toplevel(HWND hwnd, LPARAM lParam)
2215{
2216 struct enum_windows_s *ew = (struct enum_windows_s *)lParam;
2217
Bram Moolenaar95ba5c32018-10-07 22:47:07 +02002218 if ((ew->lpEnumFunc)(hwnd, ew->lParam))
2219 return TRUE;
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002220 return EnumChildWindows(hwnd, enum_windows_child, lParam);
2221}
2222
Bram Moolenaar0f873732019-12-05 20:28:46 +01002223/*
2224 * Enumerate all windows including children.
2225 */
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002226 static BOOL
2227enum_windows(WNDENUMPROC lpEnumFunc, LPARAM lParam)
2228{
2229 struct enum_windows_s ew;
2230
2231 ew.lpEnumFunc = lpEnumFunc;
2232 ew.lParam = lParam;
2233 return EnumWindows(enum_windows_toplevel, (LPARAM)&ew);
2234}
2235
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 static HWND
2237findServer(char_u *name)
2238{
2239 struct server_id id;
2240
2241 id.name = name;
2242 id.hwnd = 0;
2243
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002244 enum_windows(enumWindowsGetServer, (LPARAM)(&id));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245
2246 return id.hwnd;
2247}
2248
2249 void
2250serverSetName(char_u *name)
2251{
2252 char_u *ok_name;
2253 HWND hwnd = 0;
2254 int i = 0;
2255 char_u *p;
2256
Bram Moolenaar0f873732019-12-05 20:28:46 +01002257 // Leave enough space for a 9-digit suffix to ensure uniqueness!
Bram Moolenaar964b3742019-05-24 18:54:09 +02002258 ok_name = alloc(STRLEN(name) + 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259
2260 STRCPY(ok_name, name);
2261 p = ok_name + STRLEN(name);
2262
2263 for (;;)
2264 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002265 // This is inefficient - we're doing an EnumWindows loop for each
2266 // possible name. It would be better to grab all names in one go,
2267 // and scan the list each time...
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 hwnd = findServer(ok_name);
2269 if (hwnd == 0)
2270 break;
2271
2272 ++i;
2273 if (i >= 1000)
2274 break;
2275
2276 sprintf((char *)p, "%d", i);
2277 }
2278
2279 if (hwnd != 0)
2280 vim_free(ok_name);
2281 else
2282 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002283 // Remember the name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 serverName = ok_name;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002285 need_maketitle = TRUE; // update Vim window title later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286
Bram Moolenaar0f873732019-12-05 20:28:46 +01002287 // Update the message window title
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002288 SetWindowText(message_window, (LPCSTR)ok_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002290# ifdef FEAT_EVAL
Bram Moolenaar0f873732019-12-05 20:28:46 +01002291 // Set the servername variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 set_vim_var_string(VV_SEND_SERVER, serverName, -1);
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002293# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 }
2295}
2296
2297 char_u *
2298serverGetVimNames(void)
2299{
2300 garray_T ga;
2301
2302 ga_init2(&ga, 1, 100);
2303
Bram Moolenaarc0543e12018-10-07 20:35:12 +02002304 enum_windows(enumWindowsGetNames, (LPARAM)(&ga));
Bram Moolenaar269ec652004-07-29 08:43:53 +00002305 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306
2307 return ga.ga_data;
2308}
2309
2310 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002311serverSendReply(
Bram Moolenaar0f873732019-12-05 20:28:46 +01002312 char_u *name, // Where to send.
2313 char_u *reply) // What to send.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314{
2315 HWND target;
2316 COPYDATASTRUCT data;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002317 long_u n = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318
Bram Moolenaar0f873732019-12-05 20:28:46 +01002319 // The "name" argument is a magic cookie obtained from expand("<client>").
2320 // It should be of the form 0xXXXXX - i.e. a C hex literal, which is the
2321 // value of the client's message window HWND.
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002322 sscanf((char *)name, SCANF_HEX_LONG_U, &n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 if (n == 0)
2324 return -1;
2325
2326 target = (HWND)n;
2327 if (!IsWindow(target))
2328 return -1;
2329
2330 data.dwData = COPYDATA_REPLY;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002331 data.cbData = (DWORD)STRLEN(reply) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 data.lpData = reply;
2333
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002334 serverSendEnc(target);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 if (SendMessage(target, WM_COPYDATA, (WPARAM)message_window,
2336 (LPARAM)(&data)))
2337 return 0;
2338
2339 return -1;
2340}
2341
2342 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002343serverSendToVim(
Bram Moolenaar0f873732019-12-05 20:28:46 +01002344 char_u *name, // Where to send.
2345 char_u *cmd, // What to send.
2346 char_u **result, // Result of eval'ed expression
2347 void *ptarget, // HWND of server
2348 int asExpr, // Expression or keys?
2349 int timeout, // timeout in seconds or zero
2350 int silent) // don't complain about no server
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351{
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002352 HWND target;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 COPYDATASTRUCT data;
2354 char_u *retval = NULL;
2355 int retcode = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002356 char_u altname_buf[MAX_PATH];
2357
Bram Moolenaar0f873732019-12-05 20:28:46 +01002358 // Execute locally if no display or target is ourselves
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01002359 if (serverName != NULL && STRICMP(name, serverName) == 0)
2360 return sendToLocalVim(cmd, asExpr, result);
2361
Bram Moolenaar0f873732019-12-05 20:28:46 +01002362 // If the server name does not end in a digit then we look for an
Dominique Pelleaf4a61a2021-12-27 17:21:41 +00002363 // alternate name. e.g. when "name" is GVIM then we may find GVIM2.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002364 if (STRLEN(name) > 1 && !vim_isdigit(name[STRLEN(name) - 1]))
2365 altname_buf_ptr = altname_buf;
2366 altname_buf[0] = NUL;
2367 target = findServer(name);
2368 altname_buf_ptr = NULL;
2369 if (target == 0 && altname_buf[0] != NUL)
Bram Moolenaar0f873732019-12-05 20:28:46 +01002370 // Use another server name we found.
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002371 target = findServer(altname_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372
2373 if (target == 0)
2374 {
2375 if (!silent)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002376 semsg(_(e_no_registered_server_named_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 return -1;
2378 }
2379
2380 if (ptarget)
2381 *(HWND *)ptarget = target;
2382
2383 data.dwData = asExpr ? COPYDATA_EXPR : COPYDATA_KEYS;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002384 data.cbData = (DWORD)STRLEN(cmd) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 data.lpData = cmd;
2386
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002387 serverSendEnc(target);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 if (SendMessage(target, WM_COPYDATA, (WPARAM)message_window,
2389 (LPARAM)(&data)) == 0)
2390 return -1;
2391
2392 if (asExpr)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002393 retval = serverGetReply(target, &retcode, TRUE, TRUE, timeout);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394
2395 if (result == NULL)
2396 vim_free(retval);
2397 else
Bram Moolenaar0f873732019-12-05 20:28:46 +01002398 *result = retval; // Caller assumes responsibility for freeing
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399
2400 return retcode;
2401}
2402
2403/*
2404 * Bring the server to the foreground.
2405 */
2406 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002407serverForeground(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408{
2409 HWND target = findServer(name);
2410
2411 if (target != 0)
2412 SetForegroundWindow(target);
2413}
2414
Bram Moolenaar0f873732019-12-05 20:28:46 +01002415// Replies from server need to be stored until the client picks them up via
2416// remote_read(). So we maintain a list of server-id/reply pairs.
2417// Note that there could be multiple replies from one server pending if the
2418// client is slow picking them up.
2419// We just store the replies in a simple list. When we remove an entry, we
2420// move list entries down to fill the gap.
2421// The server ID is simply the HWND.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422typedef struct
2423{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002424 HWND server; // server window
2425 char_u *reply; // reply string
2426 int expr_result; // 0 for REPLY, 1 for RESULT 2 for error
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002427} reply_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428
2429static garray_T reply_list = {0, 0, sizeof(reply_T), 5, 0};
2430
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002431# define REPLY_ITEM(i) ((reply_T *)(reply_list.ga_data) + (i))
2432# define REPLY_COUNT (reply_list.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433
Bram Moolenaar0f873732019-12-05 20:28:46 +01002434// Flag which is used to wait for a reply
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435static int reply_received = 0;
2436
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002437/*
2438 * Store a reply. "reply" must be allocated memory (or NULL).
2439 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 static int
2441save_reply(HWND server, char_u *reply, int expr)
2442{
2443 reply_T *rep;
2444
2445 if (ga_grow(&reply_list, 1) == FAIL)
2446 return FAIL;
2447
2448 rep = REPLY_ITEM(REPLY_COUNT);
2449 rep->server = server;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002450 rep->reply = reply;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 rep->expr_result = expr;
2452 if (rep->reply == NULL)
2453 return FAIL;
2454
2455 ++REPLY_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 reply_received = 1;
2457 return OK;
2458}
2459
2460/*
2461 * Get a reply from server "server".
2462 * When "expr_res" is non NULL, get the result of an expression, otherwise a
2463 * server2client() message.
2464 * When non NULL, point to return code. 0 => OK, -1 => ERROR
2465 * If "remove" is TRUE, consume the message, the caller must free it then.
2466 * if "wait" is TRUE block until a message arrives (or the server exits).
2467 */
2468 char_u *
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002469serverGetReply(HWND server, int *expr_res, int remove, int wait, int timeout)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470{
2471 int i;
2472 char_u *reply;
2473 reply_T *rep;
Bram Moolenaar15e737f2017-03-18 21:22:47 +01002474 int did_process = FALSE;
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002475 time_t start;
2476 time_t now;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477
Bram Moolenaar0f873732019-12-05 20:28:46 +01002478 // When waiting, loop until the message waiting for is received.
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002479 time(&start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 for (;;)
2481 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002482 // Reset this here, in case a message arrives while we are going
2483 // through the already received messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 reply_received = 0;
2485
2486 for (i = 0; i < REPLY_COUNT; ++i)
2487 {
2488 rep = REPLY_ITEM(i);
2489 if (rep->server == server
2490 && ((rep->expr_result != 0) == (expr_res != NULL)))
2491 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002492 // Save the values we've found for later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 reply = rep->reply;
2494 if (expr_res != NULL)
2495 *expr_res = rep->expr_result == 1 ? 0 : -1;
2496
2497 if (remove)
2498 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002499 // Move the rest of the list down to fill the gap
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 mch_memmove(rep, rep + 1,
2501 (REPLY_COUNT - i - 1) * sizeof(reply_T));
2502 --REPLY_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 }
2504
Bram Moolenaar0f873732019-12-05 20:28:46 +01002505 // Return the reply to the caller, who takes on responsibility
2506 // for freeing it if "remove" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 return reply;
2508 }
2509 }
2510
Bram Moolenaar0f873732019-12-05 20:28:46 +01002511 // If we got here, we didn't find a reply. Return immediately if the
2512 // "wait" parameter isn't set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 if (!wait)
Bram Moolenaar15e737f2017-03-18 21:22:47 +01002514 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002515 // Process pending messages once. Without this, looping on
2516 // remote_peek() would never get the reply.
Bram Moolenaar15e737f2017-03-18 21:22:47 +01002517 if (!did_process)
2518 {
2519 did_process = TRUE;
2520 serverProcessPendingMessages();
2521 continue;
2522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 break;
Bram Moolenaar15e737f2017-03-18 21:22:47 +01002524 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525
Bram Moolenaar0f873732019-12-05 20:28:46 +01002526 // We need to wait for a reply. Enter a message loop until the
2527 // "reply_received" flag gets set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528
Bram Moolenaar0f873732019-12-05 20:28:46 +01002529 // Loop until we receive a reply
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 while (reply_received == 0)
2531 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002532# ifdef FEAT_TIMERS
Bram Moolenaar0f873732019-12-05 20:28:46 +01002533 // TODO: use the return value to decide how long to wait.
Bram Moolenaar42205552017-03-18 19:42:22 +01002534 check_due_timer();
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002535# endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002536 time(&now);
2537 if (timeout > 0 && (now - start) >= timeout)
2538 break;
2539
Bram Moolenaar0f873732019-12-05 20:28:46 +01002540 // Wait for a SendMessage() call to us. This could be the reply
2541 // we are waiting for. Use a timeout of a second, to catch the
2542 // situation that the server died unexpectedly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 MsgWaitForMultipleObjects(0, NULL, TRUE, 1000, QS_ALLINPUT);
2544
Bram Moolenaar0f873732019-12-05 20:28:46 +01002545 // If the server has died, give up
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 if (!IsWindow(server))
2547 return NULL;
2548
2549 serverProcessPendingMessages();
2550 }
2551 }
2552
2553 return NULL;
2554}
2555
2556/*
2557 * Process any messages in the Windows message queue.
2558 */
2559 void
2560serverProcessPendingMessages(void)
2561{
2562 MSG msg;
2563
K.Takatab7057bd2022-01-21 11:37:07 +00002564 while (PeekMessageW(&msg, NULL, 0, 0, PM_REMOVE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 {
2566 TranslateMessage(&msg);
K.Takatab7057bd2022-01-21 11:37:07 +00002567 DispatchMessageW(&msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 }
2569}
2570
Bram Moolenaar0f873732019-12-05 20:28:46 +01002571#endif // FEAT_CLIENTSERVER
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572
2573#if defined(FEAT_GUI) || (defined(FEAT_PRINTER) && !defined(FEAT_POSTSCRIPT)) \
2574 || defined(PROTO)
2575
2576struct charset_pair
2577{
2578 char *name;
2579 BYTE charset;
2580};
2581
2582static struct charset_pair
2583charset_pairs[] =
2584{
2585 {"ANSI", ANSI_CHARSET},
2586 {"CHINESEBIG5", CHINESEBIG5_CHARSET},
2587 {"DEFAULT", DEFAULT_CHARSET},
2588 {"HANGEUL", HANGEUL_CHARSET},
2589 {"OEM", OEM_CHARSET},
2590 {"SHIFTJIS", SHIFTJIS_CHARSET},
2591 {"SYMBOL", SYMBOL_CHARSET},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 {"ARABIC", ARABIC_CHARSET},
2593 {"BALTIC", BALTIC_CHARSET},
2594 {"EASTEUROPE", EASTEUROPE_CHARSET},
2595 {"GB2312", GB2312_CHARSET},
2596 {"GREEK", GREEK_CHARSET},
2597 {"HEBREW", HEBREW_CHARSET},
2598 {"JOHAB", JOHAB_CHARSET},
2599 {"MAC", MAC_CHARSET},
2600 {"RUSSIAN", RUSSIAN_CHARSET},
2601 {"THAI", THAI_CHARSET},
2602 {"TURKISH", TURKISH_CHARSET},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002603# ifdef VIETNAMESE_CHARSET
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 {"VIETNAMESE", VIETNAMESE_CHARSET},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002605# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 {NULL, 0}
2607};
2608
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002609struct quality_pair
2610{
2611 char *name;
2612 DWORD quality;
2613};
2614
2615static struct quality_pair
2616quality_pairs[] = {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002617# ifdef CLEARTYPE_QUALITY
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002618 {"CLEARTYPE", CLEARTYPE_QUALITY},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002619# endif
2620# ifdef ANTIALIASED_QUALITY
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002621 {"ANTIALIASED", ANTIALIASED_QUALITY},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002622# endif
2623# ifdef NONANTIALIASED_QUALITY
Bram Moolenaar73a733e2016-05-11 21:05:05 +02002624 {"NONANTIALIASED", NONANTIALIASED_QUALITY},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002625# endif
2626# ifdef PROOF_QUALITY
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002627 {"PROOF", PROOF_QUALITY},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002628# endif
2629# ifdef DRAFT_QUALITY
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002630 {"DRAFT", DRAFT_QUALITY},
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002631# endif
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002632 {"DEFAULT", DEFAULT_QUALITY},
2633 {NULL, 0}
2634};
2635
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636/*
2637 * Convert a charset ID to a name.
2638 * Return NULL when not recognized.
2639 */
2640 char *
2641charset_id2name(int id)
2642{
2643 struct charset_pair *cp;
2644
2645 for (cp = charset_pairs; cp->name != NULL; ++cp)
2646 if ((BYTE)id == cp->charset)
2647 break;
2648 return cp->name;
2649}
2650
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002651/*
2652 * Convert a quality ID to a name.
2653 * Return NULL when not recognized.
2654 */
2655 char *
2656quality_id2name(DWORD id)
2657{
2658 struct quality_pair *qp;
2659
2660 for (qp = quality_pairs; qp->name != NULL; ++qp)
2661 if (id == qp->quality)
2662 break;
2663 return qp->name;
2664}
2665
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002666static const LOGFONTW s_lfDefault =
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667{
2668 -12, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, DEFAULT_CHARSET,
2669 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
2670 PROOF_QUALITY, FIXED_PITCH | FF_DONTCARE,
Bram Moolenaar0f873732019-12-05 20:28:46 +01002671 L"Fixedsys" // see _ReadVimIni
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672};
2673
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002674// Initialise the "current height" to -12 (same as s_lfDefault) just
2675// in case the user specifies a font in "guifont" with no size before a font
2676// with an explicit size has been set. This defaults the size to this value
2677// (-12 equates to roughly 9pt).
2678int current_font_height = -12; // also used in gui_w32.c
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679
Bram Moolenaar0f873732019-12-05 20:28:46 +01002680/*
2681 * Convert a string representing a point size into pixels. The string should
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 * be a positive decimal number, with an optional decimal point (eg, "12", or
2683 * "10.5"). The pixel value is returned, and a pointer to the next unconverted
2684 * character is stored in *end. The flag "vertical" says whether this
2685 * calculation is for a vertical (height) size or a horizontal (width) one.
2686 */
2687 static int
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002688points_to_pixels(WCHAR *str, WCHAR **end, int vertical, long_i pprinter_dc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689{
2690 int pixels;
2691 int points = 0;
2692 int divisor = 0;
2693 HWND hwnd = (HWND)0;
2694 HDC hdc;
2695 HDC printer_dc = (HDC)pprinter_dc;
2696
2697 while (*str != NUL)
2698 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002699 if (*str == L'.' && divisor == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 {
Bram Moolenaar0f873732019-12-05 20:28:46 +01002701 // Start keeping a divisor, for later
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 divisor = 1;
2703 }
2704 else
2705 {
2706 if (!VIM_ISDIGIT(*str))
2707 break;
2708
2709 points *= 10;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002710 points += *str - L'0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 divisor *= 10;
2712 }
2713 ++str;
2714 }
2715
2716 if (divisor == 0)
2717 divisor = 1;
2718
2719 if (printer_dc == NULL)
2720 {
2721 hwnd = GetDesktopWindow();
2722 hdc = GetWindowDC(hwnd);
2723 }
2724 else
2725 hdc = printer_dc;
2726
2727 pixels = MulDiv(points,
2728 GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX),
2729 72 * divisor);
2730
2731 if (printer_dc == NULL)
2732 ReleaseDC(hwnd, hdc);
2733
2734 *end = str;
2735 return pixels;
2736}
2737
2738 static int CALLBACK
2739font_enumproc(
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002740 ENUMLOGFONTW *elf,
2741 NEWTEXTMETRICW *ntm UNUSED,
2742 DWORD type UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 LPARAM lparam)
2744{
Bram Moolenaar0f873732019-12-05 20:28:46 +01002745 // Return value:
2746 // 0 = terminate now (monospace & ANSI)
2747 // 1 = continue, still no luck...
2748 // 2 = continue, but we have an acceptable LOGFONTW
2749 // (monospace, not ANSI)
2750 // We use these values, as EnumFontFamilies returns 1 if the
2751 // callback function is never called. So, we check the return as
2752 // 0 = perfect, 2 = OK, 1 = no good...
2753 // It's not pretty, but it works!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002755 LOGFONTW *lf = (LOGFONTW *)(lparam);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002757# ifndef FEAT_PROPORTIONAL_FONTS
Bram Moolenaar0f873732019-12-05 20:28:46 +01002758 // Ignore non-monospace fonts without further ado
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759 if ((ntm->tmPitchAndFamily & 1) != 0)
2760 return 1;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002761# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762
Bram Moolenaar0f873732019-12-05 20:28:46 +01002763 // Remember this LOGFONTW as a "possible"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 *lf = elf->elfLogFont;
2765
Bram Moolenaar0f873732019-12-05 20:28:46 +01002766 // Terminate the scan as soon as we find an ANSI font
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 if (lf->lfCharSet == ANSI_CHARSET
2768 || lf->lfCharSet == OEM_CHARSET
2769 || lf->lfCharSet == DEFAULT_CHARSET)
2770 return 0;
2771
Bram Moolenaar0f873732019-12-05 20:28:46 +01002772 // Continue the scan - we have a non-ANSI font
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773 return 2;
2774}
2775
2776 static int
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002777init_logfont(LOGFONTW *lf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778{
2779 int n;
2780 HWND hwnd = GetDesktopWindow();
2781 HDC hdc = GetWindowDC(hwnd);
2782
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002783 n = EnumFontFamiliesW(hdc,
2784 lf->lfFaceName,
2785 (FONTENUMPROCW)font_enumproc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 (LPARAM)lf);
2787
2788 ReleaseDC(hwnd, hdc);
2789
Bram Moolenaar0f873732019-12-05 20:28:46 +01002790 // If we couldn't find a usable font, return failure
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 if (n == 1)
2792 return FAIL;
2793
Bram Moolenaar0f873732019-12-05 20:28:46 +01002794 // Tidy up the rest of the LOGFONTW structure. We set to a basic
2795 // font - get_logfont() sets bold, italic, etc based on the user's
2796 // input.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 lf->lfHeight = current_font_height;
2798 lf->lfWidth = 0;
2799 lf->lfItalic = FALSE;
2800 lf->lfUnderline = FALSE;
2801 lf->lfStrikeOut = FALSE;
2802 lf->lfWeight = FW_NORMAL;
2803
Bram Moolenaar0f873732019-12-05 20:28:46 +01002804 // Return success
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 return OK;
2806}
2807
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002808/*
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002809 * Compare a UTF-16 string and an ASCII string literally.
2810 * Only works all the code points are inside ASCII range.
2811 */
2812 static int
2813utf16ascncmp(const WCHAR *w, const char *p, size_t n)
2814{
2815 size_t i;
2816
2817 for (i = 0; i < n; i++)
2818 {
2819 if (w[i] == 0 || w[i] != p[i])
2820 return w[i] - p[i];
2821 }
2822 return 0;
2823}
2824
2825/*
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002826 * Get font info from "name" into logfont "lf".
2827 * Return OK for a valid name, FAIL otherwise.
2828 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829 int
2830get_logfont(
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002831 LOGFONTW *lf,
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002832 char_u *name,
2833 HDC printer_dc,
2834 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835{
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002836 WCHAR *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837 int i;
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002838 int ret = FAIL;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002839 static LOGFONTW *lastlf = NULL;
2840 WCHAR *wname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841
2842 *lf = s_lfDefault;
2843 if (name == NULL)
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002844 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002846 wname = enc_to_utf16(name, NULL);
2847 if (wname == NULL)
2848 return FAIL;
2849
2850 if (wcscmp(wname, L"*") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 {
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002852# if defined(FEAT_GUI_MSWIN)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002853 CHOOSEFONTW cf;
Bram Moolenaar0f873732019-12-05 20:28:46 +01002854 // if name is "*", bring up std font dialog:
Bram Moolenaara80faa82020-04-12 19:37:17 +02002855 CLEAR_FIELD(cf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 cf.lStructSize = sizeof(cf);
2857 cf.hwndOwner = s_hwnd;
2858 cf.Flags = CF_SCREENFONTS | CF_FIXEDPITCHONLY | CF_INITTOLOGFONTSTRUCT;
2859 if (lastlf != NULL)
2860 *lf = *lastlf;
2861 cf.lpLogFont = lf;
2862 cf.nFontType = 0 ; //REGULAR_FONTTYPE;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002863 if (ChooseFontW(&cf))
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002864 ret = OK;
Bram Moolenaar912bc4a2019-12-01 18:58:11 +01002865# endif
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002866 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 }
2868
2869 /*
2870 * Split name up, it could be <name>:h<height>:w<width> etc.
2871 */
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002872 for (p = wname; *p && *p != L':'; p++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002874 if (p - wname + 1 >= LF_FACESIZE)
Bram Moolenaar0f873732019-12-05 20:28:46 +01002875 goto theend; // Name too long
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002876 lf->lfFaceName[p - wname] = *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 }
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002878 if (p != wname)
2879 lf->lfFaceName[p - wname] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880
Bram Moolenaar0f873732019-12-05 20:28:46 +01002881 // First set defaults
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 lf->lfHeight = -12;
2883 lf->lfWidth = 0;
2884 lf->lfWeight = FW_NORMAL;
2885 lf->lfItalic = FALSE;
2886 lf->lfUnderline = FALSE;
2887 lf->lfStrikeOut = FALSE;
2888
2889 /*
2890 * If the font can't be found, try replacing '_' by ' '.
2891 */
2892 if (init_logfont(lf) == FAIL)
2893 {
2894 int did_replace = FALSE;
2895
2896 for (i = 0; lf->lfFaceName[i]; ++i)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002897 if (lf->lfFaceName[i] == L'_')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002899 lf->lfFaceName[i] = L' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900 did_replace = TRUE;
2901 }
2902 if (!did_replace || init_logfont(lf) == FAIL)
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002903 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904 }
2905
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002906 while (*p == L':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 p++;
2908
Bram Moolenaar0f873732019-12-05 20:28:46 +01002909 // Set the values found after ':'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 while (*p)
2911 {
2912 switch (*p++)
2913 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002914 case L'h':
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002915 lf->lfHeight = - points_to_pixels(p, &p, TRUE, (long_i)printer_dc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002917 case L'w':
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002918 lf->lfWidth = points_to_pixels(p, &p, FALSE, (long_i)printer_dc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 break;
Bram Moolenaarf720d0a2019-04-28 14:02:47 +02002920 case L'W':
2921 lf->lfWeight = wcstol(p, &p, 10);
2922 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002923 case L'b':
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 lf->lfWeight = FW_BOLD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002926 case L'i':
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 lf->lfItalic = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002929 case L'u':
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 lf->lfUnderline = TRUE;
2931 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002932 case L's':
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 lf->lfStrikeOut = TRUE;
2934 break;
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002935 case L'c':
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936 {
2937 struct charset_pair *cp;
2938
2939 for (cp = charset_pairs; cp->name != NULL; ++cp)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002940 if (utf16ascncmp(p, cp->name, strlen(cp->name)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 {
2942 lf->lfCharSet = cp->charset;
2943 p += strlen(cp->name);
2944 break;
2945 }
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002946 if (cp->name == NULL && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002948 char_u *s = utf16_to_enc(p, NULL);
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00002949
2950 semsg(_(e_illegal_str_name_str_in_font_name_str),
2951 "charset", s, name);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002952 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 break;
2954 }
2955 break;
2956 }
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002957 case L'q':
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002958 {
2959 struct quality_pair *qp;
2960
2961 for (qp = quality_pairs; qp->name != NULL; ++qp)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002962 if (utf16ascncmp(p, qp->name, strlen(qp->name)) == 0)
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002963 {
2964 lf->lfQuality = qp->quality;
2965 p += strlen(qp->name);
2966 break;
2967 }
2968 if (qp->name == NULL && verbose)
2969 {
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002970 char_u *s = utf16_to_enc(p, NULL);
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00002971 semsg(_(e_illegal_str_name_str_in_font_name_str),
2972 "quality", s, name);
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002973 vim_free(s);
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002974 break;
2975 }
2976 break;
2977 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 default:
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002979 if (verbose)
Bram Moolenaarcbadefe2022-01-01 19:33:50 +00002980 semsg(_(e_illegal_char_nr_in_font_name_str), p[-1], name);
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002981 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 }
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002983 while (*p == L':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984 p++;
2985 }
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002986 ret = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988theend:
Bram Moolenaar0f873732019-12-05 20:28:46 +01002989 // ron: init lastlf
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002990 if (ret == OK && printer_dc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991 {
2992 vim_free(lastlf);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002993 lastlf = ALLOC_ONE(LOGFONTW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994 if (lastlf != NULL)
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002995 mch_memmove(lastlf, lf, sizeof(LOGFONTW));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996 }
Bram Moolenaar433a5eb2019-03-30 16:24:16 +01002997 vim_free(wname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002999 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000}
3001
Bram Moolenaar0f873732019-12-05 20:28:46 +01003002#endif // defined(FEAT_GUI) || defined(FEAT_PRINTER)
Bram Moolenaarf12d9832016-01-29 21:11:25 +01003003
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003004#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaarf12d9832016-01-29 21:11:25 +01003005/*
3006 * Initialize the Winsock dll.
3007 */
3008 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003009channel_init_winsock(void)
Bram Moolenaarf12d9832016-01-29 21:11:25 +01003010{
3011 WSADATA wsaData;
3012 int wsaerr;
3013
3014 if (WSInitialized)
3015 return;
3016
3017 wsaerr = WSAStartup(MAKEWORD(2, 2), &wsaData);
3018 if (wsaerr == 0)
3019 WSInitialized = TRUE;
3020}
3021#endif