blob: 95c3d17380333be571ca0b0c8a2d1949cf413911 [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
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000280
281#if defined(FEAT_MBYTE) && !defined(FEAT_GUI)
282 (void)get_cmd_argsW(NULL);
283#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284}
285
286
287/*
288 * Return TRUE if the input comes from a terminal, FALSE otherwise.
289 */
290 int
291mch_input_isatty()
292{
293#ifdef FEAT_GUI_MSWIN
294 return OK; /* GUI always has a tty */
295#else
296 if (isatty(read_cmd_fd))
297 return TRUE;
298 return FALSE;
299#endif
300}
301
302#ifdef FEAT_TITLE
303/*
304 * mch_settitle(): set titlebar of our window
305 */
306 void
307mch_settitle(
308 char_u *title,
309 char_u *icon)
310{
311# ifdef FEAT_GUI_MSWIN
312 gui_mch_settitle(title, icon);
313# else
314 if (title != NULL)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000315 {
316# ifdef FEAT_MBYTE
317 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
318 {
319 /* Convert the title from 'encoding' to the active codepage. */
Bram Moolenaar36f692d2008-11-20 16:10:17 +0000320 WCHAR *wp = enc_to_utf16(title, NULL);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000321 int n;
322
323 if (wp != NULL)
324 {
325 n = SetConsoleTitleW(wp);
326 vim_free(wp);
327 if (n != 0 || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
328 return;
329 }
330 }
331# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000332 SetConsoleTitle(title);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000333 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334# endif
335}
336
337
338/*
339 * Restore the window/icon title.
340 * which is one of:
341 * 1: Just restore title
342 * 2: Just restore icon (which we don't have)
343 * 3: Restore title and icon (which we don't have)
344 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000345/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346 void
347mch_restore_title(
348 int which)
349{
350#ifndef FEAT_GUI_MSWIN
351 mch_settitle((which & 1) ? g_szOrigTitle : NULL, NULL);
352#endif
353}
354
355
356/*
357 * Return TRUE if we can restore the title (we can)
358 */
359 int
360mch_can_restore_title()
361{
362 return TRUE;
363}
364
365
366/*
367 * Return TRUE if we can restore the icon title (we can't)
368 */
369 int
370mch_can_restore_icon()
371{
372 return FALSE;
373}
374#endif /* FEAT_TITLE */
375
376
377/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000378 * Get absolute file name into buffer "buf" of length "len" bytes,
379 * turning all '/'s into '\\'s and getting the correct case of each component
380 * of the file name. Append a (back)slash to a directory name.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381 * When 'shellslash' set do it the other way around.
382 * Return OK or FAIL.
383 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000384/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385 int
386mch_FullName(
387 char_u *fname,
388 char_u *buf,
389 int len,
390 int force)
391{
392 int nResult = FAIL;
393
394#ifdef __BORLANDC__
395 if (*fname == NUL) /* Borland behaves badly here - make it consistent */
396 nResult = mch_dirname(buf, len);
397 else
398#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000400#ifdef FEAT_MBYTE
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000401 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage
402# ifdef __BORLANDC__
403 /* Wide functions of Borland C 5.5 do not work on Windows 98. */
404 && g_PlatformId == VER_PLATFORM_WIN32_NT
405# endif
406 )
407 {
408 WCHAR *wname;
409 WCHAR wbuf[MAX_PATH];
410 char_u *cname = NULL;
411
412 /* Use the wide function:
413 * - convert the fname from 'encoding' to UCS2.
414 * - invoke _wfullpath()
415 * - convert the result from UCS2 to 'encoding'.
416 */
Bram Moolenaar36f692d2008-11-20 16:10:17 +0000417 wname = enc_to_utf16(fname, NULL);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000418 if (wname != NULL && _wfullpath(wbuf, wname, MAX_PATH - 1) != NULL)
419 {
Bram Moolenaar36f692d2008-11-20 16:10:17 +0000420 cname = utf16_to_enc((short_u *)wbuf, NULL);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000421 if (cname != NULL)
422 {
Bram Moolenaarfe3ca8d2005-07-18 21:43:02 +0000423 vim_strncpy(buf, cname, len - 1);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000424 nResult = OK;
425 }
426 }
427 vim_free(wname);
428 vim_free(cname);
429 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000430 if (nResult == FAIL) /* fall back to non-wide function */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000432 {
433 if (_fullpath(buf, fname, len - 1) == NULL)
434 {
Bram Moolenaarfe3ca8d2005-07-18 21:43:02 +0000435 /* failed, use relative path name */
436 vim_strncpy(buf, fname, len - 1);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000437 }
438 else
439 nResult = OK;
440 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000442
443#ifdef USE_FNAME_CASE
444 fname_case(buf, len);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000445#else
446 slash_adjust(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000447#endif
448
449 return nResult;
450}
451
452
453/*
454 * Return TRUE if "fname" does not depend on the current directory.
455 */
456 int
457mch_isFullName(char_u *fname)
458{
Bram Moolenaard2a203b2013-08-30 16:51:18 +0200459#ifdef FEAT_MBYTE
460 /* WinNT and later can use _MAX_PATH wide characters for a pathname, which
461 * means that the maximum pathname is _MAX_PATH * 3 bytes when 'enc' is
462 * UTF-8. */
463 char szName[_MAX_PATH * 3 + 1];
464#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465 char szName[_MAX_PATH + 1];
Bram Moolenaard2a203b2013-08-30 16:51:18 +0200466#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467
468 /* A name like "d:/foo" and "//server/share" is absolute */
469 if ((fname[0] && fname[1] == ':' && (fname[2] == '/' || fname[2] == '\\'))
470 || (fname[0] == fname[1] && (fname[0] == '/' || fname[0] == '\\')))
471 return TRUE;
472
473 /* A name that can't be made absolute probably isn't absolute. */
Bram Moolenaard2a203b2013-08-30 16:51:18 +0200474 if (mch_FullName(fname, szName, sizeof(szName) - 1, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475 return FALSE;
476
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000477 return pathcmp(fname, szName, -1) == 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000478}
479
480/*
481 * Replace all slashes by backslashes.
482 * This used to be the other way around, but MS-DOS sometimes has problems
483 * with slashes (e.g. in a command name). We can't have mixed slashes and
484 * backslashes, because comparing file names will not work correctly. The
485 * commands that use a file name should try to avoid the need to type a
486 * backslash twice.
487 * When 'shellslash' set do it the other way around.
488 */
489 void
490slash_adjust(p)
491 char_u *p;
492{
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +0000493 while (*p)
494 {
495 if (*p == psepcN)
496 *p = psepc;
497 mb_ptr_adv(p);
498 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499}
500
Bram Moolenaar8962fda2013-09-29 19:05:21 +0200501#if (_MSC_VER >= 1300)
502# define OPEN_OH_ARGTYPE intptr_t
503#else
504# define OPEN_OH_ARGTYPE long
505#endif
506
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200507 static int
508stat_symlink_aware(const char *name, struct stat *stp)
509{
510#if defined(_MSC_VER) && _MSC_VER < 1700
511 /* Work around for VC10 or earlier. stat() can't handle symlinks properly.
512 * VC9 or earlier: stat() doesn't support a symlink at all. It retrieves
513 * status of a symlink itself.
514 * VC10: stat() supports a symlink to a normal file, but it doesn't support
515 * a symlink to a directory (always returns an error). */
516 WIN32_FIND_DATA findData;
517 HANDLE hFind, h;
518 DWORD attr = 0;
519 BOOL is_symlink = FALSE;
520
521 hFind = FindFirstFile(name, &findData);
522 if (hFind != INVALID_HANDLE_VALUE)
523 {
524 attr = findData.dwFileAttributes;
525 if ((attr & FILE_ATTRIBUTE_REPARSE_POINT)
526 && (findData.dwReserved0 == IO_REPARSE_TAG_SYMLINK))
527 is_symlink = TRUE;
528 FindClose(hFind);
529 }
530 if (is_symlink)
531 {
532 h = CreateFile(name, FILE_READ_ATTRIBUTES,
533 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
534 OPEN_EXISTING,
535 (attr & FILE_ATTRIBUTE_DIRECTORY)
536 ? FILE_FLAG_BACKUP_SEMANTICS : 0,
537 NULL);
538 if (h != INVALID_HANDLE_VALUE)
539 {
540 int fd, n;
541
Bram Moolenaar8962fda2013-09-29 19:05:21 +0200542 fd = _open_osfhandle((OPEN_OH_ARGTYPE)h, _O_RDONLY);
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200543 n = _fstat(fd, (struct _stat*)stp);
544 _close(fd);
545 return n;
546 }
547 }
548#endif
549 return stat(name, stp);
550}
551
552#ifdef FEAT_MBYTE
553 static int
554wstat_symlink_aware(const WCHAR *name, struct _stat *stp)
555{
556# if defined(_MSC_VER) && _MSC_VER < 1700
557 /* Work around for VC10 or earlier. _wstat() can't handle symlinks properly.
558 * VC9 or earlier: _wstat() doesn't support a symlink at all. It retrieves
559 * status of a symlink itself.
560 * VC10: _wstat() supports a symlink to a normal file, but it doesn't
561 * support a symlink to a directory (always returns an error). */
562 int n;
563 BOOL is_symlink = FALSE;
564 HANDLE hFind, h;
565 DWORD attr = 0;
566 WIN32_FIND_DATAW findDataW;
567
568 hFind = FindFirstFileW(name, &findDataW);
569 if (hFind != INVALID_HANDLE_VALUE)
570 {
571 attr = findDataW.dwFileAttributes;
572 if ((attr & FILE_ATTRIBUTE_REPARSE_POINT)
573 && (findDataW.dwReserved0 == IO_REPARSE_TAG_SYMLINK))
574 is_symlink = TRUE;
575 FindClose(hFind);
576 }
577 if (is_symlink)
578 {
579 h = CreateFileW(name, FILE_READ_ATTRIBUTES,
580 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
581 OPEN_EXISTING,
582 (attr & FILE_ATTRIBUTE_DIRECTORY)
583 ? FILE_FLAG_BACKUP_SEMANTICS : 0,
584 NULL);
585 if (h != INVALID_HANDLE_VALUE)
586 {
587 int fd;
588
Bram Moolenaar8962fda2013-09-29 19:05:21 +0200589 fd = _open_osfhandle((OPEN_OH_ARGTYPE)h, _O_RDONLY);
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200590 n = _fstat(fd, stp);
591 _close(fd);
592 return n;
593 }
594 }
595# endif
596 return _wstat(name, stp);
597}
598#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599
600/*
601 * stat() can't handle a trailing '/' or '\', remove it first.
602 */
603 int
604vim_stat(const char *name, struct stat *stp)
605{
Bram Moolenaard2a203b2013-08-30 16:51:18 +0200606#ifdef FEAT_MBYTE
607 /* WinNT and later can use _MAX_PATH wide characters for a pathname, which
608 * means that the maximum pathname is _MAX_PATH * 3 bytes when 'enc' is
609 * UTF-8. */
610 char buf[_MAX_PATH * 3 + 1];
611#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612 char buf[_MAX_PATH + 1];
Bram Moolenaard2a203b2013-08-30 16:51:18 +0200613#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614 char *p;
615
Bram Moolenaard2a203b2013-08-30 16:51:18 +0200616 vim_strncpy((char_u *)buf, (char_u *)name, sizeof(buf) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617 p = buf + strlen(buf);
618 if (p > buf)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000619 mb_ptr_back(buf, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 if (p > buf && (*p == '\\' || *p == '/') && p[-1] != ':')
621 *p = NUL;
622#ifdef FEAT_MBYTE
623 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage
624# ifdef __BORLANDC__
625 /* Wide functions of Borland C 5.5 do not work on Windows 98. */
626 && g_PlatformId == VER_PLATFORM_WIN32_NT
627# endif
628 )
629 {
Bram Moolenaar36f692d2008-11-20 16:10:17 +0000630 WCHAR *wp = enc_to_utf16(buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631 int n;
632
633 if (wp != NULL)
634 {
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200635 n = wstat_symlink_aware(wp, (struct _stat *)stp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 vim_free(wp);
637 if (n >= 0)
638 return n;
639 /* Retry with non-wide function (for Windows 98). Can't use
640 * GetLastError() here and it's unclear what errno gets set to if
641 * the _wstat() fails for missing wide functions. */
642 }
643 }
644#endif
Bram Moolenaar2ee95f72013-09-25 19:13:38 +0200645 return stat_symlink_aware(buf, stp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646}
647
648#if defined(FEAT_GUI_MSWIN) || defined(PROTO)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000649/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 void
651mch_settmode(int tmode)
652{
653 /* nothing to do */
654}
655
656 int
657mch_get_shellsize(void)
658{
659 /* never used */
660 return OK;
661}
662
663 void
664mch_set_shellsize(void)
665{
666 /* never used */
667}
668
669/*
670 * Rows and/or Columns has changed.
671 */
672 void
673mch_new_shellsize(void)
674{
675 /* never used */
676}
677
678#endif
679
680/*
681 * We have no job control, so fake it by starting a new shell.
682 */
683 void
684mch_suspend()
685{
686 suspend_shell();
687}
688
689#if defined(USE_MCH_ERRMSG) || defined(PROTO)
690
691#ifdef display_errors
692# undef display_errors
693#endif
694
695/*
696 * Display the saved error message(s).
697 */
698 void
699display_errors()
700{
701 char *p;
702
703 if (error_ga.ga_data != NULL)
704 {
705 /* avoid putting up a message box with blanks only */
706 for (p = (char *)error_ga.ga_data; *p; ++p)
707 if (!isspace(*p))
708 {
Bram Moolenaarc17ef8e2006-03-25 21:48:58 +0000709 (void)gui_mch_dialog(
710#ifdef FEAT_GUI
711 gui.starting ? VIM_INFO :
712#endif
713 VIM_ERROR,
714#ifdef FEAT_GUI
715 gui.starting ? (char_u *)_("Message") :
716#endif
717 (char_u *)_("Error"),
Bram Moolenaard2c340a2011-01-17 20:08:11 +0100718 p, (char_u *)_("&Ok"), 1, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719 break;
720 }
721 ga_clear(&error_ga);
722 }
723}
724#endif
725
726
727/*
728 * Return TRUE if "p" contain a wildcard that can be expanded by
729 * dos_expandpath().
730 */
731 int
732mch_has_exp_wildcard(char_u *p)
733{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000734 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735 {
736 if (vim_strchr((char_u *)"?*[", *p) != NULL
737 || (*p == '~' && p[1] != NUL))
738 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 }
740 return FALSE;
741}
742
743/*
744 * Return TRUE if "p" contain a wildcard or a "~1" kind of thing (could be a
745 * shortened file name).
746 */
747 int
748mch_has_wildcard(char_u *p)
749{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000750 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 {
752 if (vim_strchr((char_u *)
753# ifdef VIM_BACKTICK
754 "?*$[`"
755# else
756 "?*$["
757# endif
758 , *p) != NULL
759 || (*p == '~' && p[1] != NUL))
760 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 }
762 return FALSE;
763}
764
765
766/*
767 * The normal _chdir() does not change the default drive. This one does.
768 * Returning 0 implies success; -1 implies failure.
769 */
770 int
771mch_chdir(char *path)
772{
773 if (path[0] == NUL) /* just checking... */
774 return -1;
775
Bram Moolenaara2974d72009-07-14 16:38:36 +0000776 if (p_verbose >= 5)
777 {
778 verbose_enter();
779 smsg((char_u *)"chdir(%s)", path);
780 verbose_leave();
781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 if (isalpha(path[0]) && path[1] == ':') /* has a drive name */
783 {
784 /* If we can change to the drive, skip that part of the path. If we
785 * can't then the current directory may be invalid, try using chdir()
786 * with the whole path. */
787 if (_chdrive(TOLOWER_ASC(path[0]) - 'a' + 1) == 0)
788 path += 2;
789 }
790
791 if (*path == NUL) /* drive name only */
792 return 0;
793
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000794#ifdef FEAT_MBYTE
795 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
796 {
Bram Moolenaar36f692d2008-11-20 16:10:17 +0000797 WCHAR *p = enc_to_utf16(path, NULL);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000798 int n;
799
800 if (p != NULL)
801 {
802 n = _wchdir(p);
803 vim_free(p);
804 if (n == 0)
805 return 0;
806 /* Retry with non-wide function (for Windows 98). */
807 }
808 }
809#endif
810
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 return chdir(path); /* let the normal chdir() do the rest */
812}
813
814
815/*
816 * Switching off termcap mode is only allowed when Columns is 80, otherwise a
817 * crash may result. It's always allowed on NT or when running the GUI.
818 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000819/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 int
821can_end_termcap_mode(
822 int give_msg)
823{
824#ifdef FEAT_GUI_MSWIN
825 return TRUE; /* GUI starts a new console anyway */
826#else
827 if (g_PlatformId == VER_PLATFORM_WIN32_NT || Columns == 80)
828 return TRUE;
829 if (give_msg)
830 msg(_("'columns' is not 80, cannot execute external commands"));
831 return FALSE;
832#endif
833}
834
835#ifdef FEAT_GUI_MSWIN
836/*
837 * return non-zero if a character is available
838 */
839 int
840mch_char_avail()
841{
842 /* never used */
843 return TRUE;
844}
845#endif
846
847
848/*
849 * set screen mode, always fails.
850 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000851/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 int
853mch_screenmode(
854 char_u *arg)
855{
856 EMSG(_(e_screenmode));
857 return FAIL;
858}
859
860
861#if defined(FEAT_LIBCALL) || defined(PROTO)
862/*
863 * Call a DLL routine which takes either a string or int param
864 * and returns an allocated string.
865 * Return OK if it worked, FAIL if not.
866 */
867# ifdef WIN3264
868typedef LPTSTR (*MYSTRPROCSTR)(LPTSTR);
869typedef LPTSTR (*MYINTPROCSTR)(int);
870typedef int (*MYSTRPROCINT)(LPTSTR);
871typedef int (*MYINTPROCINT)(int);
872# else
873typedef LPSTR (*MYSTRPROCSTR)(LPSTR);
874typedef LPSTR (*MYINTPROCSTR)(int);
875typedef int (*MYSTRPROCINT)(LPSTR);
876typedef int (*MYINTPROCINT)(int);
877# endif
878
879# ifndef WIN16
880/*
881 * Check if a pointer points to a valid NUL terminated string.
882 * Return the length of the string, including terminating NUL.
883 * Returns 0 for an invalid pointer, 1 for an empty string.
884 */
885 static size_t
886check_str_len(char_u *str)
887{
888 SYSTEM_INFO si;
889 MEMORY_BASIC_INFORMATION mbi;
890 size_t length = 0;
891 size_t i;
892 const char *p;
893
894 /* get page size */
895 GetSystemInfo(&si);
896
897 /* get memory information */
898 if (VirtualQuery(str, &mbi, sizeof(mbi)))
899 {
900 /* pre cast these (typing savers) */
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000901 long_u dwStr = (long_u)str;
902 long_u dwBaseAddress = (long_u)mbi.BaseAddress;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903
904 /* get start address of page that str is on */
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000905 long_u strPage = dwStr - (dwStr - dwBaseAddress) % si.dwPageSize;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906
907 /* get length from str to end of page */
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000908 long_u pageLength = si.dwPageSize - (dwStr - strPage);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909
Bram Moolenaar442b4222010-05-24 21:34:22 +0200910 for (p = str; !IsBadReadPtr(p, (UINT)pageLength);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 p += pageLength, pageLength = si.dwPageSize)
912 for (i = 0; i < pageLength; ++i, ++length)
913 if (p[i] == NUL)
914 return length + 1;
915 }
916
917 return 0;
918}
919# endif
920
921 int
922mch_libcall(
923 char_u *libname,
924 char_u *funcname,
925 char_u *argstring, /* NULL when using a argint */
926 int argint,
927 char_u **string_result,/* NULL when using number_result */
928 int *number_result)
929{
930 HINSTANCE hinstLib;
931 MYSTRPROCSTR ProcAdd;
932 MYINTPROCSTR ProcAddI;
933 char_u *retval_str = NULL;
934 int retval_int = 0;
935 size_t len;
936
937 BOOL fRunTimeLinkSuccess = FALSE;
938
939 // Get a handle to the DLL module.
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200940# ifdef WIN16
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 hinstLib = LoadLibrary(libname);
Bram Moolenaarebbcb822010-10-23 14:02:54 +0200942# else
943 hinstLib = vimLoadLib(libname);
944# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945
946 // If the handle is valid, try to get the function address.
947 if (hinstLib != NULL)
948 {
949#ifdef HAVE_TRY_EXCEPT
950 __try
951 {
952#endif
953 if (argstring != NULL)
954 {
955 /* Call with string argument */
956 ProcAdd = (MYSTRPROCSTR) GetProcAddress(hinstLib, funcname);
957 if ((fRunTimeLinkSuccess = (ProcAdd != NULL)) != 0)
958 {
959 if (string_result == NULL)
960 retval_int = ((MYSTRPROCINT)ProcAdd)(argstring);
961 else
962 retval_str = (ProcAdd)(argstring);
963 }
964 }
965 else
966 {
967 /* Call with number argument */
968 ProcAddI = (MYINTPROCSTR) GetProcAddress(hinstLib, funcname);
969 if ((fRunTimeLinkSuccess = (ProcAddI != NULL)) != 0)
970 {
971 if (string_result == NULL)
972 retval_int = ((MYINTPROCINT)ProcAddI)(argint);
973 else
974 retval_str = (ProcAddI)(argint);
975 }
976 }
977
978 // Save the string before we free the library.
979 // Assume that a "1" result is an illegal pointer.
980 if (string_result == NULL)
981 *number_result = retval_int;
982 else if (retval_str != NULL
983# ifdef WIN16
984 && retval_str != (char_u *)1
985 && retval_str != (char_u *)-1
986 && !IsBadStringPtr(retval_str, INT_MAX)
987 && (len = strlen(retval_str) + 1) > 0
988# else
989 && (len = check_str_len(retval_str)) > 0
990# endif
991 )
992 {
993 *string_result = lalloc((long_u)len, TRUE);
994 if (*string_result != NULL)
995 mch_memmove(*string_result, retval_str, len);
996 }
997
998#ifdef HAVE_TRY_EXCEPT
999 }
1000 __except(EXCEPTION_EXECUTE_HANDLER)
1001 {
1002 if (GetExceptionCode() == EXCEPTION_STACK_OVERFLOW)
1003 RESETSTKOFLW();
1004 fRunTimeLinkSuccess = 0;
1005 }
1006#endif
1007
1008 // Free the DLL module.
1009 (void)FreeLibrary(hinstLib);
1010 }
1011
1012 if (!fRunTimeLinkSuccess)
1013 {
1014 EMSG2(_(e_libcall), funcname);
1015 return FAIL;
1016 }
1017
1018 return OK;
1019}
1020#endif
1021
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022/*
1023 * Debugging helper: expose the MCH_WRITE_DUMP stuff to other modules
1024 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001025/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 void
1027DumpPutS(
1028 const char *psz)
1029{
1030# ifdef MCH_WRITE_DUMP
1031 if (fdDump)
1032 {
1033 fputs(psz, fdDump);
1034 if (psz[strlen(psz) - 1] != '\n')
1035 fputc('\n', fdDump);
1036 fflush(fdDump);
1037 }
1038# endif
1039}
1040
1041#ifdef _DEBUG
1042
1043void __cdecl
1044Trace(
1045 char *pszFormat,
1046 ...)
1047{
1048 CHAR szBuff[2048];
1049 va_list args;
1050
1051 va_start(args, pszFormat);
1052 vsprintf(szBuff, pszFormat, args);
1053 va_end(args);
1054
1055 OutputDebugString(szBuff);
1056}
1057
1058#endif //_DEBUG
1059
Bram Moolenaar843ee412004-06-30 16:16:41 +00001060#if !defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061# if defined(FEAT_TITLE) && defined(WIN3264)
1062extern HWND g_hWnd; /* This is in os_win32.c. */
1063# endif
1064
1065/*
1066 * Showing the printer dialog is tricky since we have no GUI
1067 * window to parent it. The following routines are needed to
1068 * get the window parenting and Z-order to work properly.
1069 */
1070 static void
1071GetConsoleHwnd(void)
1072{
1073# define MY_BUFSIZE 1024 // Buffer size for console window titles.
1074
1075 char pszNewWindowTitle[MY_BUFSIZE]; // Contains fabricated WindowTitle.
1076 char pszOldWindowTitle[MY_BUFSIZE]; // Contains original WindowTitle.
1077
1078 /* Skip if it's already set. */
1079 if (s_hwnd != 0)
1080 return;
1081
1082# if defined(FEAT_TITLE) && defined(WIN3264)
1083 /* Window handle may have been found by init code (Windows NT only) */
1084 if (g_hWnd != 0)
1085 {
1086 s_hwnd = g_hWnd;
1087 return;
1088 }
1089# endif
1090
1091 GetConsoleTitle(pszOldWindowTitle, MY_BUFSIZE);
1092
1093 wsprintf(pszNewWindowTitle, "%s/%d/%d",
1094 pszOldWindowTitle,
1095 GetTickCount(),
1096 GetCurrentProcessId());
1097 SetConsoleTitle(pszNewWindowTitle);
1098 Sleep(40);
1099 s_hwnd = FindWindow(NULL, pszNewWindowTitle);
1100
1101 SetConsoleTitle(pszOldWindowTitle);
1102}
Bram Moolenaar843ee412004-06-30 16:16:41 +00001103
1104/*
1105 * Console implementation of ":winpos".
1106 */
1107 int
1108mch_get_winpos(int *x, int *y)
1109{
1110 RECT rect;
1111
1112 GetConsoleHwnd();
1113 GetWindowRect(s_hwnd, &rect);
1114 *x = rect.left;
1115 *y = rect.top;
1116 return OK;
1117}
1118
1119/*
1120 * Console implementation of ":winpos x y".
1121 */
1122 void
1123mch_set_winpos(int x, int y)
1124{
1125 GetConsoleHwnd();
1126 SetWindowPos(s_hwnd, NULL, x, y, 0, 0,
1127 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
1128}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129#endif
1130
1131#if (defined(FEAT_PRINTER) && !defined(FEAT_POSTSCRIPT)) || defined(PROTO)
1132
1133# ifdef WIN16
1134# define TEXT(a) a
1135# endif
1136/*=================================================================
1137 * Win32 printer stuff
1138 */
1139
1140static HFONT prt_font_handles[2][2][2];
1141static PRINTDLG prt_dlg;
1142static const int boldface[2] = {FW_REGULAR, FW_BOLD};
1143static TEXTMETRIC prt_tm;
1144static int prt_line_height;
1145static int prt_number_width;
1146static int prt_left_margin;
1147static int prt_right_margin;
1148static int prt_top_margin;
1149static char_u szAppName[] = TEXT("VIM");
1150static HWND hDlgPrint;
1151static int *bUserAbort = NULL;
1152static char_u *prt_name = NULL;
1153
1154/* Defines which are also in vim.rc. */
1155#define IDC_BOX1 400
1156#define IDC_PRINTTEXT1 401
1157#define IDC_PRINTTEXT2 402
1158#define IDC_PROGRESS 403
1159
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001160#if !defined(FEAT_MBYTE) || defined(WIN16)
1161# define vimSetDlgItemText(h, i, s) SetDlgItemText(h, i, s)
1162#else
1163 static BOOL
1164vimSetDlgItemText(HWND hDlg, int nIDDlgItem, char_u *s)
1165{
1166 WCHAR *wp = NULL;
1167 BOOL ret;
1168
1169 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
1170 {
1171 wp = enc_to_utf16(s, NULL);
1172 }
1173 if (wp != NULL)
1174 {
1175 ret = SetDlgItemTextW(hDlg, nIDDlgItem, wp);
1176 vim_free(wp);
1177 return ret;
1178 }
1179 return SetDlgItemText(hDlg, nIDDlgItem, s);
1180}
1181#endif
1182
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183/*
1184 * Convert BGR to RGB for Windows GDI calls
1185 */
1186 static COLORREF
1187swap_me(COLORREF colorref)
1188{
1189 int temp;
1190 char *ptr = (char *)&colorref;
1191
1192 temp = *(ptr);
1193 *(ptr ) = *(ptr + 2);
1194 *(ptr + 2) = temp;
1195 return colorref;
1196}
1197
Bram Moolenaared39e1d2008-08-09 17:55:22 +00001198/* Attempt to make this work for old and new compilers */
Bram Moolenaar9f733d12011-09-21 20:09:42 +02001199#if !defined(_WIN64) && (!defined(_MSC_VER) || _MSC_VER < 1300)
Bram Moolenaared39e1d2008-08-09 17:55:22 +00001200# define PDP_RETVAL BOOL
1201#else
1202# define PDP_RETVAL INT_PTR
1203#endif
1204
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001205/*ARGSUSED*/
Bram Moolenaared39e1d2008-08-09 17:55:22 +00001206 static PDP_RETVAL CALLBACK
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207PrintDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
1208{
1209#ifdef FEAT_GETTEXT
1210 NONCLIENTMETRICS nm;
1211 static HFONT hfont;
1212#endif
1213
1214 switch (message)
1215 {
1216 case WM_INITDIALOG:
1217#ifdef FEAT_GETTEXT
1218 nm.cbSize = sizeof(NONCLIENTMETRICS);
1219 if (SystemParametersInfo(
1220 SPI_GETNONCLIENTMETRICS,
1221 sizeof(NONCLIENTMETRICS),
1222 &nm,
1223 0))
1224 {
1225 char buff[MAX_PATH];
1226 int i;
1227
1228 /* Translate the dialog texts */
1229 hfont = CreateFontIndirect(&nm.lfMessageFont);
1230 for (i = IDC_PRINTTEXT1; i <= IDC_PROGRESS; i++)
1231 {
1232 SendDlgItemMessage(hDlg, i, WM_SETFONT, (WPARAM)hfont, 1);
1233 if (GetDlgItemText(hDlg,i, buff, sizeof(buff)))
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001234 vimSetDlgItemText(hDlg,i, _(buff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 }
1236 SendDlgItemMessage(hDlg, IDCANCEL,
1237 WM_SETFONT, (WPARAM)hfont, 1);
1238 if (GetDlgItemText(hDlg,IDCANCEL, buff, sizeof(buff)))
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001239 vimSetDlgItemText(hDlg,IDCANCEL, _(buff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 }
1241#endif
1242 SetWindowText(hDlg, szAppName);
1243 if (prt_name != NULL)
1244 {
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001245 vimSetDlgItemText(hDlg, IDC_PRINTTEXT2, (LPSTR)prt_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 vim_free(prt_name);
1247 prt_name = NULL;
1248 }
1249 EnableMenuItem(GetSystemMenu(hDlg, FALSE), SC_CLOSE, MF_GRAYED);
1250#ifndef FEAT_GUI
1251 BringWindowToTop(s_hwnd);
1252#endif
1253 return TRUE;
1254
1255 case WM_COMMAND:
1256 *bUserAbort = TRUE;
1257 EnableWindow(GetParent(hDlg), TRUE);
1258 DestroyWindow(hDlg);
1259 hDlgPrint = NULL;
1260#ifdef FEAT_GETTEXT
1261 DeleteObject(hfont);
1262#endif
1263 return TRUE;
1264 }
1265 return FALSE;
1266}
1267
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001268/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 static BOOL CALLBACK
1270AbortProc(HDC hdcPrn, int iCode)
1271{
1272 MSG msg;
1273
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02001274 while (!*bUserAbort && pPeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 {
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02001276 if (!hDlgPrint || !pIsDialogMessage(hDlgPrint, &msg))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 {
1278 TranslateMessage(&msg);
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02001279 pDispatchMessage(&msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 }
1281 }
1282 return !*bUserAbort;
1283}
1284
1285#ifndef FEAT_GUI
1286
Bram Moolenaar26fdd7d2011-11-30 13:42:44 +01001287 static UINT_PTR CALLBACK
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288PrintHookProc(
1289 HWND hDlg, // handle to dialog box
1290 UINT uiMsg, // message identifier
1291 WPARAM wParam, // message parameter
1292 LPARAM lParam // message parameter
1293 )
1294{
1295 HWND hwndOwner;
1296 RECT rc, rcDlg, rcOwner;
1297 PRINTDLG *pPD;
1298
1299 if (uiMsg == WM_INITDIALOG)
1300 {
1301 // Get the owner window and dialog box rectangles.
1302 if ((hwndOwner = GetParent(hDlg)) == NULL)
1303 hwndOwner = GetDesktopWindow();
1304
1305 GetWindowRect(hwndOwner, &rcOwner);
1306 GetWindowRect(hDlg, &rcDlg);
1307 CopyRect(&rc, &rcOwner);
1308
1309 // Offset the owner and dialog box rectangles so that
1310 // right and bottom values represent the width and
1311 // height, and then offset the owner again to discard
1312 // space taken up by the dialog box.
1313
1314 OffsetRect(&rcDlg, -rcDlg.left, -rcDlg.top);
1315 OffsetRect(&rc, -rc.left, -rc.top);
1316 OffsetRect(&rc, -rcDlg.right, -rcDlg.bottom);
1317
1318 // The new position is the sum of half the remaining
1319 // space and the owner's original position.
1320
1321 SetWindowPos(hDlg,
1322 HWND_TOP,
1323 rcOwner.left + (rc.right / 2),
1324 rcOwner.top + (rc.bottom / 2),
1325 0, 0, // ignores size arguments
1326 SWP_NOSIZE);
1327
1328 /* tackle the printdlg copiesctrl problem */
1329 pPD = (PRINTDLG *)lParam;
1330 pPD->nCopies = (WORD)pPD->lCustData;
1331 SetDlgItemInt( hDlg, edt3, pPD->nCopies, FALSE );
1332 /* Bring the window to top */
1333 BringWindowToTop(GetParent(hDlg));
1334 SetForegroundWindow(hDlg);
1335 }
1336
1337 return FALSE;
1338}
1339#endif
1340
1341 void
1342mch_print_cleanup(void)
1343{
1344 int pifItalic;
1345 int pifBold;
1346 int pifUnderline;
1347
1348 for (pifBold = 0; pifBold <= 1; pifBold++)
1349 for (pifItalic = 0; pifItalic <= 1; pifItalic++)
1350 for (pifUnderline = 0; pifUnderline <= 1; pifUnderline++)
1351 DeleteObject(prt_font_handles[pifBold][pifItalic][pifUnderline]);
1352
1353 if (prt_dlg.hDC != NULL)
1354 DeleteDC(prt_dlg.hDC);
1355 if (!*bUserAbort)
1356 SendMessage(hDlgPrint, WM_COMMAND, 0, 0);
1357}
1358
1359 static int
1360to_device_units(int idx, int dpi, int physsize, int offset, int def_number)
1361{
1362 int ret = 0;
1363 int u;
1364 int nr;
1365
1366 u = prt_get_unit(idx);
1367 if (u == PRT_UNIT_NONE)
1368 {
1369 u = PRT_UNIT_PERC;
1370 nr = def_number;
1371 }
1372 else
1373 nr = printer_opts[idx].number;
1374
1375 switch (u)
1376 {
1377 case PRT_UNIT_PERC:
1378 ret = (physsize * nr) / 100;
1379 break;
1380 case PRT_UNIT_INCH:
1381 ret = (nr * dpi);
1382 break;
1383 case PRT_UNIT_MM:
1384 ret = (nr * 10 * dpi) / 254;
1385 break;
1386 case PRT_UNIT_POINT:
1387 ret = (nr * 10 * dpi) / 720;
1388 break;
1389 }
1390
1391 if (ret < offset)
1392 return 0;
1393 else
1394 return ret - offset;
1395}
1396
1397 static int
1398prt_get_cpl(void)
1399{
1400 int hr;
1401 int phyw;
1402 int dvoff;
1403 int rev_offset;
1404 int dpi;
1405#ifdef WIN16
1406 POINT pagesize;
1407#endif
1408
1409 GetTextMetrics(prt_dlg.hDC, &prt_tm);
1410 prt_line_height = prt_tm.tmHeight + prt_tm.tmExternalLeading;
1411
1412 hr = GetDeviceCaps(prt_dlg.hDC, HORZRES);
1413#ifdef WIN16
1414 Escape(prt_dlg.hDC, GETPHYSPAGESIZE, NULL, NULL, &pagesize);
1415 phyw = pagesize.x;
1416 Escape(prt_dlg.hDC, GETPRINTINGOFFSET, NULL, NULL, &pagesize);
1417 dvoff = pagesize.x;
1418#else
1419 phyw = GetDeviceCaps(prt_dlg.hDC, PHYSICALWIDTH);
1420 dvoff = GetDeviceCaps(prt_dlg.hDC, PHYSICALOFFSETX);
1421#endif
1422 dpi = GetDeviceCaps(prt_dlg.hDC, LOGPIXELSX);
1423
1424 rev_offset = phyw - (dvoff + hr);
1425
1426 prt_left_margin = to_device_units(OPT_PRINT_LEFT, dpi, phyw, dvoff, 10);
1427 if (prt_use_number())
1428 {
1429 prt_number_width = PRINT_NUMBER_WIDTH * prt_tm.tmAveCharWidth;
1430 prt_left_margin += prt_number_width;
1431 }
1432 else
1433 prt_number_width = 0;
1434
1435 prt_right_margin = hr - to_device_units(OPT_PRINT_RIGHT, dpi, phyw,
1436 rev_offset, 5);
1437
1438 return (prt_right_margin - prt_left_margin) / prt_tm.tmAveCharWidth;
1439}
1440
1441 static int
1442prt_get_lpp(void)
1443{
1444 int vr;
1445 int phyw;
1446 int dvoff;
1447 int rev_offset;
1448 int bottom_margin;
1449 int dpi;
1450#ifdef WIN16
1451 POINT pagesize;
1452#endif
1453
1454 vr = GetDeviceCaps(prt_dlg.hDC, VERTRES);
1455#ifdef WIN16
1456 Escape(prt_dlg.hDC, GETPHYSPAGESIZE, NULL, NULL, &pagesize);
1457 phyw = pagesize.y;
1458 Escape(prt_dlg.hDC, GETPRINTINGOFFSET, NULL, NULL, &pagesize);
1459 dvoff = pagesize.y;
1460#else
1461 phyw = GetDeviceCaps(prt_dlg.hDC, PHYSICALHEIGHT);
1462 dvoff = GetDeviceCaps(prt_dlg.hDC, PHYSICALOFFSETY);
1463#endif
1464 dpi = GetDeviceCaps(prt_dlg.hDC, LOGPIXELSY);
1465
1466 rev_offset = phyw - (dvoff + vr);
1467
1468 prt_top_margin = to_device_units(OPT_PRINT_TOP, dpi, phyw, dvoff, 5);
1469
1470 /* adjust top margin if there is a header */
1471 prt_top_margin += prt_line_height * prt_header_height();
1472
1473 bottom_margin = vr - to_device_units(OPT_PRINT_BOT, dpi, phyw,
1474 rev_offset, 5);
1475
1476 return (bottom_margin - prt_top_margin) / prt_line_height;
1477}
1478
1479 int
1480mch_print_init(prt_settings_T *psettings, char_u *jobname, int forceit)
1481{
1482 static HGLOBAL stored_dm = NULL;
1483 static HGLOBAL stored_devn = NULL;
1484 static int stored_nCopies = 1;
1485 static int stored_nFlags = 0;
1486
1487 LOGFONT fLogFont;
1488 int pifItalic;
1489 int pifBold;
1490 int pifUnderline;
1491
1492 DEVMODE *mem;
1493 DEVNAMES *devname;
1494 int i;
1495
1496 bUserAbort = &(psettings->user_abort);
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02001497 vim_memset(&prt_dlg, 0, sizeof(PRINTDLG));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498 prt_dlg.lStructSize = sizeof(PRINTDLG);
1499#ifndef FEAT_GUI
1500 GetConsoleHwnd(); /* get value of s_hwnd */
1501#endif
1502 prt_dlg.hwndOwner = s_hwnd;
1503 prt_dlg.Flags = PD_NOPAGENUMS | PD_NOSELECTION | PD_RETURNDC;
1504 if (!forceit)
1505 {
1506 prt_dlg.hDevMode = stored_dm;
1507 prt_dlg.hDevNames = stored_devn;
1508 prt_dlg.lCustData = stored_nCopies; // work around bug in print dialog
1509#ifndef FEAT_GUI
1510 /*
1511 * Use hook to prevent console window being sent to back
1512 */
1513 prt_dlg.lpfnPrintHook = PrintHookProc;
1514 prt_dlg.Flags |= PD_ENABLEPRINTHOOK;
1515#endif
1516 prt_dlg.Flags |= stored_nFlags;
1517 }
1518
1519 /*
1520 * If bang present, return default printer setup with no dialog
1521 * never show dialog if we are running over telnet
1522 */
1523 if (forceit
1524#ifndef FEAT_GUI
1525 || !term_console
1526#endif
1527 )
1528 {
1529 prt_dlg.Flags |= PD_RETURNDEFAULT;
1530#ifdef WIN3264
1531 /*
1532 * MSDN suggests setting the first parameter to WINSPOOL for
1533 * NT, but NULL appears to work just as well.
1534 */
1535 if (*p_pdev != NUL)
1536 prt_dlg.hDC = CreateDC(NULL, p_pdev, NULL, NULL);
1537 else
1538#endif
1539 {
1540 prt_dlg.Flags |= PD_RETURNDEFAULT;
1541 if (PrintDlg(&prt_dlg) == 0)
1542 goto init_fail_dlg;
1543 }
1544 }
1545 else if (PrintDlg(&prt_dlg) == 0)
1546 goto init_fail_dlg;
1547 else
1548 {
1549 /*
1550 * keep the previous driver context
1551 */
1552 stored_dm = prt_dlg.hDevMode;
1553 stored_devn = prt_dlg.hDevNames;
1554 stored_nFlags = prt_dlg.Flags;
1555 stored_nCopies = prt_dlg.nCopies;
1556 }
1557
1558 if (prt_dlg.hDC == NULL)
1559 {
1560 EMSG(_("E237: Printer selection failed"));
1561 mch_print_cleanup();
1562 return FALSE;
1563 }
1564
1565 /* Not all printer drivers report the support of color (or grey) in the
1566 * same way. Let's set has_color if there appears to be some way to print
1567 * more than B&W. */
1568 i = GetDeviceCaps(prt_dlg.hDC, NUMCOLORS);
1569 psettings->has_color = (GetDeviceCaps(prt_dlg.hDC, BITSPIXEL) > 1
1570 || GetDeviceCaps(prt_dlg.hDC, PLANES) > 1
1571 || i > 2 || i == -1);
1572
1573 /* Ensure all font styles are baseline aligned */
1574 SetTextAlign(prt_dlg.hDC, TA_BASELINE|TA_LEFT);
1575
1576 /*
1577 * On some windows systems the nCopies parameter is not
1578 * passed back correctly. It must be retrieved from the
1579 * hDevMode struct.
1580 */
1581 mem = (DEVMODE *)GlobalLock(prt_dlg.hDevMode);
1582 if (mem != NULL)
1583 {
1584#ifdef WIN3264
1585 if (mem->dmCopies != 1)
1586 stored_nCopies = mem->dmCopies;
1587#endif
1588 if ((mem->dmFields & DM_DUPLEX) && (mem->dmDuplex & ~DMDUP_SIMPLEX))
1589 psettings->duplex = TRUE;
1590 if ((mem->dmFields & DM_COLOR) && (mem->dmColor & DMCOLOR_COLOR))
1591 psettings->has_color = TRUE;
1592 }
1593 GlobalUnlock(prt_dlg.hDevMode);
1594
1595 devname = (DEVNAMES *)GlobalLock(prt_dlg.hDevNames);
1596 if (devname != 0)
1597 {
1598 char_u *printer_name = (char_u *)devname + devname->wDeviceOffset;
1599 char_u *port_name = (char_u *)devname +devname->wOutputOffset;
1600 char_u *text = _("to %s on %s");
1601
Bram Moolenaare6a91fd2008-07-24 18:51:11 +00001602 prt_name = alloc((unsigned)(STRLEN(printer_name) + STRLEN(port_name)
1603 + STRLEN(text)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 if (prt_name != NULL)
1605 wsprintf(prt_name, text, printer_name, port_name);
1606 }
1607 GlobalUnlock(prt_dlg.hDevNames);
1608
1609 /*
1610 * Initialise the font according to 'printfont'
1611 */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02001612 vim_memset(&fLogFont, 0, sizeof(fLogFont));
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00001613 if (get_logfont(&fLogFont, p_pfn, prt_dlg.hDC, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 {
1615 EMSG2(_("E613: Unknown printer font: %s"), p_pfn);
1616 mch_print_cleanup();
1617 return FALSE;
1618 }
1619
1620 for (pifBold = 0; pifBold <= 1; pifBold++)
1621 for (pifItalic = 0; pifItalic <= 1; pifItalic++)
1622 for (pifUnderline = 0; pifUnderline <= 1; pifUnderline++)
1623 {
1624 fLogFont.lfWeight = boldface[pifBold];
1625 fLogFont.lfItalic = pifItalic;
1626 fLogFont.lfUnderline = pifUnderline;
1627 prt_font_handles[pifBold][pifItalic][pifUnderline]
1628 = CreateFontIndirect(&fLogFont);
1629 }
1630
1631 SetBkMode(prt_dlg.hDC, OPAQUE);
1632 SelectObject(prt_dlg.hDC, prt_font_handles[0][0][0]);
1633
1634 /*
1635 * Fill in the settings struct
1636 */
1637 psettings->chars_per_line = prt_get_cpl();
1638 psettings->lines_per_page = prt_get_lpp();
1639 psettings->n_collated_copies = (prt_dlg.Flags & PD_COLLATE)
1640 ? prt_dlg.nCopies : 1;
1641 psettings->n_uncollated_copies = (prt_dlg.Flags & PD_COLLATE)
1642 ? 1 : prt_dlg.nCopies;
1643
1644 if (psettings->n_collated_copies == 0)
1645 psettings->n_collated_copies = 1;
1646
1647 if (psettings->n_uncollated_copies == 0)
1648 psettings->n_uncollated_copies = 1;
1649
1650 psettings->jobname = jobname;
1651
1652 return TRUE;
1653
1654init_fail_dlg:
1655 {
1656 DWORD err = CommDlgExtendedError();
1657
1658 if (err)
1659 {
1660#ifdef WIN16
1661 char buf[20];
1662
1663 sprintf(buf, "%ld", err);
1664 EMSG2(_("E238: Print error: %s"), buf);
1665#else
1666 char_u *buf;
1667
1668 /* I suspect FormatMessage() doesn't work for values returned by
1669 * CommDlgExtendedError(). What does? */
1670 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
1671 FORMAT_MESSAGE_FROM_SYSTEM |
1672 FORMAT_MESSAGE_IGNORE_INSERTS,
1673 NULL, err, 0, (LPTSTR)(&buf), 0, NULL);
1674 EMSG2(_("E238: Print error: %s"),
1675 buf == NULL ? (char_u *)_("Unknown") : buf);
1676 LocalFree((LPVOID)(buf));
1677#endif
1678 }
1679 else
1680 msg_clr_eos(); /* Maybe canceled */
1681
1682 mch_print_cleanup();
1683 return FALSE;
1684 }
1685}
1686
1687
1688 int
1689mch_print_begin(prt_settings_T *psettings)
1690{
1691 int ret;
1692 static DOCINFO di;
1693 char szBuffer[300];
1694
1695 hDlgPrint = CreateDialog(GetModuleHandle(NULL), TEXT("PrintDlgBox"),
1696 prt_dlg.hwndOwner, PrintDlgProc);
1697#ifdef WIN16
1698 Escape(prt_dlg.hDC, SETABORTPROC, 0, (LPSTR)AbortProc, NULL);
1699#else
1700 SetAbortProc(prt_dlg.hDC, AbortProc);
1701#endif
1702 wsprintf(szBuffer, _("Printing '%s'"), gettail(psettings->jobname));
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001703 vimSetDlgItemText(hDlgPrint, IDC_PRINTTEXT1, (LPSTR)szBuffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02001705 vim_memset(&di, 0, sizeof(DOCINFO));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 di.cbSize = sizeof(DOCINFO);
1707 di.lpszDocName = psettings->jobname;
1708 ret = StartDoc(prt_dlg.hDC, &di);
1709
1710#ifdef FEAT_GUI
1711 /* Give focus back to main window (when using MDI). */
1712 SetFocus(s_hwnd);
1713#endif
1714
1715 return (ret > 0);
1716}
1717
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001718/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 void
1720mch_print_end(prt_settings_T *psettings)
1721{
1722 EndDoc(prt_dlg.hDC);
1723 if (!*bUserAbort)
1724 SendMessage(hDlgPrint, WM_COMMAND, 0, 0);
1725}
1726
1727 int
1728mch_print_end_page(void)
1729{
1730 return (EndPage(prt_dlg.hDC) > 0);
1731}
1732
1733 int
1734mch_print_begin_page(char_u *msg)
1735{
1736 if (msg != NULL)
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001737 vimSetDlgItemText(hDlgPrint, IDC_PROGRESS, (LPSTR)msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 return (StartPage(prt_dlg.hDC) > 0);
1739}
1740
1741 int
1742mch_print_blank_page(void)
1743{
1744 return (mch_print_begin_page(NULL) ? (mch_print_end_page()) : FALSE);
1745}
1746
1747static int prt_pos_x = 0;
1748static int prt_pos_y = 0;
1749
1750 void
1751mch_print_start_line(margin, page_line)
1752 int margin;
1753 int page_line;
1754{
1755 if (margin)
1756 prt_pos_x = -prt_number_width;
1757 else
1758 prt_pos_x = 0;
1759 prt_pos_y = page_line * prt_line_height
1760 + prt_tm.tmAscent + prt_tm.tmExternalLeading;
1761}
1762
1763 int
1764mch_print_text_out(char_u *p, int len)
1765{
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001766#if defined(FEAT_PROPORTIONAL_FONTS) || (defined(FEAT_MBYTE) && !defined(WIN16))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 SIZE sz;
1768#endif
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001769#if defined(FEAT_MBYTE) && !defined(WIN16)
1770 WCHAR *wp = NULL;
1771 int wlen = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772
Bram Moolenaar5246cd72013-06-16 16:41:47 +02001773 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
1774 {
1775 wp = enc_to_utf16(p, &wlen);
1776 }
1777 if (wp != NULL)
1778 {
1779 int ret = FALSE;
1780
1781 TextOutW(prt_dlg.hDC, prt_pos_x + prt_left_margin,
1782 prt_pos_y + prt_top_margin, wp, wlen);
1783 GetTextExtentPoint32W(prt_dlg.hDC, wp, wlen, &sz);
1784 vim_free(wp);
1785 prt_pos_x += (sz.cx - prt_tm.tmOverhang);
1786 /* This is wrong when printing spaces for a TAB. */
1787 if (p[len] != NUL)
1788 {
1789 wlen = MB_PTR2LEN(p + len);
1790 wp = enc_to_utf16(p + len, &wlen);
1791 if (wp != NULL)
1792 {
1793 GetTextExtentPoint32W(prt_dlg.hDC, wp, 1, &sz);
1794 ret = (prt_pos_x + prt_left_margin + sz.cx > prt_right_margin);
1795 vim_free(wp);
1796 }
1797 }
1798 return ret;
1799 }
1800#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 TextOut(prt_dlg.hDC, prt_pos_x + prt_left_margin,
1802 prt_pos_y + prt_top_margin, p, len);
1803#ifndef FEAT_PROPORTIONAL_FONTS
1804 prt_pos_x += len * prt_tm.tmAveCharWidth;
1805 return (prt_pos_x + prt_left_margin + prt_tm.tmAveCharWidth
1806 + prt_tm.tmOverhang > prt_right_margin);
1807#else
1808# ifdef WIN16
1809 GetTextExtentPoint(prt_dlg.hDC, p, len, &sz);
1810# else
1811 GetTextExtentPoint32(prt_dlg.hDC, p, len, &sz);
1812# endif
1813 prt_pos_x += (sz.cx - prt_tm.tmOverhang);
1814 /* This is wrong when printing spaces for a TAB. */
1815 if (p[len] == NUL)
1816 return FALSE;
1817# ifdef WIN16
1818 GetTextExtentPoint(prt_dlg.hDC, p + len, 1, &sz);
1819# else
1820 GetTextExtentPoint32(prt_dlg.hDC, p + len, 1, &sz);
1821# endif
1822 return (prt_pos_x + prt_left_margin + sz.cx > prt_right_margin);
1823#endif
1824}
1825
1826 void
1827mch_print_set_font(int iBold, int iItalic, int iUnderline)
1828{
1829 SelectObject(prt_dlg.hDC, prt_font_handles[iBold][iItalic][iUnderline]);
1830}
1831
1832 void
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001833mch_print_set_bg(long_u bgcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834{
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001835 SetBkColor(prt_dlg.hDC, GetNearestColor(prt_dlg.hDC,
1836 swap_me((COLORREF)bgcol)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 /*
1838 * With a white background we can draw characters transparent, which is
1839 * good for italic characters that overlap to the next char cell.
1840 */
1841 if (bgcol == 0xffffffUL)
1842 SetBkMode(prt_dlg.hDC, TRANSPARENT);
1843 else
1844 SetBkMode(prt_dlg.hDC, OPAQUE);
1845}
1846
1847 void
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001848mch_print_set_fg(long_u fgcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849{
Bram Moolenaar551dbcc2006-04-25 22:13:59 +00001850 SetTextColor(prt_dlg.hDC, GetNearestColor(prt_dlg.hDC,
1851 swap_me((COLORREF)fgcol)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852}
1853
1854#endif /*FEAT_PRINTER && !FEAT_POSTSCRIPT*/
1855
Bram Moolenaar58d98232005-07-23 22:25:46 +00001856
1857
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858#if defined(FEAT_SHORTCUT) || defined(PROTO)
Bram Moolenaar82881492012-11-20 16:53:39 +01001859# ifndef PROTO
1860# include <shlobj.h>
1861# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862
1863/*
1864 * When "fname" is the name of a shortcut (*.lnk) resolve the file it points
1865 * to and return that name in allocated memory.
1866 * Otherwise NULL is returned.
1867 */
1868 char_u *
1869mch_resolve_shortcut(char_u *fname)
1870{
1871 HRESULT hr;
1872 IShellLink *psl = NULL;
1873 IPersistFile *ppf = NULL;
1874 OLECHAR wsz[MAX_PATH];
1875 WIN32_FIND_DATA ffd; // we get those free of charge
Bram Moolenaar604729e2013-08-30 16:44:19 +02001876 CHAR buf[MAX_PATH]; // could have simply reused 'wsz'...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 char_u *rfname = NULL;
1878 int len;
Bram Moolenaar604729e2013-08-30 16:44:19 +02001879# ifdef FEAT_MBYTE
1880 IShellLinkW *pslw = NULL;
1881 WIN32_FIND_DATAW ffdw; // we get those free of charge
1882# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883
1884 /* Check if the file name ends in ".lnk". Avoid calling
1885 * CoCreateInstance(), it's quite slow. */
1886 if (fname == NULL)
1887 return rfname;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001888 len = (int)STRLEN(fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 if (len <= 4 || STRNICMP(fname + len - 4, ".lnk", 4) != 0)
1890 return rfname;
1891
1892 CoInitialize(NULL);
1893
Bram Moolenaar604729e2013-08-30 16:44:19 +02001894# ifdef FEAT_MBYTE
1895 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
1896 {
1897 // create a link manager object and request its interface
1898 hr = CoCreateInstance(
1899 &CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
1900 &IID_IShellLinkW, (void**)&pslw);
1901 if (hr == S_OK)
1902 {
1903 WCHAR *p = enc_to_utf16(fname, NULL);
1904
1905 if (p != NULL)
1906 {
1907 // Get a pointer to the IPersistFile interface.
1908 hr = pslw->lpVtbl->QueryInterface(
1909 pslw, &IID_IPersistFile, (void**)&ppf);
1910 if (hr != S_OK)
1911 goto shortcut_errorw;
1912
1913 // "load" the name and resolve the link
1914 hr = ppf->lpVtbl->Load(ppf, p, STGM_READ);
1915 if (hr != S_OK)
1916 goto shortcut_errorw;
1917# if 0 // This makes Vim wait a long time if the target does not exist.
1918 hr = pslw->lpVtbl->Resolve(pslw, NULL, SLR_NO_UI);
1919 if (hr != S_OK)
1920 goto shortcut_errorw;
1921# endif
1922
1923 // Get the path to the link target.
1924 ZeroMemory(wsz, MAX_PATH * sizeof(WCHAR));
1925 hr = pslw->lpVtbl->GetPath(pslw, wsz, MAX_PATH, &ffdw, 0);
1926 if (hr == S_OK && wsz[0] != NUL)
1927 rfname = utf16_to_enc(wsz, NULL);
1928
1929shortcut_errorw:
1930 vim_free(p);
1931 if (hr == S_OK)
1932 goto shortcut_end;
1933 }
1934 }
1935 /* Retry with non-wide function (for Windows 98). */
1936 }
1937# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 // create a link manager object and request its interface
1939 hr = CoCreateInstance(
1940 &CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
1941 &IID_IShellLink, (void**)&psl);
1942 if (hr != S_OK)
Bram Moolenaar604729e2013-08-30 16:44:19 +02001943 goto shortcut_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944
1945 // Get a pointer to the IPersistFile interface.
1946 hr = psl->lpVtbl->QueryInterface(
1947 psl, &IID_IPersistFile, (void**)&ppf);
1948 if (hr != S_OK)
Bram Moolenaar604729e2013-08-30 16:44:19 +02001949 goto shortcut_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950
1951 // full path string must be in Unicode.
1952 MultiByteToWideChar(CP_ACP, 0, fname, -1, wsz, MAX_PATH);
1953
Bram Moolenaarff1d0d42007-05-10 17:24:16 +00001954 // "load" the name and resolve the link
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 hr = ppf->lpVtbl->Load(ppf, wsz, STGM_READ);
1956 if (hr != S_OK)
Bram Moolenaar604729e2013-08-30 16:44:19 +02001957 goto shortcut_end;
1958# if 0 // This makes Vim wait a long time if the target doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 hr = psl->lpVtbl->Resolve(psl, NULL, SLR_NO_UI);
1960 if (hr != S_OK)
Bram Moolenaar604729e2013-08-30 16:44:19 +02001961 goto shortcut_end;
1962# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963
1964 // Get the path to the link target.
1965 ZeroMemory(buf, MAX_PATH);
1966 hr = psl->lpVtbl->GetPath(psl, buf, MAX_PATH, &ffd, 0);
1967 if (hr == S_OK && buf[0] != NUL)
1968 rfname = vim_strsave(buf);
1969
Bram Moolenaar604729e2013-08-30 16:44:19 +02001970shortcut_end:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 // Release all interface pointers (both belong to the same object)
1972 if (ppf != NULL)
1973 ppf->lpVtbl->Release(ppf);
1974 if (psl != NULL)
1975 psl->lpVtbl->Release(psl);
Bram Moolenaar604729e2013-08-30 16:44:19 +02001976# ifdef FEAT_MBYTE
1977 if (pslw != NULL)
1978 pslw->lpVtbl->Release(pslw);
1979# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980
1981 CoUninitialize();
1982 return rfname;
1983}
1984#endif
1985
1986#if (defined(FEAT_EVAL) && !defined(FEAT_GUI)) || defined(PROTO)
1987/*
1988 * Bring ourselves to the foreground. Does work if the OS doesn't allow it.
1989 */
1990 void
1991win32_set_foreground()
1992{
1993# ifndef FEAT_GUI
1994 GetConsoleHwnd(); /* get value of s_hwnd */
1995# endif
1996 if (s_hwnd != 0)
1997 SetForegroundWindow(s_hwnd);
1998}
1999#endif
2000
2001#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
2002/*
2003 * Client-server code for Vim
2004 *
2005 * Originally written by Paul Moore
2006 */
2007
2008/* In order to handle inter-process messages, we need to have a window. But
2009 * the functions in this module can be called before the main GUI window is
2010 * created (and may also be called in the console version, where there is no
2011 * GUI window at all).
2012 *
2013 * So we create a hidden window, and arrange to destroy it on exit.
2014 */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002015HWND message_window = 0; /* window that's handling messages */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016
2017#define VIM_CLASSNAME "VIM_MESSAGES"
2018#define VIM_CLASSNAME_LEN (sizeof(VIM_CLASSNAME) - 1)
2019
2020/* Communication is via WM_COPYDATA messages. The message type is send in
2021 * the dwData parameter. Types are defined here. */
2022#define COPYDATA_KEYS 0
2023#define COPYDATA_REPLY 1
2024#define COPYDATA_EXPR 10
2025#define COPYDATA_RESULT 11
2026#define COPYDATA_ERROR_RESULT 12
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002027#define COPYDATA_ENCODING 20
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028
2029/* This is a structure containing a server HWND and its name. */
2030struct server_id
2031{
2032 HWND hwnd;
2033 char_u *name;
2034};
2035
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002036/* Last received 'encoding' that the client uses. */
2037static char_u *client_enc = NULL;
2038
2039/*
2040 * Tell the other side what encoding we are using.
2041 * Errors are ignored.
2042 */
2043 static void
2044serverSendEnc(HWND target)
2045{
2046 COPYDATASTRUCT data;
2047
2048 data.dwData = COPYDATA_ENCODING;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002049#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002050 data.cbData = (DWORD)STRLEN(p_enc) + 1;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002051 data.lpData = p_enc;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002052#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +02002053 data.cbData = (DWORD)STRLEN("latin1") + 1;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002054 data.lpData = "latin1";
2055#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002056 (void)SendMessage(target, WM_COPYDATA, (WPARAM)message_window,
2057 (LPARAM)(&data));
2058}
2059
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060/*
2061 * Clean up on exit. This destroys the hidden message window.
2062 */
2063 static void
2064#ifdef __BORLANDC__
2065 _RTLENTRYF
2066#endif
2067CleanUpMessaging(void)
2068{
2069 if (message_window != 0)
2070 {
2071 DestroyWindow(message_window);
2072 message_window = 0;
2073 }
2074}
2075
2076static int save_reply(HWND server, char_u *reply, int expr);
2077
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002078/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 * The window procedure for the hidden message window.
2080 * It handles callback messages and notifications from servers.
2081 * In order to process these messages, it is necessary to run a
2082 * message loop. Code which may run before the main message loop
2083 * is started (in the GUI) is careful to pump messages when it needs
2084 * to. Features which require message delivery during normal use will
2085 * not work in the console version - this basically means those
2086 * features which allow Vim to act as a server, rather than a client.
2087 */
2088 static LRESULT CALLBACK
2089Messaging_WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
2090{
2091 if (msg == WM_COPYDATA)
2092 {
2093 /* This is a message from another Vim. The dwData member of the
2094 * COPYDATASTRUCT determines the type of message:
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002095 * COPYDATA_ENCODING:
2096 * The encoding that the client uses. Following messages will
2097 * use this encoding, convert if needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 * COPYDATA_KEYS:
2099 * A key sequence. We are a server, and a client wants these keys
2100 * adding to the input queue.
2101 * COPYDATA_REPLY:
2102 * A reply. We are a client, and a server has sent this message
2103 * in response to a request. (server2client())
2104 * COPYDATA_EXPR:
2105 * An expression. We are a server, and a client wants us to
2106 * evaluate this expression.
2107 * COPYDATA_RESULT:
2108 * A reply. We are a client, and a server has sent this message
2109 * in response to a COPYDATA_EXPR.
2110 * COPYDATA_ERROR_RESULT:
2111 * A reply. We are a client, and a server has sent this message
2112 * in response to a COPYDATA_EXPR that failed to evaluate.
2113 */
2114 COPYDATASTRUCT *data = (COPYDATASTRUCT*)lParam;
2115 HWND sender = (HWND)wParam;
2116 COPYDATASTRUCT reply;
2117 char_u *res;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 int retval;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002119 char_u *str;
2120 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121
2122 switch (data->dwData)
2123 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002124 case COPYDATA_ENCODING:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002125# ifdef FEAT_MBYTE
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002126 /* Remember the encoding that the client uses. */
2127 vim_free(client_enc);
2128 client_enc = enc_canonize((char_u *)data->lpData);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002129# endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002130 return 1;
2131
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 case COPYDATA_KEYS:
2133 /* Remember who sent this, for <client> */
2134 clientWindow = sender;
2135
2136 /* Add the received keys to the input buffer. The loop waiting
2137 * for the user to do something should check the input buffer. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002138 str = serverConvert(client_enc, (char_u *)data->lpData, &tofree);
2139 server_to_input_buf(str);
2140 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141
2142# ifdef FEAT_GUI
2143 /* Wake up the main GUI loop. */
2144 if (s_hwnd != 0)
2145 PostMessage(s_hwnd, WM_NULL, 0, 0);
2146# endif
2147 return 1;
2148
2149 case COPYDATA_EXPR:
2150 /* Remember who sent this, for <client> */
2151 clientWindow = sender;
2152
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002153 str = serverConvert(client_enc, (char_u *)data->lpData, &tofree);
2154 res = eval_client_expr_to_string(str);
2155 vim_free(tofree);
2156
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 if (res == NULL)
2158 {
2159 res = vim_strsave(_(e_invexprmsg));
2160 reply.dwData = COPYDATA_ERROR_RESULT;
2161 }
2162 else
2163 reply.dwData = COPYDATA_RESULT;
2164 reply.lpData = res;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002165 reply.cbData = (DWORD)STRLEN(res) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002167 serverSendEnc(sender);
Bram Moolenaar5246cd72013-06-16 16:41:47 +02002168 retval = (int)SendMessage(sender, WM_COPYDATA,
2169 (WPARAM)message_window, (LPARAM)(&reply));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 vim_free(res);
2171 return retval;
2172
2173 case COPYDATA_REPLY:
2174 case COPYDATA_RESULT:
2175 case COPYDATA_ERROR_RESULT:
2176 if (data->lpData != NULL)
2177 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002178 str = serverConvert(client_enc, (char_u *)data->lpData,
2179 &tofree);
2180 if (tofree == NULL)
2181 str = vim_strsave(str);
2182 if (save_reply(sender, str,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 (data->dwData == COPYDATA_REPLY ? 0 :
2184 (data->dwData == COPYDATA_RESULT ? 1 :
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002185 2))) == FAIL)
2186 vim_free(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187#ifdef FEAT_AUTOCMD
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002188 else if (data->dwData == COPYDATA_REPLY)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 {
Bram Moolenaar427d51c2013-06-16 16:01:25 +02002190 char_u winstr[30];
2191
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002192 sprintf((char *)winstr, PRINTF_HEX_LONG_U, (long_u)sender);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002193 apply_autocmds(EVENT_REMOTEREPLY, winstr, str,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 TRUE, curbuf);
2195 }
2196#endif
2197 }
2198 return 1;
2199 }
2200
2201 return 0;
2202 }
2203
2204 else if (msg == WM_ACTIVATE && wParam == WA_ACTIVE)
2205 {
2206 /* When the message window is activated (brought to the foreground),
2207 * this actually applies to the text window. */
2208#ifndef FEAT_GUI
2209 GetConsoleHwnd(); /* get value of s_hwnd */
2210#endif
2211 if (s_hwnd != 0)
2212 {
2213 SetForegroundWindow(s_hwnd);
2214 return 0;
2215 }
2216 }
2217
2218 return DefWindowProc(hwnd, msg, wParam, lParam);
2219}
2220
2221/*
2222 * Initialise the message handling process. This involves creating a window
2223 * to handle messages - the window will not be visible.
2224 */
2225 void
2226serverInitMessaging(void)
2227{
2228 WNDCLASS wndclass;
2229 HINSTANCE s_hinst;
2230
2231 /* Clean up on exit */
2232 atexit(CleanUpMessaging);
2233
2234 /* Register a window class - we only really care
2235 * about the window procedure
2236 */
2237 s_hinst = (HINSTANCE)GetModuleHandle(0);
2238 wndclass.style = 0;
2239 wndclass.lpfnWndProc = Messaging_WndProc;
2240 wndclass.cbClsExtra = 0;
2241 wndclass.cbWndExtra = 0;
2242 wndclass.hInstance = s_hinst;
2243 wndclass.hIcon = NULL;
2244 wndclass.hCursor = NULL;
2245 wndclass.hbrBackground = NULL;
2246 wndclass.lpszMenuName = NULL;
2247 wndclass.lpszClassName = VIM_CLASSNAME;
2248 RegisterClass(&wndclass);
2249
2250 /* Create the message window. It will be hidden, so the details don't
2251 * matter. Don't use WS_OVERLAPPEDWINDOW, it will make a shortcut remove
2252 * focus from gvim. */
2253 message_window = CreateWindow(VIM_CLASSNAME, "",
2254 WS_POPUPWINDOW | WS_CAPTION,
2255 CW_USEDEFAULT, CW_USEDEFAULT,
2256 100, 100, NULL, NULL,
2257 s_hinst, NULL);
2258}
2259
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002260/* Used by serverSendToVim() to find an alternate server name. */
2261static char_u *altname_buf_ptr = NULL;
2262
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263/*
2264 * Get the title of the window "hwnd", which is the Vim server name, in
2265 * "name[namelen]" and return the length.
2266 * Returns zero if window "hwnd" is not a Vim server.
2267 */
2268 static int
2269getVimServerName(HWND hwnd, char *name, int namelen)
2270{
2271 int len;
2272 char buffer[VIM_CLASSNAME_LEN + 1];
2273
2274 /* Ignore windows which aren't Vim message windows */
2275 len = GetClassName(hwnd, buffer, sizeof(buffer));
2276 if (len != VIM_CLASSNAME_LEN || STRCMP(buffer, VIM_CLASSNAME) != 0)
2277 return 0;
2278
2279 /* Get the title of the window */
2280 return GetWindowText(hwnd, name, namelen);
2281}
2282
2283 static BOOL CALLBACK
2284enumWindowsGetServer(HWND hwnd, LPARAM lparam)
2285{
2286 struct server_id *id = (struct server_id *)lparam;
2287 char server[MAX_PATH];
2288
2289 /* Get the title of the window */
2290 if (getVimServerName(hwnd, server, sizeof(server)) == 0)
2291 return TRUE;
2292
2293 /* If this is the server we're looking for, return its HWND */
2294 if (STRICMP(server, id->name) == 0)
2295 {
2296 id->hwnd = hwnd;
2297 return FALSE;
2298 }
2299
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002300 /* If we are looking for an alternate server, remember this name. */
2301 if (altname_buf_ptr != NULL
2302 && STRNICMP(server, id->name, STRLEN(id->name)) == 0
2303 && vim_isdigit(server[STRLEN(id->name)]))
2304 {
2305 STRCPY(altname_buf_ptr, server);
2306 altname_buf_ptr = NULL; /* don't use another name */
2307 }
2308
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 /* Otherwise, keep looking */
2310 return TRUE;
2311}
2312
2313 static BOOL CALLBACK
2314enumWindowsGetNames(HWND hwnd, LPARAM lparam)
2315{
2316 garray_T *ga = (garray_T *)lparam;
2317 char server[MAX_PATH];
2318
2319 /* Get the title of the window */
2320 if (getVimServerName(hwnd, server, sizeof(server)) == 0)
2321 return TRUE;
2322
2323 /* Add the name to the list */
2324 ga_concat(ga, server);
2325 ga_concat(ga, "\n");
2326 return TRUE;
2327}
2328
2329 static HWND
2330findServer(char_u *name)
2331{
2332 struct server_id id;
2333
2334 id.name = name;
2335 id.hwnd = 0;
2336
2337 EnumWindows(enumWindowsGetServer, (LPARAM)(&id));
2338
2339 return id.hwnd;
2340}
2341
2342 void
2343serverSetName(char_u *name)
2344{
2345 char_u *ok_name;
2346 HWND hwnd = 0;
2347 int i = 0;
2348 char_u *p;
2349
2350 /* Leave enough space for a 9-digit suffix to ensure uniqueness! */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002351 ok_name = alloc((unsigned)STRLEN(name) + 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352
2353 STRCPY(ok_name, name);
2354 p = ok_name + STRLEN(name);
2355
2356 for (;;)
2357 {
2358 /* This is inefficient - we're doing an EnumWindows loop for each
2359 * possible name. It would be better to grab all names in one go,
2360 * and scan the list each time...
2361 */
2362 hwnd = findServer(ok_name);
2363 if (hwnd == 0)
2364 break;
2365
2366 ++i;
2367 if (i >= 1000)
2368 break;
2369
2370 sprintf((char *)p, "%d", i);
2371 }
2372
2373 if (hwnd != 0)
2374 vim_free(ok_name);
2375 else
2376 {
2377 /* Remember the name */
2378 serverName = ok_name;
2379#ifdef FEAT_TITLE
2380 need_maketitle = TRUE; /* update Vim window title later */
2381#endif
2382
2383 /* Update the message window title */
2384 SetWindowText(message_window, ok_name);
2385
2386#ifdef FEAT_EVAL
2387 /* Set the servername variable */
2388 set_vim_var_string(VV_SEND_SERVER, serverName, -1);
2389#endif
2390 }
2391}
2392
2393 char_u *
2394serverGetVimNames(void)
2395{
2396 garray_T ga;
2397
2398 ga_init2(&ga, 1, 100);
2399
2400 EnumWindows(enumWindowsGetNames, (LPARAM)(&ga));
Bram Moolenaar269ec652004-07-29 08:43:53 +00002401 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402
2403 return ga.ga_data;
2404}
2405
2406 int
2407serverSendReply(name, reply)
2408 char_u *name; /* Where to send. */
2409 char_u *reply; /* What to send. */
2410{
2411 HWND target;
2412 COPYDATASTRUCT data;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002413 long_u n = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414
2415 /* The "name" argument is a magic cookie obtained from expand("<client>").
2416 * It should be of the form 0xXXXXX - i.e. a C hex literal, which is the
2417 * value of the client's message window HWND.
2418 */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002419 sscanf((char *)name, SCANF_HEX_LONG_U, &n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 if (n == 0)
2421 return -1;
2422
2423 target = (HWND)n;
2424 if (!IsWindow(target))
2425 return -1;
2426
2427 data.dwData = COPYDATA_REPLY;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002428 data.cbData = (DWORD)STRLEN(reply) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 data.lpData = reply;
2430
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002431 serverSendEnc(target);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 if (SendMessage(target, WM_COPYDATA, (WPARAM)message_window,
2433 (LPARAM)(&data)))
2434 return 0;
2435
2436 return -1;
2437}
2438
2439 int
2440serverSendToVim(name, cmd, result, ptarget, asExpr, silent)
2441 char_u *name; /* Where to send. */
2442 char_u *cmd; /* What to send. */
2443 char_u **result; /* Result of eval'ed expression */
2444 void *ptarget; /* HWND of server */
2445 int asExpr; /* Expression or keys? */
2446 int silent; /* don't complain about no server */
2447{
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002448 HWND target;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 COPYDATASTRUCT data;
2450 char_u *retval = NULL;
2451 int retcode = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002452 char_u altname_buf[MAX_PATH];
2453
2454 /* If the server name does not end in a digit then we look for an
2455 * alternate name. e.g. when "name" is GVIM the we may find GVIM2. */
2456 if (STRLEN(name) > 1 && !vim_isdigit(name[STRLEN(name) - 1]))
2457 altname_buf_ptr = altname_buf;
2458 altname_buf[0] = NUL;
2459 target = findServer(name);
2460 altname_buf_ptr = NULL;
2461 if (target == 0 && altname_buf[0] != NUL)
2462 /* Use another server name we found. */
2463 target = findServer(altname_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464
2465 if (target == 0)
2466 {
2467 if (!silent)
2468 EMSG2(_(e_noserver), name);
2469 return -1;
2470 }
2471
2472 if (ptarget)
2473 *(HWND *)ptarget = target;
2474
2475 data.dwData = asExpr ? COPYDATA_EXPR : COPYDATA_KEYS;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002476 data.cbData = (DWORD)STRLEN(cmd) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 data.lpData = cmd;
2478
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002479 serverSendEnc(target);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 if (SendMessage(target, WM_COPYDATA, (WPARAM)message_window,
2481 (LPARAM)(&data)) == 0)
2482 return -1;
2483
2484 if (asExpr)
2485 retval = serverGetReply(target, &retcode, TRUE, TRUE);
2486
2487 if (result == NULL)
2488 vim_free(retval);
2489 else
2490 *result = retval; /* Caller assumes responsibility for freeing */
2491
2492 return retcode;
2493}
2494
2495/*
2496 * Bring the server to the foreground.
2497 */
2498 void
2499serverForeground(name)
2500 char_u *name;
2501{
2502 HWND target = findServer(name);
2503
2504 if (target != 0)
2505 SetForegroundWindow(target);
2506}
2507
2508/* Replies from server need to be stored until the client picks them up via
2509 * remote_read(). So we maintain a list of server-id/reply pairs.
2510 * Note that there could be multiple replies from one server pending if the
2511 * client is slow picking them up.
2512 * We just store the replies in a simple list. When we remove an entry, we
2513 * move list entries down to fill the gap.
2514 * The server ID is simply the HWND.
2515 */
2516typedef struct
2517{
2518 HWND server; /* server window */
2519 char_u *reply; /* reply string */
2520 int expr_result; /* 0 for REPLY, 1 for RESULT 2 for error */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002521} reply_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522
2523static garray_T reply_list = {0, 0, sizeof(reply_T), 5, 0};
2524
2525#define REPLY_ITEM(i) ((reply_T *)(reply_list.ga_data) + (i))
2526#define REPLY_COUNT (reply_list.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527
2528/* Flag which is used to wait for a reply */
2529static int reply_received = 0;
2530
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002531/*
2532 * Store a reply. "reply" must be allocated memory (or NULL).
2533 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 static int
2535save_reply(HWND server, char_u *reply, int expr)
2536{
2537 reply_T *rep;
2538
2539 if (ga_grow(&reply_list, 1) == FAIL)
2540 return FAIL;
2541
2542 rep = REPLY_ITEM(REPLY_COUNT);
2543 rep->server = server;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002544 rep->reply = reply;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 rep->expr_result = expr;
2546 if (rep->reply == NULL)
2547 return FAIL;
2548
2549 ++REPLY_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 reply_received = 1;
2551 return OK;
2552}
2553
2554/*
2555 * Get a reply from server "server".
2556 * When "expr_res" is non NULL, get the result of an expression, otherwise a
2557 * server2client() message.
2558 * When non NULL, point to return code. 0 => OK, -1 => ERROR
2559 * If "remove" is TRUE, consume the message, the caller must free it then.
2560 * if "wait" is TRUE block until a message arrives (or the server exits).
2561 */
2562 char_u *
2563serverGetReply(HWND server, int *expr_res, int remove, int wait)
2564{
2565 int i;
2566 char_u *reply;
2567 reply_T *rep;
2568
2569 /* When waiting, loop until the message waiting for is received. */
2570 for (;;)
2571 {
2572 /* Reset this here, in case a message arrives while we are going
2573 * through the already received messages. */
2574 reply_received = 0;
2575
2576 for (i = 0; i < REPLY_COUNT; ++i)
2577 {
2578 rep = REPLY_ITEM(i);
2579 if (rep->server == server
2580 && ((rep->expr_result != 0) == (expr_res != NULL)))
2581 {
2582 /* Save the values we've found for later */
2583 reply = rep->reply;
2584 if (expr_res != NULL)
2585 *expr_res = rep->expr_result == 1 ? 0 : -1;
2586
2587 if (remove)
2588 {
2589 /* Move the rest of the list down to fill the gap */
2590 mch_memmove(rep, rep + 1,
2591 (REPLY_COUNT - i - 1) * sizeof(reply_T));
2592 --REPLY_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 }
2594
2595 /* Return the reply to the caller, who takes on responsibility
2596 * for freeing it if "remove" is TRUE. */
2597 return reply;
2598 }
2599 }
2600
2601 /* If we got here, we didn't find a reply. Return immediately if the
2602 * "wait" parameter isn't set. */
2603 if (!wait)
2604 break;
2605
2606 /* We need to wait for a reply. Enter a message loop until the
2607 * "reply_received" flag gets set. */
2608
2609 /* Loop until we receive a reply */
2610 while (reply_received == 0)
2611 {
2612 /* Wait for a SendMessage() call to us. This could be the reply
2613 * we are waiting for. Use a timeout of a second, to catch the
2614 * situation that the server died unexpectedly. */
2615 MsgWaitForMultipleObjects(0, NULL, TRUE, 1000, QS_ALLINPUT);
2616
2617 /* If the server has died, give up */
2618 if (!IsWindow(server))
2619 return NULL;
2620
2621 serverProcessPendingMessages();
2622 }
2623 }
2624
2625 return NULL;
2626}
2627
2628/*
2629 * Process any messages in the Windows message queue.
2630 */
2631 void
2632serverProcessPendingMessages(void)
2633{
2634 MSG msg;
2635
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02002636 while (pPeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 {
2638 TranslateMessage(&msg);
Bram Moolenaar8c85fa32011-08-10 17:08:03 +02002639 pDispatchMessage(&msg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 }
2641}
2642
2643#endif /* FEAT_CLIENTSERVER */
2644
2645#if defined(FEAT_GUI) || (defined(FEAT_PRINTER) && !defined(FEAT_POSTSCRIPT)) \
2646 || defined(PROTO)
2647
2648struct charset_pair
2649{
2650 char *name;
2651 BYTE charset;
2652};
2653
2654static struct charset_pair
2655charset_pairs[] =
2656{
2657 {"ANSI", ANSI_CHARSET},
2658 {"CHINESEBIG5", CHINESEBIG5_CHARSET},
2659 {"DEFAULT", DEFAULT_CHARSET},
2660 {"HANGEUL", HANGEUL_CHARSET},
2661 {"OEM", OEM_CHARSET},
2662 {"SHIFTJIS", SHIFTJIS_CHARSET},
2663 {"SYMBOL", SYMBOL_CHARSET},
2664#ifdef WIN3264
2665 {"ARABIC", ARABIC_CHARSET},
2666 {"BALTIC", BALTIC_CHARSET},
2667 {"EASTEUROPE", EASTEUROPE_CHARSET},
2668 {"GB2312", GB2312_CHARSET},
2669 {"GREEK", GREEK_CHARSET},
2670 {"HEBREW", HEBREW_CHARSET},
2671 {"JOHAB", JOHAB_CHARSET},
2672 {"MAC", MAC_CHARSET},
2673 {"RUSSIAN", RUSSIAN_CHARSET},
2674 {"THAI", THAI_CHARSET},
2675 {"TURKISH", TURKISH_CHARSET},
2676# if (!defined(_MSC_VER) || (_MSC_VER > 1010)) \
2677 && (!defined(__BORLANDC__) || (__BORLANDC__ > 0x0500))
2678 {"VIETNAMESE", VIETNAMESE_CHARSET},
2679# endif
2680#endif
2681 {NULL, 0}
2682};
2683
2684/*
2685 * Convert a charset ID to a name.
2686 * Return NULL when not recognized.
2687 */
2688 char *
2689charset_id2name(int id)
2690{
2691 struct charset_pair *cp;
2692
2693 for (cp = charset_pairs; cp->name != NULL; ++cp)
2694 if ((BYTE)id == cp->charset)
2695 break;
2696 return cp->name;
2697}
2698
2699static const LOGFONT s_lfDefault =
2700{
2701 -12, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, DEFAULT_CHARSET,
2702 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
2703 PROOF_QUALITY, FIXED_PITCH | FF_DONTCARE,
2704 "Fixedsys" /* see _ReadVimIni */
2705};
2706
2707/* Initialise the "current height" to -12 (same as s_lfDefault) just
2708 * in case the user specifies a font in "guifont" with no size before a font
2709 * with an explicit size has been set. This defaults the size to this value
2710 * (-12 equates to roughly 9pt).
2711 */
2712int current_font_height = -12; /* also used in gui_w48.c */
2713
2714/* Convert a string representing a point size into pixels. The string should
2715 * be a positive decimal number, with an optional decimal point (eg, "12", or
2716 * "10.5"). The pixel value is returned, and a pointer to the next unconverted
2717 * character is stored in *end. The flag "vertical" says whether this
2718 * calculation is for a vertical (height) size or a horizontal (width) one.
2719 */
2720 static int
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002721points_to_pixels(char_u *str, char_u **end, int vertical, long_i pprinter_dc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722{
2723 int pixels;
2724 int points = 0;
2725 int divisor = 0;
2726 HWND hwnd = (HWND)0;
2727 HDC hdc;
2728 HDC printer_dc = (HDC)pprinter_dc;
2729
2730 while (*str != NUL)
2731 {
2732 if (*str == '.' && divisor == 0)
2733 {
2734 /* Start keeping a divisor, for later */
2735 divisor = 1;
2736 }
2737 else
2738 {
2739 if (!VIM_ISDIGIT(*str))
2740 break;
2741
2742 points *= 10;
2743 points += *str - '0';
2744 divisor *= 10;
2745 }
2746 ++str;
2747 }
2748
2749 if (divisor == 0)
2750 divisor = 1;
2751
2752 if (printer_dc == NULL)
2753 {
2754 hwnd = GetDesktopWindow();
2755 hdc = GetWindowDC(hwnd);
2756 }
2757 else
2758 hdc = printer_dc;
2759
2760 pixels = MulDiv(points,
2761 GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX),
2762 72 * divisor);
2763
2764 if (printer_dc == NULL)
2765 ReleaseDC(hwnd, hdc);
2766
2767 *end = str;
2768 return pixels;
2769}
2770
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002771/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 static int CALLBACK
2773font_enumproc(
2774 ENUMLOGFONT *elf,
2775 NEWTEXTMETRIC *ntm,
2776 int type,
2777 LPARAM lparam)
2778{
2779 /* Return value:
2780 * 0 = terminate now (monospace & ANSI)
2781 * 1 = continue, still no luck...
2782 * 2 = continue, but we have an acceptable LOGFONT
2783 * (monospace, not ANSI)
2784 * We use these values, as EnumFontFamilies returns 1 if the
2785 * callback function is never called. So, we check the return as
2786 * 0 = perfect, 2 = OK, 1 = no good...
2787 * It's not pretty, but it works!
2788 */
2789
2790 LOGFONT *lf = (LOGFONT *)(lparam);
2791
2792#ifndef FEAT_PROPORTIONAL_FONTS
2793 /* Ignore non-monospace fonts without further ado */
2794 if ((ntm->tmPitchAndFamily & 1) != 0)
2795 return 1;
2796#endif
2797
2798 /* Remember this LOGFONT as a "possible" */
2799 *lf = elf->elfLogFont;
2800
2801 /* Terminate the scan as soon as we find an ANSI font */
2802 if (lf->lfCharSet == ANSI_CHARSET
2803 || lf->lfCharSet == OEM_CHARSET
2804 || lf->lfCharSet == DEFAULT_CHARSET)
2805 return 0;
2806
2807 /* Continue the scan - we have a non-ANSI font */
2808 return 2;
2809}
2810
2811 static int
2812init_logfont(LOGFONT *lf)
2813{
2814 int n;
2815 HWND hwnd = GetDesktopWindow();
2816 HDC hdc = GetWindowDC(hwnd);
2817
2818 n = EnumFontFamilies(hdc,
2819 (LPCSTR)lf->lfFaceName,
2820 (FONTENUMPROC)font_enumproc,
2821 (LPARAM)lf);
2822
2823 ReleaseDC(hwnd, hdc);
2824
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002825 /* If we couldn't find a usable font, return failure */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 if (n == 1)
2827 return FAIL;
2828
2829 /* Tidy up the rest of the LOGFONT structure. We set to a basic
2830 * font - get_logfont() sets bold, italic, etc based on the user's
2831 * input.
2832 */
2833 lf->lfHeight = current_font_height;
2834 lf->lfWidth = 0;
2835 lf->lfItalic = FALSE;
2836 lf->lfUnderline = FALSE;
2837 lf->lfStrikeOut = FALSE;
2838 lf->lfWeight = FW_NORMAL;
2839
2840 /* Return success */
2841 return OK;
2842}
2843
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002844/*
2845 * Get font info from "name" into logfont "lf".
2846 * Return OK for a valid name, FAIL otherwise.
2847 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 int
2849get_logfont(
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002850 LOGFONT *lf,
2851 char_u *name,
2852 HDC printer_dc,
2853 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854{
2855 char_u *p;
2856 int i;
2857 static LOGFONT *lastlf = NULL;
2858
2859 *lf = s_lfDefault;
2860 if (name == NULL)
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002861 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862
2863 if (STRCMP(name, "*") == 0)
2864 {
2865#if defined(FEAT_GUI_W32)
2866 CHOOSEFONT cf;
2867 /* if name is "*", bring up std font dialog: */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02002868 vim_memset(&cf, 0, sizeof(cf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 cf.lStructSize = sizeof(cf);
2870 cf.hwndOwner = s_hwnd;
2871 cf.Flags = CF_SCREENFONTS | CF_FIXEDPITCHONLY | CF_INITTOLOGFONTSTRUCT;
2872 if (lastlf != NULL)
2873 *lf = *lastlf;
2874 cf.lpLogFont = lf;
2875 cf.nFontType = 0 ; //REGULAR_FONTTYPE;
2876 if (ChooseFont(&cf))
2877 goto theend;
2878#else
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002879 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880#endif
2881 }
2882
2883 /*
2884 * Split name up, it could be <name>:h<height>:w<width> etc.
2885 */
2886 for (p = name; *p && *p != ':'; p++)
2887 {
2888 if (p - name + 1 > LF_FACESIZE)
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002889 return FAIL; /* Name too long */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 lf->lfFaceName[p - name] = *p;
2891 }
2892 if (p != name)
2893 lf->lfFaceName[p - name] = NUL;
2894
2895 /* First set defaults */
2896 lf->lfHeight = -12;
2897 lf->lfWidth = 0;
2898 lf->lfWeight = FW_NORMAL;
2899 lf->lfItalic = FALSE;
2900 lf->lfUnderline = FALSE;
2901 lf->lfStrikeOut = FALSE;
2902
2903 /*
2904 * If the font can't be found, try replacing '_' by ' '.
2905 */
2906 if (init_logfont(lf) == FAIL)
2907 {
2908 int did_replace = FALSE;
2909
2910 for (i = 0; lf->lfFaceName[i]; ++i)
2911 if (lf->lfFaceName[i] == '_')
2912 {
2913 lf->lfFaceName[i] = ' ';
2914 did_replace = TRUE;
2915 }
2916 if (!did_replace || init_logfont(lf) == FAIL)
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002917 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 }
2919
2920 while (*p == ':')
2921 p++;
2922
2923 /* Set the values found after ':' */
2924 while (*p)
2925 {
2926 switch (*p++)
2927 {
2928 case 'h':
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002929 lf->lfHeight = - points_to_pixels(p, &p, TRUE, (long_i)printer_dc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 break;
2931 case 'w':
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002932 lf->lfWidth = points_to_pixels(p, &p, FALSE, (long_i)printer_dc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 break;
2934 case 'b':
2935#ifndef MSWIN16_FASTTEXT
2936 lf->lfWeight = FW_BOLD;
2937#endif
2938 break;
2939 case 'i':
2940#ifndef MSWIN16_FASTTEXT
2941 lf->lfItalic = TRUE;
2942#endif
2943 break;
2944 case 'u':
2945 lf->lfUnderline = TRUE;
2946 break;
2947 case 's':
2948 lf->lfStrikeOut = TRUE;
2949 break;
2950 case 'c':
2951 {
2952 struct charset_pair *cp;
2953
2954 for (cp = charset_pairs; cp->name != NULL; ++cp)
2955 if (STRNCMP(p, cp->name, strlen(cp->name)) == 0)
2956 {
2957 lf->lfCharSet = cp->charset;
2958 p += strlen(cp->name);
2959 break;
2960 }
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002961 if (cp->name == NULL && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962 {
Bram Moolenaar051b7822005-05-19 21:00:46 +00002963 vim_snprintf((char *)IObuff, IOSIZE,
2964 _("E244: Illegal charset name \"%s\" in font name \"%s\""), p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965 EMSG(IObuff);
2966 break;
2967 }
2968 break;
2969 }
2970 default:
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002971 if (verbose)
2972 {
Bram Moolenaar051b7822005-05-19 21:00:46 +00002973 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002974 _("E245: Illegal char '%c' in font name \"%s\""),
2975 p[-1], name);
2976 EMSG(IObuff);
2977 }
2978 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 }
2980 while (*p == ':')
2981 p++;
2982 }
2983
2984#if defined(FEAT_GUI_W32)
2985theend:
2986#endif
2987 /* ron: init lastlf */
2988 if (printer_dc == NULL)
2989 {
2990 vim_free(lastlf);
2991 lastlf = (LOGFONT *)alloc(sizeof(LOGFONT));
2992 if (lastlf != NULL)
2993 mch_memmove(lastlf, lf, sizeof(LOGFONT));
2994 }
2995
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002996 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997}
2998
2999#endif /* defined(FEAT_GUI) || defined(FEAT_PRINTER) */