blob: ce4420b33428bda292e87d3bd968e746772e8a66 [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
25#include <io.h>
26#include "vim.h"
27
28#ifdef HAVE_FCNTL_H
29# include <fcntl.h>
30#endif
31#ifdef WIN16
32# define SHORT_FNAME /* always 8.3 file name */
33# include <dos.h>
34# include <string.h>
35#endif
36#include <sys/types.h>
37#include <errno.h>
38#include <signal.h>
39#include <limits.h>
40#include <process.h>
41
42#undef chdir
43#ifdef __GNUC__
44# ifndef __MINGW32__
45# include <dirent.h>
46# endif
47#else
48# include <direct.h>
49#endif
50
51#if defined(FEAT_TITLE) && !defined(FEAT_GUI_W32)
52# include <shellapi.h>
53#endif
54
55#if defined(FEAT_PRINTER) && !defined(FEAT_POSTSCRIPT)
56# include <dlgs.h>
57# ifdef WIN3264
58# include <winspool.h>
59# else
60# include <print.h>
61# endif
62# include <commdlg.h>
63#endif
64
65#ifdef __MINGW32__
66# ifndef FROM_LEFT_1ST_BUTTON_PRESSED
67# define FROM_LEFT_1ST_BUTTON_PRESSED 0x0001
68# endif
69# ifndef RIGHTMOST_BUTTON_PRESSED
70# define RIGHTMOST_BUTTON_PRESSED 0x0002
71# endif
72# ifndef FROM_LEFT_2ND_BUTTON_PRESSED
73# define FROM_LEFT_2ND_BUTTON_PRESSED 0x0004
74# endif
75# ifndef FROM_LEFT_3RD_BUTTON_PRESSED
76# define FROM_LEFT_3RD_BUTTON_PRESSED 0x0008
77# endif
78# ifndef FROM_LEFT_4TH_BUTTON_PRESSED
79# define FROM_LEFT_4TH_BUTTON_PRESSED 0x0010
80# endif
81
82/*
83 * EventFlags
84 */
85# ifndef MOUSE_MOVED
86# define MOUSE_MOVED 0x0001
87# endif
88# ifndef DOUBLE_CLICK
89# define DOUBLE_CLICK 0x0002
90# endif
91#endif
92
93/*
94 * When generating prototypes for Win32 on Unix, these lines make the syntax
95 * errors disappear. They do not need to be correct.
96 */
97#ifdef PROTO
98#define WINAPI
99#define WINBASEAPI
100typedef int BOOL;
101typedef int CALLBACK;
102typedef int COLORREF;
103typedef int CONSOLE_CURSOR_INFO;
104typedef int COORD;
105typedef int DWORD;
106typedef int ENUMLOGFONT;
107typedef int HANDLE;
108typedef int HDC;
109typedef int HFONT;
110typedef int HICON;
111typedef int HWND;
112typedef int INPUT_RECORD;
113typedef int KEY_EVENT_RECORD;
114typedef int LOGFONT;
115typedef int LPARAM;
116typedef int LPBOOL;
117typedef int LPCSTR;
118typedef int LPCWSTR;
119typedef int LPSTR;
120typedef int LPTSTR;
121typedef int LPWSTR;
122typedef int LRESULT;
123typedef int MOUSE_EVENT_RECORD;
124typedef int NEWTEXTMETRIC;
125typedef int PACL;
126typedef int PRINTDLG;
127typedef int PSECURITY_DESCRIPTOR;
128typedef int PSID;
129typedef int SECURITY_INFORMATION;
130typedef int SHORT;
131typedef int SMALL_RECT;
132typedef int TEXTMETRIC;
133typedef int UINT;
134typedef int WCHAR;
135typedef int WORD;
136typedef int WPARAM;
137typedef void VOID;
138#endif
139
140/* Record all output and all keyboard & mouse input */
141/* #define MCH_WRITE_DUMP */
142
143#ifdef MCH_WRITE_DUMP
144FILE* fdDump = NULL;
145#endif
146
147#ifdef WIN3264
148extern DWORD g_PlatformId;
149#endif
150
151#ifndef FEAT_GUI_MSWIN
152extern char g_szOrigTitle[];
153#endif
154
155#ifdef FEAT_GUI
156extern HWND s_hwnd;
157#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158static HWND s_hwnd = 0; /* console window handle, set by GetConsoleHwnd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159#endif
160
161extern int WSInitialized;
162
163/* Don't generate prototypes here, because some systems do have these
164 * functions. */
165#if defined(__GNUC__) && !defined(PROTO)
166# ifndef __MINGW32__
167int _stricoll(char *a, char *b)
168{
169 // the ANSI-ish correct way is to use strxfrm():
170 char a_buff[512], b_buff[512]; // file names, so this is enough on Win32
171 strxfrm(a_buff, a, 512);
172 strxfrm(b_buff, b, 512);
173 return strcoll(a_buff, b_buff);
174}
175
176char * _fullpath(char *buf, char *fname, int len)
177{
178 LPTSTR toss;
179
180 return (char *)GetFullPathName(fname, len, buf, &toss);
181}
182# endif
183
184int _chdrive(int drive)
185{
186 char temp [3] = "-:";
187 temp[0] = drive + 'A' - 1;
188 return !SetCurrentDirectory(temp);
189}
190#else
191# ifdef __BORLANDC__
192/* being a more ANSI compliant compiler, BorlandC doesn't define _stricoll:
193 * but it does in BC 5.02! */
194# if __BORLANDC__ < 0x502
195int _stricoll(char *a, char *b)
196{
197# if 1
198 // this is fast but not correct:
199 return stricmp(a, b);
200# else
201 // the ANSI-ish correct way is to use strxfrm():
202 char a_buff[512], b_buff[512]; // file names, so this is enough on Win32
203 strxfrm(a_buff, a, 512);
204 strxfrm(b_buff, b, 512);
205 return strcoll(a_buff, b_buff);
206# endif
207}
208# endif
209# endif
210#endif
211
212
213#if defined(FEAT_GUI_MSWIN) || defined(PROTO)
214/*
215 * GUI version of mch_exit().
216 * Shut down and exit with status `r'
217 * Careful: mch_exit() may be called before mch_init()!
218 */
219 void
220mch_exit(int r)
221{
222 display_errors();
223
224 ml_close_all(TRUE); /* remove all memfiles */
225
226# ifdef FEAT_OLE
227 UninitOLE();
228# endif
229# ifdef FEAT_NETBEANS_INTG
230 if (WSInitialized)
231 {
232 WSInitialized = FALSE;
233 WSACleanup();
234 }
235# endif
236#ifdef DYNAMIC_GETTEXT
237 dyn_libintl_end();
238#endif
239
240 if (gui.in_use)
241 gui_exit(r);
242 exit(r);
243}
244
245#endif /* FEAT_GUI_MSWIN */
246
247
248/*
249 * Init the tables for toupper() and tolower().
250 */
251 void
252mch_early_init(void)
253{
254 int i;
255
256#ifdef WIN3264
257 PlatformId();
258#endif
259
260 /* Init the tables for toupper() and tolower() */
261 for (i = 0; i < 256; ++i)
262 toupper_tab[i] = tolower_tab[i] = i;
263#ifdef WIN3264
264 CharUpperBuff(toupper_tab, 256);
265 CharLowerBuff(tolower_tab, 256);
266#else
267 AnsiUpperBuff(toupper_tab, 256);
268 AnsiLowerBuff(tolower_tab, 256);
269#endif
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000270
271#if defined(FEAT_MBYTE) && !defined(FEAT_GUI)
272 (void)get_cmd_argsW(NULL);
273#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274}
275
276
277/*
278 * Return TRUE if the input comes from a terminal, FALSE otherwise.
279 */
280 int
281mch_input_isatty()
282{
283#ifdef FEAT_GUI_MSWIN
284 return OK; /* GUI always has a tty */
285#else
286 if (isatty(read_cmd_fd))
287 return TRUE;
288 return FALSE;
289#endif
290}
291
292#ifdef FEAT_TITLE
293/*
294 * mch_settitle(): set titlebar of our window
295 */
296 void
297mch_settitle(
298 char_u *title,
299 char_u *icon)
300{
301# ifdef FEAT_GUI_MSWIN
302 gui_mch_settitle(title, icon);
303# else
304 if (title != NULL)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000305 {
306# ifdef FEAT_MBYTE
307 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
308 {
309 /* Convert the title from 'encoding' to the active codepage. */
310 WCHAR *wp = enc_to_ucs2(title, NULL);
311 int n;
312
313 if (wp != NULL)
314 {
315 n = SetConsoleTitleW(wp);
316 vim_free(wp);
317 if (n != 0 || GetLastError() != ERROR_CALL_NOT_IMPLEMENTED)
318 return;
319 }
320 }
321# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322 SetConsoleTitle(title);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000323 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324# endif
325}
326
327
328/*
329 * Restore the window/icon title.
330 * which is one of:
331 * 1: Just restore title
332 * 2: Just restore icon (which we don't have)
333 * 3: Restore title and icon (which we don't have)
334 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000335/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336 void
337mch_restore_title(
338 int which)
339{
340#ifndef FEAT_GUI_MSWIN
341 mch_settitle((which & 1) ? g_szOrigTitle : NULL, NULL);
342#endif
343}
344
345
346/*
347 * Return TRUE if we can restore the title (we can)
348 */
349 int
350mch_can_restore_title()
351{
352 return TRUE;
353}
354
355
356/*
357 * Return TRUE if we can restore the icon title (we can't)
358 */
359 int
360mch_can_restore_icon()
361{
362 return FALSE;
363}
364#endif /* FEAT_TITLE */
365
366
367/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000368 * Get absolute file name into buffer "buf" of length "len" bytes,
369 * turning all '/'s into '\\'s and getting the correct case of each component
370 * of the file name. Append a (back)slash to a directory name.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371 * When 'shellslash' set do it the other way around.
372 * Return OK or FAIL.
373 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000374/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375 int
376mch_FullName(
377 char_u *fname,
378 char_u *buf,
379 int len,
380 int force)
381{
382 int nResult = FAIL;
383
384#ifdef __BORLANDC__
385 if (*fname == NUL) /* Borland behaves badly here - make it consistent */
386 nResult = mch_dirname(buf, len);
387 else
388#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000390#ifdef FEAT_MBYTE
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000391 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage
392# ifdef __BORLANDC__
393 /* Wide functions of Borland C 5.5 do not work on Windows 98. */
394 && g_PlatformId == VER_PLATFORM_WIN32_NT
395# endif
396 )
397 {
398 WCHAR *wname;
399 WCHAR wbuf[MAX_PATH];
400 char_u *cname = NULL;
401
402 /* Use the wide function:
403 * - convert the fname from 'encoding' to UCS2.
404 * - invoke _wfullpath()
405 * - convert the result from UCS2 to 'encoding'.
406 */
407 wname = enc_to_ucs2(fname, NULL);
408 if (wname != NULL && _wfullpath(wbuf, wname, MAX_PATH - 1) != NULL)
409 {
410 cname = ucs2_to_enc((short_u *)wbuf, NULL);
411 if (cname != NULL)
412 {
Bram Moolenaarfe3ca8d2005-07-18 21:43:02 +0000413 vim_strncpy(buf, cname, len - 1);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000414 nResult = OK;
415 }
416 }
417 vim_free(wname);
418 vim_free(cname);
419 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000420 if (nResult == FAIL) /* fall back to non-wide function */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000422 {
423 if (_fullpath(buf, fname, len - 1) == NULL)
424 {
Bram Moolenaarfe3ca8d2005-07-18 21:43:02 +0000425 /* failed, use relative path name */
426 vim_strncpy(buf, fname, len - 1);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000427 }
428 else
429 nResult = OK;
430 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000432
433#ifdef USE_FNAME_CASE
434 fname_case(buf, len);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000435#else
436 slash_adjust(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437#endif
438
439 return nResult;
440}
441
442
443/*
444 * Return TRUE if "fname" does not depend on the current directory.
445 */
446 int
447mch_isFullName(char_u *fname)
448{
449 char szName[_MAX_PATH + 1];
450
451 /* A name like "d:/foo" and "//server/share" is absolute */
452 if ((fname[0] && fname[1] == ':' && (fname[2] == '/' || fname[2] == '\\'))
453 || (fname[0] == fname[1] && (fname[0] == '/' || fname[0] == '\\')))
454 return TRUE;
455
456 /* A name that can't be made absolute probably isn't absolute. */
457 if (mch_FullName(fname, szName, _MAX_PATH, FALSE) == FAIL)
458 return FALSE;
459
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000460 return pathcmp(fname, szName, -1) == 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000461}
462
463/*
464 * Replace all slashes by backslashes.
465 * This used to be the other way around, but MS-DOS sometimes has problems
466 * with slashes (e.g. in a command name). We can't have mixed slashes and
467 * backslashes, because comparing file names will not work correctly. The
468 * commands that use a file name should try to avoid the need to type a
469 * backslash twice.
470 * When 'shellslash' set do it the other way around.
471 */
472 void
473slash_adjust(p)
474 char_u *p;
475{
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +0000476 while (*p)
477 {
478 if (*p == psepcN)
479 *p = psepc;
480 mb_ptr_adv(p);
481 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482}
483
484
485/*
486 * stat() can't handle a trailing '/' or '\', remove it first.
487 */
488 int
489vim_stat(const char *name, struct stat *stp)
490{
491 char buf[_MAX_PATH + 1];
492 char *p;
493
Bram Moolenaar84110ac2005-07-20 21:56:21 +0000494 vim_strncpy((char_u *)buf, (char_u *)name, _MAX_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495 p = buf + strlen(buf);
496 if (p > buf)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000497 mb_ptr_back(buf, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498 if (p > buf && (*p == '\\' || *p == '/') && p[-1] != ':')
499 *p = NUL;
500#ifdef FEAT_MBYTE
501 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage
502# ifdef __BORLANDC__
503 /* Wide functions of Borland C 5.5 do not work on Windows 98. */
504 && g_PlatformId == VER_PLATFORM_WIN32_NT
505# endif
506 )
507 {
508 WCHAR *wp = enc_to_ucs2(buf, NULL);
509 int n;
510
511 if (wp != NULL)
512 {
513 n = _wstat(wp, (struct _stat *)stp);
514 vim_free(wp);
515 if (n >= 0)
516 return n;
517 /* Retry with non-wide function (for Windows 98). Can't use
518 * GetLastError() here and it's unclear what errno gets set to if
519 * the _wstat() fails for missing wide functions. */
520 }
521 }
522#endif
523 return stat(buf, stp);
524}
525
526#if defined(FEAT_GUI_MSWIN) || defined(PROTO)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000527/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528 void
529mch_settmode(int tmode)
530{
531 /* nothing to do */
532}
533
534 int
535mch_get_shellsize(void)
536{
537 /* never used */
538 return OK;
539}
540
541 void
542mch_set_shellsize(void)
543{
544 /* never used */
545}
546
547/*
548 * Rows and/or Columns has changed.
549 */
550 void
551mch_new_shellsize(void)
552{
553 /* never used */
554}
555
556#endif
557
558/*
559 * We have no job control, so fake it by starting a new shell.
560 */
561 void
562mch_suspend()
563{
564 suspend_shell();
565}
566
567#if defined(USE_MCH_ERRMSG) || defined(PROTO)
568
569#ifdef display_errors
570# undef display_errors
571#endif
572
573/*
574 * Display the saved error message(s).
575 */
576 void
577display_errors()
578{
579 char *p;
580
581 if (error_ga.ga_data != NULL)
582 {
583 /* avoid putting up a message box with blanks only */
584 for (p = (char *)error_ga.ga_data; *p; ++p)
585 if (!isspace(*p))
586 {
Bram Moolenaar8089cae2005-02-05 21:35:05 +0000587#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588 /* Truncate a very long message, it will go off-screen. */
589 if (STRLEN(p) > 2000)
590 {
591 char_u *s = p + 2000 - 14;
592
593#ifdef FEAT_MBYTE
594 if (has_mbyte)
595 s -= (*mb_head_off)(p, s);
596#endif
597 STRCPY(s, _("...(truncated)"));
598 }
Bram Moolenaar8089cae2005-02-05 21:35:05 +0000599#endif
Bram Moolenaar748bf032005-02-02 23:04:36 +0000600
601 (void)gui_mch_dialog(VIM_ERROR, (char_u *)_("Error"),
602 p, (char_u *)_("&Ok"), 1, NULL);
603#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604#ifdef WIN3264
605 MessageBox(NULL, p, "Vim", MB_TASKMODAL|MB_SETFOREGROUND);
606#else
607 MessageBox(NULL, p, "Vim", MB_TASKMODAL);
608#endif
Bram Moolenaar748bf032005-02-02 23:04:36 +0000609#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610 break;
611 }
612 ga_clear(&error_ga);
613 }
614}
615#endif
616
617
618/*
619 * Return TRUE if "p" contain a wildcard that can be expanded by
620 * dos_expandpath().
621 */
622 int
623mch_has_exp_wildcard(char_u *p)
624{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000625 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 {
627 if (vim_strchr((char_u *)"?*[", *p) != NULL
628 || (*p == '~' && p[1] != NUL))
629 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630 }
631 return FALSE;
632}
633
634/*
635 * Return TRUE if "p" contain a wildcard or a "~1" kind of thing (could be a
636 * shortened file name).
637 */
638 int
639mch_has_wildcard(char_u *p)
640{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000641 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 {
643 if (vim_strchr((char_u *)
644# ifdef VIM_BACKTICK
645 "?*$[`"
646# else
647 "?*$["
648# endif
649 , *p) != NULL
650 || (*p == '~' && p[1] != NUL))
651 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652 }
653 return FALSE;
654}
655
656
657/*
658 * The normal _chdir() does not change the default drive. This one does.
659 * Returning 0 implies success; -1 implies failure.
660 */
661 int
662mch_chdir(char *path)
663{
664 if (path[0] == NUL) /* just checking... */
665 return -1;
666
667 if (isalpha(path[0]) && path[1] == ':') /* has a drive name */
668 {
669 /* If we can change to the drive, skip that part of the path. If we
670 * can't then the current directory may be invalid, try using chdir()
671 * with the whole path. */
672 if (_chdrive(TOLOWER_ASC(path[0]) - 'a' + 1) == 0)
673 path += 2;
674 }
675
676 if (*path == NUL) /* drive name only */
677 return 0;
678
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000679#ifdef FEAT_MBYTE
680 if (enc_codepage >= 0 && (int)GetACP() != enc_codepage)
681 {
682 WCHAR *p = enc_to_ucs2(path, NULL);
683 int n;
684
685 if (p != NULL)
686 {
687 n = _wchdir(p);
688 vim_free(p);
689 if (n == 0)
690 return 0;
691 /* Retry with non-wide function (for Windows 98). */
692 }
693 }
694#endif
695
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 return chdir(path); /* let the normal chdir() do the rest */
697}
698
699
700/*
701 * Switching off termcap mode is only allowed when Columns is 80, otherwise a
702 * crash may result. It's always allowed on NT or when running the GUI.
703 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000704/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 int
706can_end_termcap_mode(
707 int give_msg)
708{
709#ifdef FEAT_GUI_MSWIN
710 return TRUE; /* GUI starts a new console anyway */
711#else
712 if (g_PlatformId == VER_PLATFORM_WIN32_NT || Columns == 80)
713 return TRUE;
714 if (give_msg)
715 msg(_("'columns' is not 80, cannot execute external commands"));
716 return FALSE;
717#endif
718}
719
720#ifdef FEAT_GUI_MSWIN
721/*
722 * return non-zero if a character is available
723 */
724 int
725mch_char_avail()
726{
727 /* never used */
728 return TRUE;
729}
730#endif
731
732
733/*
734 * set screen mode, always fails.
735 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000736/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 int
738mch_screenmode(
739 char_u *arg)
740{
741 EMSG(_(e_screenmode));
742 return FAIL;
743}
744
745
746#if defined(FEAT_LIBCALL) || defined(PROTO)
747/*
748 * Call a DLL routine which takes either a string or int param
749 * and returns an allocated string.
750 * Return OK if it worked, FAIL if not.
751 */
752# ifdef WIN3264
753typedef LPTSTR (*MYSTRPROCSTR)(LPTSTR);
754typedef LPTSTR (*MYINTPROCSTR)(int);
755typedef int (*MYSTRPROCINT)(LPTSTR);
756typedef int (*MYINTPROCINT)(int);
757# else
758typedef LPSTR (*MYSTRPROCSTR)(LPSTR);
759typedef LPSTR (*MYINTPROCSTR)(int);
760typedef int (*MYSTRPROCINT)(LPSTR);
761typedef int (*MYINTPROCINT)(int);
762# endif
763
764# ifndef WIN16
765/*
766 * Check if a pointer points to a valid NUL terminated string.
767 * Return the length of the string, including terminating NUL.
768 * Returns 0 for an invalid pointer, 1 for an empty string.
769 */
770 static size_t
771check_str_len(char_u *str)
772{
773 SYSTEM_INFO si;
774 MEMORY_BASIC_INFORMATION mbi;
775 size_t length = 0;
776 size_t i;
777 const char *p;
778
779 /* get page size */
780 GetSystemInfo(&si);
781
782 /* get memory information */
783 if (VirtualQuery(str, &mbi, sizeof(mbi)))
784 {
785 /* pre cast these (typing savers) */
786 DWORD dwStr = (DWORD)str;
787 DWORD dwBaseAddress = (DWORD)mbi.BaseAddress;
788
789 /* get start address of page that str is on */
790 DWORD strPage = dwStr - (dwStr - dwBaseAddress) % si.dwPageSize;
791
792 /* get length from str to end of page */
793 DWORD pageLength = si.dwPageSize - (dwStr - strPage);
794
795 for (p = str; !IsBadReadPtr(p, pageLength);
796 p += pageLength, pageLength = si.dwPageSize)
797 for (i = 0; i < pageLength; ++i, ++length)
798 if (p[i] == NUL)
799 return length + 1;
800 }
801
802 return 0;
803}
804# endif
805
806 int
807mch_libcall(
808 char_u *libname,
809 char_u *funcname,
810 char_u *argstring, /* NULL when using a argint */
811 int argint,
812 char_u **string_result,/* NULL when using number_result */
813 int *number_result)
814{
815 HINSTANCE hinstLib;
816 MYSTRPROCSTR ProcAdd;
817 MYINTPROCSTR ProcAddI;
818 char_u *retval_str = NULL;
819 int retval_int = 0;
820 size_t len;
821
822 BOOL fRunTimeLinkSuccess = FALSE;
823
824 // Get a handle to the DLL module.
825 hinstLib = LoadLibrary(libname);
826
827 // If the handle is valid, try to get the function address.
828 if (hinstLib != NULL)
829 {
830#ifdef HAVE_TRY_EXCEPT
831 __try
832 {
833#endif
834 if (argstring != NULL)
835 {
836 /* Call with string argument */
837 ProcAdd = (MYSTRPROCSTR) GetProcAddress(hinstLib, funcname);
838 if ((fRunTimeLinkSuccess = (ProcAdd != NULL)) != 0)
839 {
840 if (string_result == NULL)
841 retval_int = ((MYSTRPROCINT)ProcAdd)(argstring);
842 else
843 retval_str = (ProcAdd)(argstring);
844 }
845 }
846 else
847 {
848 /* Call with number argument */
849 ProcAddI = (MYINTPROCSTR) GetProcAddress(hinstLib, funcname);
850 if ((fRunTimeLinkSuccess = (ProcAddI != NULL)) != 0)
851 {
852 if (string_result == NULL)
853 retval_int = ((MYINTPROCINT)ProcAddI)(argint);
854 else
855 retval_str = (ProcAddI)(argint);
856 }
857 }
858
859 // Save the string before we free the library.
860 // Assume that a "1" result is an illegal pointer.
861 if (string_result == NULL)
862 *number_result = retval_int;
863 else if (retval_str != NULL
864# ifdef WIN16
865 && retval_str != (char_u *)1
866 && retval_str != (char_u *)-1
867 && !IsBadStringPtr(retval_str, INT_MAX)
868 && (len = strlen(retval_str) + 1) > 0
869# else
870 && (len = check_str_len(retval_str)) > 0
871# endif
872 )
873 {
874 *string_result = lalloc((long_u)len, TRUE);
875 if (*string_result != NULL)
876 mch_memmove(*string_result, retval_str, len);
877 }
878
879#ifdef HAVE_TRY_EXCEPT
880 }
881 __except(EXCEPTION_EXECUTE_HANDLER)
882 {
883 if (GetExceptionCode() == EXCEPTION_STACK_OVERFLOW)
884 RESETSTKOFLW();
885 fRunTimeLinkSuccess = 0;
886 }
887#endif
888
889 // Free the DLL module.
890 (void)FreeLibrary(hinstLib);
891 }
892
893 if (!fRunTimeLinkSuccess)
894 {
895 EMSG2(_(e_libcall), funcname);
896 return FAIL;
897 }
898
899 return OK;
900}
901#endif
902
903#if defined(FEAT_MBYTE) || defined(PROTO)
904/*
905 * Convert an UTF-8 string to UCS-2.
906 * "instr[inlen]" is the input. "inlen" is in bytes.
907 * When "outstr" is NULL only return the number of UCS-2 words produced.
908 * Otherwise "outstr" must be a buffer of sufficient size.
909 * Returns the number of UCS-2 words produced.
910 */
911 int
912utf8_to_ucs2(char_u *instr, int inlen, short_u *outstr, int *unconvlenp)
913{
914 int outlen = 0;
915 char_u *p = instr;
916 int todo = inlen;
917 int l;
918
919 while (todo > 0)
920 {
921 /* Only convert if we have a complete sequence. */
922 l = utf_ptr2len_check_len(p, todo);
923 if (l > todo)
924 {
925 /* Return length of incomplete sequence. */
926 if (unconvlenp != NULL)
927 *unconvlenp = todo;
928 break;
929 }
930
931 if (outstr != NULL)
932 *outstr++ = utf_ptr2char(p);
933 ++outlen;
934 p += l;
935 todo -= l;
936 }
937
938 return outlen;
939}
940
941/*
942 * Convert an UCS-2 string to UTF-8.
943 * The input is "instr[inlen]" with "inlen" in number of ucs-2 words.
944 * When "outstr" is NULL only return the required number of bytes.
945 * Otherwise "outstr" must be a buffer of sufficient size.
946 * Return the number of bytes produced.
947 */
948 int
949ucs2_to_utf8(short_u *instr, int inlen, char_u *outstr)
950{
951 int outlen = 0;
952 int todo = inlen;
953 short_u *p = instr;
954 int l;
955
956 while (todo > 0)
957 {
958 if (outstr != NULL)
959 {
960 l = utf_char2bytes(*p, outstr);
961 outstr += l;
962 }
963 else
964 l = utf_char2len(*p);
965 ++p;
966 outlen += l;
967 --todo;
968 }
969
970 return outlen;
971}
972
973/*
974 * Call MultiByteToWideChar() and allocate memory for the result.
975 * Returns the result in "*out[*outlen]" with an extra zero appended.
976 * "outlen" is in words.
977 */
978 void
979MultiByteToWideChar_alloc(UINT cp, DWORD flags,
980 LPCSTR in, int inlen,
981 LPWSTR *out, int *outlen)
982{
983 *outlen = MultiByteToWideChar(cp, flags, in, inlen, 0, 0);
984 /* Add one one word to avoid a zero-length alloc(). */
985 *out = (LPWSTR)alloc(sizeof(WCHAR) * (*outlen + 1));
986 if (*out != NULL)
987 {
988 MultiByteToWideChar(cp, flags, in, inlen, *out, *outlen);
989 (*out)[*outlen] = 0;
990 }
991}
992
993/*
994 * Call WideCharToMultiByte() and allocate memory for the result.
995 * Returns the result in "*out[*outlen]" with an extra NUL appended.
996 */
997 void
998WideCharToMultiByte_alloc(UINT cp, DWORD flags,
999 LPCWSTR in, int inlen,
1000 LPSTR *out, int *outlen,
1001 LPCSTR def, LPBOOL useddef)
1002{
1003 *outlen = WideCharToMultiByte(cp, flags, in, inlen, NULL, 0, def, useddef);
1004 /* Add one one byte to avoid a zero-length alloc(). */
1005 *out = alloc((unsigned)*outlen + 1);
1006 if (*out != NULL)
1007 {
1008 WideCharToMultiByte(cp, flags, in, inlen, *out, *outlen, def, useddef);
1009 (*out)[*outlen] = 0;
1010 }
1011}
1012
1013#endif /* FEAT_MBYTE */
1014
1015#ifdef FEAT_CLIPBOARD
1016/*
1017 * Clipboard stuff, for cutting and pasting text to other windows.
1018 */
1019
1020/* Type used for the clipboard type of Vim's data. */
1021typedef struct
1022{
1023 int type; /* MCHAR, MBLOCK or MLINE */
1024 int txtlen; /* length of CF_TEXT in bytes */
1025 int ucslen; /* length of CF_UNICODETEXT in words */
1026 int rawlen; /* length of clip_star.format_raw, including encoding,
1027 excluding terminating NUL */
1028} VimClipType_t;
1029
1030/*
1031 * Make vim the owner of the current selection. Return OK upon success.
1032 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001033/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 int
1035clip_mch_own_selection(VimClipboard *cbd)
1036{
1037 /*
1038 * Never actually own the clipboard. If another application sets the
1039 * clipboard, we don't want to think that we still own it.
1040 */
1041 return FAIL;
1042}
1043
1044/*
1045 * Make vim NOT the owner of the current selection.
1046 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001047/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 void
1049clip_mch_lose_selection(VimClipboard *cbd)
1050{
1051 /* Nothing needs to be done here */
1052}
1053
1054/*
1055 * Copy "str[*size]" into allocated memory, changing CR-NL to NL.
1056 * Return the allocated result and the size in "*size".
1057 * Returns NULL when out of memory.
1058 */
1059 static char_u *
1060crnl_to_nl(const char_u *str, int *size)
1061{
1062 int pos = 0;
1063 int str_len = *size;
1064 char_u *ret;
1065 char_u *retp;
1066
1067 /* Avoid allocating zero bytes, it generates an error message. */
1068 ret = lalloc((long_u)(str_len == 0 ? 1 : str_len), TRUE);
1069 if (ret != NULL)
1070 {
1071 retp = ret;
1072 for (pos = 0; pos < str_len; ++pos)
1073 {
1074 if (str[pos] == '\r' && str[pos + 1] == '\n')
1075 {
1076 ++pos;
1077 --(*size);
1078 }
1079 *retp++ = str[pos];
1080 }
1081 }
1082
1083 return ret;
1084}
1085
1086#if defined(FEAT_MBYTE) || defined(PROTO)
1087/*
1088 * Note: the following two functions are only guaranteed to work when using
1089 * valid MS-Windows codepages or when iconv() is available.
1090 */
1091
1092/*
1093 * Convert "str" from 'encoding' to UCS-2.
1094 * Input in "str" with length "*lenp". When "lenp" is NULL, use strlen().
1095 * Output is returned as an allocated string. "*lenp" is set to the length of
1096 * the result. A trailing NUL is always added.
1097 * Returns NULL when out of memory.
1098 */
1099 short_u *
1100enc_to_ucs2(char_u *str, int *lenp)
1101{
1102 vimconv_T conv;
1103 WCHAR *ret;
1104 char_u *allocbuf = NULL;
1105 int len_loc;
1106 int length;
1107
1108 if (lenp == NULL)
1109 {
1110 len_loc = STRLEN(str) + 1;
1111 lenp = &len_loc;
1112 }
1113
1114 if (enc_codepage > 0)
1115 {
1116 /* We can do any CP### -> UCS-2 in one pass, and we can do it
1117 * without iconv() (convert_* may need iconv). */
1118 MultiByteToWideChar_alloc(enc_codepage, 0, str, *lenp, &ret, &length);
1119 }
1120 else
1121 {
1122 /* Use "latin1" by default, we might be called before we have p_enc
1123 * set up. Convert to utf-8 first, works better with iconv(). Does
1124 * nothing if 'encoding' is "utf-8". */
1125 conv.vc_type = CONV_NONE;
1126 if (convert_setup(&conv, p_enc ? p_enc : (char_u *)"latin1",
1127 (char_u *)"utf-8") == FAIL)
1128 return NULL;
1129 if (conv.vc_type != CONV_NONE)
1130 {
1131 str = allocbuf = string_convert(&conv, str, lenp);
1132 if (str == NULL)
1133 return NULL;
1134 }
1135 convert_setup(&conv, NULL, NULL);
1136
1137 length = utf8_to_ucs2(str, *lenp, NULL, NULL);
1138 ret = (WCHAR *)alloc((unsigned)((length + 1) * sizeof(WCHAR)));
1139 if (ret != NULL)
1140 {
1141 utf8_to_ucs2(str, *lenp, (short_u *)ret, NULL);
1142 ret[length] = 0;
1143 }
1144
1145 vim_free(allocbuf);
1146 }
1147
1148 *lenp = length;
1149 return (short_u *)ret;
1150}
1151
1152/*
1153 * Convert an UCS-2 string to 'encoding'.
1154 * Input in "str" with length (counted in wide characters) "*lenp". When
1155 * "lenp" is NULL, use wcslen().
1156 * Output is returned as an allocated string. If "*lenp" is not NULL it is
1157 * set to the length of the result.
1158 * Returns NULL when out of memory.
1159 */
1160 char_u *
1161ucs2_to_enc(short_u *str, int *lenp)
1162{
1163 vimconv_T conv;
1164 char_u *utf8_str = NULL, *enc_str = NULL;
1165 int len_loc;
1166
1167 if (lenp == NULL)
1168 {
1169 len_loc = wcslen(str) + 1;
1170 lenp = &len_loc;
1171 }
1172
1173 if (enc_codepage > 0)
1174 {
1175 /* We can do any UCS-2 -> CP### in one pass. */
1176 int length;
1177
1178 WideCharToMultiByte_alloc(enc_codepage, 0, str, *lenp,
1179 (LPSTR *)&enc_str, &length, 0, 0);
1180 *lenp = length;
1181 return enc_str;
1182 }
1183
1184 /* Avoid allocating zero bytes, it generates an error message. */
1185 utf8_str = alloc(ucs2_to_utf8(str, *lenp == 0 ? 1 : *lenp, NULL));
1186 if (utf8_str != NULL)
1187 {
1188 *lenp = ucs2_to_utf8(str, *lenp, utf8_str);
1189
1190 /* We might be called before we have p_enc set up. */
1191 conv.vc_type = CONV_NONE;
1192 convert_setup(&conv, (char_u *)"utf-8",
1193 p_enc? p_enc: (char_u *)"latin1");
1194 if (conv.vc_type == CONV_NONE)
1195 {
1196 /* p_enc is utf-8, so we're done. */
1197 enc_str = utf8_str;
1198 }
1199 else
1200 {
1201 enc_str = string_convert(&conv, utf8_str, lenp);
1202 vim_free(utf8_str);
1203 }
1204
1205 convert_setup(&conv, NULL, NULL);
1206 }
1207
1208 return enc_str;
1209}
1210#endif /* FEAT_MBYTE */
1211
1212/*
1213 * Get the current selection and put it in the clipboard register.
1214 *
1215 * NOTE: Must use GlobalLock/Unlock here to ensure Win32s compatibility.
1216 * On NT/W95 the clipboard data is a fixed global memory object and
1217 * so its handle = its pointer.
1218 * On Win32s, however, co-operation with the Win16 system means that
1219 * the clipboard data is moveable and its handle is not a pointer at all,
1220 * so we can't just cast the return value of GetClipboardData to (char_u*).
1221 * <VN>
1222 */
1223 void
1224clip_mch_request_selection(VimClipboard *cbd)
1225{
1226 VimClipType_t metadata = { -1, -1, -1, -1 };
1227 HGLOBAL hMem = NULL;
1228 char_u *str = NULL;
1229#if defined(FEAT_MBYTE) && defined(WIN3264)
1230 char_u *to_free = NULL;
1231#endif
1232#ifdef FEAT_MBYTE
1233 HGLOBAL rawh = NULL;
1234#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 int str_size = 0;
1236 int maxlen;
1237 size_t n;
1238
1239 /*
1240 * Don't pass GetActiveWindow() as an argument to OpenClipboard() because
1241 * then we can't paste back into the same window for some reason - webb.
1242 */
1243 if (!OpenClipboard(NULL))
1244 return;
1245
1246 /* Check for vim's own clipboard format first. This only gets the type of
1247 * the data, still need to use CF_UNICODETEXT or CF_TEXT for the text. */
1248 if (IsClipboardFormatAvailable(cbd->format))
1249 {
1250 VimClipType_t *meta_p;
1251 HGLOBAL meta_h;
1252
1253 /* We have metadata on the clipboard; try to get it. */
1254 if ((meta_h = GetClipboardData(cbd->format)) != NULL
1255 && (meta_p = (VimClipType_t *)GlobalLock(meta_h)) != NULL)
1256 {
1257 /* The size of "VimClipType_t" changed, "rawlen" was added later.
1258 * Only copy what is available for backwards compatibility. */
1259 n = sizeof(VimClipType_t);
1260 if (GlobalSize(meta_h) < n)
1261 n = GlobalSize(meta_h);
1262 memcpy(&metadata, meta_p, n);
1263 GlobalUnlock(meta_h);
1264 }
1265 }
1266
1267#ifdef FEAT_MBYTE
1268 /* Check for Vim's raw clipboard format first. This is used without
1269 * conversion, but only if 'encoding' matches. */
1270 if (IsClipboardFormatAvailable(cbd->format_raw)
1271 && metadata.rawlen > (int)STRLEN(p_enc))
1272 {
1273 /* We have raw data on the clipboard; try to get it. */
1274 if ((rawh = GetClipboardData(cbd->format_raw)) != NULL)
1275 {
1276 char_u *rawp;
1277
1278 rawp = (char_u *)GlobalLock(rawh);
1279 if (rawp != NULL && STRCMP(p_enc, rawp) == 0)
1280 {
1281 n = STRLEN(p_enc) + 1;
1282 str = rawp + n;
1283 str_size = metadata.rawlen - n;
1284 }
1285 else
1286 {
1287 GlobalUnlock(rawh);
1288 rawh = NULL;
1289 }
1290 }
1291 }
1292 if (str == NULL)
1293 {
1294#endif
1295
1296#if defined(FEAT_MBYTE) && defined(WIN3264)
1297 /* Try to get the clipboard in Unicode if it's not an empty string. */
1298 if (IsClipboardFormatAvailable(CF_UNICODETEXT) && metadata.ucslen != 0)
1299 {
1300 HGLOBAL hMemW;
1301
1302 if ((hMemW = GetClipboardData(CF_UNICODETEXT)) != NULL)
1303 {
1304 WCHAR *hMemWstr = (WCHAR *)GlobalLock(hMemW);
1305
1306 /* Use the length of our metadata if possible, but limit it to the
1307 * GlobalSize() for safety. */
1308 maxlen = GlobalSize(hMemW) / sizeof(WCHAR);
1309 if (metadata.ucslen >= 0)
1310 {
1311 if (metadata.ucslen > maxlen)
1312 str_size = maxlen;
1313 else
1314 str_size = metadata.ucslen;
1315 }
1316 else
1317 {
1318 for (str_size = 0; str_size < maxlen; ++str_size)
1319 if (hMemWstr[str_size] == NUL)
1320 break;
1321 }
1322 to_free = str = ucs2_to_enc((short_u *)hMemWstr, &str_size);
1323 GlobalUnlock(hMemW);
1324 }
1325 }
1326 else
1327#endif
1328 /* Get the clipboard in the Active codepage. */
1329 if (IsClipboardFormatAvailable(CF_TEXT))
1330 {
1331 if ((hMem = GetClipboardData(CF_TEXT)) != NULL)
1332 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001333 str = (char_u *)GlobalLock(hMem);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334
1335 /* The length is either what our metadata says or the strlen().
1336 * But limit it to the GlobalSize() for safety. */
1337 maxlen = GlobalSize(hMem);
1338 if (metadata.txtlen >= 0)
1339 {
1340 if (metadata.txtlen > maxlen)
1341 str_size = maxlen;
1342 else
1343 str_size = metadata.txtlen;
1344 }
1345 else
1346 {
1347 for (str_size = 0; str_size < maxlen; ++str_size)
1348 if (str[str_size] == NUL)
1349 break;
1350 }
1351
Bram Moolenaar05159a02005-02-26 23:04:13 +00001352# if defined(FEAT_MBYTE) && defined(WIN3264)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 /* The text is in the active codepage. Convert to 'encoding',
1354 * going through UCS-2. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001355 acp_to_enc(str, str_size, &to_free, &maxlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 if (to_free != NULL)
1357 {
1358 str_size = maxlen;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001359 str = to_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00001361# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 }
1363 }
1364#ifdef FEAT_MBYTE
1365 }
1366#endif
1367
1368 if (str != NULL && *str != NUL)
1369 {
1370 char_u *temp_clipboard;
1371
1372 /* If the type is not known guess it. */
1373 if (metadata.type == -1)
1374 metadata.type = (vim_strchr(str, '\n') == NULL) ? MCHAR : MLINE;
1375
1376 /* Translate <CR><NL> into <NL>. */
1377 temp_clipboard = crnl_to_nl(str, &str_size);
1378 if (temp_clipboard != NULL)
1379 {
1380 clip_yank_selection(metadata.type, temp_clipboard, str_size, cbd);
1381 vim_free(temp_clipboard);
1382 }
1383 }
1384
1385 /* unlock the global object */
1386 if (hMem != NULL)
1387 GlobalUnlock(hMem);
1388#ifdef FEAT_MBYTE
1389 if (rawh != NULL)
1390 GlobalUnlock(rawh);
1391#endif
1392 CloseClipboard();
1393#if defined(FEAT_MBYTE) && defined(WIN3264)
1394 vim_free(to_free);
1395#endif
1396}
1397
Bram Moolenaar05159a02005-02-26 23:04:13 +00001398#if (defined(FEAT_MBYTE) && defined(WIN3264)) || defined(PROTO)
1399/*
1400 * Convert from the active codepage to 'encoding'.
1401 * Input is "str[str_size]".
1402 * The result is in allocated memory: "out[outlen]". With terminating NUL.
1403 */
1404 void
1405acp_to_enc(str, str_size, out, outlen)
1406 char_u *str;
1407 int str_size;
1408 char_u **out;
1409 int *outlen;
1410
1411{
1412 LPWSTR widestr;
1413
1414 MultiByteToWideChar_alloc(GetACP(), 0, str, str_size, &widestr, outlen);
1415 if (widestr != NULL)
1416 {
Bram Moolenaar44ecf652005-03-07 23:09:59 +00001417 ++*outlen; /* Include the 0 after the string */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001418 *out = ucs2_to_enc((short_u *)widestr, outlen);
1419 vim_free(widestr);
1420 }
1421}
1422#endif
1423
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424/*
1425 * Send the current selection to the clipboard.
1426 */
1427 void
1428clip_mch_set_selection(VimClipboard *cbd)
1429{
1430 char_u *str = NULL;
1431 VimClipType_t metadata;
1432 long_u txtlen;
1433 HGLOBAL hMemRaw = NULL;
1434 HGLOBAL hMem = NULL;
1435 HGLOBAL hMemVim = NULL;
1436# if defined(FEAT_MBYTE) && defined(WIN3264)
1437 HGLOBAL hMemW = NULL;
1438# endif
1439
1440 /* If the '*' register isn't already filled in, fill it in now */
1441 cbd->owned = TRUE;
1442 clip_get_selection(cbd);
1443 cbd->owned = FALSE;
1444
1445 /* Get the text to be put on the clipboard, with CR-LF. */
1446 metadata.type = clip_convert_selection(&str, &txtlen, cbd);
1447 if (metadata.type < 0)
1448 return;
1449 metadata.txtlen = (int)txtlen;
1450 metadata.ucslen = 0;
1451 metadata.rawlen = 0;
1452
1453#ifdef FEAT_MBYTE
1454 /* Always set the raw bytes: 'encoding', NUL and the text. This is used
1455 * when copy/paste from/to Vim with the same 'encoding', so that illegal
1456 * bytes can also be copied and no conversion is needed. */
1457 {
1458 LPSTR lpszMemRaw;
1459
1460 metadata.rawlen = txtlen + STRLEN(p_enc) + 1;
1461 hMemRaw = (LPSTR)GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE,
1462 metadata.rawlen + 1);
1463 lpszMemRaw = (LPSTR)GlobalLock(hMemRaw);
1464 if (lpszMemRaw != NULL)
1465 {
1466 STRCPY(lpszMemRaw, p_enc);
1467 memcpy(lpszMemRaw + STRLEN(p_enc) + 1, str, txtlen + 1);
1468 GlobalUnlock(hMemRaw);
1469 }
1470 else
1471 metadata.rawlen = 0;
1472 }
1473#endif
1474
1475# if defined(FEAT_MBYTE) && defined(WIN3264)
1476 {
1477 WCHAR *out;
1478 int len = metadata.txtlen;
1479
1480 /* Convert the text to UCS-2. This is put on the clipboard as
1481 * CF_UNICODETEXT. */
1482 out = (WCHAR *)enc_to_ucs2(str, &len);
1483 if (out != NULL)
1484 {
1485 WCHAR *lpszMemW;
1486
1487 /* Convert the text for CF_TEXT to Active codepage. Otherwise it's
1488 * p_enc, which has no relation to the Active codepage. */
1489 metadata.txtlen = WideCharToMultiByte(GetACP(), 0, out, len,
1490 NULL, 0, 0, 0);
1491 vim_free(str);
1492 str = (char_u *)alloc((unsigned)(metadata.txtlen == 0 ? 1
1493 : metadata.txtlen));
1494 if (str == NULL)
1495 {
1496 vim_free(out);
1497 return; /* out of memory */
1498 }
1499 WideCharToMultiByte(GetACP(), 0, out, len,
1500 str, metadata.txtlen, 0, 0);
1501
1502 /* Allocate memory for the UCS-2 text, add one NUL word to
1503 * terminate the string. */
1504 hMemW = (LPSTR)GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE,
1505 (len + 1) * sizeof(WCHAR));
1506 lpszMemW = (WCHAR *)GlobalLock(hMemW);
1507 if (lpszMemW != NULL)
1508 {
1509 memcpy(lpszMemW, out, len * sizeof(WCHAR));
1510 lpszMemW[len] = NUL;
1511 GlobalUnlock(hMemW);
1512 }
1513 vim_free(out);
1514 metadata.ucslen = len;
1515 }
1516 }
1517# endif
1518
1519 /* Allocate memory for the text, add one NUL byte to terminate the string.
1520 */
1521 hMem = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, metadata.txtlen + 1);
1522 {
1523 LPSTR lpszMem = (LPSTR)GlobalLock(hMem);
1524
1525 if (lpszMem)
1526 {
Bram Moolenaarfe3ca8d2005-07-18 21:43:02 +00001527 vim_strncpy(lpszMem, str, metadata.txtlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 GlobalUnlock(hMem);
1529 }
1530 }
1531
1532 /* Set up metadata: */
1533 {
1534 VimClipType_t *lpszMemVim = NULL;
1535
1536 hMemVim = GlobalAlloc(GMEM_MOVEABLE|GMEM_DDESHARE,
1537 sizeof(VimClipType_t));
1538 lpszMemVim = (VimClipType_t *)GlobalLock(hMemVim);
1539 memcpy(lpszMemVim, &metadata, sizeof(metadata));
1540 GlobalUnlock(hMemVim);
1541 }
1542
1543 /*
1544 * Open the clipboard, clear it and put our text on it.
1545 * Always set our Vim format. Put Unicode and plain text on it.
1546 *
1547 * Don't pass GetActiveWindow() as an argument to OpenClipboard()
1548 * because then we can't paste back into the same window for some
1549 * reason - webb.
1550 */
1551 if (OpenClipboard(NULL))
1552 {
1553 if (EmptyClipboard())
1554 {
1555 SetClipboardData(cbd->format, hMemVim);
1556 hMemVim = 0;
1557# if defined(FEAT_MBYTE) && defined(WIN3264)
1558 if (hMemW != NULL)
1559 {
1560 if (SetClipboardData(CF_UNICODETEXT, hMemW) != NULL)
1561 hMemW = NULL;
1562 }
1563# endif
1564 /* Always use CF_TEXT. On Win98 Notepad won't obtain the
1565 * CF_UNICODETEXT text, only CF_TEXT. */
1566 SetClipboardData(CF_TEXT, hMem);
1567 hMem = 0;
1568 }
1569 CloseClipboard();
1570 }
1571
1572 vim_free(str);
1573 /* Free any allocations we didn't give to the clipboard: */
1574 if (hMemRaw)
1575 GlobalFree(hMemRaw);
1576 if (hMem)
1577 GlobalFree(hMem);
1578# if defined(FEAT_MBYTE) && defined(WIN3264)
1579 if (hMemW)
1580 GlobalFree(hMemW);
1581# endif
1582 if (hMemVim)
1583 GlobalFree(hMemVim);
1584}
1585
1586#endif /* FEAT_CLIPBOARD */
1587
1588
1589/*
1590 * Debugging helper: expose the MCH_WRITE_DUMP stuff to other modules
1591 */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001592/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 void
1594DumpPutS(
1595 const char *psz)
1596{
1597# ifdef MCH_WRITE_DUMP
1598 if (fdDump)
1599 {
1600 fputs(psz, fdDump);
1601 if (psz[strlen(psz) - 1] != '\n')
1602 fputc('\n', fdDump);
1603 fflush(fdDump);
1604 }
1605# endif
1606}
1607
1608#ifdef _DEBUG
1609
1610void __cdecl
1611Trace(
1612 char *pszFormat,
1613 ...)
1614{
1615 CHAR szBuff[2048];
1616 va_list args;
1617
1618 va_start(args, pszFormat);
1619 vsprintf(szBuff, pszFormat, args);
1620 va_end(args);
1621
1622 OutputDebugString(szBuff);
1623}
1624
1625#endif //_DEBUG
1626
Bram Moolenaar843ee412004-06-30 16:16:41 +00001627#if !defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628# if defined(FEAT_TITLE) && defined(WIN3264)
1629extern HWND g_hWnd; /* This is in os_win32.c. */
1630# endif
1631
1632/*
1633 * Showing the printer dialog is tricky since we have no GUI
1634 * window to parent it. The following routines are needed to
1635 * get the window parenting and Z-order to work properly.
1636 */
1637 static void
1638GetConsoleHwnd(void)
1639{
1640# define MY_BUFSIZE 1024 // Buffer size for console window titles.
1641
1642 char pszNewWindowTitle[MY_BUFSIZE]; // Contains fabricated WindowTitle.
1643 char pszOldWindowTitle[MY_BUFSIZE]; // Contains original WindowTitle.
1644
1645 /* Skip if it's already set. */
1646 if (s_hwnd != 0)
1647 return;
1648
1649# if defined(FEAT_TITLE) && defined(WIN3264)
1650 /* Window handle may have been found by init code (Windows NT only) */
1651 if (g_hWnd != 0)
1652 {
1653 s_hwnd = g_hWnd;
1654 return;
1655 }
1656# endif
1657
1658 GetConsoleTitle(pszOldWindowTitle, MY_BUFSIZE);
1659
1660 wsprintf(pszNewWindowTitle, "%s/%d/%d",
1661 pszOldWindowTitle,
1662 GetTickCount(),
1663 GetCurrentProcessId());
1664 SetConsoleTitle(pszNewWindowTitle);
1665 Sleep(40);
1666 s_hwnd = FindWindow(NULL, pszNewWindowTitle);
1667
1668 SetConsoleTitle(pszOldWindowTitle);
1669}
Bram Moolenaar843ee412004-06-30 16:16:41 +00001670
1671/*
1672 * Console implementation of ":winpos".
1673 */
1674 int
1675mch_get_winpos(int *x, int *y)
1676{
1677 RECT rect;
1678
1679 GetConsoleHwnd();
1680 GetWindowRect(s_hwnd, &rect);
1681 *x = rect.left;
1682 *y = rect.top;
1683 return OK;
1684}
1685
1686/*
1687 * Console implementation of ":winpos x y".
1688 */
1689 void
1690mch_set_winpos(int x, int y)
1691{
1692 GetConsoleHwnd();
1693 SetWindowPos(s_hwnd, NULL, x, y, 0, 0,
1694 SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
1695}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696#endif
1697
1698#if (defined(FEAT_PRINTER) && !defined(FEAT_POSTSCRIPT)) || defined(PROTO)
1699
1700# ifdef WIN16
1701# define TEXT(a) a
1702# endif
1703/*=================================================================
1704 * Win32 printer stuff
1705 */
1706
1707static HFONT prt_font_handles[2][2][2];
1708static PRINTDLG prt_dlg;
1709static const int boldface[2] = {FW_REGULAR, FW_BOLD};
1710static TEXTMETRIC prt_tm;
1711static int prt_line_height;
1712static int prt_number_width;
1713static int prt_left_margin;
1714static int prt_right_margin;
1715static int prt_top_margin;
1716static char_u szAppName[] = TEXT("VIM");
1717static HWND hDlgPrint;
1718static int *bUserAbort = NULL;
1719static char_u *prt_name = NULL;
1720
1721/* Defines which are also in vim.rc. */
1722#define IDC_BOX1 400
1723#define IDC_PRINTTEXT1 401
1724#define IDC_PRINTTEXT2 402
1725#define IDC_PROGRESS 403
1726
1727/*
1728 * Convert BGR to RGB for Windows GDI calls
1729 */
1730 static COLORREF
1731swap_me(COLORREF colorref)
1732{
1733 int temp;
1734 char *ptr = (char *)&colorref;
1735
1736 temp = *(ptr);
1737 *(ptr ) = *(ptr + 2);
1738 *(ptr + 2) = temp;
1739 return colorref;
1740}
1741
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001742/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 static BOOL CALLBACK
1744PrintDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
1745{
1746#ifdef FEAT_GETTEXT
1747 NONCLIENTMETRICS nm;
1748 static HFONT hfont;
1749#endif
1750
1751 switch (message)
1752 {
1753 case WM_INITDIALOG:
1754#ifdef FEAT_GETTEXT
1755 nm.cbSize = sizeof(NONCLIENTMETRICS);
1756 if (SystemParametersInfo(
1757 SPI_GETNONCLIENTMETRICS,
1758 sizeof(NONCLIENTMETRICS),
1759 &nm,
1760 0))
1761 {
1762 char buff[MAX_PATH];
1763 int i;
1764
1765 /* Translate the dialog texts */
1766 hfont = CreateFontIndirect(&nm.lfMessageFont);
1767 for (i = IDC_PRINTTEXT1; i <= IDC_PROGRESS; i++)
1768 {
1769 SendDlgItemMessage(hDlg, i, WM_SETFONT, (WPARAM)hfont, 1);
1770 if (GetDlgItemText(hDlg,i, buff, sizeof(buff)))
1771 SetDlgItemText(hDlg,i, _(buff));
1772 }
1773 SendDlgItemMessage(hDlg, IDCANCEL,
1774 WM_SETFONT, (WPARAM)hfont, 1);
1775 if (GetDlgItemText(hDlg,IDCANCEL, buff, sizeof(buff)))
1776 SetDlgItemText(hDlg,IDCANCEL, _(buff));
1777 }
1778#endif
1779 SetWindowText(hDlg, szAppName);
1780 if (prt_name != NULL)
1781 {
1782 SetDlgItemText(hDlg, IDC_PRINTTEXT2, (LPSTR)prt_name);
1783 vim_free(prt_name);
1784 prt_name = NULL;
1785 }
1786 EnableMenuItem(GetSystemMenu(hDlg, FALSE), SC_CLOSE, MF_GRAYED);
1787#ifndef FEAT_GUI
1788 BringWindowToTop(s_hwnd);
1789#endif
1790 return TRUE;
1791
1792 case WM_COMMAND:
1793 *bUserAbort = TRUE;
1794 EnableWindow(GetParent(hDlg), TRUE);
1795 DestroyWindow(hDlg);
1796 hDlgPrint = NULL;
1797#ifdef FEAT_GETTEXT
1798 DeleteObject(hfont);
1799#endif
1800 return TRUE;
1801 }
1802 return FALSE;
1803}
1804
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001805/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 static BOOL CALLBACK
1807AbortProc(HDC hdcPrn, int iCode)
1808{
1809 MSG msg;
1810
1811 while (!*bUserAbort && PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
1812 {
1813 if (!hDlgPrint || !IsDialogMessage(hDlgPrint, &msg))
1814 {
1815 TranslateMessage(&msg);
1816 DispatchMessage(&msg);
1817 }
1818 }
1819 return !*bUserAbort;
1820}
1821
1822#ifndef FEAT_GUI
1823
1824 static UINT CALLBACK
1825PrintHookProc(
1826 HWND hDlg, // handle to dialog box
1827 UINT uiMsg, // message identifier
1828 WPARAM wParam, // message parameter
1829 LPARAM lParam // message parameter
1830 )
1831{
1832 HWND hwndOwner;
1833 RECT rc, rcDlg, rcOwner;
1834 PRINTDLG *pPD;
1835
1836 if (uiMsg == WM_INITDIALOG)
1837 {
1838 // Get the owner window and dialog box rectangles.
1839 if ((hwndOwner = GetParent(hDlg)) == NULL)
1840 hwndOwner = GetDesktopWindow();
1841
1842 GetWindowRect(hwndOwner, &rcOwner);
1843 GetWindowRect(hDlg, &rcDlg);
1844 CopyRect(&rc, &rcOwner);
1845
1846 // Offset the owner and dialog box rectangles so that
1847 // right and bottom values represent the width and
1848 // height, and then offset the owner again to discard
1849 // space taken up by the dialog box.
1850
1851 OffsetRect(&rcDlg, -rcDlg.left, -rcDlg.top);
1852 OffsetRect(&rc, -rc.left, -rc.top);
1853 OffsetRect(&rc, -rcDlg.right, -rcDlg.bottom);
1854
1855 // The new position is the sum of half the remaining
1856 // space and the owner's original position.
1857
1858 SetWindowPos(hDlg,
1859 HWND_TOP,
1860 rcOwner.left + (rc.right / 2),
1861 rcOwner.top + (rc.bottom / 2),
1862 0, 0, // ignores size arguments
1863 SWP_NOSIZE);
1864
1865 /* tackle the printdlg copiesctrl problem */
1866 pPD = (PRINTDLG *)lParam;
1867 pPD->nCopies = (WORD)pPD->lCustData;
1868 SetDlgItemInt( hDlg, edt3, pPD->nCopies, FALSE );
1869 /* Bring the window to top */
1870 BringWindowToTop(GetParent(hDlg));
1871 SetForegroundWindow(hDlg);
1872 }
1873
1874 return FALSE;
1875}
1876#endif
1877
1878 void
1879mch_print_cleanup(void)
1880{
1881 int pifItalic;
1882 int pifBold;
1883 int pifUnderline;
1884
1885 for (pifBold = 0; pifBold <= 1; pifBold++)
1886 for (pifItalic = 0; pifItalic <= 1; pifItalic++)
1887 for (pifUnderline = 0; pifUnderline <= 1; pifUnderline++)
1888 DeleteObject(prt_font_handles[pifBold][pifItalic][pifUnderline]);
1889
1890 if (prt_dlg.hDC != NULL)
1891 DeleteDC(prt_dlg.hDC);
1892 if (!*bUserAbort)
1893 SendMessage(hDlgPrint, WM_COMMAND, 0, 0);
1894}
1895
1896 static int
1897to_device_units(int idx, int dpi, int physsize, int offset, int def_number)
1898{
1899 int ret = 0;
1900 int u;
1901 int nr;
1902
1903 u = prt_get_unit(idx);
1904 if (u == PRT_UNIT_NONE)
1905 {
1906 u = PRT_UNIT_PERC;
1907 nr = def_number;
1908 }
1909 else
1910 nr = printer_opts[idx].number;
1911
1912 switch (u)
1913 {
1914 case PRT_UNIT_PERC:
1915 ret = (physsize * nr) / 100;
1916 break;
1917 case PRT_UNIT_INCH:
1918 ret = (nr * dpi);
1919 break;
1920 case PRT_UNIT_MM:
1921 ret = (nr * 10 * dpi) / 254;
1922 break;
1923 case PRT_UNIT_POINT:
1924 ret = (nr * 10 * dpi) / 720;
1925 break;
1926 }
1927
1928 if (ret < offset)
1929 return 0;
1930 else
1931 return ret - offset;
1932}
1933
1934 static int
1935prt_get_cpl(void)
1936{
1937 int hr;
1938 int phyw;
1939 int dvoff;
1940 int rev_offset;
1941 int dpi;
1942#ifdef WIN16
1943 POINT pagesize;
1944#endif
1945
1946 GetTextMetrics(prt_dlg.hDC, &prt_tm);
1947 prt_line_height = prt_tm.tmHeight + prt_tm.tmExternalLeading;
1948
1949 hr = GetDeviceCaps(prt_dlg.hDC, HORZRES);
1950#ifdef WIN16
1951 Escape(prt_dlg.hDC, GETPHYSPAGESIZE, NULL, NULL, &pagesize);
1952 phyw = pagesize.x;
1953 Escape(prt_dlg.hDC, GETPRINTINGOFFSET, NULL, NULL, &pagesize);
1954 dvoff = pagesize.x;
1955#else
1956 phyw = GetDeviceCaps(prt_dlg.hDC, PHYSICALWIDTH);
1957 dvoff = GetDeviceCaps(prt_dlg.hDC, PHYSICALOFFSETX);
1958#endif
1959 dpi = GetDeviceCaps(prt_dlg.hDC, LOGPIXELSX);
1960
1961 rev_offset = phyw - (dvoff + hr);
1962
1963 prt_left_margin = to_device_units(OPT_PRINT_LEFT, dpi, phyw, dvoff, 10);
1964 if (prt_use_number())
1965 {
1966 prt_number_width = PRINT_NUMBER_WIDTH * prt_tm.tmAveCharWidth;
1967 prt_left_margin += prt_number_width;
1968 }
1969 else
1970 prt_number_width = 0;
1971
1972 prt_right_margin = hr - to_device_units(OPT_PRINT_RIGHT, dpi, phyw,
1973 rev_offset, 5);
1974
1975 return (prt_right_margin - prt_left_margin) / prt_tm.tmAveCharWidth;
1976}
1977
1978 static int
1979prt_get_lpp(void)
1980{
1981 int vr;
1982 int phyw;
1983 int dvoff;
1984 int rev_offset;
1985 int bottom_margin;
1986 int dpi;
1987#ifdef WIN16
1988 POINT pagesize;
1989#endif
1990
1991 vr = GetDeviceCaps(prt_dlg.hDC, VERTRES);
1992#ifdef WIN16
1993 Escape(prt_dlg.hDC, GETPHYSPAGESIZE, NULL, NULL, &pagesize);
1994 phyw = pagesize.y;
1995 Escape(prt_dlg.hDC, GETPRINTINGOFFSET, NULL, NULL, &pagesize);
1996 dvoff = pagesize.y;
1997#else
1998 phyw = GetDeviceCaps(prt_dlg.hDC, PHYSICALHEIGHT);
1999 dvoff = GetDeviceCaps(prt_dlg.hDC, PHYSICALOFFSETY);
2000#endif
2001 dpi = GetDeviceCaps(prt_dlg.hDC, LOGPIXELSY);
2002
2003 rev_offset = phyw - (dvoff + vr);
2004
2005 prt_top_margin = to_device_units(OPT_PRINT_TOP, dpi, phyw, dvoff, 5);
2006
2007 /* adjust top margin if there is a header */
2008 prt_top_margin += prt_line_height * prt_header_height();
2009
2010 bottom_margin = vr - to_device_units(OPT_PRINT_BOT, dpi, phyw,
2011 rev_offset, 5);
2012
2013 return (bottom_margin - prt_top_margin) / prt_line_height;
2014}
2015
2016 int
2017mch_print_init(prt_settings_T *psettings, char_u *jobname, int forceit)
2018{
2019 static HGLOBAL stored_dm = NULL;
2020 static HGLOBAL stored_devn = NULL;
2021 static int stored_nCopies = 1;
2022 static int stored_nFlags = 0;
2023
2024 LOGFONT fLogFont;
2025 int pifItalic;
2026 int pifBold;
2027 int pifUnderline;
2028
2029 DEVMODE *mem;
2030 DEVNAMES *devname;
2031 int i;
2032
2033 bUserAbort = &(psettings->user_abort);
2034 memset(&prt_dlg, 0, sizeof(PRINTDLG));
2035 prt_dlg.lStructSize = sizeof(PRINTDLG);
2036#ifndef FEAT_GUI
2037 GetConsoleHwnd(); /* get value of s_hwnd */
2038#endif
2039 prt_dlg.hwndOwner = s_hwnd;
2040 prt_dlg.Flags = PD_NOPAGENUMS | PD_NOSELECTION | PD_RETURNDC;
2041 if (!forceit)
2042 {
2043 prt_dlg.hDevMode = stored_dm;
2044 prt_dlg.hDevNames = stored_devn;
2045 prt_dlg.lCustData = stored_nCopies; // work around bug in print dialog
2046#ifndef FEAT_GUI
2047 /*
2048 * Use hook to prevent console window being sent to back
2049 */
2050 prt_dlg.lpfnPrintHook = PrintHookProc;
2051 prt_dlg.Flags |= PD_ENABLEPRINTHOOK;
2052#endif
2053 prt_dlg.Flags |= stored_nFlags;
2054 }
2055
2056 /*
2057 * If bang present, return default printer setup with no dialog
2058 * never show dialog if we are running over telnet
2059 */
2060 if (forceit
2061#ifndef FEAT_GUI
2062 || !term_console
2063#endif
2064 )
2065 {
2066 prt_dlg.Flags |= PD_RETURNDEFAULT;
2067#ifdef WIN3264
2068 /*
2069 * MSDN suggests setting the first parameter to WINSPOOL for
2070 * NT, but NULL appears to work just as well.
2071 */
2072 if (*p_pdev != NUL)
2073 prt_dlg.hDC = CreateDC(NULL, p_pdev, NULL, NULL);
2074 else
2075#endif
2076 {
2077 prt_dlg.Flags |= PD_RETURNDEFAULT;
2078 if (PrintDlg(&prt_dlg) == 0)
2079 goto init_fail_dlg;
2080 }
2081 }
2082 else if (PrintDlg(&prt_dlg) == 0)
2083 goto init_fail_dlg;
2084 else
2085 {
2086 /*
2087 * keep the previous driver context
2088 */
2089 stored_dm = prt_dlg.hDevMode;
2090 stored_devn = prt_dlg.hDevNames;
2091 stored_nFlags = prt_dlg.Flags;
2092 stored_nCopies = prt_dlg.nCopies;
2093 }
2094
2095 if (prt_dlg.hDC == NULL)
2096 {
2097 EMSG(_("E237: Printer selection failed"));
2098 mch_print_cleanup();
2099 return FALSE;
2100 }
2101
2102 /* Not all printer drivers report the support of color (or grey) in the
2103 * same way. Let's set has_color if there appears to be some way to print
2104 * more than B&W. */
2105 i = GetDeviceCaps(prt_dlg.hDC, NUMCOLORS);
2106 psettings->has_color = (GetDeviceCaps(prt_dlg.hDC, BITSPIXEL) > 1
2107 || GetDeviceCaps(prt_dlg.hDC, PLANES) > 1
2108 || i > 2 || i == -1);
2109
2110 /* Ensure all font styles are baseline aligned */
2111 SetTextAlign(prt_dlg.hDC, TA_BASELINE|TA_LEFT);
2112
2113 /*
2114 * On some windows systems the nCopies parameter is not
2115 * passed back correctly. It must be retrieved from the
2116 * hDevMode struct.
2117 */
2118 mem = (DEVMODE *)GlobalLock(prt_dlg.hDevMode);
2119 if (mem != NULL)
2120 {
2121#ifdef WIN3264
2122 if (mem->dmCopies != 1)
2123 stored_nCopies = mem->dmCopies;
2124#endif
2125 if ((mem->dmFields & DM_DUPLEX) && (mem->dmDuplex & ~DMDUP_SIMPLEX))
2126 psettings->duplex = TRUE;
2127 if ((mem->dmFields & DM_COLOR) && (mem->dmColor & DMCOLOR_COLOR))
2128 psettings->has_color = TRUE;
2129 }
2130 GlobalUnlock(prt_dlg.hDevMode);
2131
2132 devname = (DEVNAMES *)GlobalLock(prt_dlg.hDevNames);
2133 if (devname != 0)
2134 {
2135 char_u *printer_name = (char_u *)devname + devname->wDeviceOffset;
2136 char_u *port_name = (char_u *)devname +devname->wOutputOffset;
2137 char_u *text = _("to %s on %s");
2138
2139 prt_name = alloc(STRLEN(printer_name) + STRLEN(port_name)
2140 + STRLEN(text));
2141 if (prt_name != NULL)
2142 wsprintf(prt_name, text, printer_name, port_name);
2143 }
2144 GlobalUnlock(prt_dlg.hDevNames);
2145
2146 /*
2147 * Initialise the font according to 'printfont'
2148 */
2149 memset(&fLogFont, 0, sizeof(fLogFont));
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00002150 if (get_logfont(&fLogFont, p_pfn, prt_dlg.hDC, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 {
2152 EMSG2(_("E613: Unknown printer font: %s"), p_pfn);
2153 mch_print_cleanup();
2154 return FALSE;
2155 }
2156
2157 for (pifBold = 0; pifBold <= 1; pifBold++)
2158 for (pifItalic = 0; pifItalic <= 1; pifItalic++)
2159 for (pifUnderline = 0; pifUnderline <= 1; pifUnderline++)
2160 {
2161 fLogFont.lfWeight = boldface[pifBold];
2162 fLogFont.lfItalic = pifItalic;
2163 fLogFont.lfUnderline = pifUnderline;
2164 prt_font_handles[pifBold][pifItalic][pifUnderline]
2165 = CreateFontIndirect(&fLogFont);
2166 }
2167
2168 SetBkMode(prt_dlg.hDC, OPAQUE);
2169 SelectObject(prt_dlg.hDC, prt_font_handles[0][0][0]);
2170
2171 /*
2172 * Fill in the settings struct
2173 */
2174 psettings->chars_per_line = prt_get_cpl();
2175 psettings->lines_per_page = prt_get_lpp();
2176 psettings->n_collated_copies = (prt_dlg.Flags & PD_COLLATE)
2177 ? prt_dlg.nCopies : 1;
2178 psettings->n_uncollated_copies = (prt_dlg.Flags & PD_COLLATE)
2179 ? 1 : prt_dlg.nCopies;
2180
2181 if (psettings->n_collated_copies == 0)
2182 psettings->n_collated_copies = 1;
2183
2184 if (psettings->n_uncollated_copies == 0)
2185 psettings->n_uncollated_copies = 1;
2186
2187 psettings->jobname = jobname;
2188
2189 return TRUE;
2190
2191init_fail_dlg:
2192 {
2193 DWORD err = CommDlgExtendedError();
2194
2195 if (err)
2196 {
2197#ifdef WIN16
2198 char buf[20];
2199
2200 sprintf(buf, "%ld", err);
2201 EMSG2(_("E238: Print error: %s"), buf);
2202#else
2203 char_u *buf;
2204
2205 /* I suspect FormatMessage() doesn't work for values returned by
2206 * CommDlgExtendedError(). What does? */
2207 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
2208 FORMAT_MESSAGE_FROM_SYSTEM |
2209 FORMAT_MESSAGE_IGNORE_INSERTS,
2210 NULL, err, 0, (LPTSTR)(&buf), 0, NULL);
2211 EMSG2(_("E238: Print error: %s"),
2212 buf == NULL ? (char_u *)_("Unknown") : buf);
2213 LocalFree((LPVOID)(buf));
2214#endif
2215 }
2216 else
2217 msg_clr_eos(); /* Maybe canceled */
2218
2219 mch_print_cleanup();
2220 return FALSE;
2221 }
2222}
2223
2224
2225 int
2226mch_print_begin(prt_settings_T *psettings)
2227{
2228 int ret;
2229 static DOCINFO di;
2230 char szBuffer[300];
2231
2232 hDlgPrint = CreateDialog(GetModuleHandle(NULL), TEXT("PrintDlgBox"),
2233 prt_dlg.hwndOwner, PrintDlgProc);
2234#ifdef WIN16
2235 Escape(prt_dlg.hDC, SETABORTPROC, 0, (LPSTR)AbortProc, NULL);
2236#else
2237 SetAbortProc(prt_dlg.hDC, AbortProc);
2238#endif
2239 wsprintf(szBuffer, _("Printing '%s'"), gettail(psettings->jobname));
2240 SetDlgItemText(hDlgPrint, IDC_PRINTTEXT1, (LPSTR)szBuffer);
2241
2242 memset(&di, 0, sizeof(DOCINFO));
2243 di.cbSize = sizeof(DOCINFO);
2244 di.lpszDocName = psettings->jobname;
2245 ret = StartDoc(prt_dlg.hDC, &di);
2246
2247#ifdef FEAT_GUI
2248 /* Give focus back to main window (when using MDI). */
2249 SetFocus(s_hwnd);
2250#endif
2251
2252 return (ret > 0);
2253}
2254
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002255/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 void
2257mch_print_end(prt_settings_T *psettings)
2258{
2259 EndDoc(prt_dlg.hDC);
2260 if (!*bUserAbort)
2261 SendMessage(hDlgPrint, WM_COMMAND, 0, 0);
2262}
2263
2264 int
2265mch_print_end_page(void)
2266{
2267 return (EndPage(prt_dlg.hDC) > 0);
2268}
2269
2270 int
2271mch_print_begin_page(char_u *msg)
2272{
2273 if (msg != NULL)
2274 SetDlgItemText(hDlgPrint, IDC_PROGRESS, (LPSTR)msg);
2275 return (StartPage(prt_dlg.hDC) > 0);
2276}
2277
2278 int
2279mch_print_blank_page(void)
2280{
2281 return (mch_print_begin_page(NULL) ? (mch_print_end_page()) : FALSE);
2282}
2283
2284static int prt_pos_x = 0;
2285static int prt_pos_y = 0;
2286
2287 void
2288mch_print_start_line(margin, page_line)
2289 int margin;
2290 int page_line;
2291{
2292 if (margin)
2293 prt_pos_x = -prt_number_width;
2294 else
2295 prt_pos_x = 0;
2296 prt_pos_y = page_line * prt_line_height
2297 + prt_tm.tmAscent + prt_tm.tmExternalLeading;
2298}
2299
2300 int
2301mch_print_text_out(char_u *p, int len)
2302{
2303#ifdef FEAT_PROPORTIONAL_FONTS
2304 SIZE sz;
2305#endif
2306
2307 TextOut(prt_dlg.hDC, prt_pos_x + prt_left_margin,
2308 prt_pos_y + prt_top_margin, p, len);
2309#ifndef FEAT_PROPORTIONAL_FONTS
2310 prt_pos_x += len * prt_tm.tmAveCharWidth;
2311 return (prt_pos_x + prt_left_margin + prt_tm.tmAveCharWidth
2312 + prt_tm.tmOverhang > prt_right_margin);
2313#else
2314# ifdef WIN16
2315 GetTextExtentPoint(prt_dlg.hDC, p, len, &sz);
2316# else
2317 GetTextExtentPoint32(prt_dlg.hDC, p, len, &sz);
2318# endif
2319 prt_pos_x += (sz.cx - prt_tm.tmOverhang);
2320 /* This is wrong when printing spaces for a TAB. */
2321 if (p[len] == NUL)
2322 return FALSE;
2323# ifdef WIN16
2324 GetTextExtentPoint(prt_dlg.hDC, p + len, 1, &sz);
2325# else
2326 GetTextExtentPoint32(prt_dlg.hDC, p + len, 1, &sz);
2327# endif
2328 return (prt_pos_x + prt_left_margin + sz.cx > prt_right_margin);
2329#endif
2330}
2331
2332 void
2333mch_print_set_font(int iBold, int iItalic, int iUnderline)
2334{
2335 SelectObject(prt_dlg.hDC, prt_font_handles[iBold][iItalic][iUnderline]);
2336}
2337
2338 void
2339mch_print_set_bg(unsigned long bgcol)
2340{
2341 SetBkColor(prt_dlg.hDC, GetNearestColor(prt_dlg.hDC, swap_me(bgcol)));
2342 /*
2343 * With a white background we can draw characters transparent, which is
2344 * good for italic characters that overlap to the next char cell.
2345 */
2346 if (bgcol == 0xffffffUL)
2347 SetBkMode(prt_dlg.hDC, TRANSPARENT);
2348 else
2349 SetBkMode(prt_dlg.hDC, OPAQUE);
2350}
2351
2352 void
2353mch_print_set_fg(unsigned long fgcol)
2354{
2355 SetTextColor(prt_dlg.hDC, GetNearestColor(prt_dlg.hDC, swap_me(fgcol)));
2356}
2357
2358#endif /*FEAT_PRINTER && !FEAT_POSTSCRIPT*/
2359
2360#if defined(FEAT_SHORTCUT) || defined(PROTO)
2361# include <shlobj.h>
2362
2363/*
2364 * When "fname" is the name of a shortcut (*.lnk) resolve the file it points
2365 * to and return that name in allocated memory.
2366 * Otherwise NULL is returned.
2367 */
2368 char_u *
2369mch_resolve_shortcut(char_u *fname)
2370{
2371 HRESULT hr;
2372 IShellLink *psl = NULL;
2373 IPersistFile *ppf = NULL;
2374 OLECHAR wsz[MAX_PATH];
2375 WIN32_FIND_DATA ffd; // we get those free of charge
2376 TCHAR buf[MAX_PATH]; // could have simply reused 'wsz'...
2377 char_u *rfname = NULL;
2378 int len;
2379
2380 /* Check if the file name ends in ".lnk". Avoid calling
2381 * CoCreateInstance(), it's quite slow. */
2382 if (fname == NULL)
2383 return rfname;
2384 len = STRLEN(fname);
2385 if (len <= 4 || STRNICMP(fname + len - 4, ".lnk", 4) != 0)
2386 return rfname;
2387
2388 CoInitialize(NULL);
2389
2390 // create a link manager object and request its interface
2391 hr = CoCreateInstance(
2392 &CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
2393 &IID_IShellLink, (void**)&psl);
2394 if (hr != S_OK)
2395 goto shortcut_error;
2396
2397 // Get a pointer to the IPersistFile interface.
2398 hr = psl->lpVtbl->QueryInterface(
2399 psl, &IID_IPersistFile, (void**)&ppf);
2400 if (hr != S_OK)
2401 goto shortcut_error;
2402
2403 // full path string must be in Unicode.
2404 MultiByteToWideChar(CP_ACP, 0, fname, -1, wsz, MAX_PATH);
2405
2406 // "load" the name and resove the link
2407 hr = ppf->lpVtbl->Load(ppf, wsz, STGM_READ);
2408 if (hr != S_OK)
2409 goto shortcut_error;
2410#if 0 // This makes Vim wait a long time if the target doesn't exist.
2411 hr = psl->lpVtbl->Resolve(psl, NULL, SLR_NO_UI);
2412 if (hr != S_OK)
2413 goto shortcut_error;
2414#endif
2415
2416 // Get the path to the link target.
2417 ZeroMemory(buf, MAX_PATH);
2418 hr = psl->lpVtbl->GetPath(psl, buf, MAX_PATH, &ffd, 0);
2419 if (hr == S_OK && buf[0] != NUL)
2420 rfname = vim_strsave(buf);
2421
2422shortcut_error:
2423 // Release all interface pointers (both belong to the same object)
2424 if (ppf != NULL)
2425 ppf->lpVtbl->Release(ppf);
2426 if (psl != NULL)
2427 psl->lpVtbl->Release(psl);
2428
2429 CoUninitialize();
2430 return rfname;
2431}
2432#endif
2433
2434#if (defined(FEAT_EVAL) && !defined(FEAT_GUI)) || defined(PROTO)
2435/*
2436 * Bring ourselves to the foreground. Does work if the OS doesn't allow it.
2437 */
2438 void
2439win32_set_foreground()
2440{
2441# ifndef FEAT_GUI
2442 GetConsoleHwnd(); /* get value of s_hwnd */
2443# endif
2444 if (s_hwnd != 0)
2445 SetForegroundWindow(s_hwnd);
2446}
2447#endif
2448
2449#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
2450/*
2451 * Client-server code for Vim
2452 *
2453 * Originally written by Paul Moore
2454 */
2455
2456/* In order to handle inter-process messages, we need to have a window. But
2457 * the functions in this module can be called before the main GUI window is
2458 * created (and may also be called in the console version, where there is no
2459 * GUI window at all).
2460 *
2461 * So we create a hidden window, and arrange to destroy it on exit.
2462 */
2463HWND message_window = 0; /* window that's handling messsages */
2464
2465#define VIM_CLASSNAME "VIM_MESSAGES"
2466#define VIM_CLASSNAME_LEN (sizeof(VIM_CLASSNAME) - 1)
2467
2468/* Communication is via WM_COPYDATA messages. The message type is send in
2469 * the dwData parameter. Types are defined here. */
2470#define COPYDATA_KEYS 0
2471#define COPYDATA_REPLY 1
2472#define COPYDATA_EXPR 10
2473#define COPYDATA_RESULT 11
2474#define COPYDATA_ERROR_RESULT 12
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002475#define COPYDATA_ENCODING 20
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476
2477/* This is a structure containing a server HWND and its name. */
2478struct server_id
2479{
2480 HWND hwnd;
2481 char_u *name;
2482};
2483
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002484/* Last received 'encoding' that the client uses. */
2485static char_u *client_enc = NULL;
2486
2487/*
2488 * Tell the other side what encoding we are using.
2489 * Errors are ignored.
2490 */
2491 static void
2492serverSendEnc(HWND target)
2493{
2494 COPYDATASTRUCT data;
2495
2496 data.dwData = COPYDATA_ENCODING;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002497#ifdef FEAT_MBYTE
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002498 data.cbData = STRLEN(p_enc) + 1;
2499 data.lpData = p_enc;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002500#else
2501 data.cbData = STRLEN("latin1") + 1;
2502 data.lpData = "latin1";
2503#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002504 (void)SendMessage(target, WM_COPYDATA, (WPARAM)message_window,
2505 (LPARAM)(&data));
2506}
2507
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508/*
2509 * Clean up on exit. This destroys the hidden message window.
2510 */
2511 static void
2512#ifdef __BORLANDC__
2513 _RTLENTRYF
2514#endif
2515CleanUpMessaging(void)
2516{
2517 if (message_window != 0)
2518 {
2519 DestroyWindow(message_window);
2520 message_window = 0;
2521 }
2522}
2523
2524static int save_reply(HWND server, char_u *reply, int expr);
2525
2526/*s
2527 * The window procedure for the hidden message window.
2528 * It handles callback messages and notifications from servers.
2529 * In order to process these messages, it is necessary to run a
2530 * message loop. Code which may run before the main message loop
2531 * is started (in the GUI) is careful to pump messages when it needs
2532 * to. Features which require message delivery during normal use will
2533 * not work in the console version - this basically means those
2534 * features which allow Vim to act as a server, rather than a client.
2535 */
2536 static LRESULT CALLBACK
2537Messaging_WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
2538{
2539 if (msg == WM_COPYDATA)
2540 {
2541 /* This is a message from another Vim. The dwData member of the
2542 * COPYDATASTRUCT determines the type of message:
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002543 * COPYDATA_ENCODING:
2544 * The encoding that the client uses. Following messages will
2545 * use this encoding, convert if needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 * COPYDATA_KEYS:
2547 * A key sequence. We are a server, and a client wants these keys
2548 * adding to the input queue.
2549 * COPYDATA_REPLY:
2550 * A reply. We are a client, and a server has sent this message
2551 * in response to a request. (server2client())
2552 * COPYDATA_EXPR:
2553 * An expression. We are a server, and a client wants us to
2554 * evaluate this expression.
2555 * COPYDATA_RESULT:
2556 * A reply. We are a client, and a server has sent this message
2557 * in response to a COPYDATA_EXPR.
2558 * COPYDATA_ERROR_RESULT:
2559 * A reply. We are a client, and a server has sent this message
2560 * in response to a COPYDATA_EXPR that failed to evaluate.
2561 */
2562 COPYDATASTRUCT *data = (COPYDATASTRUCT*)lParam;
2563 HWND sender = (HWND)wParam;
2564 COPYDATASTRUCT reply;
2565 char_u *res;
2566 char_u winstr[30];
2567 int retval;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002568 char_u *str;
2569 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570
2571 switch (data->dwData)
2572 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002573 case COPYDATA_ENCODING:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002574# ifdef FEAT_MBYTE
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002575 /* Remember the encoding that the client uses. */
2576 vim_free(client_enc);
2577 client_enc = enc_canonize((char_u *)data->lpData);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002578# endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002579 return 1;
2580
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 case COPYDATA_KEYS:
2582 /* Remember who sent this, for <client> */
2583 clientWindow = sender;
2584
2585 /* Add the received keys to the input buffer. The loop waiting
2586 * for the user to do something should check the input buffer. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002587 str = serverConvert(client_enc, (char_u *)data->lpData, &tofree);
2588 server_to_input_buf(str);
2589 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590
2591# ifdef FEAT_GUI
2592 /* Wake up the main GUI loop. */
2593 if (s_hwnd != 0)
2594 PostMessage(s_hwnd, WM_NULL, 0, 0);
2595# endif
2596 return 1;
2597
2598 case COPYDATA_EXPR:
2599 /* Remember who sent this, for <client> */
2600 clientWindow = sender;
2601
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002602 str = serverConvert(client_enc, (char_u *)data->lpData, &tofree);
2603 res = eval_client_expr_to_string(str);
2604 vim_free(tofree);
2605
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 if (res == NULL)
2607 {
2608 res = vim_strsave(_(e_invexprmsg));
2609 reply.dwData = COPYDATA_ERROR_RESULT;
2610 }
2611 else
2612 reply.dwData = COPYDATA_RESULT;
2613 reply.lpData = res;
2614 reply.cbData = STRLEN(res) + 1;
2615
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002616 serverSendEnc(sender);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 retval = SendMessage(sender, WM_COPYDATA, (WPARAM)message_window,
2618 (LPARAM)(&reply));
2619 vim_free(res);
2620 return retval;
2621
2622 case COPYDATA_REPLY:
2623 case COPYDATA_RESULT:
2624 case COPYDATA_ERROR_RESULT:
2625 if (data->lpData != NULL)
2626 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002627 str = serverConvert(client_enc, (char_u *)data->lpData,
2628 &tofree);
2629 if (tofree == NULL)
2630 str = vim_strsave(str);
2631 if (save_reply(sender, str,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 (data->dwData == COPYDATA_REPLY ? 0 :
2633 (data->dwData == COPYDATA_RESULT ? 1 :
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002634 2))) == FAIL)
2635 vim_free(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636#ifdef FEAT_AUTOCMD
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002637 else if (data->dwData == COPYDATA_REPLY)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 {
2639 sprintf((char *)winstr, "0x%x", (unsigned)sender);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002640 apply_autocmds(EVENT_REMOTEREPLY, winstr, str,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 TRUE, curbuf);
2642 }
2643#endif
2644 }
2645 return 1;
2646 }
2647
2648 return 0;
2649 }
2650
2651 else if (msg == WM_ACTIVATE && wParam == WA_ACTIVE)
2652 {
2653 /* When the message window is activated (brought to the foreground),
2654 * this actually applies to the text window. */
2655#ifndef FEAT_GUI
2656 GetConsoleHwnd(); /* get value of s_hwnd */
2657#endif
2658 if (s_hwnd != 0)
2659 {
2660 SetForegroundWindow(s_hwnd);
2661 return 0;
2662 }
2663 }
2664
2665 return DefWindowProc(hwnd, msg, wParam, lParam);
2666}
2667
2668/*
2669 * Initialise the message handling process. This involves creating a window
2670 * to handle messages - the window will not be visible.
2671 */
2672 void
2673serverInitMessaging(void)
2674{
2675 WNDCLASS wndclass;
2676 HINSTANCE s_hinst;
2677
2678 /* Clean up on exit */
2679 atexit(CleanUpMessaging);
2680
2681 /* Register a window class - we only really care
2682 * about the window procedure
2683 */
2684 s_hinst = (HINSTANCE)GetModuleHandle(0);
2685 wndclass.style = 0;
2686 wndclass.lpfnWndProc = Messaging_WndProc;
2687 wndclass.cbClsExtra = 0;
2688 wndclass.cbWndExtra = 0;
2689 wndclass.hInstance = s_hinst;
2690 wndclass.hIcon = NULL;
2691 wndclass.hCursor = NULL;
2692 wndclass.hbrBackground = NULL;
2693 wndclass.lpszMenuName = NULL;
2694 wndclass.lpszClassName = VIM_CLASSNAME;
2695 RegisterClass(&wndclass);
2696
2697 /* Create the message window. It will be hidden, so the details don't
2698 * matter. Don't use WS_OVERLAPPEDWINDOW, it will make a shortcut remove
2699 * focus from gvim. */
2700 message_window = CreateWindow(VIM_CLASSNAME, "",
2701 WS_POPUPWINDOW | WS_CAPTION,
2702 CW_USEDEFAULT, CW_USEDEFAULT,
2703 100, 100, NULL, NULL,
2704 s_hinst, NULL);
2705}
2706
2707/*
2708 * Get the title of the window "hwnd", which is the Vim server name, in
2709 * "name[namelen]" and return the length.
2710 * Returns zero if window "hwnd" is not a Vim server.
2711 */
2712 static int
2713getVimServerName(HWND hwnd, char *name, int namelen)
2714{
2715 int len;
2716 char buffer[VIM_CLASSNAME_LEN + 1];
2717
2718 /* Ignore windows which aren't Vim message windows */
2719 len = GetClassName(hwnd, buffer, sizeof(buffer));
2720 if (len != VIM_CLASSNAME_LEN || STRCMP(buffer, VIM_CLASSNAME) != 0)
2721 return 0;
2722
2723 /* Get the title of the window */
2724 return GetWindowText(hwnd, name, namelen);
2725}
2726
2727 static BOOL CALLBACK
2728enumWindowsGetServer(HWND hwnd, LPARAM lparam)
2729{
2730 struct server_id *id = (struct server_id *)lparam;
2731 char server[MAX_PATH];
2732
2733 /* Get the title of the window */
2734 if (getVimServerName(hwnd, server, sizeof(server)) == 0)
2735 return TRUE;
2736
2737 /* If this is the server we're looking for, return its HWND */
2738 if (STRICMP(server, id->name) == 0)
2739 {
2740 id->hwnd = hwnd;
2741 return FALSE;
2742 }
2743
2744 /* Otherwise, keep looking */
2745 return TRUE;
2746}
2747
2748 static BOOL CALLBACK
2749enumWindowsGetNames(HWND hwnd, LPARAM lparam)
2750{
2751 garray_T *ga = (garray_T *)lparam;
2752 char server[MAX_PATH];
2753
2754 /* Get the title of the window */
2755 if (getVimServerName(hwnd, server, sizeof(server)) == 0)
2756 return TRUE;
2757
2758 /* Add the name to the list */
2759 ga_concat(ga, server);
2760 ga_concat(ga, "\n");
2761 return TRUE;
2762}
2763
2764 static HWND
2765findServer(char_u *name)
2766{
2767 struct server_id id;
2768
2769 id.name = name;
2770 id.hwnd = 0;
2771
2772 EnumWindows(enumWindowsGetServer, (LPARAM)(&id));
2773
2774 return id.hwnd;
2775}
2776
2777 void
2778serverSetName(char_u *name)
2779{
2780 char_u *ok_name;
2781 HWND hwnd = 0;
2782 int i = 0;
2783 char_u *p;
2784
2785 /* Leave enough space for a 9-digit suffix to ensure uniqueness! */
2786 ok_name = alloc(STRLEN(name) + 10);
2787
2788 STRCPY(ok_name, name);
2789 p = ok_name + STRLEN(name);
2790
2791 for (;;)
2792 {
2793 /* This is inefficient - we're doing an EnumWindows loop for each
2794 * possible name. It would be better to grab all names in one go,
2795 * and scan the list each time...
2796 */
2797 hwnd = findServer(ok_name);
2798 if (hwnd == 0)
2799 break;
2800
2801 ++i;
2802 if (i >= 1000)
2803 break;
2804
2805 sprintf((char *)p, "%d", i);
2806 }
2807
2808 if (hwnd != 0)
2809 vim_free(ok_name);
2810 else
2811 {
2812 /* Remember the name */
2813 serverName = ok_name;
2814#ifdef FEAT_TITLE
2815 need_maketitle = TRUE; /* update Vim window title later */
2816#endif
2817
2818 /* Update the message window title */
2819 SetWindowText(message_window, ok_name);
2820
2821#ifdef FEAT_EVAL
2822 /* Set the servername variable */
2823 set_vim_var_string(VV_SEND_SERVER, serverName, -1);
2824#endif
2825 }
2826}
2827
2828 char_u *
2829serverGetVimNames(void)
2830{
2831 garray_T ga;
2832
2833 ga_init2(&ga, 1, 100);
2834
2835 EnumWindows(enumWindowsGetNames, (LPARAM)(&ga));
Bram Moolenaar269ec652004-07-29 08:43:53 +00002836 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837
2838 return ga.ga_data;
2839}
2840
2841 int
2842serverSendReply(name, reply)
2843 char_u *name; /* Where to send. */
2844 char_u *reply; /* What to send. */
2845{
2846 HWND target;
2847 COPYDATASTRUCT data;
2848 int n = 0;
2849
2850 /* The "name" argument is a magic cookie obtained from expand("<client>").
2851 * It should be of the form 0xXXXXX - i.e. a C hex literal, which is the
2852 * value of the client's message window HWND.
2853 */
2854 sscanf((char *)name, "%x", &n);
2855 if (n == 0)
2856 return -1;
2857
2858 target = (HWND)n;
2859 if (!IsWindow(target))
2860 return -1;
2861
2862 data.dwData = COPYDATA_REPLY;
2863 data.cbData = STRLEN(reply) + 1;
2864 data.lpData = reply;
2865
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002866 serverSendEnc(target);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 if (SendMessage(target, WM_COPYDATA, (WPARAM)message_window,
2868 (LPARAM)(&data)))
2869 return 0;
2870
2871 return -1;
2872}
2873
2874 int
2875serverSendToVim(name, cmd, result, ptarget, asExpr, silent)
2876 char_u *name; /* Where to send. */
2877 char_u *cmd; /* What to send. */
2878 char_u **result; /* Result of eval'ed expression */
2879 void *ptarget; /* HWND of server */
2880 int asExpr; /* Expression or keys? */
2881 int silent; /* don't complain about no server */
2882{
2883 HWND target = findServer(name);
2884 COPYDATASTRUCT data;
2885 char_u *retval = NULL;
2886 int retcode = 0;
2887
2888 if (target == 0)
2889 {
2890 if (!silent)
2891 EMSG2(_(e_noserver), name);
2892 return -1;
2893 }
2894
2895 if (ptarget)
2896 *(HWND *)ptarget = target;
2897
2898 data.dwData = asExpr ? COPYDATA_EXPR : COPYDATA_KEYS;
2899 data.cbData = STRLEN(cmd) + 1;
2900 data.lpData = cmd;
2901
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002902 serverSendEnc(target);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 if (SendMessage(target, WM_COPYDATA, (WPARAM)message_window,
2904 (LPARAM)(&data)) == 0)
2905 return -1;
2906
2907 if (asExpr)
2908 retval = serverGetReply(target, &retcode, TRUE, TRUE);
2909
2910 if (result == NULL)
2911 vim_free(retval);
2912 else
2913 *result = retval; /* Caller assumes responsibility for freeing */
2914
2915 return retcode;
2916}
2917
2918/*
2919 * Bring the server to the foreground.
2920 */
2921 void
2922serverForeground(name)
2923 char_u *name;
2924{
2925 HWND target = findServer(name);
2926
2927 if (target != 0)
2928 SetForegroundWindow(target);
2929}
2930
2931/* Replies from server need to be stored until the client picks them up via
2932 * remote_read(). So we maintain a list of server-id/reply pairs.
2933 * Note that there could be multiple replies from one server pending if the
2934 * client is slow picking them up.
2935 * We just store the replies in a simple list. When we remove an entry, we
2936 * move list entries down to fill the gap.
2937 * The server ID is simply the HWND.
2938 */
2939typedef struct
2940{
2941 HWND server; /* server window */
2942 char_u *reply; /* reply string */
2943 int expr_result; /* 0 for REPLY, 1 for RESULT 2 for error */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002944} reply_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945
2946static garray_T reply_list = {0, 0, sizeof(reply_T), 5, 0};
2947
2948#define REPLY_ITEM(i) ((reply_T *)(reply_list.ga_data) + (i))
2949#define REPLY_COUNT (reply_list.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950
2951/* Flag which is used to wait for a reply */
2952static int reply_received = 0;
2953
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002954/*
2955 * Store a reply. "reply" must be allocated memory (or NULL).
2956 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 static int
2958save_reply(HWND server, char_u *reply, int expr)
2959{
2960 reply_T *rep;
2961
2962 if (ga_grow(&reply_list, 1) == FAIL)
2963 return FAIL;
2964
2965 rep = REPLY_ITEM(REPLY_COUNT);
2966 rep->server = server;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002967 rep->reply = reply;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 rep->expr_result = expr;
2969 if (rep->reply == NULL)
2970 return FAIL;
2971
2972 ++REPLY_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 reply_received = 1;
2974 return OK;
2975}
2976
2977/*
2978 * Get a reply from server "server".
2979 * When "expr_res" is non NULL, get the result of an expression, otherwise a
2980 * server2client() message.
2981 * When non NULL, point to return code. 0 => OK, -1 => ERROR
2982 * If "remove" is TRUE, consume the message, the caller must free it then.
2983 * if "wait" is TRUE block until a message arrives (or the server exits).
2984 */
2985 char_u *
2986serverGetReply(HWND server, int *expr_res, int remove, int wait)
2987{
2988 int i;
2989 char_u *reply;
2990 reply_T *rep;
2991
2992 /* When waiting, loop until the message waiting for is received. */
2993 for (;;)
2994 {
2995 /* Reset this here, in case a message arrives while we are going
2996 * through the already received messages. */
2997 reply_received = 0;
2998
2999 for (i = 0; i < REPLY_COUNT; ++i)
3000 {
3001 rep = REPLY_ITEM(i);
3002 if (rep->server == server
3003 && ((rep->expr_result != 0) == (expr_res != NULL)))
3004 {
3005 /* Save the values we've found for later */
3006 reply = rep->reply;
3007 if (expr_res != NULL)
3008 *expr_res = rep->expr_result == 1 ? 0 : -1;
3009
3010 if (remove)
3011 {
3012 /* Move the rest of the list down to fill the gap */
3013 mch_memmove(rep, rep + 1,
3014 (REPLY_COUNT - i - 1) * sizeof(reply_T));
3015 --REPLY_COUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 }
3017
3018 /* Return the reply to the caller, who takes on responsibility
3019 * for freeing it if "remove" is TRUE. */
3020 return reply;
3021 }
3022 }
3023
3024 /* If we got here, we didn't find a reply. Return immediately if the
3025 * "wait" parameter isn't set. */
3026 if (!wait)
3027 break;
3028
3029 /* We need to wait for a reply. Enter a message loop until the
3030 * "reply_received" flag gets set. */
3031
3032 /* Loop until we receive a reply */
3033 while (reply_received == 0)
3034 {
3035 /* Wait for a SendMessage() call to us. This could be the reply
3036 * we are waiting for. Use a timeout of a second, to catch the
3037 * situation that the server died unexpectedly. */
3038 MsgWaitForMultipleObjects(0, NULL, TRUE, 1000, QS_ALLINPUT);
3039
3040 /* If the server has died, give up */
3041 if (!IsWindow(server))
3042 return NULL;
3043
3044 serverProcessPendingMessages();
3045 }
3046 }
3047
3048 return NULL;
3049}
3050
3051/*
3052 * Process any messages in the Windows message queue.
3053 */
3054 void
3055serverProcessPendingMessages(void)
3056{
3057 MSG msg;
3058
3059 while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
3060 {
3061 TranslateMessage(&msg);
3062 DispatchMessage(&msg);
3063 }
3064}
3065
3066#endif /* FEAT_CLIENTSERVER */
3067
3068#if defined(FEAT_GUI) || (defined(FEAT_PRINTER) && !defined(FEAT_POSTSCRIPT)) \
3069 || defined(PROTO)
3070
3071struct charset_pair
3072{
3073 char *name;
3074 BYTE charset;
3075};
3076
3077static struct charset_pair
3078charset_pairs[] =
3079{
3080 {"ANSI", ANSI_CHARSET},
3081 {"CHINESEBIG5", CHINESEBIG5_CHARSET},
3082 {"DEFAULT", DEFAULT_CHARSET},
3083 {"HANGEUL", HANGEUL_CHARSET},
3084 {"OEM", OEM_CHARSET},
3085 {"SHIFTJIS", SHIFTJIS_CHARSET},
3086 {"SYMBOL", SYMBOL_CHARSET},
3087#ifdef WIN3264
3088 {"ARABIC", ARABIC_CHARSET},
3089 {"BALTIC", BALTIC_CHARSET},
3090 {"EASTEUROPE", EASTEUROPE_CHARSET},
3091 {"GB2312", GB2312_CHARSET},
3092 {"GREEK", GREEK_CHARSET},
3093 {"HEBREW", HEBREW_CHARSET},
3094 {"JOHAB", JOHAB_CHARSET},
3095 {"MAC", MAC_CHARSET},
3096 {"RUSSIAN", RUSSIAN_CHARSET},
3097 {"THAI", THAI_CHARSET},
3098 {"TURKISH", TURKISH_CHARSET},
3099# if (!defined(_MSC_VER) || (_MSC_VER > 1010)) \
3100 && (!defined(__BORLANDC__) || (__BORLANDC__ > 0x0500))
3101 {"VIETNAMESE", VIETNAMESE_CHARSET},
3102# endif
3103#endif
3104 {NULL, 0}
3105};
3106
3107/*
3108 * Convert a charset ID to a name.
3109 * Return NULL when not recognized.
3110 */
3111 char *
3112charset_id2name(int id)
3113{
3114 struct charset_pair *cp;
3115
3116 for (cp = charset_pairs; cp->name != NULL; ++cp)
3117 if ((BYTE)id == cp->charset)
3118 break;
3119 return cp->name;
3120}
3121
3122static const LOGFONT s_lfDefault =
3123{
3124 -12, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, DEFAULT_CHARSET,
3125 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
3126 PROOF_QUALITY, FIXED_PITCH | FF_DONTCARE,
3127 "Fixedsys" /* see _ReadVimIni */
3128};
3129
3130/* Initialise the "current height" to -12 (same as s_lfDefault) just
3131 * in case the user specifies a font in "guifont" with no size before a font
3132 * with an explicit size has been set. This defaults the size to this value
3133 * (-12 equates to roughly 9pt).
3134 */
3135int current_font_height = -12; /* also used in gui_w48.c */
3136
3137/* Convert a string representing a point size into pixels. The string should
3138 * be a positive decimal number, with an optional decimal point (eg, "12", or
3139 * "10.5"). The pixel value is returned, and a pointer to the next unconverted
3140 * character is stored in *end. The flag "vertical" says whether this
3141 * calculation is for a vertical (height) size or a horizontal (width) one.
3142 */
3143 static int
3144points_to_pixels(char_u *str, char_u **end, int vertical, int pprinter_dc)
3145{
3146 int pixels;
3147 int points = 0;
3148 int divisor = 0;
3149 HWND hwnd = (HWND)0;
3150 HDC hdc;
3151 HDC printer_dc = (HDC)pprinter_dc;
3152
3153 while (*str != NUL)
3154 {
3155 if (*str == '.' && divisor == 0)
3156 {
3157 /* Start keeping a divisor, for later */
3158 divisor = 1;
3159 }
3160 else
3161 {
3162 if (!VIM_ISDIGIT(*str))
3163 break;
3164
3165 points *= 10;
3166 points += *str - '0';
3167 divisor *= 10;
3168 }
3169 ++str;
3170 }
3171
3172 if (divisor == 0)
3173 divisor = 1;
3174
3175 if (printer_dc == NULL)
3176 {
3177 hwnd = GetDesktopWindow();
3178 hdc = GetWindowDC(hwnd);
3179 }
3180 else
3181 hdc = printer_dc;
3182
3183 pixels = MulDiv(points,
3184 GetDeviceCaps(hdc, vertical ? LOGPIXELSY : LOGPIXELSX),
3185 72 * divisor);
3186
3187 if (printer_dc == NULL)
3188 ReleaseDC(hwnd, hdc);
3189
3190 *end = str;
3191 return pixels;
3192}
3193
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003194/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 static int CALLBACK
3196font_enumproc(
3197 ENUMLOGFONT *elf,
3198 NEWTEXTMETRIC *ntm,
3199 int type,
3200 LPARAM lparam)
3201{
3202 /* Return value:
3203 * 0 = terminate now (monospace & ANSI)
3204 * 1 = continue, still no luck...
3205 * 2 = continue, but we have an acceptable LOGFONT
3206 * (monospace, not ANSI)
3207 * We use these values, as EnumFontFamilies returns 1 if the
3208 * callback function is never called. So, we check the return as
3209 * 0 = perfect, 2 = OK, 1 = no good...
3210 * It's not pretty, but it works!
3211 */
3212
3213 LOGFONT *lf = (LOGFONT *)(lparam);
3214
3215#ifndef FEAT_PROPORTIONAL_FONTS
3216 /* Ignore non-monospace fonts without further ado */
3217 if ((ntm->tmPitchAndFamily & 1) != 0)
3218 return 1;
3219#endif
3220
3221 /* Remember this LOGFONT as a "possible" */
3222 *lf = elf->elfLogFont;
3223
3224 /* Terminate the scan as soon as we find an ANSI font */
3225 if (lf->lfCharSet == ANSI_CHARSET
3226 || lf->lfCharSet == OEM_CHARSET
3227 || lf->lfCharSet == DEFAULT_CHARSET)
3228 return 0;
3229
3230 /* Continue the scan - we have a non-ANSI font */
3231 return 2;
3232}
3233
3234 static int
3235init_logfont(LOGFONT *lf)
3236{
3237 int n;
3238 HWND hwnd = GetDesktopWindow();
3239 HDC hdc = GetWindowDC(hwnd);
3240
3241 n = EnumFontFamilies(hdc,
3242 (LPCSTR)lf->lfFaceName,
3243 (FONTENUMPROC)font_enumproc,
3244 (LPARAM)lf);
3245
3246 ReleaseDC(hwnd, hdc);
3247
3248 /* If we couldn't find a useable font, return failure */
3249 if (n == 1)
3250 return FAIL;
3251
3252 /* Tidy up the rest of the LOGFONT structure. We set to a basic
3253 * font - get_logfont() sets bold, italic, etc based on the user's
3254 * input.
3255 */
3256 lf->lfHeight = current_font_height;
3257 lf->lfWidth = 0;
3258 lf->lfItalic = FALSE;
3259 lf->lfUnderline = FALSE;
3260 lf->lfStrikeOut = FALSE;
3261 lf->lfWeight = FW_NORMAL;
3262
3263 /* Return success */
3264 return OK;
3265}
3266
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003267/*
3268 * Get font info from "name" into logfont "lf".
3269 * Return OK for a valid name, FAIL otherwise.
3270 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 int
3272get_logfont(
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003273 LOGFONT *lf,
3274 char_u *name,
3275 HDC printer_dc,
3276 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277{
3278 char_u *p;
3279 int i;
3280 static LOGFONT *lastlf = NULL;
3281
3282 *lf = s_lfDefault;
3283 if (name == NULL)
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003284 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285
3286 if (STRCMP(name, "*") == 0)
3287 {
3288#if defined(FEAT_GUI_W32)
3289 CHOOSEFONT cf;
3290 /* if name is "*", bring up std font dialog: */
3291 memset(&cf, 0, sizeof(cf));
3292 cf.lStructSize = sizeof(cf);
3293 cf.hwndOwner = s_hwnd;
3294 cf.Flags = CF_SCREENFONTS | CF_FIXEDPITCHONLY | CF_INITTOLOGFONTSTRUCT;
3295 if (lastlf != NULL)
3296 *lf = *lastlf;
3297 cf.lpLogFont = lf;
3298 cf.nFontType = 0 ; //REGULAR_FONTTYPE;
3299 if (ChooseFont(&cf))
3300 goto theend;
3301#else
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003302 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303#endif
3304 }
3305
3306 /*
3307 * Split name up, it could be <name>:h<height>:w<width> etc.
3308 */
3309 for (p = name; *p && *p != ':'; p++)
3310 {
3311 if (p - name + 1 > LF_FACESIZE)
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003312 return FAIL; /* Name too long */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 lf->lfFaceName[p - name] = *p;
3314 }
3315 if (p != name)
3316 lf->lfFaceName[p - name] = NUL;
3317
3318 /* First set defaults */
3319 lf->lfHeight = -12;
3320 lf->lfWidth = 0;
3321 lf->lfWeight = FW_NORMAL;
3322 lf->lfItalic = FALSE;
3323 lf->lfUnderline = FALSE;
3324 lf->lfStrikeOut = FALSE;
3325
3326 /*
3327 * If the font can't be found, try replacing '_' by ' '.
3328 */
3329 if (init_logfont(lf) == FAIL)
3330 {
3331 int did_replace = FALSE;
3332
3333 for (i = 0; lf->lfFaceName[i]; ++i)
3334 if (lf->lfFaceName[i] == '_')
3335 {
3336 lf->lfFaceName[i] = ' ';
3337 did_replace = TRUE;
3338 }
3339 if (!did_replace || init_logfont(lf) == FAIL)
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003340 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341 }
3342
3343 while (*p == ':')
3344 p++;
3345
3346 /* Set the values found after ':' */
3347 while (*p)
3348 {
3349 switch (*p++)
3350 {
3351 case 'h':
3352 lf->lfHeight = - points_to_pixels(p, &p, TRUE, (int)printer_dc);
3353 break;
3354 case 'w':
3355 lf->lfWidth = points_to_pixels(p, &p, FALSE, (int)printer_dc);
3356 break;
3357 case 'b':
3358#ifndef MSWIN16_FASTTEXT
3359 lf->lfWeight = FW_BOLD;
3360#endif
3361 break;
3362 case 'i':
3363#ifndef MSWIN16_FASTTEXT
3364 lf->lfItalic = TRUE;
3365#endif
3366 break;
3367 case 'u':
3368 lf->lfUnderline = TRUE;
3369 break;
3370 case 's':
3371 lf->lfStrikeOut = TRUE;
3372 break;
3373 case 'c':
3374 {
3375 struct charset_pair *cp;
3376
3377 for (cp = charset_pairs; cp->name != NULL; ++cp)
3378 if (STRNCMP(p, cp->name, strlen(cp->name)) == 0)
3379 {
3380 lf->lfCharSet = cp->charset;
3381 p += strlen(cp->name);
3382 break;
3383 }
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003384 if (cp->name == NULL && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 {
Bram Moolenaar051b7822005-05-19 21:00:46 +00003386 vim_snprintf((char *)IObuff, IOSIZE,
3387 _("E244: Illegal charset name \"%s\" in font name \"%s\""), p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 EMSG(IObuff);
3389 break;
3390 }
3391 break;
3392 }
3393 default:
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003394 if (verbose)
3395 {
Bram Moolenaar051b7822005-05-19 21:00:46 +00003396 vim_snprintf((char *)IObuff, IOSIZE,
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003397 _("E245: Illegal char '%c' in font name \"%s\""),
3398 p[-1], name);
3399 EMSG(IObuff);
3400 }
3401 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 }
3403 while (*p == ':')
3404 p++;
3405 }
3406
3407#if defined(FEAT_GUI_W32)
3408theend:
3409#endif
3410 /* ron: init lastlf */
3411 if (printer_dc == NULL)
3412 {
3413 vim_free(lastlf);
3414 lastlf = (LOGFONT *)alloc(sizeof(LOGFONT));
3415 if (lastlf != NULL)
3416 mch_memmove(lastlf, lf, sizeof(LOGFONT));
3417 }
3418
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00003419 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420}
3421
3422#endif /* defined(FEAT_GUI) || defined(FEAT_PRINTER) */