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