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