blob: 14fe8c6a22ced789d3d85e769cdf8c7e5f03b407 [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
35# if defined(FEAT_TITLE) && !defined(FEAT_GUI_W32)
36# 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 Moolenaar82881492012-11-20 16:53:39 +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
80#define WINAPI
81#define WINBASEAPI
82typedef int BOOL;
83typedef int CALLBACK;
84typedef int COLORREF;
85typedef int CONSOLE_CURSOR_INFO;
86typedef int COORD;
87typedef int DWORD;
88typedef int ENUMLOGFONT;
89typedef int HANDLE;
90typedef int HDC;
91typedef int HFONT;
92typedef int HICON;
93typedef int HWND;
94typedef int INPUT_RECORD;
95typedef int KEY_EVENT_RECORD;
96typedef int LOGFONT;
97typedef int LPARAM;
98typedef int LPBOOL;
99typedef int LPCSTR;
100typedef int LPCWSTR;
101typedef int LPSTR;
102typedef int LPTSTR;
103typedef int LPWSTR;
104typedef int LRESULT;
105typedef int MOUSE_EVENT_RECORD;
106typedef int NEWTEXTMETRIC;
107typedef int PACL;
108typedef int PRINTDLG;
109typedef int PSECURITY_DESCRIPTOR;
110typedef int PSID;
111typedef int SECURITY_INFORMATION;
112typedef int SHORT;
113typedef int SMALL_RECT;
114typedef int TEXTMETRIC;
115typedef int UINT;
116typedef int WCHAR;
117typedef int WORD;
118typedef int WPARAM;
119typedef void VOID;
120#endif
121
122/* Record all output and all keyboard & mouse input */
123/* #define MCH_WRITE_DUMP */
124
125#ifdef MCH_WRITE_DUMP
126FILE* fdDump = NULL;
127#endif
128
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129#ifndef FEAT_GUI_MSWIN
130extern char g_szOrigTitle[];
131#endif
132
133#ifdef FEAT_GUI
134extern HWND s_hwnd;
135#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136static HWND s_hwnd = 0; /* console window handle, set by GetConsoleHwnd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137#endif
138
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100139#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100140int WSInitialized = FALSE; /* WinSock is initialized */
141#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142
143/* Don't generate prototypes here, because some systems do have these
144 * functions. */
145#if defined(__GNUC__) && !defined(PROTO)
146# ifndef __MINGW32__
147int _stricoll(char *a, char *b)
148{
149 // the ANSI-ish correct way is to use strxfrm():
150 char a_buff[512], b_buff[512]; // file names, so this is enough on Win32
151 strxfrm(a_buff, a, 512);
152 strxfrm(b_buff, b, 512);
153 return strcoll(a_buff, b_buff);
154}
155
156char * _fullpath(char *buf, char *fname, int len)
157{
158 LPTSTR toss;
159
160 return (char *)GetFullPathName(fname, len, buf, &toss);
161}
162# endif
163
Bram Moolenaaraf62ff32013-03-19 14:48:29 +0100164# if !defined(__MINGW32__) || (__GNUC__ < 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165int _chdrive(int drive)
166{
167 char temp [3] = "-:";
168 temp[0] = drive + 'A' - 1;
169 return !SetCurrentDirectory(temp);
170}
Bram Moolenaaraf62ff32013-03-19 14:48:29 +0100171# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172#else
173# ifdef __BORLANDC__
174/* being a more ANSI compliant compiler, BorlandC doesn't define _stricoll:
175 * but it does in BC 5.02! */
176# if __BORLANDC__ < 0x502
177int _stricoll(char *a, char *b)
178{
179# if 1
180 // this is fast but not correct:
181 return stricmp(a, b);
182# else
183 // the ANSI-ish correct way is to use strxfrm():
184 char a_buff[512], b_buff[512]; // file names, so this is enough on Win32
185 strxfrm(a_buff, a, 512);
186 strxfrm(b_buff, b, 512);
187 return strcoll(a_buff, b_buff);
188# endif
189}
190# endif
191# endif
192#endif
193
194
195#if defined(FEAT_GUI_MSWIN) || defined(PROTO)
196/*
197 * GUI version of mch_exit().
198 * Shut down and exit with status `r'
199 * Careful: mch_exit() may be called before mch_init()!
200 */
201 void
202mch_exit(int r)
203{
204 display_errors();
205
206 ml_close_all(TRUE); /* remove all memfiles */
207
208# ifdef FEAT_OLE
209 UninitOLE();
210# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100211# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000212 if (WSInitialized)
213 {
214 WSInitialized = FALSE;
215 WSACleanup();
216 }
217# endif
218#ifdef DYNAMIC_GETTEXT
219 dyn_libintl_end();
220#endif
221
222 if (gui.in_use)
223 gui_exit(r);
Bram Moolenaar85c79d32007-02-20 01:59:20 +0000224
225#ifdef EXITFREE
226 free_all_mem();
227#endif
228
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229 exit(r);
230}
231
232#endif /* FEAT_GUI_MSWIN */
233
234
235/*
236 * Init the tables for toupper() and tolower().
237 */
238 void
239mch_early_init(void)
240{
241 int i;
242
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243 PlatformId();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244
245 /* Init the tables for toupper() and tolower() */
246 for (i = 0; i < 256; ++i)
247 toupper_tab[i] = tolower_tab[i] = i;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100248 CharUpperBuff((LPSTR)toupper_tab, 256);
249 CharLowerBuff((LPSTR)tolower_tab, 256);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000250}
251
252
253/*
254 * Return TRUE if the input comes from a terminal, FALSE otherwise.
255 */
256 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100257mch_input_isatty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258{
259#ifdef FEAT_GUI_MSWIN
260 return OK; /* GUI always has a tty */
261#else
262 if (isatty(read_cmd_fd))
263 return TRUE;
264 return FALSE;
265#endif
266}
267
268#ifdef FEAT_TITLE
269/*
270 * mch_settitle(): set titlebar of our window
271 */
272 void
273mch_settitle(
274 char_u *title,
275 char_u *icon)
276{
277# ifdef FEAT_GUI_MSWIN
278 gui_mch_settitle(title, icon);
279# else
280 if (title != NULL)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000281 {
282# ifdef FEAT_MBYTE
283 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
284 {
285 /* Convert the title from 'encoding' to the active codepage. */
Bram Moolenaar36f692d2008-11-20 16:10:17 +0000286 WCHAR *wp = enc_to_utf16(title, NULL);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000287
288 if (wp != NULL)
289 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200290 SetConsoleTitleW(wp);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000291 vim_free(wp);
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200292 return;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000293 }
294 }
295# endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100296 SetConsoleTitle((LPCSTR)title);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000297 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000298# endif
299}
300
301
302/*
303 * Restore the window/icon title.
304 * which is one of:
305 * 1: Just restore title
306 * 2: Just restore icon (which we don't have)
307 * 3: Restore title and icon (which we don't have)
308 */
309 void
Bram Moolenaar1266d672017-02-01 13:43:36 +0100310mch_restore_title(int which UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311{
312#ifndef FEAT_GUI_MSWIN
Bram Moolenaar1df52d72014-10-15 22:50:10 +0200313 SetConsoleTitle(g_szOrigTitle);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314#endif
315}
316
317
318/*
319 * Return TRUE if we can restore the title (we can)
320 */
321 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100322mch_can_restore_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323{
324 return TRUE;
325}
326
327
328/*
329 * Return TRUE if we can restore the icon title (we can't)
330 */
331 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100332mch_can_restore_icon(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333{
334 return FALSE;
335}
336#endif /* FEAT_TITLE */
337
338
339/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000340 * Get absolute file name into buffer "buf" of length "len" bytes,
341 * turning all '/'s into '\\'s and getting the correct case of each component
342 * of the file name. Append a (back)slash to a directory name.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343 * When 'shellslash' set do it the other way around.
344 * Return OK or FAIL.
345 */
346 int
347mch_FullName(
348 char_u *fname,
349 char_u *buf,
350 int len,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100351 int force UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352{
353 int nResult = FAIL;
354
355#ifdef __BORLANDC__
356 if (*fname == NUL) /* Borland behaves badly here - make it consistent */
357 nResult = mch_dirname(buf, len);
358 else
359#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000361#ifdef FEAT_MBYTE
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200362 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000363 {
364 WCHAR *wname;
365 WCHAR wbuf[MAX_PATH];
366 char_u *cname = NULL;
367
368 /* Use the wide function:
369 * - convert the fname from 'encoding' to UCS2.
370 * - invoke _wfullpath()
371 * - convert the result from UCS2 to 'encoding'.
372 */
Bram Moolenaar36f692d2008-11-20 16:10:17 +0000373 wname = enc_to_utf16(fname, NULL);
Bram Moolenaar374bf022014-11-05 19:33:24 +0100374 if (wname != NULL && _wfullpath(wbuf, wname, MAX_PATH) != NULL)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000375 {
Bram Moolenaar36f692d2008-11-20 16:10:17 +0000376 cname = utf16_to_enc((short_u *)wbuf, NULL);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000377 if (cname != NULL)
378 {
Bram Moolenaarfe3ca8d2005-07-18 21:43:02 +0000379 vim_strncpy(buf, cname, len - 1);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000380 nResult = OK;
381 }
382 }
383 vim_free(wname);
384 vim_free(cname);
385 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000386 if (nResult == FAIL) /* fall back to non-wide function */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000388 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100389 if (_fullpath((char *)buf, (const char *)fname, len - 1) == NULL)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000390 {
Bram Moolenaarfe3ca8d2005-07-18 21:43:02 +0000391 /* failed, use relative path name */
392 vim_strncpy(buf, fname, len - 1);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000393 }
394 else
395 nResult = OK;
396 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398
399#ifdef USE_FNAME_CASE
400 fname_case(buf, len);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000401#else
402 slash_adjust(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403#endif
404
405 return nResult;
406}
407
408
409/*
410 * Return TRUE if "fname" does not depend on the current directory.
411 */
412 int
413mch_isFullName(char_u *fname)
414{
Bram Moolenaard2a203b2013-08-30 16:51:18 +0200415#ifdef FEAT_MBYTE
416 /* WinNT and later can use _MAX_PATH wide characters for a pathname, which
417 * means that the maximum pathname is _MAX_PATH * 3 bytes when 'enc' is
418 * UTF-8. */
419 char szName[_MAX_PATH * 3 + 1];
420#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421 char szName[_MAX_PATH + 1];
Bram Moolenaard2a203b2013-08-30 16:51:18 +0200422#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423
424 /* A name like "d:/foo" and "//server/share" is absolute */
425 if ((fname[0] && fname[1] == ':' && (fname[2] == '/' || fname[2] == '\\'))
426 || (fname[0] == fname[1] && (fname[0] == '/' || fname[0] == '\\')))
427 return TRUE;
428
429 /* A name that can't be made absolute probably isn't absolute. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100430 if (mch_FullName(fname, (char_u *)szName, sizeof(szName) - 1, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431 return FALSE;
432
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100433 return pathcmp((const char *)fname, (const char *)szName, -1) == 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434}
435
436/*
437 * Replace all slashes by backslashes.
438 * This used to be the other way around, but MS-DOS sometimes has problems
439 * with slashes (e.g. in a command name). We can't have mixed slashes and
440 * backslashes, because comparing file names will not work correctly. The
441 * commands that use a file name should try to avoid the need to type a
442 * backslash twice.
443 * When 'shellslash' set do it the other way around.
Bram Moolenaarb4f6a462015-10-13 19:43:17 +0200444 * When the path looks like a URL leave it unmodified.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445 */
446 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100447slash_adjust(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448{
Bram Moolenaarb4f6a462015-10-13 19:43:17 +0200449 if (path_with_url(p))
450 return;
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +0000451 while (*p)
452 {
453 if (*p == psepcN)
454 *p = psepc;
455 mb_ptr_adv(p);
456 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000457}
458
Bram Moolenaar8767f522016-07-01 17:17:39 +0200459/* Use 64-bit stat functions if available. */
460#ifdef HAVE_STAT64
461# undef stat
462# undef _stat
463# undef _wstat
464# undef _fstat
465# define stat _stat64
466# define _stat _stat64
467# define _wstat _wstat64
468# define _fstat _fstat64
469#endif
470
Bram Moolenaar9d1685d2014-01-14 12:18:45 +0100471#if (defined(_MSC_VER) && (_MSC_VER >= 1300)) || defined(__MINGW32__)
Bram Moolenaar8962fda2013-09-29 19:05:21 +0200472# define OPEN_OH_ARGTYPE intptr_t
473#else
474# define OPEN_OH_ARGTYPE long
475#endif
476
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200477 static int
Bram Moolenaar8767f522016-07-01 17:17:39 +0200478stat_symlink_aware(const char *name, stat_T *stp)
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200479{
Bram Moolenaarfce7b3d2016-01-19 19:00:32 +0100480#if (defined(_MSC_VER) && (_MSC_VER < 1900)) || defined(__MINGW32__)
481 /* Work around for VC12 or earlier (and MinGW). stat() can't handle
482 * symlinks properly.
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200483 * VC9 or earlier: stat() doesn't support a symlink at all. It retrieves
484 * status of a symlink itself.
485 * VC10: stat() supports a symlink to a normal file, but it doesn't support
Bram Moolenaarfce7b3d2016-01-19 19:00:32 +0100486 * a symlink to a directory (always returns an error).
487 * VC11 and VC12: stat() doesn't return an error for a symlink to a
488 * directory, but it doesn't set S_IFDIR flag.
489 * MinGW: Same as VC9. */
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200490 WIN32_FIND_DATA findData;
491 HANDLE hFind, h;
492 DWORD attr = 0;
493 BOOL is_symlink = FALSE;
494
495 hFind = FindFirstFile(name, &findData);
496 if (hFind != INVALID_HANDLE_VALUE)
497 {
498 attr = findData.dwFileAttributes;
499 if ((attr & FILE_ATTRIBUTE_REPARSE_POINT)
500 && (findData.dwReserved0 == IO_REPARSE_TAG_SYMLINK))
501 is_symlink = TRUE;
502 FindClose(hFind);
503 }
504 if (is_symlink)
505 {
506 h = CreateFile(name, FILE_READ_ATTRIBUTES,
507 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
508 OPEN_EXISTING,
509 (attr & FILE_ATTRIBUTE_DIRECTORY)
510 ? FILE_FLAG_BACKUP_SEMANTICS : 0,
511 NULL);
512 if (h != INVALID_HANDLE_VALUE)
513 {
514 int fd, n;
515
Bram Moolenaar8962fda2013-09-29 19:05:21 +0200516 fd = _open_osfhandle((OPEN_OH_ARGTYPE)h, _O_RDONLY);
Bram Moolenaar8767f522016-07-01 17:17:39 +0200517 n = _fstat(fd, (struct _stat *)stp);
Bram Moolenaarfce7b3d2016-01-19 19:00:32 +0100518 if ((n == 0) && (attr & FILE_ATTRIBUTE_DIRECTORY))
519 stp->st_mode = (stp->st_mode & ~S_IFREG) | S_IFDIR;
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200520 _close(fd);
521 return n;
522 }
523 }
524#endif
525 return stat(name, stp);
526}
527
528#ifdef FEAT_MBYTE
529 static int
Bram Moolenaar8767f522016-07-01 17:17:39 +0200530wstat_symlink_aware(const WCHAR *name, stat_T *stp)
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200531{
Bram Moolenaarfce7b3d2016-01-19 19:00:32 +0100532# if (defined(_MSC_VER) && (_MSC_VER < 1900)) || defined(__MINGW32__)
533 /* Work around for VC12 or earlier (and MinGW). _wstat() can't handle
534 * symlinks properly.
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200535 * VC9 or earlier: _wstat() doesn't support a symlink at all. It retrieves
536 * status of a symlink itself.
537 * VC10: _wstat() supports a symlink to a normal file, but it doesn't
Bram Moolenaarfce7b3d2016-01-19 19:00:32 +0100538 * support a symlink to a directory (always returns an error).
539 * VC11 and VC12: _wstat() doesn't return an error for a symlink to a
540 * directory, but it doesn't set S_IFDIR flag.
541 * MinGW: Same as VC9. */
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200542 int n;
543 BOOL is_symlink = FALSE;
544 HANDLE hFind, h;
545 DWORD attr = 0;
546 WIN32_FIND_DATAW findDataW;
547
548 hFind = FindFirstFileW(name, &findDataW);
549 if (hFind != INVALID_HANDLE_VALUE)
550 {
551 attr = findDataW.dwFileAttributes;
552 if ((attr & FILE_ATTRIBUTE_REPARSE_POINT)
553 && (findDataW.dwReserved0 == IO_REPARSE_TAG_SYMLINK))
554 is_symlink = TRUE;
555 FindClose(hFind);
556 }
557 if (is_symlink)
558 {
559 h = CreateFileW(name, FILE_READ_ATTRIBUTES,
560 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
561 OPEN_EXISTING,
562 (attr & FILE_ATTRIBUTE_DIRECTORY)
563 ? FILE_FLAG_BACKUP_SEMANTICS : 0,
564 NULL);
565 if (h != INVALID_HANDLE_VALUE)
566 {
567 int fd;
568
Bram Moolenaar8962fda2013-09-29 19:05:21 +0200569 fd = _open_osfhandle((OPEN_OH_ARGTYPE)h, _O_RDONLY);
Bram Moolenaar8767f522016-07-01 17:17:39 +0200570 n = _fstat(fd, (struct _stat *)stp);
Bram Moolenaarfce7b3d2016-01-19 19:00:32 +0100571 if ((n == 0) && (attr & FILE_ATTRIBUTE_DIRECTORY))
572 stp->st_mode = (stp->st_mode & ~S_IFREG) | S_IFDIR;
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200573 _close(fd);
574 return n;
575 }
576 }
577# endif
Bram Moolenaar8767f522016-07-01 17:17:39 +0200578 return _wstat(name, (struct _stat *)stp);
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200579}
580#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581
582/*
583 * stat() can't handle a trailing '/' or '\', remove it first.
584 */
585 int
Bram Moolenaar8767f522016-07-01 17:17:39 +0200586vim_stat(const char *name, stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587{
Bram Moolenaard2a203b2013-08-30 16:51:18 +0200588#ifdef FEAT_MBYTE
589 /* WinNT and later can use _MAX_PATH wide characters for a pathname, which
590 * means that the maximum pathname is _MAX_PATH * 3 bytes when 'enc' is
591 * UTF-8. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100592 char_u buf[_MAX_PATH * 3 + 1];
Bram Moolenaard2a203b2013-08-30 16:51:18 +0200593#else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100594 char_u buf[_MAX_PATH + 1];
Bram Moolenaard2a203b2013-08-30 16:51:18 +0200595#endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100596 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597
Bram Moolenaard2a203b2013-08-30 16:51:18 +0200598 vim_strncpy((char_u *)buf, (char_u *)name, sizeof(buf) - 1);
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100599 p = buf + STRLEN(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600 if (p > buf)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000601 mb_ptr_back(buf, p);
Bram Moolenaarb1cb35f2014-01-10 13:05:20 +0100602
603 /* Remove trailing '\\' except root path. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604 if (p > buf && (*p == '\\' || *p == '/') && p[-1] != ':')
605 *p = NUL;
Bram Moolenaarb1cb35f2014-01-10 13:05:20 +0100606
607 if ((buf[0] == '\\' && buf[1] == '\\') || (buf[0] == '/' && buf[1] == '/'))
608 {
609 /* UNC root path must be followed by '\\'. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100610 p = vim_strpbrk(buf + 2, (char_u *)"\\/");
Bram Moolenaarb1cb35f2014-01-10 13:05:20 +0100611 if (p != NULL)
612 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100613 p = vim_strpbrk(p + 1, (char_u *)"\\/");
Bram Moolenaarb1cb35f2014-01-10 13:05:20 +0100614 if (p == NULL)
615 STRCAT(buf, "\\");
616 }
617 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618#ifdef FEAT_MBYTE
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200619 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 {
Bram Moolenaar36f692d2008-11-20 16:10:17 +0000621 WCHAR *wp = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622 int n;
623
624 if (wp != NULL)
625 {
Bram Moolenaar8767f522016-07-01 17:17:39 +0200626 n = wstat_symlink_aware(wp, stp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627 vim_free(wp);
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200628 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629 }
630 }
631#endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100632 return stat_symlink_aware((char *)buf, stp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633}
634
635#if defined(FEAT_GUI_MSWIN) || defined(PROTO)
636 void
Bram Moolenaar1266d672017-02-01 13:43:36 +0100637mch_settmode(int tmode UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638{
639 /* nothing to do */
640}
641
642 int
643mch_get_shellsize(void)
644{
645 /* never used */
646 return OK;
647}
648
649 void
650mch_set_shellsize(void)
651{
652 /* never used */
653}
654
655/*
656 * Rows and/or Columns has changed.
657 */
658 void
659mch_new_shellsize(void)
660{
661 /* never used */
662}
663
664#endif
665
666/*
667 * We have no job control, so fake it by starting a new shell.
668 */
669 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100670mch_suspend(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671{
672 suspend_shell();
673}
674
675#if defined(USE_MCH_ERRMSG) || defined(PROTO)
676
677#ifdef display_errors
678# undef display_errors
679#endif
680
681/*
682 * Display the saved error message(s).
683 */
684 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100685display_errors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686{
687 char *p;
688
689 if (error_ga.ga_data != NULL)
690 {
691 /* avoid putting up a message box with blanks only */
692 for (p = (char *)error_ga.ga_data; *p; ++p)
693 if (!isspace(*p))
694 {
Bram Moolenaarc17ef8e2006-03-25 21:48:58 +0000695 (void)gui_mch_dialog(
696#ifdef FEAT_GUI
697 gui.starting ? VIM_INFO :
698#endif
699 VIM_ERROR,
700#ifdef FEAT_GUI
701 gui.starting ? (char_u *)_("Message") :
702#endif
703 (char_u *)_("Error"),
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100704 (char_u *)p, (char_u *)_("&Ok"),
705 1, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 break;
707 }
708 ga_clear(&error_ga);
709 }
710}
711#endif
712
713
714/*
715 * Return TRUE if "p" contain a wildcard that can be expanded by
716 * dos_expandpath().
717 */
718 int
719mch_has_exp_wildcard(char_u *p)
720{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000721 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722 {
723 if (vim_strchr((char_u *)"?*[", *p) != NULL
724 || (*p == '~' && p[1] != NUL))
725 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726 }
727 return FALSE;
728}
729
730/*
731 * Return TRUE if "p" contain a wildcard or a "~1" kind of thing (could be a
732 * shortened file name).
733 */
734 int
735mch_has_wildcard(char_u *p)
736{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000737 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738 {
739 if (vim_strchr((char_u *)
740# ifdef VIM_BACKTICK
741 "?*$[`"
742# else
743 "?*$["
744# endif
745 , *p) != NULL
746 || (*p == '~' && p[1] != NUL))
747 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 }
749 return FALSE;
750}
751
752
753/*
754 * The normal _chdir() does not change the default drive. This one does.
755 * Returning 0 implies success; -1 implies failure.
756 */
757 int
758mch_chdir(char *path)
759{
760 if (path[0] == NUL) /* just checking... */
761 return -1;
762
Bram Moolenaara2974d72009-07-14 16:38:36 +0000763 if (p_verbose >= 5)
764 {
765 verbose_enter();
766 smsg((char_u *)"chdir(%s)", path);
767 verbose_leave();
768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 if (isalpha(path[0]) && path[1] == ':') /* has a drive name */
770 {
771 /* If we can change to the drive, skip that part of the path. If we
772 * can't then the current directory may be invalid, try using chdir()
773 * with the whole path. */
774 if (_chdrive(TOLOWER_ASC(path[0]) - 'a' + 1) == 0)
775 path += 2;
776 }
777
778 if (*path == NUL) /* drive name only */
779 return 0;
780
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000781#ifdef FEAT_MBYTE
782 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
783 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100784 WCHAR *p = enc_to_utf16((char_u *)path, NULL);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000785 int n;
786
787 if (p != NULL)
788 {
789 n = _wchdir(p);
790 vim_free(p);
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200791 return n;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000792 }
793 }
794#endif
795
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 return chdir(path); /* let the normal chdir() do the rest */
797}
798
799
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800#ifdef FEAT_GUI_MSWIN
801/*
802 * return non-zero if a character is available
803 */
804 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100805mch_char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806{
807 /* never used */
808 return TRUE;
809}
810#endif
811
812
813/*
814 * set screen mode, always fails.
815 */
816 int
Bram Moolenaar1266d672017-02-01 13:43:36 +0100817mch_screenmode(char_u *arg UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818{
819 EMSG(_(e_screenmode));
820 return FAIL;
821}
822
823
824#if defined(FEAT_LIBCALL) || defined(PROTO)
825/*
826 * Call a DLL routine which takes either a string or int param
827 * and returns an allocated string.
828 * Return OK if it worked, FAIL if not.
829 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830typedef LPTSTR (*MYSTRPROCSTR)(LPTSTR);
831typedef LPTSTR (*MYINTPROCSTR)(int);
832typedef int (*MYSTRPROCINT)(LPTSTR);
833typedef int (*MYINTPROCINT)(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835/*
836 * Check if a pointer points to a valid NUL terminated string.
837 * Return the length of the string, including terminating NUL.
838 * Returns 0 for an invalid pointer, 1 for an empty string.
839 */
840 static size_t
841check_str_len(char_u *str)
842{
843 SYSTEM_INFO si;
844 MEMORY_BASIC_INFORMATION mbi;
845 size_t length = 0;
846 size_t i;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100847 const char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848
849 /* get page size */
850 GetSystemInfo(&si);
851
852 /* get memory information */
853 if (VirtualQuery(str, &mbi, sizeof(mbi)))
854 {
855 /* pre cast these (typing savers) */
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000856 long_u dwStr = (long_u)str;
857 long_u dwBaseAddress = (long_u)mbi.BaseAddress;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858
859 /* get start address of page that str is on */
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000860 long_u strPage = dwStr - (dwStr - dwBaseAddress) % si.dwPageSize;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861
862 /* get length from str to end of page */
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000863 long_u pageLength = si.dwPageSize - (dwStr - strPage);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864
Bram Moolenaar442b4222010-05-24 21:34:22 +0200865 for (p = str; !IsBadReadPtr(p, (UINT)pageLength);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866 p += pageLength, pageLength = si.dwPageSize)
867 for (i = 0; i < pageLength; ++i, ++length)
868 if (p[i] == NUL)
869 return length + 1;
870 }
871
872 return 0;
873}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200875/*
876 * Passed to do_in_runtimepath() to load a vim.ico file.
877 */
878 static void
879mch_icon_load_cb(char_u *fname, void *cookie)
880{
881 HANDLE *h = (HANDLE *)cookie;
882
883 *h = LoadImage(NULL,
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100884 (LPSTR)fname,
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200885 IMAGE_ICON,
886 64,
887 64,
888 LR_LOADFROMFILE | LR_LOADMAP3DCOLORS);
889}
890
891/*
892 * Try loading an icon file from 'runtimepath'.
893 */
894 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100895mch_icon_load(HANDLE *iconp)
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200896{
897 return do_in_runtimepath((char_u *)"bitmaps/vim.ico",
Bram Moolenaar7f8989d2016-03-12 22:11:39 +0100898 0, mch_icon_load_cb, iconp);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200899}
900
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 int
902mch_libcall(
903 char_u *libname,
904 char_u *funcname,
905 char_u *argstring, /* NULL when using a argint */
906 int argint,
907 char_u **string_result,/* NULL when using number_result */
908 int *number_result)
909{
910 HINSTANCE hinstLib;
911 MYSTRPROCSTR ProcAdd;
912 MYINTPROCSTR ProcAddI;
913 char_u *retval_str = NULL;
914 int retval_int = 0;
915 size_t len;
916
917 BOOL fRunTimeLinkSuccess = FALSE;
918
919 // Get a handle to the DLL module.
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100920 hinstLib = vimLoadLib((char *)libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921
922 // If the handle is valid, try to get the function address.
923 if (hinstLib != NULL)
924 {
925#ifdef HAVE_TRY_EXCEPT
926 __try
927 {
928#endif
929 if (argstring != NULL)
930 {
931 /* Call with string argument */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100932 ProcAdd = (MYSTRPROCSTR)GetProcAddress(hinstLib, (LPCSTR)funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 if ((fRunTimeLinkSuccess = (ProcAdd != NULL)) != 0)
934 {
935 if (string_result == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100936 retval_int = ((MYSTRPROCINT)ProcAdd)((LPSTR)argstring);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100938 retval_str = (char_u *)(ProcAdd)((LPSTR)argstring);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 }
940 }
941 else
942 {
943 /* Call with number argument */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100944 ProcAddI = (MYINTPROCSTR) GetProcAddress(hinstLib, (LPCSTR)funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 if ((fRunTimeLinkSuccess = (ProcAddI != NULL)) != 0)
946 {
947 if (string_result == NULL)
948 retval_int = ((MYINTPROCINT)ProcAddI)(argint);
949 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100950 retval_str = (char_u *)(ProcAddI)(argint);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 }
952 }
953
954 // Save the string before we free the library.
955 // Assume that a "1" result is an illegal pointer.
956 if (string_result == NULL)
957 *number_result = retval_int;
958 else if (retval_str != NULL
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100959 && (len = check_str_len(retval_str)) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 {
961 *string_result = lalloc((long_u)len, TRUE);
962 if (*string_result != NULL)
963 mch_memmove(*string_result, retval_str, len);
964 }
965
966#ifdef HAVE_TRY_EXCEPT
967 }
968 __except(EXCEPTION_EXECUTE_HANDLER)
969 {
970 if (GetExceptionCode() == EXCEPTION_STACK_OVERFLOW)
971 RESETSTKOFLW();
972 fRunTimeLinkSuccess = 0;
973 }
974#endif
975
976 // Free the DLL module.
977 (void)FreeLibrary(hinstLib);
978 }
979
980 if (!fRunTimeLinkSuccess)
981 {
982 EMSG2(_(e_libcall), funcname);
983 return FAIL;
984 }
985
986 return OK;
987}
988#endif
989
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990/*
991 * Debugging helper: expose the MCH_WRITE_DUMP stuff to other modules
992 */
993 void
Bram Moolenaar1266d672017-02-01 13:43:36 +0100994DumpPutS(const char *psz UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995{
996# ifdef MCH_WRITE_DUMP
997 if (fdDump)
998 {
999 fputs(psz, fdDump);
1000 if (psz[strlen(psz) - 1] != '\n')
1001 fputc('\n', fdDump);
1002 fflush(fdDump);
1003 }
1004# endif
1005}
1006
1007#ifdef _DEBUG
1008
1009void __cdecl
1010Trace(
1011 char *pszFormat,
1012 ...)
1013{
1014 CHAR szBuff[2048];
1015 va_list args;
1016
1017 va_start(args, pszFormat);
1018 vsprintf(szBuff, pszFormat, args);
1019 va_end(args);
1020
1021 OutputDebugString(szBuff);
1022}
1023
1024#endif //_DEBUG
1025
Bram Moolenaar843ee412004-06-30 16:16:41 +00001026#if !defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001027# ifdef FEAT_TITLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028extern HWND g_hWnd; /* This is in os_win32.c. */
1029# endif
1030
1031/*
1032 * Showing the printer dialog is tricky since we have no GUI
1033 * window to parent it. The following routines are needed to
1034 * get the window parenting and Z-order to work properly.
1035 */
1036 static void
1037GetConsoleHwnd(void)
1038{
1039# define MY_BUFSIZE 1024 // Buffer size for console window titles.
1040
1041 char pszNewWindowTitle[MY_BUFSIZE]; // Contains fabricated WindowTitle.
1042 char pszOldWindowTitle[MY_BUFSIZE]; // Contains original WindowTitle.
1043
1044 /* Skip if it's already set. */
1045 if (s_hwnd != 0)
1046 return;
1047
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001048# ifdef FEAT_TITLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 /* Window handle may have been found by init code (Windows NT only) */
1050 if (g_hWnd != 0)
1051 {
1052 s_hwnd = g_hWnd;
1053 return;
1054 }
1055# endif
1056
1057 GetConsoleTitle(pszOldWindowTitle, MY_BUFSIZE);
1058
1059 wsprintf(pszNewWindowTitle, "%s/%d/%d",
1060 pszOldWindowTitle,
1061 GetTickCount(),
1062 GetCurrentProcessId());
1063 SetConsoleTitle(pszNewWindowTitle);
1064 Sleep(40);
1065 s_hwnd = FindWindow(NULL, pszNewWindowTitle);
1066
1067 SetConsoleTitle(pszOldWindowTitle);
1068}
Bram Moolenaar843ee412004-06-30 16:16:41 +00001069
1070/*
1071 * Console implementation of ":winpos".
1072 */
1073 int
1074mch_get_winpos(int *x, int *y)
1075{
1076 RECT rect;
1077
1078 GetConsoleHwnd();
1079 GetWindowRect(s_hwnd, &rect);
1080 *x = rect.left;
1081 *y = rect.top;
1082 return OK;
1083}
1084
1085/*
1086 * Console implementation of ":winpos x y".
1087 */
1088 void
1089mch_set_winpos(int x, int y)
1090{
1091 GetConsoleHwnd();
1092 SetWindowPos(s_hwnd, NULL, x, y, 0, 0,
1093 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
1094}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095#endif
1096
1097#if (defined(FEAT_PRINTER) && !defined(FEAT_POSTSCRIPT)) || defined(PROTO)
1098
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099/*=================================================================
1100 * Win32 printer stuff
1101 */
1102
1103static HFONT prt_font_handles[2][2][2];
1104static PRINTDLG prt_dlg;
1105static const int boldface[2] = {FW_REGULAR, FW_BOLD};
1106static TEXTMETRIC prt_tm;
1107static int prt_line_height;
1108static int prt_number_width;
1109static int prt_left_margin;
1110static int prt_right_margin;
1111static int prt_top_margin;
1112static char_u szAppName[] = TEXT("VIM");
1113static HWND hDlgPrint;
1114static int *bUserAbort = NULL;
1115static char_u *prt_name = NULL;
1116
1117/* Defines which are also in vim.rc. */
1118#define IDC_BOX1 400
1119#define IDC_PRINTTEXT1 401
1120#define IDC_PRINTTEXT2 402
1121#define IDC_PROGRESS 403
1122
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001123#if !defined(FEAT_MBYTE)
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001124# define vimSetDlgItemText(h, i, s) SetDlgItemText(h, i, s)
1125#else
1126 static BOOL
1127vimSetDlgItemText(HWND hDlg, int nIDDlgItem, char_u *s)
1128{
1129 WCHAR *wp = NULL;
1130 BOOL ret;
1131
1132 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
1133 {
1134 wp = enc_to_utf16(s, NULL);
1135 }
1136 if (wp != NULL)
1137 {
1138 ret = SetDlgItemTextW(hDlg, nIDDlgItem, wp);
1139 vim_free(wp);
1140 return ret;
1141 }
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001142 return SetDlgItemText(hDlg, nIDDlgItem, (LPCSTR)s);
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001143}
1144#endif
1145
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146/*
1147 * Convert BGR to RGB for Windows GDI calls
1148 */
1149 static COLORREF
1150swap_me(COLORREF colorref)
1151{
1152 int temp;
1153 char *ptr = (char *)&colorref;
1154
1155 temp = *(ptr);
1156 *(ptr ) = *(ptr + 2);
1157 *(ptr + 2) = temp;
1158 return colorref;
1159}
1160
Bram Moolenaared39e1d2008-08-09 17:55:22 +00001161/* Attempt to make this work for old and new compilers */
Bram Moolenaar9f733d12011-09-21 20:09:42 +02001162#if !defined(_WIN64) && (!defined(_MSC_VER) || _MSC_VER < 1300)
Bram Moolenaared39e1d2008-08-09 17:55:22 +00001163# define PDP_RETVAL BOOL
1164#else
1165# define PDP_RETVAL INT_PTR
1166#endif
1167
Bram Moolenaared39e1d2008-08-09 17:55:22 +00001168 static PDP_RETVAL CALLBACK
Bram Moolenaar1266d672017-02-01 13:43:36 +01001169PrintDlgProc(
1170 HWND hDlg,
1171 UINT message,
1172 WPARAM wParam UNUSED,
1173 LPARAM lParam UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174{
1175#ifdef FEAT_GETTEXT
1176 NONCLIENTMETRICS nm;
1177 static HFONT hfont;
1178#endif
1179
1180 switch (message)
1181 {
1182 case WM_INITDIALOG:
1183#ifdef FEAT_GETTEXT
1184 nm.cbSize = sizeof(NONCLIENTMETRICS);
1185 if (SystemParametersInfo(
1186 SPI_GETNONCLIENTMETRICS,
1187 sizeof(NONCLIENTMETRICS),
1188 &nm,
1189 0))
1190 {
1191 char buff[MAX_PATH];
1192 int i;
1193
1194 /* Translate the dialog texts */
1195 hfont = CreateFontIndirect(&nm.lfMessageFont);
1196 for (i = IDC_PRINTTEXT1; i <= IDC_PROGRESS; i++)
1197 {
1198 SendDlgItemMessage(hDlg, i, WM_SETFONT, (WPARAM)hfont, 1);
1199 if (GetDlgItemText(hDlg,i, buff, sizeof(buff)))
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001200 vimSetDlgItemText(hDlg,i, (char_u *)_(buff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 }
1202 SendDlgItemMessage(hDlg, IDCANCEL,
1203 WM_SETFONT, (WPARAM)hfont, 1);
1204 if (GetDlgItemText(hDlg,IDCANCEL, buff, sizeof(buff)))
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001205 vimSetDlgItemText(hDlg,IDCANCEL, (char_u *)_(buff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 }
1207#endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001208 SetWindowText(hDlg, (LPCSTR)szAppName);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 if (prt_name != NULL)
1210 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001211 vimSetDlgItemText(hDlg, IDC_PRINTTEXT2, (char_u *)prt_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 vim_free(prt_name);
1213 prt_name = NULL;
1214 }
1215 EnableMenuItem(GetSystemMenu(hDlg, FALSE), SC_CLOSE, MF_GRAYED);
1216#ifndef FEAT_GUI
1217 BringWindowToTop(s_hwnd);
1218#endif
1219 return TRUE;
1220
1221 case WM_COMMAND:
1222 *bUserAbort = TRUE;
1223 EnableWindow(GetParent(hDlg), TRUE);
1224 DestroyWindow(hDlg);
1225 hDlgPrint = NULL;
1226#ifdef FEAT_GETTEXT
1227 DeleteObject(hfont);
1228#endif
1229 return TRUE;
1230 }
1231 return FALSE;
1232}
1233
1234 static BOOL CALLBACK
Bram Moolenaar1266d672017-02-01 13:43:36 +01001235AbortProc(HDC hdcPrn UNUSED, int iCode UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236{
1237 MSG msg;
1238
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02001239 while (!*bUserAbort && pPeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 {
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02001241 if (!hDlgPrint || !pIsDialogMessage(hDlgPrint, &msg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 {
1243 TranslateMessage(&msg);
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02001244 pDispatchMessage(&msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 }
1246 }
1247 return !*bUserAbort;
1248}
1249
1250#ifndef FEAT_GUI
1251
Bram Moolenaar26fdd7d2011-11-30 13:42:44 +01001252 static UINT_PTR CALLBACK
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253PrintHookProc(
1254 HWND hDlg, // handle to dialog box
1255 UINT uiMsg, // message identifier
1256 WPARAM wParam, // message parameter
1257 LPARAM lParam // message parameter
1258 )
1259{
1260 HWND hwndOwner;
1261 RECT rc, rcDlg, rcOwner;
1262 PRINTDLG *pPD;
1263
1264 if (uiMsg == WM_INITDIALOG)
1265 {
1266 // Get the owner window and dialog box rectangles.
1267 if ((hwndOwner = GetParent(hDlg)) == NULL)
1268 hwndOwner = GetDesktopWindow();
1269
1270 GetWindowRect(hwndOwner, &rcOwner);
1271 GetWindowRect(hDlg, &rcDlg);
1272 CopyRect(&rc, &rcOwner);
1273
1274 // Offset the owner and dialog box rectangles so that
1275 // right and bottom values represent the width and
1276 // height, and then offset the owner again to discard
1277 // space taken up by the dialog box.
1278
1279 OffsetRect(&rcDlg, -rcDlg.left, -rcDlg.top);
1280 OffsetRect(&rc, -rc.left, -rc.top);
1281 OffsetRect(&rc, -rcDlg.right, -rcDlg.bottom);
1282
1283 // The new position is the sum of half the remaining
1284 // space and the owner's original position.
1285
1286 SetWindowPos(hDlg,
1287 HWND_TOP,
1288 rcOwner.left + (rc.right / 2),
1289 rcOwner.top + (rc.bottom / 2),
1290 0, 0, // ignores size arguments
1291 SWP_NOSIZE);
1292
1293 /* tackle the printdlg copiesctrl problem */
1294 pPD = (PRINTDLG *)lParam;
1295 pPD->nCopies = (WORD)pPD->lCustData;
1296 SetDlgItemInt( hDlg, edt3, pPD->nCopies, FALSE );
1297 /* Bring the window to top */
1298 BringWindowToTop(GetParent(hDlg));
1299 SetForegroundWindow(hDlg);
1300 }
1301
1302 return FALSE;
1303}
1304#endif
1305
1306 void
1307mch_print_cleanup(void)
1308{
1309 int pifItalic;
1310 int pifBold;
1311 int pifUnderline;
1312
1313 for (pifBold = 0; pifBold <= 1; pifBold++)
1314 for (pifItalic = 0; pifItalic <= 1; pifItalic++)
1315 for (pifUnderline = 0; pifUnderline <= 1; pifUnderline++)
1316 DeleteObject(prt_font_handles[pifBold][pifItalic][pifUnderline]);
1317
1318 if (prt_dlg.hDC != NULL)
1319 DeleteDC(prt_dlg.hDC);
1320 if (!*bUserAbort)
1321 SendMessage(hDlgPrint, WM_COMMAND, 0, 0);
1322}
1323
1324 static int
1325to_device_units(int idx, int dpi, int physsize, int offset, int def_number)
1326{
1327 int ret = 0;
1328 int u;
1329 int nr;
1330
1331 u = prt_get_unit(idx);
1332 if (u == PRT_UNIT_NONE)
1333 {
1334 u = PRT_UNIT_PERC;
1335 nr = def_number;
1336 }
1337 else
1338 nr = printer_opts[idx].number;
1339
1340 switch (u)
1341 {
1342 case PRT_UNIT_PERC:
1343 ret = (physsize * nr) / 100;
1344 break;
1345 case PRT_UNIT_INCH:
1346 ret = (nr * dpi);
1347 break;
1348 case PRT_UNIT_MM:
1349 ret = (nr * 10 * dpi) / 254;
1350 break;
1351 case PRT_UNIT_POINT:
1352 ret = (nr * 10 * dpi) / 720;
1353 break;
1354 }
1355
1356 if (ret < offset)
1357 return 0;
1358 else
1359 return ret - offset;
1360}
1361
1362 static int
1363prt_get_cpl(void)
1364{
1365 int hr;
1366 int phyw;
1367 int dvoff;
1368 int rev_offset;
1369 int dpi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370
1371 GetTextMetrics(prt_dlg.hDC, &prt_tm);
1372 prt_line_height = prt_tm.tmHeight + prt_tm.tmExternalLeading;
1373
1374 hr = GetDeviceCaps(prt_dlg.hDC, HORZRES);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 phyw = GetDeviceCaps(prt_dlg.hDC, PHYSICALWIDTH);
1376 dvoff = GetDeviceCaps(prt_dlg.hDC, PHYSICALOFFSETX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 dpi = GetDeviceCaps(prt_dlg.hDC, LOGPIXELSX);
1378
1379 rev_offset = phyw - (dvoff + hr);
1380
1381 prt_left_margin = to_device_units(OPT_PRINT_LEFT, dpi, phyw, dvoff, 10);
1382 if (prt_use_number())
1383 {
1384 prt_number_width = PRINT_NUMBER_WIDTH * prt_tm.tmAveCharWidth;
1385 prt_left_margin += prt_number_width;
1386 }
1387 else
1388 prt_number_width = 0;
1389
1390 prt_right_margin = hr - to_device_units(OPT_PRINT_RIGHT, dpi, phyw,
1391 rev_offset, 5);
1392
1393 return (prt_right_margin - prt_left_margin) / prt_tm.tmAveCharWidth;
1394}
1395
1396 static int
1397prt_get_lpp(void)
1398{
1399 int vr;
1400 int phyw;
1401 int dvoff;
1402 int rev_offset;
1403 int bottom_margin;
1404 int dpi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405
1406 vr = GetDeviceCaps(prt_dlg.hDC, VERTRES);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 phyw = GetDeviceCaps(prt_dlg.hDC, PHYSICALHEIGHT);
1408 dvoff = GetDeviceCaps(prt_dlg.hDC, PHYSICALOFFSETY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 dpi = GetDeviceCaps(prt_dlg.hDC, LOGPIXELSY);
1410
1411 rev_offset = phyw - (dvoff + vr);
1412
1413 prt_top_margin = to_device_units(OPT_PRINT_TOP, dpi, phyw, dvoff, 5);
1414
1415 /* adjust top margin if there is a header */
1416 prt_top_margin += prt_line_height * prt_header_height();
1417
1418 bottom_margin = vr - to_device_units(OPT_PRINT_BOT, dpi, phyw,
1419 rev_offset, 5);
1420
1421 return (bottom_margin - prt_top_margin) / prt_line_height;
1422}
1423
1424 int
1425mch_print_init(prt_settings_T *psettings, char_u *jobname, int forceit)
1426{
1427 static HGLOBAL stored_dm = NULL;
1428 static HGLOBAL stored_devn = NULL;
1429 static int stored_nCopies = 1;
1430 static int stored_nFlags = 0;
1431
1432 LOGFONT fLogFont;
1433 int pifItalic;
1434 int pifBold;
1435 int pifUnderline;
1436
1437 DEVMODE *mem;
1438 DEVNAMES *devname;
1439 int i;
1440
1441 bUserAbort = &(psettings->user_abort);
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02001442 vim_memset(&prt_dlg, 0, sizeof(PRINTDLG));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 prt_dlg.lStructSize = sizeof(PRINTDLG);
1444#ifndef FEAT_GUI
1445 GetConsoleHwnd(); /* get value of s_hwnd */
1446#endif
1447 prt_dlg.hwndOwner = s_hwnd;
1448 prt_dlg.Flags = PD_NOPAGENUMS | PD_NOSELECTION | PD_RETURNDC;
1449 if (!forceit)
1450 {
1451 prt_dlg.hDevMode = stored_dm;
1452 prt_dlg.hDevNames = stored_devn;
1453 prt_dlg.lCustData = stored_nCopies; // work around bug in print dialog
1454#ifndef FEAT_GUI
1455 /*
1456 * Use hook to prevent console window being sent to back
1457 */
1458 prt_dlg.lpfnPrintHook = PrintHookProc;
1459 prt_dlg.Flags |= PD_ENABLEPRINTHOOK;
1460#endif
1461 prt_dlg.Flags |= stored_nFlags;
1462 }
1463
1464 /*
1465 * If bang present, return default printer setup with no dialog
1466 * never show dialog if we are running over telnet
1467 */
1468 if (forceit
1469#ifndef FEAT_GUI
1470 || !term_console
1471#endif
1472 )
1473 {
1474 prt_dlg.Flags |= PD_RETURNDEFAULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 /*
1476 * MSDN suggests setting the first parameter to WINSPOOL for
1477 * NT, but NULL appears to work just as well.
1478 */
1479 if (*p_pdev != NUL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001480 prt_dlg.hDC = CreateDC(NULL, (LPCSTR)p_pdev, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 {
1483 prt_dlg.Flags |= PD_RETURNDEFAULT;
1484 if (PrintDlg(&prt_dlg) == 0)
1485 goto init_fail_dlg;
1486 }
1487 }
1488 else if (PrintDlg(&prt_dlg) == 0)
1489 goto init_fail_dlg;
1490 else
1491 {
1492 /*
1493 * keep the previous driver context
1494 */
1495 stored_dm = prt_dlg.hDevMode;
1496 stored_devn = prt_dlg.hDevNames;
1497 stored_nFlags = prt_dlg.Flags;
1498 stored_nCopies = prt_dlg.nCopies;
1499 }
1500
1501 if (prt_dlg.hDC == NULL)
1502 {
1503 EMSG(_("E237: Printer selection failed"));
1504 mch_print_cleanup();
1505 return FALSE;
1506 }
1507
1508 /* Not all printer drivers report the support of color (or grey) in the
1509 * same way. Let's set has_color if there appears to be some way to print
1510 * more than B&W. */
1511 i = GetDeviceCaps(prt_dlg.hDC, NUMCOLORS);
1512 psettings->has_color = (GetDeviceCaps(prt_dlg.hDC, BITSPIXEL) > 1
1513 || GetDeviceCaps(prt_dlg.hDC, PLANES) > 1
1514 || i > 2 || i == -1);
1515
1516 /* Ensure all font styles are baseline aligned */
1517 SetTextAlign(prt_dlg.hDC, TA_BASELINE|TA_LEFT);
1518
1519 /*
1520 * On some windows systems the nCopies parameter is not
1521 * passed back correctly. It must be retrieved from the
1522 * hDevMode struct.
1523 */
1524 mem = (DEVMODE *)GlobalLock(prt_dlg.hDevMode);
1525 if (mem != NULL)
1526 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 if (mem->dmCopies != 1)
1528 stored_nCopies = mem->dmCopies;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 if ((mem->dmFields & DM_DUPLEX) && (mem->dmDuplex & ~DMDUP_SIMPLEX))
1530 psettings->duplex = TRUE;
1531 if ((mem->dmFields & DM_COLOR) && (mem->dmColor & DMCOLOR_COLOR))
1532 psettings->has_color = TRUE;
1533 }
1534 GlobalUnlock(prt_dlg.hDevMode);
1535
1536 devname = (DEVNAMES *)GlobalLock(prt_dlg.hDevNames);
1537 if (devname != 0)
1538 {
1539 char_u *printer_name = (char_u *)devname + devname->wDeviceOffset;
1540 char_u *port_name = (char_u *)devname +devname->wOutputOffset;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001541 char_u *text = (char_u *)_("to %s on %s");
Bram Moolenaarf191d552014-10-09 17:05:56 +02001542#ifdef FEAT_MBYTE
1543 char_u *printer_name_orig = printer_name;
1544 char_u *port_name_orig = port_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545
Bram Moolenaarf191d552014-10-09 17:05:56 +02001546 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
1547 {
1548 char_u *to_free = NULL;
1549 int maxlen;
1550
Bram Moolenaar861d80a2014-10-16 20:35:36 +02001551 acp_to_enc(printer_name, (int)STRLEN(printer_name), &to_free,
1552 &maxlen);
Bram Moolenaarf191d552014-10-09 17:05:56 +02001553 if (to_free != NULL)
1554 printer_name = to_free;
Bram Moolenaar861d80a2014-10-16 20:35:36 +02001555 acp_to_enc(port_name, (int)STRLEN(port_name), &to_free, &maxlen);
Bram Moolenaarf191d552014-10-09 17:05:56 +02001556 if (to_free != NULL)
1557 port_name = to_free;
1558 }
1559#endif
Bram Moolenaare6a91fd2008-07-24 18:51:11 +00001560 prt_name = alloc((unsigned)(STRLEN(printer_name) + STRLEN(port_name)
1561 + STRLEN(text)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 if (prt_name != NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001563 wsprintf((char *)prt_name, (const char *)text,
1564 printer_name, port_name);
Bram Moolenaarf191d552014-10-09 17:05:56 +02001565#ifdef FEAT_MBYTE
1566 if (printer_name != printer_name_orig)
1567 vim_free(printer_name);
1568 if (port_name != port_name_orig)
1569 vim_free(port_name);
1570#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 }
1572 GlobalUnlock(prt_dlg.hDevNames);
1573
1574 /*
1575 * Initialise the font according to 'printfont'
1576 */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02001577 vim_memset(&fLogFont, 0, sizeof(fLogFont));
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001578 if (get_logfont(&fLogFont, p_pfn, prt_dlg.hDC, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 {
1580 EMSG2(_("E613: Unknown printer font: %s"), p_pfn);
1581 mch_print_cleanup();
1582 return FALSE;
1583 }
1584
1585 for (pifBold = 0; pifBold <= 1; pifBold++)
1586 for (pifItalic = 0; pifItalic <= 1; pifItalic++)
1587 for (pifUnderline = 0; pifUnderline <= 1; pifUnderline++)
1588 {
1589 fLogFont.lfWeight = boldface[pifBold];
1590 fLogFont.lfItalic = pifItalic;
1591 fLogFont.lfUnderline = pifUnderline;
1592 prt_font_handles[pifBold][pifItalic][pifUnderline]
1593 = CreateFontIndirect(&fLogFont);
1594 }
1595
1596 SetBkMode(prt_dlg.hDC, OPAQUE);
1597 SelectObject(prt_dlg.hDC, prt_font_handles[0][0][0]);
1598
1599 /*
1600 * Fill in the settings struct
1601 */
1602 psettings->chars_per_line = prt_get_cpl();
1603 psettings->lines_per_page = prt_get_lpp();
Bram Moolenaar7ddc6422014-09-27 11:18:19 +02001604 if (prt_dlg.Flags & PD_USEDEVMODECOPIESANDCOLLATE)
1605 {
1606 psettings->n_collated_copies = (prt_dlg.Flags & PD_COLLATE)
1607 ? prt_dlg.nCopies : 1;
1608 psettings->n_uncollated_copies = (prt_dlg.Flags & PD_COLLATE)
1609 ? 1 : prt_dlg.nCopies;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610
Bram Moolenaar7ddc6422014-09-27 11:18:19 +02001611 if (psettings->n_collated_copies == 0)
1612 psettings->n_collated_copies = 1;
1613
1614 if (psettings->n_uncollated_copies == 0)
1615 psettings->n_uncollated_copies = 1;
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01001616 }
1617 else
1618 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 psettings->n_collated_copies = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 psettings->n_uncollated_copies = 1;
Bram Moolenaar7ddc6422014-09-27 11:18:19 +02001621 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622
1623 psettings->jobname = jobname;
1624
1625 return TRUE;
1626
1627init_fail_dlg:
1628 {
1629 DWORD err = CommDlgExtendedError();
1630
1631 if (err)
1632 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 char_u *buf;
1634
1635 /* I suspect FormatMessage() doesn't work for values returned by
1636 * CommDlgExtendedError(). What does? */
1637 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
1638 FORMAT_MESSAGE_FROM_SYSTEM |
1639 FORMAT_MESSAGE_IGNORE_INSERTS,
1640 NULL, err, 0, (LPTSTR)(&buf), 0, NULL);
1641 EMSG2(_("E238: Print error: %s"),
1642 buf == NULL ? (char_u *)_("Unknown") : buf);
1643 LocalFree((LPVOID)(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 }
1645 else
1646 msg_clr_eos(); /* Maybe canceled */
1647
1648 mch_print_cleanup();
1649 return FALSE;
1650 }
1651}
1652
1653
1654 int
1655mch_print_begin(prt_settings_T *psettings)
1656{
1657 int ret;
1658 static DOCINFO di;
1659 char szBuffer[300];
1660
1661 hDlgPrint = CreateDialog(GetModuleHandle(NULL), TEXT("PrintDlgBox"),
1662 prt_dlg.hwndOwner, PrintDlgProc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 SetAbortProc(prt_dlg.hDC, AbortProc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 wsprintf(szBuffer, _("Printing '%s'"), gettail(psettings->jobname));
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001665 vimSetDlgItemText(hDlgPrint, IDC_PRINTTEXT1, (char_u *)szBuffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02001667 vim_memset(&di, 0, sizeof(DOCINFO));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 di.cbSize = sizeof(DOCINFO);
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001669 di.lpszDocName = (LPCSTR)psettings->jobname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 ret = StartDoc(prt_dlg.hDC, &di);
1671
1672#ifdef FEAT_GUI
1673 /* Give focus back to main window (when using MDI). */
1674 SetFocus(s_hwnd);
1675#endif
1676
1677 return (ret > 0);
1678}
1679
1680 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01001681mch_print_end(prt_settings_T *psettings UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682{
1683 EndDoc(prt_dlg.hDC);
1684 if (!*bUserAbort)
1685 SendMessage(hDlgPrint, WM_COMMAND, 0, 0);
1686}
1687
1688 int
1689mch_print_end_page(void)
1690{
1691 return (EndPage(prt_dlg.hDC) > 0);
1692}
1693
1694 int
1695mch_print_begin_page(char_u *msg)
1696{
1697 if (msg != NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001698 vimSetDlgItemText(hDlgPrint, IDC_PROGRESS, msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 return (StartPage(prt_dlg.hDC) > 0);
1700}
1701
1702 int
1703mch_print_blank_page(void)
1704{
1705 return (mch_print_begin_page(NULL) ? (mch_print_end_page()) : FALSE);
1706}
1707
1708static int prt_pos_x = 0;
1709static int prt_pos_y = 0;
1710
1711 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001712mch_print_start_line(int margin, int page_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713{
1714 if (margin)
1715 prt_pos_x = -prt_number_width;
1716 else
1717 prt_pos_x = 0;
1718 prt_pos_y = page_line * prt_line_height
1719 + prt_tm.tmAscent + prt_tm.tmExternalLeading;
1720}
1721
1722 int
1723mch_print_text_out(char_u *p, int len)
1724{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001725#if defined(FEAT_PROPORTIONAL_FONTS) || defined(FEAT_MBYTE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 SIZE sz;
1727#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001728#if defined(FEAT_MBYTE)
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001729 WCHAR *wp = NULL;
1730 int wlen = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001732 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
1733 {
1734 wp = enc_to_utf16(p, &wlen);
1735 }
1736 if (wp != NULL)
1737 {
1738 int ret = FALSE;
1739
1740 TextOutW(prt_dlg.hDC, prt_pos_x + prt_left_margin,
1741 prt_pos_y + prt_top_margin, wp, wlen);
1742 GetTextExtentPoint32W(prt_dlg.hDC, wp, wlen, &sz);
1743 vim_free(wp);
1744 prt_pos_x += (sz.cx - prt_tm.tmOverhang);
1745 /* This is wrong when printing spaces for a TAB. */
1746 if (p[len] != NUL)
1747 {
1748 wlen = MB_PTR2LEN(p + len);
1749 wp = enc_to_utf16(p + len, &wlen);
1750 if (wp != NULL)
1751 {
1752 GetTextExtentPoint32W(prt_dlg.hDC, wp, 1, &sz);
1753 ret = (prt_pos_x + prt_left_margin + sz.cx > prt_right_margin);
1754 vim_free(wp);
1755 }
1756 }
1757 return ret;
1758 }
1759#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 TextOut(prt_dlg.hDC, prt_pos_x + prt_left_margin,
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001761 prt_pos_y + prt_top_margin,
1762 (LPCSTR)p, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763#ifndef FEAT_PROPORTIONAL_FONTS
1764 prt_pos_x += len * prt_tm.tmAveCharWidth;
1765 return (prt_pos_x + prt_left_margin + prt_tm.tmAveCharWidth
1766 + prt_tm.tmOverhang > prt_right_margin);
1767#else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001768 GetTextExtentPoint32(prt_dlg.hDC, (LPCSTR)p, len, &sz);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 prt_pos_x += (sz.cx - prt_tm.tmOverhang);
1770 /* This is wrong when printing spaces for a TAB. */
1771 if (p[len] == NUL)
1772 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 GetTextExtentPoint32(prt_dlg.hDC, p + len, 1, &sz);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 return (prt_pos_x + prt_left_margin + sz.cx > prt_right_margin);
1775#endif
1776}
1777
1778 void
1779mch_print_set_font(int iBold, int iItalic, int iUnderline)
1780{
1781 SelectObject(prt_dlg.hDC, prt_font_handles[iBold][iItalic][iUnderline]);
1782}
1783
1784 void
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001785mch_print_set_bg(long_u bgcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786{
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001787 SetBkColor(prt_dlg.hDC, GetNearestColor(prt_dlg.hDC,
1788 swap_me((COLORREF)bgcol)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 /*
1790 * With a white background we can draw characters transparent, which is
1791 * good for italic characters that overlap to the next char cell.
1792 */
1793 if (bgcol == 0xffffffUL)
1794 SetBkMode(prt_dlg.hDC, TRANSPARENT);
1795 else
1796 SetBkMode(prt_dlg.hDC, OPAQUE);
1797}
1798
1799 void
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001800mch_print_set_fg(long_u fgcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801{
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001802 SetTextColor(prt_dlg.hDC, GetNearestColor(prt_dlg.hDC,
1803 swap_me((COLORREF)fgcol)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804}
1805
1806#endif /*FEAT_PRINTER && !FEAT_POSTSCRIPT*/
1807
Bram Moolenaar58d98232005-07-23 22:25:46 +00001808
1809
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810#if defined(FEAT_SHORTCUT) || defined(PROTO)
Bram Moolenaar82881492012-11-20 16:53:39 +01001811# ifndef PROTO
1812# include <shlobj.h>
1813# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814
1815/*
1816 * When "fname" is the name of a shortcut (*.lnk) resolve the file it points
1817 * to and return that name in allocated memory.
1818 * Otherwise NULL is returned.
1819 */
1820 char_u *
1821mch_resolve_shortcut(char_u *fname)
1822{
1823 HRESULT hr;
1824 IShellLink *psl = NULL;
1825 IPersistFile *ppf = NULL;
1826 OLECHAR wsz[MAX_PATH];
1827 WIN32_FIND_DATA ffd; // we get those free of charge
Bram Moolenaar604729e2013-08-30 16:44:19 +02001828 CHAR buf[MAX_PATH]; // could have simply reused 'wsz'...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 char_u *rfname = NULL;
1830 int len;
Bram Moolenaar604729e2013-08-30 16:44:19 +02001831# ifdef FEAT_MBYTE
1832 IShellLinkW *pslw = NULL;
1833 WIN32_FIND_DATAW ffdw; // we get those free of charge
1834# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835
1836 /* Check if the file name ends in ".lnk". Avoid calling
1837 * CoCreateInstance(), it's quite slow. */
1838 if (fname == NULL)
1839 return rfname;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001840 len = (int)STRLEN(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 if (len <= 4 || STRNICMP(fname + len - 4, ".lnk", 4) != 0)
1842 return rfname;
1843
1844 CoInitialize(NULL);
1845
Bram Moolenaar604729e2013-08-30 16:44:19 +02001846# ifdef FEAT_MBYTE
1847 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
1848 {
1849 // create a link manager object and request its interface
1850 hr = CoCreateInstance(
1851 &CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
1852 &IID_IShellLinkW, (void**)&pslw);
1853 if (hr == S_OK)
1854 {
1855 WCHAR *p = enc_to_utf16(fname, NULL);
1856
1857 if (p != NULL)
1858 {
1859 // Get a pointer to the IPersistFile interface.
1860 hr = pslw->lpVtbl->QueryInterface(
1861 pslw, &IID_IPersistFile, (void**)&ppf);
1862 if (hr != S_OK)
1863 goto shortcut_errorw;
1864
1865 // "load" the name and resolve the link
1866 hr = ppf->lpVtbl->Load(ppf, p, STGM_READ);
1867 if (hr != S_OK)
1868 goto shortcut_errorw;
1869# if 0 // This makes Vim wait a long time if the target does not exist.
1870 hr = pslw->lpVtbl->Resolve(pslw, NULL, SLR_NO_UI);
1871 if (hr != S_OK)
1872 goto shortcut_errorw;
1873# endif
1874
1875 // Get the path to the link target.
1876 ZeroMemory(wsz, MAX_PATH * sizeof(WCHAR));
1877 hr = pslw->lpVtbl->GetPath(pslw, wsz, MAX_PATH, &ffdw, 0);
1878 if (hr == S_OK && wsz[0] != NUL)
1879 rfname = utf16_to_enc(wsz, NULL);
1880
1881shortcut_errorw:
1882 vim_free(p);
Bram Moolenaarcd981f22014-02-11 17:06:00 +01001883 goto shortcut_end;
Bram Moolenaar604729e2013-08-30 16:44:19 +02001884 }
1885 }
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001886 goto shortcut_end;
Bram Moolenaar604729e2013-08-30 16:44:19 +02001887 }
1888# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 // create a link manager object and request its interface
1890 hr = CoCreateInstance(
1891 &CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
1892 &IID_IShellLink, (void**)&psl);
1893 if (hr != S_OK)
Bram Moolenaar604729e2013-08-30 16:44:19 +02001894 goto shortcut_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895
1896 // Get a pointer to the IPersistFile interface.
1897 hr = psl->lpVtbl->QueryInterface(
1898 psl, &IID_IPersistFile, (void**)&ppf);
1899 if (hr != S_OK)
Bram Moolenaar604729e2013-08-30 16:44:19 +02001900 goto shortcut_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901
1902 // full path string must be in Unicode.
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001903 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)fname, -1, wsz, MAX_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00001905 // "load" the name and resolve the link
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 hr = ppf->lpVtbl->Load(ppf, wsz, STGM_READ);
1907 if (hr != S_OK)
Bram Moolenaar604729e2013-08-30 16:44:19 +02001908 goto shortcut_end;
1909# if 0 // This makes Vim wait a long time if the target doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 hr = psl->lpVtbl->Resolve(psl, NULL, SLR_NO_UI);
1911 if (hr != S_OK)
Bram Moolenaar604729e2013-08-30 16:44:19 +02001912 goto shortcut_end;
1913# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914
1915 // Get the path to the link target.
1916 ZeroMemory(buf, MAX_PATH);
1917 hr = psl->lpVtbl->GetPath(psl, buf, MAX_PATH, &ffd, 0);
1918 if (hr == S_OK && buf[0] != NUL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001919 rfname = vim_strsave((char_u *)buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920
Bram Moolenaar604729e2013-08-30 16:44:19 +02001921shortcut_end:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 // Release all interface pointers (both belong to the same object)
1923 if (ppf != NULL)
1924 ppf->lpVtbl->Release(ppf);
1925 if (psl != NULL)
1926 psl->lpVtbl->Release(psl);
Bram Moolenaar604729e2013-08-30 16:44:19 +02001927# ifdef FEAT_MBYTE
1928 if (pslw != NULL)
1929 pslw->lpVtbl->Release(pslw);
1930# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931
1932 CoUninitialize();
1933 return rfname;
1934}
1935#endif
1936
1937#if (defined(FEAT_EVAL) && !defined(FEAT_GUI)) || defined(PROTO)
1938/*
1939 * Bring ourselves to the foreground. Does work if the OS doesn't allow it.
1940 */
1941 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001942win32_set_foreground(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943{
1944# ifndef FEAT_GUI
1945 GetConsoleHwnd(); /* get value of s_hwnd */
1946# endif
1947 if (s_hwnd != 0)
1948 SetForegroundWindow(s_hwnd);
1949}
1950#endif
1951
1952#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
1953/*
1954 * Client-server code for Vim
1955 *
1956 * Originally written by Paul Moore
1957 */
1958
1959/* In order to handle inter-process messages, we need to have a window. But
1960 * the functions in this module can be called before the main GUI window is
1961 * created (and may also be called in the console version, where there is no
1962 * GUI window at all).
1963 *
1964 * So we create a hidden window, and arrange to destroy it on exit.
1965 */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001966HWND message_window = 0; /* window that's handling messages */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967
1968#define VIM_CLASSNAME "VIM_MESSAGES"
1969#define VIM_CLASSNAME_LEN (sizeof(VIM_CLASSNAME) - 1)
1970
1971/* Communication is via WM_COPYDATA messages. The message type is send in
1972 * the dwData parameter. Types are defined here. */
1973#define COPYDATA_KEYS 0
1974#define COPYDATA_REPLY 1
1975#define COPYDATA_EXPR 10
1976#define COPYDATA_RESULT 11
1977#define COPYDATA_ERROR_RESULT 12
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001978#define COPYDATA_ENCODING 20
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979
1980/* This is a structure containing a server HWND and its name. */
1981struct server_id
1982{
1983 HWND hwnd;
1984 char_u *name;
1985};
1986
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001987/* Last received 'encoding' that the client uses. */
1988static char_u *client_enc = NULL;
1989
1990/*
1991 * Tell the other side what encoding we are using.
1992 * Errors are ignored.
1993 */
1994 static void
1995serverSendEnc(HWND target)
1996{
1997 COPYDATASTRUCT data;
1998
1999 data.dwData = COPYDATA_ENCODING;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002000#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002001 data.cbData = (DWORD)STRLEN(p_enc) + 1;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002002 data.lpData = p_enc;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002003#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +02002004 data.cbData = (DWORD)STRLEN("latin1") + 1;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002005 data.lpData = "latin1";
2006#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002007 (void)SendMessage(target, WM_COPYDATA, (WPARAM)message_window,
2008 (LPARAM)(&data));
2009}
2010
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011/*
2012 * Clean up on exit. This destroys the hidden message window.
2013 */
2014 static void
2015#ifdef __BORLANDC__
2016 _RTLENTRYF
2017#endif
2018CleanUpMessaging(void)
2019{
2020 if (message_window != 0)
2021 {
2022 DestroyWindow(message_window);
2023 message_window = 0;
2024 }
2025}
2026
2027static int save_reply(HWND server, char_u *reply, int expr);
2028
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002029/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 * The window procedure for the hidden message window.
2031 * It handles callback messages and notifications from servers.
2032 * In order to process these messages, it is necessary to run a
2033 * message loop. Code which may run before the main message loop
2034 * is started (in the GUI) is careful to pump messages when it needs
2035 * to. Features which require message delivery during normal use will
2036 * not work in the console version - this basically means those
2037 * features which allow Vim to act as a server, rather than a client.
2038 */
2039 static LRESULT CALLBACK
2040Messaging_WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
2041{
2042 if (msg == WM_COPYDATA)
2043 {
2044 /* This is a message from another Vim. The dwData member of the
2045 * COPYDATASTRUCT determines the type of message:
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002046 * COPYDATA_ENCODING:
2047 * The encoding that the client uses. Following messages will
2048 * use this encoding, convert if needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 * COPYDATA_KEYS:
2050 * A key sequence. We are a server, and a client wants these keys
2051 * adding to the input queue.
2052 * COPYDATA_REPLY:
2053 * A reply. We are a client, and a server has sent this message
2054 * in response to a request. (server2client())
2055 * COPYDATA_EXPR:
2056 * An expression. We are a server, and a client wants us to
2057 * evaluate this expression.
2058 * COPYDATA_RESULT:
2059 * A reply. We are a client, and a server has sent this message
2060 * in response to a COPYDATA_EXPR.
2061 * COPYDATA_ERROR_RESULT:
2062 * A reply. We are a client, and a server has sent this message
2063 * in response to a COPYDATA_EXPR that failed to evaluate.
2064 */
2065 COPYDATASTRUCT *data = (COPYDATASTRUCT*)lParam;
2066 HWND sender = (HWND)wParam;
2067 COPYDATASTRUCT reply;
2068 char_u *res;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 int retval;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002070 char_u *str;
2071 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072
2073 switch (data->dwData)
2074 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002075 case COPYDATA_ENCODING:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002076# ifdef FEAT_MBYTE
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002077 /* Remember the encoding that the client uses. */
2078 vim_free(client_enc);
2079 client_enc = enc_canonize((char_u *)data->lpData);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002080# endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002081 return 1;
2082
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 case COPYDATA_KEYS:
2084 /* Remember who sent this, for <client> */
2085 clientWindow = sender;
2086
2087 /* Add the received keys to the input buffer. The loop waiting
2088 * for the user to do something should check the input buffer. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002089 str = serverConvert(client_enc, (char_u *)data->lpData, &tofree);
2090 server_to_input_buf(str);
2091 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092
2093# ifdef FEAT_GUI
2094 /* Wake up the main GUI loop. */
2095 if (s_hwnd != 0)
2096 PostMessage(s_hwnd, WM_NULL, 0, 0);
2097# endif
2098 return 1;
2099
2100 case COPYDATA_EXPR:
2101 /* Remember who sent this, for <client> */
2102 clientWindow = sender;
2103
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002104 str = serverConvert(client_enc, (char_u *)data->lpData, &tofree);
2105 res = eval_client_expr_to_string(str);
2106 vim_free(tofree);
2107
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 if (res == NULL)
2109 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002110 res = vim_strsave((char_u *)_(e_invexprmsg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 reply.dwData = COPYDATA_ERROR_RESULT;
2112 }
2113 else
2114 reply.dwData = COPYDATA_RESULT;
2115 reply.lpData = res;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002116 reply.cbData = (DWORD)STRLEN(res) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002118 serverSendEnc(sender);
Bram Moolenaar5246cd72013-06-16 16:41:47 +02002119 retval = (int)SendMessage(sender, WM_COPYDATA,
2120 (WPARAM)message_window, (LPARAM)(&reply));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 vim_free(res);
2122 return retval;
2123
2124 case COPYDATA_REPLY:
2125 case COPYDATA_RESULT:
2126 case COPYDATA_ERROR_RESULT:
2127 if (data->lpData != NULL)
2128 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002129 str = serverConvert(client_enc, (char_u *)data->lpData,
2130 &tofree);
2131 if (tofree == NULL)
2132 str = vim_strsave(str);
2133 if (save_reply(sender, str,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 (data->dwData == COPYDATA_REPLY ? 0 :
2135 (data->dwData == COPYDATA_RESULT ? 1 :
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002136 2))) == FAIL)
2137 vim_free(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138#ifdef FEAT_AUTOCMD
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002139 else if (data->dwData == COPYDATA_REPLY)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 {
Bram Moolenaar427d51c2013-06-16 16:01:25 +02002141 char_u winstr[30];
2142
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002143 sprintf((char *)winstr, PRINTF_HEX_LONG_U, (long_u)sender);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002144 apply_autocmds(EVENT_REMOTEREPLY, winstr, str,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 TRUE, curbuf);
2146 }
2147#endif
2148 }
2149 return 1;
2150 }
2151
2152 return 0;
2153 }
2154
2155 else if (msg == WM_ACTIVATE && wParam == WA_ACTIVE)
2156 {
2157 /* When the message window is activated (brought to the foreground),
2158 * this actually applies to the text window. */
2159#ifndef FEAT_GUI
2160 GetConsoleHwnd(); /* get value of s_hwnd */
2161#endif
2162 if (s_hwnd != 0)
2163 {
2164 SetForegroundWindow(s_hwnd);
2165 return 0;
2166 }
2167 }
2168
2169 return DefWindowProc(hwnd, msg, wParam, lParam);
2170}
2171
2172/*
2173 * Initialise the message handling process. This involves creating a window
2174 * to handle messages - the window will not be visible.
2175 */
2176 void
2177serverInitMessaging(void)
2178{
2179 WNDCLASS wndclass;
2180 HINSTANCE s_hinst;
2181
2182 /* Clean up on exit */
2183 atexit(CleanUpMessaging);
2184
2185 /* Register a window class - we only really care
2186 * about the window procedure
2187 */
2188 s_hinst = (HINSTANCE)GetModuleHandle(0);
2189 wndclass.style = 0;
2190 wndclass.lpfnWndProc = Messaging_WndProc;
2191 wndclass.cbClsExtra = 0;
2192 wndclass.cbWndExtra = 0;
2193 wndclass.hInstance = s_hinst;
2194 wndclass.hIcon = NULL;
2195 wndclass.hCursor = NULL;
2196 wndclass.hbrBackground = NULL;
2197 wndclass.lpszMenuName = NULL;
2198 wndclass.lpszClassName = VIM_CLASSNAME;
2199 RegisterClass(&wndclass);
2200
2201 /* Create the message window. It will be hidden, so the details don't
2202 * matter. Don't use WS_OVERLAPPEDWINDOW, it will make a shortcut remove
2203 * focus from gvim. */
2204 message_window = CreateWindow(VIM_CLASSNAME, "",
2205 WS_POPUPWINDOW | WS_CAPTION,
2206 CW_USEDEFAULT, CW_USEDEFAULT,
2207 100, 100, NULL, NULL,
2208 s_hinst, NULL);
2209}
2210
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002211/* Used by serverSendToVim() to find an alternate server name. */
2212static char_u *altname_buf_ptr = NULL;
2213
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214/*
2215 * Get the title of the window "hwnd", which is the Vim server name, in
2216 * "name[namelen]" and return the length.
2217 * Returns zero if window "hwnd" is not a Vim server.
2218 */
2219 static int
2220getVimServerName(HWND hwnd, char *name, int namelen)
2221{
2222 int len;
2223 char buffer[VIM_CLASSNAME_LEN + 1];
2224
2225 /* Ignore windows which aren't Vim message windows */
2226 len = GetClassName(hwnd, buffer, sizeof(buffer));
2227 if (len != VIM_CLASSNAME_LEN || STRCMP(buffer, VIM_CLASSNAME) != 0)
2228 return 0;
2229
2230 /* Get the title of the window */
2231 return GetWindowText(hwnd, name, namelen);
2232}
2233
2234 static BOOL CALLBACK
2235enumWindowsGetServer(HWND hwnd, LPARAM lparam)
2236{
2237 struct server_id *id = (struct server_id *)lparam;
2238 char server[MAX_PATH];
2239
2240 /* Get the title of the window */
2241 if (getVimServerName(hwnd, server, sizeof(server)) == 0)
2242 return TRUE;
2243
2244 /* If this is the server we're looking for, return its HWND */
2245 if (STRICMP(server, id->name) == 0)
2246 {
2247 id->hwnd = hwnd;
2248 return FALSE;
2249 }
2250
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002251 /* If we are looking for an alternate server, remember this name. */
2252 if (altname_buf_ptr != NULL
2253 && STRNICMP(server, id->name, STRLEN(id->name)) == 0
2254 && vim_isdigit(server[STRLEN(id->name)]))
2255 {
2256 STRCPY(altname_buf_ptr, server);
2257 altname_buf_ptr = NULL; /* don't use another name */
2258 }
2259
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 /* Otherwise, keep looking */
2261 return TRUE;
2262}
2263
2264 static BOOL CALLBACK
2265enumWindowsGetNames(HWND hwnd, LPARAM lparam)
2266{
2267 garray_T *ga = (garray_T *)lparam;
2268 char server[MAX_PATH];
2269
2270 /* Get the title of the window */
2271 if (getVimServerName(hwnd, server, sizeof(server)) == 0)
2272 return TRUE;
2273
2274 /* Add the name to the list */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002275 ga_concat(ga, (char_u *)server);
2276 ga_concat(ga, (char_u *)"\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 return TRUE;
2278}
2279
2280 static HWND
2281findServer(char_u *name)
2282{
2283 struct server_id id;
2284
2285 id.name = name;
2286 id.hwnd = 0;
2287
2288 EnumWindows(enumWindowsGetServer, (LPARAM)(&id));
2289
2290 return id.hwnd;
2291}
2292
2293 void
2294serverSetName(char_u *name)
2295{
2296 char_u *ok_name;
2297 HWND hwnd = 0;
2298 int i = 0;
2299 char_u *p;
2300
2301 /* Leave enough space for a 9-digit suffix to ensure uniqueness! */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002302 ok_name = alloc((unsigned)STRLEN(name) + 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303
2304 STRCPY(ok_name, name);
2305 p = ok_name + STRLEN(name);
2306
2307 for (;;)
2308 {
2309 /* This is inefficient - we're doing an EnumWindows loop for each
2310 * possible name. It would be better to grab all names in one go,
2311 * and scan the list each time...
2312 */
2313 hwnd = findServer(ok_name);
2314 if (hwnd == 0)
2315 break;
2316
2317 ++i;
2318 if (i >= 1000)
2319 break;
2320
2321 sprintf((char *)p, "%d", i);
2322 }
2323
2324 if (hwnd != 0)
2325 vim_free(ok_name);
2326 else
2327 {
2328 /* Remember the name */
2329 serverName = ok_name;
2330#ifdef FEAT_TITLE
2331 need_maketitle = TRUE; /* update Vim window title later */
2332#endif
2333
2334 /* Update the message window title */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002335 SetWindowText(message_window, (LPCSTR)ok_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336
2337#ifdef FEAT_EVAL
2338 /* Set the servername variable */
2339 set_vim_var_string(VV_SEND_SERVER, serverName, -1);
2340#endif
2341 }
2342}
2343
2344 char_u *
2345serverGetVimNames(void)
2346{
2347 garray_T ga;
2348
2349 ga_init2(&ga, 1, 100);
2350
2351 EnumWindows(enumWindowsGetNames, (LPARAM)(&ga));
Bram Moolenaar269ec652004-07-29 08:43:53 +00002352 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353
2354 return ga.ga_data;
2355}
2356
2357 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002358serverSendReply(
2359 char_u *name, /* Where to send. */
2360 char_u *reply) /* What to send. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361{
2362 HWND target;
2363 COPYDATASTRUCT data;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002364 long_u n = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365
2366 /* The "name" argument is a magic cookie obtained from expand("<client>").
2367 * It should be of the form 0xXXXXX - i.e. a C hex literal, which is the
2368 * value of the client's message window HWND.
2369 */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002370 sscanf((char *)name, SCANF_HEX_LONG_U, &n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 if (n == 0)
2372 return -1;
2373
2374 target = (HWND)n;
2375 if (!IsWindow(target))
2376 return -1;
2377
2378 data.dwData = COPYDATA_REPLY;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002379 data.cbData = (DWORD)STRLEN(reply) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 data.lpData = reply;
2381
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002382 serverSendEnc(target);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 if (SendMessage(target, WM_COPYDATA, (WPARAM)message_window,
2384 (LPARAM)(&data)))
2385 return 0;
2386
2387 return -1;
2388}
2389
2390 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002391serverSendToVim(
2392 char_u *name, /* Where to send. */
2393 char_u *cmd, /* What to send. */
2394 char_u **result, /* Result of eval'ed expression */
2395 void *ptarget, /* HWND of server */
2396 int asExpr, /* Expression or keys? */
2397 int silent) /* don't complain about no server */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398{
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002399 HWND target;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 COPYDATASTRUCT data;
2401 char_u *retval = NULL;
2402 int retcode = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002403 char_u altname_buf[MAX_PATH];
2404
2405 /* If the server name does not end in a digit then we look for an
2406 * alternate name. e.g. when "name" is GVIM the we may find GVIM2. */
2407 if (STRLEN(name) > 1 && !vim_isdigit(name[STRLEN(name) - 1]))
2408 altname_buf_ptr = altname_buf;
2409 altname_buf[0] = NUL;
2410 target = findServer(name);
2411 altname_buf_ptr = NULL;
2412 if (target == 0 && altname_buf[0] != NUL)
2413 /* Use another server name we found. */
2414 target = findServer(altname_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415
2416 if (target == 0)
2417 {
2418 if (!silent)
2419 EMSG2(_(e_noserver), name);
2420 return -1;
2421 }
2422
2423 if (ptarget)
2424 *(HWND *)ptarget = target;
2425
2426 data.dwData = asExpr ? COPYDATA_EXPR : COPYDATA_KEYS;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002427 data.cbData = (DWORD)STRLEN(cmd) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 data.lpData = cmd;
2429
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002430 serverSendEnc(target);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 if (SendMessage(target, WM_COPYDATA, (WPARAM)message_window,
2432 (LPARAM)(&data)) == 0)
2433 return -1;
2434
2435 if (asExpr)
2436 retval = serverGetReply(target, &retcode, TRUE, TRUE);
2437
2438 if (result == NULL)
2439 vim_free(retval);
2440 else
2441 *result = retval; /* Caller assumes responsibility for freeing */
2442
2443 return retcode;
2444}
2445
2446/*
2447 * Bring the server to the foreground.
2448 */
2449 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002450serverForeground(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451{
2452 HWND target = findServer(name);
2453
2454 if (target != 0)
2455 SetForegroundWindow(target);
2456}
2457
2458/* Replies from server need to be stored until the client picks them up via
2459 * remote_read(). So we maintain a list of server-id/reply pairs.
2460 * Note that there could be multiple replies from one server pending if the
2461 * client is slow picking them up.
2462 * We just store the replies in a simple list. When we remove an entry, we
2463 * move list entries down to fill the gap.
2464 * The server ID is simply the HWND.
2465 */
2466typedef struct
2467{
2468 HWND server; /* server window */
2469 char_u *reply; /* reply string */
2470 int expr_result; /* 0 for REPLY, 1 for RESULT 2 for error */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002471} reply_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472
2473static garray_T reply_list = {0, 0, sizeof(reply_T), 5, 0};
2474
2475#define REPLY_ITEM(i) ((reply_T *)(reply_list.ga_data) + (i))
2476#define REPLY_COUNT (reply_list.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477
2478/* Flag which is used to wait for a reply */
2479static int reply_received = 0;
2480
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002481/*
2482 * Store a reply. "reply" must be allocated memory (or NULL).
2483 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 static int
2485save_reply(HWND server, char_u *reply, int expr)
2486{
2487 reply_T *rep;
2488
2489 if (ga_grow(&reply_list, 1) == FAIL)
2490 return FAIL;
2491
2492 rep = REPLY_ITEM(REPLY_COUNT);
2493 rep->server = server;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002494 rep->reply = reply;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 rep->expr_result = expr;
2496 if (rep->reply == NULL)
2497 return FAIL;
2498
2499 ++REPLY_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 reply_received = 1;
2501 return OK;
2502}
2503
2504/*
2505 * Get a reply from server "server".
2506 * When "expr_res" is non NULL, get the result of an expression, otherwise a
2507 * server2client() message.
2508 * When non NULL, point to return code. 0 => OK, -1 => ERROR
2509 * If "remove" is TRUE, consume the message, the caller must free it then.
2510 * if "wait" is TRUE block until a message arrives (or the server exits).
2511 */
2512 char_u *
2513serverGetReply(HWND server, int *expr_res, int remove, int wait)
2514{
2515 int i;
2516 char_u *reply;
2517 reply_T *rep;
2518
2519 /* When waiting, loop until the message waiting for is received. */
2520 for (;;)
2521 {
2522 /* Reset this here, in case a message arrives while we are going
2523 * through the already received messages. */
2524 reply_received = 0;
2525
2526 for (i = 0; i < REPLY_COUNT; ++i)
2527 {
2528 rep = REPLY_ITEM(i);
2529 if (rep->server == server
2530 && ((rep->expr_result != 0) == (expr_res != NULL)))
2531 {
2532 /* Save the values we've found for later */
2533 reply = rep->reply;
2534 if (expr_res != NULL)
2535 *expr_res = rep->expr_result == 1 ? 0 : -1;
2536
2537 if (remove)
2538 {
2539 /* Move the rest of the list down to fill the gap */
2540 mch_memmove(rep, rep + 1,
2541 (REPLY_COUNT - i - 1) * sizeof(reply_T));
2542 --REPLY_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 }
2544
2545 /* Return the reply to the caller, who takes on responsibility
2546 * for freeing it if "remove" is TRUE. */
2547 return reply;
2548 }
2549 }
2550
2551 /* If we got here, we didn't find a reply. Return immediately if the
2552 * "wait" parameter isn't set. */
2553 if (!wait)
2554 break;
2555
2556 /* We need to wait for a reply. Enter a message loop until the
2557 * "reply_received" flag gets set. */
2558
2559 /* Loop until we receive a reply */
2560 while (reply_received == 0)
2561 {
2562 /* Wait for a SendMessage() call to us. This could be the reply
2563 * we are waiting for. Use a timeout of a second, to catch the
2564 * situation that the server died unexpectedly. */
2565 MsgWaitForMultipleObjects(0, NULL, TRUE, 1000, QS_ALLINPUT);
2566
2567 /* If the server has died, give up */
2568 if (!IsWindow(server))
2569 return NULL;
2570
2571 serverProcessPendingMessages();
2572 }
2573 }
2574
2575 return NULL;
2576}
2577
2578/*
2579 * Process any messages in the Windows message queue.
2580 */
2581 void
2582serverProcessPendingMessages(void)
2583{
2584 MSG msg;
2585
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02002586 while (pPeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 {
2588 TranslateMessage(&msg);
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02002589 pDispatchMessage(&msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 }
2591}
2592
2593#endif /* FEAT_CLIENTSERVER */
2594
2595#if defined(FEAT_GUI) || (defined(FEAT_PRINTER) && !defined(FEAT_POSTSCRIPT)) \
2596 || defined(PROTO)
2597
2598struct charset_pair
2599{
2600 char *name;
2601 BYTE charset;
2602};
2603
2604static struct charset_pair
2605charset_pairs[] =
2606{
2607 {"ANSI", ANSI_CHARSET},
2608 {"CHINESEBIG5", CHINESEBIG5_CHARSET},
2609 {"DEFAULT", DEFAULT_CHARSET},
2610 {"HANGEUL", HANGEUL_CHARSET},
2611 {"OEM", OEM_CHARSET},
2612 {"SHIFTJIS", SHIFTJIS_CHARSET},
2613 {"SYMBOL", SYMBOL_CHARSET},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 {"ARABIC", ARABIC_CHARSET},
2615 {"BALTIC", BALTIC_CHARSET},
2616 {"EASTEUROPE", EASTEUROPE_CHARSET},
2617 {"GB2312", GB2312_CHARSET},
2618 {"GREEK", GREEK_CHARSET},
2619 {"HEBREW", HEBREW_CHARSET},
2620 {"JOHAB", JOHAB_CHARSET},
2621 {"MAC", MAC_CHARSET},
2622 {"RUSSIAN", RUSSIAN_CHARSET},
2623 {"THAI", THAI_CHARSET},
2624 {"TURKISH", TURKISH_CHARSET},
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002625#ifdef VIETNAMESE_CHARSET
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 {"VIETNAMESE", VIETNAMESE_CHARSET},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627#endif
2628 {NULL, 0}
2629};
2630
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002631struct quality_pair
2632{
2633 char *name;
2634 DWORD quality;
2635};
2636
2637static struct quality_pair
2638quality_pairs[] = {
2639#ifdef CLEARTYPE_QUALITY
2640 {"CLEARTYPE", CLEARTYPE_QUALITY},
2641#endif
2642#ifdef ANTIALIASED_QUALITY
2643 {"ANTIALIASED", ANTIALIASED_QUALITY},
2644#endif
Bram Moolenaar73a733e2016-05-11 21:05:05 +02002645#ifdef NONANTIALIASED_QUALITY
2646 {"NONANTIALIASED", NONANTIALIASED_QUALITY},
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002647#endif
2648#ifdef PROOF_QUALITY
2649 {"PROOF", PROOF_QUALITY},
2650#endif
Bram Moolenaar4c9ce052016-04-04 21:06:19 +02002651#ifdef DRAFT_QUALITY
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002652 {"DRAFT", DRAFT_QUALITY},
2653#endif
2654 {"DEFAULT", DEFAULT_QUALITY},
2655 {NULL, 0}
2656};
2657
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658/*
2659 * Convert a charset ID to a name.
2660 * Return NULL when not recognized.
2661 */
2662 char *
2663charset_id2name(int id)
2664{
2665 struct charset_pair *cp;
2666
2667 for (cp = charset_pairs; cp->name != NULL; ++cp)
2668 if ((BYTE)id == cp->charset)
2669 break;
2670 return cp->name;
2671}
2672
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002673/*
2674 * Convert a quality ID to a name.
2675 * Return NULL when not recognized.
2676 */
2677 char *
2678quality_id2name(DWORD id)
2679{
2680 struct quality_pair *qp;
2681
2682 for (qp = quality_pairs; qp->name != NULL; ++qp)
2683 if (id == qp->quality)
2684 break;
2685 return qp->name;
2686}
2687
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688static const LOGFONT s_lfDefault =
2689{
2690 -12, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, DEFAULT_CHARSET,
2691 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
2692 PROOF_QUALITY, FIXED_PITCH | FF_DONTCARE,
2693 "Fixedsys" /* see _ReadVimIni */
2694};
2695
2696/* Initialise the "current height" to -12 (same as s_lfDefault) just
2697 * in case the user specifies a font in "guifont" with no size before a font
2698 * with an explicit size has been set. This defaults the size to this value
2699 * (-12 equates to roughly 9pt).
2700 */
2701int current_font_height = -12; /* also used in gui_w48.c */
2702
2703/* Convert a string representing a point size into pixels. The string should
2704 * be a positive decimal number, with an optional decimal point (eg, "12", or
2705 * "10.5"). The pixel value is returned, and a pointer to the next unconverted
2706 * character is stored in *end. The flag "vertical" says whether this
2707 * calculation is for a vertical (height) size or a horizontal (width) one.
2708 */
2709 static int
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002710points_to_pixels(char_u *str, char_u **end, int vertical, long_i pprinter_dc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711{
2712 int pixels;
2713 int points = 0;
2714 int divisor = 0;
2715 HWND hwnd = (HWND)0;
2716 HDC hdc;
2717 HDC printer_dc = (HDC)pprinter_dc;
2718
2719 while (*str != NUL)
2720 {
2721 if (*str == '.' && divisor == 0)
2722 {
2723 /* Start keeping a divisor, for later */
2724 divisor = 1;
2725 }
2726 else
2727 {
2728 if (!VIM_ISDIGIT(*str))
2729 break;
2730
2731 points *= 10;
2732 points += *str - '0';
2733 divisor *= 10;
2734 }
2735 ++str;
2736 }
2737
2738 if (divisor == 0)
2739 divisor = 1;
2740
2741 if (printer_dc == NULL)
2742 {
2743 hwnd = GetDesktopWindow();
2744 hdc = GetWindowDC(hwnd);
2745 }
2746 else
2747 hdc = printer_dc;
2748
2749 pixels = MulDiv(points,
2750 GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX),
2751 72 * divisor);
2752
2753 if (printer_dc == NULL)
2754 ReleaseDC(hwnd, hdc);
2755
2756 *end = str;
2757 return pixels;
2758}
2759
2760 static int CALLBACK
2761font_enumproc(
2762 ENUMLOGFONT *elf,
Bram Moolenaar1266d672017-02-01 13:43:36 +01002763 NEWTEXTMETRIC *ntm UNUSED,
2764 int type UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 LPARAM lparam)
2766{
2767 /* Return value:
2768 * 0 = terminate now (monospace & ANSI)
2769 * 1 = continue, still no luck...
2770 * 2 = continue, but we have an acceptable LOGFONT
2771 * (monospace, not ANSI)
2772 * We use these values, as EnumFontFamilies returns 1 if the
2773 * callback function is never called. So, we check the return as
2774 * 0 = perfect, 2 = OK, 1 = no good...
2775 * It's not pretty, but it works!
2776 */
2777
2778 LOGFONT *lf = (LOGFONT *)(lparam);
2779
2780#ifndef FEAT_PROPORTIONAL_FONTS
2781 /* Ignore non-monospace fonts without further ado */
2782 if ((ntm->tmPitchAndFamily & 1) != 0)
2783 return 1;
2784#endif
2785
2786 /* Remember this LOGFONT as a "possible" */
2787 *lf = elf->elfLogFont;
2788
2789 /* Terminate the scan as soon as we find an ANSI font */
2790 if (lf->lfCharSet == ANSI_CHARSET
2791 || lf->lfCharSet == OEM_CHARSET
2792 || lf->lfCharSet == DEFAULT_CHARSET)
2793 return 0;
2794
2795 /* Continue the scan - we have a non-ANSI font */
2796 return 2;
2797}
2798
2799 static int
2800init_logfont(LOGFONT *lf)
2801{
2802 int n;
2803 HWND hwnd = GetDesktopWindow();
2804 HDC hdc = GetWindowDC(hwnd);
2805
2806 n = EnumFontFamilies(hdc,
2807 (LPCSTR)lf->lfFaceName,
2808 (FONTENUMPROC)font_enumproc,
2809 (LPARAM)lf);
2810
2811 ReleaseDC(hwnd, hdc);
2812
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002813 /* If we couldn't find a usable font, return failure */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 if (n == 1)
2815 return FAIL;
2816
2817 /* Tidy up the rest of the LOGFONT structure. We set to a basic
2818 * font - get_logfont() sets bold, italic, etc based on the user's
2819 * input.
2820 */
2821 lf->lfHeight = current_font_height;
2822 lf->lfWidth = 0;
2823 lf->lfItalic = FALSE;
2824 lf->lfUnderline = FALSE;
2825 lf->lfStrikeOut = FALSE;
2826 lf->lfWeight = FW_NORMAL;
2827
2828 /* Return success */
2829 return OK;
2830}
2831
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002832/*
2833 * Get font info from "name" into logfont "lf".
2834 * Return OK for a valid name, FAIL otherwise.
2835 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836 int
2837get_logfont(
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002838 LOGFONT *lf,
2839 char_u *name,
2840 HDC printer_dc,
2841 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842{
2843 char_u *p;
2844 int i;
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002845 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846 static LOGFONT *lastlf = NULL;
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002847#ifdef FEAT_MBYTE
2848 char_u *acpname = NULL;
2849#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850
2851 *lf = s_lfDefault;
2852 if (name == NULL)
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002853 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002855#ifdef FEAT_MBYTE
2856 /* Convert 'name' from 'encoding' to the current codepage, because
2857 * lf->lfFaceName uses the current codepage.
2858 * TODO: Use Wide APIs instead of ANSI APIs. */
2859 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
2860 {
2861 int len;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01002862 enc_to_acp(name, (int)STRLEN(name), &acpname, &len);
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002863 name = acpname;
2864 }
2865#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 if (STRCMP(name, "*") == 0)
2867 {
2868#if defined(FEAT_GUI_W32)
2869 CHOOSEFONT cf;
2870 /* if name is "*", bring up std font dialog: */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02002871 vim_memset(&cf, 0, sizeof(cf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 cf.lStructSize = sizeof(cf);
2873 cf.hwndOwner = s_hwnd;
2874 cf.Flags = CF_SCREENFONTS | CF_FIXEDPITCHONLY | CF_INITTOLOGFONTSTRUCT;
2875 if (lastlf != NULL)
2876 *lf = *lastlf;
2877 cf.lpLogFont = lf;
2878 cf.nFontType = 0 ; //REGULAR_FONTTYPE;
2879 if (ChooseFont(&cf))
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002880 ret = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881#endif
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002882 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 }
2884
2885 /*
2886 * Split name up, it could be <name>:h<height>:w<width> etc.
2887 */
2888 for (p = name; *p && *p != ':'; p++)
2889 {
Bram Moolenaar38bc4952016-11-10 17:10:51 +01002890 if (p - name + 1 >= LF_FACESIZE)
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002891 goto theend; /* Name too long */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892 lf->lfFaceName[p - name] = *p;
2893 }
2894 if (p != name)
2895 lf->lfFaceName[p - name] = NUL;
2896
2897 /* First set defaults */
2898 lf->lfHeight = -12;
2899 lf->lfWidth = 0;
2900 lf->lfWeight = FW_NORMAL;
2901 lf->lfItalic = FALSE;
2902 lf->lfUnderline = FALSE;
2903 lf->lfStrikeOut = FALSE;
2904
2905 /*
2906 * If the font can't be found, try replacing '_' by ' '.
2907 */
2908 if (init_logfont(lf) == FAIL)
2909 {
2910 int did_replace = FALSE;
2911
2912 for (i = 0; lf->lfFaceName[i]; ++i)
2913 if (lf->lfFaceName[i] == '_')
2914 {
2915 lf->lfFaceName[i] = ' ';
2916 did_replace = TRUE;
2917 }
2918 if (!did_replace || init_logfont(lf) == FAIL)
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002919 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 }
2921
2922 while (*p == ':')
2923 p++;
2924
2925 /* Set the values found after ':' */
2926 while (*p)
2927 {
2928 switch (*p++)
2929 {
2930 case 'h':
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002931 lf->lfHeight = - points_to_pixels(p, &p, TRUE, (long_i)printer_dc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 break;
2933 case 'w':
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002934 lf->lfWidth = points_to_pixels(p, &p, FALSE, (long_i)printer_dc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 break;
2936 case 'b':
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 lf->lfWeight = FW_BOLD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 break;
2939 case 'i':
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 lf->lfItalic = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 break;
2942 case 'u':
2943 lf->lfUnderline = TRUE;
2944 break;
2945 case 's':
2946 lf->lfStrikeOut = TRUE;
2947 break;
2948 case 'c':
2949 {
2950 struct charset_pair *cp;
2951
2952 for (cp = charset_pairs; cp->name != NULL; ++cp)
2953 if (STRNCMP(p, cp->name, strlen(cp->name)) == 0)
2954 {
2955 lf->lfCharSet = cp->charset;
2956 p += strlen(cp->name);
2957 break;
2958 }
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002959 if (cp->name == NULL && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960 {
Bram Moolenaar051b7822005-05-19 21:00:46 +00002961 vim_snprintf((char *)IObuff, IOSIZE,
2962 _("E244: Illegal charset name \"%s\" in font name \"%s\""), p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963 EMSG(IObuff);
2964 break;
2965 }
2966 break;
2967 }
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002968 case 'q':
2969 {
2970 struct quality_pair *qp;
2971
2972 for (qp = quality_pairs; qp->name != NULL; ++qp)
2973 if (STRNCMP(p, qp->name, strlen(qp->name)) == 0)
2974 {
2975 lf->lfQuality = qp->quality;
2976 p += strlen(qp->name);
2977 break;
2978 }
2979 if (qp->name == NULL && verbose)
2980 {
2981 vim_snprintf((char *)IObuff, IOSIZE,
2982 _("E244: Illegal quality name \"%s\" in font name \"%s\""), p, name);
2983 EMSG(IObuff);
2984 break;
2985 }
2986 break;
2987 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988 default:
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002989 if (verbose)
2990 {
Bram Moolenaar051b7822005-05-19 21:00:46 +00002991 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002992 _("E245: Illegal char '%c' in font name \"%s\""),
2993 p[-1], name);
2994 EMSG(IObuff);
2995 }
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002996 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997 }
2998 while (*p == ':')
2999 p++;
3000 }
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003001 ret = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004 /* ron: init lastlf */
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003005 if (ret == OK && printer_dc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006 {
3007 vim_free(lastlf);
3008 lastlf = (LOGFONT *)alloc(sizeof(LOGFONT));
3009 if (lastlf != NULL)
3010 mch_memmove(lastlf, lf, sizeof(LOGFONT));
3011 }
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003012#ifdef FEAT_MBYTE
3013 vim_free(acpname);
3014#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003016 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017}
3018
3019#endif /* defined(FEAT_GUI) || defined(FEAT_PRINTER) */
Bram Moolenaarf12d9832016-01-29 21:11:25 +01003020
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003021#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaarf12d9832016-01-29 21:11:25 +01003022/*
3023 * Initialize the Winsock dll.
3024 */
3025 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003026channel_init_winsock(void)
Bram Moolenaarf12d9832016-01-29 21:11:25 +01003027{
3028 WSADATA wsaData;
3029 int wsaerr;
3030
3031 if (WSInitialized)
3032 return;
3033
3034 wsaerr = WSAStartup(MAKEWORD(2, 2), &wsaData);
3035 if (wsaerr == 0)
3036 WSInitialized = TRUE;
3037}
3038#endif