blob: 408438ed7458723e4195f31922a63db78185f262 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * os_mswin.c
12 *
Bram Moolenaarcf7164a2016-02-20 13:55:06 +010013 * Routines for Win32.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014 */
15
Bram Moolenaar071d4272004-06-13 20:20:40 +000016#include "vim.h"
17
Bram Moolenaar071d4272004-06-13 20:20:40 +000018#include <sys/types.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000019#include <signal.h>
20#include <limits.h>
Bram Moolenaar82881492012-11-20 16:53:39 +010021#ifndef PROTO
22# include <process.h>
23#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024
25#undef chdir
26#ifdef __GNUC__
27# ifndef __MINGW32__
28# include <dirent.h>
29# endif
30#else
31# include <direct.h>
32#endif
33
Bram Moolenaar82881492012-11-20 16:53:39 +010034#ifndef PROTO
35# if defined(FEAT_TITLE) && !defined(FEAT_GUI_W32)
36# include <shellapi.h>
37# endif
38
39# if defined(FEAT_PRINTER) && !defined(FEAT_POSTSCRIPT)
40# include <dlgs.h>
Bram Moolenaarcea912a2016-10-12 14:20:24 +020041# include <winspool.h>
Bram Moolenaar82881492012-11-20 16:53:39 +010042# include <commdlg.h>
Bram Moolenaarb04a98f2016-12-01 20:32:29 +010043# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000044
Bram Moolenaar82881492012-11-20 16:53:39 +010045#endif /* PROTO */
Bram Moolenaar071d4272004-06-13 20:20:40 +000046
47#ifdef __MINGW32__
48# ifndef FROM_LEFT_1ST_BUTTON_PRESSED
49# define FROM_LEFT_1ST_BUTTON_PRESSED 0x0001
50# endif
51# ifndef RIGHTMOST_BUTTON_PRESSED
52# define RIGHTMOST_BUTTON_PRESSED 0x0002
53# endif
54# ifndef FROM_LEFT_2ND_BUTTON_PRESSED
55# define FROM_LEFT_2ND_BUTTON_PRESSED 0x0004
56# endif
57# ifndef FROM_LEFT_3RD_BUTTON_PRESSED
58# define FROM_LEFT_3RD_BUTTON_PRESSED 0x0008
59# endif
60# ifndef FROM_LEFT_4TH_BUTTON_PRESSED
61# define FROM_LEFT_4TH_BUTTON_PRESSED 0x0010
62# endif
63
64/*
65 * EventFlags
66 */
67# ifndef MOUSE_MOVED
68# define MOUSE_MOVED 0x0001
69# endif
70# ifndef DOUBLE_CLICK
71# define DOUBLE_CLICK 0x0002
72# endif
73#endif
74
75/*
76 * When generating prototypes for Win32 on Unix, these lines make the syntax
77 * errors disappear. They do not need to be correct.
78 */
79#ifdef PROTO
80#define WINAPI
81#define WINBASEAPI
82typedef int BOOL;
83typedef int CALLBACK;
84typedef int COLORREF;
85typedef int CONSOLE_CURSOR_INFO;
86typedef int COORD;
87typedef int DWORD;
88typedef int ENUMLOGFONT;
89typedef int HANDLE;
90typedef int HDC;
91typedef int HFONT;
92typedef int HICON;
93typedef int HWND;
94typedef int INPUT_RECORD;
95typedef int KEY_EVENT_RECORD;
96typedef int LOGFONT;
97typedef int LPARAM;
98typedef int LPBOOL;
99typedef int LPCSTR;
100typedef int LPCWSTR;
101typedef int LPSTR;
102typedef int LPTSTR;
103typedef int LPWSTR;
104typedef int LRESULT;
105typedef int MOUSE_EVENT_RECORD;
106typedef int NEWTEXTMETRIC;
107typedef int PACL;
108typedef int PRINTDLG;
109typedef int PSECURITY_DESCRIPTOR;
110typedef int PSID;
111typedef int SECURITY_INFORMATION;
112typedef int SHORT;
113typedef int SMALL_RECT;
114typedef int TEXTMETRIC;
115typedef int UINT;
116typedef int WCHAR;
117typedef int WORD;
118typedef int WPARAM;
119typedef void VOID;
120#endif
121
122/* Record all output and all keyboard & mouse input */
123/* #define MCH_WRITE_DUMP */
124
125#ifdef MCH_WRITE_DUMP
126FILE* fdDump = NULL;
127#endif
128
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129#ifndef FEAT_GUI_MSWIN
130extern char g_szOrigTitle[];
131#endif
132
133#ifdef FEAT_GUI
134extern HWND s_hwnd;
135#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136static HWND s_hwnd = 0; /* console window handle, set by GetConsoleHwnd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137#endif
138
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100139#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100140int WSInitialized = FALSE; /* WinSock is initialized */
141#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142
143/* Don't generate prototypes here, because some systems do have these
144 * functions. */
145#if defined(__GNUC__) && !defined(PROTO)
146# ifndef __MINGW32__
147int _stricoll(char *a, char *b)
148{
149 // the ANSI-ish correct way is to use strxfrm():
150 char a_buff[512], b_buff[512]; // file names, so this is enough on Win32
151 strxfrm(a_buff, a, 512);
152 strxfrm(b_buff, b, 512);
153 return strcoll(a_buff, b_buff);
154}
155
156char * _fullpath(char *buf, char *fname, int len)
157{
158 LPTSTR toss;
159
160 return (char *)GetFullPathName(fname, len, buf, &toss);
161}
162# endif
163
Bram Moolenaaraf62ff32013-03-19 14:48:29 +0100164# if !defined(__MINGW32__) || (__GNUC__ < 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165int _chdrive(int drive)
166{
167 char temp [3] = "-:";
168 temp[0] = drive + 'A' - 1;
169 return !SetCurrentDirectory(temp);
170}
Bram Moolenaaraf62ff32013-03-19 14:48:29 +0100171# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172#else
173# ifdef __BORLANDC__
174/* being a more ANSI compliant compiler, BorlandC doesn't define _stricoll:
175 * but it does in BC 5.02! */
176# if __BORLANDC__ < 0x502
177int _stricoll(char *a, char *b)
178{
179# if 1
180 // this is fast but not correct:
181 return stricmp(a, b);
182# else
183 // the ANSI-ish correct way is to use strxfrm():
184 char a_buff[512], b_buff[512]; // file names, so this is enough on Win32
185 strxfrm(a_buff, a, 512);
186 strxfrm(b_buff, b, 512);
187 return strcoll(a_buff, b_buff);
188# endif
189}
190# endif
191# endif
192#endif
193
194
195#if defined(FEAT_GUI_MSWIN) || defined(PROTO)
196/*
197 * GUI version of mch_exit().
198 * Shut down and exit with status `r'
199 * Careful: mch_exit() may be called before mch_init()!
200 */
201 void
202mch_exit(int r)
203{
204 display_errors();
205
206 ml_close_all(TRUE); /* remove all memfiles */
207
208# ifdef FEAT_OLE
209 UninitOLE();
210# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100211# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000212 if (WSInitialized)
213 {
214 WSInitialized = FALSE;
215 WSACleanup();
216 }
217# endif
218#ifdef DYNAMIC_GETTEXT
219 dyn_libintl_end();
220#endif
221
222 if (gui.in_use)
223 gui_exit(r);
Bram Moolenaar85c79d32007-02-20 01:59:20 +0000224
225#ifdef EXITFREE
226 free_all_mem();
227#endif
228
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229 exit(r);
230}
231
232#endif /* FEAT_GUI_MSWIN */
233
234
235/*
236 * Init the tables for toupper() and tolower().
237 */
238 void
239mch_early_init(void)
240{
241 int i;
242
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243 PlatformId();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244
245 /* Init the tables for toupper() and tolower() */
246 for (i = 0; i < 256; ++i)
247 toupper_tab[i] = tolower_tab[i] = i;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100248 CharUpperBuff((LPSTR)toupper_tab, 256);
249 CharLowerBuff((LPSTR)tolower_tab, 256);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000250}
251
252
253/*
254 * Return TRUE if the input comes from a terminal, FALSE otherwise.
255 */
256 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100257mch_input_isatty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258{
259#ifdef FEAT_GUI_MSWIN
260 return OK; /* GUI always has a tty */
261#else
262 if (isatty(read_cmd_fd))
263 return TRUE;
264 return FALSE;
265#endif
266}
267
268#ifdef FEAT_TITLE
269/*
270 * mch_settitle(): set titlebar of our window
271 */
272 void
273mch_settitle(
274 char_u *title,
275 char_u *icon)
276{
277# ifdef FEAT_GUI_MSWIN
278 gui_mch_settitle(title, icon);
279# else
280 if (title != NULL)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000281 {
282# ifdef FEAT_MBYTE
283 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
284 {
285 /* Convert the title from 'encoding' to the active codepage. */
Bram Moolenaar36f692d2008-11-20 16:10:17 +0000286 WCHAR *wp = enc_to_utf16(title, NULL);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000287
288 if (wp != NULL)
289 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200290 SetConsoleTitleW(wp);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000291 vim_free(wp);
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200292 return;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000293 }
294 }
295# endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100296 SetConsoleTitle((LPCSTR)title);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000297 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000298# endif
299}
300
301
302/*
303 * Restore the window/icon title.
304 * which is one of:
305 * 1: Just restore title
306 * 2: Just restore icon (which we don't have)
307 * 3: Restore title and icon (which we don't have)
308 */
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;
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01001622 }
1623 else
1624 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 psettings->n_collated_copies = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 psettings->n_uncollated_copies = 1;
Bram Moolenaar7ddc6422014-09-27 11:18:19 +02001627 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628
1629 psettings->jobname = jobname;
1630
1631 return TRUE;
1632
1633init_fail_dlg:
1634 {
1635 DWORD err = CommDlgExtendedError();
1636
1637 if (err)
1638 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 char_u *buf;
1640
1641 /* I suspect FormatMessage() doesn't work for values returned by
1642 * CommDlgExtendedError(). What does? */
1643 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
1644 FORMAT_MESSAGE_FROM_SYSTEM |
1645 FORMAT_MESSAGE_IGNORE_INSERTS,
1646 NULL, err, 0, (LPTSTR)(&buf), 0, NULL);
1647 EMSG2(_("E238: Print error: %s"),
1648 buf == NULL ? (char_u *)_("Unknown") : buf);
1649 LocalFree((LPVOID)(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 }
1651 else
1652 msg_clr_eos(); /* Maybe canceled */
1653
1654 mch_print_cleanup();
1655 return FALSE;
1656 }
1657}
1658
1659
1660 int
1661mch_print_begin(prt_settings_T *psettings)
1662{
1663 int ret;
1664 static DOCINFO di;
1665 char szBuffer[300];
1666
1667 hDlgPrint = CreateDialog(GetModuleHandle(NULL), TEXT("PrintDlgBox"),
1668 prt_dlg.hwndOwner, PrintDlgProc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 SetAbortProc(prt_dlg.hDC, AbortProc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 wsprintf(szBuffer, _("Printing '%s'"), gettail(psettings->jobname));
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001671 vimSetDlgItemText(hDlgPrint, IDC_PRINTTEXT1, (char_u *)szBuffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02001673 vim_memset(&di, 0, sizeof(DOCINFO));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 di.cbSize = sizeof(DOCINFO);
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001675 di.lpszDocName = (LPCSTR)psettings->jobname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 ret = StartDoc(prt_dlg.hDC, &di);
1677
1678#ifdef FEAT_GUI
1679 /* Give focus back to main window (when using MDI). */
1680 SetFocus(s_hwnd);
1681#endif
1682
1683 return (ret > 0);
1684}
1685
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001686/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 void
1688mch_print_end(prt_settings_T *psettings)
1689{
1690 EndDoc(prt_dlg.hDC);
1691 if (!*bUserAbort)
1692 SendMessage(hDlgPrint, WM_COMMAND, 0, 0);
1693}
1694
1695 int
1696mch_print_end_page(void)
1697{
1698 return (EndPage(prt_dlg.hDC) > 0);
1699}
1700
1701 int
1702mch_print_begin_page(char_u *msg)
1703{
1704 if (msg != NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001705 vimSetDlgItemText(hDlgPrint, IDC_PROGRESS, msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 return (StartPage(prt_dlg.hDC) > 0);
1707}
1708
1709 int
1710mch_print_blank_page(void)
1711{
1712 return (mch_print_begin_page(NULL) ? (mch_print_end_page()) : FALSE);
1713}
1714
1715static int prt_pos_x = 0;
1716static int prt_pos_y = 0;
1717
1718 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001719mch_print_start_line(int margin, int page_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720{
1721 if (margin)
1722 prt_pos_x = -prt_number_width;
1723 else
1724 prt_pos_x = 0;
1725 prt_pos_y = page_line * prt_line_height
1726 + prt_tm.tmAscent + prt_tm.tmExternalLeading;
1727}
1728
1729 int
1730mch_print_text_out(char_u *p, int len)
1731{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001732#if defined(FEAT_PROPORTIONAL_FONTS) || defined(FEAT_MBYTE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 SIZE sz;
1734#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001735#if defined(FEAT_MBYTE)
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001736 WCHAR *wp = NULL;
1737 int wlen = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001739 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
1740 {
1741 wp = enc_to_utf16(p, &wlen);
1742 }
1743 if (wp != NULL)
1744 {
1745 int ret = FALSE;
1746
1747 TextOutW(prt_dlg.hDC, prt_pos_x + prt_left_margin,
1748 prt_pos_y + prt_top_margin, wp, wlen);
1749 GetTextExtentPoint32W(prt_dlg.hDC, wp, wlen, &sz);
1750 vim_free(wp);
1751 prt_pos_x += (sz.cx - prt_tm.tmOverhang);
1752 /* This is wrong when printing spaces for a TAB. */
1753 if (p[len] != NUL)
1754 {
1755 wlen = MB_PTR2LEN(p + len);
1756 wp = enc_to_utf16(p + len, &wlen);
1757 if (wp != NULL)
1758 {
1759 GetTextExtentPoint32W(prt_dlg.hDC, wp, 1, &sz);
1760 ret = (prt_pos_x + prt_left_margin + sz.cx > prt_right_margin);
1761 vim_free(wp);
1762 }
1763 }
1764 return ret;
1765 }
1766#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 TextOut(prt_dlg.hDC, prt_pos_x + prt_left_margin,
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001768 prt_pos_y + prt_top_margin,
1769 (LPCSTR)p, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770#ifndef FEAT_PROPORTIONAL_FONTS
1771 prt_pos_x += len * prt_tm.tmAveCharWidth;
1772 return (prt_pos_x + prt_left_margin + prt_tm.tmAveCharWidth
1773 + prt_tm.tmOverhang > prt_right_margin);
1774#else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001775 GetTextExtentPoint32(prt_dlg.hDC, (LPCSTR)p, len, &sz);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776 prt_pos_x += (sz.cx - prt_tm.tmOverhang);
1777 /* This is wrong when printing spaces for a TAB. */
1778 if (p[len] == NUL)
1779 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 GetTextExtentPoint32(prt_dlg.hDC, p + len, 1, &sz);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781 return (prt_pos_x + prt_left_margin + sz.cx > prt_right_margin);
1782#endif
1783}
1784
1785 void
1786mch_print_set_font(int iBold, int iItalic, int iUnderline)
1787{
1788 SelectObject(prt_dlg.hDC, prt_font_handles[iBold][iItalic][iUnderline]);
1789}
1790
1791 void
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001792mch_print_set_bg(long_u bgcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793{
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001794 SetBkColor(prt_dlg.hDC, GetNearestColor(prt_dlg.hDC,
1795 swap_me((COLORREF)bgcol)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 /*
1797 * With a white background we can draw characters transparent, which is
1798 * good for italic characters that overlap to the next char cell.
1799 */
1800 if (bgcol == 0xffffffUL)
1801 SetBkMode(prt_dlg.hDC, TRANSPARENT);
1802 else
1803 SetBkMode(prt_dlg.hDC, OPAQUE);
1804}
1805
1806 void
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001807mch_print_set_fg(long_u fgcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808{
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001809 SetTextColor(prt_dlg.hDC, GetNearestColor(prt_dlg.hDC,
1810 swap_me((COLORREF)fgcol)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811}
1812
1813#endif /*FEAT_PRINTER && !FEAT_POSTSCRIPT*/
1814
Bram Moolenaar58d98232005-07-23 22:25:46 +00001815
1816
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817#if defined(FEAT_SHORTCUT) || defined(PROTO)
Bram Moolenaar82881492012-11-20 16:53:39 +01001818# ifndef PROTO
1819# include <shlobj.h>
1820# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821
1822/*
1823 * When "fname" is the name of a shortcut (*.lnk) resolve the file it points
1824 * to and return that name in allocated memory.
1825 * Otherwise NULL is returned.
1826 */
1827 char_u *
1828mch_resolve_shortcut(char_u *fname)
1829{
1830 HRESULT hr;
1831 IShellLink *psl = NULL;
1832 IPersistFile *ppf = NULL;
1833 OLECHAR wsz[MAX_PATH];
1834 WIN32_FIND_DATA ffd; // we get those free of charge
Bram Moolenaar604729e2013-08-30 16:44:19 +02001835 CHAR buf[MAX_PATH]; // could have simply reused 'wsz'...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 char_u *rfname = NULL;
1837 int len;
Bram Moolenaar604729e2013-08-30 16:44:19 +02001838# ifdef FEAT_MBYTE
1839 IShellLinkW *pslw = NULL;
1840 WIN32_FIND_DATAW ffdw; // we get those free of charge
1841# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842
1843 /* Check if the file name ends in ".lnk". Avoid calling
1844 * CoCreateInstance(), it's quite slow. */
1845 if (fname == NULL)
1846 return rfname;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001847 len = (int)STRLEN(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 if (len <= 4 || STRNICMP(fname + len - 4, ".lnk", 4) != 0)
1849 return rfname;
1850
1851 CoInitialize(NULL);
1852
Bram Moolenaar604729e2013-08-30 16:44:19 +02001853# ifdef FEAT_MBYTE
1854 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
1855 {
1856 // create a link manager object and request its interface
1857 hr = CoCreateInstance(
1858 &CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
1859 &IID_IShellLinkW, (void**)&pslw);
1860 if (hr == S_OK)
1861 {
1862 WCHAR *p = enc_to_utf16(fname, NULL);
1863
1864 if (p != NULL)
1865 {
1866 // Get a pointer to the IPersistFile interface.
1867 hr = pslw->lpVtbl->QueryInterface(
1868 pslw, &IID_IPersistFile, (void**)&ppf);
1869 if (hr != S_OK)
1870 goto shortcut_errorw;
1871
1872 // "load" the name and resolve the link
1873 hr = ppf->lpVtbl->Load(ppf, p, STGM_READ);
1874 if (hr != S_OK)
1875 goto shortcut_errorw;
1876# if 0 // This makes Vim wait a long time if the target does not exist.
1877 hr = pslw->lpVtbl->Resolve(pslw, NULL, SLR_NO_UI);
1878 if (hr != S_OK)
1879 goto shortcut_errorw;
1880# endif
1881
1882 // Get the path to the link target.
1883 ZeroMemory(wsz, MAX_PATH * sizeof(WCHAR));
1884 hr = pslw->lpVtbl->GetPath(pslw, wsz, MAX_PATH, &ffdw, 0);
1885 if (hr == S_OK && wsz[0] != NUL)
1886 rfname = utf16_to_enc(wsz, NULL);
1887
1888shortcut_errorw:
1889 vim_free(p);
Bram Moolenaarcd981f22014-02-11 17:06:00 +01001890 goto shortcut_end;
Bram Moolenaar604729e2013-08-30 16:44:19 +02001891 }
1892 }
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001893 goto shortcut_end;
Bram Moolenaar604729e2013-08-30 16:44:19 +02001894 }
1895# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 // create a link manager object and request its interface
1897 hr = CoCreateInstance(
1898 &CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
1899 &IID_IShellLink, (void**)&psl);
1900 if (hr != S_OK)
Bram Moolenaar604729e2013-08-30 16:44:19 +02001901 goto shortcut_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902
1903 // Get a pointer to the IPersistFile interface.
1904 hr = psl->lpVtbl->QueryInterface(
1905 psl, &IID_IPersistFile, (void**)&ppf);
1906 if (hr != S_OK)
Bram Moolenaar604729e2013-08-30 16:44:19 +02001907 goto shortcut_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908
1909 // full path string must be in Unicode.
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001910 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)fname, -1, wsz, MAX_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00001912 // "load" the name and resolve the link
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 hr = ppf->lpVtbl->Load(ppf, wsz, STGM_READ);
1914 if (hr != S_OK)
Bram Moolenaar604729e2013-08-30 16:44:19 +02001915 goto shortcut_end;
1916# if 0 // This makes Vim wait a long time if the target doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 hr = psl->lpVtbl->Resolve(psl, NULL, SLR_NO_UI);
1918 if (hr != S_OK)
Bram Moolenaar604729e2013-08-30 16:44:19 +02001919 goto shortcut_end;
1920# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921
1922 // Get the path to the link target.
1923 ZeroMemory(buf, MAX_PATH);
1924 hr = psl->lpVtbl->GetPath(psl, buf, MAX_PATH, &ffd, 0);
1925 if (hr == S_OK && buf[0] != NUL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001926 rfname = vim_strsave((char_u *)buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927
Bram Moolenaar604729e2013-08-30 16:44:19 +02001928shortcut_end:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 // Release all interface pointers (both belong to the same object)
1930 if (ppf != NULL)
1931 ppf->lpVtbl->Release(ppf);
1932 if (psl != NULL)
1933 psl->lpVtbl->Release(psl);
Bram Moolenaar604729e2013-08-30 16:44:19 +02001934# ifdef FEAT_MBYTE
1935 if (pslw != NULL)
1936 pslw->lpVtbl->Release(pslw);
1937# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938
1939 CoUninitialize();
1940 return rfname;
1941}
1942#endif
1943
1944#if (defined(FEAT_EVAL) && !defined(FEAT_GUI)) || defined(PROTO)
1945/*
1946 * Bring ourselves to the foreground. Does work if the OS doesn't allow it.
1947 */
1948 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001949win32_set_foreground(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950{
1951# ifndef FEAT_GUI
1952 GetConsoleHwnd(); /* get value of s_hwnd */
1953# endif
1954 if (s_hwnd != 0)
1955 SetForegroundWindow(s_hwnd);
1956}
1957#endif
1958
1959#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
1960/*
1961 * Client-server code for Vim
1962 *
1963 * Originally written by Paul Moore
1964 */
1965
1966/* In order to handle inter-process messages, we need to have a window. But
1967 * the functions in this module can be called before the main GUI window is
1968 * created (and may also be called in the console version, where there is no
1969 * GUI window at all).
1970 *
1971 * So we create a hidden window, and arrange to destroy it on exit.
1972 */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001973HWND message_window = 0; /* window that's handling messages */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974
1975#define VIM_CLASSNAME "VIM_MESSAGES"
1976#define VIM_CLASSNAME_LEN (sizeof(VIM_CLASSNAME) - 1)
1977
1978/* Communication is via WM_COPYDATA messages. The message type is send in
1979 * the dwData parameter. Types are defined here. */
1980#define COPYDATA_KEYS 0
1981#define COPYDATA_REPLY 1
1982#define COPYDATA_EXPR 10
1983#define COPYDATA_RESULT 11
1984#define COPYDATA_ERROR_RESULT 12
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001985#define COPYDATA_ENCODING 20
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986
1987/* This is a structure containing a server HWND and its name. */
1988struct server_id
1989{
1990 HWND hwnd;
1991 char_u *name;
1992};
1993
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001994/* Last received 'encoding' that the client uses. */
1995static char_u *client_enc = NULL;
1996
1997/*
1998 * Tell the other side what encoding we are using.
1999 * Errors are ignored.
2000 */
2001 static void
2002serverSendEnc(HWND target)
2003{
2004 COPYDATASTRUCT data;
2005
2006 data.dwData = COPYDATA_ENCODING;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002007#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002008 data.cbData = (DWORD)STRLEN(p_enc) + 1;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002009 data.lpData = p_enc;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002010#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +02002011 data.cbData = (DWORD)STRLEN("latin1") + 1;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002012 data.lpData = "latin1";
2013#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002014 (void)SendMessage(target, WM_COPYDATA, (WPARAM)message_window,
2015 (LPARAM)(&data));
2016}
2017
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018/*
2019 * Clean up on exit. This destroys the hidden message window.
2020 */
2021 static void
2022#ifdef __BORLANDC__
2023 _RTLENTRYF
2024#endif
2025CleanUpMessaging(void)
2026{
2027 if (message_window != 0)
2028 {
2029 DestroyWindow(message_window);
2030 message_window = 0;
2031 }
2032}
2033
2034static int save_reply(HWND server, char_u *reply, int expr);
2035
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002036/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 * The window procedure for the hidden message window.
2038 * It handles callback messages and notifications from servers.
2039 * In order to process these messages, it is necessary to run a
2040 * message loop. Code which may run before the main message loop
2041 * is started (in the GUI) is careful to pump messages when it needs
2042 * to. Features which require message delivery during normal use will
2043 * not work in the console version - this basically means those
2044 * features which allow Vim to act as a server, rather than a client.
2045 */
2046 static LRESULT CALLBACK
2047Messaging_WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
2048{
2049 if (msg == WM_COPYDATA)
2050 {
2051 /* This is a message from another Vim. The dwData member of the
2052 * COPYDATASTRUCT determines the type of message:
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002053 * COPYDATA_ENCODING:
2054 * The encoding that the client uses. Following messages will
2055 * use this encoding, convert if needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 * COPYDATA_KEYS:
2057 * A key sequence. We are a server, and a client wants these keys
2058 * adding to the input queue.
2059 * COPYDATA_REPLY:
2060 * A reply. We are a client, and a server has sent this message
2061 * in response to a request. (server2client())
2062 * COPYDATA_EXPR:
2063 * An expression. We are a server, and a client wants us to
2064 * evaluate this expression.
2065 * COPYDATA_RESULT:
2066 * A reply. We are a client, and a server has sent this message
2067 * in response to a COPYDATA_EXPR.
2068 * COPYDATA_ERROR_RESULT:
2069 * A reply. We are a client, and a server has sent this message
2070 * in response to a COPYDATA_EXPR that failed to evaluate.
2071 */
2072 COPYDATASTRUCT *data = (COPYDATASTRUCT*)lParam;
2073 HWND sender = (HWND)wParam;
2074 COPYDATASTRUCT reply;
2075 char_u *res;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 int retval;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002077 char_u *str;
2078 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079
2080 switch (data->dwData)
2081 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002082 case COPYDATA_ENCODING:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002083# ifdef FEAT_MBYTE
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002084 /* Remember the encoding that the client uses. */
2085 vim_free(client_enc);
2086 client_enc = enc_canonize((char_u *)data->lpData);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002087# endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002088 return 1;
2089
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 case COPYDATA_KEYS:
2091 /* Remember who sent this, for <client> */
2092 clientWindow = sender;
2093
2094 /* Add the received keys to the input buffer. The loop waiting
2095 * for the user to do something should check the input buffer. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002096 str = serverConvert(client_enc, (char_u *)data->lpData, &tofree);
2097 server_to_input_buf(str);
2098 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099
2100# ifdef FEAT_GUI
2101 /* Wake up the main GUI loop. */
2102 if (s_hwnd != 0)
2103 PostMessage(s_hwnd, WM_NULL, 0, 0);
2104# endif
2105 return 1;
2106
2107 case COPYDATA_EXPR:
2108 /* Remember who sent this, for <client> */
2109 clientWindow = sender;
2110
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002111 str = serverConvert(client_enc, (char_u *)data->lpData, &tofree);
2112 res = eval_client_expr_to_string(str);
2113 vim_free(tofree);
2114
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 if (res == NULL)
2116 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002117 res = vim_strsave((char_u *)_(e_invexprmsg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 reply.dwData = COPYDATA_ERROR_RESULT;
2119 }
2120 else
2121 reply.dwData = COPYDATA_RESULT;
2122 reply.lpData = res;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002123 reply.cbData = (DWORD)STRLEN(res) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002125 serverSendEnc(sender);
Bram Moolenaar5246cd72013-06-16 16:41:47 +02002126 retval = (int)SendMessage(sender, WM_COPYDATA,
2127 (WPARAM)message_window, (LPARAM)(&reply));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 vim_free(res);
2129 return retval;
2130
2131 case COPYDATA_REPLY:
2132 case COPYDATA_RESULT:
2133 case COPYDATA_ERROR_RESULT:
2134 if (data->lpData != NULL)
2135 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002136 str = serverConvert(client_enc, (char_u *)data->lpData,
2137 &tofree);
2138 if (tofree == NULL)
2139 str = vim_strsave(str);
2140 if (save_reply(sender, str,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 (data->dwData == COPYDATA_REPLY ? 0 :
2142 (data->dwData == COPYDATA_RESULT ? 1 :
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002143 2))) == FAIL)
2144 vim_free(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145#ifdef FEAT_AUTOCMD
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002146 else if (data->dwData == COPYDATA_REPLY)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 {
Bram Moolenaar427d51c2013-06-16 16:01:25 +02002148 char_u winstr[30];
2149
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002150 sprintf((char *)winstr, PRINTF_HEX_LONG_U, (long_u)sender);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002151 apply_autocmds(EVENT_REMOTEREPLY, winstr, str,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 TRUE, curbuf);
2153 }
2154#endif
2155 }
2156 return 1;
2157 }
2158
2159 return 0;
2160 }
2161
2162 else if (msg == WM_ACTIVATE && wParam == WA_ACTIVE)
2163 {
2164 /* When the message window is activated (brought to the foreground),
2165 * this actually applies to the text window. */
2166#ifndef FEAT_GUI
2167 GetConsoleHwnd(); /* get value of s_hwnd */
2168#endif
2169 if (s_hwnd != 0)
2170 {
2171 SetForegroundWindow(s_hwnd);
2172 return 0;
2173 }
2174 }
2175
2176 return DefWindowProc(hwnd, msg, wParam, lParam);
2177}
2178
2179/*
2180 * Initialise the message handling process. This involves creating a window
2181 * to handle messages - the window will not be visible.
2182 */
2183 void
2184serverInitMessaging(void)
2185{
2186 WNDCLASS wndclass;
2187 HINSTANCE s_hinst;
2188
2189 /* Clean up on exit */
2190 atexit(CleanUpMessaging);
2191
2192 /* Register a window class - we only really care
2193 * about the window procedure
2194 */
2195 s_hinst = (HINSTANCE)GetModuleHandle(0);
2196 wndclass.style = 0;
2197 wndclass.lpfnWndProc = Messaging_WndProc;
2198 wndclass.cbClsExtra = 0;
2199 wndclass.cbWndExtra = 0;
2200 wndclass.hInstance = s_hinst;
2201 wndclass.hIcon = NULL;
2202 wndclass.hCursor = NULL;
2203 wndclass.hbrBackground = NULL;
2204 wndclass.lpszMenuName = NULL;
2205 wndclass.lpszClassName = VIM_CLASSNAME;
2206 RegisterClass(&wndclass);
2207
2208 /* Create the message window. It will be hidden, so the details don't
2209 * matter. Don't use WS_OVERLAPPEDWINDOW, it will make a shortcut remove
2210 * focus from gvim. */
2211 message_window = CreateWindow(VIM_CLASSNAME, "",
2212 WS_POPUPWINDOW | WS_CAPTION,
2213 CW_USEDEFAULT, CW_USEDEFAULT,
2214 100, 100, NULL, NULL,
2215 s_hinst, NULL);
2216}
2217
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002218/* Used by serverSendToVim() to find an alternate server name. */
2219static char_u *altname_buf_ptr = NULL;
2220
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221/*
2222 * Get the title of the window "hwnd", which is the Vim server name, in
2223 * "name[namelen]" and return the length.
2224 * Returns zero if window "hwnd" is not a Vim server.
2225 */
2226 static int
2227getVimServerName(HWND hwnd, char *name, int namelen)
2228{
2229 int len;
2230 char buffer[VIM_CLASSNAME_LEN + 1];
2231
2232 /* Ignore windows which aren't Vim message windows */
2233 len = GetClassName(hwnd, buffer, sizeof(buffer));
2234 if (len != VIM_CLASSNAME_LEN || STRCMP(buffer, VIM_CLASSNAME) != 0)
2235 return 0;
2236
2237 /* Get the title of the window */
2238 return GetWindowText(hwnd, name, namelen);
2239}
2240
2241 static BOOL CALLBACK
2242enumWindowsGetServer(HWND hwnd, LPARAM lparam)
2243{
2244 struct server_id *id = (struct server_id *)lparam;
2245 char server[MAX_PATH];
2246
2247 /* Get the title of the window */
2248 if (getVimServerName(hwnd, server, sizeof(server)) == 0)
2249 return TRUE;
2250
2251 /* If this is the server we're looking for, return its HWND */
2252 if (STRICMP(server, id->name) == 0)
2253 {
2254 id->hwnd = hwnd;
2255 return FALSE;
2256 }
2257
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002258 /* If we are looking for an alternate server, remember this name. */
2259 if (altname_buf_ptr != NULL
2260 && STRNICMP(server, id->name, STRLEN(id->name)) == 0
2261 && vim_isdigit(server[STRLEN(id->name)]))
2262 {
2263 STRCPY(altname_buf_ptr, server);
2264 altname_buf_ptr = NULL; /* don't use another name */
2265 }
2266
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 /* Otherwise, keep looking */
2268 return TRUE;
2269}
2270
2271 static BOOL CALLBACK
2272enumWindowsGetNames(HWND hwnd, LPARAM lparam)
2273{
2274 garray_T *ga = (garray_T *)lparam;
2275 char server[MAX_PATH];
2276
2277 /* Get the title of the window */
2278 if (getVimServerName(hwnd, server, sizeof(server)) == 0)
2279 return TRUE;
2280
2281 /* Add the name to the list */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002282 ga_concat(ga, (char_u *)server);
2283 ga_concat(ga, (char_u *)"\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 return TRUE;
2285}
2286
2287 static HWND
2288findServer(char_u *name)
2289{
2290 struct server_id id;
2291
2292 id.name = name;
2293 id.hwnd = 0;
2294
2295 EnumWindows(enumWindowsGetServer, (LPARAM)(&id));
2296
2297 return id.hwnd;
2298}
2299
2300 void
2301serverSetName(char_u *name)
2302{
2303 char_u *ok_name;
2304 HWND hwnd = 0;
2305 int i = 0;
2306 char_u *p;
2307
2308 /* Leave enough space for a 9-digit suffix to ensure uniqueness! */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002309 ok_name = alloc((unsigned)STRLEN(name) + 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310
2311 STRCPY(ok_name, name);
2312 p = ok_name + STRLEN(name);
2313
2314 for (;;)
2315 {
2316 /* This is inefficient - we're doing an EnumWindows loop for each
2317 * possible name. It would be better to grab all names in one go,
2318 * and scan the list each time...
2319 */
2320 hwnd = findServer(ok_name);
2321 if (hwnd == 0)
2322 break;
2323
2324 ++i;
2325 if (i >= 1000)
2326 break;
2327
2328 sprintf((char *)p, "%d", i);
2329 }
2330
2331 if (hwnd != 0)
2332 vim_free(ok_name);
2333 else
2334 {
2335 /* Remember the name */
2336 serverName = ok_name;
2337#ifdef FEAT_TITLE
2338 need_maketitle = TRUE; /* update Vim window title later */
2339#endif
2340
2341 /* Update the message window title */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002342 SetWindowText(message_window, (LPCSTR)ok_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343
2344#ifdef FEAT_EVAL
2345 /* Set the servername variable */
2346 set_vim_var_string(VV_SEND_SERVER, serverName, -1);
2347#endif
2348 }
2349}
2350
2351 char_u *
2352serverGetVimNames(void)
2353{
2354 garray_T ga;
2355
2356 ga_init2(&ga, 1, 100);
2357
2358 EnumWindows(enumWindowsGetNames, (LPARAM)(&ga));
Bram Moolenaar269ec652004-07-29 08:43:53 +00002359 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360
2361 return ga.ga_data;
2362}
2363
2364 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002365serverSendReply(
2366 char_u *name, /* Where to send. */
2367 char_u *reply) /* What to send. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368{
2369 HWND target;
2370 COPYDATASTRUCT data;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002371 long_u n = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372
2373 /* The "name" argument is a magic cookie obtained from expand("<client>").
2374 * It should be of the form 0xXXXXX - i.e. a C hex literal, which is the
2375 * value of the client's message window HWND.
2376 */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002377 sscanf((char *)name, SCANF_HEX_LONG_U, &n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 if (n == 0)
2379 return -1;
2380
2381 target = (HWND)n;
2382 if (!IsWindow(target))
2383 return -1;
2384
2385 data.dwData = COPYDATA_REPLY;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002386 data.cbData = (DWORD)STRLEN(reply) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 data.lpData = reply;
2388
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002389 serverSendEnc(target);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 if (SendMessage(target, WM_COPYDATA, (WPARAM)message_window,
2391 (LPARAM)(&data)))
2392 return 0;
2393
2394 return -1;
2395}
2396
2397 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002398serverSendToVim(
2399 char_u *name, /* Where to send. */
2400 char_u *cmd, /* What to send. */
2401 char_u **result, /* Result of eval'ed expression */
2402 void *ptarget, /* HWND of server */
2403 int asExpr, /* Expression or keys? */
2404 int silent) /* don't complain about no server */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405{
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002406 HWND target;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 COPYDATASTRUCT data;
2408 char_u *retval = NULL;
2409 int retcode = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002410 char_u altname_buf[MAX_PATH];
2411
2412 /* If the server name does not end in a digit then we look for an
2413 * alternate name. e.g. when "name" is GVIM the we may find GVIM2. */
2414 if (STRLEN(name) > 1 && !vim_isdigit(name[STRLEN(name) - 1]))
2415 altname_buf_ptr = altname_buf;
2416 altname_buf[0] = NUL;
2417 target = findServer(name);
2418 altname_buf_ptr = NULL;
2419 if (target == 0 && altname_buf[0] != NUL)
2420 /* Use another server name we found. */
2421 target = findServer(altname_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422
2423 if (target == 0)
2424 {
2425 if (!silent)
2426 EMSG2(_(e_noserver), name);
2427 return -1;
2428 }
2429
2430 if (ptarget)
2431 *(HWND *)ptarget = target;
2432
2433 data.dwData = asExpr ? COPYDATA_EXPR : COPYDATA_KEYS;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002434 data.cbData = (DWORD)STRLEN(cmd) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 data.lpData = cmd;
2436
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002437 serverSendEnc(target);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 if (SendMessage(target, WM_COPYDATA, (WPARAM)message_window,
2439 (LPARAM)(&data)) == 0)
2440 return -1;
2441
2442 if (asExpr)
2443 retval = serverGetReply(target, &retcode, TRUE, TRUE);
2444
2445 if (result == NULL)
2446 vim_free(retval);
2447 else
2448 *result = retval; /* Caller assumes responsibility for freeing */
2449
2450 return retcode;
2451}
2452
2453/*
2454 * Bring the server to the foreground.
2455 */
2456 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002457serverForeground(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458{
2459 HWND target = findServer(name);
2460
2461 if (target != 0)
2462 SetForegroundWindow(target);
2463}
2464
2465/* Replies from server need to be stored until the client picks them up via
2466 * remote_read(). So we maintain a list of server-id/reply pairs.
2467 * Note that there could be multiple replies from one server pending if the
2468 * client is slow picking them up.
2469 * We just store the replies in a simple list. When we remove an entry, we
2470 * move list entries down to fill the gap.
2471 * The server ID is simply the HWND.
2472 */
2473typedef struct
2474{
2475 HWND server; /* server window */
2476 char_u *reply; /* reply string */
2477 int expr_result; /* 0 for REPLY, 1 for RESULT 2 for error */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002478} reply_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479
2480static garray_T reply_list = {0, 0, sizeof(reply_T), 5, 0};
2481
2482#define REPLY_ITEM(i) ((reply_T *)(reply_list.ga_data) + (i))
2483#define REPLY_COUNT (reply_list.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484
2485/* Flag which is used to wait for a reply */
2486static int reply_received = 0;
2487
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002488/*
2489 * Store a reply. "reply" must be allocated memory (or NULL).
2490 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 static int
2492save_reply(HWND server, char_u *reply, int expr)
2493{
2494 reply_T *rep;
2495
2496 if (ga_grow(&reply_list, 1) == FAIL)
2497 return FAIL;
2498
2499 rep = REPLY_ITEM(REPLY_COUNT);
2500 rep->server = server;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002501 rep->reply = reply;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 rep->expr_result = expr;
2503 if (rep->reply == NULL)
2504 return FAIL;
2505
2506 ++REPLY_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 reply_received = 1;
2508 return OK;
2509}
2510
2511/*
2512 * Get a reply from server "server".
2513 * When "expr_res" is non NULL, get the result of an expression, otherwise a
2514 * server2client() message.
2515 * When non NULL, point to return code. 0 => OK, -1 => ERROR
2516 * If "remove" is TRUE, consume the message, the caller must free it then.
2517 * if "wait" is TRUE block until a message arrives (or the server exits).
2518 */
2519 char_u *
2520serverGetReply(HWND server, int *expr_res, int remove, int wait)
2521{
2522 int i;
2523 char_u *reply;
2524 reply_T *rep;
2525
2526 /* When waiting, loop until the message waiting for is received. */
2527 for (;;)
2528 {
2529 /* Reset this here, in case a message arrives while we are going
2530 * through the already received messages. */
2531 reply_received = 0;
2532
2533 for (i = 0; i < REPLY_COUNT; ++i)
2534 {
2535 rep = REPLY_ITEM(i);
2536 if (rep->server == server
2537 && ((rep->expr_result != 0) == (expr_res != NULL)))
2538 {
2539 /* Save the values we've found for later */
2540 reply = rep->reply;
2541 if (expr_res != NULL)
2542 *expr_res = rep->expr_result == 1 ? 0 : -1;
2543
2544 if (remove)
2545 {
2546 /* Move the rest of the list down to fill the gap */
2547 mch_memmove(rep, rep + 1,
2548 (REPLY_COUNT - i - 1) * sizeof(reply_T));
2549 --REPLY_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 }
2551
2552 /* Return the reply to the caller, who takes on responsibility
2553 * for freeing it if "remove" is TRUE. */
2554 return reply;
2555 }
2556 }
2557
2558 /* If we got here, we didn't find a reply. Return immediately if the
2559 * "wait" parameter isn't set. */
2560 if (!wait)
2561 break;
2562
2563 /* We need to wait for a reply. Enter a message loop until the
2564 * "reply_received" flag gets set. */
2565
2566 /* Loop until we receive a reply */
2567 while (reply_received == 0)
2568 {
2569 /* Wait for a SendMessage() call to us. This could be the reply
2570 * we are waiting for. Use a timeout of a second, to catch the
2571 * situation that the server died unexpectedly. */
2572 MsgWaitForMultipleObjects(0, NULL, TRUE, 1000, QS_ALLINPUT);
2573
2574 /* If the server has died, give up */
2575 if (!IsWindow(server))
2576 return NULL;
2577
2578 serverProcessPendingMessages();
2579 }
2580 }
2581
2582 return NULL;
2583}
2584
2585/*
2586 * Process any messages in the Windows message queue.
2587 */
2588 void
2589serverProcessPendingMessages(void)
2590{
2591 MSG msg;
2592
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02002593 while (pPeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 {
2595 TranslateMessage(&msg);
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02002596 pDispatchMessage(&msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 }
2598}
2599
2600#endif /* FEAT_CLIENTSERVER */
2601
2602#if defined(FEAT_GUI) || (defined(FEAT_PRINTER) && !defined(FEAT_POSTSCRIPT)) \
2603 || defined(PROTO)
2604
2605struct charset_pair
2606{
2607 char *name;
2608 BYTE charset;
2609};
2610
2611static struct charset_pair
2612charset_pairs[] =
2613{
2614 {"ANSI", ANSI_CHARSET},
2615 {"CHINESEBIG5", CHINESEBIG5_CHARSET},
2616 {"DEFAULT", DEFAULT_CHARSET},
2617 {"HANGEUL", HANGEUL_CHARSET},
2618 {"OEM", OEM_CHARSET},
2619 {"SHIFTJIS", SHIFTJIS_CHARSET},
2620 {"SYMBOL", SYMBOL_CHARSET},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 {"ARABIC", ARABIC_CHARSET},
2622 {"BALTIC", BALTIC_CHARSET},
2623 {"EASTEUROPE", EASTEUROPE_CHARSET},
2624 {"GB2312", GB2312_CHARSET},
2625 {"GREEK", GREEK_CHARSET},
2626 {"HEBREW", HEBREW_CHARSET},
2627 {"JOHAB", JOHAB_CHARSET},
2628 {"MAC", MAC_CHARSET},
2629 {"RUSSIAN", RUSSIAN_CHARSET},
2630 {"THAI", THAI_CHARSET},
2631 {"TURKISH", TURKISH_CHARSET},
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002632#ifdef VIETNAMESE_CHARSET
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 {"VIETNAMESE", VIETNAMESE_CHARSET},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634#endif
2635 {NULL, 0}
2636};
2637
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002638struct quality_pair
2639{
2640 char *name;
2641 DWORD quality;
2642};
2643
2644static struct quality_pair
2645quality_pairs[] = {
2646#ifdef CLEARTYPE_QUALITY
2647 {"CLEARTYPE", CLEARTYPE_QUALITY},
2648#endif
2649#ifdef ANTIALIASED_QUALITY
2650 {"ANTIALIASED", ANTIALIASED_QUALITY},
2651#endif
Bram Moolenaar73a733e2016-05-11 21:05:05 +02002652#ifdef NONANTIALIASED_QUALITY
2653 {"NONANTIALIASED", NONANTIALIASED_QUALITY},
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002654#endif
2655#ifdef PROOF_QUALITY
2656 {"PROOF", PROOF_QUALITY},
2657#endif
Bram Moolenaar4c9ce052016-04-04 21:06:19 +02002658#ifdef DRAFT_QUALITY
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002659 {"DRAFT", DRAFT_QUALITY},
2660#endif
2661 {"DEFAULT", DEFAULT_QUALITY},
2662 {NULL, 0}
2663};
2664
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665/*
2666 * Convert a charset ID to a name.
2667 * Return NULL when not recognized.
2668 */
2669 char *
2670charset_id2name(int id)
2671{
2672 struct charset_pair *cp;
2673
2674 for (cp = charset_pairs; cp->name != NULL; ++cp)
2675 if ((BYTE)id == cp->charset)
2676 break;
2677 return cp->name;
2678}
2679
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002680/*
2681 * Convert a quality ID to a name.
2682 * Return NULL when not recognized.
2683 */
2684 char *
2685quality_id2name(DWORD id)
2686{
2687 struct quality_pair *qp;
2688
2689 for (qp = quality_pairs; qp->name != NULL; ++qp)
2690 if (id == qp->quality)
2691 break;
2692 return qp->name;
2693}
2694
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695static const LOGFONT s_lfDefault =
2696{
2697 -12, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, DEFAULT_CHARSET,
2698 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
2699 PROOF_QUALITY, FIXED_PITCH | FF_DONTCARE,
2700 "Fixedsys" /* see _ReadVimIni */
2701};
2702
2703/* Initialise the "current height" to -12 (same as s_lfDefault) just
2704 * in case the user specifies a font in "guifont" with no size before a font
2705 * with an explicit size has been set. This defaults the size to this value
2706 * (-12 equates to roughly 9pt).
2707 */
2708int current_font_height = -12; /* also used in gui_w48.c */
2709
2710/* Convert a string representing a point size into pixels. The string should
2711 * be a positive decimal number, with an optional decimal point (eg, "12", or
2712 * "10.5"). The pixel value is returned, and a pointer to the next unconverted
2713 * character is stored in *end. The flag "vertical" says whether this
2714 * calculation is for a vertical (height) size or a horizontal (width) one.
2715 */
2716 static int
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002717points_to_pixels(char_u *str, char_u **end, int vertical, long_i pprinter_dc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718{
2719 int pixels;
2720 int points = 0;
2721 int divisor = 0;
2722 HWND hwnd = (HWND)0;
2723 HDC hdc;
2724 HDC printer_dc = (HDC)pprinter_dc;
2725
2726 while (*str != NUL)
2727 {
2728 if (*str == '.' && divisor == 0)
2729 {
2730 /* Start keeping a divisor, for later */
2731 divisor = 1;
2732 }
2733 else
2734 {
2735 if (!VIM_ISDIGIT(*str))
2736 break;
2737
2738 points *= 10;
2739 points += *str - '0';
2740 divisor *= 10;
2741 }
2742 ++str;
2743 }
2744
2745 if (divisor == 0)
2746 divisor = 1;
2747
2748 if (printer_dc == NULL)
2749 {
2750 hwnd = GetDesktopWindow();
2751 hdc = GetWindowDC(hwnd);
2752 }
2753 else
2754 hdc = printer_dc;
2755
2756 pixels = MulDiv(points,
2757 GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX),
2758 72 * divisor);
2759
2760 if (printer_dc == NULL)
2761 ReleaseDC(hwnd, hdc);
2762
2763 *end = str;
2764 return pixels;
2765}
2766
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002767/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 static int CALLBACK
2769font_enumproc(
2770 ENUMLOGFONT *elf,
2771 NEWTEXTMETRIC *ntm,
2772 int type,
2773 LPARAM lparam)
2774{
2775 /* Return value:
2776 * 0 = terminate now (monospace & ANSI)
2777 * 1 = continue, still no luck...
2778 * 2 = continue, but we have an acceptable LOGFONT
2779 * (monospace, not ANSI)
2780 * We use these values, as EnumFontFamilies returns 1 if the
2781 * callback function is never called. So, we check the return as
2782 * 0 = perfect, 2 = OK, 1 = no good...
2783 * It's not pretty, but it works!
2784 */
2785
2786 LOGFONT *lf = (LOGFONT *)(lparam);
2787
2788#ifndef FEAT_PROPORTIONAL_FONTS
2789 /* Ignore non-monospace fonts without further ado */
2790 if ((ntm->tmPitchAndFamily & 1) != 0)
2791 return 1;
2792#endif
2793
2794 /* Remember this LOGFONT as a "possible" */
2795 *lf = elf->elfLogFont;
2796
2797 /* Terminate the scan as soon as we find an ANSI font */
2798 if (lf->lfCharSet == ANSI_CHARSET
2799 || lf->lfCharSet == OEM_CHARSET
2800 || lf->lfCharSet == DEFAULT_CHARSET)
2801 return 0;
2802
2803 /* Continue the scan - we have a non-ANSI font */
2804 return 2;
2805}
2806
2807 static int
2808init_logfont(LOGFONT *lf)
2809{
2810 int n;
2811 HWND hwnd = GetDesktopWindow();
2812 HDC hdc = GetWindowDC(hwnd);
2813
2814 n = EnumFontFamilies(hdc,
2815 (LPCSTR)lf->lfFaceName,
2816 (FONTENUMPROC)font_enumproc,
2817 (LPARAM)lf);
2818
2819 ReleaseDC(hwnd, hdc);
2820
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002821 /* If we couldn't find a usable font, return failure */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 if (n == 1)
2823 return FAIL;
2824
2825 /* Tidy up the rest of the LOGFONT structure. We set to a basic
2826 * font - get_logfont() sets bold, italic, etc based on the user's
2827 * input.
2828 */
2829 lf->lfHeight = current_font_height;
2830 lf->lfWidth = 0;
2831 lf->lfItalic = FALSE;
2832 lf->lfUnderline = FALSE;
2833 lf->lfStrikeOut = FALSE;
2834 lf->lfWeight = FW_NORMAL;
2835
2836 /* Return success */
2837 return OK;
2838}
2839
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002840/*
2841 * Get font info from "name" into logfont "lf".
2842 * Return OK for a valid name, FAIL otherwise.
2843 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 int
2845get_logfont(
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002846 LOGFONT *lf,
2847 char_u *name,
2848 HDC printer_dc,
2849 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850{
2851 char_u *p;
2852 int i;
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002853 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 static LOGFONT *lastlf = NULL;
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002855#ifdef FEAT_MBYTE
2856 char_u *acpname = NULL;
2857#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858
2859 *lf = s_lfDefault;
2860 if (name == NULL)
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002861 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002863#ifdef FEAT_MBYTE
2864 /* Convert 'name' from 'encoding' to the current codepage, because
2865 * lf->lfFaceName uses the current codepage.
2866 * TODO: Use Wide APIs instead of ANSI APIs. */
2867 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
2868 {
2869 int len;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01002870 enc_to_acp(name, (int)STRLEN(name), &acpname, &len);
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002871 name = acpname;
2872 }
2873#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 if (STRCMP(name, "*") == 0)
2875 {
2876#if defined(FEAT_GUI_W32)
2877 CHOOSEFONT cf;
2878 /* if name is "*", bring up std font dialog: */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02002879 vim_memset(&cf, 0, sizeof(cf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880 cf.lStructSize = sizeof(cf);
2881 cf.hwndOwner = s_hwnd;
2882 cf.Flags = CF_SCREENFONTS | CF_FIXEDPITCHONLY | CF_INITTOLOGFONTSTRUCT;
2883 if (lastlf != NULL)
2884 *lf = *lastlf;
2885 cf.lpLogFont = lf;
2886 cf.nFontType = 0 ; //REGULAR_FONTTYPE;
2887 if (ChooseFont(&cf))
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002888 ret = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889#endif
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002890 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 }
2892
2893 /*
2894 * Split name up, it could be <name>:h<height>:w<width> etc.
2895 */
2896 for (p = name; *p && *p != ':'; p++)
2897 {
Bram Moolenaar38bc4952016-11-10 17:10:51 +01002898 if (p - name + 1 >= LF_FACESIZE)
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002899 goto theend; /* Name too long */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900 lf->lfFaceName[p - name] = *p;
2901 }
2902 if (p != name)
2903 lf->lfFaceName[p - name] = NUL;
2904
2905 /* First set defaults */
2906 lf->lfHeight = -12;
2907 lf->lfWidth = 0;
2908 lf->lfWeight = FW_NORMAL;
2909 lf->lfItalic = FALSE;
2910 lf->lfUnderline = FALSE;
2911 lf->lfStrikeOut = FALSE;
2912
2913 /*
2914 * If the font can't be found, try replacing '_' by ' '.
2915 */
2916 if (init_logfont(lf) == FAIL)
2917 {
2918 int did_replace = FALSE;
2919
2920 for (i = 0; lf->lfFaceName[i]; ++i)
2921 if (lf->lfFaceName[i] == '_')
2922 {
2923 lf->lfFaceName[i] = ' ';
2924 did_replace = TRUE;
2925 }
2926 if (!did_replace || init_logfont(lf) == FAIL)
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002927 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 }
2929
2930 while (*p == ':')
2931 p++;
2932
2933 /* Set the values found after ':' */
2934 while (*p)
2935 {
2936 switch (*p++)
2937 {
2938 case 'h':
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002939 lf->lfHeight = - points_to_pixels(p, &p, TRUE, (long_i)printer_dc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 break;
2941 case 'w':
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002942 lf->lfWidth = points_to_pixels(p, &p, FALSE, (long_i)printer_dc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 break;
2944 case 'b':
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 lf->lfWeight = FW_BOLD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 break;
2947 case 'i':
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948 lf->lfItalic = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 break;
2950 case 'u':
2951 lf->lfUnderline = TRUE;
2952 break;
2953 case 's':
2954 lf->lfStrikeOut = TRUE;
2955 break;
2956 case 'c':
2957 {
2958 struct charset_pair *cp;
2959
2960 for (cp = charset_pairs; cp->name != NULL; ++cp)
2961 if (STRNCMP(p, cp->name, strlen(cp->name)) == 0)
2962 {
2963 lf->lfCharSet = cp->charset;
2964 p += strlen(cp->name);
2965 break;
2966 }
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002967 if (cp->name == NULL && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 {
Bram Moolenaar051b7822005-05-19 21:00:46 +00002969 vim_snprintf((char *)IObuff, IOSIZE,
2970 _("E244: Illegal charset name \"%s\" in font name \"%s\""), p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 EMSG(IObuff);
2972 break;
2973 }
2974 break;
2975 }
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002976 case 'q':
2977 {
2978 struct quality_pair *qp;
2979
2980 for (qp = quality_pairs; qp->name != NULL; ++qp)
2981 if (STRNCMP(p, qp->name, strlen(qp->name)) == 0)
2982 {
2983 lf->lfQuality = qp->quality;
2984 p += strlen(qp->name);
2985 break;
2986 }
2987 if (qp->name == NULL && verbose)
2988 {
2989 vim_snprintf((char *)IObuff, IOSIZE,
2990 _("E244: Illegal quality name \"%s\" in font name \"%s\""), p, name);
2991 EMSG(IObuff);
2992 break;
2993 }
2994 break;
2995 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996 default:
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002997 if (verbose)
2998 {
Bram Moolenaar051b7822005-05-19 21:00:46 +00002999 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003000 _("E245: Illegal char '%c' in font name \"%s\""),
3001 p[-1], name);
3002 EMSG(IObuff);
3003 }
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003004 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 }
3006 while (*p == ':')
3007 p++;
3008 }
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003009 ret = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 /* ron: init lastlf */
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003013 if (ret == OK && printer_dc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014 {
3015 vim_free(lastlf);
3016 lastlf = (LOGFONT *)alloc(sizeof(LOGFONT));
3017 if (lastlf != NULL)
3018 mch_memmove(lastlf, lf, sizeof(LOGFONT));
3019 }
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003020#ifdef FEAT_MBYTE
3021 vim_free(acpname);
3022#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003024 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025}
3026
3027#endif /* defined(FEAT_GUI) || defined(FEAT_PRINTER) */
Bram Moolenaarf12d9832016-01-29 21:11:25 +01003028
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003029#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaarf12d9832016-01-29 21:11:25 +01003030/*
3031 * Initialize the Winsock dll.
3032 */
3033 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003034channel_init_winsock(void)
Bram Moolenaarf12d9832016-01-29 21:11:25 +01003035{
3036 WSADATA wsaData;
3037 int wsaerr;
3038
3039 if (WSInitialized)
3040 return;
3041
3042 wsaerr = WSAStartup(MAKEWORD(2, 2), &wsaData);
3043 if (wsaerr == 0)
3044 WSInitialized = TRUE;
3045}
3046#endif