blob: dd420122248f6092c14bd82c81de52a7d86aa314 [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{
Bram Moolenaar955f1982017-02-05 15:10:51 +0100204 exiting = TRUE;
205
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206 display_errors();
207
208 ml_close_all(TRUE); /* remove all memfiles */
209
210# ifdef FEAT_OLE
211 UninitOLE();
212# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100213# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214 if (WSInitialized)
215 {
216 WSInitialized = FALSE;
217 WSACleanup();
218 }
219# endif
220#ifdef DYNAMIC_GETTEXT
221 dyn_libintl_end();
222#endif
223
224 if (gui.in_use)
225 gui_exit(r);
Bram Moolenaar85c79d32007-02-20 01:59:20 +0000226
227#ifdef EXITFREE
228 free_all_mem();
229#endif
230
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231 exit(r);
232}
233
234#endif /* FEAT_GUI_MSWIN */
235
236
237/*
238 * Init the tables for toupper() and tolower().
239 */
240 void
241mch_early_init(void)
242{
243 int i;
244
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245 PlatformId();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246
247 /* Init the tables for toupper() and tolower() */
248 for (i = 0; i < 256; ++i)
249 toupper_tab[i] = tolower_tab[i] = i;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100250 CharUpperBuff((LPSTR)toupper_tab, 256);
251 CharLowerBuff((LPSTR)tolower_tab, 256);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252}
253
254
255/*
256 * Return TRUE if the input comes from a terminal, FALSE otherwise.
257 */
258 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100259mch_input_isatty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260{
261#ifdef FEAT_GUI_MSWIN
262 return OK; /* GUI always has a tty */
263#else
264 if (isatty(read_cmd_fd))
265 return TRUE;
266 return FALSE;
267#endif
268}
269
270#ifdef FEAT_TITLE
271/*
272 * mch_settitle(): set titlebar of our window
273 */
274 void
275mch_settitle(
276 char_u *title,
277 char_u *icon)
278{
279# ifdef FEAT_GUI_MSWIN
280 gui_mch_settitle(title, icon);
281# else
282 if (title != NULL)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000283 {
284# ifdef FEAT_MBYTE
285 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
286 {
287 /* Convert the title from 'encoding' to the active codepage. */
Bram Moolenaar36f692d2008-11-20 16:10:17 +0000288 WCHAR *wp = enc_to_utf16(title, NULL);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000289
290 if (wp != NULL)
291 {
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200292 SetConsoleTitleW(wp);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000293 vim_free(wp);
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200294 return;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000295 }
296 }
297# endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100298 SetConsoleTitle((LPCSTR)title);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000299 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000300# endif
301}
302
303
304/*
305 * Restore the window/icon title.
306 * which is one of:
307 * 1: Just restore title
308 * 2: Just restore icon (which we don't have)
309 * 3: Restore title and icon (which we don't have)
310 */
311 void
Bram Moolenaar1266d672017-02-01 13:43:36 +0100312mch_restore_title(int which UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313{
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 */
348 int
349mch_FullName(
350 char_u *fname,
351 char_u *buf,
352 int len,
Bram Moolenaar1266d672017-02-01 13:43:36 +0100353 int force UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354{
355 int nResult = FAIL;
356
357#ifdef __BORLANDC__
358 if (*fname == NUL) /* Borland behaves badly here - make it consistent */
359 nResult = mch_dirname(buf, len);
360 else
361#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000363#ifdef FEAT_MBYTE
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200364 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000365 {
366 WCHAR *wname;
367 WCHAR wbuf[MAX_PATH];
368 char_u *cname = NULL;
369
370 /* Use the wide function:
371 * - convert the fname from 'encoding' to UCS2.
372 * - invoke _wfullpath()
373 * - convert the result from UCS2 to 'encoding'.
374 */
Bram Moolenaar36f692d2008-11-20 16:10:17 +0000375 wname = enc_to_utf16(fname, NULL);
Bram Moolenaar374bf022014-11-05 19:33:24 +0100376 if (wname != NULL && _wfullpath(wbuf, wname, MAX_PATH) != NULL)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000377 {
Bram Moolenaar36f692d2008-11-20 16:10:17 +0000378 cname = utf16_to_enc((short_u *)wbuf, NULL);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000379 if (cname != NULL)
380 {
Bram Moolenaarfe3ca8d2005-07-18 21:43:02 +0000381 vim_strncpy(buf, cname, len - 1);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000382 nResult = OK;
383 }
384 }
385 vim_free(wname);
386 vim_free(cname);
387 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000388 if (nResult == FAIL) /* fall back to non-wide function */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000390 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100391 if (_fullpath((char *)buf, (const char *)fname, len - 1) == NULL)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000392 {
Bram Moolenaarfe3ca8d2005-07-18 21:43:02 +0000393 /* failed, use relative path name */
394 vim_strncpy(buf, fname, len - 1);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000395 }
396 else
397 nResult = OK;
398 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400
401#ifdef USE_FNAME_CASE
402 fname_case(buf, len);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000403#else
404 slash_adjust(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405#endif
406
407 return nResult;
408}
409
410
411/*
412 * Return TRUE if "fname" does not depend on the current directory.
413 */
414 int
415mch_isFullName(char_u *fname)
416{
Bram Moolenaard2a203b2013-08-30 16:51:18 +0200417#ifdef FEAT_MBYTE
418 /* WinNT and later can use _MAX_PATH wide characters for a pathname, which
419 * means that the maximum pathname is _MAX_PATH * 3 bytes when 'enc' is
420 * UTF-8. */
421 char szName[_MAX_PATH * 3 + 1];
422#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423 char szName[_MAX_PATH + 1];
Bram Moolenaard2a203b2013-08-30 16:51:18 +0200424#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000425
426 /* A name like "d:/foo" and "//server/share" is absolute */
427 if ((fname[0] && fname[1] == ':' && (fname[2] == '/' || fname[2] == '\\'))
428 || (fname[0] == fname[1] && (fname[0] == '/' || fname[0] == '\\')))
429 return TRUE;
430
431 /* A name that can't be made absolute probably isn't absolute. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100432 if (mch_FullName(fname, (char_u *)szName, sizeof(szName) - 1, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000433 return FALSE;
434
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100435 return pathcmp((const char *)fname, (const char *)szName, -1) == 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436}
437
438/*
439 * Replace all slashes by backslashes.
440 * This used to be the other way around, but MS-DOS sometimes has problems
441 * with slashes (e.g. in a command name). We can't have mixed slashes and
442 * backslashes, because comparing file names will not work correctly. The
443 * commands that use a file name should try to avoid the need to type a
444 * backslash twice.
445 * When 'shellslash' set do it the other way around.
Bram Moolenaarb4f6a462015-10-13 19:43:17 +0200446 * When the path looks like a URL leave it unmodified.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000447 */
448 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100449slash_adjust(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450{
Bram Moolenaarb4f6a462015-10-13 19:43:17 +0200451 if (path_with_url(p))
452 return;
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +0000453 while (*p)
454 {
455 if (*p == psepcN)
456 *p = psepc;
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100457 MB_PTR_ADV(p);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +0000458 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459}
460
Bram Moolenaar8767f522016-07-01 17:17:39 +0200461/* Use 64-bit stat functions if available. */
462#ifdef HAVE_STAT64
463# undef stat
464# undef _stat
465# undef _wstat
466# undef _fstat
467# define stat _stat64
468# define _stat _stat64
469# define _wstat _wstat64
470# define _fstat _fstat64
471#endif
472
Bram Moolenaar9d1685d2014-01-14 12:18:45 +0100473#if (defined(_MSC_VER) && (_MSC_VER >= 1300)) || defined(__MINGW32__)
Bram Moolenaar8962fda2013-09-29 19:05:21 +0200474# define OPEN_OH_ARGTYPE intptr_t
475#else
476# define OPEN_OH_ARGTYPE long
477#endif
478
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200479 static int
Bram Moolenaar8767f522016-07-01 17:17:39 +0200480stat_symlink_aware(const char *name, stat_T *stp)
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200481{
Bram Moolenaarfce7b3d2016-01-19 19:00:32 +0100482#if (defined(_MSC_VER) && (_MSC_VER < 1900)) || defined(__MINGW32__)
483 /* Work around for VC12 or earlier (and MinGW). stat() can't handle
484 * symlinks properly.
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200485 * VC9 or earlier: stat() doesn't support a symlink at all. It retrieves
486 * status of a symlink itself.
487 * VC10: stat() supports a symlink to a normal file, but it doesn't support
Bram Moolenaarfce7b3d2016-01-19 19:00:32 +0100488 * a symlink to a directory (always returns an error).
489 * VC11 and VC12: stat() doesn't return an error for a symlink to a
490 * directory, but it doesn't set S_IFDIR flag.
491 * MinGW: Same as VC9. */
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200492 WIN32_FIND_DATA findData;
493 HANDLE hFind, h;
494 DWORD attr = 0;
495 BOOL is_symlink = FALSE;
496
497 hFind = FindFirstFile(name, &findData);
498 if (hFind != INVALID_HANDLE_VALUE)
499 {
500 attr = findData.dwFileAttributes;
501 if ((attr & FILE_ATTRIBUTE_REPARSE_POINT)
502 && (findData.dwReserved0 == IO_REPARSE_TAG_SYMLINK))
503 is_symlink = TRUE;
504 FindClose(hFind);
505 }
506 if (is_symlink)
507 {
508 h = CreateFile(name, FILE_READ_ATTRIBUTES,
509 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
510 OPEN_EXISTING,
511 (attr & FILE_ATTRIBUTE_DIRECTORY)
512 ? FILE_FLAG_BACKUP_SEMANTICS : 0,
513 NULL);
514 if (h != INVALID_HANDLE_VALUE)
515 {
516 int fd, n;
517
Bram Moolenaar8962fda2013-09-29 19:05:21 +0200518 fd = _open_osfhandle((OPEN_OH_ARGTYPE)h, _O_RDONLY);
Bram Moolenaar8767f522016-07-01 17:17:39 +0200519 n = _fstat(fd, (struct _stat *)stp);
Bram Moolenaarfce7b3d2016-01-19 19:00:32 +0100520 if ((n == 0) && (attr & FILE_ATTRIBUTE_DIRECTORY))
521 stp->st_mode = (stp->st_mode & ~S_IFREG) | S_IFDIR;
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200522 _close(fd);
523 return n;
524 }
525 }
526#endif
527 return stat(name, stp);
528}
529
530#ifdef FEAT_MBYTE
531 static int
Bram Moolenaar8767f522016-07-01 17:17:39 +0200532wstat_symlink_aware(const WCHAR *name, stat_T *stp)
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200533{
Bram Moolenaarfce7b3d2016-01-19 19:00:32 +0100534# if (defined(_MSC_VER) && (_MSC_VER < 1900)) || defined(__MINGW32__)
535 /* Work around for VC12 or earlier (and MinGW). _wstat() can't handle
536 * symlinks properly.
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200537 * VC9 or earlier: _wstat() doesn't support a symlink at all. It retrieves
538 * status of a symlink itself.
539 * VC10: _wstat() supports a symlink to a normal file, but it doesn't
Bram Moolenaarfce7b3d2016-01-19 19:00:32 +0100540 * support a symlink to a directory (always returns an error).
541 * VC11 and VC12: _wstat() doesn't return an error for a symlink to a
542 * directory, but it doesn't set S_IFDIR flag.
543 * MinGW: Same as VC9. */
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200544 int n;
545 BOOL is_symlink = FALSE;
546 HANDLE hFind, h;
547 DWORD attr = 0;
548 WIN32_FIND_DATAW findDataW;
549
550 hFind = FindFirstFileW(name, &findDataW);
551 if (hFind != INVALID_HANDLE_VALUE)
552 {
553 attr = findDataW.dwFileAttributes;
554 if ((attr & FILE_ATTRIBUTE_REPARSE_POINT)
555 && (findDataW.dwReserved0 == IO_REPARSE_TAG_SYMLINK))
556 is_symlink = TRUE;
557 FindClose(hFind);
558 }
559 if (is_symlink)
560 {
561 h = CreateFileW(name, FILE_READ_ATTRIBUTES,
562 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
563 OPEN_EXISTING,
564 (attr & FILE_ATTRIBUTE_DIRECTORY)
565 ? FILE_FLAG_BACKUP_SEMANTICS : 0,
566 NULL);
567 if (h != INVALID_HANDLE_VALUE)
568 {
569 int fd;
570
Bram Moolenaar8962fda2013-09-29 19:05:21 +0200571 fd = _open_osfhandle((OPEN_OH_ARGTYPE)h, _O_RDONLY);
Bram Moolenaar8767f522016-07-01 17:17:39 +0200572 n = _fstat(fd, (struct _stat *)stp);
Bram Moolenaarfce7b3d2016-01-19 19:00:32 +0100573 if ((n == 0) && (attr & FILE_ATTRIBUTE_DIRECTORY))
574 stp->st_mode = (stp->st_mode & ~S_IFREG) | S_IFDIR;
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200575 _close(fd);
576 return n;
577 }
578 }
579# endif
Bram Moolenaar8767f522016-07-01 17:17:39 +0200580 return _wstat(name, (struct _stat *)stp);
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200581}
582#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583
584/*
585 * stat() can't handle a trailing '/' or '\', remove it first.
586 */
587 int
Bram Moolenaar8767f522016-07-01 17:17:39 +0200588vim_stat(const char *name, stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589{
Bram Moolenaard2a203b2013-08-30 16:51:18 +0200590#ifdef FEAT_MBYTE
591 /* WinNT and later can use _MAX_PATH wide characters for a pathname, which
592 * means that the maximum pathname is _MAX_PATH * 3 bytes when 'enc' is
593 * UTF-8. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100594 char_u buf[_MAX_PATH * 3 + 1];
Bram Moolenaard2a203b2013-08-30 16:51:18 +0200595#else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100596 char_u buf[_MAX_PATH + 1];
Bram Moolenaard2a203b2013-08-30 16:51:18 +0200597#endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100598 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599
Bram Moolenaard2a203b2013-08-30 16:51:18 +0200600 vim_strncpy((char_u *)buf, (char_u *)name, sizeof(buf) - 1);
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100601 p = buf + STRLEN(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602 if (p > buf)
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100603 MB_PTR_BACK(buf, p);
Bram Moolenaarb1cb35f2014-01-10 13:05:20 +0100604
605 /* Remove trailing '\\' except root path. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 if (p > buf && (*p == '\\' || *p == '/') && p[-1] != ':')
607 *p = NUL;
Bram Moolenaarb1cb35f2014-01-10 13:05:20 +0100608
609 if ((buf[0] == '\\' && buf[1] == '\\') || (buf[0] == '/' && buf[1] == '/'))
610 {
611 /* UNC root path must be followed by '\\'. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100612 p = vim_strpbrk(buf + 2, (char_u *)"\\/");
Bram Moolenaarb1cb35f2014-01-10 13:05:20 +0100613 if (p != NULL)
614 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100615 p = vim_strpbrk(p + 1, (char_u *)"\\/");
Bram Moolenaarb1cb35f2014-01-10 13:05:20 +0100616 if (p == NULL)
617 STRCAT(buf, "\\");
618 }
619 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620#ifdef FEAT_MBYTE
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200621 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622 {
Bram Moolenaar36f692d2008-11-20 16:10:17 +0000623 WCHAR *wp = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624 int n;
625
626 if (wp != NULL)
627 {
Bram Moolenaar8767f522016-07-01 17:17:39 +0200628 n = wstat_symlink_aware(wp, stp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629 vim_free(wp);
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200630 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631 }
632 }
633#endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100634 return stat_symlink_aware((char *)buf, stp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635}
636
637#if defined(FEAT_GUI_MSWIN) || defined(PROTO)
638 void
Bram Moolenaar1266d672017-02-01 13:43:36 +0100639mch_settmode(int tmode UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640{
641 /* nothing to do */
642}
643
644 int
645mch_get_shellsize(void)
646{
647 /* never used */
648 return OK;
649}
650
651 void
652mch_set_shellsize(void)
653{
654 /* never used */
655}
656
657/*
658 * Rows and/or Columns has changed.
659 */
660 void
661mch_new_shellsize(void)
662{
663 /* never used */
664}
665
666#endif
667
668/*
669 * We have no job control, so fake it by starting a new shell.
670 */
671 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100672mch_suspend(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673{
674 suspend_shell();
675}
676
677#if defined(USE_MCH_ERRMSG) || defined(PROTO)
678
679#ifdef display_errors
680# undef display_errors
681#endif
682
683/*
684 * Display the saved error message(s).
685 */
686 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100687display_errors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688{
689 char *p;
690
691 if (error_ga.ga_data != NULL)
692 {
693 /* avoid putting up a message box with blanks only */
694 for (p = (char *)error_ga.ga_data; *p; ++p)
695 if (!isspace(*p))
696 {
Bram Moolenaarc17ef8e2006-03-25 21:48:58 +0000697 (void)gui_mch_dialog(
698#ifdef FEAT_GUI
699 gui.starting ? VIM_INFO :
700#endif
701 VIM_ERROR,
702#ifdef FEAT_GUI
703 gui.starting ? (char_u *)_("Message") :
704#endif
705 (char_u *)_("Error"),
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100706 (char_u *)p, (char_u *)_("&Ok"),
707 1, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 break;
709 }
710 ga_clear(&error_ga);
711 }
712}
713#endif
714
715
716/*
717 * Return TRUE if "p" contain a wildcard that can be expanded by
718 * dos_expandpath().
719 */
720 int
721mch_has_exp_wildcard(char_u *p)
722{
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100723 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 {
725 if (vim_strchr((char_u *)"?*[", *p) != NULL
726 || (*p == '~' && p[1] != NUL))
727 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 }
729 return FALSE;
730}
731
732/*
733 * Return TRUE if "p" contain a wildcard or a "~1" kind of thing (could be a
734 * shortened file name).
735 */
736 int
737mch_has_wildcard(char_u *p)
738{
Bram Moolenaar91acfff2017-03-12 19:22:36 +0100739 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740 {
741 if (vim_strchr((char_u *)
742# ifdef VIM_BACKTICK
743 "?*$[`"
744# else
745 "?*$["
746# endif
747 , *p) != NULL
748 || (*p == '~' && p[1] != NUL))
749 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 }
751 return FALSE;
752}
753
754
755/*
756 * The normal _chdir() does not change the default drive. This one does.
757 * Returning 0 implies success; -1 implies failure.
758 */
759 int
760mch_chdir(char *path)
761{
762 if (path[0] == NUL) /* just checking... */
763 return -1;
764
Bram Moolenaara2974d72009-07-14 16:38:36 +0000765 if (p_verbose >= 5)
766 {
767 verbose_enter();
768 smsg((char_u *)"chdir(%s)", path);
769 verbose_leave();
770 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 if (isalpha(path[0]) && path[1] == ':') /* has a drive name */
772 {
773 /* If we can change to the drive, skip that part of the path. If we
774 * can't then the current directory may be invalid, try using chdir()
775 * with the whole path. */
776 if (_chdrive(TOLOWER_ASC(path[0]) - 'a' + 1) == 0)
777 path += 2;
778 }
779
780 if (*path == NUL) /* drive name only */
781 return 0;
782
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000783#ifdef FEAT_MBYTE
784 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
785 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100786 WCHAR *p = enc_to_utf16((char_u *)path, NULL);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000787 int n;
788
789 if (p != NULL)
790 {
791 n = _wchdir(p);
792 vim_free(p);
Bram Moolenaarcea912a2016-10-12 14:20:24 +0200793 return n;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000794 }
795 }
796#endif
797
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 return chdir(path); /* let the normal chdir() do the rest */
799}
800
801
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802#ifdef FEAT_GUI_MSWIN
803/*
804 * return non-zero if a character is available
805 */
806 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100807mch_char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808{
809 /* never used */
810 return TRUE;
811}
Bram Moolenaare9c21ae2017-08-03 20:44:48 +0200812
813# if defined(FEAT_TERMINAL) || defined(PROTO)
814/*
815 * Check for any pending input or messages.
816 */
817 int
818mch_check_messages(void)
819{
820 /* TODO: check for messages */
821 return TRUE;
822}
823# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824#endif
825
826
827/*
828 * set screen mode, always fails.
829 */
830 int
Bram Moolenaar1266d672017-02-01 13:43:36 +0100831mch_screenmode(char_u *arg UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832{
833 EMSG(_(e_screenmode));
834 return FAIL;
835}
836
837
838#if defined(FEAT_LIBCALL) || defined(PROTO)
839/*
840 * Call a DLL routine which takes either a string or int param
841 * and returns an allocated string.
842 * Return OK if it worked, FAIL if not.
843 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844typedef LPTSTR (*MYSTRPROCSTR)(LPTSTR);
845typedef LPTSTR (*MYINTPROCSTR)(int);
846typedef int (*MYSTRPROCINT)(LPTSTR);
847typedef int (*MYINTPROCINT)(int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849/*
850 * Check if a pointer points to a valid NUL terminated string.
851 * Return the length of the string, including terminating NUL.
852 * Returns 0 for an invalid pointer, 1 for an empty string.
853 */
854 static size_t
855check_str_len(char_u *str)
856{
857 SYSTEM_INFO si;
858 MEMORY_BASIC_INFORMATION mbi;
859 size_t length = 0;
860 size_t i;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100861 const char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862
863 /* get page size */
864 GetSystemInfo(&si);
865
866 /* get memory information */
867 if (VirtualQuery(str, &mbi, sizeof(mbi)))
868 {
869 /* pre cast these (typing savers) */
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000870 long_u dwStr = (long_u)str;
871 long_u dwBaseAddress = (long_u)mbi.BaseAddress;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872
873 /* get start address of page that str is on */
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000874 long_u strPage = dwStr - (dwStr - dwBaseAddress) % si.dwPageSize;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875
876 /* get length from str to end of page */
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000877 long_u pageLength = si.dwPageSize - (dwStr - strPage);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878
Bram Moolenaar442b4222010-05-24 21:34:22 +0200879 for (p = str; !IsBadReadPtr(p, (UINT)pageLength);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 p += pageLength, pageLength = si.dwPageSize)
881 for (i = 0; i < pageLength; ++i, ++length)
882 if (p[i] == NUL)
883 return length + 1;
884 }
885
886 return 0;
887}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200889/*
890 * Passed to do_in_runtimepath() to load a vim.ico file.
891 */
892 static void
893mch_icon_load_cb(char_u *fname, void *cookie)
894{
895 HANDLE *h = (HANDLE *)cookie;
896
897 *h = LoadImage(NULL,
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100898 (LPSTR)fname,
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200899 IMAGE_ICON,
900 64,
901 64,
902 LR_LOADFROMFILE | LR_LOADMAP3DCOLORS);
903}
904
905/*
906 * Try loading an icon file from 'runtimepath'.
907 */
908 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100909mch_icon_load(HANDLE *iconp)
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200910{
911 return do_in_runtimepath((char_u *)"bitmaps/vim.ico",
Bram Moolenaar7f8989d2016-03-12 22:11:39 +0100912 0, mch_icon_load_cb, iconp);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200913}
914
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 int
916mch_libcall(
917 char_u *libname,
918 char_u *funcname,
919 char_u *argstring, /* NULL when using a argint */
920 int argint,
921 char_u **string_result,/* NULL when using number_result */
922 int *number_result)
923{
924 HINSTANCE hinstLib;
925 MYSTRPROCSTR ProcAdd;
926 MYINTPROCSTR ProcAddI;
927 char_u *retval_str = NULL;
928 int retval_int = 0;
929 size_t len;
930
931 BOOL fRunTimeLinkSuccess = FALSE;
932
933 // Get a handle to the DLL module.
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100934 hinstLib = vimLoadLib((char *)libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935
936 // If the handle is valid, try to get the function address.
937 if (hinstLib != NULL)
938 {
939#ifdef HAVE_TRY_EXCEPT
940 __try
941 {
942#endif
943 if (argstring != NULL)
944 {
945 /* Call with string argument */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100946 ProcAdd = (MYSTRPROCSTR)GetProcAddress(hinstLib, (LPCSTR)funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 if ((fRunTimeLinkSuccess = (ProcAdd != NULL)) != 0)
948 {
949 if (string_result == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100950 retval_int = ((MYSTRPROCINT)ProcAdd)((LPSTR)argstring);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100952 retval_str = (char_u *)(ProcAdd)((LPSTR)argstring);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 }
954 }
955 else
956 {
957 /* Call with number argument */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100958 ProcAddI = (MYINTPROCSTR) GetProcAddress(hinstLib, (LPCSTR)funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 if ((fRunTimeLinkSuccess = (ProcAddI != NULL)) != 0)
960 {
961 if (string_result == NULL)
962 retval_int = ((MYINTPROCINT)ProcAddI)(argint);
963 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100964 retval_str = (char_u *)(ProcAddI)(argint);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965 }
966 }
967
968 // Save the string before we free the library.
969 // Assume that a "1" result is an illegal pointer.
970 if (string_result == NULL)
971 *number_result = retval_int;
972 else if (retval_str != NULL
Bram Moolenaarcf7164a2016-02-20 13:55:06 +0100973 && (len = check_str_len(retval_str)) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 {
975 *string_result = lalloc((long_u)len, TRUE);
976 if (*string_result != NULL)
977 mch_memmove(*string_result, retval_str, len);
978 }
979
980#ifdef HAVE_TRY_EXCEPT
981 }
982 __except(EXCEPTION_EXECUTE_HANDLER)
983 {
984 if (GetExceptionCode() == EXCEPTION_STACK_OVERFLOW)
985 RESETSTKOFLW();
986 fRunTimeLinkSuccess = 0;
987 }
988#endif
989
990 // Free the DLL module.
991 (void)FreeLibrary(hinstLib);
992 }
993
994 if (!fRunTimeLinkSuccess)
995 {
996 EMSG2(_(e_libcall), funcname);
997 return FAIL;
998 }
999
1000 return OK;
1001}
1002#endif
1003
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004/*
1005 * Debugging helper: expose the MCH_WRITE_DUMP stuff to other modules
1006 */
1007 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01001008DumpPutS(const char *psz UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009{
1010# ifdef MCH_WRITE_DUMP
1011 if (fdDump)
1012 {
1013 fputs(psz, fdDump);
1014 if (psz[strlen(psz) - 1] != '\n')
1015 fputc('\n', fdDump);
1016 fflush(fdDump);
1017 }
1018# endif
1019}
1020
1021#ifdef _DEBUG
1022
1023void __cdecl
1024Trace(
1025 char *pszFormat,
1026 ...)
1027{
1028 CHAR szBuff[2048];
1029 va_list args;
1030
1031 va_start(args, pszFormat);
1032 vsprintf(szBuff, pszFormat, args);
1033 va_end(args);
1034
1035 OutputDebugString(szBuff);
1036}
1037
1038#endif //_DEBUG
1039
Bram Moolenaar843ee412004-06-30 16:16:41 +00001040#if !defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001041# ifdef FEAT_TITLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042extern HWND g_hWnd; /* This is in os_win32.c. */
1043# endif
1044
1045/*
1046 * Showing the printer dialog is tricky since we have no GUI
1047 * window to parent it. The following routines are needed to
1048 * get the window parenting and Z-order to work properly.
1049 */
1050 static void
1051GetConsoleHwnd(void)
1052{
1053# define MY_BUFSIZE 1024 // Buffer size for console window titles.
1054
1055 char pszNewWindowTitle[MY_BUFSIZE]; // Contains fabricated WindowTitle.
1056 char pszOldWindowTitle[MY_BUFSIZE]; // Contains original WindowTitle.
1057
1058 /* Skip if it's already set. */
1059 if (s_hwnd != 0)
1060 return;
1061
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001062# ifdef FEAT_TITLE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 /* Window handle may have been found by init code (Windows NT only) */
1064 if (g_hWnd != 0)
1065 {
1066 s_hwnd = g_hWnd;
1067 return;
1068 }
1069# endif
1070
1071 GetConsoleTitle(pszOldWindowTitle, MY_BUFSIZE);
1072
1073 wsprintf(pszNewWindowTitle, "%s/%d/%d",
1074 pszOldWindowTitle,
1075 GetTickCount(),
1076 GetCurrentProcessId());
1077 SetConsoleTitle(pszNewWindowTitle);
1078 Sleep(40);
1079 s_hwnd = FindWindow(NULL, pszNewWindowTitle);
1080
1081 SetConsoleTitle(pszOldWindowTitle);
1082}
Bram Moolenaar843ee412004-06-30 16:16:41 +00001083
1084/*
1085 * Console implementation of ":winpos".
1086 */
1087 int
1088mch_get_winpos(int *x, int *y)
1089{
1090 RECT rect;
1091
1092 GetConsoleHwnd();
1093 GetWindowRect(s_hwnd, &rect);
1094 *x = rect.left;
1095 *y = rect.top;
1096 return OK;
1097}
1098
1099/*
1100 * Console implementation of ":winpos x y".
1101 */
1102 void
1103mch_set_winpos(int x, int y)
1104{
1105 GetConsoleHwnd();
1106 SetWindowPos(s_hwnd, NULL, x, y, 0, 0,
1107 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
1108}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109#endif
1110
1111#if (defined(FEAT_PRINTER) && !defined(FEAT_POSTSCRIPT)) || defined(PROTO)
1112
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113/*=================================================================
1114 * Win32 printer stuff
1115 */
1116
1117static HFONT prt_font_handles[2][2][2];
1118static PRINTDLG prt_dlg;
1119static const int boldface[2] = {FW_REGULAR, FW_BOLD};
1120static TEXTMETRIC prt_tm;
1121static int prt_line_height;
1122static int prt_number_width;
1123static int prt_left_margin;
1124static int prt_right_margin;
1125static int prt_top_margin;
1126static char_u szAppName[] = TEXT("VIM");
1127static HWND hDlgPrint;
1128static int *bUserAbort = NULL;
1129static char_u *prt_name = NULL;
1130
1131/* Defines which are also in vim.rc. */
1132#define IDC_BOX1 400
1133#define IDC_PRINTTEXT1 401
1134#define IDC_PRINTTEXT2 402
1135#define IDC_PROGRESS 403
1136
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001137#if !defined(FEAT_MBYTE)
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001138# define vimSetDlgItemText(h, i, s) SetDlgItemText(h, i, s)
1139#else
1140 static BOOL
1141vimSetDlgItemText(HWND hDlg, int nIDDlgItem, char_u *s)
1142{
1143 WCHAR *wp = NULL;
1144 BOOL ret;
1145
1146 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
1147 {
1148 wp = enc_to_utf16(s, NULL);
1149 }
1150 if (wp != NULL)
1151 {
1152 ret = SetDlgItemTextW(hDlg, nIDDlgItem, wp);
1153 vim_free(wp);
1154 return ret;
1155 }
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001156 return SetDlgItemText(hDlg, nIDDlgItem, (LPCSTR)s);
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001157}
1158#endif
1159
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160/*
1161 * Convert BGR to RGB for Windows GDI calls
1162 */
1163 static COLORREF
1164swap_me(COLORREF colorref)
1165{
1166 int temp;
1167 char *ptr = (char *)&colorref;
1168
1169 temp = *(ptr);
1170 *(ptr ) = *(ptr + 2);
1171 *(ptr + 2) = temp;
1172 return colorref;
1173}
1174
Bram Moolenaared39e1d2008-08-09 17:55:22 +00001175/* Attempt to make this work for old and new compilers */
Bram Moolenaar9f733d12011-09-21 20:09:42 +02001176#if !defined(_WIN64) && (!defined(_MSC_VER) || _MSC_VER < 1300)
Bram Moolenaared39e1d2008-08-09 17:55:22 +00001177# define PDP_RETVAL BOOL
1178#else
1179# define PDP_RETVAL INT_PTR
1180#endif
1181
Bram Moolenaared39e1d2008-08-09 17:55:22 +00001182 static PDP_RETVAL CALLBACK
Bram Moolenaar1266d672017-02-01 13:43:36 +01001183PrintDlgProc(
1184 HWND hDlg,
1185 UINT message,
1186 WPARAM wParam UNUSED,
1187 LPARAM lParam UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188{
1189#ifdef FEAT_GETTEXT
1190 NONCLIENTMETRICS nm;
1191 static HFONT hfont;
1192#endif
1193
1194 switch (message)
1195 {
1196 case WM_INITDIALOG:
1197#ifdef FEAT_GETTEXT
1198 nm.cbSize = sizeof(NONCLIENTMETRICS);
1199 if (SystemParametersInfo(
1200 SPI_GETNONCLIENTMETRICS,
1201 sizeof(NONCLIENTMETRICS),
1202 &nm,
1203 0))
1204 {
1205 char buff[MAX_PATH];
1206 int i;
1207
1208 /* Translate the dialog texts */
1209 hfont = CreateFontIndirect(&nm.lfMessageFont);
1210 for (i = IDC_PRINTTEXT1; i <= IDC_PROGRESS; i++)
1211 {
1212 SendDlgItemMessage(hDlg, i, WM_SETFONT, (WPARAM)hfont, 1);
1213 if (GetDlgItemText(hDlg,i, buff, sizeof(buff)))
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001214 vimSetDlgItemText(hDlg,i, (char_u *)_(buff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 }
1216 SendDlgItemMessage(hDlg, IDCANCEL,
1217 WM_SETFONT, (WPARAM)hfont, 1);
1218 if (GetDlgItemText(hDlg,IDCANCEL, buff, sizeof(buff)))
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001219 vimSetDlgItemText(hDlg,IDCANCEL, (char_u *)_(buff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 }
1221#endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001222 SetWindowText(hDlg, (LPCSTR)szAppName);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 if (prt_name != NULL)
1224 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001225 vimSetDlgItemText(hDlg, IDC_PRINTTEXT2, (char_u *)prt_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 vim_free(prt_name);
1227 prt_name = NULL;
1228 }
1229 EnableMenuItem(GetSystemMenu(hDlg, FALSE), SC_CLOSE, MF_GRAYED);
1230#ifndef FEAT_GUI
1231 BringWindowToTop(s_hwnd);
1232#endif
1233 return TRUE;
1234
1235 case WM_COMMAND:
1236 *bUserAbort = TRUE;
1237 EnableWindow(GetParent(hDlg), TRUE);
1238 DestroyWindow(hDlg);
1239 hDlgPrint = NULL;
1240#ifdef FEAT_GETTEXT
1241 DeleteObject(hfont);
1242#endif
1243 return TRUE;
1244 }
1245 return FALSE;
1246}
1247
1248 static BOOL CALLBACK
Bram Moolenaar1266d672017-02-01 13:43:36 +01001249AbortProc(HDC hdcPrn UNUSED, int iCode UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250{
1251 MSG msg;
1252
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02001253 while (!*bUserAbort && pPeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 {
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02001255 if (!hDlgPrint || !pIsDialogMessage(hDlgPrint, &msg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 {
1257 TranslateMessage(&msg);
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02001258 pDispatchMessage(&msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 }
1260 }
1261 return !*bUserAbort;
1262}
1263
1264#ifndef FEAT_GUI
1265
Bram Moolenaar26fdd7d2011-11-30 13:42:44 +01001266 static UINT_PTR CALLBACK
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267PrintHookProc(
1268 HWND hDlg, // handle to dialog box
1269 UINT uiMsg, // message identifier
1270 WPARAM wParam, // message parameter
1271 LPARAM lParam // message parameter
1272 )
1273{
1274 HWND hwndOwner;
1275 RECT rc, rcDlg, rcOwner;
1276 PRINTDLG *pPD;
1277
1278 if (uiMsg == WM_INITDIALOG)
1279 {
1280 // Get the owner window and dialog box rectangles.
1281 if ((hwndOwner = GetParent(hDlg)) == NULL)
1282 hwndOwner = GetDesktopWindow();
1283
1284 GetWindowRect(hwndOwner, &rcOwner);
1285 GetWindowRect(hDlg, &rcDlg);
1286 CopyRect(&rc, &rcOwner);
1287
1288 // Offset the owner and dialog box rectangles so that
1289 // right and bottom values represent the width and
1290 // height, and then offset the owner again to discard
1291 // space taken up by the dialog box.
1292
1293 OffsetRect(&rcDlg, -rcDlg.left, -rcDlg.top);
1294 OffsetRect(&rc, -rc.left, -rc.top);
1295 OffsetRect(&rc, -rcDlg.right, -rcDlg.bottom);
1296
1297 // The new position is the sum of half the remaining
1298 // space and the owner's original position.
1299
1300 SetWindowPos(hDlg,
1301 HWND_TOP,
1302 rcOwner.left + (rc.right / 2),
1303 rcOwner.top + (rc.bottom / 2),
1304 0, 0, // ignores size arguments
1305 SWP_NOSIZE);
1306
1307 /* tackle the printdlg copiesctrl problem */
1308 pPD = (PRINTDLG *)lParam;
1309 pPD->nCopies = (WORD)pPD->lCustData;
1310 SetDlgItemInt( hDlg, edt3, pPD->nCopies, FALSE );
1311 /* Bring the window to top */
1312 BringWindowToTop(GetParent(hDlg));
1313 SetForegroundWindow(hDlg);
1314 }
1315
1316 return FALSE;
1317}
1318#endif
1319
1320 void
1321mch_print_cleanup(void)
1322{
1323 int pifItalic;
1324 int pifBold;
1325 int pifUnderline;
1326
1327 for (pifBold = 0; pifBold <= 1; pifBold++)
1328 for (pifItalic = 0; pifItalic <= 1; pifItalic++)
1329 for (pifUnderline = 0; pifUnderline <= 1; pifUnderline++)
1330 DeleteObject(prt_font_handles[pifBold][pifItalic][pifUnderline]);
1331
1332 if (prt_dlg.hDC != NULL)
1333 DeleteDC(prt_dlg.hDC);
1334 if (!*bUserAbort)
1335 SendMessage(hDlgPrint, WM_COMMAND, 0, 0);
1336}
1337
1338 static int
1339to_device_units(int idx, int dpi, int physsize, int offset, int def_number)
1340{
1341 int ret = 0;
1342 int u;
1343 int nr;
1344
1345 u = prt_get_unit(idx);
1346 if (u == PRT_UNIT_NONE)
1347 {
1348 u = PRT_UNIT_PERC;
1349 nr = def_number;
1350 }
1351 else
1352 nr = printer_opts[idx].number;
1353
1354 switch (u)
1355 {
1356 case PRT_UNIT_PERC:
1357 ret = (physsize * nr) / 100;
1358 break;
1359 case PRT_UNIT_INCH:
1360 ret = (nr * dpi);
1361 break;
1362 case PRT_UNIT_MM:
1363 ret = (nr * 10 * dpi) / 254;
1364 break;
1365 case PRT_UNIT_POINT:
1366 ret = (nr * 10 * dpi) / 720;
1367 break;
1368 }
1369
1370 if (ret < offset)
1371 return 0;
1372 else
1373 return ret - offset;
1374}
1375
1376 static int
1377prt_get_cpl(void)
1378{
1379 int hr;
1380 int phyw;
1381 int dvoff;
1382 int rev_offset;
1383 int dpi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384
1385 GetTextMetrics(prt_dlg.hDC, &prt_tm);
1386 prt_line_height = prt_tm.tmHeight + prt_tm.tmExternalLeading;
1387
1388 hr = GetDeviceCaps(prt_dlg.hDC, HORZRES);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 phyw = GetDeviceCaps(prt_dlg.hDC, PHYSICALWIDTH);
1390 dvoff = GetDeviceCaps(prt_dlg.hDC, PHYSICALOFFSETX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 dpi = GetDeviceCaps(prt_dlg.hDC, LOGPIXELSX);
1392
1393 rev_offset = phyw - (dvoff + hr);
1394
1395 prt_left_margin = to_device_units(OPT_PRINT_LEFT, dpi, phyw, dvoff, 10);
1396 if (prt_use_number())
1397 {
1398 prt_number_width = PRINT_NUMBER_WIDTH * prt_tm.tmAveCharWidth;
1399 prt_left_margin += prt_number_width;
1400 }
1401 else
1402 prt_number_width = 0;
1403
1404 prt_right_margin = hr - to_device_units(OPT_PRINT_RIGHT, dpi, phyw,
1405 rev_offset, 5);
1406
1407 return (prt_right_margin - prt_left_margin) / prt_tm.tmAveCharWidth;
1408}
1409
1410 static int
1411prt_get_lpp(void)
1412{
1413 int vr;
1414 int phyw;
1415 int dvoff;
1416 int rev_offset;
1417 int bottom_margin;
1418 int dpi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419
1420 vr = GetDeviceCaps(prt_dlg.hDC, VERTRES);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 phyw = GetDeviceCaps(prt_dlg.hDC, PHYSICALHEIGHT);
1422 dvoff = GetDeviceCaps(prt_dlg.hDC, PHYSICALOFFSETY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 dpi = GetDeviceCaps(prt_dlg.hDC, LOGPIXELSY);
1424
1425 rev_offset = phyw - (dvoff + vr);
1426
1427 prt_top_margin = to_device_units(OPT_PRINT_TOP, dpi, phyw, dvoff, 5);
1428
1429 /* adjust top margin if there is a header */
1430 prt_top_margin += prt_line_height * prt_header_height();
1431
1432 bottom_margin = vr - to_device_units(OPT_PRINT_BOT, dpi, phyw,
1433 rev_offset, 5);
1434
1435 return (bottom_margin - prt_top_margin) / prt_line_height;
1436}
1437
1438 int
1439mch_print_init(prt_settings_T *psettings, char_u *jobname, int forceit)
1440{
1441 static HGLOBAL stored_dm = NULL;
1442 static HGLOBAL stored_devn = NULL;
1443 static int stored_nCopies = 1;
1444 static int stored_nFlags = 0;
1445
1446 LOGFONT fLogFont;
1447 int pifItalic;
1448 int pifBold;
1449 int pifUnderline;
1450
1451 DEVMODE *mem;
1452 DEVNAMES *devname;
1453 int i;
1454
1455 bUserAbort = &(psettings->user_abort);
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02001456 vim_memset(&prt_dlg, 0, sizeof(PRINTDLG));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 prt_dlg.lStructSize = sizeof(PRINTDLG);
1458#ifndef FEAT_GUI
1459 GetConsoleHwnd(); /* get value of s_hwnd */
1460#endif
1461 prt_dlg.hwndOwner = s_hwnd;
1462 prt_dlg.Flags = PD_NOPAGENUMS | PD_NOSELECTION | PD_RETURNDC;
1463 if (!forceit)
1464 {
1465 prt_dlg.hDevMode = stored_dm;
1466 prt_dlg.hDevNames = stored_devn;
1467 prt_dlg.lCustData = stored_nCopies; // work around bug in print dialog
1468#ifndef FEAT_GUI
1469 /*
1470 * Use hook to prevent console window being sent to back
1471 */
1472 prt_dlg.lpfnPrintHook = PrintHookProc;
1473 prt_dlg.Flags |= PD_ENABLEPRINTHOOK;
1474#endif
1475 prt_dlg.Flags |= stored_nFlags;
1476 }
1477
1478 /*
1479 * If bang present, return default printer setup with no dialog
1480 * never show dialog if we are running over telnet
1481 */
1482 if (forceit
1483#ifndef FEAT_GUI
1484 || !term_console
1485#endif
1486 )
1487 {
1488 prt_dlg.Flags |= PD_RETURNDEFAULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489 /*
1490 * MSDN suggests setting the first parameter to WINSPOOL for
1491 * NT, but NULL appears to work just as well.
1492 */
1493 if (*p_pdev != NUL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001494 prt_dlg.hDC = CreateDC(NULL, (LPCSTR)p_pdev, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 {
1497 prt_dlg.Flags |= PD_RETURNDEFAULT;
1498 if (PrintDlg(&prt_dlg) == 0)
1499 goto init_fail_dlg;
1500 }
1501 }
1502 else if (PrintDlg(&prt_dlg) == 0)
1503 goto init_fail_dlg;
1504 else
1505 {
1506 /*
1507 * keep the previous driver context
1508 */
1509 stored_dm = prt_dlg.hDevMode;
1510 stored_devn = prt_dlg.hDevNames;
1511 stored_nFlags = prt_dlg.Flags;
1512 stored_nCopies = prt_dlg.nCopies;
1513 }
1514
1515 if (prt_dlg.hDC == NULL)
1516 {
1517 EMSG(_("E237: Printer selection failed"));
1518 mch_print_cleanup();
1519 return FALSE;
1520 }
1521
1522 /* Not all printer drivers report the support of color (or grey) in the
1523 * same way. Let's set has_color if there appears to be some way to print
1524 * more than B&W. */
1525 i = GetDeviceCaps(prt_dlg.hDC, NUMCOLORS);
1526 psettings->has_color = (GetDeviceCaps(prt_dlg.hDC, BITSPIXEL) > 1
1527 || GetDeviceCaps(prt_dlg.hDC, PLANES) > 1
1528 || i > 2 || i == -1);
1529
1530 /* Ensure all font styles are baseline aligned */
1531 SetTextAlign(prt_dlg.hDC, TA_BASELINE|TA_LEFT);
1532
1533 /*
1534 * On some windows systems the nCopies parameter is not
1535 * passed back correctly. It must be retrieved from the
1536 * hDevMode struct.
1537 */
1538 mem = (DEVMODE *)GlobalLock(prt_dlg.hDevMode);
1539 if (mem != NULL)
1540 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 if (mem->dmCopies != 1)
1542 stored_nCopies = mem->dmCopies;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 if ((mem->dmFields & DM_DUPLEX) && (mem->dmDuplex & ~DMDUP_SIMPLEX))
1544 psettings->duplex = TRUE;
1545 if ((mem->dmFields & DM_COLOR) && (mem->dmColor & DMCOLOR_COLOR))
1546 psettings->has_color = TRUE;
1547 }
1548 GlobalUnlock(prt_dlg.hDevMode);
1549
1550 devname = (DEVNAMES *)GlobalLock(prt_dlg.hDevNames);
1551 if (devname != 0)
1552 {
1553 char_u *printer_name = (char_u *)devname + devname->wDeviceOffset;
1554 char_u *port_name = (char_u *)devname +devname->wOutputOffset;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001555 char_u *text = (char_u *)_("to %s on %s");
Bram Moolenaarf191d552014-10-09 17:05:56 +02001556#ifdef FEAT_MBYTE
1557 char_u *printer_name_orig = printer_name;
1558 char_u *port_name_orig = port_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559
Bram Moolenaarf191d552014-10-09 17:05:56 +02001560 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
1561 {
1562 char_u *to_free = NULL;
1563 int maxlen;
1564
Bram Moolenaar861d80a2014-10-16 20:35:36 +02001565 acp_to_enc(printer_name, (int)STRLEN(printer_name), &to_free,
1566 &maxlen);
Bram Moolenaarf191d552014-10-09 17:05:56 +02001567 if (to_free != NULL)
1568 printer_name = to_free;
Bram Moolenaar861d80a2014-10-16 20:35:36 +02001569 acp_to_enc(port_name, (int)STRLEN(port_name), &to_free, &maxlen);
Bram Moolenaarf191d552014-10-09 17:05:56 +02001570 if (to_free != NULL)
1571 port_name = to_free;
1572 }
1573#endif
Bram Moolenaare6a91fd2008-07-24 18:51:11 +00001574 prt_name = alloc((unsigned)(STRLEN(printer_name) + STRLEN(port_name)
1575 + STRLEN(text)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 if (prt_name != NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001577 wsprintf((char *)prt_name, (const char *)text,
1578 printer_name, port_name);
Bram Moolenaarf191d552014-10-09 17:05:56 +02001579#ifdef FEAT_MBYTE
1580 if (printer_name != printer_name_orig)
1581 vim_free(printer_name);
1582 if (port_name != port_name_orig)
1583 vim_free(port_name);
1584#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 }
1586 GlobalUnlock(prt_dlg.hDevNames);
1587
1588 /*
1589 * Initialise the font according to 'printfont'
1590 */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02001591 vim_memset(&fLogFont, 0, sizeof(fLogFont));
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001592 if (get_logfont(&fLogFont, p_pfn, prt_dlg.hDC, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 {
1594 EMSG2(_("E613: Unknown printer font: %s"), p_pfn);
1595 mch_print_cleanup();
1596 return FALSE;
1597 }
1598
1599 for (pifBold = 0; pifBold <= 1; pifBold++)
1600 for (pifItalic = 0; pifItalic <= 1; pifItalic++)
1601 for (pifUnderline = 0; pifUnderline <= 1; pifUnderline++)
1602 {
1603 fLogFont.lfWeight = boldface[pifBold];
1604 fLogFont.lfItalic = pifItalic;
1605 fLogFont.lfUnderline = pifUnderline;
1606 prt_font_handles[pifBold][pifItalic][pifUnderline]
1607 = CreateFontIndirect(&fLogFont);
1608 }
1609
1610 SetBkMode(prt_dlg.hDC, OPAQUE);
1611 SelectObject(prt_dlg.hDC, prt_font_handles[0][0][0]);
1612
1613 /*
1614 * Fill in the settings struct
1615 */
1616 psettings->chars_per_line = prt_get_cpl();
1617 psettings->lines_per_page = prt_get_lpp();
Bram Moolenaar7ddc6422014-09-27 11:18:19 +02001618 if (prt_dlg.Flags & PD_USEDEVMODECOPIESANDCOLLATE)
1619 {
1620 psettings->n_collated_copies = (prt_dlg.Flags & PD_COLLATE)
1621 ? prt_dlg.nCopies : 1;
1622 psettings->n_uncollated_copies = (prt_dlg.Flags & PD_COLLATE)
1623 ? 1 : prt_dlg.nCopies;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624
Bram Moolenaar7ddc6422014-09-27 11:18:19 +02001625 if (psettings->n_collated_copies == 0)
1626 psettings->n_collated_copies = 1;
1627
1628 if (psettings->n_uncollated_copies == 0)
1629 psettings->n_uncollated_copies = 1;
Bram Moolenaarb04a98f2016-12-01 20:32:29 +01001630 }
1631 else
1632 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 psettings->n_collated_copies = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 psettings->n_uncollated_copies = 1;
Bram Moolenaar7ddc6422014-09-27 11:18:19 +02001635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636
1637 psettings->jobname = jobname;
1638
1639 return TRUE;
1640
1641init_fail_dlg:
1642 {
1643 DWORD err = CommDlgExtendedError();
1644
1645 if (err)
1646 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 char_u *buf;
1648
1649 /* I suspect FormatMessage() doesn't work for values returned by
1650 * CommDlgExtendedError(). What does? */
1651 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
1652 FORMAT_MESSAGE_FROM_SYSTEM |
1653 FORMAT_MESSAGE_IGNORE_INSERTS,
1654 NULL, err, 0, (LPTSTR)(&buf), 0, NULL);
1655 EMSG2(_("E238: Print error: %s"),
1656 buf == NULL ? (char_u *)_("Unknown") : buf);
1657 LocalFree((LPVOID)(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 }
1659 else
1660 msg_clr_eos(); /* Maybe canceled */
1661
1662 mch_print_cleanup();
1663 return FALSE;
1664 }
1665}
1666
1667
1668 int
1669mch_print_begin(prt_settings_T *psettings)
1670{
1671 int ret;
1672 static DOCINFO di;
1673 char szBuffer[300];
1674
1675 hDlgPrint = CreateDialog(GetModuleHandle(NULL), TEXT("PrintDlgBox"),
1676 prt_dlg.hwndOwner, PrintDlgProc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 SetAbortProc(prt_dlg.hDC, AbortProc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 wsprintf(szBuffer, _("Printing '%s'"), gettail(psettings->jobname));
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001679 vimSetDlgItemText(hDlgPrint, IDC_PRINTTEXT1, (char_u *)szBuffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02001681 vim_memset(&di, 0, sizeof(DOCINFO));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682 di.cbSize = sizeof(DOCINFO);
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001683 di.lpszDocName = (LPCSTR)psettings->jobname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 ret = StartDoc(prt_dlg.hDC, &di);
1685
1686#ifdef FEAT_GUI
1687 /* Give focus back to main window (when using MDI). */
1688 SetFocus(s_hwnd);
1689#endif
1690
1691 return (ret > 0);
1692}
1693
1694 void
Bram Moolenaar1266d672017-02-01 13:43:36 +01001695mch_print_end(prt_settings_T *psettings UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696{
1697 EndDoc(prt_dlg.hDC);
1698 if (!*bUserAbort)
1699 SendMessage(hDlgPrint, WM_COMMAND, 0, 0);
1700}
1701
1702 int
1703mch_print_end_page(void)
1704{
1705 return (EndPage(prt_dlg.hDC) > 0);
1706}
1707
1708 int
1709mch_print_begin_page(char_u *msg)
1710{
1711 if (msg != NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001712 vimSetDlgItemText(hDlgPrint, IDC_PROGRESS, msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 return (StartPage(prt_dlg.hDC) > 0);
1714}
1715
1716 int
1717mch_print_blank_page(void)
1718{
1719 return (mch_print_begin_page(NULL) ? (mch_print_end_page()) : FALSE);
1720}
1721
1722static int prt_pos_x = 0;
1723static int prt_pos_y = 0;
1724
1725 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001726mch_print_start_line(int margin, int page_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727{
1728 if (margin)
1729 prt_pos_x = -prt_number_width;
1730 else
1731 prt_pos_x = 0;
1732 prt_pos_y = page_line * prt_line_height
1733 + prt_tm.tmAscent + prt_tm.tmExternalLeading;
1734}
1735
1736 int
1737mch_print_text_out(char_u *p, int len)
1738{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001739#if defined(FEAT_PROPORTIONAL_FONTS) || defined(FEAT_MBYTE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 SIZE sz;
1741#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001742#if defined(FEAT_MBYTE)
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001743 WCHAR *wp = NULL;
1744 int wlen = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001746 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
1747 {
1748 wp = enc_to_utf16(p, &wlen);
1749 }
1750 if (wp != NULL)
1751 {
1752 int ret = FALSE;
1753
1754 TextOutW(prt_dlg.hDC, prt_pos_x + prt_left_margin,
1755 prt_pos_y + prt_top_margin, wp, wlen);
1756 GetTextExtentPoint32W(prt_dlg.hDC, wp, wlen, &sz);
1757 vim_free(wp);
1758 prt_pos_x += (sz.cx - prt_tm.tmOverhang);
1759 /* This is wrong when printing spaces for a TAB. */
1760 if (p[len] != NUL)
1761 {
1762 wlen = MB_PTR2LEN(p + len);
1763 wp = enc_to_utf16(p + len, &wlen);
1764 if (wp != NULL)
1765 {
1766 GetTextExtentPoint32W(prt_dlg.hDC, wp, 1, &sz);
1767 ret = (prt_pos_x + prt_left_margin + sz.cx > prt_right_margin);
1768 vim_free(wp);
1769 }
1770 }
1771 return ret;
1772 }
1773#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 TextOut(prt_dlg.hDC, prt_pos_x + prt_left_margin,
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001775 prt_pos_y + prt_top_margin,
1776 (LPCSTR)p, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777#ifndef FEAT_PROPORTIONAL_FONTS
1778 prt_pos_x += len * prt_tm.tmAveCharWidth;
1779 return (prt_pos_x + prt_left_margin + prt_tm.tmAveCharWidth
1780 + prt_tm.tmOverhang > prt_right_margin);
1781#else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001782 GetTextExtentPoint32(prt_dlg.hDC, (LPCSTR)p, len, &sz);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 prt_pos_x += (sz.cx - prt_tm.tmOverhang);
1784 /* This is wrong when printing spaces for a TAB. */
1785 if (p[len] == NUL)
1786 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 GetTextExtentPoint32(prt_dlg.hDC, p + len, 1, &sz);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 return (prt_pos_x + prt_left_margin + sz.cx > prt_right_margin);
1789#endif
1790}
1791
1792 void
1793mch_print_set_font(int iBold, int iItalic, int iUnderline)
1794{
1795 SelectObject(prt_dlg.hDC, prt_font_handles[iBold][iItalic][iUnderline]);
1796}
1797
1798 void
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001799mch_print_set_bg(long_u bgcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800{
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001801 SetBkColor(prt_dlg.hDC, GetNearestColor(prt_dlg.hDC,
1802 swap_me((COLORREF)bgcol)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 /*
1804 * With a white background we can draw characters transparent, which is
1805 * good for italic characters that overlap to the next char cell.
1806 */
1807 if (bgcol == 0xffffffUL)
1808 SetBkMode(prt_dlg.hDC, TRANSPARENT);
1809 else
1810 SetBkMode(prt_dlg.hDC, OPAQUE);
1811}
1812
1813 void
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001814mch_print_set_fg(long_u fgcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815{
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001816 SetTextColor(prt_dlg.hDC, GetNearestColor(prt_dlg.hDC,
1817 swap_me((COLORREF)fgcol)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818}
1819
1820#endif /*FEAT_PRINTER && !FEAT_POSTSCRIPT*/
1821
Bram Moolenaar58d98232005-07-23 22:25:46 +00001822
1823
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824#if defined(FEAT_SHORTCUT) || defined(PROTO)
Bram Moolenaar82881492012-11-20 16:53:39 +01001825# ifndef PROTO
1826# include <shlobj.h>
1827# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828
1829/*
1830 * When "fname" is the name of a shortcut (*.lnk) resolve the file it points
1831 * to and return that name in allocated memory.
1832 * Otherwise NULL is returned.
1833 */
1834 char_u *
1835mch_resolve_shortcut(char_u *fname)
1836{
1837 HRESULT hr;
1838 IShellLink *psl = NULL;
1839 IPersistFile *ppf = NULL;
1840 OLECHAR wsz[MAX_PATH];
1841 WIN32_FIND_DATA ffd; // we get those free of charge
Bram Moolenaar604729e2013-08-30 16:44:19 +02001842 CHAR buf[MAX_PATH]; // could have simply reused 'wsz'...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 char_u *rfname = NULL;
1844 int len;
Bram Moolenaar604729e2013-08-30 16:44:19 +02001845# ifdef FEAT_MBYTE
1846 IShellLinkW *pslw = NULL;
1847 WIN32_FIND_DATAW ffdw; // we get those free of charge
1848# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849
1850 /* Check if the file name ends in ".lnk". Avoid calling
1851 * CoCreateInstance(), it's quite slow. */
1852 if (fname == NULL)
1853 return rfname;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001854 len = (int)STRLEN(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 if (len <= 4 || STRNICMP(fname + len - 4, ".lnk", 4) != 0)
1856 return rfname;
1857
1858 CoInitialize(NULL);
1859
Bram Moolenaar604729e2013-08-30 16:44:19 +02001860# ifdef FEAT_MBYTE
1861 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
1862 {
1863 // create a link manager object and request its interface
1864 hr = CoCreateInstance(
1865 &CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
1866 &IID_IShellLinkW, (void**)&pslw);
1867 if (hr == S_OK)
1868 {
1869 WCHAR *p = enc_to_utf16(fname, NULL);
1870
1871 if (p != NULL)
1872 {
1873 // Get a pointer to the IPersistFile interface.
1874 hr = pslw->lpVtbl->QueryInterface(
1875 pslw, &IID_IPersistFile, (void**)&ppf);
1876 if (hr != S_OK)
1877 goto shortcut_errorw;
1878
1879 // "load" the name and resolve the link
1880 hr = ppf->lpVtbl->Load(ppf, p, STGM_READ);
1881 if (hr != S_OK)
1882 goto shortcut_errorw;
1883# if 0 // This makes Vim wait a long time if the target does not exist.
1884 hr = pslw->lpVtbl->Resolve(pslw, NULL, SLR_NO_UI);
1885 if (hr != S_OK)
1886 goto shortcut_errorw;
1887# endif
1888
1889 // Get the path to the link target.
1890 ZeroMemory(wsz, MAX_PATH * sizeof(WCHAR));
1891 hr = pslw->lpVtbl->GetPath(pslw, wsz, MAX_PATH, &ffdw, 0);
1892 if (hr == S_OK && wsz[0] != NUL)
1893 rfname = utf16_to_enc(wsz, NULL);
1894
1895shortcut_errorw:
1896 vim_free(p);
Bram Moolenaarcd981f22014-02-11 17:06:00 +01001897 goto shortcut_end;
Bram Moolenaar604729e2013-08-30 16:44:19 +02001898 }
1899 }
Bram Moolenaarcea912a2016-10-12 14:20:24 +02001900 goto shortcut_end;
Bram Moolenaar604729e2013-08-30 16:44:19 +02001901 }
1902# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 // create a link manager object and request its interface
1904 hr = CoCreateInstance(
1905 &CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
1906 &IID_IShellLink, (void**)&psl);
1907 if (hr != S_OK)
Bram Moolenaar604729e2013-08-30 16:44:19 +02001908 goto shortcut_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909
1910 // Get a pointer to the IPersistFile interface.
1911 hr = psl->lpVtbl->QueryInterface(
1912 psl, &IID_IPersistFile, (void**)&ppf);
1913 if (hr != S_OK)
Bram Moolenaar604729e2013-08-30 16:44:19 +02001914 goto shortcut_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915
1916 // full path string must be in Unicode.
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001917 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)fname, -1, wsz, MAX_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00001919 // "load" the name and resolve the link
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 hr = ppf->lpVtbl->Load(ppf, wsz, STGM_READ);
1921 if (hr != S_OK)
Bram Moolenaar604729e2013-08-30 16:44:19 +02001922 goto shortcut_end;
1923# if 0 // This makes Vim wait a long time if the target doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 hr = psl->lpVtbl->Resolve(psl, NULL, SLR_NO_UI);
1925 if (hr != S_OK)
Bram Moolenaar604729e2013-08-30 16:44:19 +02001926 goto shortcut_end;
1927# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928
1929 // Get the path to the link target.
1930 ZeroMemory(buf, MAX_PATH);
1931 hr = psl->lpVtbl->GetPath(psl, buf, MAX_PATH, &ffd, 0);
1932 if (hr == S_OK && buf[0] != NUL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001933 rfname = vim_strsave((char_u *)buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934
Bram Moolenaar604729e2013-08-30 16:44:19 +02001935shortcut_end:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 // Release all interface pointers (both belong to the same object)
1937 if (ppf != NULL)
1938 ppf->lpVtbl->Release(ppf);
1939 if (psl != NULL)
1940 psl->lpVtbl->Release(psl);
Bram Moolenaar604729e2013-08-30 16:44:19 +02001941# ifdef FEAT_MBYTE
1942 if (pslw != NULL)
1943 pslw->lpVtbl->Release(pslw);
1944# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945
1946 CoUninitialize();
1947 return rfname;
1948}
1949#endif
1950
1951#if (defined(FEAT_EVAL) && !defined(FEAT_GUI)) || defined(PROTO)
1952/*
1953 * Bring ourselves to the foreground. Does work if the OS doesn't allow it.
1954 */
1955 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001956win32_set_foreground(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957{
1958# ifndef FEAT_GUI
1959 GetConsoleHwnd(); /* get value of s_hwnd */
1960# endif
1961 if (s_hwnd != 0)
1962 SetForegroundWindow(s_hwnd);
1963}
1964#endif
1965
1966#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
1967/*
1968 * Client-server code for Vim
1969 *
1970 * Originally written by Paul Moore
1971 */
1972
1973/* In order to handle inter-process messages, we need to have a window. But
1974 * the functions in this module can be called before the main GUI window is
1975 * created (and may also be called in the console version, where there is no
1976 * GUI window at all).
1977 *
1978 * So we create a hidden window, and arrange to destroy it on exit.
1979 */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001980HWND message_window = 0; /* window that's handling messages */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981
1982#define VIM_CLASSNAME "VIM_MESSAGES"
1983#define VIM_CLASSNAME_LEN (sizeof(VIM_CLASSNAME) - 1)
1984
1985/* Communication is via WM_COPYDATA messages. The message type is send in
1986 * the dwData parameter. Types are defined here. */
1987#define COPYDATA_KEYS 0
1988#define COPYDATA_REPLY 1
1989#define COPYDATA_EXPR 10
1990#define COPYDATA_RESULT 11
1991#define COPYDATA_ERROR_RESULT 12
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001992#define COPYDATA_ENCODING 20
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993
1994/* This is a structure containing a server HWND and its name. */
1995struct server_id
1996{
1997 HWND hwnd;
1998 char_u *name;
1999};
2000
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002001/* Last received 'encoding' that the client uses. */
2002static char_u *client_enc = NULL;
2003
2004/*
2005 * Tell the other side what encoding we are using.
2006 * Errors are ignored.
2007 */
2008 static void
2009serverSendEnc(HWND target)
2010{
2011 COPYDATASTRUCT data;
2012
2013 data.dwData = COPYDATA_ENCODING;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002014#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002015 data.cbData = (DWORD)STRLEN(p_enc) + 1;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002016 data.lpData = p_enc;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002017#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +02002018 data.cbData = (DWORD)STRLEN("latin1") + 1;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002019 data.lpData = "latin1";
2020#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002021 (void)SendMessage(target, WM_COPYDATA, (WPARAM)message_window,
2022 (LPARAM)(&data));
2023}
2024
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025/*
2026 * Clean up on exit. This destroys the hidden message window.
2027 */
2028 static void
2029#ifdef __BORLANDC__
2030 _RTLENTRYF
2031#endif
2032CleanUpMessaging(void)
2033{
2034 if (message_window != 0)
2035 {
2036 DestroyWindow(message_window);
2037 message_window = 0;
2038 }
2039}
2040
2041static int save_reply(HWND server, char_u *reply, int expr);
2042
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002043/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 * The window procedure for the hidden message window.
2045 * It handles callback messages and notifications from servers.
2046 * In order to process these messages, it is necessary to run a
2047 * message loop. Code which may run before the main message loop
2048 * is started (in the GUI) is careful to pump messages when it needs
2049 * to. Features which require message delivery during normal use will
2050 * not work in the console version - this basically means those
2051 * features which allow Vim to act as a server, rather than a client.
2052 */
2053 static LRESULT CALLBACK
2054Messaging_WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
2055{
2056 if (msg == WM_COPYDATA)
2057 {
2058 /* This is a message from another Vim. The dwData member of the
2059 * COPYDATASTRUCT determines the type of message:
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002060 * COPYDATA_ENCODING:
2061 * The encoding that the client uses. Following messages will
2062 * use this encoding, convert if needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 * COPYDATA_KEYS:
2064 * A key sequence. We are a server, and a client wants these keys
2065 * adding to the input queue.
2066 * COPYDATA_REPLY:
2067 * A reply. We are a client, and a server has sent this message
2068 * in response to a request. (server2client())
2069 * COPYDATA_EXPR:
2070 * An expression. We are a server, and a client wants us to
2071 * evaluate this expression.
2072 * COPYDATA_RESULT:
2073 * A reply. We are a client, and a server has sent this message
2074 * in response to a COPYDATA_EXPR.
2075 * COPYDATA_ERROR_RESULT:
2076 * A reply. We are a client, and a server has sent this message
2077 * in response to a COPYDATA_EXPR that failed to evaluate.
2078 */
2079 COPYDATASTRUCT *data = (COPYDATASTRUCT*)lParam;
2080 HWND sender = (HWND)wParam;
2081 COPYDATASTRUCT reply;
2082 char_u *res;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 int retval;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002084 char_u *str;
2085 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086
2087 switch (data->dwData)
2088 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002089 case COPYDATA_ENCODING:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002090# ifdef FEAT_MBYTE
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002091 /* Remember the encoding that the client uses. */
2092 vim_free(client_enc);
2093 client_enc = enc_canonize((char_u *)data->lpData);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002094# endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002095 return 1;
2096
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097 case COPYDATA_KEYS:
2098 /* Remember who sent this, for <client> */
2099 clientWindow = sender;
2100
2101 /* Add the received keys to the input buffer. The loop waiting
2102 * for the user to do something should check the input buffer. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002103 str = serverConvert(client_enc, (char_u *)data->lpData, &tofree);
2104 server_to_input_buf(str);
2105 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106
2107# ifdef FEAT_GUI
2108 /* Wake up the main GUI loop. */
2109 if (s_hwnd != 0)
2110 PostMessage(s_hwnd, WM_NULL, 0, 0);
2111# endif
2112 return 1;
2113
2114 case COPYDATA_EXPR:
2115 /* Remember who sent this, for <client> */
2116 clientWindow = sender;
2117
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002118 str = serverConvert(client_enc, (char_u *)data->lpData, &tofree);
2119 res = eval_client_expr_to_string(str);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002120
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 if (res == NULL)
2122 {
Bram Moolenaar15bf76d2017-03-18 16:18:37 +01002123 char *err = _(e_invexprmsg);
2124 size_t len = STRLEN(str) + STRLEN(err) + 5;
2125
Bram Moolenaar1662ce12017-03-19 21:47:50 +01002126 res = alloc((unsigned)len);
Bram Moolenaar15bf76d2017-03-18 16:18:37 +01002127 if (res != NULL)
2128 vim_snprintf((char *)res, len, "%s: \"%s\"", err, str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 reply.dwData = COPYDATA_ERROR_RESULT;
2130 }
2131 else
2132 reply.dwData = COPYDATA_RESULT;
2133 reply.lpData = res;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002134 reply.cbData = (DWORD)STRLEN(res) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002136 serverSendEnc(sender);
Bram Moolenaar5246cd72013-06-16 16:41:47 +02002137 retval = (int)SendMessage(sender, WM_COPYDATA,
2138 (WPARAM)message_window, (LPARAM)(&reply));
Bram Moolenaar15bf76d2017-03-18 16:18:37 +01002139 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 vim_free(res);
2141 return retval;
2142
2143 case COPYDATA_REPLY:
2144 case COPYDATA_RESULT:
2145 case COPYDATA_ERROR_RESULT:
2146 if (data->lpData != NULL)
2147 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002148 str = serverConvert(client_enc, (char_u *)data->lpData,
2149 &tofree);
2150 if (tofree == NULL)
2151 str = vim_strsave(str);
2152 if (save_reply(sender, str,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 (data->dwData == COPYDATA_REPLY ? 0 :
2154 (data->dwData == COPYDATA_RESULT ? 1 :
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002155 2))) == FAIL)
2156 vim_free(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157#ifdef FEAT_AUTOCMD
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002158 else if (data->dwData == COPYDATA_REPLY)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 {
Bram Moolenaar427d51c2013-06-16 16:01:25 +02002160 char_u winstr[30];
2161
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002162 sprintf((char *)winstr, PRINTF_HEX_LONG_U, (long_u)sender);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002163 apply_autocmds(EVENT_REMOTEREPLY, winstr, str,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 TRUE, curbuf);
2165 }
2166#endif
2167 }
2168 return 1;
2169 }
2170
2171 return 0;
2172 }
2173
2174 else if (msg == WM_ACTIVATE && wParam == WA_ACTIVE)
2175 {
2176 /* When the message window is activated (brought to the foreground),
2177 * this actually applies to the text window. */
2178#ifndef FEAT_GUI
2179 GetConsoleHwnd(); /* get value of s_hwnd */
2180#endif
2181 if (s_hwnd != 0)
2182 {
2183 SetForegroundWindow(s_hwnd);
2184 return 0;
2185 }
2186 }
2187
2188 return DefWindowProc(hwnd, msg, wParam, lParam);
2189}
2190
2191/*
2192 * Initialise the message handling process. This involves creating a window
2193 * to handle messages - the window will not be visible.
2194 */
2195 void
2196serverInitMessaging(void)
2197{
2198 WNDCLASS wndclass;
2199 HINSTANCE s_hinst;
2200
2201 /* Clean up on exit */
2202 atexit(CleanUpMessaging);
2203
2204 /* Register a window class - we only really care
2205 * about the window procedure
2206 */
2207 s_hinst = (HINSTANCE)GetModuleHandle(0);
2208 wndclass.style = 0;
2209 wndclass.lpfnWndProc = Messaging_WndProc;
2210 wndclass.cbClsExtra = 0;
2211 wndclass.cbWndExtra = 0;
2212 wndclass.hInstance = s_hinst;
2213 wndclass.hIcon = NULL;
2214 wndclass.hCursor = NULL;
2215 wndclass.hbrBackground = NULL;
2216 wndclass.lpszMenuName = NULL;
2217 wndclass.lpszClassName = VIM_CLASSNAME;
2218 RegisterClass(&wndclass);
2219
2220 /* Create the message window. It will be hidden, so the details don't
2221 * matter. Don't use WS_OVERLAPPEDWINDOW, it will make a shortcut remove
2222 * focus from gvim. */
2223 message_window = CreateWindow(VIM_CLASSNAME, "",
2224 WS_POPUPWINDOW | WS_CAPTION,
2225 CW_USEDEFAULT, CW_USEDEFAULT,
2226 100, 100, NULL, NULL,
2227 s_hinst, NULL);
2228}
2229
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002230/* Used by serverSendToVim() to find an alternate server name. */
2231static char_u *altname_buf_ptr = NULL;
2232
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233/*
2234 * Get the title of the window "hwnd", which is the Vim server name, in
2235 * "name[namelen]" and return the length.
2236 * Returns zero if window "hwnd" is not a Vim server.
2237 */
2238 static int
2239getVimServerName(HWND hwnd, char *name, int namelen)
2240{
2241 int len;
2242 char buffer[VIM_CLASSNAME_LEN + 1];
2243
2244 /* Ignore windows which aren't Vim message windows */
2245 len = GetClassName(hwnd, buffer, sizeof(buffer));
2246 if (len != VIM_CLASSNAME_LEN || STRCMP(buffer, VIM_CLASSNAME) != 0)
2247 return 0;
2248
2249 /* Get the title of the window */
2250 return GetWindowText(hwnd, name, namelen);
2251}
2252
2253 static BOOL CALLBACK
2254enumWindowsGetServer(HWND hwnd, LPARAM lparam)
2255{
2256 struct server_id *id = (struct server_id *)lparam;
2257 char server[MAX_PATH];
2258
2259 /* Get the title of the window */
2260 if (getVimServerName(hwnd, server, sizeof(server)) == 0)
2261 return TRUE;
2262
2263 /* If this is the server we're looking for, return its HWND */
2264 if (STRICMP(server, id->name) == 0)
2265 {
2266 id->hwnd = hwnd;
2267 return FALSE;
2268 }
2269
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002270 /* If we are looking for an alternate server, remember this name. */
2271 if (altname_buf_ptr != NULL
2272 && STRNICMP(server, id->name, STRLEN(id->name)) == 0
2273 && vim_isdigit(server[STRLEN(id->name)]))
2274 {
2275 STRCPY(altname_buf_ptr, server);
2276 altname_buf_ptr = NULL; /* don't use another name */
2277 }
2278
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 /* Otherwise, keep looking */
2280 return TRUE;
2281}
2282
2283 static BOOL CALLBACK
2284enumWindowsGetNames(HWND hwnd, LPARAM lparam)
2285{
2286 garray_T *ga = (garray_T *)lparam;
2287 char server[MAX_PATH];
2288
2289 /* Get the title of the window */
2290 if (getVimServerName(hwnd, server, sizeof(server)) == 0)
2291 return TRUE;
2292
2293 /* Add the name to the list */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002294 ga_concat(ga, (char_u *)server);
2295 ga_concat(ga, (char_u *)"\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 return TRUE;
2297}
2298
2299 static HWND
2300findServer(char_u *name)
2301{
2302 struct server_id id;
2303
2304 id.name = name;
2305 id.hwnd = 0;
2306
2307 EnumWindows(enumWindowsGetServer, (LPARAM)(&id));
2308
2309 return id.hwnd;
2310}
2311
2312 void
2313serverSetName(char_u *name)
2314{
2315 char_u *ok_name;
2316 HWND hwnd = 0;
2317 int i = 0;
2318 char_u *p;
2319
2320 /* Leave enough space for a 9-digit suffix to ensure uniqueness! */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002321 ok_name = alloc((unsigned)STRLEN(name) + 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322
2323 STRCPY(ok_name, name);
2324 p = ok_name + STRLEN(name);
2325
2326 for (;;)
2327 {
2328 /* This is inefficient - we're doing an EnumWindows loop for each
2329 * possible name. It would be better to grab all names in one go,
2330 * and scan the list each time...
2331 */
2332 hwnd = findServer(ok_name);
2333 if (hwnd == 0)
2334 break;
2335
2336 ++i;
2337 if (i >= 1000)
2338 break;
2339
2340 sprintf((char *)p, "%d", i);
2341 }
2342
2343 if (hwnd != 0)
2344 vim_free(ok_name);
2345 else
2346 {
2347 /* Remember the name */
2348 serverName = ok_name;
2349#ifdef FEAT_TITLE
2350 need_maketitle = TRUE; /* update Vim window title later */
2351#endif
2352
2353 /* Update the message window title */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002354 SetWindowText(message_window, (LPCSTR)ok_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355
2356#ifdef FEAT_EVAL
2357 /* Set the servername variable */
2358 set_vim_var_string(VV_SEND_SERVER, serverName, -1);
2359#endif
2360 }
2361}
2362
2363 char_u *
2364serverGetVimNames(void)
2365{
2366 garray_T ga;
2367
2368 ga_init2(&ga, 1, 100);
2369
2370 EnumWindows(enumWindowsGetNames, (LPARAM)(&ga));
Bram Moolenaar269ec652004-07-29 08:43:53 +00002371 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372
2373 return ga.ga_data;
2374}
2375
2376 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002377serverSendReply(
2378 char_u *name, /* Where to send. */
2379 char_u *reply) /* What to send. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380{
2381 HWND target;
2382 COPYDATASTRUCT data;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002383 long_u n = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384
2385 /* The "name" argument is a magic cookie obtained from expand("<client>").
2386 * It should be of the form 0xXXXXX - i.e. a C hex literal, which is the
2387 * value of the client's message window HWND.
2388 */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002389 sscanf((char *)name, SCANF_HEX_LONG_U, &n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 if (n == 0)
2391 return -1;
2392
2393 target = (HWND)n;
2394 if (!IsWindow(target))
2395 return -1;
2396
2397 data.dwData = COPYDATA_REPLY;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002398 data.cbData = (DWORD)STRLEN(reply) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 data.lpData = reply;
2400
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002401 serverSendEnc(target);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 if (SendMessage(target, WM_COPYDATA, (WPARAM)message_window,
2403 (LPARAM)(&data)))
2404 return 0;
2405
2406 return -1;
2407}
2408
2409 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002410serverSendToVim(
2411 char_u *name, /* Where to send. */
2412 char_u *cmd, /* What to send. */
2413 char_u **result, /* Result of eval'ed expression */
2414 void *ptarget, /* HWND of server */
2415 int asExpr, /* Expression or keys? */
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002416 int timeout, /* timeout in seconds or zero */
Bram Moolenaar05540972016-01-30 20:31:25 +01002417 int silent) /* don't complain about no server */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418{
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002419 HWND target;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 COPYDATASTRUCT data;
2421 char_u *retval = NULL;
2422 int retcode = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002423 char_u altname_buf[MAX_PATH];
2424
Bram Moolenaar7416f3e2017-03-18 18:10:13 +01002425 /* Execute locally if no display or target is ourselves */
2426 if (serverName != NULL && STRICMP(name, serverName) == 0)
2427 return sendToLocalVim(cmd, asExpr, result);
2428
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002429 /* If the server name does not end in a digit then we look for an
2430 * alternate name. e.g. when "name" is GVIM the we may find GVIM2. */
2431 if (STRLEN(name) > 1 && !vim_isdigit(name[STRLEN(name) - 1]))
2432 altname_buf_ptr = altname_buf;
2433 altname_buf[0] = NUL;
2434 target = findServer(name);
2435 altname_buf_ptr = NULL;
2436 if (target == 0 && altname_buf[0] != NUL)
2437 /* Use another server name we found. */
2438 target = findServer(altname_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439
2440 if (target == 0)
2441 {
2442 if (!silent)
2443 EMSG2(_(e_noserver), name);
2444 return -1;
2445 }
2446
2447 if (ptarget)
2448 *(HWND *)ptarget = target;
2449
2450 data.dwData = asExpr ? COPYDATA_EXPR : COPYDATA_KEYS;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002451 data.cbData = (DWORD)STRLEN(cmd) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 data.lpData = cmd;
2453
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002454 serverSendEnc(target);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 if (SendMessage(target, WM_COPYDATA, (WPARAM)message_window,
2456 (LPARAM)(&data)) == 0)
2457 return -1;
2458
2459 if (asExpr)
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002460 retval = serverGetReply(target, &retcode, TRUE, TRUE, timeout);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461
2462 if (result == NULL)
2463 vim_free(retval);
2464 else
2465 *result = retval; /* Caller assumes responsibility for freeing */
2466
2467 return retcode;
2468}
2469
2470/*
2471 * Bring the server to the foreground.
2472 */
2473 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002474serverForeground(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475{
2476 HWND target = findServer(name);
2477
2478 if (target != 0)
2479 SetForegroundWindow(target);
2480}
2481
2482/* Replies from server need to be stored until the client picks them up via
2483 * remote_read(). So we maintain a list of server-id/reply pairs.
2484 * Note that there could be multiple replies from one server pending if the
2485 * client is slow picking them up.
2486 * We just store the replies in a simple list. When we remove an entry, we
2487 * move list entries down to fill the gap.
2488 * The server ID is simply the HWND.
2489 */
2490typedef struct
2491{
2492 HWND server; /* server window */
2493 char_u *reply; /* reply string */
2494 int expr_result; /* 0 for REPLY, 1 for RESULT 2 for error */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002495} reply_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496
2497static garray_T reply_list = {0, 0, sizeof(reply_T), 5, 0};
2498
2499#define REPLY_ITEM(i) ((reply_T *)(reply_list.ga_data) + (i))
2500#define REPLY_COUNT (reply_list.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501
2502/* Flag which is used to wait for a reply */
2503static int reply_received = 0;
2504
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002505/*
2506 * Store a reply. "reply" must be allocated memory (or NULL).
2507 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 static int
2509save_reply(HWND server, char_u *reply, int expr)
2510{
2511 reply_T *rep;
2512
2513 if (ga_grow(&reply_list, 1) == FAIL)
2514 return FAIL;
2515
2516 rep = REPLY_ITEM(REPLY_COUNT);
2517 rep->server = server;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002518 rep->reply = reply;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 rep->expr_result = expr;
2520 if (rep->reply == NULL)
2521 return FAIL;
2522
2523 ++REPLY_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 reply_received = 1;
2525 return OK;
2526}
2527
2528/*
2529 * Get a reply from server "server".
2530 * When "expr_res" is non NULL, get the result of an expression, otherwise a
2531 * server2client() message.
2532 * When non NULL, point to return code. 0 => OK, -1 => ERROR
2533 * If "remove" is TRUE, consume the message, the caller must free it then.
2534 * if "wait" is TRUE block until a message arrives (or the server exits).
2535 */
2536 char_u *
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002537serverGetReply(HWND server, int *expr_res, int remove, int wait, int timeout)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538{
2539 int i;
2540 char_u *reply;
2541 reply_T *rep;
Bram Moolenaar15e737f2017-03-18 21:22:47 +01002542 int did_process = FALSE;
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002543 time_t start;
2544 time_t now;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545
2546 /* When waiting, loop until the message waiting for is received. */
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002547 time(&start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 for (;;)
2549 {
2550 /* Reset this here, in case a message arrives while we are going
2551 * through the already received messages. */
2552 reply_received = 0;
2553
2554 for (i = 0; i < REPLY_COUNT; ++i)
2555 {
2556 rep = REPLY_ITEM(i);
2557 if (rep->server == server
2558 && ((rep->expr_result != 0) == (expr_res != NULL)))
2559 {
2560 /* Save the values we've found for later */
2561 reply = rep->reply;
2562 if (expr_res != NULL)
2563 *expr_res = rep->expr_result == 1 ? 0 : -1;
2564
2565 if (remove)
2566 {
2567 /* Move the rest of the list down to fill the gap */
2568 mch_memmove(rep, rep + 1,
2569 (REPLY_COUNT - i - 1) * sizeof(reply_T));
2570 --REPLY_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 }
2572
2573 /* Return the reply to the caller, who takes on responsibility
2574 * for freeing it if "remove" is TRUE. */
2575 return reply;
2576 }
2577 }
2578
2579 /* If we got here, we didn't find a reply. Return immediately if the
2580 * "wait" parameter isn't set. */
2581 if (!wait)
Bram Moolenaar15e737f2017-03-18 21:22:47 +01002582 {
2583 /* Process pending messages once. Without this, looping on
2584 * remote_peek() would never get the reply. */
2585 if (!did_process)
2586 {
2587 did_process = TRUE;
2588 serverProcessPendingMessages();
2589 continue;
2590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 break;
Bram Moolenaar15e737f2017-03-18 21:22:47 +01002592 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593
2594 /* We need to wait for a reply. Enter a message loop until the
2595 * "reply_received" flag gets set. */
2596
2597 /* Loop until we receive a reply */
2598 while (reply_received == 0)
2599 {
Bram Moolenaar42205552017-03-18 19:42:22 +01002600#ifdef FEAT_TIMERS
2601 check_due_timer();
2602#endif
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01002603 time(&now);
2604 if (timeout > 0 && (now - start) >= timeout)
2605 break;
2606
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 /* Wait for a SendMessage() call to us. This could be the reply
2608 * we are waiting for. Use a timeout of a second, to catch the
2609 * situation that the server died unexpectedly. */
2610 MsgWaitForMultipleObjects(0, NULL, TRUE, 1000, QS_ALLINPUT);
2611
2612 /* If the server has died, give up */
2613 if (!IsWindow(server))
2614 return NULL;
2615
2616 serverProcessPendingMessages();
2617 }
2618 }
2619
2620 return NULL;
2621}
2622
2623/*
2624 * Process any messages in the Windows message queue.
2625 */
2626 void
2627serverProcessPendingMessages(void)
2628{
2629 MSG msg;
2630
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02002631 while (pPeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 {
2633 TranslateMessage(&msg);
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02002634 pDispatchMessage(&msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 }
2636}
2637
2638#endif /* FEAT_CLIENTSERVER */
2639
2640#if defined(FEAT_GUI) || (defined(FEAT_PRINTER) && !defined(FEAT_POSTSCRIPT)) \
2641 || defined(PROTO)
2642
2643struct charset_pair
2644{
2645 char *name;
2646 BYTE charset;
2647};
2648
2649static struct charset_pair
2650charset_pairs[] =
2651{
2652 {"ANSI", ANSI_CHARSET},
2653 {"CHINESEBIG5", CHINESEBIG5_CHARSET},
2654 {"DEFAULT", DEFAULT_CHARSET},
2655 {"HANGEUL", HANGEUL_CHARSET},
2656 {"OEM", OEM_CHARSET},
2657 {"SHIFTJIS", SHIFTJIS_CHARSET},
2658 {"SYMBOL", SYMBOL_CHARSET},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 {"ARABIC", ARABIC_CHARSET},
2660 {"BALTIC", BALTIC_CHARSET},
2661 {"EASTEUROPE", EASTEUROPE_CHARSET},
2662 {"GB2312", GB2312_CHARSET},
2663 {"GREEK", GREEK_CHARSET},
2664 {"HEBREW", HEBREW_CHARSET},
2665 {"JOHAB", JOHAB_CHARSET},
2666 {"MAC", MAC_CHARSET},
2667 {"RUSSIAN", RUSSIAN_CHARSET},
2668 {"THAI", THAI_CHARSET},
2669 {"TURKISH", TURKISH_CHARSET},
Bram Moolenaarcea912a2016-10-12 14:20:24 +02002670#ifdef VIETNAMESE_CHARSET
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 {"VIETNAMESE", VIETNAMESE_CHARSET},
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672#endif
2673 {NULL, 0}
2674};
2675
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002676struct quality_pair
2677{
2678 char *name;
2679 DWORD quality;
2680};
2681
2682static struct quality_pair
2683quality_pairs[] = {
2684#ifdef CLEARTYPE_QUALITY
2685 {"CLEARTYPE", CLEARTYPE_QUALITY},
2686#endif
2687#ifdef ANTIALIASED_QUALITY
2688 {"ANTIALIASED", ANTIALIASED_QUALITY},
2689#endif
Bram Moolenaar73a733e2016-05-11 21:05:05 +02002690#ifdef NONANTIALIASED_QUALITY
2691 {"NONANTIALIASED", NONANTIALIASED_QUALITY},
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002692#endif
2693#ifdef PROOF_QUALITY
2694 {"PROOF", PROOF_QUALITY},
2695#endif
Bram Moolenaar4c9ce052016-04-04 21:06:19 +02002696#ifdef DRAFT_QUALITY
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002697 {"DRAFT", DRAFT_QUALITY},
2698#endif
2699 {"DEFAULT", DEFAULT_QUALITY},
2700 {NULL, 0}
2701};
2702
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703/*
2704 * Convert a charset ID to a name.
2705 * Return NULL when not recognized.
2706 */
2707 char *
2708charset_id2name(int id)
2709{
2710 struct charset_pair *cp;
2711
2712 for (cp = charset_pairs; cp->name != NULL; ++cp)
2713 if ((BYTE)id == cp->charset)
2714 break;
2715 return cp->name;
2716}
2717
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002718/*
2719 * Convert a quality ID to a name.
2720 * Return NULL when not recognized.
2721 */
2722 char *
2723quality_id2name(DWORD id)
2724{
2725 struct quality_pair *qp;
2726
2727 for (qp = quality_pairs; qp->name != NULL; ++qp)
2728 if (id == qp->quality)
2729 break;
2730 return qp->name;
2731}
2732
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733static const LOGFONT s_lfDefault =
2734{
2735 -12, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, DEFAULT_CHARSET,
2736 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
2737 PROOF_QUALITY, FIXED_PITCH | FF_DONTCARE,
2738 "Fixedsys" /* see _ReadVimIni */
2739};
2740
2741/* Initialise the "current height" to -12 (same as s_lfDefault) just
2742 * in case the user specifies a font in "guifont" with no size before a font
2743 * with an explicit size has been set. This defaults the size to this value
2744 * (-12 equates to roughly 9pt).
2745 */
2746int current_font_height = -12; /* also used in gui_w48.c */
2747
2748/* Convert a string representing a point size into pixels. The string should
2749 * be a positive decimal number, with an optional decimal point (eg, "12", or
2750 * "10.5"). The pixel value is returned, and a pointer to the next unconverted
2751 * character is stored in *end. The flag "vertical" says whether this
2752 * calculation is for a vertical (height) size or a horizontal (width) one.
2753 */
2754 static int
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002755points_to_pixels(char_u *str, char_u **end, int vertical, long_i pprinter_dc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756{
2757 int pixels;
2758 int points = 0;
2759 int divisor = 0;
2760 HWND hwnd = (HWND)0;
2761 HDC hdc;
2762 HDC printer_dc = (HDC)pprinter_dc;
2763
2764 while (*str != NUL)
2765 {
2766 if (*str == '.' && divisor == 0)
2767 {
2768 /* Start keeping a divisor, for later */
2769 divisor = 1;
2770 }
2771 else
2772 {
2773 if (!VIM_ISDIGIT(*str))
2774 break;
2775
2776 points *= 10;
2777 points += *str - '0';
2778 divisor *= 10;
2779 }
2780 ++str;
2781 }
2782
2783 if (divisor == 0)
2784 divisor = 1;
2785
2786 if (printer_dc == NULL)
2787 {
2788 hwnd = GetDesktopWindow();
2789 hdc = GetWindowDC(hwnd);
2790 }
2791 else
2792 hdc = printer_dc;
2793
2794 pixels = MulDiv(points,
2795 GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX),
2796 72 * divisor);
2797
2798 if (printer_dc == NULL)
2799 ReleaseDC(hwnd, hdc);
2800
2801 *end = str;
2802 return pixels;
2803}
2804
2805 static int CALLBACK
2806font_enumproc(
2807 ENUMLOGFONT *elf,
Bram Moolenaar1266d672017-02-01 13:43:36 +01002808 NEWTEXTMETRIC *ntm UNUSED,
2809 int type UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 LPARAM lparam)
2811{
2812 /* Return value:
2813 * 0 = terminate now (monospace & ANSI)
2814 * 1 = continue, still no luck...
2815 * 2 = continue, but we have an acceptable LOGFONT
2816 * (monospace, not ANSI)
2817 * We use these values, as EnumFontFamilies returns 1 if the
2818 * callback function is never called. So, we check the return as
2819 * 0 = perfect, 2 = OK, 1 = no good...
2820 * It's not pretty, but it works!
2821 */
2822
2823 LOGFONT *lf = (LOGFONT *)(lparam);
2824
2825#ifndef FEAT_PROPORTIONAL_FONTS
2826 /* Ignore non-monospace fonts without further ado */
2827 if ((ntm->tmPitchAndFamily & 1) != 0)
2828 return 1;
2829#endif
2830
2831 /* Remember this LOGFONT as a "possible" */
2832 *lf = elf->elfLogFont;
2833
2834 /* Terminate the scan as soon as we find an ANSI font */
2835 if (lf->lfCharSet == ANSI_CHARSET
2836 || lf->lfCharSet == OEM_CHARSET
2837 || lf->lfCharSet == DEFAULT_CHARSET)
2838 return 0;
2839
2840 /* Continue the scan - we have a non-ANSI font */
2841 return 2;
2842}
2843
2844 static int
2845init_logfont(LOGFONT *lf)
2846{
2847 int n;
2848 HWND hwnd = GetDesktopWindow();
2849 HDC hdc = GetWindowDC(hwnd);
2850
2851 n = EnumFontFamilies(hdc,
2852 (LPCSTR)lf->lfFaceName,
2853 (FONTENUMPROC)font_enumproc,
2854 (LPARAM)lf);
2855
2856 ReleaseDC(hwnd, hdc);
2857
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002858 /* If we couldn't find a usable font, return failure */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 if (n == 1)
2860 return FAIL;
2861
2862 /* Tidy up the rest of the LOGFONT structure. We set to a basic
2863 * font - get_logfont() sets bold, italic, etc based on the user's
2864 * input.
2865 */
2866 lf->lfHeight = current_font_height;
2867 lf->lfWidth = 0;
2868 lf->lfItalic = FALSE;
2869 lf->lfUnderline = FALSE;
2870 lf->lfStrikeOut = FALSE;
2871 lf->lfWeight = FW_NORMAL;
2872
2873 /* Return success */
2874 return OK;
2875}
2876
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002877/*
2878 * Get font info from "name" into logfont "lf".
2879 * Return OK for a valid name, FAIL otherwise.
2880 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 int
2882get_logfont(
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002883 LOGFONT *lf,
2884 char_u *name,
2885 HDC printer_dc,
2886 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887{
2888 char_u *p;
2889 int i;
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002890 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 static LOGFONT *lastlf = NULL;
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002892#ifdef FEAT_MBYTE
2893 char_u *acpname = NULL;
2894#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895
2896 *lf = s_lfDefault;
2897 if (name == NULL)
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002898 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002900#ifdef FEAT_MBYTE
2901 /* Convert 'name' from 'encoding' to the current codepage, because
2902 * lf->lfFaceName uses the current codepage.
2903 * TODO: Use Wide APIs instead of ANSI APIs. */
2904 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
2905 {
2906 int len;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01002907 enc_to_acp(name, (int)STRLEN(name), &acpname, &len);
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002908 name = acpname;
2909 }
2910#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 if (STRCMP(name, "*") == 0)
2912 {
2913#if defined(FEAT_GUI_W32)
2914 CHOOSEFONT cf;
2915 /* if name is "*", bring up std font dialog: */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02002916 vim_memset(&cf, 0, sizeof(cf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 cf.lStructSize = sizeof(cf);
2918 cf.hwndOwner = s_hwnd;
2919 cf.Flags = CF_SCREENFONTS | CF_FIXEDPITCHONLY | CF_INITTOLOGFONTSTRUCT;
2920 if (lastlf != NULL)
2921 *lf = *lastlf;
2922 cf.lpLogFont = lf;
2923 cf.nFontType = 0 ; //REGULAR_FONTTYPE;
2924 if (ChooseFont(&cf))
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002925 ret = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926#endif
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002927 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 }
2929
2930 /*
2931 * Split name up, it could be <name>:h<height>:w<width> etc.
2932 */
2933 for (p = name; *p && *p != ':'; p++)
2934 {
Bram Moolenaar38bc4952016-11-10 17:10:51 +01002935 if (p - name + 1 >= LF_FACESIZE)
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002936 goto theend; /* Name too long */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 lf->lfFaceName[p - name] = *p;
2938 }
2939 if (p != name)
2940 lf->lfFaceName[p - name] = NUL;
2941
2942 /* First set defaults */
2943 lf->lfHeight = -12;
2944 lf->lfWidth = 0;
2945 lf->lfWeight = FW_NORMAL;
2946 lf->lfItalic = FALSE;
2947 lf->lfUnderline = FALSE;
2948 lf->lfStrikeOut = FALSE;
2949
2950 /*
2951 * If the font can't be found, try replacing '_' by ' '.
2952 */
2953 if (init_logfont(lf) == FAIL)
2954 {
2955 int did_replace = FALSE;
2956
2957 for (i = 0; lf->lfFaceName[i]; ++i)
2958 if (lf->lfFaceName[i] == '_')
2959 {
2960 lf->lfFaceName[i] = ' ';
2961 did_replace = TRUE;
2962 }
2963 if (!did_replace || init_logfont(lf) == FAIL)
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002964 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965 }
2966
2967 while (*p == ':')
2968 p++;
2969
2970 /* Set the values found after ':' */
2971 while (*p)
2972 {
2973 switch (*p++)
2974 {
2975 case 'h':
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002976 lf->lfHeight = - points_to_pixels(p, &p, TRUE, (long_i)printer_dc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977 break;
2978 case 'w':
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002979 lf->lfWidth = points_to_pixels(p, &p, FALSE, (long_i)printer_dc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 break;
2981 case 'b':
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 lf->lfWeight = FW_BOLD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 break;
2984 case 'i':
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 lf->lfItalic = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986 break;
2987 case 'u':
2988 lf->lfUnderline = TRUE;
2989 break;
2990 case 's':
2991 lf->lfStrikeOut = TRUE;
2992 break;
2993 case 'c':
2994 {
2995 struct charset_pair *cp;
2996
2997 for (cp = charset_pairs; cp->name != NULL; ++cp)
2998 if (STRNCMP(p, cp->name, strlen(cp->name)) == 0)
2999 {
3000 lf->lfCharSet = cp->charset;
3001 p += strlen(cp->name);
3002 break;
3003 }
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003004 if (cp->name == NULL && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 {
Bram Moolenaar051b7822005-05-19 21:00:46 +00003006 vim_snprintf((char *)IObuff, IOSIZE,
3007 _("E244: Illegal charset name \"%s\" in font name \"%s\""), p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008 EMSG(IObuff);
3009 break;
3010 }
3011 break;
3012 }
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003013 case 'q':
3014 {
3015 struct quality_pair *qp;
3016
3017 for (qp = quality_pairs; qp->name != NULL; ++qp)
3018 if (STRNCMP(p, qp->name, strlen(qp->name)) == 0)
3019 {
3020 lf->lfQuality = qp->quality;
3021 p += strlen(qp->name);
3022 break;
3023 }
3024 if (qp->name == NULL && verbose)
3025 {
3026 vim_snprintf((char *)IObuff, IOSIZE,
3027 _("E244: Illegal quality name \"%s\" in font name \"%s\""), p, name);
3028 EMSG(IObuff);
3029 break;
3030 }
3031 break;
3032 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 default:
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003034 if (verbose)
3035 {
Bram Moolenaar051b7822005-05-19 21:00:46 +00003036 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003037 _("E245: Illegal char '%c' in font name \"%s\""),
3038 p[-1], name);
3039 EMSG(IObuff);
3040 }
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003041 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 }
3043 while (*p == ':')
3044 p++;
3045 }
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003046 ret = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049 /* ron: init lastlf */
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003050 if (ret == OK && printer_dc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051 {
3052 vim_free(lastlf);
3053 lastlf = (LOGFONT *)alloc(sizeof(LOGFONT));
3054 if (lastlf != NULL)
3055 mch_memmove(lastlf, lf, sizeof(LOGFONT));
3056 }
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003057#ifdef FEAT_MBYTE
3058 vim_free(acpname);
3059#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003061 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062}
3063
3064#endif /* defined(FEAT_GUI) || defined(FEAT_PRINTER) */
Bram Moolenaarf12d9832016-01-29 21:11:25 +01003065
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003066#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaarf12d9832016-01-29 21:11:25 +01003067/*
3068 * Initialize the Winsock dll.
3069 */
3070 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003071channel_init_winsock(void)
Bram Moolenaarf12d9832016-01-29 21:11:25 +01003072{
3073 WSADATA wsaData;
3074 int wsaerr;
3075
3076 if (WSInitialized)
3077 return;
3078
3079 wsaerr = WSAStartup(MAKEWORD(2, 2), &wsaData);
3080 if (wsaerr == 0)
3081 WSInitialized = TRUE;
3082}
3083#endif