blob: dc55389475f4da51d4a8853360c31b2cc041ec8a [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 Moolenaar071d4272004-06-13 20:20:40 +000043#endif
44
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 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000309/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +0000310 void
311mch_restore_title(
312 int which)
313{
314#ifndef FEAT_GUI_MSWIN
Bram Moolenaar1df52d72014-10-15 22:50:10 +0200315 SetConsoleTitle(g_szOrigTitle);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316#endif
317}
318
319
320/*
321 * Return TRUE if we can restore the title (we can)
322 */
323 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100324mch_can_restore_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325{
326 return TRUE;
327}
328
329
330/*
331 * Return TRUE if we can restore the icon title (we can't)
332 */
333 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100334mch_can_restore_icon(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335{
336 return FALSE;
337}
338#endif /* FEAT_TITLE */
339
340
341/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000342 * Get absolute file name into buffer "buf" of length "len" bytes,
343 * turning all '/'s into '\\'s and getting the correct case of each component
344 * of the file name. Append a (back)slash to a directory name.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345 * When 'shellslash' set do it the other way around.
346 * Return OK or FAIL.
347 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000348/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349 int
350mch_FullName(
351 char_u *fname,
352 char_u *buf,
353 int len,
354 int force)
355{
356 int nResult = FAIL;
357
358#ifdef __BORLANDC__
359 if (*fname == NUL) /* Borland behaves badly here - make it consistent */
360 nResult = mch_dirname(buf, len);
361 else
362#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000364#ifdef FEAT_MBYTE
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200365 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000366 {
367 WCHAR *wname;
368 WCHAR wbuf[MAX_PATH];
369 char_u *cname = NULL;
370
371 /* Use the wide function:
372 * - convert the fname from 'encoding' to UCS2.
373 * - invoke _wfullpath()
374 * - convert the result from UCS2 to 'encoding'.
375 */
Bram Moolenaar36f692d2008-11-20 16:10:17 +0000376 wname = enc_to_utf16(fname, NULL);
Bram Moolenaar374bf022014-11-05 19:33:24 +0100377 if (wname != NULL && _wfullpath(wbuf, wname, MAX_PATH) != NULL)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000378 {
Bram Moolenaar36f692d2008-11-20 16:10:17 +0000379 cname = utf16_to_enc((short_u *)wbuf, NULL);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000380 if (cname != NULL)
381 {
Bram Moolenaarfe3ca8d2005-07-18 21:43:02 +0000382 vim_strncpy(buf, cname, len - 1);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000383 nResult = OK;
384 }
385 }
386 vim_free(wname);
387 vim_free(cname);
388 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000389 if (nResult == FAIL) /* fall back to non-wide function */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000391 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100392 if (_fullpath((char *)buf, (const char *)fname, len - 1) == NULL)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000393 {
Bram Moolenaarfe3ca8d2005-07-18 21:43:02 +0000394 /* failed, use relative path name */
395 vim_strncpy(buf, fname, len - 1);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000396 }
397 else
398 nResult = OK;
399 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401
402#ifdef USE_FNAME_CASE
403 fname_case(buf, len);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000404#else
405 slash_adjust(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406#endif
407
408 return nResult;
409}
410
411
412/*
413 * Return TRUE if "fname" does not depend on the current directory.
414 */
415 int
416mch_isFullName(char_u *fname)
417{
Bram Moolenaard2a203b2013-08-30 16:51:18 +0200418#ifdef FEAT_MBYTE
419 /* WinNT and later can use _MAX_PATH wide characters for a pathname, which
420 * means that the maximum pathname is _MAX_PATH * 3 bytes when 'enc' is
421 * UTF-8. */
422 char szName[_MAX_PATH * 3 + 1];
423#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424 char szName[_MAX_PATH + 1];
Bram Moolenaard2a203b2013-08-30 16:51:18 +0200425#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426
427 /* A name like "d:/foo" and "//server/share" is absolute */
428 if ((fname[0] && fname[1] == ':' && (fname[2] == '/' || fname[2] == '\\'))
429 || (fname[0] == fname[1] && (fname[0] == '/' || fname[0] == '\\')))
430 return TRUE;
431
432 /* A name that can't be made absolute probably isn't absolute. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100433 if (mch_FullName(fname, (char_u *)szName, sizeof(szName) - 1, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434 return FALSE;
435
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100436 return pathcmp((const char *)fname, (const char *)szName, -1) == 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437}
438
439/*
440 * Replace all slashes by backslashes.
441 * This used to be the other way around, but MS-DOS sometimes has problems
442 * with slashes (e.g. in a command name). We can't have mixed slashes and
443 * backslashes, because comparing file names will not work correctly. The
444 * commands that use a file name should try to avoid the need to type a
445 * backslash twice.
446 * When 'shellslash' set do it the other way around.
Bram Moolenaarb4f6a462015-10-13 19:43:17 +0200447 * When the path looks like a URL leave it unmodified.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448 */
449 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100450slash_adjust(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451{
Bram Moolenaarb4f6a462015-10-13 19:43:17 +0200452 if (path_with_url(p))
453 return;
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +0000454 while (*p)
455 {
456 if (*p == psepcN)
457 *p = psepc;
458 mb_ptr_adv(p);
459 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460}
461
Bram Moolenaar8767f522016-07-01 17:17:39 +0200462/* Use 64-bit stat functions if available. */
463#ifdef HAVE_STAT64
464# undef stat
465# undef _stat
466# undef _wstat
467# undef _fstat
468# define stat _stat64
469# define _stat _stat64
470# define _wstat _wstat64
471# define _fstat _fstat64
472#endif
473
Bram Moolenaar9d1685d2014-01-14 12:18:45 +0100474#if (defined(_MSC_VER) && (_MSC_VER >= 1300)) || defined(__MINGW32__)
Bram Moolenaar8962fda2013-09-29 19:05:21 +0200475# define OPEN_OH_ARGTYPE intptr_t
476#else
477# define OPEN_OH_ARGTYPE long
478#endif
479
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200480 static int
Bram Moolenaar8767f522016-07-01 17:17:39 +0200481stat_symlink_aware(const char *name, stat_T *stp)
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200482{
Bram Moolenaarfce7b3d2016-01-19 19:00:32 +0100483#if (defined(_MSC_VER) && (_MSC_VER < 1900)) || defined(__MINGW32__)
484 /* Work around for VC12 or earlier (and MinGW). stat() can't handle
485 * symlinks properly.
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200486 * VC9 or earlier: stat() doesn't support a symlink at all. It retrieves
487 * status of a symlink itself.
488 * VC10: stat() supports a symlink to a normal file, but it doesn't support
Bram Moolenaarfce7b3d2016-01-19 19:00:32 +0100489 * a symlink to a directory (always returns an error).
490 * VC11 and VC12: stat() doesn't return an error for a symlink to a
491 * directory, but it doesn't set S_IFDIR flag.
492 * MinGW: Same as VC9. */
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200493 WIN32_FIND_DATA findData;
494 HANDLE hFind, h;
495 DWORD attr = 0;
496 BOOL is_symlink = FALSE;
497
498 hFind = FindFirstFile(name, &findData);
499 if (hFind != INVALID_HANDLE_VALUE)
500 {
501 attr = findData.dwFileAttributes;
502 if ((attr & FILE_ATTRIBUTE_REPARSE_POINT)
503 && (findData.dwReserved0 == IO_REPARSE_TAG_SYMLINK))
504 is_symlink = TRUE;
505 FindClose(hFind);
506 }
507 if (is_symlink)
508 {
509 h = CreateFile(name, FILE_READ_ATTRIBUTES,
510 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
511 OPEN_EXISTING,
512 (attr & FILE_ATTRIBUTE_DIRECTORY)
513 ? FILE_FLAG_BACKUP_SEMANTICS : 0,
514 NULL);
515 if (h != INVALID_HANDLE_VALUE)
516 {
517 int fd, n;
518
Bram Moolenaar8962fda2013-09-29 19:05:21 +0200519 fd = _open_osfhandle((OPEN_OH_ARGTYPE)h, _O_RDONLY);
Bram Moolenaar8767f522016-07-01 17:17:39 +0200520 n = _fstat(fd, (struct _stat *)stp);
Bram Moolenaarfce7b3d2016-01-19 19:00:32 +0100521 if ((n == 0) && (attr & FILE_ATTRIBUTE_DIRECTORY))
522 stp->st_mode = (stp->st_mode & ~S_IFREG) | S_IFDIR;
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200523 _close(fd);
524 return n;
525 }
526 }
527#endif
528 return stat(name, stp);
529}
530
531#ifdef FEAT_MBYTE
532 static int
Bram Moolenaar8767f522016-07-01 17:17:39 +0200533wstat_symlink_aware(const WCHAR *name, stat_T *stp)
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200534{
Bram Moolenaarfce7b3d2016-01-19 19:00:32 +0100535# if (defined(_MSC_VER) && (_MSC_VER < 1900)) || defined(__MINGW32__)
536 /* Work around for VC12 or earlier (and MinGW). _wstat() can't handle
537 * symlinks properly.
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200538 * VC9 or earlier: _wstat() doesn't support a symlink at all. It retrieves
539 * status of a symlink itself.
540 * VC10: _wstat() supports a symlink to a normal file, but it doesn't
Bram Moolenaarfce7b3d2016-01-19 19:00:32 +0100541 * support a symlink to a directory (always returns an error).
542 * VC11 and VC12: _wstat() doesn't return an error for a symlink to a
543 * directory, but it doesn't set S_IFDIR flag.
544 * MinGW: Same as VC9. */
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200545 int n;
546 BOOL is_symlink = FALSE;
547 HANDLE hFind, h;
548 DWORD attr = 0;
549 WIN32_FIND_DATAW findDataW;
550
551 hFind = FindFirstFileW(name, &findDataW);
552 if (hFind != INVALID_HANDLE_VALUE)
553 {
554 attr = findDataW.dwFileAttributes;
555 if ((attr & FILE_ATTRIBUTE_REPARSE_POINT)
556 && (findDataW.dwReserved0 == IO_REPARSE_TAG_SYMLINK))
557 is_symlink = TRUE;
558 FindClose(hFind);
559 }
560 if (is_symlink)
561 {
562 h = CreateFileW(name, FILE_READ_ATTRIBUTES,
563 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
564 OPEN_EXISTING,
565 (attr & FILE_ATTRIBUTE_DIRECTORY)
566 ? FILE_FLAG_BACKUP_SEMANTICS : 0,
567 NULL);
568 if (h != INVALID_HANDLE_VALUE)
569 {
570 int fd;
571
Bram Moolenaar8962fda2013-09-29 19:05:21 +0200572 fd = _open_osfhandle((OPEN_OH_ARGTYPE)h, _O_RDONLY);
Bram Moolenaar8767f522016-07-01 17:17:39 +0200573 n = _fstat(fd, (struct _stat *)stp);
Bram Moolenaarfce7b3d2016-01-19 19:00:32 +0100574 if ((n == 0) && (attr & FILE_ATTRIBUTE_DIRECTORY))
575 stp->st_mode = (stp->st_mode & ~S_IFREG) | S_IFDIR;
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200576 _close(fd);
577 return n;
578 }
579 }
580# endif
Bram Moolenaar8767f522016-07-01 17:17:39 +0200581 return _wstat(name, (struct _stat *)stp);
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200582}
583#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584
585/*
586 * stat() can't handle a trailing '/' or '\', remove it first.
587 */
588 int
Bram Moolenaar8767f522016-07-01 17:17:39 +0200589vim_stat(const char *name, stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590{
Bram Moolenaard2a203b2013-08-30 16:51:18 +0200591#ifdef FEAT_MBYTE
592 /* WinNT and later can use _MAX_PATH wide characters for a pathname, which
593 * means that the maximum pathname is _MAX_PATH * 3 bytes when 'enc' is
594 * UTF-8. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100595 char_u buf[_MAX_PATH * 3 + 1];
Bram Moolenaard2a203b2013-08-30 16:51:18 +0200596#else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100597 char_u buf[_MAX_PATH + 1];
Bram Moolenaard2a203b2013-08-30 16:51:18 +0200598#endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100599 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600
Bram Moolenaard2a203b2013-08-30 16:51:18 +0200601 vim_strncpy((char_u *)buf, (char_u *)name, sizeof(buf) - 1);
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100602 p = buf + STRLEN(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603 if (p > buf)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000604 mb_ptr_back(buf, p);
Bram Moolenaarb1cb35f2014-01-10 13:05:20 +0100605
606 /* Remove trailing '\\' except root path. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 if (p > buf && (*p == '\\' || *p == '/') && p[-1] != ':')
608 *p = NUL;
Bram Moolenaarb1cb35f2014-01-10 13:05:20 +0100609
610 if ((buf[0] == '\\' && buf[1] == '\\') || (buf[0] == '/' && buf[1] == '/'))
611 {
612 /* UNC root path must be followed by '\\'. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100613 p = vim_strpbrk(buf + 2, (char_u *)"\\/");
Bram Moolenaarb1cb35f2014-01-10 13:05:20 +0100614 if (p != NULL)
615 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100616 p = vim_strpbrk(p + 1, (char_u *)"\\/");
Bram Moolenaarb1cb35f2014-01-10 13:05:20 +0100617 if (p == NULL)
618 STRCAT(buf, "\\");
619 }
620 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621#ifdef FEAT_MBYTE
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200622 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623 {
Bram Moolenaar36f692d2008-11-20 16:10:17 +0000624 WCHAR *wp = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625 int n;
626
627 if (wp != NULL)
628 {
Bram Moolenaar8767f522016-07-01 17:17:39 +0200629 n = wstat_symlink_aware(wp, stp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630 vim_free(wp);
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200631 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 }
633 }
634#endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100635 return stat_symlink_aware((char *)buf, stp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636}
637
638#if defined(FEAT_GUI_MSWIN) || defined(PROTO)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000639/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 void
641mch_settmode(int tmode)
642{
643 /* nothing to do */
644}
645
646 int
647mch_get_shellsize(void)
648{
649 /* never used */
650 return OK;
651}
652
653 void
654mch_set_shellsize(void)
655{
656 /* never used */
657}
658
659/*
660 * Rows and/or Columns has changed.
661 */
662 void
663mch_new_shellsize(void)
664{
665 /* never used */
666}
667
668#endif
669
670/*
671 * We have no job control, so fake it by starting a new shell.
672 */
673 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100674mch_suspend(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675{
676 suspend_shell();
677}
678
679#if defined(USE_MCH_ERRMSG) || defined(PROTO)
680
681#ifdef display_errors
682# undef display_errors
683#endif
684
685/*
686 * Display the saved error message(s).
687 */
688 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100689display_errors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690{
691 char *p;
692
693 if (error_ga.ga_data != NULL)
694 {
695 /* avoid putting up a message box with blanks only */
696 for (p = (char *)error_ga.ga_data; *p; ++p)
697 if (!isspace(*p))
698 {
Bram Moolenaarc17ef8e2006-03-25 21:48:58 +0000699 (void)gui_mch_dialog(
700#ifdef FEAT_GUI
701 gui.starting ? VIM_INFO :
702#endif
703 VIM_ERROR,
704#ifdef FEAT_GUI
705 gui.starting ? (char_u *)_("Message") :
706#endif
707 (char_u *)_("Error"),
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100708 (char_u *)p, (char_u *)_("&Ok"),
709 1, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 break;
711 }
712 ga_clear(&error_ga);
713 }
714}
715#endif
716
717
718/*
719 * Return TRUE if "p" contain a wildcard that can be expanded by
720 * dos_expandpath().
721 */
722 int
723mch_has_exp_wildcard(char_u *p)
724{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000725 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726 {
727 if (vim_strchr((char_u *)"?*[", *p) != NULL
728 || (*p == '~' && p[1] != NUL))
729 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730 }
731 return FALSE;
732}
733
734/*
735 * Return TRUE if "p" contain a wildcard or a "~1" kind of thing (could be a
736 * shortened file name).
737 */
738 int
739mch_has_wildcard(char_u *p)
740{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000741 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 {
743 if (vim_strchr((char_u *)
744# ifdef VIM_BACKTICK
745 "?*$[`"
746# else
747 "?*$["
748# endif
749 , *p) != NULL
750 || (*p == '~' && p[1] != NUL))
751 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 }
753 return FALSE;
754}
755
756
757/*
758 * The normal _chdir() does not change the default drive. This one does.
759 * Returning 0 implies success; -1 implies failure.
760 */
761 int
762mch_chdir(char *path)
763{
764 if (path[0] == NUL) /* just checking... */
765 return -1;
766
Bram Moolenaara2974d72009-07-14 16:38:36 +0000767 if (p_verbose >= 5)
768 {
769 verbose_enter();
770 smsg((char_u *)"chdir(%s)", path);
771 verbose_leave();
772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 if (isalpha(path[0]) && path[1] == ':') /* has a drive name */
774 {
775 /* If we can change to the drive, skip that part of the path. If we
776 * can't then the current directory may be invalid, try using chdir()
777 * with the whole path. */
778 if (_chdrive(TOLOWER_ASC(path[0]) - 'a' + 1) == 0)
779 path += 2;
780 }
781
782 if (*path == NUL) /* drive name only */
783 return 0;
784
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000785#ifdef FEAT_MBYTE
786 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
787 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100788 WCHAR *p = enc_to_utf16((char_u *)path, NULL);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000789 int n;
790
791 if (p != NULL)
792 {
793 n = _wchdir(p);
794 vim_free(p);
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200795 return n;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000796 }
797 }
798#endif
799
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 return chdir(path); /* let the normal chdir() do the rest */
801}
802
803
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804#ifdef FEAT_GUI_MSWIN
805/*
806 * return non-zero if a character is available
807 */
808 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100809mch_char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810{
811 /* never used */
812 return TRUE;
813}
814#endif
815
816
817/*
818 * set screen mode, always fails.
819 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000820/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 int
822mch_screenmode(
823 char_u *arg)
824{
825 EMSG(_(e_screenmode));
826 return FAIL;
827}
828
829
830#if defined(FEAT_LIBCALL) || defined(PROTO)
831/*
832 * Call a DLL routine which takes either a string or int param
833 * and returns an allocated string.
834 * Return OK if it worked, FAIL if not.
835 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836typedef LPTSTR (*MYSTRPROCSTR)(LPTSTR);
837typedef LPTSTR (*MYINTPROCSTR)(int);
838typedef int (*MYSTRPROCINT)(LPTSTR);
839typedef int (*MYINTPROCINT)(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841/*
842 * Check if a pointer points to a valid NUL terminated string.
843 * Return the length of the string, including terminating NUL.
844 * Returns 0 for an invalid pointer, 1 for an empty string.
845 */
846 static size_t
847check_str_len(char_u *str)
848{
849 SYSTEM_INFO si;
850 MEMORY_BASIC_INFORMATION mbi;
851 size_t length = 0;
852 size_t i;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100853 const char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854
855 /* get page size */
856 GetSystemInfo(&si);
857
858 /* get memory information */
859 if (VirtualQuery(str, &mbi, sizeof(mbi)))
860 {
861 /* pre cast these (typing savers) */
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000862 long_u dwStr = (long_u)str;
863 long_u dwBaseAddress = (long_u)mbi.BaseAddress;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864
865 /* get start address of page that str is on */
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000866 long_u strPage = dwStr - (dwStr - dwBaseAddress) % si.dwPageSize;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867
868 /* get length from str to end of page */
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000869 long_u pageLength = si.dwPageSize - (dwStr - strPage);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870
Bram Moolenaar442b4222010-05-24 21:34:22 +0200871 for (p = str; !IsBadReadPtr(p, (UINT)pageLength);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 p += pageLength, pageLength = si.dwPageSize)
873 for (i = 0; i < pageLength; ++i, ++length)
874 if (p[i] == NUL)
875 return length + 1;
876 }
877
878 return 0;
879}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200881/*
882 * Passed to do_in_runtimepath() to load a vim.ico file.
883 */
884 static void
885mch_icon_load_cb(char_u *fname, void *cookie)
886{
887 HANDLE *h = (HANDLE *)cookie;
888
889 *h = LoadImage(NULL,
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100890 (LPSTR)fname,
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200891 IMAGE_ICON,
892 64,
893 64,
894 LR_LOADFROMFILE | LR_LOADMAP3DCOLORS);
895}
896
897/*
898 * Try loading an icon file from 'runtimepath'.
899 */
900 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100901mch_icon_load(HANDLE *iconp)
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200902{
903 return do_in_runtimepath((char_u *)"bitmaps/vim.ico",
Bram Moolenaar7f8989d2016-03-12 22:11:39 +0100904 0, mch_icon_load_cb, iconp);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200905}
906
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907 int
908mch_libcall(
909 char_u *libname,
910 char_u *funcname,
911 char_u *argstring, /* NULL when using a argint */
912 int argint,
913 char_u **string_result,/* NULL when using number_result */
914 int *number_result)
915{
916 HINSTANCE hinstLib;
917 MYSTRPROCSTR ProcAdd;
918 MYINTPROCSTR ProcAddI;
919 char_u *retval_str = NULL;
920 int retval_int = 0;
921 size_t len;
922
923 BOOL fRunTimeLinkSuccess = FALSE;
924
925 // Get a handle to the DLL module.
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100926 hinstLib = vimLoadLib((char *)libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927
928 // If the handle is valid, try to get the function address.
929 if (hinstLib != NULL)
930 {
931#ifdef HAVE_TRY_EXCEPT
932 __try
933 {
934#endif
935 if (argstring != NULL)
936 {
937 /* Call with string argument */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100938 ProcAdd = (MYSTRPROCSTR)GetProcAddress(hinstLib, (LPCSTR)funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 if ((fRunTimeLinkSuccess = (ProcAdd != NULL)) != 0)
940 {
941 if (string_result == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100942 retval_int = ((MYSTRPROCINT)ProcAdd)((LPSTR)argstring);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100944 retval_str = (char_u *)(ProcAdd)((LPSTR)argstring);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 }
946 }
947 else
948 {
949 /* Call with number argument */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100950 ProcAddI = (MYINTPROCSTR) GetProcAddress(hinstLib, (LPCSTR)funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 if ((fRunTimeLinkSuccess = (ProcAddI != NULL)) != 0)
952 {
953 if (string_result == NULL)
954 retval_int = ((MYINTPROCINT)ProcAddI)(argint);
955 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100956 retval_str = (char_u *)(ProcAddI)(argint);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 }
958 }
959
960 // Save the string before we free the library.
961 // Assume that a "1" result is an illegal pointer.
962 if (string_result == NULL)
963 *number_result = retval_int;
964 else if (retval_str != NULL
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100965 && (len = check_str_len(retval_str)) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 {
967 *string_result = lalloc((long_u)len, TRUE);
968 if (*string_result != NULL)
969 mch_memmove(*string_result, retval_str, len);
970 }
971
972#ifdef HAVE_TRY_EXCEPT
973 }
974 __except(EXCEPTION_EXECUTE_HANDLER)
975 {
976 if (GetExceptionCode() == EXCEPTION_STACK_OVERFLOW)
977 RESETSTKOFLW();
978 fRunTimeLinkSuccess = 0;
979 }
980#endif
981
982 // Free the DLL module.
983 (void)FreeLibrary(hinstLib);
984 }
985
986 if (!fRunTimeLinkSuccess)
987 {
988 EMSG2(_(e_libcall), funcname);
989 return FAIL;
990 }
991
992 return OK;
993}
994#endif
995
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996/*
997 * Debugging helper: expose the MCH_WRITE_DUMP stuff to other modules
998 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000999/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 void
1001DumpPutS(
1002 const char *psz)
1003{
1004# ifdef MCH_WRITE_DUMP
1005 if (fdDump)
1006 {
1007 fputs(psz, fdDump);
1008 if (psz[strlen(psz) - 1] != '\n')
1009 fputc('\n', fdDump);
1010 fflush(fdDump);
1011 }
1012# endif
1013}
1014
1015#ifdef _DEBUG
1016
1017void __cdecl
1018Trace(
1019 char *pszFormat,
1020 ...)
1021{
1022 CHAR szBuff[2048];
1023 va_list args;
1024
1025 va_start(args, pszFormat);
1026 vsprintf(szBuff, pszFormat, args);
1027 va_end(args);
1028
1029 OutputDebugString(szBuff);
1030}
1031
1032#endif //_DEBUG
1033
Bram Moolenaar843ee412004-06-30 16:16:41 +00001034#if !defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001035# ifdef FEAT_TITLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036extern HWND g_hWnd; /* This is in os_win32.c. */
1037# endif
1038
1039/*
1040 * Showing the printer dialog is tricky since we have no GUI
1041 * window to parent it. The following routines are needed to
1042 * get the window parenting and Z-order to work properly.
1043 */
1044 static void
1045GetConsoleHwnd(void)
1046{
1047# define MY_BUFSIZE 1024 // Buffer size for console window titles.
1048
1049 char pszNewWindowTitle[MY_BUFSIZE]; // Contains fabricated WindowTitle.
1050 char pszOldWindowTitle[MY_BUFSIZE]; // Contains original WindowTitle.
1051
1052 /* Skip if it's already set. */
1053 if (s_hwnd != 0)
1054 return;
1055
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001056# ifdef FEAT_TITLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 /* Window handle may have been found by init code (Windows NT only) */
1058 if (g_hWnd != 0)
1059 {
1060 s_hwnd = g_hWnd;
1061 return;
1062 }
1063# endif
1064
1065 GetConsoleTitle(pszOldWindowTitle, MY_BUFSIZE);
1066
1067 wsprintf(pszNewWindowTitle, "%s/%d/%d",
1068 pszOldWindowTitle,
1069 GetTickCount(),
1070 GetCurrentProcessId());
1071 SetConsoleTitle(pszNewWindowTitle);
1072 Sleep(40);
1073 s_hwnd = FindWindow(NULL, pszNewWindowTitle);
1074
1075 SetConsoleTitle(pszOldWindowTitle);
1076}
Bram Moolenaar843ee412004-06-30 16:16:41 +00001077
1078/*
1079 * Console implementation of ":winpos".
1080 */
1081 int
1082mch_get_winpos(int *x, int *y)
1083{
1084 RECT rect;
1085
1086 GetConsoleHwnd();
1087 GetWindowRect(s_hwnd, &rect);
1088 *x = rect.left;
1089 *y = rect.top;
1090 return OK;
1091}
1092
1093/*
1094 * Console implementation of ":winpos x y".
1095 */
1096 void
1097mch_set_winpos(int x, int y)
1098{
1099 GetConsoleHwnd();
1100 SetWindowPos(s_hwnd, NULL, x, y, 0, 0,
1101 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
1102}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103#endif
1104
1105#if (defined(FEAT_PRINTER) && !defined(FEAT_POSTSCRIPT)) || defined(PROTO)
1106
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107/*=================================================================
1108 * Win32 printer stuff
1109 */
1110
1111static HFONT prt_font_handles[2][2][2];
1112static PRINTDLG prt_dlg;
1113static const int boldface[2] = {FW_REGULAR, FW_BOLD};
1114static TEXTMETRIC prt_tm;
1115static int prt_line_height;
1116static int prt_number_width;
1117static int prt_left_margin;
1118static int prt_right_margin;
1119static int prt_top_margin;
1120static char_u szAppName[] = TEXT("VIM");
1121static HWND hDlgPrint;
1122static int *bUserAbort = NULL;
1123static char_u *prt_name = NULL;
1124
1125/* Defines which are also in vim.rc. */
1126#define IDC_BOX1 400
1127#define IDC_PRINTTEXT1 401
1128#define IDC_PRINTTEXT2 402
1129#define IDC_PROGRESS 403
1130
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001131#if !defined(FEAT_MBYTE)
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001132# define vimSetDlgItemText(h, i, s) SetDlgItemText(h, i, s)
1133#else
1134 static BOOL
1135vimSetDlgItemText(HWND hDlg, int nIDDlgItem, char_u *s)
1136{
1137 WCHAR *wp = NULL;
1138 BOOL ret;
1139
1140 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
1141 {
1142 wp = enc_to_utf16(s, NULL);
1143 }
1144 if (wp != NULL)
1145 {
1146 ret = SetDlgItemTextW(hDlg, nIDDlgItem, wp);
1147 vim_free(wp);
1148 return ret;
1149 }
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001150 return SetDlgItemText(hDlg, nIDDlgItem, (LPCSTR)s);
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001151}
1152#endif
1153
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154/*
1155 * Convert BGR to RGB for Windows GDI calls
1156 */
1157 static COLORREF
1158swap_me(COLORREF colorref)
1159{
1160 int temp;
1161 char *ptr = (char *)&colorref;
1162
1163 temp = *(ptr);
1164 *(ptr ) = *(ptr + 2);
1165 *(ptr + 2) = temp;
1166 return colorref;
1167}
1168
Bram Moolenaared39e1d2008-08-09 17:55:22 +00001169/* Attempt to make this work for old and new compilers */
Bram Moolenaar9f733d12011-09-21 20:09:42 +02001170#if !defined(_WIN64) && (!defined(_MSC_VER) || _MSC_VER < 1300)
Bram Moolenaared39e1d2008-08-09 17:55:22 +00001171# define PDP_RETVAL BOOL
1172#else
1173# define PDP_RETVAL INT_PTR
1174#endif
1175
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001176/*ARGSUSED*/
Bram Moolenaared39e1d2008-08-09 17:55:22 +00001177 static PDP_RETVAL CALLBACK
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178PrintDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
1179{
1180#ifdef FEAT_GETTEXT
1181 NONCLIENTMETRICS nm;
1182 static HFONT hfont;
1183#endif
1184
1185 switch (message)
1186 {
1187 case WM_INITDIALOG:
1188#ifdef FEAT_GETTEXT
1189 nm.cbSize = sizeof(NONCLIENTMETRICS);
1190 if (SystemParametersInfo(
1191 SPI_GETNONCLIENTMETRICS,
1192 sizeof(NONCLIENTMETRICS),
1193 &nm,
1194 0))
1195 {
1196 char buff[MAX_PATH];
1197 int i;
1198
1199 /* Translate the dialog texts */
1200 hfont = CreateFontIndirect(&nm.lfMessageFont);
1201 for (i = IDC_PRINTTEXT1; i <= IDC_PROGRESS; i++)
1202 {
1203 SendDlgItemMessage(hDlg, i, WM_SETFONT, (WPARAM)hfont, 1);
1204 if (GetDlgItemText(hDlg,i, buff, sizeof(buff)))
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001205 vimSetDlgItemText(hDlg,i, (char_u *)_(buff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 }
1207 SendDlgItemMessage(hDlg, IDCANCEL,
1208 WM_SETFONT, (WPARAM)hfont, 1);
1209 if (GetDlgItemText(hDlg,IDCANCEL, buff, sizeof(buff)))
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001210 vimSetDlgItemText(hDlg,IDCANCEL, (char_u *)_(buff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 }
1212#endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001213 SetWindowText(hDlg, (LPCSTR)szAppName);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 if (prt_name != NULL)
1215 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001216 vimSetDlgItemText(hDlg, IDC_PRINTTEXT2, (char_u *)prt_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 vim_free(prt_name);
1218 prt_name = NULL;
1219 }
1220 EnableMenuItem(GetSystemMenu(hDlg, FALSE), SC_CLOSE, MF_GRAYED);
1221#ifndef FEAT_GUI
1222 BringWindowToTop(s_hwnd);
1223#endif
1224 return TRUE;
1225
1226 case WM_COMMAND:
1227 *bUserAbort = TRUE;
1228 EnableWindow(GetParent(hDlg), TRUE);
1229 DestroyWindow(hDlg);
1230 hDlgPrint = NULL;
1231#ifdef FEAT_GETTEXT
1232 DeleteObject(hfont);
1233#endif
1234 return TRUE;
1235 }
1236 return FALSE;
1237}
1238
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001239/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 static BOOL CALLBACK
1241AbortProc(HDC hdcPrn, int iCode)
1242{
1243 MSG msg;
1244
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02001245 while (!*bUserAbort && pPeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 {
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02001247 if (!hDlgPrint || !pIsDialogMessage(hDlgPrint, &msg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 {
1249 TranslateMessage(&msg);
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02001250 pDispatchMessage(&msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 }
1252 }
1253 return !*bUserAbort;
1254}
1255
1256#ifndef FEAT_GUI
1257
Bram Moolenaar26fdd7d2011-11-30 13:42:44 +01001258 static UINT_PTR CALLBACK
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259PrintHookProc(
1260 HWND hDlg, // handle to dialog box
1261 UINT uiMsg, // message identifier
1262 WPARAM wParam, // message parameter
1263 LPARAM lParam // message parameter
1264 )
1265{
1266 HWND hwndOwner;
1267 RECT rc, rcDlg, rcOwner;
1268 PRINTDLG *pPD;
1269
1270 if (uiMsg == WM_INITDIALOG)
1271 {
1272 // Get the owner window and dialog box rectangles.
1273 if ((hwndOwner = GetParent(hDlg)) == NULL)
1274 hwndOwner = GetDesktopWindow();
1275
1276 GetWindowRect(hwndOwner, &rcOwner);
1277 GetWindowRect(hDlg, &rcDlg);
1278 CopyRect(&rc, &rcOwner);
1279
1280 // Offset the owner and dialog box rectangles so that
1281 // right and bottom values represent the width and
1282 // height, and then offset the owner again to discard
1283 // space taken up by the dialog box.
1284
1285 OffsetRect(&rcDlg, -rcDlg.left, -rcDlg.top);
1286 OffsetRect(&rc, -rc.left, -rc.top);
1287 OffsetRect(&rc, -rcDlg.right, -rcDlg.bottom);
1288
1289 // The new position is the sum of half the remaining
1290 // space and the owner's original position.
1291
1292 SetWindowPos(hDlg,
1293 HWND_TOP,
1294 rcOwner.left + (rc.right / 2),
1295 rcOwner.top + (rc.bottom / 2),
1296 0, 0, // ignores size arguments
1297 SWP_NOSIZE);
1298
1299 /* tackle the printdlg copiesctrl problem */
1300 pPD = (PRINTDLG *)lParam;
1301 pPD->nCopies = (WORD)pPD->lCustData;
1302 SetDlgItemInt( hDlg, edt3, pPD->nCopies, FALSE );
1303 /* Bring the window to top */
1304 BringWindowToTop(GetParent(hDlg));
1305 SetForegroundWindow(hDlg);
1306 }
1307
1308 return FALSE;
1309}
1310#endif
1311
1312 void
1313mch_print_cleanup(void)
1314{
1315 int pifItalic;
1316 int pifBold;
1317 int pifUnderline;
1318
1319 for (pifBold = 0; pifBold <= 1; pifBold++)
1320 for (pifItalic = 0; pifItalic <= 1; pifItalic++)
1321 for (pifUnderline = 0; pifUnderline <= 1; pifUnderline++)
1322 DeleteObject(prt_font_handles[pifBold][pifItalic][pifUnderline]);
1323
1324 if (prt_dlg.hDC != NULL)
1325 DeleteDC(prt_dlg.hDC);
1326 if (!*bUserAbort)
1327 SendMessage(hDlgPrint, WM_COMMAND, 0, 0);
1328}
1329
1330 static int
1331to_device_units(int idx, int dpi, int physsize, int offset, int def_number)
1332{
1333 int ret = 0;
1334 int u;
1335 int nr;
1336
1337 u = prt_get_unit(idx);
1338 if (u == PRT_UNIT_NONE)
1339 {
1340 u = PRT_UNIT_PERC;
1341 nr = def_number;
1342 }
1343 else
1344 nr = printer_opts[idx].number;
1345
1346 switch (u)
1347 {
1348 case PRT_UNIT_PERC:
1349 ret = (physsize * nr) / 100;
1350 break;
1351 case PRT_UNIT_INCH:
1352 ret = (nr * dpi);
1353 break;
1354 case PRT_UNIT_MM:
1355 ret = (nr * 10 * dpi) / 254;
1356 break;
1357 case PRT_UNIT_POINT:
1358 ret = (nr * 10 * dpi) / 720;
1359 break;
1360 }
1361
1362 if (ret < offset)
1363 return 0;
1364 else
1365 return ret - offset;
1366}
1367
1368 static int
1369prt_get_cpl(void)
1370{
1371 int hr;
1372 int phyw;
1373 int dvoff;
1374 int rev_offset;
1375 int dpi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376
1377 GetTextMetrics(prt_dlg.hDC, &prt_tm);
1378 prt_line_height = prt_tm.tmHeight + prt_tm.tmExternalLeading;
1379
1380 hr = GetDeviceCaps(prt_dlg.hDC, HORZRES);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 phyw = GetDeviceCaps(prt_dlg.hDC, PHYSICALWIDTH);
1382 dvoff = GetDeviceCaps(prt_dlg.hDC, PHYSICALOFFSETX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 dpi = GetDeviceCaps(prt_dlg.hDC, LOGPIXELSX);
1384
1385 rev_offset = phyw - (dvoff + hr);
1386
1387 prt_left_margin = to_device_units(OPT_PRINT_LEFT, dpi, phyw, dvoff, 10);
1388 if (prt_use_number())
1389 {
1390 prt_number_width = PRINT_NUMBER_WIDTH * prt_tm.tmAveCharWidth;
1391 prt_left_margin += prt_number_width;
1392 }
1393 else
1394 prt_number_width = 0;
1395
1396 prt_right_margin = hr - to_device_units(OPT_PRINT_RIGHT, dpi, phyw,
1397 rev_offset, 5);
1398
1399 return (prt_right_margin - prt_left_margin) / prt_tm.tmAveCharWidth;
1400}
1401
1402 static int
1403prt_get_lpp(void)
1404{
1405 int vr;
1406 int phyw;
1407 int dvoff;
1408 int rev_offset;
1409 int bottom_margin;
1410 int dpi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411
1412 vr = GetDeviceCaps(prt_dlg.hDC, VERTRES);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 phyw = GetDeviceCaps(prt_dlg.hDC, PHYSICALHEIGHT);
1414 dvoff = GetDeviceCaps(prt_dlg.hDC, PHYSICALOFFSETY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 dpi = GetDeviceCaps(prt_dlg.hDC, LOGPIXELSY);
1416
1417 rev_offset = phyw - (dvoff + vr);
1418
1419 prt_top_margin = to_device_units(OPT_PRINT_TOP, dpi, phyw, dvoff, 5);
1420
1421 /* adjust top margin if there is a header */
1422 prt_top_margin += prt_line_height * prt_header_height();
1423
1424 bottom_margin = vr - to_device_units(OPT_PRINT_BOT, dpi, phyw,
1425 rev_offset, 5);
1426
1427 return (bottom_margin - prt_top_margin) / prt_line_height;
1428}
1429
1430 int
1431mch_print_init(prt_settings_T *psettings, char_u *jobname, int forceit)
1432{
1433 static HGLOBAL stored_dm = NULL;
1434 static HGLOBAL stored_devn = NULL;
1435 static int stored_nCopies = 1;
1436 static int stored_nFlags = 0;
1437
1438 LOGFONT fLogFont;
1439 int pifItalic;
1440 int pifBold;
1441 int pifUnderline;
1442
1443 DEVMODE *mem;
1444 DEVNAMES *devname;
1445 int i;
1446
1447 bUserAbort = &(psettings->user_abort);
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02001448 vim_memset(&prt_dlg, 0, sizeof(PRINTDLG));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 prt_dlg.lStructSize = sizeof(PRINTDLG);
1450#ifndef FEAT_GUI
1451 GetConsoleHwnd(); /* get value of s_hwnd */
1452#endif
1453 prt_dlg.hwndOwner = s_hwnd;
1454 prt_dlg.Flags = PD_NOPAGENUMS | PD_NOSELECTION | PD_RETURNDC;
1455 if (!forceit)
1456 {
1457 prt_dlg.hDevMode = stored_dm;
1458 prt_dlg.hDevNames = stored_devn;
1459 prt_dlg.lCustData = stored_nCopies; // work around bug in print dialog
1460#ifndef FEAT_GUI
1461 /*
1462 * Use hook to prevent console window being sent to back
1463 */
1464 prt_dlg.lpfnPrintHook = PrintHookProc;
1465 prt_dlg.Flags |= PD_ENABLEPRINTHOOK;
1466#endif
1467 prt_dlg.Flags |= stored_nFlags;
1468 }
1469
1470 /*
1471 * If bang present, return default printer setup with no dialog
1472 * never show dialog if we are running over telnet
1473 */
1474 if (forceit
1475#ifndef FEAT_GUI
1476 || !term_console
1477#endif
1478 )
1479 {
1480 prt_dlg.Flags |= PD_RETURNDEFAULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 /*
1482 * MSDN suggests setting the first parameter to WINSPOOL for
1483 * NT, but NULL appears to work just as well.
1484 */
1485 if (*p_pdev != NUL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001486 prt_dlg.hDC = CreateDC(NULL, (LPCSTR)p_pdev, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 {
1489 prt_dlg.Flags |= PD_RETURNDEFAULT;
1490 if (PrintDlg(&prt_dlg) == 0)
1491 goto init_fail_dlg;
1492 }
1493 }
1494 else if (PrintDlg(&prt_dlg) == 0)
1495 goto init_fail_dlg;
1496 else
1497 {
1498 /*
1499 * keep the previous driver context
1500 */
1501 stored_dm = prt_dlg.hDevMode;
1502 stored_devn = prt_dlg.hDevNames;
1503 stored_nFlags = prt_dlg.Flags;
1504 stored_nCopies = prt_dlg.nCopies;
1505 }
1506
1507 if (prt_dlg.hDC == NULL)
1508 {
1509 EMSG(_("E237: Printer selection failed"));
1510 mch_print_cleanup();
1511 return FALSE;
1512 }
1513
1514 /* Not all printer drivers report the support of color (or grey) in the
1515 * same way. Let's set has_color if there appears to be some way to print
1516 * more than B&W. */
1517 i = GetDeviceCaps(prt_dlg.hDC, NUMCOLORS);
1518 psettings->has_color = (GetDeviceCaps(prt_dlg.hDC, BITSPIXEL) > 1
1519 || GetDeviceCaps(prt_dlg.hDC, PLANES) > 1
1520 || i > 2 || i == -1);
1521
1522 /* Ensure all font styles are baseline aligned */
1523 SetTextAlign(prt_dlg.hDC, TA_BASELINE|TA_LEFT);
1524
1525 /*
1526 * On some windows systems the nCopies parameter is not
1527 * passed back correctly. It must be retrieved from the
1528 * hDevMode struct.
1529 */
1530 mem = (DEVMODE *)GlobalLock(prt_dlg.hDevMode);
1531 if (mem != NULL)
1532 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 if (mem->dmCopies != 1)
1534 stored_nCopies = mem->dmCopies;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 if ((mem->dmFields & DM_DUPLEX) && (mem->dmDuplex & ~DMDUP_SIMPLEX))
1536 psettings->duplex = TRUE;
1537 if ((mem->dmFields & DM_COLOR) && (mem->dmColor & DMCOLOR_COLOR))
1538 psettings->has_color = TRUE;
1539 }
1540 GlobalUnlock(prt_dlg.hDevMode);
1541
1542 devname = (DEVNAMES *)GlobalLock(prt_dlg.hDevNames);
1543 if (devname != 0)
1544 {
1545 char_u *printer_name = (char_u *)devname + devname->wDeviceOffset;
1546 char_u *port_name = (char_u *)devname +devname->wOutputOffset;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001547 char_u *text = (char_u *)_("to %s on %s");
Bram Moolenaarf191d552014-10-09 17:05:56 +02001548#ifdef FEAT_MBYTE
1549 char_u *printer_name_orig = printer_name;
1550 char_u *port_name_orig = port_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551
Bram Moolenaarf191d552014-10-09 17:05:56 +02001552 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
1553 {
1554 char_u *to_free = NULL;
1555 int maxlen;
1556
Bram Moolenaar861d80a2014-10-16 20:35:36 +02001557 acp_to_enc(printer_name, (int)STRLEN(printer_name), &to_free,
1558 &maxlen);
Bram Moolenaarf191d552014-10-09 17:05:56 +02001559 if (to_free != NULL)
1560 printer_name = to_free;
Bram Moolenaar861d80a2014-10-16 20:35:36 +02001561 acp_to_enc(port_name, (int)STRLEN(port_name), &to_free, &maxlen);
Bram Moolenaarf191d552014-10-09 17:05:56 +02001562 if (to_free != NULL)
1563 port_name = to_free;
1564 }
1565#endif
Bram Moolenaare6a91fd2008-07-24 18:51:11 +00001566 prt_name = alloc((unsigned)(STRLEN(printer_name) + STRLEN(port_name)
1567 + STRLEN(text)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 if (prt_name != NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001569 wsprintf((char *)prt_name, (const char *)text,
1570 printer_name, port_name);
Bram Moolenaarf191d552014-10-09 17:05:56 +02001571#ifdef FEAT_MBYTE
1572 if (printer_name != printer_name_orig)
1573 vim_free(printer_name);
1574 if (port_name != port_name_orig)
1575 vim_free(port_name);
1576#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 }
1578 GlobalUnlock(prt_dlg.hDevNames);
1579
1580 /*
1581 * Initialise the font according to 'printfont'
1582 */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02001583 vim_memset(&fLogFont, 0, sizeof(fLogFont));
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001584 if (get_logfont(&fLogFont, p_pfn, prt_dlg.hDC, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 {
1586 EMSG2(_("E613: Unknown printer font: %s"), p_pfn);
1587 mch_print_cleanup();
1588 return FALSE;
1589 }
1590
1591 for (pifBold = 0; pifBold <= 1; pifBold++)
1592 for (pifItalic = 0; pifItalic <= 1; pifItalic++)
1593 for (pifUnderline = 0; pifUnderline <= 1; pifUnderline++)
1594 {
1595 fLogFont.lfWeight = boldface[pifBold];
1596 fLogFont.lfItalic = pifItalic;
1597 fLogFont.lfUnderline = pifUnderline;
1598 prt_font_handles[pifBold][pifItalic][pifUnderline]
1599 = CreateFontIndirect(&fLogFont);
1600 }
1601
1602 SetBkMode(prt_dlg.hDC, OPAQUE);
1603 SelectObject(prt_dlg.hDC, prt_font_handles[0][0][0]);
1604
1605 /*
1606 * Fill in the settings struct
1607 */
1608 psettings->chars_per_line = prt_get_cpl();
1609 psettings->lines_per_page = prt_get_lpp();
Bram Moolenaar7ddc6422014-09-27 11:18:19 +02001610 if (prt_dlg.Flags & PD_USEDEVMODECOPIESANDCOLLATE)
1611 {
1612 psettings->n_collated_copies = (prt_dlg.Flags & PD_COLLATE)
1613 ? prt_dlg.nCopies : 1;
1614 psettings->n_uncollated_copies = (prt_dlg.Flags & PD_COLLATE)
1615 ? 1 : prt_dlg.nCopies;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616
Bram Moolenaar7ddc6422014-09-27 11:18:19 +02001617 if (psettings->n_collated_copies == 0)
1618 psettings->n_collated_copies = 1;
1619
1620 if (psettings->n_uncollated_copies == 0)
1621 psettings->n_uncollated_copies = 1;
1622 } else {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 psettings->n_collated_copies = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 psettings->n_uncollated_copies = 1;
Bram Moolenaar7ddc6422014-09-27 11:18:19 +02001625 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626
1627 psettings->jobname = jobname;
1628
1629 return TRUE;
1630
1631init_fail_dlg:
1632 {
1633 DWORD err = CommDlgExtendedError();
1634
1635 if (err)
1636 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637 char_u *buf;
1638
1639 /* I suspect FormatMessage() doesn't work for values returned by
1640 * CommDlgExtendedError(). What does? */
1641 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
1642 FORMAT_MESSAGE_FROM_SYSTEM |
1643 FORMAT_MESSAGE_IGNORE_INSERTS,
1644 NULL, err, 0, (LPTSTR)(&buf), 0, NULL);
1645 EMSG2(_("E238: Print error: %s"),
1646 buf == NULL ? (char_u *)_("Unknown") : buf);
1647 LocalFree((LPVOID)(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 }
1649 else
1650 msg_clr_eos(); /* Maybe canceled */
1651
1652 mch_print_cleanup();
1653 return FALSE;
1654 }
1655}
1656
1657
1658 int
1659mch_print_begin(prt_settings_T *psettings)
1660{
1661 int ret;
1662 static DOCINFO di;
1663 char szBuffer[300];
1664
1665 hDlgPrint = CreateDialog(GetModuleHandle(NULL), TEXT("PrintDlgBox"),
1666 prt_dlg.hwndOwner, PrintDlgProc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 SetAbortProc(prt_dlg.hDC, AbortProc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 wsprintf(szBuffer, _("Printing '%s'"), gettail(psettings->jobname));
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001669 vimSetDlgItemText(hDlgPrint, IDC_PRINTTEXT1, (char_u *)szBuffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02001671 vim_memset(&di, 0, sizeof(DOCINFO));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672 di.cbSize = sizeof(DOCINFO);
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001673 di.lpszDocName = (LPCSTR)psettings->jobname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 ret = StartDoc(prt_dlg.hDC, &di);
1675
1676#ifdef FEAT_GUI
1677 /* Give focus back to main window (when using MDI). */
1678 SetFocus(s_hwnd);
1679#endif
1680
1681 return (ret > 0);
1682}
1683
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001684/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 void
1686mch_print_end(prt_settings_T *psettings)
1687{
1688 EndDoc(prt_dlg.hDC);
1689 if (!*bUserAbort)
1690 SendMessage(hDlgPrint, WM_COMMAND, 0, 0);
1691}
1692
1693 int
1694mch_print_end_page(void)
1695{
1696 return (EndPage(prt_dlg.hDC) > 0);
1697}
1698
1699 int
1700mch_print_begin_page(char_u *msg)
1701{
1702 if (msg != NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001703 vimSetDlgItemText(hDlgPrint, IDC_PROGRESS, msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704 return (StartPage(prt_dlg.hDC) > 0);
1705}
1706
1707 int
1708mch_print_blank_page(void)
1709{
1710 return (mch_print_begin_page(NULL) ? (mch_print_end_page()) : FALSE);
1711}
1712
1713static int prt_pos_x = 0;
1714static int prt_pos_y = 0;
1715
1716 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001717mch_print_start_line(int margin, int page_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718{
1719 if (margin)
1720 prt_pos_x = -prt_number_width;
1721 else
1722 prt_pos_x = 0;
1723 prt_pos_y = page_line * prt_line_height
1724 + prt_tm.tmAscent + prt_tm.tmExternalLeading;
1725}
1726
1727 int
1728mch_print_text_out(char_u *p, int len)
1729{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001730#if defined(FEAT_PROPORTIONAL_FONTS) || defined(FEAT_MBYTE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 SIZE sz;
1732#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001733#if defined(FEAT_MBYTE)
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001734 WCHAR *wp = NULL;
1735 int wlen = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001737 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
1738 {
1739 wp = enc_to_utf16(p, &wlen);
1740 }
1741 if (wp != NULL)
1742 {
1743 int ret = FALSE;
1744
1745 TextOutW(prt_dlg.hDC, prt_pos_x + prt_left_margin,
1746 prt_pos_y + prt_top_margin, wp, wlen);
1747 GetTextExtentPoint32W(prt_dlg.hDC, wp, wlen, &sz);
1748 vim_free(wp);
1749 prt_pos_x += (sz.cx - prt_tm.tmOverhang);
1750 /* This is wrong when printing spaces for a TAB. */
1751 if (p[len] != NUL)
1752 {
1753 wlen = MB_PTR2LEN(p + len);
1754 wp = enc_to_utf16(p + len, &wlen);
1755 if (wp != NULL)
1756 {
1757 GetTextExtentPoint32W(prt_dlg.hDC, wp, 1, &sz);
1758 ret = (prt_pos_x + prt_left_margin + sz.cx > prt_right_margin);
1759 vim_free(wp);
1760 }
1761 }
1762 return ret;
1763 }
1764#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 TextOut(prt_dlg.hDC, prt_pos_x + prt_left_margin,
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001766 prt_pos_y + prt_top_margin,
1767 (LPCSTR)p, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768#ifndef FEAT_PROPORTIONAL_FONTS
1769 prt_pos_x += len * prt_tm.tmAveCharWidth;
1770 return (prt_pos_x + prt_left_margin + prt_tm.tmAveCharWidth
1771 + prt_tm.tmOverhang > prt_right_margin);
1772#else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001773 GetTextExtentPoint32(prt_dlg.hDC, (LPCSTR)p, len, &sz);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 prt_pos_x += (sz.cx - prt_tm.tmOverhang);
1775 /* This is wrong when printing spaces for a TAB. */
1776 if (p[len] == NUL)
1777 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 GetTextExtentPoint32(prt_dlg.hDC, p + len, 1, &sz);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 return (prt_pos_x + prt_left_margin + sz.cx > prt_right_margin);
1780#endif
1781}
1782
1783 void
1784mch_print_set_font(int iBold, int iItalic, int iUnderline)
1785{
1786 SelectObject(prt_dlg.hDC, prt_font_handles[iBold][iItalic][iUnderline]);
1787}
1788
1789 void
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001790mch_print_set_bg(long_u bgcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791{
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001792 SetBkColor(prt_dlg.hDC, GetNearestColor(prt_dlg.hDC,
1793 swap_me((COLORREF)bgcol)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 /*
1795 * With a white background we can draw characters transparent, which is
1796 * good for italic characters that overlap to the next char cell.
1797 */
1798 if (bgcol == 0xffffffUL)
1799 SetBkMode(prt_dlg.hDC, TRANSPARENT);
1800 else
1801 SetBkMode(prt_dlg.hDC, OPAQUE);
1802}
1803
1804 void
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001805mch_print_set_fg(long_u fgcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806{
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001807 SetTextColor(prt_dlg.hDC, GetNearestColor(prt_dlg.hDC,
1808 swap_me((COLORREF)fgcol)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809}
1810
1811#endif /*FEAT_PRINTER && !FEAT_POSTSCRIPT*/
1812
Bram Moolenaar58d98232005-07-23 22:25:46 +00001813
1814
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815#if defined(FEAT_SHORTCUT) || defined(PROTO)
Bram Moolenaar82881492012-11-20 16:53:39 +01001816# ifndef PROTO
1817# include <shlobj.h>
1818# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819
1820/*
1821 * When "fname" is the name of a shortcut (*.lnk) resolve the file it points
1822 * to and return that name in allocated memory.
1823 * Otherwise NULL is returned.
1824 */
1825 char_u *
1826mch_resolve_shortcut(char_u *fname)
1827{
1828 HRESULT hr;
1829 IShellLink *psl = NULL;
1830 IPersistFile *ppf = NULL;
1831 OLECHAR wsz[MAX_PATH];
1832 WIN32_FIND_DATA ffd; // we get those free of charge
Bram Moolenaar604729e2013-08-30 16:44:19 +02001833 CHAR buf[MAX_PATH]; // could have simply reused 'wsz'...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 char_u *rfname = NULL;
1835 int len;
Bram Moolenaar604729e2013-08-30 16:44:19 +02001836# ifdef FEAT_MBYTE
1837 IShellLinkW *pslw = NULL;
1838 WIN32_FIND_DATAW ffdw; // we get those free of charge
1839# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840
1841 /* Check if the file name ends in ".lnk". Avoid calling
1842 * CoCreateInstance(), it's quite slow. */
1843 if (fname == NULL)
1844 return rfname;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001845 len = (int)STRLEN(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 if (len <= 4 || STRNICMP(fname + len - 4, ".lnk", 4) != 0)
1847 return rfname;
1848
1849 CoInitialize(NULL);
1850
Bram Moolenaar604729e2013-08-30 16:44:19 +02001851# ifdef FEAT_MBYTE
1852 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
1853 {
1854 // create a link manager object and request its interface
1855 hr = CoCreateInstance(
1856 &CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
1857 &IID_IShellLinkW, (void**)&pslw);
1858 if (hr == S_OK)
1859 {
1860 WCHAR *p = enc_to_utf16(fname, NULL);
1861
1862 if (p != NULL)
1863 {
1864 // Get a pointer to the IPersistFile interface.
1865 hr = pslw->lpVtbl->QueryInterface(
1866 pslw, &IID_IPersistFile, (void**)&ppf);
1867 if (hr != S_OK)
1868 goto shortcut_errorw;
1869
1870 // "load" the name and resolve the link
1871 hr = ppf->lpVtbl->Load(ppf, p, STGM_READ);
1872 if (hr != S_OK)
1873 goto shortcut_errorw;
1874# if 0 // This makes Vim wait a long time if the target does not exist.
1875 hr = pslw->lpVtbl->Resolve(pslw, NULL, SLR_NO_UI);
1876 if (hr != S_OK)
1877 goto shortcut_errorw;
1878# endif
1879
1880 // Get the path to the link target.
1881 ZeroMemory(wsz, MAX_PATH * sizeof(WCHAR));
1882 hr = pslw->lpVtbl->GetPath(pslw, wsz, MAX_PATH, &ffdw, 0);
1883 if (hr == S_OK && wsz[0] != NUL)
1884 rfname = utf16_to_enc(wsz, NULL);
1885
1886shortcut_errorw:
1887 vim_free(p);
Bram Moolenaarcd981f22014-02-11 17:06:00 +01001888 goto shortcut_end;
Bram Moolenaar604729e2013-08-30 16:44:19 +02001889 }
1890 }
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001891 goto shortcut_end;
Bram Moolenaar604729e2013-08-30 16:44:19 +02001892 }
1893# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 // create a link manager object and request its interface
1895 hr = CoCreateInstance(
1896 &CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
1897 &IID_IShellLink, (void**)&psl);
1898 if (hr != S_OK)
Bram Moolenaar604729e2013-08-30 16:44:19 +02001899 goto shortcut_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900
1901 // Get a pointer to the IPersistFile interface.
1902 hr = psl->lpVtbl->QueryInterface(
1903 psl, &IID_IPersistFile, (void**)&ppf);
1904 if (hr != S_OK)
Bram Moolenaar604729e2013-08-30 16:44:19 +02001905 goto shortcut_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906
1907 // full path string must be in Unicode.
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001908 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)fname, -1, wsz, MAX_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00001910 // "load" the name and resolve the link
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 hr = ppf->lpVtbl->Load(ppf, wsz, STGM_READ);
1912 if (hr != S_OK)
Bram Moolenaar604729e2013-08-30 16:44:19 +02001913 goto shortcut_end;
1914# if 0 // This makes Vim wait a long time if the target doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 hr = psl->lpVtbl->Resolve(psl, NULL, SLR_NO_UI);
1916 if (hr != S_OK)
Bram Moolenaar604729e2013-08-30 16:44:19 +02001917 goto shortcut_end;
1918# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919
1920 // Get the path to the link target.
1921 ZeroMemory(buf, MAX_PATH);
1922 hr = psl->lpVtbl->GetPath(psl, buf, MAX_PATH, &ffd, 0);
1923 if (hr == S_OK && buf[0] != NUL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001924 rfname = vim_strsave((char_u *)buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925
Bram Moolenaar604729e2013-08-30 16:44:19 +02001926shortcut_end:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 // Release all interface pointers (both belong to the same object)
1928 if (ppf != NULL)
1929 ppf->lpVtbl->Release(ppf);
1930 if (psl != NULL)
1931 psl->lpVtbl->Release(psl);
Bram Moolenaar604729e2013-08-30 16:44:19 +02001932# ifdef FEAT_MBYTE
1933 if (pslw != NULL)
1934 pslw->lpVtbl->Release(pslw);
1935# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936
1937 CoUninitialize();
1938 return rfname;
1939}
1940#endif
1941
1942#if (defined(FEAT_EVAL) && !defined(FEAT_GUI)) || defined(PROTO)
1943/*
1944 * Bring ourselves to the foreground. Does work if the OS doesn't allow it.
1945 */
1946 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001947win32_set_foreground(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948{
1949# ifndef FEAT_GUI
1950 GetConsoleHwnd(); /* get value of s_hwnd */
1951# endif
1952 if (s_hwnd != 0)
1953 SetForegroundWindow(s_hwnd);
1954}
1955#endif
1956
1957#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
1958/*
1959 * Client-server code for Vim
1960 *
1961 * Originally written by Paul Moore
1962 */
1963
1964/* In order to handle inter-process messages, we need to have a window. But
1965 * the functions in this module can be called before the main GUI window is
1966 * created (and may also be called in the console version, where there is no
1967 * GUI window at all).
1968 *
1969 * So we create a hidden window, and arrange to destroy it on exit.
1970 */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001971HWND message_window = 0; /* window that's handling messages */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972
1973#define VIM_CLASSNAME "VIM_MESSAGES"
1974#define VIM_CLASSNAME_LEN (sizeof(VIM_CLASSNAME) - 1)
1975
1976/* Communication is via WM_COPYDATA messages. The message type is send in
1977 * the dwData parameter. Types are defined here. */
1978#define COPYDATA_KEYS 0
1979#define COPYDATA_REPLY 1
1980#define COPYDATA_EXPR 10
1981#define COPYDATA_RESULT 11
1982#define COPYDATA_ERROR_RESULT 12
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001983#define COPYDATA_ENCODING 20
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984
1985/* This is a structure containing a server HWND and its name. */
1986struct server_id
1987{
1988 HWND hwnd;
1989 char_u *name;
1990};
1991
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001992/* Last received 'encoding' that the client uses. */
1993static char_u *client_enc = NULL;
1994
1995/*
1996 * Tell the other side what encoding we are using.
1997 * Errors are ignored.
1998 */
1999 static void
2000serverSendEnc(HWND target)
2001{
2002 COPYDATASTRUCT data;
2003
2004 data.dwData = COPYDATA_ENCODING;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002005#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002006 data.cbData = (DWORD)STRLEN(p_enc) + 1;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002007 data.lpData = p_enc;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002008#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +02002009 data.cbData = (DWORD)STRLEN("latin1") + 1;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002010 data.lpData = "latin1";
2011#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002012 (void)SendMessage(target, WM_COPYDATA, (WPARAM)message_window,
2013 (LPARAM)(&data));
2014}
2015
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016/*
2017 * Clean up on exit. This destroys the hidden message window.
2018 */
2019 static void
2020#ifdef __BORLANDC__
2021 _RTLENTRYF
2022#endif
2023CleanUpMessaging(void)
2024{
2025 if (message_window != 0)
2026 {
2027 DestroyWindow(message_window);
2028 message_window = 0;
2029 }
2030}
2031
2032static int save_reply(HWND server, char_u *reply, int expr);
2033
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002034/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 * The window procedure for the hidden message window.
2036 * It handles callback messages and notifications from servers.
2037 * In order to process these messages, it is necessary to run a
2038 * message loop. Code which may run before the main message loop
2039 * is started (in the GUI) is careful to pump messages when it needs
2040 * to. Features which require message delivery during normal use will
2041 * not work in the console version - this basically means those
2042 * features which allow Vim to act as a server, rather than a client.
2043 */
2044 static LRESULT CALLBACK
2045Messaging_WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
2046{
2047 if (msg == WM_COPYDATA)
2048 {
2049 /* This is a message from another Vim. The dwData member of the
2050 * COPYDATASTRUCT determines the type of message:
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002051 * COPYDATA_ENCODING:
2052 * The encoding that the client uses. Following messages will
2053 * use this encoding, convert if needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 * COPYDATA_KEYS:
2055 * A key sequence. We are a server, and a client wants these keys
2056 * adding to the input queue.
2057 * COPYDATA_REPLY:
2058 * A reply. We are a client, and a server has sent this message
2059 * in response to a request. (server2client())
2060 * COPYDATA_EXPR:
2061 * An expression. We are a server, and a client wants us to
2062 * evaluate this expression.
2063 * COPYDATA_RESULT:
2064 * A reply. We are a client, and a server has sent this message
2065 * in response to a COPYDATA_EXPR.
2066 * COPYDATA_ERROR_RESULT:
2067 * A reply. We are a client, and a server has sent this message
2068 * in response to a COPYDATA_EXPR that failed to evaluate.
2069 */
2070 COPYDATASTRUCT *data = (COPYDATASTRUCT*)lParam;
2071 HWND sender = (HWND)wParam;
2072 COPYDATASTRUCT reply;
2073 char_u *res;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 int retval;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002075 char_u *str;
2076 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077
2078 switch (data->dwData)
2079 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002080 case COPYDATA_ENCODING:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002081# ifdef FEAT_MBYTE
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002082 /* Remember the encoding that the client uses. */
2083 vim_free(client_enc);
2084 client_enc = enc_canonize((char_u *)data->lpData);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002085# endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002086 return 1;
2087
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 case COPYDATA_KEYS:
2089 /* Remember who sent this, for <client> */
2090 clientWindow = sender;
2091
2092 /* Add the received keys to the input buffer. The loop waiting
2093 * for the user to do something should check the input buffer. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002094 str = serverConvert(client_enc, (char_u *)data->lpData, &tofree);
2095 server_to_input_buf(str);
2096 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097
2098# ifdef FEAT_GUI
2099 /* Wake up the main GUI loop. */
2100 if (s_hwnd != 0)
2101 PostMessage(s_hwnd, WM_NULL, 0, 0);
2102# endif
2103 return 1;
2104
2105 case COPYDATA_EXPR:
2106 /* Remember who sent this, for <client> */
2107 clientWindow = sender;
2108
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002109 str = serverConvert(client_enc, (char_u *)data->lpData, &tofree);
2110 res = eval_client_expr_to_string(str);
2111 vim_free(tofree);
2112
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 if (res == NULL)
2114 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002115 res = vim_strsave((char_u *)_(e_invexprmsg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 reply.dwData = COPYDATA_ERROR_RESULT;
2117 }
2118 else
2119 reply.dwData = COPYDATA_RESULT;
2120 reply.lpData = res;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002121 reply.cbData = (DWORD)STRLEN(res) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002123 serverSendEnc(sender);
Bram Moolenaar5246cd72013-06-16 16:41:47 +02002124 retval = (int)SendMessage(sender, WM_COPYDATA,
2125 (WPARAM)message_window, (LPARAM)(&reply));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 vim_free(res);
2127 return retval;
2128
2129 case COPYDATA_REPLY:
2130 case COPYDATA_RESULT:
2131 case COPYDATA_ERROR_RESULT:
2132 if (data->lpData != NULL)
2133 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002134 str = serverConvert(client_enc, (char_u *)data->lpData,
2135 &tofree);
2136 if (tofree == NULL)
2137 str = vim_strsave(str);
2138 if (save_reply(sender, str,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 (data->dwData == COPYDATA_REPLY ? 0 :
2140 (data->dwData == COPYDATA_RESULT ? 1 :
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002141 2))) == FAIL)
2142 vim_free(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143#ifdef FEAT_AUTOCMD
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002144 else if (data->dwData == COPYDATA_REPLY)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 {
Bram Moolenaar427d51c2013-06-16 16:01:25 +02002146 char_u winstr[30];
2147
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002148 sprintf((char *)winstr, PRINTF_HEX_LONG_U, (long_u)sender);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002149 apply_autocmds(EVENT_REMOTEREPLY, winstr, str,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 TRUE, curbuf);
2151 }
2152#endif
2153 }
2154 return 1;
2155 }
2156
2157 return 0;
2158 }
2159
2160 else if (msg == WM_ACTIVATE && wParam == WA_ACTIVE)
2161 {
2162 /* When the message window is activated (brought to the foreground),
2163 * this actually applies to the text window. */
2164#ifndef FEAT_GUI
2165 GetConsoleHwnd(); /* get value of s_hwnd */
2166#endif
2167 if (s_hwnd != 0)
2168 {
2169 SetForegroundWindow(s_hwnd);
2170 return 0;
2171 }
2172 }
2173
2174 return DefWindowProc(hwnd, msg, wParam, lParam);
2175}
2176
2177/*
2178 * Initialise the message handling process. This involves creating a window
2179 * to handle messages - the window will not be visible.
2180 */
2181 void
2182serverInitMessaging(void)
2183{
2184 WNDCLASS wndclass;
2185 HINSTANCE s_hinst;
2186
2187 /* Clean up on exit */
2188 atexit(CleanUpMessaging);
2189
2190 /* Register a window class - we only really care
2191 * about the window procedure
2192 */
2193 s_hinst = (HINSTANCE)GetModuleHandle(0);
2194 wndclass.style = 0;
2195 wndclass.lpfnWndProc = Messaging_WndProc;
2196 wndclass.cbClsExtra = 0;
2197 wndclass.cbWndExtra = 0;
2198 wndclass.hInstance = s_hinst;
2199 wndclass.hIcon = NULL;
2200 wndclass.hCursor = NULL;
2201 wndclass.hbrBackground = NULL;
2202 wndclass.lpszMenuName = NULL;
2203 wndclass.lpszClassName = VIM_CLASSNAME;
2204 RegisterClass(&wndclass);
2205
2206 /* Create the message window. It will be hidden, so the details don't
2207 * matter. Don't use WS_OVERLAPPEDWINDOW, it will make a shortcut remove
2208 * focus from gvim. */
2209 message_window = CreateWindow(VIM_CLASSNAME, "",
2210 WS_POPUPWINDOW | WS_CAPTION,
2211 CW_USEDEFAULT, CW_USEDEFAULT,
2212 100, 100, NULL, NULL,
2213 s_hinst, NULL);
2214}
2215
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002216/* Used by serverSendToVim() to find an alternate server name. */
2217static char_u *altname_buf_ptr = NULL;
2218
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219/*
2220 * Get the title of the window "hwnd", which is the Vim server name, in
2221 * "name[namelen]" and return the length.
2222 * Returns zero if window "hwnd" is not a Vim server.
2223 */
2224 static int
2225getVimServerName(HWND hwnd, char *name, int namelen)
2226{
2227 int len;
2228 char buffer[VIM_CLASSNAME_LEN + 1];
2229
2230 /* Ignore windows which aren't Vim message windows */
2231 len = GetClassName(hwnd, buffer, sizeof(buffer));
2232 if (len != VIM_CLASSNAME_LEN || STRCMP(buffer, VIM_CLASSNAME) != 0)
2233 return 0;
2234
2235 /* Get the title of the window */
2236 return GetWindowText(hwnd, name, namelen);
2237}
2238
2239 static BOOL CALLBACK
2240enumWindowsGetServer(HWND hwnd, LPARAM lparam)
2241{
2242 struct server_id *id = (struct server_id *)lparam;
2243 char server[MAX_PATH];
2244
2245 /* Get the title of the window */
2246 if (getVimServerName(hwnd, server, sizeof(server)) == 0)
2247 return TRUE;
2248
2249 /* If this is the server we're looking for, return its HWND */
2250 if (STRICMP(server, id->name) == 0)
2251 {
2252 id->hwnd = hwnd;
2253 return FALSE;
2254 }
2255
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002256 /* If we are looking for an alternate server, remember this name. */
2257 if (altname_buf_ptr != NULL
2258 && STRNICMP(server, id->name, STRLEN(id->name)) == 0
2259 && vim_isdigit(server[STRLEN(id->name)]))
2260 {
2261 STRCPY(altname_buf_ptr, server);
2262 altname_buf_ptr = NULL; /* don't use another name */
2263 }
2264
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 /* Otherwise, keep looking */
2266 return TRUE;
2267}
2268
2269 static BOOL CALLBACK
2270enumWindowsGetNames(HWND hwnd, LPARAM lparam)
2271{
2272 garray_T *ga = (garray_T *)lparam;
2273 char server[MAX_PATH];
2274
2275 /* Get the title of the window */
2276 if (getVimServerName(hwnd, server, sizeof(server)) == 0)
2277 return TRUE;
2278
2279 /* Add the name to the list */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002280 ga_concat(ga, (char_u *)server);
2281 ga_concat(ga, (char_u *)"\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 return TRUE;
2283}
2284
2285 static HWND
2286findServer(char_u *name)
2287{
2288 struct server_id id;
2289
2290 id.name = name;
2291 id.hwnd = 0;
2292
2293 EnumWindows(enumWindowsGetServer, (LPARAM)(&id));
2294
2295 return id.hwnd;
2296}
2297
2298 void
2299serverSetName(char_u *name)
2300{
2301 char_u *ok_name;
2302 HWND hwnd = 0;
2303 int i = 0;
2304 char_u *p;
2305
2306 /* Leave enough space for a 9-digit suffix to ensure uniqueness! */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002307 ok_name = alloc((unsigned)STRLEN(name) + 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308
2309 STRCPY(ok_name, name);
2310 p = ok_name + STRLEN(name);
2311
2312 for (;;)
2313 {
2314 /* This is inefficient - we're doing an EnumWindows loop for each
2315 * possible name. It would be better to grab all names in one go,
2316 * and scan the list each time...
2317 */
2318 hwnd = findServer(ok_name);
2319 if (hwnd == 0)
2320 break;
2321
2322 ++i;
2323 if (i >= 1000)
2324 break;
2325
2326 sprintf((char *)p, "%d", i);
2327 }
2328
2329 if (hwnd != 0)
2330 vim_free(ok_name);
2331 else
2332 {
2333 /* Remember the name */
2334 serverName = ok_name;
2335#ifdef FEAT_TITLE
2336 need_maketitle = TRUE; /* update Vim window title later */
2337#endif
2338
2339 /* Update the message window title */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002340 SetWindowText(message_window, (LPCSTR)ok_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341
2342#ifdef FEAT_EVAL
2343 /* Set the servername variable */
2344 set_vim_var_string(VV_SEND_SERVER, serverName, -1);
2345#endif
2346 }
2347}
2348
2349 char_u *
2350serverGetVimNames(void)
2351{
2352 garray_T ga;
2353
2354 ga_init2(&ga, 1, 100);
2355
2356 EnumWindows(enumWindowsGetNames, (LPARAM)(&ga));
Bram Moolenaar269ec652004-07-29 08:43:53 +00002357 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358
2359 return ga.ga_data;
2360}
2361
2362 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002363serverSendReply(
2364 char_u *name, /* Where to send. */
2365 char_u *reply) /* What to send. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366{
2367 HWND target;
2368 COPYDATASTRUCT data;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002369 long_u n = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370
2371 /* The "name" argument is a magic cookie obtained from expand("<client>").
2372 * It should be of the form 0xXXXXX - i.e. a C hex literal, which is the
2373 * value of the client's message window HWND.
2374 */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002375 sscanf((char *)name, SCANF_HEX_LONG_U, &n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 if (n == 0)
2377 return -1;
2378
2379 target = (HWND)n;
2380 if (!IsWindow(target))
2381 return -1;
2382
2383 data.dwData = COPYDATA_REPLY;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002384 data.cbData = (DWORD)STRLEN(reply) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 data.lpData = reply;
2386
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002387 serverSendEnc(target);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 if (SendMessage(target, WM_COPYDATA, (WPARAM)message_window,
2389 (LPARAM)(&data)))
2390 return 0;
2391
2392 return -1;
2393}
2394
2395 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002396serverSendToVim(
2397 char_u *name, /* Where to send. */
2398 char_u *cmd, /* What to send. */
2399 char_u **result, /* Result of eval'ed expression */
2400 void *ptarget, /* HWND of server */
2401 int asExpr, /* Expression or keys? */
2402 int silent) /* don't complain about no server */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403{
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002404 HWND target;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 COPYDATASTRUCT data;
2406 char_u *retval = NULL;
2407 int retcode = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002408 char_u altname_buf[MAX_PATH];
2409
2410 /* If the server name does not end in a digit then we look for an
2411 * alternate name. e.g. when "name" is GVIM the we may find GVIM2. */
2412 if (STRLEN(name) > 1 && !vim_isdigit(name[STRLEN(name) - 1]))
2413 altname_buf_ptr = altname_buf;
2414 altname_buf[0] = NUL;
2415 target = findServer(name);
2416 altname_buf_ptr = NULL;
2417 if (target == 0 && altname_buf[0] != NUL)
2418 /* Use another server name we found. */
2419 target = findServer(altname_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420
2421 if (target == 0)
2422 {
2423 if (!silent)
2424 EMSG2(_(e_noserver), name);
2425 return -1;
2426 }
2427
2428 if (ptarget)
2429 *(HWND *)ptarget = target;
2430
2431 data.dwData = asExpr ? COPYDATA_EXPR : COPYDATA_KEYS;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002432 data.cbData = (DWORD)STRLEN(cmd) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 data.lpData = cmd;
2434
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002435 serverSendEnc(target);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 if (SendMessage(target, WM_COPYDATA, (WPARAM)message_window,
2437 (LPARAM)(&data)) == 0)
2438 return -1;
2439
2440 if (asExpr)
2441 retval = serverGetReply(target, &retcode, TRUE, TRUE);
2442
2443 if (result == NULL)
2444 vim_free(retval);
2445 else
2446 *result = retval; /* Caller assumes responsibility for freeing */
2447
2448 return retcode;
2449}
2450
2451/*
2452 * Bring the server to the foreground.
2453 */
2454 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002455serverForeground(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456{
2457 HWND target = findServer(name);
2458
2459 if (target != 0)
2460 SetForegroundWindow(target);
2461}
2462
2463/* Replies from server need to be stored until the client picks them up via
2464 * remote_read(). So we maintain a list of server-id/reply pairs.
2465 * Note that there could be multiple replies from one server pending if the
2466 * client is slow picking them up.
2467 * We just store the replies in a simple list. When we remove an entry, we
2468 * move list entries down to fill the gap.
2469 * The server ID is simply the HWND.
2470 */
2471typedef struct
2472{
2473 HWND server; /* server window */
2474 char_u *reply; /* reply string */
2475 int expr_result; /* 0 for REPLY, 1 for RESULT 2 for error */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002476} reply_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477
2478static garray_T reply_list = {0, 0, sizeof(reply_T), 5, 0};
2479
2480#define REPLY_ITEM(i) ((reply_T *)(reply_list.ga_data) + (i))
2481#define REPLY_COUNT (reply_list.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482
2483/* Flag which is used to wait for a reply */
2484static int reply_received = 0;
2485
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002486/*
2487 * Store a reply. "reply" must be allocated memory (or NULL).
2488 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 static int
2490save_reply(HWND server, char_u *reply, int expr)
2491{
2492 reply_T *rep;
2493
2494 if (ga_grow(&reply_list, 1) == FAIL)
2495 return FAIL;
2496
2497 rep = REPLY_ITEM(REPLY_COUNT);
2498 rep->server = server;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002499 rep->reply = reply;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 rep->expr_result = expr;
2501 if (rep->reply == NULL)
2502 return FAIL;
2503
2504 ++REPLY_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 reply_received = 1;
2506 return OK;
2507}
2508
2509/*
2510 * Get a reply from server "server".
2511 * When "expr_res" is non NULL, get the result of an expression, otherwise a
2512 * server2client() message.
2513 * When non NULL, point to return code. 0 => OK, -1 => ERROR
2514 * If "remove" is TRUE, consume the message, the caller must free it then.
2515 * if "wait" is TRUE block until a message arrives (or the server exits).
2516 */
2517 char_u *
2518serverGetReply(HWND server, int *expr_res, int remove, int wait)
2519{
2520 int i;
2521 char_u *reply;
2522 reply_T *rep;
2523
2524 /* When waiting, loop until the message waiting for is received. */
2525 for (;;)
2526 {
2527 /* Reset this here, in case a message arrives while we are going
2528 * through the already received messages. */
2529 reply_received = 0;
2530
2531 for (i = 0; i < REPLY_COUNT; ++i)
2532 {
2533 rep = REPLY_ITEM(i);
2534 if (rep->server == server
2535 && ((rep->expr_result != 0) == (expr_res != NULL)))
2536 {
2537 /* Save the values we've found for later */
2538 reply = rep->reply;
2539 if (expr_res != NULL)
2540 *expr_res = rep->expr_result == 1 ? 0 : -1;
2541
2542 if (remove)
2543 {
2544 /* Move the rest of the list down to fill the gap */
2545 mch_memmove(rep, rep + 1,
2546 (REPLY_COUNT - i - 1) * sizeof(reply_T));
2547 --REPLY_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 }
2549
2550 /* Return the reply to the caller, who takes on responsibility
2551 * for freeing it if "remove" is TRUE. */
2552 return reply;
2553 }
2554 }
2555
2556 /* If we got here, we didn't find a reply. Return immediately if the
2557 * "wait" parameter isn't set. */
2558 if (!wait)
2559 break;
2560
2561 /* We need to wait for a reply. Enter a message loop until the
2562 * "reply_received" flag gets set. */
2563
2564 /* Loop until we receive a reply */
2565 while (reply_received == 0)
2566 {
2567 /* Wait for a SendMessage() call to us. This could be the reply
2568 * we are waiting for. Use a timeout of a second, to catch the
2569 * situation that the server died unexpectedly. */
2570 MsgWaitForMultipleObjects(0, NULL, TRUE, 1000, QS_ALLINPUT);
2571
2572 /* If the server has died, give up */
2573 if (!IsWindow(server))
2574 return NULL;
2575
2576 serverProcessPendingMessages();
2577 }
2578 }
2579
2580 return NULL;
2581}
2582
2583/*
2584 * Process any messages in the Windows message queue.
2585 */
2586 void
2587serverProcessPendingMessages(void)
2588{
2589 MSG msg;
2590
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02002591 while (pPeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 {
2593 TranslateMessage(&msg);
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02002594 pDispatchMessage(&msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 }
2596}
2597
2598#endif /* FEAT_CLIENTSERVER */
2599
2600#if defined(FEAT_GUI) || (defined(FEAT_PRINTER) && !defined(FEAT_POSTSCRIPT)) \
2601 || defined(PROTO)
2602
2603struct charset_pair
2604{
2605 char *name;
2606 BYTE charset;
2607};
2608
2609static struct charset_pair
2610charset_pairs[] =
2611{
2612 {"ANSI", ANSI_CHARSET},
2613 {"CHINESEBIG5", CHINESEBIG5_CHARSET},
2614 {"DEFAULT", DEFAULT_CHARSET},
2615 {"HANGEUL", HANGEUL_CHARSET},
2616 {"OEM", OEM_CHARSET},
2617 {"SHIFTJIS", SHIFTJIS_CHARSET},
2618 {"SYMBOL", SYMBOL_CHARSET},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 {"ARABIC", ARABIC_CHARSET},
2620 {"BALTIC", BALTIC_CHARSET},
2621 {"EASTEUROPE", EASTEUROPE_CHARSET},
2622 {"GB2312", GB2312_CHARSET},
2623 {"GREEK", GREEK_CHARSET},
2624 {"HEBREW", HEBREW_CHARSET},
2625 {"JOHAB", JOHAB_CHARSET},
2626 {"MAC", MAC_CHARSET},
2627 {"RUSSIAN", RUSSIAN_CHARSET},
2628 {"THAI", THAI_CHARSET},
2629 {"TURKISH", TURKISH_CHARSET},
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002630#ifdef VIETNAMESE_CHARSET
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 {"VIETNAMESE", VIETNAMESE_CHARSET},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632#endif
2633 {NULL, 0}
2634};
2635
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002636struct quality_pair
2637{
2638 char *name;
2639 DWORD quality;
2640};
2641
2642static struct quality_pair
2643quality_pairs[] = {
2644#ifdef CLEARTYPE_QUALITY
2645 {"CLEARTYPE", CLEARTYPE_QUALITY},
2646#endif
2647#ifdef ANTIALIASED_QUALITY
2648 {"ANTIALIASED", ANTIALIASED_QUALITY},
2649#endif
Bram Moolenaar73a733e2016-05-11 21:05:05 +02002650#ifdef NONANTIALIASED_QUALITY
2651 {"NONANTIALIASED", NONANTIALIASED_QUALITY},
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002652#endif
2653#ifdef PROOF_QUALITY
2654 {"PROOF", PROOF_QUALITY},
2655#endif
Bram Moolenaar4c9ce052016-04-04 21:06:19 +02002656#ifdef DRAFT_QUALITY
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002657 {"DRAFT", DRAFT_QUALITY},
2658#endif
2659 {"DEFAULT", DEFAULT_QUALITY},
2660 {NULL, 0}
2661};
2662
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663/*
2664 * Convert a charset ID to a name.
2665 * Return NULL when not recognized.
2666 */
2667 char *
2668charset_id2name(int id)
2669{
2670 struct charset_pair *cp;
2671
2672 for (cp = charset_pairs; cp->name != NULL; ++cp)
2673 if ((BYTE)id == cp->charset)
2674 break;
2675 return cp->name;
2676}
2677
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002678/*
2679 * Convert a quality ID to a name.
2680 * Return NULL when not recognized.
2681 */
2682 char *
2683quality_id2name(DWORD id)
2684{
2685 struct quality_pair *qp;
2686
2687 for (qp = quality_pairs; qp->name != NULL; ++qp)
2688 if (id == qp->quality)
2689 break;
2690 return qp->name;
2691}
2692
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693static const LOGFONT s_lfDefault =
2694{
2695 -12, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, DEFAULT_CHARSET,
2696 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
2697 PROOF_QUALITY, FIXED_PITCH | FF_DONTCARE,
2698 "Fixedsys" /* see _ReadVimIni */
2699};
2700
2701/* Initialise the "current height" to -12 (same as s_lfDefault) just
2702 * in case the user specifies a font in "guifont" with no size before a font
2703 * with an explicit size has been set. This defaults the size to this value
2704 * (-12 equates to roughly 9pt).
2705 */
2706int current_font_height = -12; /* also used in gui_w48.c */
2707
2708/* Convert a string representing a point size into pixels. The string should
2709 * be a positive decimal number, with an optional decimal point (eg, "12", or
2710 * "10.5"). The pixel value is returned, and a pointer to the next unconverted
2711 * character is stored in *end. The flag "vertical" says whether this
2712 * calculation is for a vertical (height) size or a horizontal (width) one.
2713 */
2714 static int
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002715points_to_pixels(char_u *str, char_u **end, int vertical, long_i pprinter_dc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716{
2717 int pixels;
2718 int points = 0;
2719 int divisor = 0;
2720 HWND hwnd = (HWND)0;
2721 HDC hdc;
2722 HDC printer_dc = (HDC)pprinter_dc;
2723
2724 while (*str != NUL)
2725 {
2726 if (*str == '.' && divisor == 0)
2727 {
2728 /* Start keeping a divisor, for later */
2729 divisor = 1;
2730 }
2731 else
2732 {
2733 if (!VIM_ISDIGIT(*str))
2734 break;
2735
2736 points *= 10;
2737 points += *str - '0';
2738 divisor *= 10;
2739 }
2740 ++str;
2741 }
2742
2743 if (divisor == 0)
2744 divisor = 1;
2745
2746 if (printer_dc == NULL)
2747 {
2748 hwnd = GetDesktopWindow();
2749 hdc = GetWindowDC(hwnd);
2750 }
2751 else
2752 hdc = printer_dc;
2753
2754 pixels = MulDiv(points,
2755 GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX),
2756 72 * divisor);
2757
2758 if (printer_dc == NULL)
2759 ReleaseDC(hwnd, hdc);
2760
2761 *end = str;
2762 return pixels;
2763}
2764
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002765/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 static int CALLBACK
2767font_enumproc(
2768 ENUMLOGFONT *elf,
2769 NEWTEXTMETRIC *ntm,
2770 int type,
2771 LPARAM lparam)
2772{
2773 /* Return value:
2774 * 0 = terminate now (monospace & ANSI)
2775 * 1 = continue, still no luck...
2776 * 2 = continue, but we have an acceptable LOGFONT
2777 * (monospace, not ANSI)
2778 * We use these values, as EnumFontFamilies returns 1 if the
2779 * callback function is never called. So, we check the return as
2780 * 0 = perfect, 2 = OK, 1 = no good...
2781 * It's not pretty, but it works!
2782 */
2783
2784 LOGFONT *lf = (LOGFONT *)(lparam);
2785
2786#ifndef FEAT_PROPORTIONAL_FONTS
2787 /* Ignore non-monospace fonts without further ado */
2788 if ((ntm->tmPitchAndFamily & 1) != 0)
2789 return 1;
2790#endif
2791
2792 /* Remember this LOGFONT as a "possible" */
2793 *lf = elf->elfLogFont;
2794
2795 /* Terminate the scan as soon as we find an ANSI font */
2796 if (lf->lfCharSet == ANSI_CHARSET
2797 || lf->lfCharSet == OEM_CHARSET
2798 || lf->lfCharSet == DEFAULT_CHARSET)
2799 return 0;
2800
2801 /* Continue the scan - we have a non-ANSI font */
2802 return 2;
2803}
2804
2805 static int
2806init_logfont(LOGFONT *lf)
2807{
2808 int n;
2809 HWND hwnd = GetDesktopWindow();
2810 HDC hdc = GetWindowDC(hwnd);
2811
2812 n = EnumFontFamilies(hdc,
2813 (LPCSTR)lf->lfFaceName,
2814 (FONTENUMPROC)font_enumproc,
2815 (LPARAM)lf);
2816
2817 ReleaseDC(hwnd, hdc);
2818
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002819 /* If we couldn't find a usable font, return failure */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820 if (n == 1)
2821 return FAIL;
2822
2823 /* Tidy up the rest of the LOGFONT structure. We set to a basic
2824 * font - get_logfont() sets bold, italic, etc based on the user's
2825 * input.
2826 */
2827 lf->lfHeight = current_font_height;
2828 lf->lfWidth = 0;
2829 lf->lfItalic = FALSE;
2830 lf->lfUnderline = FALSE;
2831 lf->lfStrikeOut = FALSE;
2832 lf->lfWeight = FW_NORMAL;
2833
2834 /* Return success */
2835 return OK;
2836}
2837
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002838/*
2839 * Get font info from "name" into logfont "lf".
2840 * Return OK for a valid name, FAIL otherwise.
2841 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 int
2843get_logfont(
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002844 LOGFONT *lf,
2845 char_u *name,
2846 HDC printer_dc,
2847 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848{
2849 char_u *p;
2850 int i;
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002851 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 static LOGFONT *lastlf = NULL;
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002853#ifdef FEAT_MBYTE
2854 char_u *acpname = NULL;
2855#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856
2857 *lf = s_lfDefault;
2858 if (name == NULL)
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002859 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002861#ifdef FEAT_MBYTE
2862 /* Convert 'name' from 'encoding' to the current codepage, because
2863 * lf->lfFaceName uses the current codepage.
2864 * TODO: Use Wide APIs instead of ANSI APIs. */
2865 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
2866 {
2867 int len;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01002868 enc_to_acp(name, (int)STRLEN(name), &acpname, &len);
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002869 name = acpname;
2870 }
2871#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 if (STRCMP(name, "*") == 0)
2873 {
2874#if defined(FEAT_GUI_W32)
2875 CHOOSEFONT cf;
2876 /* if name is "*", bring up std font dialog: */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02002877 vim_memset(&cf, 0, sizeof(cf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 cf.lStructSize = sizeof(cf);
2879 cf.hwndOwner = s_hwnd;
2880 cf.Flags = CF_SCREENFONTS | CF_FIXEDPITCHONLY | CF_INITTOLOGFONTSTRUCT;
2881 if (lastlf != NULL)
2882 *lf = *lastlf;
2883 cf.lpLogFont = lf;
2884 cf.nFontType = 0 ; //REGULAR_FONTTYPE;
2885 if (ChooseFont(&cf))
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002886 ret = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887#endif
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002888 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 }
2890
2891 /*
2892 * Split name up, it could be <name>:h<height>:w<width> etc.
2893 */
2894 for (p = name; *p && *p != ':'; p++)
2895 {
Bram Moolenaar38bc4952016-11-10 17:10:51 +01002896 if (p - name + 1 >= LF_FACESIZE)
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002897 goto theend; /* Name too long */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 lf->lfFaceName[p - name] = *p;
2899 }
2900 if (p != name)
2901 lf->lfFaceName[p - name] = NUL;
2902
2903 /* First set defaults */
2904 lf->lfHeight = -12;
2905 lf->lfWidth = 0;
2906 lf->lfWeight = FW_NORMAL;
2907 lf->lfItalic = FALSE;
2908 lf->lfUnderline = FALSE;
2909 lf->lfStrikeOut = FALSE;
2910
2911 /*
2912 * If the font can't be found, try replacing '_' by ' '.
2913 */
2914 if (init_logfont(lf) == FAIL)
2915 {
2916 int did_replace = FALSE;
2917
2918 for (i = 0; lf->lfFaceName[i]; ++i)
2919 if (lf->lfFaceName[i] == '_')
2920 {
2921 lf->lfFaceName[i] = ' ';
2922 did_replace = TRUE;
2923 }
2924 if (!did_replace || init_logfont(lf) == FAIL)
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002925 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 }
2927
2928 while (*p == ':')
2929 p++;
2930
2931 /* Set the values found after ':' */
2932 while (*p)
2933 {
2934 switch (*p++)
2935 {
2936 case 'h':
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002937 lf->lfHeight = - points_to_pixels(p, &p, TRUE, (long_i)printer_dc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 break;
2939 case 'w':
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002940 lf->lfWidth = points_to_pixels(p, &p, FALSE, (long_i)printer_dc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 break;
2942 case 'b':
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 lf->lfWeight = FW_BOLD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944 break;
2945 case 'i':
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 lf->lfItalic = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 break;
2948 case 'u':
2949 lf->lfUnderline = TRUE;
2950 break;
2951 case 's':
2952 lf->lfStrikeOut = TRUE;
2953 break;
2954 case 'c':
2955 {
2956 struct charset_pair *cp;
2957
2958 for (cp = charset_pairs; cp->name != NULL; ++cp)
2959 if (STRNCMP(p, cp->name, strlen(cp->name)) == 0)
2960 {
2961 lf->lfCharSet = cp->charset;
2962 p += strlen(cp->name);
2963 break;
2964 }
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002965 if (cp->name == NULL && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966 {
Bram Moolenaar051b7822005-05-19 21:00:46 +00002967 vim_snprintf((char *)IObuff, IOSIZE,
2968 _("E244: Illegal charset name \"%s\" in font name \"%s\""), p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969 EMSG(IObuff);
2970 break;
2971 }
2972 break;
2973 }
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002974 case 'q':
2975 {
2976 struct quality_pair *qp;
2977
2978 for (qp = quality_pairs; qp->name != NULL; ++qp)
2979 if (STRNCMP(p, qp->name, strlen(qp->name)) == 0)
2980 {
2981 lf->lfQuality = qp->quality;
2982 p += strlen(qp->name);
2983 break;
2984 }
2985 if (qp->name == NULL && verbose)
2986 {
2987 vim_snprintf((char *)IObuff, IOSIZE,
2988 _("E244: Illegal quality name \"%s\" in font name \"%s\""), p, name);
2989 EMSG(IObuff);
2990 break;
2991 }
2992 break;
2993 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994 default:
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002995 if (verbose)
2996 {
Bram Moolenaar051b7822005-05-19 21:00:46 +00002997 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002998 _("E245: Illegal char '%c' in font name \"%s\""),
2999 p[-1], name);
3000 EMSG(IObuff);
3001 }
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003002 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003 }
3004 while (*p == ':')
3005 p++;
3006 }
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003007 ret = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010 /* ron: init lastlf */
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003011 if (ret == OK && printer_dc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 {
3013 vim_free(lastlf);
3014 lastlf = (LOGFONT *)alloc(sizeof(LOGFONT));
3015 if (lastlf != NULL)
3016 mch_memmove(lastlf, lf, sizeof(LOGFONT));
3017 }
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003018#ifdef FEAT_MBYTE
3019 vim_free(acpname);
3020#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003022 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023}
3024
3025#endif /* defined(FEAT_GUI) || defined(FEAT_PRINTER) */
Bram Moolenaarf12d9832016-01-29 21:11:25 +01003026
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003027#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaarf12d9832016-01-29 21:11:25 +01003028/*
3029 * Initialize the Winsock dll.
3030 */
3031 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003032channel_init_winsock(void)
Bram Moolenaarf12d9832016-01-29 21:11:25 +01003033{
3034 WSADATA wsaData;
3035 int wsaerr;
3036
3037 if (WSInitialized)
3038 return;
3039
3040 wsaerr = WSAStartup(MAKEWORD(2, 2), &wsaData);
3041 if (wsaerr == 0)
3042 WSInitialized = TRUE;
3043}
3044#endif