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