-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui.c
5541 lines (5030 loc) · 137 KB
/
gui.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* vi:set ts=8 sts=4 sw=4 noet:
*
* VIM - Vi IMproved by Bram Moolenaar
* GUI/Motif support by Robert Webb
*
* Do ":help uganda" in Vim to read copying and usage conditions.
* Do ":help credits" in Vim to see a list of people who contributed.
* See README.txt for an overview of the Vim source code.
*/
#include "vim.h"
// Structure containing all the GUI information
gui_T gui;
#if defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)
# define USE_SET_GUIFONTWIDE
static void set_guifontwide(char_u *font_name);
#endif
static void gui_check_pos(void);
static void gui_reset_scroll_region(void);
static void gui_outstr(char_u *, int);
static int gui_screenchar(int off, int flags, guicolor_T fg, guicolor_T bg, int back);
static int gui_outstr_nowrap(char_u *s, int len, int flags, guicolor_T fg, guicolor_T bg, int back);
static void gui_delete_lines(int row, int count);
static void gui_insert_lines(int row, int count);
static int gui_xy2colrow(int x, int y, int *colp);
#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
static int gui_has_tabline(void);
#endif
static void gui_do_scrollbar(win_T *wp, int which, int enable);
static void gui_update_horiz_scrollbar(int);
static void gui_set_fg_color(char_u *name);
static void gui_set_bg_color(char_u *name);
static void init_gui_options(void);
static win_T *xy2win(int x, int y, mouse_find_T popup);
#ifdef GUI_MAY_FORK
static void gui_do_fork(void);
static int gui_read_child_pipe(int fd);
// Return values for gui_read_child_pipe
enum {
GUI_CHILD_IO_ERROR,
GUI_CHILD_OK,
GUI_CHILD_FAILED
};
#endif
static void gui_attempt_start(void);
static int can_update_cursor = TRUE; // can display the cursor
static int disable_flush = 0; // If > 0, gui_mch_flush() is disabled.
/*
* gui_start -- Called when user wants to start the GUI.
*
* Careful: This function can be called recursively when there is a ":gui"
* command in the .gvimrc file. Only the first call should fork, not the
* recursive call.
*/
void
gui_start(char_u *arg UNUSED)
{
char_u *old_term;
#ifdef GUI_MAY_FORK
static int recursive = 0;
#endif
#if defined(GUI_MAY_SPAWN) && defined(EXPERIMENTAL_GUI_CMD)
char *msg = NULL;
#endif
old_term = vim_strsave(T_NAME);
settmode(TMODE_COOK); // stop RAW mode
if (full_screen)
cursor_on(); // needed for ":gui" in .vimrc
full_screen = FALSE;
#ifdef GUI_MAY_FORK
++recursive;
/*
* Quit the current process and continue in the child.
* Makes "gvim file" disconnect from the shell it was started in.
* Don't do this when Vim was started with "-f" or the 'f' flag is present
* in 'guioptions'.
* Don't do this when there is a running job, we can only get the status
* of a child from the parent.
*/
if (gui.dofork && !vim_strchr(p_go, GO_FORG) && recursive <= 1
# ifdef FEAT_JOB_CHANNEL
&& !job_any_running()
# endif
)
{
gui_do_fork();
}
else
#endif
#ifdef GUI_MAY_SPAWN
if (gui.dospawn
# ifdef EXPERIMENTAL_GUI_CMD
&& gui.dofork
# endif
&& !vim_strchr(p_go, GO_FORG)
&& !anyBufIsChanged()
# ifdef FEAT_JOB_CHANNEL
&& !job_any_running()
# endif
)
{
# ifdef EXPERIMENTAL_GUI_CMD
msg =
# endif
gui_mch_do_spawn(arg);
}
else
#endif
{
#ifdef FEAT_GUI_GTK
// If there is 'f' in 'guioptions' and specify -g argument,
// gui_mch_init_check() was not called yet.
if (gui_mch_init_check() != OK)
getout_preserve_modified(1);
#endif
gui_attempt_start();
}
if (!gui.in_use) // failed to start GUI
{
// Back to old term settings
//
// FIXME: If we got here because a child process failed and flagged to
// the parent to resume, and X11 is enabled, this will
// hit an X11 I/O error and do a longjmp(), leaving recursive
// permanently set to 1. This is probably not as big a problem as it
// sounds, because gui_mch_init() in both gui_x11.c and gui_gtk_x11.c
// return "OK" unconditionally, so it would be very difficult to
// actually hit this case.
termcapinit(old_term);
settmode(TMODE_RAW); // restart RAW mode
set_title_defaults(); // set 'title' and 'icon' again
#if defined(GUI_MAY_SPAWN) && defined(EXPERIMENTAL_GUI_CMD)
if (msg != NULL)
emsg(msg);
#endif
}
vim_free(old_term);
// If the GUI started successfully, trigger the GUIEnter event, otherwise
// the GUIFailed event.
gui_mch_update();
apply_autocmds(gui.in_use ? EVENT_GUIENTER : EVENT_GUIFAILED,
NULL, NULL, FALSE, curbuf);
#ifdef GUI_MAY_FORK
--recursive;
#endif
}
/*
* Set_termname() will call gui_init() to start the GUI.
* Set the "starting" flag, to indicate that the GUI will start.
*
* We don't want to open the GUI shell until after we've read .gvimrc,
* otherwise we don't know what font we will use, and hence we don't know
* what size the shell should be. So if there are errors in the .gvimrc
* file, they will have to go to the terminal: Set full_screen to FALSE.
* full_screen will be set to TRUE again by a successful termcapinit().
*/
static void
gui_attempt_start(void)
{
static int recursive = 0;
++recursive;
gui.starting = TRUE;
#ifdef FEAT_GUI_GTK
gui.event_time = GDK_CURRENT_TIME;
#endif
termcapinit((char_u *)"builtin_gui");
gui.starting = recursive - 1;
#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11)
if (gui.in_use)
{
# ifdef FEAT_EVAL
Window x11_window;
Display *x11_display;
if (gui_get_x11_windis(&x11_window, &x11_display) == OK)
set_vim_var_nr(VV_WINDOWID, (long)x11_window);
# endif
// Display error messages in a dialog now.
display_errors();
}
#endif
--recursive;
}
#ifdef GUI_MAY_FORK
// for waitpid()
# if defined(HAVE_SYS_WAIT_H) || defined(HAVE_UNION_WAIT)
# include <sys/wait.h>
# endif
/*
* Create a new process, by forking. In the child, start the GUI, and in
* the parent, exit.
*
* If something goes wrong, this will return with gui.in_use still set
* to FALSE, in which case the caller should continue execution without
* the GUI.
*
* If the child fails to start the GUI, then the child will exit and the
* parent will return. If the child succeeds, then the parent will exit
* and the child will return.
*/
static void
gui_do_fork(void)
{
int pipefd[2]; // pipe between parent and child
int pipe_error;
int status;
int exit_status;
pid_t pid = -1;
# if defined(FEAT_RELTIME) && defined(PROF_NSEC)
// a timer is not carried forward
delete_timer();
# endif
// Setup a pipe between the child and the parent, so that the parent
// knows when the child has done the setsid() call and is allowed to
// exit.
pipe_error = (pipe(pipefd) < 0);
pid = fork();
if (pid < 0) // Fork error
{
emsg(_(e_failed_to_create_new_process_for_GUI));
return;
}
else if (pid > 0) // Parent
{
// Give the child some time to do the setsid(), otherwise the
// exit() may kill the child too (when starting gvim from inside a
// gvim).
if (!pipe_error)
{
// The read returns when the child closes the pipe (or when
// the child dies for some reason).
close(pipefd[1]);
status = gui_read_child_pipe(pipefd[0]);
if (status == GUI_CHILD_FAILED)
{
// The child failed to start the GUI, so the caller must
// continue. There may be more error information written
// to stderr by the child.
# ifdef __NeXT__
wait4(pid, &exit_status, 0, (struct rusage *)0);
# else
waitpid(pid, &exit_status, 0);
# endif
emsg(_(e_the_child_process_failed_to_start_GUI));
return;
}
else if (status == GUI_CHILD_IO_ERROR)
{
pipe_error = TRUE;
}
// else GUI_CHILD_OK: parent exit
}
if (pipe_error)
ui_delay(301L, TRUE);
// When swapping screens we may need to go to the next line, e.g.,
// after a hit-enter prompt and using ":gui".
if (newline_on_exit)
mch_errmsg("\r\n");
/*
* The parent must skip the normal exit() processing, the child
* will do it. For example, GTK messes up signals when exiting.
*/
_exit(0);
}
// Child
# ifdef FEAT_GUI_GTK
// Call gtk_init_check() here after fork(). See gui_init_check().
if (gui_mch_init_check() != OK)
getout_preserve_modified(1);
# endif
# if defined(HAVE_SETSID) || defined(HAVE_SETPGID)
/*
* Change our process group. On some systems/shells a CTRL-C in the
* shell where Vim was started would otherwise kill gvim!
*/
# if defined(HAVE_SETSID)
(void)setsid();
# else
(void)setpgid(0, 0);
# endif
# endif
if (!pipe_error)
close(pipefd[0]);
# if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
// Tell the session manager our new PID
gui_mch_forked();
# endif
// Try to start the GUI
gui_attempt_start();
// Notify the parent
if (!pipe_error)
{
if (gui.in_use)
write_eintr(pipefd[1], "ok", 3);
else
write_eintr(pipefd[1], "fail", 5);
close(pipefd[1]);
}
// If we failed to start the GUI, exit now.
if (!gui.in_use)
getout_preserve_modified(1);
}
/*
* Read from a pipe assumed to be connected to the child process (this
* function is called from the parent).
* Return GUI_CHILD_OK if the child successfully started the GUI,
* GUY_CHILD_FAILED if the child failed, or GUI_CHILD_IO_ERROR if there was
* some other error.
*
* The file descriptor will be closed before the function returns.
*/
static int
gui_read_child_pipe(int fd)
{
long bytes_read;
# define READ_BUFFER_SIZE 10
char buffer[READ_BUFFER_SIZE];
bytes_read = read_eintr(fd, buffer, READ_BUFFER_SIZE - 1);
# undef READ_BUFFER_SIZE
close(fd);
if (bytes_read < 0)
return GUI_CHILD_IO_ERROR;
buffer[bytes_read] = NUL;
if (strcmp(buffer, "ok") == 0)
return GUI_CHILD_OK;
return GUI_CHILD_FAILED;
}
#endif // GUI_MAY_FORK
/*
* Call this when vim starts up, whether or not the GUI is started
*/
void
gui_prepare(int *argc, char **argv)
{
gui.in_use = FALSE; // No GUI yet (maybe later)
gui.starting = FALSE; // No GUI yet (maybe later)
gui_mch_prepare(argc, argv);
}
/*
* Try initializing the GUI and check if it can be started.
* Used from main() to check early if "vim -g" can start the GUI.
* Used from gui_init() to prepare for starting the GUI.
* Returns FAIL or OK.
*/
int
gui_init_check(void)
{
static int result = MAYBE;
if (result != MAYBE)
{
if (result == FAIL)
emsg(_(e_cannot_start_the_GUI));
return result;
}
gui.shell_created = FALSE;
gui.dying = FALSE;
gui.in_focus = TRUE; // so the guicursor setting works
gui.dragged_sb = SBAR_NONE;
gui.dragged_wp = NULL;
gui.pointer_hidden = FALSE;
gui.col = 0;
gui.row = 0;
gui.num_cols = Columns;
gui.num_rows = Rows;
gui.cursor_is_valid = FALSE;
gui.scroll_region_top = 0;
gui.scroll_region_bot = Rows - 1;
gui.scroll_region_left = 0;
gui.scroll_region_right = Columns - 1;
gui.highlight_mask = HL_NORMAL;
gui.char_width = 1;
gui.char_height = 1;
gui.char_ascent = 0;
gui.border_width = 0;
gui.norm_font = NOFONT;
#ifndef FEAT_GUI_GTK
gui.bold_font = NOFONT;
gui.ital_font = NOFONT;
gui.boldital_font = NOFONT;
# ifdef FEAT_XFONTSET
gui.fontset = NOFONTSET;
# endif
#endif
gui.wide_font = NOFONT;
#ifndef FEAT_GUI_GTK
gui.wide_bold_font = NOFONT;
gui.wide_ital_font = NOFONT;
gui.wide_boldital_font = NOFONT;
#endif
#ifdef FEAT_MENU
# ifndef FEAT_GUI_GTK
# ifdef FONTSET_ALWAYS
gui.menu_fontset = NOFONTSET;
# else
gui.menu_font = NOFONT;
# endif
# endif
gui.menu_is_active = TRUE; // default: include menu
# ifndef FEAT_GUI_GTK
gui.menu_height = MENU_DEFAULT_HEIGHT;
gui.menu_width = 0;
# endif
#endif
#if defined(FEAT_TOOLBAR) && (defined(FEAT_GUI_MOTIF) || defined(FEAT_GUI_HAIKU))
gui.toolbar_height = 0;
#endif
#ifdef FEAT_BEVAL_TIP
gui.tooltip_fontset = NOFONTSET;
#endif
gui.scrollbar_width = gui.scrollbar_height = SB_DEFAULT_WIDTH;
gui.prev_wrap = -1;
#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN)
// Note: gui_set_ligatures() might already have been called e.g. from .vimrc,
// and in that case we don't want to overwrite ligatures map that has already
// been correctly populated (as that would lead to a cleared ligatures maps).
if (*p_guiligatures == NUL)
CLEAR_FIELD(gui.ligatures_map);
#endif
#if defined(ALWAYS_USE_GUI) || defined(VIMDLL)
result = OK;
#else
# ifdef FEAT_GUI_GTK
/*
* Note: Don't call gtk_init_check() before fork, it will be called after
* the fork. When calling it before fork, it make vim hang for a while.
* See gui_do_fork().
* Use a simpler check if the GUI window can probably be opened.
*/
result = gui.dofork ? gui_mch_early_init_check(TRUE) : gui_mch_init_check();
# else
result = gui_mch_init_check();
# endif
#endif
return result;
}
/*
* This is the call which starts the GUI.
*/
void
gui_init(void)
{
win_T *wp;
static int recursive = 0;
/*
* It's possible to use ":gui" in a .gvimrc file. The first half of this
* function will then be executed at the first call, the rest by the
* recursive call. This allow the shell to be opened halfway reading a
* gvimrc file.
*/
if (!recursive)
{
++recursive;
clip_init(TRUE);
// If can't initialize, don't try doing the rest
if (gui_init_check() == FAIL)
{
--recursive;
clip_init(FALSE);
return;
}
/*
* Reset 'paste'. It's useful in the terminal, but not in the GUI. It
* breaks the Paste toolbar button.
*/
set_option_value_give_err((char_u *)"paste", 0L, NULL, 0);
// Set t_Co to the number of colors: RGB.
set_color_count(256 * 256 * 256);
/*
* Set up system-wide default menus.
*/
#if defined(SYS_MENU_FILE) && defined(FEAT_MENU)
if (vim_strchr(p_go, GO_NOSYSMENU) == NULL)
{
sys_menu = TRUE;
do_source((char_u *)SYS_MENU_FILE, FALSE, DOSO_NONE, NULL);
sys_menu = FALSE;
}
#endif
/*
* Switch on the mouse by default, unless the user changed it already.
* This can then be changed in the .gvimrc.
*/
if (!option_was_set((char_u *)"mouse"))
set_string_option_direct((char_u *)"mouse", -1,
(char_u *)"a", OPT_FREE, SID_NONE);
/*
* If -U option given, use only the initializations from that file and
* nothing else. Skip all initializations for "-U NONE" or "-u NORC".
*/
if (use_gvimrc != NULL)
{
if (STRCMP(use_gvimrc, "NONE") != 0
&& STRCMP(use_gvimrc, "NORC") != 0
&& do_source(use_gvimrc, FALSE, DOSO_NONE, NULL) != OK)
semsg(_(e_cannot_read_from_str), use_gvimrc);
}
else
{
/*
* Get system wide defaults for gvim, only when file name defined.
*/
#ifdef SYS_GVIMRC_FILE
do_source((char_u *)SYS_GVIMRC_FILE, FALSE, DOSO_NONE, NULL);
#endif
/*
* Try to read GUI initialization commands from the following
* places:
* - environment variable GVIMINIT
* - the user gvimrc file (~/.gvimrc)
* - the second user gvimrc file ($VIM/.gvimrc for Dos)
* - the third user gvimrc file ($VIM/.gvimrc for Amiga)
* The first that exists is used, the rest is ignored.
*/
if (process_env((char_u *)"GVIMINIT", FALSE) == FAIL
&& do_source((char_u *)USR_GVIMRC_FILE, TRUE,
DOSO_GVIMRC, NULL) == FAIL
#ifdef USR_GVIMRC_FILE2
&& do_source((char_u *)USR_GVIMRC_FILE2, TRUE,
DOSO_GVIMRC, NULL) == FAIL
#endif
#ifdef USR_GVIMRC_FILE3
&& do_source((char_u *)USR_GVIMRC_FILE3, TRUE,
DOSO_GVIMRC, NULL) == FAIL
#endif
)
{
#ifdef USR_GVIMRC_FILE4
(void)do_source((char_u *)USR_GVIMRC_FILE4, TRUE,
DOSO_GVIMRC, NULL);
#endif
}
/*
* Read initialization commands from ".gvimrc" in current
* directory. This is only done if the 'exrc' option is set.
* Because of security reasons we disallow shell and write
* commands now, except for unix if the file is owned by the user
* or 'secure' option has been reset in environment of global
* ".gvimrc".
* Only do this if GVIMRC_FILE is not the same as USR_GVIMRC_FILE,
* USR_GVIMRC_FILE2, USR_GVIMRC_FILE3 or SYS_GVIMRC_FILE.
*/
if (p_exrc)
{
#ifdef UNIX
{
stat_T s;
// if ".gvimrc" file is not owned by user, set 'secure'
// mode
if (mch_stat(GVIMRC_FILE, &s) || s.st_uid != getuid())
secure = p_secure;
}
#else
secure = p_secure;
#endif
if ( fullpathcmp((char_u *)USR_GVIMRC_FILE,
(char_u *)GVIMRC_FILE, FALSE, TRUE) != FPC_SAME
#ifdef SYS_GVIMRC_FILE
&& fullpathcmp((char_u *)SYS_GVIMRC_FILE,
(char_u *)GVIMRC_FILE, FALSE, TRUE) != FPC_SAME
#endif
#ifdef USR_GVIMRC_FILE2
&& fullpathcmp((char_u *)USR_GVIMRC_FILE2,
(char_u *)GVIMRC_FILE, FALSE, TRUE) != FPC_SAME
#endif
#ifdef USR_GVIMRC_FILE3
&& fullpathcmp((char_u *)USR_GVIMRC_FILE3,
(char_u *)GVIMRC_FILE, FALSE, TRUE) != FPC_SAME
#endif
#ifdef USR_GVIMRC_FILE4
&& fullpathcmp((char_u *)USR_GVIMRC_FILE4,
(char_u *)GVIMRC_FILE, FALSE, TRUE) != FPC_SAME
#endif
)
do_source((char_u *)GVIMRC_FILE, TRUE, DOSO_GVIMRC, NULL);
if (secure == 2)
need_wait_return = TRUE;
secure = 0;
}
}
if (need_wait_return || msg_didany)
wait_return(TRUE);
--recursive;
}
// If recursive call opened the shell, return here from the first call
if (gui.in_use)
return;
/*
* Create the GUI shell.
*/
gui.in_use = TRUE; // Must be set after menus have been set up
if (gui_mch_init() == FAIL)
goto error;
// Avoid a delay for an error message that was printed in the terminal
// where Vim was started.
emsg_on_display = FALSE;
msg_scrolled = 0;
clear_sb_text(TRUE);
need_wait_return = FALSE;
msg_didany = FALSE;
/*
* Check validity of any generic resources that may have been loaded.
*/
if (gui.border_width < 0)
gui.border_width = 0;
/*
* Set up the fonts. First use a font specified with "-fn" or "-font".
*/
if (font_argument != NULL)
set_option_value_give_err((char_u *)"gfn",
0L, (char_u *)font_argument, 0);
if (
#ifdef FEAT_XFONTSET
(*p_guifontset == NUL
|| gui_init_font(p_guifontset, TRUE) == FAIL) &&
#endif
gui_init_font(*p_guifont == NUL ? hl_get_font_name()
: p_guifont, FALSE) == FAIL)
{
emsg(_(e_cannot_start_gui_no_valid_font_found));
goto error2;
}
if (gui_get_wide_font() == FAIL)
emsg(_(e_guifontwide_invalid));
gui.num_cols = Columns;
gui.num_rows = Rows;
gui_reset_scroll_region();
// Create initial scrollbars
FOR_ALL_WINDOWS(wp)
{
gui_create_scrollbar(&wp->w_scrollbars[SBAR_LEFT], SBAR_LEFT, wp);
gui_create_scrollbar(&wp->w_scrollbars[SBAR_RIGHT], SBAR_RIGHT, wp);
}
gui_create_scrollbar(&gui.bottom_sbar, SBAR_BOTTOM, NULL);
#ifdef FEAT_MENU
gui_create_initial_menus(root_menu);
#endif
#ifdef FEAT_SIGN_ICONS
sign_gui_started();
#endif
// Configure the desired menu and scrollbars
gui_init_which_components(NULL);
// All components of the GUI have been created now
gui.shell_created = TRUE;
#ifdef FEAT_GUI_MSWIN
// Set the shell size, adjusted for the screen size. For GTK this only
// works after the shell has been opened, thus it is further down.
// If the window is already maximized (e.g. when --windowid is passed in),
// we want to use the system-provided dimensions by passing FALSE to
// mustset. Otherwise, we want to initialize with the default rows/columns.
if (gui_mch_maximized())
gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
else
gui_set_shellsize(TRUE, TRUE, RESIZE_BOTH);
#else
# ifndef FEAT_GUI_GTK
gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
# endif
#endif
#if defined(FEAT_GUI_MOTIF) && defined(FEAT_MENU)
// Need to set the size of the menubar after all the menus have been
// created.
gui_mch_compute_menu_height((Widget)0);
#endif
/*
* Actually open the GUI shell.
*/
if (gui_mch_open() != FAIL)
{
maketitle();
resettitle();
init_gui_options();
#ifdef FEAT_ARABIC
// Our GUI can't do bidi.
p_tbidi = FALSE;
#endif
#if defined(FEAT_GUI_GTK)
// Give GTK+ a chance to put all widget's into place.
gui_mch_update();
# ifdef FEAT_MENU
// If there is no 'm' in 'guioptions' we need to remove the menu now.
// It was still there to make F10 work.
if (vim_strchr(p_go, GO_MENUS) == NULL)
{
--gui.starting;
gui_mch_enable_menu(FALSE);
++gui.starting;
gui_mch_update();
}
# endif
// Now make sure the shell fits on the screen.
if (gui_mch_maximized())
gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
else
gui_set_shellsize(TRUE, TRUE, RESIZE_BOTH);
#endif
// When 'lines' was set while starting up the topframe may have to be
// resized.
win_new_shellsize();
#ifdef FEAT_BEVAL_GUI
// Always create the Balloon Evaluation area, but disable it when
// 'ballooneval' is off.
if (balloonEval != NULL)
{
# ifdef FEAT_VARTABS
vim_free(balloonEval->vts);
# endif
vim_free(balloonEval);
}
balloonEvalForTerm = FALSE;
# ifdef FEAT_GUI_GTK
balloonEval = gui_mch_create_beval_area(gui.drawarea, NULL,
&general_beval_cb, NULL);
# else
# if defined(FEAT_GUI_MOTIF)
{
extern Widget textArea;
balloonEval = gui_mch_create_beval_area(textArea, NULL,
&general_beval_cb, NULL);
}
# else
# ifdef FEAT_GUI_MSWIN
balloonEval = gui_mch_create_beval_area(NULL, NULL,
&general_beval_cb, NULL);
# endif
# endif
# endif
if (!p_beval)
gui_mch_disable_beval_area(balloonEval);
#endif
#ifndef FEAT_GUI_MSWIN
// In the GUI modifiers are prepended to keys.
// Don't do this for MS-Windows yet, it sends CTRL-K without the
// modifier.
seenModifyOtherKeys = TRUE;
#endif
#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
if (!im_xim_isvalid_imactivate())
emsg(_(e_value_of_imactivatekey_is_invalid));
#endif
// When 'cmdheight' was set during startup it may not have taken
// effect yet.
if (p_ch != 1L)
command_height();
return;
}
error2:
#ifdef FEAT_GUI_X11
// undo gui_mch_init()
gui_mch_uninit();
#endif
error:
gui.in_use = FALSE;
clip_init(FALSE);
}
void
gui_exit(int rc)
{
// don't free the fonts, it leads to a BUS error
// [email protected] Jul 99
free_highlight_fonts();
gui.in_use = FALSE;
gui_mch_exit(rc);
}
#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_MSWIN) \
|| defined(FEAT_GUI_PHOTON) || defined(PROTO)
# define NEED_GUI_UPDATE_SCREEN 1
/*
* Called when the GUI shell is closed by the user. If there are no changed
* files Vim exits, otherwise there will be a dialog to ask the user what to
* do.
* When this function returns, Vim should NOT exit!
*/
void
gui_shell_closed(void)
{
cmdmod_T save_cmdmod = cmdmod;
if (before_quit_autocmds(curwin, TRUE, FALSE))
return;
// Only exit when there are no changed files
exiting = TRUE;
# ifdef FEAT_BROWSE
cmdmod.cmod_flags |= CMOD_BROWSE;
# endif
# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
cmdmod.cmod_flags |= CMOD_CONFIRM;
# endif
// If there are changed buffers, present the user with a dialog if
// possible, otherwise give an error message.
if (!check_changed_any(FALSE, FALSE))
getout(0);
exiting = FALSE;
cmdmod = save_cmdmod;
gui_update_screen(); // redraw, window may show changed buffer
}
#endif
/*
* Set the font. "font_list" is a comma separated list of font names. The
* first font name that works is used. If none is found, use the default
* font.
* If "fontset" is TRUE, the "font_list" is used as one name for the fontset.
* Return OK when able to set the font. When it failed FAIL is returned and
* the fonts are unchanged.
*/
int
gui_init_font(char_u *font_list, int fontset UNUSED)
{
#define FONTLEN 320
char_u font_name[FONTLEN];
int font_list_empty = FALSE;
int ret = FAIL;
if (!gui.in_use)
return FAIL;
font_name[0] = NUL;
if (*font_list == NUL)
font_list_empty = TRUE;
else
{
#ifdef FEAT_XFONTSET
// When using a fontset, the whole list of fonts is one name.
if (fontset)
ret = gui_mch_init_font(font_list, TRUE);
else
#endif
while (*font_list != NUL)
{
// Isolate one comma separated font name.
(void)copy_option_part(&font_list, font_name, FONTLEN, ",");
// Careful!!! The Win32 version of gui_mch_init_font(), when
// called with "*" will change p_guifont to the selected font
// name, which frees the old value. This makes font_list
// invalid. Thus when OK is returned here, font_list must no
// longer be used!
if (gui_mch_init_font(font_name, FALSE) == OK)
{
#ifdef USE_SET_GUIFONTWIDE
// If it's a Unicode font, try setting 'guifontwide' to a
// similar double-width font.
if ((p_guifontwide == NULL || *p_guifontwide == NUL)
&& strstr((char *)font_name, "10646") != NULL)
set_guifontwide(font_name);
#endif
ret = OK;
break;
}
}
}
if (ret != OK
&& STRCMP(font_list, "*") != 0
&& (font_list_empty || gui.norm_font == NOFONT))
{
/*
* Couldn't load any font in 'font_list', keep the current font if
* there is one. If 'font_list' is empty, or if there is no current
* font, tell gui_mch_init_font() to try to find a font we can load.
*/
ret = gui_mch_init_font(NULL, FALSE);
}
if (ret == OK)
{
#ifndef FEAT_GUI_GTK
// Set normal font as current font
# ifdef FEAT_XFONTSET
if (gui.fontset != NOFONTSET)
gui_mch_set_fontset(gui.fontset);
else
# endif
gui_mch_set_font(gui.norm_font);
#endif
gui_set_shellsize(FALSE, TRUE, RESIZE_BOTH);
}
return ret;
}
#ifdef USE_SET_GUIFONTWIDE
/*
* Try setting 'guifontwide' to a font twice as wide as "name".
*/
static void
set_guifontwide(char_u *name)
{
int i = 0;
char_u wide_name[FONTLEN + 10]; // room for 2 * width and '*'
char_u *wp = NULL;
char_u *p;
GuiFont font;
wp = wide_name;
for (p = name; *p != NUL; ++p)
{
*wp++ = *p;
if (*p == '-')
{
++i;
if (i == 6) // font type: change "--" to "-*-"
{
if (p[1] == '-')
*wp++ = '*';
}
else if (i == 12) // found the width
{
++p;
i = getdigits(&p);
if (i != 0)