blob: 607fd6a51fdc364255dea56c1cf8715ec55342af [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
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>
41# ifdef WIN3264
42# include <winspool.h>
43# else
44# include <print.h>
45# endif
46# include <commdlg.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000047#endif
48
Bram Moolenaar82881492012-11-20 16:53:39 +010049#endif /* PROTO */
Bram Moolenaar071d4272004-06-13 20:20:40 +000050
51#ifdef __MINGW32__
52# ifndef FROM_LEFT_1ST_BUTTON_PRESSED
53# define FROM_LEFT_1ST_BUTTON_PRESSED 0x0001
54# endif
55# ifndef RIGHTMOST_BUTTON_PRESSED
56# define RIGHTMOST_BUTTON_PRESSED 0x0002
57# endif
58# ifndef FROM_LEFT_2ND_BUTTON_PRESSED
59# define FROM_LEFT_2ND_BUTTON_PRESSED 0x0004
60# endif
61# ifndef FROM_LEFT_3RD_BUTTON_PRESSED
62# define FROM_LEFT_3RD_BUTTON_PRESSED 0x0008
63# endif
64# ifndef FROM_LEFT_4TH_BUTTON_PRESSED
65# define FROM_LEFT_4TH_BUTTON_PRESSED 0x0010
66# endif
67
68/*
69 * EventFlags
70 */
71# ifndef MOUSE_MOVED
72# define MOUSE_MOVED 0x0001
73# endif
74# ifndef DOUBLE_CLICK
75# define DOUBLE_CLICK 0x0002
76# endif
77#endif
78
79/*
80 * When generating prototypes for Win32 on Unix, these lines make the syntax
81 * errors disappear. They do not need to be correct.
82 */
83#ifdef PROTO
84#define WINAPI
85#define WINBASEAPI
86typedef int BOOL;
87typedef int CALLBACK;
88typedef int COLORREF;
89typedef int CONSOLE_CURSOR_INFO;
90typedef int COORD;
91typedef int DWORD;
92typedef int ENUMLOGFONT;
93typedef int HANDLE;
94typedef int HDC;
95typedef int HFONT;
96typedef int HICON;
97typedef int HWND;
98typedef int INPUT_RECORD;
99typedef int KEY_EVENT_RECORD;
100typedef int LOGFONT;
101typedef int LPARAM;
102typedef int LPBOOL;
103typedef int LPCSTR;
104typedef int LPCWSTR;
105typedef int LPSTR;
106typedef int LPTSTR;
107typedef int LPWSTR;
108typedef int LRESULT;
109typedef int MOUSE_EVENT_RECORD;
110typedef int NEWTEXTMETRIC;
111typedef int PACL;
112typedef int PRINTDLG;
113typedef int PSECURITY_DESCRIPTOR;
114typedef int PSID;
115typedef int SECURITY_INFORMATION;
116typedef int SHORT;
117typedef int SMALL_RECT;
118typedef int TEXTMETRIC;
119typedef int UINT;
120typedef int WCHAR;
121typedef int WORD;
122typedef int WPARAM;
123typedef void VOID;
124#endif
125
126/* Record all output and all keyboard & mouse input */
127/* #define MCH_WRITE_DUMP */
128
129#ifdef MCH_WRITE_DUMP
130FILE* fdDump = NULL;
131#endif
132
133#ifdef WIN3264
134extern DWORD g_PlatformId;
135#endif
136
137#ifndef FEAT_GUI_MSWIN
138extern char g_szOrigTitle[];
139#endif
140
141#ifdef FEAT_GUI
142extern HWND s_hwnd;
143#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144static HWND s_hwnd = 0; /* console window handle, set by GetConsoleHwnd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145#endif
146
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100147#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf12d9832016-01-29 21:11:25 +0100148int WSInitialized = FALSE; /* WinSock is initialized */
149#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150
151/* Don't generate prototypes here, because some systems do have these
152 * functions. */
153#if defined(__GNUC__) && !defined(PROTO)
154# ifndef __MINGW32__
155int _stricoll(char *a, char *b)
156{
157 // the ANSI-ish correct way is to use strxfrm():
158 char a_buff[512], b_buff[512]; // file names, so this is enough on Win32
159 strxfrm(a_buff, a, 512);
160 strxfrm(b_buff, b, 512);
161 return strcoll(a_buff, b_buff);
162}
163
164char * _fullpath(char *buf, char *fname, int len)
165{
166 LPTSTR toss;
167
168 return (char *)GetFullPathName(fname, len, buf, &toss);
169}
170# endif
171
Bram Moolenaaraf62ff32013-03-19 14:48:29 +0100172# if !defined(__MINGW32__) || (__GNUC__ < 4)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173int _chdrive(int drive)
174{
175 char temp [3] = "-:";
176 temp[0] = drive + 'A' - 1;
177 return !SetCurrentDirectory(temp);
178}
Bram Moolenaaraf62ff32013-03-19 14:48:29 +0100179# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180#else
181# ifdef __BORLANDC__
182/* being a more ANSI compliant compiler, BorlandC doesn't define _stricoll:
183 * but it does in BC 5.02! */
184# if __BORLANDC__ < 0x502
185int _stricoll(char *a, char *b)
186{
187# if 1
188 // this is fast but not correct:
189 return stricmp(a, b);
190# else
191 // the ANSI-ish correct way is to use strxfrm():
192 char a_buff[512], b_buff[512]; // file names, so this is enough on Win32
193 strxfrm(a_buff, a, 512);
194 strxfrm(b_buff, b, 512);
195 return strcoll(a_buff, b_buff);
196# endif
197}
198# endif
199# endif
200#endif
201
202
203#if defined(FEAT_GUI_MSWIN) || defined(PROTO)
204/*
205 * GUI version of mch_exit().
206 * Shut down and exit with status `r'
207 * Careful: mch_exit() may be called before mch_init()!
208 */
209 void
210mch_exit(int r)
211{
212 display_errors();
213
214 ml_close_all(TRUE); /* remove all memfiles */
215
216# ifdef FEAT_OLE
217 UninitOLE();
218# endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100219# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220 if (WSInitialized)
221 {
222 WSInitialized = FALSE;
223 WSACleanup();
224 }
225# endif
226#ifdef DYNAMIC_GETTEXT
227 dyn_libintl_end();
228#endif
229
230 if (gui.in_use)
231 gui_exit(r);
Bram Moolenaar85c79d32007-02-20 01:59:20 +0000232
233#ifdef EXITFREE
234 free_all_mem();
235#endif
236
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237 exit(r);
238}
239
240#endif /* FEAT_GUI_MSWIN */
241
242
243/*
244 * Init the tables for toupper() and tolower().
245 */
246 void
247mch_early_init(void)
248{
249 int i;
250
251#ifdef WIN3264
252 PlatformId();
253#endif
254
255 /* Init the tables for toupper() and tolower() */
256 for (i = 0; i < 256; ++i)
257 toupper_tab[i] = tolower_tab[i] = i;
258#ifdef WIN3264
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100259 CharUpperBuff((LPSTR)toupper_tab, 256);
260 CharLowerBuff((LPSTR)tolower_tab, 256);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261#else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100262 AnsiUpperBuff((LPSTR)toupper_tab, 256);
263 AnsiLowerBuff((LPSTR)tolower_tab, 256);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264#endif
265}
266
267
268/*
269 * Return TRUE if the input comes from a terminal, FALSE otherwise.
270 */
271 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100272mch_input_isatty(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273{
274#ifdef FEAT_GUI_MSWIN
275 return OK; /* GUI always has a tty */
276#else
277 if (isatty(read_cmd_fd))
278 return TRUE;
279 return FALSE;
280#endif
281}
282
283#ifdef FEAT_TITLE
284/*
285 * mch_settitle(): set titlebar of our window
286 */
287 void
288mch_settitle(
289 char_u *title,
290 char_u *icon)
291{
292# ifdef FEAT_GUI_MSWIN
293 gui_mch_settitle(title, icon);
294# else
295 if (title != NULL)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000296 {
297# ifdef FEAT_MBYTE
298 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
299 {
300 /* Convert the title from 'encoding' to the active codepage. */
Bram Moolenaar36f692d2008-11-20 16:10:17 +0000301 WCHAR *wp = enc_to_utf16(title, NULL);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000302 int n;
303
304 if (wp != NULL)
305 {
306 n = SetConsoleTitleW(wp);
307 vim_free(wp);
308 if (n != 0 || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
309 return;
310 }
311 }
312# endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100313 SetConsoleTitle((LPCSTR)title);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000314 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315# endif
316}
317
318
319/*
320 * Restore the window/icon title.
321 * which is one of:
322 * 1: Just restore title
323 * 2: Just restore icon (which we don't have)
324 * 3: Restore title and icon (which we don't have)
325 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000326/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327 void
328mch_restore_title(
329 int which)
330{
331#ifndef FEAT_GUI_MSWIN
Bram Moolenaar1df52d72014-10-15 22:50:10 +0200332 SetConsoleTitle(g_szOrigTitle);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333#endif
334}
335
336
337/*
338 * Return TRUE if we can restore the title (we can)
339 */
340 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100341mch_can_restore_title(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342{
343 return TRUE;
344}
345
346
347/*
348 * Return TRUE if we can restore the icon title (we can't)
349 */
350 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100351mch_can_restore_icon(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352{
353 return FALSE;
354}
355#endif /* FEAT_TITLE */
356
357
358/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000359 * Get absolute file name into buffer "buf" of length "len" bytes,
360 * turning all '/'s into '\\'s and getting the correct case of each component
361 * of the file name. Append a (back)slash to a directory name.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362 * When 'shellslash' set do it the other way around.
363 * Return OK or FAIL.
364 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000365/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366 int
367mch_FullName(
368 char_u *fname,
369 char_u *buf,
370 int len,
371 int force)
372{
373 int nResult = FAIL;
374
375#ifdef __BORLANDC__
376 if (*fname == NUL) /* Borland behaves badly here - make it consistent */
377 nResult = mch_dirname(buf, len);
378 else
379#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000381#ifdef FEAT_MBYTE
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000382 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage
383# ifdef __BORLANDC__
384 /* Wide functions of Borland C 5.5 do not work on Windows 98. */
385 && g_PlatformId == VER_PLATFORM_WIN32_NT
386# endif
387 )
388 {
389 WCHAR *wname;
390 WCHAR wbuf[MAX_PATH];
391 char_u *cname = NULL;
392
393 /* Use the wide function:
394 * - convert the fname from 'encoding' to UCS2.
395 * - invoke _wfullpath()
396 * - convert the result from UCS2 to 'encoding'.
397 */
Bram Moolenaar36f692d2008-11-20 16:10:17 +0000398 wname = enc_to_utf16(fname, NULL);
Bram Moolenaar374bf022014-11-05 19:33:24 +0100399 if (wname != NULL && _wfullpath(wbuf, wname, MAX_PATH) != NULL)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000400 {
Bram Moolenaar36f692d2008-11-20 16:10:17 +0000401 cname = utf16_to_enc((short_u *)wbuf, NULL);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000402 if (cname != NULL)
403 {
Bram Moolenaarfe3ca8d2005-07-18 21:43:02 +0000404 vim_strncpy(buf, cname, len - 1);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000405 nResult = OK;
406 }
407 }
408 vim_free(wname);
409 vim_free(cname);
410 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000411 if (nResult == FAIL) /* fall back to non-wide function */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000413 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100414 if (_fullpath((char *)buf, (const char *)fname, len - 1) == NULL)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000415 {
Bram Moolenaarfe3ca8d2005-07-18 21:43:02 +0000416 /* failed, use relative path name */
417 vim_strncpy(buf, fname, len - 1);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000418 }
419 else
420 nResult = OK;
421 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423
424#ifdef USE_FNAME_CASE
425 fname_case(buf, len);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000426#else
427 slash_adjust(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428#endif
429
430 return nResult;
431}
432
433
434/*
435 * Return TRUE if "fname" does not depend on the current directory.
436 */
437 int
438mch_isFullName(char_u *fname)
439{
Bram Moolenaard2a203b2013-08-30 16:51:18 +0200440#ifdef FEAT_MBYTE
441 /* WinNT and later can use _MAX_PATH wide characters for a pathname, which
442 * means that the maximum pathname is _MAX_PATH * 3 bytes when 'enc' is
443 * UTF-8. */
444 char szName[_MAX_PATH * 3 + 1];
445#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446 char szName[_MAX_PATH + 1];
Bram Moolenaard2a203b2013-08-30 16:51:18 +0200447#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448
449 /* A name like "d:/foo" and "//server/share" is absolute */
450 if ((fname[0] && fname[1] == ':' && (fname[2] == '/' || fname[2] == '\\'))
451 || (fname[0] == fname[1] && (fname[0] == '/' || fname[0] == '\\')))
452 return TRUE;
453
454 /* A name that can't be made absolute probably isn't absolute. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100455 if (mch_FullName(fname, (char_u *)szName, sizeof(szName) - 1, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456 return FALSE;
457
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100458 return pathcmp((const char *)fname, (const char *)szName, -1) == 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459}
460
461/*
462 * Replace all slashes by backslashes.
463 * This used to be the other way around, but MS-DOS sometimes has problems
464 * with slashes (e.g. in a command name). We can't have mixed slashes and
465 * backslashes, because comparing file names will not work correctly. The
466 * commands that use a file name should try to avoid the need to type a
467 * backslash twice.
468 * When 'shellslash' set do it the other way around.
Bram Moolenaarb4f6a462015-10-13 19:43:17 +0200469 * When the path looks like a URL leave it unmodified.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470 */
471 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100472slash_adjust(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473{
Bram Moolenaarb4f6a462015-10-13 19:43:17 +0200474 if (path_with_url(p))
475 return;
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +0000476 while (*p)
477 {
478 if (*p == psepcN)
479 *p = psepc;
480 mb_ptr_adv(p);
481 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482}
483
Bram Moolenaar9d1685d2014-01-14 12:18:45 +0100484#if (defined(_MSC_VER) && (_MSC_VER >= 1300)) || defined(__MINGW32__)
Bram Moolenaar8962fda2013-09-29 19:05:21 +0200485# define OPEN_OH_ARGTYPE intptr_t
486#else
487# define OPEN_OH_ARGTYPE long
488#endif
489
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200490 static int
491stat_symlink_aware(const char *name, struct stat *stp)
492{
Bram Moolenaarfce7b3d2016-01-19 19:00:32 +0100493#if (defined(_MSC_VER) && (_MSC_VER < 1900)) || defined(__MINGW32__)
494 /* Work around for VC12 or earlier (and MinGW). stat() can't handle
495 * symlinks properly.
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200496 * VC9 or earlier: stat() doesn't support a symlink at all. It retrieves
497 * status of a symlink itself.
498 * VC10: stat() supports a symlink to a normal file, but it doesn't support
Bram Moolenaarfce7b3d2016-01-19 19:00:32 +0100499 * a symlink to a directory (always returns an error).
500 * VC11 and VC12: stat() doesn't return an error for a symlink to a
501 * directory, but it doesn't set S_IFDIR flag.
502 * MinGW: Same as VC9. */
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200503 WIN32_FIND_DATA findData;
504 HANDLE hFind, h;
505 DWORD attr = 0;
506 BOOL is_symlink = FALSE;
507
508 hFind = FindFirstFile(name, &findData);
509 if (hFind != INVALID_HANDLE_VALUE)
510 {
511 attr = findData.dwFileAttributes;
512 if ((attr & FILE_ATTRIBUTE_REPARSE_POINT)
513 && (findData.dwReserved0 == IO_REPARSE_TAG_SYMLINK))
514 is_symlink = TRUE;
515 FindClose(hFind);
516 }
517 if (is_symlink)
518 {
519 h = CreateFile(name, FILE_READ_ATTRIBUTES,
520 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
521 OPEN_EXISTING,
522 (attr & FILE_ATTRIBUTE_DIRECTORY)
523 ? FILE_FLAG_BACKUP_SEMANTICS : 0,
524 NULL);
525 if (h != INVALID_HANDLE_VALUE)
526 {
527 int fd, n;
528
Bram Moolenaar8962fda2013-09-29 19:05:21 +0200529 fd = _open_osfhandle((OPEN_OH_ARGTYPE)h, _O_RDONLY);
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200530 n = _fstat(fd, (struct _stat*)stp);
Bram Moolenaarfce7b3d2016-01-19 19:00:32 +0100531 if ((n == 0) && (attr & FILE_ATTRIBUTE_DIRECTORY))
532 stp->st_mode = (stp->st_mode & ~S_IFREG) | S_IFDIR;
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200533 _close(fd);
534 return n;
535 }
536 }
537#endif
538 return stat(name, stp);
539}
540
541#ifdef FEAT_MBYTE
542 static int
543wstat_symlink_aware(const WCHAR *name, struct _stat *stp)
544{
Bram Moolenaarfce7b3d2016-01-19 19:00:32 +0100545# if (defined(_MSC_VER) && (_MSC_VER < 1900)) || defined(__MINGW32__)
546 /* Work around for VC12 or earlier (and MinGW). _wstat() can't handle
547 * symlinks properly.
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200548 * VC9 or earlier: _wstat() doesn't support a symlink at all. It retrieves
549 * status of a symlink itself.
550 * VC10: _wstat() supports a symlink to a normal file, but it doesn't
Bram Moolenaarfce7b3d2016-01-19 19:00:32 +0100551 * support a symlink to a directory (always returns an error).
552 * VC11 and VC12: _wstat() doesn't return an error for a symlink to a
553 * directory, but it doesn't set S_IFDIR flag.
554 * MinGW: Same as VC9. */
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200555 int n;
556 BOOL is_symlink = FALSE;
557 HANDLE hFind, h;
558 DWORD attr = 0;
559 WIN32_FIND_DATAW findDataW;
560
561 hFind = FindFirstFileW(name, &findDataW);
562 if (hFind != INVALID_HANDLE_VALUE)
563 {
564 attr = findDataW.dwFileAttributes;
565 if ((attr & FILE_ATTRIBUTE_REPARSE_POINT)
566 && (findDataW.dwReserved0 == IO_REPARSE_TAG_SYMLINK))
567 is_symlink = TRUE;
568 FindClose(hFind);
569 }
570 if (is_symlink)
571 {
572 h = CreateFileW(name, FILE_READ_ATTRIBUTES,
573 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
574 OPEN_EXISTING,
575 (attr & FILE_ATTRIBUTE_DIRECTORY)
576 ? FILE_FLAG_BACKUP_SEMANTICS : 0,
577 NULL);
578 if (h != INVALID_HANDLE_VALUE)
579 {
580 int fd;
581
Bram Moolenaar8962fda2013-09-29 19:05:21 +0200582 fd = _open_osfhandle((OPEN_OH_ARGTYPE)h, _O_RDONLY);
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200583 n = _fstat(fd, stp);
Bram Moolenaarfce7b3d2016-01-19 19:00:32 +0100584 if ((n == 0) && (attr & FILE_ATTRIBUTE_DIRECTORY))
585 stp->st_mode = (stp->st_mode & ~S_IFREG) | S_IFDIR;
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200586 _close(fd);
587 return n;
588 }
589 }
590# endif
591 return _wstat(name, stp);
592}
593#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594
595/*
596 * stat() can't handle a trailing '/' or '\', remove it first.
597 */
598 int
599vim_stat(const char *name, struct stat *stp)
600{
Bram Moolenaard2a203b2013-08-30 16:51:18 +0200601#ifdef FEAT_MBYTE
602 /* WinNT and later can use _MAX_PATH wide characters for a pathname, which
603 * means that the maximum pathname is _MAX_PATH * 3 bytes when 'enc' is
604 * UTF-8. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100605 char_u buf[_MAX_PATH * 3 + 1];
Bram Moolenaard2a203b2013-08-30 16:51:18 +0200606#else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100607 char_u buf[_MAX_PATH + 1];
Bram Moolenaard2a203b2013-08-30 16:51:18 +0200608#endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100609 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610
Bram Moolenaard2a203b2013-08-30 16:51:18 +0200611 vim_strncpy((char_u *)buf, (char_u *)name, sizeof(buf) - 1);
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100612 p = buf + STRLEN(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613 if (p > buf)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000614 mb_ptr_back(buf, p);
Bram Moolenaarb1cb35f2014-01-10 13:05:20 +0100615
616 /* Remove trailing '\\' except root path. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617 if (p > buf && (*p == '\\' || *p == '/') && p[-1] != ':')
618 *p = NUL;
Bram Moolenaarb1cb35f2014-01-10 13:05:20 +0100619
620 if ((buf[0] == '\\' && buf[1] == '\\') || (buf[0] == '/' && buf[1] == '/'))
621 {
622 /* UNC root path must be followed by '\\'. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100623 p = vim_strpbrk(buf + 2, (char_u *)"\\/");
Bram Moolenaarb1cb35f2014-01-10 13:05:20 +0100624 if (p != NULL)
625 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100626 p = vim_strpbrk(p + 1, (char_u *)"\\/");
Bram Moolenaarb1cb35f2014-01-10 13:05:20 +0100627 if (p == NULL)
628 STRCAT(buf, "\\");
629 }
630 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631#ifdef FEAT_MBYTE
632 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage
633# ifdef __BORLANDC__
634 /* Wide functions of Borland C 5.5 do not work on Windows 98. */
635 && g_PlatformId == VER_PLATFORM_WIN32_NT
636# endif
637 )
638 {
Bram Moolenaar36f692d2008-11-20 16:10:17 +0000639 WCHAR *wp = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 int n;
641
642 if (wp != NULL)
643 {
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200644 n = wstat_symlink_aware(wp, (struct _stat *)stp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 vim_free(wp);
Bram Moolenaarcd981f22014-02-11 17:06:00 +0100646 if (n >= 0 || g_PlatformId == VER_PLATFORM_WIN32_NT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647 return n;
648 /* Retry with non-wide function (for Windows 98). Can't use
649 * GetLastError() here and it's unclear what errno gets set to if
650 * the _wstat() fails for missing wide functions. */
651 }
652 }
653#endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100654 return stat_symlink_aware((char *)buf, stp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655}
656
657#if defined(FEAT_GUI_MSWIN) || defined(PROTO)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000658/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 void
660mch_settmode(int tmode)
661{
662 /* nothing to do */
663}
664
665 int
666mch_get_shellsize(void)
667{
668 /* never used */
669 return OK;
670}
671
672 void
673mch_set_shellsize(void)
674{
675 /* never used */
676}
677
678/*
679 * Rows and/or Columns has changed.
680 */
681 void
682mch_new_shellsize(void)
683{
684 /* never used */
685}
686
687#endif
688
689/*
690 * We have no job control, so fake it by starting a new shell.
691 */
692 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100693mch_suspend(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694{
695 suspend_shell();
696}
697
698#if defined(USE_MCH_ERRMSG) || defined(PROTO)
699
700#ifdef display_errors
701# undef display_errors
702#endif
703
704/*
705 * Display the saved error message(s).
706 */
707 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100708display_errors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709{
710 char *p;
711
712 if (error_ga.ga_data != NULL)
713 {
714 /* avoid putting up a message box with blanks only */
715 for (p = (char *)error_ga.ga_data; *p; ++p)
716 if (!isspace(*p))
717 {
Bram Moolenaarc17ef8e2006-03-25 21:48:58 +0000718 (void)gui_mch_dialog(
719#ifdef FEAT_GUI
720 gui.starting ? VIM_INFO :
721#endif
722 VIM_ERROR,
723#ifdef FEAT_GUI
724 gui.starting ? (char_u *)_("Message") :
725#endif
726 (char_u *)_("Error"),
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100727 (char_u *)p, (char_u *)_("&Ok"),
728 1, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 break;
730 }
731 ga_clear(&error_ga);
732 }
733}
734#endif
735
736
737/*
738 * Return TRUE if "p" contain a wildcard that can be expanded by
739 * dos_expandpath().
740 */
741 int
742mch_has_exp_wildcard(char_u *p)
743{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000744 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 {
746 if (vim_strchr((char_u *)"?*[", *p) != NULL
747 || (*p == '~' && p[1] != NUL))
748 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 }
750 return FALSE;
751}
752
753/*
754 * Return TRUE if "p" contain a wildcard or a "~1" kind of thing (could be a
755 * shortened file name).
756 */
757 int
758mch_has_wildcard(char_u *p)
759{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000760 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 {
762 if (vim_strchr((char_u *)
763# ifdef VIM_BACKTICK
764 "?*$[`"
765# else
766 "?*$["
767# endif
768 , *p) != NULL
769 || (*p == '~' && p[1] != NUL))
770 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 }
772 return FALSE;
773}
774
775
776/*
777 * The normal _chdir() does not change the default drive. This one does.
778 * Returning 0 implies success; -1 implies failure.
779 */
780 int
781mch_chdir(char *path)
782{
783 if (path[0] == NUL) /* just checking... */
784 return -1;
785
Bram Moolenaara2974d72009-07-14 16:38:36 +0000786 if (p_verbose >= 5)
787 {
788 verbose_enter();
789 smsg((char_u *)"chdir(%s)", path);
790 verbose_leave();
791 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 if (isalpha(path[0]) && path[1] == ':') /* has a drive name */
793 {
794 /* If we can change to the drive, skip that part of the path. If we
795 * can't then the current directory may be invalid, try using chdir()
796 * with the whole path. */
797 if (_chdrive(TOLOWER_ASC(path[0]) - 'a' + 1) == 0)
798 path += 2;
799 }
800
801 if (*path == NUL) /* drive name only */
802 return 0;
803
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000804#ifdef FEAT_MBYTE
805 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
806 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100807 WCHAR *p = enc_to_utf16((char_u *)path, NULL);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000808 int n;
809
810 if (p != NULL)
811 {
812 n = _wchdir(p);
813 vim_free(p);
Bram Moolenaarcd981f22014-02-11 17:06:00 +0100814 if (n == 0 || g_PlatformId == VER_PLATFORM_WIN32_NT)
815 return n;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000816 /* Retry with non-wide function (for Windows 98). */
817 }
818 }
819#endif
820
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 return chdir(path); /* let the normal chdir() do the rest */
822}
823
824
825/*
826 * Switching off termcap mode is only allowed when Columns is 80, otherwise a
827 * crash may result. It's always allowed on NT or when running the GUI.
828 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000829/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 int
831can_end_termcap_mode(
832 int give_msg)
833{
834#ifdef FEAT_GUI_MSWIN
835 return TRUE; /* GUI starts a new console anyway */
836#else
837 if (g_PlatformId == VER_PLATFORM_WIN32_NT || Columns == 80)
838 return TRUE;
839 if (give_msg)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100840 msg((char_u *)
841 _("'columns' is not 80, cannot execute external commands"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 return FALSE;
843#endif
844}
845
846#ifdef FEAT_GUI_MSWIN
847/*
848 * return non-zero if a character is available
849 */
850 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100851mch_char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852{
853 /* never used */
854 return TRUE;
855}
856#endif
857
858
859/*
860 * set screen mode, always fails.
861 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000862/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 int
864mch_screenmode(
865 char_u *arg)
866{
867 EMSG(_(e_screenmode));
868 return FAIL;
869}
870
871
872#if defined(FEAT_LIBCALL) || defined(PROTO)
873/*
874 * Call a DLL routine which takes either a string or int param
875 * and returns an allocated string.
876 * Return OK if it worked, FAIL if not.
877 */
878# ifdef WIN3264
879typedef LPTSTR (*MYSTRPROCSTR)(LPTSTR);
880typedef LPTSTR (*MYINTPROCSTR)(int);
881typedef int (*MYSTRPROCINT)(LPTSTR);
882typedef int (*MYINTPROCINT)(int);
883# else
884typedef LPSTR (*MYSTRPROCSTR)(LPSTR);
885typedef LPSTR (*MYINTPROCSTR)(int);
886typedef int (*MYSTRPROCINT)(LPSTR);
887typedef int (*MYINTPROCINT)(int);
888# endif
889
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890/*
891 * Check if a pointer points to a valid NUL terminated string.
892 * Return the length of the string, including terminating NUL.
893 * Returns 0 for an invalid pointer, 1 for an empty string.
894 */
895 static size_t
896check_str_len(char_u *str)
897{
898 SYSTEM_INFO si;
899 MEMORY_BASIC_INFORMATION mbi;
900 size_t length = 0;
901 size_t i;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100902 const char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903
904 /* get page size */
905 GetSystemInfo(&si);
906
907 /* get memory information */
908 if (VirtualQuery(str, &mbi, sizeof(mbi)))
909 {
910 /* pre cast these (typing savers) */
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000911 long_u dwStr = (long_u)str;
912 long_u dwBaseAddress = (long_u)mbi.BaseAddress;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913
914 /* get start address of page that str is on */
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000915 long_u strPage = dwStr - (dwStr - dwBaseAddress) % si.dwPageSize;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916
917 /* get length from str to end of page */
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000918 long_u pageLength = si.dwPageSize - (dwStr - strPage);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919
Bram Moolenaar442b4222010-05-24 21:34:22 +0200920 for (p = str; !IsBadReadPtr(p, (UINT)pageLength);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 p += pageLength, pageLength = si.dwPageSize)
922 for (i = 0; i < pageLength; ++i, ++length)
923 if (p[i] == NUL)
924 return length + 1;
925 }
926
927 return 0;
928}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200930/*
931 * Passed to do_in_runtimepath() to load a vim.ico file.
932 */
933 static void
934mch_icon_load_cb(char_u *fname, void *cookie)
935{
936 HANDLE *h = (HANDLE *)cookie;
937
938 *h = LoadImage(NULL,
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100939 (LPSTR)fname,
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200940 IMAGE_ICON,
941 64,
942 64,
943 LR_LOADFROMFILE | LR_LOADMAP3DCOLORS);
944}
945
946/*
947 * Try loading an icon file from 'runtimepath'.
948 */
949 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100950mch_icon_load(HANDLE *iconp)
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200951{
952 return do_in_runtimepath((char_u *)"bitmaps/vim.ico",
Bram Moolenaar7f8989d2016-03-12 22:11:39 +0100953 0, mch_icon_load_cb, iconp);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200954}
955
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 int
957mch_libcall(
958 char_u *libname,
959 char_u *funcname,
960 char_u *argstring, /* NULL when using a argint */
961 int argint,
962 char_u **string_result,/* NULL when using number_result */
963 int *number_result)
964{
965 HINSTANCE hinstLib;
966 MYSTRPROCSTR ProcAdd;
967 MYINTPROCSTR ProcAddI;
968 char_u *retval_str = NULL;
969 int retval_int = 0;
970 size_t len;
971
972 BOOL fRunTimeLinkSuccess = FALSE;
973
974 // Get a handle to the DLL module.
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100975 hinstLib = vimLoadLib((char *)libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976
977 // If the handle is valid, try to get the function address.
978 if (hinstLib != NULL)
979 {
980#ifdef HAVE_TRY_EXCEPT
981 __try
982 {
983#endif
984 if (argstring != NULL)
985 {
986 /* Call with string argument */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100987 ProcAdd = (MYSTRPROCSTR)GetProcAddress(hinstLib, (LPCSTR)funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 if ((fRunTimeLinkSuccess = (ProcAdd != NULL)) != 0)
989 {
990 if (string_result == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100991 retval_int = ((MYSTRPROCINT)ProcAdd)((LPSTR)argstring);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100993 retval_str = (char_u *)(ProcAdd)((LPSTR)argstring);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 }
995 }
996 else
997 {
998 /* Call with number argument */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100999 ProcAddI = (MYINTPROCSTR) GetProcAddress(hinstLib, (LPCSTR)funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 if ((fRunTimeLinkSuccess = (ProcAddI != NULL)) != 0)
1001 {
1002 if (string_result == NULL)
1003 retval_int = ((MYINTPROCINT)ProcAddI)(argint);
1004 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001005 retval_str = (char_u *)(ProcAddI)(argint);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 }
1007 }
1008
1009 // Save the string before we free the library.
1010 // Assume that a "1" result is an illegal pointer.
1011 if (string_result == NULL)
1012 *number_result = retval_int;
1013 else if (retval_str != NULL
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001014 && (len = check_str_len(retval_str)) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 {
1016 *string_result = lalloc((long_u)len, TRUE);
1017 if (*string_result != NULL)
1018 mch_memmove(*string_result, retval_str, len);
1019 }
1020
1021#ifdef HAVE_TRY_EXCEPT
1022 }
1023 __except(EXCEPTION_EXECUTE_HANDLER)
1024 {
1025 if (GetExceptionCode() == EXCEPTION_STACK_OVERFLOW)
1026 RESETSTKOFLW();
1027 fRunTimeLinkSuccess = 0;
1028 }
1029#endif
1030
1031 // Free the DLL module.
1032 (void)FreeLibrary(hinstLib);
1033 }
1034
1035 if (!fRunTimeLinkSuccess)
1036 {
1037 EMSG2(_(e_libcall), funcname);
1038 return FAIL;
1039 }
1040
1041 return OK;
1042}
1043#endif
1044
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045/*
1046 * Debugging helper: expose the MCH_WRITE_DUMP stuff to other modules
1047 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001048/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 void
1050DumpPutS(
1051 const char *psz)
1052{
1053# ifdef MCH_WRITE_DUMP
1054 if (fdDump)
1055 {
1056 fputs(psz, fdDump);
1057 if (psz[strlen(psz) - 1] != '\n')
1058 fputc('\n', fdDump);
1059 fflush(fdDump);
1060 }
1061# endif
1062}
1063
1064#ifdef _DEBUG
1065
1066void __cdecl
1067Trace(
1068 char *pszFormat,
1069 ...)
1070{
1071 CHAR szBuff[2048];
1072 va_list args;
1073
1074 va_start(args, pszFormat);
1075 vsprintf(szBuff, pszFormat, args);
1076 va_end(args);
1077
1078 OutputDebugString(szBuff);
1079}
1080
1081#endif //_DEBUG
1082
Bram Moolenaar843ee412004-06-30 16:16:41 +00001083#if !defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084# if defined(FEAT_TITLE) && defined(WIN3264)
1085extern HWND g_hWnd; /* This is in os_win32.c. */
1086# endif
1087
1088/*
1089 * Showing the printer dialog is tricky since we have no GUI
1090 * window to parent it. The following routines are needed to
1091 * get the window parenting and Z-order to work properly.
1092 */
1093 static void
1094GetConsoleHwnd(void)
1095{
1096# define MY_BUFSIZE 1024 // Buffer size for console window titles.
1097
1098 char pszNewWindowTitle[MY_BUFSIZE]; // Contains fabricated WindowTitle.
1099 char pszOldWindowTitle[MY_BUFSIZE]; // Contains original WindowTitle.
1100
1101 /* Skip if it's already set. */
1102 if (s_hwnd != 0)
1103 return;
1104
1105# if defined(FEAT_TITLE) && defined(WIN3264)
1106 /* Window handle may have been found by init code (Windows NT only) */
1107 if (g_hWnd != 0)
1108 {
1109 s_hwnd = g_hWnd;
1110 return;
1111 }
1112# endif
1113
1114 GetConsoleTitle(pszOldWindowTitle, MY_BUFSIZE);
1115
1116 wsprintf(pszNewWindowTitle, "%s/%d/%d",
1117 pszOldWindowTitle,
1118 GetTickCount(),
1119 GetCurrentProcessId());
1120 SetConsoleTitle(pszNewWindowTitle);
1121 Sleep(40);
1122 s_hwnd = FindWindow(NULL, pszNewWindowTitle);
1123
1124 SetConsoleTitle(pszOldWindowTitle);
1125}
Bram Moolenaar843ee412004-06-30 16:16:41 +00001126
1127/*
1128 * Console implementation of ":winpos".
1129 */
1130 int
1131mch_get_winpos(int *x, int *y)
1132{
1133 RECT rect;
1134
1135 GetConsoleHwnd();
1136 GetWindowRect(s_hwnd, &rect);
1137 *x = rect.left;
1138 *y = rect.top;
1139 return OK;
1140}
1141
1142/*
1143 * Console implementation of ":winpos x y".
1144 */
1145 void
1146mch_set_winpos(int x, int y)
1147{
1148 GetConsoleHwnd();
1149 SetWindowPos(s_hwnd, NULL, x, y, 0, 0,
1150 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
1151}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152#endif
1153
1154#if (defined(FEAT_PRINTER) && !defined(FEAT_POSTSCRIPT)) || defined(PROTO)
1155
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156/*=================================================================
1157 * Win32 printer stuff
1158 */
1159
1160static HFONT prt_font_handles[2][2][2];
1161static PRINTDLG prt_dlg;
1162static const int boldface[2] = {FW_REGULAR, FW_BOLD};
1163static TEXTMETRIC prt_tm;
1164static int prt_line_height;
1165static int prt_number_width;
1166static int prt_left_margin;
1167static int prt_right_margin;
1168static int prt_top_margin;
1169static char_u szAppName[] = TEXT("VIM");
1170static HWND hDlgPrint;
1171static int *bUserAbort = NULL;
1172static char_u *prt_name = NULL;
1173
1174/* Defines which are also in vim.rc. */
1175#define IDC_BOX1 400
1176#define IDC_PRINTTEXT1 401
1177#define IDC_PRINTTEXT2 402
1178#define IDC_PROGRESS 403
1179
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001180#if !defined(FEAT_MBYTE)
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001181# define vimSetDlgItemText(h, i, s) SetDlgItemText(h, i, s)
1182#else
1183 static BOOL
1184vimSetDlgItemText(HWND hDlg, int nIDDlgItem, char_u *s)
1185{
1186 WCHAR *wp = NULL;
1187 BOOL ret;
1188
1189 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
1190 {
1191 wp = enc_to_utf16(s, NULL);
1192 }
1193 if (wp != NULL)
1194 {
1195 ret = SetDlgItemTextW(hDlg, nIDDlgItem, wp);
1196 vim_free(wp);
1197 return ret;
1198 }
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001199 return SetDlgItemText(hDlg, nIDDlgItem, (LPCSTR)s);
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001200}
1201#endif
1202
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203/*
1204 * Convert BGR to RGB for Windows GDI calls
1205 */
1206 static COLORREF
1207swap_me(COLORREF colorref)
1208{
1209 int temp;
1210 char *ptr = (char *)&colorref;
1211
1212 temp = *(ptr);
1213 *(ptr ) = *(ptr + 2);
1214 *(ptr + 2) = temp;
1215 return colorref;
1216}
1217
Bram Moolenaared39e1d2008-08-09 17:55:22 +00001218/* Attempt to make this work for old and new compilers */
Bram Moolenaar9f733d12011-09-21 20:09:42 +02001219#if !defined(_WIN64) && (!defined(_MSC_VER) || _MSC_VER < 1300)
Bram Moolenaared39e1d2008-08-09 17:55:22 +00001220# define PDP_RETVAL BOOL
1221#else
1222# define PDP_RETVAL INT_PTR
1223#endif
1224
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001225/*ARGSUSED*/
Bram Moolenaared39e1d2008-08-09 17:55:22 +00001226 static PDP_RETVAL CALLBACK
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227PrintDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
1228{
1229#ifdef FEAT_GETTEXT
1230 NONCLIENTMETRICS nm;
1231 static HFONT hfont;
1232#endif
1233
1234 switch (message)
1235 {
1236 case WM_INITDIALOG:
1237#ifdef FEAT_GETTEXT
1238 nm.cbSize = sizeof(NONCLIENTMETRICS);
1239 if (SystemParametersInfo(
1240 SPI_GETNONCLIENTMETRICS,
1241 sizeof(NONCLIENTMETRICS),
1242 &nm,
1243 0))
1244 {
1245 char buff[MAX_PATH];
1246 int i;
1247
1248 /* Translate the dialog texts */
1249 hfont = CreateFontIndirect(&nm.lfMessageFont);
1250 for (i = IDC_PRINTTEXT1; i <= IDC_PROGRESS; i++)
1251 {
1252 SendDlgItemMessage(hDlg, i, WM_SETFONT, (WPARAM)hfont, 1);
1253 if (GetDlgItemText(hDlg,i, buff, sizeof(buff)))
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001254 vimSetDlgItemText(hDlg,i, (char_u *)_(buff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 }
1256 SendDlgItemMessage(hDlg, IDCANCEL,
1257 WM_SETFONT, (WPARAM)hfont, 1);
1258 if (GetDlgItemText(hDlg,IDCANCEL, buff, sizeof(buff)))
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001259 vimSetDlgItemText(hDlg,IDCANCEL, (char_u *)_(buff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 }
1261#endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001262 SetWindowText(hDlg, (LPCSTR)szAppName);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 if (prt_name != NULL)
1264 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001265 vimSetDlgItemText(hDlg, IDC_PRINTTEXT2, (char_u *)prt_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 vim_free(prt_name);
1267 prt_name = NULL;
1268 }
1269 EnableMenuItem(GetSystemMenu(hDlg, FALSE), SC_CLOSE, MF_GRAYED);
1270#ifndef FEAT_GUI
1271 BringWindowToTop(s_hwnd);
1272#endif
1273 return TRUE;
1274
1275 case WM_COMMAND:
1276 *bUserAbort = TRUE;
1277 EnableWindow(GetParent(hDlg), TRUE);
1278 DestroyWindow(hDlg);
1279 hDlgPrint = NULL;
1280#ifdef FEAT_GETTEXT
1281 DeleteObject(hfont);
1282#endif
1283 return TRUE;
1284 }
1285 return FALSE;
1286}
1287
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001288/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 static BOOL CALLBACK
1290AbortProc(HDC hdcPrn, int iCode)
1291{
1292 MSG msg;
1293
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02001294 while (!*bUserAbort && pPeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295 {
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02001296 if (!hDlgPrint || !pIsDialogMessage(hDlgPrint, &msg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 {
1298 TranslateMessage(&msg);
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02001299 pDispatchMessage(&msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 }
1301 }
1302 return !*bUserAbort;
1303}
1304
1305#ifndef FEAT_GUI
1306
Bram Moolenaar26fdd7d2011-11-30 13:42:44 +01001307 static UINT_PTR CALLBACK
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308PrintHookProc(
1309 HWND hDlg, // handle to dialog box
1310 UINT uiMsg, // message identifier
1311 WPARAM wParam, // message parameter
1312 LPARAM lParam // message parameter
1313 )
1314{
1315 HWND hwndOwner;
1316 RECT rc, rcDlg, rcOwner;
1317 PRINTDLG *pPD;
1318
1319 if (uiMsg == WM_INITDIALOG)
1320 {
1321 // Get the owner window and dialog box rectangles.
1322 if ((hwndOwner = GetParent(hDlg)) == NULL)
1323 hwndOwner = GetDesktopWindow();
1324
1325 GetWindowRect(hwndOwner, &rcOwner);
1326 GetWindowRect(hDlg, &rcDlg);
1327 CopyRect(&rc, &rcOwner);
1328
1329 // Offset the owner and dialog box rectangles so that
1330 // right and bottom values represent the width and
1331 // height, and then offset the owner again to discard
1332 // space taken up by the dialog box.
1333
1334 OffsetRect(&rcDlg, -rcDlg.left, -rcDlg.top);
1335 OffsetRect(&rc, -rc.left, -rc.top);
1336 OffsetRect(&rc, -rcDlg.right, -rcDlg.bottom);
1337
1338 // The new position is the sum of half the remaining
1339 // space and the owner's original position.
1340
1341 SetWindowPos(hDlg,
1342 HWND_TOP,
1343 rcOwner.left + (rc.right / 2),
1344 rcOwner.top + (rc.bottom / 2),
1345 0, 0, // ignores size arguments
1346 SWP_NOSIZE);
1347
1348 /* tackle the printdlg copiesctrl problem */
1349 pPD = (PRINTDLG *)lParam;
1350 pPD->nCopies = (WORD)pPD->lCustData;
1351 SetDlgItemInt( hDlg, edt3, pPD->nCopies, FALSE );
1352 /* Bring the window to top */
1353 BringWindowToTop(GetParent(hDlg));
1354 SetForegroundWindow(hDlg);
1355 }
1356
1357 return FALSE;
1358}
1359#endif
1360
1361 void
1362mch_print_cleanup(void)
1363{
1364 int pifItalic;
1365 int pifBold;
1366 int pifUnderline;
1367
1368 for (pifBold = 0; pifBold <= 1; pifBold++)
1369 for (pifItalic = 0; pifItalic <= 1; pifItalic++)
1370 for (pifUnderline = 0; pifUnderline <= 1; pifUnderline++)
1371 DeleteObject(prt_font_handles[pifBold][pifItalic][pifUnderline]);
1372
1373 if (prt_dlg.hDC != NULL)
1374 DeleteDC(prt_dlg.hDC);
1375 if (!*bUserAbort)
1376 SendMessage(hDlgPrint, WM_COMMAND, 0, 0);
1377}
1378
1379 static int
1380to_device_units(int idx, int dpi, int physsize, int offset, int def_number)
1381{
1382 int ret = 0;
1383 int u;
1384 int nr;
1385
1386 u = prt_get_unit(idx);
1387 if (u == PRT_UNIT_NONE)
1388 {
1389 u = PRT_UNIT_PERC;
1390 nr = def_number;
1391 }
1392 else
1393 nr = printer_opts[idx].number;
1394
1395 switch (u)
1396 {
1397 case PRT_UNIT_PERC:
1398 ret = (physsize * nr) / 100;
1399 break;
1400 case PRT_UNIT_INCH:
1401 ret = (nr * dpi);
1402 break;
1403 case PRT_UNIT_MM:
1404 ret = (nr * 10 * dpi) / 254;
1405 break;
1406 case PRT_UNIT_POINT:
1407 ret = (nr * 10 * dpi) / 720;
1408 break;
1409 }
1410
1411 if (ret < offset)
1412 return 0;
1413 else
1414 return ret - offset;
1415}
1416
1417 static int
1418prt_get_cpl(void)
1419{
1420 int hr;
1421 int phyw;
1422 int dvoff;
1423 int rev_offset;
1424 int dpi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425
1426 GetTextMetrics(prt_dlg.hDC, &prt_tm);
1427 prt_line_height = prt_tm.tmHeight + prt_tm.tmExternalLeading;
1428
1429 hr = GetDeviceCaps(prt_dlg.hDC, HORZRES);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 phyw = GetDeviceCaps(prt_dlg.hDC, PHYSICALWIDTH);
1431 dvoff = GetDeviceCaps(prt_dlg.hDC, PHYSICALOFFSETX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 dpi = GetDeviceCaps(prt_dlg.hDC, LOGPIXELSX);
1433
1434 rev_offset = phyw - (dvoff + hr);
1435
1436 prt_left_margin = to_device_units(OPT_PRINT_LEFT, dpi, phyw, dvoff, 10);
1437 if (prt_use_number())
1438 {
1439 prt_number_width = PRINT_NUMBER_WIDTH * prt_tm.tmAveCharWidth;
1440 prt_left_margin += prt_number_width;
1441 }
1442 else
1443 prt_number_width = 0;
1444
1445 prt_right_margin = hr - to_device_units(OPT_PRINT_RIGHT, dpi, phyw,
1446 rev_offset, 5);
1447
1448 return (prt_right_margin - prt_left_margin) / prt_tm.tmAveCharWidth;
1449}
1450
1451 static int
1452prt_get_lpp(void)
1453{
1454 int vr;
1455 int phyw;
1456 int dvoff;
1457 int rev_offset;
1458 int bottom_margin;
1459 int dpi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460
1461 vr = GetDeviceCaps(prt_dlg.hDC, VERTRES);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 phyw = GetDeviceCaps(prt_dlg.hDC, PHYSICALHEIGHT);
1463 dvoff = GetDeviceCaps(prt_dlg.hDC, PHYSICALOFFSETY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 dpi = GetDeviceCaps(prt_dlg.hDC, LOGPIXELSY);
1465
1466 rev_offset = phyw - (dvoff + vr);
1467
1468 prt_top_margin = to_device_units(OPT_PRINT_TOP, dpi, phyw, dvoff, 5);
1469
1470 /* adjust top margin if there is a header */
1471 prt_top_margin += prt_line_height * prt_header_height();
1472
1473 bottom_margin = vr - to_device_units(OPT_PRINT_BOT, dpi, phyw,
1474 rev_offset, 5);
1475
1476 return (bottom_margin - prt_top_margin) / prt_line_height;
1477}
1478
1479 int
1480mch_print_init(prt_settings_T *psettings, char_u *jobname, int forceit)
1481{
1482 static HGLOBAL stored_dm = NULL;
1483 static HGLOBAL stored_devn = NULL;
1484 static int stored_nCopies = 1;
1485 static int stored_nFlags = 0;
1486
1487 LOGFONT fLogFont;
1488 int pifItalic;
1489 int pifBold;
1490 int pifUnderline;
1491
1492 DEVMODE *mem;
1493 DEVNAMES *devname;
1494 int i;
1495
1496 bUserAbort = &(psettings->user_abort);
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02001497 vim_memset(&prt_dlg, 0, sizeof(PRINTDLG));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498 prt_dlg.lStructSize = sizeof(PRINTDLG);
1499#ifndef FEAT_GUI
1500 GetConsoleHwnd(); /* get value of s_hwnd */
1501#endif
1502 prt_dlg.hwndOwner = s_hwnd;
1503 prt_dlg.Flags = PD_NOPAGENUMS | PD_NOSELECTION | PD_RETURNDC;
1504 if (!forceit)
1505 {
1506 prt_dlg.hDevMode = stored_dm;
1507 prt_dlg.hDevNames = stored_devn;
1508 prt_dlg.lCustData = stored_nCopies; // work around bug in print dialog
1509#ifndef FEAT_GUI
1510 /*
1511 * Use hook to prevent console window being sent to back
1512 */
1513 prt_dlg.lpfnPrintHook = PrintHookProc;
1514 prt_dlg.Flags |= PD_ENABLEPRINTHOOK;
1515#endif
1516 prt_dlg.Flags |= stored_nFlags;
1517 }
1518
1519 /*
1520 * If bang present, return default printer setup with no dialog
1521 * never show dialog if we are running over telnet
1522 */
1523 if (forceit
1524#ifndef FEAT_GUI
1525 || !term_console
1526#endif
1527 )
1528 {
1529 prt_dlg.Flags |= PD_RETURNDEFAULT;
1530#ifdef WIN3264
1531 /*
1532 * MSDN suggests setting the first parameter to WINSPOOL for
1533 * NT, but NULL appears to work just as well.
1534 */
1535 if (*p_pdev != NUL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001536 prt_dlg.hDC = CreateDC(NULL, (LPCSTR)p_pdev, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 else
1538#endif
1539 {
1540 prt_dlg.Flags |= PD_RETURNDEFAULT;
1541 if (PrintDlg(&prt_dlg) == 0)
1542 goto init_fail_dlg;
1543 }
1544 }
1545 else if (PrintDlg(&prt_dlg) == 0)
1546 goto init_fail_dlg;
1547 else
1548 {
1549 /*
1550 * keep the previous driver context
1551 */
1552 stored_dm = prt_dlg.hDevMode;
1553 stored_devn = prt_dlg.hDevNames;
1554 stored_nFlags = prt_dlg.Flags;
1555 stored_nCopies = prt_dlg.nCopies;
1556 }
1557
1558 if (prt_dlg.hDC == NULL)
1559 {
1560 EMSG(_("E237: Printer selection failed"));
1561 mch_print_cleanup();
1562 return FALSE;
1563 }
1564
1565 /* Not all printer drivers report the support of color (or grey) in the
1566 * same way. Let's set has_color if there appears to be some way to print
1567 * more than B&W. */
1568 i = GetDeviceCaps(prt_dlg.hDC, NUMCOLORS);
1569 psettings->has_color = (GetDeviceCaps(prt_dlg.hDC, BITSPIXEL) > 1
1570 || GetDeviceCaps(prt_dlg.hDC, PLANES) > 1
1571 || i > 2 || i == -1);
1572
1573 /* Ensure all font styles are baseline aligned */
1574 SetTextAlign(prt_dlg.hDC, TA_BASELINE|TA_LEFT);
1575
1576 /*
1577 * On some windows systems the nCopies parameter is not
1578 * passed back correctly. It must be retrieved from the
1579 * hDevMode struct.
1580 */
1581 mem = (DEVMODE *)GlobalLock(prt_dlg.hDevMode);
1582 if (mem != NULL)
1583 {
1584#ifdef WIN3264
1585 if (mem->dmCopies != 1)
1586 stored_nCopies = mem->dmCopies;
1587#endif
1588 if ((mem->dmFields & DM_DUPLEX) && (mem->dmDuplex & ~DMDUP_SIMPLEX))
1589 psettings->duplex = TRUE;
1590 if ((mem->dmFields & DM_COLOR) && (mem->dmColor & DMCOLOR_COLOR))
1591 psettings->has_color = TRUE;
1592 }
1593 GlobalUnlock(prt_dlg.hDevMode);
1594
1595 devname = (DEVNAMES *)GlobalLock(prt_dlg.hDevNames);
1596 if (devname != 0)
1597 {
1598 char_u *printer_name = (char_u *)devname + devname->wDeviceOffset;
1599 char_u *port_name = (char_u *)devname +devname->wOutputOffset;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001600 char_u *text = (char_u *)_("to %s on %s");
Bram Moolenaarf191d552014-10-09 17:05:56 +02001601#ifdef FEAT_MBYTE
1602 char_u *printer_name_orig = printer_name;
1603 char_u *port_name_orig = port_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604
Bram Moolenaarf191d552014-10-09 17:05:56 +02001605 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
1606 {
1607 char_u *to_free = NULL;
1608 int maxlen;
1609
Bram Moolenaar861d80a2014-10-16 20:35:36 +02001610 acp_to_enc(printer_name, (int)STRLEN(printer_name), &to_free,
1611 &maxlen);
Bram Moolenaarf191d552014-10-09 17:05:56 +02001612 if (to_free != NULL)
1613 printer_name = to_free;
Bram Moolenaar861d80a2014-10-16 20:35:36 +02001614 acp_to_enc(port_name, (int)STRLEN(port_name), &to_free, &maxlen);
Bram Moolenaarf191d552014-10-09 17:05:56 +02001615 if (to_free != NULL)
1616 port_name = to_free;
1617 }
1618#endif
Bram Moolenaare6a91fd2008-07-24 18:51:11 +00001619 prt_name = alloc((unsigned)(STRLEN(printer_name) + STRLEN(port_name)
1620 + STRLEN(text)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 if (prt_name != NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001622 wsprintf((char *)prt_name, (const char *)text,
1623 printer_name, port_name);
Bram Moolenaarf191d552014-10-09 17:05:56 +02001624#ifdef FEAT_MBYTE
1625 if (printer_name != printer_name_orig)
1626 vim_free(printer_name);
1627 if (port_name != port_name_orig)
1628 vim_free(port_name);
1629#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 }
1631 GlobalUnlock(prt_dlg.hDevNames);
1632
1633 /*
1634 * Initialise the font according to 'printfont'
1635 */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02001636 vim_memset(&fLogFont, 0, sizeof(fLogFont));
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001637 if (get_logfont(&fLogFont, p_pfn, prt_dlg.hDC, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 {
1639 EMSG2(_("E613: Unknown printer font: %s"), p_pfn);
1640 mch_print_cleanup();
1641 return FALSE;
1642 }
1643
1644 for (pifBold = 0; pifBold <= 1; pifBold++)
1645 for (pifItalic = 0; pifItalic <= 1; pifItalic++)
1646 for (pifUnderline = 0; pifUnderline <= 1; pifUnderline++)
1647 {
1648 fLogFont.lfWeight = boldface[pifBold];
1649 fLogFont.lfItalic = pifItalic;
1650 fLogFont.lfUnderline = pifUnderline;
1651 prt_font_handles[pifBold][pifItalic][pifUnderline]
1652 = CreateFontIndirect(&fLogFont);
1653 }
1654
1655 SetBkMode(prt_dlg.hDC, OPAQUE);
1656 SelectObject(prt_dlg.hDC, prt_font_handles[0][0][0]);
1657
1658 /*
1659 * Fill in the settings struct
1660 */
1661 psettings->chars_per_line = prt_get_cpl();
1662 psettings->lines_per_page = prt_get_lpp();
Bram Moolenaar7ddc6422014-09-27 11:18:19 +02001663 if (prt_dlg.Flags & PD_USEDEVMODECOPIESANDCOLLATE)
1664 {
1665 psettings->n_collated_copies = (prt_dlg.Flags & PD_COLLATE)
1666 ? prt_dlg.nCopies : 1;
1667 psettings->n_uncollated_copies = (prt_dlg.Flags & PD_COLLATE)
1668 ? 1 : prt_dlg.nCopies;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669
Bram Moolenaar7ddc6422014-09-27 11:18:19 +02001670 if (psettings->n_collated_copies == 0)
1671 psettings->n_collated_copies = 1;
1672
1673 if (psettings->n_uncollated_copies == 0)
1674 psettings->n_uncollated_copies = 1;
1675 } else {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 psettings->n_collated_copies = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 psettings->n_uncollated_copies = 1;
Bram Moolenaar7ddc6422014-09-27 11:18:19 +02001678 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679
1680 psettings->jobname = jobname;
1681
1682 return TRUE;
1683
1684init_fail_dlg:
1685 {
1686 DWORD err = CommDlgExtendedError();
1687
1688 if (err)
1689 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 char_u *buf;
1691
1692 /* I suspect FormatMessage() doesn't work for values returned by
1693 * CommDlgExtendedError(). What does? */
1694 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
1695 FORMAT_MESSAGE_FROM_SYSTEM |
1696 FORMAT_MESSAGE_IGNORE_INSERTS,
1697 NULL, err, 0, (LPTSTR)(&buf), 0, NULL);
1698 EMSG2(_("E238: Print error: %s"),
1699 buf == NULL ? (char_u *)_("Unknown") : buf);
1700 LocalFree((LPVOID)(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 }
1702 else
1703 msg_clr_eos(); /* Maybe canceled */
1704
1705 mch_print_cleanup();
1706 return FALSE;
1707 }
1708}
1709
1710
1711 int
1712mch_print_begin(prt_settings_T *psettings)
1713{
1714 int ret;
1715 static DOCINFO di;
1716 char szBuffer[300];
1717
1718 hDlgPrint = CreateDialog(GetModuleHandle(NULL), TEXT("PrintDlgBox"),
1719 prt_dlg.hwndOwner, PrintDlgProc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 SetAbortProc(prt_dlg.hDC, AbortProc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 wsprintf(szBuffer, _("Printing '%s'"), gettail(psettings->jobname));
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001722 vimSetDlgItemText(hDlgPrint, IDC_PRINTTEXT1, (char_u *)szBuffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02001724 vim_memset(&di, 0, sizeof(DOCINFO));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 di.cbSize = sizeof(DOCINFO);
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001726 di.lpszDocName = (LPCSTR)psettings->jobname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 ret = StartDoc(prt_dlg.hDC, &di);
1728
1729#ifdef FEAT_GUI
1730 /* Give focus back to main window (when using MDI). */
1731 SetFocus(s_hwnd);
1732#endif
1733
1734 return (ret > 0);
1735}
1736
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001737/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 void
1739mch_print_end(prt_settings_T *psettings)
1740{
1741 EndDoc(prt_dlg.hDC);
1742 if (!*bUserAbort)
1743 SendMessage(hDlgPrint, WM_COMMAND, 0, 0);
1744}
1745
1746 int
1747mch_print_end_page(void)
1748{
1749 return (EndPage(prt_dlg.hDC) > 0);
1750}
1751
1752 int
1753mch_print_begin_page(char_u *msg)
1754{
1755 if (msg != NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001756 vimSetDlgItemText(hDlgPrint, IDC_PROGRESS, msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 return (StartPage(prt_dlg.hDC) > 0);
1758}
1759
1760 int
1761mch_print_blank_page(void)
1762{
1763 return (mch_print_begin_page(NULL) ? (mch_print_end_page()) : FALSE);
1764}
1765
1766static int prt_pos_x = 0;
1767static int prt_pos_y = 0;
1768
1769 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001770mch_print_start_line(int margin, int page_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771{
1772 if (margin)
1773 prt_pos_x = -prt_number_width;
1774 else
1775 prt_pos_x = 0;
1776 prt_pos_y = page_line * prt_line_height
1777 + prt_tm.tmAscent + prt_tm.tmExternalLeading;
1778}
1779
1780 int
1781mch_print_text_out(char_u *p, int len)
1782{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001783#if defined(FEAT_PROPORTIONAL_FONTS) || defined(FEAT_MBYTE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 SIZE sz;
1785#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001786#if defined(FEAT_MBYTE)
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001787 WCHAR *wp = NULL;
1788 int wlen = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001790 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
1791 {
1792 wp = enc_to_utf16(p, &wlen);
1793 }
1794 if (wp != NULL)
1795 {
1796 int ret = FALSE;
1797
1798 TextOutW(prt_dlg.hDC, prt_pos_x + prt_left_margin,
1799 prt_pos_y + prt_top_margin, wp, wlen);
1800 GetTextExtentPoint32W(prt_dlg.hDC, wp, wlen, &sz);
1801 vim_free(wp);
1802 prt_pos_x += (sz.cx - prt_tm.tmOverhang);
1803 /* This is wrong when printing spaces for a TAB. */
1804 if (p[len] != NUL)
1805 {
1806 wlen = MB_PTR2LEN(p + len);
1807 wp = enc_to_utf16(p + len, &wlen);
1808 if (wp != NULL)
1809 {
1810 GetTextExtentPoint32W(prt_dlg.hDC, wp, 1, &sz);
1811 ret = (prt_pos_x + prt_left_margin + sz.cx > prt_right_margin);
1812 vim_free(wp);
1813 }
1814 }
1815 return ret;
1816 }
1817#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 TextOut(prt_dlg.hDC, prt_pos_x + prt_left_margin,
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001819 prt_pos_y + prt_top_margin,
1820 (LPCSTR)p, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821#ifndef FEAT_PROPORTIONAL_FONTS
1822 prt_pos_x += len * prt_tm.tmAveCharWidth;
1823 return (prt_pos_x + prt_left_margin + prt_tm.tmAveCharWidth
1824 + prt_tm.tmOverhang > prt_right_margin);
1825#else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001826 GetTextExtentPoint32(prt_dlg.hDC, (LPCSTR)p, len, &sz);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 prt_pos_x += (sz.cx - prt_tm.tmOverhang);
1828 /* This is wrong when printing spaces for a TAB. */
1829 if (p[len] == NUL)
1830 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 GetTextExtentPoint32(prt_dlg.hDC, p + len, 1, &sz);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 return (prt_pos_x + prt_left_margin + sz.cx > prt_right_margin);
1833#endif
1834}
1835
1836 void
1837mch_print_set_font(int iBold, int iItalic, int iUnderline)
1838{
1839 SelectObject(prt_dlg.hDC, prt_font_handles[iBold][iItalic][iUnderline]);
1840}
1841
1842 void
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001843mch_print_set_bg(long_u bgcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844{
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001845 SetBkColor(prt_dlg.hDC, GetNearestColor(prt_dlg.hDC,
1846 swap_me((COLORREF)bgcol)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 /*
1848 * With a white background we can draw characters transparent, which is
1849 * good for italic characters that overlap to the next char cell.
1850 */
1851 if (bgcol == 0xffffffUL)
1852 SetBkMode(prt_dlg.hDC, TRANSPARENT);
1853 else
1854 SetBkMode(prt_dlg.hDC, OPAQUE);
1855}
1856
1857 void
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001858mch_print_set_fg(long_u fgcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859{
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001860 SetTextColor(prt_dlg.hDC, GetNearestColor(prt_dlg.hDC,
1861 swap_me((COLORREF)fgcol)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862}
1863
1864#endif /*FEAT_PRINTER && !FEAT_POSTSCRIPT*/
1865
Bram Moolenaar58d98232005-07-23 22:25:46 +00001866
1867
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868#if defined(FEAT_SHORTCUT) || defined(PROTO)
Bram Moolenaar82881492012-11-20 16:53:39 +01001869# ifndef PROTO
1870# include <shlobj.h>
1871# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872
1873/*
1874 * When "fname" is the name of a shortcut (*.lnk) resolve the file it points
1875 * to and return that name in allocated memory.
1876 * Otherwise NULL is returned.
1877 */
1878 char_u *
1879mch_resolve_shortcut(char_u *fname)
1880{
1881 HRESULT hr;
1882 IShellLink *psl = NULL;
1883 IPersistFile *ppf = NULL;
1884 OLECHAR wsz[MAX_PATH];
1885 WIN32_FIND_DATA ffd; // we get those free of charge
Bram Moolenaar604729e2013-08-30 16:44:19 +02001886 CHAR buf[MAX_PATH]; // could have simply reused 'wsz'...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 char_u *rfname = NULL;
1888 int len;
Bram Moolenaar604729e2013-08-30 16:44:19 +02001889# ifdef FEAT_MBYTE
1890 IShellLinkW *pslw = NULL;
1891 WIN32_FIND_DATAW ffdw; // we get those free of charge
1892# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893
1894 /* Check if the file name ends in ".lnk". Avoid calling
1895 * CoCreateInstance(), it's quite slow. */
1896 if (fname == NULL)
1897 return rfname;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001898 len = (int)STRLEN(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 if (len <= 4 || STRNICMP(fname + len - 4, ".lnk", 4) != 0)
1900 return rfname;
1901
1902 CoInitialize(NULL);
1903
Bram Moolenaar604729e2013-08-30 16:44:19 +02001904# ifdef FEAT_MBYTE
1905 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
1906 {
1907 // create a link manager object and request its interface
1908 hr = CoCreateInstance(
1909 &CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
1910 &IID_IShellLinkW, (void**)&pslw);
1911 if (hr == S_OK)
1912 {
1913 WCHAR *p = enc_to_utf16(fname, NULL);
1914
1915 if (p != NULL)
1916 {
1917 // Get a pointer to the IPersistFile interface.
1918 hr = pslw->lpVtbl->QueryInterface(
1919 pslw, &IID_IPersistFile, (void**)&ppf);
1920 if (hr != S_OK)
1921 goto shortcut_errorw;
1922
1923 // "load" the name and resolve the link
1924 hr = ppf->lpVtbl->Load(ppf, p, STGM_READ);
1925 if (hr != S_OK)
1926 goto shortcut_errorw;
1927# if 0 // This makes Vim wait a long time if the target does not exist.
1928 hr = pslw->lpVtbl->Resolve(pslw, NULL, SLR_NO_UI);
1929 if (hr != S_OK)
1930 goto shortcut_errorw;
1931# endif
1932
1933 // Get the path to the link target.
1934 ZeroMemory(wsz, MAX_PATH * sizeof(WCHAR));
1935 hr = pslw->lpVtbl->GetPath(pslw, wsz, MAX_PATH, &ffdw, 0);
1936 if (hr == S_OK && wsz[0] != NUL)
1937 rfname = utf16_to_enc(wsz, NULL);
1938
1939shortcut_errorw:
1940 vim_free(p);
Bram Moolenaarcd981f22014-02-11 17:06:00 +01001941 goto shortcut_end;
Bram Moolenaar604729e2013-08-30 16:44:19 +02001942 }
1943 }
1944 /* Retry with non-wide function (for Windows 98). */
1945 }
1946# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 // create a link manager object and request its interface
1948 hr = CoCreateInstance(
1949 &CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
1950 &IID_IShellLink, (void**)&psl);
1951 if (hr != S_OK)
Bram Moolenaar604729e2013-08-30 16:44:19 +02001952 goto shortcut_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953
1954 // Get a pointer to the IPersistFile interface.
1955 hr = psl->lpVtbl->QueryInterface(
1956 psl, &IID_IPersistFile, (void**)&ppf);
1957 if (hr != S_OK)
Bram Moolenaar604729e2013-08-30 16:44:19 +02001958 goto shortcut_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959
1960 // full path string must be in Unicode.
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001961 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)fname, -1, wsz, MAX_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00001963 // "load" the name and resolve the link
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 hr = ppf->lpVtbl->Load(ppf, wsz, STGM_READ);
1965 if (hr != S_OK)
Bram Moolenaar604729e2013-08-30 16:44:19 +02001966 goto shortcut_end;
1967# if 0 // This makes Vim wait a long time if the target doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 hr = psl->lpVtbl->Resolve(psl, NULL, SLR_NO_UI);
1969 if (hr != S_OK)
Bram Moolenaar604729e2013-08-30 16:44:19 +02001970 goto shortcut_end;
1971# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972
1973 // Get the path to the link target.
1974 ZeroMemory(buf, MAX_PATH);
1975 hr = psl->lpVtbl->GetPath(psl, buf, MAX_PATH, &ffd, 0);
1976 if (hr == S_OK && buf[0] != NUL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001977 rfname = vim_strsave((char_u *)buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978
Bram Moolenaar604729e2013-08-30 16:44:19 +02001979shortcut_end:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 // Release all interface pointers (both belong to the same object)
1981 if (ppf != NULL)
1982 ppf->lpVtbl->Release(ppf);
1983 if (psl != NULL)
1984 psl->lpVtbl->Release(psl);
Bram Moolenaar604729e2013-08-30 16:44:19 +02001985# ifdef FEAT_MBYTE
1986 if (pslw != NULL)
1987 pslw->lpVtbl->Release(pslw);
1988# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989
1990 CoUninitialize();
1991 return rfname;
1992}
1993#endif
1994
1995#if (defined(FEAT_EVAL) && !defined(FEAT_GUI)) || defined(PROTO)
1996/*
1997 * Bring ourselves to the foreground. Does work if the OS doesn't allow it.
1998 */
1999 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002000win32_set_foreground(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001{
2002# ifndef FEAT_GUI
2003 GetConsoleHwnd(); /* get value of s_hwnd */
2004# endif
2005 if (s_hwnd != 0)
2006 SetForegroundWindow(s_hwnd);
2007}
2008#endif
2009
2010#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
2011/*
2012 * Client-server code for Vim
2013 *
2014 * Originally written by Paul Moore
2015 */
2016
2017/* In order to handle inter-process messages, we need to have a window. But
2018 * the functions in this module can be called before the main GUI window is
2019 * created (and may also be called in the console version, where there is no
2020 * GUI window at all).
2021 *
2022 * So we create a hidden window, and arrange to destroy it on exit.
2023 */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002024HWND message_window = 0; /* window that's handling messages */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025
2026#define VIM_CLASSNAME "VIM_MESSAGES"
2027#define VIM_CLASSNAME_LEN (sizeof(VIM_CLASSNAME) - 1)
2028
2029/* Communication is via WM_COPYDATA messages. The message type is send in
2030 * the dwData parameter. Types are defined here. */
2031#define COPYDATA_KEYS 0
2032#define COPYDATA_REPLY 1
2033#define COPYDATA_EXPR 10
2034#define COPYDATA_RESULT 11
2035#define COPYDATA_ERROR_RESULT 12
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002036#define COPYDATA_ENCODING 20
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037
2038/* This is a structure containing a server HWND and its name. */
2039struct server_id
2040{
2041 HWND hwnd;
2042 char_u *name;
2043};
2044
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002045/* Last received 'encoding' that the client uses. */
2046static char_u *client_enc = NULL;
2047
2048/*
2049 * Tell the other side what encoding we are using.
2050 * Errors are ignored.
2051 */
2052 static void
2053serverSendEnc(HWND target)
2054{
2055 COPYDATASTRUCT data;
2056
2057 data.dwData = COPYDATA_ENCODING;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002058#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002059 data.cbData = (DWORD)STRLEN(p_enc) + 1;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002060 data.lpData = p_enc;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002061#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +02002062 data.cbData = (DWORD)STRLEN("latin1") + 1;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002063 data.lpData = "latin1";
2064#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002065 (void)SendMessage(target, WM_COPYDATA, (WPARAM)message_window,
2066 (LPARAM)(&data));
2067}
2068
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069/*
2070 * Clean up on exit. This destroys the hidden message window.
2071 */
2072 static void
2073#ifdef __BORLANDC__
2074 _RTLENTRYF
2075#endif
2076CleanUpMessaging(void)
2077{
2078 if (message_window != 0)
2079 {
2080 DestroyWindow(message_window);
2081 message_window = 0;
2082 }
2083}
2084
2085static int save_reply(HWND server, char_u *reply, int expr);
2086
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002087/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 * The window procedure for the hidden message window.
2089 * It handles callback messages and notifications from servers.
2090 * In order to process these messages, it is necessary to run a
2091 * message loop. Code which may run before the main message loop
2092 * is started (in the GUI) is careful to pump messages when it needs
2093 * to. Features which require message delivery during normal use will
2094 * not work in the console version - this basically means those
2095 * features which allow Vim to act as a server, rather than a client.
2096 */
2097 static LRESULT CALLBACK
2098Messaging_WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
2099{
2100 if (msg == WM_COPYDATA)
2101 {
2102 /* This is a message from another Vim. The dwData member of the
2103 * COPYDATASTRUCT determines the type of message:
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002104 * COPYDATA_ENCODING:
2105 * The encoding that the client uses. Following messages will
2106 * use this encoding, convert if needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 * COPYDATA_KEYS:
2108 * A key sequence. We are a server, and a client wants these keys
2109 * adding to the input queue.
2110 * COPYDATA_REPLY:
2111 * A reply. We are a client, and a server has sent this message
2112 * in response to a request. (server2client())
2113 * COPYDATA_EXPR:
2114 * An expression. We are a server, and a client wants us to
2115 * evaluate this expression.
2116 * COPYDATA_RESULT:
2117 * A reply. We are a client, and a server has sent this message
2118 * in response to a COPYDATA_EXPR.
2119 * COPYDATA_ERROR_RESULT:
2120 * A reply. We are a client, and a server has sent this message
2121 * in response to a COPYDATA_EXPR that failed to evaluate.
2122 */
2123 COPYDATASTRUCT *data = (COPYDATASTRUCT*)lParam;
2124 HWND sender = (HWND)wParam;
2125 COPYDATASTRUCT reply;
2126 char_u *res;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127 int retval;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002128 char_u *str;
2129 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130
2131 switch (data->dwData)
2132 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002133 case COPYDATA_ENCODING:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002134# ifdef FEAT_MBYTE
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002135 /* Remember the encoding that the client uses. */
2136 vim_free(client_enc);
2137 client_enc = enc_canonize((char_u *)data->lpData);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002138# endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002139 return 1;
2140
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 case COPYDATA_KEYS:
2142 /* Remember who sent this, for <client> */
2143 clientWindow = sender;
2144
2145 /* Add the received keys to the input buffer. The loop waiting
2146 * for the user to do something should check the input buffer. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002147 str = serverConvert(client_enc, (char_u *)data->lpData, &tofree);
2148 server_to_input_buf(str);
2149 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150
2151# ifdef FEAT_GUI
2152 /* Wake up the main GUI loop. */
2153 if (s_hwnd != 0)
2154 PostMessage(s_hwnd, WM_NULL, 0, 0);
2155# endif
2156 return 1;
2157
2158 case COPYDATA_EXPR:
2159 /* Remember who sent this, for <client> */
2160 clientWindow = sender;
2161
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002162 str = serverConvert(client_enc, (char_u *)data->lpData, &tofree);
2163 res = eval_client_expr_to_string(str);
2164 vim_free(tofree);
2165
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 if (res == NULL)
2167 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002168 res = vim_strsave((char_u *)_(e_invexprmsg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 reply.dwData = COPYDATA_ERROR_RESULT;
2170 }
2171 else
2172 reply.dwData = COPYDATA_RESULT;
2173 reply.lpData = res;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002174 reply.cbData = (DWORD)STRLEN(res) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002176 serverSendEnc(sender);
Bram Moolenaar5246cd72013-06-16 16:41:47 +02002177 retval = (int)SendMessage(sender, WM_COPYDATA,
2178 (WPARAM)message_window, (LPARAM)(&reply));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 vim_free(res);
2180 return retval;
2181
2182 case COPYDATA_REPLY:
2183 case COPYDATA_RESULT:
2184 case COPYDATA_ERROR_RESULT:
2185 if (data->lpData != NULL)
2186 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002187 str = serverConvert(client_enc, (char_u *)data->lpData,
2188 &tofree);
2189 if (tofree == NULL)
2190 str = vim_strsave(str);
2191 if (save_reply(sender, str,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 (data->dwData == COPYDATA_REPLY ? 0 :
2193 (data->dwData == COPYDATA_RESULT ? 1 :
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002194 2))) == FAIL)
2195 vim_free(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196#ifdef FEAT_AUTOCMD
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002197 else if (data->dwData == COPYDATA_REPLY)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 {
Bram Moolenaar427d51c2013-06-16 16:01:25 +02002199 char_u winstr[30];
2200
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002201 sprintf((char *)winstr, PRINTF_HEX_LONG_U, (long_u)sender);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002202 apply_autocmds(EVENT_REMOTEREPLY, winstr, str,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 TRUE, curbuf);
2204 }
2205#endif
2206 }
2207 return 1;
2208 }
2209
2210 return 0;
2211 }
2212
2213 else if (msg == WM_ACTIVATE && wParam == WA_ACTIVE)
2214 {
2215 /* When the message window is activated (brought to the foreground),
2216 * this actually applies to the text window. */
2217#ifndef FEAT_GUI
2218 GetConsoleHwnd(); /* get value of s_hwnd */
2219#endif
2220 if (s_hwnd != 0)
2221 {
2222 SetForegroundWindow(s_hwnd);
2223 return 0;
2224 }
2225 }
2226
2227 return DefWindowProc(hwnd, msg, wParam, lParam);
2228}
2229
2230/*
2231 * Initialise the message handling process. This involves creating a window
2232 * to handle messages - the window will not be visible.
2233 */
2234 void
2235serverInitMessaging(void)
2236{
2237 WNDCLASS wndclass;
2238 HINSTANCE s_hinst;
2239
2240 /* Clean up on exit */
2241 atexit(CleanUpMessaging);
2242
2243 /* Register a window class - we only really care
2244 * about the window procedure
2245 */
2246 s_hinst = (HINSTANCE)GetModuleHandle(0);
2247 wndclass.style = 0;
2248 wndclass.lpfnWndProc = Messaging_WndProc;
2249 wndclass.cbClsExtra = 0;
2250 wndclass.cbWndExtra = 0;
2251 wndclass.hInstance = s_hinst;
2252 wndclass.hIcon = NULL;
2253 wndclass.hCursor = NULL;
2254 wndclass.hbrBackground = NULL;
2255 wndclass.lpszMenuName = NULL;
2256 wndclass.lpszClassName = VIM_CLASSNAME;
2257 RegisterClass(&wndclass);
2258
2259 /* Create the message window. It will be hidden, so the details don't
2260 * matter. Don't use WS_OVERLAPPEDWINDOW, it will make a shortcut remove
2261 * focus from gvim. */
2262 message_window = CreateWindow(VIM_CLASSNAME, "",
2263 WS_POPUPWINDOW | WS_CAPTION,
2264 CW_USEDEFAULT, CW_USEDEFAULT,
2265 100, 100, NULL, NULL,
2266 s_hinst, NULL);
2267}
2268
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002269/* Used by serverSendToVim() to find an alternate server name. */
2270static char_u *altname_buf_ptr = NULL;
2271
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272/*
2273 * Get the title of the window "hwnd", which is the Vim server name, in
2274 * "name[namelen]" and return the length.
2275 * Returns zero if window "hwnd" is not a Vim server.
2276 */
2277 static int
2278getVimServerName(HWND hwnd, char *name, int namelen)
2279{
2280 int len;
2281 char buffer[VIM_CLASSNAME_LEN + 1];
2282
2283 /* Ignore windows which aren't Vim message windows */
2284 len = GetClassName(hwnd, buffer, sizeof(buffer));
2285 if (len != VIM_CLASSNAME_LEN || STRCMP(buffer, VIM_CLASSNAME) != 0)
2286 return 0;
2287
2288 /* Get the title of the window */
2289 return GetWindowText(hwnd, name, namelen);
2290}
2291
2292 static BOOL CALLBACK
2293enumWindowsGetServer(HWND hwnd, LPARAM lparam)
2294{
2295 struct server_id *id = (struct server_id *)lparam;
2296 char server[MAX_PATH];
2297
2298 /* Get the title of the window */
2299 if (getVimServerName(hwnd, server, sizeof(server)) == 0)
2300 return TRUE;
2301
2302 /* If this is the server we're looking for, return its HWND */
2303 if (STRICMP(server, id->name) == 0)
2304 {
2305 id->hwnd = hwnd;
2306 return FALSE;
2307 }
2308
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002309 /* If we are looking for an alternate server, remember this name. */
2310 if (altname_buf_ptr != NULL
2311 && STRNICMP(server, id->name, STRLEN(id->name)) == 0
2312 && vim_isdigit(server[STRLEN(id->name)]))
2313 {
2314 STRCPY(altname_buf_ptr, server);
2315 altname_buf_ptr = NULL; /* don't use another name */
2316 }
2317
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 /* Otherwise, keep looking */
2319 return TRUE;
2320}
2321
2322 static BOOL CALLBACK
2323enumWindowsGetNames(HWND hwnd, LPARAM lparam)
2324{
2325 garray_T *ga = (garray_T *)lparam;
2326 char server[MAX_PATH];
2327
2328 /* Get the title of the window */
2329 if (getVimServerName(hwnd, server, sizeof(server)) == 0)
2330 return TRUE;
2331
2332 /* Add the name to the list */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002333 ga_concat(ga, (char_u *)server);
2334 ga_concat(ga, (char_u *)"\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 return TRUE;
2336}
2337
2338 static HWND
2339findServer(char_u *name)
2340{
2341 struct server_id id;
2342
2343 id.name = name;
2344 id.hwnd = 0;
2345
2346 EnumWindows(enumWindowsGetServer, (LPARAM)(&id));
2347
2348 return id.hwnd;
2349}
2350
2351 void
2352serverSetName(char_u *name)
2353{
2354 char_u *ok_name;
2355 HWND hwnd = 0;
2356 int i = 0;
2357 char_u *p;
2358
2359 /* Leave enough space for a 9-digit suffix to ensure uniqueness! */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002360 ok_name = alloc((unsigned)STRLEN(name) + 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361
2362 STRCPY(ok_name, name);
2363 p = ok_name + STRLEN(name);
2364
2365 for (;;)
2366 {
2367 /* This is inefficient - we're doing an EnumWindows loop for each
2368 * possible name. It would be better to grab all names in one go,
2369 * and scan the list each time...
2370 */
2371 hwnd = findServer(ok_name);
2372 if (hwnd == 0)
2373 break;
2374
2375 ++i;
2376 if (i >= 1000)
2377 break;
2378
2379 sprintf((char *)p, "%d", i);
2380 }
2381
2382 if (hwnd != 0)
2383 vim_free(ok_name);
2384 else
2385 {
2386 /* Remember the name */
2387 serverName = ok_name;
2388#ifdef FEAT_TITLE
2389 need_maketitle = TRUE; /* update Vim window title later */
2390#endif
2391
2392 /* Update the message window title */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002393 SetWindowText(message_window, (LPCSTR)ok_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394
2395#ifdef FEAT_EVAL
2396 /* Set the servername variable */
2397 set_vim_var_string(VV_SEND_SERVER, serverName, -1);
2398#endif
2399 }
2400}
2401
2402 char_u *
2403serverGetVimNames(void)
2404{
2405 garray_T ga;
2406
2407 ga_init2(&ga, 1, 100);
2408
2409 EnumWindows(enumWindowsGetNames, (LPARAM)(&ga));
Bram Moolenaar269ec652004-07-29 08:43:53 +00002410 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411
2412 return ga.ga_data;
2413}
2414
2415 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002416serverSendReply(
2417 char_u *name, /* Where to send. */
2418 char_u *reply) /* What to send. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419{
2420 HWND target;
2421 COPYDATASTRUCT data;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002422 long_u n = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423
2424 /* The "name" argument is a magic cookie obtained from expand("<client>").
2425 * It should be of the form 0xXXXXX - i.e. a C hex literal, which is the
2426 * value of the client's message window HWND.
2427 */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002428 sscanf((char *)name, SCANF_HEX_LONG_U, &n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 if (n == 0)
2430 return -1;
2431
2432 target = (HWND)n;
2433 if (!IsWindow(target))
2434 return -1;
2435
2436 data.dwData = COPYDATA_REPLY;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002437 data.cbData = (DWORD)STRLEN(reply) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 data.lpData = reply;
2439
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002440 serverSendEnc(target);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 if (SendMessage(target, WM_COPYDATA, (WPARAM)message_window,
2442 (LPARAM)(&data)))
2443 return 0;
2444
2445 return -1;
2446}
2447
2448 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002449serverSendToVim(
2450 char_u *name, /* Where to send. */
2451 char_u *cmd, /* What to send. */
2452 char_u **result, /* Result of eval'ed expression */
2453 void *ptarget, /* HWND of server */
2454 int asExpr, /* Expression or keys? */
2455 int silent) /* don't complain about no server */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456{
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002457 HWND target;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 COPYDATASTRUCT data;
2459 char_u *retval = NULL;
2460 int retcode = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002461 char_u altname_buf[MAX_PATH];
2462
2463 /* If the server name does not end in a digit then we look for an
2464 * alternate name. e.g. when "name" is GVIM the we may find GVIM2. */
2465 if (STRLEN(name) > 1 && !vim_isdigit(name[STRLEN(name) - 1]))
2466 altname_buf_ptr = altname_buf;
2467 altname_buf[0] = NUL;
2468 target = findServer(name);
2469 altname_buf_ptr = NULL;
2470 if (target == 0 && altname_buf[0] != NUL)
2471 /* Use another server name we found. */
2472 target = findServer(altname_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473
2474 if (target == 0)
2475 {
2476 if (!silent)
2477 EMSG2(_(e_noserver), name);
2478 return -1;
2479 }
2480
2481 if (ptarget)
2482 *(HWND *)ptarget = target;
2483
2484 data.dwData = asExpr ? COPYDATA_EXPR : COPYDATA_KEYS;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002485 data.cbData = (DWORD)STRLEN(cmd) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 data.lpData = cmd;
2487
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002488 serverSendEnc(target);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 if (SendMessage(target, WM_COPYDATA, (WPARAM)message_window,
2490 (LPARAM)(&data)) == 0)
2491 return -1;
2492
2493 if (asExpr)
2494 retval = serverGetReply(target, &retcode, TRUE, TRUE);
2495
2496 if (result == NULL)
2497 vim_free(retval);
2498 else
2499 *result = retval; /* Caller assumes responsibility for freeing */
2500
2501 return retcode;
2502}
2503
2504/*
2505 * Bring the server to the foreground.
2506 */
2507 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002508serverForeground(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509{
2510 HWND target = findServer(name);
2511
2512 if (target != 0)
2513 SetForegroundWindow(target);
2514}
2515
2516/* Replies from server need to be stored until the client picks them up via
2517 * remote_read(). So we maintain a list of server-id/reply pairs.
2518 * Note that there could be multiple replies from one server pending if the
2519 * client is slow picking them up.
2520 * We just store the replies in a simple list. When we remove an entry, we
2521 * move list entries down to fill the gap.
2522 * The server ID is simply the HWND.
2523 */
2524typedef struct
2525{
2526 HWND server; /* server window */
2527 char_u *reply; /* reply string */
2528 int expr_result; /* 0 for REPLY, 1 for RESULT 2 for error */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002529} reply_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530
2531static garray_T reply_list = {0, 0, sizeof(reply_T), 5, 0};
2532
2533#define REPLY_ITEM(i) ((reply_T *)(reply_list.ga_data) + (i))
2534#define REPLY_COUNT (reply_list.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535
2536/* Flag which is used to wait for a reply */
2537static int reply_received = 0;
2538
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002539/*
2540 * Store a reply. "reply" must be allocated memory (or NULL).
2541 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 static int
2543save_reply(HWND server, char_u *reply, int expr)
2544{
2545 reply_T *rep;
2546
2547 if (ga_grow(&reply_list, 1) == FAIL)
2548 return FAIL;
2549
2550 rep = REPLY_ITEM(REPLY_COUNT);
2551 rep->server = server;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002552 rep->reply = reply;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 rep->expr_result = expr;
2554 if (rep->reply == NULL)
2555 return FAIL;
2556
2557 ++REPLY_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 reply_received = 1;
2559 return OK;
2560}
2561
2562/*
2563 * Get a reply from server "server".
2564 * When "expr_res" is non NULL, get the result of an expression, otherwise a
2565 * server2client() message.
2566 * When non NULL, point to return code. 0 => OK, -1 => ERROR
2567 * If "remove" is TRUE, consume the message, the caller must free it then.
2568 * if "wait" is TRUE block until a message arrives (or the server exits).
2569 */
2570 char_u *
2571serverGetReply(HWND server, int *expr_res, int remove, int wait)
2572{
2573 int i;
2574 char_u *reply;
2575 reply_T *rep;
2576
2577 /* When waiting, loop until the message waiting for is received. */
2578 for (;;)
2579 {
2580 /* Reset this here, in case a message arrives while we are going
2581 * through the already received messages. */
2582 reply_received = 0;
2583
2584 for (i = 0; i < REPLY_COUNT; ++i)
2585 {
2586 rep = REPLY_ITEM(i);
2587 if (rep->server == server
2588 && ((rep->expr_result != 0) == (expr_res != NULL)))
2589 {
2590 /* Save the values we've found for later */
2591 reply = rep->reply;
2592 if (expr_res != NULL)
2593 *expr_res = rep->expr_result == 1 ? 0 : -1;
2594
2595 if (remove)
2596 {
2597 /* Move the rest of the list down to fill the gap */
2598 mch_memmove(rep, rep + 1,
2599 (REPLY_COUNT - i - 1) * sizeof(reply_T));
2600 --REPLY_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 }
2602
2603 /* Return the reply to the caller, who takes on responsibility
2604 * for freeing it if "remove" is TRUE. */
2605 return reply;
2606 }
2607 }
2608
2609 /* If we got here, we didn't find a reply. Return immediately if the
2610 * "wait" parameter isn't set. */
2611 if (!wait)
2612 break;
2613
2614 /* We need to wait for a reply. Enter a message loop until the
2615 * "reply_received" flag gets set. */
2616
2617 /* Loop until we receive a reply */
2618 while (reply_received == 0)
2619 {
2620 /* Wait for a SendMessage() call to us. This could be the reply
2621 * we are waiting for. Use a timeout of a second, to catch the
2622 * situation that the server died unexpectedly. */
2623 MsgWaitForMultipleObjects(0, NULL, TRUE, 1000, QS_ALLINPUT);
2624
2625 /* If the server has died, give up */
2626 if (!IsWindow(server))
2627 return NULL;
2628
2629 serverProcessPendingMessages();
2630 }
2631 }
2632
2633 return NULL;
2634}
2635
2636/*
2637 * Process any messages in the Windows message queue.
2638 */
2639 void
2640serverProcessPendingMessages(void)
2641{
2642 MSG msg;
2643
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02002644 while (pPeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 {
2646 TranslateMessage(&msg);
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02002647 pDispatchMessage(&msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 }
2649}
2650
2651#endif /* FEAT_CLIENTSERVER */
2652
2653#if defined(FEAT_GUI) || (defined(FEAT_PRINTER) && !defined(FEAT_POSTSCRIPT)) \
2654 || defined(PROTO)
2655
2656struct charset_pair
2657{
2658 char *name;
2659 BYTE charset;
2660};
2661
2662static struct charset_pair
2663charset_pairs[] =
2664{
2665 {"ANSI", ANSI_CHARSET},
2666 {"CHINESEBIG5", CHINESEBIG5_CHARSET},
2667 {"DEFAULT", DEFAULT_CHARSET},
2668 {"HANGEUL", HANGEUL_CHARSET},
2669 {"OEM", OEM_CHARSET},
2670 {"SHIFTJIS", SHIFTJIS_CHARSET},
2671 {"SYMBOL", SYMBOL_CHARSET},
2672#ifdef WIN3264
2673 {"ARABIC", ARABIC_CHARSET},
2674 {"BALTIC", BALTIC_CHARSET},
2675 {"EASTEUROPE", EASTEUROPE_CHARSET},
2676 {"GB2312", GB2312_CHARSET},
2677 {"GREEK", GREEK_CHARSET},
2678 {"HEBREW", HEBREW_CHARSET},
2679 {"JOHAB", JOHAB_CHARSET},
2680 {"MAC", MAC_CHARSET},
2681 {"RUSSIAN", RUSSIAN_CHARSET},
2682 {"THAI", THAI_CHARSET},
2683 {"TURKISH", TURKISH_CHARSET},
2684# if (!defined(_MSC_VER) || (_MSC_VER > 1010)) \
2685 && (!defined(__BORLANDC__) || (__BORLANDC__ > 0x0500))
2686 {"VIETNAMESE", VIETNAMESE_CHARSET},
2687# endif
2688#endif
2689 {NULL, 0}
2690};
2691
2692/*
2693 * Convert a charset ID to a name.
2694 * Return NULL when not recognized.
2695 */
2696 char *
2697charset_id2name(int id)
2698{
2699 struct charset_pair *cp;
2700
2701 for (cp = charset_pairs; cp->name != NULL; ++cp)
2702 if ((BYTE)id == cp->charset)
2703 break;
2704 return cp->name;
2705}
2706
2707static const LOGFONT s_lfDefault =
2708{
2709 -12, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, DEFAULT_CHARSET,
2710 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
2711 PROOF_QUALITY, FIXED_PITCH | FF_DONTCARE,
2712 "Fixedsys" /* see _ReadVimIni */
2713};
2714
2715/* Initialise the "current height" to -12 (same as s_lfDefault) just
2716 * in case the user specifies a font in "guifont" with no size before a font
2717 * with an explicit size has been set. This defaults the size to this value
2718 * (-12 equates to roughly 9pt).
2719 */
2720int current_font_height = -12; /* also used in gui_w48.c */
2721
2722/* Convert a string representing a point size into pixels. The string should
2723 * be a positive decimal number, with an optional decimal point (eg, "12", or
2724 * "10.5"). The pixel value is returned, and a pointer to the next unconverted
2725 * character is stored in *end. The flag "vertical" says whether this
2726 * calculation is for a vertical (height) size or a horizontal (width) one.
2727 */
2728 static int
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002729points_to_pixels(char_u *str, char_u **end, int vertical, long_i pprinter_dc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730{
2731 int pixels;
2732 int points = 0;
2733 int divisor = 0;
2734 HWND hwnd = (HWND)0;
2735 HDC hdc;
2736 HDC printer_dc = (HDC)pprinter_dc;
2737
2738 while (*str != NUL)
2739 {
2740 if (*str == '.' && divisor == 0)
2741 {
2742 /* Start keeping a divisor, for later */
2743 divisor = 1;
2744 }
2745 else
2746 {
2747 if (!VIM_ISDIGIT(*str))
2748 break;
2749
2750 points *= 10;
2751 points += *str - '0';
2752 divisor *= 10;
2753 }
2754 ++str;
2755 }
2756
2757 if (divisor == 0)
2758 divisor = 1;
2759
2760 if (printer_dc == NULL)
2761 {
2762 hwnd = GetDesktopWindow();
2763 hdc = GetWindowDC(hwnd);
2764 }
2765 else
2766 hdc = printer_dc;
2767
2768 pixels = MulDiv(points,
2769 GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX),
2770 72 * divisor);
2771
2772 if (printer_dc == NULL)
2773 ReleaseDC(hwnd, hdc);
2774
2775 *end = str;
2776 return pixels;
2777}
2778
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002779/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 static int CALLBACK
2781font_enumproc(
2782 ENUMLOGFONT *elf,
2783 NEWTEXTMETRIC *ntm,
2784 int type,
2785 LPARAM lparam)
2786{
2787 /* Return value:
2788 * 0 = terminate now (monospace & ANSI)
2789 * 1 = continue, still no luck...
2790 * 2 = continue, but we have an acceptable LOGFONT
2791 * (monospace, not ANSI)
2792 * We use these values, as EnumFontFamilies returns 1 if the
2793 * callback function is never called. So, we check the return as
2794 * 0 = perfect, 2 = OK, 1 = no good...
2795 * It's not pretty, but it works!
2796 */
2797
2798 LOGFONT *lf = (LOGFONT *)(lparam);
2799
2800#ifndef FEAT_PROPORTIONAL_FONTS
2801 /* Ignore non-monospace fonts without further ado */
2802 if ((ntm->tmPitchAndFamily & 1) != 0)
2803 return 1;
2804#endif
2805
2806 /* Remember this LOGFONT as a "possible" */
2807 *lf = elf->elfLogFont;
2808
2809 /* Terminate the scan as soon as we find an ANSI font */
2810 if (lf->lfCharSet == ANSI_CHARSET
2811 || lf->lfCharSet == OEM_CHARSET
2812 || lf->lfCharSet == DEFAULT_CHARSET)
2813 return 0;
2814
2815 /* Continue the scan - we have a non-ANSI font */
2816 return 2;
2817}
2818
2819 static int
2820init_logfont(LOGFONT *lf)
2821{
2822 int n;
2823 HWND hwnd = GetDesktopWindow();
2824 HDC hdc = GetWindowDC(hwnd);
2825
2826 n = EnumFontFamilies(hdc,
2827 (LPCSTR)lf->lfFaceName,
2828 (FONTENUMPROC)font_enumproc,
2829 (LPARAM)lf);
2830
2831 ReleaseDC(hwnd, hdc);
2832
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002833 /* If we couldn't find a usable font, return failure */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 if (n == 1)
2835 return FAIL;
2836
2837 /* Tidy up the rest of the LOGFONT structure. We set to a basic
2838 * font - get_logfont() sets bold, italic, etc based on the user's
2839 * input.
2840 */
2841 lf->lfHeight = current_font_height;
2842 lf->lfWidth = 0;
2843 lf->lfItalic = FALSE;
2844 lf->lfUnderline = FALSE;
2845 lf->lfStrikeOut = FALSE;
2846 lf->lfWeight = FW_NORMAL;
2847
2848 /* Return success */
2849 return OK;
2850}
2851
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002852/*
2853 * Get font info from "name" into logfont "lf".
2854 * Return OK for a valid name, FAIL otherwise.
2855 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 int
2857get_logfont(
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002858 LOGFONT *lf,
2859 char_u *name,
2860 HDC printer_dc,
2861 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862{
2863 char_u *p;
2864 int i;
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002865 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 static LOGFONT *lastlf = NULL;
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002867#ifdef FEAT_MBYTE
2868 char_u *acpname = NULL;
2869#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870
2871 *lf = s_lfDefault;
2872 if (name == NULL)
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002873 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002875#ifdef FEAT_MBYTE
2876 /* Convert 'name' from 'encoding' to the current codepage, because
2877 * lf->lfFaceName uses the current codepage.
2878 * TODO: Use Wide APIs instead of ANSI APIs. */
2879 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
2880 {
2881 int len;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01002882 enc_to_acp(name, (int)STRLEN(name), &acpname, &len);
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002883 name = acpname;
2884 }
2885#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 if (STRCMP(name, "*") == 0)
2887 {
2888#if defined(FEAT_GUI_W32)
2889 CHOOSEFONT cf;
2890 /* if name is "*", bring up std font dialog: */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02002891 vim_memset(&cf, 0, sizeof(cf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892 cf.lStructSize = sizeof(cf);
2893 cf.hwndOwner = s_hwnd;
2894 cf.Flags = CF_SCREENFONTS | CF_FIXEDPITCHONLY | CF_INITTOLOGFONTSTRUCT;
2895 if (lastlf != NULL)
2896 *lf = *lastlf;
2897 cf.lpLogFont = lf;
2898 cf.nFontType = 0 ; //REGULAR_FONTTYPE;
2899 if (ChooseFont(&cf))
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002900 ret = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901#endif
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002902 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 }
2904
2905 /*
2906 * Split name up, it could be <name>:h<height>:w<width> etc.
2907 */
2908 for (p = name; *p && *p != ':'; p++)
2909 {
2910 if (p - name + 1 > LF_FACESIZE)
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002911 goto theend; /* Name too long */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 lf->lfFaceName[p - name] = *p;
2913 }
2914 if (p != name)
2915 lf->lfFaceName[p - name] = NUL;
2916
2917 /* First set defaults */
2918 lf->lfHeight = -12;
2919 lf->lfWidth = 0;
2920 lf->lfWeight = FW_NORMAL;
2921 lf->lfItalic = FALSE;
2922 lf->lfUnderline = FALSE;
2923 lf->lfStrikeOut = FALSE;
2924
2925 /*
2926 * If the font can't be found, try replacing '_' by ' '.
2927 */
2928 if (init_logfont(lf) == FAIL)
2929 {
2930 int did_replace = FALSE;
2931
2932 for (i = 0; lf->lfFaceName[i]; ++i)
2933 if (lf->lfFaceName[i] == '_')
2934 {
2935 lf->lfFaceName[i] = ' ';
2936 did_replace = TRUE;
2937 }
2938 if (!did_replace || init_logfont(lf) == FAIL)
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002939 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 }
2941
2942 while (*p == ':')
2943 p++;
2944
2945 /* Set the values found after ':' */
2946 while (*p)
2947 {
2948 switch (*p++)
2949 {
2950 case 'h':
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002951 lf->lfHeight = - points_to_pixels(p, &p, TRUE, (long_i)printer_dc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 break;
2953 case 'w':
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002954 lf->lfWidth = points_to_pixels(p, &p, FALSE, (long_i)printer_dc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 break;
2956 case 'b':
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 lf->lfWeight = FW_BOLD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958 break;
2959 case 'i':
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960 lf->lfItalic = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 break;
2962 case 'u':
2963 lf->lfUnderline = TRUE;
2964 break;
2965 case 's':
2966 lf->lfStrikeOut = TRUE;
2967 break;
2968 case 'c':
2969 {
2970 struct charset_pair *cp;
2971
2972 for (cp = charset_pairs; cp->name != NULL; ++cp)
2973 if (STRNCMP(p, cp->name, strlen(cp->name)) == 0)
2974 {
2975 lf->lfCharSet = cp->charset;
2976 p += strlen(cp->name);
2977 break;
2978 }
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002979 if (cp->name == NULL && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 {
Bram Moolenaar051b7822005-05-19 21:00:46 +00002981 vim_snprintf((char *)IObuff, IOSIZE,
2982 _("E244: Illegal charset name \"%s\" in font name \"%s\""), p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 EMSG(IObuff);
2984 break;
2985 }
2986 break;
2987 }
2988 default:
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002989 if (verbose)
2990 {
Bram Moolenaar051b7822005-05-19 21:00:46 +00002991 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002992 _("E245: Illegal char '%c' in font name \"%s\""),
2993 p[-1], name);
2994 EMSG(IObuff);
2995 }
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002996 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997 }
2998 while (*p == ':')
2999 p++;
3000 }
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003001 ret = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004 /* ron: init lastlf */
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003005 if (ret == OK && printer_dc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006 {
3007 vim_free(lastlf);
3008 lastlf = (LOGFONT *)alloc(sizeof(LOGFONT));
3009 if (lastlf != NULL)
3010 mch_memmove(lastlf, lf, sizeof(LOGFONT));
3011 }
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003012#ifdef FEAT_MBYTE
3013 vim_free(acpname);
3014#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003016 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017}
3018
3019#endif /* defined(FEAT_GUI) || defined(FEAT_PRINTER) */
Bram Moolenaarf12d9832016-01-29 21:11:25 +01003020
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003021#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaarf12d9832016-01-29 21:11:25 +01003022/*
3023 * Initialize the Winsock dll.
3024 */
3025 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003026channel_init_winsock(void)
Bram Moolenaarf12d9832016-01-29 21:11:25 +01003027{
3028 WSADATA wsaData;
3029 int wsaerr;
3030
3031 if (WSInitialized)
3032 return;
3033
3034 wsaerr = WSAStartup(MAKEWORD(2, 2), &wsaData);
3035 if (wsaerr == 0)
3036 WSInitialized = TRUE;
3037}
3038#endif