blob: c79d2b8c870583becc87c86fa1ec3d5c2c1199f7 [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 Moolenaarc2b4c622011-02-15 16:29:59 +01001201 if (conceal_old_cursor_line != conceal_new_cursor_line
1202 && conceal_old_cursor_line
1203 <= curbuf->b_ml.ml_line_count)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001204 update_single_line(curwin, conceal_old_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001205 update_single_line(curwin, conceal_new_cursor_line);
1206 curwin->w_valid &= ~VALID_CROW;
1207 }
1208# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001209 setcursor();
1210 cursor_on();
1211
1212 do_redraw = FALSE;
Bram Moolenaar3f269672009-11-03 11:11:11 +00001213
1214#ifdef STARTUPTIME
1215 /* Now that we have drawn the first screen all the startup stuff
1216 * has been done, close any file for startup messages. */
1217 if (time_fd != NULL)
1218 {
1219 TIME_MSG("first screen update");
1220 TIME_MSG("--- VIM STARTED ---");
1221 fclose(time_fd);
1222 time_fd = NULL;
1223 }
1224#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001225 }
1226#ifdef FEAT_GUI
1227 if (need_mouse_correct)
1228 gui_mouse_correct();
1229#endif
1230
1231 /*
1232 * Update w_curswant if w_set_curswant has been set.
1233 * Postponed until here to avoid computing w_virtcol too often.
1234 */
1235 update_curswant();
1236
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001237#ifdef FEAT_EVAL
1238 /*
1239 * May perform garbage collection when waiting for a character, but
1240 * only at the very toplevel. Otherwise we may be using a List or
1241 * Dict internally somewhere.
1242 * "may_garbage_collect" is reset in vgetc() which is invoked through
1243 * do_exmode() and normal_cmd().
1244 */
1245 may_garbage_collect = (!cmdwin && !noexmode);
1246#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001247 /*
1248 * If we're invoked as ex, do a round of ex commands.
1249 * Otherwise, get and execute a normal mode command.
1250 */
1251 if (exmode_active)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001252 {
1253 if (noexmode) /* End of ":global/path/visual" commands */
1254 return;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001255 do_exmode(exmode_active == EXMODE_VIM);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001256 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001257 else
1258 normal_cmd(&oa, TRUE);
1259 }
1260}
1261
1262
1263#if defined(USE_XSMP) || defined(FEAT_GUI_MSWIN) || defined(PROTO)
1264/*
1265 * Exit, but leave behind swap files for modified buffers.
1266 */
1267 void
1268getout_preserve_modified(exitval)
1269 int exitval;
1270{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001271# if defined(SIGHUP) && defined(SIG_IGN)
1272 /* Ignore SIGHUP, because a dropped connection causes a read error, which
1273 * makes Vim exit and then handling SIGHUP causes various reentrance
1274 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001275 signal(SIGHUP, SIG_IGN);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001276# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001277
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001278 ml_close_notmod(); /* close all not-modified buffers */
1279 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
1280 ml_close_all(FALSE); /* close all memfiles, without deleting */
1281 getout(exitval); /* exit Vim properly */
1282}
1283#endif
1284
1285
1286/* Exit properly */
1287 void
1288getout(exitval)
1289 int exitval;
1290{
1291#ifdef FEAT_AUTOCMD
1292 buf_T *buf;
1293 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001294 tabpage_T *tp, *next_tp;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001295#endif
1296
1297 exiting = TRUE;
1298
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001299 /* When running in Ex mode an error causes us to exit with a non-zero exit
1300 * code. POSIX requires this, although it's not 100% clear from the
1301 * standard. */
1302 if (exmode_active)
1303 exitval += ex_exitval;
1304
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001305 /* Position the cursor on the last screen line, below all the text */
1306#ifdef FEAT_GUI
1307 if (!gui.in_use)
1308#endif
1309 windgoto((int)Rows - 1, 0);
1310
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +00001311#if defined(FEAT_EVAL) || defined(FEAT_SYN_HL)
1312 /* Optionally print hashtable efficiency. */
1313 hash_debug_results();
1314#endif
1315
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001316#ifdef FEAT_GUI
1317 msg_didany = FALSE;
1318#endif
1319
1320#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001321 if (get_vim_var_nr(VV_DYING) <= 1)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001322 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001323 /* Trigger BufWinLeave for all windows, but only once per buffer. */
1324# if defined FEAT_WINDOWS
1325 for (tp = first_tabpage; tp != NULL; tp = next_tp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001326 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001327 next_tp = tp->tp_next;
1328 for (wp = (tp == curtab)
1329 ? firstwin : tp->tp_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001330 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001331 buf = wp->w_buffer;
1332 if (buf->b_changedtick != -1)
1333 {
1334 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname,
1335 buf->b_fname, FALSE, buf);
1336 buf->b_changedtick = -1; /* note that we did it already */
1337 /* start all over, autocommands may mess up the lists */
1338 next_tp = first_tabpage;
1339 break;
1340 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001341 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001342 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001343# else
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001344 apply_autocmds(EVENT_BUFWINLEAVE, curbuf, curbuf->b_fname,
1345 FALSE, curbuf);
Bram Moolenaarf740b292006-02-16 22:11:02 +00001346# endif
Bram Moolenaar33aec762006-01-22 23:30:12 +00001347
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001348 /* Trigger BufUnload for buffers that are loaded */
1349 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1350 if (buf->b_ml.ml_mfp != NULL)
1351 {
1352 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001353 FALSE, buf);
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001354 if (!buf_valid(buf)) /* autocmd may delete the buffer */
1355 break;
1356 }
1357 apply_autocmds(EVENT_VIMLEAVEPRE, NULL, NULL, FALSE, curbuf);
1358 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001359#endif
1360
1361#ifdef FEAT_VIMINFO
1362 if (*p_viminfo != NUL)
1363 /* Write out the registers, history, marks etc, to the viminfo file */
1364 write_viminfo(NULL, FALSE);
1365#endif
1366
1367#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001368 if (get_vim_var_nr(VV_DYING) <= 1)
1369 apply_autocmds(EVENT_VIMLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001370#endif
1371
Bram Moolenaar05159a02005-02-26 23:04:13 +00001372#ifdef FEAT_PROFILE
1373 profile_dump();
1374#endif
1375
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001376 if (did_emsg
1377#ifdef FEAT_GUI
1378 || (gui.in_use && msg_didany && p_verbose > 0)
1379#endif
1380 )
1381 {
1382 /* give the user a chance to read the (error) message */
1383 no_wait_return = FALSE;
1384 wait_return(FALSE);
1385 }
1386
1387#ifdef FEAT_AUTOCMD
1388 /* Position the cursor again, the autocommands may have moved it */
1389# ifdef FEAT_GUI
1390 if (!gui.in_use)
1391# endif
1392 windgoto((int)Rows - 1, 0);
1393#endif
1394
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001395#ifdef FEAT_LUA
1396 lua_end();
1397#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001398#ifdef FEAT_MZSCHEME
1399 mzscheme_end();
1400#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001401#ifdef FEAT_TCL
1402 tcl_end();
1403#endif
1404#ifdef FEAT_RUBY
1405 ruby_end();
1406#endif
1407#ifdef FEAT_PYTHON
1408 python_end();
1409#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001410#ifdef FEAT_PYTHON3
1411 python3_end();
1412#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001413#ifdef FEAT_PERL
1414 perl_end();
1415#endif
1416#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
1417 iconv_end();
1418#endif
1419#ifdef FEAT_NETBEANS_INTG
1420 netbeans_end();
1421#endif
Bram Moolenaar02b06312007-09-06 15:39:22 +00001422#ifdef FEAT_CSCOPE
1423 cs_end();
1424#endif
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001425#ifdef FEAT_EVAL
1426 if (garbage_collect_at_exit)
1427 garbage_collect();
1428#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001429
1430 mch_exit(exitval);
1431}
1432
1433/*
1434 * Get a (optional) count for a Vim argument.
1435 */
1436 static int
1437get_number_arg(p, idx, def)
1438 char_u *p; /* pointer to argument */
1439 int *idx; /* index in argument, is incremented */
1440 int def; /* default value */
1441{
1442 if (vim_isdigit(p[*idx]))
1443 {
1444 def = atoi((char *)&(p[*idx]));
1445 while (vim_isdigit(p[*idx]))
1446 *idx = *idx + 1;
1447 }
1448 return def;
1449}
1450
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001451#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1452/*
1453 * Setup to use the current locale (for ctype() and many other things).
1454 */
1455 static void
1456init_locale()
1457{
1458 setlocale(LC_ALL, "");
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001459
Bram Moolenaarc6af8122010-05-21 12:04:55 +02001460# ifdef FEAT_GUI_GTK
1461 /* Tell Gtk not to change our locale settings. */
1462 gtk_disable_setlocale();
1463# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001464# if defined(FEAT_FLOAT) && defined(LC_NUMERIC)
1465 /* Make sure strtod() uses a decimal point, not a comma. */
1466 setlocale(LC_NUMERIC, "C");
1467# endif
1468
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001469# ifdef WIN32
1470 /* Apparently MS-Windows printf() may cause a crash when we give it 8-bit
1471 * text while it's expecting text in the current locale. This call avoids
1472 * that. */
1473 setlocale(LC_CTYPE, "C");
1474# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001475
1476# ifdef FEAT_GETTEXT
1477 {
1478 int mustfree = FALSE;
1479 char_u *p;
1480
1481# ifdef DYNAMIC_GETTEXT
1482 /* Initialize the gettext library */
1483 dyn_libintl_init(NULL);
1484# endif
1485 /* expand_env() doesn't work yet, because chartab[] is not initialized
1486 * yet, call vim_getenv() directly */
1487 p = vim_getenv((char_u *)"VIMRUNTIME", &mustfree);
1488 if (p != NULL && *p != NUL)
1489 {
Bram Moolenaar1ad2f132007-06-19 18:27:18 +00001490 vim_snprintf((char *)NameBuff, MAXPATHL, "%s/lang", p);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001491 bindtextdomain(VIMPACKAGE, (char *)NameBuff);
1492 }
1493 if (mustfree)
1494 vim_free(p);
1495 textdomain(VIMPACKAGE);
1496 }
1497# endif
1498}
1499#endif
1500
1501/*
1502 * Check for: [r][e][g][vi|vim|view][diff][ex[im]]
1503 * If the executable name starts with "r" we disable shell commands.
1504 * If the next character is "e" we run in Easy mode.
1505 * If the next character is "g" we run the GUI version.
1506 * If the next characters are "view" we start in readonly mode.
1507 * If the next characters are "diff" or "vimdiff" we start in diff mode.
1508 * If the next characters are "ex" we start in Ex mode. If it's followed
1509 * by "im" use improved Ex mode.
1510 */
1511 static void
1512parse_command_name(parmp)
1513 mparm_T *parmp;
1514{
1515 char_u *initstr;
1516
1517 initstr = gettail((char_u *)parmp->argv[0]);
1518
1519#ifdef MACOS_X_UNIX
1520 /* An issue has been seen when launching Vim in such a way that
1521 * $PWD/$ARGV[0] or $ARGV[0] is not the absolute path to the
1522 * executable or a symbolic link of it. Until this issue is resolved
1523 * we prohibit the GUI from being used.
1524 */
1525 if (STRCMP(initstr, parmp->argv[0]) == 0)
1526 disallow_gui = TRUE;
1527
1528 /* TODO: On MacOS X default to gui if argv[0] ends in:
Bram Moolenaar27dc1952006-03-15 23:06:44 +00001529 * /Vim.app/Contents/MacOS/Vim */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001530#endif
1531
1532#ifdef FEAT_EVAL
1533 set_vim_var_string(VV_PROGNAME, initstr, -1);
1534#endif
1535
1536 if (TOLOWER_ASC(initstr[0]) == 'r')
1537 {
1538 restricted = TRUE;
1539 ++initstr;
1540 }
1541
Bram Moolenaarad249fb2010-05-07 16:35:04 +02001542 /* Use evim mode for "evim" and "egvim", not for "editor". */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001543 if (TOLOWER_ASC(initstr[0]) == 'e'
1544 && (TOLOWER_ASC(initstr[1]) == 'v'
1545 || TOLOWER_ASC(initstr[1]) == 'g'))
1546 {
1547#ifdef FEAT_GUI
1548 gui.starting = TRUE;
1549#endif
1550 parmp->evim_mode = TRUE;
1551 ++initstr;
1552 }
1553
Bram Moolenaar806875d2008-09-18 18:57:10 +00001554 /* "gvim" starts the GUI. Also accept "Gvim" for MS-Windows. */
1555 if (TOLOWER_ASC(initstr[0]) == 'g')
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001556 {
1557 main_start_gui();
1558#ifdef FEAT_GUI
1559 ++initstr;
1560#endif
1561 }
1562
1563 if (STRNICMP(initstr, "view", 4) == 0)
1564 {
1565 readonlymode = TRUE;
1566 curbuf->b_p_ro = TRUE;
1567 p_uc = 10000; /* don't update very often */
1568 initstr += 4;
1569 }
1570 else if (STRNICMP(initstr, "vim", 3) == 0)
1571 initstr += 3;
1572
1573 /* Catch "[r][g]vimdiff" and "[r][g]viewdiff". */
1574 if (STRICMP(initstr, "diff") == 0)
1575 {
1576#ifdef FEAT_DIFF
1577 parmp->diff_mode = TRUE;
1578#else
1579 mch_errmsg(_("This Vim was not compiled with the diff feature."));
1580 mch_errmsg("\n");
1581 mch_exit(2);
1582#endif
1583 }
1584
1585 if (STRNICMP(initstr, "ex", 2) == 0)
1586 {
1587 if (STRNICMP(initstr + 2, "im", 2) == 0)
1588 exmode_active = EXMODE_VIM;
1589 else
1590 exmode_active = EXMODE_NORMAL;
1591 change_compatible(TRUE); /* set 'compatible' */
1592 }
1593}
1594
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001595/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00001596 * Get the name of the display, before gui_prepare() removes it from
1597 * argv[]. Used for the xterm-clipboard display.
1598 *
Bram Moolenaar78e17622007-08-30 10:26:19 +00001599 * Also find the --server... arguments and --socketid and --windowid
Bram Moolenaar58d98232005-07-23 22:25:46 +00001600 */
Bram Moolenaar58d98232005-07-23 22:25:46 +00001601 static void
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001602early_arg_scan(parmp)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00001603 mparm_T *parmp UNUSED;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001604{
Bram Moolenaar03005972008-11-20 13:12:36 +00001605#if defined(FEAT_XCLIPBOARD) || defined(FEAT_CLIENTSERVER) \
1606 || !defined(FEAT_NETBEANS_INTG)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001607 int argc = parmp->argc;
1608 char **argv = parmp->argv;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001609 int i;
1610
1611 for (i = 1; i < argc; i++)
1612 {
1613 if (STRCMP(argv[i], "--") == 0)
1614 break;
1615# ifdef FEAT_XCLIPBOARD
1616 else if (STRICMP(argv[i], "-display") == 0
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001617# if defined(FEAT_GUI_GTK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001618 || STRICMP(argv[i], "--display") == 0
1619# endif
1620 )
1621 {
1622 if (i == argc - 1)
1623 mainerr_arg_missing((char_u *)argv[i]);
1624 xterm_display = argv[++i];
1625 }
1626# endif
1627# ifdef FEAT_CLIENTSERVER
1628 else if (STRICMP(argv[i], "--servername") == 0)
1629 {
1630 if (i == argc - 1)
1631 mainerr_arg_missing((char_u *)argv[i]);
1632 parmp->serverName_arg = (char_u *)argv[++i];
1633 }
Bram Moolenaareb94e552006-03-11 21:35:11 +00001634 else if (STRICMP(argv[i], "--serverlist") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001635 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001636 else if (STRNICMP(argv[i], "--remote", 8) == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001637 {
1638 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001639# ifdef FEAT_GUI
1640 if (strstr(argv[i], "-wait") != 0)
1641 /* don't fork() when starting the GUI to edit files ourself */
1642 gui.dofork = FALSE;
1643# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001644 }
1645# endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001646
1647# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1648# ifdef FEAT_GUI_W32
1649 else if (STRICMP(argv[i], "--windowid") == 0)
1650# else
Bram Moolenaar58d98232005-07-23 22:25:46 +00001651 else if (STRICMP(argv[i], "--socketid") == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001652# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001653 {
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001654 long_u id;
1655 int count;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001656
1657 if (i == argc - 1)
1658 mainerr_arg_missing((char_u *)argv[i]);
1659 if (STRNICMP(argv[i+1], "0x", 2) == 0)
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001660 count = sscanf(&(argv[i + 1][2]), SCANF_HEX_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001661 else
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001662 count = sscanf(argv[i + 1], SCANF_DECIMAL_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001663 if (count != 1)
1664 mainerr(ME_INVALID_ARG, (char_u *)argv[i]);
1665 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00001666# ifdef FEAT_GUI_W32
1667 win_socket_id = id;
1668# else
1669 gtk_socket_id = id;
1670# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001671 i++;
1672 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001673# endif
1674# ifdef FEAT_GUI_GTK
Bram Moolenaar58d98232005-07-23 22:25:46 +00001675 else if (STRICMP(argv[i], "--echo-wid") == 0)
1676 echo_wid_arg = TRUE;
1677# endif
Bram Moolenaar03005972008-11-20 13:12:36 +00001678# ifndef FEAT_NETBEANS_INTG
1679 else if (strncmp(argv[i], "-nb", (size_t)3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +02001680 {
1681 mch_errmsg(_("'-nb' cannot be used: not enabled at compile time\n"));
1682 mch_exit(2);
1683 }
Bram Moolenaar03005972008-11-20 13:12:36 +00001684# endif
1685
Bram Moolenaar58d98232005-07-23 22:25:46 +00001686 }
1687#endif
1688}
1689
1690/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001691 * Scan the command line arguments.
1692 */
1693 static void
1694command_line_scan(parmp)
1695 mparm_T *parmp;
1696{
1697 int argc = parmp->argc;
1698 char **argv = parmp->argv;
1699 int argv_idx; /* index in argv[n][] */
1700 int had_minmin = FALSE; /* found "--" argument */
1701 int want_argument; /* option argument with argument */
1702 int c;
Bram Moolenaar231334e2005-07-25 20:46:57 +00001703 char_u *p = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001704 long n;
1705
1706 --argc;
1707 ++argv;
1708 argv_idx = 1; /* active option letter is argv[0][argv_idx] */
1709 while (argc > 0)
1710 {
1711 /*
1712 * "+" or "+{number}" or "+/{pat}" or "+{command}" argument.
1713 */
1714 if (argv[0][0] == '+' && !had_minmin)
1715 {
1716 if (parmp->n_commands >= MAX_ARG_CMDS)
1717 mainerr(ME_EXTRA_CMD, NULL);
1718 argv_idx = -1; /* skip to next argument */
1719 if (argv[0][1] == NUL)
1720 parmp->commands[parmp->n_commands++] = (char_u *)"$";
1721 else
1722 parmp->commands[parmp->n_commands++] = (char_u *)&(argv[0][1]);
1723 }
1724
1725 /*
1726 * Optional argument.
1727 */
1728 else if (argv[0][0] == '-' && !had_minmin)
1729 {
1730 want_argument = FALSE;
1731 c = argv[0][argv_idx++];
1732#ifdef VMS
1733 /*
1734 * VMS only uses upper case command lines. Interpret "-X" as "-x"
1735 * and "-/X" as "-X".
1736 */
1737 if (c == '/')
1738 {
1739 c = argv[0][argv_idx++];
1740 c = TOUPPER_ASC(c);
1741 }
1742 else
1743 c = TOLOWER_ASC(c);
1744#endif
1745 switch (c)
1746 {
1747 case NUL: /* "vim -" read from stdin */
1748 /* "ex -" silent mode */
1749 if (exmode_active)
1750 silent_mode = TRUE;
1751 else
1752 {
1753 if (parmp->edit_type != EDIT_NONE)
1754 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1755 parmp->edit_type = EDIT_STDIN;
1756 read_cmd_fd = 2; /* read from stderr instead of stdin */
1757 }
1758 argv_idx = -1; /* skip to next argument */
1759 break;
1760
1761 case '-': /* "--" don't take any more option arguments */
1762 /* "--help" give help message */
1763 /* "--version" give version message */
1764 /* "--literal" take files literally */
1765 /* "--nofork" don't fork */
1766 /* "--noplugin[s]" skip plugins */
1767 /* "--cmd <cmd>" execute cmd before vimrc */
1768 if (STRICMP(argv[0] + argv_idx, "help") == 0)
1769 usage();
1770 else if (STRICMP(argv[0] + argv_idx, "version") == 0)
1771 {
1772 Columns = 80; /* need to init Columns */
1773 info_message = TRUE; /* use mch_msg(), not mch_errmsg() */
1774 list_version();
1775 msg_putchar('\n');
1776 msg_didout = FALSE;
1777 mch_exit(0);
1778 }
1779 else if (STRNICMP(argv[0] + argv_idx, "literal", 7) == 0)
1780 {
1781#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
1782 parmp->literal = TRUE;
1783#endif
1784 }
1785 else if (STRNICMP(argv[0] + argv_idx, "nofork", 6) == 0)
1786 {
1787#ifdef FEAT_GUI
1788 gui.dofork = FALSE; /* don't fork() when starting GUI */
1789#endif
1790 }
1791 else if (STRNICMP(argv[0] + argv_idx, "noplugin", 8) == 0)
1792 p_lpl = FALSE;
1793 else if (STRNICMP(argv[0] + argv_idx, "cmd", 3) == 0)
1794 {
1795 want_argument = TRUE;
1796 argv_idx += 3;
1797 }
Bram Moolenaaref94eec2009-11-11 13:22:11 +00001798 else if (STRNICMP(argv[0] + argv_idx, "startuptime", 11) == 0)
1799 {
1800 want_argument = TRUE;
1801 argv_idx += 11;
1802 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001803#ifdef FEAT_CLIENTSERVER
1804 else if (STRNICMP(argv[0] + argv_idx, "serverlist", 10) == 0)
1805 ; /* already processed -- no arg */
1806 else if (STRNICMP(argv[0] + argv_idx, "servername", 10) == 0
1807 || STRNICMP(argv[0] + argv_idx, "serversend", 10) == 0)
1808 {
1809 /* already processed -- snatch the following arg */
1810 if (argc > 1)
1811 {
1812 --argc;
1813 ++argv;
1814 }
1815 }
1816#endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001817#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1818# ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001819 else if (STRNICMP(argv[0] + argv_idx, "socketid", 8) == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001820# else
1821 else if (STRNICMP(argv[0] + argv_idx, "windowid", 8) == 0)
1822# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001823 {
1824 /* already processed -- snatch the following arg */
1825 if (argc > 1)
1826 {
1827 --argc;
1828 ++argv;
1829 }
1830 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001831#endif
1832#ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001833 else if (STRNICMP(argv[0] + argv_idx, "echo-wid", 8) == 0)
1834 {
1835 /* already processed, skip */
1836 }
1837#endif
1838 else
1839 {
1840 if (argv[0][argv_idx])
1841 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
1842 had_minmin = TRUE;
1843 }
1844 if (!want_argument)
1845 argv_idx = -1; /* skip to next argument */
1846 break;
1847
1848 case 'A': /* "-A" start in Arabic mode */
1849#ifdef FEAT_ARABIC
1850 set_option_value((char_u *)"arabic", 1L, NULL, 0);
1851#else
1852 mch_errmsg(_(e_noarabic));
1853 mch_exit(2);
1854#endif
1855 break;
1856
1857 case 'b': /* "-b" binary mode */
Bram Moolenaar231334e2005-07-25 20:46:57 +00001858 /* Needs to be effective before expanding file names, because
1859 * for Win32 this makes us edit a shortcut file itself,
1860 * instead of the file it links to. */
1861 set_options_bin(curbuf->b_p_bin, 1, 0);
1862 curbuf->b_p_bin = 1; /* binary file I/O */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001863 break;
1864
1865 case 'C': /* "-C" Compatible */
1866 change_compatible(TRUE);
1867 break;
1868
1869 case 'e': /* "-e" Ex mode */
1870 exmode_active = EXMODE_NORMAL;
1871 break;
1872
1873 case 'E': /* "-E" Improved Ex mode */
1874 exmode_active = EXMODE_VIM;
1875 break;
1876
1877 case 'f': /* "-f" GUI: run in foreground. Amiga: open
1878 window directly, not with newcli */
1879#ifdef FEAT_GUI
1880 gui.dofork = FALSE; /* don't fork() when starting GUI */
1881#endif
1882 break;
1883
1884 case 'g': /* "-g" start GUI */
1885 main_start_gui();
1886 break;
1887
1888 case 'F': /* "-F" start in Farsi mode: rl + fkmap set */
1889#ifdef FEAT_FKMAP
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00001890 p_fkmap = TRUE;
1891 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001892#else
1893 mch_errmsg(_(e_nofarsi));
1894 mch_exit(2);
1895#endif
1896 break;
1897
1898 case 'h': /* "-h" give help message */
1899#ifdef FEAT_GUI_GNOME
1900 /* Tell usage() to exit for "gvim". */
1901 gui.starting = FALSE;
1902#endif
1903 usage();
1904 break;
1905
1906 case 'H': /* "-H" start in Hebrew mode: rl + hkmap set */
1907#ifdef FEAT_RIGHTLEFT
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00001908 p_hkmap = TRUE;
1909 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001910#else
1911 mch_errmsg(_(e_nohebrew));
1912 mch_exit(2);
1913#endif
1914 break;
1915
1916 case 'l': /* "-l" lisp mode, 'lisp' and 'showmatch' on */
1917#ifdef FEAT_LISP
1918 set_option_value((char_u *)"lisp", 1L, NULL, 0);
1919 p_sm = TRUE;
1920#endif
1921 break;
1922
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001923 case 'M': /* "-M" no changes or writing of files */
1924 reset_modifiable();
1925 /* FALLTHROUGH */
1926
1927 case 'm': /* "-m" no writing of files */
1928 p_write = FALSE;
1929 break;
1930
1931 case 'y': /* "-y" easy mode */
1932#ifdef FEAT_GUI
1933 gui.starting = TRUE; /* start GUI a bit later */
1934#endif
1935 parmp->evim_mode = TRUE;
1936 break;
1937
1938 case 'N': /* "-N" Nocompatible */
1939 change_compatible(FALSE);
1940 break;
1941
1942 case 'n': /* "-n" no swap file */
Bram Moolenaar67c53842010-05-22 18:28:27 +02001943#ifdef FEAT_NETBEANS_INTG
1944 /* checking for "-nb", netbeans parameters */
1945 if (argv[0][argv_idx] == 'b')
1946 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02001947 netbeansArg = argv[0];
1948 argv_idx = -1; /* skip to next argument */
1949 }
1950 else
1951#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001952 parmp->no_swap_file = TRUE;
1953 break;
1954
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001955 case 'p': /* "-p[N]" open N tab pages */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00001956#ifdef TARGET_API_MAC_OSX
1957 /* For some reason on MacOS X, an argument like:
1958 -psn_0_10223617 is passed in when invoke from Finder
1959 or with the 'open' command */
1960 if (argv[0][argv_idx] == 's')
1961 {
1962 argv_idx = -1; /* bypass full -psn */
1963 main_start_gui();
1964 break;
1965 }
1966#endif
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001967#ifdef FEAT_WINDOWS
1968 /* default is 0: open window for each file */
1969 parmp->window_count = get_number_arg((char_u *)argv[0],
1970 &argv_idx, 0);
1971 parmp->window_layout = WIN_TABS;
1972#endif
1973 break;
1974
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001975 case 'o': /* "-o[N]" open N horizontal split windows */
1976#ifdef FEAT_WINDOWS
1977 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00001978 parmp->window_count = get_number_arg((char_u *)argv[0],
1979 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001980 parmp->window_layout = WIN_HOR;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001981#endif
1982 break;
1983
1984 case 'O': /* "-O[N]" open N vertical split windows */
1985#if defined(FEAT_VERTSPLIT) && defined(FEAT_WINDOWS)
1986 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00001987 parmp->window_count = get_number_arg((char_u *)argv[0],
1988 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001989 parmp->window_layout = WIN_VER;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001990#endif
1991 break;
1992
1993#ifdef FEAT_QUICKFIX
1994 case 'q': /* "-q" QuickFix mode */
1995 if (parmp->edit_type != EDIT_NONE)
1996 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1997 parmp->edit_type = EDIT_QF;
1998 if (argv[0][argv_idx]) /* "-q{errorfile}" */
1999 {
2000 parmp->use_ef = (char_u *)argv[0] + argv_idx;
2001 argv_idx = -1;
2002 }
2003 else if (argc > 1) /* "-q {errorfile}" */
2004 want_argument = TRUE;
2005 break;
2006#endif
2007
2008 case 'R': /* "-R" readonly mode */
2009 readonlymode = TRUE;
2010 curbuf->b_p_ro = TRUE;
2011 p_uc = 10000; /* don't update very often */
2012 break;
2013
2014 case 'r': /* "-r" recovery mode */
2015 case 'L': /* "-L" recovery mode */
2016 recoverymode = 1;
2017 break;
2018
2019 case 's':
2020 if (exmode_active) /* "-s" silent (batch) mode */
2021 silent_mode = TRUE;
2022 else /* "-s {scriptin}" read from script file */
2023 want_argument = TRUE;
2024 break;
2025
2026 case 't': /* "-t {tag}" or "-t{tag}" jump to tag */
2027 if (parmp->edit_type != EDIT_NONE)
2028 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2029 parmp->edit_type = EDIT_TAG;
2030 if (argv[0][argv_idx]) /* "-t{tag}" */
2031 {
2032 parmp->tagname = (char_u *)argv[0] + argv_idx;
2033 argv_idx = -1;
2034 }
2035 else /* "-t {tag}" */
2036 want_argument = TRUE;
2037 break;
2038
2039#ifdef FEAT_EVAL
2040 case 'D': /* "-D" Debugging */
2041 parmp->use_debug_break_level = 9999;
2042 break;
2043#endif
2044#ifdef FEAT_DIFF
2045 case 'd': /* "-d" 'diff' */
2046# ifdef AMIGA
2047 /* check for "-dev {device}" */
2048 if (argv[0][argv_idx] == 'e' && argv[0][argv_idx + 1] == 'v')
2049 want_argument = TRUE;
2050 else
2051# endif
2052 parmp->diff_mode = TRUE;
2053 break;
2054#endif
2055 case 'V': /* "-V{N}" Verbose level */
2056 /* default is 10: a little bit verbose */
2057 p_verbose = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2058 if (argv[0][argv_idx] != NUL)
2059 {
2060 set_option_value((char_u *)"verbosefile", 0L,
2061 (char_u *)argv[0] + argv_idx, 0);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002062 argv_idx = (int)STRLEN(argv[0]);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002063 }
2064 break;
2065
2066 case 'v': /* "-v" Vi-mode (as if called "vi") */
2067 exmode_active = 0;
2068#ifdef FEAT_GUI
2069 gui.starting = FALSE; /* don't start GUI */
2070#endif
2071 break;
2072
2073 case 'w': /* "-w{number}" set window height */
2074 /* "-w {scriptout}" write to script */
2075 if (vim_isdigit(((char_u *)argv[0])[argv_idx]))
2076 {
2077 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2078 set_option_value((char_u *)"window", n, NULL, 0);
2079 break;
2080 }
2081 want_argument = TRUE;
2082 break;
2083
2084#ifdef FEAT_CRYPT
2085 case 'x': /* "-x" encrypted reading/writing of files */
2086 parmp->ask_for_key = TRUE;
2087 break;
2088#endif
2089
2090 case 'X': /* "-X" don't connect to X server */
2091#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
2092 x_no_connect = TRUE;
2093#endif
2094 break;
2095
2096 case 'Z': /* "-Z" restricted mode */
2097 restricted = TRUE;
2098 break;
2099
2100 case 'c': /* "-c{command}" or "-c {command}" execute
2101 command */
2102 if (argv[0][argv_idx] != NUL)
2103 {
2104 if (parmp->n_commands >= MAX_ARG_CMDS)
2105 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar231334e2005-07-25 20:46:57 +00002106 parmp->commands[parmp->n_commands++] = (char_u *)argv[0]
2107 + argv_idx;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002108 argv_idx = -1;
2109 break;
2110 }
2111 /*FALLTHROUGH*/
2112 case 'S': /* "-S {file}" execute Vim script */
2113 case 'i': /* "-i {viminfo}" use for viminfo */
2114#ifndef FEAT_DIFF
2115 case 'd': /* "-d {device}" device (for Amiga) */
2116#endif
2117 case 'T': /* "-T {terminal}" terminal name */
2118 case 'u': /* "-u {vimrc}" vim inits file */
2119 case 'U': /* "-U {gvimrc}" gvim inits file */
2120 case 'W': /* "-W {scriptout}" overwrite */
2121#ifdef FEAT_GUI_W32
2122 case 'P': /* "-P {parent title}" MDI parent */
2123#endif
2124 want_argument = TRUE;
2125 break;
2126
2127 default:
2128 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
2129 }
2130
2131 /*
2132 * Handle option arguments with argument.
2133 */
2134 if (want_argument)
2135 {
2136 /*
2137 * Check for garbage immediately after the option letter.
2138 */
2139 if (argv[0][argv_idx] != NUL)
2140 mainerr(ME_GARBAGE, (char_u *)argv[0]);
2141
2142 --argc;
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002143 if (argc < 1 && c != 'S') /* -S has an optional argument */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002144 mainerr_arg_missing((char_u *)argv[0]);
2145 ++argv;
2146 argv_idx = -1;
2147
2148 switch (c)
2149 {
2150 case 'c': /* "-c {command}" execute command */
2151 case 'S': /* "-S {file}" execute Vim script */
2152 if (parmp->n_commands >= MAX_ARG_CMDS)
2153 mainerr(ME_EXTRA_CMD, NULL);
2154 if (c == 'S')
2155 {
2156 char *a;
2157
2158 if (argc < 1)
2159 /* "-S" without argument: use default session file
2160 * name. */
2161 a = SESSION_FILE;
2162 else if (argv[0][0] == '-')
2163 {
2164 /* "-S" followed by another option: use default
2165 * session file name. */
2166 a = SESSION_FILE;
2167 ++argc;
2168 --argv;
2169 }
2170 else
2171 a = argv[0];
2172 p = alloc((unsigned)(STRLEN(a) + 4));
2173 if (p == NULL)
2174 mch_exit(2);
2175 sprintf((char *)p, "so %s", a);
2176 parmp->cmds_tofree[parmp->n_commands] = TRUE;
2177 parmp->commands[parmp->n_commands++] = p;
2178 }
2179 else
Bram Moolenaar231334e2005-07-25 20:46:57 +00002180 parmp->commands[parmp->n_commands++] =
2181 (char_u *)argv[0];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002182 break;
2183
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002184 case '-':
2185 if (argv[-1][2] == 'c')
2186 {
2187 /* "--cmd {command}" execute command */
2188 if (parmp->n_pre_commands >= MAX_ARG_CMDS)
2189 mainerr(ME_EXTRA_CMD, NULL);
2190 parmp->pre_commands[parmp->n_pre_commands++] =
Bram Moolenaar231334e2005-07-25 20:46:57 +00002191 (char_u *)argv[0];
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002192 }
2193 /* "--startuptime <file>" already handled */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002194 break;
2195
2196 /* case 'd': -d {device} is handled in mch_check_win() for the
2197 * Amiga */
2198
2199#ifdef FEAT_QUICKFIX
2200 case 'q': /* "-q {errorfile}" QuickFix mode */
2201 parmp->use_ef = (char_u *)argv[0];
2202 break;
2203#endif
2204
2205 case 'i': /* "-i {viminfo}" use for viminfo */
2206 use_viminfo = (char_u *)argv[0];
2207 break;
2208
2209 case 's': /* "-s {scriptin}" read from script file */
2210 if (scriptin[0] != NULL)
2211 {
2212scripterror:
2213 mch_errmsg(_("Attempt to open script file again: \""));
2214 mch_errmsg(argv[-1]);
2215 mch_errmsg(" ");
2216 mch_errmsg(argv[0]);
2217 mch_errmsg("\"\n");
2218 mch_exit(2);
2219 }
2220 if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL)
2221 {
2222 mch_errmsg(_("Cannot open for reading: \""));
2223 mch_errmsg(argv[0]);
2224 mch_errmsg("\"\n");
2225 mch_exit(2);
2226 }
2227 if (save_typebuf() == FAIL)
2228 mch_exit(2); /* out of memory */
2229 break;
2230
2231 case 't': /* "-t {tag}" */
2232 parmp->tagname = (char_u *)argv[0];
2233 break;
2234
2235 case 'T': /* "-T {terminal}" terminal name */
2236 /*
2237 * The -T term argument is always available and when
2238 * HAVE_TERMLIB is supported it overrides the environment
2239 * variable TERM.
2240 */
2241#ifdef FEAT_GUI
2242 if (term_is_gui((char_u *)argv[0]))
2243 gui.starting = TRUE; /* start GUI a bit later */
2244 else
2245#endif
2246 parmp->term = (char_u *)argv[0];
2247 break;
2248
2249 case 'u': /* "-u {vimrc}" vim inits file */
2250 parmp->use_vimrc = (char_u *)argv[0];
2251 break;
2252
2253 case 'U': /* "-U {gvimrc}" gvim inits file */
2254#ifdef FEAT_GUI
2255 use_gvimrc = (char_u *)argv[0];
2256#endif
2257 break;
2258
2259 case 'w': /* "-w {nr}" 'window' value */
2260 /* "-w {scriptout}" append to script file */
2261 if (vim_isdigit(*((char_u *)argv[0])))
2262 {
2263 argv_idx = 0;
2264 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2265 set_option_value((char_u *)"window", n, NULL, 0);
2266 argv_idx = -1;
2267 break;
2268 }
2269 /*FALLTHROUGH*/
2270 case 'W': /* "-W {scriptout}" overwrite script file */
2271 if (scriptout != NULL)
2272 goto scripterror;
2273 if ((scriptout = mch_fopen(argv[0],
2274 c == 'w' ? APPENDBIN : WRITEBIN)) == NULL)
2275 {
2276 mch_errmsg(_("Cannot open for script output: \""));
2277 mch_errmsg(argv[0]);
2278 mch_errmsg("\"\n");
2279 mch_exit(2);
2280 }
2281 break;
2282
2283#ifdef FEAT_GUI_W32
2284 case 'P': /* "-P {parent title}" MDI parent */
2285 gui_mch_set_parent(argv[0]);
2286 break;
2287#endif
2288 }
2289 }
2290 }
2291
2292 /*
2293 * File name argument.
2294 */
2295 else
2296 {
2297 argv_idx = -1; /* skip to next argument */
2298
2299 /* Check for only one type of editing. */
2300 if (parmp->edit_type != EDIT_NONE && parmp->edit_type != EDIT_FILE)
2301 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2302 parmp->edit_type = EDIT_FILE;
2303
2304#ifdef MSWIN
2305 /* Remember if the argument was a full path before changing
2306 * slashes to backslashes. */
2307 if (argv[0][0] != NUL && argv[0][1] == ':' && argv[0][2] == '\\')
2308 parmp->full_path = TRUE;
2309#endif
2310
2311 /* Add the file to the global argument list. */
2312 if (ga_grow(&global_alist.al_ga, 1) == FAIL
2313 || (p = vim_strsave((char_u *)argv[0])) == NULL)
2314 mch_exit(2);
2315#ifdef FEAT_DIFF
2316 if (parmp->diff_mode && mch_isdir(p) && GARGCOUNT > 0
2317 && !mch_isdir(alist_name(&GARGLIST[0])))
2318 {
2319 char_u *r;
2320
2321 r = concat_fnames(p, gettail(alist_name(&GARGLIST[0])), TRUE);
2322 if (r != NULL)
2323 {
2324 vim_free(p);
2325 p = r;
2326 }
2327 }
2328#endif
2329#if defined(__CYGWIN32__) && !defined(WIN32)
2330 /*
2331 * If vim is invoked by non-Cygwin tools, convert away any
2332 * DOS paths, so things like .swp files are created correctly.
2333 * Look for evidence of non-Cygwin paths before we bother.
2334 * This is only for when using the Unix files.
2335 */
Bram Moolenaarad249fb2010-05-07 16:35:04 +02002336 if (strpbrk(p, "\\:") != NULL && !path_with_url(p))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002337 {
2338 char posix_path[PATH_MAX];
2339
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002340# if CYGWIN_VERSION_DLL_MAJOR >= 1007
2341 cygwin_conv_path(CCP_WIN_A_TO_POSIX, p, posix_path, PATH_MAX);
2342# else
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002343 cygwin_conv_to_posix_path(p, posix_path);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002344# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002345 vim_free(p);
2346 p = vim_strsave(posix_path);
2347 if (p == NULL)
2348 mch_exit(2);
2349 }
2350#endif
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002351
2352#ifdef USE_FNAME_CASE
2353 /* Make the case of the file name match the actual file. */
2354 fname_case(p, 0);
2355#endif
2356
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002357 alist_add(&global_alist, p,
2358#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
Bram Moolenaar231334e2005-07-25 20:46:57 +00002359 parmp->literal ? 2 : 0 /* add buffer nr after exp. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002360#else
2361 2 /* add buffer number now and use curbuf */
2362#endif
2363 );
2364
2365#if defined(FEAT_MBYTE) && defined(WIN32)
2366 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002367 /* Remember this argument has been added to the argument list.
2368 * Needed when 'encoding' is changed. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002369 used_file_arg(argv[0], parmp->literal, parmp->full_path,
Bram Moolenaar688e5f72008-07-24 11:51:40 +00002370# ifdef FEAT_DIFF
2371 parmp->diff_mode
2372# else
2373 FALSE
2374# endif
2375 );
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002376 }
2377#endif
2378 }
2379
2380 /*
2381 * If there are no more letters after the current "-", go to next
2382 * argument. argv_idx is set to -1 when the current argument is to be
2383 * skipped.
2384 */
2385 if (argv_idx <= 0 || argv[0][argv_idx] == NUL)
2386 {
2387 --argc;
2388 ++argv;
2389 argv_idx = 1;
2390 }
2391 }
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002392
2393#ifdef FEAT_EVAL
2394 /* If there is a "+123" or "-c" command, set v:swapcommand to the first
2395 * one. */
2396 if (parmp->n_commands > 0)
2397 {
2398 p = alloc((unsigned)STRLEN(parmp->commands[0]) + 3);
2399 if (p != NULL)
2400 {
2401 sprintf((char *)p, ":%s\r", parmp->commands[0]);
2402 set_vim_var_string(VV_SWAPCOMMAND, p, -1);
2403 vim_free(p);
2404 }
2405 }
2406#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002407}
2408
2409/*
2410 * Print a warning if stdout is not a terminal.
2411 * When starting in Ex mode and commands come from a file, set Silent mode.
2412 */
2413 static void
2414check_tty(parmp)
2415 mparm_T *parmp;
2416{
2417 int input_isatty; /* is active input a terminal? */
2418
2419 input_isatty = mch_input_isatty();
2420 if (exmode_active)
2421 {
2422 if (!input_isatty)
2423 silent_mode = TRUE;
2424 }
2425 else if (parmp->want_full_screen && (!parmp->stdout_isatty || !input_isatty)
2426#ifdef FEAT_GUI
2427 /* don't want the delay when started from the desktop */
2428 && !gui.starting
2429#endif
2430 )
2431 {
2432#ifdef NBDEBUG
2433 /*
2434 * This shouldn't be necessary. But if I run netbeans with the log
2435 * output coming to the console and XOpenDisplay fails, I get vim
2436 * trying to start with input/output to my console tty. This fills my
2437 * input buffer so fast I can't even kill the process in under 2
Bram Moolenaar49325942007-05-10 19:19:59 +00002438 * minutes (and it beeps continuously the whole time :-)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002439 */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002440 if (netbeans_active() && (!parmp->stdout_isatty || !input_isatty))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002441 {
2442 mch_errmsg(_("Vim: Error: Failure to start gvim from NetBeans\n"));
2443 exit(1);
2444 }
2445#endif
2446 if (!parmp->stdout_isatty)
2447 mch_errmsg(_("Vim: Warning: Output is not to a terminal\n"));
2448 if (!input_isatty)
2449 mch_errmsg(_("Vim: Warning: Input is not from a terminal\n"));
2450 out_flush();
2451 if (scriptin[0] == NULL)
2452 ui_delay(2000L, TRUE);
2453 TIME_MSG("Warning delay");
2454 }
2455}
2456
2457/*
2458 * Read text from stdin.
2459 */
2460 static void
2461read_stdin()
2462{
2463 int i;
2464
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002465#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002466 /* When getting the ATTENTION prompt here, use a dialog */
2467 swap_exists_action = SEA_DIALOG;
2468#endif
2469 no_wait_return = TRUE;
2470 i = msg_didany;
2471 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002472 (void)open_buffer(TRUE, NULL, 0); /* create memfile and read file */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002473 no_wait_return = FALSE;
2474 msg_didany = i;
2475 TIME_MSG("reading stdin");
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002476#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002477 check_swap_exists_action();
2478#endif
2479#if !(defined(AMIGA) || defined(MACOS))
2480 /*
2481 * Close stdin and dup it from stderr. Required for GPM to work
2482 * properly, and for running external commands.
2483 * Is there any other system that cannot do this?
2484 */
2485 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00002486 ignored = dup(2);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002487#endif
2488}
2489
2490/*
2491 * Create the requested number of windows and edit buffers in them.
2492 * Also does recovery if "recoverymode" set.
2493 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002494 static void
2495create_windows(parmp)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002496 mparm_T *parmp UNUSED;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002497{
2498#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002499 int dorewind;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002500 int done = 0;
2501
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002502 /*
2503 * Create the number of windows that was requested.
2504 */
2505 if (parmp->window_count == -1) /* was not set */
2506 parmp->window_count = 1;
2507 if (parmp->window_count == 0)
2508 parmp->window_count = GARGCOUNT;
2509 if (parmp->window_count > 1)
2510 {
2511 /* Don't change the windows if there was a command in .vimrc that
2512 * already split some windows */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002513 if (parmp->window_layout == 0)
2514 parmp->window_layout = WIN_HOR;
2515 if (parmp->window_layout == WIN_TABS)
2516 {
2517 parmp->window_count = make_tabpages(parmp->window_count);
2518 TIME_MSG("making tab pages");
2519 }
2520 else if (firstwin->w_next == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002521 {
2522 parmp->window_count = make_windows(parmp->window_count,
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002523 parmp->window_layout == WIN_VER);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002524 TIME_MSG("making windows");
2525 }
2526 else
2527 parmp->window_count = win_count();
2528 }
2529 else
2530 parmp->window_count = 1;
2531#endif
2532
2533 if (recoverymode) /* do recover */
2534 {
2535 msg_scroll = TRUE; /* scroll message up */
2536 ml_recover();
2537 if (curbuf->b_ml.ml_mfp == NULL) /* failed */
2538 getout(1);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002539 do_modelines(0); /* do modelines */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002540 }
2541 else
2542 {
2543 /*
2544 * Open a buffer for windows that don't have one yet.
2545 * Commands in the .vimrc might have loaded a file or split the window.
2546 * Watch out for autocommands that delete a window.
2547 */
2548#ifdef FEAT_AUTOCMD
2549 /*
2550 * Don't execute Win/Buf Enter/Leave autocommands here
2551 */
2552 ++autocmd_no_enter;
2553 ++autocmd_no_leave;
2554#endif
2555#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002556 dorewind = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002557 while (done++ < 1000)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002558 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002559 if (dorewind)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002560 {
2561 if (parmp->window_layout == WIN_TABS)
2562 goto_tabpage(1);
2563 else
2564 curwin = firstwin;
2565 }
2566 else if (parmp->window_layout == WIN_TABS)
2567 {
2568 if (curtab->tp_next == NULL)
2569 break;
2570 goto_tabpage(0);
2571 }
2572 else
2573 {
2574 if (curwin->w_next == NULL)
2575 break;
2576 curwin = curwin->w_next;
2577 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002578 dorewind = FALSE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002579#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002580 curbuf = curwin->w_buffer;
2581 if (curbuf->b_ml.ml_mfp == NULL)
2582 {
2583#ifdef FEAT_FOLDING
2584 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2585 if (p_fdls >= 0)
2586 curwin->w_p_fdl = p_fdls;
2587#endif
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002588#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002589 /* When getting the ATTENTION prompt here, use a dialog */
2590 swap_exists_action = SEA_DIALOG;
2591#endif
2592 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002593
2594 /* create memfile, read file */
2595 (void)open_buffer(FALSE, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002596
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002597#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar84212822006-11-07 21:59:47 +00002598 if (swap_exists_action == SEA_QUIT)
2599 {
2600 if (got_int || only_one_window())
2601 {
2602 /* abort selected or quit and only one window */
2603 did_emsg = FALSE; /* avoid hit-enter prompt */
2604 getout(1);
2605 }
2606 /* We can't close the window, it would disturb what
2607 * happens next. Clear the file name and set the arg
2608 * index to -1 to delete it later. */
2609 setfname(curbuf, NULL, NULL, FALSE);
2610 curwin->w_arg_idx = -1;
2611 swap_exists_action = SEA_NONE;
2612 }
2613 else
2614 handle_swap_exists(NULL);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002615#endif
2616#ifdef FEAT_AUTOCMD
Bram Moolenaar89d40322006-08-29 15:30:07 +00002617 dorewind = TRUE; /* start again */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002618#endif
2619 }
2620#ifdef FEAT_WINDOWS
2621 ui_breakcheck();
2622 if (got_int)
2623 {
2624 (void)vgetc(); /* only break the file loading, not the rest */
2625 break;
2626 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002627 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002628#endif
2629#ifdef FEAT_WINDOWS
2630 if (parmp->window_layout == WIN_TABS)
2631 goto_tabpage(1);
2632 else
2633 curwin = firstwin;
2634 curbuf = curwin->w_buffer;
2635#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002636#ifdef FEAT_AUTOCMD
2637 --autocmd_no_enter;
2638 --autocmd_no_leave;
2639#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002640 }
2641}
2642
2643#ifdef FEAT_WINDOWS
2644 /*
2645 * If opened more than one window, start editing files in the other
2646 * windows. make_windows() has already opened the windows.
2647 */
2648 static void
2649edit_buffers(parmp)
2650 mparm_T *parmp;
2651{
2652 int arg_idx; /* index in argument list */
2653 int i;
Bram Moolenaar84212822006-11-07 21:59:47 +00002654 int advance = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002655
2656# ifdef FEAT_AUTOCMD
2657 /*
2658 * Don't execute Win/Buf Enter/Leave autocommands here
2659 */
2660 ++autocmd_no_enter;
2661 ++autocmd_no_leave;
2662# endif
Bram Moolenaar84212822006-11-07 21:59:47 +00002663
2664 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2665 if (curwin->w_arg_idx == -1)
2666 {
2667 win_close(curwin, TRUE);
2668 advance = FALSE;
2669 }
2670
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002671 arg_idx = 1;
2672 for (i = 1; i < parmp->window_count; ++i)
2673 {
Bram Moolenaar84212822006-11-07 21:59:47 +00002674 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2675 if (curwin->w_arg_idx == -1)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002676 {
Bram Moolenaar84212822006-11-07 21:59:47 +00002677 ++arg_idx;
2678 win_close(curwin, TRUE);
2679 advance = FALSE;
2680 continue;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002681 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002682
Bram Moolenaar84212822006-11-07 21:59:47 +00002683 if (advance)
2684 {
2685 if (parmp->window_layout == WIN_TABS)
2686 {
2687 if (curtab->tp_next == NULL) /* just checking */
2688 break;
2689 goto_tabpage(0);
2690 }
2691 else
2692 {
2693 if (curwin->w_next == NULL) /* just checking */
2694 break;
2695 win_enter(curwin->w_next, FALSE);
2696 }
2697 }
2698 advance = TRUE;
2699
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002700 /* Only open the file if there is no file in this window yet (that can
Bram Moolenaar84212822006-11-07 21:59:47 +00002701 * happen when .vimrc contains ":sall"). */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002702 if (curbuf == firstwin->w_buffer || curbuf->b_ffname == NULL)
2703 {
2704 curwin->w_arg_idx = arg_idx;
Bram Moolenaar84212822006-11-07 21:59:47 +00002705 /* Edit file from arg list, if there is one. When "Quit" selected
2706 * at the ATTENTION prompt close the window. */
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002707# ifdef HAS_SWAP_EXISTS_ACTION
2708 swap_exists_did_quit = FALSE;
2709# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002710 (void)do_ecmd(0, arg_idx < GARGCOUNT
2711 ? alist_name(&GARGLIST[arg_idx]) : NULL,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002712 NULL, NULL, ECMD_LASTL, ECMD_HIDE, curwin);
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002713# ifdef HAS_SWAP_EXISTS_ACTION
2714 if (swap_exists_did_quit)
Bram Moolenaar84212822006-11-07 21:59:47 +00002715 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002716 /* abort or quit selected */
Bram Moolenaar84212822006-11-07 21:59:47 +00002717 if (got_int || only_one_window())
2718 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002719 /* abort selected and only one window */
Bram Moolenaar84212822006-11-07 21:59:47 +00002720 did_emsg = FALSE; /* avoid hit-enter prompt */
2721 getout(1);
2722 }
2723 win_close(curwin, TRUE);
2724 advance = FALSE;
2725 }
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002726# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002727 if (arg_idx == GARGCOUNT - 1)
2728 arg_had_last = TRUE;
2729 ++arg_idx;
2730 }
2731 ui_breakcheck();
2732 if (got_int)
2733 {
2734 (void)vgetc(); /* only break the file loading, not the rest */
2735 break;
2736 }
2737 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002738
2739 if (parmp->window_layout == WIN_TABS)
2740 goto_tabpage(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002741# ifdef FEAT_AUTOCMD
2742 --autocmd_no_enter;
2743# endif
2744 win_enter(firstwin, FALSE); /* back to first window */
2745# ifdef FEAT_AUTOCMD
2746 --autocmd_no_leave;
2747# endif
2748 TIME_MSG("editing files in windows");
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002749 if (parmp->window_count > 1 && parmp->window_layout != WIN_TABS)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002750 win_equal(curwin, FALSE, 'b'); /* adjust heights */
2751}
2752#endif /* FEAT_WINDOWS */
2753
2754/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002755 * Execute the commands from --cmd arguments "cmds[cnt]".
2756 */
2757 static void
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002758exe_pre_commands(parmp)
2759 mparm_T *parmp;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002760{
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002761 char_u **cmds = parmp->pre_commands;
2762 int cnt = parmp->n_pre_commands;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002763 int i;
2764
2765 if (cnt > 0)
2766 {
2767 curwin->w_cursor.lnum = 0; /* just in case.. */
2768 sourcing_name = (char_u *)_("pre-vimrc command line");
2769# ifdef FEAT_EVAL
2770 current_SID = SID_CMDARG;
2771# endif
2772 for (i = 0; i < cnt; ++i)
2773 do_cmdline_cmd(cmds[i]);
2774 sourcing_name = NULL;
2775# ifdef FEAT_EVAL
2776 current_SID = 0;
2777# endif
2778 TIME_MSG("--cmd commands");
2779 }
2780}
2781
2782/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002783 * Execute "+", "-c" and "-S" arguments.
2784 */
2785 static void
2786exe_commands(parmp)
2787 mparm_T *parmp;
2788{
2789 int i;
2790
2791 /*
2792 * We start commands on line 0, make "vim +/pat file" match a
2793 * pattern on line 1. But don't move the cursor when an autocommand
2794 * with g`" was used.
2795 */
2796 msg_scroll = TRUE;
2797 if (parmp->tagname == NULL && curwin->w_cursor.lnum <= 1)
2798 curwin->w_cursor.lnum = 0;
2799 sourcing_name = (char_u *)"command line";
2800#ifdef FEAT_EVAL
2801 current_SID = SID_CARG;
2802#endif
2803 for (i = 0; i < parmp->n_commands; ++i)
2804 {
2805 do_cmdline_cmd(parmp->commands[i]);
2806 if (parmp->cmds_tofree[i])
2807 vim_free(parmp->commands[i]);
2808 }
2809 sourcing_name = NULL;
2810#ifdef FEAT_EVAL
2811 current_SID = 0;
2812#endif
2813 if (curwin->w_cursor.lnum == 0)
2814 curwin->w_cursor.lnum = 1;
2815
2816 if (!exmode_active)
2817 msg_scroll = FALSE;
2818
2819#ifdef FEAT_QUICKFIX
2820 /* When started with "-q errorfile" jump to first error again. */
2821 if (parmp->edit_type == EDIT_QF)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002822 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002823#endif
2824 TIME_MSG("executing command arguments");
2825}
2826
2827/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002828 * Source startup scripts.
2829 */
2830 static void
2831source_startup_scripts(parmp)
2832 mparm_T *parmp;
2833{
2834 int i;
2835
2836 /*
2837 * For "evim" source evim.vim first of all, so that the user can overrule
2838 * any things he doesn't like.
2839 */
2840 if (parmp->evim_mode)
2841 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002842 (void)do_source((char_u *)EVIM_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002843 TIME_MSG("source evim file");
2844 }
2845
2846 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002847 * If -u argument given, use only the initializations from that file and
Bram Moolenaar58d98232005-07-23 22:25:46 +00002848 * nothing else.
2849 */
2850 if (parmp->use_vimrc != NULL)
2851 {
Bram Moolenaar231334e2005-07-25 20:46:57 +00002852 if (STRCMP(parmp->use_vimrc, "NONE") == 0
2853 || STRCMP(parmp->use_vimrc, "NORC") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002854 {
2855#ifdef FEAT_GUI
2856 if (use_gvimrc == NULL) /* don't load gvimrc either */
2857 use_gvimrc = parmp->use_vimrc;
2858#endif
2859 if (parmp->use_vimrc[2] == 'N')
2860 p_lpl = FALSE; /* don't load plugins either */
2861 }
2862 else
2863 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002864 if (do_source(parmp->use_vimrc, FALSE, DOSO_NONE) != OK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002865 EMSG2(_("E282: Cannot read from \"%s\""), parmp->use_vimrc);
2866 }
2867 }
2868 else if (!silent_mode)
2869 {
2870#ifdef AMIGA
2871 struct Process *proc = (struct Process *)FindTask(0L);
2872 APTR save_winptr = proc->pr_WindowPtr;
2873
2874 /* Avoid a requester here for a volume that doesn't exist. */
2875 proc->pr_WindowPtr = (APTR)-1L;
2876#endif
2877
2878 /*
2879 * Get system wide defaults, if the file name is defined.
2880 */
2881#ifdef SYS_VIMRC_FILE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002882 (void)do_source((char_u *)SYS_VIMRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002883#endif
Bram Moolenaar1056d982006-03-09 22:37:52 +00002884#ifdef MACOS_X
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002885 (void)do_source((char_u *)"$VIMRUNTIME/macmap.vim", FALSE, DOSO_NONE);
Bram Moolenaar1056d982006-03-09 22:37:52 +00002886#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00002887
2888 /*
2889 * Try to read initialization commands from the following places:
2890 * - environment variable VIMINIT
2891 * - user vimrc file (s:.vimrc for Amiga, ~/.vimrc otherwise)
2892 * - second user vimrc file ($VIM/.vimrc for Dos)
2893 * - environment variable EXINIT
2894 * - user exrc file (s:.exrc for Amiga, ~/.exrc otherwise)
2895 * - second user exrc file ($VIM/.exrc for Dos)
2896 * The first that exists is used, the rest is ignored.
2897 */
2898 if (process_env((char_u *)"VIMINIT", TRUE) != OK)
2899 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002900 if (do_source((char_u *)USR_VIMRC_FILE, TRUE, DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00002901#ifdef USR_VIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002902 && do_source((char_u *)USR_VIMRC_FILE2, TRUE,
2903 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00002904#endif
2905#ifdef USR_VIMRC_FILE3
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002906 && do_source((char_u *)USR_VIMRC_FILE3, TRUE,
2907 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00002908#endif
2909 && process_env((char_u *)"EXINIT", FALSE) == FAIL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002910 && do_source((char_u *)USR_EXRC_FILE, FALSE, DOSO_NONE) == FAIL)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002911 {
2912#ifdef USR_EXRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002913 (void)do_source((char_u *)USR_EXRC_FILE2, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002914#endif
2915 }
2916 }
2917
2918 /*
2919 * Read initialization commands from ".vimrc" or ".exrc" in current
2920 * directory. This is only done if the 'exrc' option is set.
2921 * Because of security reasons we disallow shell and write commands
2922 * now, except for unix if the file is owned by the user or 'secure'
2923 * option has been reset in environment of global ".exrc" or ".vimrc".
2924 * Only do this if VIMRC_FILE is not the same as USR_VIMRC_FILE or
2925 * SYS_VIMRC_FILE.
2926 */
2927 if (p_exrc)
2928 {
2929#if defined(UNIX) || defined(VMS)
2930 /* If ".vimrc" file is not owned by user, set 'secure' mode. */
2931 if (!file_owned(VIMRC_FILE))
2932#endif
2933 secure = p_secure;
2934
2935 i = FAIL;
2936 if (fullpathcmp((char_u *)USR_VIMRC_FILE,
2937 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
2938#ifdef USR_VIMRC_FILE2
2939 && fullpathcmp((char_u *)USR_VIMRC_FILE2,
2940 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
2941#endif
2942#ifdef USR_VIMRC_FILE3
2943 && fullpathcmp((char_u *)USR_VIMRC_FILE3,
2944 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
2945#endif
2946#ifdef SYS_VIMRC_FILE
2947 && fullpathcmp((char_u *)SYS_VIMRC_FILE,
2948 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
2949#endif
2950 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002951 i = do_source((char_u *)VIMRC_FILE, TRUE, DOSO_VIMRC);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002952
2953 if (i == FAIL)
2954 {
2955#if defined(UNIX) || defined(VMS)
2956 /* if ".exrc" is not owned by user set 'secure' mode */
2957 if (!file_owned(EXRC_FILE))
2958 secure = p_secure;
2959 else
2960 secure = 0;
2961#endif
2962 if ( fullpathcmp((char_u *)USR_EXRC_FILE,
2963 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
2964#ifdef USR_EXRC_FILE2
2965 && fullpathcmp((char_u *)USR_EXRC_FILE2,
2966 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
2967#endif
2968 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002969 (void)do_source((char_u *)EXRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002970 }
2971 }
2972 if (secure == 2)
2973 need_wait_return = TRUE;
2974 secure = 0;
2975#ifdef AMIGA
2976 proc->pr_WindowPtr = save_winptr;
2977#endif
2978 }
2979 TIME_MSG("sourcing vimrc file(s)");
2980}
2981
2982/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002983 * Setup to start using the GUI. Exit with an error when not available.
2984 */
2985 static void
2986main_start_gui()
2987{
2988#ifdef FEAT_GUI
2989 gui.starting = TRUE; /* start GUI a bit later */
2990#else
2991 mch_errmsg(_(e_nogvim));
2992 mch_errmsg("\n");
2993 mch_exit(2);
2994#endif
2995}
2996
2997/*
Bram Moolenaar49325942007-05-10 19:19:59 +00002998 * Get an environment variable, and execute it as Ex commands.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002999 * Returns FAIL if the environment variable was not executed, OK otherwise.
3000 */
3001 int
3002process_env(env, is_viminit)
3003 char_u *env;
3004 int is_viminit; /* when TRUE, called for VIMINIT */
3005{
3006 char_u *initstr;
3007 char_u *save_sourcing_name;
3008 linenr_T save_sourcing_lnum;
3009#ifdef FEAT_EVAL
3010 scid_T save_sid;
3011#endif
3012
3013 if ((initstr = mch_getenv(env)) != NULL && *initstr != NUL)
3014 {
3015 if (is_viminit)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003016 vimrc_found(NULL, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003017 save_sourcing_name = sourcing_name;
3018 save_sourcing_lnum = sourcing_lnum;
3019 sourcing_name = env;
3020 sourcing_lnum = 0;
3021#ifdef FEAT_EVAL
3022 save_sid = current_SID;
3023 current_SID = SID_ENV;
3024#endif
3025 do_cmdline_cmd(initstr);
3026 sourcing_name = save_sourcing_name;
3027 sourcing_lnum = save_sourcing_lnum;
3028#ifdef FEAT_EVAL
3029 current_SID = save_sid;;
3030#endif
3031 return OK;
3032 }
3033 return FAIL;
3034}
3035
3036#if defined(UNIX) || defined(VMS)
3037/*
3038 * Return TRUE if we are certain the user owns the file "fname".
3039 * Used for ".vimrc" and ".exrc".
3040 * Use both stat() and lstat() for extra security.
3041 */
3042 static int
3043file_owned(fname)
3044 char *fname;
3045{
3046 struct stat s;
3047# ifdef UNIX
3048 uid_t uid = getuid();
3049# else /* VMS */
3050 uid_t uid = ((getgid() << 16) | getuid());
3051# endif
3052
3053 return !(mch_stat(fname, &s) != 0 || s.st_uid != uid
3054# ifdef HAVE_LSTAT
3055 || mch_lstat(fname, &s) != 0 || s.st_uid != uid
3056# endif
3057 );
3058}
3059#endif
3060
3061/*
3062 * Give an error message main_errors["n"] and exit.
3063 */
3064 static void
3065mainerr(n, str)
3066 int n; /* one of the ME_ defines */
3067 char_u *str; /* extra argument or NULL */
3068{
3069#if defined(UNIX) || defined(__EMX__) || defined(VMS)
3070 reset_signals(); /* kill us with CTRL-C here, if you like */
3071#endif
3072
3073 mch_errmsg(longVersion);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003074 mch_errmsg("\n");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003075 mch_errmsg(_(main_errors[n]));
3076 if (str != NULL)
3077 {
3078 mch_errmsg(": \"");
3079 mch_errmsg((char *)str);
3080 mch_errmsg("\"");
3081 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003082 mch_errmsg(_("\nMore info with: \"vim -h\"\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003083
3084 mch_exit(1);
3085}
3086
3087 void
3088mainerr_arg_missing(str)
3089 char_u *str;
3090{
3091 mainerr(ME_ARG_MISSING, str);
3092}
3093
3094/*
3095 * print a message with three spaces prepended and '\n' appended.
3096 */
3097 static void
3098main_msg(s)
3099 char *s;
3100{
3101 mch_msg(" ");
3102 mch_msg(s);
3103 mch_msg("\n");
3104}
3105
3106/*
3107 * Print messages for "vim -h" or "vim --help" and exit.
3108 */
3109 static void
3110usage()
3111{
3112 int i;
3113 static char *(use[]) =
3114 {
3115 N_("[file ..] edit specified file(s)"),
3116 N_("- read text from stdin"),
3117 N_("-t tag edit file where tag is defined"),
3118#ifdef FEAT_QUICKFIX
3119 N_("-q [errorfile] edit file with first error")
3120#endif
3121 };
3122
3123#if defined(UNIX) || defined(__EMX__) || defined(VMS)
3124 reset_signals(); /* kill us with CTRL-C here, if you like */
3125#endif
3126
3127 mch_msg(longVersion);
3128 mch_msg(_("\n\nusage:"));
3129 for (i = 0; ; ++i)
3130 {
3131 mch_msg(_(" vim [arguments] "));
3132 mch_msg(_(use[i]));
3133 if (i == (sizeof(use) / sizeof(char_u *)) - 1)
3134 break;
3135 mch_msg(_("\n or:"));
3136 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003137#ifdef VMS
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003138 mch_msg(_("\nWhere case is ignored prepend / to make flag upper case"));
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003139#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003140
3141 mch_msg(_("\n\nArguments:\n"));
3142 main_msg(_("--\t\t\tOnly file names after this"));
3143#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
3144 main_msg(_("--literal\t\tDon't expand wildcards"));
3145#endif
3146#ifdef FEAT_OLE
3147 main_msg(_("-register\t\tRegister this gvim for OLE"));
3148 main_msg(_("-unregister\t\tUnregister gvim for OLE"));
3149#endif
3150#ifdef FEAT_GUI
3151 main_msg(_("-g\t\t\tRun using GUI (like \"gvim\")"));
3152 main_msg(_("-f or --nofork\tForeground: Don't fork when starting GUI"));
3153#endif
3154 main_msg(_("-v\t\t\tVi mode (like \"vi\")"));
3155 main_msg(_("-e\t\t\tEx mode (like \"ex\")"));
3156 main_msg(_("-s\t\t\tSilent (batch) mode (only for \"ex\")"));
3157#ifdef FEAT_DIFF
3158 main_msg(_("-d\t\t\tDiff mode (like \"vimdiff\")"));
3159#endif
3160 main_msg(_("-y\t\t\tEasy mode (like \"evim\", modeless)"));
3161 main_msg(_("-R\t\t\tReadonly mode (like \"view\")"));
3162 main_msg(_("-Z\t\t\tRestricted mode (like \"rvim\")"));
3163 main_msg(_("-m\t\t\tModifications (writing files) not allowed"));
3164 main_msg(_("-M\t\t\tModifications in text not allowed"));
3165 main_msg(_("-b\t\t\tBinary mode"));
3166#ifdef FEAT_LISP
3167 main_msg(_("-l\t\t\tLisp mode"));
3168#endif
3169 main_msg(_("-C\t\t\tCompatible with Vi: 'compatible'"));
3170 main_msg(_("-N\t\t\tNot fully Vi compatible: 'nocompatible'"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003171 main_msg(_("-V[N][fname]\t\tBe verbose [level N] [log messages to fname]"));
3172#ifdef FEAT_EVAL
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003173 main_msg(_("-D\t\t\tDebugging mode"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003174#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003175 main_msg(_("-n\t\t\tNo swap file, use memory only"));
3176 main_msg(_("-r\t\t\tList swap files and exit"));
3177 main_msg(_("-r (with file name)\tRecover crashed session"));
3178 main_msg(_("-L\t\t\tSame as -r"));
3179#ifdef AMIGA
3180 main_msg(_("-f\t\t\tDon't use newcli to open window"));
3181 main_msg(_("-dev <device>\t\tUse <device> for I/O"));
3182#endif
3183#ifdef FEAT_ARABIC
3184 main_msg(_("-A\t\t\tstart in Arabic mode"));
3185#endif
3186#ifdef FEAT_RIGHTLEFT
3187 main_msg(_("-H\t\t\tStart in Hebrew mode"));
3188#endif
3189#ifdef FEAT_FKMAP
3190 main_msg(_("-F\t\t\tStart in Farsi mode"));
3191#endif
3192 main_msg(_("-T <terminal>\tSet terminal type to <terminal>"));
3193 main_msg(_("-u <vimrc>\t\tUse <vimrc> instead of any .vimrc"));
3194#ifdef FEAT_GUI
3195 main_msg(_("-U <gvimrc>\t\tUse <gvimrc> instead of any .gvimrc"));
3196#endif
3197 main_msg(_("--noplugin\t\tDon't load plugin scripts"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003198#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003199 main_msg(_("-p[N]\t\tOpen N tab pages (default: one for each file)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003200 main_msg(_("-o[N]\t\tOpen N windows (default: one for each file)"));
3201 main_msg(_("-O[N]\t\tLike -o but split vertically"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003202#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003203 main_msg(_("+\t\t\tStart at end of file"));
3204 main_msg(_("+<lnum>\t\tStart at line <lnum>"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003205 main_msg(_("--cmd <command>\tExecute <command> before loading any vimrc file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003206 main_msg(_("-c <command>\t\tExecute <command> after loading the first file"));
3207 main_msg(_("-S <session>\t\tSource file <session> after loading the first file"));
3208 main_msg(_("-s <scriptin>\tRead Normal mode commands from file <scriptin>"));
3209 main_msg(_("-w <scriptout>\tAppend all typed commands to file <scriptout>"));
3210 main_msg(_("-W <scriptout>\tWrite all typed commands to file <scriptout>"));
3211#ifdef FEAT_CRYPT
3212 main_msg(_("-x\t\t\tEdit encrypted files"));
3213#endif
3214#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
3215# if defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)
3216 main_msg(_("-display <display>\tConnect vim to this particular X-server"));
3217# endif
3218 main_msg(_("-X\t\t\tDo not connect to X server"));
3219#endif
3220#ifdef FEAT_CLIENTSERVER
3221 main_msg(_("--remote <files>\tEdit <files> in a Vim server if possible"));
3222 main_msg(_("--remote-silent <files> Same, don't complain if there is no server"));
3223 main_msg(_("--remote-wait <files> As --remote but wait for files to have been edited"));
3224 main_msg(_("--remote-wait-silent <files> Same, don't complain if there is no server"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003225# ifdef FEAT_WINDOWS
Bram Moolenaar82ad3242008-01-11 19:26:36 +00003226 main_msg(_("--remote-tab[-wait][-silent] <files> As --remote but use tab page per file"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003227# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003228 main_msg(_("--remote-send <keys>\tSend <keys> to a Vim server and exit"));
3229 main_msg(_("--remote-expr <expr>\tEvaluate <expr> in a Vim server and print result"));
3230 main_msg(_("--serverlist\t\tList available Vim server names and exit"));
3231 main_msg(_("--servername <name>\tSend to/become the Vim server <name>"));
3232#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003233#ifdef STARTUPTIME
Bram Moolenaar34ef52d2009-11-17 11:31:25 +00003234 main_msg(_("--startuptime <file>\tWrite startup timing messages to <file>"));
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003235#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003236#ifdef FEAT_VIMINFO
3237 main_msg(_("-i <viminfo>\t\tUse <viminfo> instead of .viminfo"));
3238#endif
3239 main_msg(_("-h or --help\tPrint Help (this message) and exit"));
3240 main_msg(_("--version\t\tPrint version information and exit"));
3241
3242#ifdef FEAT_GUI_X11
3243# ifdef FEAT_GUI_MOTIF
3244 mch_msg(_("\nArguments recognised by gvim (Motif version):\n"));
3245# else
3246# ifdef FEAT_GUI_ATHENA
3247# ifdef FEAT_GUI_NEXTAW
3248 mch_msg(_("\nArguments recognised by gvim (neXtaw version):\n"));
3249# else
3250 mch_msg(_("\nArguments recognised by gvim (Athena version):\n"));
3251# endif
3252# endif
3253# endif
3254 main_msg(_("-display <display>\tRun vim on <display>"));
3255 main_msg(_("-iconic\t\tStart vim iconified"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003256 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
3257 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
3258 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3259 main_msg(_("-boldfont <font>\tUse <font> for bold text"));
3260 main_msg(_("-italicfont <font>\tUse <font> for italic text"));
3261 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3262 main_msg(_("-borderwidth <width>\tUse a border width of <width> (also: -bw)"));
3263 main_msg(_("-scrollbarwidth <width> Use a scrollbar width of <width> (also: -sw)"));
3264# ifdef FEAT_GUI_ATHENA
3265 main_msg(_("-menuheight <height>\tUse a menu bar height of <height> (also: -mh)"));
3266# endif
3267 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3268 main_msg(_("+reverse\t\tDon't use reverse video (also: +rv)"));
3269 main_msg(_("-xrm <resource>\tSet the specified resource"));
3270#endif /* FEAT_GUI_X11 */
3271#if defined(FEAT_GUI) && defined(RISCOS)
3272 mch_msg(_("\nArguments recognised by gvim (RISC OS version):\n"));
3273 main_msg(_("--columns <number>\tInitial width of window in columns"));
3274 main_msg(_("--rows <number>\tInitial height of window in rows"));
3275#endif
3276#ifdef FEAT_GUI_GTK
3277 mch_msg(_("\nArguments recognised by gvim (GTK+ version):\n"));
3278 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3279 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3280 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3281 main_msg(_("-display <display>\tRun vim on <display> (also: --display)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003282 main_msg(_("--role <role>\tSet a unique role to identify the main window"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003283 main_msg(_("--socketid <xid>\tOpen Vim inside another GTK widget"));
3284#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003285#ifdef FEAT_GUI_W32
3286 main_msg(_("-P <parent title>\tOpen Vim inside parent application"));
Bram Moolenaar78e17622007-08-30 10:26:19 +00003287 main_msg(_("--windowid <HWND>\tOpen Vim inside another win32 widget"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003288#endif
3289
3290#ifdef FEAT_GUI_GNOME
3291 /* Gnome gives extra messages for --help if we continue, but not for -h. */
3292 if (gui.starting)
3293 mch_msg("\n");
3294 else
3295#endif
3296 mch_exit(0);
3297}
3298
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00003299#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003300/*
3301 * Check the result of the ATTENTION dialog:
3302 * When "Quit" selected, exit Vim.
3303 * When "Recover" selected, recover the file.
3304 */
3305 static void
3306check_swap_exists_action()
3307{
3308 if (swap_exists_action == SEA_QUIT)
3309 getout(1);
3310 handle_swap_exists(NULL);
3311}
3312#endif
3313
3314#if defined(STARTUPTIME) || defined(PROTO)
3315static void time_diff __ARGS((struct timeval *then, struct timeval *now));
3316
3317static struct timeval prev_timeval;
3318
Bram Moolenaar3f269672009-11-03 11:11:11 +00003319# ifdef WIN3264
3320/*
3321 * Windows doesn't have gettimeofday(), although it does have struct timeval.
3322 */
3323 static int
3324gettimeofday(struct timeval *tv, char *dummy)
3325{
3326 long t = clock();
3327 tv->tv_sec = t / CLOCKS_PER_SEC;
3328 tv->tv_usec = (t - tv->tv_sec * CLOCKS_PER_SEC) * 1000000 / CLOCKS_PER_SEC;
3329 return 0;
3330}
3331# endif
3332
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003333/*
3334 * Save the previous time before doing something that could nest.
3335 * set "*tv_rel" to the time elapsed so far.
3336 */
3337 void
3338time_push(tv_rel, tv_start)
3339 void *tv_rel, *tv_start;
3340{
3341 *((struct timeval *)tv_rel) = prev_timeval;
3342 gettimeofday(&prev_timeval, NULL);
3343 ((struct timeval *)tv_rel)->tv_usec = prev_timeval.tv_usec
3344 - ((struct timeval *)tv_rel)->tv_usec;
3345 ((struct timeval *)tv_rel)->tv_sec = prev_timeval.tv_sec
3346 - ((struct timeval *)tv_rel)->tv_sec;
3347 if (((struct timeval *)tv_rel)->tv_usec < 0)
3348 {
3349 ((struct timeval *)tv_rel)->tv_usec += 1000000;
3350 --((struct timeval *)tv_rel)->tv_sec;
3351 }
3352 *(struct timeval *)tv_start = prev_timeval;
3353}
3354
3355/*
3356 * Compute the previous time after doing something that could nest.
3357 * Subtract "*tp" from prev_timeval;
3358 * Note: The arguments are (void *) to avoid trouble with systems that don't
3359 * have struct timeval.
3360 */
3361 void
3362time_pop(tp)
3363 void *tp; /* actually (struct timeval *) */
3364{
3365 prev_timeval.tv_usec -= ((struct timeval *)tp)->tv_usec;
3366 prev_timeval.tv_sec -= ((struct timeval *)tp)->tv_sec;
3367 if (prev_timeval.tv_usec < 0)
3368 {
3369 prev_timeval.tv_usec += 1000000;
3370 --prev_timeval.tv_sec;
3371 }
3372}
3373
3374 static void
3375time_diff(then, now)
3376 struct timeval *then;
3377 struct timeval *now;
3378{
3379 long usec;
3380 long msec;
3381
3382 usec = now->tv_usec - then->tv_usec;
3383 msec = (now->tv_sec - then->tv_sec) * 1000L + usec / 1000L,
3384 usec = usec % 1000L;
3385 fprintf(time_fd, "%03ld.%03ld", msec, usec >= 0 ? usec : usec + 1000L);
3386}
3387
3388 void
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003389time_msg(mesg, tv_start)
3390 char *mesg;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003391 void *tv_start; /* only for do_source: start time; actually
3392 (struct timeval *) */
3393{
3394 static struct timeval start;
3395 struct timeval now;
3396
3397 if (time_fd != NULL)
3398 {
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003399 if (strstr(mesg, "STARTING") != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003400 {
3401 gettimeofday(&start, NULL);
3402 prev_timeval = start;
3403 fprintf(time_fd, "\n\ntimes in msec\n");
3404 fprintf(time_fd, " clock self+sourced self: sourced script\n");
3405 fprintf(time_fd, " clock elapsed: other lines\n\n");
3406 }
3407 gettimeofday(&now, NULL);
3408 time_diff(&start, &now);
3409 if (((struct timeval *)tv_start) != NULL)
3410 {
3411 fprintf(time_fd, " ");
3412 time_diff(((struct timeval *)tv_start), &now);
3413 }
3414 fprintf(time_fd, " ");
3415 time_diff(&prev_timeval, &now);
3416 prev_timeval = now;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003417 fprintf(time_fd, ": %s\n", mesg);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003418 }
3419}
3420
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003421#endif
3422
3423#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
3424
3425/*
3426 * Common code for the X command server and the Win32 command server.
3427 */
3428
Bram Moolenaareb94e552006-03-11 21:35:11 +00003429static char_u *build_drop_cmd __ARGS((int filec, char **filev, int tabs, int sendReply));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003430
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003431/*
3432 * Do the client-server stuff, unless "--servername ''" was used.
3433 */
3434 static void
3435exec_on_server(parmp)
3436 mparm_T *parmp;
3437{
3438 if (parmp->serverName_arg == NULL || *parmp->serverName_arg != NUL)
3439 {
3440# ifdef WIN32
3441 /* Initialise the client/server messaging infrastructure. */
3442 serverInitMessaging();
3443# endif
3444
3445 /*
3446 * When a command server argument was found, execute it. This may
3447 * exit Vim when it was successful. Otherwise it's executed further
3448 * on. Remember the encoding used here in "serverStrEnc".
3449 */
3450 if (parmp->serverArg)
3451 {
3452 cmdsrv_main(&parmp->argc, parmp->argv,
3453 parmp->serverName_arg, &parmp->serverStr);
3454# ifdef FEAT_MBYTE
3455 parmp->serverStrEnc = vim_strsave(p_enc);
3456# endif
3457 }
3458
3459 /* If we're still running, get the name to register ourselves.
3460 * On Win32 can register right now, for X11 need to setup the
3461 * clipboard first, it's further down. */
3462 parmp->servername = serverMakeName(parmp->serverName_arg,
3463 parmp->argv[0]);
3464# ifdef WIN32
3465 if (parmp->servername != NULL)
3466 {
3467 serverSetName(parmp->servername);
3468 vim_free(parmp->servername);
3469 }
3470# endif
3471 }
3472}
3473
3474/*
3475 * Prepare for running as a Vim server.
3476 */
3477 static void
3478prepare_server(parmp)
3479 mparm_T *parmp;
3480{
3481# if defined(FEAT_X11)
3482 /*
3483 * Register for remote command execution with :serversend and --remote
3484 * unless there was a -X or a --servername '' on the command line.
3485 * Only register nongui-vim's with an explicit --servername argument.
Bram Moolenaar5f402312006-08-15 19:40:35 +00003486 * When running as root --servername is also required.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003487 */
3488 if (X_DISPLAY != NULL && parmp->servername != NULL && (
3489# ifdef FEAT_GUI
Bram Moolenaar5f402312006-08-15 19:40:35 +00003490 (gui.in_use
3491# ifdef UNIX
Bram Moolenaar311d9822007-02-27 15:48:28 +00003492 && getuid() != ROOT_UID
Bram Moolenaar5f402312006-08-15 19:40:35 +00003493# endif
3494 ) ||
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003495# endif
3496 parmp->serverName_arg != NULL))
3497 {
3498 (void)serverRegisterName(X_DISPLAY, parmp->servername);
3499 vim_free(parmp->servername);
3500 TIME_MSG("register server name");
3501 }
3502 else
3503 serverDelayedStartName = parmp->servername;
3504# endif
3505
3506 /*
3507 * Execute command ourselves if we're here because the send failed (or
3508 * else we would have exited above).
3509 */
3510 if (parmp->serverStr != NULL)
3511 {
3512 char_u *p;
3513
3514 server_to_input_buf(serverConvert(parmp->serverStrEnc,
3515 parmp->serverStr, &p));
3516 vim_free(p);
3517 }
3518}
3519
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003520 static void
3521cmdsrv_main(argc, argv, serverName_arg, serverStr)
3522 int *argc;
3523 char **argv;
3524 char_u *serverName_arg;
3525 char_u **serverStr;
3526{
3527 char_u *res;
3528 int i;
3529 char_u *sname;
3530 int ret;
3531 int didone = FALSE;
3532 int exiterr = 0;
3533 char **newArgV = argv + 1;
3534 int newArgC = 1,
3535 Argc = *argc;
3536 int argtype;
3537#define ARGTYPE_OTHER 0
3538#define ARGTYPE_EDIT 1
3539#define ARGTYPE_EDIT_WAIT 2
3540#define ARGTYPE_SEND 3
3541 int silent = FALSE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003542 int tabs = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003543# ifndef FEAT_X11
3544 HWND srv;
3545# else
3546 Window srv;
3547
3548 setup_term_clip();
3549# endif
3550
3551 sname = serverMakeName(serverName_arg, argv[0]);
3552 if (sname == NULL)
3553 return;
3554
3555 /*
3556 * Execute the command server related arguments and remove them
3557 * from the argc/argv array; We may have to return into main()
3558 */
3559 for (i = 1; i < Argc; i++)
3560 {
3561 res = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003562 if (STRCMP(argv[i], "--") == 0) /* end of option arguments */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003563 {
3564 for (; i < *argc; i++)
3565 {
3566 *newArgV++ = argv[i];
3567 newArgC++;
3568 }
3569 break;
3570 }
3571
Bram Moolenaareb94e552006-03-11 21:35:11 +00003572 if (STRICMP(argv[i], "--remote-send") == 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003573 argtype = ARGTYPE_SEND;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003574 else if (STRNICMP(argv[i], "--remote", 8) == 0)
3575 {
3576 char *p = argv[i] + 8;
3577
3578 argtype = ARGTYPE_EDIT;
3579 while (*p != NUL)
3580 {
3581 if (STRNICMP(p, "-wait", 5) == 0)
3582 {
3583 argtype = ARGTYPE_EDIT_WAIT;
3584 p += 5;
3585 }
3586 else if (STRNICMP(p, "-silent", 7) == 0)
3587 {
3588 silent = TRUE;
3589 p += 7;
3590 }
3591 else if (STRNICMP(p, "-tab", 4) == 0)
3592 {
3593 tabs = TRUE;
3594 p += 4;
3595 }
3596 else
3597 {
3598 argtype = ARGTYPE_OTHER;
3599 break;
3600 }
3601 }
3602 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003603 else
3604 argtype = ARGTYPE_OTHER;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003605
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003606 if (argtype != ARGTYPE_OTHER)
3607 {
3608 if (i == *argc - 1)
3609 mainerr_arg_missing((char_u *)argv[i]);
3610 if (argtype == ARGTYPE_SEND)
3611 {
3612 *serverStr = (char_u *)argv[i + 1];
3613 i++;
3614 }
3615 else
3616 {
3617 *serverStr = build_drop_cmd(*argc - i - 1, argv + i + 1,
Bram Moolenaareb94e552006-03-11 21:35:11 +00003618 tabs, argtype == ARGTYPE_EDIT_WAIT);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003619 if (*serverStr == NULL)
3620 {
3621 /* Probably out of memory, exit. */
3622 didone = TRUE;
3623 exiterr = 1;
3624 break;
3625 }
3626 Argc = i;
3627 }
3628# ifdef FEAT_X11
3629 if (xterm_dpy == NULL)
3630 {
3631 mch_errmsg(_("No display"));
3632 ret = -1;
3633 }
3634 else
3635 ret = serverSendToVim(xterm_dpy, sname, *serverStr,
3636 NULL, &srv, 0, 0, silent);
3637# else
3638 /* Win32 always works? */
3639 ret = serverSendToVim(sname, *serverStr, NULL, &srv, 0, silent);
3640# endif
3641 if (ret < 0)
3642 {
3643 if (argtype == ARGTYPE_SEND)
3644 {
3645 /* Failed to send, abort. */
3646 mch_errmsg(_(": Send failed.\n"));
3647 didone = TRUE;
3648 exiterr = 1;
3649 }
3650 else if (!silent)
3651 /* Let vim start normally. */
3652 mch_errmsg(_(": Send failed. Trying to execute locally\n"));
3653 break;
3654 }
3655
3656# ifdef FEAT_GUI_W32
3657 /* Guess that when the server name starts with "g" it's a GUI
3658 * server, which we can bring to the foreground here.
3659 * Foreground() in the server doesn't work very well. */
3660 if (argtype != ARGTYPE_SEND && TOUPPER_ASC(*sname) == 'G')
3661 SetForegroundWindow(srv);
3662# endif
3663
3664 /*
3665 * For --remote-wait: Wait until the server did edit each
3666 * file. Also detect that the server no longer runs.
3667 */
3668 if (ret >= 0 && argtype == ARGTYPE_EDIT_WAIT)
3669 {
3670 int numFiles = *argc - i - 1;
3671 int j;
3672 char_u *done = alloc(numFiles);
3673 char_u *p;
3674# ifdef FEAT_GUI_W32
3675 NOTIFYICONDATA ni;
3676 int count = 0;
3677 extern HWND message_window;
3678# endif
3679
3680 if (numFiles > 0 && argv[i + 1][0] == '+')
3681 /* Skip "+cmd" argument, don't wait for it to be edited. */
3682 --numFiles;
3683
3684# ifdef FEAT_GUI_W32
3685 ni.cbSize = sizeof(ni);
3686 ni.hWnd = message_window;
3687 ni.uID = 0;
3688 ni.uFlags = NIF_ICON|NIF_TIP;
3689 ni.hIcon = LoadIcon((HINSTANCE)GetModuleHandle(0), "IDR_VIM");
3690 sprintf(ni.szTip, _("%d of %d edited"), count, numFiles);
3691 Shell_NotifyIcon(NIM_ADD, &ni);
3692# endif
3693
3694 /* Wait for all files to unload in remote */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02003695 vim_memset(done, 0, numFiles);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003696 while (memchr(done, 0, numFiles) != NULL)
3697 {
3698# ifdef WIN32
3699 p = serverGetReply(srv, NULL, TRUE, TRUE);
3700 if (p == NULL)
3701 break;
3702# else
3703 if (serverReadReply(xterm_dpy, srv, &p, TRUE) < 0)
3704 break;
3705# endif
3706 j = atoi((char *)p);
3707 if (j >= 0 && j < numFiles)
3708 {
3709# ifdef FEAT_GUI_W32
3710 ++count;
3711 sprintf(ni.szTip, _("%d of %d edited"),
3712 count, numFiles);
3713 Shell_NotifyIcon(NIM_MODIFY, &ni);
3714# endif
3715 done[j] = 1;
3716 }
3717 }
3718# ifdef FEAT_GUI_W32
3719 Shell_NotifyIcon(NIM_DELETE, &ni);
3720# endif
3721 }
3722 }
3723 else if (STRICMP(argv[i], "--remote-expr") == 0)
3724 {
3725 if (i == *argc - 1)
3726 mainerr_arg_missing((char_u *)argv[i]);
3727# ifdef WIN32
3728 /* Win32 always works? */
3729 if (serverSendToVim(sname, (char_u *)argv[i + 1],
3730 &res, NULL, 1, FALSE) < 0)
3731# else
3732 if (xterm_dpy == NULL)
3733 mch_errmsg(_("No display: Send expression failed.\n"));
3734 else if (serverSendToVim(xterm_dpy, sname, (char_u *)argv[i + 1],
3735 &res, NULL, 1, 1, FALSE) < 0)
3736# endif
3737 {
3738 if (res != NULL && *res != NUL)
3739 {
3740 /* Output error from remote */
3741 mch_errmsg((char *)res);
3742 vim_free(res);
3743 res = NULL;
3744 }
3745 mch_errmsg(_(": Send expression failed.\n"));
3746 }
3747 }
3748 else if (STRICMP(argv[i], "--serverlist") == 0)
3749 {
3750# ifdef WIN32
3751 /* Win32 always works? */
3752 res = serverGetVimNames();
3753# else
3754 if (xterm_dpy != NULL)
3755 res = serverGetVimNames(xterm_dpy);
3756# endif
3757 if (called_emsg)
3758 mch_errmsg("\n");
3759 }
3760 else if (STRICMP(argv[i], "--servername") == 0)
3761 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +00003762 /* Already processed. Take it out of the command line */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003763 i++;
3764 continue;
3765 }
3766 else
3767 {
3768 *newArgV++ = argv[i];
3769 newArgC++;
3770 continue;
3771 }
3772 didone = TRUE;
3773 if (res != NULL && *res != NUL)
3774 {
3775 mch_msg((char *)res);
3776 if (res[STRLEN(res) - 1] != '\n')
3777 mch_msg("\n");
3778 }
3779 vim_free(res);
3780 }
3781
3782 if (didone)
3783 {
3784 display_errors(); /* display any collected messages */
3785 exit(exiterr); /* Mission accomplished - get out */
3786 }
3787
3788 /* Return back into main() */
3789 *argc = newArgC;
3790 vim_free(sname);
3791}
3792
3793/*
3794 * Build a ":drop" command to send to a Vim server.
3795 */
3796 static char_u *
Bram Moolenaareb94e552006-03-11 21:35:11 +00003797build_drop_cmd(filec, filev, tabs, sendReply)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003798 int filec;
3799 char **filev;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003800 int tabs; /* Use ":tab drop" instead of ":drop". */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003801 int sendReply;
3802{
3803 garray_T ga;
3804 int i;
3805 char_u *inicmd = NULL;
3806 char_u *p;
3807 char_u cwd[MAXPATHL];
3808
3809 if (filec > 0 && filev[0][0] == '+')
3810 {
3811 inicmd = (char_u *)filev[0] + 1;
3812 filev++;
3813 filec--;
3814 }
3815 /* Check if we have at least one argument. */
3816 if (filec <= 0)
3817 mainerr_arg_missing((char_u *)filev[-1]);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003818
3819 /* Temporarily cd to the current directory to handle relative file names. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003820 if (mch_dirname(cwd, MAXPATHL) != OK)
3821 return NULL;
Bram Moolenaar02b06312007-09-06 15:39:22 +00003822 if ((p = vim_strsave_escaped_ext(cwd,
3823#ifdef BACKSLASH_IN_FILENAME
3824 "", /* rem_backslash() will tell what chars to escape */
3825#else
3826 PATH_ESC_CHARS,
3827#endif
3828 '\\', TRUE)) == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003829 return NULL;
3830 ga_init2(&ga, 1, 100);
3831 ga_concat(&ga, (char_u *)"<C-\\><C-N>:cd ");
3832 ga_concat(&ga, p);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003833 vim_free(p);
Bram Moolenaareb94e552006-03-11 21:35:11 +00003834
3835 /* Call inputsave() so that a prompt for an encryption key works. */
3836 ga_concat(&ga, (char_u *)"<CR>:if exists('*inputsave')|call inputsave()|endif|");
3837 if (tabs)
3838 ga_concat(&ga, (char_u *)"tab ");
3839 ga_concat(&ga, (char_u *)"drop");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003840 for (i = 0; i < filec; i++)
3841 {
3842 /* On Unix the shell has already expanded the wildcards, don't want to
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003843 * do it again in the Vim server. On MS-Windows only escape
3844 * non-wildcard characters. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003845 p = vim_strsave_escaped((char_u *)filev[i],
3846#ifdef UNIX
3847 PATH_ESC_CHARS
3848#else
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003849 (char_u *)" \t%#"
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003850#endif
3851 );
3852 if (p == NULL)
3853 {
3854 vim_free(ga.ga_data);
3855 return NULL;
3856 }
3857 ga_concat(&ga, (char_u *)" ");
3858 ga_concat(&ga, p);
3859 vim_free(p);
3860 }
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003861 ga_concat(&ga, (char_u *)"|if exists('*inputrestore')|call inputrestore()|endif<CR>");
3862
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003863 /* The :drop commands goes to Insert mode when 'insertmode' is set, use
3864 * CTRL-\ CTRL-N again. */
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003865 ga_concat(&ga, (char_u *)"<C-\\><C-N>");
3866
3867 /* Switch back to the correct current directory (prior to temporary path
3868 * switch) unless 'autochdir' is set, in which case it will already be
3869 * correct after the :drop command. */
3870 ga_concat(&ga, (char_u *)":if !exists('+acd')||!&acd|cd -|endif<CR>");
3871
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003872 if (sendReply)
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003873 ga_concat(&ga, (char_u *)":call SetupRemoteReplies()<CR>");
3874 ga_concat(&ga, (char_u *)":");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003875 if (inicmd != NULL)
3876 {
3877 /* Can't use <CR> after "inicmd", because an "startinsert" would cause
3878 * the following commands to be inserted as text. Use a "|",
3879 * hopefully "inicmd" does allow this... */
3880 ga_concat(&ga, inicmd);
3881 ga_concat(&ga, (char_u *)"|");
3882 }
3883 /* Bring the window to the foreground, goto Insert mode when 'im' set and
3884 * clear command line. */
Bram Moolenaar567e4de2004-12-31 21:01:02 +00003885 ga_concat(&ga, (char_u *)"cal foreground()|if &im|star|en|redr|f<CR>");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003886 ga_append(&ga, NUL);
3887 return ga.ga_data;
3888}
3889
3890/*
3891 * Replace termcodes such as <CR> and insert as key presses if there is room.
3892 */
3893 void
3894server_to_input_buf(str)
3895 char_u *str;
3896{
3897 char_u *ptr = NULL;
3898 char_u *cpo_save = p_cpo;
3899
3900 /* Set 'cpoptions' the way we want it.
3901 * B set - backslashes are *not* treated specially
3902 * k set - keycodes are *not* reverse-engineered
3903 * < unset - <Key> sequences *are* interpreted
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00003904 * The last but one parameter of replace_termcodes() is TRUE so that the
3905 * <lt> sequence is recognised - needed for a real backslash.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003906 */
3907 p_cpo = (char_u *)"Bk";
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00003908 str = replace_termcodes((char_u *)str, &ptr, FALSE, TRUE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003909 p_cpo = cpo_save;
3910
3911 if (*ptr != NUL) /* trailing CTRL-V results in nothing */
3912 {
3913 /*
3914 * Add the string to the input stream.
3915 * Can't use add_to_input_buf() here, we now have K_SPECIAL bytes.
3916 *
3917 * First clear typed characters from the typeahead buffer, there could
3918 * be half a mapping there. Then append to the existing string, so
3919 * that multiple commands from a client are concatenated.
3920 */
3921 if (typebuf.tb_maplen < typebuf.tb_len)
3922 del_typebuf(typebuf.tb_len - typebuf.tb_maplen, typebuf.tb_maplen);
3923 (void)ins_typebuf(str, REMAP_NONE, typebuf.tb_len, TRUE, FALSE);
3924
3925 /* Let input_available() know we inserted text in the typeahead
3926 * buffer. */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003927 typebuf_was_filled = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003928 }
3929 vim_free((char_u *)ptr);
3930}
3931
3932/*
3933 * Evaluate an expression that the client sent to a string.
3934 * Handles disabling error messages and disables debugging, otherwise Vim
3935 * hangs, waiting for "cont" to be typed.
3936 */
3937 char_u *
3938eval_client_expr_to_string(expr)
3939 char_u *expr;
3940{
3941 char_u *res;
3942 int save_dbl = debug_break_level;
3943 int save_ro = redir_off;
3944
3945 debug_break_level = -1;
3946 redir_off = 0;
3947 ++emsg_skip;
3948
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003949 res = eval_to_string(expr, NULL, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003950
3951 debug_break_level = save_dbl;
3952 redir_off = save_ro;
3953 --emsg_skip;
3954
Bram Moolenaar63a121b2005-12-11 21:36:39 +00003955 /* A client can tell us to redraw, but not to display the cursor, so do
3956 * that here. */
3957 setcursor();
3958 out_flush();
3959#ifdef FEAT_GUI
3960 if (gui.in_use)
3961 gui_update_cursor(FALSE, FALSE);
3962#endif
3963
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003964 return res;
3965}
3966
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003967/*
3968 * If conversion is needed, convert "data" from "client_enc" to 'encoding' and
3969 * return an allocated string. Otherwise return "data".
3970 * "*tofree" is set to the result when it needs to be freed later.
3971 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003972 char_u *
3973serverConvert(client_enc, data, tofree)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003974 char_u *client_enc UNUSED;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003975 char_u *data;
3976 char_u **tofree;
3977{
3978 char_u *res = data;
3979
3980 *tofree = NULL;
3981# ifdef FEAT_MBYTE
3982 if (client_enc != NULL && p_enc != NULL)
3983 {
3984 vimconv_T vimconv;
3985
3986 vimconv.vc_type = CONV_NONE;
3987 if (convert_setup(&vimconv, client_enc, p_enc) != FAIL
3988 && vimconv.vc_type != CONV_NONE)
3989 {
3990 res = string_convert(&vimconv, data, NULL);
3991 if (res == NULL)
3992 res = data;
3993 else
3994 *tofree = res;
3995 }
3996 convert_setup(&vimconv, NULL, NULL);
3997 }
3998# endif
3999 return res;
4000}
4001
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004002
4003/*
4004 * Make our basic server name: use the specified "arg" if given, otherwise use
4005 * the tail of the command "cmd" we were started with.
4006 * Return the name in allocated memory. This doesn't include a serial number.
4007 */
4008 static char_u *
4009serverMakeName(arg, cmd)
4010 char_u *arg;
4011 char *cmd;
4012{
4013 char_u *p;
4014
4015 if (arg != NULL && *arg != NUL)
4016 p = vim_strsave_up(arg);
4017 else
4018 {
4019 p = vim_strsave_up(gettail((char_u *)cmd));
4020 /* Remove .exe or .bat from the name. */
4021 if (p != NULL && vim_strchr(p, '.') != NULL)
4022 *vim_strchr(p, '.') = NUL;
4023 }
4024 return p;
4025}
4026#endif /* FEAT_CLIENTSERVER */
4027
4028/*
4029 * When FEAT_FKMAP is defined, also compile the Farsi source code.
4030 */
4031#if defined(FEAT_FKMAP) || defined(PROTO)
4032# include "farsi.c"
4033#endif
4034
4035/*
4036 * When FEAT_ARABIC is defined, also compile the Arabic source code.
4037 */
4038#if defined(FEAT_ARABIC) || defined(PROTO)
4039# include "arabic.c"
4040#endif