blob: cce99cd1319d074f257dc594b09922c2b4aa300e [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
Bram Moolenaarc8bbaa32010-07-14 16:54:21 +0200714#if defined(MACOS_X) && defined(FEAT_CLIPBOARD)
715 clip_init(TRUE);
716#endif
717
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000718#ifdef FEAT_XCLIPBOARD
719 /* Start using the X clipboard, unless the GUI was started. */
720# ifdef FEAT_GUI
721 if (!gui.in_use)
722# endif
723 {
724 setup_term_clip();
725 TIME_MSG("setup clipboard");
726 }
727#endif
728
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000729#ifdef FEAT_CLIENTSERVER
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000730 /* Prepare for being a Vim server. */
731 prepare_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000732#endif
733
734 /*
735 * If "-" argument given: Read file from stdin.
736 * Do this before starting Raw mode, because it may change things that the
737 * writing end of the pipe doesn't like, e.g., in case stdin and stderr
738 * are the same terminal: "cat | vim -".
739 * Using autocommands here may cause trouble...
740 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000741 if (params.edit_type == EDIT_STDIN && !recoverymode)
742 read_stdin();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000743
744#if defined(UNIX) || defined(VMS)
745 /* When switching screens and something caused a message from a vimrc
746 * script, need to output an extra newline on exit. */
747 if ((did_emsg || msg_didout) && *T_TI != NUL)
748 newline_on_exit = TRUE;
749#endif
750
751 /*
752 * When done something that is not allowed or error message call
753 * wait_return. This must be done before starttermcap(), because it may
754 * switch to another screen. It must be done after settmode(TMODE_RAW),
755 * because we want to react on a single key stroke.
756 * Call settmode and starttermcap here, so the T_KS and T_TI may be
Bram Moolenaar49325942007-05-10 19:19:59 +0000757 * defined by termcapinit and redefined in .exrc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000758 */
759 settmode(TMODE_RAW);
760 TIME_MSG("setting raw mode");
761
762 if (need_wait_return || msg_didany)
763 {
764 wait_return(TRUE);
765 TIME_MSG("waiting for return");
766 }
767
768 starttermcap(); /* start termcap if not done by wait_return() */
769 TIME_MSG("start termcap");
770
771#ifdef FEAT_MOUSE
772 setmouse(); /* may start using the mouse */
773#endif
774 if (scroll_region)
775 scroll_region_reset(); /* In case Rows changed */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000776 scroll_start(); /* may scroll the screen to the right position */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000777
778 /*
779 * Don't clear the screen when starting in Ex mode, unless using the GUI.
780 */
781 if (exmode_active
782#ifdef FEAT_GUI
783 && !gui.in_use
784#endif
785 )
786 must_redraw = CLEAR;
787 else
788 {
789 screenclear(); /* clear screen */
790 TIME_MSG("clearing screen");
791 }
792
793#ifdef FEAT_CRYPT
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000794 if (params.ask_for_key)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000795 {
Bram Moolenaar40e6a712010-05-16 22:32:54 +0200796 (void)blowfish_self_test();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000797 (void)get_crypt_key(TRUE, TRUE);
798 TIME_MSG("getting crypt key");
799 }
800#endif
801
802 no_wait_return = TRUE;
803
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000804 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000805 * Create the requested number of windows and edit buffers in them.
806 * Also does recovery if "recoverymode" set.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000807 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000808 create_windows(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000809 TIME_MSG("opening buffers");
810
Bram Moolenaar867a4b72007-03-18 20:51:46 +0000811#ifdef FEAT_EVAL
812 /* clear v:swapcommand */
813 set_vim_var_string(VV_SWAPCOMMAND, NULL, -1);
814#endif
815
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000816 /* Ex starts at last line of the file */
817 if (exmode_active)
818 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
819
820#ifdef FEAT_AUTOCMD
821 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
822 TIME_MSG("BufEnter autocommands");
823#endif
824 setpcmark();
825
826#ifdef FEAT_QUICKFIX
827 /*
828 * When started with "-q errorfile" jump to first error now.
829 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000830 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000831 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000832 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000833 TIME_MSG("jump to first error");
834 }
835#endif
836
837#ifdef FEAT_WINDOWS
838 /*
839 * If opened more than one window, start editing files in the other
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000840 * windows.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000841 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000842 edit_buffers(&params);
843#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000844
845#ifdef FEAT_DIFF
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000846 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000847 {
848 win_T *wp;
849
850 /* set options in each window for "vimdiff". */
851 for (wp = firstwin; wp != NULL; wp = wp->w_next)
852 diff_win_options(wp, TRUE);
853 }
854#endif
855
856 /*
857 * Shorten any of the filenames, but only when absolute.
858 */
859 shorten_fnames(FALSE);
860
861 /*
862 * Need to jump to the tag before executing the '-c command'.
863 * Makes "vim -c '/return' -t main" work.
864 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000865 if (params.tagname != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000866 {
Bram Moolenaar146522e2005-12-16 21:55:46 +0000867#if defined(HAS_SWAP_EXISTS_ACTION)
868 swap_exists_did_quit = FALSE;
869#endif
870
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000871 vim_snprintf((char *)IObuff, IOSIZE, "ta %s", params.tagname);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000872 do_cmdline_cmd(IObuff);
873 TIME_MSG("jumping to tag");
Bram Moolenaar146522e2005-12-16 21:55:46 +0000874
875#if defined(HAS_SWAP_EXISTS_ACTION)
876 /* If the user doesn't want to edit the file then we quit here. */
877 if (swap_exists_did_quit)
878 getout(1);
879#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000880 }
881
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000882 /* Execute any "+", "-c" and "-S" arguments. */
883 if (params.n_commands > 0)
884 exe_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000885
886 RedrawingDisabled = 0;
887 redraw_all_later(NOT_VALID);
888 no_wait_return = FALSE;
889 starting = 0;
890
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000891#ifdef FEAT_TERMRESPONSE
892 /* Requesting the termresponse is postponed until here, so that a "-c q"
893 * argument doesn't make it appear in the shell Vim was started from. */
894 may_req_termresponse();
895#endif
896
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000897 /* start in insert mode */
898 if (p_im)
899 need_start_insertmode = TRUE;
900
901#ifdef FEAT_AUTOCMD
902 apply_autocmds(EVENT_VIMENTER, NULL, NULL, FALSE, curbuf);
903 TIME_MSG("VimEnter autocommands");
904#endif
905
906#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
907 /* When a startup script or session file setup for diff'ing and
908 * scrollbind, sync the scrollbind now. */
909 if (curwin->w_p_diff && curwin->w_p_scb)
910 {
911 update_topline();
912 check_scrollbind((linenr_T)0, 0L);
913 TIME_MSG("diff scrollbinding");
914 }
915#endif
916
917#if defined(WIN3264) && !defined(FEAT_GUI_W32)
918 mch_set_winsize_now(); /* Allow winsize changes from now on */
919#endif
920
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000921#if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
922 /* When tab pages were created, may need to update the tab pages line and
923 * scrollbars. This is skipped while creating them. */
924 if (first_tabpage->tp_next != NULL)
925 {
926 out_flush();
927 gui_init_which_components(NULL);
928 gui_update_scrollbars(TRUE);
929 }
930 need_mouse_correct = TRUE;
931#endif
932
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000933 /* If ":startinsert" command used, stuff a dummy command to be able to
934 * call normal_cmd(), which will then start Insert mode. */
935 if (restart_edit != 0)
Bram Moolenaarebefac62005-12-28 22:39:57 +0000936 stuffcharReadbuff(K_NOP);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000937
938#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200939 if (netbeansArg != NULL && strncmp("-nb", netbeansArg, 3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200940 {
941# ifdef FEAT_GUI
942# if !defined(FEAT_GUI_MOTIF) && !defined(FEAT_GUI_GTK) \
943 && !defined(FEAT_GUI_W32)
944 if (gui.in_use)
945 {
946 mch_errmsg(_("netbeans is not supported with this GUI\n"));
947 mch_exit(2);
948 }
949# endif
950# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000951 /* Tell the client that it can start sending commands. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200952 netbeans_open(netbeansArg + 3, TRUE);
Bram Moolenaar67c53842010-05-22 18:28:27 +0200953 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000954#endif
955
956 TIME_MSG("before starting main loop");
957
958 /*
959 * Call the main command loop. This never returns.
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000960 * For embedded MzScheme the main_loop will be called by Scheme
961 * for proper stack tracking
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000962 */
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000963#ifndef FEAT_MZSCHEME
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000964 main_loop(FALSE, FALSE);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000965#else
966 mzscheme_main();
967#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000968
969 return 0;
970}
971#endif /* PROTO */
972
973/*
974 * Main loop: Execute Normal mode commands until exiting Vim.
975 * Also used to handle commands in the command-line window, until the window
976 * is closed.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000977 * Also used to handle ":visual" command after ":global": execute Normal mode
978 * commands, return when entering Ex mode. "noexmode" is TRUE then.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000979 */
980 void
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000981main_loop(cmdwin, noexmode)
982 int cmdwin; /* TRUE when working in the command-line window */
983 int noexmode; /* TRUE when return on entering Ex mode */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000984{
Bram Moolenaar225d32b2007-08-10 19:33:47 +0000985 oparg_T oa; /* operator arguments */
986 int previous_got_int = FALSE; /* "got_int" was TRUE */
Bram Moolenaarb2c03502010-07-02 20:20:09 +0200987#ifdef FEAT_CONCEAL
988 linenr_T conceal_old_cursor_line = 0;
989 linenr_T conceal_new_cursor_line = 0;
990 int conceal_update_lines = FALSE;
991#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000992
993#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
994 /* Setup to catch a terminating error from the X server. Just ignore
995 * it, restore the state and continue. This might not always work
996 * properly, but at least we don't exit unexpectedly when the X server
997 * exists while Vim is running in a console. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000998 if (!cmdwin && !noexmode && SETJMP(x_jump_env))
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000999 {
1000 State = NORMAL;
1001# ifdef FEAT_VISUAL
1002 VIsual_active = FALSE;
1003# endif
1004 got_int = TRUE;
1005 need_wait_return = FALSE;
1006 global_busy = FALSE;
1007 exmode_active = 0;
1008 skip_redraw = FALSE;
1009 RedrawingDisabled = 0;
1010 no_wait_return = 0;
1011# ifdef FEAT_EVAL
1012 emsg_skip = 0;
1013# endif
1014 emsg_off = 0;
1015# ifdef FEAT_MOUSE
1016 setmouse();
1017# endif
1018 settmode(TMODE_RAW);
1019 starttermcap();
1020 scroll_start();
1021 redraw_later_clear();
1022 }
1023#endif
1024
1025 clear_oparg(&oa);
1026 while (!cmdwin
1027#ifdef FEAT_CMDWIN
1028 || cmdwin_result == 0
1029#endif
1030 )
1031 {
1032 if (stuff_empty())
1033 {
1034 did_check_timestamps = FALSE;
1035 if (need_check_timestamps)
1036 check_timestamps(FALSE);
1037 if (need_wait_return) /* if wait_return still needed ... */
1038 wait_return(FALSE); /* ... call it now */
1039 if (need_start_insertmode && goto_im()
1040#ifdef FEAT_VISUAL
1041 && !VIsual_active
1042#endif
1043 )
1044 {
1045 need_start_insertmode = FALSE;
1046 stuffReadbuff((char_u *)"i"); /* start insert mode next */
1047 /* skip the fileinfo message now, because it would be shown
1048 * after insert mode finishes! */
1049 need_fileinfo = FALSE;
1050 }
1051 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001052
1053 /* Reset "got_int" now that we got back to the main loop. Except when
1054 * inside a ":g/pat/cmd" command, then the "got_int" needs to abort
1055 * the ":g" command.
1056 * For ":g/pat/vi" we reset "got_int" when used once. When used
1057 * a second time we go back to Ex mode and abort the ":g" command. */
1058 if (got_int)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001059 {
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001060 if (noexmode && global_busy && !exmode_active && previous_got_int)
1061 {
1062 /* Typed two CTRL-C in a row: go back to ex mode as if "Q" was
1063 * used and keep "got_int" set, so that it aborts ":g". */
1064 exmode_active = EXMODE_NORMAL;
1065 State = NORMAL;
1066 }
1067 else if (!global_busy || !exmode_active)
1068 {
1069 if (!quit_more)
1070 (void)vgetc(); /* flush all buffers */
1071 got_int = FALSE;
1072 }
1073 previous_got_int = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001074 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001075 else
1076 previous_got_int = FALSE;
1077
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001078 if (!exmode_active)
1079 msg_scroll = FALSE;
1080 quit_more = FALSE;
1081
1082 /*
1083 * If skip redraw is set (for ":" in wait_return()), don't redraw now.
1084 * If there is nothing in the stuff_buffer or do_redraw is TRUE,
1085 * update cursor and redraw.
1086 */
1087 if (skip_redraw || exmode_active)
1088 skip_redraw = FALSE;
1089 else if (do_redraw || stuff_empty())
1090 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001091#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001092 /* Trigger CursorMoved if the cursor moved. */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001093 if (!finish_op && (
1094# ifdef FEAT_AUTOCMD
1095 has_cursormoved()
1096# endif
1097# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
1098 ||
1099# endif
1100# ifdef FEAT_CONCEAL
1101 curwin->w_p_conceal
1102# endif
1103 )
1104 && !equalpos(last_cursormoved, curwin->w_cursor))
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001105 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001106# ifdef FEAT_AUTOCMD
1107 if (has_cursormoved())
1108 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL,
1109 FALSE, curbuf);
1110# endif
1111# ifdef FEAT_CONCEAL
1112 if (curwin->w_p_conceal)
1113 {
1114 conceal_old_cursor_line = last_cursormoved.lnum;
1115 conceal_new_cursor_line = curwin->w_cursor.lnum;
1116 conceal_update_lines = TRUE;
1117 }
1118# endif
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001119 last_cursormoved = curwin->w_cursor;
1120 }
1121#endif
1122
Bram Moolenaar33aec762006-01-22 23:30:12 +00001123#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
1124 /* Scroll-binding for diff mode may have been postponed until
1125 * here. Avoids doing it for every change. */
1126 if (diff_need_scrollbind)
1127 {
1128 check_scrollbind((linenr_T)0, 0L);
1129 diff_need_scrollbind = FALSE;
1130 }
1131#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001132#if defined(FEAT_FOLDING) && defined(FEAT_VISUAL)
1133 /* Include a closed fold completely in the Visual area. */
1134 foldAdjustVisual();
1135#endif
1136#ifdef FEAT_FOLDING
1137 /*
1138 * When 'foldclose' is set, apply 'foldlevel' to folds that don't
1139 * contain the cursor.
1140 * When 'foldopen' is "all", open the fold(s) under the cursor.
1141 * This may mark the window for redrawing.
1142 */
1143 if (hasAnyFolding(curwin) && !char_avail())
1144 {
1145 foldCheckClose();
1146 if (fdo_flags & FDO_ALL)
1147 foldOpenCursor();
1148 }
1149#endif
1150
1151 /*
1152 * Before redrawing, make sure w_topline is correct, and w_leftcol
1153 * if lines don't wrap, and w_skipcol if lines wrap.
1154 */
1155 update_topline();
1156 validate_cursor();
1157
1158#ifdef FEAT_VISUAL
1159 if (VIsual_active)
1160 update_curbuf(INVERTED);/* update inverted part */
1161 else
1162#endif
1163 if (must_redraw)
1164 update_screen(0);
1165 else if (redraw_cmdline || clear_cmdline)
1166 showmode();
1167#ifdef FEAT_WINDOWS
1168 redraw_statuslines();
1169#endif
1170#ifdef FEAT_TITLE
1171 if (need_maketitle)
1172 maketitle();
1173#endif
1174 /* display message after redraw */
1175 if (keep_msg != NULL)
1176 {
1177 char_u *p;
1178
1179 /* msg_attr_keep() will set keep_msg to NULL, must free the
1180 * string here. */
1181 p = keep_msg;
Bram Moolenaar238a5642006-02-21 22:12:05 +00001182 keep_msg = NULL;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001183 msg_attr(p, keep_msg_attr);
1184 vim_free(p);
1185 }
1186 if (need_fileinfo) /* show file info after redraw */
1187 {
1188 fileinfo(FALSE, TRUE, FALSE);
1189 need_fileinfo = FALSE;
1190 }
1191
1192 emsg_on_display = FALSE; /* can delete error message now */
1193 did_emsg = FALSE;
1194 msg_didany = FALSE; /* reset lines_left in msg_start() */
Bram Moolenaar661b1822005-07-28 22:36:45 +00001195 may_clear_sb_text(); /* clear scroll-back text on next msg */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001196 showruler(FALSE);
1197
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001198# if defined(FEAT_CONCEAL)
1199 if (conceal_update_lines
1200 && conceal_old_cursor_line != conceal_new_cursor_line)
1201 {
1202 update_single_line(curwin, conceal_old_cursor_line);
1203 update_single_line(curwin, conceal_new_cursor_line);
1204 curwin->w_valid &= ~VALID_CROW;
1205 }
1206# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001207 setcursor();
1208 cursor_on();
1209
1210 do_redraw = FALSE;
Bram Moolenaar3f269672009-11-03 11:11:11 +00001211
1212#ifdef STARTUPTIME
1213 /* Now that we have drawn the first screen all the startup stuff
1214 * has been done, close any file for startup messages. */
1215 if (time_fd != NULL)
1216 {
1217 TIME_MSG("first screen update");
1218 TIME_MSG("--- VIM STARTED ---");
1219 fclose(time_fd);
1220 time_fd = NULL;
1221 }
1222#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001223 }
1224#ifdef FEAT_GUI
1225 if (need_mouse_correct)
1226 gui_mouse_correct();
1227#endif
1228
1229 /*
1230 * Update w_curswant if w_set_curswant has been set.
1231 * Postponed until here to avoid computing w_virtcol too often.
1232 */
1233 update_curswant();
1234
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001235#ifdef FEAT_EVAL
1236 /*
1237 * May perform garbage collection when waiting for a character, but
1238 * only at the very toplevel. Otherwise we may be using a List or
1239 * Dict internally somewhere.
1240 * "may_garbage_collect" is reset in vgetc() which is invoked through
1241 * do_exmode() and normal_cmd().
1242 */
1243 may_garbage_collect = (!cmdwin && !noexmode);
1244#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001245 /*
1246 * If we're invoked as ex, do a round of ex commands.
1247 * Otherwise, get and execute a normal mode command.
1248 */
1249 if (exmode_active)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001250 {
1251 if (noexmode) /* End of ":global/path/visual" commands */
1252 return;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001253 do_exmode(exmode_active == EXMODE_VIM);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001254 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001255 else
1256 normal_cmd(&oa, TRUE);
1257 }
1258}
1259
1260
1261#if defined(USE_XSMP) || defined(FEAT_GUI_MSWIN) || defined(PROTO)
1262/*
1263 * Exit, but leave behind swap files for modified buffers.
1264 */
1265 void
1266getout_preserve_modified(exitval)
1267 int exitval;
1268{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001269# if defined(SIGHUP) && defined(SIG_IGN)
1270 /* Ignore SIGHUP, because a dropped connection causes a read error, which
1271 * makes Vim exit and then handling SIGHUP causes various reentrance
1272 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001273 signal(SIGHUP, SIG_IGN);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001274# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001275
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001276 ml_close_notmod(); /* close all not-modified buffers */
1277 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
1278 ml_close_all(FALSE); /* close all memfiles, without deleting */
1279 getout(exitval); /* exit Vim properly */
1280}
1281#endif
1282
1283
1284/* Exit properly */
1285 void
1286getout(exitval)
1287 int exitval;
1288{
1289#ifdef FEAT_AUTOCMD
1290 buf_T *buf;
1291 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001292 tabpage_T *tp, *next_tp;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001293#endif
1294
1295 exiting = TRUE;
1296
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001297 /* When running in Ex mode an error causes us to exit with a non-zero exit
1298 * code. POSIX requires this, although it's not 100% clear from the
1299 * standard. */
1300 if (exmode_active)
1301 exitval += ex_exitval;
1302
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001303 /* Position the cursor on the last screen line, below all the text */
1304#ifdef FEAT_GUI
1305 if (!gui.in_use)
1306#endif
1307 windgoto((int)Rows - 1, 0);
1308
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +00001309#if defined(FEAT_EVAL) || defined(FEAT_SYN_HL)
1310 /* Optionally print hashtable efficiency. */
1311 hash_debug_results();
1312#endif
1313
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001314#ifdef FEAT_GUI
1315 msg_didany = FALSE;
1316#endif
1317
1318#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001319 if (get_vim_var_nr(VV_DYING) <= 1)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001320 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001321 /* Trigger BufWinLeave for all windows, but only once per buffer. */
1322# if defined FEAT_WINDOWS
1323 for (tp = first_tabpage; tp != NULL; tp = next_tp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001324 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001325 next_tp = tp->tp_next;
1326 for (wp = (tp == curtab)
1327 ? firstwin : tp->tp_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001328 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001329 buf = wp->w_buffer;
1330 if (buf->b_changedtick != -1)
1331 {
1332 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname,
1333 buf->b_fname, FALSE, buf);
1334 buf->b_changedtick = -1; /* note that we did it already */
1335 /* start all over, autocommands may mess up the lists */
1336 next_tp = first_tabpage;
1337 break;
1338 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001339 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001340 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001341# else
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001342 apply_autocmds(EVENT_BUFWINLEAVE, curbuf, curbuf->b_fname,
1343 FALSE, curbuf);
Bram Moolenaarf740b292006-02-16 22:11:02 +00001344# endif
Bram Moolenaar33aec762006-01-22 23:30:12 +00001345
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001346 /* Trigger BufUnload for buffers that are loaded */
1347 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1348 if (buf->b_ml.ml_mfp != NULL)
1349 {
1350 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001351 FALSE, buf);
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001352 if (!buf_valid(buf)) /* autocmd may delete the buffer */
1353 break;
1354 }
1355 apply_autocmds(EVENT_VIMLEAVEPRE, NULL, NULL, FALSE, curbuf);
1356 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001357#endif
1358
1359#ifdef FEAT_VIMINFO
1360 if (*p_viminfo != NUL)
1361 /* Write out the registers, history, marks etc, to the viminfo file */
1362 write_viminfo(NULL, FALSE);
1363#endif
1364
1365#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001366 if (get_vim_var_nr(VV_DYING) <= 1)
1367 apply_autocmds(EVENT_VIMLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001368#endif
1369
Bram Moolenaar05159a02005-02-26 23:04:13 +00001370#ifdef FEAT_PROFILE
1371 profile_dump();
1372#endif
1373
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001374 if (did_emsg
1375#ifdef FEAT_GUI
1376 || (gui.in_use && msg_didany && p_verbose > 0)
1377#endif
1378 )
1379 {
1380 /* give the user a chance to read the (error) message */
1381 no_wait_return = FALSE;
1382 wait_return(FALSE);
1383 }
1384
1385#ifdef FEAT_AUTOCMD
1386 /* Position the cursor again, the autocommands may have moved it */
1387# ifdef FEAT_GUI
1388 if (!gui.in_use)
1389# endif
1390 windgoto((int)Rows - 1, 0);
1391#endif
1392
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001393#ifdef FEAT_LUA
1394 lua_end();
1395#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001396#ifdef FEAT_MZSCHEME
1397 mzscheme_end();
1398#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001399#ifdef FEAT_TCL
1400 tcl_end();
1401#endif
1402#ifdef FEAT_RUBY
1403 ruby_end();
1404#endif
1405#ifdef FEAT_PYTHON
1406 python_end();
1407#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001408#ifdef FEAT_PYTHON3
1409 python3_end();
1410#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001411#ifdef FEAT_PERL
1412 perl_end();
1413#endif
1414#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
1415 iconv_end();
1416#endif
1417#ifdef FEAT_NETBEANS_INTG
1418 netbeans_end();
1419#endif
Bram Moolenaar02b06312007-09-06 15:39:22 +00001420#ifdef FEAT_CSCOPE
1421 cs_end();
1422#endif
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001423#ifdef FEAT_EVAL
1424 if (garbage_collect_at_exit)
1425 garbage_collect();
1426#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001427
1428 mch_exit(exitval);
1429}
1430
1431/*
1432 * Get a (optional) count for a Vim argument.
1433 */
1434 static int
1435get_number_arg(p, idx, def)
1436 char_u *p; /* pointer to argument */
1437 int *idx; /* index in argument, is incremented */
1438 int def; /* default value */
1439{
1440 if (vim_isdigit(p[*idx]))
1441 {
1442 def = atoi((char *)&(p[*idx]));
1443 while (vim_isdigit(p[*idx]))
1444 *idx = *idx + 1;
1445 }
1446 return def;
1447}
1448
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001449#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1450/*
1451 * Setup to use the current locale (for ctype() and many other things).
1452 */
1453 static void
1454init_locale()
1455{
1456 setlocale(LC_ALL, "");
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001457
Bram Moolenaarc6af8122010-05-21 12:04:55 +02001458# ifdef FEAT_GUI_GTK
1459 /* Tell Gtk not to change our locale settings. */
1460 gtk_disable_setlocale();
1461# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001462# if defined(FEAT_FLOAT) && defined(LC_NUMERIC)
1463 /* Make sure strtod() uses a decimal point, not a comma. */
1464 setlocale(LC_NUMERIC, "C");
1465# endif
1466
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001467# ifdef WIN32
1468 /* Apparently MS-Windows printf() may cause a crash when we give it 8-bit
1469 * text while it's expecting text in the current locale. This call avoids
1470 * that. */
1471 setlocale(LC_CTYPE, "C");
1472# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001473
1474# ifdef FEAT_GETTEXT
1475 {
1476 int mustfree = FALSE;
1477 char_u *p;
1478
1479# ifdef DYNAMIC_GETTEXT
1480 /* Initialize the gettext library */
1481 dyn_libintl_init(NULL);
1482# endif
1483 /* expand_env() doesn't work yet, because chartab[] is not initialized
1484 * yet, call vim_getenv() directly */
1485 p = vim_getenv((char_u *)"VIMRUNTIME", &mustfree);
1486 if (p != NULL && *p != NUL)
1487 {
Bram Moolenaar1ad2f132007-06-19 18:27:18 +00001488 vim_snprintf((char *)NameBuff, MAXPATHL, "%s/lang", p);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001489 bindtextdomain(VIMPACKAGE, (char *)NameBuff);
1490 }
1491 if (mustfree)
1492 vim_free(p);
1493 textdomain(VIMPACKAGE);
1494 }
1495# endif
1496}
1497#endif
1498
1499/*
1500 * Check for: [r][e][g][vi|vim|view][diff][ex[im]]
1501 * If the executable name starts with "r" we disable shell commands.
1502 * If the next character is "e" we run in Easy mode.
1503 * If the next character is "g" we run the GUI version.
1504 * If the next characters are "view" we start in readonly mode.
1505 * If the next characters are "diff" or "vimdiff" we start in diff mode.
1506 * If the next characters are "ex" we start in Ex mode. If it's followed
1507 * by "im" use improved Ex mode.
1508 */
1509 static void
1510parse_command_name(parmp)
1511 mparm_T *parmp;
1512{
1513 char_u *initstr;
1514
1515 initstr = gettail((char_u *)parmp->argv[0]);
1516
1517#ifdef MACOS_X_UNIX
1518 /* An issue has been seen when launching Vim in such a way that
1519 * $PWD/$ARGV[0] or $ARGV[0] is not the absolute path to the
1520 * executable or a symbolic link of it. Until this issue is resolved
1521 * we prohibit the GUI from being used.
1522 */
1523 if (STRCMP(initstr, parmp->argv[0]) == 0)
1524 disallow_gui = TRUE;
1525
1526 /* TODO: On MacOS X default to gui if argv[0] ends in:
Bram Moolenaar27dc1952006-03-15 23:06:44 +00001527 * /Vim.app/Contents/MacOS/Vim */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001528#endif
1529
1530#ifdef FEAT_EVAL
1531 set_vim_var_string(VV_PROGNAME, initstr, -1);
1532#endif
1533
1534 if (TOLOWER_ASC(initstr[0]) == 'r')
1535 {
1536 restricted = TRUE;
1537 ++initstr;
1538 }
1539
Bram Moolenaarad249fb2010-05-07 16:35:04 +02001540 /* Use evim mode for "evim" and "egvim", not for "editor". */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001541 if (TOLOWER_ASC(initstr[0]) == 'e'
1542 && (TOLOWER_ASC(initstr[1]) == 'v'
1543 || TOLOWER_ASC(initstr[1]) == 'g'))
1544 {
1545#ifdef FEAT_GUI
1546 gui.starting = TRUE;
1547#endif
1548 parmp->evim_mode = TRUE;
1549 ++initstr;
1550 }
1551
Bram Moolenaar806875d2008-09-18 18:57:10 +00001552 /* "gvim" starts the GUI. Also accept "Gvim" for MS-Windows. */
1553 if (TOLOWER_ASC(initstr[0]) == 'g')
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001554 {
1555 main_start_gui();
1556#ifdef FEAT_GUI
1557 ++initstr;
1558#endif
1559 }
1560
1561 if (STRNICMP(initstr, "view", 4) == 0)
1562 {
1563 readonlymode = TRUE;
1564 curbuf->b_p_ro = TRUE;
1565 p_uc = 10000; /* don't update very often */
1566 initstr += 4;
1567 }
1568 else if (STRNICMP(initstr, "vim", 3) == 0)
1569 initstr += 3;
1570
1571 /* Catch "[r][g]vimdiff" and "[r][g]viewdiff". */
1572 if (STRICMP(initstr, "diff") == 0)
1573 {
1574#ifdef FEAT_DIFF
1575 parmp->diff_mode = TRUE;
1576#else
1577 mch_errmsg(_("This Vim was not compiled with the diff feature."));
1578 mch_errmsg("\n");
1579 mch_exit(2);
1580#endif
1581 }
1582
1583 if (STRNICMP(initstr, "ex", 2) == 0)
1584 {
1585 if (STRNICMP(initstr + 2, "im", 2) == 0)
1586 exmode_active = EXMODE_VIM;
1587 else
1588 exmode_active = EXMODE_NORMAL;
1589 change_compatible(TRUE); /* set 'compatible' */
1590 }
1591}
1592
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001593/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00001594 * Get the name of the display, before gui_prepare() removes it from
1595 * argv[]. Used for the xterm-clipboard display.
1596 *
Bram Moolenaar78e17622007-08-30 10:26:19 +00001597 * Also find the --server... arguments and --socketid and --windowid
Bram Moolenaar58d98232005-07-23 22:25:46 +00001598 */
Bram Moolenaar58d98232005-07-23 22:25:46 +00001599 static void
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001600early_arg_scan(parmp)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00001601 mparm_T *parmp UNUSED;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001602{
Bram Moolenaar03005972008-11-20 13:12:36 +00001603#if defined(FEAT_XCLIPBOARD) || defined(FEAT_CLIENTSERVER) \
1604 || !defined(FEAT_NETBEANS_INTG)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001605 int argc = parmp->argc;
1606 char **argv = parmp->argv;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001607 int i;
1608
1609 for (i = 1; i < argc; i++)
1610 {
1611 if (STRCMP(argv[i], "--") == 0)
1612 break;
1613# ifdef FEAT_XCLIPBOARD
1614 else if (STRICMP(argv[i], "-display") == 0
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001615# if defined(FEAT_GUI_GTK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001616 || STRICMP(argv[i], "--display") == 0
1617# endif
1618 )
1619 {
1620 if (i == argc - 1)
1621 mainerr_arg_missing((char_u *)argv[i]);
1622 xterm_display = argv[++i];
1623 }
1624# endif
1625# ifdef FEAT_CLIENTSERVER
1626 else if (STRICMP(argv[i], "--servername") == 0)
1627 {
1628 if (i == argc - 1)
1629 mainerr_arg_missing((char_u *)argv[i]);
1630 parmp->serverName_arg = (char_u *)argv[++i];
1631 }
Bram Moolenaareb94e552006-03-11 21:35:11 +00001632 else if (STRICMP(argv[i], "--serverlist") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001633 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001634 else if (STRNICMP(argv[i], "--remote", 8) == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001635 {
1636 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001637# ifdef FEAT_GUI
1638 if (strstr(argv[i], "-wait") != 0)
1639 /* don't fork() when starting the GUI to edit files ourself */
1640 gui.dofork = FALSE;
1641# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001642 }
1643# endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001644
1645# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1646# ifdef FEAT_GUI_W32
1647 else if (STRICMP(argv[i], "--windowid") == 0)
1648# else
Bram Moolenaar58d98232005-07-23 22:25:46 +00001649 else if (STRICMP(argv[i], "--socketid") == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001650# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001651 {
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001652 long_u id;
1653 int count;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001654
1655 if (i == argc - 1)
1656 mainerr_arg_missing((char_u *)argv[i]);
1657 if (STRNICMP(argv[i+1], "0x", 2) == 0)
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001658 count = sscanf(&(argv[i + 1][2]), SCANF_HEX_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001659 else
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001660 count = sscanf(argv[i + 1], SCANF_DECIMAL_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001661 if (count != 1)
1662 mainerr(ME_INVALID_ARG, (char_u *)argv[i]);
1663 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00001664# ifdef FEAT_GUI_W32
1665 win_socket_id = id;
1666# else
1667 gtk_socket_id = id;
1668# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001669 i++;
1670 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001671# endif
1672# ifdef FEAT_GUI_GTK
Bram Moolenaar58d98232005-07-23 22:25:46 +00001673 else if (STRICMP(argv[i], "--echo-wid") == 0)
1674 echo_wid_arg = TRUE;
1675# endif
Bram Moolenaar03005972008-11-20 13:12:36 +00001676# ifndef FEAT_NETBEANS_INTG
1677 else if (strncmp(argv[i], "-nb", (size_t)3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +02001678 {
1679 mch_errmsg(_("'-nb' cannot be used: not enabled at compile time\n"));
1680 mch_exit(2);
1681 }
Bram Moolenaar03005972008-11-20 13:12:36 +00001682# endif
1683
Bram Moolenaar58d98232005-07-23 22:25:46 +00001684 }
1685#endif
1686}
1687
1688/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001689 * Scan the command line arguments.
1690 */
1691 static void
1692command_line_scan(parmp)
1693 mparm_T *parmp;
1694{
1695 int argc = parmp->argc;
1696 char **argv = parmp->argv;
1697 int argv_idx; /* index in argv[n][] */
1698 int had_minmin = FALSE; /* found "--" argument */
1699 int want_argument; /* option argument with argument */
1700 int c;
Bram Moolenaar231334e2005-07-25 20:46:57 +00001701 char_u *p = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001702 long n;
1703
1704 --argc;
1705 ++argv;
1706 argv_idx = 1; /* active option letter is argv[0][argv_idx] */
1707 while (argc > 0)
1708 {
1709 /*
1710 * "+" or "+{number}" or "+/{pat}" or "+{command}" argument.
1711 */
1712 if (argv[0][0] == '+' && !had_minmin)
1713 {
1714 if (parmp->n_commands >= MAX_ARG_CMDS)
1715 mainerr(ME_EXTRA_CMD, NULL);
1716 argv_idx = -1; /* skip to next argument */
1717 if (argv[0][1] == NUL)
1718 parmp->commands[parmp->n_commands++] = (char_u *)"$";
1719 else
1720 parmp->commands[parmp->n_commands++] = (char_u *)&(argv[0][1]);
1721 }
1722
1723 /*
1724 * Optional argument.
1725 */
1726 else if (argv[0][0] == '-' && !had_minmin)
1727 {
1728 want_argument = FALSE;
1729 c = argv[0][argv_idx++];
1730#ifdef VMS
1731 /*
1732 * VMS only uses upper case command lines. Interpret "-X" as "-x"
1733 * and "-/X" as "-X".
1734 */
1735 if (c == '/')
1736 {
1737 c = argv[0][argv_idx++];
1738 c = TOUPPER_ASC(c);
1739 }
1740 else
1741 c = TOLOWER_ASC(c);
1742#endif
1743 switch (c)
1744 {
1745 case NUL: /* "vim -" read from stdin */
1746 /* "ex -" silent mode */
1747 if (exmode_active)
1748 silent_mode = TRUE;
1749 else
1750 {
1751 if (parmp->edit_type != EDIT_NONE)
1752 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1753 parmp->edit_type = EDIT_STDIN;
1754 read_cmd_fd = 2; /* read from stderr instead of stdin */
1755 }
1756 argv_idx = -1; /* skip to next argument */
1757 break;
1758
1759 case '-': /* "--" don't take any more option arguments */
1760 /* "--help" give help message */
1761 /* "--version" give version message */
1762 /* "--literal" take files literally */
1763 /* "--nofork" don't fork */
1764 /* "--noplugin[s]" skip plugins */
1765 /* "--cmd <cmd>" execute cmd before vimrc */
1766 if (STRICMP(argv[0] + argv_idx, "help") == 0)
1767 usage();
1768 else if (STRICMP(argv[0] + argv_idx, "version") == 0)
1769 {
1770 Columns = 80; /* need to init Columns */
1771 info_message = TRUE; /* use mch_msg(), not mch_errmsg() */
1772 list_version();
1773 msg_putchar('\n');
1774 msg_didout = FALSE;
1775 mch_exit(0);
1776 }
1777 else if (STRNICMP(argv[0] + argv_idx, "literal", 7) == 0)
1778 {
1779#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
1780 parmp->literal = TRUE;
1781#endif
1782 }
1783 else if (STRNICMP(argv[0] + argv_idx, "nofork", 6) == 0)
1784 {
1785#ifdef FEAT_GUI
1786 gui.dofork = FALSE; /* don't fork() when starting GUI */
1787#endif
1788 }
1789 else if (STRNICMP(argv[0] + argv_idx, "noplugin", 8) == 0)
1790 p_lpl = FALSE;
1791 else if (STRNICMP(argv[0] + argv_idx, "cmd", 3) == 0)
1792 {
1793 want_argument = TRUE;
1794 argv_idx += 3;
1795 }
Bram Moolenaaref94eec2009-11-11 13:22:11 +00001796 else if (STRNICMP(argv[0] + argv_idx, "startuptime", 11) == 0)
1797 {
1798 want_argument = TRUE;
1799 argv_idx += 11;
1800 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001801#ifdef FEAT_CLIENTSERVER
1802 else if (STRNICMP(argv[0] + argv_idx, "serverlist", 10) == 0)
1803 ; /* already processed -- no arg */
1804 else if (STRNICMP(argv[0] + argv_idx, "servername", 10) == 0
1805 || STRNICMP(argv[0] + argv_idx, "serversend", 10) == 0)
1806 {
1807 /* already processed -- snatch the following arg */
1808 if (argc > 1)
1809 {
1810 --argc;
1811 ++argv;
1812 }
1813 }
1814#endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001815#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1816# ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001817 else if (STRNICMP(argv[0] + argv_idx, "socketid", 8) == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001818# else
1819 else if (STRNICMP(argv[0] + argv_idx, "windowid", 8) == 0)
1820# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001821 {
1822 /* already processed -- snatch the following arg */
1823 if (argc > 1)
1824 {
1825 --argc;
1826 ++argv;
1827 }
1828 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001829#endif
1830#ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001831 else if (STRNICMP(argv[0] + argv_idx, "echo-wid", 8) == 0)
1832 {
1833 /* already processed, skip */
1834 }
1835#endif
1836 else
1837 {
1838 if (argv[0][argv_idx])
1839 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
1840 had_minmin = TRUE;
1841 }
1842 if (!want_argument)
1843 argv_idx = -1; /* skip to next argument */
1844 break;
1845
1846 case 'A': /* "-A" start in Arabic mode */
1847#ifdef FEAT_ARABIC
1848 set_option_value((char_u *)"arabic", 1L, NULL, 0);
1849#else
1850 mch_errmsg(_(e_noarabic));
1851 mch_exit(2);
1852#endif
1853 break;
1854
1855 case 'b': /* "-b" binary mode */
Bram Moolenaar231334e2005-07-25 20:46:57 +00001856 /* Needs to be effective before expanding file names, because
1857 * for Win32 this makes us edit a shortcut file itself,
1858 * instead of the file it links to. */
1859 set_options_bin(curbuf->b_p_bin, 1, 0);
1860 curbuf->b_p_bin = 1; /* binary file I/O */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001861 break;
1862
1863 case 'C': /* "-C" Compatible */
1864 change_compatible(TRUE);
1865 break;
1866
1867 case 'e': /* "-e" Ex mode */
1868 exmode_active = EXMODE_NORMAL;
1869 break;
1870
1871 case 'E': /* "-E" Improved Ex mode */
1872 exmode_active = EXMODE_VIM;
1873 break;
1874
1875 case 'f': /* "-f" GUI: run in foreground. Amiga: open
1876 window directly, not with newcli */
1877#ifdef FEAT_GUI
1878 gui.dofork = FALSE; /* don't fork() when starting GUI */
1879#endif
1880 break;
1881
1882 case 'g': /* "-g" start GUI */
1883 main_start_gui();
1884 break;
1885
1886 case 'F': /* "-F" start in Farsi mode: rl + fkmap set */
1887#ifdef FEAT_FKMAP
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00001888 p_fkmap = TRUE;
1889 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001890#else
1891 mch_errmsg(_(e_nofarsi));
1892 mch_exit(2);
1893#endif
1894 break;
1895
1896 case 'h': /* "-h" give help message */
1897#ifdef FEAT_GUI_GNOME
1898 /* Tell usage() to exit for "gvim". */
1899 gui.starting = FALSE;
1900#endif
1901 usage();
1902 break;
1903
1904 case 'H': /* "-H" start in Hebrew mode: rl + hkmap set */
1905#ifdef FEAT_RIGHTLEFT
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00001906 p_hkmap = TRUE;
1907 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001908#else
1909 mch_errmsg(_(e_nohebrew));
1910 mch_exit(2);
1911#endif
1912 break;
1913
1914 case 'l': /* "-l" lisp mode, 'lisp' and 'showmatch' on */
1915#ifdef FEAT_LISP
1916 set_option_value((char_u *)"lisp", 1L, NULL, 0);
1917 p_sm = TRUE;
1918#endif
1919 break;
1920
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001921 case 'M': /* "-M" no changes or writing of files */
1922 reset_modifiable();
1923 /* FALLTHROUGH */
1924
1925 case 'm': /* "-m" no writing of files */
1926 p_write = FALSE;
1927 break;
1928
1929 case 'y': /* "-y" easy mode */
1930#ifdef FEAT_GUI
1931 gui.starting = TRUE; /* start GUI a bit later */
1932#endif
1933 parmp->evim_mode = TRUE;
1934 break;
1935
1936 case 'N': /* "-N" Nocompatible */
1937 change_compatible(FALSE);
1938 break;
1939
1940 case 'n': /* "-n" no swap file */
Bram Moolenaar67c53842010-05-22 18:28:27 +02001941#ifdef FEAT_NETBEANS_INTG
1942 /* checking for "-nb", netbeans parameters */
1943 if (argv[0][argv_idx] == 'b')
1944 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02001945 netbeansArg = argv[0];
1946 argv_idx = -1; /* skip to next argument */
1947 }
1948 else
1949#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001950 parmp->no_swap_file = TRUE;
1951 break;
1952
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001953 case 'p': /* "-p[N]" open N tab pages */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00001954#ifdef TARGET_API_MAC_OSX
1955 /* For some reason on MacOS X, an argument like:
1956 -psn_0_10223617 is passed in when invoke from Finder
1957 or with the 'open' command */
1958 if (argv[0][argv_idx] == 's')
1959 {
1960 argv_idx = -1; /* bypass full -psn */
1961 main_start_gui();
1962 break;
1963 }
1964#endif
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001965#ifdef FEAT_WINDOWS
1966 /* default is 0: open window for each file */
1967 parmp->window_count = get_number_arg((char_u *)argv[0],
1968 &argv_idx, 0);
1969 parmp->window_layout = WIN_TABS;
1970#endif
1971 break;
1972
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001973 case 'o': /* "-o[N]" open N horizontal split windows */
1974#ifdef FEAT_WINDOWS
1975 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00001976 parmp->window_count = get_number_arg((char_u *)argv[0],
1977 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001978 parmp->window_layout = WIN_HOR;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001979#endif
1980 break;
1981
1982 case 'O': /* "-O[N]" open N vertical split windows */
1983#if defined(FEAT_VERTSPLIT) && defined(FEAT_WINDOWS)
1984 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00001985 parmp->window_count = get_number_arg((char_u *)argv[0],
1986 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001987 parmp->window_layout = WIN_VER;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001988#endif
1989 break;
1990
1991#ifdef FEAT_QUICKFIX
1992 case 'q': /* "-q" QuickFix mode */
1993 if (parmp->edit_type != EDIT_NONE)
1994 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1995 parmp->edit_type = EDIT_QF;
1996 if (argv[0][argv_idx]) /* "-q{errorfile}" */
1997 {
1998 parmp->use_ef = (char_u *)argv[0] + argv_idx;
1999 argv_idx = -1;
2000 }
2001 else if (argc > 1) /* "-q {errorfile}" */
2002 want_argument = TRUE;
2003 break;
2004#endif
2005
2006 case 'R': /* "-R" readonly mode */
2007 readonlymode = TRUE;
2008 curbuf->b_p_ro = TRUE;
2009 p_uc = 10000; /* don't update very often */
2010 break;
2011
2012 case 'r': /* "-r" recovery mode */
2013 case 'L': /* "-L" recovery mode */
2014 recoverymode = 1;
2015 break;
2016
2017 case 's':
2018 if (exmode_active) /* "-s" silent (batch) mode */
2019 silent_mode = TRUE;
2020 else /* "-s {scriptin}" read from script file */
2021 want_argument = TRUE;
2022 break;
2023
2024 case 't': /* "-t {tag}" or "-t{tag}" jump to tag */
2025 if (parmp->edit_type != EDIT_NONE)
2026 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2027 parmp->edit_type = EDIT_TAG;
2028 if (argv[0][argv_idx]) /* "-t{tag}" */
2029 {
2030 parmp->tagname = (char_u *)argv[0] + argv_idx;
2031 argv_idx = -1;
2032 }
2033 else /* "-t {tag}" */
2034 want_argument = TRUE;
2035 break;
2036
2037#ifdef FEAT_EVAL
2038 case 'D': /* "-D" Debugging */
2039 parmp->use_debug_break_level = 9999;
2040 break;
2041#endif
2042#ifdef FEAT_DIFF
2043 case 'd': /* "-d" 'diff' */
2044# ifdef AMIGA
2045 /* check for "-dev {device}" */
2046 if (argv[0][argv_idx] == 'e' && argv[0][argv_idx + 1] == 'v')
2047 want_argument = TRUE;
2048 else
2049# endif
2050 parmp->diff_mode = TRUE;
2051 break;
2052#endif
2053 case 'V': /* "-V{N}" Verbose level */
2054 /* default is 10: a little bit verbose */
2055 p_verbose = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2056 if (argv[0][argv_idx] != NUL)
2057 {
2058 set_option_value((char_u *)"verbosefile", 0L,
2059 (char_u *)argv[0] + argv_idx, 0);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002060 argv_idx = (int)STRLEN(argv[0]);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002061 }
2062 break;
2063
2064 case 'v': /* "-v" Vi-mode (as if called "vi") */
2065 exmode_active = 0;
2066#ifdef FEAT_GUI
2067 gui.starting = FALSE; /* don't start GUI */
2068#endif
2069 break;
2070
2071 case 'w': /* "-w{number}" set window height */
2072 /* "-w {scriptout}" write to script */
2073 if (vim_isdigit(((char_u *)argv[0])[argv_idx]))
2074 {
2075 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2076 set_option_value((char_u *)"window", n, NULL, 0);
2077 break;
2078 }
2079 want_argument = TRUE;
2080 break;
2081
2082#ifdef FEAT_CRYPT
2083 case 'x': /* "-x" encrypted reading/writing of files */
2084 parmp->ask_for_key = TRUE;
2085 break;
2086#endif
2087
2088 case 'X': /* "-X" don't connect to X server */
2089#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
2090 x_no_connect = TRUE;
2091#endif
2092 break;
2093
2094 case 'Z': /* "-Z" restricted mode */
2095 restricted = TRUE;
2096 break;
2097
2098 case 'c': /* "-c{command}" or "-c {command}" execute
2099 command */
2100 if (argv[0][argv_idx] != NUL)
2101 {
2102 if (parmp->n_commands >= MAX_ARG_CMDS)
2103 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar231334e2005-07-25 20:46:57 +00002104 parmp->commands[parmp->n_commands++] = (char_u *)argv[0]
2105 + argv_idx;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002106 argv_idx = -1;
2107 break;
2108 }
2109 /*FALLTHROUGH*/
2110 case 'S': /* "-S {file}" execute Vim script */
2111 case 'i': /* "-i {viminfo}" use for viminfo */
2112#ifndef FEAT_DIFF
2113 case 'd': /* "-d {device}" device (for Amiga) */
2114#endif
2115 case 'T': /* "-T {terminal}" terminal name */
2116 case 'u': /* "-u {vimrc}" vim inits file */
2117 case 'U': /* "-U {gvimrc}" gvim inits file */
2118 case 'W': /* "-W {scriptout}" overwrite */
2119#ifdef FEAT_GUI_W32
2120 case 'P': /* "-P {parent title}" MDI parent */
2121#endif
2122 want_argument = TRUE;
2123 break;
2124
2125 default:
2126 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
2127 }
2128
2129 /*
2130 * Handle option arguments with argument.
2131 */
2132 if (want_argument)
2133 {
2134 /*
2135 * Check for garbage immediately after the option letter.
2136 */
2137 if (argv[0][argv_idx] != NUL)
2138 mainerr(ME_GARBAGE, (char_u *)argv[0]);
2139
2140 --argc;
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002141 if (argc < 1 && c != 'S') /* -S has an optional argument */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002142 mainerr_arg_missing((char_u *)argv[0]);
2143 ++argv;
2144 argv_idx = -1;
2145
2146 switch (c)
2147 {
2148 case 'c': /* "-c {command}" execute command */
2149 case 'S': /* "-S {file}" execute Vim script */
2150 if (parmp->n_commands >= MAX_ARG_CMDS)
2151 mainerr(ME_EXTRA_CMD, NULL);
2152 if (c == 'S')
2153 {
2154 char *a;
2155
2156 if (argc < 1)
2157 /* "-S" without argument: use default session file
2158 * name. */
2159 a = SESSION_FILE;
2160 else if (argv[0][0] == '-')
2161 {
2162 /* "-S" followed by another option: use default
2163 * session file name. */
2164 a = SESSION_FILE;
2165 ++argc;
2166 --argv;
2167 }
2168 else
2169 a = argv[0];
2170 p = alloc((unsigned)(STRLEN(a) + 4));
2171 if (p == NULL)
2172 mch_exit(2);
2173 sprintf((char *)p, "so %s", a);
2174 parmp->cmds_tofree[parmp->n_commands] = TRUE;
2175 parmp->commands[parmp->n_commands++] = p;
2176 }
2177 else
Bram Moolenaar231334e2005-07-25 20:46:57 +00002178 parmp->commands[parmp->n_commands++] =
2179 (char_u *)argv[0];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002180 break;
2181
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002182 case '-':
2183 if (argv[-1][2] == 'c')
2184 {
2185 /* "--cmd {command}" execute command */
2186 if (parmp->n_pre_commands >= MAX_ARG_CMDS)
2187 mainerr(ME_EXTRA_CMD, NULL);
2188 parmp->pre_commands[parmp->n_pre_commands++] =
Bram Moolenaar231334e2005-07-25 20:46:57 +00002189 (char_u *)argv[0];
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002190 }
2191 /* "--startuptime <file>" already handled */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002192 break;
2193
2194 /* case 'd': -d {device} is handled in mch_check_win() for the
2195 * Amiga */
2196
2197#ifdef FEAT_QUICKFIX
2198 case 'q': /* "-q {errorfile}" QuickFix mode */
2199 parmp->use_ef = (char_u *)argv[0];
2200 break;
2201#endif
2202
2203 case 'i': /* "-i {viminfo}" use for viminfo */
2204 use_viminfo = (char_u *)argv[0];
2205 break;
2206
2207 case 's': /* "-s {scriptin}" read from script file */
2208 if (scriptin[0] != NULL)
2209 {
2210scripterror:
2211 mch_errmsg(_("Attempt to open script file again: \""));
2212 mch_errmsg(argv[-1]);
2213 mch_errmsg(" ");
2214 mch_errmsg(argv[0]);
2215 mch_errmsg("\"\n");
2216 mch_exit(2);
2217 }
2218 if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL)
2219 {
2220 mch_errmsg(_("Cannot open for reading: \""));
2221 mch_errmsg(argv[0]);
2222 mch_errmsg("\"\n");
2223 mch_exit(2);
2224 }
2225 if (save_typebuf() == FAIL)
2226 mch_exit(2); /* out of memory */
2227 break;
2228
2229 case 't': /* "-t {tag}" */
2230 parmp->tagname = (char_u *)argv[0];
2231 break;
2232
2233 case 'T': /* "-T {terminal}" terminal name */
2234 /*
2235 * The -T term argument is always available and when
2236 * HAVE_TERMLIB is supported it overrides the environment
2237 * variable TERM.
2238 */
2239#ifdef FEAT_GUI
2240 if (term_is_gui((char_u *)argv[0]))
2241 gui.starting = TRUE; /* start GUI a bit later */
2242 else
2243#endif
2244 parmp->term = (char_u *)argv[0];
2245 break;
2246
2247 case 'u': /* "-u {vimrc}" vim inits file */
2248 parmp->use_vimrc = (char_u *)argv[0];
2249 break;
2250
2251 case 'U': /* "-U {gvimrc}" gvim inits file */
2252#ifdef FEAT_GUI
2253 use_gvimrc = (char_u *)argv[0];
2254#endif
2255 break;
2256
2257 case 'w': /* "-w {nr}" 'window' value */
2258 /* "-w {scriptout}" append to script file */
2259 if (vim_isdigit(*((char_u *)argv[0])))
2260 {
2261 argv_idx = 0;
2262 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2263 set_option_value((char_u *)"window", n, NULL, 0);
2264 argv_idx = -1;
2265 break;
2266 }
2267 /*FALLTHROUGH*/
2268 case 'W': /* "-W {scriptout}" overwrite script file */
2269 if (scriptout != NULL)
2270 goto scripterror;
2271 if ((scriptout = mch_fopen(argv[0],
2272 c == 'w' ? APPENDBIN : WRITEBIN)) == NULL)
2273 {
2274 mch_errmsg(_("Cannot open for script output: \""));
2275 mch_errmsg(argv[0]);
2276 mch_errmsg("\"\n");
2277 mch_exit(2);
2278 }
2279 break;
2280
2281#ifdef FEAT_GUI_W32
2282 case 'P': /* "-P {parent title}" MDI parent */
2283 gui_mch_set_parent(argv[0]);
2284 break;
2285#endif
2286 }
2287 }
2288 }
2289
2290 /*
2291 * File name argument.
2292 */
2293 else
2294 {
2295 argv_idx = -1; /* skip to next argument */
2296
2297 /* Check for only one type of editing. */
2298 if (parmp->edit_type != EDIT_NONE && parmp->edit_type != EDIT_FILE)
2299 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2300 parmp->edit_type = EDIT_FILE;
2301
2302#ifdef MSWIN
2303 /* Remember if the argument was a full path before changing
2304 * slashes to backslashes. */
2305 if (argv[0][0] != NUL && argv[0][1] == ':' && argv[0][2] == '\\')
2306 parmp->full_path = TRUE;
2307#endif
2308
2309 /* Add the file to the global argument list. */
2310 if (ga_grow(&global_alist.al_ga, 1) == FAIL
2311 || (p = vim_strsave((char_u *)argv[0])) == NULL)
2312 mch_exit(2);
2313#ifdef FEAT_DIFF
2314 if (parmp->diff_mode && mch_isdir(p) && GARGCOUNT > 0
2315 && !mch_isdir(alist_name(&GARGLIST[0])))
2316 {
2317 char_u *r;
2318
2319 r = concat_fnames(p, gettail(alist_name(&GARGLIST[0])), TRUE);
2320 if (r != NULL)
2321 {
2322 vim_free(p);
2323 p = r;
2324 }
2325 }
2326#endif
2327#if defined(__CYGWIN32__) && !defined(WIN32)
2328 /*
2329 * If vim is invoked by non-Cygwin tools, convert away any
2330 * DOS paths, so things like .swp files are created correctly.
2331 * Look for evidence of non-Cygwin paths before we bother.
2332 * This is only for when using the Unix files.
2333 */
Bram Moolenaarad249fb2010-05-07 16:35:04 +02002334 if (strpbrk(p, "\\:") != NULL && !path_with_url(p))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002335 {
2336 char posix_path[PATH_MAX];
2337
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002338# if CYGWIN_VERSION_DLL_MAJOR >= 1007
2339 cygwin_conv_path(CCP_WIN_A_TO_POSIX, p, posix_path, PATH_MAX);
2340# else
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002341 cygwin_conv_to_posix_path(p, posix_path);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002342# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002343 vim_free(p);
2344 p = vim_strsave(posix_path);
2345 if (p == NULL)
2346 mch_exit(2);
2347 }
2348#endif
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002349
2350#ifdef USE_FNAME_CASE
2351 /* Make the case of the file name match the actual file. */
2352 fname_case(p, 0);
2353#endif
2354
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002355 alist_add(&global_alist, p,
2356#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
Bram Moolenaar231334e2005-07-25 20:46:57 +00002357 parmp->literal ? 2 : 0 /* add buffer nr after exp. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002358#else
2359 2 /* add buffer number now and use curbuf */
2360#endif
2361 );
2362
2363#if defined(FEAT_MBYTE) && defined(WIN32)
2364 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002365 /* Remember this argument has been added to the argument list.
2366 * Needed when 'encoding' is changed. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002367 used_file_arg(argv[0], parmp->literal, parmp->full_path,
Bram Moolenaar688e5f72008-07-24 11:51:40 +00002368# ifdef FEAT_DIFF
2369 parmp->diff_mode
2370# else
2371 FALSE
2372# endif
2373 );
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002374 }
2375#endif
2376 }
2377
2378 /*
2379 * If there are no more letters after the current "-", go to next
2380 * argument. argv_idx is set to -1 when the current argument is to be
2381 * skipped.
2382 */
2383 if (argv_idx <= 0 || argv[0][argv_idx] == NUL)
2384 {
2385 --argc;
2386 ++argv;
2387 argv_idx = 1;
2388 }
2389 }
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002390
2391#ifdef FEAT_EVAL
2392 /* If there is a "+123" or "-c" command, set v:swapcommand to the first
2393 * one. */
2394 if (parmp->n_commands > 0)
2395 {
2396 p = alloc((unsigned)STRLEN(parmp->commands[0]) + 3);
2397 if (p != NULL)
2398 {
2399 sprintf((char *)p, ":%s\r", parmp->commands[0]);
2400 set_vim_var_string(VV_SWAPCOMMAND, p, -1);
2401 vim_free(p);
2402 }
2403 }
2404#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002405}
2406
2407/*
2408 * Print a warning if stdout is not a terminal.
2409 * When starting in Ex mode and commands come from a file, set Silent mode.
2410 */
2411 static void
2412check_tty(parmp)
2413 mparm_T *parmp;
2414{
2415 int input_isatty; /* is active input a terminal? */
2416
2417 input_isatty = mch_input_isatty();
2418 if (exmode_active)
2419 {
2420 if (!input_isatty)
2421 silent_mode = TRUE;
2422 }
2423 else if (parmp->want_full_screen && (!parmp->stdout_isatty || !input_isatty)
2424#ifdef FEAT_GUI
2425 /* don't want the delay when started from the desktop */
2426 && !gui.starting
2427#endif
2428 )
2429 {
2430#ifdef NBDEBUG
2431 /*
2432 * This shouldn't be necessary. But if I run netbeans with the log
2433 * output coming to the console and XOpenDisplay fails, I get vim
2434 * trying to start with input/output to my console tty. This fills my
2435 * input buffer so fast I can't even kill the process in under 2
Bram Moolenaar49325942007-05-10 19:19:59 +00002436 * minutes (and it beeps continuously the whole time :-)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002437 */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002438 if (netbeans_active() && (!parmp->stdout_isatty || !input_isatty))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002439 {
2440 mch_errmsg(_("Vim: Error: Failure to start gvim from NetBeans\n"));
2441 exit(1);
2442 }
2443#endif
2444 if (!parmp->stdout_isatty)
2445 mch_errmsg(_("Vim: Warning: Output is not to a terminal\n"));
2446 if (!input_isatty)
2447 mch_errmsg(_("Vim: Warning: Input is not from a terminal\n"));
2448 out_flush();
2449 if (scriptin[0] == NULL)
2450 ui_delay(2000L, TRUE);
2451 TIME_MSG("Warning delay");
2452 }
2453}
2454
2455/*
2456 * Read text from stdin.
2457 */
2458 static void
2459read_stdin()
2460{
2461 int i;
2462
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002463#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002464 /* When getting the ATTENTION prompt here, use a dialog */
2465 swap_exists_action = SEA_DIALOG;
2466#endif
2467 no_wait_return = TRUE;
2468 i = msg_didany;
2469 set_buflisted(TRUE);
2470 (void)open_buffer(TRUE, NULL); /* create memfile and read file */
2471 no_wait_return = FALSE;
2472 msg_didany = i;
2473 TIME_MSG("reading stdin");
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002474#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002475 check_swap_exists_action();
2476#endif
2477#if !(defined(AMIGA) || defined(MACOS))
2478 /*
2479 * Close stdin and dup it from stderr. Required for GPM to work
2480 * properly, and for running external commands.
2481 * Is there any other system that cannot do this?
2482 */
2483 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00002484 ignored = dup(2);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002485#endif
2486}
2487
2488/*
2489 * Create the requested number of windows and edit buffers in them.
2490 * Also does recovery if "recoverymode" set.
2491 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002492 static void
2493create_windows(parmp)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002494 mparm_T *parmp UNUSED;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002495{
2496#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002497 int dorewind;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002498 int done = 0;
2499
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002500 /*
2501 * Create the number of windows that was requested.
2502 */
2503 if (parmp->window_count == -1) /* was not set */
2504 parmp->window_count = 1;
2505 if (parmp->window_count == 0)
2506 parmp->window_count = GARGCOUNT;
2507 if (parmp->window_count > 1)
2508 {
2509 /* Don't change the windows if there was a command in .vimrc that
2510 * already split some windows */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002511 if (parmp->window_layout == 0)
2512 parmp->window_layout = WIN_HOR;
2513 if (parmp->window_layout == WIN_TABS)
2514 {
2515 parmp->window_count = make_tabpages(parmp->window_count);
2516 TIME_MSG("making tab pages");
2517 }
2518 else if (firstwin->w_next == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002519 {
2520 parmp->window_count = make_windows(parmp->window_count,
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002521 parmp->window_layout == WIN_VER);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002522 TIME_MSG("making windows");
2523 }
2524 else
2525 parmp->window_count = win_count();
2526 }
2527 else
2528 parmp->window_count = 1;
2529#endif
2530
2531 if (recoverymode) /* do recover */
2532 {
2533 msg_scroll = TRUE; /* scroll message up */
2534 ml_recover();
2535 if (curbuf->b_ml.ml_mfp == NULL) /* failed */
2536 getout(1);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002537 do_modelines(0); /* do modelines */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002538 }
2539 else
2540 {
2541 /*
2542 * Open a buffer for windows that don't have one yet.
2543 * Commands in the .vimrc might have loaded a file or split the window.
2544 * Watch out for autocommands that delete a window.
2545 */
2546#ifdef FEAT_AUTOCMD
2547 /*
2548 * Don't execute Win/Buf Enter/Leave autocommands here
2549 */
2550 ++autocmd_no_enter;
2551 ++autocmd_no_leave;
2552#endif
2553#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002554 dorewind = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002555 while (done++ < 1000)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002556 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002557 if (dorewind)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002558 {
2559 if (parmp->window_layout == WIN_TABS)
2560 goto_tabpage(1);
2561 else
2562 curwin = firstwin;
2563 }
2564 else if (parmp->window_layout == WIN_TABS)
2565 {
2566 if (curtab->tp_next == NULL)
2567 break;
2568 goto_tabpage(0);
2569 }
2570 else
2571 {
2572 if (curwin->w_next == NULL)
2573 break;
2574 curwin = curwin->w_next;
2575 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002576 dorewind = FALSE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002577#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002578 curbuf = curwin->w_buffer;
2579 if (curbuf->b_ml.ml_mfp == NULL)
2580 {
2581#ifdef FEAT_FOLDING
2582 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2583 if (p_fdls >= 0)
2584 curwin->w_p_fdl = p_fdls;
2585#endif
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002586#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002587 /* When getting the ATTENTION prompt here, use a dialog */
2588 swap_exists_action = SEA_DIALOG;
2589#endif
2590 set_buflisted(TRUE);
2591 (void)open_buffer(FALSE, NULL); /* create memfile, read file */
2592
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002593#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar84212822006-11-07 21:59:47 +00002594 if (swap_exists_action == SEA_QUIT)
2595 {
2596 if (got_int || only_one_window())
2597 {
2598 /* abort selected or quit and only one window */
2599 did_emsg = FALSE; /* avoid hit-enter prompt */
2600 getout(1);
2601 }
2602 /* We can't close the window, it would disturb what
2603 * happens next. Clear the file name and set the arg
2604 * index to -1 to delete it later. */
2605 setfname(curbuf, NULL, NULL, FALSE);
2606 curwin->w_arg_idx = -1;
2607 swap_exists_action = SEA_NONE;
2608 }
2609 else
2610 handle_swap_exists(NULL);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002611#endif
2612#ifdef FEAT_AUTOCMD
Bram Moolenaar89d40322006-08-29 15:30:07 +00002613 dorewind = TRUE; /* start again */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002614#endif
2615 }
2616#ifdef FEAT_WINDOWS
2617 ui_breakcheck();
2618 if (got_int)
2619 {
2620 (void)vgetc(); /* only break the file loading, not the rest */
2621 break;
2622 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002623 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002624#endif
2625#ifdef FEAT_WINDOWS
2626 if (parmp->window_layout == WIN_TABS)
2627 goto_tabpage(1);
2628 else
2629 curwin = firstwin;
2630 curbuf = curwin->w_buffer;
2631#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002632#ifdef FEAT_AUTOCMD
2633 --autocmd_no_enter;
2634 --autocmd_no_leave;
2635#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002636 }
2637}
2638
2639#ifdef FEAT_WINDOWS
2640 /*
2641 * If opened more than one window, start editing files in the other
2642 * windows. make_windows() has already opened the windows.
2643 */
2644 static void
2645edit_buffers(parmp)
2646 mparm_T *parmp;
2647{
2648 int arg_idx; /* index in argument list */
2649 int i;
Bram Moolenaar84212822006-11-07 21:59:47 +00002650 int advance = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002651
2652# ifdef FEAT_AUTOCMD
2653 /*
2654 * Don't execute Win/Buf Enter/Leave autocommands here
2655 */
2656 ++autocmd_no_enter;
2657 ++autocmd_no_leave;
2658# endif
Bram Moolenaar84212822006-11-07 21:59:47 +00002659
2660 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2661 if (curwin->w_arg_idx == -1)
2662 {
2663 win_close(curwin, TRUE);
2664 advance = FALSE;
2665 }
2666
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002667 arg_idx = 1;
2668 for (i = 1; i < parmp->window_count; ++i)
2669 {
Bram Moolenaar84212822006-11-07 21:59:47 +00002670 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2671 if (curwin->w_arg_idx == -1)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002672 {
Bram Moolenaar84212822006-11-07 21:59:47 +00002673 ++arg_idx;
2674 win_close(curwin, TRUE);
2675 advance = FALSE;
2676 continue;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002677 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002678
Bram Moolenaar84212822006-11-07 21:59:47 +00002679 if (advance)
2680 {
2681 if (parmp->window_layout == WIN_TABS)
2682 {
2683 if (curtab->tp_next == NULL) /* just checking */
2684 break;
2685 goto_tabpage(0);
2686 }
2687 else
2688 {
2689 if (curwin->w_next == NULL) /* just checking */
2690 break;
2691 win_enter(curwin->w_next, FALSE);
2692 }
2693 }
2694 advance = TRUE;
2695
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002696 /* Only open the file if there is no file in this window yet (that can
Bram Moolenaar84212822006-11-07 21:59:47 +00002697 * happen when .vimrc contains ":sall"). */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002698 if (curbuf == firstwin->w_buffer || curbuf->b_ffname == NULL)
2699 {
2700 curwin->w_arg_idx = arg_idx;
Bram Moolenaar84212822006-11-07 21:59:47 +00002701 /* Edit file from arg list, if there is one. When "Quit" selected
2702 * at the ATTENTION prompt close the window. */
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002703# ifdef HAS_SWAP_EXISTS_ACTION
2704 swap_exists_did_quit = FALSE;
2705# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002706 (void)do_ecmd(0, arg_idx < GARGCOUNT
2707 ? alist_name(&GARGLIST[arg_idx]) : NULL,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002708 NULL, NULL, ECMD_LASTL, ECMD_HIDE, curwin);
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002709# ifdef HAS_SWAP_EXISTS_ACTION
2710 if (swap_exists_did_quit)
Bram Moolenaar84212822006-11-07 21:59:47 +00002711 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002712 /* abort or quit selected */
Bram Moolenaar84212822006-11-07 21:59:47 +00002713 if (got_int || only_one_window())
2714 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002715 /* abort selected and only one window */
Bram Moolenaar84212822006-11-07 21:59:47 +00002716 did_emsg = FALSE; /* avoid hit-enter prompt */
2717 getout(1);
2718 }
2719 win_close(curwin, TRUE);
2720 advance = FALSE;
2721 }
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002722# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002723 if (arg_idx == GARGCOUNT - 1)
2724 arg_had_last = TRUE;
2725 ++arg_idx;
2726 }
2727 ui_breakcheck();
2728 if (got_int)
2729 {
2730 (void)vgetc(); /* only break the file loading, not the rest */
2731 break;
2732 }
2733 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002734
2735 if (parmp->window_layout == WIN_TABS)
2736 goto_tabpage(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002737# ifdef FEAT_AUTOCMD
2738 --autocmd_no_enter;
2739# endif
2740 win_enter(firstwin, FALSE); /* back to first window */
2741# ifdef FEAT_AUTOCMD
2742 --autocmd_no_leave;
2743# endif
2744 TIME_MSG("editing files in windows");
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002745 if (parmp->window_count > 1 && parmp->window_layout != WIN_TABS)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002746 win_equal(curwin, FALSE, 'b'); /* adjust heights */
2747}
2748#endif /* FEAT_WINDOWS */
2749
2750/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002751 * Execute the commands from --cmd arguments "cmds[cnt]".
2752 */
2753 static void
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002754exe_pre_commands(parmp)
2755 mparm_T *parmp;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002756{
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002757 char_u **cmds = parmp->pre_commands;
2758 int cnt = parmp->n_pre_commands;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002759 int i;
2760
2761 if (cnt > 0)
2762 {
2763 curwin->w_cursor.lnum = 0; /* just in case.. */
2764 sourcing_name = (char_u *)_("pre-vimrc command line");
2765# ifdef FEAT_EVAL
2766 current_SID = SID_CMDARG;
2767# endif
2768 for (i = 0; i < cnt; ++i)
2769 do_cmdline_cmd(cmds[i]);
2770 sourcing_name = NULL;
2771# ifdef FEAT_EVAL
2772 current_SID = 0;
2773# endif
2774 TIME_MSG("--cmd commands");
2775 }
2776}
2777
2778/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002779 * Execute "+", "-c" and "-S" arguments.
2780 */
2781 static void
2782exe_commands(parmp)
2783 mparm_T *parmp;
2784{
2785 int i;
2786
2787 /*
2788 * We start commands on line 0, make "vim +/pat file" match a
2789 * pattern on line 1. But don't move the cursor when an autocommand
2790 * with g`" was used.
2791 */
2792 msg_scroll = TRUE;
2793 if (parmp->tagname == NULL && curwin->w_cursor.lnum <= 1)
2794 curwin->w_cursor.lnum = 0;
2795 sourcing_name = (char_u *)"command line";
2796#ifdef FEAT_EVAL
2797 current_SID = SID_CARG;
2798#endif
2799 for (i = 0; i < parmp->n_commands; ++i)
2800 {
2801 do_cmdline_cmd(parmp->commands[i]);
2802 if (parmp->cmds_tofree[i])
2803 vim_free(parmp->commands[i]);
2804 }
2805 sourcing_name = NULL;
2806#ifdef FEAT_EVAL
2807 current_SID = 0;
2808#endif
2809 if (curwin->w_cursor.lnum == 0)
2810 curwin->w_cursor.lnum = 1;
2811
2812 if (!exmode_active)
2813 msg_scroll = FALSE;
2814
2815#ifdef FEAT_QUICKFIX
2816 /* When started with "-q errorfile" jump to first error again. */
2817 if (parmp->edit_type == EDIT_QF)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002818 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002819#endif
2820 TIME_MSG("executing command arguments");
2821}
2822
2823/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002824 * Source startup scripts.
2825 */
2826 static void
2827source_startup_scripts(parmp)
2828 mparm_T *parmp;
2829{
2830 int i;
2831
2832 /*
2833 * For "evim" source evim.vim first of all, so that the user can overrule
2834 * any things he doesn't like.
2835 */
2836 if (parmp->evim_mode)
2837 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002838 (void)do_source((char_u *)EVIM_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002839 TIME_MSG("source evim file");
2840 }
2841
2842 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002843 * If -u argument given, use only the initializations from that file and
Bram Moolenaar58d98232005-07-23 22:25:46 +00002844 * nothing else.
2845 */
2846 if (parmp->use_vimrc != NULL)
2847 {
Bram Moolenaar231334e2005-07-25 20:46:57 +00002848 if (STRCMP(parmp->use_vimrc, "NONE") == 0
2849 || STRCMP(parmp->use_vimrc, "NORC") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002850 {
2851#ifdef FEAT_GUI
2852 if (use_gvimrc == NULL) /* don't load gvimrc either */
2853 use_gvimrc = parmp->use_vimrc;
2854#endif
2855 if (parmp->use_vimrc[2] == 'N')
2856 p_lpl = FALSE; /* don't load plugins either */
2857 }
2858 else
2859 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002860 if (do_source(parmp->use_vimrc, FALSE, DOSO_NONE) != OK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002861 EMSG2(_("E282: Cannot read from \"%s\""), parmp->use_vimrc);
2862 }
2863 }
2864 else if (!silent_mode)
2865 {
2866#ifdef AMIGA
2867 struct Process *proc = (struct Process *)FindTask(0L);
2868 APTR save_winptr = proc->pr_WindowPtr;
2869
2870 /* Avoid a requester here for a volume that doesn't exist. */
2871 proc->pr_WindowPtr = (APTR)-1L;
2872#endif
2873
2874 /*
2875 * Get system wide defaults, if the file name is defined.
2876 */
2877#ifdef SYS_VIMRC_FILE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002878 (void)do_source((char_u *)SYS_VIMRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002879#endif
Bram Moolenaar1056d982006-03-09 22:37:52 +00002880#ifdef MACOS_X
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002881 (void)do_source((char_u *)"$VIMRUNTIME/macmap.vim", FALSE, DOSO_NONE);
Bram Moolenaar1056d982006-03-09 22:37:52 +00002882#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00002883
2884 /*
2885 * Try to read initialization commands from the following places:
2886 * - environment variable VIMINIT
2887 * - user vimrc file (s:.vimrc for Amiga, ~/.vimrc otherwise)
2888 * - second user vimrc file ($VIM/.vimrc for Dos)
2889 * - environment variable EXINIT
2890 * - user exrc file (s:.exrc for Amiga, ~/.exrc otherwise)
2891 * - second user exrc file ($VIM/.exrc for Dos)
2892 * The first that exists is used, the rest is ignored.
2893 */
2894 if (process_env((char_u *)"VIMINIT", TRUE) != OK)
2895 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002896 if (do_source((char_u *)USR_VIMRC_FILE, TRUE, DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00002897#ifdef USR_VIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002898 && do_source((char_u *)USR_VIMRC_FILE2, TRUE,
2899 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00002900#endif
2901#ifdef USR_VIMRC_FILE3
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002902 && do_source((char_u *)USR_VIMRC_FILE3, TRUE,
2903 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00002904#endif
2905 && process_env((char_u *)"EXINIT", FALSE) == FAIL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002906 && do_source((char_u *)USR_EXRC_FILE, FALSE, DOSO_NONE) == FAIL)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002907 {
2908#ifdef USR_EXRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002909 (void)do_source((char_u *)USR_EXRC_FILE2, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002910#endif
2911 }
2912 }
2913
2914 /*
2915 * Read initialization commands from ".vimrc" or ".exrc" in current
2916 * directory. This is only done if the 'exrc' option is set.
2917 * Because of security reasons we disallow shell and write commands
2918 * now, except for unix if the file is owned by the user or 'secure'
2919 * option has been reset in environment of global ".exrc" or ".vimrc".
2920 * Only do this if VIMRC_FILE is not the same as USR_VIMRC_FILE or
2921 * SYS_VIMRC_FILE.
2922 */
2923 if (p_exrc)
2924 {
2925#if defined(UNIX) || defined(VMS)
2926 /* If ".vimrc" file is not owned by user, set 'secure' mode. */
2927 if (!file_owned(VIMRC_FILE))
2928#endif
2929 secure = p_secure;
2930
2931 i = FAIL;
2932 if (fullpathcmp((char_u *)USR_VIMRC_FILE,
2933 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
2934#ifdef USR_VIMRC_FILE2
2935 && fullpathcmp((char_u *)USR_VIMRC_FILE2,
2936 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
2937#endif
2938#ifdef USR_VIMRC_FILE3
2939 && fullpathcmp((char_u *)USR_VIMRC_FILE3,
2940 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
2941#endif
2942#ifdef SYS_VIMRC_FILE
2943 && fullpathcmp((char_u *)SYS_VIMRC_FILE,
2944 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
2945#endif
2946 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002947 i = do_source((char_u *)VIMRC_FILE, TRUE, DOSO_VIMRC);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002948
2949 if (i == FAIL)
2950 {
2951#if defined(UNIX) || defined(VMS)
2952 /* if ".exrc" is not owned by user set 'secure' mode */
2953 if (!file_owned(EXRC_FILE))
2954 secure = p_secure;
2955 else
2956 secure = 0;
2957#endif
2958 if ( fullpathcmp((char_u *)USR_EXRC_FILE,
2959 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
2960#ifdef USR_EXRC_FILE2
2961 && fullpathcmp((char_u *)USR_EXRC_FILE2,
2962 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
2963#endif
2964 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002965 (void)do_source((char_u *)EXRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002966 }
2967 }
2968 if (secure == 2)
2969 need_wait_return = TRUE;
2970 secure = 0;
2971#ifdef AMIGA
2972 proc->pr_WindowPtr = save_winptr;
2973#endif
2974 }
2975 TIME_MSG("sourcing vimrc file(s)");
2976}
2977
2978/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002979 * Setup to start using the GUI. Exit with an error when not available.
2980 */
2981 static void
2982main_start_gui()
2983{
2984#ifdef FEAT_GUI
2985 gui.starting = TRUE; /* start GUI a bit later */
2986#else
2987 mch_errmsg(_(e_nogvim));
2988 mch_errmsg("\n");
2989 mch_exit(2);
2990#endif
2991}
2992
2993/*
Bram Moolenaar49325942007-05-10 19:19:59 +00002994 * Get an environment variable, and execute it as Ex commands.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002995 * Returns FAIL if the environment variable was not executed, OK otherwise.
2996 */
2997 int
2998process_env(env, is_viminit)
2999 char_u *env;
3000 int is_viminit; /* when TRUE, called for VIMINIT */
3001{
3002 char_u *initstr;
3003 char_u *save_sourcing_name;
3004 linenr_T save_sourcing_lnum;
3005#ifdef FEAT_EVAL
3006 scid_T save_sid;
3007#endif
3008
3009 if ((initstr = mch_getenv(env)) != NULL && *initstr != NUL)
3010 {
3011 if (is_viminit)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003012 vimrc_found(NULL, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003013 save_sourcing_name = sourcing_name;
3014 save_sourcing_lnum = sourcing_lnum;
3015 sourcing_name = env;
3016 sourcing_lnum = 0;
3017#ifdef FEAT_EVAL
3018 save_sid = current_SID;
3019 current_SID = SID_ENV;
3020#endif
3021 do_cmdline_cmd(initstr);
3022 sourcing_name = save_sourcing_name;
3023 sourcing_lnum = save_sourcing_lnum;
3024#ifdef FEAT_EVAL
3025 current_SID = save_sid;;
3026#endif
3027 return OK;
3028 }
3029 return FAIL;
3030}
3031
3032#if defined(UNIX) || defined(VMS)
3033/*
3034 * Return TRUE if we are certain the user owns the file "fname".
3035 * Used for ".vimrc" and ".exrc".
3036 * Use both stat() and lstat() for extra security.
3037 */
3038 static int
3039file_owned(fname)
3040 char *fname;
3041{
3042 struct stat s;
3043# ifdef UNIX
3044 uid_t uid = getuid();
3045# else /* VMS */
3046 uid_t uid = ((getgid() << 16) | getuid());
3047# endif
3048
3049 return !(mch_stat(fname, &s) != 0 || s.st_uid != uid
3050# ifdef HAVE_LSTAT
3051 || mch_lstat(fname, &s) != 0 || s.st_uid != uid
3052# endif
3053 );
3054}
3055#endif
3056
3057/*
3058 * Give an error message main_errors["n"] and exit.
3059 */
3060 static void
3061mainerr(n, str)
3062 int n; /* one of the ME_ defines */
3063 char_u *str; /* extra argument or NULL */
3064{
3065#if defined(UNIX) || defined(__EMX__) || defined(VMS)
3066 reset_signals(); /* kill us with CTRL-C here, if you like */
3067#endif
3068
3069 mch_errmsg(longVersion);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003070 mch_errmsg("\n");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003071 mch_errmsg(_(main_errors[n]));
3072 if (str != NULL)
3073 {
3074 mch_errmsg(": \"");
3075 mch_errmsg((char *)str);
3076 mch_errmsg("\"");
3077 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003078 mch_errmsg(_("\nMore info with: \"vim -h\"\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003079
3080 mch_exit(1);
3081}
3082
3083 void
3084mainerr_arg_missing(str)
3085 char_u *str;
3086{
3087 mainerr(ME_ARG_MISSING, str);
3088}
3089
3090/*
3091 * print a message with three spaces prepended and '\n' appended.
3092 */
3093 static void
3094main_msg(s)
3095 char *s;
3096{
3097 mch_msg(" ");
3098 mch_msg(s);
3099 mch_msg("\n");
3100}
3101
3102/*
3103 * Print messages for "vim -h" or "vim --help" and exit.
3104 */
3105 static void
3106usage()
3107{
3108 int i;
3109 static char *(use[]) =
3110 {
3111 N_("[file ..] edit specified file(s)"),
3112 N_("- read text from stdin"),
3113 N_("-t tag edit file where tag is defined"),
3114#ifdef FEAT_QUICKFIX
3115 N_("-q [errorfile] edit file with first error")
3116#endif
3117 };
3118
3119#if defined(UNIX) || defined(__EMX__) || defined(VMS)
3120 reset_signals(); /* kill us with CTRL-C here, if you like */
3121#endif
3122
3123 mch_msg(longVersion);
3124 mch_msg(_("\n\nusage:"));
3125 for (i = 0; ; ++i)
3126 {
3127 mch_msg(_(" vim [arguments] "));
3128 mch_msg(_(use[i]));
3129 if (i == (sizeof(use) / sizeof(char_u *)) - 1)
3130 break;
3131 mch_msg(_("\n or:"));
3132 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003133#ifdef VMS
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003134 mch_msg(_("\nWhere case is ignored prepend / to make flag upper case"));
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003135#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003136
3137 mch_msg(_("\n\nArguments:\n"));
3138 main_msg(_("--\t\t\tOnly file names after this"));
3139#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
3140 main_msg(_("--literal\t\tDon't expand wildcards"));
3141#endif
3142#ifdef FEAT_OLE
3143 main_msg(_("-register\t\tRegister this gvim for OLE"));
3144 main_msg(_("-unregister\t\tUnregister gvim for OLE"));
3145#endif
3146#ifdef FEAT_GUI
3147 main_msg(_("-g\t\t\tRun using GUI (like \"gvim\")"));
3148 main_msg(_("-f or --nofork\tForeground: Don't fork when starting GUI"));
3149#endif
3150 main_msg(_("-v\t\t\tVi mode (like \"vi\")"));
3151 main_msg(_("-e\t\t\tEx mode (like \"ex\")"));
3152 main_msg(_("-s\t\t\tSilent (batch) mode (only for \"ex\")"));
3153#ifdef FEAT_DIFF
3154 main_msg(_("-d\t\t\tDiff mode (like \"vimdiff\")"));
3155#endif
3156 main_msg(_("-y\t\t\tEasy mode (like \"evim\", modeless)"));
3157 main_msg(_("-R\t\t\tReadonly mode (like \"view\")"));
3158 main_msg(_("-Z\t\t\tRestricted mode (like \"rvim\")"));
3159 main_msg(_("-m\t\t\tModifications (writing files) not allowed"));
3160 main_msg(_("-M\t\t\tModifications in text not allowed"));
3161 main_msg(_("-b\t\t\tBinary mode"));
3162#ifdef FEAT_LISP
3163 main_msg(_("-l\t\t\tLisp mode"));
3164#endif
3165 main_msg(_("-C\t\t\tCompatible with Vi: 'compatible'"));
3166 main_msg(_("-N\t\t\tNot fully Vi compatible: 'nocompatible'"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003167 main_msg(_("-V[N][fname]\t\tBe verbose [level N] [log messages to fname]"));
3168#ifdef FEAT_EVAL
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003169 main_msg(_("-D\t\t\tDebugging mode"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003170#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003171 main_msg(_("-n\t\t\tNo swap file, use memory only"));
3172 main_msg(_("-r\t\t\tList swap files and exit"));
3173 main_msg(_("-r (with file name)\tRecover crashed session"));
3174 main_msg(_("-L\t\t\tSame as -r"));
3175#ifdef AMIGA
3176 main_msg(_("-f\t\t\tDon't use newcli to open window"));
3177 main_msg(_("-dev <device>\t\tUse <device> for I/O"));
3178#endif
3179#ifdef FEAT_ARABIC
3180 main_msg(_("-A\t\t\tstart in Arabic mode"));
3181#endif
3182#ifdef FEAT_RIGHTLEFT
3183 main_msg(_("-H\t\t\tStart in Hebrew mode"));
3184#endif
3185#ifdef FEAT_FKMAP
3186 main_msg(_("-F\t\t\tStart in Farsi mode"));
3187#endif
3188 main_msg(_("-T <terminal>\tSet terminal type to <terminal>"));
3189 main_msg(_("-u <vimrc>\t\tUse <vimrc> instead of any .vimrc"));
3190#ifdef FEAT_GUI
3191 main_msg(_("-U <gvimrc>\t\tUse <gvimrc> instead of any .gvimrc"));
3192#endif
3193 main_msg(_("--noplugin\t\tDon't load plugin scripts"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003194#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003195 main_msg(_("-p[N]\t\tOpen N tab pages (default: one for each file)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003196 main_msg(_("-o[N]\t\tOpen N windows (default: one for each file)"));
3197 main_msg(_("-O[N]\t\tLike -o but split vertically"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003198#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003199 main_msg(_("+\t\t\tStart at end of file"));
3200 main_msg(_("+<lnum>\t\tStart at line <lnum>"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003201 main_msg(_("--cmd <command>\tExecute <command> before loading any vimrc file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003202 main_msg(_("-c <command>\t\tExecute <command> after loading the first file"));
3203 main_msg(_("-S <session>\t\tSource file <session> after loading the first file"));
3204 main_msg(_("-s <scriptin>\tRead Normal mode commands from file <scriptin>"));
3205 main_msg(_("-w <scriptout>\tAppend all typed commands to file <scriptout>"));
3206 main_msg(_("-W <scriptout>\tWrite all typed commands to file <scriptout>"));
3207#ifdef FEAT_CRYPT
3208 main_msg(_("-x\t\t\tEdit encrypted files"));
3209#endif
3210#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
3211# if defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)
3212 main_msg(_("-display <display>\tConnect vim to this particular X-server"));
3213# endif
3214 main_msg(_("-X\t\t\tDo not connect to X server"));
3215#endif
3216#ifdef FEAT_CLIENTSERVER
3217 main_msg(_("--remote <files>\tEdit <files> in a Vim server if possible"));
3218 main_msg(_("--remote-silent <files> Same, don't complain if there is no server"));
3219 main_msg(_("--remote-wait <files> As --remote but wait for files to have been edited"));
3220 main_msg(_("--remote-wait-silent <files> Same, don't complain if there is no server"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003221# ifdef FEAT_WINDOWS
Bram Moolenaar82ad3242008-01-11 19:26:36 +00003222 main_msg(_("--remote-tab[-wait][-silent] <files> As --remote but use tab page per file"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003223# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003224 main_msg(_("--remote-send <keys>\tSend <keys> to a Vim server and exit"));
3225 main_msg(_("--remote-expr <expr>\tEvaluate <expr> in a Vim server and print result"));
3226 main_msg(_("--serverlist\t\tList available Vim server names and exit"));
3227 main_msg(_("--servername <name>\tSend to/become the Vim server <name>"));
3228#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003229#ifdef STARTUPTIME
Bram Moolenaar34ef52d2009-11-17 11:31:25 +00003230 main_msg(_("--startuptime <file>\tWrite startup timing messages to <file>"));
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003231#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003232#ifdef FEAT_VIMINFO
3233 main_msg(_("-i <viminfo>\t\tUse <viminfo> instead of .viminfo"));
3234#endif
3235 main_msg(_("-h or --help\tPrint Help (this message) and exit"));
3236 main_msg(_("--version\t\tPrint version information and exit"));
3237
3238#ifdef FEAT_GUI_X11
3239# ifdef FEAT_GUI_MOTIF
3240 mch_msg(_("\nArguments recognised by gvim (Motif version):\n"));
3241# else
3242# ifdef FEAT_GUI_ATHENA
3243# ifdef FEAT_GUI_NEXTAW
3244 mch_msg(_("\nArguments recognised by gvim (neXtaw version):\n"));
3245# else
3246 mch_msg(_("\nArguments recognised by gvim (Athena version):\n"));
3247# endif
3248# endif
3249# endif
3250 main_msg(_("-display <display>\tRun vim on <display>"));
3251 main_msg(_("-iconic\t\tStart vim iconified"));
3252# if 0
3253 main_msg(_("-name <name>\t\tUse resource as if vim was <name>"));
3254 mch_msg(_("\t\t\t (Unimplemented)\n"));
3255# endif
3256 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
3257 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
3258 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3259 main_msg(_("-boldfont <font>\tUse <font> for bold text"));
3260 main_msg(_("-italicfont <font>\tUse <font> for italic text"));
3261 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3262 main_msg(_("-borderwidth <width>\tUse a border width of <width> (also: -bw)"));
3263 main_msg(_("-scrollbarwidth <width> Use a scrollbar width of <width> (also: -sw)"));
3264# ifdef FEAT_GUI_ATHENA
3265 main_msg(_("-menuheight <height>\tUse a menu bar height of <height> (also: -mh)"));
3266# endif
3267 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3268 main_msg(_("+reverse\t\tDon't use reverse video (also: +rv)"));
3269 main_msg(_("-xrm <resource>\tSet the specified resource"));
3270#endif /* FEAT_GUI_X11 */
3271#if defined(FEAT_GUI) && defined(RISCOS)
3272 mch_msg(_("\nArguments recognised by gvim (RISC OS version):\n"));
3273 main_msg(_("--columns <number>\tInitial width of window in columns"));
3274 main_msg(_("--rows <number>\tInitial height of window in rows"));
3275#endif
3276#ifdef FEAT_GUI_GTK
3277 mch_msg(_("\nArguments recognised by gvim (GTK+ version):\n"));
3278 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3279 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3280 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3281 main_msg(_("-display <display>\tRun vim on <display> (also: --display)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003282 main_msg(_("--role <role>\tSet a unique role to identify the main window"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003283 main_msg(_("--socketid <xid>\tOpen Vim inside another GTK widget"));
3284#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003285#ifdef FEAT_GUI_W32
3286 main_msg(_("-P <parent title>\tOpen Vim inside parent application"));
Bram Moolenaar78e17622007-08-30 10:26:19 +00003287 main_msg(_("--windowid <HWND>\tOpen Vim inside another win32 widget"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003288#endif
3289
3290#ifdef FEAT_GUI_GNOME
3291 /* Gnome gives extra messages for --help if we continue, but not for -h. */
3292 if (gui.starting)
3293 mch_msg("\n");
3294 else
3295#endif
3296 mch_exit(0);
3297}
3298
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00003299#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003300/*
3301 * Check the result of the ATTENTION dialog:
3302 * When "Quit" selected, exit Vim.
3303 * When "Recover" selected, recover the file.
3304 */
3305 static void
3306check_swap_exists_action()
3307{
3308 if (swap_exists_action == SEA_QUIT)
3309 getout(1);
3310 handle_swap_exists(NULL);
3311}
3312#endif
3313
3314#if defined(STARTUPTIME) || defined(PROTO)
3315static void time_diff __ARGS((struct timeval *then, struct timeval *now));
3316
3317static struct timeval prev_timeval;
3318
Bram Moolenaar3f269672009-11-03 11:11:11 +00003319# ifdef WIN3264
3320/*
3321 * Windows doesn't have gettimeofday(), although it does have struct timeval.
3322 */
3323 static int
3324gettimeofday(struct timeval *tv, char *dummy)
3325{
3326 long t = clock();
3327 tv->tv_sec = t / CLOCKS_PER_SEC;
3328 tv->tv_usec = (t - tv->tv_sec * CLOCKS_PER_SEC) * 1000000 / CLOCKS_PER_SEC;
3329 return 0;
3330}
3331# endif
3332
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003333/*
3334 * Save the previous time before doing something that could nest.
3335 * set "*tv_rel" to the time elapsed so far.
3336 */
3337 void
3338time_push(tv_rel, tv_start)
3339 void *tv_rel, *tv_start;
3340{
3341 *((struct timeval *)tv_rel) = prev_timeval;
3342 gettimeofday(&prev_timeval, NULL);
3343 ((struct timeval *)tv_rel)->tv_usec = prev_timeval.tv_usec
3344 - ((struct timeval *)tv_rel)->tv_usec;
3345 ((struct timeval *)tv_rel)->tv_sec = prev_timeval.tv_sec
3346 - ((struct timeval *)tv_rel)->tv_sec;
3347 if (((struct timeval *)tv_rel)->tv_usec < 0)
3348 {
3349 ((struct timeval *)tv_rel)->tv_usec += 1000000;
3350 --((struct timeval *)tv_rel)->tv_sec;
3351 }
3352 *(struct timeval *)tv_start = prev_timeval;
3353}
3354
3355/*
3356 * Compute the previous time after doing something that could nest.
3357 * Subtract "*tp" from prev_timeval;
3358 * Note: The arguments are (void *) to avoid trouble with systems that don't
3359 * have struct timeval.
3360 */
3361 void
3362time_pop(tp)
3363 void *tp; /* actually (struct timeval *) */
3364{
3365 prev_timeval.tv_usec -= ((struct timeval *)tp)->tv_usec;
3366 prev_timeval.tv_sec -= ((struct timeval *)tp)->tv_sec;
3367 if (prev_timeval.tv_usec < 0)
3368 {
3369 prev_timeval.tv_usec += 1000000;
3370 --prev_timeval.tv_sec;
3371 }
3372}
3373
3374 static void
3375time_diff(then, now)
3376 struct timeval *then;
3377 struct timeval *now;
3378{
3379 long usec;
3380 long msec;
3381
3382 usec = now->tv_usec - then->tv_usec;
3383 msec = (now->tv_sec - then->tv_sec) * 1000L + usec / 1000L,
3384 usec = usec % 1000L;
3385 fprintf(time_fd, "%03ld.%03ld", msec, usec >= 0 ? usec : usec + 1000L);
3386}
3387
3388 void
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003389time_msg(mesg, tv_start)
3390 char *mesg;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003391 void *tv_start; /* only for do_source: start time; actually
3392 (struct timeval *) */
3393{
3394 static struct timeval start;
3395 struct timeval now;
3396
3397 if (time_fd != NULL)
3398 {
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003399 if (strstr(mesg, "STARTING") != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003400 {
3401 gettimeofday(&start, NULL);
3402 prev_timeval = start;
3403 fprintf(time_fd, "\n\ntimes in msec\n");
3404 fprintf(time_fd, " clock self+sourced self: sourced script\n");
3405 fprintf(time_fd, " clock elapsed: other lines\n\n");
3406 }
3407 gettimeofday(&now, NULL);
3408 time_diff(&start, &now);
3409 if (((struct timeval *)tv_start) != NULL)
3410 {
3411 fprintf(time_fd, " ");
3412 time_diff(((struct timeval *)tv_start), &now);
3413 }
3414 fprintf(time_fd, " ");
3415 time_diff(&prev_timeval, &now);
3416 prev_timeval = now;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003417 fprintf(time_fd, ": %s\n", mesg);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003418 }
3419}
3420
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003421#endif
3422
3423#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
3424
3425/*
3426 * Common code for the X command server and the Win32 command server.
3427 */
3428
Bram Moolenaareb94e552006-03-11 21:35:11 +00003429static char_u *build_drop_cmd __ARGS((int filec, char **filev, int tabs, int sendReply));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003430
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003431/*
3432 * Do the client-server stuff, unless "--servername ''" was used.
3433 */
3434 static void
3435exec_on_server(parmp)
3436 mparm_T *parmp;
3437{
3438 if (parmp->serverName_arg == NULL || *parmp->serverName_arg != NUL)
3439 {
3440# ifdef WIN32
3441 /* Initialise the client/server messaging infrastructure. */
3442 serverInitMessaging();
3443# endif
3444
3445 /*
3446 * When a command server argument was found, execute it. This may
3447 * exit Vim when it was successful. Otherwise it's executed further
3448 * on. Remember the encoding used here in "serverStrEnc".
3449 */
3450 if (parmp->serverArg)
3451 {
3452 cmdsrv_main(&parmp->argc, parmp->argv,
3453 parmp->serverName_arg, &parmp->serverStr);
3454# ifdef FEAT_MBYTE
3455 parmp->serverStrEnc = vim_strsave(p_enc);
3456# endif
3457 }
3458
3459 /* If we're still running, get the name to register ourselves.
3460 * On Win32 can register right now, for X11 need to setup the
3461 * clipboard first, it's further down. */
3462 parmp->servername = serverMakeName(parmp->serverName_arg,
3463 parmp->argv[0]);
3464# ifdef WIN32
3465 if (parmp->servername != NULL)
3466 {
3467 serverSetName(parmp->servername);
3468 vim_free(parmp->servername);
3469 }
3470# endif
3471 }
3472}
3473
3474/*
3475 * Prepare for running as a Vim server.
3476 */
3477 static void
3478prepare_server(parmp)
3479 mparm_T *parmp;
3480{
3481# if defined(FEAT_X11)
3482 /*
3483 * Register for remote command execution with :serversend and --remote
3484 * unless there was a -X or a --servername '' on the command line.
3485 * Only register nongui-vim's with an explicit --servername argument.
Bram Moolenaar5f402312006-08-15 19:40:35 +00003486 * When running as root --servername is also required.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003487 */
3488 if (X_DISPLAY != NULL && parmp->servername != NULL && (
3489# ifdef FEAT_GUI
Bram Moolenaar5f402312006-08-15 19:40:35 +00003490 (gui.in_use
3491# ifdef UNIX
Bram Moolenaar311d9822007-02-27 15:48:28 +00003492 && getuid() != ROOT_UID
Bram Moolenaar5f402312006-08-15 19:40:35 +00003493# endif
3494 ) ||
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003495# endif
3496 parmp->serverName_arg != NULL))
3497 {
3498 (void)serverRegisterName(X_DISPLAY, parmp->servername);
3499 vim_free(parmp->servername);
3500 TIME_MSG("register server name");
3501 }
3502 else
3503 serverDelayedStartName = parmp->servername;
3504# endif
3505
3506 /*
3507 * Execute command ourselves if we're here because the send failed (or
3508 * else we would have exited above).
3509 */
3510 if (parmp->serverStr != NULL)
3511 {
3512 char_u *p;
3513
3514 server_to_input_buf(serverConvert(parmp->serverStrEnc,
3515 parmp->serverStr, &p));
3516 vim_free(p);
3517 }
3518}
3519
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003520 static void
3521cmdsrv_main(argc, argv, serverName_arg, serverStr)
3522 int *argc;
3523 char **argv;
3524 char_u *serverName_arg;
3525 char_u **serverStr;
3526{
3527 char_u *res;
3528 int i;
3529 char_u *sname;
3530 int ret;
3531 int didone = FALSE;
3532 int exiterr = 0;
3533 char **newArgV = argv + 1;
3534 int newArgC = 1,
3535 Argc = *argc;
3536 int argtype;
3537#define ARGTYPE_OTHER 0
3538#define ARGTYPE_EDIT 1
3539#define ARGTYPE_EDIT_WAIT 2
3540#define ARGTYPE_SEND 3
3541 int silent = FALSE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003542 int tabs = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003543# ifndef FEAT_X11
3544 HWND srv;
3545# else
3546 Window srv;
3547
3548 setup_term_clip();
3549# endif
3550
3551 sname = serverMakeName(serverName_arg, argv[0]);
3552 if (sname == NULL)
3553 return;
3554
3555 /*
3556 * Execute the command server related arguments and remove them
3557 * from the argc/argv array; We may have to return into main()
3558 */
3559 for (i = 1; i < Argc; i++)
3560 {
3561 res = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003562 if (STRCMP(argv[i], "--") == 0) /* end of option arguments */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003563 {
3564 for (; i < *argc; i++)
3565 {
3566 *newArgV++ = argv[i];
3567 newArgC++;
3568 }
3569 break;
3570 }
3571
Bram Moolenaareb94e552006-03-11 21:35:11 +00003572 if (STRICMP(argv[i], "--remote-send") == 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003573 argtype = ARGTYPE_SEND;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003574 else if (STRNICMP(argv[i], "--remote", 8) == 0)
3575 {
3576 char *p = argv[i] + 8;
3577
3578 argtype = ARGTYPE_EDIT;
3579 while (*p != NUL)
3580 {
3581 if (STRNICMP(p, "-wait", 5) == 0)
3582 {
3583 argtype = ARGTYPE_EDIT_WAIT;
3584 p += 5;
3585 }
3586 else if (STRNICMP(p, "-silent", 7) == 0)
3587 {
3588 silent = TRUE;
3589 p += 7;
3590 }
3591 else if (STRNICMP(p, "-tab", 4) == 0)
3592 {
3593 tabs = TRUE;
3594 p += 4;
3595 }
3596 else
3597 {
3598 argtype = ARGTYPE_OTHER;
3599 break;
3600 }
3601 }
3602 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003603 else
3604 argtype = ARGTYPE_OTHER;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003605
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003606 if (argtype != ARGTYPE_OTHER)
3607 {
3608 if (i == *argc - 1)
3609 mainerr_arg_missing((char_u *)argv[i]);
3610 if (argtype == ARGTYPE_SEND)
3611 {
3612 *serverStr = (char_u *)argv[i + 1];
3613 i++;
3614 }
3615 else
3616 {
3617 *serverStr = build_drop_cmd(*argc - i - 1, argv + i + 1,
Bram Moolenaareb94e552006-03-11 21:35:11 +00003618 tabs, argtype == ARGTYPE_EDIT_WAIT);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003619 if (*serverStr == NULL)
3620 {
3621 /* Probably out of memory, exit. */
3622 didone = TRUE;
3623 exiterr = 1;
3624 break;
3625 }
3626 Argc = i;
3627 }
3628# ifdef FEAT_X11
3629 if (xterm_dpy == NULL)
3630 {
3631 mch_errmsg(_("No display"));
3632 ret = -1;
3633 }
3634 else
3635 ret = serverSendToVim(xterm_dpy, sname, *serverStr,
3636 NULL, &srv, 0, 0, silent);
3637# else
3638 /* Win32 always works? */
3639 ret = serverSendToVim(sname, *serverStr, NULL, &srv, 0, silent);
3640# endif
3641 if (ret < 0)
3642 {
3643 if (argtype == ARGTYPE_SEND)
3644 {
3645 /* Failed to send, abort. */
3646 mch_errmsg(_(": Send failed.\n"));
3647 didone = TRUE;
3648 exiterr = 1;
3649 }
3650 else if (!silent)
3651 /* Let vim start normally. */
3652 mch_errmsg(_(": Send failed. Trying to execute locally\n"));
3653 break;
3654 }
3655
3656# ifdef FEAT_GUI_W32
3657 /* Guess that when the server name starts with "g" it's a GUI
3658 * server, which we can bring to the foreground here.
3659 * Foreground() in the server doesn't work very well. */
3660 if (argtype != ARGTYPE_SEND && TOUPPER_ASC(*sname) == 'G')
3661 SetForegroundWindow(srv);
3662# endif
3663
3664 /*
3665 * For --remote-wait: Wait until the server did edit each
3666 * file. Also detect that the server no longer runs.
3667 */
3668 if (ret >= 0 && argtype == ARGTYPE_EDIT_WAIT)
3669 {
3670 int numFiles = *argc - i - 1;
3671 int j;
3672 char_u *done = alloc(numFiles);
3673 char_u *p;
3674# ifdef FEAT_GUI_W32
3675 NOTIFYICONDATA ni;
3676 int count = 0;
3677 extern HWND message_window;
3678# endif
3679
3680 if (numFiles > 0 && argv[i + 1][0] == '+')
3681 /* Skip "+cmd" argument, don't wait for it to be edited. */
3682 --numFiles;
3683
3684# ifdef FEAT_GUI_W32
3685 ni.cbSize = sizeof(ni);
3686 ni.hWnd = message_window;
3687 ni.uID = 0;
3688 ni.uFlags = NIF_ICON|NIF_TIP;
3689 ni.hIcon = LoadIcon((HINSTANCE)GetModuleHandle(0), "IDR_VIM");
3690 sprintf(ni.szTip, _("%d of %d edited"), count, numFiles);
3691 Shell_NotifyIcon(NIM_ADD, &ni);
3692# endif
3693
3694 /* Wait for all files to unload in remote */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02003695 vim_memset(done, 0, numFiles);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003696 while (memchr(done, 0, numFiles) != NULL)
3697 {
3698# ifdef WIN32
3699 p = serverGetReply(srv, NULL, TRUE, TRUE);
3700 if (p == NULL)
3701 break;
3702# else
3703 if (serverReadReply(xterm_dpy, srv, &p, TRUE) < 0)
3704 break;
3705# endif
3706 j = atoi((char *)p);
3707 if (j >= 0 && j < numFiles)
3708 {
3709# ifdef FEAT_GUI_W32
3710 ++count;
3711 sprintf(ni.szTip, _("%d of %d edited"),
3712 count, numFiles);
3713 Shell_NotifyIcon(NIM_MODIFY, &ni);
3714# endif
3715 done[j] = 1;
3716 }
3717 }
3718# ifdef FEAT_GUI_W32
3719 Shell_NotifyIcon(NIM_DELETE, &ni);
3720# endif
3721 }
3722 }
3723 else if (STRICMP(argv[i], "--remote-expr") == 0)
3724 {
3725 if (i == *argc - 1)
3726 mainerr_arg_missing((char_u *)argv[i]);
3727# ifdef WIN32
3728 /* Win32 always works? */
3729 if (serverSendToVim(sname, (char_u *)argv[i + 1],
3730 &res, NULL, 1, FALSE) < 0)
3731# else
3732 if (xterm_dpy == NULL)
3733 mch_errmsg(_("No display: Send expression failed.\n"));
3734 else if (serverSendToVim(xterm_dpy, sname, (char_u *)argv[i + 1],
3735 &res, NULL, 1, 1, FALSE) < 0)
3736# endif
3737 {
3738 if (res != NULL && *res != NUL)
3739 {
3740 /* Output error from remote */
3741 mch_errmsg((char *)res);
3742 vim_free(res);
3743 res = NULL;
3744 }
3745 mch_errmsg(_(": Send expression failed.\n"));
3746 }
3747 }
3748 else if (STRICMP(argv[i], "--serverlist") == 0)
3749 {
3750# ifdef WIN32
3751 /* Win32 always works? */
3752 res = serverGetVimNames();
3753# else
3754 if (xterm_dpy != NULL)
3755 res = serverGetVimNames(xterm_dpy);
3756# endif
3757 if (called_emsg)
3758 mch_errmsg("\n");
3759 }
3760 else if (STRICMP(argv[i], "--servername") == 0)
3761 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +00003762 /* Already processed. Take it out of the command line */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003763 i++;
3764 continue;
3765 }
3766 else
3767 {
3768 *newArgV++ = argv[i];
3769 newArgC++;
3770 continue;
3771 }
3772 didone = TRUE;
3773 if (res != NULL && *res != NUL)
3774 {
3775 mch_msg((char *)res);
3776 if (res[STRLEN(res) - 1] != '\n')
3777 mch_msg("\n");
3778 }
3779 vim_free(res);
3780 }
3781
3782 if (didone)
3783 {
3784 display_errors(); /* display any collected messages */
3785 exit(exiterr); /* Mission accomplished - get out */
3786 }
3787
3788 /* Return back into main() */
3789 *argc = newArgC;
3790 vim_free(sname);
3791}
3792
3793/*
3794 * Build a ":drop" command to send to a Vim server.
3795 */
3796 static char_u *
Bram Moolenaareb94e552006-03-11 21:35:11 +00003797build_drop_cmd(filec, filev, tabs, sendReply)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003798 int filec;
3799 char **filev;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003800 int tabs; /* Use ":tab drop" instead of ":drop". */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003801 int sendReply;
3802{
3803 garray_T ga;
3804 int i;
3805 char_u *inicmd = NULL;
3806 char_u *p;
3807 char_u cwd[MAXPATHL];
3808
3809 if (filec > 0 && filev[0][0] == '+')
3810 {
3811 inicmd = (char_u *)filev[0] + 1;
3812 filev++;
3813 filec--;
3814 }
3815 /* Check if we have at least one argument. */
3816 if (filec <= 0)
3817 mainerr_arg_missing((char_u *)filev[-1]);
3818 if (mch_dirname(cwd, MAXPATHL) != OK)
3819 return NULL;
Bram Moolenaar02b06312007-09-06 15:39:22 +00003820 if ((p = vim_strsave_escaped_ext(cwd,
3821#ifdef BACKSLASH_IN_FILENAME
3822 "", /* rem_backslash() will tell what chars to escape */
3823#else
3824 PATH_ESC_CHARS,
3825#endif
3826 '\\', TRUE)) == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003827 return NULL;
3828 ga_init2(&ga, 1, 100);
3829 ga_concat(&ga, (char_u *)"<C-\\><C-N>:cd ");
3830 ga_concat(&ga, p);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003831 vim_free(p);
Bram Moolenaareb94e552006-03-11 21:35:11 +00003832
3833 /* Call inputsave() so that a prompt for an encryption key works. */
3834 ga_concat(&ga, (char_u *)"<CR>:if exists('*inputsave')|call inputsave()|endif|");
3835 if (tabs)
3836 ga_concat(&ga, (char_u *)"tab ");
3837 ga_concat(&ga, (char_u *)"drop");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003838 for (i = 0; i < filec; i++)
3839 {
3840 /* On Unix the shell has already expanded the wildcards, don't want to
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003841 * do it again in the Vim server. On MS-Windows only escape
3842 * non-wildcard characters. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003843 p = vim_strsave_escaped((char_u *)filev[i],
3844#ifdef UNIX
3845 PATH_ESC_CHARS
3846#else
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003847 (char_u *)" \t%#"
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003848#endif
3849 );
3850 if (p == NULL)
3851 {
3852 vim_free(ga.ga_data);
3853 return NULL;
3854 }
3855 ga_concat(&ga, (char_u *)" ");
3856 ga_concat(&ga, p);
3857 vim_free(p);
3858 }
3859 /* The :drop commands goes to Insert mode when 'insertmode' is set, use
3860 * CTRL-\ CTRL-N again. */
3861 ga_concat(&ga, (char_u *)"|if exists('*inputrestore')|call inputrestore()|endif<CR>");
3862 ga_concat(&ga, (char_u *)"<C-\\><C-N>:cd -");
3863 if (sendReply)
3864 ga_concat(&ga, (char_u *)"<CR>:call SetupRemoteReplies()");
3865 ga_concat(&ga, (char_u *)"<CR>:");
3866 if (inicmd != NULL)
3867 {
3868 /* Can't use <CR> after "inicmd", because an "startinsert" would cause
3869 * the following commands to be inserted as text. Use a "|",
3870 * hopefully "inicmd" does allow this... */
3871 ga_concat(&ga, inicmd);
3872 ga_concat(&ga, (char_u *)"|");
3873 }
3874 /* Bring the window to the foreground, goto Insert mode when 'im' set and
3875 * clear command line. */
Bram Moolenaar567e4de2004-12-31 21:01:02 +00003876 ga_concat(&ga, (char_u *)"cal foreground()|if &im|star|en|redr|f<CR>");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003877 ga_append(&ga, NUL);
3878 return ga.ga_data;
3879}
3880
3881/*
3882 * Replace termcodes such as <CR> and insert as key presses if there is room.
3883 */
3884 void
3885server_to_input_buf(str)
3886 char_u *str;
3887{
3888 char_u *ptr = NULL;
3889 char_u *cpo_save = p_cpo;
3890
3891 /* Set 'cpoptions' the way we want it.
3892 * B set - backslashes are *not* treated specially
3893 * k set - keycodes are *not* reverse-engineered
3894 * < unset - <Key> sequences *are* interpreted
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00003895 * The last but one parameter of replace_termcodes() is TRUE so that the
3896 * <lt> sequence is recognised - needed for a real backslash.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003897 */
3898 p_cpo = (char_u *)"Bk";
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00003899 str = replace_termcodes((char_u *)str, &ptr, FALSE, TRUE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003900 p_cpo = cpo_save;
3901
3902 if (*ptr != NUL) /* trailing CTRL-V results in nothing */
3903 {
3904 /*
3905 * Add the string to the input stream.
3906 * Can't use add_to_input_buf() here, we now have K_SPECIAL bytes.
3907 *
3908 * First clear typed characters from the typeahead buffer, there could
3909 * be half a mapping there. Then append to the existing string, so
3910 * that multiple commands from a client are concatenated.
3911 */
3912 if (typebuf.tb_maplen < typebuf.tb_len)
3913 del_typebuf(typebuf.tb_len - typebuf.tb_maplen, typebuf.tb_maplen);
3914 (void)ins_typebuf(str, REMAP_NONE, typebuf.tb_len, TRUE, FALSE);
3915
3916 /* Let input_available() know we inserted text in the typeahead
3917 * buffer. */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003918 typebuf_was_filled = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003919 }
3920 vim_free((char_u *)ptr);
3921}
3922
3923/*
3924 * Evaluate an expression that the client sent to a string.
3925 * Handles disabling error messages and disables debugging, otherwise Vim
3926 * hangs, waiting for "cont" to be typed.
3927 */
3928 char_u *
3929eval_client_expr_to_string(expr)
3930 char_u *expr;
3931{
3932 char_u *res;
3933 int save_dbl = debug_break_level;
3934 int save_ro = redir_off;
3935
3936 debug_break_level = -1;
3937 redir_off = 0;
3938 ++emsg_skip;
3939
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003940 res = eval_to_string(expr, NULL, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003941
3942 debug_break_level = save_dbl;
3943 redir_off = save_ro;
3944 --emsg_skip;
3945
Bram Moolenaar63a121b2005-12-11 21:36:39 +00003946 /* A client can tell us to redraw, but not to display the cursor, so do
3947 * that here. */
3948 setcursor();
3949 out_flush();
3950#ifdef FEAT_GUI
3951 if (gui.in_use)
3952 gui_update_cursor(FALSE, FALSE);
3953#endif
3954
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003955 return res;
3956}
3957
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003958/*
3959 * If conversion is needed, convert "data" from "client_enc" to 'encoding' and
3960 * return an allocated string. Otherwise return "data".
3961 * "*tofree" is set to the result when it needs to be freed later.
3962 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003963 char_u *
3964serverConvert(client_enc, data, tofree)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003965 char_u *client_enc UNUSED;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003966 char_u *data;
3967 char_u **tofree;
3968{
3969 char_u *res = data;
3970
3971 *tofree = NULL;
3972# ifdef FEAT_MBYTE
3973 if (client_enc != NULL && p_enc != NULL)
3974 {
3975 vimconv_T vimconv;
3976
3977 vimconv.vc_type = CONV_NONE;
3978 if (convert_setup(&vimconv, client_enc, p_enc) != FAIL
3979 && vimconv.vc_type != CONV_NONE)
3980 {
3981 res = string_convert(&vimconv, data, NULL);
3982 if (res == NULL)
3983 res = data;
3984 else
3985 *tofree = res;
3986 }
3987 convert_setup(&vimconv, NULL, NULL);
3988 }
3989# endif
3990 return res;
3991}
3992
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003993
3994/*
3995 * Make our basic server name: use the specified "arg" if given, otherwise use
3996 * the tail of the command "cmd" we were started with.
3997 * Return the name in allocated memory. This doesn't include a serial number.
3998 */
3999 static char_u *
4000serverMakeName(arg, cmd)
4001 char_u *arg;
4002 char *cmd;
4003{
4004 char_u *p;
4005
4006 if (arg != NULL && *arg != NUL)
4007 p = vim_strsave_up(arg);
4008 else
4009 {
4010 p = vim_strsave_up(gettail((char_u *)cmd));
4011 /* Remove .exe or .bat from the name. */
4012 if (p != NULL && vim_strchr(p, '.') != NULL)
4013 *vim_strchr(p, '.') = NUL;
4014 }
4015 return p;
4016}
4017#endif /* FEAT_CLIENTSERVER */
4018
4019/*
4020 * When FEAT_FKMAP is defined, also compile the Farsi source code.
4021 */
4022#if defined(FEAT_FKMAP) || defined(PROTO)
4023# include "farsi.c"
4024#endif
4025
4026/*
4027 * When FEAT_ARABIC is defined, also compile the Arabic source code.
4028 */
4029#if defined(FEAT_ARABIC) || defined(PROTO)
4030# include "arabic.c"
4031#endif