blob: 0f2925586438202a51c725fb1b59f352288585e4 [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
Bram Moolenaarc236c162008-07-13 17:41:49 +000010#if defined(MSDOS) || defined(WIN16) || defined(WIN32) || defined(_WIN64)
Bram Moolenaar362e1a32006-03-06 23:29:24 +000011# include "vimio.h" /* for close() and dup() */
Bram Moolenaarb4210b32004-06-13 14:51:16 +000012#endif
13
14#define EXTERN
15#include "vim.h"
16
17#ifdef SPAWNO
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +000018# include <spawno.h> /* special MS-DOS swapping library */
Bram Moolenaarb4210b32004-06-13 14:51:16 +000019#endif
20
Bram Moolenaarb4210b32004-06-13 14:51:16 +000021#ifdef __CYGWIN__
22# ifndef WIN32
Bram Moolenaar0d1498e2008-06-29 12:00:49 +000023# include <cygwin/version.h>
24# include <sys/cygwin.h> /* for cygwin_conv_to_posix_path() and/or
25 * cygwin_conv_path() */
Bram Moolenaarb4210b32004-06-13 14:51:16 +000026# endif
27# include <limits.h>
28#endif
29
Bram Moolenaarc013cb62005-07-24 21:18:31 +000030/* Maximum number of commands from + or -c arguments. */
31#define MAX_ARG_CMDS 10
32
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000033/* values for "window_layout" */
34#define WIN_HOR 1 /* "-o" horizontally split windows */
35#define WIN_VER 2 /* "-O" vertically split windows */
36#define WIN_TABS 3 /* "-p" windows on tab pages */
37
Bram Moolenaar58d98232005-07-23 22:25:46 +000038/* Struct for various parameters passed between main() and other functions. */
39typedef struct
40{
Bram Moolenaarc013cb62005-07-24 21:18:31 +000041 int argc;
42 char **argv;
43
44 int evim_mode; /* started as "evim" */
Bram Moolenaarc013cb62005-07-24 21:18:31 +000045 char_u *use_vimrc; /* vimrc from -u argument */
46
47 int n_commands; /* no. of commands from + or -c */
48 char_u *commands[MAX_ARG_CMDS]; /* commands from + or -c arg. */
49 char_u cmds_tofree[MAX_ARG_CMDS]; /* commands that need free() */
50 int n_pre_commands; /* no. of commands from --cmd */
51 char_u *pre_commands[MAX_ARG_CMDS]; /* commands from --cmd argument */
52
53 int edit_type; /* type of editing to do */
54 char_u *tagname; /* tag from -t argument */
55#ifdef FEAT_QUICKFIX
56 char_u *use_ef; /* 'errorfile' from -q argument */
57#endif
58
59 int want_full_screen;
60 int stdout_isatty; /* is stdout a terminal? */
61 char_u *term; /* specified terminal name */
62#ifdef FEAT_CRYPT
63 int ask_for_key; /* -x argument */
64#endif
65 int no_swap_file; /* "-n" argument used */
66#ifdef FEAT_EVAL
67 int use_debug_break_level;
68#endif
69#ifdef FEAT_WINDOWS
70 int window_count; /* number of windows to use */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000071 int window_layout; /* 0, WIN_HOR, WIN_VER or WIN_TABS */
Bram Moolenaarc013cb62005-07-24 21:18:31 +000072#endif
73
74#ifdef FEAT_CLIENTSERVER
Bram Moolenaar58d98232005-07-23 22:25:46 +000075 int serverArg; /* TRUE when argument for a server */
76 char_u *serverName_arg; /* cmdline arg for server name */
Bram Moolenaarc013cb62005-07-24 21:18:31 +000077 char_u *serverStr; /* remote server command */
78 char_u *serverStrEnc; /* encoding of serverStr */
79 char_u *servername; /* allocated name for our server */
80#endif
81#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
82 int literal; /* don't expand file names */
83#endif
84#ifdef MSWIN
85 int full_path; /* file name argument was full path */
86#endif
87#ifdef FEAT_DIFF
88 int diff_mode; /* start with 'diff' set */
89#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +000090} mparm_T;
91
Bram Moolenaarc013cb62005-07-24 21:18:31 +000092/* Values for edit_type. */
93#define EDIT_NONE 0 /* no edit type yet */
94#define EDIT_FILE 1 /* file name argument[s] given, use argument list */
95#define EDIT_STDIN 2 /* read file from stdin */
96#define EDIT_TAG 3 /* tag name argument given, use tagname */
97#define EDIT_QF 4 /* start in quickfix mode */
98
Bram Moolenaarb4210b32004-06-13 14:51:16 +000099#if defined(UNIX) || defined(VMS)
100static int file_owned __ARGS((char *fname));
101#endif
102static void mainerr __ARGS((int, char_u *));
103static void main_msg __ARGS((char *s));
104static void usage __ARGS((void));
105static int get_number_arg __ARGS((char_u *p, int *idx, int def));
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000106#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
107static void init_locale __ARGS((void));
108#endif
109static void parse_command_name __ARGS((mparm_T *parmp));
110static void early_arg_scan __ARGS((mparm_T *parmp));
111static void command_line_scan __ARGS((mparm_T *parmp));
112static void check_tty __ARGS((mparm_T *parmp));
113static void read_stdin __ARGS((void));
114static void create_windows __ARGS((mparm_T *parmp));
115#ifdef FEAT_WINDOWS
116static void edit_buffers __ARGS((mparm_T *parmp));
117#endif
118static void exe_pre_commands __ARGS((mparm_T *parmp));
119static void exe_commands __ARGS((mparm_T *parmp));
Bram Moolenaar58d98232005-07-23 22:25:46 +0000120static void source_startup_scripts __ARGS((mparm_T *parmp));
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000121static void main_start_gui __ARGS((void));
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000122#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000123static void check_swap_exists_action __ARGS((void));
124#endif
125#ifdef FEAT_CLIENTSERVER
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000126static void exec_on_server __ARGS((mparm_T *parmp));
127static void prepare_server __ARGS((mparm_T *parmp));
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000128static void cmdsrv_main __ARGS((int *argc, char **argv, char_u *serverName_arg, char_u **serverStr));
129static char_u *serverMakeName __ARGS((char_u *arg, char *cmd));
130#endif
131
132
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000133/*
134 * Different types of error messages.
135 */
136static char *(main_errors[]) =
137{
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000138 N_("Unknown option argument"),
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000139#define ME_UNKNOWN_OPTION 0
140 N_("Too many edit arguments"),
141#define ME_TOO_MANY_ARGS 1
142 N_("Argument missing after"),
143#define ME_ARG_MISSING 2
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000144 N_("Garbage after option argument"),
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000145#define ME_GARBAGE 3
146 N_("Too many \"+command\", \"-c command\" or \"--cmd command\" arguments"),
147#define ME_EXTRA_CMD 4
148 N_("Invalid argument for"),
149#define ME_INVALID_ARG 5
150};
151
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000152#ifndef PROTO /* don't want a prototype for main() */
153 int
154# ifdef VIMDLL
155_export
156# endif
157# ifdef FEAT_GUI_MSWIN
158# ifdef __BORLANDC__
159_cdecl
160# endif
161VimMain
162# else
163main
164# endif
165(argc, argv)
166 int argc;
167 char **argv;
168{
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000169 char_u *fname = NULL; /* file name from command line */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000170 mparm_T params; /* various parameters passed between
171 * main() and other functions. */
Bram Moolenaar3f269672009-11-03 11:11:11 +0000172#ifdef STARTUPTIME
173 int i;
174#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000175
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000176 /*
177 * Do any system-specific initialisations. These can NOT use IObuff or
178 * NameBuff. Thus emsg2() cannot be called!
179 */
180 mch_early_init();
181
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000182 /* Many variables are in "params" so that we can pass them to invoked
183 * functions without a lot of arguments. "argc" and "argv" are also
184 * copied, so that they can be changed. */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000185 vim_memset(&params, 0, sizeof(params));
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000186 params.argc = argc;
187 params.argv = argv;
188 params.want_full_screen = TRUE;
189#ifdef FEAT_EVAL
190 params.use_debug_break_level = -1;
191#endif
192#ifdef FEAT_WINDOWS
193 params.window_count = -1;
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000194#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +0000195
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000196#ifdef FEAT_TCL
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000197 vim_tcl_init(params.argv[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000198#endif
199
200#ifdef MEM_PROFILE
201 atexit(vim_mem_profile_dump);
202#endif
203
204#ifdef STARTUPTIME
Bram Moolenaar3f269672009-11-03 11:11:11 +0000205 for (i = 1; i < argc; ++i)
206 {
Bram Moolenaaref94eec2009-11-11 13:22:11 +0000207 if (STRICMP(argv[i], "--startuptime") == 0 && i + 1 < argc)
Bram Moolenaar3f269672009-11-03 11:11:11 +0000208 {
Bram Moolenaaref94eec2009-11-11 13:22:11 +0000209 time_fd = mch_fopen(argv[i + 1], "a");
Bram Moolenaar3f269672009-11-03 11:11:11 +0000210 TIME_MSG("--- VIM STARTING ---");
211 break;
212 }
213 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000214#endif
Bram Moolenaarca003e12006-03-17 23:19:38 +0000215 starttime = time(NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000216
217#ifdef __EMX__
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000218 _wildcard(&params.argc, &params.argv);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000219#endif
220
221#ifdef FEAT_MBYTE
222 (void)mb_init(); /* init mb_bytelen_tab[] to ones */
223#endif
Bram Moolenaardcaf10e2005-01-21 11:55:25 +0000224#ifdef FEAT_EVAL
225 eval_init(); /* init global variables */
226#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000227
228#ifdef __QNXNTO__
229 qnx_init(); /* PhAttach() for clipboard, (and gui) */
230#endif
231
232#ifdef MAC_OS_CLASSIC
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000233 /* Prepare for possibly starting GUI sometime */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000234 /* Macintosh needs this before any memory is allocated. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000235 gui_prepare(&params.argc, params.argv);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000236 TIME_MSG("GUI prepared");
237#endif
238
239 /* Init the table of Normal mode commands. */
240 init_normal_cmds();
241
242#if defined(HAVE_DATE_TIME) && defined(VMS) && defined(VAXC)
Bram Moolenaar58d98232005-07-23 22:25:46 +0000243 make_version(); /* Construct the long version string. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000244#endif
245
246 /*
247 * Allocate space for the generic buffers (needed for set_init_1() and
248 * EMSG2()).
249 */
250 if ((IObuff = alloc(IOSIZE)) == NULL
251 || (NameBuff = alloc(MAXPATHL)) == NULL)
252 mch_exit(0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000253 TIME_MSG("Allocated generic buffers");
254
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000255#ifdef NBDEBUG
256 /* Wait a moment for debugging NetBeans. Must be after allocating
257 * NameBuff. */
258 nbdebug_log_init("SPRO_GVIM_DEBUG", "SPRO_GVIM_DLEVEL");
259 nbdebug_wait(WT_ENV | WT_WAIT | WT_STOP, "SPRO_GVIM_WAIT", 20);
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000260 TIME_MSG("NetBeans debug wait");
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000261#endif
262
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000263#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
264 /*
265 * Setup to use the current locale (for ctype() and many other things).
266 * NOTE: Translated messages with encodings other than latin1 will not
267 * work until set_init_1() has been called!
268 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000269 init_locale();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000270 TIME_MSG("locale set");
271#endif
272
273#ifdef FEAT_GUI
274 gui.dofork = TRUE; /* default is to use fork() */
275#endif
276
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000277 /*
Bram Moolenaar58d98232005-07-23 22:25:46 +0000278 * Do a first scan of the arguments in "argv[]":
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000279 * -display or --display
Bram Moolenaar58d98232005-07-23 22:25:46 +0000280 * --server...
281 * --socketid
Bram Moolenaar78e17622007-08-30 10:26:19 +0000282 * --windowid
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000283 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000284 early_arg_scan(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000285
286#ifdef FEAT_SUN_WORKSHOP
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000287 findYourself(params.argv[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000288#endif
289#if defined(FEAT_GUI) && !defined(MAC_OS_CLASSIC)
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000290 /* Prepare for possibly starting GUI sometime */
291 gui_prepare(&params.argc, params.argv);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000292 TIME_MSG("GUI prepared");
293#endif
294
295#ifdef FEAT_CLIPBOARD
296 clip_init(FALSE); /* Initialise clipboard stuff */
297 TIME_MSG("clipboard setup");
298#endif
299
300 /*
301 * Check if we have an interactive window.
302 * On the Amiga: If there is no window, we open one with a newcli command
303 * (needed for :! to * work). mch_check_win() will also handle the -d or
304 * -dev argument.
305 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000306 params.stdout_isatty = (mch_check_win(params.argc, params.argv) != FAIL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000307 TIME_MSG("window checked");
308
309 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000310 * Allocate the first window and buffer.
311 * Can't do anything without it, exit when it fails.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000312 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000313 if (win_alloc_first() == FAIL)
314 mch_exit(0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000315
316 init_yank(); /* init yank buffers */
317
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000318 alist_init(&global_alist); /* Init the argument list to empty. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000319
320 /*
321 * Set the default values for the options.
322 * NOTE: Non-latin1 translated messages are working only after this,
323 * because this is where "has_mbyte" will be set, which is used by
324 * msg_outtrans_len_attr().
325 * First find out the home directory, needed to expand "~" in options.
326 */
327 init_homedir(); /* find real value of $HOME */
328 set_init_1();
329 TIME_MSG("inits 1");
330
331#ifdef FEAT_EVAL
332 set_lang_var(); /* set v:lang and v:ctype */
333#endif
334
335#ifdef FEAT_CLIENTSERVER
336 /*
337 * Do the client-server stuff, unless "--servername ''" was used.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000338 * This may exit Vim if the command was sent to the server.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000339 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000340 exec_on_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000341#endif
342
343 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000344 * Figure out the way to work from the command name argv[0].
345 * "vimdiff" starts diff mode, "rvim" sets "restricted", etc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000346 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000347 parse_command_name(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000348
349 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000350 * Process the command line arguments. File names are put in the global
351 * argument list "global_alist".
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000352 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000353 command_line_scan(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000354 TIME_MSG("parsing arguments");
355
356 /*
357 * On some systems, when we compile with the GUI, we always use it. On Mac
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000358 * there is no terminal version, and on Windows we can't fork one off with
359 * :gui.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000360 */
361#ifdef ALWAYS_USE_GUI
362 gui.starting = TRUE;
363#else
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000364# if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000365 /*
366 * Check if the GUI can be started. Reset gui.starting if not.
367 * Don't know about other systems, stay on the safe side and don't check.
368 */
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000369 if (gui.starting)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000370 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000371 if (gui_init_check() == FAIL)
372 {
373 gui.starting = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000374
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000375 /* When running "evim" or "gvim -y" we need the menus, exit if we
376 * don't have them. */
377 if (params.evim_mode)
378 mch_exit(1);
379 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000380 }
381# endif
382#endif
383
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000384 if (GARGCOUNT > 0)
385 {
386#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
387 /*
388 * Expand wildcards in file names.
389 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000390 if (!params.literal)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000391 {
392 /* Temporarily add '(' and ')' to 'isfname'. These are valid
393 * filename characters but are excluded from 'isfname' to make
394 * "gf" work on a file name in parenthesis (e.g.: see vim.h). */
395 do_cmdline_cmd((char_u *)":set isf+=(,)");
Bram Moolenaar86b68352004-12-27 21:59:20 +0000396 alist_expand(NULL, 0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000397 do_cmdline_cmd((char_u *)":set isf&");
398 }
399#endif
400 fname = alist_name(&GARGLIST[0]);
401 }
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000402
403#if defined(WIN32) && defined(FEAT_MBYTE)
404 {
405 extern void set_alist_count(void);
406
407 /* Remember the number of entries in the argument list. If it changes
408 * we don't react on setting 'encoding'. */
409 set_alist_count();
410 }
411#endif
412
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000413#ifdef MSWIN
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000414 if (GARGCOUNT == 1 && params.full_path)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000415 {
416 /*
417 * If there is one filename, fully qualified, we have very probably
418 * been invoked from explorer, so change to the file's directory.
419 * Hint: to avoid this when typing a command use a forward slash.
420 * If the cd fails, it doesn't matter.
421 */
422 (void)vim_chdirfile(fname);
423 }
424#endif
425 TIME_MSG("expanding arguments");
426
427#ifdef FEAT_DIFF
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000428 if (params.diff_mode && params.window_count == -1)
429 params.window_count = 0; /* open up to 3 windows */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000430#endif
431
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000432 /* Don't redraw until much later. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000433 ++RedrawingDisabled;
434
435 /*
436 * When listing swap file names, don't do cursor positioning et. al.
437 */
438 if (recoverymode && fname == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000439 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000440
441 /*
442 * When certain to start the GUI, don't check capabilities of terminal.
443 * For GTK we can't be sure, but when started from the desktop it doesn't
444 * make sense to try using a terminal.
445 */
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000446#if defined(ALWAYS_USE_GUI) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000447 if (gui.starting
448# ifdef FEAT_GUI_GTK
449 && !isatty(2)
450# endif
451 )
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000452 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000453#endif
454
455#if defined(FEAT_GUI_MAC) && defined(MACOS_X_UNIX)
456 /* When the GUI is started from Finder, need to display messages in a
457 * message box. isatty(2) returns TRUE anyway, thus we need to check the
458 * name to know we're not started from a terminal. */
459 if (gui.starting && (!isatty(2) || strcmp("/dev/console", ttyname(2)) == 0))
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000460 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000461 params.want_full_screen = FALSE;
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000462
463 /* Avoid always using "/" as the current directory. Note that when
464 * started from Finder the arglist will be filled later in
465 * HandleODocAE() and "fname" will be NULL. */
466 if (getcwd((char *)NameBuff, MAXPATHL) != NULL
467 && STRCMP(NameBuff, "/") == 0)
468 {
469 if (fname != NULL)
470 (void)vim_chdirfile(fname);
471 else
472 {
473 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
474 vim_chdir(NameBuff);
475 }
476 }
477 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000478#endif
479
480 /*
481 * mch_init() sets up the terminal (window) for use. This must be
482 * done after resetting full_screen, otherwise it may move the cursor
483 * (MSDOS).
484 * Note that we may use mch_exit() before mch_init()!
485 */
486 mch_init();
487 TIME_MSG("shell init");
488
489#ifdef USE_XSMP
490 /*
491 * For want of anywhere else to do it, try to connect to xsmp here.
492 * Fitting it in after gui_mch_init, but before gui_init (via termcapinit).
493 * Hijacking -X 'no X connection' to also disable XSMP connection as that
494 * has a similar delay upon failure.
495 * Only try if SESSION_MANAGER is set to something non-null.
496 */
497 if (!x_no_connect)
498 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000499 char *p = getenv("SESSION_MANAGER");
500
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000501 if (p != NULL && *p != NUL)
502 {
503 xsmp_init();
504 TIME_MSG("xsmp init");
505 }
506 }
507#endif
508
509 /*
510 * Print a warning if stdout is not a terminal.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000511 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000512 check_tty(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000513
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000514 /* This message comes before term inits, but after setting "silent_mode"
515 * when the input is not a tty. */
516 if (GARGCOUNT > 1 && !silent_mode)
517 printf(_("%d files to edit\n"), GARGCOUNT);
518
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000519 if (params.want_full_screen && !silent_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000520 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000521 termcapinit(params.term); /* set terminal name and get terminal
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000522 capabilities (will set full_screen) */
523 screen_start(); /* don't know where cursor is now */
524 TIME_MSG("Termcap init");
525 }
526
527 /*
528 * Set the default values for the options that use Rows and Columns.
529 */
530 ui_get_shellsize(); /* inits Rows and Columns */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000531 win_init_size();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000532#ifdef FEAT_DIFF
533 /* Set the 'diff' option now, so that it can be checked for in a .vimrc
534 * file. There is no buffer yet though. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000535 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000536 diff_win_options(firstwin, FALSE);
537#endif
538
539 cmdline_row = Rows - p_ch;
540 msg_row = cmdline_row;
541 screenalloc(FALSE); /* allocate screen buffers */
542 set_init_2();
543 TIME_MSG("inits 2");
544
545 msg_scroll = TRUE;
546 no_wait_return = TRUE;
547
548 init_mappings(); /* set up initial mappings */
549
550 init_highlight(TRUE, FALSE); /* set the default highlight groups */
551 TIME_MSG("init highlight");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000552
553#ifdef FEAT_EVAL
554 /* Set the break level after the terminal is initialized. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000555 debug_break_level = params.use_debug_break_level;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000556#endif
557
Bram Moolenaar58d98232005-07-23 22:25:46 +0000558 /* Execute --cmd arguments. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000559 exe_pre_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000560
Bram Moolenaar58d98232005-07-23 22:25:46 +0000561 /* Source startup scripts. */
562 source_startup_scripts(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000563
564#ifdef FEAT_EVAL
565 /*
566 * Read all the plugin files.
567 * Only when compiled with +eval, since most plugins need it.
568 */
569 if (p_lpl)
570 {
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000571# ifdef VMS /* Somehow VMS doesn't handle the "**". */
572 source_runtime((char_u *)"plugin/*.vim", TRUE);
573# else
Bram Moolenaar07d4d732005-10-03 22:04:08 +0000574 source_runtime((char_u *)"plugin/**/*.vim", TRUE);
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000575# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000576 TIME_MSG("loading plugins");
577 }
578#endif
579
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000580#ifdef FEAT_DIFF
581 /* Decide about window layout for diff mode after reading vimrc. */
582 if (params.diff_mode && params.window_layout == 0)
583 {
584 if (diffopt_horizontal())
585 params.window_layout = WIN_HOR; /* use horizontal split */
586 else
587 params.window_layout = WIN_VER; /* use vertical split */
588 }
589#endif
590
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000591 /*
592 * Recovery mode without a file name: List swap files.
593 * This uses the 'dir' option, therefore it must be after the
594 * initializations.
595 */
596 if (recoverymode && fname == NULL)
597 {
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200598 recover_names(NULL, TRUE, 0, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000599 mch_exit(0);
600 }
601
602 /*
603 * Set a few option defaults after reading .vimrc files:
604 * 'title' and 'icon', Unix: 'shellpipe' and 'shellredir'.
605 */
606 set_init_3();
607 TIME_MSG("inits 3");
608
609 /*
610 * "-n" argument: Disable swap file by setting 'updatecount' to 0.
611 * Note that this overrides anything from a vimrc file.
612 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000613 if (params.no_swap_file)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000614 p_uc = 0;
615
616#ifdef FEAT_FKMAP
617 if (curwin->w_p_rl && p_altkeymap)
618 {
619 p_hkmap = FALSE; /* Reset the Hebrew keymap mode */
620# ifdef FEAT_ARABIC
621 curwin->w_p_arab = FALSE; /* Reset the Arabic keymap mode */
622# endif
623 p_fkmap = TRUE; /* Set the Farsi keymap mode */
624 }
625#endif
626
627#ifdef FEAT_GUI
628 if (gui.starting)
629 {
630#if defined(UNIX) || defined(VMS)
631 /* When something caused a message from a vimrc script, need to output
632 * an extra newline before the shell prompt. */
633 if (did_emsg || msg_didout)
634 putchar('\n');
635#endif
636
637 gui_start(); /* will set full_screen to TRUE */
638 TIME_MSG("starting GUI");
639
640 /* When running "evim" or "gvim -y" we need the menus, exit if we
641 * don't have them. */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000642 if (!gui.in_use && params.evim_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000643 mch_exit(1);
644 }
645#endif
646
647#ifdef SPAWNO /* special MSDOS swapping library */
648 init_SPAWNO("", SWAP_ANY);
649#endif
650
651#ifdef FEAT_VIMINFO
652 /*
Bram Moolenaard812df62008-11-09 12:46:09 +0000653 * Read in registers, history etc, but not marks, from the viminfo file.
654 * This is where v:oldfiles gets filled.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000655 */
656 if (*p_viminfo != NUL)
657 {
Bram Moolenaard812df62008-11-09 12:46:09 +0000658 read_viminfo(NULL, VIF_WANT_INFO | VIF_GET_OLDFILES);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000659 TIME_MSG("reading viminfo");
660 }
661#endif
662
663#ifdef FEAT_QUICKFIX
664 /*
665 * "-q errorfile": Load the error file now.
666 * If the error file can't be read, exit before doing anything else.
667 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000668 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000669 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000670 if (params.use_ef != NULL)
671 set_string_option_direct((char_u *)"ef", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000672 params.use_ef, OPT_FREE, SID_CARG);
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000673 if (qf_init(NULL, p_ef, p_efm, TRUE) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000674 {
675 out_char('\n');
676 mch_exit(3);
677 }
678 TIME_MSG("reading errorfile");
679 }
680#endif
681
682 /*
683 * Start putting things on the screen.
684 * Scroll screen down before drawing over it
685 * Clear screen now, so file message will not be cleared.
686 */
687 starting = NO_BUFFERS;
688 no_wait_return = FALSE;
689 if (!exmode_active)
690 msg_scroll = FALSE;
691
692#ifdef FEAT_GUI
693 /*
694 * This seems to be required to make callbacks to be called now, instead
695 * of after things have been put on the screen, which then may be deleted
696 * when getting a resize callback.
697 * For the Mac this handles putting files dropped on the Vim icon to
698 * global_alist.
699 */
700 if (gui.in_use)
701 {
702# ifdef FEAT_SUN_WORKSHOP
703 if (!usingSunWorkShop)
704# endif
705 gui_wait_for_chars(50L);
706 TIME_MSG("GUI delay");
707 }
708#endif
709
710#if defined(FEAT_GUI_PHOTON) && defined(FEAT_CLIPBOARD)
711 qnx_clip_init();
712#endif
713
714#ifdef FEAT_XCLIPBOARD
715 /* Start using the X clipboard, unless the GUI was started. */
716# ifdef FEAT_GUI
717 if (!gui.in_use)
718# endif
719 {
720 setup_term_clip();
721 TIME_MSG("setup clipboard");
722 }
723#endif
724
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000725#ifdef FEAT_CLIENTSERVER
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000726 /* Prepare for being a Vim server. */
727 prepare_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000728#endif
729
730 /*
731 * If "-" argument given: Read file from stdin.
732 * Do this before starting Raw mode, because it may change things that the
733 * writing end of the pipe doesn't like, e.g., in case stdin and stderr
734 * are the same terminal: "cat | vim -".
735 * Using autocommands here may cause trouble...
736 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000737 if (params.edit_type == EDIT_STDIN && !recoverymode)
738 read_stdin();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000739
740#if defined(UNIX) || defined(VMS)
741 /* When switching screens and something caused a message from a vimrc
742 * script, need to output an extra newline on exit. */
743 if ((did_emsg || msg_didout) && *T_TI != NUL)
744 newline_on_exit = TRUE;
745#endif
746
747 /*
748 * When done something that is not allowed or error message call
749 * wait_return. This must be done before starttermcap(), because it may
750 * switch to another screen. It must be done after settmode(TMODE_RAW),
751 * because we want to react on a single key stroke.
752 * Call settmode and starttermcap here, so the T_KS and T_TI may be
Bram Moolenaar49325942007-05-10 19:19:59 +0000753 * defined by termcapinit and redefined in .exrc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000754 */
755 settmode(TMODE_RAW);
756 TIME_MSG("setting raw mode");
757
758 if (need_wait_return || msg_didany)
759 {
760 wait_return(TRUE);
761 TIME_MSG("waiting for return");
762 }
763
764 starttermcap(); /* start termcap if not done by wait_return() */
765 TIME_MSG("start termcap");
766
767#ifdef FEAT_MOUSE
768 setmouse(); /* may start using the mouse */
769#endif
770 if (scroll_region)
771 scroll_region_reset(); /* In case Rows changed */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000772 scroll_start(); /* may scroll the screen to the right position */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000773
774 /*
775 * Don't clear the screen when starting in Ex mode, unless using the GUI.
776 */
777 if (exmode_active
778#ifdef FEAT_GUI
779 && !gui.in_use
780#endif
781 )
782 must_redraw = CLEAR;
783 else
784 {
785 screenclear(); /* clear screen */
786 TIME_MSG("clearing screen");
787 }
788
789#ifdef FEAT_CRYPT
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000790 if (params.ask_for_key)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000791 {
Bram Moolenaar40e6a712010-05-16 22:32:54 +0200792 (void)blowfish_self_test();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000793 (void)get_crypt_key(TRUE, TRUE);
794 TIME_MSG("getting crypt key");
795 }
796#endif
797
798 no_wait_return = TRUE;
799
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000800 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000801 * Create the requested number of windows and edit buffers in them.
802 * Also does recovery if "recoverymode" set.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000803 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000804 create_windows(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000805 TIME_MSG("opening buffers");
806
Bram Moolenaar867a4b72007-03-18 20:51:46 +0000807#ifdef FEAT_EVAL
808 /* clear v:swapcommand */
809 set_vim_var_string(VV_SWAPCOMMAND, NULL, -1);
810#endif
811
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000812 /* Ex starts at last line of the file */
813 if (exmode_active)
814 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
815
816#ifdef FEAT_AUTOCMD
817 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
818 TIME_MSG("BufEnter autocommands");
819#endif
820 setpcmark();
821
822#ifdef FEAT_QUICKFIX
823 /*
824 * When started with "-q errorfile" jump to first error now.
825 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000826 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000827 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000828 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000829 TIME_MSG("jump to first error");
830 }
831#endif
832
833#ifdef FEAT_WINDOWS
834 /*
835 * If opened more than one window, start editing files in the other
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000836 * windows.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000837 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000838 edit_buffers(&params);
839#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000840
841#ifdef FEAT_DIFF
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000842 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000843 {
844 win_T *wp;
845
846 /* set options in each window for "vimdiff". */
847 for (wp = firstwin; wp != NULL; wp = wp->w_next)
848 diff_win_options(wp, TRUE);
849 }
850#endif
851
852 /*
853 * Shorten any of the filenames, but only when absolute.
854 */
855 shorten_fnames(FALSE);
856
857 /*
858 * Need to jump to the tag before executing the '-c command'.
859 * Makes "vim -c '/return' -t main" work.
860 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000861 if (params.tagname != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000862 {
Bram Moolenaar146522e2005-12-16 21:55:46 +0000863#if defined(HAS_SWAP_EXISTS_ACTION)
864 swap_exists_did_quit = FALSE;
865#endif
866
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000867 vim_snprintf((char *)IObuff, IOSIZE, "ta %s", params.tagname);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000868 do_cmdline_cmd(IObuff);
869 TIME_MSG("jumping to tag");
Bram Moolenaar146522e2005-12-16 21:55:46 +0000870
871#if defined(HAS_SWAP_EXISTS_ACTION)
872 /* If the user doesn't want to edit the file then we quit here. */
873 if (swap_exists_did_quit)
874 getout(1);
875#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000876 }
877
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000878 /* Execute any "+", "-c" and "-S" arguments. */
879 if (params.n_commands > 0)
880 exe_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000881
882 RedrawingDisabled = 0;
883 redraw_all_later(NOT_VALID);
884 no_wait_return = FALSE;
885 starting = 0;
886
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000887#ifdef FEAT_TERMRESPONSE
888 /* Requesting the termresponse is postponed until here, so that a "-c q"
889 * argument doesn't make it appear in the shell Vim was started from. */
890 may_req_termresponse();
891#endif
892
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000893 /* start in insert mode */
894 if (p_im)
895 need_start_insertmode = TRUE;
896
897#ifdef FEAT_AUTOCMD
898 apply_autocmds(EVENT_VIMENTER, NULL, NULL, FALSE, curbuf);
899 TIME_MSG("VimEnter autocommands");
900#endif
901
902#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
903 /* When a startup script or session file setup for diff'ing and
904 * scrollbind, sync the scrollbind now. */
905 if (curwin->w_p_diff && curwin->w_p_scb)
906 {
907 update_topline();
908 check_scrollbind((linenr_T)0, 0L);
909 TIME_MSG("diff scrollbinding");
910 }
911#endif
912
913#if defined(WIN3264) && !defined(FEAT_GUI_W32)
914 mch_set_winsize_now(); /* Allow winsize changes from now on */
915#endif
916
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000917#if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
918 /* When tab pages were created, may need to update the tab pages line and
919 * scrollbars. This is skipped while creating them. */
920 if (first_tabpage->tp_next != NULL)
921 {
922 out_flush();
923 gui_init_which_components(NULL);
924 gui_update_scrollbars(TRUE);
925 }
926 need_mouse_correct = TRUE;
927#endif
928
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000929 /* If ":startinsert" command used, stuff a dummy command to be able to
930 * call normal_cmd(), which will then start Insert mode. */
931 if (restart_edit != 0)
Bram Moolenaarebefac62005-12-28 22:39:57 +0000932 stuffcharReadbuff(K_NOP);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000933
934#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200935 if (netbeansArg != NULL && strncmp("-nb", netbeansArg, 3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200936 {
937# ifdef FEAT_GUI
938# if !defined(FEAT_GUI_MOTIF) && !defined(FEAT_GUI_GTK) \
939 && !defined(FEAT_GUI_W32)
940 if (gui.in_use)
941 {
942 mch_errmsg(_("netbeans is not supported with this GUI\n"));
943 mch_exit(2);
944 }
945# endif
946# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000947 /* Tell the client that it can start sending commands. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200948 netbeans_open(netbeansArg + 3, TRUE);
Bram Moolenaar67c53842010-05-22 18:28:27 +0200949 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000950#endif
951
952 TIME_MSG("before starting main loop");
953
954 /*
955 * Call the main command loop. This never returns.
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000956 * For embedded MzScheme the main_loop will be called by Scheme
957 * for proper stack tracking
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000958 */
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000959#ifndef FEAT_MZSCHEME
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000960 main_loop(FALSE, FALSE);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000961#else
962 mzscheme_main();
963#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000964
965 return 0;
966}
967#endif /* PROTO */
968
969/*
970 * Main loop: Execute Normal mode commands until exiting Vim.
971 * Also used to handle commands in the command-line window, until the window
972 * is closed.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000973 * Also used to handle ":visual" command after ":global": execute Normal mode
974 * commands, return when entering Ex mode. "noexmode" is TRUE then.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000975 */
976 void
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000977main_loop(cmdwin, noexmode)
978 int cmdwin; /* TRUE when working in the command-line window */
979 int noexmode; /* TRUE when return on entering Ex mode */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000980{
Bram Moolenaar225d32b2007-08-10 19:33:47 +0000981 oparg_T oa; /* operator arguments */
982 int previous_got_int = FALSE; /* "got_int" was TRUE */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000983
984#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
985 /* Setup to catch a terminating error from the X server. Just ignore
986 * it, restore the state and continue. This might not always work
987 * properly, but at least we don't exit unexpectedly when the X server
988 * exists while Vim is running in a console. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000989 if (!cmdwin && !noexmode && SETJMP(x_jump_env))
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000990 {
991 State = NORMAL;
992# ifdef FEAT_VISUAL
993 VIsual_active = FALSE;
994# endif
995 got_int = TRUE;
996 need_wait_return = FALSE;
997 global_busy = FALSE;
998 exmode_active = 0;
999 skip_redraw = FALSE;
1000 RedrawingDisabled = 0;
1001 no_wait_return = 0;
1002# ifdef FEAT_EVAL
1003 emsg_skip = 0;
1004# endif
1005 emsg_off = 0;
1006# ifdef FEAT_MOUSE
1007 setmouse();
1008# endif
1009 settmode(TMODE_RAW);
1010 starttermcap();
1011 scroll_start();
1012 redraw_later_clear();
1013 }
1014#endif
1015
1016 clear_oparg(&oa);
1017 while (!cmdwin
1018#ifdef FEAT_CMDWIN
1019 || cmdwin_result == 0
1020#endif
1021 )
1022 {
1023 if (stuff_empty())
1024 {
1025 did_check_timestamps = FALSE;
1026 if (need_check_timestamps)
1027 check_timestamps(FALSE);
1028 if (need_wait_return) /* if wait_return still needed ... */
1029 wait_return(FALSE); /* ... call it now */
1030 if (need_start_insertmode && goto_im()
1031#ifdef FEAT_VISUAL
1032 && !VIsual_active
1033#endif
1034 )
1035 {
1036 need_start_insertmode = FALSE;
1037 stuffReadbuff((char_u *)"i"); /* start insert mode next */
1038 /* skip the fileinfo message now, because it would be shown
1039 * after insert mode finishes! */
1040 need_fileinfo = FALSE;
1041 }
1042 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001043
1044 /* Reset "got_int" now that we got back to the main loop. Except when
1045 * inside a ":g/pat/cmd" command, then the "got_int" needs to abort
1046 * the ":g" command.
1047 * For ":g/pat/vi" we reset "got_int" when used once. When used
1048 * a second time we go back to Ex mode and abort the ":g" command. */
1049 if (got_int)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001050 {
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001051 if (noexmode && global_busy && !exmode_active && previous_got_int)
1052 {
1053 /* Typed two CTRL-C in a row: go back to ex mode as if "Q" was
1054 * used and keep "got_int" set, so that it aborts ":g". */
1055 exmode_active = EXMODE_NORMAL;
1056 State = NORMAL;
1057 }
1058 else if (!global_busy || !exmode_active)
1059 {
1060 if (!quit_more)
1061 (void)vgetc(); /* flush all buffers */
1062 got_int = FALSE;
1063 }
1064 previous_got_int = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001065 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001066 else
1067 previous_got_int = FALSE;
1068
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001069 if (!exmode_active)
1070 msg_scroll = FALSE;
1071 quit_more = FALSE;
1072
1073 /*
1074 * If skip redraw is set (for ":" in wait_return()), don't redraw now.
1075 * If there is nothing in the stuff_buffer or do_redraw is TRUE,
1076 * update cursor and redraw.
1077 */
1078 if (skip_redraw || exmode_active)
1079 skip_redraw = FALSE;
1080 else if (do_redraw || stuff_empty())
1081 {
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001082#ifdef FEAT_AUTOCMD
1083 /* Trigger CursorMoved if the cursor moved. */
1084 if (!finish_op && has_cursormoved()
1085 && !equalpos(last_cursormoved, curwin->w_cursor))
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001086 {
1087 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL, FALSE, curbuf);
1088 last_cursormoved = curwin->w_cursor;
1089 }
1090#endif
1091
Bram Moolenaar33aec762006-01-22 23:30:12 +00001092#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
1093 /* Scroll-binding for diff mode may have been postponed until
1094 * here. Avoids doing it for every change. */
1095 if (diff_need_scrollbind)
1096 {
1097 check_scrollbind((linenr_T)0, 0L);
1098 diff_need_scrollbind = FALSE;
1099 }
1100#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001101#if defined(FEAT_FOLDING) && defined(FEAT_VISUAL)
1102 /* Include a closed fold completely in the Visual area. */
1103 foldAdjustVisual();
1104#endif
1105#ifdef FEAT_FOLDING
1106 /*
1107 * When 'foldclose' is set, apply 'foldlevel' to folds that don't
1108 * contain the cursor.
1109 * When 'foldopen' is "all", open the fold(s) under the cursor.
1110 * This may mark the window for redrawing.
1111 */
1112 if (hasAnyFolding(curwin) && !char_avail())
1113 {
1114 foldCheckClose();
1115 if (fdo_flags & FDO_ALL)
1116 foldOpenCursor();
1117 }
1118#endif
1119
1120 /*
1121 * Before redrawing, make sure w_topline is correct, and w_leftcol
1122 * if lines don't wrap, and w_skipcol if lines wrap.
1123 */
1124 update_topline();
1125 validate_cursor();
1126
1127#ifdef FEAT_VISUAL
1128 if (VIsual_active)
1129 update_curbuf(INVERTED);/* update inverted part */
1130 else
1131#endif
1132 if (must_redraw)
1133 update_screen(0);
1134 else if (redraw_cmdline || clear_cmdline)
1135 showmode();
1136#ifdef FEAT_WINDOWS
1137 redraw_statuslines();
1138#endif
1139#ifdef FEAT_TITLE
1140 if (need_maketitle)
1141 maketitle();
1142#endif
1143 /* display message after redraw */
1144 if (keep_msg != NULL)
1145 {
1146 char_u *p;
1147
1148 /* msg_attr_keep() will set keep_msg to NULL, must free the
1149 * string here. */
1150 p = keep_msg;
Bram Moolenaar238a5642006-02-21 22:12:05 +00001151 keep_msg = NULL;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001152 msg_attr(p, keep_msg_attr);
1153 vim_free(p);
1154 }
1155 if (need_fileinfo) /* show file info after redraw */
1156 {
1157 fileinfo(FALSE, TRUE, FALSE);
1158 need_fileinfo = FALSE;
1159 }
1160
1161 emsg_on_display = FALSE; /* can delete error message now */
1162 did_emsg = FALSE;
1163 msg_didany = FALSE; /* reset lines_left in msg_start() */
Bram Moolenaar661b1822005-07-28 22:36:45 +00001164 may_clear_sb_text(); /* clear scroll-back text on next msg */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001165 showruler(FALSE);
1166
1167 setcursor();
1168 cursor_on();
1169
1170 do_redraw = FALSE;
Bram Moolenaar3f269672009-11-03 11:11:11 +00001171
1172#ifdef STARTUPTIME
1173 /* Now that we have drawn the first screen all the startup stuff
1174 * has been done, close any file for startup messages. */
1175 if (time_fd != NULL)
1176 {
1177 TIME_MSG("first screen update");
1178 TIME_MSG("--- VIM STARTED ---");
1179 fclose(time_fd);
1180 time_fd = NULL;
1181 }
1182#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001183 }
1184#ifdef FEAT_GUI
1185 if (need_mouse_correct)
1186 gui_mouse_correct();
1187#endif
1188
1189 /*
1190 * Update w_curswant if w_set_curswant has been set.
1191 * Postponed until here to avoid computing w_virtcol too often.
1192 */
1193 update_curswant();
1194
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001195#ifdef FEAT_EVAL
1196 /*
1197 * May perform garbage collection when waiting for a character, but
1198 * only at the very toplevel. Otherwise we may be using a List or
1199 * Dict internally somewhere.
1200 * "may_garbage_collect" is reset in vgetc() which is invoked through
1201 * do_exmode() and normal_cmd().
1202 */
1203 may_garbage_collect = (!cmdwin && !noexmode);
1204#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001205 /*
1206 * If we're invoked as ex, do a round of ex commands.
1207 * Otherwise, get and execute a normal mode command.
1208 */
1209 if (exmode_active)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001210 {
1211 if (noexmode) /* End of ":global/path/visual" commands */
1212 return;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001213 do_exmode(exmode_active == EXMODE_VIM);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001214 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001215 else
1216 normal_cmd(&oa, TRUE);
1217 }
1218}
1219
1220
1221#if defined(USE_XSMP) || defined(FEAT_GUI_MSWIN) || defined(PROTO)
1222/*
1223 * Exit, but leave behind swap files for modified buffers.
1224 */
1225 void
1226getout_preserve_modified(exitval)
1227 int exitval;
1228{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001229# if defined(SIGHUP) && defined(SIG_IGN)
1230 /* Ignore SIGHUP, because a dropped connection causes a read error, which
1231 * makes Vim exit and then handling SIGHUP causes various reentrance
1232 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001233 signal(SIGHUP, SIG_IGN);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001234# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001235
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001236 ml_close_notmod(); /* close all not-modified buffers */
1237 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
1238 ml_close_all(FALSE); /* close all memfiles, without deleting */
1239 getout(exitval); /* exit Vim properly */
1240}
1241#endif
1242
1243
1244/* Exit properly */
1245 void
1246getout(exitval)
1247 int exitval;
1248{
1249#ifdef FEAT_AUTOCMD
1250 buf_T *buf;
1251 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001252 tabpage_T *tp, *next_tp;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001253#endif
1254
1255 exiting = TRUE;
1256
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001257 /* When running in Ex mode an error causes us to exit with a non-zero exit
1258 * code. POSIX requires this, although it's not 100% clear from the
1259 * standard. */
1260 if (exmode_active)
1261 exitval += ex_exitval;
1262
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001263 /* Position the cursor on the last screen line, below all the text */
1264#ifdef FEAT_GUI
1265 if (!gui.in_use)
1266#endif
1267 windgoto((int)Rows - 1, 0);
1268
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +00001269#if defined(FEAT_EVAL) || defined(FEAT_SYN_HL)
1270 /* Optionally print hashtable efficiency. */
1271 hash_debug_results();
1272#endif
1273
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001274#ifdef FEAT_GUI
1275 msg_didany = FALSE;
1276#endif
1277
1278#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001279 if (get_vim_var_nr(VV_DYING) <= 1)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001280 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001281 /* Trigger BufWinLeave for all windows, but only once per buffer. */
1282# if defined FEAT_WINDOWS
1283 for (tp = first_tabpage; tp != NULL; tp = next_tp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001284 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001285 next_tp = tp->tp_next;
1286 for (wp = (tp == curtab)
1287 ? firstwin : tp->tp_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001288 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001289 buf = wp->w_buffer;
1290 if (buf->b_changedtick != -1)
1291 {
1292 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname,
1293 buf->b_fname, FALSE, buf);
1294 buf->b_changedtick = -1; /* note that we did it already */
1295 /* start all over, autocommands may mess up the lists */
1296 next_tp = first_tabpage;
1297 break;
1298 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001299 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001300 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001301# else
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001302 apply_autocmds(EVENT_BUFWINLEAVE, curbuf, curbuf->b_fname,
1303 FALSE, curbuf);
Bram Moolenaarf740b292006-02-16 22:11:02 +00001304# endif
Bram Moolenaar33aec762006-01-22 23:30:12 +00001305
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001306 /* Trigger BufUnload for buffers that are loaded */
1307 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1308 if (buf->b_ml.ml_mfp != NULL)
1309 {
1310 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001311 FALSE, buf);
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001312 if (!buf_valid(buf)) /* autocmd may delete the buffer */
1313 break;
1314 }
1315 apply_autocmds(EVENT_VIMLEAVEPRE, NULL, NULL, FALSE, curbuf);
1316 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001317#endif
1318
1319#ifdef FEAT_VIMINFO
1320 if (*p_viminfo != NUL)
1321 /* Write out the registers, history, marks etc, to the viminfo file */
1322 write_viminfo(NULL, FALSE);
1323#endif
1324
1325#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001326 if (get_vim_var_nr(VV_DYING) <= 1)
1327 apply_autocmds(EVENT_VIMLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001328#endif
1329
Bram Moolenaar05159a02005-02-26 23:04:13 +00001330#ifdef FEAT_PROFILE
1331 profile_dump();
1332#endif
1333
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001334 if (did_emsg
1335#ifdef FEAT_GUI
1336 || (gui.in_use && msg_didany && p_verbose > 0)
1337#endif
1338 )
1339 {
1340 /* give the user a chance to read the (error) message */
1341 no_wait_return = FALSE;
1342 wait_return(FALSE);
1343 }
1344
1345#ifdef FEAT_AUTOCMD
1346 /* Position the cursor again, the autocommands may have moved it */
1347# ifdef FEAT_GUI
1348 if (!gui.in_use)
1349# endif
1350 windgoto((int)Rows - 1, 0);
1351#endif
1352
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001353#ifdef FEAT_MZSCHEME
1354 mzscheme_end();
1355#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001356#ifdef FEAT_TCL
1357 tcl_end();
1358#endif
1359#ifdef FEAT_RUBY
1360 ruby_end();
1361#endif
1362#ifdef FEAT_PYTHON
1363 python_end();
1364#endif
1365#ifdef FEAT_PERL
1366 perl_end();
1367#endif
1368#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
1369 iconv_end();
1370#endif
1371#ifdef FEAT_NETBEANS_INTG
1372 netbeans_end();
1373#endif
Bram Moolenaar02b06312007-09-06 15:39:22 +00001374#ifdef FEAT_CSCOPE
1375 cs_end();
1376#endif
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001377#ifdef FEAT_EVAL
1378 if (garbage_collect_at_exit)
1379 garbage_collect();
1380#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001381
1382 mch_exit(exitval);
1383}
1384
1385/*
1386 * Get a (optional) count for a Vim argument.
1387 */
1388 static int
1389get_number_arg(p, idx, def)
1390 char_u *p; /* pointer to argument */
1391 int *idx; /* index in argument, is incremented */
1392 int def; /* default value */
1393{
1394 if (vim_isdigit(p[*idx]))
1395 {
1396 def = atoi((char *)&(p[*idx]));
1397 while (vim_isdigit(p[*idx]))
1398 *idx = *idx + 1;
1399 }
1400 return def;
1401}
1402
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001403#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1404/*
1405 * Setup to use the current locale (for ctype() and many other things).
1406 */
1407 static void
1408init_locale()
1409{
1410 setlocale(LC_ALL, "");
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001411
Bram Moolenaarc6af8122010-05-21 12:04:55 +02001412# ifdef FEAT_GUI_GTK
1413 /* Tell Gtk not to change our locale settings. */
1414 gtk_disable_setlocale();
1415# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001416# if defined(FEAT_FLOAT) && defined(LC_NUMERIC)
1417 /* Make sure strtod() uses a decimal point, not a comma. */
1418 setlocale(LC_NUMERIC, "C");
1419# endif
1420
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001421# ifdef WIN32
1422 /* Apparently MS-Windows printf() may cause a crash when we give it 8-bit
1423 * text while it's expecting text in the current locale. This call avoids
1424 * that. */
1425 setlocale(LC_CTYPE, "C");
1426# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001427
1428# ifdef FEAT_GETTEXT
1429 {
1430 int mustfree = FALSE;
1431 char_u *p;
1432
1433# ifdef DYNAMIC_GETTEXT
1434 /* Initialize the gettext library */
1435 dyn_libintl_init(NULL);
1436# endif
1437 /* expand_env() doesn't work yet, because chartab[] is not initialized
1438 * yet, call vim_getenv() directly */
1439 p = vim_getenv((char_u *)"VIMRUNTIME", &mustfree);
1440 if (p != NULL && *p != NUL)
1441 {
Bram Moolenaar1ad2f132007-06-19 18:27:18 +00001442 vim_snprintf((char *)NameBuff, MAXPATHL, "%s/lang", p);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001443 bindtextdomain(VIMPACKAGE, (char *)NameBuff);
1444 }
1445 if (mustfree)
1446 vim_free(p);
1447 textdomain(VIMPACKAGE);
1448 }
1449# endif
1450}
1451#endif
1452
1453/*
1454 * Check for: [r][e][g][vi|vim|view][diff][ex[im]]
1455 * If the executable name starts with "r" we disable shell commands.
1456 * If the next character is "e" we run in Easy mode.
1457 * If the next character is "g" we run the GUI version.
1458 * If the next characters are "view" we start in readonly mode.
1459 * If the next characters are "diff" or "vimdiff" we start in diff mode.
1460 * If the next characters are "ex" we start in Ex mode. If it's followed
1461 * by "im" use improved Ex mode.
1462 */
1463 static void
1464parse_command_name(parmp)
1465 mparm_T *parmp;
1466{
1467 char_u *initstr;
1468
1469 initstr = gettail((char_u *)parmp->argv[0]);
1470
1471#ifdef MACOS_X_UNIX
1472 /* An issue has been seen when launching Vim in such a way that
1473 * $PWD/$ARGV[0] or $ARGV[0] is not the absolute path to the
1474 * executable or a symbolic link of it. Until this issue is resolved
1475 * we prohibit the GUI from being used.
1476 */
1477 if (STRCMP(initstr, parmp->argv[0]) == 0)
1478 disallow_gui = TRUE;
1479
1480 /* TODO: On MacOS X default to gui if argv[0] ends in:
Bram Moolenaar27dc1952006-03-15 23:06:44 +00001481 * /Vim.app/Contents/MacOS/Vim */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001482#endif
1483
1484#ifdef FEAT_EVAL
1485 set_vim_var_string(VV_PROGNAME, initstr, -1);
1486#endif
1487
1488 if (TOLOWER_ASC(initstr[0]) == 'r')
1489 {
1490 restricted = TRUE;
1491 ++initstr;
1492 }
1493
Bram Moolenaarad249fb2010-05-07 16:35:04 +02001494 /* Use evim mode for "evim" and "egvim", not for "editor". */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001495 if (TOLOWER_ASC(initstr[0]) == 'e'
1496 && (TOLOWER_ASC(initstr[1]) == 'v'
1497 || TOLOWER_ASC(initstr[1]) == 'g'))
1498 {
1499#ifdef FEAT_GUI
1500 gui.starting = TRUE;
1501#endif
1502 parmp->evim_mode = TRUE;
1503 ++initstr;
1504 }
1505
Bram Moolenaar806875d2008-09-18 18:57:10 +00001506 /* "gvim" starts the GUI. Also accept "Gvim" for MS-Windows. */
1507 if (TOLOWER_ASC(initstr[0]) == 'g')
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001508 {
1509 main_start_gui();
1510#ifdef FEAT_GUI
1511 ++initstr;
1512#endif
1513 }
1514
1515 if (STRNICMP(initstr, "view", 4) == 0)
1516 {
1517 readonlymode = TRUE;
1518 curbuf->b_p_ro = TRUE;
1519 p_uc = 10000; /* don't update very often */
1520 initstr += 4;
1521 }
1522 else if (STRNICMP(initstr, "vim", 3) == 0)
1523 initstr += 3;
1524
1525 /* Catch "[r][g]vimdiff" and "[r][g]viewdiff". */
1526 if (STRICMP(initstr, "diff") == 0)
1527 {
1528#ifdef FEAT_DIFF
1529 parmp->diff_mode = TRUE;
1530#else
1531 mch_errmsg(_("This Vim was not compiled with the diff feature."));
1532 mch_errmsg("\n");
1533 mch_exit(2);
1534#endif
1535 }
1536
1537 if (STRNICMP(initstr, "ex", 2) == 0)
1538 {
1539 if (STRNICMP(initstr + 2, "im", 2) == 0)
1540 exmode_active = EXMODE_VIM;
1541 else
1542 exmode_active = EXMODE_NORMAL;
1543 change_compatible(TRUE); /* set 'compatible' */
1544 }
1545}
1546
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001547/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00001548 * Get the name of the display, before gui_prepare() removes it from
1549 * argv[]. Used for the xterm-clipboard display.
1550 *
Bram Moolenaar78e17622007-08-30 10:26:19 +00001551 * Also find the --server... arguments and --socketid and --windowid
Bram Moolenaar58d98232005-07-23 22:25:46 +00001552 */
Bram Moolenaar58d98232005-07-23 22:25:46 +00001553 static void
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001554early_arg_scan(parmp)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00001555 mparm_T *parmp UNUSED;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001556{
Bram Moolenaar03005972008-11-20 13:12:36 +00001557#if defined(FEAT_XCLIPBOARD) || defined(FEAT_CLIENTSERVER) \
1558 || !defined(FEAT_NETBEANS_INTG)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001559 int argc = parmp->argc;
1560 char **argv = parmp->argv;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001561 int i;
1562
1563 for (i = 1; i < argc; i++)
1564 {
1565 if (STRCMP(argv[i], "--") == 0)
1566 break;
1567# ifdef FEAT_XCLIPBOARD
1568 else if (STRICMP(argv[i], "-display") == 0
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001569# if defined(FEAT_GUI_GTK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001570 || STRICMP(argv[i], "--display") == 0
1571# endif
1572 )
1573 {
1574 if (i == argc - 1)
1575 mainerr_arg_missing((char_u *)argv[i]);
1576 xterm_display = argv[++i];
1577 }
1578# endif
1579# ifdef FEAT_CLIENTSERVER
1580 else if (STRICMP(argv[i], "--servername") == 0)
1581 {
1582 if (i == argc - 1)
1583 mainerr_arg_missing((char_u *)argv[i]);
1584 parmp->serverName_arg = (char_u *)argv[++i];
1585 }
Bram Moolenaareb94e552006-03-11 21:35:11 +00001586 else if (STRICMP(argv[i], "--serverlist") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001587 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001588 else if (STRNICMP(argv[i], "--remote", 8) == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001589 {
1590 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001591# ifdef FEAT_GUI
1592 if (strstr(argv[i], "-wait") != 0)
1593 /* don't fork() when starting the GUI to edit files ourself */
1594 gui.dofork = FALSE;
1595# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001596 }
1597# endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001598
1599# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1600# ifdef FEAT_GUI_W32
1601 else if (STRICMP(argv[i], "--windowid") == 0)
1602# else
Bram Moolenaar58d98232005-07-23 22:25:46 +00001603 else if (STRICMP(argv[i], "--socketid") == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001604# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001605 {
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001606 long_u id;
1607 int count;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001608
1609 if (i == argc - 1)
1610 mainerr_arg_missing((char_u *)argv[i]);
1611 if (STRNICMP(argv[i+1], "0x", 2) == 0)
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001612 count = sscanf(&(argv[i + 1][2]), SCANF_HEX_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001613 else
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001614 count = sscanf(argv[i + 1], SCANF_DECIMAL_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001615 if (count != 1)
1616 mainerr(ME_INVALID_ARG, (char_u *)argv[i]);
1617 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00001618# ifdef FEAT_GUI_W32
1619 win_socket_id = id;
1620# else
1621 gtk_socket_id = id;
1622# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001623 i++;
1624 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001625# endif
1626# ifdef FEAT_GUI_GTK
Bram Moolenaar58d98232005-07-23 22:25:46 +00001627 else if (STRICMP(argv[i], "--echo-wid") == 0)
1628 echo_wid_arg = TRUE;
1629# endif
Bram Moolenaar03005972008-11-20 13:12:36 +00001630# ifndef FEAT_NETBEANS_INTG
1631 else if (strncmp(argv[i], "-nb", (size_t)3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +02001632 {
1633 mch_errmsg(_("'-nb' cannot be used: not enabled at compile time\n"));
1634 mch_exit(2);
1635 }
Bram Moolenaar03005972008-11-20 13:12:36 +00001636# endif
1637
Bram Moolenaar58d98232005-07-23 22:25:46 +00001638 }
1639#endif
1640}
1641
1642/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001643 * Scan the command line arguments.
1644 */
1645 static void
1646command_line_scan(parmp)
1647 mparm_T *parmp;
1648{
1649 int argc = parmp->argc;
1650 char **argv = parmp->argv;
1651 int argv_idx; /* index in argv[n][] */
1652 int had_minmin = FALSE; /* found "--" argument */
1653 int want_argument; /* option argument with argument */
1654 int c;
Bram Moolenaar231334e2005-07-25 20:46:57 +00001655 char_u *p = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001656 long n;
1657
1658 --argc;
1659 ++argv;
1660 argv_idx = 1; /* active option letter is argv[0][argv_idx] */
1661 while (argc > 0)
1662 {
1663 /*
1664 * "+" or "+{number}" or "+/{pat}" or "+{command}" argument.
1665 */
1666 if (argv[0][0] == '+' && !had_minmin)
1667 {
1668 if (parmp->n_commands >= MAX_ARG_CMDS)
1669 mainerr(ME_EXTRA_CMD, NULL);
1670 argv_idx = -1; /* skip to next argument */
1671 if (argv[0][1] == NUL)
1672 parmp->commands[parmp->n_commands++] = (char_u *)"$";
1673 else
1674 parmp->commands[parmp->n_commands++] = (char_u *)&(argv[0][1]);
1675 }
1676
1677 /*
1678 * Optional argument.
1679 */
1680 else if (argv[0][0] == '-' && !had_minmin)
1681 {
1682 want_argument = FALSE;
1683 c = argv[0][argv_idx++];
1684#ifdef VMS
1685 /*
1686 * VMS only uses upper case command lines. Interpret "-X" as "-x"
1687 * and "-/X" as "-X".
1688 */
1689 if (c == '/')
1690 {
1691 c = argv[0][argv_idx++];
1692 c = TOUPPER_ASC(c);
1693 }
1694 else
1695 c = TOLOWER_ASC(c);
1696#endif
1697 switch (c)
1698 {
1699 case NUL: /* "vim -" read from stdin */
1700 /* "ex -" silent mode */
1701 if (exmode_active)
1702 silent_mode = TRUE;
1703 else
1704 {
1705 if (parmp->edit_type != EDIT_NONE)
1706 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1707 parmp->edit_type = EDIT_STDIN;
1708 read_cmd_fd = 2; /* read from stderr instead of stdin */
1709 }
1710 argv_idx = -1; /* skip to next argument */
1711 break;
1712
1713 case '-': /* "--" don't take any more option arguments */
1714 /* "--help" give help message */
1715 /* "--version" give version message */
1716 /* "--literal" take files literally */
1717 /* "--nofork" don't fork */
1718 /* "--noplugin[s]" skip plugins */
1719 /* "--cmd <cmd>" execute cmd before vimrc */
1720 if (STRICMP(argv[0] + argv_idx, "help") == 0)
1721 usage();
1722 else if (STRICMP(argv[0] + argv_idx, "version") == 0)
1723 {
1724 Columns = 80; /* need to init Columns */
1725 info_message = TRUE; /* use mch_msg(), not mch_errmsg() */
1726 list_version();
1727 msg_putchar('\n');
1728 msg_didout = FALSE;
1729 mch_exit(0);
1730 }
1731 else if (STRNICMP(argv[0] + argv_idx, "literal", 7) == 0)
1732 {
1733#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
1734 parmp->literal = TRUE;
1735#endif
1736 }
1737 else if (STRNICMP(argv[0] + argv_idx, "nofork", 6) == 0)
1738 {
1739#ifdef FEAT_GUI
1740 gui.dofork = FALSE; /* don't fork() when starting GUI */
1741#endif
1742 }
1743 else if (STRNICMP(argv[0] + argv_idx, "noplugin", 8) == 0)
1744 p_lpl = FALSE;
1745 else if (STRNICMP(argv[0] + argv_idx, "cmd", 3) == 0)
1746 {
1747 want_argument = TRUE;
1748 argv_idx += 3;
1749 }
Bram Moolenaaref94eec2009-11-11 13:22:11 +00001750 else if (STRNICMP(argv[0] + argv_idx, "startuptime", 11) == 0)
1751 {
1752 want_argument = TRUE;
1753 argv_idx += 11;
1754 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001755#ifdef FEAT_CLIENTSERVER
1756 else if (STRNICMP(argv[0] + argv_idx, "serverlist", 10) == 0)
1757 ; /* already processed -- no arg */
1758 else if (STRNICMP(argv[0] + argv_idx, "servername", 10) == 0
1759 || STRNICMP(argv[0] + argv_idx, "serversend", 10) == 0)
1760 {
1761 /* already processed -- snatch the following arg */
1762 if (argc > 1)
1763 {
1764 --argc;
1765 ++argv;
1766 }
1767 }
1768#endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001769#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1770# ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001771 else if (STRNICMP(argv[0] + argv_idx, "socketid", 8) == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001772# else
1773 else if (STRNICMP(argv[0] + argv_idx, "windowid", 8) == 0)
1774# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001775 {
1776 /* already processed -- snatch the following arg */
1777 if (argc > 1)
1778 {
1779 --argc;
1780 ++argv;
1781 }
1782 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001783#endif
1784#ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001785 else if (STRNICMP(argv[0] + argv_idx, "echo-wid", 8) == 0)
1786 {
1787 /* already processed, skip */
1788 }
1789#endif
1790 else
1791 {
1792 if (argv[0][argv_idx])
1793 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
1794 had_minmin = TRUE;
1795 }
1796 if (!want_argument)
1797 argv_idx = -1; /* skip to next argument */
1798 break;
1799
1800 case 'A': /* "-A" start in Arabic mode */
1801#ifdef FEAT_ARABIC
1802 set_option_value((char_u *)"arabic", 1L, NULL, 0);
1803#else
1804 mch_errmsg(_(e_noarabic));
1805 mch_exit(2);
1806#endif
1807 break;
1808
1809 case 'b': /* "-b" binary mode */
Bram Moolenaar231334e2005-07-25 20:46:57 +00001810 /* Needs to be effective before expanding file names, because
1811 * for Win32 this makes us edit a shortcut file itself,
1812 * instead of the file it links to. */
1813 set_options_bin(curbuf->b_p_bin, 1, 0);
1814 curbuf->b_p_bin = 1; /* binary file I/O */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001815 break;
1816
1817 case 'C': /* "-C" Compatible */
1818 change_compatible(TRUE);
1819 break;
1820
1821 case 'e': /* "-e" Ex mode */
1822 exmode_active = EXMODE_NORMAL;
1823 break;
1824
1825 case 'E': /* "-E" Improved Ex mode */
1826 exmode_active = EXMODE_VIM;
1827 break;
1828
1829 case 'f': /* "-f" GUI: run in foreground. Amiga: open
1830 window directly, not with newcli */
1831#ifdef FEAT_GUI
1832 gui.dofork = FALSE; /* don't fork() when starting GUI */
1833#endif
1834 break;
1835
1836 case 'g': /* "-g" start GUI */
1837 main_start_gui();
1838 break;
1839
1840 case 'F': /* "-F" start in Farsi mode: rl + fkmap set */
1841#ifdef FEAT_FKMAP
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00001842 p_fkmap = TRUE;
1843 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001844#else
1845 mch_errmsg(_(e_nofarsi));
1846 mch_exit(2);
1847#endif
1848 break;
1849
1850 case 'h': /* "-h" give help message */
1851#ifdef FEAT_GUI_GNOME
1852 /* Tell usage() to exit for "gvim". */
1853 gui.starting = FALSE;
1854#endif
1855 usage();
1856 break;
1857
1858 case 'H': /* "-H" start in Hebrew mode: rl + hkmap set */
1859#ifdef FEAT_RIGHTLEFT
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00001860 p_hkmap = TRUE;
1861 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001862#else
1863 mch_errmsg(_(e_nohebrew));
1864 mch_exit(2);
1865#endif
1866 break;
1867
1868 case 'l': /* "-l" lisp mode, 'lisp' and 'showmatch' on */
1869#ifdef FEAT_LISP
1870 set_option_value((char_u *)"lisp", 1L, NULL, 0);
1871 p_sm = TRUE;
1872#endif
1873 break;
1874
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001875 case 'M': /* "-M" no changes or writing of files */
1876 reset_modifiable();
1877 /* FALLTHROUGH */
1878
1879 case 'm': /* "-m" no writing of files */
1880 p_write = FALSE;
1881 break;
1882
1883 case 'y': /* "-y" easy mode */
1884#ifdef FEAT_GUI
1885 gui.starting = TRUE; /* start GUI a bit later */
1886#endif
1887 parmp->evim_mode = TRUE;
1888 break;
1889
1890 case 'N': /* "-N" Nocompatible */
1891 change_compatible(FALSE);
1892 break;
1893
1894 case 'n': /* "-n" no swap file */
Bram Moolenaar67c53842010-05-22 18:28:27 +02001895#ifdef FEAT_NETBEANS_INTG
1896 /* checking for "-nb", netbeans parameters */
1897 if (argv[0][argv_idx] == 'b')
1898 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02001899 netbeansArg = argv[0];
1900 argv_idx = -1; /* skip to next argument */
1901 }
1902 else
1903#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001904 parmp->no_swap_file = TRUE;
1905 break;
1906
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001907 case 'p': /* "-p[N]" open N tab pages */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00001908#ifdef TARGET_API_MAC_OSX
1909 /* For some reason on MacOS X, an argument like:
1910 -psn_0_10223617 is passed in when invoke from Finder
1911 or with the 'open' command */
1912 if (argv[0][argv_idx] == 's')
1913 {
1914 argv_idx = -1; /* bypass full -psn */
1915 main_start_gui();
1916 break;
1917 }
1918#endif
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001919#ifdef FEAT_WINDOWS
1920 /* default is 0: open window for each file */
1921 parmp->window_count = get_number_arg((char_u *)argv[0],
1922 &argv_idx, 0);
1923 parmp->window_layout = WIN_TABS;
1924#endif
1925 break;
1926
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001927 case 'o': /* "-o[N]" open N horizontal split windows */
1928#ifdef FEAT_WINDOWS
1929 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00001930 parmp->window_count = get_number_arg((char_u *)argv[0],
1931 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001932 parmp->window_layout = WIN_HOR;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001933#endif
1934 break;
1935
1936 case 'O': /* "-O[N]" open N vertical split windows */
1937#if defined(FEAT_VERTSPLIT) && defined(FEAT_WINDOWS)
1938 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00001939 parmp->window_count = get_number_arg((char_u *)argv[0],
1940 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001941 parmp->window_layout = WIN_VER;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001942#endif
1943 break;
1944
1945#ifdef FEAT_QUICKFIX
1946 case 'q': /* "-q" QuickFix mode */
1947 if (parmp->edit_type != EDIT_NONE)
1948 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1949 parmp->edit_type = EDIT_QF;
1950 if (argv[0][argv_idx]) /* "-q{errorfile}" */
1951 {
1952 parmp->use_ef = (char_u *)argv[0] + argv_idx;
1953 argv_idx = -1;
1954 }
1955 else if (argc > 1) /* "-q {errorfile}" */
1956 want_argument = TRUE;
1957 break;
1958#endif
1959
1960 case 'R': /* "-R" readonly mode */
1961 readonlymode = TRUE;
1962 curbuf->b_p_ro = TRUE;
1963 p_uc = 10000; /* don't update very often */
1964 break;
1965
1966 case 'r': /* "-r" recovery mode */
1967 case 'L': /* "-L" recovery mode */
1968 recoverymode = 1;
1969 break;
1970
1971 case 's':
1972 if (exmode_active) /* "-s" silent (batch) mode */
1973 silent_mode = TRUE;
1974 else /* "-s {scriptin}" read from script file */
1975 want_argument = TRUE;
1976 break;
1977
1978 case 't': /* "-t {tag}" or "-t{tag}" jump to tag */
1979 if (parmp->edit_type != EDIT_NONE)
1980 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1981 parmp->edit_type = EDIT_TAG;
1982 if (argv[0][argv_idx]) /* "-t{tag}" */
1983 {
1984 parmp->tagname = (char_u *)argv[0] + argv_idx;
1985 argv_idx = -1;
1986 }
1987 else /* "-t {tag}" */
1988 want_argument = TRUE;
1989 break;
1990
1991#ifdef FEAT_EVAL
1992 case 'D': /* "-D" Debugging */
1993 parmp->use_debug_break_level = 9999;
1994 break;
1995#endif
1996#ifdef FEAT_DIFF
1997 case 'd': /* "-d" 'diff' */
1998# ifdef AMIGA
1999 /* check for "-dev {device}" */
2000 if (argv[0][argv_idx] == 'e' && argv[0][argv_idx + 1] == 'v')
2001 want_argument = TRUE;
2002 else
2003# endif
2004 parmp->diff_mode = TRUE;
2005 break;
2006#endif
2007 case 'V': /* "-V{N}" Verbose level */
2008 /* default is 10: a little bit verbose */
2009 p_verbose = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2010 if (argv[0][argv_idx] != NUL)
2011 {
2012 set_option_value((char_u *)"verbosefile", 0L,
2013 (char_u *)argv[0] + argv_idx, 0);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002014 argv_idx = (int)STRLEN(argv[0]);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002015 }
2016 break;
2017
2018 case 'v': /* "-v" Vi-mode (as if called "vi") */
2019 exmode_active = 0;
2020#ifdef FEAT_GUI
2021 gui.starting = FALSE; /* don't start GUI */
2022#endif
2023 break;
2024
2025 case 'w': /* "-w{number}" set window height */
2026 /* "-w {scriptout}" write to script */
2027 if (vim_isdigit(((char_u *)argv[0])[argv_idx]))
2028 {
2029 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2030 set_option_value((char_u *)"window", n, NULL, 0);
2031 break;
2032 }
2033 want_argument = TRUE;
2034 break;
2035
2036#ifdef FEAT_CRYPT
2037 case 'x': /* "-x" encrypted reading/writing of files */
2038 parmp->ask_for_key = TRUE;
2039 break;
2040#endif
2041
2042 case 'X': /* "-X" don't connect to X server */
2043#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
2044 x_no_connect = TRUE;
2045#endif
2046 break;
2047
2048 case 'Z': /* "-Z" restricted mode */
2049 restricted = TRUE;
2050 break;
2051
2052 case 'c': /* "-c{command}" or "-c {command}" execute
2053 command */
2054 if (argv[0][argv_idx] != NUL)
2055 {
2056 if (parmp->n_commands >= MAX_ARG_CMDS)
2057 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar231334e2005-07-25 20:46:57 +00002058 parmp->commands[parmp->n_commands++] = (char_u *)argv[0]
2059 + argv_idx;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002060 argv_idx = -1;
2061 break;
2062 }
2063 /*FALLTHROUGH*/
2064 case 'S': /* "-S {file}" execute Vim script */
2065 case 'i': /* "-i {viminfo}" use for viminfo */
2066#ifndef FEAT_DIFF
2067 case 'd': /* "-d {device}" device (for Amiga) */
2068#endif
2069 case 'T': /* "-T {terminal}" terminal name */
2070 case 'u': /* "-u {vimrc}" vim inits file */
2071 case 'U': /* "-U {gvimrc}" gvim inits file */
2072 case 'W': /* "-W {scriptout}" overwrite */
2073#ifdef FEAT_GUI_W32
2074 case 'P': /* "-P {parent title}" MDI parent */
2075#endif
2076 want_argument = TRUE;
2077 break;
2078
2079 default:
2080 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
2081 }
2082
2083 /*
2084 * Handle option arguments with argument.
2085 */
2086 if (want_argument)
2087 {
2088 /*
2089 * Check for garbage immediately after the option letter.
2090 */
2091 if (argv[0][argv_idx] != NUL)
2092 mainerr(ME_GARBAGE, (char_u *)argv[0]);
2093
2094 --argc;
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002095 if (argc < 1 && c != 'S') /* -S has an optional argument */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002096 mainerr_arg_missing((char_u *)argv[0]);
2097 ++argv;
2098 argv_idx = -1;
2099
2100 switch (c)
2101 {
2102 case 'c': /* "-c {command}" execute command */
2103 case 'S': /* "-S {file}" execute Vim script */
2104 if (parmp->n_commands >= MAX_ARG_CMDS)
2105 mainerr(ME_EXTRA_CMD, NULL);
2106 if (c == 'S')
2107 {
2108 char *a;
2109
2110 if (argc < 1)
2111 /* "-S" without argument: use default session file
2112 * name. */
2113 a = SESSION_FILE;
2114 else if (argv[0][0] == '-')
2115 {
2116 /* "-S" followed by another option: use default
2117 * session file name. */
2118 a = SESSION_FILE;
2119 ++argc;
2120 --argv;
2121 }
2122 else
2123 a = argv[0];
2124 p = alloc((unsigned)(STRLEN(a) + 4));
2125 if (p == NULL)
2126 mch_exit(2);
2127 sprintf((char *)p, "so %s", a);
2128 parmp->cmds_tofree[parmp->n_commands] = TRUE;
2129 parmp->commands[parmp->n_commands++] = p;
2130 }
2131 else
Bram Moolenaar231334e2005-07-25 20:46:57 +00002132 parmp->commands[parmp->n_commands++] =
2133 (char_u *)argv[0];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002134 break;
2135
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002136 case '-':
2137 if (argv[-1][2] == 'c')
2138 {
2139 /* "--cmd {command}" execute command */
2140 if (parmp->n_pre_commands >= MAX_ARG_CMDS)
2141 mainerr(ME_EXTRA_CMD, NULL);
2142 parmp->pre_commands[parmp->n_pre_commands++] =
Bram Moolenaar231334e2005-07-25 20:46:57 +00002143 (char_u *)argv[0];
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002144 }
2145 /* "--startuptime <file>" already handled */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002146 break;
2147
2148 /* case 'd': -d {device} is handled in mch_check_win() for the
2149 * Amiga */
2150
2151#ifdef FEAT_QUICKFIX
2152 case 'q': /* "-q {errorfile}" QuickFix mode */
2153 parmp->use_ef = (char_u *)argv[0];
2154 break;
2155#endif
2156
2157 case 'i': /* "-i {viminfo}" use for viminfo */
2158 use_viminfo = (char_u *)argv[0];
2159 break;
2160
2161 case 's': /* "-s {scriptin}" read from script file */
2162 if (scriptin[0] != NULL)
2163 {
2164scripterror:
2165 mch_errmsg(_("Attempt to open script file again: \""));
2166 mch_errmsg(argv[-1]);
2167 mch_errmsg(" ");
2168 mch_errmsg(argv[0]);
2169 mch_errmsg("\"\n");
2170 mch_exit(2);
2171 }
2172 if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL)
2173 {
2174 mch_errmsg(_("Cannot open for reading: \""));
2175 mch_errmsg(argv[0]);
2176 mch_errmsg("\"\n");
2177 mch_exit(2);
2178 }
2179 if (save_typebuf() == FAIL)
2180 mch_exit(2); /* out of memory */
2181 break;
2182
2183 case 't': /* "-t {tag}" */
2184 parmp->tagname = (char_u *)argv[0];
2185 break;
2186
2187 case 'T': /* "-T {terminal}" terminal name */
2188 /*
2189 * The -T term argument is always available and when
2190 * HAVE_TERMLIB is supported it overrides the environment
2191 * variable TERM.
2192 */
2193#ifdef FEAT_GUI
2194 if (term_is_gui((char_u *)argv[0]))
2195 gui.starting = TRUE; /* start GUI a bit later */
2196 else
2197#endif
2198 parmp->term = (char_u *)argv[0];
2199 break;
2200
2201 case 'u': /* "-u {vimrc}" vim inits file */
2202 parmp->use_vimrc = (char_u *)argv[0];
2203 break;
2204
2205 case 'U': /* "-U {gvimrc}" gvim inits file */
2206#ifdef FEAT_GUI
2207 use_gvimrc = (char_u *)argv[0];
2208#endif
2209 break;
2210
2211 case 'w': /* "-w {nr}" 'window' value */
2212 /* "-w {scriptout}" append to script file */
2213 if (vim_isdigit(*((char_u *)argv[0])))
2214 {
2215 argv_idx = 0;
2216 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2217 set_option_value((char_u *)"window", n, NULL, 0);
2218 argv_idx = -1;
2219 break;
2220 }
2221 /*FALLTHROUGH*/
2222 case 'W': /* "-W {scriptout}" overwrite script file */
2223 if (scriptout != NULL)
2224 goto scripterror;
2225 if ((scriptout = mch_fopen(argv[0],
2226 c == 'w' ? APPENDBIN : WRITEBIN)) == NULL)
2227 {
2228 mch_errmsg(_("Cannot open for script output: \""));
2229 mch_errmsg(argv[0]);
2230 mch_errmsg("\"\n");
2231 mch_exit(2);
2232 }
2233 break;
2234
2235#ifdef FEAT_GUI_W32
2236 case 'P': /* "-P {parent title}" MDI parent */
2237 gui_mch_set_parent(argv[0]);
2238 break;
2239#endif
2240 }
2241 }
2242 }
2243
2244 /*
2245 * File name argument.
2246 */
2247 else
2248 {
2249 argv_idx = -1; /* skip to next argument */
2250
2251 /* Check for only one type of editing. */
2252 if (parmp->edit_type != EDIT_NONE && parmp->edit_type != EDIT_FILE)
2253 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2254 parmp->edit_type = EDIT_FILE;
2255
2256#ifdef MSWIN
2257 /* Remember if the argument was a full path before changing
2258 * slashes to backslashes. */
2259 if (argv[0][0] != NUL && argv[0][1] == ':' && argv[0][2] == '\\')
2260 parmp->full_path = TRUE;
2261#endif
2262
2263 /* Add the file to the global argument list. */
2264 if (ga_grow(&global_alist.al_ga, 1) == FAIL
2265 || (p = vim_strsave((char_u *)argv[0])) == NULL)
2266 mch_exit(2);
2267#ifdef FEAT_DIFF
2268 if (parmp->diff_mode && mch_isdir(p) && GARGCOUNT > 0
2269 && !mch_isdir(alist_name(&GARGLIST[0])))
2270 {
2271 char_u *r;
2272
2273 r = concat_fnames(p, gettail(alist_name(&GARGLIST[0])), TRUE);
2274 if (r != NULL)
2275 {
2276 vim_free(p);
2277 p = r;
2278 }
2279 }
2280#endif
2281#if defined(__CYGWIN32__) && !defined(WIN32)
2282 /*
2283 * If vim is invoked by non-Cygwin tools, convert away any
2284 * DOS paths, so things like .swp files are created correctly.
2285 * Look for evidence of non-Cygwin paths before we bother.
2286 * This is only for when using the Unix files.
2287 */
Bram Moolenaarad249fb2010-05-07 16:35:04 +02002288 if (strpbrk(p, "\\:") != NULL && !path_with_url(p))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002289 {
2290 char posix_path[PATH_MAX];
2291
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002292# if CYGWIN_VERSION_DLL_MAJOR >= 1007
2293 cygwin_conv_path(CCP_WIN_A_TO_POSIX, p, posix_path, PATH_MAX);
2294# else
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002295 cygwin_conv_to_posix_path(p, posix_path);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002296# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002297 vim_free(p);
2298 p = vim_strsave(posix_path);
2299 if (p == NULL)
2300 mch_exit(2);
2301 }
2302#endif
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002303
2304#ifdef USE_FNAME_CASE
2305 /* Make the case of the file name match the actual file. */
2306 fname_case(p, 0);
2307#endif
2308
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002309 alist_add(&global_alist, p,
2310#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
Bram Moolenaar231334e2005-07-25 20:46:57 +00002311 parmp->literal ? 2 : 0 /* add buffer nr after exp. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002312#else
2313 2 /* add buffer number now and use curbuf */
2314#endif
2315 );
2316
2317#if defined(FEAT_MBYTE) && defined(WIN32)
2318 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002319 /* Remember this argument has been added to the argument list.
2320 * Needed when 'encoding' is changed. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002321 used_file_arg(argv[0], parmp->literal, parmp->full_path,
Bram Moolenaar688e5f72008-07-24 11:51:40 +00002322# ifdef FEAT_DIFF
2323 parmp->diff_mode
2324# else
2325 FALSE
2326# endif
2327 );
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002328 }
2329#endif
2330 }
2331
2332 /*
2333 * If there are no more letters after the current "-", go to next
2334 * argument. argv_idx is set to -1 when the current argument is to be
2335 * skipped.
2336 */
2337 if (argv_idx <= 0 || argv[0][argv_idx] == NUL)
2338 {
2339 --argc;
2340 ++argv;
2341 argv_idx = 1;
2342 }
2343 }
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002344
2345#ifdef FEAT_EVAL
2346 /* If there is a "+123" or "-c" command, set v:swapcommand to the first
2347 * one. */
2348 if (parmp->n_commands > 0)
2349 {
2350 p = alloc((unsigned)STRLEN(parmp->commands[0]) + 3);
2351 if (p != NULL)
2352 {
2353 sprintf((char *)p, ":%s\r", parmp->commands[0]);
2354 set_vim_var_string(VV_SWAPCOMMAND, p, -1);
2355 vim_free(p);
2356 }
2357 }
2358#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002359}
2360
2361/*
2362 * Print a warning if stdout is not a terminal.
2363 * When starting in Ex mode and commands come from a file, set Silent mode.
2364 */
2365 static void
2366check_tty(parmp)
2367 mparm_T *parmp;
2368{
2369 int input_isatty; /* is active input a terminal? */
2370
2371 input_isatty = mch_input_isatty();
2372 if (exmode_active)
2373 {
2374 if (!input_isatty)
2375 silent_mode = TRUE;
2376 }
2377 else if (parmp->want_full_screen && (!parmp->stdout_isatty || !input_isatty)
2378#ifdef FEAT_GUI
2379 /* don't want the delay when started from the desktop */
2380 && !gui.starting
2381#endif
2382 )
2383 {
2384#ifdef NBDEBUG
2385 /*
2386 * This shouldn't be necessary. But if I run netbeans with the log
2387 * output coming to the console and XOpenDisplay fails, I get vim
2388 * trying to start with input/output to my console tty. This fills my
2389 * input buffer so fast I can't even kill the process in under 2
Bram Moolenaar49325942007-05-10 19:19:59 +00002390 * minutes (and it beeps continuously the whole time :-)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002391 */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002392 if (netbeans_active() && (!parmp->stdout_isatty || !input_isatty))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002393 {
2394 mch_errmsg(_("Vim: Error: Failure to start gvim from NetBeans\n"));
2395 exit(1);
2396 }
2397#endif
2398 if (!parmp->stdout_isatty)
2399 mch_errmsg(_("Vim: Warning: Output is not to a terminal\n"));
2400 if (!input_isatty)
2401 mch_errmsg(_("Vim: Warning: Input is not from a terminal\n"));
2402 out_flush();
2403 if (scriptin[0] == NULL)
2404 ui_delay(2000L, TRUE);
2405 TIME_MSG("Warning delay");
2406 }
2407}
2408
2409/*
2410 * Read text from stdin.
2411 */
2412 static void
2413read_stdin()
2414{
2415 int i;
2416
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002417#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002418 /* When getting the ATTENTION prompt here, use a dialog */
2419 swap_exists_action = SEA_DIALOG;
2420#endif
2421 no_wait_return = TRUE;
2422 i = msg_didany;
2423 set_buflisted(TRUE);
2424 (void)open_buffer(TRUE, NULL); /* create memfile and read file */
2425 no_wait_return = FALSE;
2426 msg_didany = i;
2427 TIME_MSG("reading stdin");
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002428#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002429 check_swap_exists_action();
2430#endif
2431#if !(defined(AMIGA) || defined(MACOS))
2432 /*
2433 * Close stdin and dup it from stderr. Required for GPM to work
2434 * properly, and for running external commands.
2435 * Is there any other system that cannot do this?
2436 */
2437 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00002438 ignored = dup(2);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002439#endif
2440}
2441
2442/*
2443 * Create the requested number of windows and edit buffers in them.
2444 * Also does recovery if "recoverymode" set.
2445 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002446 static void
2447create_windows(parmp)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002448 mparm_T *parmp UNUSED;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002449{
2450#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002451 int dorewind;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002452 int done = 0;
2453
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002454 /*
2455 * Create the number of windows that was requested.
2456 */
2457 if (parmp->window_count == -1) /* was not set */
2458 parmp->window_count = 1;
2459 if (parmp->window_count == 0)
2460 parmp->window_count = GARGCOUNT;
2461 if (parmp->window_count > 1)
2462 {
2463 /* Don't change the windows if there was a command in .vimrc that
2464 * already split some windows */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002465 if (parmp->window_layout == 0)
2466 parmp->window_layout = WIN_HOR;
2467 if (parmp->window_layout == WIN_TABS)
2468 {
2469 parmp->window_count = make_tabpages(parmp->window_count);
2470 TIME_MSG("making tab pages");
2471 }
2472 else if (firstwin->w_next == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002473 {
2474 parmp->window_count = make_windows(parmp->window_count,
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002475 parmp->window_layout == WIN_VER);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002476 TIME_MSG("making windows");
2477 }
2478 else
2479 parmp->window_count = win_count();
2480 }
2481 else
2482 parmp->window_count = 1;
2483#endif
2484
2485 if (recoverymode) /* do recover */
2486 {
2487 msg_scroll = TRUE; /* scroll message up */
2488 ml_recover();
2489 if (curbuf->b_ml.ml_mfp == NULL) /* failed */
2490 getout(1);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002491 do_modelines(0); /* do modelines */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002492 }
2493 else
2494 {
2495 /*
2496 * Open a buffer for windows that don't have one yet.
2497 * Commands in the .vimrc might have loaded a file or split the window.
2498 * Watch out for autocommands that delete a window.
2499 */
2500#ifdef FEAT_AUTOCMD
2501 /*
2502 * Don't execute Win/Buf Enter/Leave autocommands here
2503 */
2504 ++autocmd_no_enter;
2505 ++autocmd_no_leave;
2506#endif
2507#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002508 dorewind = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002509 while (done++ < 1000)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002510 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002511 if (dorewind)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002512 {
2513 if (parmp->window_layout == WIN_TABS)
2514 goto_tabpage(1);
2515 else
2516 curwin = firstwin;
2517 }
2518 else if (parmp->window_layout == WIN_TABS)
2519 {
2520 if (curtab->tp_next == NULL)
2521 break;
2522 goto_tabpage(0);
2523 }
2524 else
2525 {
2526 if (curwin->w_next == NULL)
2527 break;
2528 curwin = curwin->w_next;
2529 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002530 dorewind = FALSE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002531#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002532 curbuf = curwin->w_buffer;
2533 if (curbuf->b_ml.ml_mfp == NULL)
2534 {
2535#ifdef FEAT_FOLDING
2536 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2537 if (p_fdls >= 0)
2538 curwin->w_p_fdl = p_fdls;
2539#endif
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002540#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002541 /* When getting the ATTENTION prompt here, use a dialog */
2542 swap_exists_action = SEA_DIALOG;
2543#endif
2544 set_buflisted(TRUE);
2545 (void)open_buffer(FALSE, NULL); /* create memfile, read file */
2546
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002547#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar84212822006-11-07 21:59:47 +00002548 if (swap_exists_action == SEA_QUIT)
2549 {
2550 if (got_int || only_one_window())
2551 {
2552 /* abort selected or quit and only one window */
2553 did_emsg = FALSE; /* avoid hit-enter prompt */
2554 getout(1);
2555 }
2556 /* We can't close the window, it would disturb what
2557 * happens next. Clear the file name and set the arg
2558 * index to -1 to delete it later. */
2559 setfname(curbuf, NULL, NULL, FALSE);
2560 curwin->w_arg_idx = -1;
2561 swap_exists_action = SEA_NONE;
2562 }
2563 else
2564 handle_swap_exists(NULL);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002565#endif
2566#ifdef FEAT_AUTOCMD
Bram Moolenaar89d40322006-08-29 15:30:07 +00002567 dorewind = TRUE; /* start again */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002568#endif
2569 }
2570#ifdef FEAT_WINDOWS
2571 ui_breakcheck();
2572 if (got_int)
2573 {
2574 (void)vgetc(); /* only break the file loading, not the rest */
2575 break;
2576 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002577 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002578#endif
2579#ifdef FEAT_WINDOWS
2580 if (parmp->window_layout == WIN_TABS)
2581 goto_tabpage(1);
2582 else
2583 curwin = firstwin;
2584 curbuf = curwin->w_buffer;
2585#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002586#ifdef FEAT_AUTOCMD
2587 --autocmd_no_enter;
2588 --autocmd_no_leave;
2589#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002590 }
2591}
2592
2593#ifdef FEAT_WINDOWS
2594 /*
2595 * If opened more than one window, start editing files in the other
2596 * windows. make_windows() has already opened the windows.
2597 */
2598 static void
2599edit_buffers(parmp)
2600 mparm_T *parmp;
2601{
2602 int arg_idx; /* index in argument list */
2603 int i;
Bram Moolenaar84212822006-11-07 21:59:47 +00002604 int advance = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002605
2606# ifdef FEAT_AUTOCMD
2607 /*
2608 * Don't execute Win/Buf Enter/Leave autocommands here
2609 */
2610 ++autocmd_no_enter;
2611 ++autocmd_no_leave;
2612# endif
Bram Moolenaar84212822006-11-07 21:59:47 +00002613
2614 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2615 if (curwin->w_arg_idx == -1)
2616 {
2617 win_close(curwin, TRUE);
2618 advance = FALSE;
2619 }
2620
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002621 arg_idx = 1;
2622 for (i = 1; i < parmp->window_count; ++i)
2623 {
Bram Moolenaar84212822006-11-07 21:59:47 +00002624 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2625 if (curwin->w_arg_idx == -1)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002626 {
Bram Moolenaar84212822006-11-07 21:59:47 +00002627 ++arg_idx;
2628 win_close(curwin, TRUE);
2629 advance = FALSE;
2630 continue;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002631 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002632
Bram Moolenaar84212822006-11-07 21:59:47 +00002633 if (advance)
2634 {
2635 if (parmp->window_layout == WIN_TABS)
2636 {
2637 if (curtab->tp_next == NULL) /* just checking */
2638 break;
2639 goto_tabpage(0);
2640 }
2641 else
2642 {
2643 if (curwin->w_next == NULL) /* just checking */
2644 break;
2645 win_enter(curwin->w_next, FALSE);
2646 }
2647 }
2648 advance = TRUE;
2649
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002650 /* Only open the file if there is no file in this window yet (that can
Bram Moolenaar84212822006-11-07 21:59:47 +00002651 * happen when .vimrc contains ":sall"). */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002652 if (curbuf == firstwin->w_buffer || curbuf->b_ffname == NULL)
2653 {
2654 curwin->w_arg_idx = arg_idx;
Bram Moolenaar84212822006-11-07 21:59:47 +00002655 /* Edit file from arg list, if there is one. When "Quit" selected
2656 * at the ATTENTION prompt close the window. */
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002657# ifdef HAS_SWAP_EXISTS_ACTION
2658 swap_exists_did_quit = FALSE;
2659# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002660 (void)do_ecmd(0, arg_idx < GARGCOUNT
2661 ? alist_name(&GARGLIST[arg_idx]) : NULL,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002662 NULL, NULL, ECMD_LASTL, ECMD_HIDE, curwin);
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002663# ifdef HAS_SWAP_EXISTS_ACTION
2664 if (swap_exists_did_quit)
Bram Moolenaar84212822006-11-07 21:59:47 +00002665 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002666 /* abort or quit selected */
Bram Moolenaar84212822006-11-07 21:59:47 +00002667 if (got_int || only_one_window())
2668 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002669 /* abort selected and only one window */
Bram Moolenaar84212822006-11-07 21:59:47 +00002670 did_emsg = FALSE; /* avoid hit-enter prompt */
2671 getout(1);
2672 }
2673 win_close(curwin, TRUE);
2674 advance = FALSE;
2675 }
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002676# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002677 if (arg_idx == GARGCOUNT - 1)
2678 arg_had_last = TRUE;
2679 ++arg_idx;
2680 }
2681 ui_breakcheck();
2682 if (got_int)
2683 {
2684 (void)vgetc(); /* only break the file loading, not the rest */
2685 break;
2686 }
2687 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002688
2689 if (parmp->window_layout == WIN_TABS)
2690 goto_tabpage(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002691# ifdef FEAT_AUTOCMD
2692 --autocmd_no_enter;
2693# endif
2694 win_enter(firstwin, FALSE); /* back to first window */
2695# ifdef FEAT_AUTOCMD
2696 --autocmd_no_leave;
2697# endif
2698 TIME_MSG("editing files in windows");
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002699 if (parmp->window_count > 1 && parmp->window_layout != WIN_TABS)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002700 win_equal(curwin, FALSE, 'b'); /* adjust heights */
2701}
2702#endif /* FEAT_WINDOWS */
2703
2704/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002705 * Execute the commands from --cmd arguments "cmds[cnt]".
2706 */
2707 static void
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002708exe_pre_commands(parmp)
2709 mparm_T *parmp;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002710{
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002711 char_u **cmds = parmp->pre_commands;
2712 int cnt = parmp->n_pre_commands;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002713 int i;
2714
2715 if (cnt > 0)
2716 {
2717 curwin->w_cursor.lnum = 0; /* just in case.. */
2718 sourcing_name = (char_u *)_("pre-vimrc command line");
2719# ifdef FEAT_EVAL
2720 current_SID = SID_CMDARG;
2721# endif
2722 for (i = 0; i < cnt; ++i)
2723 do_cmdline_cmd(cmds[i]);
2724 sourcing_name = NULL;
2725# ifdef FEAT_EVAL
2726 current_SID = 0;
2727# endif
2728 TIME_MSG("--cmd commands");
2729 }
2730}
2731
2732/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002733 * Execute "+", "-c" and "-S" arguments.
2734 */
2735 static void
2736exe_commands(parmp)
2737 mparm_T *parmp;
2738{
2739 int i;
2740
2741 /*
2742 * We start commands on line 0, make "vim +/pat file" match a
2743 * pattern on line 1. But don't move the cursor when an autocommand
2744 * with g`" was used.
2745 */
2746 msg_scroll = TRUE;
2747 if (parmp->tagname == NULL && curwin->w_cursor.lnum <= 1)
2748 curwin->w_cursor.lnum = 0;
2749 sourcing_name = (char_u *)"command line";
2750#ifdef FEAT_EVAL
2751 current_SID = SID_CARG;
2752#endif
2753 for (i = 0; i < parmp->n_commands; ++i)
2754 {
2755 do_cmdline_cmd(parmp->commands[i]);
2756 if (parmp->cmds_tofree[i])
2757 vim_free(parmp->commands[i]);
2758 }
2759 sourcing_name = NULL;
2760#ifdef FEAT_EVAL
2761 current_SID = 0;
2762#endif
2763 if (curwin->w_cursor.lnum == 0)
2764 curwin->w_cursor.lnum = 1;
2765
2766 if (!exmode_active)
2767 msg_scroll = FALSE;
2768
2769#ifdef FEAT_QUICKFIX
2770 /* When started with "-q errorfile" jump to first error again. */
2771 if (parmp->edit_type == EDIT_QF)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002772 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002773#endif
2774 TIME_MSG("executing command arguments");
2775}
2776
2777/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002778 * Source startup scripts.
2779 */
2780 static void
2781source_startup_scripts(parmp)
2782 mparm_T *parmp;
2783{
2784 int i;
2785
2786 /*
2787 * For "evim" source evim.vim first of all, so that the user can overrule
2788 * any things he doesn't like.
2789 */
2790 if (parmp->evim_mode)
2791 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002792 (void)do_source((char_u *)EVIM_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002793 TIME_MSG("source evim file");
2794 }
2795
2796 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002797 * If -u argument given, use only the initializations from that file and
Bram Moolenaar58d98232005-07-23 22:25:46 +00002798 * nothing else.
2799 */
2800 if (parmp->use_vimrc != NULL)
2801 {
Bram Moolenaar231334e2005-07-25 20:46:57 +00002802 if (STRCMP(parmp->use_vimrc, "NONE") == 0
2803 || STRCMP(parmp->use_vimrc, "NORC") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002804 {
2805#ifdef FEAT_GUI
2806 if (use_gvimrc == NULL) /* don't load gvimrc either */
2807 use_gvimrc = parmp->use_vimrc;
2808#endif
2809 if (parmp->use_vimrc[2] == 'N')
2810 p_lpl = FALSE; /* don't load plugins either */
2811 }
2812 else
2813 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002814 if (do_source(parmp->use_vimrc, FALSE, DOSO_NONE) != OK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002815 EMSG2(_("E282: Cannot read from \"%s\""), parmp->use_vimrc);
2816 }
2817 }
2818 else if (!silent_mode)
2819 {
2820#ifdef AMIGA
2821 struct Process *proc = (struct Process *)FindTask(0L);
2822 APTR save_winptr = proc->pr_WindowPtr;
2823
2824 /* Avoid a requester here for a volume that doesn't exist. */
2825 proc->pr_WindowPtr = (APTR)-1L;
2826#endif
2827
2828 /*
2829 * Get system wide defaults, if the file name is defined.
2830 */
2831#ifdef SYS_VIMRC_FILE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002832 (void)do_source((char_u *)SYS_VIMRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002833#endif
Bram Moolenaar1056d982006-03-09 22:37:52 +00002834#ifdef MACOS_X
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002835 (void)do_source((char_u *)"$VIMRUNTIME/macmap.vim", FALSE, DOSO_NONE);
Bram Moolenaar1056d982006-03-09 22:37:52 +00002836#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00002837
2838 /*
2839 * Try to read initialization commands from the following places:
2840 * - environment variable VIMINIT
2841 * - user vimrc file (s:.vimrc for Amiga, ~/.vimrc otherwise)
2842 * - second user vimrc file ($VIM/.vimrc for Dos)
2843 * - environment variable EXINIT
2844 * - user exrc file (s:.exrc for Amiga, ~/.exrc otherwise)
2845 * - second user exrc file ($VIM/.exrc for Dos)
2846 * The first that exists is used, the rest is ignored.
2847 */
2848 if (process_env((char_u *)"VIMINIT", TRUE) != OK)
2849 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002850 if (do_source((char_u *)USR_VIMRC_FILE, TRUE, DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00002851#ifdef USR_VIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002852 && do_source((char_u *)USR_VIMRC_FILE2, TRUE,
2853 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00002854#endif
2855#ifdef USR_VIMRC_FILE3
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002856 && do_source((char_u *)USR_VIMRC_FILE3, TRUE,
2857 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00002858#endif
2859 && process_env((char_u *)"EXINIT", FALSE) == FAIL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002860 && do_source((char_u *)USR_EXRC_FILE, FALSE, DOSO_NONE) == FAIL)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002861 {
2862#ifdef USR_EXRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002863 (void)do_source((char_u *)USR_EXRC_FILE2, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002864#endif
2865 }
2866 }
2867
2868 /*
2869 * Read initialization commands from ".vimrc" or ".exrc" in current
2870 * directory. This is only done if the 'exrc' option is set.
2871 * Because of security reasons we disallow shell and write commands
2872 * now, except for unix if the file is owned by the user or 'secure'
2873 * option has been reset in environment of global ".exrc" or ".vimrc".
2874 * Only do this if VIMRC_FILE is not the same as USR_VIMRC_FILE or
2875 * SYS_VIMRC_FILE.
2876 */
2877 if (p_exrc)
2878 {
2879#if defined(UNIX) || defined(VMS)
2880 /* If ".vimrc" file is not owned by user, set 'secure' mode. */
2881 if (!file_owned(VIMRC_FILE))
2882#endif
2883 secure = p_secure;
2884
2885 i = FAIL;
2886 if (fullpathcmp((char_u *)USR_VIMRC_FILE,
2887 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
2888#ifdef USR_VIMRC_FILE2
2889 && fullpathcmp((char_u *)USR_VIMRC_FILE2,
2890 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
2891#endif
2892#ifdef USR_VIMRC_FILE3
2893 && fullpathcmp((char_u *)USR_VIMRC_FILE3,
2894 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
2895#endif
2896#ifdef SYS_VIMRC_FILE
2897 && fullpathcmp((char_u *)SYS_VIMRC_FILE,
2898 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
2899#endif
2900 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002901 i = do_source((char_u *)VIMRC_FILE, TRUE, DOSO_VIMRC);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002902
2903 if (i == FAIL)
2904 {
2905#if defined(UNIX) || defined(VMS)
2906 /* if ".exrc" is not owned by user set 'secure' mode */
2907 if (!file_owned(EXRC_FILE))
2908 secure = p_secure;
2909 else
2910 secure = 0;
2911#endif
2912 if ( fullpathcmp((char_u *)USR_EXRC_FILE,
2913 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
2914#ifdef USR_EXRC_FILE2
2915 && fullpathcmp((char_u *)USR_EXRC_FILE2,
2916 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
2917#endif
2918 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002919 (void)do_source((char_u *)EXRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002920 }
2921 }
2922 if (secure == 2)
2923 need_wait_return = TRUE;
2924 secure = 0;
2925#ifdef AMIGA
2926 proc->pr_WindowPtr = save_winptr;
2927#endif
2928 }
2929 TIME_MSG("sourcing vimrc file(s)");
2930}
2931
2932/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002933 * Setup to start using the GUI. Exit with an error when not available.
2934 */
2935 static void
2936main_start_gui()
2937{
2938#ifdef FEAT_GUI
2939 gui.starting = TRUE; /* start GUI a bit later */
2940#else
2941 mch_errmsg(_(e_nogvim));
2942 mch_errmsg("\n");
2943 mch_exit(2);
2944#endif
2945}
2946
2947/*
Bram Moolenaar49325942007-05-10 19:19:59 +00002948 * Get an environment variable, and execute it as Ex commands.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002949 * Returns FAIL if the environment variable was not executed, OK otherwise.
2950 */
2951 int
2952process_env(env, is_viminit)
2953 char_u *env;
2954 int is_viminit; /* when TRUE, called for VIMINIT */
2955{
2956 char_u *initstr;
2957 char_u *save_sourcing_name;
2958 linenr_T save_sourcing_lnum;
2959#ifdef FEAT_EVAL
2960 scid_T save_sid;
2961#endif
2962
2963 if ((initstr = mch_getenv(env)) != NULL && *initstr != NUL)
2964 {
2965 if (is_viminit)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002966 vimrc_found(NULL, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002967 save_sourcing_name = sourcing_name;
2968 save_sourcing_lnum = sourcing_lnum;
2969 sourcing_name = env;
2970 sourcing_lnum = 0;
2971#ifdef FEAT_EVAL
2972 save_sid = current_SID;
2973 current_SID = SID_ENV;
2974#endif
2975 do_cmdline_cmd(initstr);
2976 sourcing_name = save_sourcing_name;
2977 sourcing_lnum = save_sourcing_lnum;
2978#ifdef FEAT_EVAL
2979 current_SID = save_sid;;
2980#endif
2981 return OK;
2982 }
2983 return FAIL;
2984}
2985
2986#if defined(UNIX) || defined(VMS)
2987/*
2988 * Return TRUE if we are certain the user owns the file "fname".
2989 * Used for ".vimrc" and ".exrc".
2990 * Use both stat() and lstat() for extra security.
2991 */
2992 static int
2993file_owned(fname)
2994 char *fname;
2995{
2996 struct stat s;
2997# ifdef UNIX
2998 uid_t uid = getuid();
2999# else /* VMS */
3000 uid_t uid = ((getgid() << 16) | getuid());
3001# endif
3002
3003 return !(mch_stat(fname, &s) != 0 || s.st_uid != uid
3004# ifdef HAVE_LSTAT
3005 || mch_lstat(fname, &s) != 0 || s.st_uid != uid
3006# endif
3007 );
3008}
3009#endif
3010
3011/*
3012 * Give an error message main_errors["n"] and exit.
3013 */
3014 static void
3015mainerr(n, str)
3016 int n; /* one of the ME_ defines */
3017 char_u *str; /* extra argument or NULL */
3018{
3019#if defined(UNIX) || defined(__EMX__) || defined(VMS)
3020 reset_signals(); /* kill us with CTRL-C here, if you like */
3021#endif
3022
3023 mch_errmsg(longVersion);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003024 mch_errmsg("\n");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003025 mch_errmsg(_(main_errors[n]));
3026 if (str != NULL)
3027 {
3028 mch_errmsg(": \"");
3029 mch_errmsg((char *)str);
3030 mch_errmsg("\"");
3031 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003032 mch_errmsg(_("\nMore info with: \"vim -h\"\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003033
3034 mch_exit(1);
3035}
3036
3037 void
3038mainerr_arg_missing(str)
3039 char_u *str;
3040{
3041 mainerr(ME_ARG_MISSING, str);
3042}
3043
3044/*
3045 * print a message with three spaces prepended and '\n' appended.
3046 */
3047 static void
3048main_msg(s)
3049 char *s;
3050{
3051 mch_msg(" ");
3052 mch_msg(s);
3053 mch_msg("\n");
3054}
3055
3056/*
3057 * Print messages for "vim -h" or "vim --help" and exit.
3058 */
3059 static void
3060usage()
3061{
3062 int i;
3063 static char *(use[]) =
3064 {
3065 N_("[file ..] edit specified file(s)"),
3066 N_("- read text from stdin"),
3067 N_("-t tag edit file where tag is defined"),
3068#ifdef FEAT_QUICKFIX
3069 N_("-q [errorfile] edit file with first error")
3070#endif
3071 };
3072
3073#if defined(UNIX) || defined(__EMX__) || defined(VMS)
3074 reset_signals(); /* kill us with CTRL-C here, if you like */
3075#endif
3076
3077 mch_msg(longVersion);
3078 mch_msg(_("\n\nusage:"));
3079 for (i = 0; ; ++i)
3080 {
3081 mch_msg(_(" vim [arguments] "));
3082 mch_msg(_(use[i]));
3083 if (i == (sizeof(use) / sizeof(char_u *)) - 1)
3084 break;
3085 mch_msg(_("\n or:"));
3086 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003087#ifdef VMS
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003088 mch_msg(_("\nWhere case is ignored prepend / to make flag upper case"));
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003089#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003090
3091 mch_msg(_("\n\nArguments:\n"));
3092 main_msg(_("--\t\t\tOnly file names after this"));
3093#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
3094 main_msg(_("--literal\t\tDon't expand wildcards"));
3095#endif
3096#ifdef FEAT_OLE
3097 main_msg(_("-register\t\tRegister this gvim for OLE"));
3098 main_msg(_("-unregister\t\tUnregister gvim for OLE"));
3099#endif
3100#ifdef FEAT_GUI
3101 main_msg(_("-g\t\t\tRun using GUI (like \"gvim\")"));
3102 main_msg(_("-f or --nofork\tForeground: Don't fork when starting GUI"));
3103#endif
3104 main_msg(_("-v\t\t\tVi mode (like \"vi\")"));
3105 main_msg(_("-e\t\t\tEx mode (like \"ex\")"));
3106 main_msg(_("-s\t\t\tSilent (batch) mode (only for \"ex\")"));
3107#ifdef FEAT_DIFF
3108 main_msg(_("-d\t\t\tDiff mode (like \"vimdiff\")"));
3109#endif
3110 main_msg(_("-y\t\t\tEasy mode (like \"evim\", modeless)"));
3111 main_msg(_("-R\t\t\tReadonly mode (like \"view\")"));
3112 main_msg(_("-Z\t\t\tRestricted mode (like \"rvim\")"));
3113 main_msg(_("-m\t\t\tModifications (writing files) not allowed"));
3114 main_msg(_("-M\t\t\tModifications in text not allowed"));
3115 main_msg(_("-b\t\t\tBinary mode"));
3116#ifdef FEAT_LISP
3117 main_msg(_("-l\t\t\tLisp mode"));
3118#endif
3119 main_msg(_("-C\t\t\tCompatible with Vi: 'compatible'"));
3120 main_msg(_("-N\t\t\tNot fully Vi compatible: 'nocompatible'"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003121 main_msg(_("-V[N][fname]\t\tBe verbose [level N] [log messages to fname]"));
3122#ifdef FEAT_EVAL
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003123 main_msg(_("-D\t\t\tDebugging mode"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003124#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003125 main_msg(_("-n\t\t\tNo swap file, use memory only"));
3126 main_msg(_("-r\t\t\tList swap files and exit"));
3127 main_msg(_("-r (with file name)\tRecover crashed session"));
3128 main_msg(_("-L\t\t\tSame as -r"));
3129#ifdef AMIGA
3130 main_msg(_("-f\t\t\tDon't use newcli to open window"));
3131 main_msg(_("-dev <device>\t\tUse <device> for I/O"));
3132#endif
3133#ifdef FEAT_ARABIC
3134 main_msg(_("-A\t\t\tstart in Arabic mode"));
3135#endif
3136#ifdef FEAT_RIGHTLEFT
3137 main_msg(_("-H\t\t\tStart in Hebrew mode"));
3138#endif
3139#ifdef FEAT_FKMAP
3140 main_msg(_("-F\t\t\tStart in Farsi mode"));
3141#endif
3142 main_msg(_("-T <terminal>\tSet terminal type to <terminal>"));
3143 main_msg(_("-u <vimrc>\t\tUse <vimrc> instead of any .vimrc"));
3144#ifdef FEAT_GUI
3145 main_msg(_("-U <gvimrc>\t\tUse <gvimrc> instead of any .gvimrc"));
3146#endif
3147 main_msg(_("--noplugin\t\tDon't load plugin scripts"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003148#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003149 main_msg(_("-p[N]\t\tOpen N tab pages (default: one for each file)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003150 main_msg(_("-o[N]\t\tOpen N windows (default: one for each file)"));
3151 main_msg(_("-O[N]\t\tLike -o but split vertically"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003152#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003153 main_msg(_("+\t\t\tStart at end of file"));
3154 main_msg(_("+<lnum>\t\tStart at line <lnum>"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003155 main_msg(_("--cmd <command>\tExecute <command> before loading any vimrc file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003156 main_msg(_("-c <command>\t\tExecute <command> after loading the first file"));
3157 main_msg(_("-S <session>\t\tSource file <session> after loading the first file"));
3158 main_msg(_("-s <scriptin>\tRead Normal mode commands from file <scriptin>"));
3159 main_msg(_("-w <scriptout>\tAppend all typed commands to file <scriptout>"));
3160 main_msg(_("-W <scriptout>\tWrite all typed commands to file <scriptout>"));
3161#ifdef FEAT_CRYPT
3162 main_msg(_("-x\t\t\tEdit encrypted files"));
3163#endif
3164#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
3165# if defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)
3166 main_msg(_("-display <display>\tConnect vim to this particular X-server"));
3167# endif
3168 main_msg(_("-X\t\t\tDo not connect to X server"));
3169#endif
3170#ifdef FEAT_CLIENTSERVER
3171 main_msg(_("--remote <files>\tEdit <files> in a Vim server if possible"));
3172 main_msg(_("--remote-silent <files> Same, don't complain if there is no server"));
3173 main_msg(_("--remote-wait <files> As --remote but wait for files to have been edited"));
3174 main_msg(_("--remote-wait-silent <files> Same, don't complain if there is no server"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003175# ifdef FEAT_WINDOWS
Bram Moolenaar82ad3242008-01-11 19:26:36 +00003176 main_msg(_("--remote-tab[-wait][-silent] <files> As --remote but use tab page per file"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003177# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003178 main_msg(_("--remote-send <keys>\tSend <keys> to a Vim server and exit"));
3179 main_msg(_("--remote-expr <expr>\tEvaluate <expr> in a Vim server and print result"));
3180 main_msg(_("--serverlist\t\tList available Vim server names and exit"));
3181 main_msg(_("--servername <name>\tSend to/become the Vim server <name>"));
3182#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003183#ifdef STARTUPTIME
Bram Moolenaar34ef52d2009-11-17 11:31:25 +00003184 main_msg(_("--startuptime <file>\tWrite startup timing messages to <file>"));
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003185#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003186#ifdef FEAT_VIMINFO
3187 main_msg(_("-i <viminfo>\t\tUse <viminfo> instead of .viminfo"));
3188#endif
3189 main_msg(_("-h or --help\tPrint Help (this message) and exit"));
3190 main_msg(_("--version\t\tPrint version information and exit"));
3191
3192#ifdef FEAT_GUI_X11
3193# ifdef FEAT_GUI_MOTIF
3194 mch_msg(_("\nArguments recognised by gvim (Motif version):\n"));
3195# else
3196# ifdef FEAT_GUI_ATHENA
3197# ifdef FEAT_GUI_NEXTAW
3198 mch_msg(_("\nArguments recognised by gvim (neXtaw version):\n"));
3199# else
3200 mch_msg(_("\nArguments recognised by gvim (Athena version):\n"));
3201# endif
3202# endif
3203# endif
3204 main_msg(_("-display <display>\tRun vim on <display>"));
3205 main_msg(_("-iconic\t\tStart vim iconified"));
3206# if 0
3207 main_msg(_("-name <name>\t\tUse resource as if vim was <name>"));
3208 mch_msg(_("\t\t\t (Unimplemented)\n"));
3209# endif
3210 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
3211 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
3212 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3213 main_msg(_("-boldfont <font>\tUse <font> for bold text"));
3214 main_msg(_("-italicfont <font>\tUse <font> for italic text"));
3215 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3216 main_msg(_("-borderwidth <width>\tUse a border width of <width> (also: -bw)"));
3217 main_msg(_("-scrollbarwidth <width> Use a scrollbar width of <width> (also: -sw)"));
3218# ifdef FEAT_GUI_ATHENA
3219 main_msg(_("-menuheight <height>\tUse a menu bar height of <height> (also: -mh)"));
3220# endif
3221 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3222 main_msg(_("+reverse\t\tDon't use reverse video (also: +rv)"));
3223 main_msg(_("-xrm <resource>\tSet the specified resource"));
3224#endif /* FEAT_GUI_X11 */
3225#if defined(FEAT_GUI) && defined(RISCOS)
3226 mch_msg(_("\nArguments recognised by gvim (RISC OS version):\n"));
3227 main_msg(_("--columns <number>\tInitial width of window in columns"));
3228 main_msg(_("--rows <number>\tInitial height of window in rows"));
3229#endif
3230#ifdef FEAT_GUI_GTK
3231 mch_msg(_("\nArguments recognised by gvim (GTK+ version):\n"));
3232 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3233 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3234 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3235 main_msg(_("-display <display>\tRun vim on <display> (also: --display)"));
3236# ifdef HAVE_GTK2
3237 main_msg(_("--role <role>\tSet a unique role to identify the main window"));
3238# endif
3239 main_msg(_("--socketid <xid>\tOpen Vim inside another GTK widget"));
3240#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003241#ifdef FEAT_GUI_W32
3242 main_msg(_("-P <parent title>\tOpen Vim inside parent application"));
Bram Moolenaar78e17622007-08-30 10:26:19 +00003243 main_msg(_("--windowid <HWND>\tOpen Vim inside another win32 widget"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003244#endif
3245
3246#ifdef FEAT_GUI_GNOME
3247 /* Gnome gives extra messages for --help if we continue, but not for -h. */
3248 if (gui.starting)
3249 mch_msg("\n");
3250 else
3251#endif
3252 mch_exit(0);
3253}
3254
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00003255#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003256/*
3257 * Check the result of the ATTENTION dialog:
3258 * When "Quit" selected, exit Vim.
3259 * When "Recover" selected, recover the file.
3260 */
3261 static void
3262check_swap_exists_action()
3263{
3264 if (swap_exists_action == SEA_QUIT)
3265 getout(1);
3266 handle_swap_exists(NULL);
3267}
3268#endif
3269
3270#if defined(STARTUPTIME) || defined(PROTO)
3271static void time_diff __ARGS((struct timeval *then, struct timeval *now));
3272
3273static struct timeval prev_timeval;
3274
Bram Moolenaar3f269672009-11-03 11:11:11 +00003275# ifdef WIN3264
3276/*
3277 * Windows doesn't have gettimeofday(), although it does have struct timeval.
3278 */
3279 static int
3280gettimeofday(struct timeval *tv, char *dummy)
3281{
3282 long t = clock();
3283 tv->tv_sec = t / CLOCKS_PER_SEC;
3284 tv->tv_usec = (t - tv->tv_sec * CLOCKS_PER_SEC) * 1000000 / CLOCKS_PER_SEC;
3285 return 0;
3286}
3287# endif
3288
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003289/*
3290 * Save the previous time before doing something that could nest.
3291 * set "*tv_rel" to the time elapsed so far.
3292 */
3293 void
3294time_push(tv_rel, tv_start)
3295 void *tv_rel, *tv_start;
3296{
3297 *((struct timeval *)tv_rel) = prev_timeval;
3298 gettimeofday(&prev_timeval, NULL);
3299 ((struct timeval *)tv_rel)->tv_usec = prev_timeval.tv_usec
3300 - ((struct timeval *)tv_rel)->tv_usec;
3301 ((struct timeval *)tv_rel)->tv_sec = prev_timeval.tv_sec
3302 - ((struct timeval *)tv_rel)->tv_sec;
3303 if (((struct timeval *)tv_rel)->tv_usec < 0)
3304 {
3305 ((struct timeval *)tv_rel)->tv_usec += 1000000;
3306 --((struct timeval *)tv_rel)->tv_sec;
3307 }
3308 *(struct timeval *)tv_start = prev_timeval;
3309}
3310
3311/*
3312 * Compute the previous time after doing something that could nest.
3313 * Subtract "*tp" from prev_timeval;
3314 * Note: The arguments are (void *) to avoid trouble with systems that don't
3315 * have struct timeval.
3316 */
3317 void
3318time_pop(tp)
3319 void *tp; /* actually (struct timeval *) */
3320{
3321 prev_timeval.tv_usec -= ((struct timeval *)tp)->tv_usec;
3322 prev_timeval.tv_sec -= ((struct timeval *)tp)->tv_sec;
3323 if (prev_timeval.tv_usec < 0)
3324 {
3325 prev_timeval.tv_usec += 1000000;
3326 --prev_timeval.tv_sec;
3327 }
3328}
3329
3330 static void
3331time_diff(then, now)
3332 struct timeval *then;
3333 struct timeval *now;
3334{
3335 long usec;
3336 long msec;
3337
3338 usec = now->tv_usec - then->tv_usec;
3339 msec = (now->tv_sec - then->tv_sec) * 1000L + usec / 1000L,
3340 usec = usec % 1000L;
3341 fprintf(time_fd, "%03ld.%03ld", msec, usec >= 0 ? usec : usec + 1000L);
3342}
3343
3344 void
3345time_msg(msg, tv_start)
3346 char *msg;
3347 void *tv_start; /* only for do_source: start time; actually
3348 (struct timeval *) */
3349{
3350 static struct timeval start;
3351 struct timeval now;
3352
3353 if (time_fd != NULL)
3354 {
3355 if (strstr(msg, "STARTING") != NULL)
3356 {
3357 gettimeofday(&start, NULL);
3358 prev_timeval = start;
3359 fprintf(time_fd, "\n\ntimes in msec\n");
3360 fprintf(time_fd, " clock self+sourced self: sourced script\n");
3361 fprintf(time_fd, " clock elapsed: other lines\n\n");
3362 }
3363 gettimeofday(&now, NULL);
3364 time_diff(&start, &now);
3365 if (((struct timeval *)tv_start) != NULL)
3366 {
3367 fprintf(time_fd, " ");
3368 time_diff(((struct timeval *)tv_start), &now);
3369 }
3370 fprintf(time_fd, " ");
3371 time_diff(&prev_timeval, &now);
3372 prev_timeval = now;
3373 fprintf(time_fd, ": %s\n", msg);
3374 }
3375}
3376
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003377#endif
3378
3379#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
3380
3381/*
3382 * Common code for the X command server and the Win32 command server.
3383 */
3384
Bram Moolenaareb94e552006-03-11 21:35:11 +00003385static char_u *build_drop_cmd __ARGS((int filec, char **filev, int tabs, int sendReply));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003386
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003387/*
3388 * Do the client-server stuff, unless "--servername ''" was used.
3389 */
3390 static void
3391exec_on_server(parmp)
3392 mparm_T *parmp;
3393{
3394 if (parmp->serverName_arg == NULL || *parmp->serverName_arg != NUL)
3395 {
3396# ifdef WIN32
3397 /* Initialise the client/server messaging infrastructure. */
3398 serverInitMessaging();
3399# endif
3400
3401 /*
3402 * When a command server argument was found, execute it. This may
3403 * exit Vim when it was successful. Otherwise it's executed further
3404 * on. Remember the encoding used here in "serverStrEnc".
3405 */
3406 if (parmp->serverArg)
3407 {
3408 cmdsrv_main(&parmp->argc, parmp->argv,
3409 parmp->serverName_arg, &parmp->serverStr);
3410# ifdef FEAT_MBYTE
3411 parmp->serverStrEnc = vim_strsave(p_enc);
3412# endif
3413 }
3414
3415 /* If we're still running, get the name to register ourselves.
3416 * On Win32 can register right now, for X11 need to setup the
3417 * clipboard first, it's further down. */
3418 parmp->servername = serverMakeName(parmp->serverName_arg,
3419 parmp->argv[0]);
3420# ifdef WIN32
3421 if (parmp->servername != NULL)
3422 {
3423 serverSetName(parmp->servername);
3424 vim_free(parmp->servername);
3425 }
3426# endif
3427 }
3428}
3429
3430/*
3431 * Prepare for running as a Vim server.
3432 */
3433 static void
3434prepare_server(parmp)
3435 mparm_T *parmp;
3436{
3437# if defined(FEAT_X11)
3438 /*
3439 * Register for remote command execution with :serversend and --remote
3440 * unless there was a -X or a --servername '' on the command line.
3441 * Only register nongui-vim's with an explicit --servername argument.
Bram Moolenaar5f402312006-08-15 19:40:35 +00003442 * When running as root --servername is also required.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003443 */
3444 if (X_DISPLAY != NULL && parmp->servername != NULL && (
3445# ifdef FEAT_GUI
Bram Moolenaar5f402312006-08-15 19:40:35 +00003446 (gui.in_use
3447# ifdef UNIX
Bram Moolenaar311d9822007-02-27 15:48:28 +00003448 && getuid() != ROOT_UID
Bram Moolenaar5f402312006-08-15 19:40:35 +00003449# endif
3450 ) ||
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003451# endif
3452 parmp->serverName_arg != NULL))
3453 {
3454 (void)serverRegisterName(X_DISPLAY, parmp->servername);
3455 vim_free(parmp->servername);
3456 TIME_MSG("register server name");
3457 }
3458 else
3459 serverDelayedStartName = parmp->servername;
3460# endif
3461
3462 /*
3463 * Execute command ourselves if we're here because the send failed (or
3464 * else we would have exited above).
3465 */
3466 if (parmp->serverStr != NULL)
3467 {
3468 char_u *p;
3469
3470 server_to_input_buf(serverConvert(parmp->serverStrEnc,
3471 parmp->serverStr, &p));
3472 vim_free(p);
3473 }
3474}
3475
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003476 static void
3477cmdsrv_main(argc, argv, serverName_arg, serverStr)
3478 int *argc;
3479 char **argv;
3480 char_u *serverName_arg;
3481 char_u **serverStr;
3482{
3483 char_u *res;
3484 int i;
3485 char_u *sname;
3486 int ret;
3487 int didone = FALSE;
3488 int exiterr = 0;
3489 char **newArgV = argv + 1;
3490 int newArgC = 1,
3491 Argc = *argc;
3492 int argtype;
3493#define ARGTYPE_OTHER 0
3494#define ARGTYPE_EDIT 1
3495#define ARGTYPE_EDIT_WAIT 2
3496#define ARGTYPE_SEND 3
3497 int silent = FALSE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003498 int tabs = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003499# ifndef FEAT_X11
3500 HWND srv;
3501# else
3502 Window srv;
3503
3504 setup_term_clip();
3505# endif
3506
3507 sname = serverMakeName(serverName_arg, argv[0]);
3508 if (sname == NULL)
3509 return;
3510
3511 /*
3512 * Execute the command server related arguments and remove them
3513 * from the argc/argv array; We may have to return into main()
3514 */
3515 for (i = 1; i < Argc; i++)
3516 {
3517 res = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003518 if (STRCMP(argv[i], "--") == 0) /* end of option arguments */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003519 {
3520 for (; i < *argc; i++)
3521 {
3522 *newArgV++ = argv[i];
3523 newArgC++;
3524 }
3525 break;
3526 }
3527
Bram Moolenaareb94e552006-03-11 21:35:11 +00003528 if (STRICMP(argv[i], "--remote-send") == 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003529 argtype = ARGTYPE_SEND;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003530 else if (STRNICMP(argv[i], "--remote", 8) == 0)
3531 {
3532 char *p = argv[i] + 8;
3533
3534 argtype = ARGTYPE_EDIT;
3535 while (*p != NUL)
3536 {
3537 if (STRNICMP(p, "-wait", 5) == 0)
3538 {
3539 argtype = ARGTYPE_EDIT_WAIT;
3540 p += 5;
3541 }
3542 else if (STRNICMP(p, "-silent", 7) == 0)
3543 {
3544 silent = TRUE;
3545 p += 7;
3546 }
3547 else if (STRNICMP(p, "-tab", 4) == 0)
3548 {
3549 tabs = TRUE;
3550 p += 4;
3551 }
3552 else
3553 {
3554 argtype = ARGTYPE_OTHER;
3555 break;
3556 }
3557 }
3558 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003559 else
3560 argtype = ARGTYPE_OTHER;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003561
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003562 if (argtype != ARGTYPE_OTHER)
3563 {
3564 if (i == *argc - 1)
3565 mainerr_arg_missing((char_u *)argv[i]);
3566 if (argtype == ARGTYPE_SEND)
3567 {
3568 *serverStr = (char_u *)argv[i + 1];
3569 i++;
3570 }
3571 else
3572 {
3573 *serverStr = build_drop_cmd(*argc - i - 1, argv + i + 1,
Bram Moolenaareb94e552006-03-11 21:35:11 +00003574 tabs, argtype == ARGTYPE_EDIT_WAIT);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003575 if (*serverStr == NULL)
3576 {
3577 /* Probably out of memory, exit. */
3578 didone = TRUE;
3579 exiterr = 1;
3580 break;
3581 }
3582 Argc = i;
3583 }
3584# ifdef FEAT_X11
3585 if (xterm_dpy == NULL)
3586 {
3587 mch_errmsg(_("No display"));
3588 ret = -1;
3589 }
3590 else
3591 ret = serverSendToVim(xterm_dpy, sname, *serverStr,
3592 NULL, &srv, 0, 0, silent);
3593# else
3594 /* Win32 always works? */
3595 ret = serverSendToVim(sname, *serverStr, NULL, &srv, 0, silent);
3596# endif
3597 if (ret < 0)
3598 {
3599 if (argtype == ARGTYPE_SEND)
3600 {
3601 /* Failed to send, abort. */
3602 mch_errmsg(_(": Send failed.\n"));
3603 didone = TRUE;
3604 exiterr = 1;
3605 }
3606 else if (!silent)
3607 /* Let vim start normally. */
3608 mch_errmsg(_(": Send failed. Trying to execute locally\n"));
3609 break;
3610 }
3611
3612# ifdef FEAT_GUI_W32
3613 /* Guess that when the server name starts with "g" it's a GUI
3614 * server, which we can bring to the foreground here.
3615 * Foreground() in the server doesn't work very well. */
3616 if (argtype != ARGTYPE_SEND && TOUPPER_ASC(*sname) == 'G')
3617 SetForegroundWindow(srv);
3618# endif
3619
3620 /*
3621 * For --remote-wait: Wait until the server did edit each
3622 * file. Also detect that the server no longer runs.
3623 */
3624 if (ret >= 0 && argtype == ARGTYPE_EDIT_WAIT)
3625 {
3626 int numFiles = *argc - i - 1;
3627 int j;
3628 char_u *done = alloc(numFiles);
3629 char_u *p;
3630# ifdef FEAT_GUI_W32
3631 NOTIFYICONDATA ni;
3632 int count = 0;
3633 extern HWND message_window;
3634# endif
3635
3636 if (numFiles > 0 && argv[i + 1][0] == '+')
3637 /* Skip "+cmd" argument, don't wait for it to be edited. */
3638 --numFiles;
3639
3640# ifdef FEAT_GUI_W32
3641 ni.cbSize = sizeof(ni);
3642 ni.hWnd = message_window;
3643 ni.uID = 0;
3644 ni.uFlags = NIF_ICON|NIF_TIP;
3645 ni.hIcon = LoadIcon((HINSTANCE)GetModuleHandle(0), "IDR_VIM");
3646 sprintf(ni.szTip, _("%d of %d edited"), count, numFiles);
3647 Shell_NotifyIcon(NIM_ADD, &ni);
3648# endif
3649
3650 /* Wait for all files to unload in remote */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02003651 vim_memset(done, 0, numFiles);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003652 while (memchr(done, 0, numFiles) != NULL)
3653 {
3654# ifdef WIN32
3655 p = serverGetReply(srv, NULL, TRUE, TRUE);
3656 if (p == NULL)
3657 break;
3658# else
3659 if (serverReadReply(xterm_dpy, srv, &p, TRUE) < 0)
3660 break;
3661# endif
3662 j = atoi((char *)p);
3663 if (j >= 0 && j < numFiles)
3664 {
3665# ifdef FEAT_GUI_W32
3666 ++count;
3667 sprintf(ni.szTip, _("%d of %d edited"),
3668 count, numFiles);
3669 Shell_NotifyIcon(NIM_MODIFY, &ni);
3670# endif
3671 done[j] = 1;
3672 }
3673 }
3674# ifdef FEAT_GUI_W32
3675 Shell_NotifyIcon(NIM_DELETE, &ni);
3676# endif
3677 }
3678 }
3679 else if (STRICMP(argv[i], "--remote-expr") == 0)
3680 {
3681 if (i == *argc - 1)
3682 mainerr_arg_missing((char_u *)argv[i]);
3683# ifdef WIN32
3684 /* Win32 always works? */
3685 if (serverSendToVim(sname, (char_u *)argv[i + 1],
3686 &res, NULL, 1, FALSE) < 0)
3687# else
3688 if (xterm_dpy == NULL)
3689 mch_errmsg(_("No display: Send expression failed.\n"));
3690 else if (serverSendToVim(xterm_dpy, sname, (char_u *)argv[i + 1],
3691 &res, NULL, 1, 1, FALSE) < 0)
3692# endif
3693 {
3694 if (res != NULL && *res != NUL)
3695 {
3696 /* Output error from remote */
3697 mch_errmsg((char *)res);
3698 vim_free(res);
3699 res = NULL;
3700 }
3701 mch_errmsg(_(": Send expression failed.\n"));
3702 }
3703 }
3704 else if (STRICMP(argv[i], "--serverlist") == 0)
3705 {
3706# ifdef WIN32
3707 /* Win32 always works? */
3708 res = serverGetVimNames();
3709# else
3710 if (xterm_dpy != NULL)
3711 res = serverGetVimNames(xterm_dpy);
3712# endif
3713 if (called_emsg)
3714 mch_errmsg("\n");
3715 }
3716 else if (STRICMP(argv[i], "--servername") == 0)
3717 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +00003718 /* Already processed. Take it out of the command line */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003719 i++;
3720 continue;
3721 }
3722 else
3723 {
3724 *newArgV++ = argv[i];
3725 newArgC++;
3726 continue;
3727 }
3728 didone = TRUE;
3729 if (res != NULL && *res != NUL)
3730 {
3731 mch_msg((char *)res);
3732 if (res[STRLEN(res) - 1] != '\n')
3733 mch_msg("\n");
3734 }
3735 vim_free(res);
3736 }
3737
3738 if (didone)
3739 {
3740 display_errors(); /* display any collected messages */
3741 exit(exiterr); /* Mission accomplished - get out */
3742 }
3743
3744 /* Return back into main() */
3745 *argc = newArgC;
3746 vim_free(sname);
3747}
3748
3749/*
3750 * Build a ":drop" command to send to a Vim server.
3751 */
3752 static char_u *
Bram Moolenaareb94e552006-03-11 21:35:11 +00003753build_drop_cmd(filec, filev, tabs, sendReply)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003754 int filec;
3755 char **filev;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003756 int tabs; /* Use ":tab drop" instead of ":drop". */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003757 int sendReply;
3758{
3759 garray_T ga;
3760 int i;
3761 char_u *inicmd = NULL;
3762 char_u *p;
3763 char_u cwd[MAXPATHL];
3764
3765 if (filec > 0 && filev[0][0] == '+')
3766 {
3767 inicmd = (char_u *)filev[0] + 1;
3768 filev++;
3769 filec--;
3770 }
3771 /* Check if we have at least one argument. */
3772 if (filec <= 0)
3773 mainerr_arg_missing((char_u *)filev[-1]);
3774 if (mch_dirname(cwd, MAXPATHL) != OK)
3775 return NULL;
Bram Moolenaar02b06312007-09-06 15:39:22 +00003776 if ((p = vim_strsave_escaped_ext(cwd,
3777#ifdef BACKSLASH_IN_FILENAME
3778 "", /* rem_backslash() will tell what chars to escape */
3779#else
3780 PATH_ESC_CHARS,
3781#endif
3782 '\\', TRUE)) == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003783 return NULL;
3784 ga_init2(&ga, 1, 100);
3785 ga_concat(&ga, (char_u *)"<C-\\><C-N>:cd ");
3786 ga_concat(&ga, p);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003787 vim_free(p);
Bram Moolenaareb94e552006-03-11 21:35:11 +00003788
3789 /* Call inputsave() so that a prompt for an encryption key works. */
3790 ga_concat(&ga, (char_u *)"<CR>:if exists('*inputsave')|call inputsave()|endif|");
3791 if (tabs)
3792 ga_concat(&ga, (char_u *)"tab ");
3793 ga_concat(&ga, (char_u *)"drop");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003794 for (i = 0; i < filec; i++)
3795 {
3796 /* On Unix the shell has already expanded the wildcards, don't want to
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003797 * do it again in the Vim server. On MS-Windows only escape
3798 * non-wildcard characters. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003799 p = vim_strsave_escaped((char_u *)filev[i],
3800#ifdef UNIX
3801 PATH_ESC_CHARS
3802#else
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003803 (char_u *)" \t%#"
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003804#endif
3805 );
3806 if (p == NULL)
3807 {
3808 vim_free(ga.ga_data);
3809 return NULL;
3810 }
3811 ga_concat(&ga, (char_u *)" ");
3812 ga_concat(&ga, p);
3813 vim_free(p);
3814 }
3815 /* The :drop commands goes to Insert mode when 'insertmode' is set, use
3816 * CTRL-\ CTRL-N again. */
3817 ga_concat(&ga, (char_u *)"|if exists('*inputrestore')|call inputrestore()|endif<CR>");
3818 ga_concat(&ga, (char_u *)"<C-\\><C-N>:cd -");
3819 if (sendReply)
3820 ga_concat(&ga, (char_u *)"<CR>:call SetupRemoteReplies()");
3821 ga_concat(&ga, (char_u *)"<CR>:");
3822 if (inicmd != NULL)
3823 {
3824 /* Can't use <CR> after "inicmd", because an "startinsert" would cause
3825 * the following commands to be inserted as text. Use a "|",
3826 * hopefully "inicmd" does allow this... */
3827 ga_concat(&ga, inicmd);
3828 ga_concat(&ga, (char_u *)"|");
3829 }
3830 /* Bring the window to the foreground, goto Insert mode when 'im' set and
3831 * clear command line. */
Bram Moolenaar567e4de2004-12-31 21:01:02 +00003832 ga_concat(&ga, (char_u *)"cal foreground()|if &im|star|en|redr|f<CR>");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003833 ga_append(&ga, NUL);
3834 return ga.ga_data;
3835}
3836
3837/*
3838 * Replace termcodes such as <CR> and insert as key presses if there is room.
3839 */
3840 void
3841server_to_input_buf(str)
3842 char_u *str;
3843{
3844 char_u *ptr = NULL;
3845 char_u *cpo_save = p_cpo;
3846
3847 /* Set 'cpoptions' the way we want it.
3848 * B set - backslashes are *not* treated specially
3849 * k set - keycodes are *not* reverse-engineered
3850 * < unset - <Key> sequences *are* interpreted
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00003851 * The last but one parameter of replace_termcodes() is TRUE so that the
3852 * <lt> sequence is recognised - needed for a real backslash.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003853 */
3854 p_cpo = (char_u *)"Bk";
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00003855 str = replace_termcodes((char_u *)str, &ptr, FALSE, TRUE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003856 p_cpo = cpo_save;
3857
3858 if (*ptr != NUL) /* trailing CTRL-V results in nothing */
3859 {
3860 /*
3861 * Add the string to the input stream.
3862 * Can't use add_to_input_buf() here, we now have K_SPECIAL bytes.
3863 *
3864 * First clear typed characters from the typeahead buffer, there could
3865 * be half a mapping there. Then append to the existing string, so
3866 * that multiple commands from a client are concatenated.
3867 */
3868 if (typebuf.tb_maplen < typebuf.tb_len)
3869 del_typebuf(typebuf.tb_len - typebuf.tb_maplen, typebuf.tb_maplen);
3870 (void)ins_typebuf(str, REMAP_NONE, typebuf.tb_len, TRUE, FALSE);
3871
3872 /* Let input_available() know we inserted text in the typeahead
3873 * buffer. */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003874 typebuf_was_filled = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003875 }
3876 vim_free((char_u *)ptr);
3877}
3878
3879/*
3880 * Evaluate an expression that the client sent to a string.
3881 * Handles disabling error messages and disables debugging, otherwise Vim
3882 * hangs, waiting for "cont" to be typed.
3883 */
3884 char_u *
3885eval_client_expr_to_string(expr)
3886 char_u *expr;
3887{
3888 char_u *res;
3889 int save_dbl = debug_break_level;
3890 int save_ro = redir_off;
3891
3892 debug_break_level = -1;
3893 redir_off = 0;
3894 ++emsg_skip;
3895
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003896 res = eval_to_string(expr, NULL, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003897
3898 debug_break_level = save_dbl;
3899 redir_off = save_ro;
3900 --emsg_skip;
3901
Bram Moolenaar63a121b2005-12-11 21:36:39 +00003902 /* A client can tell us to redraw, but not to display the cursor, so do
3903 * that here. */
3904 setcursor();
3905 out_flush();
3906#ifdef FEAT_GUI
3907 if (gui.in_use)
3908 gui_update_cursor(FALSE, FALSE);
3909#endif
3910
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003911 return res;
3912}
3913
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003914/*
3915 * If conversion is needed, convert "data" from "client_enc" to 'encoding' and
3916 * return an allocated string. Otherwise return "data".
3917 * "*tofree" is set to the result when it needs to be freed later.
3918 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003919 char_u *
3920serverConvert(client_enc, data, tofree)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003921 char_u *client_enc UNUSED;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003922 char_u *data;
3923 char_u **tofree;
3924{
3925 char_u *res = data;
3926
3927 *tofree = NULL;
3928# ifdef FEAT_MBYTE
3929 if (client_enc != NULL && p_enc != NULL)
3930 {
3931 vimconv_T vimconv;
3932
3933 vimconv.vc_type = CONV_NONE;
3934 if (convert_setup(&vimconv, client_enc, p_enc) != FAIL
3935 && vimconv.vc_type != CONV_NONE)
3936 {
3937 res = string_convert(&vimconv, data, NULL);
3938 if (res == NULL)
3939 res = data;
3940 else
3941 *tofree = res;
3942 }
3943 convert_setup(&vimconv, NULL, NULL);
3944 }
3945# endif
3946 return res;
3947}
3948
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003949
3950/*
3951 * Make our basic server name: use the specified "arg" if given, otherwise use
3952 * the tail of the command "cmd" we were started with.
3953 * Return the name in allocated memory. This doesn't include a serial number.
3954 */
3955 static char_u *
3956serverMakeName(arg, cmd)
3957 char_u *arg;
3958 char *cmd;
3959{
3960 char_u *p;
3961
3962 if (arg != NULL && *arg != NUL)
3963 p = vim_strsave_up(arg);
3964 else
3965 {
3966 p = vim_strsave_up(gettail((char_u *)cmd));
3967 /* Remove .exe or .bat from the name. */
3968 if (p != NULL && vim_strchr(p, '.') != NULL)
3969 *vim_strchr(p, '.') = NUL;
3970 }
3971 return p;
3972}
3973#endif /* FEAT_CLIENTSERVER */
3974
3975/*
3976 * When FEAT_FKMAP is defined, also compile the Farsi source code.
3977 */
3978#if defined(FEAT_FKMAP) || defined(PROTO)
3979# include "farsi.c"
3980#endif
3981
3982/*
3983 * When FEAT_ARABIC is defined, also compile the Arabic source code.
3984 */
3985#if defined(FEAT_ARABIC) || defined(PROTO)
3986# include "arabic.c"
3987#endif