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