blob: 2bed19281610a6d0556a6f845d9bcd05781b8c9c [file] [log] [blame]
Bram Moolenaarb4210b32004-06-13 14:51:16 +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#if defined(MSDOS) || defined(WIN32) || defined(_WIN64)
11# include <io.h> /* for close() and dup() */
12#endif
13
14#define EXTERN
15#include "vim.h"
16
17#ifdef SPAWNO
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +000018# include <spawno.h> /* special MS-DOS swapping library */
Bram Moolenaarb4210b32004-06-13 14:51:16 +000019#endif
20
21#ifdef HAVE_FCNTL_H
22# include <fcntl.h>
23#endif
24
25#ifdef __CYGWIN__
26# ifndef WIN32
27# include <sys/cygwin.h> /* for cygwin_conv_to_posix_path() */
28# endif
29# include <limits.h>
30#endif
31
32#if defined(UNIX) || defined(VMS)
33static int file_owned __ARGS((char *fname));
34#endif
35static void mainerr __ARGS((int, char_u *));
36static void main_msg __ARGS((char *s));
37static void usage __ARGS((void));
38static int get_number_arg __ARGS((char_u *p, int *idx, int def));
39static void main_start_gui __ARGS((void));
40#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
41static void check_swap_exists_action __ARGS((void));
42#endif
43#ifdef FEAT_CLIENTSERVER
44static void cmdsrv_main __ARGS((int *argc, char **argv, char_u *serverName_arg, char_u **serverStr));
45static char_u *serverMakeName __ARGS((char_u *arg, char *cmd));
46#endif
47
48
49#ifdef STARTUPTIME
50static FILE *time_fd = NULL;
51#endif
52
53#define FEAT_PRECOMMANDS
54
55/*
56 * Different types of error messages.
57 */
58static char *(main_errors[]) =
59{
60 N_("Unknown option"),
61#define ME_UNKNOWN_OPTION 0
62 N_("Too many edit arguments"),
63#define ME_TOO_MANY_ARGS 1
64 N_("Argument missing after"),
65#define ME_ARG_MISSING 2
66 N_("Garbage after option"),
67#define ME_GARBAGE 3
68 N_("Too many \"+command\", \"-c command\" or \"--cmd command\" arguments"),
69#define ME_EXTRA_CMD 4
70 N_("Invalid argument for"),
71#define ME_INVALID_ARG 5
72};
73
74/* Maximum number of commands from + or -c options */
75#define MAX_ARG_CMDS 10
76
77#ifndef PROTO /* don't want a prototype for main() */
78 int
79# ifdef VIMDLL
80_export
81# endif
82# ifdef FEAT_GUI_MSWIN
83# ifdef __BORLANDC__
84_cdecl
85# endif
86VimMain
87# else
88main
89# endif
90(argc, argv)
91 int argc;
92 char **argv;
93{
94 char_u *initstr; /* init string from environment */
95 char_u *term = NULL; /* specified terminal name */
96 char_u *fname = NULL; /* file name from command line */
97 char_u *tagname = NULL; /* tag from -t option */
98 char_u *use_vimrc = NULL; /* vimrc from -u option */
99#ifdef FEAT_QUICKFIX
100 char_u *use_ef = NULL; /* 'errorfile' from -q option */
101#endif
102#ifdef FEAT_CRYPT
103 int ask_for_key = FALSE; /* -x argument */
104#endif
105 int n_commands = 0; /* no. of commands from + or -c */
106 char_u *commands[MAX_ARG_CMDS]; /* commands from + or -c option */
107#ifdef FEAT_PRECOMMANDS
108 int p_commands = 0; /* no. of commands from --cmd */
109 char_u *pre_commands[MAX_ARG_CMDS]; /* commands from --cmd option */
110#endif
111 int no_swap_file = FALSE; /* "-n" option used */
112 int c;
113 int i;
114 char_u *p = NULL;
115 int bin_mode = FALSE; /* -b option used */
116#ifdef FEAT_EVAL
117 int use_debug_break_level = -1;
118#endif
119#ifdef FEAT_WINDOWS
120 int window_count = -1; /* number of windows to use */
121 int arg_idx; /* index in argument list */
122 int vert_windows = MAYBE; /* "-O" used instead of "-o" */
123#endif
124 int had_minmin = FALSE; /* found "--" option */
125 int argv_idx; /* index in argv[n][] */
126 int want_full_screen = TRUE;
127 int want_argument; /* option with argument */
128#define EDIT_NONE 0 /* no edit type yet */
129#define EDIT_FILE 1 /* file name argument[s] given, use argument list */
130#define EDIT_STDIN 2 /* read file from stdin */
131#define EDIT_TAG 3 /* tag name argument given, use tagname */
132#define EDIT_QF 4 /* start in quickfix mode */
133 int edit_type = EDIT_NONE; /* type of editing to do */
134#ifdef FEAT_DIFF
135 int diff_mode = FALSE; /* start with 'diff' set */
136#endif
137 int evim_mode = FALSE; /* started as "evim" */
138 int stdout_isatty; /* is stdout a terminal? */
139 int input_isatty; /* is active input a terminal? */
140#ifdef MSWIN
141 int full_path = FALSE;
142#endif
143#ifdef FEAT_CLIENTSERVER
Bram Moolenaar8fc061c2004-12-29 21:03:02 +0000144 char_u *serverStr = NULL; /* remote server command */
145 char_u *serverStrEnc = NULL; /* encoding of serverStr */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000146 char_u *serverName_arg = NULL; /* cmdline arg for server name */
147 int serverArg = FALSE; /* TRUE when argument for a server */
148 char_u *servername = NULL; /* allocated name for our server */
149#endif
150#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
151 int literal = FALSE; /* don't expand file names */
152#endif
153
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000154 /*
155 * Do any system-specific initialisations. These can NOT use IObuff or
156 * NameBuff. Thus emsg2() cannot be called!
157 */
158 mch_early_init();
159
160#ifdef FEAT_TCL
161 vim_tcl_init(argv[0]);
162#endif
163
164#ifdef MEM_PROFILE
165 atexit(vim_mem_profile_dump);
166#endif
167
168#ifdef STARTUPTIME
169 time_fd = fopen(STARTUPTIME, "a");
170 TIME_MSG("--- VIM STARTING ---");
171#endif
172
173#ifdef __EMX__
174 _wildcard(&argc, &argv);
175#endif
176
177#ifdef FEAT_MBYTE
178 (void)mb_init(); /* init mb_bytelen_tab[] to ones */
179#endif
Bram Moolenaardcaf10e2005-01-21 11:55:25 +0000180#ifdef FEAT_EVAL
181 eval_init(); /* init global variables */
182#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000183
184#ifdef __QNXNTO__
185 qnx_init(); /* PhAttach() for clipboard, (and gui) */
186#endif
187
188#ifdef MAC_OS_CLASSIC
189 /* Macintosh needs this before any memory is allocated. */
190 gui_prepare(&argc, argv); /* Prepare for possibly starting GUI sometime */
191 TIME_MSG("GUI prepared");
192#endif
193
194 /* Init the table of Normal mode commands. */
195 init_normal_cmds();
196
197#if defined(HAVE_DATE_TIME) && defined(VMS) && defined(VAXC)
198 make_version();
199#endif
200
201 /*
202 * Allocate space for the generic buffers (needed for set_init_1() and
203 * EMSG2()).
204 */
205 if ((IObuff = alloc(IOSIZE)) == NULL
206 || (NameBuff = alloc(MAXPATHL)) == NULL)
207 mch_exit(0);
208
209 TIME_MSG("Allocated generic buffers");
210
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000211#ifdef NBDEBUG
212 /* Wait a moment for debugging NetBeans. Must be after allocating
213 * NameBuff. */
214 nbdebug_log_init("SPRO_GVIM_DEBUG", "SPRO_GVIM_DLEVEL");
215 nbdebug_wait(WT_ENV | WT_WAIT | WT_STOP, "SPRO_GVIM_WAIT", 20);
216#endif
217
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000218#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
219 /*
220 * Setup to use the current locale (for ctype() and many other things).
221 * NOTE: Translated messages with encodings other than latin1 will not
222 * work until set_init_1() has been called!
223 */
224 setlocale(LC_ALL, "");
225
226# ifdef FEAT_GETTEXT
227 {
228 int mustfree = FALSE;
229
230# ifdef DYNAMIC_GETTEXT
231 /* Initialize the gettext library */
232 dyn_libintl_init(NULL);
233# endif
234 /* expand_env() doesn't work yet, because chartab[] is not initialized
235 * yet, call vim_getenv() directly */
236 p = vim_getenv((char_u *)"VIMRUNTIME", &mustfree);
237 if (p != NULL && *p != NUL)
238 {
239 STRCPY(NameBuff, p);
240 STRCAT(NameBuff, "/lang");
241 bindtextdomain(VIMPACKAGE, (char *)NameBuff);
242 }
243 if (mustfree)
244 vim_free(p);
245 textdomain(VIMPACKAGE);
246 }
247# endif
248 TIME_MSG("locale set");
249#endif
250
251#ifdef FEAT_GUI
252 gui.dofork = TRUE; /* default is to use fork() */
253#endif
254
255#if defined(FEAT_XCLIPBOARD) || defined(FEAT_CLIENTSERVER)
256 /*
257 * Get the name of the display, before gui_prepare() removes it from
258 * argv[]. Used for the xterm-clipboard display.
259 *
260 * Also find the --server... arguments
261 */
262 for (i = 1; i < argc; i++)
263 {
264 if (STRCMP(argv[i], "--") == 0)
265 break;
266# ifdef FEAT_XCLIPBOARD
267 else if (STRICMP(argv[i], "-display") == 0
Bram Moolenaar843ee412004-06-30 16:16:41 +0000268# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_KDE)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000269 || STRICMP(argv[i], "--display") == 0
270# endif
271 )
272 {
273 if (i == argc - 1)
274 mainerr_arg_missing((char_u *)argv[i]);
275 xterm_display = argv[++i];
276 }
277# endif
278# ifdef FEAT_CLIENTSERVER
279 else if (STRICMP(argv[i], "--servername") == 0)
280 {
281 if (i == argc - 1)
282 mainerr_arg_missing((char_u *)argv[i]);
283 serverName_arg = (char_u *)argv[++i];
284 }
285 else if (STRICMP(argv[i], "--serverlist") == 0
286 || STRICMP(argv[i], "--remote-send") == 0
287 || STRICMP(argv[i], "--remote-expr") == 0
288 || STRICMP(argv[i], "--remote") == 0
289 || STRICMP(argv[i], "--remote-silent") == 0)
290 serverArg = TRUE;
291 else if (STRICMP(argv[i], "--remote-wait") == 0
292 || STRICMP(argv[i], "--remote-wait-silent") == 0)
293 {
294 serverArg = TRUE;
295#ifdef FEAT_GUI
296 /* don't fork() when starting the GUI to edit the files ourself */
297 gui.dofork = FALSE;
298#endif
299 }
300# endif
301# ifdef FEAT_GUI_GTK
302 else if (STRICMP(argv[i], "--socketid") == 0)
303 {
304 unsigned int socket_id;
305 int count;
306
307 if (i == argc - 1)
308 mainerr_arg_missing((char_u *)argv[i]);
309 if (STRNICMP(argv[i+1], "0x", 2) == 0)
310 count = sscanf(&(argv[i + 1][2]), "%x", &socket_id);
311 else
312 count = sscanf(argv[i+1], "%u", &socket_id);
313 if (count != 1)
314 mainerr(ME_INVALID_ARG, (char_u *)argv[i]);
315 else
316 gtk_socket_id = socket_id;
317 i++;
318 }
319 else if (STRICMP(argv[i], "--echo-wid") == 0)
320 echo_wid_arg = TRUE;
321# endif
322 }
323#endif
324
325#ifdef FEAT_SUN_WORKSHOP
326 findYourself(argv[0]);
327#endif
328#if defined(FEAT_GUI) && !defined(MAC_OS_CLASSIC)
329 gui_prepare(&argc, argv); /* Prepare for possibly starting GUI sometime */
330 TIME_MSG("GUI prepared");
331#endif
332
333#ifdef FEAT_CLIPBOARD
334 clip_init(FALSE); /* Initialise clipboard stuff */
335 TIME_MSG("clipboard setup");
336#endif
337
338 /*
339 * Check if we have an interactive window.
340 * On the Amiga: If there is no window, we open one with a newcli command
341 * (needed for :! to * work). mch_check_win() will also handle the -d or
342 * -dev argument.
343 */
344 stdout_isatty = (mch_check_win(argc, argv) != FAIL);
345 TIME_MSG("window checked");
346
347 /*
348 * Allocate the first window and buffer. Can't do much without it.
349 */
350 win_alloc_first();
351
352 init_yank(); /* init yank buffers */
353
354 /* Init the argument list to empty. */
355 alist_init(&global_alist);
356
357 /*
358 * Set the default values for the options.
359 * NOTE: Non-latin1 translated messages are working only after this,
360 * because this is where "has_mbyte" will be set, which is used by
361 * msg_outtrans_len_attr().
362 * First find out the home directory, needed to expand "~" in options.
363 */
364 init_homedir(); /* find real value of $HOME */
365 set_init_1();
366 TIME_MSG("inits 1");
367
368#ifdef FEAT_EVAL
369 set_lang_var(); /* set v:lang and v:ctype */
370#endif
371
372#ifdef FEAT_CLIENTSERVER
373 /*
374 * Do the client-server stuff, unless "--servername ''" was used.
375 */
376 if (serverName_arg == NULL || *serverName_arg != NUL)
377 {
378# ifdef WIN32
379 /* Initialise the client/server messaging infrastructure. */
380 serverInitMessaging();
381# endif
382
383 /*
384 * When a command server argument was found, execute it. This may
Bram Moolenaar8fc061c2004-12-29 21:03:02 +0000385 * exit Vim when it was successful. Otherwise it's executed further
386 * on. Remember the encoding used here in "serverStrEnc".
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000387 */
388 if (serverArg)
Bram Moolenaar8fc061c2004-12-29 21:03:02 +0000389 {
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000390 cmdsrv_main(&argc, argv, serverName_arg, &serverStr);
Bram Moolenaar8fc061c2004-12-29 21:03:02 +0000391# ifdef FEAT_MBYTE
392 serverStrEnc = vim_strsave(p_enc);
393# endif
394 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000395
396 /* If we're still running, get the name to register ourselves.
397 * On Win32 can register right now, for X11 need to setup the
398 * clipboard first, it's further down. */
399 servername = serverMakeName(serverName_arg, argv[0]);
400# ifdef WIN32
401 if (servername != NULL)
402 {
403 serverSetName(servername);
404 vim_free(servername);
405 }
406# endif
407 }
408#endif
409
410 /*
411 * Check for: [r][e][g][vi|vim|view][diff][ex[im]]
412 * If the executable name starts with "r" we disable shell commands.
413 * If the next character is "e" we run in Easy mode.
414 * If the next character is "g" we run the GUI version.
415 * If the next characters are "view" we start in readonly mode.
416 * If the next characters are "diff" or "vimdiff" we start in diff mode.
417 * If the next characters are "ex" we start in Ex mode. If it's followed
418 * by "im" use improved Ex mode.
419 */
420 initstr = gettail((char_u *)argv[0]);
421
422#ifdef MACOS_X_UNIX
423 /* An issue has been seen when launching Vim in such a way that
424 * $PWD/$ARGV[0] or $ARGV[0] is not the absolute path to the
425 * executable or a symbolic link of it. Until this issue is resolved
426 * we prohibit the GUI from being used.
427 */
428 if (STRCMP(initstr, argv[0]) == 0)
429 disallow_gui = TRUE;
430#endif
431
432#ifdef FEAT_EVAL
433 set_vim_var_string(VV_PROGNAME, initstr, -1);
434#endif
435
436 /* TODO: On MacOS X default to gui if argv[0] ends in:
437 * /vim.app/Contents/MacOS/Vim */
438
439 if (TOLOWER_ASC(initstr[0]) == 'r')
440 {
441 restricted = TRUE;
442 ++initstr;
443 }
444
445 /* Avoid using evim mode for "editor". */
446 if (TOLOWER_ASC(initstr[0]) == 'e'
447 && (TOLOWER_ASC(initstr[1]) == 'v'
448 || TOLOWER_ASC(initstr[1]) == 'g'))
449 {
450#ifdef FEAT_GUI
451 gui.starting = TRUE;
452#endif
453 evim_mode = TRUE;
454 ++initstr;
455 }
456
Bram Moolenaar843ee412004-06-30 16:16:41 +0000457 if (TOLOWER_ASC(initstr[0]) == 'g' || initstr[0] == 'k')
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000458 {
459 main_start_gui();
460#ifdef FEAT_GUI
461 ++initstr;
462#endif
463 }
464
465 if (STRNICMP(initstr, "view", 4) == 0)
466 {
467 readonlymode = TRUE;
468 curbuf->b_p_ro = TRUE;
469 p_uc = 10000; /* don't update very often */
470 initstr += 4;
471 }
472 else if (STRNICMP(initstr, "vim", 3) == 0)
473 initstr += 3;
474
475 /* Catch "[r][g]vimdiff" and "[r][g]viewdiff". */
476 if (STRICMP(initstr, "diff") == 0)
477 {
478#ifdef FEAT_DIFF
479 diff_mode = TRUE;
480#else
481 mch_errmsg(_("This Vim was not compiled with the diff feature."));
482 mch_errmsg("\n");
483 mch_exit(2);
484#endif
485 }
486
487 if (STRNICMP(initstr, "ex", 2) == 0)
488 {
489 if (STRNICMP(initstr + 2, "im", 2) == 0)
490 exmode_active = EXMODE_VIM;
491 else
492 exmode_active = EXMODE_NORMAL;
493 change_compatible(TRUE); /* set 'compatible' */
494 }
495
496 initstr = gettail((char_u *)argv[0]);
497 ++argv;
498 --argc;
499
500 /*
501 * Process the command line arguments.
502 */
503 argv_idx = 1; /* active option letter is argv[0][argv_idx] */
504 while (argc > 0)
505 {
506 /*
507 * "+" or "+{number}" or "+/{pat}" or "+{command}" argument.
508 */
509 if (argv[0][0] == '+' && !had_minmin)
510 {
511 if (n_commands >= MAX_ARG_CMDS)
512 mainerr(ME_EXTRA_CMD, NULL);
513 argv_idx = -1; /* skip to next argument */
514 if (argv[0][1] == NUL)
515 commands[n_commands++] = (char_u *)"$";
516 else
517 commands[n_commands++] = (char_u *)&(argv[0][1]);
518 }
519
520 /*
521 * Optional argument.
522 */
523 else if (argv[0][0] == '-' && !had_minmin)
524 {
525 want_argument = FALSE;
526 c = argv[0][argv_idx++];
527#ifdef VMS
528 /*
529 * VMS only uses upper case command lines. Interpret "-X" as "-x"
530 * and "-/X" as "-X".
531 */
532 if (c == '/')
533 {
534 c = argv[0][argv_idx++];
535 c = TOUPPER_ASC(c);
536 }
537 else
538 c = TOLOWER_ASC(c);
539#endif
540 switch (c)
541 {
542 case NUL: /* "vim -" read from stdin */
543 /* "ex -" silent mode */
544 if (exmode_active)
545 silent_mode = TRUE;
546 else
547 {
548 if (edit_type != EDIT_NONE)
549 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
550 edit_type = EDIT_STDIN;
551 read_cmd_fd = 2; /* read from stderr instead of stdin */
552 }
553 argv_idx = -1; /* skip to next argument */
554 break;
555
556 case '-': /* "--" don't take any more options */
557 /* "--help" give help message */
558 /* "--version" give version message */
559 /* "--literal" take files literally */
560 /* "--nofork" don't fork */
561 /* "--noplugin[s]" skip plugins */
562 /* "--cmd <cmd>" execute cmd before vimrc */
563 if (STRICMP(argv[0] + argv_idx, "help") == 0)
564 usage();
565 else if (STRICMP(argv[0] + argv_idx, "version") == 0)
566 {
567 Columns = 80; /* need to init Columns */
568 info_message = TRUE; /* use mch_msg(), not mch_errmsg() */
569 list_version();
570 msg_putchar('\n');
571 msg_didout = FALSE;
572 mch_exit(0);
573 }
574 else if (STRNICMP(argv[0] + argv_idx, "literal", 7) == 0)
575 {
576#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
577 literal = TRUE;
578#endif
579 }
580 else if (STRNICMP(argv[0] + argv_idx, "nofork", 6) == 0)
581 {
582#ifdef FEAT_GUI
583 gui.dofork = FALSE; /* don't fork() when starting GUI */
584#endif
585 }
586 else if (STRNICMP(argv[0] + argv_idx, "noplugin", 8) == 0)
587 p_lpl = FALSE;
588#ifdef FEAT_PRECOMMANDS
589 else if (STRNICMP(argv[0] + argv_idx, "cmd", 3) == 0)
590 {
591 want_argument = TRUE;
592 argv_idx += 3;
593 }
594#endif
595#ifdef FEAT_CLIENTSERVER
596 else if (STRNICMP(argv[0] + argv_idx, "serverlist", 10) == 0)
597 ; /* already processed -- no arg */
598 else if (STRNICMP(argv[0] + argv_idx, "servername", 10) == 0
599 || STRNICMP(argv[0] + argv_idx, "serversend", 10) == 0)
600 {
601 /* already processed -- snatch the following arg */
602 if (argc > 1)
603 {
604 --argc;
605 ++argv;
606 }
607 }
608#endif
609#ifdef FEAT_GUI_GTK
610 else if (STRNICMP(argv[0] + argv_idx, "socketid", 8) == 0)
611 {
612 /* already processed -- snatch the following arg */
613 if (argc > 1)
614 {
615 --argc;
616 ++argv;
617 }
618 }
619 else if (STRNICMP(argv[0] + argv_idx, "echo-wid", 8) == 0)
620 {
621 /* already processed, skip */
622 }
623#endif
624 else
625 {
626 if (argv[0][argv_idx])
627 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
628 had_minmin = TRUE;
629 }
630 if (!want_argument)
631 argv_idx = -1; /* skip to next argument */
632 break;
633
634 case 'A': /* "-A" start in Arabic mode */
635#ifdef FEAT_ARABIC
636 set_option_value((char_u *)"arabic", 1L, NULL, 0);
637#else
638 mch_errmsg(_(e_noarabic));
639 mch_exit(2);
640#endif
641 break;
642
643 case 'b': /* "-b" binary mode */
644 bin_mode = TRUE; /* postpone to after reading .exrc files */
645 break;
646
647 case 'C': /* "-C" Compatible */
648 change_compatible(TRUE);
649 break;
650
651 case 'e': /* "-e" Ex mode */
652 exmode_active = EXMODE_NORMAL;
653 break;
654
655 case 'E': /* "-E" Improved Ex mode */
656 exmode_active = EXMODE_VIM;
657 break;
658
659 case 'f': /* "-f" GUI: run in foreground. Amiga: open
660 window directly, not with newcli */
661#ifdef FEAT_GUI
662 gui.dofork = FALSE; /* don't fork() when starting GUI */
663#endif
664 break;
665
666 case 'g': /* "-g" start GUI */
667 main_start_gui();
668 break;
669
670 case 'F': /* "-F" start in Farsi mode: rl + fkmap set */
671#ifdef FEAT_FKMAP
672 curwin->w_p_rl = p_fkmap = TRUE;
673#else
674 mch_errmsg(_(e_nofarsi));
675 mch_exit(2);
676#endif
677 break;
678
679 case 'h': /* "-h" give help message */
680#ifdef FEAT_GUI_GNOME
681 /* Tell usage() to exit for "gvim". */
682 gui.starting = FALSE;
683#endif
684 usage();
685 break;
686
687 case 'H': /* "-H" start in Hebrew mode: rl + hkmap set */
688#ifdef FEAT_RIGHTLEFT
689 curwin->w_p_rl = p_hkmap = TRUE;
690#else
691 mch_errmsg(_(e_nohebrew));
692 mch_exit(2);
693#endif
694 break;
695
696 case 'l': /* "-l" lisp mode, 'lisp' and 'showmatch' on */
697#ifdef FEAT_LISP
698 set_option_value((char_u *)"lisp", 1L, NULL, 0);
699 p_sm = TRUE;
700#endif
701 break;
702
703#ifdef TARGET_API_MAC_OSX
704 /* For some reason on MacOS X, an argument like:
705 -psn_0_10223617 is passed in when invoke from Finder
706 or with the 'open' command */
707 case 'p':
708 argv_idx = -1; /* bypass full -psn */
709 main_start_gui();
710 break;
711#endif
712 case 'M': /* "-M" no changes or writing of files */
713 reset_modifiable();
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +0000714 /* FALLTHROUGH */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000715
716 case 'm': /* "-m" no writing of files */
717 p_write = FALSE;
718 break;
719
720 case 'y': /* "-y" easy mode */
721#ifdef FEAT_GUI
722 gui.starting = TRUE; /* start GUI a bit later */
723#endif
724 evim_mode = TRUE;
725 break;
726
727 case 'N': /* "-N" Nocompatible */
728 change_compatible(FALSE);
729 break;
730
731 case 'n': /* "-n" no swap file */
732 no_swap_file = TRUE;
733 break;
734
735 case 'o': /* "-o[N]" open N horizontal split windows */
736#ifdef FEAT_WINDOWS
737 /* default is 0: open window for each file */
738 window_count = get_number_arg((char_u *)argv[0], &argv_idx, 0);
739 vert_windows = FALSE;
740#endif
741 break;
742
743 case 'O': /* "-O[N]" open N vertical split windows */
744#if defined(FEAT_VERTSPLIT) && defined(FEAT_WINDOWS)
745 /* default is 0: open window for each file */
746 window_count = get_number_arg((char_u *)argv[0], &argv_idx, 0);
747 vert_windows = TRUE;
748#endif
749 break;
750
751#ifdef FEAT_QUICKFIX
752 case 'q': /* "-q" QuickFix mode */
753 if (edit_type != EDIT_NONE)
754 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
755 edit_type = EDIT_QF;
756 if (argv[0][argv_idx]) /* "-q{errorfile}" */
757 {
758 use_ef = (char_u *)argv[0] + argv_idx;
759 argv_idx = -1;
760 }
761 else if (argc > 1) /* "-q {errorfile}" */
762 want_argument = TRUE;
763 break;
764#endif
765
766 case 'R': /* "-R" readonly mode */
767 readonlymode = TRUE;
768 curbuf->b_p_ro = TRUE;
769 p_uc = 10000; /* don't update very often */
770 break;
771
772 case 'r': /* "-r" recovery mode */
773 case 'L': /* "-L" recovery mode */
774 recoverymode = 1;
775 break;
776
777 case 's':
778 if (exmode_active) /* "-s" silent (batch) mode */
779 silent_mode = TRUE;
780 else /* "-s {scriptin}" read from script file */
781 want_argument = TRUE;
782 break;
783
784 case 't': /* "-t {tag}" or "-t{tag}" jump to tag */
785 if (edit_type != EDIT_NONE)
786 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
787 edit_type = EDIT_TAG;
788 if (argv[0][argv_idx]) /* "-t{tag}" */
789 {
790 tagname = (char_u *)argv[0] + argv_idx;
791 argv_idx = -1;
792 }
793 else /* "-t {tag}" */
794 want_argument = TRUE;
795 break;
796
797#ifdef FEAT_EVAL
798 case 'D': /* "-D" Debugging */
799 use_debug_break_level = 9999;
800 break;
801#endif
802#ifdef FEAT_DIFF
803 case 'd': /* "-d" 'diff' */
804# ifdef AMIGA
805 /* check for "-dev {device}" */
806 if (argv[0][argv_idx] == 'e' && argv[0][argv_idx + 1] == 'v')
807 want_argument = TRUE;
808 else
809# endif
810 diff_mode = TRUE;
811 break;
812#endif
813 case 'V': /* "-V{N}" Verbose level */
814 /* default is 10: a little bit verbose */
815 p_verbose = get_number_arg((char_u *)argv[0], &argv_idx, 10);
Bram Moolenaar54ee7752005-05-31 22:22:17 +0000816 if (argv[0][argv_idx] != NUL)
817 {
818 set_option_value((char_u *)"verbosefile", 0L,
819 (char_u *)argv[0] + argv_idx, 0);
820 argv_idx = STRLEN(argv[0]);
821 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000822 break;
823
824 case 'v': /* "-v" Vi-mode (as if called "vi") */
825 exmode_active = 0;
826#ifdef FEAT_GUI
827 gui.starting = FALSE; /* don't start GUI */
828#endif
829 break;
830
831 case 'w': /* "-w{number}" set window height */
832 /* "-w {scriptout}" write to script */
833 if (vim_isdigit(((char_u *)argv[0])[argv_idx]))
834 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000835 i = get_number_arg((char_u *)argv[0], &argv_idx, 10);
836 set_option_value((char_u *)"window", (long)i, NULL, 0);
837 break;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000838 }
839 want_argument = TRUE;
840 break;
841
842#ifdef FEAT_CRYPT
843 case 'x': /* "-x" encrypted reading/writing of files */
844 ask_for_key = TRUE;
845 break;
846#endif
847
848 case 'X': /* "-X" don't connect to X server */
849#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
850 x_no_connect = TRUE;
851#endif
852 break;
853
854 case 'Z': /* "-Z" restricted mode */
855 restricted = TRUE;
856 break;
857
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000858 case 'c': /* "-c{command}" or "-c {command}" execute
859 command */
860 if (argv[0][argv_idx] != NUL)
861 {
862 if (n_commands >= MAX_ARG_CMDS)
863 mainerr(ME_EXTRA_CMD, NULL);
864 commands[n_commands++] = (char_u *)argv[0] + argv_idx;
865 argv_idx = -1;
866 break;
867 }
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +0000868 /*FALLTHROUGH*/
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000869 case 'S': /* "-S {file}" execute Vim script */
870 case 'i': /* "-i {viminfo}" use for viminfo */
871#ifndef FEAT_DIFF
872 case 'd': /* "-d {device}" device (for Amiga) */
873#endif
874 case 'T': /* "-T {terminal}" terminal name */
875 case 'u': /* "-u {vimrc}" vim inits file */
876 case 'U': /* "-U {gvimrc}" gvim inits file */
877 case 'W': /* "-W {scriptout}" overwrite */
878#ifdef FEAT_GUI_W32
879 case 'P': /* "-P {parent title}" MDI parent */
880#endif
881 want_argument = TRUE;
882 break;
883
884 default:
885 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
886 }
887
888 /*
889 * Handle options with argument.
890 */
891 if (want_argument)
892 {
893 /*
894 * Check for garbage immediately after the option letter.
895 */
896 if (argv[0][argv_idx] != NUL)
897 mainerr(ME_GARBAGE, (char_u *)argv[0]);
898
899 --argc;
900 if (argc < 1 && c != 'S')
901 mainerr_arg_missing((char_u *)argv[0]);
902 ++argv;
903 argv_idx = -1;
904
905 switch (c)
906 {
907 case 'c': /* "-c {command}" execute command */
908 case 'S': /* "-S {file}" execute Vim script */
909 if (n_commands >= MAX_ARG_CMDS)
910 mainerr(ME_EXTRA_CMD, NULL);
911 if (c == 'S')
912 {
913 char *a;
914
915 if (argc < 1)
916 /* "-S" without argument: use default session file
917 * name. */
918 a = SESSION_FILE;
919 else if (argv[0][0] == '-')
920 {
921 /* "-S" followed by another option: use default
922 * session file name. */
923 a = SESSION_FILE;
924 ++argc;
925 --argv;
926 }
927 else
928 a = argv[0];
929 p = alloc((unsigned)(STRLEN(a) + 4));
930 if (p == NULL)
931 mch_exit(2);
932 sprintf((char *)p, "so %s", a);
933 commands[n_commands++] = p;
934 }
935 else
936 commands[n_commands++] = (char_u *)argv[0];
937 break;
938
939#ifdef FEAT_PRECOMMANDS
940 case '-': /* "--cmd {command}" execute command */
941 if (p_commands >= MAX_ARG_CMDS)
942 mainerr(ME_EXTRA_CMD, NULL);
943 pre_commands[p_commands++] = (char_u *)argv[0];
944 break;
945#endif
946
947 /* case 'd': -d {device} is handled in mch_check_win() for the
948 * Amiga */
949
950#ifdef FEAT_QUICKFIX
951 case 'q': /* "-q {errorfile}" QuickFix mode */
952 use_ef = (char_u *)argv[0];
953 break;
954#endif
955
956 case 'i': /* "-i {viminfo}" use for viminfo */
957 use_viminfo = (char_u *)argv[0];
958 break;
959
960 case 's': /* "-s {scriptin}" read from script file */
961 if (scriptin[0] != NULL)
962 {
963scripterror:
964 mch_errmsg(_("Attempt to open script file again: \""));
965 mch_errmsg(argv[-1]);
966 mch_errmsg(" ");
967 mch_errmsg(argv[0]);
968 mch_errmsg("\"\n");
969 mch_exit(2);
970 }
971 if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL)
972 {
973 mch_errmsg(_("Cannot open for reading: \""));
974 mch_errmsg(argv[0]);
975 mch_errmsg("\"\n");
976 mch_exit(2);
977 }
978 if (save_typebuf() == FAIL)
979 mch_exit(2); /* out of memory */
980 break;
981
982 case 't': /* "-t {tag}" */
983 tagname = (char_u *)argv[0];
984 break;
985
986 case 'T': /* "-T {terminal}" terminal name */
987 /*
988 * The -T term option is always available and when
989 * HAVE_TERMLIB is supported it overrides the environment
990 * variable TERM.
991 */
992#ifdef FEAT_GUI
993 if (term_is_gui((char_u *)argv[0]))
994 gui.starting = TRUE; /* start GUI a bit later */
995 else
996#endif
997 term = (char_u *)argv[0];
998 break;
999
1000 case 'u': /* "-u {vimrc}" vim inits file */
1001 use_vimrc = (char_u *)argv[0];
1002 break;
1003
1004 case 'U': /* "-U {gvimrc}" gvim inits file */
1005#ifdef FEAT_GUI
1006 use_gvimrc = (char_u *)argv[0];
1007#endif
1008 break;
1009
Bram Moolenaar4399ef42005-02-12 14:29:27 +00001010 case 'w': /* "-w {nr}" 'window' value */
1011 /* "-w {scriptout}" append to script file */
1012 if (vim_isdigit(*((char_u *)argv[0])))
1013 {
1014 argv_idx = 0;
1015 i = get_number_arg((char_u *)argv[0], &argv_idx, 10);
1016 set_option_value((char_u *)"window", (long)i, NULL, 0);
1017 argv_idx = -1;
1018 break;
1019 }
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00001020 /*FALLTHROUGH*/
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001021 case 'W': /* "-W {scriptout}" overwrite script file */
1022 if (scriptout != NULL)
1023 goto scripterror;
1024 if ((scriptout = mch_fopen(argv[0],
1025 c == 'w' ? APPENDBIN : WRITEBIN)) == NULL)
1026 {
1027 mch_errmsg(_("Cannot open for script output: \""));
1028 mch_errmsg(argv[0]);
1029 mch_errmsg("\"\n");
1030 mch_exit(2);
1031 }
1032 break;
1033
1034#ifdef FEAT_GUI_W32
1035 case 'P': /* "-P {parent title}" MDI parent */
1036 gui_mch_set_parent(argv[0]);
1037 break;
1038#endif
1039 }
1040 }
1041 }
1042
1043 /*
1044 * File name argument.
1045 */
1046 else
1047 {
1048 argv_idx = -1; /* skip to next argument */
1049
1050 /* Check for only one type of editing. */
1051 if (edit_type != EDIT_NONE && edit_type != EDIT_FILE)
1052 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1053 edit_type = EDIT_FILE;
1054
1055#ifdef MSWIN
1056 /* Remember if the argument was a full path before changing
1057 * slashes to backslashes. */
1058 if (argv[0][0] != NUL && argv[0][1] == ':' && argv[0][2] == '\\')
1059 full_path = TRUE;
1060#endif
1061
1062 /* Add the file to the global argument list. */
1063 if (ga_grow(&global_alist.al_ga, 1) == FAIL
1064 || (p = vim_strsave((char_u *)argv[0])) == NULL)
1065 mch_exit(2);
1066#ifdef FEAT_DIFF
1067 if (diff_mode && mch_isdir(p) && GARGCOUNT > 0
1068 && !mch_isdir(alist_name(&GARGLIST[0])))
1069 {
1070 char_u *r;
1071
1072 r = concat_fnames(p, gettail(alist_name(&GARGLIST[0])), TRUE);
1073 if (r != NULL)
1074 {
1075 vim_free(p);
1076 p = r;
1077 }
1078 }
1079#endif
1080#if defined(__CYGWIN32__) && !defined(WIN32)
1081 /*
1082 * If vim is invoked by non-Cygwin tools, convert away any
1083 * DOS paths, so things like .swp files are created correctly.
1084 * Look for evidence of non-Cygwin paths before we bother.
1085 * This is only for when using the Unix files.
1086 */
1087 if (strpbrk(p, "\\:") != NULL)
1088 {
1089 char posix_path[PATH_MAX];
1090
1091 cygwin_conv_to_posix_path(p, posix_path);
1092 vim_free(p);
1093 p = vim_strsave(posix_path);
1094 if (p == NULL)
1095 mch_exit(2);
1096 }
1097#endif
1098 alist_add(&global_alist, p,
1099#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
1100 literal ? 2 : 0 /* add buffer number after expanding */
1101#else
1102 2 /* add buffer number now and use curbuf */
1103#endif
1104 );
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00001105
1106#if defined(FEAT_MBYTE) && defined(WIN32)
1107 {
1108 extern void used_file_arg(char *, int, int);
1109
1110 /* Remember this argument has been added to the argument list.
1111 * Needed when 'encoding' is changed. */
1112 used_file_arg(argv[0], literal, full_path);
1113 }
1114#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001115 }
1116
1117 /*
1118 * If there are no more letters after the current "-", go to next
1119 * argument. argv_idx is set to -1 when the current argument is to be
1120 * skipped.
1121 */
1122 if (argv_idx <= 0 || argv[0][argv_idx] == NUL)
1123 {
1124 --argc;
1125 ++argv;
1126 argv_idx = 1;
1127 }
1128 }
1129 TIME_MSG("parsing arguments");
1130
1131 /*
1132 * On some systems, when we compile with the GUI, we always use it. On Mac
1133 * there is no terminal version, and on Windows we can't figure out how to
1134 * fork one off with :gui.
1135 */
1136#ifdef ALWAYS_USE_GUI
1137 gui.starting = TRUE;
1138#else
Bram Moolenaar843ee412004-06-30 16:16:41 +00001139# if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_KDE)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001140 /*
1141 * Check if the GUI can be started. Reset gui.starting if not.
1142 * Don't know about other systems, stay on the safe side and don't check.
1143 */
1144 if (gui.starting && gui_init_check() == FAIL)
1145 {
1146 gui.starting = FALSE;
1147
1148 /* When running "evim" or "gvim -y" we need the menus, exit if we
1149 * don't have them. */
1150 if (evim_mode)
1151 mch_exit(1);
1152 }
1153# endif
1154#endif
1155
1156 /* "-b" argument used. Check before expanding file names, because for
1157 * Win32 this makes us edit a shortcut file itself, instead of the file it
1158 * links to. */
1159 if (bin_mode)
1160 {
1161 set_options_bin(curbuf->b_p_bin, 1, 0);
1162 curbuf->b_p_bin = 1; /* binary file I/O */
1163 }
1164
1165 if (GARGCOUNT > 0)
1166 {
1167#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
1168 /*
1169 * Expand wildcards in file names.
1170 */
1171 if (!literal)
1172 {
1173 /* Temporarily add '(' and ')' to 'isfname'. These are valid
1174 * filename characters but are excluded from 'isfname' to make
1175 * "gf" work on a file name in parenthesis (e.g.: see vim.h). */
1176 do_cmdline_cmd((char_u *)":set isf+=(,)");
Bram Moolenaar86b68352004-12-27 21:59:20 +00001177 alist_expand(NULL, 0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001178 do_cmdline_cmd((char_u *)":set isf&");
1179 }
1180#endif
1181 fname = alist_name(&GARGLIST[0]);
1182 }
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00001183
1184#if defined(WIN32) && defined(FEAT_MBYTE)
1185 {
1186 extern void set_alist_count(void);
1187
1188 /* Remember the number of entries in the argument list. If it changes
1189 * we don't react on setting 'encoding'. */
1190 set_alist_count();
1191 }
1192#endif
1193
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001194#ifdef MSWIN
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001195 if (GARGCOUNT == 1 && full_path)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001196 {
1197 /*
1198 * If there is one filename, fully qualified, we have very probably
1199 * been invoked from explorer, so change to the file's directory.
1200 * Hint: to avoid this when typing a command use a forward slash.
1201 * If the cd fails, it doesn't matter.
1202 */
1203 (void)vim_chdirfile(fname);
1204 }
1205#endif
1206 TIME_MSG("expanding arguments");
1207
1208#ifdef FEAT_DIFF
1209 if (diff_mode)
1210 {
1211 if (window_count == -1)
1212 window_count = 0; /* open up to 3 files in a window */
1213 if (vert_windows == MAYBE)
1214 vert_windows = TRUE; /* use vertical split */
1215 }
1216#endif
1217
1218 ++RedrawingDisabled;
1219
1220 /*
1221 * When listing swap file names, don't do cursor positioning et. al.
1222 */
1223 if (recoverymode && fname == NULL)
1224 want_full_screen = FALSE;
1225
1226 /*
1227 * When certain to start the GUI, don't check capabilities of terminal.
1228 * For GTK we can't be sure, but when started from the desktop it doesn't
1229 * make sense to try using a terminal.
1230 */
Bram Moolenaar843ee412004-06-30 16:16:41 +00001231#if defined(ALWAYS_USE_GUI) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_KDE)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001232 if (gui.starting
1233# ifdef FEAT_GUI_GTK
1234 && !isatty(2)
1235# endif
1236 )
1237 want_full_screen = FALSE;
1238#endif
1239
1240#if defined(FEAT_GUI_MAC) && defined(MACOS_X_UNIX)
1241 /* When the GUI is started from Finder, need to display messages in a
1242 * message box. isatty(2) returns TRUE anyway, thus we need to check the
1243 * name to know we're not started from a terminal. */
1244 if (gui.starting && (!isatty(2) || strcmp("/dev/console", ttyname(2)) == 0))
1245 want_full_screen = FALSE;
1246#endif
1247
1248 /*
1249 * mch_init() sets up the terminal (window) for use. This must be
1250 * done after resetting full_screen, otherwise it may move the cursor
1251 * (MSDOS).
1252 * Note that we may use mch_exit() before mch_init()!
1253 */
1254 mch_init();
1255 TIME_MSG("shell init");
1256
1257#ifdef USE_XSMP
1258 /*
1259 * For want of anywhere else to do it, try to connect to xsmp here.
1260 * Fitting it in after gui_mch_init, but before gui_init (via termcapinit).
1261 * Hijacking -X 'no X connection' to also disable XSMP connection as that
1262 * has a similar delay upon failure.
1263 * Only try if SESSION_MANAGER is set to something non-null.
1264 */
1265 if (!x_no_connect)
1266 {
1267 p = (char_u *)getenv("SESSION_MANAGER");
1268 if (p != NULL && *p != NUL)
1269 {
1270 xsmp_init();
1271 TIME_MSG("xsmp init");
1272 }
1273 }
1274#endif
1275
1276 /*
1277 * Print a warning if stdout is not a terminal.
1278 * When starting in Ex mode and commands come from a file, set Silent mode.
1279 */
1280 input_isatty = mch_input_isatty();
1281 if (exmode_active)
1282 {
1283 if (!input_isatty)
1284 silent_mode = TRUE;
1285 }
1286 else if (want_full_screen && (!stdout_isatty || !input_isatty)
1287#ifdef FEAT_GUI
1288 /* don't want the delay when started from the desktop */
1289 && !gui.starting
1290#endif
1291 )
1292 {
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +00001293#ifdef NBDEBUG
1294 /*
1295 * This shouldn't be necessary. But if I run netbeans with the log
1296 * output coming to the console and XOpenDisplay fails, I get vim
1297 * trying to start with input/output to my console tty. This fills my
1298 * input buffer so fast I can't even kill the process in under 2
1299 * minutes (and it beeps continuosly the whole time :-)
1300 */
1301 if (usingNetbeans && (!stdout_isatty || !input_isatty))
1302 {
1303 mch_errmsg(_("Vim: Error: Failure to start gvim from NetBeans\n"));
1304 exit(1);
1305 }
1306#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001307 if (!stdout_isatty)
1308 mch_errmsg(_("Vim: Warning: Output is not to a terminal\n"));
1309 if (!input_isatty)
1310 mch_errmsg(_("Vim: Warning: Input is not from a terminal\n"));
1311 out_flush();
1312 if (scriptin[0] == NULL)
1313 ui_delay(2000L, TRUE);
1314 TIME_MSG("Warning delay");
1315 }
1316
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001317 /* This message comes before term inits, but after setting "silent_mode"
1318 * when the input is not a tty. */
1319 if (GARGCOUNT > 1 && !silent_mode)
1320 printf(_("%d files to edit\n"), GARGCOUNT);
1321
1322 if (want_full_screen && !silent_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001323 {
1324 termcapinit(term); /* set terminal name and get terminal
1325 capabilities (will set full_screen) */
1326 screen_start(); /* don't know where cursor is now */
1327 TIME_MSG("Termcap init");
1328 }
1329
1330 /*
1331 * Set the default values for the options that use Rows and Columns.
1332 */
1333 ui_get_shellsize(); /* inits Rows and Columns */
1334#ifdef FEAT_NETBEANS_INTG
1335 if (usingNetbeans)
1336 Columns += 2; /* leave room for glyph gutter */
1337#endif
1338 firstwin->w_height = Rows - p_ch;
1339 topframe->fr_height = Rows - p_ch;
1340#ifdef FEAT_VERTSPLIT
1341 firstwin->w_width = Columns;
1342 topframe->fr_width = Columns;
1343#endif
1344#ifdef FEAT_DIFF
1345 /* Set the 'diff' option now, so that it can be checked for in a .vimrc
1346 * file. There is no buffer yet though. */
1347 if (diff_mode)
1348 diff_win_options(firstwin, FALSE);
1349#endif
1350
1351 cmdline_row = Rows - p_ch;
1352 msg_row = cmdline_row;
1353 screenalloc(FALSE); /* allocate screen buffers */
1354 set_init_2();
1355 TIME_MSG("inits 2");
1356
1357 msg_scroll = TRUE;
1358 no_wait_return = TRUE;
1359
1360 init_mappings(); /* set up initial mappings */
1361
1362 init_highlight(TRUE, FALSE); /* set the default highlight groups */
1363 TIME_MSG("init highlight");
1364#ifdef CURSOR_SHAPE
1365 parse_shape_opt(SHAPE_CURSOR); /* set cursor shapes from 'guicursor' */
1366#endif
1367#ifdef FEAT_MOUSESHAPE
1368 parse_shape_opt(SHAPE_MOUSE); /* set mouse shapes from 'mouseshape' */
1369#endif
1370#ifdef FEAT_PRINTER
1371 parse_list_options(p_popt, printer_opts, OPT_PRINT_NUM_OPTIONS);
1372#endif
1373
1374#ifdef FEAT_EVAL
1375 /* Set the break level after the terminal is initialized. */
1376 debug_break_level = use_debug_break_level;
1377#endif
1378
1379#ifdef FEAT_PRECOMMANDS
1380 if (p_commands > 0)
1381 {
1382 curwin->w_cursor.lnum = 0; /* just in case.. */
1383 sourcing_name = (char_u *)_("pre-vimrc command line");
1384# ifdef FEAT_EVAL
1385 current_SID = SID_CMDARG;
1386# endif
1387 for (i = 0; i < p_commands; ++i)
1388 do_cmdline_cmd(pre_commands[i]);
1389 sourcing_name = NULL;
1390# ifdef FEAT_EVAL
1391 current_SID = 0;
1392# endif
1393 }
1394#endif
1395
1396 /*
1397 * For "evim" source evim.vim first of all, so that the user can overrule
1398 * any things he doesn't like.
1399 */
1400 if (evim_mode)
1401 {
1402 (void)do_source((char_u *)EVIM_FILE, FALSE, FALSE);
1403 TIME_MSG("source evim file");
1404 }
1405
1406 /*
1407 * If -u option given, use only the initializations from that file and
1408 * nothing else.
1409 */
1410 if (use_vimrc != NULL)
1411 {
1412 if (STRCMP(use_vimrc, "NONE") == 0 || STRCMP(use_vimrc, "NORC") == 0)
1413 {
1414#ifdef FEAT_GUI
1415 if (use_gvimrc == NULL) /* don't load gvimrc either */
1416 use_gvimrc = use_vimrc;
1417#endif
1418 if (use_vimrc[2] == 'N')
1419 p_lpl = FALSE; /* don't load plugins either */
1420 }
1421 else
1422 {
1423 if (do_source(use_vimrc, FALSE, FALSE) != OK)
1424 EMSG2(_("E282: Cannot read from \"%s\""), use_vimrc);
1425 }
1426 }
1427 else if (!silent_mode)
1428 {
1429#ifdef AMIGA
1430 struct Process *proc = (struct Process *)FindTask(0L);
1431 APTR save_winptr = proc->pr_WindowPtr;
1432
1433 /* Avoid a requester here for a volume that doesn't exist. */
1434 proc->pr_WindowPtr = (APTR)-1L;
1435#endif
1436
1437 /*
1438 * Get system wide defaults, if the file name is defined.
1439 */
1440#ifdef SYS_VIMRC_FILE
1441 (void)do_source((char_u *)SYS_VIMRC_FILE, FALSE, FALSE);
1442#endif
1443
1444 /*
1445 * Try to read initialization commands from the following places:
1446 * - environment variable VIMINIT
1447 * - user vimrc file (s:.vimrc for Amiga, ~/.vimrc otherwise)
1448 * - second user vimrc file ($VIM/.vimrc for Dos)
1449 * - environment variable EXINIT
1450 * - user exrc file (s:.exrc for Amiga, ~/.exrc otherwise)
1451 * - second user exrc file ($VIM/.exrc for Dos)
1452 * The first that exists is used, the rest is ignored.
1453 */
1454 if (process_env((char_u *)"VIMINIT", TRUE) != OK)
1455 {
1456 if (do_source((char_u *)USR_VIMRC_FILE, TRUE, TRUE) == FAIL
1457#ifdef USR_VIMRC_FILE2
1458 && do_source((char_u *)USR_VIMRC_FILE2, TRUE, TRUE) == FAIL
1459#endif
1460#ifdef USR_VIMRC_FILE3
1461 && do_source((char_u *)USR_VIMRC_FILE3, TRUE, TRUE) == FAIL
1462#endif
1463 && process_env((char_u *)"EXINIT", FALSE) == FAIL
1464 && do_source((char_u *)USR_EXRC_FILE, FALSE, FALSE) == FAIL)
1465 {
1466#ifdef USR_EXRC_FILE2
1467 (void)do_source((char_u *)USR_EXRC_FILE2, FALSE, FALSE);
1468#endif
1469 }
1470 }
1471
1472 /*
1473 * Read initialization commands from ".vimrc" or ".exrc" in current
1474 * directory. This is only done if the 'exrc' option is set.
1475 * Because of security reasons we disallow shell and write commands
1476 * now, except for unix if the file is owned by the user or 'secure'
1477 * option has been reset in environmet of global ".exrc" or ".vimrc".
1478 * Only do this if VIMRC_FILE is not the same as USR_VIMRC_FILE or
1479 * SYS_VIMRC_FILE.
1480 */
1481 if (p_exrc)
1482 {
1483#if defined(UNIX) || defined(VMS)
1484 /* If ".vimrc" file is not owned by user, set 'secure' mode. */
1485 if (!file_owned(VIMRC_FILE))
1486#endif
1487 secure = p_secure;
1488
1489 i = FAIL;
1490 if (fullpathcmp((char_u *)USR_VIMRC_FILE,
1491 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
1492#ifdef USR_VIMRC_FILE2
1493 && fullpathcmp((char_u *)USR_VIMRC_FILE2,
1494 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
1495#endif
1496#ifdef USR_VIMRC_FILE3
1497 && fullpathcmp((char_u *)USR_VIMRC_FILE3,
1498 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
1499#endif
1500#ifdef SYS_VIMRC_FILE
1501 && fullpathcmp((char_u *)SYS_VIMRC_FILE,
1502 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
1503#endif
1504 )
1505 i = do_source((char_u *)VIMRC_FILE, TRUE, TRUE);
1506
1507 if (i == FAIL)
1508 {
1509#if defined(UNIX) || defined(VMS)
1510 /* if ".exrc" is not owned by user set 'secure' mode */
1511 if (!file_owned(EXRC_FILE))
1512 secure = p_secure;
1513 else
1514 secure = 0;
1515#endif
1516 if ( fullpathcmp((char_u *)USR_EXRC_FILE,
1517 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
1518#ifdef USR_EXRC_FILE2
1519 && fullpathcmp((char_u *)USR_EXRC_FILE2,
1520 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
1521#endif
1522 )
1523 (void)do_source((char_u *)EXRC_FILE, FALSE, FALSE);
1524 }
1525 }
1526 if (secure == 2)
1527 need_wait_return = TRUE;
1528 secure = 0;
1529#ifdef AMIGA
1530 proc->pr_WindowPtr = save_winptr;
1531#endif
1532 }
1533 TIME_MSG("sourcing vimrc file(s)");
1534
1535#ifdef FEAT_EVAL
1536 /*
1537 * Read all the plugin files.
1538 * Only when compiled with +eval, since most plugins need it.
1539 */
1540 if (p_lpl)
1541 {
1542 cmd_runtime((char_u *)"plugin/*.vim", TRUE);
1543 TIME_MSG("loading plugins");
1544 }
1545#endif
1546
1547 /*
1548 * Recovery mode without a file name: List swap files.
1549 * This uses the 'dir' option, therefore it must be after the
1550 * initializations.
1551 */
1552 if (recoverymode && fname == NULL)
1553 {
1554 recover_names(NULL, TRUE, 0);
1555 mch_exit(0);
1556 }
1557
1558 /*
1559 * Set a few option defaults after reading .vimrc files:
1560 * 'title' and 'icon', Unix: 'shellpipe' and 'shellredir'.
1561 */
1562 set_init_3();
1563 TIME_MSG("inits 3");
1564
1565 /*
1566 * "-n" argument: Disable swap file by setting 'updatecount' to 0.
1567 * Note that this overrides anything from a vimrc file.
1568 */
1569 if (no_swap_file)
1570 p_uc = 0;
1571
1572#ifdef FEAT_FKMAP
1573 if (curwin->w_p_rl && p_altkeymap)
1574 {
1575 p_hkmap = FALSE; /* Reset the Hebrew keymap mode */
1576# ifdef FEAT_ARABIC
1577 curwin->w_p_arab = FALSE; /* Reset the Arabic keymap mode */
1578# endif
1579 p_fkmap = TRUE; /* Set the Farsi keymap mode */
1580 }
1581#endif
1582
1583#ifdef FEAT_GUI
1584 if (gui.starting)
1585 {
1586#if defined(UNIX) || defined(VMS)
1587 /* When something caused a message from a vimrc script, need to output
1588 * an extra newline before the shell prompt. */
1589 if (did_emsg || msg_didout)
1590 putchar('\n');
1591#endif
1592
1593 gui_start(); /* will set full_screen to TRUE */
1594 TIME_MSG("starting GUI");
1595
1596 /* When running "evim" or "gvim -y" we need the menus, exit if we
1597 * don't have them. */
1598 if (!gui.in_use && evim_mode)
1599 mch_exit(1);
1600 }
1601#endif
1602
1603#ifdef SPAWNO /* special MSDOS swapping library */
1604 init_SPAWNO("", SWAP_ANY);
1605#endif
1606
1607#ifdef FEAT_VIMINFO
1608 /*
1609 * Read in registers, history etc, but not marks, from the viminfo file
1610 */
1611 if (*p_viminfo != NUL)
1612 {
1613 read_viminfo(NULL, TRUE, FALSE, FALSE);
1614 TIME_MSG("reading viminfo");
1615 }
1616#endif
1617
1618#ifdef FEAT_QUICKFIX
1619 /*
1620 * "-q errorfile": Load the error file now.
1621 * If the error file can't be read, exit before doing anything else.
1622 */
1623 if (edit_type == EDIT_QF)
1624 {
1625 if (use_ef != NULL)
1626 set_string_option_direct((char_u *)"ef", -1, use_ef, OPT_FREE);
1627 if (qf_init(p_ef, p_efm, TRUE) < 0)
1628 {
1629 out_char('\n');
1630 mch_exit(3);
1631 }
1632 TIME_MSG("reading errorfile");
1633 }
1634#endif
1635
1636 /*
1637 * Start putting things on the screen.
1638 * Scroll screen down before drawing over it
1639 * Clear screen now, so file message will not be cleared.
1640 */
1641 starting = NO_BUFFERS;
1642 no_wait_return = FALSE;
1643 if (!exmode_active)
1644 msg_scroll = FALSE;
1645
1646#ifdef FEAT_GUI
1647 /*
1648 * This seems to be required to make callbacks to be called now, instead
1649 * of after things have been put on the screen, which then may be deleted
1650 * when getting a resize callback.
1651 * For the Mac this handles putting files dropped on the Vim icon to
1652 * global_alist.
1653 */
1654 if (gui.in_use)
1655 {
1656# ifdef FEAT_SUN_WORKSHOP
1657 if (!usingSunWorkShop)
1658# endif
1659 gui_wait_for_chars(50L);
1660 TIME_MSG("GUI delay");
1661 }
1662#endif
1663
1664#if defined(FEAT_GUI_PHOTON) && defined(FEAT_CLIPBOARD)
1665 qnx_clip_init();
1666#endif
1667
1668#ifdef FEAT_XCLIPBOARD
1669 /* Start using the X clipboard, unless the GUI was started. */
1670# ifdef FEAT_GUI
1671 if (!gui.in_use)
1672# endif
1673 {
1674 setup_term_clip();
1675 TIME_MSG("setup clipboard");
1676 }
1677#endif
1678
1679#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
1680 /*
1681 * Register for remote command execution with :serversend and --remote
1682 * unless there was a -X or a --servername '' on the command line.
1683 * Only register nongui-vim's with an explicit --servername argument.
1684 */
1685 if (X_DISPLAY != NULL && servername != NULL && (
1686# ifdef FEAT_GUI
1687 gui.in_use ||
1688# endif
1689 serverName_arg != NULL))
1690 {
1691 (void)serverRegisterName(X_DISPLAY, servername);
1692 vim_free(servername);
1693 TIME_MSG("register server name");
1694 }
1695 else
1696 serverDelayedStartName = servername;
1697#endif
1698
1699#ifdef FEAT_CLIENTSERVER
1700 /*
1701 * Execute command ourselves if we're here because the send failed (or
1702 * else we would have exited above).
1703 */
1704 if (serverStr != NULL)
Bram Moolenaar8fc061c2004-12-29 21:03:02 +00001705 {
1706 server_to_input_buf(serverConvert(serverStrEnc, serverStr, &p));
1707 vim_free(p);
1708 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001709#endif
1710
1711 /*
1712 * If "-" argument given: Read file from stdin.
1713 * Do this before starting Raw mode, because it may change things that the
1714 * writing end of the pipe doesn't like, e.g., in case stdin and stderr
1715 * are the same terminal: "cat | vim -".
1716 * Using autocommands here may cause trouble...
1717 */
1718 if (edit_type == EDIT_STDIN && !recoverymode)
1719 {
1720#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1721 /* When getting the ATTENTION prompt here, use a dialog */
1722 swap_exists_action = SEA_DIALOG;
1723#endif
1724 no_wait_return = TRUE;
1725 i = msg_didany;
1726 set_buflisted(TRUE);
1727 (void)open_buffer(TRUE, NULL); /* create memfile and read file */
1728 no_wait_return = FALSE;
1729 msg_didany = i;
1730 TIME_MSG("reading stdin");
1731#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1732 check_swap_exists_action();
1733#endif
1734#if !(defined(AMIGA) || defined(MACOS))
1735 /*
1736 * Close stdin and dup it from stderr. Required for GPM to work
1737 * properly, and for running external commands.
1738 * Is there any other system that cannot do this?
1739 */
1740 close(0);
1741 dup(2);
1742#endif
1743 }
1744
1745#if defined(UNIX) || defined(VMS)
1746 /* When switching screens and something caused a message from a vimrc
1747 * script, need to output an extra newline on exit. */
1748 if ((did_emsg || msg_didout) && *T_TI != NUL)
1749 newline_on_exit = TRUE;
1750#endif
1751
1752 /*
1753 * When done something that is not allowed or error message call
1754 * wait_return. This must be done before starttermcap(), because it may
1755 * switch to another screen. It must be done after settmode(TMODE_RAW),
1756 * because we want to react on a single key stroke.
1757 * Call settmode and starttermcap here, so the T_KS and T_TI may be
1758 * defined by termcapinit and redifined in .exrc.
1759 */
1760 settmode(TMODE_RAW);
1761 TIME_MSG("setting raw mode");
1762
1763 if (need_wait_return || msg_didany)
1764 {
1765 wait_return(TRUE);
1766 TIME_MSG("waiting for return");
1767 }
1768
1769 starttermcap(); /* start termcap if not done by wait_return() */
1770 TIME_MSG("start termcap");
1771
1772#ifdef FEAT_MOUSE
1773 setmouse(); /* may start using the mouse */
1774#endif
1775 if (scroll_region)
1776 scroll_region_reset(); /* In case Rows changed */
1777
1778 scroll_start();
1779
1780 /*
1781 * Don't clear the screen when starting in Ex mode, unless using the GUI.
1782 */
1783 if (exmode_active
1784#ifdef FEAT_GUI
1785 && !gui.in_use
1786#endif
1787 )
1788 must_redraw = CLEAR;
1789 else
1790 {
1791 screenclear(); /* clear screen */
1792 TIME_MSG("clearing screen");
1793 }
1794
1795#ifdef FEAT_CRYPT
1796 if (ask_for_key)
1797 {
1798 (void)get_crypt_key(TRUE, TRUE);
1799 TIME_MSG("getting crypt key");
1800 }
1801#endif
1802
1803 no_wait_return = TRUE;
1804
1805#ifdef FEAT_WINDOWS
1806 /*
1807 * Create the number of windows that was requested.
1808 */
1809 if (window_count == -1) /* was not set */
1810 window_count = 1;
1811 if (window_count == 0)
1812 window_count = GARGCOUNT;
1813 if (window_count > 1)
1814 {
1815 /* Don't change the windows if there was a command in .vimrc that
1816 * already split some windows */
1817 if (vert_windows == MAYBE)
1818 vert_windows = FALSE;
1819 if (firstwin->w_next == NULL)
1820 {
1821 window_count = make_windows(window_count, vert_windows);
1822 TIME_MSG("making windows");
1823 }
1824 else
1825 window_count = win_count();
1826 }
1827 else
1828 window_count = 1;
1829#endif
1830
1831 if (recoverymode) /* do recover */
1832 {
1833 msg_scroll = TRUE; /* scroll message up */
1834 ml_recover();
1835 if (curbuf->b_ml.ml_mfp == NULL) /* failed */
1836 getout(1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00001837 do_modelines(FALSE); /* do modelines */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001838 }
1839 else
1840 {
1841 /*
1842 * Open a buffer for windows that don't have one yet.
1843 * Commands in the .vimrc might have loaded a file or split the window.
1844 * Watch out for autocommands that delete a window.
1845 */
1846#ifdef FEAT_AUTOCMD
1847 /*
1848 * Don't execute Win/Buf Enter/Leave autocommands here
1849 */
1850 ++autocmd_no_enter;
1851 ++autocmd_no_leave;
1852#endif
1853#ifdef FEAT_WINDOWS
1854 for (curwin = firstwin; curwin != NULL; curwin = W_NEXT(curwin))
1855#endif
1856 {
1857 curbuf = curwin->w_buffer;
1858 if (curbuf->b_ml.ml_mfp == NULL)
1859 {
1860#ifdef FEAT_FOLDING
1861 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
1862 if (p_fdls >= 0)
1863 curwin->w_p_fdl = p_fdls;
1864#endif
1865#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1866 /* When getting the ATTENTION prompt here, use a dialog */
1867 swap_exists_action = SEA_DIALOG;
1868#endif
1869 set_buflisted(TRUE);
1870 (void)open_buffer(FALSE, NULL); /* create memfile, read file */
1871
1872#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1873 check_swap_exists_action();
1874#endif
1875#ifdef FEAT_AUTOCMD
1876 curwin = firstwin; /* start again */
1877#endif
1878 }
1879#ifdef FEAT_WINDOWS
1880 ui_breakcheck();
1881 if (got_int)
1882 {
1883 (void)vgetc(); /* only break the file loading, not the rest */
1884 break;
1885 }
1886#endif
1887 }
1888#ifdef FEAT_AUTOCMD
1889 --autocmd_no_enter;
1890 --autocmd_no_leave;
1891#endif
1892#ifdef FEAT_WINDOWS
1893 curwin = firstwin;
1894 curbuf = curwin->w_buffer;
1895#endif
1896 }
1897 TIME_MSG("opening buffers");
1898
1899 /* Ex starts at last line of the file */
1900 if (exmode_active)
1901 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1902
1903#ifdef FEAT_AUTOCMD
1904 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1905 TIME_MSG("BufEnter autocommands");
1906#endif
1907 setpcmark();
1908
1909#ifdef FEAT_QUICKFIX
1910 /*
1911 * When started with "-q errorfile" jump to first error now.
1912 */
1913 if (edit_type == EDIT_QF)
1914 {
1915 qf_jump(0, 0, FALSE);
1916 TIME_MSG("jump to first error");
1917 }
1918#endif
1919
1920#ifdef FEAT_WINDOWS
1921 /*
1922 * If opened more than one window, start editing files in the other
1923 * windows. Make_windows() has already opened the windows.
1924 */
1925# ifdef FEAT_AUTOCMD
1926 /*
1927 * Don't execute Win/Buf Enter/Leave autocommands here
1928 */
1929 ++autocmd_no_enter;
1930 ++autocmd_no_leave;
1931# endif
1932 arg_idx = 1;
1933 for (i = 1; i < window_count; ++i)
1934 {
1935 if (curwin->w_next == NULL) /* just checking */
1936 break;
1937 win_enter(curwin->w_next, FALSE);
1938
1939 /* Only open the file if there is no file in this window yet (that can
1940 * happen when .vimrc contains ":sall") */
1941 if (curbuf == firstwin->w_buffer || curbuf->b_ffname == NULL)
1942 {
1943 curwin->w_arg_idx = arg_idx;
1944 /* edit file from arg list, if there is one */
1945 (void)do_ecmd(0, arg_idx < GARGCOUNT
1946 ? alist_name(&GARGLIST[arg_idx]) : NULL,
1947 NULL, NULL, ECMD_LASTL, ECMD_HIDE);
1948 if (arg_idx == GARGCOUNT - 1)
1949 arg_had_last = TRUE;
1950 ++arg_idx;
1951 }
1952 ui_breakcheck();
1953 if (got_int)
1954 {
1955 (void)vgetc(); /* only break the file loading, not the rest */
1956 break;
1957 }
1958 }
1959# ifdef FEAT_AUTOCMD
1960 --autocmd_no_enter;
1961# endif
1962 win_enter(firstwin, FALSE); /* back to first window */
1963# ifdef FEAT_AUTOCMD
1964 --autocmd_no_leave;
1965# endif
1966 TIME_MSG("editing files in windows");
1967 if (window_count > 1)
1968 win_equal(curwin, FALSE, 'b'); /* adjust heights */
1969#endif /* FEAT_WINDOWS */
1970
1971#ifdef FEAT_DIFF
1972 if (diff_mode)
1973 {
1974 win_T *wp;
1975
1976 /* set options in each window for "vimdiff". */
1977 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1978 diff_win_options(wp, TRUE);
1979 }
1980#endif
1981
1982 /*
1983 * Shorten any of the filenames, but only when absolute.
1984 */
1985 shorten_fnames(FALSE);
1986
1987 /*
1988 * Need to jump to the tag before executing the '-c command'.
1989 * Makes "vim -c '/return' -t main" work.
1990 */
1991 if (tagname != NULL)
1992 {
1993 STRCPY(IObuff, "ta ");
1994
1995 STRNCAT(IObuff, tagname, IOSIZE - 4);
1996 IObuff[IOSIZE - 1] = NUL;
1997 do_cmdline_cmd(IObuff);
1998 TIME_MSG("jumping to tag");
1999 }
2000
2001 if (n_commands > 0)
2002 {
2003 /*
2004 * We start commands on line 0, make "vim +/pat file" match a
Bram Moolenaarc0761132005-03-18 20:30:32 +00002005 * pattern on line 1. But don't move the cursor when an autocommand
2006 * with g`" was used.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002007 */
2008 msg_scroll = TRUE;
Bram Moolenaarc0761132005-03-18 20:30:32 +00002009 if (tagname == NULL && curwin->w_cursor.lnum <= 1)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002010 curwin->w_cursor.lnum = 0;
2011 sourcing_name = (char_u *)"command line";
2012#ifdef FEAT_EVAL
2013 current_SID = SID_CARG;
2014#endif
2015 for (i = 0; i < n_commands; ++i)
2016 do_cmdline_cmd(commands[i]);
2017 sourcing_name = NULL;
2018#ifdef FEAT_EVAL
2019 current_SID = 0;
2020#endif
2021 if (curwin->w_cursor.lnum == 0)
2022 curwin->w_cursor.lnum = 1;
2023
2024 if (!exmode_active)
2025 msg_scroll = FALSE;
2026
2027#ifdef FEAT_QUICKFIX
2028 /* When started with "-q errorfile" jump to first error again. */
2029 if (edit_type == EDIT_QF)
2030 qf_jump(0, 0, FALSE);
2031#endif
2032 TIME_MSG("executing command arguments");
2033 }
2034
2035 RedrawingDisabled = 0;
2036 redraw_all_later(NOT_VALID);
2037 no_wait_return = FALSE;
2038 starting = 0;
2039
2040 /* start in insert mode */
2041 if (p_im)
2042 need_start_insertmode = TRUE;
2043
2044#ifdef FEAT_AUTOCMD
2045 apply_autocmds(EVENT_VIMENTER, NULL, NULL, FALSE, curbuf);
2046 TIME_MSG("VimEnter autocommands");
2047#endif
2048
2049#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
2050 /* When a startup script or session file setup for diff'ing and
2051 * scrollbind, sync the scrollbind now. */
2052 if (curwin->w_p_diff && curwin->w_p_scb)
2053 {
2054 update_topline();
2055 check_scrollbind((linenr_T)0, 0L);
2056 TIME_MSG("diff scrollbinding");
2057 }
2058#endif
2059
2060#if defined(WIN3264) && !defined(FEAT_GUI_W32)
2061 mch_set_winsize_now(); /* Allow winsize changes from now on */
2062#endif
2063
2064 /* If ":startinsert" command used, stuff a dummy command to be able to
2065 * call normal_cmd(), which will then start Insert mode. */
2066 if (restart_edit != 0)
2067 stuffcharReadbuff(K_IGNORE);
2068
2069#ifdef FEAT_NETBEANS_INTG
2070 if (usingNetbeans)
2071 /* Tell the client that it can start sending commands. */
2072 netbeans_startup_done();
2073#endif
2074
2075 TIME_MSG("before starting main loop");
2076
2077 /*
2078 * Call the main command loop. This never returns.
2079 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00002080 main_loop(FALSE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002081
2082 return 0;
2083}
2084#endif /* PROTO */
2085
2086/*
2087 * Main loop: Execute Normal mode commands until exiting Vim.
2088 * Also used to handle commands in the command-line window, until the window
2089 * is closed.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00002090 * Also used to handle ":visual" command after ":global": execute Normal mode
2091 * commands, return when entering Ex mode. "noexmode" is TRUE then.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002092 */
2093 void
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00002094main_loop(cmdwin, noexmode)
2095 int cmdwin; /* TRUE when working in the command-line window */
2096 int noexmode; /* TRUE when return on entering Ex mode */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002097{
2098 oparg_T oa; /* operator arguments */
2099
2100#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
2101 /* Setup to catch a terminating error from the X server. Just ignore
2102 * it, restore the state and continue. This might not always work
2103 * properly, but at least we don't exit unexpectedly when the X server
2104 * exists while Vim is running in a console. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00002105 if (!cmdwin && !noexmode && SETJMP(x_jump_env))
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002106 {
2107 State = NORMAL;
2108# ifdef FEAT_VISUAL
2109 VIsual_active = FALSE;
2110# endif
2111 got_int = TRUE;
2112 need_wait_return = FALSE;
2113 global_busy = FALSE;
2114 exmode_active = 0;
2115 skip_redraw = FALSE;
2116 RedrawingDisabled = 0;
2117 no_wait_return = 0;
2118# ifdef FEAT_EVAL
2119 emsg_skip = 0;
2120# endif
2121 emsg_off = 0;
2122# ifdef FEAT_MOUSE
2123 setmouse();
2124# endif
2125 settmode(TMODE_RAW);
2126 starttermcap();
2127 scroll_start();
2128 redraw_later_clear();
2129 }
2130#endif
2131
2132 clear_oparg(&oa);
2133 while (!cmdwin
2134#ifdef FEAT_CMDWIN
2135 || cmdwin_result == 0
2136#endif
2137 )
2138 {
2139 if (stuff_empty())
2140 {
2141 did_check_timestamps = FALSE;
2142 if (need_check_timestamps)
2143 check_timestamps(FALSE);
2144 if (need_wait_return) /* if wait_return still needed ... */
2145 wait_return(FALSE); /* ... call it now */
2146 if (need_start_insertmode && goto_im()
2147#ifdef FEAT_VISUAL
2148 && !VIsual_active
2149#endif
2150 )
2151 {
2152 need_start_insertmode = FALSE;
2153 stuffReadbuff((char_u *)"i"); /* start insert mode next */
2154 /* skip the fileinfo message now, because it would be shown
2155 * after insert mode finishes! */
2156 need_fileinfo = FALSE;
2157 }
2158 }
2159 if (got_int && !global_busy)
2160 {
2161 if (!quit_more)
2162 (void)vgetc(); /* flush all buffers */
2163 got_int = FALSE;
2164 }
2165 if (!exmode_active)
2166 msg_scroll = FALSE;
2167 quit_more = FALSE;
2168
2169 /*
2170 * If skip redraw is set (for ":" in wait_return()), don't redraw now.
2171 * If there is nothing in the stuff_buffer or do_redraw is TRUE,
2172 * update cursor and redraw.
2173 */
2174 if (skip_redraw || exmode_active)
2175 skip_redraw = FALSE;
2176 else if (do_redraw || stuff_empty())
2177 {
2178#if defined(FEAT_FOLDING) && defined(FEAT_VISUAL)
2179 /* Include a closed fold completely in the Visual area. */
2180 foldAdjustVisual();
2181#endif
2182#ifdef FEAT_FOLDING
2183 /*
2184 * When 'foldclose' is set, apply 'foldlevel' to folds that don't
2185 * contain the cursor.
2186 * When 'foldopen' is "all", open the fold(s) under the cursor.
2187 * This may mark the window for redrawing.
2188 */
2189 if (hasAnyFolding(curwin) && !char_avail())
2190 {
2191 foldCheckClose();
2192 if (fdo_flags & FDO_ALL)
2193 foldOpenCursor();
2194 }
2195#endif
2196
2197 /*
2198 * Before redrawing, make sure w_topline is correct, and w_leftcol
2199 * if lines don't wrap, and w_skipcol if lines wrap.
2200 */
2201 update_topline();
2202 validate_cursor();
2203
2204#ifdef FEAT_VISUAL
2205 if (VIsual_active)
2206 update_curbuf(INVERTED);/* update inverted part */
2207 else
2208#endif
2209 if (must_redraw)
2210 update_screen(0);
2211 else if (redraw_cmdline || clear_cmdline)
2212 showmode();
2213#ifdef FEAT_WINDOWS
2214 redraw_statuslines();
2215#endif
2216#ifdef FEAT_TITLE
2217 if (need_maketitle)
2218 maketitle();
2219#endif
2220 /* display message after redraw */
2221 if (keep_msg != NULL)
2222 {
2223 char_u *p;
2224
2225 /* msg_attr_keep() will set keep_msg to NULL, must free the
2226 * string here. */
2227 p = keep_msg;
2228 msg_attr(p, keep_msg_attr);
2229 vim_free(p);
2230 }
2231 if (need_fileinfo) /* show file info after redraw */
2232 {
2233 fileinfo(FALSE, TRUE, FALSE);
2234 need_fileinfo = FALSE;
2235 }
2236
2237 emsg_on_display = FALSE; /* can delete error message now */
2238 did_emsg = FALSE;
2239 msg_didany = FALSE; /* reset lines_left in msg_start() */
2240 showruler(FALSE);
2241
2242 setcursor();
2243 cursor_on();
2244
2245 do_redraw = FALSE;
2246 }
2247#ifdef FEAT_GUI
2248 if (need_mouse_correct)
2249 gui_mouse_correct();
2250#endif
2251
2252 /*
2253 * Update w_curswant if w_set_curswant has been set.
2254 * Postponed until here to avoid computing w_virtcol too often.
2255 */
2256 update_curswant();
2257
2258 /*
2259 * If we're invoked as ex, do a round of ex commands.
2260 * Otherwise, get and execute a normal mode command.
2261 */
2262 if (exmode_active)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00002263 {
2264 if (noexmode) /* End of ":global/path/visual" commands */
2265 return;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002266 do_exmode(exmode_active == EXMODE_VIM);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00002267 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002268 else
2269 normal_cmd(&oa, TRUE);
2270 }
2271}
2272
2273
2274#if defined(USE_XSMP) || defined(FEAT_GUI_MSWIN) || defined(PROTO)
2275/*
2276 * Exit, but leave behind swap files for modified buffers.
2277 */
2278 void
2279getout_preserve_modified(exitval)
2280 int exitval;
2281{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002282# if defined(SIGHUP) && defined(SIG_IGN)
2283 /* Ignore SIGHUP, because a dropped connection causes a read error, which
2284 * makes Vim exit and then handling SIGHUP causes various reentrance
2285 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002286 signal(SIGHUP, SIG_IGN);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002287# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002288
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002289 ml_close_notmod(); /* close all not-modified buffers */
2290 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
2291 ml_close_all(FALSE); /* close all memfiles, without deleting */
2292 getout(exitval); /* exit Vim properly */
2293}
2294#endif
2295
2296
2297/* Exit properly */
2298 void
2299getout(exitval)
2300 int exitval;
2301{
2302#ifdef FEAT_AUTOCMD
2303 buf_T *buf;
2304 win_T *wp;
2305#endif
2306
2307 exiting = TRUE;
2308
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00002309 /* When running in Ex mode an error causes us to exit with a non-zero exit
2310 * code. POSIX requires this, although it's not 100% clear from the
2311 * standard. */
2312 if (exmode_active)
2313 exitval += ex_exitval;
2314
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002315 /* Position the cursor on the last screen line, below all the text */
2316#ifdef FEAT_GUI
2317 if (!gui.in_use)
2318#endif
2319 windgoto((int)Rows - 1, 0);
2320
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +00002321#if defined(FEAT_EVAL) || defined(FEAT_SYN_HL)
2322 /* Optionally print hashtable efficiency. */
2323 hash_debug_results();
2324#endif
2325
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002326#ifdef FEAT_GUI
2327 msg_didany = FALSE;
2328#endif
2329
2330#ifdef FEAT_AUTOCMD
2331 /* Trigger BufWinLeave for all windows, but only once per buffer. */
2332 for (wp = firstwin; wp != NULL; )
2333 {
2334 buf = wp->w_buffer;
2335 if (buf->b_changedtick != -1)
2336 {
2337 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
2338 FALSE, buf);
2339 buf->b_changedtick = -1; /* note that we did it already */
2340 wp = firstwin; /* restart, window may be closed */
2341 }
2342 else
2343 wp = wp->w_next;
2344 }
2345 /* Trigger BufUnload for buffers that are loaded */
2346 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2347 if (buf->b_ml.ml_mfp != NULL)
2348 {
2349 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
2350 FALSE, buf);
2351 if (!buf_valid(buf)) /* autocmd may delete the buffer */
2352 break;
2353 }
2354 apply_autocmds(EVENT_VIMLEAVEPRE, NULL, NULL, FALSE, curbuf);
2355#endif
2356
2357#ifdef FEAT_VIMINFO
2358 if (*p_viminfo != NUL)
2359 /* Write out the registers, history, marks etc, to the viminfo file */
2360 write_viminfo(NULL, FALSE);
2361#endif
2362
2363#ifdef FEAT_AUTOCMD
2364 apply_autocmds(EVENT_VIMLEAVE, NULL, NULL, FALSE, curbuf);
2365#endif
2366
Bram Moolenaar05159a02005-02-26 23:04:13 +00002367#ifdef FEAT_PROFILE
2368 profile_dump();
2369#endif
2370
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002371 if (did_emsg
2372#ifdef FEAT_GUI
2373 || (gui.in_use && msg_didany && p_verbose > 0)
2374#endif
2375 )
2376 {
2377 /* give the user a chance to read the (error) message */
2378 no_wait_return = FALSE;
2379 wait_return(FALSE);
2380 }
2381
2382#ifdef FEAT_AUTOCMD
2383 /* Position the cursor again, the autocommands may have moved it */
2384# ifdef FEAT_GUI
2385 if (!gui.in_use)
2386# endif
2387 windgoto((int)Rows - 1, 0);
2388#endif
2389
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002390#ifdef FEAT_MZSCHEME
2391 mzscheme_end();
2392#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002393#ifdef FEAT_TCL
2394 tcl_end();
2395#endif
2396#ifdef FEAT_RUBY
2397 ruby_end();
2398#endif
2399#ifdef FEAT_PYTHON
2400 python_end();
2401#endif
2402#ifdef FEAT_PERL
2403 perl_end();
2404#endif
2405#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
2406 iconv_end();
2407#endif
2408#ifdef FEAT_NETBEANS_INTG
2409 netbeans_end();
2410#endif
2411
2412 mch_exit(exitval);
2413}
2414
2415/*
2416 * Get a (optional) count for a Vim argument.
2417 */
2418 static int
2419get_number_arg(p, idx, def)
2420 char_u *p; /* pointer to argument */
2421 int *idx; /* index in argument, is incremented */
2422 int def; /* default value */
2423{
2424 if (vim_isdigit(p[*idx]))
2425 {
2426 def = atoi((char *)&(p[*idx]));
2427 while (vim_isdigit(p[*idx]))
2428 *idx = *idx + 1;
2429 }
2430 return def;
2431}
2432
2433/*
2434 * Setup to start using the GUI. Exit with an error when not available.
2435 */
2436 static void
2437main_start_gui()
2438{
2439#ifdef FEAT_GUI
2440 gui.starting = TRUE; /* start GUI a bit later */
2441#else
2442 mch_errmsg(_(e_nogvim));
2443 mch_errmsg("\n");
2444 mch_exit(2);
2445#endif
2446}
2447
2448/*
2449 * Get an evironment variable, and execute it as Ex commands.
2450 * Returns FAIL if the environment variable was not executed, OK otherwise.
2451 */
2452 int
2453process_env(env, is_viminit)
2454 char_u *env;
2455 int is_viminit; /* when TRUE, called for VIMINIT */
2456{
2457 char_u *initstr;
2458 char_u *save_sourcing_name;
2459 linenr_T save_sourcing_lnum;
2460#ifdef FEAT_EVAL
2461 scid_T save_sid;
2462#endif
2463
2464 if ((initstr = mch_getenv(env)) != NULL && *initstr != NUL)
2465 {
2466 if (is_viminit)
2467 vimrc_found();
2468 save_sourcing_name = sourcing_name;
2469 save_sourcing_lnum = sourcing_lnum;
2470 sourcing_name = env;
2471 sourcing_lnum = 0;
2472#ifdef FEAT_EVAL
2473 save_sid = current_SID;
2474 current_SID = SID_ENV;
2475#endif
2476 do_cmdline_cmd(initstr);
2477 sourcing_name = save_sourcing_name;
2478 sourcing_lnum = save_sourcing_lnum;
2479#ifdef FEAT_EVAL
2480 current_SID = save_sid;;
2481#endif
2482 return OK;
2483 }
2484 return FAIL;
2485}
2486
2487#if defined(UNIX) || defined(VMS)
2488/*
2489 * Return TRUE if we are certain the user owns the file "fname".
2490 * Used for ".vimrc" and ".exrc".
2491 * Use both stat() and lstat() for extra security.
2492 */
2493 static int
2494file_owned(fname)
2495 char *fname;
2496{
2497 struct stat s;
2498# ifdef UNIX
2499 uid_t uid = getuid();
2500# else /* VMS */
2501 uid_t uid = ((getgid() << 16) | getuid());
2502# endif
2503
2504 return !(mch_stat(fname, &s) != 0 || s.st_uid != uid
2505# ifdef HAVE_LSTAT
2506 || mch_lstat(fname, &s) != 0 || s.st_uid != uid
2507# endif
2508 );
2509}
2510#endif
2511
2512/*
2513 * Give an error message main_errors["n"] and exit.
2514 */
2515 static void
2516mainerr(n, str)
2517 int n; /* one of the ME_ defines */
2518 char_u *str; /* extra argument or NULL */
2519{
2520#if defined(UNIX) || defined(__EMX__) || defined(VMS)
2521 reset_signals(); /* kill us with CTRL-C here, if you like */
2522#endif
2523
2524 mch_errmsg(longVersion);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002525 mch_errmsg("\n");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002526 mch_errmsg(_(main_errors[n]));
2527 if (str != NULL)
2528 {
2529 mch_errmsg(": \"");
2530 mch_errmsg((char *)str);
2531 mch_errmsg("\"");
2532 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002533 mch_errmsg(_("\nMore info with: \"vim -h\"\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002534
2535 mch_exit(1);
2536}
2537
2538 void
2539mainerr_arg_missing(str)
2540 char_u *str;
2541{
2542 mainerr(ME_ARG_MISSING, str);
2543}
2544
2545/*
2546 * print a message with three spaces prepended and '\n' appended.
2547 */
2548 static void
2549main_msg(s)
2550 char *s;
2551{
2552 mch_msg(" ");
2553 mch_msg(s);
2554 mch_msg("\n");
2555}
2556
2557/*
2558 * Print messages for "vim -h" or "vim --help" and exit.
2559 */
2560 static void
2561usage()
2562{
2563 int i;
2564 static char *(use[]) =
2565 {
2566 N_("[file ..] edit specified file(s)"),
2567 N_("- read text from stdin"),
2568 N_("-t tag edit file where tag is defined"),
2569#ifdef FEAT_QUICKFIX
2570 N_("-q [errorfile] edit file with first error")
2571#endif
2572 };
2573
2574#if defined(UNIX) || defined(__EMX__) || defined(VMS)
2575 reset_signals(); /* kill us with CTRL-C here, if you like */
2576#endif
2577
2578 mch_msg(longVersion);
2579 mch_msg(_("\n\nusage:"));
2580 for (i = 0; ; ++i)
2581 {
2582 mch_msg(_(" vim [arguments] "));
2583 mch_msg(_(use[i]));
2584 if (i == (sizeof(use) / sizeof(char_u *)) - 1)
2585 break;
2586 mch_msg(_("\n or:"));
2587 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002588#ifdef VMS
2589 mch_msg(_("where case is ignored prepend / to make flag upper case"));
2590#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002591
2592 mch_msg(_("\n\nArguments:\n"));
2593 main_msg(_("--\t\t\tOnly file names after this"));
2594#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
2595 main_msg(_("--literal\t\tDon't expand wildcards"));
2596#endif
2597#ifdef FEAT_OLE
2598 main_msg(_("-register\t\tRegister this gvim for OLE"));
2599 main_msg(_("-unregister\t\tUnregister gvim for OLE"));
2600#endif
2601#ifdef FEAT_GUI
2602 main_msg(_("-g\t\t\tRun using GUI (like \"gvim\")"));
2603 main_msg(_("-f or --nofork\tForeground: Don't fork when starting GUI"));
2604#endif
2605 main_msg(_("-v\t\t\tVi mode (like \"vi\")"));
2606 main_msg(_("-e\t\t\tEx mode (like \"ex\")"));
2607 main_msg(_("-s\t\t\tSilent (batch) mode (only for \"ex\")"));
2608#ifdef FEAT_DIFF
2609 main_msg(_("-d\t\t\tDiff mode (like \"vimdiff\")"));
2610#endif
2611 main_msg(_("-y\t\t\tEasy mode (like \"evim\", modeless)"));
2612 main_msg(_("-R\t\t\tReadonly mode (like \"view\")"));
2613 main_msg(_("-Z\t\t\tRestricted mode (like \"rvim\")"));
2614 main_msg(_("-m\t\t\tModifications (writing files) not allowed"));
2615 main_msg(_("-M\t\t\tModifications in text not allowed"));
2616 main_msg(_("-b\t\t\tBinary mode"));
2617#ifdef FEAT_LISP
2618 main_msg(_("-l\t\t\tLisp mode"));
2619#endif
2620 main_msg(_("-C\t\t\tCompatible with Vi: 'compatible'"));
2621 main_msg(_("-N\t\t\tNot fully Vi compatible: 'nocompatible'"));
2622 main_msg(_("-V[N]\t\tVerbose level"));
2623 main_msg(_("-D\t\t\tDebugging mode"));
2624 main_msg(_("-n\t\t\tNo swap file, use memory only"));
2625 main_msg(_("-r\t\t\tList swap files and exit"));
2626 main_msg(_("-r (with file name)\tRecover crashed session"));
2627 main_msg(_("-L\t\t\tSame as -r"));
2628#ifdef AMIGA
2629 main_msg(_("-f\t\t\tDon't use newcli to open window"));
2630 main_msg(_("-dev <device>\t\tUse <device> for I/O"));
2631#endif
2632#ifdef FEAT_ARABIC
2633 main_msg(_("-A\t\t\tstart in Arabic mode"));
2634#endif
2635#ifdef FEAT_RIGHTLEFT
2636 main_msg(_("-H\t\t\tStart in Hebrew mode"));
2637#endif
2638#ifdef FEAT_FKMAP
2639 main_msg(_("-F\t\t\tStart in Farsi mode"));
2640#endif
2641 main_msg(_("-T <terminal>\tSet terminal type to <terminal>"));
2642 main_msg(_("-u <vimrc>\t\tUse <vimrc> instead of any .vimrc"));
2643#ifdef FEAT_GUI
2644 main_msg(_("-U <gvimrc>\t\tUse <gvimrc> instead of any .gvimrc"));
2645#endif
2646 main_msg(_("--noplugin\t\tDon't load plugin scripts"));
2647 main_msg(_("-o[N]\t\tOpen N windows (default: one for each file)"));
2648 main_msg(_("-O[N]\t\tLike -o but split vertically"));
2649 main_msg(_("+\t\t\tStart at end of file"));
2650 main_msg(_("+<lnum>\t\tStart at line <lnum>"));
2651#ifdef FEAT_PRECOMMANDS
2652 main_msg(_("--cmd <command>\tExecute <command> before loading any vimrc file"));
2653#endif
2654 main_msg(_("-c <command>\t\tExecute <command> after loading the first file"));
2655 main_msg(_("-S <session>\t\tSource file <session> after loading the first file"));
2656 main_msg(_("-s <scriptin>\tRead Normal mode commands from file <scriptin>"));
2657 main_msg(_("-w <scriptout>\tAppend all typed commands to file <scriptout>"));
2658 main_msg(_("-W <scriptout>\tWrite all typed commands to file <scriptout>"));
2659#ifdef FEAT_CRYPT
2660 main_msg(_("-x\t\t\tEdit encrypted files"));
2661#endif
2662#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
2663# if defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)
2664 main_msg(_("-display <display>\tConnect vim to this particular X-server"));
2665# endif
2666 main_msg(_("-X\t\t\tDo not connect to X server"));
2667#endif
2668#ifdef FEAT_CLIENTSERVER
2669 main_msg(_("--remote <files>\tEdit <files> in a Vim server if possible"));
2670 main_msg(_("--remote-silent <files> Same, don't complain if there is no server"));
2671 main_msg(_("--remote-wait <files> As --remote but wait for files to have been edited"));
2672 main_msg(_("--remote-wait-silent <files> Same, don't complain if there is no server"));
2673 main_msg(_("--remote-send <keys>\tSend <keys> to a Vim server and exit"));
2674 main_msg(_("--remote-expr <expr>\tEvaluate <expr> in a Vim server and print result"));
2675 main_msg(_("--serverlist\t\tList available Vim server names and exit"));
2676 main_msg(_("--servername <name>\tSend to/become the Vim server <name>"));
2677#endif
2678#ifdef FEAT_VIMINFO
2679 main_msg(_("-i <viminfo>\t\tUse <viminfo> instead of .viminfo"));
2680#endif
2681 main_msg(_("-h or --help\tPrint Help (this message) and exit"));
2682 main_msg(_("--version\t\tPrint version information and exit"));
2683
2684#ifdef FEAT_GUI_X11
2685# ifdef FEAT_GUI_MOTIF
2686 mch_msg(_("\nArguments recognised by gvim (Motif version):\n"));
2687# else
2688# ifdef FEAT_GUI_ATHENA
2689# ifdef FEAT_GUI_NEXTAW
2690 mch_msg(_("\nArguments recognised by gvim (neXtaw version):\n"));
2691# else
2692 mch_msg(_("\nArguments recognised by gvim (Athena version):\n"));
2693# endif
2694# endif
2695# endif
2696 main_msg(_("-display <display>\tRun vim on <display>"));
2697 main_msg(_("-iconic\t\tStart vim iconified"));
2698# if 0
2699 main_msg(_("-name <name>\t\tUse resource as if vim was <name>"));
2700 mch_msg(_("\t\t\t (Unimplemented)\n"));
2701# endif
2702 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
2703 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
2704 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
2705 main_msg(_("-boldfont <font>\tUse <font> for bold text"));
2706 main_msg(_("-italicfont <font>\tUse <font> for italic text"));
2707 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
2708 main_msg(_("-borderwidth <width>\tUse a border width of <width> (also: -bw)"));
2709 main_msg(_("-scrollbarwidth <width> Use a scrollbar width of <width> (also: -sw)"));
2710# ifdef FEAT_GUI_ATHENA
2711 main_msg(_("-menuheight <height>\tUse a menu bar height of <height> (also: -mh)"));
2712# endif
2713 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
2714 main_msg(_("+reverse\t\tDon't use reverse video (also: +rv)"));
2715 main_msg(_("-xrm <resource>\tSet the specified resource"));
2716#endif /* FEAT_GUI_X11 */
2717#if defined(FEAT_GUI) && defined(RISCOS)
2718 mch_msg(_("\nArguments recognised by gvim (RISC OS version):\n"));
2719 main_msg(_("--columns <number>\tInitial width of window in columns"));
2720 main_msg(_("--rows <number>\tInitial height of window in rows"));
2721#endif
2722#ifdef FEAT_GUI_GTK
2723 mch_msg(_("\nArguments recognised by gvim (GTK+ version):\n"));
2724 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
2725 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
2726 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
2727 main_msg(_("-display <display>\tRun vim on <display> (also: --display)"));
2728# ifdef HAVE_GTK2
2729 main_msg(_("--role <role>\tSet a unique role to identify the main window"));
2730# endif
2731 main_msg(_("--socketid <xid>\tOpen Vim inside another GTK widget"));
2732#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00002733#ifdef FEAT_GUI_KDE
2734 mch_msg(_("\nArguments recognised by kvim (KDE version):\n"));
2735 main_msg(_("-black\t\tUse reverse video"));
2736#if QT_VERSION>=300
2737 main_msg(_("-tip\t\t\tDisplay the tip dialog on startup"));
2738 main_msg(_("-notip\t\tDisable the tip dialog"));
2739#endif
2740 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
2741 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
2742 main_msg(_("--display <display>\tRun vim on <display>"));
2743#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002744#ifdef FEAT_GUI_W32
2745 main_msg(_("-P <parent title>\tOpen Vim inside parent application"));
2746#endif
2747
2748#ifdef FEAT_GUI_GNOME
2749 /* Gnome gives extra messages for --help if we continue, but not for -h. */
2750 if (gui.starting)
2751 mch_msg("\n");
2752 else
2753#endif
2754 mch_exit(0);
2755}
2756
2757#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2758/*
2759 * Check the result of the ATTENTION dialog:
2760 * When "Quit" selected, exit Vim.
2761 * When "Recover" selected, recover the file.
2762 */
2763 static void
2764check_swap_exists_action()
2765{
2766 if (swap_exists_action == SEA_QUIT)
2767 getout(1);
2768 handle_swap_exists(NULL);
2769}
2770#endif
2771
2772#if defined(STARTUPTIME) || defined(PROTO)
2773static void time_diff __ARGS((struct timeval *then, struct timeval *now));
2774
2775static struct timeval prev_timeval;
2776
2777/*
2778 * Save the previous time before doing something that could nest.
2779 * set "*tv_rel" to the time elapsed so far.
2780 */
2781 void
2782time_push(tv_rel, tv_start)
2783 void *tv_rel, *tv_start;
2784{
2785 *((struct timeval *)tv_rel) = prev_timeval;
2786 gettimeofday(&prev_timeval, NULL);
2787 ((struct timeval *)tv_rel)->tv_usec = prev_timeval.tv_usec
2788 - ((struct timeval *)tv_rel)->tv_usec;
2789 ((struct timeval *)tv_rel)->tv_sec = prev_timeval.tv_sec
2790 - ((struct timeval *)tv_rel)->tv_sec;
2791 if (((struct timeval *)tv_rel)->tv_usec < 0)
2792 {
2793 ((struct timeval *)tv_rel)->tv_usec += 1000000;
2794 --((struct timeval *)tv_rel)->tv_sec;
2795 }
2796 *(struct timeval *)tv_start = prev_timeval;
2797}
2798
2799/*
2800 * Compute the previous time after doing something that could nest.
2801 * Subtract "*tp" from prev_timeval;
2802 * Note: The arguments are (void *) to avoid trouble with systems that don't
2803 * have struct timeval.
2804 */
2805 void
2806time_pop(tp)
2807 void *tp; /* actually (struct timeval *) */
2808{
2809 prev_timeval.tv_usec -= ((struct timeval *)tp)->tv_usec;
2810 prev_timeval.tv_sec -= ((struct timeval *)tp)->tv_sec;
2811 if (prev_timeval.tv_usec < 0)
2812 {
2813 prev_timeval.tv_usec += 1000000;
2814 --prev_timeval.tv_sec;
2815 }
2816}
2817
2818 static void
2819time_diff(then, now)
2820 struct timeval *then;
2821 struct timeval *now;
2822{
2823 long usec;
2824 long msec;
2825
2826 usec = now->tv_usec - then->tv_usec;
2827 msec = (now->tv_sec - then->tv_sec) * 1000L + usec / 1000L,
2828 usec = usec % 1000L;
2829 fprintf(time_fd, "%03ld.%03ld", msec, usec >= 0 ? usec : usec + 1000L);
2830}
2831
2832 void
2833time_msg(msg, tv_start)
2834 char *msg;
2835 void *tv_start; /* only for do_source: start time; actually
2836 (struct timeval *) */
2837{
2838 static struct timeval start;
2839 struct timeval now;
2840
2841 if (time_fd != NULL)
2842 {
2843 if (strstr(msg, "STARTING") != NULL)
2844 {
2845 gettimeofday(&start, NULL);
2846 prev_timeval = start;
2847 fprintf(time_fd, "\n\ntimes in msec\n");
2848 fprintf(time_fd, " clock self+sourced self: sourced script\n");
2849 fprintf(time_fd, " clock elapsed: other lines\n\n");
2850 }
2851 gettimeofday(&now, NULL);
2852 time_diff(&start, &now);
2853 if (((struct timeval *)tv_start) != NULL)
2854 {
2855 fprintf(time_fd, " ");
2856 time_diff(((struct timeval *)tv_start), &now);
2857 }
2858 fprintf(time_fd, " ");
2859 time_diff(&prev_timeval, &now);
2860 prev_timeval = now;
2861 fprintf(time_fd, ": %s\n", msg);
2862 }
2863}
2864
2865# ifdef WIN3264
2866/*
2867 * Windows doesn't have gettimeofday(), although it does have struct timeval.
2868 */
2869 int
2870gettimeofday(struct timeval *tv, char *dummy)
2871{
2872 long t = clock();
2873 tv->tv_sec = t / CLOCKS_PER_SEC;
2874 tv->tv_usec = (t - tv->tv_sec * CLOCKS_PER_SEC) * 1000000 / CLOCKS_PER_SEC;
2875 return 0;
2876}
2877# endif
2878
2879#endif
2880
2881#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
2882
2883/*
2884 * Common code for the X command server and the Win32 command server.
2885 */
2886
2887static char_u *build_drop_cmd __ARGS((int filec, char **filev, int sendReply));
2888
2889 static void
2890cmdsrv_main(argc, argv, serverName_arg, serverStr)
2891 int *argc;
2892 char **argv;
2893 char_u *serverName_arg;
2894 char_u **serverStr;
2895{
2896 char_u *res;
2897 int i;
2898 char_u *sname;
2899 int ret;
2900 int didone = FALSE;
2901 int exiterr = 0;
2902 char **newArgV = argv + 1;
2903 int newArgC = 1,
2904 Argc = *argc;
2905 int argtype;
2906#define ARGTYPE_OTHER 0
2907#define ARGTYPE_EDIT 1
2908#define ARGTYPE_EDIT_WAIT 2
2909#define ARGTYPE_SEND 3
2910 int silent = FALSE;
2911# ifndef FEAT_X11
2912 HWND srv;
2913# else
2914 Window srv;
2915
2916 setup_term_clip();
2917# endif
2918
2919 sname = serverMakeName(serverName_arg, argv[0]);
2920 if (sname == NULL)
2921 return;
2922
2923 /*
2924 * Execute the command server related arguments and remove them
2925 * from the argc/argv array; We may have to return into main()
2926 */
2927 for (i = 1; i < Argc; i++)
2928 {
2929 res = NULL;
2930 if (STRCMP(argv[i], "--") == 0) /* end of options */
2931 {
2932 for (; i < *argc; i++)
2933 {
2934 *newArgV++ = argv[i];
2935 newArgC++;
2936 }
2937 break;
2938 }
2939
2940 if (STRICMP(argv[i], "--remote") == 0)
2941 argtype = ARGTYPE_EDIT;
2942 else if (STRICMP(argv[i], "--remote-silent") == 0)
2943 {
2944 argtype = ARGTYPE_EDIT;
2945 silent = TRUE;
2946 }
2947 else if (STRICMP(argv[i], "--remote-wait") == 0)
2948 argtype = ARGTYPE_EDIT_WAIT;
2949 else if (STRICMP(argv[i], "--remote-wait-silent") == 0)
2950 {
2951 argtype = ARGTYPE_EDIT_WAIT;
2952 silent = TRUE;
2953 }
2954 else if (STRICMP(argv[i], "--remote-send") == 0)
2955 argtype = ARGTYPE_SEND;
2956 else
2957 argtype = ARGTYPE_OTHER;
2958 if (argtype != ARGTYPE_OTHER)
2959 {
2960 if (i == *argc - 1)
2961 mainerr_arg_missing((char_u *)argv[i]);
2962 if (argtype == ARGTYPE_SEND)
2963 {
2964 *serverStr = (char_u *)argv[i + 1];
2965 i++;
2966 }
2967 else
2968 {
2969 *serverStr = build_drop_cmd(*argc - i - 1, argv + i + 1,
2970 argtype == ARGTYPE_EDIT_WAIT);
2971 if (*serverStr == NULL)
2972 {
2973 /* Probably out of memory, exit. */
2974 didone = TRUE;
2975 exiterr = 1;
2976 break;
2977 }
2978 Argc = i;
2979 }
2980# ifdef FEAT_X11
2981 if (xterm_dpy == NULL)
2982 {
2983 mch_errmsg(_("No display"));
2984 ret = -1;
2985 }
2986 else
2987 ret = serverSendToVim(xterm_dpy, sname, *serverStr,
2988 NULL, &srv, 0, 0, silent);
2989# else
2990 /* Win32 always works? */
2991 ret = serverSendToVim(sname, *serverStr, NULL, &srv, 0, silent);
2992# endif
2993 if (ret < 0)
2994 {
2995 if (argtype == ARGTYPE_SEND)
2996 {
2997 /* Failed to send, abort. */
2998 mch_errmsg(_(": Send failed.\n"));
2999 didone = TRUE;
3000 exiterr = 1;
3001 }
3002 else if (!silent)
3003 /* Let vim start normally. */
3004 mch_errmsg(_(": Send failed. Trying to execute locally\n"));
3005 break;
3006 }
3007
3008# ifdef FEAT_GUI_W32
3009 /* Guess that when the server name starts with "g" it's a GUI
3010 * server, which we can bring to the foreground here.
3011 * Foreground() in the server doesn't work very well. */
3012 if (argtype != ARGTYPE_SEND && TOUPPER_ASC(*sname) == 'G')
3013 SetForegroundWindow(srv);
3014# endif
3015
3016 /*
3017 * For --remote-wait: Wait until the server did edit each
3018 * file. Also detect that the server no longer runs.
3019 */
3020 if (ret >= 0 && argtype == ARGTYPE_EDIT_WAIT)
3021 {
3022 int numFiles = *argc - i - 1;
3023 int j;
3024 char_u *done = alloc(numFiles);
3025 char_u *p;
3026# ifdef FEAT_GUI_W32
3027 NOTIFYICONDATA ni;
3028 int count = 0;
3029 extern HWND message_window;
3030# endif
3031
3032 if (numFiles > 0 && argv[i + 1][0] == '+')
3033 /* Skip "+cmd" argument, don't wait for it to be edited. */
3034 --numFiles;
3035
3036# ifdef FEAT_GUI_W32
3037 ni.cbSize = sizeof(ni);
3038 ni.hWnd = message_window;
3039 ni.uID = 0;
3040 ni.uFlags = NIF_ICON|NIF_TIP;
3041 ni.hIcon = LoadIcon((HINSTANCE)GetModuleHandle(0), "IDR_VIM");
3042 sprintf(ni.szTip, _("%d of %d edited"), count, numFiles);
3043 Shell_NotifyIcon(NIM_ADD, &ni);
3044# endif
3045
3046 /* Wait for all files to unload in remote */
3047 memset(done, 0, numFiles);
3048 while (memchr(done, 0, numFiles) != NULL)
3049 {
3050# ifdef WIN32
3051 p = serverGetReply(srv, NULL, TRUE, TRUE);
3052 if (p == NULL)
3053 break;
3054# else
3055 if (serverReadReply(xterm_dpy, srv, &p, TRUE) < 0)
3056 break;
3057# endif
3058 j = atoi((char *)p);
3059 if (j >= 0 && j < numFiles)
3060 {
3061# ifdef FEAT_GUI_W32
3062 ++count;
3063 sprintf(ni.szTip, _("%d of %d edited"),
3064 count, numFiles);
3065 Shell_NotifyIcon(NIM_MODIFY, &ni);
3066# endif
3067 done[j] = 1;
3068 }
3069 }
3070# ifdef FEAT_GUI_W32
3071 Shell_NotifyIcon(NIM_DELETE, &ni);
3072# endif
3073 }
3074 }
3075 else if (STRICMP(argv[i], "--remote-expr") == 0)
3076 {
3077 if (i == *argc - 1)
3078 mainerr_arg_missing((char_u *)argv[i]);
3079# ifdef WIN32
3080 /* Win32 always works? */
3081 if (serverSendToVim(sname, (char_u *)argv[i + 1],
3082 &res, NULL, 1, FALSE) < 0)
3083# else
3084 if (xterm_dpy == NULL)
3085 mch_errmsg(_("No display: Send expression failed.\n"));
3086 else if (serverSendToVim(xterm_dpy, sname, (char_u *)argv[i + 1],
3087 &res, NULL, 1, 1, FALSE) < 0)
3088# endif
3089 {
3090 if (res != NULL && *res != NUL)
3091 {
3092 /* Output error from remote */
3093 mch_errmsg((char *)res);
3094 vim_free(res);
3095 res = NULL;
3096 }
3097 mch_errmsg(_(": Send expression failed.\n"));
3098 }
3099 }
3100 else if (STRICMP(argv[i], "--serverlist") == 0)
3101 {
3102# ifdef WIN32
3103 /* Win32 always works? */
3104 res = serverGetVimNames();
3105# else
3106 if (xterm_dpy != NULL)
3107 res = serverGetVimNames(xterm_dpy);
3108# endif
3109 if (called_emsg)
3110 mch_errmsg("\n");
3111 }
3112 else if (STRICMP(argv[i], "--servername") == 0)
3113 {
3114 /* Alredy processed. Take it out of the command line */
3115 i++;
3116 continue;
3117 }
3118 else
3119 {
3120 *newArgV++ = argv[i];
3121 newArgC++;
3122 continue;
3123 }
3124 didone = TRUE;
3125 if (res != NULL && *res != NUL)
3126 {
3127 mch_msg((char *)res);
3128 if (res[STRLEN(res) - 1] != '\n')
3129 mch_msg("\n");
3130 }
3131 vim_free(res);
3132 }
3133
3134 if (didone)
3135 {
3136 display_errors(); /* display any collected messages */
3137 exit(exiterr); /* Mission accomplished - get out */
3138 }
3139
3140 /* Return back into main() */
3141 *argc = newArgC;
3142 vim_free(sname);
3143}
3144
3145/*
3146 * Build a ":drop" command to send to a Vim server.
3147 */
3148 static char_u *
3149build_drop_cmd(filec, filev, sendReply)
3150 int filec;
3151 char **filev;
3152 int sendReply;
3153{
3154 garray_T ga;
3155 int i;
3156 char_u *inicmd = NULL;
3157 char_u *p;
3158 char_u cwd[MAXPATHL];
3159
3160 if (filec > 0 && filev[0][0] == '+')
3161 {
3162 inicmd = (char_u *)filev[0] + 1;
3163 filev++;
3164 filec--;
3165 }
3166 /* Check if we have at least one argument. */
3167 if (filec <= 0)
3168 mainerr_arg_missing((char_u *)filev[-1]);
3169 if (mch_dirname(cwd, MAXPATHL) != OK)
3170 return NULL;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003171 if ((p = vim_strsave_escaped_ext(cwd, PATH_ESC_CHARS, '\\', TRUE)) == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003172 return NULL;
3173 ga_init2(&ga, 1, 100);
3174 ga_concat(&ga, (char_u *)"<C-\\><C-N>:cd ");
3175 ga_concat(&ga, p);
3176 /* Call inputsave() so that a prompt for an encryption key works. */
3177 ga_concat(&ga, (char_u *)"<CR>:if exists('*inputsave')|call inputsave()|endif|drop");
3178 vim_free(p);
3179 for (i = 0; i < filec; i++)
3180 {
3181 /* On Unix the shell has already expanded the wildcards, don't want to
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003182 * do it again in the Vim server. On MS-Windows only escape
3183 * non-wildcard characters. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003184 p = vim_strsave_escaped((char_u *)filev[i],
3185#ifdef UNIX
3186 PATH_ESC_CHARS
3187#else
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003188 (char_u *)" \t%#"
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003189#endif
3190 );
3191 if (p == NULL)
3192 {
3193 vim_free(ga.ga_data);
3194 return NULL;
3195 }
3196 ga_concat(&ga, (char_u *)" ");
3197 ga_concat(&ga, p);
3198 vim_free(p);
3199 }
3200 /* The :drop commands goes to Insert mode when 'insertmode' is set, use
3201 * CTRL-\ CTRL-N again. */
3202 ga_concat(&ga, (char_u *)"|if exists('*inputrestore')|call inputrestore()|endif<CR>");
3203 ga_concat(&ga, (char_u *)"<C-\\><C-N>:cd -");
3204 if (sendReply)
3205 ga_concat(&ga, (char_u *)"<CR>:call SetupRemoteReplies()");
3206 ga_concat(&ga, (char_u *)"<CR>:");
3207 if (inicmd != NULL)
3208 {
3209 /* Can't use <CR> after "inicmd", because an "startinsert" would cause
3210 * the following commands to be inserted as text. Use a "|",
3211 * hopefully "inicmd" does allow this... */
3212 ga_concat(&ga, inicmd);
3213 ga_concat(&ga, (char_u *)"|");
3214 }
3215 /* Bring the window to the foreground, goto Insert mode when 'im' set and
3216 * clear command line. */
Bram Moolenaar567e4de2004-12-31 21:01:02 +00003217 ga_concat(&ga, (char_u *)"cal foreground()|if &im|star|en|redr|f<CR>");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003218 ga_append(&ga, NUL);
3219 return ga.ga_data;
3220}
3221
3222/*
3223 * Replace termcodes such as <CR> and insert as key presses if there is room.
3224 */
3225 void
3226server_to_input_buf(str)
3227 char_u *str;
3228{
3229 char_u *ptr = NULL;
3230 char_u *cpo_save = p_cpo;
3231
3232 /* Set 'cpoptions' the way we want it.
3233 * B set - backslashes are *not* treated specially
3234 * k set - keycodes are *not* reverse-engineered
3235 * < unset - <Key> sequences *are* interpreted
3236 * The last parameter of replace_termcodes() is TRUE so that the <lt>
3237 * sequence is recognised - needed for a real backslash.
3238 */
3239 p_cpo = (char_u *)"Bk";
3240 str = replace_termcodes((char_u *)str, &ptr, FALSE, TRUE);
3241 p_cpo = cpo_save;
3242
3243 if (*ptr != NUL) /* trailing CTRL-V results in nothing */
3244 {
3245 /*
3246 * Add the string to the input stream.
3247 * Can't use add_to_input_buf() here, we now have K_SPECIAL bytes.
3248 *
3249 * First clear typed characters from the typeahead buffer, there could
3250 * be half a mapping there. Then append to the existing string, so
3251 * that multiple commands from a client are concatenated.
3252 */
3253 if (typebuf.tb_maplen < typebuf.tb_len)
3254 del_typebuf(typebuf.tb_len - typebuf.tb_maplen, typebuf.tb_maplen);
3255 (void)ins_typebuf(str, REMAP_NONE, typebuf.tb_len, TRUE, FALSE);
3256
3257 /* Let input_available() know we inserted text in the typeahead
3258 * buffer. */
3259 received_from_client = TRUE;
3260 }
3261 vim_free((char_u *)ptr);
3262}
3263
3264/*
3265 * Evaluate an expression that the client sent to a string.
3266 * Handles disabling error messages and disables debugging, otherwise Vim
3267 * hangs, waiting for "cont" to be typed.
3268 */
3269 char_u *
3270eval_client_expr_to_string(expr)
3271 char_u *expr;
3272{
3273 char_u *res;
3274 int save_dbl = debug_break_level;
3275 int save_ro = redir_off;
3276
3277 debug_break_level = -1;
3278 redir_off = 0;
3279 ++emsg_skip;
3280
3281 res = eval_to_string(expr, NULL);
3282
3283 debug_break_level = save_dbl;
3284 redir_off = save_ro;
3285 --emsg_skip;
3286
3287 return res;
3288}
3289
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003290/*
3291 * If conversion is needed, convert "data" from "client_enc" to 'encoding' and
3292 * return an allocated string. Otherwise return "data".
3293 * "*tofree" is set to the result when it needs to be freed later.
3294 */
3295/*ARGSUSED*/
3296 char_u *
3297serverConvert(client_enc, data, tofree)
3298 char_u *client_enc;
3299 char_u *data;
3300 char_u **tofree;
3301{
3302 char_u *res = data;
3303
3304 *tofree = NULL;
3305# ifdef FEAT_MBYTE
3306 if (client_enc != NULL && p_enc != NULL)
3307 {
3308 vimconv_T vimconv;
3309
3310 vimconv.vc_type = CONV_NONE;
3311 if (convert_setup(&vimconv, client_enc, p_enc) != FAIL
3312 && vimconv.vc_type != CONV_NONE)
3313 {
3314 res = string_convert(&vimconv, data, NULL);
3315 if (res == NULL)
3316 res = data;
3317 else
3318 *tofree = res;
3319 }
3320 convert_setup(&vimconv, NULL, NULL);
3321 }
3322# endif
3323 return res;
3324}
3325
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003326
3327/*
3328 * Make our basic server name: use the specified "arg" if given, otherwise use
3329 * the tail of the command "cmd" we were started with.
3330 * Return the name in allocated memory. This doesn't include a serial number.
3331 */
3332 static char_u *
3333serverMakeName(arg, cmd)
3334 char_u *arg;
3335 char *cmd;
3336{
3337 char_u *p;
3338
3339 if (arg != NULL && *arg != NUL)
3340 p = vim_strsave_up(arg);
3341 else
3342 {
3343 p = vim_strsave_up(gettail((char_u *)cmd));
3344 /* Remove .exe or .bat from the name. */
3345 if (p != NULL && vim_strchr(p, '.') != NULL)
3346 *vim_strchr(p, '.') = NUL;
3347 }
3348 return p;
3349}
3350#endif /* FEAT_CLIENTSERVER */
3351
3352/*
3353 * When FEAT_FKMAP is defined, also compile the Farsi source code.
3354 */
3355#if defined(FEAT_FKMAP) || defined(PROTO)
3356# include "farsi.c"
3357#endif
3358
3359/*
3360 * When FEAT_ARABIC is defined, also compile the Arabic source code.
3361 */
3362#if defined(FEAT_ARABIC) || defined(PROTO)
3363# include "arabic.c"
3364#endif