blob: 586f8740de63338cbf25b42e75c41c62a362be1e [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * os_mswin.c
12 *
Bram Moolenaarcf7164a2016-02-20 13:55:06 +010013 * Routines for Win32.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014 */
15
Bram Moolenaar071d4272004-06-13 20:20:40 +000016#include "vim.h"
17
Bram Moolenaar071d4272004-06-13 20:20:40 +000018#include <sys/types.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000019#include <signal.h>
20#include <limits.h>
Bram Moolenaar82881492012-11-20 16:53:39 +010021#ifndef PROTO
22# include <process.h>
23#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024
25#undef chdir
26#ifdef __GNUC__
27# ifndef __MINGW32__
28# include <dirent.h>
29# endif
30#else
31# include <direct.h>
32#endif
33
Bram Moolenaar82881492012-11-20 16:53:39 +010034#ifndef PROTO
35# if defined(FEAT_TITLE) && !defined(FEAT_GUI_W32)
36# include <shellapi.h>
37# endif
38
39# if defined(FEAT_PRINTER) && !defined(FEAT_POSTSCRIPT)
40# include <dlgs.h>
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 Moolenaar8767f522016-07-01 17:17:39 +0200484/* Use 64-bit stat functions if available. */
485#ifdef HAVE_STAT64
486# undef stat
487# undef _stat
488# undef _wstat
489# undef _fstat
490# define stat _stat64
491# define _stat _stat64
492# define _wstat _wstat64
493# define _fstat _fstat64
494#endif
495
Bram Moolenaar9d1685d2014-01-14 12:18:45 +0100496#if (defined(_MSC_VER) && (_MSC_VER >= 1300)) || defined(__MINGW32__)
Bram Moolenaar8962fda2013-09-29 19:05:21 +0200497# define OPEN_OH_ARGTYPE intptr_t
498#else
499# define OPEN_OH_ARGTYPE long
500#endif
501
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200502 static int
Bram Moolenaar8767f522016-07-01 17:17:39 +0200503stat_symlink_aware(const char *name, stat_T *stp)
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200504{
Bram Moolenaarfce7b3d2016-01-19 19:00:32 +0100505#if (defined(_MSC_VER) && (_MSC_VER < 1900)) || defined(__MINGW32__)
506 /* Work around for VC12 or earlier (and MinGW). stat() can't handle
507 * symlinks properly.
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200508 * VC9 or earlier: stat() doesn't support a symlink at all. It retrieves
509 * status of a symlink itself.
510 * VC10: stat() supports a symlink to a normal file, but it doesn't support
Bram Moolenaarfce7b3d2016-01-19 19:00:32 +0100511 * a symlink to a directory (always returns an error).
512 * VC11 and VC12: stat() doesn't return an error for a symlink to a
513 * directory, but it doesn't set S_IFDIR flag.
514 * MinGW: Same as VC9. */
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200515 WIN32_FIND_DATA findData;
516 HANDLE hFind, h;
517 DWORD attr = 0;
518 BOOL is_symlink = FALSE;
519
520 hFind = FindFirstFile(name, &findData);
521 if (hFind != INVALID_HANDLE_VALUE)
522 {
523 attr = findData.dwFileAttributes;
524 if ((attr & FILE_ATTRIBUTE_REPARSE_POINT)
525 && (findData.dwReserved0 == IO_REPARSE_TAG_SYMLINK))
526 is_symlink = TRUE;
527 FindClose(hFind);
528 }
529 if (is_symlink)
530 {
531 h = CreateFile(name, FILE_READ_ATTRIBUTES,
532 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
533 OPEN_EXISTING,
534 (attr & FILE_ATTRIBUTE_DIRECTORY)
535 ? FILE_FLAG_BACKUP_SEMANTICS : 0,
536 NULL);
537 if (h != INVALID_HANDLE_VALUE)
538 {
539 int fd, n;
540
Bram Moolenaar8962fda2013-09-29 19:05:21 +0200541 fd = _open_osfhandle((OPEN_OH_ARGTYPE)h, _O_RDONLY);
Bram Moolenaar8767f522016-07-01 17:17:39 +0200542 n = _fstat(fd, (struct _stat *)stp);
Bram Moolenaarfce7b3d2016-01-19 19:00:32 +0100543 if ((n == 0) && (attr & FILE_ATTRIBUTE_DIRECTORY))
544 stp->st_mode = (stp->st_mode & ~S_IFREG) | S_IFDIR;
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200545 _close(fd);
546 return n;
547 }
548 }
549#endif
550 return stat(name, stp);
551}
552
553#ifdef FEAT_MBYTE
554 static int
Bram Moolenaar8767f522016-07-01 17:17:39 +0200555wstat_symlink_aware(const WCHAR *name, stat_T *stp)
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200556{
Bram Moolenaarfce7b3d2016-01-19 19:00:32 +0100557# if (defined(_MSC_VER) && (_MSC_VER < 1900)) || defined(__MINGW32__)
558 /* Work around for VC12 or earlier (and MinGW). _wstat() can't handle
559 * symlinks properly.
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200560 * VC9 or earlier: _wstat() doesn't support a symlink at all. It retrieves
561 * status of a symlink itself.
562 * VC10: _wstat() supports a symlink to a normal file, but it doesn't
Bram Moolenaarfce7b3d2016-01-19 19:00:32 +0100563 * support a symlink to a directory (always returns an error).
564 * VC11 and VC12: _wstat() doesn't return an error for a symlink to a
565 * directory, but it doesn't set S_IFDIR flag.
566 * MinGW: Same as VC9. */
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200567 int n;
568 BOOL is_symlink = FALSE;
569 HANDLE hFind, h;
570 DWORD attr = 0;
571 WIN32_FIND_DATAW findDataW;
572
573 hFind = FindFirstFileW(name, &findDataW);
574 if (hFind != INVALID_HANDLE_VALUE)
575 {
576 attr = findDataW.dwFileAttributes;
577 if ((attr & FILE_ATTRIBUTE_REPARSE_POINT)
578 && (findDataW.dwReserved0 == IO_REPARSE_TAG_SYMLINK))
579 is_symlink = TRUE;
580 FindClose(hFind);
581 }
582 if (is_symlink)
583 {
584 h = CreateFileW(name, FILE_READ_ATTRIBUTES,
585 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
586 OPEN_EXISTING,
587 (attr & FILE_ATTRIBUTE_DIRECTORY)
588 ? FILE_FLAG_BACKUP_SEMANTICS : 0,
589 NULL);
590 if (h != INVALID_HANDLE_VALUE)
591 {
592 int fd;
593
Bram Moolenaar8962fda2013-09-29 19:05:21 +0200594 fd = _open_osfhandle((OPEN_OH_ARGTYPE)h, _O_RDONLY);
Bram Moolenaar8767f522016-07-01 17:17:39 +0200595 n = _fstat(fd, (struct _stat *)stp);
Bram Moolenaarfce7b3d2016-01-19 19:00:32 +0100596 if ((n == 0) && (attr & FILE_ATTRIBUTE_DIRECTORY))
597 stp->st_mode = (stp->st_mode & ~S_IFREG) | S_IFDIR;
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200598 _close(fd);
599 return n;
600 }
601 }
602# endif
Bram Moolenaar8767f522016-07-01 17:17:39 +0200603 return _wstat(name, (struct _stat *)stp);
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200604}
605#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606
607/*
608 * stat() can't handle a trailing '/' or '\', remove it first.
609 */
610 int
Bram Moolenaar8767f522016-07-01 17:17:39 +0200611vim_stat(const char *name, stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612{
Bram Moolenaard2a203b2013-08-30 16:51:18 +0200613#ifdef FEAT_MBYTE
614 /* WinNT and later can use _MAX_PATH wide characters for a pathname, which
615 * means that the maximum pathname is _MAX_PATH * 3 bytes when 'enc' is
616 * UTF-8. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100617 char_u buf[_MAX_PATH * 3 + 1];
Bram Moolenaard2a203b2013-08-30 16:51:18 +0200618#else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100619 char_u buf[_MAX_PATH + 1];
Bram Moolenaard2a203b2013-08-30 16:51:18 +0200620#endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100621 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622
Bram Moolenaard2a203b2013-08-30 16:51:18 +0200623 vim_strncpy((char_u *)buf, (char_u *)name, sizeof(buf) - 1);
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100624 p = buf + STRLEN(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625 if (p > buf)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000626 mb_ptr_back(buf, p);
Bram Moolenaarb1cb35f2014-01-10 13:05:20 +0100627
628 /* Remove trailing '\\' except root path. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629 if (p > buf && (*p == '\\' || *p == '/') && p[-1] != ':')
630 *p = NUL;
Bram Moolenaarb1cb35f2014-01-10 13:05:20 +0100631
632 if ((buf[0] == '\\' && buf[1] == '\\') || (buf[0] == '/' && buf[1] == '/'))
633 {
634 /* UNC root path must be followed by '\\'. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100635 p = vim_strpbrk(buf + 2, (char_u *)"\\/");
Bram Moolenaarb1cb35f2014-01-10 13:05:20 +0100636 if (p != NULL)
637 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100638 p = vim_strpbrk(p + 1, (char_u *)"\\/");
Bram Moolenaarb1cb35f2014-01-10 13:05:20 +0100639 if (p == NULL)
640 STRCAT(buf, "\\");
641 }
642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643#ifdef FEAT_MBYTE
644 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage
645# ifdef __BORLANDC__
646 /* Wide functions of Borland C 5.5 do not work on Windows 98. */
647 && g_PlatformId == VER_PLATFORM_WIN32_NT
648# endif
649 )
650 {
Bram Moolenaar36f692d2008-11-20 16:10:17 +0000651 WCHAR *wp = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652 int n;
653
654 if (wp != NULL)
655 {
Bram Moolenaar8767f522016-07-01 17:17:39 +0200656 n = wstat_symlink_aware(wp, stp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657 vim_free(wp);
Bram Moolenaarcd981f22014-02-11 17:06:00 +0100658 if (n >= 0 || g_PlatformId == VER_PLATFORM_WIN32_NT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 return n;
660 /* Retry with non-wide function (for Windows 98). Can't use
661 * GetLastError() here and it's unclear what errno gets set to if
662 * the _wstat() fails for missing wide functions. */
663 }
664 }
665#endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100666 return stat_symlink_aware((char *)buf, stp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667}
668
669#if defined(FEAT_GUI_MSWIN) || defined(PROTO)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000670/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 void
672mch_settmode(int tmode)
673{
674 /* nothing to do */
675}
676
677 int
678mch_get_shellsize(void)
679{
680 /* never used */
681 return OK;
682}
683
684 void
685mch_set_shellsize(void)
686{
687 /* never used */
688}
689
690/*
691 * Rows and/or Columns has changed.
692 */
693 void
694mch_new_shellsize(void)
695{
696 /* never used */
697}
698
699#endif
700
701/*
702 * We have no job control, so fake it by starting a new shell.
703 */
704 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100705mch_suspend(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706{
707 suspend_shell();
708}
709
710#if defined(USE_MCH_ERRMSG) || defined(PROTO)
711
712#ifdef display_errors
713# undef display_errors
714#endif
715
716/*
717 * Display the saved error message(s).
718 */
719 void
Bram Moolenaar05540972016-01-30 20:31:25 +0100720display_errors(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721{
722 char *p;
723
724 if (error_ga.ga_data != NULL)
725 {
726 /* avoid putting up a message box with blanks only */
727 for (p = (char *)error_ga.ga_data; *p; ++p)
728 if (!isspace(*p))
729 {
Bram Moolenaarc17ef8e2006-03-25 21:48:58 +0000730 (void)gui_mch_dialog(
731#ifdef FEAT_GUI
732 gui.starting ? VIM_INFO :
733#endif
734 VIM_ERROR,
735#ifdef FEAT_GUI
736 gui.starting ? (char_u *)_("Message") :
737#endif
738 (char_u *)_("Error"),
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100739 (char_u *)p, (char_u *)_("&Ok"),
740 1, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 break;
742 }
743 ga_clear(&error_ga);
744 }
745}
746#endif
747
748
749/*
750 * Return TRUE if "p" contain a wildcard that can be expanded by
751 * dos_expandpath().
752 */
753 int
754mch_has_exp_wildcard(char_u *p)
755{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000756 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 {
758 if (vim_strchr((char_u *)"?*[", *p) != NULL
759 || (*p == '~' && p[1] != NUL))
760 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 }
762 return FALSE;
763}
764
765/*
766 * Return TRUE if "p" contain a wildcard or a "~1" kind of thing (could be a
767 * shortened file name).
768 */
769 int
770mch_has_wildcard(char_u *p)
771{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000772 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 {
774 if (vim_strchr((char_u *)
775# ifdef VIM_BACKTICK
776 "?*$[`"
777# else
778 "?*$["
779# endif
780 , *p) != NULL
781 || (*p == '~' && p[1] != NUL))
782 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 }
784 return FALSE;
785}
786
787
788/*
789 * The normal _chdir() does not change the default drive. This one does.
790 * Returning 0 implies success; -1 implies failure.
791 */
792 int
793mch_chdir(char *path)
794{
795 if (path[0] == NUL) /* just checking... */
796 return -1;
797
Bram Moolenaara2974d72009-07-14 16:38:36 +0000798 if (p_verbose >= 5)
799 {
800 verbose_enter();
801 smsg((char_u *)"chdir(%s)", path);
802 verbose_leave();
803 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 if (isalpha(path[0]) && path[1] == ':') /* has a drive name */
805 {
806 /* If we can change to the drive, skip that part of the path. If we
807 * can't then the current directory may be invalid, try using chdir()
808 * with the whole path. */
809 if (_chdrive(TOLOWER_ASC(path[0]) - 'a' + 1) == 0)
810 path += 2;
811 }
812
813 if (*path == NUL) /* drive name only */
814 return 0;
815
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000816#ifdef FEAT_MBYTE
817 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
818 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100819 WCHAR *p = enc_to_utf16((char_u *)path, NULL);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000820 int n;
821
822 if (p != NULL)
823 {
824 n = _wchdir(p);
825 vim_free(p);
Bram Moolenaarcd981f22014-02-11 17:06:00 +0100826 if (n == 0 || g_PlatformId == VER_PLATFORM_WIN32_NT)
827 return n;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000828 /* Retry with non-wide function (for Windows 98). */
829 }
830 }
831#endif
832
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 return chdir(path); /* let the normal chdir() do the rest */
834}
835
836
837/*
838 * Switching off termcap mode is only allowed when Columns is 80, otherwise a
839 * crash may result. It's always allowed on NT or when running the GUI.
840 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000841/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 int
843can_end_termcap_mode(
844 int give_msg)
845{
846#ifdef FEAT_GUI_MSWIN
847 return TRUE; /* GUI starts a new console anyway */
848#else
849 if (g_PlatformId == VER_PLATFORM_WIN32_NT || Columns == 80)
850 return TRUE;
851 if (give_msg)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100852 msg((char_u *)
853 _("'columns' is not 80, cannot execute external commands"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 return FALSE;
855#endif
856}
857
858#ifdef FEAT_GUI_MSWIN
859/*
860 * return non-zero if a character is available
861 */
862 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100863mch_char_avail(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864{
865 /* never used */
866 return TRUE;
867}
868#endif
869
870
871/*
872 * set screen mode, always fails.
873 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000874/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 int
876mch_screenmode(
877 char_u *arg)
878{
879 EMSG(_(e_screenmode));
880 return FAIL;
881}
882
883
884#if defined(FEAT_LIBCALL) || defined(PROTO)
885/*
886 * Call a DLL routine which takes either a string or int param
887 * and returns an allocated string.
888 * Return OK if it worked, FAIL if not.
889 */
890# ifdef WIN3264
891typedef LPTSTR (*MYSTRPROCSTR)(LPTSTR);
892typedef LPTSTR (*MYINTPROCSTR)(int);
893typedef int (*MYSTRPROCINT)(LPTSTR);
894typedef int (*MYINTPROCINT)(int);
895# else
896typedef LPSTR (*MYSTRPROCSTR)(LPSTR);
897typedef LPSTR (*MYINTPROCSTR)(int);
898typedef int (*MYSTRPROCINT)(LPSTR);
899typedef int (*MYINTPROCINT)(int);
900# endif
901
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902/*
903 * Check if a pointer points to a valid NUL terminated string.
904 * Return the length of the string, including terminating NUL.
905 * Returns 0 for an invalid pointer, 1 for an empty string.
906 */
907 static size_t
908check_str_len(char_u *str)
909{
910 SYSTEM_INFO si;
911 MEMORY_BASIC_INFORMATION mbi;
912 size_t length = 0;
913 size_t i;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100914 const char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915
916 /* get page size */
917 GetSystemInfo(&si);
918
919 /* get memory information */
920 if (VirtualQuery(str, &mbi, sizeof(mbi)))
921 {
922 /* pre cast these (typing savers) */
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000923 long_u dwStr = (long_u)str;
924 long_u dwBaseAddress = (long_u)mbi.BaseAddress;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925
926 /* get start address of page that str is on */
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000927 long_u strPage = dwStr - (dwStr - dwBaseAddress) % si.dwPageSize;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928
929 /* get length from str to end of page */
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000930 long_u pageLength = si.dwPageSize - (dwStr - strPage);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931
Bram Moolenaar442b4222010-05-24 21:34:22 +0200932 for (p = str; !IsBadReadPtr(p, (UINT)pageLength);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 p += pageLength, pageLength = si.dwPageSize)
934 for (i = 0; i < pageLength; ++i, ++length)
935 if (p[i] == NUL)
936 return length + 1;
937 }
938
939 return 0;
940}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200942/*
943 * Passed to do_in_runtimepath() to load a vim.ico file.
944 */
945 static void
946mch_icon_load_cb(char_u *fname, void *cookie)
947{
948 HANDLE *h = (HANDLE *)cookie;
949
950 *h = LoadImage(NULL,
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100951 (LPSTR)fname,
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200952 IMAGE_ICON,
953 64,
954 64,
955 LR_LOADFROMFILE | LR_LOADMAP3DCOLORS);
956}
957
958/*
959 * Try loading an icon file from 'runtimepath'.
960 */
961 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100962mch_icon_load(HANDLE *iconp)
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200963{
964 return do_in_runtimepath((char_u *)"bitmaps/vim.ico",
Bram Moolenaar7f8989d2016-03-12 22:11:39 +0100965 0, mch_icon_load_cb, iconp);
Bram Moolenaarcddc91c2014-09-23 21:53:41 +0200966}
967
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 int
969mch_libcall(
970 char_u *libname,
971 char_u *funcname,
972 char_u *argstring, /* NULL when using a argint */
973 int argint,
974 char_u **string_result,/* NULL when using number_result */
975 int *number_result)
976{
977 HINSTANCE hinstLib;
978 MYSTRPROCSTR ProcAdd;
979 MYINTPROCSTR ProcAddI;
980 char_u *retval_str = NULL;
981 int retval_int = 0;
982 size_t len;
983
984 BOOL fRunTimeLinkSuccess = FALSE;
985
986 // Get a handle to the DLL module.
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100987 hinstLib = vimLoadLib((char *)libname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988
989 // If the handle is valid, try to get the function address.
990 if (hinstLib != NULL)
991 {
992#ifdef HAVE_TRY_EXCEPT
993 __try
994 {
995#endif
996 if (argstring != NULL)
997 {
998 /* Call with string argument */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100999 ProcAdd = (MYSTRPROCSTR)GetProcAddress(hinstLib, (LPCSTR)funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 if ((fRunTimeLinkSuccess = (ProcAdd != NULL)) != 0)
1001 {
1002 if (string_result == NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001003 retval_int = ((MYSTRPROCINT)ProcAdd)((LPSTR)argstring);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001005 retval_str = (char_u *)(ProcAdd)((LPSTR)argstring);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 }
1007 }
1008 else
1009 {
1010 /* Call with number argument */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001011 ProcAddI = (MYINTPROCSTR) GetProcAddress(hinstLib, (LPCSTR)funcname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 if ((fRunTimeLinkSuccess = (ProcAddI != NULL)) != 0)
1013 {
1014 if (string_result == NULL)
1015 retval_int = ((MYINTPROCINT)ProcAddI)(argint);
1016 else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001017 retval_str = (char_u *)(ProcAddI)(argint);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 }
1019 }
1020
1021 // Save the string before we free the library.
1022 // Assume that a "1" result is an illegal pointer.
1023 if (string_result == NULL)
1024 *number_result = retval_int;
1025 else if (retval_str != NULL
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001026 && (len = check_str_len(retval_str)) > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 {
1028 *string_result = lalloc((long_u)len, TRUE);
1029 if (*string_result != NULL)
1030 mch_memmove(*string_result, retval_str, len);
1031 }
1032
1033#ifdef HAVE_TRY_EXCEPT
1034 }
1035 __except(EXCEPTION_EXECUTE_HANDLER)
1036 {
1037 if (GetExceptionCode() == EXCEPTION_STACK_OVERFLOW)
1038 RESETSTKOFLW();
1039 fRunTimeLinkSuccess = 0;
1040 }
1041#endif
1042
1043 // Free the DLL module.
1044 (void)FreeLibrary(hinstLib);
1045 }
1046
1047 if (!fRunTimeLinkSuccess)
1048 {
1049 EMSG2(_(e_libcall), funcname);
1050 return FAIL;
1051 }
1052
1053 return OK;
1054}
1055#endif
1056
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057/*
1058 * Debugging helper: expose the MCH_WRITE_DUMP stuff to other modules
1059 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001060/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 void
1062DumpPutS(
1063 const char *psz)
1064{
1065# ifdef MCH_WRITE_DUMP
1066 if (fdDump)
1067 {
1068 fputs(psz, fdDump);
1069 if (psz[strlen(psz) - 1] != '\n')
1070 fputc('\n', fdDump);
1071 fflush(fdDump);
1072 }
1073# endif
1074}
1075
1076#ifdef _DEBUG
1077
1078void __cdecl
1079Trace(
1080 char *pszFormat,
1081 ...)
1082{
1083 CHAR szBuff[2048];
1084 va_list args;
1085
1086 va_start(args, pszFormat);
1087 vsprintf(szBuff, pszFormat, args);
1088 va_end(args);
1089
1090 OutputDebugString(szBuff);
1091}
1092
1093#endif //_DEBUG
1094
Bram Moolenaar843ee412004-06-30 16:16:41 +00001095#if !defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096# if defined(FEAT_TITLE) && defined(WIN3264)
1097extern HWND g_hWnd; /* This is in os_win32.c. */
1098# endif
1099
1100/*
1101 * Showing the printer dialog is tricky since we have no GUI
1102 * window to parent it. The following routines are needed to
1103 * get the window parenting and Z-order to work properly.
1104 */
1105 static void
1106GetConsoleHwnd(void)
1107{
1108# define MY_BUFSIZE 1024 // Buffer size for console window titles.
1109
1110 char pszNewWindowTitle[MY_BUFSIZE]; // Contains fabricated WindowTitle.
1111 char pszOldWindowTitle[MY_BUFSIZE]; // Contains original WindowTitle.
1112
1113 /* Skip if it's already set. */
1114 if (s_hwnd != 0)
1115 return;
1116
1117# if defined(FEAT_TITLE) && defined(WIN3264)
1118 /* Window handle may have been found by init code (Windows NT only) */
1119 if (g_hWnd != 0)
1120 {
1121 s_hwnd = g_hWnd;
1122 return;
1123 }
1124# endif
1125
1126 GetConsoleTitle(pszOldWindowTitle, MY_BUFSIZE);
1127
1128 wsprintf(pszNewWindowTitle, "%s/%d/%d",
1129 pszOldWindowTitle,
1130 GetTickCount(),
1131 GetCurrentProcessId());
1132 SetConsoleTitle(pszNewWindowTitle);
1133 Sleep(40);
1134 s_hwnd = FindWindow(NULL, pszNewWindowTitle);
1135
1136 SetConsoleTitle(pszOldWindowTitle);
1137}
Bram Moolenaar843ee412004-06-30 16:16:41 +00001138
1139/*
1140 * Console implementation of ":winpos".
1141 */
1142 int
1143mch_get_winpos(int *x, int *y)
1144{
1145 RECT rect;
1146
1147 GetConsoleHwnd();
1148 GetWindowRect(s_hwnd, &rect);
1149 *x = rect.left;
1150 *y = rect.top;
1151 return OK;
1152}
1153
1154/*
1155 * Console implementation of ":winpos x y".
1156 */
1157 void
1158mch_set_winpos(int x, int y)
1159{
1160 GetConsoleHwnd();
1161 SetWindowPos(s_hwnd, NULL, x, y, 0, 0,
1162 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
1163}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164#endif
1165
1166#if (defined(FEAT_PRINTER) && !defined(FEAT_POSTSCRIPT)) || defined(PROTO)
1167
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168/*=================================================================
1169 * Win32 printer stuff
1170 */
1171
1172static HFONT prt_font_handles[2][2][2];
1173static PRINTDLG prt_dlg;
1174static const int boldface[2] = {FW_REGULAR, FW_BOLD};
1175static TEXTMETRIC prt_tm;
1176static int prt_line_height;
1177static int prt_number_width;
1178static int prt_left_margin;
1179static int prt_right_margin;
1180static int prt_top_margin;
1181static char_u szAppName[] = TEXT("VIM");
1182static HWND hDlgPrint;
1183static int *bUserAbort = NULL;
1184static char_u *prt_name = NULL;
1185
1186/* Defines which are also in vim.rc. */
1187#define IDC_BOX1 400
1188#define IDC_PRINTTEXT1 401
1189#define IDC_PRINTTEXT2 402
1190#define IDC_PROGRESS 403
1191
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001192#if !defined(FEAT_MBYTE)
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001193# define vimSetDlgItemText(h, i, s) SetDlgItemText(h, i, s)
1194#else
1195 static BOOL
1196vimSetDlgItemText(HWND hDlg, int nIDDlgItem, char_u *s)
1197{
1198 WCHAR *wp = NULL;
1199 BOOL ret;
1200
1201 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
1202 {
1203 wp = enc_to_utf16(s, NULL);
1204 }
1205 if (wp != NULL)
1206 {
1207 ret = SetDlgItemTextW(hDlg, nIDDlgItem, wp);
1208 vim_free(wp);
1209 return ret;
1210 }
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001211 return SetDlgItemText(hDlg, nIDDlgItem, (LPCSTR)s);
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001212}
1213#endif
1214
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215/*
1216 * Convert BGR to RGB for Windows GDI calls
1217 */
1218 static COLORREF
1219swap_me(COLORREF colorref)
1220{
1221 int temp;
1222 char *ptr = (char *)&colorref;
1223
1224 temp = *(ptr);
1225 *(ptr ) = *(ptr + 2);
1226 *(ptr + 2) = temp;
1227 return colorref;
1228}
1229
Bram Moolenaared39e1d2008-08-09 17:55:22 +00001230/* Attempt to make this work for old and new compilers */
Bram Moolenaar9f733d12011-09-21 20:09:42 +02001231#if !defined(_WIN64) && (!defined(_MSC_VER) || _MSC_VER < 1300)
Bram Moolenaared39e1d2008-08-09 17:55:22 +00001232# define PDP_RETVAL BOOL
1233#else
1234# define PDP_RETVAL INT_PTR
1235#endif
1236
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001237/*ARGSUSED*/
Bram Moolenaared39e1d2008-08-09 17:55:22 +00001238 static PDP_RETVAL CALLBACK
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239PrintDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
1240{
1241#ifdef FEAT_GETTEXT
1242 NONCLIENTMETRICS nm;
1243 static HFONT hfont;
1244#endif
1245
1246 switch (message)
1247 {
1248 case WM_INITDIALOG:
1249#ifdef FEAT_GETTEXT
1250 nm.cbSize = sizeof(NONCLIENTMETRICS);
1251 if (SystemParametersInfo(
1252 SPI_GETNONCLIENTMETRICS,
1253 sizeof(NONCLIENTMETRICS),
1254 &nm,
1255 0))
1256 {
1257 char buff[MAX_PATH];
1258 int i;
1259
1260 /* Translate the dialog texts */
1261 hfont = CreateFontIndirect(&nm.lfMessageFont);
1262 for (i = IDC_PRINTTEXT1; i <= IDC_PROGRESS; i++)
1263 {
1264 SendDlgItemMessage(hDlg, i, WM_SETFONT, (WPARAM)hfont, 1);
1265 if (GetDlgItemText(hDlg,i, buff, sizeof(buff)))
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001266 vimSetDlgItemText(hDlg,i, (char_u *)_(buff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 }
1268 SendDlgItemMessage(hDlg, IDCANCEL,
1269 WM_SETFONT, (WPARAM)hfont, 1);
1270 if (GetDlgItemText(hDlg,IDCANCEL, buff, sizeof(buff)))
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001271 vimSetDlgItemText(hDlg,IDCANCEL, (char_u *)_(buff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 }
1273#endif
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001274 SetWindowText(hDlg, (LPCSTR)szAppName);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 if (prt_name != NULL)
1276 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001277 vimSetDlgItemText(hDlg, IDC_PRINTTEXT2, (char_u *)prt_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 vim_free(prt_name);
1279 prt_name = NULL;
1280 }
1281 EnableMenuItem(GetSystemMenu(hDlg, FALSE), SC_CLOSE, MF_GRAYED);
1282#ifndef FEAT_GUI
1283 BringWindowToTop(s_hwnd);
1284#endif
1285 return TRUE;
1286
1287 case WM_COMMAND:
1288 *bUserAbort = TRUE;
1289 EnableWindow(GetParent(hDlg), TRUE);
1290 DestroyWindow(hDlg);
1291 hDlgPrint = NULL;
1292#ifdef FEAT_GETTEXT
1293 DeleteObject(hfont);
1294#endif
1295 return TRUE;
1296 }
1297 return FALSE;
1298}
1299
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001300/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 static BOOL CALLBACK
1302AbortProc(HDC hdcPrn, int iCode)
1303{
1304 MSG msg;
1305
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02001306 while (!*bUserAbort && pPeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 {
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02001308 if (!hDlgPrint || !pIsDialogMessage(hDlgPrint, &msg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 {
1310 TranslateMessage(&msg);
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02001311 pDispatchMessage(&msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 }
1313 }
1314 return !*bUserAbort;
1315}
1316
1317#ifndef FEAT_GUI
1318
Bram Moolenaar26fdd7d2011-11-30 13:42:44 +01001319 static UINT_PTR CALLBACK
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320PrintHookProc(
1321 HWND hDlg, // handle to dialog box
1322 UINT uiMsg, // message identifier
1323 WPARAM wParam, // message parameter
1324 LPARAM lParam // message parameter
1325 )
1326{
1327 HWND hwndOwner;
1328 RECT rc, rcDlg, rcOwner;
1329 PRINTDLG *pPD;
1330
1331 if (uiMsg == WM_INITDIALOG)
1332 {
1333 // Get the owner window and dialog box rectangles.
1334 if ((hwndOwner = GetParent(hDlg)) == NULL)
1335 hwndOwner = GetDesktopWindow();
1336
1337 GetWindowRect(hwndOwner, &rcOwner);
1338 GetWindowRect(hDlg, &rcDlg);
1339 CopyRect(&rc, &rcOwner);
1340
1341 // Offset the owner and dialog box rectangles so that
1342 // right and bottom values represent the width and
1343 // height, and then offset the owner again to discard
1344 // space taken up by the dialog box.
1345
1346 OffsetRect(&rcDlg, -rcDlg.left, -rcDlg.top);
1347 OffsetRect(&rc, -rc.left, -rc.top);
1348 OffsetRect(&rc, -rcDlg.right, -rcDlg.bottom);
1349
1350 // The new position is the sum of half the remaining
1351 // space and the owner's original position.
1352
1353 SetWindowPos(hDlg,
1354 HWND_TOP,
1355 rcOwner.left + (rc.right / 2),
1356 rcOwner.top + (rc.bottom / 2),
1357 0, 0, // ignores size arguments
1358 SWP_NOSIZE);
1359
1360 /* tackle the printdlg copiesctrl problem */
1361 pPD = (PRINTDLG *)lParam;
1362 pPD->nCopies = (WORD)pPD->lCustData;
1363 SetDlgItemInt( hDlg, edt3, pPD->nCopies, FALSE );
1364 /* Bring the window to top */
1365 BringWindowToTop(GetParent(hDlg));
1366 SetForegroundWindow(hDlg);
1367 }
1368
1369 return FALSE;
1370}
1371#endif
1372
1373 void
1374mch_print_cleanup(void)
1375{
1376 int pifItalic;
1377 int pifBold;
1378 int pifUnderline;
1379
1380 for (pifBold = 0; pifBold <= 1; pifBold++)
1381 for (pifItalic = 0; pifItalic <= 1; pifItalic++)
1382 for (pifUnderline = 0; pifUnderline <= 1; pifUnderline++)
1383 DeleteObject(prt_font_handles[pifBold][pifItalic][pifUnderline]);
1384
1385 if (prt_dlg.hDC != NULL)
1386 DeleteDC(prt_dlg.hDC);
1387 if (!*bUserAbort)
1388 SendMessage(hDlgPrint, WM_COMMAND, 0, 0);
1389}
1390
1391 static int
1392to_device_units(int idx, int dpi, int physsize, int offset, int def_number)
1393{
1394 int ret = 0;
1395 int u;
1396 int nr;
1397
1398 u = prt_get_unit(idx);
1399 if (u == PRT_UNIT_NONE)
1400 {
1401 u = PRT_UNIT_PERC;
1402 nr = def_number;
1403 }
1404 else
1405 nr = printer_opts[idx].number;
1406
1407 switch (u)
1408 {
1409 case PRT_UNIT_PERC:
1410 ret = (physsize * nr) / 100;
1411 break;
1412 case PRT_UNIT_INCH:
1413 ret = (nr * dpi);
1414 break;
1415 case PRT_UNIT_MM:
1416 ret = (nr * 10 * dpi) / 254;
1417 break;
1418 case PRT_UNIT_POINT:
1419 ret = (nr * 10 * dpi) / 720;
1420 break;
1421 }
1422
1423 if (ret < offset)
1424 return 0;
1425 else
1426 return ret - offset;
1427}
1428
1429 static int
1430prt_get_cpl(void)
1431{
1432 int hr;
1433 int phyw;
1434 int dvoff;
1435 int rev_offset;
1436 int dpi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437
1438 GetTextMetrics(prt_dlg.hDC, &prt_tm);
1439 prt_line_height = prt_tm.tmHeight + prt_tm.tmExternalLeading;
1440
1441 hr = GetDeviceCaps(prt_dlg.hDC, HORZRES);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 phyw = GetDeviceCaps(prt_dlg.hDC, PHYSICALWIDTH);
1443 dvoff = GetDeviceCaps(prt_dlg.hDC, PHYSICALOFFSETX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 dpi = GetDeviceCaps(prt_dlg.hDC, LOGPIXELSX);
1445
1446 rev_offset = phyw - (dvoff + hr);
1447
1448 prt_left_margin = to_device_units(OPT_PRINT_LEFT, dpi, phyw, dvoff, 10);
1449 if (prt_use_number())
1450 {
1451 prt_number_width = PRINT_NUMBER_WIDTH * prt_tm.tmAveCharWidth;
1452 prt_left_margin += prt_number_width;
1453 }
1454 else
1455 prt_number_width = 0;
1456
1457 prt_right_margin = hr - to_device_units(OPT_PRINT_RIGHT, dpi, phyw,
1458 rev_offset, 5);
1459
1460 return (prt_right_margin - prt_left_margin) / prt_tm.tmAveCharWidth;
1461}
1462
1463 static int
1464prt_get_lpp(void)
1465{
1466 int vr;
1467 int phyw;
1468 int dvoff;
1469 int rev_offset;
1470 int bottom_margin;
1471 int dpi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472
1473 vr = GetDeviceCaps(prt_dlg.hDC, VERTRES);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 phyw = GetDeviceCaps(prt_dlg.hDC, PHYSICALHEIGHT);
1475 dvoff = GetDeviceCaps(prt_dlg.hDC, PHYSICALOFFSETY);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 dpi = GetDeviceCaps(prt_dlg.hDC, LOGPIXELSY);
1477
1478 rev_offset = phyw - (dvoff + vr);
1479
1480 prt_top_margin = to_device_units(OPT_PRINT_TOP, dpi, phyw, dvoff, 5);
1481
1482 /* adjust top margin if there is a header */
1483 prt_top_margin += prt_line_height * prt_header_height();
1484
1485 bottom_margin = vr - to_device_units(OPT_PRINT_BOT, dpi, phyw,
1486 rev_offset, 5);
1487
1488 return (bottom_margin - prt_top_margin) / prt_line_height;
1489}
1490
1491 int
1492mch_print_init(prt_settings_T *psettings, char_u *jobname, int forceit)
1493{
1494 static HGLOBAL stored_dm = NULL;
1495 static HGLOBAL stored_devn = NULL;
1496 static int stored_nCopies = 1;
1497 static int stored_nFlags = 0;
1498
1499 LOGFONT fLogFont;
1500 int pifItalic;
1501 int pifBold;
1502 int pifUnderline;
1503
1504 DEVMODE *mem;
1505 DEVNAMES *devname;
1506 int i;
1507
1508 bUserAbort = &(psettings->user_abort);
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02001509 vim_memset(&prt_dlg, 0, sizeof(PRINTDLG));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 prt_dlg.lStructSize = sizeof(PRINTDLG);
1511#ifndef FEAT_GUI
1512 GetConsoleHwnd(); /* get value of s_hwnd */
1513#endif
1514 prt_dlg.hwndOwner = s_hwnd;
1515 prt_dlg.Flags = PD_NOPAGENUMS | PD_NOSELECTION | PD_RETURNDC;
1516 if (!forceit)
1517 {
1518 prt_dlg.hDevMode = stored_dm;
1519 prt_dlg.hDevNames = stored_devn;
1520 prt_dlg.lCustData = stored_nCopies; // work around bug in print dialog
1521#ifndef FEAT_GUI
1522 /*
1523 * Use hook to prevent console window being sent to back
1524 */
1525 prt_dlg.lpfnPrintHook = PrintHookProc;
1526 prt_dlg.Flags |= PD_ENABLEPRINTHOOK;
1527#endif
1528 prt_dlg.Flags |= stored_nFlags;
1529 }
1530
1531 /*
1532 * If bang present, return default printer setup with no dialog
1533 * never show dialog if we are running over telnet
1534 */
1535 if (forceit
1536#ifndef FEAT_GUI
1537 || !term_console
1538#endif
1539 )
1540 {
1541 prt_dlg.Flags |= PD_RETURNDEFAULT;
1542#ifdef WIN3264
1543 /*
1544 * MSDN suggests setting the first parameter to WINSPOOL for
1545 * NT, but NULL appears to work just as well.
1546 */
1547 if (*p_pdev != NUL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001548 prt_dlg.hDC = CreateDC(NULL, (LPCSTR)p_pdev, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 else
1550#endif
1551 {
1552 prt_dlg.Flags |= PD_RETURNDEFAULT;
1553 if (PrintDlg(&prt_dlg) == 0)
1554 goto init_fail_dlg;
1555 }
1556 }
1557 else if (PrintDlg(&prt_dlg) == 0)
1558 goto init_fail_dlg;
1559 else
1560 {
1561 /*
1562 * keep the previous driver context
1563 */
1564 stored_dm = prt_dlg.hDevMode;
1565 stored_devn = prt_dlg.hDevNames;
1566 stored_nFlags = prt_dlg.Flags;
1567 stored_nCopies = prt_dlg.nCopies;
1568 }
1569
1570 if (prt_dlg.hDC == NULL)
1571 {
1572 EMSG(_("E237: Printer selection failed"));
1573 mch_print_cleanup();
1574 return FALSE;
1575 }
1576
1577 /* Not all printer drivers report the support of color (or grey) in the
1578 * same way. Let's set has_color if there appears to be some way to print
1579 * more than B&W. */
1580 i = GetDeviceCaps(prt_dlg.hDC, NUMCOLORS);
1581 psettings->has_color = (GetDeviceCaps(prt_dlg.hDC, BITSPIXEL) > 1
1582 || GetDeviceCaps(prt_dlg.hDC, PLANES) > 1
1583 || i > 2 || i == -1);
1584
1585 /* Ensure all font styles are baseline aligned */
1586 SetTextAlign(prt_dlg.hDC, TA_BASELINE|TA_LEFT);
1587
1588 /*
1589 * On some windows systems the nCopies parameter is not
1590 * passed back correctly. It must be retrieved from the
1591 * hDevMode struct.
1592 */
1593 mem = (DEVMODE *)GlobalLock(prt_dlg.hDevMode);
1594 if (mem != NULL)
1595 {
1596#ifdef WIN3264
1597 if (mem->dmCopies != 1)
1598 stored_nCopies = mem->dmCopies;
1599#endif
1600 if ((mem->dmFields & DM_DUPLEX) && (mem->dmDuplex & ~DMDUP_SIMPLEX))
1601 psettings->duplex = TRUE;
1602 if ((mem->dmFields & DM_COLOR) && (mem->dmColor & DMCOLOR_COLOR))
1603 psettings->has_color = TRUE;
1604 }
1605 GlobalUnlock(prt_dlg.hDevMode);
1606
1607 devname = (DEVNAMES *)GlobalLock(prt_dlg.hDevNames);
1608 if (devname != 0)
1609 {
1610 char_u *printer_name = (char_u *)devname + devname->wDeviceOffset;
1611 char_u *port_name = (char_u *)devname +devname->wOutputOffset;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001612 char_u *text = (char_u *)_("to %s on %s");
Bram Moolenaarf191d552014-10-09 17:05:56 +02001613#ifdef FEAT_MBYTE
1614 char_u *printer_name_orig = printer_name;
1615 char_u *port_name_orig = port_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616
Bram Moolenaarf191d552014-10-09 17:05:56 +02001617 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
1618 {
1619 char_u *to_free = NULL;
1620 int maxlen;
1621
Bram Moolenaar861d80a2014-10-16 20:35:36 +02001622 acp_to_enc(printer_name, (int)STRLEN(printer_name), &to_free,
1623 &maxlen);
Bram Moolenaarf191d552014-10-09 17:05:56 +02001624 if (to_free != NULL)
1625 printer_name = to_free;
Bram Moolenaar861d80a2014-10-16 20:35:36 +02001626 acp_to_enc(port_name, (int)STRLEN(port_name), &to_free, &maxlen);
Bram Moolenaarf191d552014-10-09 17:05:56 +02001627 if (to_free != NULL)
1628 port_name = to_free;
1629 }
1630#endif
Bram Moolenaare6a91fd2008-07-24 18:51:11 +00001631 prt_name = alloc((unsigned)(STRLEN(printer_name) + STRLEN(port_name)
1632 + STRLEN(text)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 if (prt_name != NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001634 wsprintf((char *)prt_name, (const char *)text,
1635 printer_name, port_name);
Bram Moolenaarf191d552014-10-09 17:05:56 +02001636#ifdef FEAT_MBYTE
1637 if (printer_name != printer_name_orig)
1638 vim_free(printer_name);
1639 if (port_name != port_name_orig)
1640 vim_free(port_name);
1641#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642 }
1643 GlobalUnlock(prt_dlg.hDevNames);
1644
1645 /*
1646 * Initialise the font according to 'printfont'
1647 */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02001648 vim_memset(&fLogFont, 0, sizeof(fLogFont));
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001649 if (get_logfont(&fLogFont, p_pfn, prt_dlg.hDC, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 {
1651 EMSG2(_("E613: Unknown printer font: %s"), p_pfn);
1652 mch_print_cleanup();
1653 return FALSE;
1654 }
1655
1656 for (pifBold = 0; pifBold <= 1; pifBold++)
1657 for (pifItalic = 0; pifItalic <= 1; pifItalic++)
1658 for (pifUnderline = 0; pifUnderline <= 1; pifUnderline++)
1659 {
1660 fLogFont.lfWeight = boldface[pifBold];
1661 fLogFont.lfItalic = pifItalic;
1662 fLogFont.lfUnderline = pifUnderline;
1663 prt_font_handles[pifBold][pifItalic][pifUnderline]
1664 = CreateFontIndirect(&fLogFont);
1665 }
1666
1667 SetBkMode(prt_dlg.hDC, OPAQUE);
1668 SelectObject(prt_dlg.hDC, prt_font_handles[0][0][0]);
1669
1670 /*
1671 * Fill in the settings struct
1672 */
1673 psettings->chars_per_line = prt_get_cpl();
1674 psettings->lines_per_page = prt_get_lpp();
Bram Moolenaar7ddc6422014-09-27 11:18:19 +02001675 if (prt_dlg.Flags & PD_USEDEVMODECOPIESANDCOLLATE)
1676 {
1677 psettings->n_collated_copies = (prt_dlg.Flags & PD_COLLATE)
1678 ? prt_dlg.nCopies : 1;
1679 psettings->n_uncollated_copies = (prt_dlg.Flags & PD_COLLATE)
1680 ? 1 : prt_dlg.nCopies;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681
Bram Moolenaar7ddc6422014-09-27 11:18:19 +02001682 if (psettings->n_collated_copies == 0)
1683 psettings->n_collated_copies = 1;
1684
1685 if (psettings->n_uncollated_copies == 0)
1686 psettings->n_uncollated_copies = 1;
1687 } else {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688 psettings->n_collated_copies = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 psettings->n_uncollated_copies = 1;
Bram Moolenaar7ddc6422014-09-27 11:18:19 +02001690 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691
1692 psettings->jobname = jobname;
1693
1694 return TRUE;
1695
1696init_fail_dlg:
1697 {
1698 DWORD err = CommDlgExtendedError();
1699
1700 if (err)
1701 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 char_u *buf;
1703
1704 /* I suspect FormatMessage() doesn't work for values returned by
1705 * CommDlgExtendedError(). What does? */
1706 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
1707 FORMAT_MESSAGE_FROM_SYSTEM |
1708 FORMAT_MESSAGE_IGNORE_INSERTS,
1709 NULL, err, 0, (LPTSTR)(&buf), 0, NULL);
1710 EMSG2(_("E238: Print error: %s"),
1711 buf == NULL ? (char_u *)_("Unknown") : buf);
1712 LocalFree((LPVOID)(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 }
1714 else
1715 msg_clr_eos(); /* Maybe canceled */
1716
1717 mch_print_cleanup();
1718 return FALSE;
1719 }
1720}
1721
1722
1723 int
1724mch_print_begin(prt_settings_T *psettings)
1725{
1726 int ret;
1727 static DOCINFO di;
1728 char szBuffer[300];
1729
1730 hDlgPrint = CreateDialog(GetModuleHandle(NULL), TEXT("PrintDlgBox"),
1731 prt_dlg.hwndOwner, PrintDlgProc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 SetAbortProc(prt_dlg.hDC, AbortProc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 wsprintf(szBuffer, _("Printing '%s'"), gettail(psettings->jobname));
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001734 vimSetDlgItemText(hDlgPrint, IDC_PRINTTEXT1, (char_u *)szBuffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02001736 vim_memset(&di, 0, sizeof(DOCINFO));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 di.cbSize = sizeof(DOCINFO);
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001738 di.lpszDocName = (LPCSTR)psettings->jobname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 ret = StartDoc(prt_dlg.hDC, &di);
1740
1741#ifdef FEAT_GUI
1742 /* Give focus back to main window (when using MDI). */
1743 SetFocus(s_hwnd);
1744#endif
1745
1746 return (ret > 0);
1747}
1748
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001749/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750 void
1751mch_print_end(prt_settings_T *psettings)
1752{
1753 EndDoc(prt_dlg.hDC);
1754 if (!*bUserAbort)
1755 SendMessage(hDlgPrint, WM_COMMAND, 0, 0);
1756}
1757
1758 int
1759mch_print_end_page(void)
1760{
1761 return (EndPage(prt_dlg.hDC) > 0);
1762}
1763
1764 int
1765mch_print_begin_page(char_u *msg)
1766{
1767 if (msg != NULL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001768 vimSetDlgItemText(hDlgPrint, IDC_PROGRESS, msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 return (StartPage(prt_dlg.hDC) > 0);
1770}
1771
1772 int
1773mch_print_blank_page(void)
1774{
1775 return (mch_print_begin_page(NULL) ? (mch_print_end_page()) : FALSE);
1776}
1777
1778static int prt_pos_x = 0;
1779static int prt_pos_y = 0;
1780
1781 void
Bram Moolenaar05540972016-01-30 20:31:25 +01001782mch_print_start_line(int margin, int page_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783{
1784 if (margin)
1785 prt_pos_x = -prt_number_width;
1786 else
1787 prt_pos_x = 0;
1788 prt_pos_y = page_line * prt_line_height
1789 + prt_tm.tmAscent + prt_tm.tmExternalLeading;
1790}
1791
1792 int
1793mch_print_text_out(char_u *p, int len)
1794{
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001795#if defined(FEAT_PROPORTIONAL_FONTS) || defined(FEAT_MBYTE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 SIZE sz;
1797#endif
Bram Moolenaarcf7164a2016-02-20 13:55:06 +01001798#if defined(FEAT_MBYTE)
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001799 WCHAR *wp = NULL;
1800 int wlen = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001802 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
1803 {
1804 wp = enc_to_utf16(p, &wlen);
1805 }
1806 if (wp != NULL)
1807 {
1808 int ret = FALSE;
1809
1810 TextOutW(prt_dlg.hDC, prt_pos_x + prt_left_margin,
1811 prt_pos_y + prt_top_margin, wp, wlen);
1812 GetTextExtentPoint32W(prt_dlg.hDC, wp, wlen, &sz);
1813 vim_free(wp);
1814 prt_pos_x += (sz.cx - prt_tm.tmOverhang);
1815 /* This is wrong when printing spaces for a TAB. */
1816 if (p[len] != NUL)
1817 {
1818 wlen = MB_PTR2LEN(p + len);
1819 wp = enc_to_utf16(p + len, &wlen);
1820 if (wp != NULL)
1821 {
1822 GetTextExtentPoint32W(prt_dlg.hDC, wp, 1, &sz);
1823 ret = (prt_pos_x + prt_left_margin + sz.cx > prt_right_margin);
1824 vim_free(wp);
1825 }
1826 }
1827 return ret;
1828 }
1829#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 TextOut(prt_dlg.hDC, prt_pos_x + prt_left_margin,
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001831 prt_pos_y + prt_top_margin,
1832 (LPCSTR)p, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833#ifndef FEAT_PROPORTIONAL_FONTS
1834 prt_pos_x += len * prt_tm.tmAveCharWidth;
1835 return (prt_pos_x + prt_left_margin + prt_tm.tmAveCharWidth
1836 + prt_tm.tmOverhang > prt_right_margin);
1837#else
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001838 GetTextExtentPoint32(prt_dlg.hDC, (LPCSTR)p, len, &sz);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 prt_pos_x += (sz.cx - prt_tm.tmOverhang);
1840 /* This is wrong when printing spaces for a TAB. */
1841 if (p[len] == NUL)
1842 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 GetTextExtentPoint32(prt_dlg.hDC, p + len, 1, &sz);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 return (prt_pos_x + prt_left_margin + sz.cx > prt_right_margin);
1845#endif
1846}
1847
1848 void
1849mch_print_set_font(int iBold, int iItalic, int iUnderline)
1850{
1851 SelectObject(prt_dlg.hDC, prt_font_handles[iBold][iItalic][iUnderline]);
1852}
1853
1854 void
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001855mch_print_set_bg(long_u bgcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856{
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001857 SetBkColor(prt_dlg.hDC, GetNearestColor(prt_dlg.hDC,
1858 swap_me((COLORREF)bgcol)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 /*
1860 * With a white background we can draw characters transparent, which is
1861 * good for italic characters that overlap to the next char cell.
1862 */
1863 if (bgcol == 0xffffffUL)
1864 SetBkMode(prt_dlg.hDC, TRANSPARENT);
1865 else
1866 SetBkMode(prt_dlg.hDC, OPAQUE);
1867}
1868
1869 void
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001870mch_print_set_fg(long_u fgcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871{
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001872 SetTextColor(prt_dlg.hDC, GetNearestColor(prt_dlg.hDC,
1873 swap_me((COLORREF)fgcol)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874}
1875
1876#endif /*FEAT_PRINTER && !FEAT_POSTSCRIPT*/
1877
Bram Moolenaar58d98232005-07-23 22:25:46 +00001878
1879
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880#if defined(FEAT_SHORTCUT) || defined(PROTO)
Bram Moolenaar82881492012-11-20 16:53:39 +01001881# ifndef PROTO
1882# include <shlobj.h>
1883# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884
1885/*
1886 * When "fname" is the name of a shortcut (*.lnk) resolve the file it points
1887 * to and return that name in allocated memory.
1888 * Otherwise NULL is returned.
1889 */
1890 char_u *
1891mch_resolve_shortcut(char_u *fname)
1892{
1893 HRESULT hr;
1894 IShellLink *psl = NULL;
1895 IPersistFile *ppf = NULL;
1896 OLECHAR wsz[MAX_PATH];
1897 WIN32_FIND_DATA ffd; // we get those free of charge
Bram Moolenaar604729e2013-08-30 16:44:19 +02001898 CHAR buf[MAX_PATH]; // could have simply reused 'wsz'...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 char_u *rfname = NULL;
1900 int len;
Bram Moolenaar604729e2013-08-30 16:44:19 +02001901# ifdef FEAT_MBYTE
1902 IShellLinkW *pslw = NULL;
1903 WIN32_FIND_DATAW ffdw; // we get those free of charge
1904# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905
1906 /* Check if the file name ends in ".lnk". Avoid calling
1907 * CoCreateInstance(), it's quite slow. */
1908 if (fname == NULL)
1909 return rfname;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001910 len = (int)STRLEN(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 if (len <= 4 || STRNICMP(fname + len - 4, ".lnk", 4) != 0)
1912 return rfname;
1913
1914 CoInitialize(NULL);
1915
Bram Moolenaar604729e2013-08-30 16:44:19 +02001916# ifdef FEAT_MBYTE
1917 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
1918 {
1919 // create a link manager object and request its interface
1920 hr = CoCreateInstance(
1921 &CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
1922 &IID_IShellLinkW, (void**)&pslw);
1923 if (hr == S_OK)
1924 {
1925 WCHAR *p = enc_to_utf16(fname, NULL);
1926
1927 if (p != NULL)
1928 {
1929 // Get a pointer to the IPersistFile interface.
1930 hr = pslw->lpVtbl->QueryInterface(
1931 pslw, &IID_IPersistFile, (void**)&ppf);
1932 if (hr != S_OK)
1933 goto shortcut_errorw;
1934
1935 // "load" the name and resolve the link
1936 hr = ppf->lpVtbl->Load(ppf, p, STGM_READ);
1937 if (hr != S_OK)
1938 goto shortcut_errorw;
1939# if 0 // This makes Vim wait a long time if the target does not exist.
1940 hr = pslw->lpVtbl->Resolve(pslw, NULL, SLR_NO_UI);
1941 if (hr != S_OK)
1942 goto shortcut_errorw;
1943# endif
1944
1945 // Get the path to the link target.
1946 ZeroMemory(wsz, MAX_PATH * sizeof(WCHAR));
1947 hr = pslw->lpVtbl->GetPath(pslw, wsz, MAX_PATH, &ffdw, 0);
1948 if (hr == S_OK && wsz[0] != NUL)
1949 rfname = utf16_to_enc(wsz, NULL);
1950
1951shortcut_errorw:
1952 vim_free(p);
Bram Moolenaarcd981f22014-02-11 17:06:00 +01001953 goto shortcut_end;
Bram Moolenaar604729e2013-08-30 16:44:19 +02001954 }
1955 }
1956 /* Retry with non-wide function (for Windows 98). */
1957 }
1958# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 // create a link manager object and request its interface
1960 hr = CoCreateInstance(
1961 &CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
1962 &IID_IShellLink, (void**)&psl);
1963 if (hr != S_OK)
Bram Moolenaar604729e2013-08-30 16:44:19 +02001964 goto shortcut_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965
1966 // Get a pointer to the IPersistFile interface.
1967 hr = psl->lpVtbl->QueryInterface(
1968 psl, &IID_IPersistFile, (void**)&ppf);
1969 if (hr != S_OK)
Bram Moolenaar604729e2013-08-30 16:44:19 +02001970 goto shortcut_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971
1972 // full path string must be in Unicode.
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001973 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)fname, -1, wsz, MAX_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00001975 // "load" the name and resolve the link
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 hr = ppf->lpVtbl->Load(ppf, wsz, STGM_READ);
1977 if (hr != S_OK)
Bram Moolenaar604729e2013-08-30 16:44:19 +02001978 goto shortcut_end;
1979# if 0 // This makes Vim wait a long time if the target doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 hr = psl->lpVtbl->Resolve(psl, NULL, SLR_NO_UI);
1981 if (hr != S_OK)
Bram Moolenaar604729e2013-08-30 16:44:19 +02001982 goto shortcut_end;
1983# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984
1985 // Get the path to the link target.
1986 ZeroMemory(buf, MAX_PATH);
1987 hr = psl->lpVtbl->GetPath(psl, buf, MAX_PATH, &ffd, 0);
1988 if (hr == S_OK && buf[0] != NUL)
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001989 rfname = vim_strsave((char_u *)buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990
Bram Moolenaar604729e2013-08-30 16:44:19 +02001991shortcut_end:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 // Release all interface pointers (both belong to the same object)
1993 if (ppf != NULL)
1994 ppf->lpVtbl->Release(ppf);
1995 if (psl != NULL)
1996 psl->lpVtbl->Release(psl);
Bram Moolenaar604729e2013-08-30 16:44:19 +02001997# ifdef FEAT_MBYTE
1998 if (pslw != NULL)
1999 pslw->lpVtbl->Release(pslw);
2000# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001
2002 CoUninitialize();
2003 return rfname;
2004}
2005#endif
2006
2007#if (defined(FEAT_EVAL) && !defined(FEAT_GUI)) || defined(PROTO)
2008/*
2009 * Bring ourselves to the foreground. Does work if the OS doesn't allow it.
2010 */
2011 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002012win32_set_foreground(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013{
2014# ifndef FEAT_GUI
2015 GetConsoleHwnd(); /* get value of s_hwnd */
2016# endif
2017 if (s_hwnd != 0)
2018 SetForegroundWindow(s_hwnd);
2019}
2020#endif
2021
2022#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
2023/*
2024 * Client-server code for Vim
2025 *
2026 * Originally written by Paul Moore
2027 */
2028
2029/* In order to handle inter-process messages, we need to have a window. But
2030 * the functions in this module can be called before the main GUI window is
2031 * created (and may also be called in the console version, where there is no
2032 * GUI window at all).
2033 *
2034 * So we create a hidden window, and arrange to destroy it on exit.
2035 */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002036HWND message_window = 0; /* window that's handling messages */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037
2038#define VIM_CLASSNAME "VIM_MESSAGES"
2039#define VIM_CLASSNAME_LEN (sizeof(VIM_CLASSNAME) - 1)
2040
2041/* Communication is via WM_COPYDATA messages. The message type is send in
2042 * the dwData parameter. Types are defined here. */
2043#define COPYDATA_KEYS 0
2044#define COPYDATA_REPLY 1
2045#define COPYDATA_EXPR 10
2046#define COPYDATA_RESULT 11
2047#define COPYDATA_ERROR_RESULT 12
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002048#define COPYDATA_ENCODING 20
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049
2050/* This is a structure containing a server HWND and its name. */
2051struct server_id
2052{
2053 HWND hwnd;
2054 char_u *name;
2055};
2056
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002057/* Last received 'encoding' that the client uses. */
2058static char_u *client_enc = NULL;
2059
2060/*
2061 * Tell the other side what encoding we are using.
2062 * Errors are ignored.
2063 */
2064 static void
2065serverSendEnc(HWND target)
2066{
2067 COPYDATASTRUCT data;
2068
2069 data.dwData = COPYDATA_ENCODING;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002070#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002071 data.cbData = (DWORD)STRLEN(p_enc) + 1;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002072 data.lpData = p_enc;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002073#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +02002074 data.cbData = (DWORD)STRLEN("latin1") + 1;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002075 data.lpData = "latin1";
2076#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002077 (void)SendMessage(target, WM_COPYDATA, (WPARAM)message_window,
2078 (LPARAM)(&data));
2079}
2080
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081/*
2082 * Clean up on exit. This destroys the hidden message window.
2083 */
2084 static void
2085#ifdef __BORLANDC__
2086 _RTLENTRYF
2087#endif
2088CleanUpMessaging(void)
2089{
2090 if (message_window != 0)
2091 {
2092 DestroyWindow(message_window);
2093 message_window = 0;
2094 }
2095}
2096
2097static int save_reply(HWND server, char_u *reply, int expr);
2098
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002099/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 * The window procedure for the hidden message window.
2101 * It handles callback messages and notifications from servers.
2102 * In order to process these messages, it is necessary to run a
2103 * message loop. Code which may run before the main message loop
2104 * is started (in the GUI) is careful to pump messages when it needs
2105 * to. Features which require message delivery during normal use will
2106 * not work in the console version - this basically means those
2107 * features which allow Vim to act as a server, rather than a client.
2108 */
2109 static LRESULT CALLBACK
2110Messaging_WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
2111{
2112 if (msg == WM_COPYDATA)
2113 {
2114 /* This is a message from another Vim. The dwData member of the
2115 * COPYDATASTRUCT determines the type of message:
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002116 * COPYDATA_ENCODING:
2117 * The encoding that the client uses. Following messages will
2118 * use this encoding, convert if needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 * COPYDATA_KEYS:
2120 * A key sequence. We are a server, and a client wants these keys
2121 * adding to the input queue.
2122 * COPYDATA_REPLY:
2123 * A reply. We are a client, and a server has sent this message
2124 * in response to a request. (server2client())
2125 * COPYDATA_EXPR:
2126 * An expression. We are a server, and a client wants us to
2127 * evaluate this expression.
2128 * COPYDATA_RESULT:
2129 * A reply. We are a client, and a server has sent this message
2130 * in response to a COPYDATA_EXPR.
2131 * COPYDATA_ERROR_RESULT:
2132 * A reply. We are a client, and a server has sent this message
2133 * in response to a COPYDATA_EXPR that failed to evaluate.
2134 */
2135 COPYDATASTRUCT *data = (COPYDATASTRUCT*)lParam;
2136 HWND sender = (HWND)wParam;
2137 COPYDATASTRUCT reply;
2138 char_u *res;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 int retval;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002140 char_u *str;
2141 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142
2143 switch (data->dwData)
2144 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002145 case COPYDATA_ENCODING:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002146# ifdef FEAT_MBYTE
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002147 /* Remember the encoding that the client uses. */
2148 vim_free(client_enc);
2149 client_enc = enc_canonize((char_u *)data->lpData);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002150# endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002151 return 1;
2152
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 case COPYDATA_KEYS:
2154 /* Remember who sent this, for <client> */
2155 clientWindow = sender;
2156
2157 /* Add the received keys to the input buffer. The loop waiting
2158 * for the user to do something should check the input buffer. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002159 str = serverConvert(client_enc, (char_u *)data->lpData, &tofree);
2160 server_to_input_buf(str);
2161 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162
2163# ifdef FEAT_GUI
2164 /* Wake up the main GUI loop. */
2165 if (s_hwnd != 0)
2166 PostMessage(s_hwnd, WM_NULL, 0, 0);
2167# endif
2168 return 1;
2169
2170 case COPYDATA_EXPR:
2171 /* Remember who sent this, for <client> */
2172 clientWindow = sender;
2173
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002174 str = serverConvert(client_enc, (char_u *)data->lpData, &tofree);
2175 res = eval_client_expr_to_string(str);
2176 vim_free(tofree);
2177
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 if (res == NULL)
2179 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002180 res = vim_strsave((char_u *)_(e_invexprmsg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 reply.dwData = COPYDATA_ERROR_RESULT;
2182 }
2183 else
2184 reply.dwData = COPYDATA_RESULT;
2185 reply.lpData = res;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002186 reply.cbData = (DWORD)STRLEN(res) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002188 serverSendEnc(sender);
Bram Moolenaar5246cd72013-06-16 16:41:47 +02002189 retval = (int)SendMessage(sender, WM_COPYDATA,
2190 (WPARAM)message_window, (LPARAM)(&reply));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 vim_free(res);
2192 return retval;
2193
2194 case COPYDATA_REPLY:
2195 case COPYDATA_RESULT:
2196 case COPYDATA_ERROR_RESULT:
2197 if (data->lpData != NULL)
2198 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002199 str = serverConvert(client_enc, (char_u *)data->lpData,
2200 &tofree);
2201 if (tofree == NULL)
2202 str = vim_strsave(str);
2203 if (save_reply(sender, str,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 (data->dwData == COPYDATA_REPLY ? 0 :
2205 (data->dwData == COPYDATA_RESULT ? 1 :
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002206 2))) == FAIL)
2207 vim_free(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208#ifdef FEAT_AUTOCMD
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002209 else if (data->dwData == COPYDATA_REPLY)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 {
Bram Moolenaar427d51c2013-06-16 16:01:25 +02002211 char_u winstr[30];
2212
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002213 sprintf((char *)winstr, PRINTF_HEX_LONG_U, (long_u)sender);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002214 apply_autocmds(EVENT_REMOTEREPLY, winstr, str,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 TRUE, curbuf);
2216 }
2217#endif
2218 }
2219 return 1;
2220 }
2221
2222 return 0;
2223 }
2224
2225 else if (msg == WM_ACTIVATE && wParam == WA_ACTIVE)
2226 {
2227 /* When the message window is activated (brought to the foreground),
2228 * this actually applies to the text window. */
2229#ifndef FEAT_GUI
2230 GetConsoleHwnd(); /* get value of s_hwnd */
2231#endif
2232 if (s_hwnd != 0)
2233 {
2234 SetForegroundWindow(s_hwnd);
2235 return 0;
2236 }
2237 }
2238
2239 return DefWindowProc(hwnd, msg, wParam, lParam);
2240}
2241
2242/*
2243 * Initialise the message handling process. This involves creating a window
2244 * to handle messages - the window will not be visible.
2245 */
2246 void
2247serverInitMessaging(void)
2248{
2249 WNDCLASS wndclass;
2250 HINSTANCE s_hinst;
2251
2252 /* Clean up on exit */
2253 atexit(CleanUpMessaging);
2254
2255 /* Register a window class - we only really care
2256 * about the window procedure
2257 */
2258 s_hinst = (HINSTANCE)GetModuleHandle(0);
2259 wndclass.style = 0;
2260 wndclass.lpfnWndProc = Messaging_WndProc;
2261 wndclass.cbClsExtra = 0;
2262 wndclass.cbWndExtra = 0;
2263 wndclass.hInstance = s_hinst;
2264 wndclass.hIcon = NULL;
2265 wndclass.hCursor = NULL;
2266 wndclass.hbrBackground = NULL;
2267 wndclass.lpszMenuName = NULL;
2268 wndclass.lpszClassName = VIM_CLASSNAME;
2269 RegisterClass(&wndclass);
2270
2271 /* Create the message window. It will be hidden, so the details don't
2272 * matter. Don't use WS_OVERLAPPEDWINDOW, it will make a shortcut remove
2273 * focus from gvim. */
2274 message_window = CreateWindow(VIM_CLASSNAME, "",
2275 WS_POPUPWINDOW | WS_CAPTION,
2276 CW_USEDEFAULT, CW_USEDEFAULT,
2277 100, 100, NULL, NULL,
2278 s_hinst, NULL);
2279}
2280
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002281/* Used by serverSendToVim() to find an alternate server name. */
2282static char_u *altname_buf_ptr = NULL;
2283
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284/*
2285 * Get the title of the window "hwnd", which is the Vim server name, in
2286 * "name[namelen]" and return the length.
2287 * Returns zero if window "hwnd" is not a Vim server.
2288 */
2289 static int
2290getVimServerName(HWND hwnd, char *name, int namelen)
2291{
2292 int len;
2293 char buffer[VIM_CLASSNAME_LEN + 1];
2294
2295 /* Ignore windows which aren't Vim message windows */
2296 len = GetClassName(hwnd, buffer, sizeof(buffer));
2297 if (len != VIM_CLASSNAME_LEN || STRCMP(buffer, VIM_CLASSNAME) != 0)
2298 return 0;
2299
2300 /* Get the title of the window */
2301 return GetWindowText(hwnd, name, namelen);
2302}
2303
2304 static BOOL CALLBACK
2305enumWindowsGetServer(HWND hwnd, LPARAM lparam)
2306{
2307 struct server_id *id = (struct server_id *)lparam;
2308 char server[MAX_PATH];
2309
2310 /* Get the title of the window */
2311 if (getVimServerName(hwnd, server, sizeof(server)) == 0)
2312 return TRUE;
2313
2314 /* If this is the server we're looking for, return its HWND */
2315 if (STRICMP(server, id->name) == 0)
2316 {
2317 id->hwnd = hwnd;
2318 return FALSE;
2319 }
2320
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002321 /* If we are looking for an alternate server, remember this name. */
2322 if (altname_buf_ptr != NULL
2323 && STRNICMP(server, id->name, STRLEN(id->name)) == 0
2324 && vim_isdigit(server[STRLEN(id->name)]))
2325 {
2326 STRCPY(altname_buf_ptr, server);
2327 altname_buf_ptr = NULL; /* don't use another name */
2328 }
2329
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 /* Otherwise, keep looking */
2331 return TRUE;
2332}
2333
2334 static BOOL CALLBACK
2335enumWindowsGetNames(HWND hwnd, LPARAM lparam)
2336{
2337 garray_T *ga = (garray_T *)lparam;
2338 char server[MAX_PATH];
2339
2340 /* Get the title of the window */
2341 if (getVimServerName(hwnd, server, sizeof(server)) == 0)
2342 return TRUE;
2343
2344 /* Add the name to the list */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002345 ga_concat(ga, (char_u *)server);
2346 ga_concat(ga, (char_u *)"\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 return TRUE;
2348}
2349
2350 static HWND
2351findServer(char_u *name)
2352{
2353 struct server_id id;
2354
2355 id.name = name;
2356 id.hwnd = 0;
2357
2358 EnumWindows(enumWindowsGetServer, (LPARAM)(&id));
2359
2360 return id.hwnd;
2361}
2362
2363 void
2364serverSetName(char_u *name)
2365{
2366 char_u *ok_name;
2367 HWND hwnd = 0;
2368 int i = 0;
2369 char_u *p;
2370
2371 /* Leave enough space for a 9-digit suffix to ensure uniqueness! */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002372 ok_name = alloc((unsigned)STRLEN(name) + 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373
2374 STRCPY(ok_name, name);
2375 p = ok_name + STRLEN(name);
2376
2377 for (;;)
2378 {
2379 /* This is inefficient - we're doing an EnumWindows loop for each
2380 * possible name. It would be better to grab all names in one go,
2381 * and scan the list each time...
2382 */
2383 hwnd = findServer(ok_name);
2384 if (hwnd == 0)
2385 break;
2386
2387 ++i;
2388 if (i >= 1000)
2389 break;
2390
2391 sprintf((char *)p, "%d", i);
2392 }
2393
2394 if (hwnd != 0)
2395 vim_free(ok_name);
2396 else
2397 {
2398 /* Remember the name */
2399 serverName = ok_name;
2400#ifdef FEAT_TITLE
2401 need_maketitle = TRUE; /* update Vim window title later */
2402#endif
2403
2404 /* Update the message window title */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01002405 SetWindowText(message_window, (LPCSTR)ok_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406
2407#ifdef FEAT_EVAL
2408 /* Set the servername variable */
2409 set_vim_var_string(VV_SEND_SERVER, serverName, -1);
2410#endif
2411 }
2412}
2413
2414 char_u *
2415serverGetVimNames(void)
2416{
2417 garray_T ga;
2418
2419 ga_init2(&ga, 1, 100);
2420
2421 EnumWindows(enumWindowsGetNames, (LPARAM)(&ga));
Bram Moolenaar269ec652004-07-29 08:43:53 +00002422 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423
2424 return ga.ga_data;
2425}
2426
2427 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002428serverSendReply(
2429 char_u *name, /* Where to send. */
2430 char_u *reply) /* What to send. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431{
2432 HWND target;
2433 COPYDATASTRUCT data;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002434 long_u n = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435
2436 /* The "name" argument is a magic cookie obtained from expand("<client>").
2437 * It should be of the form 0xXXXXX - i.e. a C hex literal, which is the
2438 * value of the client's message window HWND.
2439 */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002440 sscanf((char *)name, SCANF_HEX_LONG_U, &n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 if (n == 0)
2442 return -1;
2443
2444 target = (HWND)n;
2445 if (!IsWindow(target))
2446 return -1;
2447
2448 data.dwData = COPYDATA_REPLY;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002449 data.cbData = (DWORD)STRLEN(reply) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 data.lpData = reply;
2451
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002452 serverSendEnc(target);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 if (SendMessage(target, WM_COPYDATA, (WPARAM)message_window,
2454 (LPARAM)(&data)))
2455 return 0;
2456
2457 return -1;
2458}
2459
2460 int
Bram Moolenaar05540972016-01-30 20:31:25 +01002461serverSendToVim(
2462 char_u *name, /* Where to send. */
2463 char_u *cmd, /* What to send. */
2464 char_u **result, /* Result of eval'ed expression */
2465 void *ptarget, /* HWND of server */
2466 int asExpr, /* Expression or keys? */
2467 int silent) /* don't complain about no server */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468{
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002469 HWND target;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 COPYDATASTRUCT data;
2471 char_u *retval = NULL;
2472 int retcode = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002473 char_u altname_buf[MAX_PATH];
2474
2475 /* If the server name does not end in a digit then we look for an
2476 * alternate name. e.g. when "name" is GVIM the we may find GVIM2. */
2477 if (STRLEN(name) > 1 && !vim_isdigit(name[STRLEN(name) - 1]))
2478 altname_buf_ptr = altname_buf;
2479 altname_buf[0] = NUL;
2480 target = findServer(name);
2481 altname_buf_ptr = NULL;
2482 if (target == 0 && altname_buf[0] != NUL)
2483 /* Use another server name we found. */
2484 target = findServer(altname_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485
2486 if (target == 0)
2487 {
2488 if (!silent)
2489 EMSG2(_(e_noserver), name);
2490 return -1;
2491 }
2492
2493 if (ptarget)
2494 *(HWND *)ptarget = target;
2495
2496 data.dwData = asExpr ? COPYDATA_EXPR : COPYDATA_KEYS;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002497 data.cbData = (DWORD)STRLEN(cmd) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 data.lpData = cmd;
2499
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002500 serverSendEnc(target);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 if (SendMessage(target, WM_COPYDATA, (WPARAM)message_window,
2502 (LPARAM)(&data)) == 0)
2503 return -1;
2504
2505 if (asExpr)
2506 retval = serverGetReply(target, &retcode, TRUE, TRUE);
2507
2508 if (result == NULL)
2509 vim_free(retval);
2510 else
2511 *result = retval; /* Caller assumes responsibility for freeing */
2512
2513 return retcode;
2514}
2515
2516/*
2517 * Bring the server to the foreground.
2518 */
2519 void
Bram Moolenaar05540972016-01-30 20:31:25 +01002520serverForeground(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521{
2522 HWND target = findServer(name);
2523
2524 if (target != 0)
2525 SetForegroundWindow(target);
2526}
2527
2528/* Replies from server need to be stored until the client picks them up via
2529 * remote_read(). So we maintain a list of server-id/reply pairs.
2530 * Note that there could be multiple replies from one server pending if the
2531 * client is slow picking them up.
2532 * We just store the replies in a simple list. When we remove an entry, we
2533 * move list entries down to fill the gap.
2534 * The server ID is simply the HWND.
2535 */
2536typedef struct
2537{
2538 HWND server; /* server window */
2539 char_u *reply; /* reply string */
2540 int expr_result; /* 0 for REPLY, 1 for RESULT 2 for error */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002541} reply_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542
2543static garray_T reply_list = {0, 0, sizeof(reply_T), 5, 0};
2544
2545#define REPLY_ITEM(i) ((reply_T *)(reply_list.ga_data) + (i))
2546#define REPLY_COUNT (reply_list.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547
2548/* Flag which is used to wait for a reply */
2549static int reply_received = 0;
2550
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002551/*
2552 * Store a reply. "reply" must be allocated memory (or NULL).
2553 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 static int
2555save_reply(HWND server, char_u *reply, int expr)
2556{
2557 reply_T *rep;
2558
2559 if (ga_grow(&reply_list, 1) == FAIL)
2560 return FAIL;
2561
2562 rep = REPLY_ITEM(REPLY_COUNT);
2563 rep->server = server;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002564 rep->reply = reply;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 rep->expr_result = expr;
2566 if (rep->reply == NULL)
2567 return FAIL;
2568
2569 ++REPLY_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 reply_received = 1;
2571 return OK;
2572}
2573
2574/*
2575 * Get a reply from server "server".
2576 * When "expr_res" is non NULL, get the result of an expression, otherwise a
2577 * server2client() message.
2578 * When non NULL, point to return code. 0 => OK, -1 => ERROR
2579 * If "remove" is TRUE, consume the message, the caller must free it then.
2580 * if "wait" is TRUE block until a message arrives (or the server exits).
2581 */
2582 char_u *
2583serverGetReply(HWND server, int *expr_res, int remove, int wait)
2584{
2585 int i;
2586 char_u *reply;
2587 reply_T *rep;
2588
2589 /* When waiting, loop until the message waiting for is received. */
2590 for (;;)
2591 {
2592 /* Reset this here, in case a message arrives while we are going
2593 * through the already received messages. */
2594 reply_received = 0;
2595
2596 for (i = 0; i < REPLY_COUNT; ++i)
2597 {
2598 rep = REPLY_ITEM(i);
2599 if (rep->server == server
2600 && ((rep->expr_result != 0) == (expr_res != NULL)))
2601 {
2602 /* Save the values we've found for later */
2603 reply = rep->reply;
2604 if (expr_res != NULL)
2605 *expr_res = rep->expr_result == 1 ? 0 : -1;
2606
2607 if (remove)
2608 {
2609 /* Move the rest of the list down to fill the gap */
2610 mch_memmove(rep, rep + 1,
2611 (REPLY_COUNT - i - 1) * sizeof(reply_T));
2612 --REPLY_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 }
2614
2615 /* Return the reply to the caller, who takes on responsibility
2616 * for freeing it if "remove" is TRUE. */
2617 return reply;
2618 }
2619 }
2620
2621 /* If we got here, we didn't find a reply. Return immediately if the
2622 * "wait" parameter isn't set. */
2623 if (!wait)
2624 break;
2625
2626 /* We need to wait for a reply. Enter a message loop until the
2627 * "reply_received" flag gets set. */
2628
2629 /* Loop until we receive a reply */
2630 while (reply_received == 0)
2631 {
2632 /* Wait for a SendMessage() call to us. This could be the reply
2633 * we are waiting for. Use a timeout of a second, to catch the
2634 * situation that the server died unexpectedly. */
2635 MsgWaitForMultipleObjects(0, NULL, TRUE, 1000, QS_ALLINPUT);
2636
2637 /* If the server has died, give up */
2638 if (!IsWindow(server))
2639 return NULL;
2640
2641 serverProcessPendingMessages();
2642 }
2643 }
2644
2645 return NULL;
2646}
2647
2648/*
2649 * Process any messages in the Windows message queue.
2650 */
2651 void
2652serverProcessPendingMessages(void)
2653{
2654 MSG msg;
2655
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02002656 while (pPeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 {
2658 TranslateMessage(&msg);
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02002659 pDispatchMessage(&msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 }
2661}
2662
2663#endif /* FEAT_CLIENTSERVER */
2664
2665#if defined(FEAT_GUI) || (defined(FEAT_PRINTER) && !defined(FEAT_POSTSCRIPT)) \
2666 || defined(PROTO)
2667
2668struct charset_pair
2669{
2670 char *name;
2671 BYTE charset;
2672};
2673
2674static struct charset_pair
2675charset_pairs[] =
2676{
2677 {"ANSI", ANSI_CHARSET},
2678 {"CHINESEBIG5", CHINESEBIG5_CHARSET},
2679 {"DEFAULT", DEFAULT_CHARSET},
2680 {"HANGEUL", HANGEUL_CHARSET},
2681 {"OEM", OEM_CHARSET},
2682 {"SHIFTJIS", SHIFTJIS_CHARSET},
2683 {"SYMBOL", SYMBOL_CHARSET},
2684#ifdef WIN3264
2685 {"ARABIC", ARABIC_CHARSET},
2686 {"BALTIC", BALTIC_CHARSET},
2687 {"EASTEUROPE", EASTEUROPE_CHARSET},
2688 {"GB2312", GB2312_CHARSET},
2689 {"GREEK", GREEK_CHARSET},
2690 {"HEBREW", HEBREW_CHARSET},
2691 {"JOHAB", JOHAB_CHARSET},
2692 {"MAC", MAC_CHARSET},
2693 {"RUSSIAN", RUSSIAN_CHARSET},
2694 {"THAI", THAI_CHARSET},
2695 {"TURKISH", TURKISH_CHARSET},
2696# if (!defined(_MSC_VER) || (_MSC_VER > 1010)) \
2697 && (!defined(__BORLANDC__) || (__BORLANDC__ > 0x0500))
2698 {"VIETNAMESE", VIETNAMESE_CHARSET},
2699# endif
2700#endif
2701 {NULL, 0}
2702};
2703
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002704struct quality_pair
2705{
2706 char *name;
2707 DWORD quality;
2708};
2709
2710static struct quality_pair
2711quality_pairs[] = {
2712#ifdef CLEARTYPE_QUALITY
2713 {"CLEARTYPE", CLEARTYPE_QUALITY},
2714#endif
2715#ifdef ANTIALIASED_QUALITY
2716 {"ANTIALIASED", ANTIALIASED_QUALITY},
2717#endif
Bram Moolenaar73a733e2016-05-11 21:05:05 +02002718#ifdef NONANTIALIASED_QUALITY
2719 {"NONANTIALIASED", NONANTIALIASED_QUALITY},
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002720#endif
2721#ifdef PROOF_QUALITY
2722 {"PROOF", PROOF_QUALITY},
2723#endif
Bram Moolenaar4c9ce052016-04-04 21:06:19 +02002724#ifdef DRAFT_QUALITY
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002725 {"DRAFT", DRAFT_QUALITY},
2726#endif
2727 {"DEFAULT", DEFAULT_QUALITY},
2728 {NULL, 0}
2729};
2730
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731/*
2732 * Convert a charset ID to a name.
2733 * Return NULL when not recognized.
2734 */
2735 char *
2736charset_id2name(int id)
2737{
2738 struct charset_pair *cp;
2739
2740 for (cp = charset_pairs; cp->name != NULL; ++cp)
2741 if ((BYTE)id == cp->charset)
2742 break;
2743 return cp->name;
2744}
2745
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02002746/*
2747 * Convert a quality ID to a name.
2748 * Return NULL when not recognized.
2749 */
2750 char *
2751quality_id2name(DWORD id)
2752{
2753 struct quality_pair *qp;
2754
2755 for (qp = quality_pairs; qp->name != NULL; ++qp)
2756 if (id == qp->quality)
2757 break;
2758 return qp->name;
2759}
2760
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761static const LOGFONT s_lfDefault =
2762{
2763 -12, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, DEFAULT_CHARSET,
2764 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
2765 PROOF_QUALITY, FIXED_PITCH | FF_DONTCARE,
2766 "Fixedsys" /* see _ReadVimIni */
2767};
2768
2769/* Initialise the "current height" to -12 (same as s_lfDefault) just
2770 * in case the user specifies a font in "guifont" with no size before a font
2771 * with an explicit size has been set. This defaults the size to this value
2772 * (-12 equates to roughly 9pt).
2773 */
2774int current_font_height = -12; /* also used in gui_w48.c */
2775
2776/* Convert a string representing a point size into pixels. The string should
2777 * be a positive decimal number, with an optional decimal point (eg, "12", or
2778 * "10.5"). The pixel value is returned, and a pointer to the next unconverted
2779 * character is stored in *end. The flag "vertical" says whether this
2780 * calculation is for a vertical (height) size or a horizontal (width) one.
2781 */
2782 static int
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002783points_to_pixels(char_u *str, char_u **end, int vertical, long_i pprinter_dc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784{
2785 int pixels;
2786 int points = 0;
2787 int divisor = 0;
2788 HWND hwnd = (HWND)0;
2789 HDC hdc;
2790 HDC printer_dc = (HDC)pprinter_dc;
2791
2792 while (*str != NUL)
2793 {
2794 if (*str == '.' && divisor == 0)
2795 {
2796 /* Start keeping a divisor, for later */
2797 divisor = 1;
2798 }
2799 else
2800 {
2801 if (!VIM_ISDIGIT(*str))
2802 break;
2803
2804 points *= 10;
2805 points += *str - '0';
2806 divisor *= 10;
2807 }
2808 ++str;
2809 }
2810
2811 if (divisor == 0)
2812 divisor = 1;
2813
2814 if (printer_dc == NULL)
2815 {
2816 hwnd = GetDesktopWindow();
2817 hdc = GetWindowDC(hwnd);
2818 }
2819 else
2820 hdc = printer_dc;
2821
2822 pixels = MulDiv(points,
2823 GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX),
2824 72 * divisor);
2825
2826 if (printer_dc == NULL)
2827 ReleaseDC(hwnd, hdc);
2828
2829 *end = str;
2830 return pixels;
2831}
2832
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002833/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 static int CALLBACK
2835font_enumproc(
2836 ENUMLOGFONT *elf,
2837 NEWTEXTMETRIC *ntm,
2838 int type,
2839 LPARAM lparam)
2840{
2841 /* Return value:
2842 * 0 = terminate now (monospace & ANSI)
2843 * 1 = continue, still no luck...
2844 * 2 = continue, but we have an acceptable LOGFONT
2845 * (monospace, not ANSI)
2846 * We use these values, as EnumFontFamilies returns 1 if the
2847 * callback function is never called. So, we check the return as
2848 * 0 = perfect, 2 = OK, 1 = no good...
2849 * It's not pretty, but it works!
2850 */
2851
2852 LOGFONT *lf = (LOGFONT *)(lparam);
2853
2854#ifndef FEAT_PROPORTIONAL_FONTS
2855 /* Ignore non-monospace fonts without further ado */
2856 if ((ntm->tmPitchAndFamily & 1) != 0)
2857 return 1;
2858#endif
2859
2860 /* Remember this LOGFONT as a "possible" */
2861 *lf = elf->elfLogFont;
2862
2863 /* Terminate the scan as soon as we find an ANSI font */
2864 if (lf->lfCharSet == ANSI_CHARSET
2865 || lf->lfCharSet == OEM_CHARSET
2866 || lf->lfCharSet == DEFAULT_CHARSET)
2867 return 0;
2868
2869 /* Continue the scan - we have a non-ANSI font */
2870 return 2;
2871}
2872
2873 static int
2874init_logfont(LOGFONT *lf)
2875{
2876 int n;
2877 HWND hwnd = GetDesktopWindow();
2878 HDC hdc = GetWindowDC(hwnd);
2879
2880 n = EnumFontFamilies(hdc,
2881 (LPCSTR)lf->lfFaceName,
2882 (FONTENUMPROC)font_enumproc,
2883 (LPARAM)lf);
2884
2885 ReleaseDC(hwnd, hdc);
2886
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002887 /* If we couldn't find a usable font, return failure */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 if (n == 1)
2889 return FAIL;
2890
2891 /* Tidy up the rest of the LOGFONT structure. We set to a basic
2892 * font - get_logfont() sets bold, italic, etc based on the user's
2893 * input.
2894 */
2895 lf->lfHeight = current_font_height;
2896 lf->lfWidth = 0;
2897 lf->lfItalic = FALSE;
2898 lf->lfUnderline = FALSE;
2899 lf->lfStrikeOut = FALSE;
2900 lf->lfWeight = FW_NORMAL;
2901
2902 /* Return success */
2903 return OK;
2904}
2905
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002906/*
2907 * Get font info from "name" into logfont "lf".
2908 * Return OK for a valid name, FAIL otherwise.
2909 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 int
2911get_logfont(
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002912 LOGFONT *lf,
2913 char_u *name,
2914 HDC printer_dc,
2915 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916{
2917 char_u *p;
2918 int i;
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002919 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 static LOGFONT *lastlf = NULL;
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002921#ifdef FEAT_MBYTE
2922 char_u *acpname = NULL;
2923#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924
2925 *lf = s_lfDefault;
2926 if (name == NULL)
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002927 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002929#ifdef FEAT_MBYTE
2930 /* Convert 'name' from 'encoding' to the current codepage, because
2931 * lf->lfFaceName uses the current codepage.
2932 * TODO: Use Wide APIs instead of ANSI APIs. */
2933 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
2934 {
2935 int len;
Bram Moolenaar418f81b2016-02-16 20:12:02 +01002936 enc_to_acp(name, (int)STRLEN(name), &acpname, &len);
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002937 name = acpname;
2938 }
2939#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 if (STRCMP(name, "*") == 0)
2941 {
2942#if defined(FEAT_GUI_W32)
2943 CHOOSEFONT cf;
2944 /* if name is "*", bring up std font dialog: */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02002945 vim_memset(&cf, 0, sizeof(cf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 cf.lStructSize = sizeof(cf);
2947 cf.hwndOwner = s_hwnd;
2948 cf.Flags = CF_SCREENFONTS | CF_FIXEDPITCHONLY | CF_INITTOLOGFONTSTRUCT;
2949 if (lastlf != NULL)
2950 *lf = *lastlf;
2951 cf.lpLogFont = lf;
2952 cf.nFontType = 0 ; //REGULAR_FONTTYPE;
2953 if (ChooseFont(&cf))
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002954 ret = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955#endif
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002956 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 }
2958
2959 /*
2960 * Split name up, it could be <name>:h<height>:w<width> etc.
2961 */
2962 for (p = name; *p && *p != ':'; p++)
2963 {
2964 if (p - name + 1 > LF_FACESIZE)
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002965 goto theend; /* Name too long */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966 lf->lfFaceName[p - name] = *p;
2967 }
2968 if (p != name)
2969 lf->lfFaceName[p - name] = NUL;
2970
2971 /* First set defaults */
2972 lf->lfHeight = -12;
2973 lf->lfWidth = 0;
2974 lf->lfWeight = FW_NORMAL;
2975 lf->lfItalic = FALSE;
2976 lf->lfUnderline = FALSE;
2977 lf->lfStrikeOut = FALSE;
2978
2979 /*
2980 * If the font can't be found, try replacing '_' by ' '.
2981 */
2982 if (init_logfont(lf) == FAIL)
2983 {
2984 int did_replace = FALSE;
2985
2986 for (i = 0; lf->lfFaceName[i]; ++i)
2987 if (lf->lfFaceName[i] == '_')
2988 {
2989 lf->lfFaceName[i] = ' ';
2990 did_replace = TRUE;
2991 }
2992 if (!did_replace || init_logfont(lf) == FAIL)
Bram Moolenaarb1692e22014-03-12 19:24:37 +01002993 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994 }
2995
2996 while (*p == ':')
2997 p++;
2998
2999 /* Set the values found after ':' */
3000 while (*p)
3001 {
3002 switch (*p++)
3003 {
3004 case 'h':
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003005 lf->lfHeight = - points_to_pixels(p, &p, TRUE, (long_i)printer_dc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006 break;
3007 case 'w':
Bram Moolenaareb3593b2006-04-22 22:33:57 +00003008 lf->lfWidth = points_to_pixels(p, &p, FALSE, (long_i)printer_dc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009 break;
3010 case 'b':
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011 lf->lfWeight = FW_BOLD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 break;
3013 case 'i':
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014 lf->lfItalic = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015 break;
3016 case 'u':
3017 lf->lfUnderline = TRUE;
3018 break;
3019 case 's':
3020 lf->lfStrikeOut = TRUE;
3021 break;
3022 case 'c':
3023 {
3024 struct charset_pair *cp;
3025
3026 for (cp = charset_pairs; cp->name != NULL; ++cp)
3027 if (STRNCMP(p, cp->name, strlen(cp->name)) == 0)
3028 {
3029 lf->lfCharSet = cp->charset;
3030 p += strlen(cp->name);
3031 break;
3032 }
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003033 if (cp->name == NULL && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034 {
Bram Moolenaar051b7822005-05-19 21:00:46 +00003035 vim_snprintf((char *)IObuff, IOSIZE,
3036 _("E244: Illegal charset name \"%s\" in font name \"%s\""), p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037 EMSG(IObuff);
3038 break;
3039 }
3040 break;
3041 }
Bram Moolenaar7c1c6db2016-04-03 22:08:05 +02003042 case 'q':
3043 {
3044 struct quality_pair *qp;
3045
3046 for (qp = quality_pairs; qp->name != NULL; ++qp)
3047 if (STRNCMP(p, qp->name, strlen(qp->name)) == 0)
3048 {
3049 lf->lfQuality = qp->quality;
3050 p += strlen(qp->name);
3051 break;
3052 }
3053 if (qp->name == NULL && verbose)
3054 {
3055 vim_snprintf((char *)IObuff, IOSIZE,
3056 _("E244: Illegal quality name \"%s\" in font name \"%s\""), p, name);
3057 EMSG(IObuff);
3058 break;
3059 }
3060 break;
3061 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062 default:
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003063 if (verbose)
3064 {
Bram Moolenaar051b7822005-05-19 21:00:46 +00003065 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003066 _("E245: Illegal char '%c' in font name \"%s\""),
3067 p[-1], name);
3068 EMSG(IObuff);
3069 }
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003070 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 }
3072 while (*p == ':')
3073 p++;
3074 }
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003075 ret = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 /* ron: init lastlf */
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003079 if (ret == OK && printer_dc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080 {
3081 vim_free(lastlf);
3082 lastlf = (LOGFONT *)alloc(sizeof(LOGFONT));
3083 if (lastlf != NULL)
3084 mch_memmove(lastlf, lf, sizeof(LOGFONT));
3085 }
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003086#ifdef FEAT_MBYTE
3087 vim_free(acpname);
3088#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089
Bram Moolenaarb1692e22014-03-12 19:24:37 +01003090 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091}
3092
3093#endif /* defined(FEAT_GUI) || defined(FEAT_PRINTER) */
Bram Moolenaarf12d9832016-01-29 21:11:25 +01003094
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003095#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaarf12d9832016-01-29 21:11:25 +01003096/*
3097 * Initialize the Winsock dll.
3098 */
3099 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003100channel_init_winsock(void)
Bram Moolenaarf12d9832016-01-29 21:11:25 +01003101{
3102 WSADATA wsaData;
3103 int wsaerr;
3104
3105 if (WSInitialized)
3106 return;
3107
3108 wsaerr = WSAStartup(MAKEWORD(2, 2), &wsaData);
3109 if (wsaerr == 0)
3110 WSInitialized = TRUE;
3111}
3112#endif