blob: 895a4359928ddcc18d7d6b09ee093e19785e8819 [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 Moolenaarb4210b32004-06-13 14:51:16 +000010#define EXTERN
11#include "vim.h"
12
13#ifdef SPAWNO
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +000014# include <spawno.h> /* special MS-DOS swapping library */
Bram Moolenaarb4210b32004-06-13 14:51:16 +000015#endif
16
Bram Moolenaarb4210b32004-06-13 14:51:16 +000017#ifdef __CYGWIN__
18# ifndef WIN32
Bram Moolenaar0d1498e2008-06-29 12:00:49 +000019# include <cygwin/version.h>
20# include <sys/cygwin.h> /* for cygwin_conv_to_posix_path() and/or
21 * cygwin_conv_path() */
Bram Moolenaarb4210b32004-06-13 14:51:16 +000022# endif
23# include <limits.h>
24#endif
25
Bram Moolenaarc013cb62005-07-24 21:18:31 +000026/* Maximum number of commands from + or -c arguments. */
27#define MAX_ARG_CMDS 10
28
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000029/* values for "window_layout" */
30#define WIN_HOR 1 /* "-o" horizontally split windows */
31#define WIN_VER 2 /* "-O" vertically split windows */
32#define WIN_TABS 3 /* "-p" windows on tab pages */
33
Bram Moolenaar58d98232005-07-23 22:25:46 +000034/* Struct for various parameters passed between main() and other functions. */
35typedef struct
36{
Bram Moolenaarc013cb62005-07-24 21:18:31 +000037 int argc;
38 char **argv;
39
40 int evim_mode; /* started as "evim" */
Bram Moolenaarc013cb62005-07-24 21:18:31 +000041 char_u *use_vimrc; /* vimrc from -u argument */
42
43 int n_commands; /* no. of commands from + or -c */
44 char_u *commands[MAX_ARG_CMDS]; /* commands from + or -c arg. */
45 char_u cmds_tofree[MAX_ARG_CMDS]; /* commands that need free() */
46 int n_pre_commands; /* no. of commands from --cmd */
47 char_u *pre_commands[MAX_ARG_CMDS]; /* commands from --cmd argument */
48
49 int edit_type; /* type of editing to do */
50 char_u *tagname; /* tag from -t argument */
51#ifdef FEAT_QUICKFIX
52 char_u *use_ef; /* 'errorfile' from -q argument */
53#endif
54
55 int want_full_screen;
56 int stdout_isatty; /* is stdout a terminal? */
57 char_u *term; /* specified terminal name */
58#ifdef FEAT_CRYPT
59 int ask_for_key; /* -x argument */
60#endif
61 int no_swap_file; /* "-n" argument used */
62#ifdef FEAT_EVAL
63 int use_debug_break_level;
64#endif
65#ifdef FEAT_WINDOWS
66 int window_count; /* number of windows to use */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000067 int window_layout; /* 0, WIN_HOR, WIN_VER or WIN_TABS */
Bram Moolenaarc013cb62005-07-24 21:18:31 +000068#endif
69
70#ifdef FEAT_CLIENTSERVER
Bram Moolenaar58d98232005-07-23 22:25:46 +000071 int serverArg; /* TRUE when argument for a server */
72 char_u *serverName_arg; /* cmdline arg for server name */
Bram Moolenaarc013cb62005-07-24 21:18:31 +000073 char_u *serverStr; /* remote server command */
74 char_u *serverStrEnc; /* encoding of serverStr */
75 char_u *servername; /* allocated name for our server */
76#endif
77#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
78 int literal; /* don't expand file names */
79#endif
80#ifdef MSWIN
81 int full_path; /* file name argument was full path */
82#endif
83#ifdef FEAT_DIFF
84 int diff_mode; /* start with 'diff' set */
85#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +000086} mparm_T;
87
Bram Moolenaarc013cb62005-07-24 21:18:31 +000088/* Values for edit_type. */
89#define EDIT_NONE 0 /* no edit type yet */
90#define EDIT_FILE 1 /* file name argument[s] given, use argument list */
91#define EDIT_STDIN 2 /* read file from stdin */
92#define EDIT_TAG 3 /* tag name argument given, use tagname */
93#define EDIT_QF 4 /* start in quickfix mode */
94
Bram Moolenaarb4210b32004-06-13 14:51:16 +000095#if defined(UNIX) || defined(VMS)
96static int file_owned __ARGS((char *fname));
97#endif
98static void mainerr __ARGS((int, char_u *));
99static void main_msg __ARGS((char *s));
100static void usage __ARGS((void));
101static int get_number_arg __ARGS((char_u *p, int *idx, int def));
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000102#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
103static void init_locale __ARGS((void));
104#endif
105static void parse_command_name __ARGS((mparm_T *parmp));
106static void early_arg_scan __ARGS((mparm_T *parmp));
107static void command_line_scan __ARGS((mparm_T *parmp));
108static void check_tty __ARGS((mparm_T *parmp));
109static void read_stdin __ARGS((void));
110static void create_windows __ARGS((mparm_T *parmp));
111#ifdef FEAT_WINDOWS
112static void edit_buffers __ARGS((mparm_T *parmp));
113#endif
114static void exe_pre_commands __ARGS((mparm_T *parmp));
115static void exe_commands __ARGS((mparm_T *parmp));
Bram Moolenaar58d98232005-07-23 22:25:46 +0000116static void source_startup_scripts __ARGS((mparm_T *parmp));
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000117static void main_start_gui __ARGS((void));
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000118#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000119static void check_swap_exists_action __ARGS((void));
120#endif
121#ifdef FEAT_CLIENTSERVER
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000122static void exec_on_server __ARGS((mparm_T *parmp));
123static void prepare_server __ARGS((mparm_T *parmp));
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000124static void cmdsrv_main __ARGS((int *argc, char **argv, char_u *serverName_arg, char_u **serverStr));
125static char_u *serverMakeName __ARGS((char_u *arg, char *cmd));
126#endif
127
128
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000129/*
130 * Different types of error messages.
131 */
132static char *(main_errors[]) =
133{
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000134 N_("Unknown option argument"),
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000135#define ME_UNKNOWN_OPTION 0
136 N_("Too many edit arguments"),
137#define ME_TOO_MANY_ARGS 1
138 N_("Argument missing after"),
139#define ME_ARG_MISSING 2
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000140 N_("Garbage after option argument"),
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000141#define ME_GARBAGE 3
142 N_("Too many \"+command\", \"-c command\" or \"--cmd command\" arguments"),
143#define ME_EXTRA_CMD 4
144 N_("Invalid argument for"),
145#define ME_INVALID_ARG 5
146};
147
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000148#ifndef PROTO /* don't want a prototype for main() */
149 int
150# ifdef VIMDLL
151_export
152# endif
153# ifdef FEAT_GUI_MSWIN
154# ifdef __BORLANDC__
155_cdecl
156# endif
157VimMain
158# else
159main
160# endif
161(argc, argv)
162 int argc;
163 char **argv;
164{
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000165 char_u *fname = NULL; /* file name from command line */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000166 mparm_T params; /* various parameters passed between
167 * main() and other functions. */
Bram Moolenaar3f269672009-11-03 11:11:11 +0000168#ifdef STARTUPTIME
169 int i;
170#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000171
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000172 /*
173 * Do any system-specific initialisations. These can NOT use IObuff or
174 * NameBuff. Thus emsg2() cannot be called!
175 */
176 mch_early_init();
177
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000178 /* Many variables are in "params" so that we can pass them to invoked
179 * functions without a lot of arguments. "argc" and "argv" are also
180 * copied, so that they can be changed. */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000181 vim_memset(&params, 0, sizeof(params));
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000182 params.argc = argc;
183 params.argv = argv;
184 params.want_full_screen = TRUE;
185#ifdef FEAT_EVAL
186 params.use_debug_break_level = -1;
187#endif
188#ifdef FEAT_WINDOWS
189 params.window_count = -1;
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000190#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +0000191
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000192#ifdef FEAT_TCL
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000193 vim_tcl_init(params.argv[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000194#endif
195
196#ifdef MEM_PROFILE
197 atexit(vim_mem_profile_dump);
198#endif
199
200#ifdef STARTUPTIME
Bram Moolenaar3f269672009-11-03 11:11:11 +0000201 for (i = 1; i < argc; ++i)
202 {
Bram Moolenaaref94eec2009-11-11 13:22:11 +0000203 if (STRICMP(argv[i], "--startuptime") == 0 && i + 1 < argc)
Bram Moolenaar3f269672009-11-03 11:11:11 +0000204 {
Bram Moolenaaref94eec2009-11-11 13:22:11 +0000205 time_fd = mch_fopen(argv[i + 1], "a");
Bram Moolenaar3f269672009-11-03 11:11:11 +0000206 TIME_MSG("--- VIM STARTING ---");
207 break;
208 }
209 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000210#endif
Bram Moolenaarca003e12006-03-17 23:19:38 +0000211 starttime = time(NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000212
213#ifdef __EMX__
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000214 _wildcard(&params.argc, &params.argv);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000215#endif
216
217#ifdef FEAT_MBYTE
218 (void)mb_init(); /* init mb_bytelen_tab[] to ones */
219#endif
Bram Moolenaardcaf10e2005-01-21 11:55:25 +0000220#ifdef FEAT_EVAL
221 eval_init(); /* init global variables */
222#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000223
224#ifdef __QNXNTO__
225 qnx_init(); /* PhAttach() for clipboard, (and gui) */
226#endif
227
228#ifdef MAC_OS_CLASSIC
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000229 /* Prepare for possibly starting GUI sometime */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000230 /* Macintosh needs this before any memory is allocated. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000231 gui_prepare(&params.argc, params.argv);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000232 TIME_MSG("GUI prepared");
233#endif
234
235 /* Init the table of Normal mode commands. */
236 init_normal_cmds();
237
238#if defined(HAVE_DATE_TIME) && defined(VMS) && defined(VAXC)
Bram Moolenaar58d98232005-07-23 22:25:46 +0000239 make_version(); /* Construct the long version string. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000240#endif
241
242 /*
243 * Allocate space for the generic buffers (needed for set_init_1() and
244 * EMSG2()).
245 */
246 if ((IObuff = alloc(IOSIZE)) == NULL
247 || (NameBuff = alloc(MAXPATHL)) == NULL)
248 mch_exit(0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000249 TIME_MSG("Allocated generic buffers");
250
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000251#ifdef NBDEBUG
252 /* Wait a moment for debugging NetBeans. Must be after allocating
253 * NameBuff. */
254 nbdebug_log_init("SPRO_GVIM_DEBUG", "SPRO_GVIM_DLEVEL");
255 nbdebug_wait(WT_ENV | WT_WAIT | WT_STOP, "SPRO_GVIM_WAIT", 20);
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000256 TIME_MSG("NetBeans debug wait");
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000257#endif
258
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000259#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
260 /*
261 * Setup to use the current locale (for ctype() and many other things).
262 * NOTE: Translated messages with encodings other than latin1 will not
263 * work until set_init_1() has been called!
264 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000265 init_locale();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000266 TIME_MSG("locale set");
267#endif
268
269#ifdef FEAT_GUI
270 gui.dofork = TRUE; /* default is to use fork() */
271#endif
272
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000273 /*
Bram Moolenaar58d98232005-07-23 22:25:46 +0000274 * Do a first scan of the arguments in "argv[]":
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000275 * -display or --display
Bram Moolenaar58d98232005-07-23 22:25:46 +0000276 * --server...
277 * --socketid
Bram Moolenaar78e17622007-08-30 10:26:19 +0000278 * --windowid
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000279 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000280 early_arg_scan(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000281
282#ifdef FEAT_SUN_WORKSHOP
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000283 findYourself(params.argv[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000284#endif
285#if defined(FEAT_GUI) && !defined(MAC_OS_CLASSIC)
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000286 /* Prepare for possibly starting GUI sometime */
287 gui_prepare(&params.argc, params.argv);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000288 TIME_MSG("GUI prepared");
289#endif
290
291#ifdef FEAT_CLIPBOARD
292 clip_init(FALSE); /* Initialise clipboard stuff */
293 TIME_MSG("clipboard setup");
294#endif
295
296 /*
297 * Check if we have an interactive window.
298 * On the Amiga: If there is no window, we open one with a newcli command
299 * (needed for :! to * work). mch_check_win() will also handle the -d or
300 * -dev argument.
301 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000302 params.stdout_isatty = (mch_check_win(params.argc, params.argv) != FAIL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000303 TIME_MSG("window checked");
304
305 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000306 * Allocate the first window and buffer.
307 * Can't do anything without it, exit when it fails.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000308 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000309 if (win_alloc_first() == FAIL)
310 mch_exit(0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000311
312 init_yank(); /* init yank buffers */
313
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000314 alist_init(&global_alist); /* Init the argument list to empty. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000315
316 /*
317 * Set the default values for the options.
318 * NOTE: Non-latin1 translated messages are working only after this,
319 * because this is where "has_mbyte" will be set, which is used by
320 * msg_outtrans_len_attr().
321 * First find out the home directory, needed to expand "~" in options.
322 */
323 init_homedir(); /* find real value of $HOME */
324 set_init_1();
325 TIME_MSG("inits 1");
326
327#ifdef FEAT_EVAL
328 set_lang_var(); /* set v:lang and v:ctype */
329#endif
330
331#ifdef FEAT_CLIENTSERVER
332 /*
333 * Do the client-server stuff, unless "--servername ''" was used.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000334 * This may exit Vim if the command was sent to the server.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000335 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000336 exec_on_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000337#endif
338
339 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000340 * Figure out the way to work from the command name argv[0].
341 * "vimdiff" starts diff mode, "rvim" sets "restricted", etc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000342 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000343 parse_command_name(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000344
345 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000346 * Process the command line arguments. File names are put in the global
347 * argument list "global_alist".
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000348 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000349 command_line_scan(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000350 TIME_MSG("parsing arguments");
351
352 /*
353 * On some systems, when we compile with the GUI, we always use it. On Mac
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000354 * there is no terminal version, and on Windows we can't fork one off with
355 * :gui.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000356 */
357#ifdef ALWAYS_USE_GUI
358 gui.starting = TRUE;
359#else
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000360# if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000361 /*
362 * Check if the GUI can be started. Reset gui.starting if not.
363 * Don't know about other systems, stay on the safe side and don't check.
364 */
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000365 if (gui.starting)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000366 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000367 if (gui_init_check() == FAIL)
368 {
369 gui.starting = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000370
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000371 /* When running "evim" or "gvim -y" we need the menus, exit if we
372 * don't have them. */
373 if (params.evim_mode)
374 mch_exit(1);
375 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000376 }
377# endif
378#endif
379
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000380 if (GARGCOUNT > 0)
381 {
382#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
383 /*
384 * Expand wildcards in file names.
385 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000386 if (!params.literal)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000387 {
388 /* Temporarily add '(' and ')' to 'isfname'. These are valid
389 * filename characters but are excluded from 'isfname' to make
390 * "gf" work on a file name in parenthesis (e.g.: see vim.h). */
391 do_cmdline_cmd((char_u *)":set isf+=(,)");
Bram Moolenaar86b68352004-12-27 21:59:20 +0000392 alist_expand(NULL, 0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000393 do_cmdline_cmd((char_u *)":set isf&");
394 }
395#endif
396 fname = alist_name(&GARGLIST[0]);
397 }
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000398
399#if defined(WIN32) && defined(FEAT_MBYTE)
400 {
401 extern void set_alist_count(void);
402
403 /* Remember the number of entries in the argument list. If it changes
404 * we don't react on setting 'encoding'. */
405 set_alist_count();
406 }
407#endif
408
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000409#ifdef MSWIN
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000410 if (GARGCOUNT == 1 && params.full_path)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000411 {
412 /*
413 * If there is one filename, fully qualified, we have very probably
414 * been invoked from explorer, so change to the file's directory.
415 * Hint: to avoid this when typing a command use a forward slash.
416 * If the cd fails, it doesn't matter.
417 */
418 (void)vim_chdirfile(fname);
419 }
420#endif
421 TIME_MSG("expanding arguments");
422
423#ifdef FEAT_DIFF
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000424 if (params.diff_mode && params.window_count == -1)
425 params.window_count = 0; /* open up to 3 windows */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000426#endif
427
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000428 /* Don't redraw until much later. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000429 ++RedrawingDisabled;
430
431 /*
432 * When listing swap file names, don't do cursor positioning et. al.
433 */
434 if (recoverymode && fname == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000435 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000436
437 /*
438 * When certain to start the GUI, don't check capabilities of terminal.
439 * For GTK we can't be sure, but when started from the desktop it doesn't
440 * make sense to try using a terminal.
441 */
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000442#if defined(ALWAYS_USE_GUI) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000443 if (gui.starting
444# ifdef FEAT_GUI_GTK
445 && !isatty(2)
446# endif
447 )
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000448 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000449#endif
450
451#if defined(FEAT_GUI_MAC) && defined(MACOS_X_UNIX)
452 /* When the GUI is started from Finder, need to display messages in a
453 * message box. isatty(2) returns TRUE anyway, thus we need to check the
454 * name to know we're not started from a terminal. */
455 if (gui.starting && (!isatty(2) || strcmp("/dev/console", ttyname(2)) == 0))
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000456 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000457 params.want_full_screen = FALSE;
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000458
459 /* Avoid always using "/" as the current directory. Note that when
460 * started from Finder the arglist will be filled later in
461 * HandleODocAE() and "fname" will be NULL. */
462 if (getcwd((char *)NameBuff, MAXPATHL) != NULL
463 && STRCMP(NameBuff, "/") == 0)
464 {
465 if (fname != NULL)
466 (void)vim_chdirfile(fname);
467 else
468 {
469 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
470 vim_chdir(NameBuff);
471 }
472 }
473 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000474#endif
475
476 /*
477 * mch_init() sets up the terminal (window) for use. This must be
478 * done after resetting full_screen, otherwise it may move the cursor
479 * (MSDOS).
480 * Note that we may use mch_exit() before mch_init()!
481 */
482 mch_init();
483 TIME_MSG("shell init");
484
485#ifdef USE_XSMP
486 /*
487 * For want of anywhere else to do it, try to connect to xsmp here.
488 * Fitting it in after gui_mch_init, but before gui_init (via termcapinit).
489 * Hijacking -X 'no X connection' to also disable XSMP connection as that
490 * has a similar delay upon failure.
491 * Only try if SESSION_MANAGER is set to something non-null.
492 */
493 if (!x_no_connect)
494 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000495 char *p = getenv("SESSION_MANAGER");
496
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000497 if (p != NULL && *p != NUL)
498 {
499 xsmp_init();
500 TIME_MSG("xsmp init");
501 }
502 }
503#endif
504
505 /*
506 * Print a warning if stdout is not a terminal.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000507 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000508 check_tty(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000509
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000510 /* This message comes before term inits, but after setting "silent_mode"
511 * when the input is not a tty. */
512 if (GARGCOUNT > 1 && !silent_mode)
513 printf(_("%d files to edit\n"), GARGCOUNT);
514
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000515 if (params.want_full_screen && !silent_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000516 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000517 termcapinit(params.term); /* set terminal name and get terminal
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000518 capabilities (will set full_screen) */
519 screen_start(); /* don't know where cursor is now */
520 TIME_MSG("Termcap init");
521 }
522
523 /*
524 * Set the default values for the options that use Rows and Columns.
525 */
526 ui_get_shellsize(); /* inits Rows and Columns */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000527 win_init_size();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000528#ifdef FEAT_DIFF
529 /* Set the 'diff' option now, so that it can be checked for in a .vimrc
530 * file. There is no buffer yet though. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000531 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000532 diff_win_options(firstwin, FALSE);
533#endif
534
535 cmdline_row = Rows - p_ch;
536 msg_row = cmdline_row;
537 screenalloc(FALSE); /* allocate screen buffers */
538 set_init_2();
539 TIME_MSG("inits 2");
540
541 msg_scroll = TRUE;
542 no_wait_return = TRUE;
543
544 init_mappings(); /* set up initial mappings */
545
546 init_highlight(TRUE, FALSE); /* set the default highlight groups */
547 TIME_MSG("init highlight");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000548
549#ifdef FEAT_EVAL
550 /* Set the break level after the terminal is initialized. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000551 debug_break_level = params.use_debug_break_level;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000552#endif
553
Bram Moolenaar58d98232005-07-23 22:25:46 +0000554 /* Execute --cmd arguments. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000555 exe_pre_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000556
Bram Moolenaar58d98232005-07-23 22:25:46 +0000557 /* Source startup scripts. */
558 source_startup_scripts(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000559
560#ifdef FEAT_EVAL
561 /*
562 * Read all the plugin files.
563 * Only when compiled with +eval, since most plugins need it.
564 */
565 if (p_lpl)
566 {
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000567# ifdef VMS /* Somehow VMS doesn't handle the "**". */
568 source_runtime((char_u *)"plugin/*.vim", TRUE);
569# else
Bram Moolenaar07d4d732005-10-03 22:04:08 +0000570 source_runtime((char_u *)"plugin/**/*.vim", TRUE);
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000571# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000572 TIME_MSG("loading plugins");
573 }
574#endif
575
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000576#ifdef FEAT_DIFF
577 /* Decide about window layout for diff mode after reading vimrc. */
578 if (params.diff_mode && params.window_layout == 0)
579 {
580 if (diffopt_horizontal())
581 params.window_layout = WIN_HOR; /* use horizontal split */
582 else
583 params.window_layout = WIN_VER; /* use vertical split */
584 }
585#endif
586
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000587 /*
588 * Recovery mode without a file name: List swap files.
589 * This uses the 'dir' option, therefore it must be after the
590 * initializations.
591 */
592 if (recoverymode && fname == NULL)
593 {
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200594 recover_names(NULL, TRUE, 0, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000595 mch_exit(0);
596 }
597
598 /*
599 * Set a few option defaults after reading .vimrc files:
600 * 'title' and 'icon', Unix: 'shellpipe' and 'shellredir'.
601 */
602 set_init_3();
603 TIME_MSG("inits 3");
604
605 /*
606 * "-n" argument: Disable swap file by setting 'updatecount' to 0.
607 * Note that this overrides anything from a vimrc file.
608 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000609 if (params.no_swap_file)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000610 p_uc = 0;
611
612#ifdef FEAT_FKMAP
613 if (curwin->w_p_rl && p_altkeymap)
614 {
615 p_hkmap = FALSE; /* Reset the Hebrew keymap mode */
616# ifdef FEAT_ARABIC
617 curwin->w_p_arab = FALSE; /* Reset the Arabic keymap mode */
618# endif
619 p_fkmap = TRUE; /* Set the Farsi keymap mode */
620 }
621#endif
622
623#ifdef FEAT_GUI
624 if (gui.starting)
625 {
626#if defined(UNIX) || defined(VMS)
627 /* When something caused a message from a vimrc script, need to output
628 * an extra newline before the shell prompt. */
629 if (did_emsg || msg_didout)
630 putchar('\n');
631#endif
632
633 gui_start(); /* will set full_screen to TRUE */
634 TIME_MSG("starting GUI");
635
636 /* When running "evim" or "gvim -y" we need the menus, exit if we
637 * don't have them. */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000638 if (!gui.in_use && params.evim_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000639 mch_exit(1);
640 }
641#endif
642
643#ifdef SPAWNO /* special MSDOS swapping library */
644 init_SPAWNO("", SWAP_ANY);
645#endif
646
647#ifdef FEAT_VIMINFO
648 /*
Bram Moolenaard812df62008-11-09 12:46:09 +0000649 * Read in registers, history etc, but not marks, from the viminfo file.
650 * This is where v:oldfiles gets filled.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000651 */
652 if (*p_viminfo != NUL)
653 {
Bram Moolenaard812df62008-11-09 12:46:09 +0000654 read_viminfo(NULL, VIF_WANT_INFO | VIF_GET_OLDFILES);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000655 TIME_MSG("reading viminfo");
656 }
657#endif
658
659#ifdef FEAT_QUICKFIX
660 /*
661 * "-q errorfile": Load the error file now.
662 * If the error file can't be read, exit before doing anything else.
663 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000664 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000665 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000666 if (params.use_ef != NULL)
667 set_string_option_direct((char_u *)"ef", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000668 params.use_ef, OPT_FREE, SID_CARG);
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200669 vim_snprintf((char *)IObuff, IOSIZE, "cfile %s", p_ef);
670 if (qf_init(NULL, p_ef, p_efm, TRUE, IObuff) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000671 {
672 out_char('\n');
673 mch_exit(3);
674 }
675 TIME_MSG("reading errorfile");
676 }
677#endif
678
679 /*
680 * Start putting things on the screen.
681 * Scroll screen down before drawing over it
682 * Clear screen now, so file message will not be cleared.
683 */
684 starting = NO_BUFFERS;
685 no_wait_return = FALSE;
686 if (!exmode_active)
687 msg_scroll = FALSE;
688
689#ifdef FEAT_GUI
690 /*
691 * This seems to be required to make callbacks to be called now, instead
692 * of after things have been put on the screen, which then may be deleted
693 * when getting a resize callback.
694 * For the Mac this handles putting files dropped on the Vim icon to
695 * global_alist.
696 */
697 if (gui.in_use)
698 {
699# ifdef FEAT_SUN_WORKSHOP
700 if (!usingSunWorkShop)
701# endif
702 gui_wait_for_chars(50L);
703 TIME_MSG("GUI delay");
704 }
705#endif
706
707#if defined(FEAT_GUI_PHOTON) && defined(FEAT_CLIPBOARD)
708 qnx_clip_init();
709#endif
710
Bram Moolenaarc8bbaa32010-07-14 16:54:21 +0200711#if defined(MACOS_X) && defined(FEAT_CLIPBOARD)
712 clip_init(TRUE);
713#endif
714
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000715#ifdef FEAT_XCLIPBOARD
716 /* Start using the X clipboard, unless the GUI was started. */
717# ifdef FEAT_GUI
718 if (!gui.in_use)
719# endif
720 {
721 setup_term_clip();
722 TIME_MSG("setup clipboard");
723 }
724#endif
725
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000726#ifdef FEAT_CLIENTSERVER
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000727 /* Prepare for being a Vim server. */
728 prepare_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000729#endif
730
731 /*
732 * If "-" argument given: Read file from stdin.
733 * Do this before starting Raw mode, because it may change things that the
734 * writing end of the pipe doesn't like, e.g., in case stdin and stderr
735 * are the same terminal: "cat | vim -".
736 * Using autocommands here may cause trouble...
737 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000738 if (params.edit_type == EDIT_STDIN && !recoverymode)
739 read_stdin();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000740
741#if defined(UNIX) || defined(VMS)
742 /* When switching screens and something caused a message from a vimrc
743 * script, need to output an extra newline on exit. */
744 if ((did_emsg || msg_didout) && *T_TI != NUL)
745 newline_on_exit = TRUE;
746#endif
747
748 /*
749 * When done something that is not allowed or error message call
750 * wait_return. This must be done before starttermcap(), because it may
751 * switch to another screen. It must be done after settmode(TMODE_RAW),
752 * because we want to react on a single key stroke.
753 * Call settmode and starttermcap here, so the T_KS and T_TI may be
Bram Moolenaar49325942007-05-10 19:19:59 +0000754 * defined by termcapinit and redefined in .exrc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000755 */
756 settmode(TMODE_RAW);
757 TIME_MSG("setting raw mode");
758
759 if (need_wait_return || msg_didany)
760 {
761 wait_return(TRUE);
762 TIME_MSG("waiting for return");
763 }
764
765 starttermcap(); /* start termcap if not done by wait_return() */
766 TIME_MSG("start termcap");
767
768#ifdef FEAT_MOUSE
769 setmouse(); /* may start using the mouse */
770#endif
771 if (scroll_region)
772 scroll_region_reset(); /* In case Rows changed */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000773 scroll_start(); /* may scroll the screen to the right position */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000774
775 /*
776 * Don't clear the screen when starting in Ex mode, unless using the GUI.
777 */
778 if (exmode_active
779#ifdef FEAT_GUI
780 && !gui.in_use
781#endif
782 )
783 must_redraw = CLEAR;
784 else
785 {
786 screenclear(); /* clear screen */
787 TIME_MSG("clearing screen");
788 }
789
790#ifdef FEAT_CRYPT
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000791 if (params.ask_for_key)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000792 {
Bram Moolenaar40e6a712010-05-16 22:32:54 +0200793 (void)blowfish_self_test();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000794 (void)get_crypt_key(TRUE, TRUE);
795 TIME_MSG("getting crypt key");
796 }
797#endif
798
799 no_wait_return = TRUE;
800
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000801 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000802 * Create the requested number of windows and edit buffers in them.
803 * Also does recovery if "recoverymode" set.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000804 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000805 create_windows(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000806 TIME_MSG("opening buffers");
807
Bram Moolenaar867a4b72007-03-18 20:51:46 +0000808#ifdef FEAT_EVAL
809 /* clear v:swapcommand */
810 set_vim_var_string(VV_SWAPCOMMAND, NULL, -1);
811#endif
812
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000813 /* Ex starts at last line of the file */
814 if (exmode_active)
815 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
816
817#ifdef FEAT_AUTOCMD
818 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
819 TIME_MSG("BufEnter autocommands");
820#endif
821 setpcmark();
822
823#ifdef FEAT_QUICKFIX
824 /*
825 * When started with "-q errorfile" jump to first error now.
826 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000827 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000828 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000829 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000830 TIME_MSG("jump to first error");
831 }
832#endif
833
834#ifdef FEAT_WINDOWS
835 /*
836 * If opened more than one window, start editing files in the other
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000837 * windows.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000838 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000839 edit_buffers(&params);
840#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000841
842#ifdef FEAT_DIFF
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000843 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000844 {
845 win_T *wp;
846
847 /* set options in each window for "vimdiff". */
848 for (wp = firstwin; wp != NULL; wp = wp->w_next)
849 diff_win_options(wp, TRUE);
850 }
851#endif
852
853 /*
854 * Shorten any of the filenames, but only when absolute.
855 */
856 shorten_fnames(FALSE);
857
858 /*
859 * Need to jump to the tag before executing the '-c command'.
860 * Makes "vim -c '/return' -t main" work.
861 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000862 if (params.tagname != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000863 {
Bram Moolenaar146522e2005-12-16 21:55:46 +0000864#if defined(HAS_SWAP_EXISTS_ACTION)
865 swap_exists_did_quit = FALSE;
866#endif
867
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000868 vim_snprintf((char *)IObuff, IOSIZE, "ta %s", params.tagname);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000869 do_cmdline_cmd(IObuff);
870 TIME_MSG("jumping to tag");
Bram Moolenaar146522e2005-12-16 21:55:46 +0000871
872#if defined(HAS_SWAP_EXISTS_ACTION)
873 /* If the user doesn't want to edit the file then we quit here. */
874 if (swap_exists_did_quit)
875 getout(1);
876#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000877 }
878
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000879 /* Execute any "+", "-c" and "-S" arguments. */
880 if (params.n_commands > 0)
881 exe_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000882
883 RedrawingDisabled = 0;
884 redraw_all_later(NOT_VALID);
885 no_wait_return = FALSE;
886 starting = 0;
887
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000888#ifdef FEAT_TERMRESPONSE
889 /* Requesting the termresponse is postponed until here, so that a "-c q"
890 * argument doesn't make it appear in the shell Vim was started from. */
891 may_req_termresponse();
892#endif
893
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000894 /* start in insert mode */
895 if (p_im)
896 need_start_insertmode = TRUE;
897
898#ifdef FEAT_AUTOCMD
899 apply_autocmds(EVENT_VIMENTER, NULL, NULL, FALSE, curbuf);
900 TIME_MSG("VimEnter autocommands");
901#endif
902
903#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
904 /* When a startup script or session file setup for diff'ing and
905 * scrollbind, sync the scrollbind now. */
906 if (curwin->w_p_diff && curwin->w_p_scb)
907 {
908 update_topline();
909 check_scrollbind((linenr_T)0, 0L);
910 TIME_MSG("diff scrollbinding");
911 }
912#endif
913
914#if defined(WIN3264) && !defined(FEAT_GUI_W32)
915 mch_set_winsize_now(); /* Allow winsize changes from now on */
916#endif
917
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000918#if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
919 /* When tab pages were created, may need to update the tab pages line and
920 * scrollbars. This is skipped while creating them. */
921 if (first_tabpage->tp_next != NULL)
922 {
923 out_flush();
924 gui_init_which_components(NULL);
925 gui_update_scrollbars(TRUE);
926 }
927 need_mouse_correct = TRUE;
928#endif
929
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000930 /* If ":startinsert" command used, stuff a dummy command to be able to
931 * call normal_cmd(), which will then start Insert mode. */
932 if (restart_edit != 0)
Bram Moolenaarebefac62005-12-28 22:39:57 +0000933 stuffcharReadbuff(K_NOP);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000934
935#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200936 if (netbeansArg != NULL && strncmp("-nb", netbeansArg, 3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200937 {
938# ifdef FEAT_GUI
Bram Moolenaar173c9852010-09-29 17:27:01 +0200939# if !defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK) \
Bram Moolenaar67c53842010-05-22 18:28:27 +0200940 && !defined(FEAT_GUI_W32)
941 if (gui.in_use)
942 {
943 mch_errmsg(_("netbeans is not supported with this GUI\n"));
944 mch_exit(2);
945 }
946# endif
947# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000948 /* Tell the client that it can start sending commands. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200949 netbeans_open(netbeansArg + 3, TRUE);
Bram Moolenaar67c53842010-05-22 18:28:27 +0200950 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000951#endif
952
953 TIME_MSG("before starting main loop");
954
955 /*
956 * Call the main command loop. This never returns.
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000957 * For embedded MzScheme the main_loop will be called by Scheme
958 * for proper stack tracking
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000959 */
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000960#ifndef FEAT_MZSCHEME
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000961 main_loop(FALSE, FALSE);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000962#else
963 mzscheme_main();
964#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000965
966 return 0;
967}
968#endif /* PROTO */
969
970/*
971 * Main loop: Execute Normal mode commands until exiting Vim.
972 * Also used to handle commands in the command-line window, until the window
973 * is closed.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000974 * Also used to handle ":visual" command after ":global": execute Normal mode
975 * commands, return when entering Ex mode. "noexmode" is TRUE then.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000976 */
977 void
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000978main_loop(cmdwin, noexmode)
979 int cmdwin; /* TRUE when working in the command-line window */
980 int noexmode; /* TRUE when return on entering Ex mode */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000981{
Bram Moolenaar225d32b2007-08-10 19:33:47 +0000982 oparg_T oa; /* operator arguments */
983 int previous_got_int = FALSE; /* "got_int" was TRUE */
Bram Moolenaarb2c03502010-07-02 20:20:09 +0200984#ifdef FEAT_CONCEAL
985 linenr_T conceal_old_cursor_line = 0;
986 linenr_T conceal_new_cursor_line = 0;
987 int conceal_update_lines = FALSE;
988#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000989
990#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
991 /* Setup to catch a terminating error from the X server. Just ignore
992 * it, restore the state and continue. This might not always work
993 * properly, but at least we don't exit unexpectedly when the X server
994 * exists while Vim is running in a console. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000995 if (!cmdwin && !noexmode && SETJMP(x_jump_env))
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000996 {
997 State = NORMAL;
998# ifdef FEAT_VISUAL
999 VIsual_active = FALSE;
1000# endif
1001 got_int = TRUE;
1002 need_wait_return = FALSE;
1003 global_busy = FALSE;
1004 exmode_active = 0;
1005 skip_redraw = FALSE;
1006 RedrawingDisabled = 0;
1007 no_wait_return = 0;
1008# ifdef FEAT_EVAL
1009 emsg_skip = 0;
1010# endif
1011 emsg_off = 0;
1012# ifdef FEAT_MOUSE
1013 setmouse();
1014# endif
1015 settmode(TMODE_RAW);
1016 starttermcap();
1017 scroll_start();
1018 redraw_later_clear();
1019 }
1020#endif
1021
1022 clear_oparg(&oa);
1023 while (!cmdwin
1024#ifdef FEAT_CMDWIN
1025 || cmdwin_result == 0
1026#endif
1027 )
1028 {
1029 if (stuff_empty())
1030 {
1031 did_check_timestamps = FALSE;
1032 if (need_check_timestamps)
1033 check_timestamps(FALSE);
1034 if (need_wait_return) /* if wait_return still needed ... */
1035 wait_return(FALSE); /* ... call it now */
1036 if (need_start_insertmode && goto_im()
1037#ifdef FEAT_VISUAL
1038 && !VIsual_active
1039#endif
1040 )
1041 {
1042 need_start_insertmode = FALSE;
1043 stuffReadbuff((char_u *)"i"); /* start insert mode next */
1044 /* skip the fileinfo message now, because it would be shown
1045 * after insert mode finishes! */
1046 need_fileinfo = FALSE;
1047 }
1048 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001049
1050 /* Reset "got_int" now that we got back to the main loop. Except when
1051 * inside a ":g/pat/cmd" command, then the "got_int" needs to abort
1052 * the ":g" command.
1053 * For ":g/pat/vi" we reset "got_int" when used once. When used
1054 * a second time we go back to Ex mode and abort the ":g" command. */
1055 if (got_int)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001056 {
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001057 if (noexmode && global_busy && !exmode_active && previous_got_int)
1058 {
1059 /* Typed two CTRL-C in a row: go back to ex mode as if "Q" was
1060 * used and keep "got_int" set, so that it aborts ":g". */
1061 exmode_active = EXMODE_NORMAL;
1062 State = NORMAL;
1063 }
1064 else if (!global_busy || !exmode_active)
1065 {
1066 if (!quit_more)
1067 (void)vgetc(); /* flush all buffers */
1068 got_int = FALSE;
1069 }
1070 previous_got_int = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001071 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001072 else
1073 previous_got_int = FALSE;
1074
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001075 if (!exmode_active)
1076 msg_scroll = FALSE;
1077 quit_more = FALSE;
1078
1079 /*
1080 * If skip redraw is set (for ":" in wait_return()), don't redraw now.
1081 * If there is nothing in the stuff_buffer or do_redraw is TRUE,
1082 * update cursor and redraw.
1083 */
1084 if (skip_redraw || exmode_active)
1085 skip_redraw = FALSE;
1086 else if (do_redraw || stuff_empty())
1087 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001088#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001089 /* Trigger CursorMoved if the cursor moved. */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001090 if (!finish_op && (
1091# ifdef FEAT_AUTOCMD
1092 has_cursormoved()
1093# endif
1094# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
1095 ||
1096# endif
1097# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001098 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001099# endif
1100 )
1101 && !equalpos(last_cursormoved, curwin->w_cursor))
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001102 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001103# ifdef FEAT_AUTOCMD
1104 if (has_cursormoved())
1105 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL,
1106 FALSE, curbuf);
1107# endif
1108# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001109 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001110 {
1111 conceal_old_cursor_line = last_cursormoved.lnum;
1112 conceal_new_cursor_line = curwin->w_cursor.lnum;
1113 conceal_update_lines = TRUE;
1114 }
1115# endif
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001116 last_cursormoved = curwin->w_cursor;
1117 }
1118#endif
1119
Bram Moolenaar33aec762006-01-22 23:30:12 +00001120#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
1121 /* Scroll-binding for diff mode may have been postponed until
1122 * here. Avoids doing it for every change. */
1123 if (diff_need_scrollbind)
1124 {
1125 check_scrollbind((linenr_T)0, 0L);
1126 diff_need_scrollbind = FALSE;
1127 }
1128#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001129#if defined(FEAT_FOLDING) && defined(FEAT_VISUAL)
1130 /* Include a closed fold completely in the Visual area. */
1131 foldAdjustVisual();
1132#endif
1133#ifdef FEAT_FOLDING
1134 /*
1135 * When 'foldclose' is set, apply 'foldlevel' to folds that don't
1136 * contain the cursor.
1137 * When 'foldopen' is "all", open the fold(s) under the cursor.
1138 * This may mark the window for redrawing.
1139 */
1140 if (hasAnyFolding(curwin) && !char_avail())
1141 {
1142 foldCheckClose();
1143 if (fdo_flags & FDO_ALL)
1144 foldOpenCursor();
1145 }
1146#endif
1147
1148 /*
1149 * Before redrawing, make sure w_topline is correct, and w_leftcol
1150 * if lines don't wrap, and w_skipcol if lines wrap.
1151 */
1152 update_topline();
1153 validate_cursor();
1154
1155#ifdef FEAT_VISUAL
1156 if (VIsual_active)
1157 update_curbuf(INVERTED);/* update inverted part */
1158 else
1159#endif
1160 if (must_redraw)
1161 update_screen(0);
1162 else if (redraw_cmdline || clear_cmdline)
1163 showmode();
1164#ifdef FEAT_WINDOWS
1165 redraw_statuslines();
1166#endif
1167#ifdef FEAT_TITLE
1168 if (need_maketitle)
1169 maketitle();
1170#endif
1171 /* display message after redraw */
1172 if (keep_msg != NULL)
1173 {
1174 char_u *p;
1175
1176 /* msg_attr_keep() will set keep_msg to NULL, must free the
1177 * string here. */
1178 p = keep_msg;
Bram Moolenaar238a5642006-02-21 22:12:05 +00001179 keep_msg = NULL;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001180 msg_attr(p, keep_msg_attr);
1181 vim_free(p);
1182 }
1183 if (need_fileinfo) /* show file info after redraw */
1184 {
1185 fileinfo(FALSE, TRUE, FALSE);
1186 need_fileinfo = FALSE;
1187 }
1188
1189 emsg_on_display = FALSE; /* can delete error message now */
1190 did_emsg = FALSE;
1191 msg_didany = FALSE; /* reset lines_left in msg_start() */
Bram Moolenaar661b1822005-07-28 22:36:45 +00001192 may_clear_sb_text(); /* clear scroll-back text on next msg */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001193 showruler(FALSE);
1194
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001195# if defined(FEAT_CONCEAL)
1196 if (conceal_update_lines
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001197 && (conceal_old_cursor_line != conceal_new_cursor_line
1198 || conceal_cursor_line(curwin)
1199 || need_cursor_line_redraw))
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001200 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001201 if (conceal_old_cursor_line != conceal_new_cursor_line)
1202 update_single_line(curwin, conceal_old_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001203 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);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002470 (void)open_buffer(TRUE, NULL, 0); /* create memfile and read file */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002471 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);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002591
2592 /* create memfile, read file */
2593 (void)open_buffer(FALSE, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002594
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002595#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar84212822006-11-07 21:59:47 +00002596 if (swap_exists_action == SEA_QUIT)
2597 {
2598 if (got_int || only_one_window())
2599 {
2600 /* abort selected or quit and only one window */
2601 did_emsg = FALSE; /* avoid hit-enter prompt */
2602 getout(1);
2603 }
2604 /* We can't close the window, it would disturb what
2605 * happens next. Clear the file name and set the arg
2606 * index to -1 to delete it later. */
2607 setfname(curbuf, NULL, NULL, FALSE);
2608 curwin->w_arg_idx = -1;
2609 swap_exists_action = SEA_NONE;
2610 }
2611 else
2612 handle_swap_exists(NULL);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002613#endif
2614#ifdef FEAT_AUTOCMD
Bram Moolenaar89d40322006-08-29 15:30:07 +00002615 dorewind = TRUE; /* start again */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002616#endif
2617 }
2618#ifdef FEAT_WINDOWS
2619 ui_breakcheck();
2620 if (got_int)
2621 {
2622 (void)vgetc(); /* only break the file loading, not the rest */
2623 break;
2624 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002625 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002626#endif
2627#ifdef FEAT_WINDOWS
2628 if (parmp->window_layout == WIN_TABS)
2629 goto_tabpage(1);
2630 else
2631 curwin = firstwin;
2632 curbuf = curwin->w_buffer;
2633#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002634#ifdef FEAT_AUTOCMD
2635 --autocmd_no_enter;
2636 --autocmd_no_leave;
2637#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002638 }
2639}
2640
2641#ifdef FEAT_WINDOWS
2642 /*
2643 * If opened more than one window, start editing files in the other
2644 * windows. make_windows() has already opened the windows.
2645 */
2646 static void
2647edit_buffers(parmp)
2648 mparm_T *parmp;
2649{
2650 int arg_idx; /* index in argument list */
2651 int i;
Bram Moolenaar84212822006-11-07 21:59:47 +00002652 int advance = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002653
2654# ifdef FEAT_AUTOCMD
2655 /*
2656 * Don't execute Win/Buf Enter/Leave autocommands here
2657 */
2658 ++autocmd_no_enter;
2659 ++autocmd_no_leave;
2660# endif
Bram Moolenaar84212822006-11-07 21:59:47 +00002661
2662 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2663 if (curwin->w_arg_idx == -1)
2664 {
2665 win_close(curwin, TRUE);
2666 advance = FALSE;
2667 }
2668
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002669 arg_idx = 1;
2670 for (i = 1; i < parmp->window_count; ++i)
2671 {
Bram Moolenaar84212822006-11-07 21:59:47 +00002672 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2673 if (curwin->w_arg_idx == -1)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002674 {
Bram Moolenaar84212822006-11-07 21:59:47 +00002675 ++arg_idx;
2676 win_close(curwin, TRUE);
2677 advance = FALSE;
2678 continue;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002679 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002680
Bram Moolenaar84212822006-11-07 21:59:47 +00002681 if (advance)
2682 {
2683 if (parmp->window_layout == WIN_TABS)
2684 {
2685 if (curtab->tp_next == NULL) /* just checking */
2686 break;
2687 goto_tabpage(0);
2688 }
2689 else
2690 {
2691 if (curwin->w_next == NULL) /* just checking */
2692 break;
2693 win_enter(curwin->w_next, FALSE);
2694 }
2695 }
2696 advance = TRUE;
2697
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002698 /* Only open the file if there is no file in this window yet (that can
Bram Moolenaar84212822006-11-07 21:59:47 +00002699 * happen when .vimrc contains ":sall"). */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002700 if (curbuf == firstwin->w_buffer || curbuf->b_ffname == NULL)
2701 {
2702 curwin->w_arg_idx = arg_idx;
Bram Moolenaar84212822006-11-07 21:59:47 +00002703 /* Edit file from arg list, if there is one. When "Quit" selected
2704 * at the ATTENTION prompt close the window. */
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002705# ifdef HAS_SWAP_EXISTS_ACTION
2706 swap_exists_did_quit = FALSE;
2707# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002708 (void)do_ecmd(0, arg_idx < GARGCOUNT
2709 ? alist_name(&GARGLIST[arg_idx]) : NULL,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002710 NULL, NULL, ECMD_LASTL, ECMD_HIDE, curwin);
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002711# ifdef HAS_SWAP_EXISTS_ACTION
2712 if (swap_exists_did_quit)
Bram Moolenaar84212822006-11-07 21:59:47 +00002713 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002714 /* abort or quit selected */
Bram Moolenaar84212822006-11-07 21:59:47 +00002715 if (got_int || only_one_window())
2716 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002717 /* abort selected and only one window */
Bram Moolenaar84212822006-11-07 21:59:47 +00002718 did_emsg = FALSE; /* avoid hit-enter prompt */
2719 getout(1);
2720 }
2721 win_close(curwin, TRUE);
2722 advance = FALSE;
2723 }
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002724# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002725 if (arg_idx == GARGCOUNT - 1)
2726 arg_had_last = TRUE;
2727 ++arg_idx;
2728 }
2729 ui_breakcheck();
2730 if (got_int)
2731 {
2732 (void)vgetc(); /* only break the file loading, not the rest */
2733 break;
2734 }
2735 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002736
2737 if (parmp->window_layout == WIN_TABS)
2738 goto_tabpage(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002739# ifdef FEAT_AUTOCMD
2740 --autocmd_no_enter;
2741# endif
2742 win_enter(firstwin, FALSE); /* back to first window */
2743# ifdef FEAT_AUTOCMD
2744 --autocmd_no_leave;
2745# endif
2746 TIME_MSG("editing files in windows");
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002747 if (parmp->window_count > 1 && parmp->window_layout != WIN_TABS)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002748 win_equal(curwin, FALSE, 'b'); /* adjust heights */
2749}
2750#endif /* FEAT_WINDOWS */
2751
2752/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002753 * Execute the commands from --cmd arguments "cmds[cnt]".
2754 */
2755 static void
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002756exe_pre_commands(parmp)
2757 mparm_T *parmp;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002758{
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002759 char_u **cmds = parmp->pre_commands;
2760 int cnt = parmp->n_pre_commands;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002761 int i;
2762
2763 if (cnt > 0)
2764 {
2765 curwin->w_cursor.lnum = 0; /* just in case.. */
2766 sourcing_name = (char_u *)_("pre-vimrc command line");
2767# ifdef FEAT_EVAL
2768 current_SID = SID_CMDARG;
2769# endif
2770 for (i = 0; i < cnt; ++i)
2771 do_cmdline_cmd(cmds[i]);
2772 sourcing_name = NULL;
2773# ifdef FEAT_EVAL
2774 current_SID = 0;
2775# endif
2776 TIME_MSG("--cmd commands");
2777 }
2778}
2779
2780/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002781 * Execute "+", "-c" and "-S" arguments.
2782 */
2783 static void
2784exe_commands(parmp)
2785 mparm_T *parmp;
2786{
2787 int i;
2788
2789 /*
2790 * We start commands on line 0, make "vim +/pat file" match a
2791 * pattern on line 1. But don't move the cursor when an autocommand
2792 * with g`" was used.
2793 */
2794 msg_scroll = TRUE;
2795 if (parmp->tagname == NULL && curwin->w_cursor.lnum <= 1)
2796 curwin->w_cursor.lnum = 0;
2797 sourcing_name = (char_u *)"command line";
2798#ifdef FEAT_EVAL
2799 current_SID = SID_CARG;
2800#endif
2801 for (i = 0; i < parmp->n_commands; ++i)
2802 {
2803 do_cmdline_cmd(parmp->commands[i]);
2804 if (parmp->cmds_tofree[i])
2805 vim_free(parmp->commands[i]);
2806 }
2807 sourcing_name = NULL;
2808#ifdef FEAT_EVAL
2809 current_SID = 0;
2810#endif
2811 if (curwin->w_cursor.lnum == 0)
2812 curwin->w_cursor.lnum = 1;
2813
2814 if (!exmode_active)
2815 msg_scroll = FALSE;
2816
2817#ifdef FEAT_QUICKFIX
2818 /* When started with "-q errorfile" jump to first error again. */
2819 if (parmp->edit_type == EDIT_QF)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002820 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002821#endif
2822 TIME_MSG("executing command arguments");
2823}
2824
2825/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002826 * Source startup scripts.
2827 */
2828 static void
2829source_startup_scripts(parmp)
2830 mparm_T *parmp;
2831{
2832 int i;
2833
2834 /*
2835 * For "evim" source evim.vim first of all, so that the user can overrule
2836 * any things he doesn't like.
2837 */
2838 if (parmp->evim_mode)
2839 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002840 (void)do_source((char_u *)EVIM_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002841 TIME_MSG("source evim file");
2842 }
2843
2844 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002845 * If -u argument given, use only the initializations from that file and
Bram Moolenaar58d98232005-07-23 22:25:46 +00002846 * nothing else.
2847 */
2848 if (parmp->use_vimrc != NULL)
2849 {
Bram Moolenaar231334e2005-07-25 20:46:57 +00002850 if (STRCMP(parmp->use_vimrc, "NONE") == 0
2851 || STRCMP(parmp->use_vimrc, "NORC") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002852 {
2853#ifdef FEAT_GUI
2854 if (use_gvimrc == NULL) /* don't load gvimrc either */
2855 use_gvimrc = parmp->use_vimrc;
2856#endif
2857 if (parmp->use_vimrc[2] == 'N')
2858 p_lpl = FALSE; /* don't load plugins either */
2859 }
2860 else
2861 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002862 if (do_source(parmp->use_vimrc, FALSE, DOSO_NONE) != OK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002863 EMSG2(_("E282: Cannot read from \"%s\""), parmp->use_vimrc);
2864 }
2865 }
2866 else if (!silent_mode)
2867 {
2868#ifdef AMIGA
2869 struct Process *proc = (struct Process *)FindTask(0L);
2870 APTR save_winptr = proc->pr_WindowPtr;
2871
2872 /* Avoid a requester here for a volume that doesn't exist. */
2873 proc->pr_WindowPtr = (APTR)-1L;
2874#endif
2875
2876 /*
2877 * Get system wide defaults, if the file name is defined.
2878 */
2879#ifdef SYS_VIMRC_FILE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002880 (void)do_source((char_u *)SYS_VIMRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002881#endif
Bram Moolenaar1056d982006-03-09 22:37:52 +00002882#ifdef MACOS_X
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002883 (void)do_source((char_u *)"$VIMRUNTIME/macmap.vim", FALSE, DOSO_NONE);
Bram Moolenaar1056d982006-03-09 22:37:52 +00002884#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00002885
2886 /*
2887 * Try to read initialization commands from the following places:
2888 * - environment variable VIMINIT
2889 * - user vimrc file (s:.vimrc for Amiga, ~/.vimrc otherwise)
2890 * - second user vimrc file ($VIM/.vimrc for Dos)
2891 * - environment variable EXINIT
2892 * - user exrc file (s:.exrc for Amiga, ~/.exrc otherwise)
2893 * - second user exrc file ($VIM/.exrc for Dos)
2894 * The first that exists is used, the rest is ignored.
2895 */
2896 if (process_env((char_u *)"VIMINIT", TRUE) != OK)
2897 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002898 if (do_source((char_u *)USR_VIMRC_FILE, TRUE, DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00002899#ifdef USR_VIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002900 && do_source((char_u *)USR_VIMRC_FILE2, TRUE,
2901 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00002902#endif
2903#ifdef USR_VIMRC_FILE3
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002904 && do_source((char_u *)USR_VIMRC_FILE3, TRUE,
2905 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00002906#endif
2907 && process_env((char_u *)"EXINIT", FALSE) == FAIL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002908 && do_source((char_u *)USR_EXRC_FILE, FALSE, DOSO_NONE) == FAIL)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002909 {
2910#ifdef USR_EXRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002911 (void)do_source((char_u *)USR_EXRC_FILE2, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002912#endif
2913 }
2914 }
2915
2916 /*
2917 * Read initialization commands from ".vimrc" or ".exrc" in current
2918 * directory. This is only done if the 'exrc' option is set.
2919 * Because of security reasons we disallow shell and write commands
2920 * now, except for unix if the file is owned by the user or 'secure'
2921 * option has been reset in environment of global ".exrc" or ".vimrc".
2922 * Only do this if VIMRC_FILE is not the same as USR_VIMRC_FILE or
2923 * SYS_VIMRC_FILE.
2924 */
2925 if (p_exrc)
2926 {
2927#if defined(UNIX) || defined(VMS)
2928 /* If ".vimrc" file is not owned by user, set 'secure' mode. */
2929 if (!file_owned(VIMRC_FILE))
2930#endif
2931 secure = p_secure;
2932
2933 i = FAIL;
2934 if (fullpathcmp((char_u *)USR_VIMRC_FILE,
2935 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
2936#ifdef USR_VIMRC_FILE2
2937 && fullpathcmp((char_u *)USR_VIMRC_FILE2,
2938 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
2939#endif
2940#ifdef USR_VIMRC_FILE3
2941 && fullpathcmp((char_u *)USR_VIMRC_FILE3,
2942 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
2943#endif
2944#ifdef SYS_VIMRC_FILE
2945 && fullpathcmp((char_u *)SYS_VIMRC_FILE,
2946 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
2947#endif
2948 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002949 i = do_source((char_u *)VIMRC_FILE, TRUE, DOSO_VIMRC);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002950
2951 if (i == FAIL)
2952 {
2953#if defined(UNIX) || defined(VMS)
2954 /* if ".exrc" is not owned by user set 'secure' mode */
2955 if (!file_owned(EXRC_FILE))
2956 secure = p_secure;
2957 else
2958 secure = 0;
2959#endif
2960 if ( fullpathcmp((char_u *)USR_EXRC_FILE,
2961 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
2962#ifdef USR_EXRC_FILE2
2963 && fullpathcmp((char_u *)USR_EXRC_FILE2,
2964 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
2965#endif
2966 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002967 (void)do_source((char_u *)EXRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002968 }
2969 }
2970 if (secure == 2)
2971 need_wait_return = TRUE;
2972 secure = 0;
2973#ifdef AMIGA
2974 proc->pr_WindowPtr = save_winptr;
2975#endif
2976 }
2977 TIME_MSG("sourcing vimrc file(s)");
2978}
2979
2980/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002981 * Setup to start using the GUI. Exit with an error when not available.
2982 */
2983 static void
2984main_start_gui()
2985{
2986#ifdef FEAT_GUI
2987 gui.starting = TRUE; /* start GUI a bit later */
2988#else
2989 mch_errmsg(_(e_nogvim));
2990 mch_errmsg("\n");
2991 mch_exit(2);
2992#endif
2993}
2994
2995/*
Bram Moolenaar49325942007-05-10 19:19:59 +00002996 * Get an environment variable, and execute it as Ex commands.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002997 * Returns FAIL if the environment variable was not executed, OK otherwise.
2998 */
2999 int
3000process_env(env, is_viminit)
3001 char_u *env;
3002 int is_viminit; /* when TRUE, called for VIMINIT */
3003{
3004 char_u *initstr;
3005 char_u *save_sourcing_name;
3006 linenr_T save_sourcing_lnum;
3007#ifdef FEAT_EVAL
3008 scid_T save_sid;
3009#endif
3010
3011 if ((initstr = mch_getenv(env)) != NULL && *initstr != NUL)
3012 {
3013 if (is_viminit)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003014 vimrc_found(NULL, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003015 save_sourcing_name = sourcing_name;
3016 save_sourcing_lnum = sourcing_lnum;
3017 sourcing_name = env;
3018 sourcing_lnum = 0;
3019#ifdef FEAT_EVAL
3020 save_sid = current_SID;
3021 current_SID = SID_ENV;
3022#endif
3023 do_cmdline_cmd(initstr);
3024 sourcing_name = save_sourcing_name;
3025 sourcing_lnum = save_sourcing_lnum;
3026#ifdef FEAT_EVAL
3027 current_SID = save_sid;;
3028#endif
3029 return OK;
3030 }
3031 return FAIL;
3032}
3033
3034#if defined(UNIX) || defined(VMS)
3035/*
3036 * Return TRUE if we are certain the user owns the file "fname".
3037 * Used for ".vimrc" and ".exrc".
3038 * Use both stat() and lstat() for extra security.
3039 */
3040 static int
3041file_owned(fname)
3042 char *fname;
3043{
3044 struct stat s;
3045# ifdef UNIX
3046 uid_t uid = getuid();
3047# else /* VMS */
3048 uid_t uid = ((getgid() << 16) | getuid());
3049# endif
3050
3051 return !(mch_stat(fname, &s) != 0 || s.st_uid != uid
3052# ifdef HAVE_LSTAT
3053 || mch_lstat(fname, &s) != 0 || s.st_uid != uid
3054# endif
3055 );
3056}
3057#endif
3058
3059/*
3060 * Give an error message main_errors["n"] and exit.
3061 */
3062 static void
3063mainerr(n, str)
3064 int n; /* one of the ME_ defines */
3065 char_u *str; /* extra argument or NULL */
3066{
3067#if defined(UNIX) || defined(__EMX__) || defined(VMS)
3068 reset_signals(); /* kill us with CTRL-C here, if you like */
3069#endif
3070
3071 mch_errmsg(longVersion);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003072 mch_errmsg("\n");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003073 mch_errmsg(_(main_errors[n]));
3074 if (str != NULL)
3075 {
3076 mch_errmsg(": \"");
3077 mch_errmsg((char *)str);
3078 mch_errmsg("\"");
3079 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003080 mch_errmsg(_("\nMore info with: \"vim -h\"\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003081
3082 mch_exit(1);
3083}
3084
3085 void
3086mainerr_arg_missing(str)
3087 char_u *str;
3088{
3089 mainerr(ME_ARG_MISSING, str);
3090}
3091
3092/*
3093 * print a message with three spaces prepended and '\n' appended.
3094 */
3095 static void
3096main_msg(s)
3097 char *s;
3098{
3099 mch_msg(" ");
3100 mch_msg(s);
3101 mch_msg("\n");
3102}
3103
3104/*
3105 * Print messages for "vim -h" or "vim --help" and exit.
3106 */
3107 static void
3108usage()
3109{
3110 int i;
3111 static char *(use[]) =
3112 {
3113 N_("[file ..] edit specified file(s)"),
3114 N_("- read text from stdin"),
3115 N_("-t tag edit file where tag is defined"),
3116#ifdef FEAT_QUICKFIX
3117 N_("-q [errorfile] edit file with first error")
3118#endif
3119 };
3120
3121#if defined(UNIX) || defined(__EMX__) || defined(VMS)
3122 reset_signals(); /* kill us with CTRL-C here, if you like */
3123#endif
3124
3125 mch_msg(longVersion);
3126 mch_msg(_("\n\nusage:"));
3127 for (i = 0; ; ++i)
3128 {
3129 mch_msg(_(" vim [arguments] "));
3130 mch_msg(_(use[i]));
3131 if (i == (sizeof(use) / sizeof(char_u *)) - 1)
3132 break;
3133 mch_msg(_("\n or:"));
3134 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003135#ifdef VMS
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003136 mch_msg(_("\nWhere case is ignored prepend / to make flag upper case"));
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003137#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003138
3139 mch_msg(_("\n\nArguments:\n"));
3140 main_msg(_("--\t\t\tOnly file names after this"));
3141#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
3142 main_msg(_("--literal\t\tDon't expand wildcards"));
3143#endif
3144#ifdef FEAT_OLE
3145 main_msg(_("-register\t\tRegister this gvim for OLE"));
3146 main_msg(_("-unregister\t\tUnregister gvim for OLE"));
3147#endif
3148#ifdef FEAT_GUI
3149 main_msg(_("-g\t\t\tRun using GUI (like \"gvim\")"));
3150 main_msg(_("-f or --nofork\tForeground: Don't fork when starting GUI"));
3151#endif
3152 main_msg(_("-v\t\t\tVi mode (like \"vi\")"));
3153 main_msg(_("-e\t\t\tEx mode (like \"ex\")"));
3154 main_msg(_("-s\t\t\tSilent (batch) mode (only for \"ex\")"));
3155#ifdef FEAT_DIFF
3156 main_msg(_("-d\t\t\tDiff mode (like \"vimdiff\")"));
3157#endif
3158 main_msg(_("-y\t\t\tEasy mode (like \"evim\", modeless)"));
3159 main_msg(_("-R\t\t\tReadonly mode (like \"view\")"));
3160 main_msg(_("-Z\t\t\tRestricted mode (like \"rvim\")"));
3161 main_msg(_("-m\t\t\tModifications (writing files) not allowed"));
3162 main_msg(_("-M\t\t\tModifications in text not allowed"));
3163 main_msg(_("-b\t\t\tBinary mode"));
3164#ifdef FEAT_LISP
3165 main_msg(_("-l\t\t\tLisp mode"));
3166#endif
3167 main_msg(_("-C\t\t\tCompatible with Vi: 'compatible'"));
3168 main_msg(_("-N\t\t\tNot fully Vi compatible: 'nocompatible'"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003169 main_msg(_("-V[N][fname]\t\tBe verbose [level N] [log messages to fname]"));
3170#ifdef FEAT_EVAL
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003171 main_msg(_("-D\t\t\tDebugging mode"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003172#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003173 main_msg(_("-n\t\t\tNo swap file, use memory only"));
3174 main_msg(_("-r\t\t\tList swap files and exit"));
3175 main_msg(_("-r (with file name)\tRecover crashed session"));
3176 main_msg(_("-L\t\t\tSame as -r"));
3177#ifdef AMIGA
3178 main_msg(_("-f\t\t\tDon't use newcli to open window"));
3179 main_msg(_("-dev <device>\t\tUse <device> for I/O"));
3180#endif
3181#ifdef FEAT_ARABIC
3182 main_msg(_("-A\t\t\tstart in Arabic mode"));
3183#endif
3184#ifdef FEAT_RIGHTLEFT
3185 main_msg(_("-H\t\t\tStart in Hebrew mode"));
3186#endif
3187#ifdef FEAT_FKMAP
3188 main_msg(_("-F\t\t\tStart in Farsi mode"));
3189#endif
3190 main_msg(_("-T <terminal>\tSet terminal type to <terminal>"));
3191 main_msg(_("-u <vimrc>\t\tUse <vimrc> instead of any .vimrc"));
3192#ifdef FEAT_GUI
3193 main_msg(_("-U <gvimrc>\t\tUse <gvimrc> instead of any .gvimrc"));
3194#endif
3195 main_msg(_("--noplugin\t\tDon't load plugin scripts"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003196#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003197 main_msg(_("-p[N]\t\tOpen N tab pages (default: one for each file)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003198 main_msg(_("-o[N]\t\tOpen N windows (default: one for each file)"));
3199 main_msg(_("-O[N]\t\tLike -o but split vertically"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003200#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003201 main_msg(_("+\t\t\tStart at end of file"));
3202 main_msg(_("+<lnum>\t\tStart at line <lnum>"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003203 main_msg(_("--cmd <command>\tExecute <command> before loading any vimrc file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003204 main_msg(_("-c <command>\t\tExecute <command> after loading the first file"));
3205 main_msg(_("-S <session>\t\tSource file <session> after loading the first file"));
3206 main_msg(_("-s <scriptin>\tRead Normal mode commands from file <scriptin>"));
3207 main_msg(_("-w <scriptout>\tAppend all typed commands to file <scriptout>"));
3208 main_msg(_("-W <scriptout>\tWrite all typed commands to file <scriptout>"));
3209#ifdef FEAT_CRYPT
3210 main_msg(_("-x\t\t\tEdit encrypted files"));
3211#endif
3212#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
3213# if defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)
3214 main_msg(_("-display <display>\tConnect vim to this particular X-server"));
3215# endif
3216 main_msg(_("-X\t\t\tDo not connect to X server"));
3217#endif
3218#ifdef FEAT_CLIENTSERVER
3219 main_msg(_("--remote <files>\tEdit <files> in a Vim server if possible"));
3220 main_msg(_("--remote-silent <files> Same, don't complain if there is no server"));
3221 main_msg(_("--remote-wait <files> As --remote but wait for files to have been edited"));
3222 main_msg(_("--remote-wait-silent <files> Same, don't complain if there is no server"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003223# ifdef FEAT_WINDOWS
Bram Moolenaar82ad3242008-01-11 19:26:36 +00003224 main_msg(_("--remote-tab[-wait][-silent] <files> As --remote but use tab page per file"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003225# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003226 main_msg(_("--remote-send <keys>\tSend <keys> to a Vim server and exit"));
3227 main_msg(_("--remote-expr <expr>\tEvaluate <expr> in a Vim server and print result"));
3228 main_msg(_("--serverlist\t\tList available Vim server names and exit"));
3229 main_msg(_("--servername <name>\tSend to/become the Vim server <name>"));
3230#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003231#ifdef STARTUPTIME
Bram Moolenaar34ef52d2009-11-17 11:31:25 +00003232 main_msg(_("--startuptime <file>\tWrite startup timing messages to <file>"));
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003233#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003234#ifdef FEAT_VIMINFO
3235 main_msg(_("-i <viminfo>\t\tUse <viminfo> instead of .viminfo"));
3236#endif
3237 main_msg(_("-h or --help\tPrint Help (this message) and exit"));
3238 main_msg(_("--version\t\tPrint version information and exit"));
3239
3240#ifdef FEAT_GUI_X11
3241# ifdef FEAT_GUI_MOTIF
3242 mch_msg(_("\nArguments recognised by gvim (Motif version):\n"));
3243# else
3244# ifdef FEAT_GUI_ATHENA
3245# ifdef FEAT_GUI_NEXTAW
3246 mch_msg(_("\nArguments recognised by gvim (neXtaw version):\n"));
3247# else
3248 mch_msg(_("\nArguments recognised by gvim (Athena version):\n"));
3249# endif
3250# endif
3251# endif
3252 main_msg(_("-display <display>\tRun vim on <display>"));
3253 main_msg(_("-iconic\t\tStart vim iconified"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003254 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
3255 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
3256 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3257 main_msg(_("-boldfont <font>\tUse <font> for bold text"));
3258 main_msg(_("-italicfont <font>\tUse <font> for italic text"));
3259 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3260 main_msg(_("-borderwidth <width>\tUse a border width of <width> (also: -bw)"));
3261 main_msg(_("-scrollbarwidth <width> Use a scrollbar width of <width> (also: -sw)"));
3262# ifdef FEAT_GUI_ATHENA
3263 main_msg(_("-menuheight <height>\tUse a menu bar height of <height> (also: -mh)"));
3264# endif
3265 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3266 main_msg(_("+reverse\t\tDon't use reverse video (also: +rv)"));
3267 main_msg(_("-xrm <resource>\tSet the specified resource"));
3268#endif /* FEAT_GUI_X11 */
3269#if defined(FEAT_GUI) && defined(RISCOS)
3270 mch_msg(_("\nArguments recognised by gvim (RISC OS version):\n"));
3271 main_msg(_("--columns <number>\tInitial width of window in columns"));
3272 main_msg(_("--rows <number>\tInitial height of window in rows"));
3273#endif
3274#ifdef FEAT_GUI_GTK
3275 mch_msg(_("\nArguments recognised by gvim (GTK+ version):\n"));
3276 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3277 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3278 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3279 main_msg(_("-display <display>\tRun vim on <display> (also: --display)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003280 main_msg(_("--role <role>\tSet a unique role to identify the main window"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003281 main_msg(_("--socketid <xid>\tOpen Vim inside another GTK widget"));
3282#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003283#ifdef FEAT_GUI_W32
3284 main_msg(_("-P <parent title>\tOpen Vim inside parent application"));
Bram Moolenaar78e17622007-08-30 10:26:19 +00003285 main_msg(_("--windowid <HWND>\tOpen Vim inside another win32 widget"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003286#endif
3287
3288#ifdef FEAT_GUI_GNOME
3289 /* Gnome gives extra messages for --help if we continue, but not for -h. */
3290 if (gui.starting)
3291 mch_msg("\n");
3292 else
3293#endif
3294 mch_exit(0);
3295}
3296
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00003297#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003298/*
3299 * Check the result of the ATTENTION dialog:
3300 * When "Quit" selected, exit Vim.
3301 * When "Recover" selected, recover the file.
3302 */
3303 static void
3304check_swap_exists_action()
3305{
3306 if (swap_exists_action == SEA_QUIT)
3307 getout(1);
3308 handle_swap_exists(NULL);
3309}
3310#endif
3311
3312#if defined(STARTUPTIME) || defined(PROTO)
3313static void time_diff __ARGS((struct timeval *then, struct timeval *now));
3314
3315static struct timeval prev_timeval;
3316
Bram Moolenaar3f269672009-11-03 11:11:11 +00003317# ifdef WIN3264
3318/*
3319 * Windows doesn't have gettimeofday(), although it does have struct timeval.
3320 */
3321 static int
3322gettimeofday(struct timeval *tv, char *dummy)
3323{
3324 long t = clock();
3325 tv->tv_sec = t / CLOCKS_PER_SEC;
3326 tv->tv_usec = (t - tv->tv_sec * CLOCKS_PER_SEC) * 1000000 / CLOCKS_PER_SEC;
3327 return 0;
3328}
3329# endif
3330
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003331/*
3332 * Save the previous time before doing something that could nest.
3333 * set "*tv_rel" to the time elapsed so far.
3334 */
3335 void
3336time_push(tv_rel, tv_start)
3337 void *tv_rel, *tv_start;
3338{
3339 *((struct timeval *)tv_rel) = prev_timeval;
3340 gettimeofday(&prev_timeval, NULL);
3341 ((struct timeval *)tv_rel)->tv_usec = prev_timeval.tv_usec
3342 - ((struct timeval *)tv_rel)->tv_usec;
3343 ((struct timeval *)tv_rel)->tv_sec = prev_timeval.tv_sec
3344 - ((struct timeval *)tv_rel)->tv_sec;
3345 if (((struct timeval *)tv_rel)->tv_usec < 0)
3346 {
3347 ((struct timeval *)tv_rel)->tv_usec += 1000000;
3348 --((struct timeval *)tv_rel)->tv_sec;
3349 }
3350 *(struct timeval *)tv_start = prev_timeval;
3351}
3352
3353/*
3354 * Compute the previous time after doing something that could nest.
3355 * Subtract "*tp" from prev_timeval;
3356 * Note: The arguments are (void *) to avoid trouble with systems that don't
3357 * have struct timeval.
3358 */
3359 void
3360time_pop(tp)
3361 void *tp; /* actually (struct timeval *) */
3362{
3363 prev_timeval.tv_usec -= ((struct timeval *)tp)->tv_usec;
3364 prev_timeval.tv_sec -= ((struct timeval *)tp)->tv_sec;
3365 if (prev_timeval.tv_usec < 0)
3366 {
3367 prev_timeval.tv_usec += 1000000;
3368 --prev_timeval.tv_sec;
3369 }
3370}
3371
3372 static void
3373time_diff(then, now)
3374 struct timeval *then;
3375 struct timeval *now;
3376{
3377 long usec;
3378 long msec;
3379
3380 usec = now->tv_usec - then->tv_usec;
3381 msec = (now->tv_sec - then->tv_sec) * 1000L + usec / 1000L,
3382 usec = usec % 1000L;
3383 fprintf(time_fd, "%03ld.%03ld", msec, usec >= 0 ? usec : usec + 1000L);
3384}
3385
3386 void
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003387time_msg(mesg, tv_start)
3388 char *mesg;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003389 void *tv_start; /* only for do_source: start time; actually
3390 (struct timeval *) */
3391{
3392 static struct timeval start;
3393 struct timeval now;
3394
3395 if (time_fd != NULL)
3396 {
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003397 if (strstr(mesg, "STARTING") != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003398 {
3399 gettimeofday(&start, NULL);
3400 prev_timeval = start;
3401 fprintf(time_fd, "\n\ntimes in msec\n");
3402 fprintf(time_fd, " clock self+sourced self: sourced script\n");
3403 fprintf(time_fd, " clock elapsed: other lines\n\n");
3404 }
3405 gettimeofday(&now, NULL);
3406 time_diff(&start, &now);
3407 if (((struct timeval *)tv_start) != NULL)
3408 {
3409 fprintf(time_fd, " ");
3410 time_diff(((struct timeval *)tv_start), &now);
3411 }
3412 fprintf(time_fd, " ");
3413 time_diff(&prev_timeval, &now);
3414 prev_timeval = now;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003415 fprintf(time_fd, ": %s\n", mesg);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003416 }
3417}
3418
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003419#endif
3420
3421#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
3422
3423/*
3424 * Common code for the X command server and the Win32 command server.
3425 */
3426
Bram Moolenaareb94e552006-03-11 21:35:11 +00003427static char_u *build_drop_cmd __ARGS((int filec, char **filev, int tabs, int sendReply));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003428
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003429/*
3430 * Do the client-server stuff, unless "--servername ''" was used.
3431 */
3432 static void
3433exec_on_server(parmp)
3434 mparm_T *parmp;
3435{
3436 if (parmp->serverName_arg == NULL || *parmp->serverName_arg != NUL)
3437 {
3438# ifdef WIN32
3439 /* Initialise the client/server messaging infrastructure. */
3440 serverInitMessaging();
3441# endif
3442
3443 /*
3444 * When a command server argument was found, execute it. This may
3445 * exit Vim when it was successful. Otherwise it's executed further
3446 * on. Remember the encoding used here in "serverStrEnc".
3447 */
3448 if (parmp->serverArg)
3449 {
3450 cmdsrv_main(&parmp->argc, parmp->argv,
3451 parmp->serverName_arg, &parmp->serverStr);
3452# ifdef FEAT_MBYTE
3453 parmp->serverStrEnc = vim_strsave(p_enc);
3454# endif
3455 }
3456
3457 /* If we're still running, get the name to register ourselves.
3458 * On Win32 can register right now, for X11 need to setup the
3459 * clipboard first, it's further down. */
3460 parmp->servername = serverMakeName(parmp->serverName_arg,
3461 parmp->argv[0]);
3462# ifdef WIN32
3463 if (parmp->servername != NULL)
3464 {
3465 serverSetName(parmp->servername);
3466 vim_free(parmp->servername);
3467 }
3468# endif
3469 }
3470}
3471
3472/*
3473 * Prepare for running as a Vim server.
3474 */
3475 static void
3476prepare_server(parmp)
3477 mparm_T *parmp;
3478{
3479# if defined(FEAT_X11)
3480 /*
3481 * Register for remote command execution with :serversend and --remote
3482 * unless there was a -X or a --servername '' on the command line.
3483 * Only register nongui-vim's with an explicit --servername argument.
Bram Moolenaar5f402312006-08-15 19:40:35 +00003484 * When running as root --servername is also required.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003485 */
3486 if (X_DISPLAY != NULL && parmp->servername != NULL && (
3487# ifdef FEAT_GUI
Bram Moolenaar5f402312006-08-15 19:40:35 +00003488 (gui.in_use
3489# ifdef UNIX
Bram Moolenaar311d9822007-02-27 15:48:28 +00003490 && getuid() != ROOT_UID
Bram Moolenaar5f402312006-08-15 19:40:35 +00003491# endif
3492 ) ||
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003493# endif
3494 parmp->serverName_arg != NULL))
3495 {
3496 (void)serverRegisterName(X_DISPLAY, parmp->servername);
3497 vim_free(parmp->servername);
3498 TIME_MSG("register server name");
3499 }
3500 else
3501 serverDelayedStartName = parmp->servername;
3502# endif
3503
3504 /*
3505 * Execute command ourselves if we're here because the send failed (or
3506 * else we would have exited above).
3507 */
3508 if (parmp->serverStr != NULL)
3509 {
3510 char_u *p;
3511
3512 server_to_input_buf(serverConvert(parmp->serverStrEnc,
3513 parmp->serverStr, &p));
3514 vim_free(p);
3515 }
3516}
3517
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003518 static void
3519cmdsrv_main(argc, argv, serverName_arg, serverStr)
3520 int *argc;
3521 char **argv;
3522 char_u *serverName_arg;
3523 char_u **serverStr;
3524{
3525 char_u *res;
3526 int i;
3527 char_u *sname;
3528 int ret;
3529 int didone = FALSE;
3530 int exiterr = 0;
3531 char **newArgV = argv + 1;
3532 int newArgC = 1,
3533 Argc = *argc;
3534 int argtype;
3535#define ARGTYPE_OTHER 0
3536#define ARGTYPE_EDIT 1
3537#define ARGTYPE_EDIT_WAIT 2
3538#define ARGTYPE_SEND 3
3539 int silent = FALSE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003540 int tabs = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003541# ifndef FEAT_X11
3542 HWND srv;
3543# else
3544 Window srv;
3545
3546 setup_term_clip();
3547# endif
3548
3549 sname = serverMakeName(serverName_arg, argv[0]);
3550 if (sname == NULL)
3551 return;
3552
3553 /*
3554 * Execute the command server related arguments and remove them
3555 * from the argc/argv array; We may have to return into main()
3556 */
3557 for (i = 1; i < Argc; i++)
3558 {
3559 res = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003560 if (STRCMP(argv[i], "--") == 0) /* end of option arguments */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003561 {
3562 for (; i < *argc; i++)
3563 {
3564 *newArgV++ = argv[i];
3565 newArgC++;
3566 }
3567 break;
3568 }
3569
Bram Moolenaareb94e552006-03-11 21:35:11 +00003570 if (STRICMP(argv[i], "--remote-send") == 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003571 argtype = ARGTYPE_SEND;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003572 else if (STRNICMP(argv[i], "--remote", 8) == 0)
3573 {
3574 char *p = argv[i] + 8;
3575
3576 argtype = ARGTYPE_EDIT;
3577 while (*p != NUL)
3578 {
3579 if (STRNICMP(p, "-wait", 5) == 0)
3580 {
3581 argtype = ARGTYPE_EDIT_WAIT;
3582 p += 5;
3583 }
3584 else if (STRNICMP(p, "-silent", 7) == 0)
3585 {
3586 silent = TRUE;
3587 p += 7;
3588 }
3589 else if (STRNICMP(p, "-tab", 4) == 0)
3590 {
3591 tabs = TRUE;
3592 p += 4;
3593 }
3594 else
3595 {
3596 argtype = ARGTYPE_OTHER;
3597 break;
3598 }
3599 }
3600 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003601 else
3602 argtype = ARGTYPE_OTHER;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003603
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003604 if (argtype != ARGTYPE_OTHER)
3605 {
3606 if (i == *argc - 1)
3607 mainerr_arg_missing((char_u *)argv[i]);
3608 if (argtype == ARGTYPE_SEND)
3609 {
3610 *serverStr = (char_u *)argv[i + 1];
3611 i++;
3612 }
3613 else
3614 {
3615 *serverStr = build_drop_cmd(*argc - i - 1, argv + i + 1,
Bram Moolenaareb94e552006-03-11 21:35:11 +00003616 tabs, argtype == ARGTYPE_EDIT_WAIT);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003617 if (*serverStr == NULL)
3618 {
3619 /* Probably out of memory, exit. */
3620 didone = TRUE;
3621 exiterr = 1;
3622 break;
3623 }
3624 Argc = i;
3625 }
3626# ifdef FEAT_X11
3627 if (xterm_dpy == NULL)
3628 {
3629 mch_errmsg(_("No display"));
3630 ret = -1;
3631 }
3632 else
3633 ret = serverSendToVim(xterm_dpy, sname, *serverStr,
3634 NULL, &srv, 0, 0, silent);
3635# else
3636 /* Win32 always works? */
3637 ret = serverSendToVim(sname, *serverStr, NULL, &srv, 0, silent);
3638# endif
3639 if (ret < 0)
3640 {
3641 if (argtype == ARGTYPE_SEND)
3642 {
3643 /* Failed to send, abort. */
3644 mch_errmsg(_(": Send failed.\n"));
3645 didone = TRUE;
3646 exiterr = 1;
3647 }
3648 else if (!silent)
3649 /* Let vim start normally. */
3650 mch_errmsg(_(": Send failed. Trying to execute locally\n"));
3651 break;
3652 }
3653
3654# ifdef FEAT_GUI_W32
3655 /* Guess that when the server name starts with "g" it's a GUI
3656 * server, which we can bring to the foreground here.
3657 * Foreground() in the server doesn't work very well. */
3658 if (argtype != ARGTYPE_SEND && TOUPPER_ASC(*sname) == 'G')
3659 SetForegroundWindow(srv);
3660# endif
3661
3662 /*
3663 * For --remote-wait: Wait until the server did edit each
3664 * file. Also detect that the server no longer runs.
3665 */
3666 if (ret >= 0 && argtype == ARGTYPE_EDIT_WAIT)
3667 {
3668 int numFiles = *argc - i - 1;
3669 int j;
3670 char_u *done = alloc(numFiles);
3671 char_u *p;
3672# ifdef FEAT_GUI_W32
3673 NOTIFYICONDATA ni;
3674 int count = 0;
3675 extern HWND message_window;
3676# endif
3677
3678 if (numFiles > 0 && argv[i + 1][0] == '+')
3679 /* Skip "+cmd" argument, don't wait for it to be edited. */
3680 --numFiles;
3681
3682# ifdef FEAT_GUI_W32
3683 ni.cbSize = sizeof(ni);
3684 ni.hWnd = message_window;
3685 ni.uID = 0;
3686 ni.uFlags = NIF_ICON|NIF_TIP;
3687 ni.hIcon = LoadIcon((HINSTANCE)GetModuleHandle(0), "IDR_VIM");
3688 sprintf(ni.szTip, _("%d of %d edited"), count, numFiles);
3689 Shell_NotifyIcon(NIM_ADD, &ni);
3690# endif
3691
3692 /* Wait for all files to unload in remote */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02003693 vim_memset(done, 0, numFiles);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003694 while (memchr(done, 0, numFiles) != NULL)
3695 {
3696# ifdef WIN32
3697 p = serverGetReply(srv, NULL, TRUE, TRUE);
3698 if (p == NULL)
3699 break;
3700# else
3701 if (serverReadReply(xterm_dpy, srv, &p, TRUE) < 0)
3702 break;
3703# endif
3704 j = atoi((char *)p);
3705 if (j >= 0 && j < numFiles)
3706 {
3707# ifdef FEAT_GUI_W32
3708 ++count;
3709 sprintf(ni.szTip, _("%d of %d edited"),
3710 count, numFiles);
3711 Shell_NotifyIcon(NIM_MODIFY, &ni);
3712# endif
3713 done[j] = 1;
3714 }
3715 }
3716# ifdef FEAT_GUI_W32
3717 Shell_NotifyIcon(NIM_DELETE, &ni);
3718# endif
3719 }
3720 }
3721 else if (STRICMP(argv[i], "--remote-expr") == 0)
3722 {
3723 if (i == *argc - 1)
3724 mainerr_arg_missing((char_u *)argv[i]);
3725# ifdef WIN32
3726 /* Win32 always works? */
3727 if (serverSendToVim(sname, (char_u *)argv[i + 1],
3728 &res, NULL, 1, FALSE) < 0)
3729# else
3730 if (xterm_dpy == NULL)
3731 mch_errmsg(_("No display: Send expression failed.\n"));
3732 else if (serverSendToVim(xterm_dpy, sname, (char_u *)argv[i + 1],
3733 &res, NULL, 1, 1, FALSE) < 0)
3734# endif
3735 {
3736 if (res != NULL && *res != NUL)
3737 {
3738 /* Output error from remote */
3739 mch_errmsg((char *)res);
3740 vim_free(res);
3741 res = NULL;
3742 }
3743 mch_errmsg(_(": Send expression failed.\n"));
3744 }
3745 }
3746 else if (STRICMP(argv[i], "--serverlist") == 0)
3747 {
3748# ifdef WIN32
3749 /* Win32 always works? */
3750 res = serverGetVimNames();
3751# else
3752 if (xterm_dpy != NULL)
3753 res = serverGetVimNames(xterm_dpy);
3754# endif
3755 if (called_emsg)
3756 mch_errmsg("\n");
3757 }
3758 else if (STRICMP(argv[i], "--servername") == 0)
3759 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +00003760 /* Already processed. Take it out of the command line */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003761 i++;
3762 continue;
3763 }
3764 else
3765 {
3766 *newArgV++ = argv[i];
3767 newArgC++;
3768 continue;
3769 }
3770 didone = TRUE;
3771 if (res != NULL && *res != NUL)
3772 {
3773 mch_msg((char *)res);
3774 if (res[STRLEN(res) - 1] != '\n')
3775 mch_msg("\n");
3776 }
3777 vim_free(res);
3778 }
3779
3780 if (didone)
3781 {
3782 display_errors(); /* display any collected messages */
3783 exit(exiterr); /* Mission accomplished - get out */
3784 }
3785
3786 /* Return back into main() */
3787 *argc = newArgC;
3788 vim_free(sname);
3789}
3790
3791/*
3792 * Build a ":drop" command to send to a Vim server.
3793 */
3794 static char_u *
Bram Moolenaareb94e552006-03-11 21:35:11 +00003795build_drop_cmd(filec, filev, tabs, sendReply)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003796 int filec;
3797 char **filev;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003798 int tabs; /* Use ":tab drop" instead of ":drop". */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003799 int sendReply;
3800{
3801 garray_T ga;
3802 int i;
3803 char_u *inicmd = NULL;
3804 char_u *p;
3805 char_u cwd[MAXPATHL];
3806
3807 if (filec > 0 && filev[0][0] == '+')
3808 {
3809 inicmd = (char_u *)filev[0] + 1;
3810 filev++;
3811 filec--;
3812 }
3813 /* Check if we have at least one argument. */
3814 if (filec <= 0)
3815 mainerr_arg_missing((char_u *)filev[-1]);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003816
3817 /* Temporarily cd to the current directory to handle relative file names. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003818 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 }
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003859 ga_concat(&ga, (char_u *)"|if exists('*inputrestore')|call inputrestore()|endif<CR>");
3860
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003861 /* The :drop commands goes to Insert mode when 'insertmode' is set, use
3862 * CTRL-\ CTRL-N again. */
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003863 ga_concat(&ga, (char_u *)"<C-\\><C-N>");
3864
3865 /* Switch back to the correct current directory (prior to temporary path
3866 * switch) unless 'autochdir' is set, in which case it will already be
3867 * correct after the :drop command. */
3868 ga_concat(&ga, (char_u *)":if !exists('+acd')||!&acd|cd -|endif<CR>");
3869
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003870 if (sendReply)
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003871 ga_concat(&ga, (char_u *)":call SetupRemoteReplies()<CR>");
3872 ga_concat(&ga, (char_u *)":");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003873 if (inicmd != NULL)
3874 {
3875 /* Can't use <CR> after "inicmd", because an "startinsert" would cause
3876 * the following commands to be inserted as text. Use a "|",
3877 * hopefully "inicmd" does allow this... */
3878 ga_concat(&ga, inicmd);
3879 ga_concat(&ga, (char_u *)"|");
3880 }
3881 /* Bring the window to the foreground, goto Insert mode when 'im' set and
3882 * clear command line. */
Bram Moolenaar567e4de2004-12-31 21:01:02 +00003883 ga_concat(&ga, (char_u *)"cal foreground()|if &im|star|en|redr|f<CR>");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003884 ga_append(&ga, NUL);
3885 return ga.ga_data;
3886}
3887
3888/*
3889 * Replace termcodes such as <CR> and insert as key presses if there is room.
3890 */
3891 void
3892server_to_input_buf(str)
3893 char_u *str;
3894{
3895 char_u *ptr = NULL;
3896 char_u *cpo_save = p_cpo;
3897
3898 /* Set 'cpoptions' the way we want it.
3899 * B set - backslashes are *not* treated specially
3900 * k set - keycodes are *not* reverse-engineered
3901 * < unset - <Key> sequences *are* interpreted
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00003902 * The last but one parameter of replace_termcodes() is TRUE so that the
3903 * <lt> sequence is recognised - needed for a real backslash.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003904 */
3905 p_cpo = (char_u *)"Bk";
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00003906 str = replace_termcodes((char_u *)str, &ptr, FALSE, TRUE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003907 p_cpo = cpo_save;
3908
3909 if (*ptr != NUL) /* trailing CTRL-V results in nothing */
3910 {
3911 /*
3912 * Add the string to the input stream.
3913 * Can't use add_to_input_buf() here, we now have K_SPECIAL bytes.
3914 *
3915 * First clear typed characters from the typeahead buffer, there could
3916 * be half a mapping there. Then append to the existing string, so
3917 * that multiple commands from a client are concatenated.
3918 */
3919 if (typebuf.tb_maplen < typebuf.tb_len)
3920 del_typebuf(typebuf.tb_len - typebuf.tb_maplen, typebuf.tb_maplen);
3921 (void)ins_typebuf(str, REMAP_NONE, typebuf.tb_len, TRUE, FALSE);
3922
3923 /* Let input_available() know we inserted text in the typeahead
3924 * buffer. */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003925 typebuf_was_filled = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003926 }
3927 vim_free((char_u *)ptr);
3928}
3929
3930/*
3931 * Evaluate an expression that the client sent to a string.
3932 * Handles disabling error messages and disables debugging, otherwise Vim
3933 * hangs, waiting for "cont" to be typed.
3934 */
3935 char_u *
3936eval_client_expr_to_string(expr)
3937 char_u *expr;
3938{
3939 char_u *res;
3940 int save_dbl = debug_break_level;
3941 int save_ro = redir_off;
3942
3943 debug_break_level = -1;
3944 redir_off = 0;
3945 ++emsg_skip;
3946
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003947 res = eval_to_string(expr, NULL, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003948
3949 debug_break_level = save_dbl;
3950 redir_off = save_ro;
3951 --emsg_skip;
3952
Bram Moolenaar63a121b2005-12-11 21:36:39 +00003953 /* A client can tell us to redraw, but not to display the cursor, so do
3954 * that here. */
3955 setcursor();
3956 out_flush();
3957#ifdef FEAT_GUI
3958 if (gui.in_use)
3959 gui_update_cursor(FALSE, FALSE);
3960#endif
3961
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003962 return res;
3963}
3964
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003965/*
3966 * If conversion is needed, convert "data" from "client_enc" to 'encoding' and
3967 * return an allocated string. Otherwise return "data".
3968 * "*tofree" is set to the result when it needs to be freed later.
3969 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003970 char_u *
3971serverConvert(client_enc, data, tofree)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003972 char_u *client_enc UNUSED;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003973 char_u *data;
3974 char_u **tofree;
3975{
3976 char_u *res = data;
3977
3978 *tofree = NULL;
3979# ifdef FEAT_MBYTE
3980 if (client_enc != NULL && p_enc != NULL)
3981 {
3982 vimconv_T vimconv;
3983
3984 vimconv.vc_type = CONV_NONE;
3985 if (convert_setup(&vimconv, client_enc, p_enc) != FAIL
3986 && vimconv.vc_type != CONV_NONE)
3987 {
3988 res = string_convert(&vimconv, data, NULL);
3989 if (res == NULL)
3990 res = data;
3991 else
3992 *tofree = res;
3993 }
3994 convert_setup(&vimconv, NULL, NULL);
3995 }
3996# endif
3997 return res;
3998}
3999
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004000
4001/*
4002 * Make our basic server name: use the specified "arg" if given, otherwise use
4003 * the tail of the command "cmd" we were started with.
4004 * Return the name in allocated memory. This doesn't include a serial number.
4005 */
4006 static char_u *
4007serverMakeName(arg, cmd)
4008 char_u *arg;
4009 char *cmd;
4010{
4011 char_u *p;
4012
4013 if (arg != NULL && *arg != NUL)
4014 p = vim_strsave_up(arg);
4015 else
4016 {
4017 p = vim_strsave_up(gettail((char_u *)cmd));
4018 /* Remove .exe or .bat from the name. */
4019 if (p != NULL && vim_strchr(p, '.') != NULL)
4020 *vim_strchr(p, '.') = NUL;
4021 }
4022 return p;
4023}
4024#endif /* FEAT_CLIENTSERVER */
4025
4026/*
4027 * When FEAT_FKMAP is defined, also compile the Farsi source code.
4028 */
4029#if defined(FEAT_FKMAP) || defined(PROTO)
4030# include "farsi.c"
4031#endif
4032
4033/*
4034 * When FEAT_ARABIC is defined, also compile the Arabic source code.
4035 */
4036#if defined(FEAT_ARABIC) || defined(PROTO)
4037# include "arabic.c"
4038#endif