blob: d1702fe57051598de3963705a5ace48dfaec6fee [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 Moolenaarb05b10a2011-03-22 18:10:45 +010095#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
Bram Moolenaarb4210b32004-06-13 14:51:16 +000096static int file_owned __ARGS((char *fname));
97#endif
98static void mainerr __ARGS((int, char_u *));
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010099#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000100static void main_msg __ARGS((char *s));
101static void usage __ARGS((void));
102static int get_number_arg __ARGS((char_u *p, int *idx, int def));
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100103# if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000104static void init_locale __ARGS((void));
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100105# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000106static void parse_command_name __ARGS((mparm_T *parmp));
107static void early_arg_scan __ARGS((mparm_T *parmp));
108static void command_line_scan __ARGS((mparm_T *parmp));
109static void check_tty __ARGS((mparm_T *parmp));
110static void read_stdin __ARGS((void));
111static void create_windows __ARGS((mparm_T *parmp));
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100112# ifdef FEAT_WINDOWS
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000113static void edit_buffers __ARGS((mparm_T *parmp));
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100114# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000115static void exe_pre_commands __ARGS((mparm_T *parmp));
116static void exe_commands __ARGS((mparm_T *parmp));
Bram Moolenaar58d98232005-07-23 22:25:46 +0000117static void source_startup_scripts __ARGS((mparm_T *parmp));
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000118static void main_start_gui __ARGS((void));
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100119# if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000120static void check_swap_exists_action __ARGS((void));
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100121# endif
122# if defined(FEAT_CLIENTSERVER) || defined(PROTO)
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000123static void exec_on_server __ARGS((mparm_T *parmp));
124static void prepare_server __ARGS((mparm_T *parmp));
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000125static void cmdsrv_main __ARGS((int *argc, char **argv, char_u *serverName_arg, char_u **serverStr));
126static char_u *serverMakeName __ARGS((char_u *arg, char *cmd));
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100127# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000128#endif
129
130
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000131/*
132 * Different types of error messages.
133 */
134static char *(main_errors[]) =
135{
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000136 N_("Unknown option argument"),
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000137#define ME_UNKNOWN_OPTION 0
138 N_("Too many edit arguments"),
139#define ME_TOO_MANY_ARGS 1
140 N_("Argument missing after"),
141#define ME_ARG_MISSING 2
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000142 N_("Garbage after option argument"),
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000143#define ME_GARBAGE 3
144 N_("Too many \"+command\", \"-c command\" or \"--cmd command\" arguments"),
145#define ME_EXTRA_CMD 4
146 N_("Invalid argument for"),
147#define ME_INVALID_ARG 5
148};
149
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100150#ifndef NO_VIM_MAIN /* skip this for unittests */
151#ifndef PROTO /* don't want a prototype for main() */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000152 int
153# ifdef VIMDLL
154_export
155# endif
156# ifdef FEAT_GUI_MSWIN
157# ifdef __BORLANDC__
158_cdecl
159# endif
160VimMain
161# else
162main
163# endif
164(argc, argv)
165 int argc;
166 char **argv;
167{
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000168 char_u *fname = NULL; /* file name from command line */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000169 mparm_T params; /* various parameters passed between
170 * main() and other functions. */
Bram Moolenaar3f269672009-11-03 11:11:11 +0000171#ifdef STARTUPTIME
172 int i;
173#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000174
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000175 /*
176 * Do any system-specific initialisations. These can NOT use IObuff or
177 * NameBuff. Thus emsg2() cannot be called!
178 */
179 mch_early_init();
180
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000181 /* Many variables are in "params" so that we can pass them to invoked
182 * functions without a lot of arguments. "argc" and "argv" are also
183 * copied, so that they can be changed. */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000184 vim_memset(&params, 0, sizeof(params));
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000185 params.argc = argc;
186 params.argv = argv;
187 params.want_full_screen = TRUE;
188#ifdef FEAT_EVAL
189 params.use_debug_break_level = -1;
190#endif
191#ifdef FEAT_WINDOWS
192 params.window_count = -1;
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000193#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +0000194
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000195#ifdef FEAT_TCL
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000196 vim_tcl_init(params.argv[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000197#endif
198
199#ifdef MEM_PROFILE
200 atexit(vim_mem_profile_dump);
201#endif
202
203#ifdef STARTUPTIME
Bram Moolenaar3f269672009-11-03 11:11:11 +0000204 for (i = 1; i < argc; ++i)
205 {
Bram Moolenaaref94eec2009-11-11 13:22:11 +0000206 if (STRICMP(argv[i], "--startuptime") == 0 && i + 1 < argc)
Bram Moolenaar3f269672009-11-03 11:11:11 +0000207 {
Bram Moolenaaref94eec2009-11-11 13:22:11 +0000208 time_fd = mch_fopen(argv[i + 1], "a");
Bram Moolenaar3f269672009-11-03 11:11:11 +0000209 TIME_MSG("--- VIM STARTING ---");
210 break;
211 }
212 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000213#endif
Bram Moolenaarca003e12006-03-17 23:19:38 +0000214 starttime = time(NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000215
216#ifdef __EMX__
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000217 _wildcard(&params.argc, &params.argv);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000218#endif
219
220#ifdef FEAT_MBYTE
221 (void)mb_init(); /* init mb_bytelen_tab[] to ones */
222#endif
Bram Moolenaardcaf10e2005-01-21 11:55:25 +0000223#ifdef FEAT_EVAL
224 eval_init(); /* init global variables */
225#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000226
227#ifdef __QNXNTO__
228 qnx_init(); /* PhAttach() for clipboard, (and gui) */
229#endif
230
231#ifdef MAC_OS_CLASSIC
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000232 /* Prepare for possibly starting GUI sometime */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000233 /* Macintosh needs this before any memory is allocated. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000234 gui_prepare(&params.argc, params.argv);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000235 TIME_MSG("GUI prepared");
236#endif
237
238 /* Init the table of Normal mode commands. */
239 init_normal_cmds();
240
241#if defined(HAVE_DATE_TIME) && defined(VMS) && defined(VAXC)
Bram Moolenaar58d98232005-07-23 22:25:46 +0000242 make_version(); /* Construct the long version string. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000243#endif
244
245 /*
246 * Allocate space for the generic buffers (needed for set_init_1() and
247 * EMSG2()).
248 */
249 if ((IObuff = alloc(IOSIZE)) == NULL
250 || (NameBuff = alloc(MAXPATHL)) == NULL)
251 mch_exit(0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000252 TIME_MSG("Allocated generic buffers");
253
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000254#ifdef NBDEBUG
255 /* Wait a moment for debugging NetBeans. Must be after allocating
256 * NameBuff. */
257 nbdebug_log_init("SPRO_GVIM_DEBUG", "SPRO_GVIM_DLEVEL");
258 nbdebug_wait(WT_ENV | WT_WAIT | WT_STOP, "SPRO_GVIM_WAIT", 20);
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000259 TIME_MSG("NetBeans debug wait");
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000260#endif
261
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000262#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
263 /*
264 * Setup to use the current locale (for ctype() and many other things).
265 * NOTE: Translated messages with encodings other than latin1 will not
266 * work until set_init_1() has been called!
267 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000268 init_locale();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000269 TIME_MSG("locale set");
270#endif
271
272#ifdef FEAT_GUI
273 gui.dofork = TRUE; /* default is to use fork() */
274#endif
275
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000276 /*
Bram Moolenaar58d98232005-07-23 22:25:46 +0000277 * Do a first scan of the arguments in "argv[]":
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000278 * -display or --display
Bram Moolenaar58d98232005-07-23 22:25:46 +0000279 * --server...
280 * --socketid
Bram Moolenaar78e17622007-08-30 10:26:19 +0000281 * --windowid
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000282 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000283 early_arg_scan(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000284
285#ifdef FEAT_SUN_WORKSHOP
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000286 findYourself(params.argv[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000287#endif
288#if defined(FEAT_GUI) && !defined(MAC_OS_CLASSIC)
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000289 /* Prepare for possibly starting GUI sometime */
290 gui_prepare(&params.argc, params.argv);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000291 TIME_MSG("GUI prepared");
292#endif
293
294#ifdef FEAT_CLIPBOARD
295 clip_init(FALSE); /* Initialise clipboard stuff */
296 TIME_MSG("clipboard setup");
297#endif
298
299 /*
300 * Check if we have an interactive window.
301 * On the Amiga: If there is no window, we open one with a newcli command
302 * (needed for :! to * work). mch_check_win() will also handle the -d or
303 * -dev argument.
304 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000305 params.stdout_isatty = (mch_check_win(params.argc, params.argv) != FAIL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000306 TIME_MSG("window checked");
307
308 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000309 * Allocate the first window and buffer.
310 * Can't do anything without it, exit when it fails.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000311 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000312 if (win_alloc_first() == FAIL)
313 mch_exit(0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000314
315 init_yank(); /* init yank buffers */
316
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000317 alist_init(&global_alist); /* Init the argument list to empty. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000318
319 /*
320 * Set the default values for the options.
321 * NOTE: Non-latin1 translated messages are working only after this,
322 * because this is where "has_mbyte" will be set, which is used by
323 * msg_outtrans_len_attr().
324 * First find out the home directory, needed to expand "~" in options.
325 */
326 init_homedir(); /* find real value of $HOME */
327 set_init_1();
328 TIME_MSG("inits 1");
329
330#ifdef FEAT_EVAL
331 set_lang_var(); /* set v:lang and v:ctype */
332#endif
333
334#ifdef FEAT_CLIENTSERVER
335 /*
336 * Do the client-server stuff, unless "--servername ''" was used.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000337 * This may exit Vim if the command was sent to the server.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000338 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000339 exec_on_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000340#endif
341
342 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000343 * Figure out the way to work from the command name argv[0].
344 * "vimdiff" starts diff mode, "rvim" sets "restricted", etc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000345 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000346 parse_command_name(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000347
348 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000349 * Process the command line arguments. File names are put in the global
350 * argument list "global_alist".
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000351 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000352 command_line_scan(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000353 TIME_MSG("parsing arguments");
354
355 /*
356 * On some systems, when we compile with the GUI, we always use it. On Mac
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000357 * there is no terminal version, and on Windows we can't fork one off with
358 * :gui.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000359 */
360#ifdef ALWAYS_USE_GUI
361 gui.starting = TRUE;
362#else
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000363# if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000364 /*
365 * Check if the GUI can be started. Reset gui.starting if not.
366 * Don't know about other systems, stay on the safe side and don't check.
367 */
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000368 if (gui.starting)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000369 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000370 if (gui_init_check() == FAIL)
371 {
372 gui.starting = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000373
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000374 /* When running "evim" or "gvim -y" we need the menus, exit if we
375 * don't have them. */
376 if (params.evim_mode)
377 mch_exit(1);
378 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000379 }
380# endif
381#endif
382
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000383 if (GARGCOUNT > 0)
384 {
385#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
386 /*
387 * Expand wildcards in file names.
388 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000389 if (!params.literal)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000390 {
391 /* Temporarily add '(' and ')' to 'isfname'. These are valid
392 * filename characters but are excluded from 'isfname' to make
393 * "gf" work on a file name in parenthesis (e.g.: see vim.h). */
394 do_cmdline_cmd((char_u *)":set isf+=(,)");
Bram Moolenaar86b68352004-12-27 21:59:20 +0000395 alist_expand(NULL, 0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000396 do_cmdline_cmd((char_u *)":set isf&");
397 }
398#endif
399 fname = alist_name(&GARGLIST[0]);
400 }
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000401
402#if defined(WIN32) && defined(FEAT_MBYTE)
403 {
404 extern void set_alist_count(void);
405
406 /* Remember the number of entries in the argument list. If it changes
407 * we don't react on setting 'encoding'. */
408 set_alist_count();
409 }
410#endif
411
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000412#ifdef MSWIN
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000413 if (GARGCOUNT == 1 && params.full_path)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000414 {
415 /*
416 * If there is one filename, fully qualified, we have very probably
417 * been invoked from explorer, so change to the file's directory.
418 * Hint: to avoid this when typing a command use a forward slash.
419 * If the cd fails, it doesn't matter.
420 */
421 (void)vim_chdirfile(fname);
422 }
423#endif
424 TIME_MSG("expanding arguments");
425
426#ifdef FEAT_DIFF
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000427 if (params.diff_mode && params.window_count == -1)
428 params.window_count = 0; /* open up to 3 windows */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000429#endif
430
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000431 /* Don't redraw until much later. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000432 ++RedrawingDisabled;
433
434 /*
435 * When listing swap file names, don't do cursor positioning et. al.
436 */
437 if (recoverymode && fname == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000438 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000439
440 /*
441 * When certain to start the GUI, don't check capabilities of terminal.
442 * For GTK we can't be sure, but when started from the desktop it doesn't
443 * make sense to try using a terminal.
444 */
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000445#if defined(ALWAYS_USE_GUI) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000446 if (gui.starting
447# ifdef FEAT_GUI_GTK
448 && !isatty(2)
449# endif
450 )
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000451 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000452#endif
453
454#if defined(FEAT_GUI_MAC) && defined(MACOS_X_UNIX)
455 /* When the GUI is started from Finder, need to display messages in a
456 * message box. isatty(2) returns TRUE anyway, thus we need to check the
457 * name to know we're not started from a terminal. */
458 if (gui.starting && (!isatty(2) || strcmp("/dev/console", ttyname(2)) == 0))
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000459 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000460 params.want_full_screen = FALSE;
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000461
462 /* Avoid always using "/" as the current directory. Note that when
463 * started from Finder the arglist will be filled later in
464 * HandleODocAE() and "fname" will be NULL. */
465 if (getcwd((char *)NameBuff, MAXPATHL) != NULL
466 && STRCMP(NameBuff, "/") == 0)
467 {
468 if (fname != NULL)
469 (void)vim_chdirfile(fname);
470 else
471 {
472 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
473 vim_chdir(NameBuff);
474 }
475 }
476 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000477#endif
478
479 /*
480 * mch_init() sets up the terminal (window) for use. This must be
481 * done after resetting full_screen, otherwise it may move the cursor
482 * (MSDOS).
483 * Note that we may use mch_exit() before mch_init()!
484 */
485 mch_init();
486 TIME_MSG("shell init");
487
488#ifdef USE_XSMP
489 /*
490 * For want of anywhere else to do it, try to connect to xsmp here.
491 * Fitting it in after gui_mch_init, but before gui_init (via termcapinit).
492 * Hijacking -X 'no X connection' to also disable XSMP connection as that
493 * has a similar delay upon failure.
494 * Only try if SESSION_MANAGER is set to something non-null.
495 */
496 if (!x_no_connect)
497 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000498 char *p = getenv("SESSION_MANAGER");
499
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000500 if (p != NULL && *p != NUL)
501 {
502 xsmp_init();
503 TIME_MSG("xsmp init");
504 }
505 }
506#endif
507
508 /*
509 * Print a warning if stdout is not a terminal.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000510 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000511 check_tty(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000512
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000513 /* This message comes before term inits, but after setting "silent_mode"
514 * when the input is not a tty. */
515 if (GARGCOUNT > 1 && !silent_mode)
516 printf(_("%d files to edit\n"), GARGCOUNT);
517
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000518 if (params.want_full_screen && !silent_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000519 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000520 termcapinit(params.term); /* set terminal name and get terminal
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000521 capabilities (will set full_screen) */
522 screen_start(); /* don't know where cursor is now */
523 TIME_MSG("Termcap init");
524 }
525
526 /*
527 * Set the default values for the options that use Rows and Columns.
528 */
529 ui_get_shellsize(); /* inits Rows and Columns */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000530 win_init_size();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000531#ifdef FEAT_DIFF
532 /* Set the 'diff' option now, so that it can be checked for in a .vimrc
533 * file. There is no buffer yet though. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000534 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000535 diff_win_options(firstwin, FALSE);
536#endif
537
538 cmdline_row = Rows - p_ch;
539 msg_row = cmdline_row;
540 screenalloc(FALSE); /* allocate screen buffers */
541 set_init_2();
542 TIME_MSG("inits 2");
543
544 msg_scroll = TRUE;
545 no_wait_return = TRUE;
546
547 init_mappings(); /* set up initial mappings */
548
549 init_highlight(TRUE, FALSE); /* set the default highlight groups */
550 TIME_MSG("init highlight");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000551
552#ifdef FEAT_EVAL
553 /* Set the break level after the terminal is initialized. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000554 debug_break_level = params.use_debug_break_level;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000555#endif
556
Bram Moolenaar58d98232005-07-23 22:25:46 +0000557 /* Execute --cmd arguments. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000558 exe_pre_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000559
Bram Moolenaar58d98232005-07-23 22:25:46 +0000560 /* Source startup scripts. */
561 source_startup_scripts(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000562
563#ifdef FEAT_EVAL
564 /*
565 * Read all the plugin files.
566 * Only when compiled with +eval, since most plugins need it.
567 */
568 if (p_lpl)
569 {
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000570# ifdef VMS /* Somehow VMS doesn't handle the "**". */
571 source_runtime((char_u *)"plugin/*.vim", TRUE);
572# else
Bram Moolenaar07d4d732005-10-03 22:04:08 +0000573 source_runtime((char_u *)"plugin/**/*.vim", TRUE);
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000574# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000575 TIME_MSG("loading plugins");
576 }
577#endif
578
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000579#ifdef FEAT_DIFF
580 /* Decide about window layout for diff mode after reading vimrc. */
581 if (params.diff_mode && params.window_layout == 0)
582 {
583 if (diffopt_horizontal())
584 params.window_layout = WIN_HOR; /* use horizontal split */
585 else
586 params.window_layout = WIN_VER; /* use vertical split */
587 }
588#endif
589
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000590 /*
591 * Recovery mode without a file name: List swap files.
592 * This uses the 'dir' option, therefore it must be after the
593 * initializations.
594 */
595 if (recoverymode && fname == NULL)
596 {
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200597 recover_names(NULL, TRUE, 0, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000598 mch_exit(0);
599 }
600
601 /*
602 * Set a few option defaults after reading .vimrc files:
603 * 'title' and 'icon', Unix: 'shellpipe' and 'shellredir'.
604 */
605 set_init_3();
606 TIME_MSG("inits 3");
607
608 /*
609 * "-n" argument: Disable swap file by setting 'updatecount' to 0.
610 * Note that this overrides anything from a vimrc file.
611 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000612 if (params.no_swap_file)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000613 p_uc = 0;
614
615#ifdef FEAT_FKMAP
616 if (curwin->w_p_rl && p_altkeymap)
617 {
618 p_hkmap = FALSE; /* Reset the Hebrew keymap mode */
619# ifdef FEAT_ARABIC
620 curwin->w_p_arab = FALSE; /* Reset the Arabic keymap mode */
621# endif
622 p_fkmap = TRUE; /* Set the Farsi keymap mode */
623 }
624#endif
625
626#ifdef FEAT_GUI
627 if (gui.starting)
628 {
629#if defined(UNIX) || defined(VMS)
630 /* When something caused a message from a vimrc script, need to output
631 * an extra newline before the shell prompt. */
632 if (did_emsg || msg_didout)
633 putchar('\n');
634#endif
635
636 gui_start(); /* will set full_screen to TRUE */
637 TIME_MSG("starting GUI");
638
639 /* When running "evim" or "gvim -y" we need the menus, exit if we
640 * don't have them. */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000641 if (!gui.in_use && params.evim_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000642 mch_exit(1);
643 }
644#endif
645
646#ifdef SPAWNO /* special MSDOS swapping library */
647 init_SPAWNO("", SWAP_ANY);
648#endif
649
650#ifdef FEAT_VIMINFO
651 /*
Bram Moolenaard812df62008-11-09 12:46:09 +0000652 * Read in registers, history etc, but not marks, from the viminfo file.
653 * This is where v:oldfiles gets filled.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000654 */
655 if (*p_viminfo != NUL)
656 {
Bram Moolenaard812df62008-11-09 12:46:09 +0000657 read_viminfo(NULL, VIF_WANT_INFO | VIF_GET_OLDFILES);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000658 TIME_MSG("reading viminfo");
659 }
660#endif
661
662#ifdef FEAT_QUICKFIX
663 /*
664 * "-q errorfile": Load the error file now.
665 * If the error file can't be read, exit before doing anything else.
666 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000667 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000668 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000669 if (params.use_ef != NULL)
670 set_string_option_direct((char_u *)"ef", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000671 params.use_ef, OPT_FREE, SID_CARG);
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200672 vim_snprintf((char *)IObuff, IOSIZE, "cfile %s", p_ef);
673 if (qf_init(NULL, p_ef, p_efm, TRUE, IObuff) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000674 {
675 out_char('\n');
676 mch_exit(3);
677 }
678 TIME_MSG("reading errorfile");
679 }
680#endif
681
682 /*
683 * Start putting things on the screen.
684 * Scroll screen down before drawing over it
685 * Clear screen now, so file message will not be cleared.
686 */
687 starting = NO_BUFFERS;
688 no_wait_return = FALSE;
689 if (!exmode_active)
690 msg_scroll = FALSE;
691
692#ifdef FEAT_GUI
693 /*
694 * This seems to be required to make callbacks to be called now, instead
695 * of after things have been put on the screen, which then may be deleted
696 * when getting a resize callback.
697 * For the Mac this handles putting files dropped on the Vim icon to
698 * global_alist.
699 */
700 if (gui.in_use)
701 {
702# ifdef FEAT_SUN_WORKSHOP
703 if (!usingSunWorkShop)
704# endif
705 gui_wait_for_chars(50L);
706 TIME_MSG("GUI delay");
707 }
708#endif
709
710#if defined(FEAT_GUI_PHOTON) && defined(FEAT_CLIPBOARD)
711 qnx_clip_init();
712#endif
713
Bram Moolenaarc8bbaa32010-07-14 16:54:21 +0200714#if defined(MACOS_X) && defined(FEAT_CLIPBOARD)
715 clip_init(TRUE);
716#endif
717
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000718#ifdef FEAT_XCLIPBOARD
719 /* Start using the X clipboard, unless the GUI was started. */
720# ifdef FEAT_GUI
721 if (!gui.in_use)
722# endif
723 {
724 setup_term_clip();
725 TIME_MSG("setup clipboard");
726 }
727#endif
728
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000729#ifdef FEAT_CLIENTSERVER
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000730 /* Prepare for being a Vim server. */
731 prepare_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000732#endif
733
734 /*
735 * If "-" argument given: Read file from stdin.
736 * Do this before starting Raw mode, because it may change things that the
737 * writing end of the pipe doesn't like, e.g., in case stdin and stderr
738 * are the same terminal: "cat | vim -".
739 * Using autocommands here may cause trouble...
740 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000741 if (params.edit_type == EDIT_STDIN && !recoverymode)
742 read_stdin();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000743
744#if defined(UNIX) || defined(VMS)
745 /* When switching screens and something caused a message from a vimrc
746 * script, need to output an extra newline on exit. */
747 if ((did_emsg || msg_didout) && *T_TI != NUL)
748 newline_on_exit = TRUE;
749#endif
750
751 /*
752 * When done something that is not allowed or error message call
753 * wait_return. This must be done before starttermcap(), because it may
754 * switch to another screen. It must be done after settmode(TMODE_RAW),
755 * because we want to react on a single key stroke.
756 * Call settmode and starttermcap here, so the T_KS and T_TI may be
Bram Moolenaar49325942007-05-10 19:19:59 +0000757 * defined by termcapinit and redefined in .exrc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000758 */
759 settmode(TMODE_RAW);
760 TIME_MSG("setting raw mode");
761
762 if (need_wait_return || msg_didany)
763 {
764 wait_return(TRUE);
765 TIME_MSG("waiting for return");
766 }
767
768 starttermcap(); /* start termcap if not done by wait_return() */
769 TIME_MSG("start termcap");
770
771#ifdef FEAT_MOUSE
772 setmouse(); /* may start using the mouse */
773#endif
774 if (scroll_region)
775 scroll_region_reset(); /* In case Rows changed */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000776 scroll_start(); /* may scroll the screen to the right position */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000777
778 /*
779 * Don't clear the screen when starting in Ex mode, unless using the GUI.
780 */
781 if (exmode_active
782#ifdef FEAT_GUI
783 && !gui.in_use
784#endif
785 )
786 must_redraw = CLEAR;
787 else
788 {
789 screenclear(); /* clear screen */
790 TIME_MSG("clearing screen");
791 }
792
793#ifdef FEAT_CRYPT
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000794 if (params.ask_for_key)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000795 {
Bram Moolenaar40e6a712010-05-16 22:32:54 +0200796 (void)blowfish_self_test();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000797 (void)get_crypt_key(TRUE, TRUE);
798 TIME_MSG("getting crypt key");
799 }
800#endif
801
802 no_wait_return = TRUE;
803
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000804 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000805 * Create the requested number of windows and edit buffers in them.
806 * Also does recovery if "recoverymode" set.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000807 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000808 create_windows(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000809 TIME_MSG("opening buffers");
810
Bram Moolenaar867a4b72007-03-18 20:51:46 +0000811#ifdef FEAT_EVAL
812 /* clear v:swapcommand */
813 set_vim_var_string(VV_SWAPCOMMAND, NULL, -1);
814#endif
815
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000816 /* Ex starts at last line of the file */
817 if (exmode_active)
818 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
819
820#ifdef FEAT_AUTOCMD
821 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
822 TIME_MSG("BufEnter autocommands");
823#endif
824 setpcmark();
825
826#ifdef FEAT_QUICKFIX
827 /*
828 * When started with "-q errorfile" jump to first error now.
829 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000830 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000831 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000832 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000833 TIME_MSG("jump to first error");
834 }
835#endif
836
837#ifdef FEAT_WINDOWS
838 /*
839 * If opened more than one window, start editing files in the other
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000840 * windows.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000841 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000842 edit_buffers(&params);
843#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000844
845#ifdef FEAT_DIFF
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000846 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000847 {
848 win_T *wp;
849
850 /* set options in each window for "vimdiff". */
851 for (wp = firstwin; wp != NULL; wp = wp->w_next)
852 diff_win_options(wp, TRUE);
853 }
854#endif
855
856 /*
857 * Shorten any of the filenames, but only when absolute.
858 */
859 shorten_fnames(FALSE);
860
861 /*
862 * Need to jump to the tag before executing the '-c command'.
863 * Makes "vim -c '/return' -t main" work.
864 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000865 if (params.tagname != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000866 {
Bram Moolenaar146522e2005-12-16 21:55:46 +0000867#if defined(HAS_SWAP_EXISTS_ACTION)
868 swap_exists_did_quit = FALSE;
869#endif
870
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000871 vim_snprintf((char *)IObuff, IOSIZE, "ta %s", params.tagname);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000872 do_cmdline_cmd(IObuff);
873 TIME_MSG("jumping to tag");
Bram Moolenaar146522e2005-12-16 21:55:46 +0000874
875#if defined(HAS_SWAP_EXISTS_ACTION)
876 /* If the user doesn't want to edit the file then we quit here. */
877 if (swap_exists_did_quit)
878 getout(1);
879#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000880 }
881
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000882 /* Execute any "+", "-c" and "-S" arguments. */
883 if (params.n_commands > 0)
884 exe_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000885
886 RedrawingDisabled = 0;
887 redraw_all_later(NOT_VALID);
888 no_wait_return = FALSE;
889 starting = 0;
890
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000891#ifdef FEAT_TERMRESPONSE
892 /* Requesting the termresponse is postponed until here, so that a "-c q"
893 * argument doesn't make it appear in the shell Vim was started from. */
894 may_req_termresponse();
895#endif
896
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000897 /* start in insert mode */
898 if (p_im)
899 need_start_insertmode = TRUE;
900
901#ifdef FEAT_AUTOCMD
902 apply_autocmds(EVENT_VIMENTER, NULL, NULL, FALSE, curbuf);
903 TIME_MSG("VimEnter autocommands");
904#endif
905
906#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
907 /* When a startup script or session file setup for diff'ing and
908 * scrollbind, sync the scrollbind now. */
909 if (curwin->w_p_diff && curwin->w_p_scb)
910 {
911 update_topline();
912 check_scrollbind((linenr_T)0, 0L);
913 TIME_MSG("diff scrollbinding");
914 }
915#endif
916
917#if defined(WIN3264) && !defined(FEAT_GUI_W32)
918 mch_set_winsize_now(); /* Allow winsize changes from now on */
919#endif
920
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000921#if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
922 /* When tab pages were created, may need to update the tab pages line and
923 * scrollbars. This is skipped while creating them. */
924 if (first_tabpage->tp_next != NULL)
925 {
926 out_flush();
927 gui_init_which_components(NULL);
928 gui_update_scrollbars(TRUE);
929 }
930 need_mouse_correct = TRUE;
931#endif
932
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000933 /* If ":startinsert" command used, stuff a dummy command to be able to
934 * call normal_cmd(), which will then start Insert mode. */
935 if (restart_edit != 0)
Bram Moolenaarebefac62005-12-28 22:39:57 +0000936 stuffcharReadbuff(K_NOP);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000937
938#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200939 if (netbeansArg != NULL && strncmp("-nb", netbeansArg, 3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200940 {
941# ifdef FEAT_GUI
Bram Moolenaar173c9852010-09-29 17:27:01 +0200942# if !defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK) \
Bram Moolenaar67c53842010-05-22 18:28:27 +0200943 && !defined(FEAT_GUI_W32)
944 if (gui.in_use)
945 {
946 mch_errmsg(_("netbeans is not supported with this GUI\n"));
947 mch_exit(2);
948 }
949# endif
950# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000951 /* Tell the client that it can start sending commands. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200952 netbeans_open(netbeansArg + 3, TRUE);
Bram Moolenaar67c53842010-05-22 18:28:27 +0200953 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000954#endif
955
956 TIME_MSG("before starting main loop");
957
958 /*
959 * Call the main command loop. This never returns.
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000960 * For embedded MzScheme the main_loop will be called by Scheme
961 * for proper stack tracking
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000962 */
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000963#ifndef FEAT_MZSCHEME
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000964 main_loop(FALSE, FALSE);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000965#else
966 mzscheme_main();
967#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000968
969 return 0;
970}
971#endif /* PROTO */
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100972#endif /* NO_VIM_MAIN */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000973
974/*
975 * Main loop: Execute Normal mode commands until exiting Vim.
976 * Also used to handle commands in the command-line window, until the window
977 * is closed.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000978 * Also used to handle ":visual" command after ":global": execute Normal mode
979 * commands, return when entering Ex mode. "noexmode" is TRUE then.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000980 */
981 void
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000982main_loop(cmdwin, noexmode)
983 int cmdwin; /* TRUE when working in the command-line window */
984 int noexmode; /* TRUE when return on entering Ex mode */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000985{
Bram Moolenaar225d32b2007-08-10 19:33:47 +0000986 oparg_T oa; /* operator arguments */
987 int previous_got_int = FALSE; /* "got_int" was TRUE */
Bram Moolenaarb2c03502010-07-02 20:20:09 +0200988#ifdef FEAT_CONCEAL
989 linenr_T conceal_old_cursor_line = 0;
990 linenr_T conceal_new_cursor_line = 0;
991 int conceal_update_lines = FALSE;
992#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000993
994#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
995 /* Setup to catch a terminating error from the X server. Just ignore
996 * it, restore the state and continue. This might not always work
997 * properly, but at least we don't exit unexpectedly when the X server
998 * exists while Vim is running in a console. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000999 if (!cmdwin && !noexmode && SETJMP(x_jump_env))
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001000 {
1001 State = NORMAL;
1002# ifdef FEAT_VISUAL
1003 VIsual_active = FALSE;
1004# endif
1005 got_int = TRUE;
1006 need_wait_return = FALSE;
1007 global_busy = FALSE;
1008 exmode_active = 0;
1009 skip_redraw = FALSE;
1010 RedrawingDisabled = 0;
1011 no_wait_return = 0;
Bram Moolenaar7701c242011-10-04 16:43:53 +02001012 vgetc_busy = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001013# ifdef FEAT_EVAL
1014 emsg_skip = 0;
1015# endif
1016 emsg_off = 0;
1017# ifdef FEAT_MOUSE
1018 setmouse();
1019# endif
1020 settmode(TMODE_RAW);
1021 starttermcap();
1022 scroll_start();
1023 redraw_later_clear();
1024 }
1025#endif
1026
1027 clear_oparg(&oa);
1028 while (!cmdwin
1029#ifdef FEAT_CMDWIN
1030 || cmdwin_result == 0
1031#endif
1032 )
1033 {
1034 if (stuff_empty())
1035 {
1036 did_check_timestamps = FALSE;
1037 if (need_check_timestamps)
1038 check_timestamps(FALSE);
1039 if (need_wait_return) /* if wait_return still needed ... */
1040 wait_return(FALSE); /* ... call it now */
1041 if (need_start_insertmode && goto_im()
1042#ifdef FEAT_VISUAL
1043 && !VIsual_active
1044#endif
1045 )
1046 {
1047 need_start_insertmode = FALSE;
1048 stuffReadbuff((char_u *)"i"); /* start insert mode next */
1049 /* skip the fileinfo message now, because it would be shown
1050 * after insert mode finishes! */
1051 need_fileinfo = FALSE;
1052 }
1053 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001054
1055 /* Reset "got_int" now that we got back to the main loop. Except when
1056 * inside a ":g/pat/cmd" command, then the "got_int" needs to abort
1057 * the ":g" command.
1058 * For ":g/pat/vi" we reset "got_int" when used once. When used
1059 * a second time we go back to Ex mode and abort the ":g" command. */
1060 if (got_int)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001061 {
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001062 if (noexmode && global_busy && !exmode_active && previous_got_int)
1063 {
1064 /* Typed two CTRL-C in a row: go back to ex mode as if "Q" was
1065 * used and keep "got_int" set, so that it aborts ":g". */
1066 exmode_active = EXMODE_NORMAL;
1067 State = NORMAL;
1068 }
1069 else if (!global_busy || !exmode_active)
1070 {
1071 if (!quit_more)
1072 (void)vgetc(); /* flush all buffers */
1073 got_int = FALSE;
1074 }
1075 previous_got_int = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001076 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001077 else
1078 previous_got_int = FALSE;
1079
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001080 if (!exmode_active)
1081 msg_scroll = FALSE;
1082 quit_more = FALSE;
1083
1084 /*
1085 * If skip redraw is set (for ":" in wait_return()), don't redraw now.
1086 * If there is nothing in the stuff_buffer or do_redraw is TRUE,
1087 * update cursor and redraw.
1088 */
1089 if (skip_redraw || exmode_active)
1090 skip_redraw = FALSE;
1091 else if (do_redraw || stuff_empty())
1092 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001093#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001094 /* Trigger CursorMoved if the cursor moved. */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001095 if (!finish_op && (
1096# ifdef FEAT_AUTOCMD
1097 has_cursormoved()
1098# endif
1099# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
1100 ||
1101# endif
1102# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001103 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001104# endif
1105 )
1106 && !equalpos(last_cursormoved, curwin->w_cursor))
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001107 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001108# ifdef FEAT_AUTOCMD
1109 if (has_cursormoved())
1110 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL,
1111 FALSE, curbuf);
1112# endif
1113# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001114 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001115 {
1116 conceal_old_cursor_line = last_cursormoved.lnum;
1117 conceal_new_cursor_line = curwin->w_cursor.lnum;
1118 conceal_update_lines = TRUE;
1119 }
1120# endif
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001121 last_cursormoved = curwin->w_cursor;
1122 }
1123#endif
1124
Bram Moolenaar33aec762006-01-22 23:30:12 +00001125#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
1126 /* Scroll-binding for diff mode may have been postponed until
1127 * here. Avoids doing it for every change. */
1128 if (diff_need_scrollbind)
1129 {
1130 check_scrollbind((linenr_T)0, 0L);
1131 diff_need_scrollbind = FALSE;
1132 }
1133#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001134#if defined(FEAT_FOLDING) && defined(FEAT_VISUAL)
1135 /* Include a closed fold completely in the Visual area. */
1136 foldAdjustVisual();
1137#endif
1138#ifdef FEAT_FOLDING
1139 /*
1140 * When 'foldclose' is set, apply 'foldlevel' to folds that don't
1141 * contain the cursor.
1142 * When 'foldopen' is "all", open the fold(s) under the cursor.
1143 * This may mark the window for redrawing.
1144 */
1145 if (hasAnyFolding(curwin) && !char_avail())
1146 {
1147 foldCheckClose();
1148 if (fdo_flags & FDO_ALL)
1149 foldOpenCursor();
1150 }
1151#endif
1152
1153 /*
1154 * Before redrawing, make sure w_topline is correct, and w_leftcol
1155 * if lines don't wrap, and w_skipcol if lines wrap.
1156 */
1157 update_topline();
1158 validate_cursor();
1159
1160#ifdef FEAT_VISUAL
1161 if (VIsual_active)
1162 update_curbuf(INVERTED);/* update inverted part */
1163 else
1164#endif
1165 if (must_redraw)
1166 update_screen(0);
1167 else if (redraw_cmdline || clear_cmdline)
1168 showmode();
1169#ifdef FEAT_WINDOWS
1170 redraw_statuslines();
1171#endif
1172#ifdef FEAT_TITLE
1173 if (need_maketitle)
1174 maketitle();
1175#endif
1176 /* display message after redraw */
1177 if (keep_msg != NULL)
1178 {
1179 char_u *p;
1180
1181 /* msg_attr_keep() will set keep_msg to NULL, must free the
1182 * string here. */
1183 p = keep_msg;
Bram Moolenaar238a5642006-02-21 22:12:05 +00001184 keep_msg = NULL;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001185 msg_attr(p, keep_msg_attr);
1186 vim_free(p);
1187 }
1188 if (need_fileinfo) /* show file info after redraw */
1189 {
1190 fileinfo(FALSE, TRUE, FALSE);
1191 need_fileinfo = FALSE;
1192 }
1193
1194 emsg_on_display = FALSE; /* can delete error message now */
1195 did_emsg = FALSE;
1196 msg_didany = FALSE; /* reset lines_left in msg_start() */
Bram Moolenaar661b1822005-07-28 22:36:45 +00001197 may_clear_sb_text(); /* clear scroll-back text on next msg */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001198 showruler(FALSE);
1199
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001200# if defined(FEAT_CONCEAL)
1201 if (conceal_update_lines
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001202 && (conceal_old_cursor_line != conceal_new_cursor_line
1203 || conceal_cursor_line(curwin)
1204 || need_cursor_line_redraw))
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001205 {
Bram Moolenaarc2b4c622011-02-15 16:29:59 +01001206 if (conceal_old_cursor_line != conceal_new_cursor_line
1207 && conceal_old_cursor_line
1208 <= curbuf->b_ml.ml_line_count)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001209 update_single_line(curwin, conceal_old_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001210 update_single_line(curwin, conceal_new_cursor_line);
1211 curwin->w_valid &= ~VALID_CROW;
1212 }
1213# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001214 setcursor();
1215 cursor_on();
1216
1217 do_redraw = FALSE;
Bram Moolenaar3f269672009-11-03 11:11:11 +00001218
1219#ifdef STARTUPTIME
1220 /* Now that we have drawn the first screen all the startup stuff
1221 * has been done, close any file for startup messages. */
1222 if (time_fd != NULL)
1223 {
1224 TIME_MSG("first screen update");
1225 TIME_MSG("--- VIM STARTED ---");
1226 fclose(time_fd);
1227 time_fd = NULL;
1228 }
1229#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001230 }
1231#ifdef FEAT_GUI
1232 if (need_mouse_correct)
1233 gui_mouse_correct();
1234#endif
1235
1236 /*
1237 * Update w_curswant if w_set_curswant has been set.
1238 * Postponed until here to avoid computing w_virtcol too often.
1239 */
1240 update_curswant();
1241
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001242#ifdef FEAT_EVAL
1243 /*
1244 * May perform garbage collection when waiting for a character, but
1245 * only at the very toplevel. Otherwise we may be using a List or
1246 * Dict internally somewhere.
1247 * "may_garbage_collect" is reset in vgetc() which is invoked through
1248 * do_exmode() and normal_cmd().
1249 */
1250 may_garbage_collect = (!cmdwin && !noexmode);
1251#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001252 /*
1253 * If we're invoked as ex, do a round of ex commands.
1254 * Otherwise, get and execute a normal mode command.
1255 */
1256 if (exmode_active)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001257 {
1258 if (noexmode) /* End of ":global/path/visual" commands */
1259 return;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001260 do_exmode(exmode_active == EXMODE_VIM);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001261 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001262 else
1263 normal_cmd(&oa, TRUE);
1264 }
1265}
1266
1267
1268#if defined(USE_XSMP) || defined(FEAT_GUI_MSWIN) || defined(PROTO)
1269/*
1270 * Exit, but leave behind swap files for modified buffers.
1271 */
1272 void
1273getout_preserve_modified(exitval)
1274 int exitval;
1275{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001276# if defined(SIGHUP) && defined(SIG_IGN)
1277 /* Ignore SIGHUP, because a dropped connection causes a read error, which
1278 * makes Vim exit and then handling SIGHUP causes various reentrance
1279 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001280 signal(SIGHUP, SIG_IGN);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001281# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001282
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001283 ml_close_notmod(); /* close all not-modified buffers */
1284 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
1285 ml_close_all(FALSE); /* close all memfiles, without deleting */
1286 getout(exitval); /* exit Vim properly */
1287}
1288#endif
1289
1290
1291/* Exit properly */
1292 void
1293getout(exitval)
1294 int exitval;
1295{
1296#ifdef FEAT_AUTOCMD
1297 buf_T *buf;
1298 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001299 tabpage_T *tp, *next_tp;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001300#endif
1301
1302 exiting = TRUE;
1303
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001304 /* When running in Ex mode an error causes us to exit with a non-zero exit
1305 * code. POSIX requires this, although it's not 100% clear from the
1306 * standard. */
1307 if (exmode_active)
1308 exitval += ex_exitval;
1309
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001310 /* Position the cursor on the last screen line, below all the text */
1311#ifdef FEAT_GUI
1312 if (!gui.in_use)
1313#endif
1314 windgoto((int)Rows - 1, 0);
1315
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +00001316#if defined(FEAT_EVAL) || defined(FEAT_SYN_HL)
1317 /* Optionally print hashtable efficiency. */
1318 hash_debug_results();
1319#endif
1320
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001321#ifdef FEAT_GUI
1322 msg_didany = FALSE;
1323#endif
1324
1325#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001326 if (get_vim_var_nr(VV_DYING) <= 1)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001327 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001328 /* Trigger BufWinLeave for all windows, but only once per buffer. */
1329# if defined FEAT_WINDOWS
1330 for (tp = first_tabpage; tp != NULL; tp = next_tp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001331 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001332 next_tp = tp->tp_next;
1333 for (wp = (tp == curtab)
1334 ? firstwin : tp->tp_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001335 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001336 buf = wp->w_buffer;
1337 if (buf->b_changedtick != -1)
1338 {
1339 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname,
1340 buf->b_fname, FALSE, buf);
1341 buf->b_changedtick = -1; /* note that we did it already */
1342 /* start all over, autocommands may mess up the lists */
1343 next_tp = first_tabpage;
1344 break;
1345 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001346 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001347 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001348# else
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001349 apply_autocmds(EVENT_BUFWINLEAVE, curbuf, curbuf->b_fname,
1350 FALSE, curbuf);
Bram Moolenaarf740b292006-02-16 22:11:02 +00001351# endif
Bram Moolenaar33aec762006-01-22 23:30:12 +00001352
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001353 /* Trigger BufUnload for buffers that are loaded */
1354 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1355 if (buf->b_ml.ml_mfp != NULL)
1356 {
1357 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001358 FALSE, buf);
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001359 if (!buf_valid(buf)) /* autocmd may delete the buffer */
1360 break;
1361 }
1362 apply_autocmds(EVENT_VIMLEAVEPRE, NULL, NULL, FALSE, curbuf);
1363 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001364#endif
1365
1366#ifdef FEAT_VIMINFO
1367 if (*p_viminfo != NUL)
1368 /* Write out the registers, history, marks etc, to the viminfo file */
1369 write_viminfo(NULL, FALSE);
1370#endif
1371
1372#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001373 if (get_vim_var_nr(VV_DYING) <= 1)
1374 apply_autocmds(EVENT_VIMLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001375#endif
1376
Bram Moolenaar05159a02005-02-26 23:04:13 +00001377#ifdef FEAT_PROFILE
1378 profile_dump();
1379#endif
1380
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001381 if (did_emsg
1382#ifdef FEAT_GUI
1383 || (gui.in_use && msg_didany && p_verbose > 0)
1384#endif
1385 )
1386 {
1387 /* give the user a chance to read the (error) message */
1388 no_wait_return = FALSE;
1389 wait_return(FALSE);
1390 }
1391
1392#ifdef FEAT_AUTOCMD
1393 /* Position the cursor again, the autocommands may have moved it */
1394# ifdef FEAT_GUI
1395 if (!gui.in_use)
1396# endif
1397 windgoto((int)Rows - 1, 0);
1398#endif
1399
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001400#ifdef FEAT_LUA
1401 lua_end();
1402#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001403#ifdef FEAT_MZSCHEME
1404 mzscheme_end();
1405#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001406#ifdef FEAT_TCL
1407 tcl_end();
1408#endif
1409#ifdef FEAT_RUBY
1410 ruby_end();
1411#endif
1412#ifdef FEAT_PYTHON
1413 python_end();
1414#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001415#ifdef FEAT_PYTHON3
1416 python3_end();
1417#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001418#ifdef FEAT_PERL
1419 perl_end();
1420#endif
1421#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
1422 iconv_end();
1423#endif
1424#ifdef FEAT_NETBEANS_INTG
1425 netbeans_end();
1426#endif
Bram Moolenaar02b06312007-09-06 15:39:22 +00001427#ifdef FEAT_CSCOPE
1428 cs_end();
1429#endif
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001430#ifdef FEAT_EVAL
1431 if (garbage_collect_at_exit)
1432 garbage_collect();
1433#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001434
1435 mch_exit(exitval);
1436}
1437
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01001438#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001439/*
1440 * Get a (optional) count for a Vim argument.
1441 */
1442 static int
1443get_number_arg(p, idx, def)
1444 char_u *p; /* pointer to argument */
1445 int *idx; /* index in argument, is incremented */
1446 int def; /* default value */
1447{
1448 if (vim_isdigit(p[*idx]))
1449 {
1450 def = atoi((char *)&(p[*idx]));
1451 while (vim_isdigit(p[*idx]))
1452 *idx = *idx + 1;
1453 }
1454 return def;
1455}
1456
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001457#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1458/*
1459 * Setup to use the current locale (for ctype() and many other things).
1460 */
1461 static void
1462init_locale()
1463{
1464 setlocale(LC_ALL, "");
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001465
Bram Moolenaarc6af8122010-05-21 12:04:55 +02001466# ifdef FEAT_GUI_GTK
1467 /* Tell Gtk not to change our locale settings. */
1468 gtk_disable_setlocale();
1469# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001470# if defined(FEAT_FLOAT) && defined(LC_NUMERIC)
1471 /* Make sure strtod() uses a decimal point, not a comma. */
1472 setlocale(LC_NUMERIC, "C");
1473# endif
1474
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001475# ifdef WIN32
1476 /* Apparently MS-Windows printf() may cause a crash when we give it 8-bit
1477 * text while it's expecting text in the current locale. This call avoids
1478 * that. */
1479 setlocale(LC_CTYPE, "C");
1480# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001481
1482# ifdef FEAT_GETTEXT
1483 {
1484 int mustfree = FALSE;
1485 char_u *p;
1486
1487# ifdef DYNAMIC_GETTEXT
1488 /* Initialize the gettext library */
1489 dyn_libintl_init(NULL);
1490# endif
1491 /* expand_env() doesn't work yet, because chartab[] is not initialized
1492 * yet, call vim_getenv() directly */
1493 p = vim_getenv((char_u *)"VIMRUNTIME", &mustfree);
1494 if (p != NULL && *p != NUL)
1495 {
Bram Moolenaar1ad2f132007-06-19 18:27:18 +00001496 vim_snprintf((char *)NameBuff, MAXPATHL, "%s/lang", p);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001497 bindtextdomain(VIMPACKAGE, (char *)NameBuff);
1498 }
1499 if (mustfree)
1500 vim_free(p);
1501 textdomain(VIMPACKAGE);
1502 }
1503# endif
1504}
1505#endif
1506
1507/*
1508 * Check for: [r][e][g][vi|vim|view][diff][ex[im]]
1509 * If the executable name starts with "r" we disable shell commands.
1510 * If the next character is "e" we run in Easy mode.
1511 * If the next character is "g" we run the GUI version.
1512 * If the next characters are "view" we start in readonly mode.
1513 * If the next characters are "diff" or "vimdiff" we start in diff mode.
1514 * If the next characters are "ex" we start in Ex mode. If it's followed
1515 * by "im" use improved Ex mode.
1516 */
1517 static void
1518parse_command_name(parmp)
1519 mparm_T *parmp;
1520{
1521 char_u *initstr;
1522
1523 initstr = gettail((char_u *)parmp->argv[0]);
1524
1525#ifdef MACOS_X_UNIX
1526 /* An issue has been seen when launching Vim in such a way that
1527 * $PWD/$ARGV[0] or $ARGV[0] is not the absolute path to the
1528 * executable or a symbolic link of it. Until this issue is resolved
1529 * we prohibit the GUI from being used.
1530 */
1531 if (STRCMP(initstr, parmp->argv[0]) == 0)
1532 disallow_gui = TRUE;
1533
1534 /* TODO: On MacOS X default to gui if argv[0] ends in:
Bram Moolenaar27dc1952006-03-15 23:06:44 +00001535 * /Vim.app/Contents/MacOS/Vim */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001536#endif
1537
1538#ifdef FEAT_EVAL
1539 set_vim_var_string(VV_PROGNAME, initstr, -1);
1540#endif
1541
1542 if (TOLOWER_ASC(initstr[0]) == 'r')
1543 {
1544 restricted = TRUE;
1545 ++initstr;
1546 }
1547
Bram Moolenaarad249fb2010-05-07 16:35:04 +02001548 /* Use evim mode for "evim" and "egvim", not for "editor". */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001549 if (TOLOWER_ASC(initstr[0]) == 'e'
1550 && (TOLOWER_ASC(initstr[1]) == 'v'
1551 || TOLOWER_ASC(initstr[1]) == 'g'))
1552 {
1553#ifdef FEAT_GUI
1554 gui.starting = TRUE;
1555#endif
1556 parmp->evim_mode = TRUE;
1557 ++initstr;
1558 }
1559
Bram Moolenaar806875d2008-09-18 18:57:10 +00001560 /* "gvim" starts the GUI. Also accept "Gvim" for MS-Windows. */
1561 if (TOLOWER_ASC(initstr[0]) == 'g')
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001562 {
1563 main_start_gui();
1564#ifdef FEAT_GUI
1565 ++initstr;
1566#endif
1567 }
1568
1569 if (STRNICMP(initstr, "view", 4) == 0)
1570 {
1571 readonlymode = TRUE;
1572 curbuf->b_p_ro = TRUE;
1573 p_uc = 10000; /* don't update very often */
1574 initstr += 4;
1575 }
1576 else if (STRNICMP(initstr, "vim", 3) == 0)
1577 initstr += 3;
1578
1579 /* Catch "[r][g]vimdiff" and "[r][g]viewdiff". */
1580 if (STRICMP(initstr, "diff") == 0)
1581 {
1582#ifdef FEAT_DIFF
1583 parmp->diff_mode = TRUE;
1584#else
1585 mch_errmsg(_("This Vim was not compiled with the diff feature."));
1586 mch_errmsg("\n");
1587 mch_exit(2);
1588#endif
1589 }
1590
1591 if (STRNICMP(initstr, "ex", 2) == 0)
1592 {
1593 if (STRNICMP(initstr + 2, "im", 2) == 0)
1594 exmode_active = EXMODE_VIM;
1595 else
1596 exmode_active = EXMODE_NORMAL;
1597 change_compatible(TRUE); /* set 'compatible' */
1598 }
1599}
1600
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001601/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00001602 * Get the name of the display, before gui_prepare() removes it from
1603 * argv[]. Used for the xterm-clipboard display.
1604 *
Bram Moolenaar78e17622007-08-30 10:26:19 +00001605 * Also find the --server... arguments and --socketid and --windowid
Bram Moolenaar58d98232005-07-23 22:25:46 +00001606 */
Bram Moolenaar58d98232005-07-23 22:25:46 +00001607 static void
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001608early_arg_scan(parmp)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00001609 mparm_T *parmp UNUSED;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001610{
Bram Moolenaar03005972008-11-20 13:12:36 +00001611#if defined(FEAT_XCLIPBOARD) || defined(FEAT_CLIENTSERVER) \
1612 || !defined(FEAT_NETBEANS_INTG)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001613 int argc = parmp->argc;
1614 char **argv = parmp->argv;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001615 int i;
1616
1617 for (i = 1; i < argc; i++)
1618 {
1619 if (STRCMP(argv[i], "--") == 0)
1620 break;
1621# ifdef FEAT_XCLIPBOARD
1622 else if (STRICMP(argv[i], "-display") == 0
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001623# if defined(FEAT_GUI_GTK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001624 || STRICMP(argv[i], "--display") == 0
1625# endif
1626 )
1627 {
1628 if (i == argc - 1)
1629 mainerr_arg_missing((char_u *)argv[i]);
1630 xterm_display = argv[++i];
1631 }
1632# endif
1633# ifdef FEAT_CLIENTSERVER
1634 else if (STRICMP(argv[i], "--servername") == 0)
1635 {
1636 if (i == argc - 1)
1637 mainerr_arg_missing((char_u *)argv[i]);
1638 parmp->serverName_arg = (char_u *)argv[++i];
1639 }
Bram Moolenaareb94e552006-03-11 21:35:11 +00001640 else if (STRICMP(argv[i], "--serverlist") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001641 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001642 else if (STRNICMP(argv[i], "--remote", 8) == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001643 {
1644 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001645# ifdef FEAT_GUI
1646 if (strstr(argv[i], "-wait") != 0)
1647 /* don't fork() when starting the GUI to edit files ourself */
1648 gui.dofork = FALSE;
1649# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001650 }
1651# endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001652
1653# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1654# ifdef FEAT_GUI_W32
1655 else if (STRICMP(argv[i], "--windowid") == 0)
1656# else
Bram Moolenaar58d98232005-07-23 22:25:46 +00001657 else if (STRICMP(argv[i], "--socketid") == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001658# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001659 {
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001660 long_u id;
1661 int count;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001662
1663 if (i == argc - 1)
1664 mainerr_arg_missing((char_u *)argv[i]);
1665 if (STRNICMP(argv[i+1], "0x", 2) == 0)
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001666 count = sscanf(&(argv[i + 1][2]), SCANF_HEX_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001667 else
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001668 count = sscanf(argv[i + 1], SCANF_DECIMAL_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001669 if (count != 1)
1670 mainerr(ME_INVALID_ARG, (char_u *)argv[i]);
1671 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00001672# ifdef FEAT_GUI_W32
1673 win_socket_id = id;
1674# else
1675 gtk_socket_id = id;
1676# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001677 i++;
1678 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001679# endif
1680# ifdef FEAT_GUI_GTK
Bram Moolenaar58d98232005-07-23 22:25:46 +00001681 else if (STRICMP(argv[i], "--echo-wid") == 0)
1682 echo_wid_arg = TRUE;
1683# endif
Bram Moolenaar03005972008-11-20 13:12:36 +00001684# ifndef FEAT_NETBEANS_INTG
1685 else if (strncmp(argv[i], "-nb", (size_t)3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +02001686 {
1687 mch_errmsg(_("'-nb' cannot be used: not enabled at compile time\n"));
1688 mch_exit(2);
1689 }
Bram Moolenaar03005972008-11-20 13:12:36 +00001690# endif
1691
Bram Moolenaar58d98232005-07-23 22:25:46 +00001692 }
1693#endif
1694}
1695
1696/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001697 * Scan the command line arguments.
1698 */
1699 static void
1700command_line_scan(parmp)
1701 mparm_T *parmp;
1702{
1703 int argc = parmp->argc;
1704 char **argv = parmp->argv;
1705 int argv_idx; /* index in argv[n][] */
1706 int had_minmin = FALSE; /* found "--" argument */
1707 int want_argument; /* option argument with argument */
1708 int c;
Bram Moolenaar231334e2005-07-25 20:46:57 +00001709 char_u *p = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001710 long n;
1711
1712 --argc;
1713 ++argv;
1714 argv_idx = 1; /* active option letter is argv[0][argv_idx] */
1715 while (argc > 0)
1716 {
1717 /*
1718 * "+" or "+{number}" or "+/{pat}" or "+{command}" argument.
1719 */
1720 if (argv[0][0] == '+' && !had_minmin)
1721 {
1722 if (parmp->n_commands >= MAX_ARG_CMDS)
1723 mainerr(ME_EXTRA_CMD, NULL);
1724 argv_idx = -1; /* skip to next argument */
1725 if (argv[0][1] == NUL)
1726 parmp->commands[parmp->n_commands++] = (char_u *)"$";
1727 else
1728 parmp->commands[parmp->n_commands++] = (char_u *)&(argv[0][1]);
1729 }
1730
1731 /*
1732 * Optional argument.
1733 */
1734 else if (argv[0][0] == '-' && !had_minmin)
1735 {
1736 want_argument = FALSE;
1737 c = argv[0][argv_idx++];
1738#ifdef VMS
1739 /*
1740 * VMS only uses upper case command lines. Interpret "-X" as "-x"
1741 * and "-/X" as "-X".
1742 */
1743 if (c == '/')
1744 {
1745 c = argv[0][argv_idx++];
1746 c = TOUPPER_ASC(c);
1747 }
1748 else
1749 c = TOLOWER_ASC(c);
1750#endif
1751 switch (c)
1752 {
1753 case NUL: /* "vim -" read from stdin */
1754 /* "ex -" silent mode */
1755 if (exmode_active)
1756 silent_mode = TRUE;
1757 else
1758 {
1759 if (parmp->edit_type != EDIT_NONE)
1760 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1761 parmp->edit_type = EDIT_STDIN;
1762 read_cmd_fd = 2; /* read from stderr instead of stdin */
1763 }
1764 argv_idx = -1; /* skip to next argument */
1765 break;
1766
1767 case '-': /* "--" don't take any more option arguments */
1768 /* "--help" give help message */
1769 /* "--version" give version message */
1770 /* "--literal" take files literally */
1771 /* "--nofork" don't fork */
1772 /* "--noplugin[s]" skip plugins */
1773 /* "--cmd <cmd>" execute cmd before vimrc */
1774 if (STRICMP(argv[0] + argv_idx, "help") == 0)
1775 usage();
1776 else if (STRICMP(argv[0] + argv_idx, "version") == 0)
1777 {
1778 Columns = 80; /* need to init Columns */
1779 info_message = TRUE; /* use mch_msg(), not mch_errmsg() */
1780 list_version();
1781 msg_putchar('\n');
1782 msg_didout = FALSE;
1783 mch_exit(0);
1784 }
1785 else if (STRNICMP(argv[0] + argv_idx, "literal", 7) == 0)
1786 {
1787#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
1788 parmp->literal = TRUE;
1789#endif
1790 }
1791 else if (STRNICMP(argv[0] + argv_idx, "nofork", 6) == 0)
1792 {
1793#ifdef FEAT_GUI
1794 gui.dofork = FALSE; /* don't fork() when starting GUI */
1795#endif
1796 }
1797 else if (STRNICMP(argv[0] + argv_idx, "noplugin", 8) == 0)
1798 p_lpl = FALSE;
1799 else if (STRNICMP(argv[0] + argv_idx, "cmd", 3) == 0)
1800 {
1801 want_argument = TRUE;
1802 argv_idx += 3;
1803 }
Bram Moolenaaref94eec2009-11-11 13:22:11 +00001804 else if (STRNICMP(argv[0] + argv_idx, "startuptime", 11) == 0)
1805 {
1806 want_argument = TRUE;
1807 argv_idx += 11;
1808 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001809#ifdef FEAT_CLIENTSERVER
1810 else if (STRNICMP(argv[0] + argv_idx, "serverlist", 10) == 0)
1811 ; /* already processed -- no arg */
1812 else if (STRNICMP(argv[0] + argv_idx, "servername", 10) == 0
1813 || STRNICMP(argv[0] + argv_idx, "serversend", 10) == 0)
1814 {
1815 /* already processed -- snatch the following arg */
1816 if (argc > 1)
1817 {
1818 --argc;
1819 ++argv;
1820 }
1821 }
1822#endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001823#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1824# ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001825 else if (STRNICMP(argv[0] + argv_idx, "socketid", 8) == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001826# else
1827 else if (STRNICMP(argv[0] + argv_idx, "windowid", 8) == 0)
1828# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001829 {
1830 /* already processed -- snatch the following arg */
1831 if (argc > 1)
1832 {
1833 --argc;
1834 ++argv;
1835 }
1836 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001837#endif
1838#ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001839 else if (STRNICMP(argv[0] + argv_idx, "echo-wid", 8) == 0)
1840 {
1841 /* already processed, skip */
1842 }
1843#endif
1844 else
1845 {
1846 if (argv[0][argv_idx])
1847 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
1848 had_minmin = TRUE;
1849 }
1850 if (!want_argument)
1851 argv_idx = -1; /* skip to next argument */
1852 break;
1853
1854 case 'A': /* "-A" start in Arabic mode */
1855#ifdef FEAT_ARABIC
1856 set_option_value((char_u *)"arabic", 1L, NULL, 0);
1857#else
1858 mch_errmsg(_(e_noarabic));
1859 mch_exit(2);
1860#endif
1861 break;
1862
1863 case 'b': /* "-b" binary mode */
Bram Moolenaar231334e2005-07-25 20:46:57 +00001864 /* Needs to be effective before expanding file names, because
1865 * for Win32 this makes us edit a shortcut file itself,
1866 * instead of the file it links to. */
1867 set_options_bin(curbuf->b_p_bin, 1, 0);
1868 curbuf->b_p_bin = 1; /* binary file I/O */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001869 break;
1870
1871 case 'C': /* "-C" Compatible */
1872 change_compatible(TRUE);
1873 break;
1874
1875 case 'e': /* "-e" Ex mode */
1876 exmode_active = EXMODE_NORMAL;
1877 break;
1878
1879 case 'E': /* "-E" Improved Ex mode */
1880 exmode_active = EXMODE_VIM;
1881 break;
1882
1883 case 'f': /* "-f" GUI: run in foreground. Amiga: open
1884 window directly, not with newcli */
1885#ifdef FEAT_GUI
1886 gui.dofork = FALSE; /* don't fork() when starting GUI */
1887#endif
1888 break;
1889
1890 case 'g': /* "-g" start GUI */
1891 main_start_gui();
1892 break;
1893
1894 case 'F': /* "-F" start in Farsi mode: rl + fkmap set */
1895#ifdef FEAT_FKMAP
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00001896 p_fkmap = TRUE;
1897 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001898#else
1899 mch_errmsg(_(e_nofarsi));
1900 mch_exit(2);
1901#endif
1902 break;
1903
1904 case 'h': /* "-h" give help message */
1905#ifdef FEAT_GUI_GNOME
1906 /* Tell usage() to exit for "gvim". */
1907 gui.starting = FALSE;
1908#endif
1909 usage();
1910 break;
1911
1912 case 'H': /* "-H" start in Hebrew mode: rl + hkmap set */
1913#ifdef FEAT_RIGHTLEFT
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00001914 p_hkmap = TRUE;
1915 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001916#else
1917 mch_errmsg(_(e_nohebrew));
1918 mch_exit(2);
1919#endif
1920 break;
1921
1922 case 'l': /* "-l" lisp mode, 'lisp' and 'showmatch' on */
1923#ifdef FEAT_LISP
1924 set_option_value((char_u *)"lisp", 1L, NULL, 0);
1925 p_sm = TRUE;
1926#endif
1927 break;
1928
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001929 case 'M': /* "-M" no changes or writing of files */
1930 reset_modifiable();
1931 /* FALLTHROUGH */
1932
1933 case 'm': /* "-m" no writing of files */
1934 p_write = FALSE;
1935 break;
1936
1937 case 'y': /* "-y" easy mode */
1938#ifdef FEAT_GUI
1939 gui.starting = TRUE; /* start GUI a bit later */
1940#endif
1941 parmp->evim_mode = TRUE;
1942 break;
1943
1944 case 'N': /* "-N" Nocompatible */
1945 change_compatible(FALSE);
1946 break;
1947
1948 case 'n': /* "-n" no swap file */
Bram Moolenaar67c53842010-05-22 18:28:27 +02001949#ifdef FEAT_NETBEANS_INTG
1950 /* checking for "-nb", netbeans parameters */
1951 if (argv[0][argv_idx] == 'b')
1952 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02001953 netbeansArg = argv[0];
1954 argv_idx = -1; /* skip to next argument */
1955 }
1956 else
1957#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001958 parmp->no_swap_file = TRUE;
1959 break;
1960
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001961 case 'p': /* "-p[N]" open N tab pages */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00001962#ifdef TARGET_API_MAC_OSX
1963 /* For some reason on MacOS X, an argument like:
1964 -psn_0_10223617 is passed in when invoke from Finder
1965 or with the 'open' command */
1966 if (argv[0][argv_idx] == 's')
1967 {
1968 argv_idx = -1; /* bypass full -psn */
1969 main_start_gui();
1970 break;
1971 }
1972#endif
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001973#ifdef FEAT_WINDOWS
1974 /* default is 0: open window for each file */
1975 parmp->window_count = get_number_arg((char_u *)argv[0],
1976 &argv_idx, 0);
1977 parmp->window_layout = WIN_TABS;
1978#endif
1979 break;
1980
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001981 case 'o': /* "-o[N]" open N horizontal split windows */
1982#ifdef FEAT_WINDOWS
1983 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00001984 parmp->window_count = get_number_arg((char_u *)argv[0],
1985 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001986 parmp->window_layout = WIN_HOR;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001987#endif
1988 break;
1989
1990 case 'O': /* "-O[N]" open N vertical split windows */
1991#if defined(FEAT_VERTSPLIT) && defined(FEAT_WINDOWS)
1992 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00001993 parmp->window_count = get_number_arg((char_u *)argv[0],
1994 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001995 parmp->window_layout = WIN_VER;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001996#endif
1997 break;
1998
1999#ifdef FEAT_QUICKFIX
2000 case 'q': /* "-q" QuickFix mode */
2001 if (parmp->edit_type != EDIT_NONE)
2002 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2003 parmp->edit_type = EDIT_QF;
2004 if (argv[0][argv_idx]) /* "-q{errorfile}" */
2005 {
2006 parmp->use_ef = (char_u *)argv[0] + argv_idx;
2007 argv_idx = -1;
2008 }
2009 else if (argc > 1) /* "-q {errorfile}" */
2010 want_argument = TRUE;
2011 break;
2012#endif
2013
2014 case 'R': /* "-R" readonly mode */
2015 readonlymode = TRUE;
2016 curbuf->b_p_ro = TRUE;
2017 p_uc = 10000; /* don't update very often */
2018 break;
2019
2020 case 'r': /* "-r" recovery mode */
2021 case 'L': /* "-L" recovery mode */
2022 recoverymode = 1;
2023 break;
2024
2025 case 's':
2026 if (exmode_active) /* "-s" silent (batch) mode */
2027 silent_mode = TRUE;
2028 else /* "-s {scriptin}" read from script file */
2029 want_argument = TRUE;
2030 break;
2031
2032 case 't': /* "-t {tag}" or "-t{tag}" jump to tag */
2033 if (parmp->edit_type != EDIT_NONE)
2034 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2035 parmp->edit_type = EDIT_TAG;
2036 if (argv[0][argv_idx]) /* "-t{tag}" */
2037 {
2038 parmp->tagname = (char_u *)argv[0] + argv_idx;
2039 argv_idx = -1;
2040 }
2041 else /* "-t {tag}" */
2042 want_argument = TRUE;
2043 break;
2044
2045#ifdef FEAT_EVAL
2046 case 'D': /* "-D" Debugging */
2047 parmp->use_debug_break_level = 9999;
2048 break;
2049#endif
2050#ifdef FEAT_DIFF
2051 case 'd': /* "-d" 'diff' */
2052# ifdef AMIGA
2053 /* check for "-dev {device}" */
2054 if (argv[0][argv_idx] == 'e' && argv[0][argv_idx + 1] == 'v')
2055 want_argument = TRUE;
2056 else
2057# endif
2058 parmp->diff_mode = TRUE;
2059 break;
2060#endif
2061 case 'V': /* "-V{N}" Verbose level */
2062 /* default is 10: a little bit verbose */
2063 p_verbose = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2064 if (argv[0][argv_idx] != NUL)
2065 {
2066 set_option_value((char_u *)"verbosefile", 0L,
2067 (char_u *)argv[0] + argv_idx, 0);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002068 argv_idx = (int)STRLEN(argv[0]);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002069 }
2070 break;
2071
2072 case 'v': /* "-v" Vi-mode (as if called "vi") */
2073 exmode_active = 0;
2074#ifdef FEAT_GUI
2075 gui.starting = FALSE; /* don't start GUI */
2076#endif
2077 break;
2078
2079 case 'w': /* "-w{number}" set window height */
2080 /* "-w {scriptout}" write to script */
2081 if (vim_isdigit(((char_u *)argv[0])[argv_idx]))
2082 {
2083 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2084 set_option_value((char_u *)"window", n, NULL, 0);
2085 break;
2086 }
2087 want_argument = TRUE;
2088 break;
2089
2090#ifdef FEAT_CRYPT
2091 case 'x': /* "-x" encrypted reading/writing of files */
2092 parmp->ask_for_key = TRUE;
2093 break;
2094#endif
2095
2096 case 'X': /* "-X" don't connect to X server */
2097#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
2098 x_no_connect = TRUE;
2099#endif
2100 break;
2101
2102 case 'Z': /* "-Z" restricted mode */
2103 restricted = TRUE;
2104 break;
2105
2106 case 'c': /* "-c{command}" or "-c {command}" execute
2107 command */
2108 if (argv[0][argv_idx] != NUL)
2109 {
2110 if (parmp->n_commands >= MAX_ARG_CMDS)
2111 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar231334e2005-07-25 20:46:57 +00002112 parmp->commands[parmp->n_commands++] = (char_u *)argv[0]
2113 + argv_idx;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002114 argv_idx = -1;
2115 break;
2116 }
2117 /*FALLTHROUGH*/
2118 case 'S': /* "-S {file}" execute Vim script */
2119 case 'i': /* "-i {viminfo}" use for viminfo */
2120#ifndef FEAT_DIFF
2121 case 'd': /* "-d {device}" device (for Amiga) */
2122#endif
2123 case 'T': /* "-T {terminal}" terminal name */
2124 case 'u': /* "-u {vimrc}" vim inits file */
2125 case 'U': /* "-U {gvimrc}" gvim inits file */
2126 case 'W': /* "-W {scriptout}" overwrite */
2127#ifdef FEAT_GUI_W32
2128 case 'P': /* "-P {parent title}" MDI parent */
2129#endif
2130 want_argument = TRUE;
2131 break;
2132
2133 default:
2134 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
2135 }
2136
2137 /*
2138 * Handle option arguments with argument.
2139 */
2140 if (want_argument)
2141 {
2142 /*
2143 * Check for garbage immediately after the option letter.
2144 */
2145 if (argv[0][argv_idx] != NUL)
2146 mainerr(ME_GARBAGE, (char_u *)argv[0]);
2147
2148 --argc;
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002149 if (argc < 1 && c != 'S') /* -S has an optional argument */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002150 mainerr_arg_missing((char_u *)argv[0]);
2151 ++argv;
2152 argv_idx = -1;
2153
2154 switch (c)
2155 {
2156 case 'c': /* "-c {command}" execute command */
2157 case 'S': /* "-S {file}" execute Vim script */
2158 if (parmp->n_commands >= MAX_ARG_CMDS)
2159 mainerr(ME_EXTRA_CMD, NULL);
2160 if (c == 'S')
2161 {
2162 char *a;
2163
2164 if (argc < 1)
2165 /* "-S" without argument: use default session file
2166 * name. */
2167 a = SESSION_FILE;
2168 else if (argv[0][0] == '-')
2169 {
2170 /* "-S" followed by another option: use default
2171 * session file name. */
2172 a = SESSION_FILE;
2173 ++argc;
2174 --argv;
2175 }
2176 else
2177 a = argv[0];
2178 p = alloc((unsigned)(STRLEN(a) + 4));
2179 if (p == NULL)
2180 mch_exit(2);
2181 sprintf((char *)p, "so %s", a);
2182 parmp->cmds_tofree[parmp->n_commands] = TRUE;
2183 parmp->commands[parmp->n_commands++] = p;
2184 }
2185 else
Bram Moolenaar231334e2005-07-25 20:46:57 +00002186 parmp->commands[parmp->n_commands++] =
2187 (char_u *)argv[0];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002188 break;
2189
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002190 case '-':
2191 if (argv[-1][2] == 'c')
2192 {
2193 /* "--cmd {command}" execute command */
2194 if (parmp->n_pre_commands >= MAX_ARG_CMDS)
2195 mainerr(ME_EXTRA_CMD, NULL);
2196 parmp->pre_commands[parmp->n_pre_commands++] =
Bram Moolenaar231334e2005-07-25 20:46:57 +00002197 (char_u *)argv[0];
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002198 }
2199 /* "--startuptime <file>" already handled */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002200 break;
2201
2202 /* case 'd': -d {device} is handled in mch_check_win() for the
2203 * Amiga */
2204
2205#ifdef FEAT_QUICKFIX
2206 case 'q': /* "-q {errorfile}" QuickFix mode */
2207 parmp->use_ef = (char_u *)argv[0];
2208 break;
2209#endif
2210
2211 case 'i': /* "-i {viminfo}" use for viminfo */
2212 use_viminfo = (char_u *)argv[0];
2213 break;
2214
2215 case 's': /* "-s {scriptin}" read from script file */
2216 if (scriptin[0] != NULL)
2217 {
2218scripterror:
2219 mch_errmsg(_("Attempt to open script file again: \""));
2220 mch_errmsg(argv[-1]);
2221 mch_errmsg(" ");
2222 mch_errmsg(argv[0]);
2223 mch_errmsg("\"\n");
2224 mch_exit(2);
2225 }
2226 if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL)
2227 {
2228 mch_errmsg(_("Cannot open for reading: \""));
2229 mch_errmsg(argv[0]);
2230 mch_errmsg("\"\n");
2231 mch_exit(2);
2232 }
2233 if (save_typebuf() == FAIL)
2234 mch_exit(2); /* out of memory */
2235 break;
2236
2237 case 't': /* "-t {tag}" */
2238 parmp->tagname = (char_u *)argv[0];
2239 break;
2240
2241 case 'T': /* "-T {terminal}" terminal name */
2242 /*
2243 * The -T term argument is always available and when
2244 * HAVE_TERMLIB is supported it overrides the environment
2245 * variable TERM.
2246 */
2247#ifdef FEAT_GUI
2248 if (term_is_gui((char_u *)argv[0]))
2249 gui.starting = TRUE; /* start GUI a bit later */
2250 else
2251#endif
2252 parmp->term = (char_u *)argv[0];
2253 break;
2254
2255 case 'u': /* "-u {vimrc}" vim inits file */
2256 parmp->use_vimrc = (char_u *)argv[0];
2257 break;
2258
2259 case 'U': /* "-U {gvimrc}" gvim inits file */
2260#ifdef FEAT_GUI
2261 use_gvimrc = (char_u *)argv[0];
2262#endif
2263 break;
2264
2265 case 'w': /* "-w {nr}" 'window' value */
2266 /* "-w {scriptout}" append to script file */
2267 if (vim_isdigit(*((char_u *)argv[0])))
2268 {
2269 argv_idx = 0;
2270 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2271 set_option_value((char_u *)"window", n, NULL, 0);
2272 argv_idx = -1;
2273 break;
2274 }
2275 /*FALLTHROUGH*/
2276 case 'W': /* "-W {scriptout}" overwrite script file */
2277 if (scriptout != NULL)
2278 goto scripterror;
2279 if ((scriptout = mch_fopen(argv[0],
2280 c == 'w' ? APPENDBIN : WRITEBIN)) == NULL)
2281 {
2282 mch_errmsg(_("Cannot open for script output: \""));
2283 mch_errmsg(argv[0]);
2284 mch_errmsg("\"\n");
2285 mch_exit(2);
2286 }
2287 break;
2288
2289#ifdef FEAT_GUI_W32
2290 case 'P': /* "-P {parent title}" MDI parent */
2291 gui_mch_set_parent(argv[0]);
2292 break;
2293#endif
2294 }
2295 }
2296 }
2297
2298 /*
2299 * File name argument.
2300 */
2301 else
2302 {
2303 argv_idx = -1; /* skip to next argument */
2304
2305 /* Check for only one type of editing. */
2306 if (parmp->edit_type != EDIT_NONE && parmp->edit_type != EDIT_FILE)
2307 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2308 parmp->edit_type = EDIT_FILE;
2309
2310#ifdef MSWIN
2311 /* Remember if the argument was a full path before changing
2312 * slashes to backslashes. */
2313 if (argv[0][0] != NUL && argv[0][1] == ':' && argv[0][2] == '\\')
2314 parmp->full_path = TRUE;
2315#endif
2316
2317 /* Add the file to the global argument list. */
2318 if (ga_grow(&global_alist.al_ga, 1) == FAIL
2319 || (p = vim_strsave((char_u *)argv[0])) == NULL)
2320 mch_exit(2);
2321#ifdef FEAT_DIFF
2322 if (parmp->diff_mode && mch_isdir(p) && GARGCOUNT > 0
2323 && !mch_isdir(alist_name(&GARGLIST[0])))
2324 {
2325 char_u *r;
2326
2327 r = concat_fnames(p, gettail(alist_name(&GARGLIST[0])), TRUE);
2328 if (r != NULL)
2329 {
2330 vim_free(p);
2331 p = r;
2332 }
2333 }
2334#endif
2335#if defined(__CYGWIN32__) && !defined(WIN32)
2336 /*
2337 * If vim is invoked by non-Cygwin tools, convert away any
2338 * DOS paths, so things like .swp files are created correctly.
2339 * Look for evidence of non-Cygwin paths before we bother.
2340 * This is only for when using the Unix files.
2341 */
Bram Moolenaarad249fb2010-05-07 16:35:04 +02002342 if (strpbrk(p, "\\:") != NULL && !path_with_url(p))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002343 {
2344 char posix_path[PATH_MAX];
2345
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002346# if CYGWIN_VERSION_DLL_MAJOR >= 1007
2347 cygwin_conv_path(CCP_WIN_A_TO_POSIX, p, posix_path, PATH_MAX);
2348# else
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002349 cygwin_conv_to_posix_path(p, posix_path);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002350# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002351 vim_free(p);
2352 p = vim_strsave(posix_path);
2353 if (p == NULL)
2354 mch_exit(2);
2355 }
2356#endif
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002357
2358#ifdef USE_FNAME_CASE
2359 /* Make the case of the file name match the actual file. */
2360 fname_case(p, 0);
2361#endif
2362
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002363 alist_add(&global_alist, p,
2364#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
Bram Moolenaar231334e2005-07-25 20:46:57 +00002365 parmp->literal ? 2 : 0 /* add buffer nr after exp. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002366#else
2367 2 /* add buffer number now and use curbuf */
2368#endif
2369 );
2370
2371#if defined(FEAT_MBYTE) && defined(WIN32)
2372 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002373 /* Remember this argument has been added to the argument list.
2374 * Needed when 'encoding' is changed. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002375 used_file_arg(argv[0], parmp->literal, parmp->full_path,
Bram Moolenaar688e5f72008-07-24 11:51:40 +00002376# ifdef FEAT_DIFF
2377 parmp->diff_mode
2378# else
2379 FALSE
2380# endif
2381 );
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002382 }
2383#endif
2384 }
2385
2386 /*
2387 * If there are no more letters after the current "-", go to next
2388 * argument. argv_idx is set to -1 when the current argument is to be
2389 * skipped.
2390 */
2391 if (argv_idx <= 0 || argv[0][argv_idx] == NUL)
2392 {
2393 --argc;
2394 ++argv;
2395 argv_idx = 1;
2396 }
2397 }
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002398
2399#ifdef FEAT_EVAL
2400 /* If there is a "+123" or "-c" command, set v:swapcommand to the first
2401 * one. */
2402 if (parmp->n_commands > 0)
2403 {
2404 p = alloc((unsigned)STRLEN(parmp->commands[0]) + 3);
2405 if (p != NULL)
2406 {
2407 sprintf((char *)p, ":%s\r", parmp->commands[0]);
2408 set_vim_var_string(VV_SWAPCOMMAND, p, -1);
2409 vim_free(p);
2410 }
2411 }
2412#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002413}
2414
2415/*
2416 * Print a warning if stdout is not a terminal.
2417 * When starting in Ex mode and commands come from a file, set Silent mode.
2418 */
2419 static void
2420check_tty(parmp)
2421 mparm_T *parmp;
2422{
2423 int input_isatty; /* is active input a terminal? */
2424
2425 input_isatty = mch_input_isatty();
2426 if (exmode_active)
2427 {
2428 if (!input_isatty)
2429 silent_mode = TRUE;
2430 }
2431 else if (parmp->want_full_screen && (!parmp->stdout_isatty || !input_isatty)
2432#ifdef FEAT_GUI
2433 /* don't want the delay when started from the desktop */
2434 && !gui.starting
2435#endif
2436 )
2437 {
2438#ifdef NBDEBUG
2439 /*
2440 * This shouldn't be necessary. But if I run netbeans with the log
2441 * output coming to the console and XOpenDisplay fails, I get vim
2442 * trying to start with input/output to my console tty. This fills my
2443 * input buffer so fast I can't even kill the process in under 2
Bram Moolenaar49325942007-05-10 19:19:59 +00002444 * minutes (and it beeps continuously the whole time :-)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002445 */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002446 if (netbeans_active() && (!parmp->stdout_isatty || !input_isatty))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002447 {
2448 mch_errmsg(_("Vim: Error: Failure to start gvim from NetBeans\n"));
2449 exit(1);
2450 }
2451#endif
2452 if (!parmp->stdout_isatty)
2453 mch_errmsg(_("Vim: Warning: Output is not to a terminal\n"));
2454 if (!input_isatty)
2455 mch_errmsg(_("Vim: Warning: Input is not from a terminal\n"));
2456 out_flush();
2457 if (scriptin[0] == NULL)
2458 ui_delay(2000L, TRUE);
2459 TIME_MSG("Warning delay");
2460 }
2461}
2462
2463/*
2464 * Read text from stdin.
2465 */
2466 static void
2467read_stdin()
2468{
2469 int i;
2470
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002471#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002472 /* When getting the ATTENTION prompt here, use a dialog */
2473 swap_exists_action = SEA_DIALOG;
2474#endif
2475 no_wait_return = TRUE;
2476 i = msg_didany;
2477 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002478 (void)open_buffer(TRUE, NULL, 0); /* create memfile and read file */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002479 no_wait_return = FALSE;
2480 msg_didany = i;
2481 TIME_MSG("reading stdin");
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002482#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002483 check_swap_exists_action();
2484#endif
2485#if !(defined(AMIGA) || defined(MACOS))
2486 /*
2487 * Close stdin and dup it from stderr. Required for GPM to work
2488 * properly, and for running external commands.
2489 * Is there any other system that cannot do this?
2490 */
2491 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00002492 ignored = dup(2);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002493#endif
2494}
2495
2496/*
2497 * Create the requested number of windows and edit buffers in them.
2498 * Also does recovery if "recoverymode" set.
2499 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002500 static void
2501create_windows(parmp)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002502 mparm_T *parmp UNUSED;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002503{
2504#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002505 int dorewind;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002506 int done = 0;
2507
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002508 /*
2509 * Create the number of windows that was requested.
2510 */
2511 if (parmp->window_count == -1) /* was not set */
2512 parmp->window_count = 1;
2513 if (parmp->window_count == 0)
2514 parmp->window_count = GARGCOUNT;
2515 if (parmp->window_count > 1)
2516 {
2517 /* Don't change the windows if there was a command in .vimrc that
2518 * already split some windows */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002519 if (parmp->window_layout == 0)
2520 parmp->window_layout = WIN_HOR;
2521 if (parmp->window_layout == WIN_TABS)
2522 {
2523 parmp->window_count = make_tabpages(parmp->window_count);
2524 TIME_MSG("making tab pages");
2525 }
2526 else if (firstwin->w_next == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002527 {
2528 parmp->window_count = make_windows(parmp->window_count,
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002529 parmp->window_layout == WIN_VER);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002530 TIME_MSG("making windows");
2531 }
2532 else
2533 parmp->window_count = win_count();
2534 }
2535 else
2536 parmp->window_count = 1;
2537#endif
2538
2539 if (recoverymode) /* do recover */
2540 {
2541 msg_scroll = TRUE; /* scroll message up */
2542 ml_recover();
2543 if (curbuf->b_ml.ml_mfp == NULL) /* failed */
2544 getout(1);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002545 do_modelines(0); /* do modelines */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002546 }
2547 else
2548 {
2549 /*
2550 * Open a buffer for windows that don't have one yet.
2551 * Commands in the .vimrc might have loaded a file or split the window.
2552 * Watch out for autocommands that delete a window.
2553 */
2554#ifdef FEAT_AUTOCMD
2555 /*
2556 * Don't execute Win/Buf Enter/Leave autocommands here
2557 */
2558 ++autocmd_no_enter;
2559 ++autocmd_no_leave;
2560#endif
2561#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002562 dorewind = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002563 while (done++ < 1000)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002564 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002565 if (dorewind)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002566 {
2567 if (parmp->window_layout == WIN_TABS)
2568 goto_tabpage(1);
2569 else
2570 curwin = firstwin;
2571 }
2572 else if (parmp->window_layout == WIN_TABS)
2573 {
2574 if (curtab->tp_next == NULL)
2575 break;
2576 goto_tabpage(0);
2577 }
2578 else
2579 {
2580 if (curwin->w_next == NULL)
2581 break;
2582 curwin = curwin->w_next;
2583 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002584 dorewind = FALSE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002585#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002586 curbuf = curwin->w_buffer;
2587 if (curbuf->b_ml.ml_mfp == NULL)
2588 {
2589#ifdef FEAT_FOLDING
2590 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2591 if (p_fdls >= 0)
2592 curwin->w_p_fdl = p_fdls;
2593#endif
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002594#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002595 /* When getting the ATTENTION prompt here, use a dialog */
2596 swap_exists_action = SEA_DIALOG;
2597#endif
2598 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002599
2600 /* create memfile, read file */
2601 (void)open_buffer(FALSE, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002602
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002603#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar84212822006-11-07 21:59:47 +00002604 if (swap_exists_action == SEA_QUIT)
2605 {
2606 if (got_int || only_one_window())
2607 {
2608 /* abort selected or quit and only one window */
2609 did_emsg = FALSE; /* avoid hit-enter prompt */
2610 getout(1);
2611 }
2612 /* We can't close the window, it would disturb what
2613 * happens next. Clear the file name and set the arg
2614 * index to -1 to delete it later. */
2615 setfname(curbuf, NULL, NULL, FALSE);
2616 curwin->w_arg_idx = -1;
2617 swap_exists_action = SEA_NONE;
2618 }
2619 else
2620 handle_swap_exists(NULL);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002621#endif
2622#ifdef FEAT_AUTOCMD
Bram Moolenaar89d40322006-08-29 15:30:07 +00002623 dorewind = TRUE; /* start again */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002624#endif
2625 }
2626#ifdef FEAT_WINDOWS
2627 ui_breakcheck();
2628 if (got_int)
2629 {
2630 (void)vgetc(); /* only break the file loading, not the rest */
2631 break;
2632 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002633 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002634#endif
2635#ifdef FEAT_WINDOWS
2636 if (parmp->window_layout == WIN_TABS)
2637 goto_tabpage(1);
2638 else
2639 curwin = firstwin;
2640 curbuf = curwin->w_buffer;
2641#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002642#ifdef FEAT_AUTOCMD
2643 --autocmd_no_enter;
2644 --autocmd_no_leave;
2645#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002646 }
2647}
2648
2649#ifdef FEAT_WINDOWS
2650 /*
2651 * If opened more than one window, start editing files in the other
2652 * windows. make_windows() has already opened the windows.
2653 */
2654 static void
2655edit_buffers(parmp)
2656 mparm_T *parmp;
2657{
2658 int arg_idx; /* index in argument list */
2659 int i;
Bram Moolenaar84212822006-11-07 21:59:47 +00002660 int advance = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002661
2662# ifdef FEAT_AUTOCMD
2663 /*
2664 * Don't execute Win/Buf Enter/Leave autocommands here
2665 */
2666 ++autocmd_no_enter;
2667 ++autocmd_no_leave;
2668# endif
Bram Moolenaar84212822006-11-07 21:59:47 +00002669
2670 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2671 if (curwin->w_arg_idx == -1)
2672 {
2673 win_close(curwin, TRUE);
2674 advance = FALSE;
2675 }
2676
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002677 arg_idx = 1;
2678 for (i = 1; i < parmp->window_count; ++i)
2679 {
Bram Moolenaar84212822006-11-07 21:59:47 +00002680 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2681 if (curwin->w_arg_idx == -1)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002682 {
Bram Moolenaar84212822006-11-07 21:59:47 +00002683 ++arg_idx;
2684 win_close(curwin, TRUE);
2685 advance = FALSE;
2686 continue;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002687 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002688
Bram Moolenaar84212822006-11-07 21:59:47 +00002689 if (advance)
2690 {
2691 if (parmp->window_layout == WIN_TABS)
2692 {
2693 if (curtab->tp_next == NULL) /* just checking */
2694 break;
2695 goto_tabpage(0);
2696 }
2697 else
2698 {
2699 if (curwin->w_next == NULL) /* just checking */
2700 break;
2701 win_enter(curwin->w_next, FALSE);
2702 }
2703 }
2704 advance = TRUE;
2705
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002706 /* Only open the file if there is no file in this window yet (that can
Bram Moolenaar84212822006-11-07 21:59:47 +00002707 * happen when .vimrc contains ":sall"). */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002708 if (curbuf == firstwin->w_buffer || curbuf->b_ffname == NULL)
2709 {
2710 curwin->w_arg_idx = arg_idx;
Bram Moolenaar84212822006-11-07 21:59:47 +00002711 /* Edit file from arg list, if there is one. When "Quit" selected
2712 * at the ATTENTION prompt close the window. */
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002713# ifdef HAS_SWAP_EXISTS_ACTION
2714 swap_exists_did_quit = FALSE;
2715# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002716 (void)do_ecmd(0, arg_idx < GARGCOUNT
2717 ? alist_name(&GARGLIST[arg_idx]) : NULL,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002718 NULL, NULL, ECMD_LASTL, ECMD_HIDE, curwin);
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002719# ifdef HAS_SWAP_EXISTS_ACTION
2720 if (swap_exists_did_quit)
Bram Moolenaar84212822006-11-07 21:59:47 +00002721 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002722 /* abort or quit selected */
Bram Moolenaar84212822006-11-07 21:59:47 +00002723 if (got_int || only_one_window())
2724 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002725 /* abort selected and only one window */
Bram Moolenaar84212822006-11-07 21:59:47 +00002726 did_emsg = FALSE; /* avoid hit-enter prompt */
2727 getout(1);
2728 }
2729 win_close(curwin, TRUE);
2730 advance = FALSE;
2731 }
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002732# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002733 if (arg_idx == GARGCOUNT - 1)
2734 arg_had_last = TRUE;
2735 ++arg_idx;
2736 }
2737 ui_breakcheck();
2738 if (got_int)
2739 {
2740 (void)vgetc(); /* only break the file loading, not the rest */
2741 break;
2742 }
2743 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002744
2745 if (parmp->window_layout == WIN_TABS)
2746 goto_tabpage(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002747# ifdef FEAT_AUTOCMD
2748 --autocmd_no_enter;
2749# endif
2750 win_enter(firstwin, FALSE); /* back to first window */
2751# ifdef FEAT_AUTOCMD
2752 --autocmd_no_leave;
2753# endif
2754 TIME_MSG("editing files in windows");
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002755 if (parmp->window_count > 1 && parmp->window_layout != WIN_TABS)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002756 win_equal(curwin, FALSE, 'b'); /* adjust heights */
2757}
2758#endif /* FEAT_WINDOWS */
2759
2760/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002761 * Execute the commands from --cmd arguments "cmds[cnt]".
2762 */
2763 static void
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002764exe_pre_commands(parmp)
2765 mparm_T *parmp;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002766{
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002767 char_u **cmds = parmp->pre_commands;
2768 int cnt = parmp->n_pre_commands;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002769 int i;
2770
2771 if (cnt > 0)
2772 {
2773 curwin->w_cursor.lnum = 0; /* just in case.. */
2774 sourcing_name = (char_u *)_("pre-vimrc command line");
2775# ifdef FEAT_EVAL
2776 current_SID = SID_CMDARG;
2777# endif
2778 for (i = 0; i < cnt; ++i)
2779 do_cmdline_cmd(cmds[i]);
2780 sourcing_name = NULL;
2781# ifdef FEAT_EVAL
2782 current_SID = 0;
2783# endif
2784 TIME_MSG("--cmd commands");
2785 }
2786}
2787
2788/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002789 * Execute "+", "-c" and "-S" arguments.
2790 */
2791 static void
2792exe_commands(parmp)
2793 mparm_T *parmp;
2794{
2795 int i;
2796
2797 /*
2798 * We start commands on line 0, make "vim +/pat file" match a
2799 * pattern on line 1. But don't move the cursor when an autocommand
2800 * with g`" was used.
2801 */
2802 msg_scroll = TRUE;
2803 if (parmp->tagname == NULL && curwin->w_cursor.lnum <= 1)
2804 curwin->w_cursor.lnum = 0;
2805 sourcing_name = (char_u *)"command line";
2806#ifdef FEAT_EVAL
2807 current_SID = SID_CARG;
2808#endif
2809 for (i = 0; i < parmp->n_commands; ++i)
2810 {
2811 do_cmdline_cmd(parmp->commands[i]);
2812 if (parmp->cmds_tofree[i])
2813 vim_free(parmp->commands[i]);
2814 }
2815 sourcing_name = NULL;
2816#ifdef FEAT_EVAL
2817 current_SID = 0;
2818#endif
2819 if (curwin->w_cursor.lnum == 0)
2820 curwin->w_cursor.lnum = 1;
2821
2822 if (!exmode_active)
2823 msg_scroll = FALSE;
2824
2825#ifdef FEAT_QUICKFIX
2826 /* When started with "-q errorfile" jump to first error again. */
2827 if (parmp->edit_type == EDIT_QF)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002828 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002829#endif
2830 TIME_MSG("executing command arguments");
2831}
2832
2833/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002834 * Source startup scripts.
2835 */
2836 static void
2837source_startup_scripts(parmp)
2838 mparm_T *parmp;
2839{
2840 int i;
2841
2842 /*
2843 * For "evim" source evim.vim first of all, so that the user can overrule
2844 * any things he doesn't like.
2845 */
2846 if (parmp->evim_mode)
2847 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002848 (void)do_source((char_u *)EVIM_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002849 TIME_MSG("source evim file");
2850 }
2851
2852 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002853 * If -u argument given, use only the initializations from that file and
Bram Moolenaar58d98232005-07-23 22:25:46 +00002854 * nothing else.
2855 */
2856 if (parmp->use_vimrc != NULL)
2857 {
Bram Moolenaar231334e2005-07-25 20:46:57 +00002858 if (STRCMP(parmp->use_vimrc, "NONE") == 0
2859 || STRCMP(parmp->use_vimrc, "NORC") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002860 {
2861#ifdef FEAT_GUI
2862 if (use_gvimrc == NULL) /* don't load gvimrc either */
2863 use_gvimrc = parmp->use_vimrc;
2864#endif
2865 if (parmp->use_vimrc[2] == 'N')
2866 p_lpl = FALSE; /* don't load plugins either */
2867 }
2868 else
2869 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002870 if (do_source(parmp->use_vimrc, FALSE, DOSO_NONE) != OK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002871 EMSG2(_("E282: Cannot read from \"%s\""), parmp->use_vimrc);
2872 }
2873 }
2874 else if (!silent_mode)
2875 {
2876#ifdef AMIGA
2877 struct Process *proc = (struct Process *)FindTask(0L);
2878 APTR save_winptr = proc->pr_WindowPtr;
2879
2880 /* Avoid a requester here for a volume that doesn't exist. */
2881 proc->pr_WindowPtr = (APTR)-1L;
2882#endif
2883
2884 /*
2885 * Get system wide defaults, if the file name is defined.
2886 */
2887#ifdef SYS_VIMRC_FILE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002888 (void)do_source((char_u *)SYS_VIMRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002889#endif
Bram Moolenaar1056d982006-03-09 22:37:52 +00002890#ifdef MACOS_X
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002891 (void)do_source((char_u *)"$VIMRUNTIME/macmap.vim", FALSE, DOSO_NONE);
Bram Moolenaar1056d982006-03-09 22:37:52 +00002892#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00002893
2894 /*
2895 * Try to read initialization commands from the following places:
2896 * - environment variable VIMINIT
2897 * - user vimrc file (s:.vimrc for Amiga, ~/.vimrc otherwise)
2898 * - second user vimrc file ($VIM/.vimrc for Dos)
2899 * - environment variable EXINIT
2900 * - user exrc file (s:.exrc for Amiga, ~/.exrc otherwise)
2901 * - second user exrc file ($VIM/.exrc for Dos)
2902 * The first that exists is used, the rest is ignored.
2903 */
2904 if (process_env((char_u *)"VIMINIT", TRUE) != OK)
2905 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002906 if (do_source((char_u *)USR_VIMRC_FILE, TRUE, DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00002907#ifdef USR_VIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002908 && do_source((char_u *)USR_VIMRC_FILE2, TRUE,
2909 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00002910#endif
2911#ifdef USR_VIMRC_FILE3
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002912 && do_source((char_u *)USR_VIMRC_FILE3, TRUE,
2913 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00002914#endif
2915 && process_env((char_u *)"EXINIT", FALSE) == FAIL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002916 && do_source((char_u *)USR_EXRC_FILE, FALSE, DOSO_NONE) == FAIL)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002917 {
2918#ifdef USR_EXRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002919 (void)do_source((char_u *)USR_EXRC_FILE2, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002920#endif
2921 }
2922 }
2923
2924 /*
2925 * Read initialization commands from ".vimrc" or ".exrc" in current
2926 * directory. This is only done if the 'exrc' option is set.
2927 * Because of security reasons we disallow shell and write commands
2928 * now, except for unix if the file is owned by the user or 'secure'
2929 * option has been reset in environment of global ".exrc" or ".vimrc".
2930 * Only do this if VIMRC_FILE is not the same as USR_VIMRC_FILE or
2931 * SYS_VIMRC_FILE.
2932 */
2933 if (p_exrc)
2934 {
2935#if defined(UNIX) || defined(VMS)
2936 /* If ".vimrc" file is not owned by user, set 'secure' mode. */
2937 if (!file_owned(VIMRC_FILE))
2938#endif
2939 secure = p_secure;
2940
2941 i = FAIL;
2942 if (fullpathcmp((char_u *)USR_VIMRC_FILE,
2943 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
2944#ifdef USR_VIMRC_FILE2
2945 && fullpathcmp((char_u *)USR_VIMRC_FILE2,
2946 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
2947#endif
2948#ifdef USR_VIMRC_FILE3
2949 && fullpathcmp((char_u *)USR_VIMRC_FILE3,
2950 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
2951#endif
2952#ifdef SYS_VIMRC_FILE
2953 && fullpathcmp((char_u *)SYS_VIMRC_FILE,
2954 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
2955#endif
2956 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002957 i = do_source((char_u *)VIMRC_FILE, TRUE, DOSO_VIMRC);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002958
2959 if (i == FAIL)
2960 {
2961#if defined(UNIX) || defined(VMS)
2962 /* if ".exrc" is not owned by user set 'secure' mode */
2963 if (!file_owned(EXRC_FILE))
2964 secure = p_secure;
2965 else
2966 secure = 0;
2967#endif
2968 if ( fullpathcmp((char_u *)USR_EXRC_FILE,
2969 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
2970#ifdef USR_EXRC_FILE2
2971 && fullpathcmp((char_u *)USR_EXRC_FILE2,
2972 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
2973#endif
2974 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002975 (void)do_source((char_u *)EXRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002976 }
2977 }
2978 if (secure == 2)
2979 need_wait_return = TRUE;
2980 secure = 0;
2981#ifdef AMIGA
2982 proc->pr_WindowPtr = save_winptr;
2983#endif
2984 }
2985 TIME_MSG("sourcing vimrc file(s)");
2986}
2987
2988/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002989 * Setup to start using the GUI. Exit with an error when not available.
2990 */
2991 static void
2992main_start_gui()
2993{
2994#ifdef FEAT_GUI
2995 gui.starting = TRUE; /* start GUI a bit later */
2996#else
2997 mch_errmsg(_(e_nogvim));
2998 mch_errmsg("\n");
2999 mch_exit(2);
3000#endif
3001}
3002
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003003#endif /* NO_VIM_MAIN */
3004
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003005/*
Bram Moolenaar49325942007-05-10 19:19:59 +00003006 * Get an environment variable, and execute it as Ex commands.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003007 * Returns FAIL if the environment variable was not executed, OK otherwise.
3008 */
3009 int
3010process_env(env, is_viminit)
3011 char_u *env;
3012 int is_viminit; /* when TRUE, called for VIMINIT */
3013{
3014 char_u *initstr;
3015 char_u *save_sourcing_name;
3016 linenr_T save_sourcing_lnum;
3017#ifdef FEAT_EVAL
3018 scid_T save_sid;
3019#endif
3020
3021 if ((initstr = mch_getenv(env)) != NULL && *initstr != NUL)
3022 {
3023 if (is_viminit)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003024 vimrc_found(NULL, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003025 save_sourcing_name = sourcing_name;
3026 save_sourcing_lnum = sourcing_lnum;
3027 sourcing_name = env;
3028 sourcing_lnum = 0;
3029#ifdef FEAT_EVAL
3030 save_sid = current_SID;
3031 current_SID = SID_ENV;
3032#endif
3033 do_cmdline_cmd(initstr);
3034 sourcing_name = save_sourcing_name;
3035 sourcing_lnum = save_sourcing_lnum;
3036#ifdef FEAT_EVAL
3037 current_SID = save_sid;;
3038#endif
3039 return OK;
3040 }
3041 return FAIL;
3042}
3043
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003044#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003045/*
3046 * Return TRUE if we are certain the user owns the file "fname".
3047 * Used for ".vimrc" and ".exrc".
3048 * Use both stat() and lstat() for extra security.
3049 */
3050 static int
3051file_owned(fname)
3052 char *fname;
3053{
3054 struct stat s;
3055# ifdef UNIX
3056 uid_t uid = getuid();
3057# else /* VMS */
3058 uid_t uid = ((getgid() << 16) | getuid());
3059# endif
3060
3061 return !(mch_stat(fname, &s) != 0 || s.st_uid != uid
3062# ifdef HAVE_LSTAT
3063 || mch_lstat(fname, &s) != 0 || s.st_uid != uid
3064# endif
3065 );
3066}
3067#endif
3068
3069/*
3070 * Give an error message main_errors["n"] and exit.
3071 */
3072 static void
3073mainerr(n, str)
3074 int n; /* one of the ME_ defines */
3075 char_u *str; /* extra argument or NULL */
3076{
3077#if defined(UNIX) || defined(__EMX__) || defined(VMS)
3078 reset_signals(); /* kill us with CTRL-C here, if you like */
3079#endif
3080
3081 mch_errmsg(longVersion);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003082 mch_errmsg("\n");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003083 mch_errmsg(_(main_errors[n]));
3084 if (str != NULL)
3085 {
3086 mch_errmsg(": \"");
3087 mch_errmsg((char *)str);
3088 mch_errmsg("\"");
3089 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003090 mch_errmsg(_("\nMore info with: \"vim -h\"\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003091
3092 mch_exit(1);
3093}
3094
3095 void
3096mainerr_arg_missing(str)
3097 char_u *str;
3098{
3099 mainerr(ME_ARG_MISSING, str);
3100}
3101
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003102#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003103/*
3104 * print a message with three spaces prepended and '\n' appended.
3105 */
3106 static void
3107main_msg(s)
3108 char *s;
3109{
3110 mch_msg(" ");
3111 mch_msg(s);
3112 mch_msg("\n");
3113}
3114
3115/*
3116 * Print messages for "vim -h" or "vim --help" and exit.
3117 */
3118 static void
3119usage()
3120{
3121 int i;
3122 static char *(use[]) =
3123 {
3124 N_("[file ..] edit specified file(s)"),
3125 N_("- read text from stdin"),
3126 N_("-t tag edit file where tag is defined"),
3127#ifdef FEAT_QUICKFIX
3128 N_("-q [errorfile] edit file with first error")
3129#endif
3130 };
3131
3132#if defined(UNIX) || defined(__EMX__) || defined(VMS)
3133 reset_signals(); /* kill us with CTRL-C here, if you like */
3134#endif
3135
3136 mch_msg(longVersion);
3137 mch_msg(_("\n\nusage:"));
3138 for (i = 0; ; ++i)
3139 {
3140 mch_msg(_(" vim [arguments] "));
3141 mch_msg(_(use[i]));
3142 if (i == (sizeof(use) / sizeof(char_u *)) - 1)
3143 break;
3144 mch_msg(_("\n or:"));
3145 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003146#ifdef VMS
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003147 mch_msg(_("\nWhere case is ignored prepend / to make flag upper case"));
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003148#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003149
3150 mch_msg(_("\n\nArguments:\n"));
3151 main_msg(_("--\t\t\tOnly file names after this"));
3152#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
3153 main_msg(_("--literal\t\tDon't expand wildcards"));
3154#endif
3155#ifdef FEAT_OLE
3156 main_msg(_("-register\t\tRegister this gvim for OLE"));
3157 main_msg(_("-unregister\t\tUnregister gvim for OLE"));
3158#endif
3159#ifdef FEAT_GUI
3160 main_msg(_("-g\t\t\tRun using GUI (like \"gvim\")"));
3161 main_msg(_("-f or --nofork\tForeground: Don't fork when starting GUI"));
3162#endif
3163 main_msg(_("-v\t\t\tVi mode (like \"vi\")"));
3164 main_msg(_("-e\t\t\tEx mode (like \"ex\")"));
3165 main_msg(_("-s\t\t\tSilent (batch) mode (only for \"ex\")"));
3166#ifdef FEAT_DIFF
3167 main_msg(_("-d\t\t\tDiff mode (like \"vimdiff\")"));
3168#endif
3169 main_msg(_("-y\t\t\tEasy mode (like \"evim\", modeless)"));
3170 main_msg(_("-R\t\t\tReadonly mode (like \"view\")"));
3171 main_msg(_("-Z\t\t\tRestricted mode (like \"rvim\")"));
3172 main_msg(_("-m\t\t\tModifications (writing files) not allowed"));
3173 main_msg(_("-M\t\t\tModifications in text not allowed"));
3174 main_msg(_("-b\t\t\tBinary mode"));
3175#ifdef FEAT_LISP
3176 main_msg(_("-l\t\t\tLisp mode"));
3177#endif
3178 main_msg(_("-C\t\t\tCompatible with Vi: 'compatible'"));
3179 main_msg(_("-N\t\t\tNot fully Vi compatible: 'nocompatible'"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003180 main_msg(_("-V[N][fname]\t\tBe verbose [level N] [log messages to fname]"));
3181#ifdef FEAT_EVAL
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003182 main_msg(_("-D\t\t\tDebugging mode"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003183#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003184 main_msg(_("-n\t\t\tNo swap file, use memory only"));
3185 main_msg(_("-r\t\t\tList swap files and exit"));
3186 main_msg(_("-r (with file name)\tRecover crashed session"));
3187 main_msg(_("-L\t\t\tSame as -r"));
3188#ifdef AMIGA
3189 main_msg(_("-f\t\t\tDon't use newcli to open window"));
3190 main_msg(_("-dev <device>\t\tUse <device> for I/O"));
3191#endif
3192#ifdef FEAT_ARABIC
3193 main_msg(_("-A\t\t\tstart in Arabic mode"));
3194#endif
3195#ifdef FEAT_RIGHTLEFT
3196 main_msg(_("-H\t\t\tStart in Hebrew mode"));
3197#endif
3198#ifdef FEAT_FKMAP
3199 main_msg(_("-F\t\t\tStart in Farsi mode"));
3200#endif
3201 main_msg(_("-T <terminal>\tSet terminal type to <terminal>"));
3202 main_msg(_("-u <vimrc>\t\tUse <vimrc> instead of any .vimrc"));
3203#ifdef FEAT_GUI
3204 main_msg(_("-U <gvimrc>\t\tUse <gvimrc> instead of any .gvimrc"));
3205#endif
3206 main_msg(_("--noplugin\t\tDon't load plugin scripts"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003207#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003208 main_msg(_("-p[N]\t\tOpen N tab pages (default: one for each file)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003209 main_msg(_("-o[N]\t\tOpen N windows (default: one for each file)"));
3210 main_msg(_("-O[N]\t\tLike -o but split vertically"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003211#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003212 main_msg(_("+\t\t\tStart at end of file"));
3213 main_msg(_("+<lnum>\t\tStart at line <lnum>"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003214 main_msg(_("--cmd <command>\tExecute <command> before loading any vimrc file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003215 main_msg(_("-c <command>\t\tExecute <command> after loading the first file"));
3216 main_msg(_("-S <session>\t\tSource file <session> after loading the first file"));
3217 main_msg(_("-s <scriptin>\tRead Normal mode commands from file <scriptin>"));
3218 main_msg(_("-w <scriptout>\tAppend all typed commands to file <scriptout>"));
3219 main_msg(_("-W <scriptout>\tWrite all typed commands to file <scriptout>"));
3220#ifdef FEAT_CRYPT
3221 main_msg(_("-x\t\t\tEdit encrypted files"));
3222#endif
3223#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
3224# if defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)
3225 main_msg(_("-display <display>\tConnect vim to this particular X-server"));
3226# endif
3227 main_msg(_("-X\t\t\tDo not connect to X server"));
3228#endif
3229#ifdef FEAT_CLIENTSERVER
3230 main_msg(_("--remote <files>\tEdit <files> in a Vim server if possible"));
3231 main_msg(_("--remote-silent <files> Same, don't complain if there is no server"));
3232 main_msg(_("--remote-wait <files> As --remote but wait for files to have been edited"));
3233 main_msg(_("--remote-wait-silent <files> Same, don't complain if there is no server"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003234# ifdef FEAT_WINDOWS
Bram Moolenaar82ad3242008-01-11 19:26:36 +00003235 main_msg(_("--remote-tab[-wait][-silent] <files> As --remote but use tab page per file"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003236# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003237 main_msg(_("--remote-send <keys>\tSend <keys> to a Vim server and exit"));
3238 main_msg(_("--remote-expr <expr>\tEvaluate <expr> in a Vim server and print result"));
3239 main_msg(_("--serverlist\t\tList available Vim server names and exit"));
3240 main_msg(_("--servername <name>\tSend to/become the Vim server <name>"));
3241#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003242#ifdef STARTUPTIME
Bram Moolenaar34ef52d2009-11-17 11:31:25 +00003243 main_msg(_("--startuptime <file>\tWrite startup timing messages to <file>"));
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003244#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003245#ifdef FEAT_VIMINFO
3246 main_msg(_("-i <viminfo>\t\tUse <viminfo> instead of .viminfo"));
3247#endif
3248 main_msg(_("-h or --help\tPrint Help (this message) and exit"));
3249 main_msg(_("--version\t\tPrint version information and exit"));
3250
3251#ifdef FEAT_GUI_X11
3252# ifdef FEAT_GUI_MOTIF
3253 mch_msg(_("\nArguments recognised by gvim (Motif version):\n"));
3254# else
3255# ifdef FEAT_GUI_ATHENA
3256# ifdef FEAT_GUI_NEXTAW
3257 mch_msg(_("\nArguments recognised by gvim (neXtaw version):\n"));
3258# else
3259 mch_msg(_("\nArguments recognised by gvim (Athena version):\n"));
3260# endif
3261# endif
3262# endif
3263 main_msg(_("-display <display>\tRun vim on <display>"));
3264 main_msg(_("-iconic\t\tStart vim iconified"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003265 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
3266 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
3267 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3268 main_msg(_("-boldfont <font>\tUse <font> for bold text"));
3269 main_msg(_("-italicfont <font>\tUse <font> for italic text"));
3270 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3271 main_msg(_("-borderwidth <width>\tUse a border width of <width> (also: -bw)"));
3272 main_msg(_("-scrollbarwidth <width> Use a scrollbar width of <width> (also: -sw)"));
3273# ifdef FEAT_GUI_ATHENA
3274 main_msg(_("-menuheight <height>\tUse a menu bar height of <height> (also: -mh)"));
3275# endif
3276 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3277 main_msg(_("+reverse\t\tDon't use reverse video (also: +rv)"));
3278 main_msg(_("-xrm <resource>\tSet the specified resource"));
3279#endif /* FEAT_GUI_X11 */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003280#ifdef FEAT_GUI_GTK
3281 mch_msg(_("\nArguments recognised by gvim (GTK+ version):\n"));
3282 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3283 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3284 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3285 main_msg(_("-display <display>\tRun vim on <display> (also: --display)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003286 main_msg(_("--role <role>\tSet a unique role to identify the main window"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003287 main_msg(_("--socketid <xid>\tOpen Vim inside another GTK widget"));
3288#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003289#ifdef FEAT_GUI_W32
3290 main_msg(_("-P <parent title>\tOpen Vim inside parent application"));
Bram Moolenaar78e17622007-08-30 10:26:19 +00003291 main_msg(_("--windowid <HWND>\tOpen Vim inside another win32 widget"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003292#endif
3293
3294#ifdef FEAT_GUI_GNOME
3295 /* Gnome gives extra messages for --help if we continue, but not for -h. */
3296 if (gui.starting)
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003297 {
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003298 mch_msg("\n");
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003299 gui.dofork = FALSE;
3300 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003301 else
3302#endif
3303 mch_exit(0);
3304}
3305
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00003306#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003307/*
3308 * Check the result of the ATTENTION dialog:
3309 * When "Quit" selected, exit Vim.
3310 * When "Recover" selected, recover the file.
3311 */
3312 static void
3313check_swap_exists_action()
3314{
3315 if (swap_exists_action == SEA_QUIT)
3316 getout(1);
3317 handle_swap_exists(NULL);
3318}
3319#endif
3320
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003321#endif
3322
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003323#if defined(STARTUPTIME) || defined(PROTO)
3324static void time_diff __ARGS((struct timeval *then, struct timeval *now));
3325
3326static struct timeval prev_timeval;
3327
Bram Moolenaar3f269672009-11-03 11:11:11 +00003328# ifdef WIN3264
3329/*
3330 * Windows doesn't have gettimeofday(), although it does have struct timeval.
3331 */
3332 static int
3333gettimeofday(struct timeval *tv, char *dummy)
3334{
3335 long t = clock();
3336 tv->tv_sec = t / CLOCKS_PER_SEC;
3337 tv->tv_usec = (t - tv->tv_sec * CLOCKS_PER_SEC) * 1000000 / CLOCKS_PER_SEC;
3338 return 0;
3339}
3340# endif
3341
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003342/*
3343 * Save the previous time before doing something that could nest.
3344 * set "*tv_rel" to the time elapsed so far.
3345 */
3346 void
3347time_push(tv_rel, tv_start)
3348 void *tv_rel, *tv_start;
3349{
3350 *((struct timeval *)tv_rel) = prev_timeval;
3351 gettimeofday(&prev_timeval, NULL);
3352 ((struct timeval *)tv_rel)->tv_usec = prev_timeval.tv_usec
3353 - ((struct timeval *)tv_rel)->tv_usec;
3354 ((struct timeval *)tv_rel)->tv_sec = prev_timeval.tv_sec
3355 - ((struct timeval *)tv_rel)->tv_sec;
3356 if (((struct timeval *)tv_rel)->tv_usec < 0)
3357 {
3358 ((struct timeval *)tv_rel)->tv_usec += 1000000;
3359 --((struct timeval *)tv_rel)->tv_sec;
3360 }
3361 *(struct timeval *)tv_start = prev_timeval;
3362}
3363
3364/*
3365 * Compute the previous time after doing something that could nest.
3366 * Subtract "*tp" from prev_timeval;
3367 * Note: The arguments are (void *) to avoid trouble with systems that don't
3368 * have struct timeval.
3369 */
3370 void
3371time_pop(tp)
3372 void *tp; /* actually (struct timeval *) */
3373{
3374 prev_timeval.tv_usec -= ((struct timeval *)tp)->tv_usec;
3375 prev_timeval.tv_sec -= ((struct timeval *)tp)->tv_sec;
3376 if (prev_timeval.tv_usec < 0)
3377 {
3378 prev_timeval.tv_usec += 1000000;
3379 --prev_timeval.tv_sec;
3380 }
3381}
3382
3383 static void
3384time_diff(then, now)
3385 struct timeval *then;
3386 struct timeval *now;
3387{
3388 long usec;
3389 long msec;
3390
3391 usec = now->tv_usec - then->tv_usec;
3392 msec = (now->tv_sec - then->tv_sec) * 1000L + usec / 1000L,
3393 usec = usec % 1000L;
3394 fprintf(time_fd, "%03ld.%03ld", msec, usec >= 0 ? usec : usec + 1000L);
3395}
3396
3397 void
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003398time_msg(mesg, tv_start)
3399 char *mesg;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003400 void *tv_start; /* only for do_source: start time; actually
3401 (struct timeval *) */
3402{
3403 static struct timeval start;
3404 struct timeval now;
3405
3406 if (time_fd != NULL)
3407 {
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003408 if (strstr(mesg, "STARTING") != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003409 {
3410 gettimeofday(&start, NULL);
3411 prev_timeval = start;
3412 fprintf(time_fd, "\n\ntimes in msec\n");
3413 fprintf(time_fd, " clock self+sourced self: sourced script\n");
3414 fprintf(time_fd, " clock elapsed: other lines\n\n");
3415 }
3416 gettimeofday(&now, NULL);
3417 time_diff(&start, &now);
3418 if (((struct timeval *)tv_start) != NULL)
3419 {
3420 fprintf(time_fd, " ");
3421 time_diff(((struct timeval *)tv_start), &now);
3422 }
3423 fprintf(time_fd, " ");
3424 time_diff(&prev_timeval, &now);
3425 prev_timeval = now;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003426 fprintf(time_fd, ": %s\n", mesg);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003427 }
3428}
3429
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003430#endif
3431
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003432#if (defined(FEAT_CLIENTSERVER) && !defined(NO_VIM_MAIN)) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003433
3434/*
3435 * Common code for the X command server and the Win32 command server.
3436 */
3437
Bram Moolenaareb94e552006-03-11 21:35:11 +00003438static char_u *build_drop_cmd __ARGS((int filec, char **filev, int tabs, int sendReply));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003439
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003440/*
3441 * Do the client-server stuff, unless "--servername ''" was used.
3442 */
3443 static void
3444exec_on_server(parmp)
3445 mparm_T *parmp;
3446{
3447 if (parmp->serverName_arg == NULL || *parmp->serverName_arg != NUL)
3448 {
3449# ifdef WIN32
3450 /* Initialise the client/server messaging infrastructure. */
3451 serverInitMessaging();
3452# endif
3453
3454 /*
3455 * When a command server argument was found, execute it. This may
3456 * exit Vim when it was successful. Otherwise it's executed further
3457 * on. Remember the encoding used here in "serverStrEnc".
3458 */
3459 if (parmp->serverArg)
3460 {
3461 cmdsrv_main(&parmp->argc, parmp->argv,
3462 parmp->serverName_arg, &parmp->serverStr);
3463# ifdef FEAT_MBYTE
3464 parmp->serverStrEnc = vim_strsave(p_enc);
3465# endif
3466 }
3467
3468 /* If we're still running, get the name to register ourselves.
3469 * On Win32 can register right now, for X11 need to setup the
3470 * clipboard first, it's further down. */
3471 parmp->servername = serverMakeName(parmp->serverName_arg,
3472 parmp->argv[0]);
3473# ifdef WIN32
3474 if (parmp->servername != NULL)
3475 {
3476 serverSetName(parmp->servername);
3477 vim_free(parmp->servername);
3478 }
3479# endif
3480 }
3481}
3482
3483/*
3484 * Prepare for running as a Vim server.
3485 */
3486 static void
3487prepare_server(parmp)
3488 mparm_T *parmp;
3489{
3490# if defined(FEAT_X11)
3491 /*
3492 * Register for remote command execution with :serversend and --remote
3493 * unless there was a -X or a --servername '' on the command line.
3494 * Only register nongui-vim's with an explicit --servername argument.
Bram Moolenaar5f402312006-08-15 19:40:35 +00003495 * When running as root --servername is also required.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003496 */
3497 if (X_DISPLAY != NULL && parmp->servername != NULL && (
3498# ifdef FEAT_GUI
Bram Moolenaar5f402312006-08-15 19:40:35 +00003499 (gui.in_use
3500# ifdef UNIX
Bram Moolenaar311d9822007-02-27 15:48:28 +00003501 && getuid() != ROOT_UID
Bram Moolenaar5f402312006-08-15 19:40:35 +00003502# endif
3503 ) ||
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003504# endif
3505 parmp->serverName_arg != NULL))
3506 {
3507 (void)serverRegisterName(X_DISPLAY, parmp->servername);
3508 vim_free(parmp->servername);
3509 TIME_MSG("register server name");
3510 }
3511 else
3512 serverDelayedStartName = parmp->servername;
3513# endif
3514
3515 /*
3516 * Execute command ourselves if we're here because the send failed (or
3517 * else we would have exited above).
3518 */
3519 if (parmp->serverStr != NULL)
3520 {
3521 char_u *p;
3522
3523 server_to_input_buf(serverConvert(parmp->serverStrEnc,
3524 parmp->serverStr, &p));
3525 vim_free(p);
3526 }
3527}
3528
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003529 static void
3530cmdsrv_main(argc, argv, serverName_arg, serverStr)
3531 int *argc;
3532 char **argv;
3533 char_u *serverName_arg;
3534 char_u **serverStr;
3535{
3536 char_u *res;
3537 int i;
3538 char_u *sname;
3539 int ret;
3540 int didone = FALSE;
3541 int exiterr = 0;
3542 char **newArgV = argv + 1;
3543 int newArgC = 1,
3544 Argc = *argc;
3545 int argtype;
3546#define ARGTYPE_OTHER 0
3547#define ARGTYPE_EDIT 1
3548#define ARGTYPE_EDIT_WAIT 2
3549#define ARGTYPE_SEND 3
3550 int silent = FALSE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003551 int tabs = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003552# ifndef FEAT_X11
3553 HWND srv;
3554# else
3555 Window srv;
3556
3557 setup_term_clip();
3558# endif
3559
3560 sname = serverMakeName(serverName_arg, argv[0]);
3561 if (sname == NULL)
3562 return;
3563
3564 /*
3565 * Execute the command server related arguments and remove them
3566 * from the argc/argv array; We may have to return into main()
3567 */
3568 for (i = 1; i < Argc; i++)
3569 {
3570 res = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003571 if (STRCMP(argv[i], "--") == 0) /* end of option arguments */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003572 {
3573 for (; i < *argc; i++)
3574 {
3575 *newArgV++ = argv[i];
3576 newArgC++;
3577 }
3578 break;
3579 }
3580
Bram Moolenaareb94e552006-03-11 21:35:11 +00003581 if (STRICMP(argv[i], "--remote-send") == 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003582 argtype = ARGTYPE_SEND;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003583 else if (STRNICMP(argv[i], "--remote", 8) == 0)
3584 {
3585 char *p = argv[i] + 8;
3586
3587 argtype = ARGTYPE_EDIT;
3588 while (*p != NUL)
3589 {
3590 if (STRNICMP(p, "-wait", 5) == 0)
3591 {
3592 argtype = ARGTYPE_EDIT_WAIT;
3593 p += 5;
3594 }
3595 else if (STRNICMP(p, "-silent", 7) == 0)
3596 {
3597 silent = TRUE;
3598 p += 7;
3599 }
3600 else if (STRNICMP(p, "-tab", 4) == 0)
3601 {
3602 tabs = TRUE;
3603 p += 4;
3604 }
3605 else
3606 {
3607 argtype = ARGTYPE_OTHER;
3608 break;
3609 }
3610 }
3611 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003612 else
3613 argtype = ARGTYPE_OTHER;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003614
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003615 if (argtype != ARGTYPE_OTHER)
3616 {
3617 if (i == *argc - 1)
3618 mainerr_arg_missing((char_u *)argv[i]);
3619 if (argtype == ARGTYPE_SEND)
3620 {
3621 *serverStr = (char_u *)argv[i + 1];
3622 i++;
3623 }
3624 else
3625 {
3626 *serverStr = build_drop_cmd(*argc - i - 1, argv + i + 1,
Bram Moolenaareb94e552006-03-11 21:35:11 +00003627 tabs, argtype == ARGTYPE_EDIT_WAIT);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003628 if (*serverStr == NULL)
3629 {
3630 /* Probably out of memory, exit. */
3631 didone = TRUE;
3632 exiterr = 1;
3633 break;
3634 }
3635 Argc = i;
3636 }
3637# ifdef FEAT_X11
3638 if (xterm_dpy == NULL)
3639 {
3640 mch_errmsg(_("No display"));
3641 ret = -1;
3642 }
3643 else
3644 ret = serverSendToVim(xterm_dpy, sname, *serverStr,
3645 NULL, &srv, 0, 0, silent);
3646# else
3647 /* Win32 always works? */
3648 ret = serverSendToVim(sname, *serverStr, NULL, &srv, 0, silent);
3649# endif
3650 if (ret < 0)
3651 {
3652 if (argtype == ARGTYPE_SEND)
3653 {
3654 /* Failed to send, abort. */
3655 mch_errmsg(_(": Send failed.\n"));
3656 didone = TRUE;
3657 exiterr = 1;
3658 }
3659 else if (!silent)
3660 /* Let vim start normally. */
3661 mch_errmsg(_(": Send failed. Trying to execute locally\n"));
3662 break;
3663 }
3664
3665# ifdef FEAT_GUI_W32
3666 /* Guess that when the server name starts with "g" it's a GUI
3667 * server, which we can bring to the foreground here.
3668 * Foreground() in the server doesn't work very well. */
3669 if (argtype != ARGTYPE_SEND && TOUPPER_ASC(*sname) == 'G')
3670 SetForegroundWindow(srv);
3671# endif
3672
3673 /*
3674 * For --remote-wait: Wait until the server did edit each
3675 * file. Also detect that the server no longer runs.
3676 */
3677 if (ret >= 0 && argtype == ARGTYPE_EDIT_WAIT)
3678 {
3679 int numFiles = *argc - i - 1;
3680 int j;
3681 char_u *done = alloc(numFiles);
3682 char_u *p;
3683# ifdef FEAT_GUI_W32
3684 NOTIFYICONDATA ni;
3685 int count = 0;
3686 extern HWND message_window;
3687# endif
3688
3689 if (numFiles > 0 && argv[i + 1][0] == '+')
3690 /* Skip "+cmd" argument, don't wait for it to be edited. */
3691 --numFiles;
3692
3693# ifdef FEAT_GUI_W32
3694 ni.cbSize = sizeof(ni);
3695 ni.hWnd = message_window;
3696 ni.uID = 0;
3697 ni.uFlags = NIF_ICON|NIF_TIP;
3698 ni.hIcon = LoadIcon((HINSTANCE)GetModuleHandle(0), "IDR_VIM");
3699 sprintf(ni.szTip, _("%d of %d edited"), count, numFiles);
3700 Shell_NotifyIcon(NIM_ADD, &ni);
3701# endif
3702
3703 /* Wait for all files to unload in remote */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02003704 vim_memset(done, 0, numFiles);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003705 while (memchr(done, 0, numFiles) != NULL)
3706 {
3707# ifdef WIN32
3708 p = serverGetReply(srv, NULL, TRUE, TRUE);
3709 if (p == NULL)
3710 break;
3711# else
3712 if (serverReadReply(xterm_dpy, srv, &p, TRUE) < 0)
3713 break;
3714# endif
3715 j = atoi((char *)p);
3716 if (j >= 0 && j < numFiles)
3717 {
3718# ifdef FEAT_GUI_W32
3719 ++count;
3720 sprintf(ni.szTip, _("%d of %d edited"),
3721 count, numFiles);
3722 Shell_NotifyIcon(NIM_MODIFY, &ni);
3723# endif
3724 done[j] = 1;
3725 }
3726 }
3727# ifdef FEAT_GUI_W32
3728 Shell_NotifyIcon(NIM_DELETE, &ni);
3729# endif
3730 }
3731 }
3732 else if (STRICMP(argv[i], "--remote-expr") == 0)
3733 {
3734 if (i == *argc - 1)
3735 mainerr_arg_missing((char_u *)argv[i]);
3736# ifdef WIN32
3737 /* Win32 always works? */
3738 if (serverSendToVim(sname, (char_u *)argv[i + 1],
3739 &res, NULL, 1, FALSE) < 0)
3740# else
3741 if (xterm_dpy == NULL)
3742 mch_errmsg(_("No display: Send expression failed.\n"));
3743 else if (serverSendToVim(xterm_dpy, sname, (char_u *)argv[i + 1],
3744 &res, NULL, 1, 1, FALSE) < 0)
3745# endif
3746 {
3747 if (res != NULL && *res != NUL)
3748 {
3749 /* Output error from remote */
3750 mch_errmsg((char *)res);
3751 vim_free(res);
3752 res = NULL;
3753 }
3754 mch_errmsg(_(": Send expression failed.\n"));
3755 }
3756 }
3757 else if (STRICMP(argv[i], "--serverlist") == 0)
3758 {
3759# ifdef WIN32
3760 /* Win32 always works? */
3761 res = serverGetVimNames();
3762# else
3763 if (xterm_dpy != NULL)
3764 res = serverGetVimNames(xterm_dpy);
3765# endif
3766 if (called_emsg)
3767 mch_errmsg("\n");
3768 }
3769 else if (STRICMP(argv[i], "--servername") == 0)
3770 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +00003771 /* Already processed. Take it out of the command line */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003772 i++;
3773 continue;
3774 }
3775 else
3776 {
3777 *newArgV++ = argv[i];
3778 newArgC++;
3779 continue;
3780 }
3781 didone = TRUE;
3782 if (res != NULL && *res != NUL)
3783 {
3784 mch_msg((char *)res);
3785 if (res[STRLEN(res) - 1] != '\n')
3786 mch_msg("\n");
3787 }
3788 vim_free(res);
3789 }
3790
3791 if (didone)
3792 {
3793 display_errors(); /* display any collected messages */
3794 exit(exiterr); /* Mission accomplished - get out */
3795 }
3796
3797 /* Return back into main() */
3798 *argc = newArgC;
3799 vim_free(sname);
3800}
3801
3802/*
3803 * Build a ":drop" command to send to a Vim server.
3804 */
3805 static char_u *
Bram Moolenaareb94e552006-03-11 21:35:11 +00003806build_drop_cmd(filec, filev, tabs, sendReply)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003807 int filec;
3808 char **filev;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003809 int tabs; /* Use ":tab drop" instead of ":drop". */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003810 int sendReply;
3811{
3812 garray_T ga;
3813 int i;
3814 char_u *inicmd = NULL;
3815 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003816 char_u *cwd;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003817
3818 if (filec > 0 && filev[0][0] == '+')
3819 {
3820 inicmd = (char_u *)filev[0] + 1;
3821 filev++;
3822 filec--;
3823 }
3824 /* Check if we have at least one argument. */
3825 if (filec <= 0)
3826 mainerr_arg_missing((char_u *)filev[-1]);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003827
3828 /* Temporarily cd to the current directory to handle relative file names. */
Bram Moolenaard9462e32011-04-11 21:35:11 +02003829 cwd = alloc(MAXPATHL);
3830 if (cwd == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003831 return NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003832 if (mch_dirname(cwd, MAXPATHL) != OK)
3833 {
3834 vim_free(cwd);
3835 return NULL;
3836 }
3837 p = vim_strsave_escaped_ext(cwd,
Bram Moolenaar02b06312007-09-06 15:39:22 +00003838#ifdef BACKSLASH_IN_FILENAME
3839 "", /* rem_backslash() will tell what chars to escape */
3840#else
3841 PATH_ESC_CHARS,
3842#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +02003843 '\\', TRUE);
3844 vim_free(cwd);
3845 if (p == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003846 return NULL;
3847 ga_init2(&ga, 1, 100);
3848 ga_concat(&ga, (char_u *)"<C-\\><C-N>:cd ");
3849 ga_concat(&ga, p);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003850 vim_free(p);
Bram Moolenaareb94e552006-03-11 21:35:11 +00003851
3852 /* Call inputsave() so that a prompt for an encryption key works. */
3853 ga_concat(&ga, (char_u *)"<CR>:if exists('*inputsave')|call inputsave()|endif|");
3854 if (tabs)
3855 ga_concat(&ga, (char_u *)"tab ");
3856 ga_concat(&ga, (char_u *)"drop");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003857 for (i = 0; i < filec; i++)
3858 {
3859 /* On Unix the shell has already expanded the wildcards, don't want to
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003860 * do it again in the Vim server. On MS-Windows only escape
3861 * non-wildcard characters. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003862 p = vim_strsave_escaped((char_u *)filev[i],
3863#ifdef UNIX
3864 PATH_ESC_CHARS
3865#else
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003866 (char_u *)" \t%#"
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003867#endif
3868 );
3869 if (p == NULL)
3870 {
3871 vim_free(ga.ga_data);
3872 return NULL;
3873 }
3874 ga_concat(&ga, (char_u *)" ");
3875 ga_concat(&ga, p);
3876 vim_free(p);
3877 }
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003878 ga_concat(&ga, (char_u *)"|if exists('*inputrestore')|call inputrestore()|endif<CR>");
3879
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003880 /* The :drop commands goes to Insert mode when 'insertmode' is set, use
3881 * CTRL-\ CTRL-N again. */
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003882 ga_concat(&ga, (char_u *)"<C-\\><C-N>");
3883
3884 /* Switch back to the correct current directory (prior to temporary path
3885 * switch) unless 'autochdir' is set, in which case it will already be
3886 * correct after the :drop command. */
3887 ga_concat(&ga, (char_u *)":if !exists('+acd')||!&acd|cd -|endif<CR>");
3888
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003889 if (sendReply)
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003890 ga_concat(&ga, (char_u *)":call SetupRemoteReplies()<CR>");
3891 ga_concat(&ga, (char_u *)":");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003892 if (inicmd != NULL)
3893 {
3894 /* Can't use <CR> after "inicmd", because an "startinsert" would cause
3895 * the following commands to be inserted as text. Use a "|",
3896 * hopefully "inicmd" does allow this... */
3897 ga_concat(&ga, inicmd);
3898 ga_concat(&ga, (char_u *)"|");
3899 }
3900 /* Bring the window to the foreground, goto Insert mode when 'im' set and
3901 * clear command line. */
Bram Moolenaar567e4de2004-12-31 21:01:02 +00003902 ga_concat(&ga, (char_u *)"cal foreground()|if &im|star|en|redr|f<CR>");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003903 ga_append(&ga, NUL);
3904 return ga.ga_data;
3905}
3906
3907/*
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003908 * Make our basic server name: use the specified "arg" if given, otherwise use
3909 * the tail of the command "cmd" we were started with.
3910 * Return the name in allocated memory. This doesn't include a serial number.
3911 */
3912 static char_u *
3913serverMakeName(arg, cmd)
3914 char_u *arg;
3915 char *cmd;
3916{
3917 char_u *p;
3918
3919 if (arg != NULL && *arg != NUL)
3920 p = vim_strsave_up(arg);
3921 else
3922 {
3923 p = vim_strsave_up(gettail((char_u *)cmd));
3924 /* Remove .exe or .bat from the name. */
3925 if (p != NULL && vim_strchr(p, '.') != NULL)
3926 *vim_strchr(p, '.') = NUL;
3927 }
3928 return p;
3929}
3930#endif /* FEAT_CLIENTSERVER */
3931
3932#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
3933/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003934 * Replace termcodes such as <CR> and insert as key presses if there is room.
3935 */
3936 void
3937server_to_input_buf(str)
3938 char_u *str;
3939{
3940 char_u *ptr = NULL;
3941 char_u *cpo_save = p_cpo;
3942
3943 /* Set 'cpoptions' the way we want it.
3944 * B set - backslashes are *not* treated specially
3945 * k set - keycodes are *not* reverse-engineered
3946 * < unset - <Key> sequences *are* interpreted
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00003947 * The last but one parameter of replace_termcodes() is TRUE so that the
3948 * <lt> sequence is recognised - needed for a real backslash.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003949 */
3950 p_cpo = (char_u *)"Bk";
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00003951 str = replace_termcodes((char_u *)str, &ptr, FALSE, TRUE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003952 p_cpo = cpo_save;
3953
3954 if (*ptr != NUL) /* trailing CTRL-V results in nothing */
3955 {
3956 /*
3957 * Add the string to the input stream.
3958 * Can't use add_to_input_buf() here, we now have K_SPECIAL bytes.
3959 *
3960 * First clear typed characters from the typeahead buffer, there could
3961 * be half a mapping there. Then append to the existing string, so
3962 * that multiple commands from a client are concatenated.
3963 */
3964 if (typebuf.tb_maplen < typebuf.tb_len)
3965 del_typebuf(typebuf.tb_len - typebuf.tb_maplen, typebuf.tb_maplen);
3966 (void)ins_typebuf(str, REMAP_NONE, typebuf.tb_len, TRUE, FALSE);
3967
3968 /* Let input_available() know we inserted text in the typeahead
3969 * buffer. */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003970 typebuf_was_filled = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003971 }
3972 vim_free((char_u *)ptr);
3973}
3974
3975/*
3976 * Evaluate an expression that the client sent to a string.
3977 * Handles disabling error messages and disables debugging, otherwise Vim
3978 * hangs, waiting for "cont" to be typed.
3979 */
3980 char_u *
3981eval_client_expr_to_string(expr)
3982 char_u *expr;
3983{
3984 char_u *res;
3985 int save_dbl = debug_break_level;
3986 int save_ro = redir_off;
3987
3988 debug_break_level = -1;
3989 redir_off = 0;
3990 ++emsg_skip;
3991
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003992 res = eval_to_string(expr, NULL, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003993
3994 debug_break_level = save_dbl;
3995 redir_off = save_ro;
3996 --emsg_skip;
3997
Bram Moolenaar63a121b2005-12-11 21:36:39 +00003998 /* A client can tell us to redraw, but not to display the cursor, so do
3999 * that here. */
4000 setcursor();
4001 out_flush();
4002#ifdef FEAT_GUI
4003 if (gui.in_use)
4004 gui_update_cursor(FALSE, FALSE);
4005#endif
4006
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004007 return res;
4008}
4009
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004010/*
4011 * If conversion is needed, convert "data" from "client_enc" to 'encoding' and
4012 * return an allocated string. Otherwise return "data".
4013 * "*tofree" is set to the result when it needs to be freed later.
4014 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004015 char_u *
4016serverConvert(client_enc, data, tofree)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004017 char_u *client_enc UNUSED;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004018 char_u *data;
4019 char_u **tofree;
4020{
4021 char_u *res = data;
4022
4023 *tofree = NULL;
4024# ifdef FEAT_MBYTE
4025 if (client_enc != NULL && p_enc != NULL)
4026 {
4027 vimconv_T vimconv;
4028
4029 vimconv.vc_type = CONV_NONE;
4030 if (convert_setup(&vimconv, client_enc, p_enc) != FAIL
4031 && vimconv.vc_type != CONV_NONE)
4032 {
4033 res = string_convert(&vimconv, data, NULL);
4034 if (res == NULL)
4035 res = data;
4036 else
4037 *tofree = res;
4038 }
4039 convert_setup(&vimconv, NULL, NULL);
4040 }
4041# endif
4042 return res;
4043}
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004044#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004045
4046/*
4047 * When FEAT_FKMAP is defined, also compile the Farsi source code.
4048 */
4049#if defined(FEAT_FKMAP) || defined(PROTO)
4050# include "farsi.c"
4051#endif
4052
4053/*
4054 * When FEAT_ARABIC is defined, also compile the Arabic source code.
4055 */
4056#if defined(FEAT_ARABIC) || defined(PROTO)
4057# include "arabic.c"
4058#endif