blob: d9f8df4b45c4d3e58c8d4db77de1568cf228ec0b [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 Moolenaarbbc98db2012-02-12 01:55:55 +0100557#ifdef FEAT_MZSCHEME
558 /*
559 * Newer version of MzScheme (Racket) require earlier (trampolined)
560 * initialisation via scheme_main_setup.
561 * Implement this by initialising it as early as possible
562 * and splitting off remaining Vim main into vim_main2
563 */
564 {
565 /* Pack up preprocessed command line arguments.
566 * It is safe because Scheme does not access argc/argv. */
567 char *args[2];
568 args[0] = (char *)fname;
569 args[1] = (char *)&params;
570 return mzscheme_main(2, args);
571 }
572}
573
574int vim_main2(int argc, char **argv)
575{
576 char_u *fname = (char_u *)argv[0];
577 mparm_T params;
578
579 memcpy(&params, argv[1], sizeof(params));
580#endif
581
Bram Moolenaar58d98232005-07-23 22:25:46 +0000582 /* Execute --cmd arguments. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000583 exe_pre_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000584
Bram Moolenaar58d98232005-07-23 22:25:46 +0000585 /* Source startup scripts. */
586 source_startup_scripts(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000587
588#ifdef FEAT_EVAL
589 /*
590 * Read all the plugin files.
591 * Only when compiled with +eval, since most plugins need it.
592 */
593 if (p_lpl)
594 {
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000595# ifdef VMS /* Somehow VMS doesn't handle the "**". */
596 source_runtime((char_u *)"plugin/*.vim", TRUE);
597# else
Bram Moolenaar07d4d732005-10-03 22:04:08 +0000598 source_runtime((char_u *)"plugin/**/*.vim", TRUE);
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000599# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000600 TIME_MSG("loading plugins");
601 }
602#endif
603
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000604#ifdef FEAT_DIFF
605 /* Decide about window layout for diff mode after reading vimrc. */
606 if (params.diff_mode && params.window_layout == 0)
607 {
608 if (diffopt_horizontal())
609 params.window_layout = WIN_HOR; /* use horizontal split */
610 else
611 params.window_layout = WIN_VER; /* use vertical split */
612 }
613#endif
614
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000615 /*
616 * Recovery mode without a file name: List swap files.
617 * This uses the 'dir' option, therefore it must be after the
618 * initializations.
619 */
620 if (recoverymode && fname == NULL)
621 {
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200622 recover_names(NULL, TRUE, 0, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000623 mch_exit(0);
624 }
625
626 /*
627 * Set a few option defaults after reading .vimrc files:
628 * 'title' and 'icon', Unix: 'shellpipe' and 'shellredir'.
629 */
630 set_init_3();
631 TIME_MSG("inits 3");
632
633 /*
634 * "-n" argument: Disable swap file by setting 'updatecount' to 0.
635 * Note that this overrides anything from a vimrc file.
636 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000637 if (params.no_swap_file)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000638 p_uc = 0;
639
640#ifdef FEAT_FKMAP
641 if (curwin->w_p_rl && p_altkeymap)
642 {
643 p_hkmap = FALSE; /* Reset the Hebrew keymap mode */
644# ifdef FEAT_ARABIC
645 curwin->w_p_arab = FALSE; /* Reset the Arabic keymap mode */
646# endif
647 p_fkmap = TRUE; /* Set the Farsi keymap mode */
648 }
649#endif
650
651#ifdef FEAT_GUI
652 if (gui.starting)
653 {
654#if defined(UNIX) || defined(VMS)
655 /* When something caused a message from a vimrc script, need to output
656 * an extra newline before the shell prompt. */
657 if (did_emsg || msg_didout)
658 putchar('\n');
659#endif
660
661 gui_start(); /* will set full_screen to TRUE */
662 TIME_MSG("starting GUI");
663
664 /* When running "evim" or "gvim -y" we need the menus, exit if we
665 * don't have them. */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000666 if (!gui.in_use && params.evim_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000667 mch_exit(1);
668 }
669#endif
670
671#ifdef SPAWNO /* special MSDOS swapping library */
672 init_SPAWNO("", SWAP_ANY);
673#endif
674
675#ifdef FEAT_VIMINFO
676 /*
Bram Moolenaard812df62008-11-09 12:46:09 +0000677 * Read in registers, history etc, but not marks, from the viminfo file.
678 * This is where v:oldfiles gets filled.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000679 */
680 if (*p_viminfo != NUL)
681 {
Bram Moolenaard812df62008-11-09 12:46:09 +0000682 read_viminfo(NULL, VIF_WANT_INFO | VIF_GET_OLDFILES);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000683 TIME_MSG("reading viminfo");
684 }
685#endif
686
687#ifdef FEAT_QUICKFIX
688 /*
689 * "-q errorfile": Load the error file now.
690 * If the error file can't be read, exit before doing anything else.
691 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000692 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000693 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000694 if (params.use_ef != NULL)
695 set_string_option_direct((char_u *)"ef", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000696 params.use_ef, OPT_FREE, SID_CARG);
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200697 vim_snprintf((char *)IObuff, IOSIZE, "cfile %s", p_ef);
698 if (qf_init(NULL, p_ef, p_efm, TRUE, IObuff) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000699 {
700 out_char('\n');
701 mch_exit(3);
702 }
703 TIME_MSG("reading errorfile");
704 }
705#endif
706
707 /*
708 * Start putting things on the screen.
709 * Scroll screen down before drawing over it
710 * Clear screen now, so file message will not be cleared.
711 */
712 starting = NO_BUFFERS;
713 no_wait_return = FALSE;
714 if (!exmode_active)
715 msg_scroll = FALSE;
716
717#ifdef FEAT_GUI
718 /*
719 * This seems to be required to make callbacks to be called now, instead
720 * of after things have been put on the screen, which then may be deleted
721 * when getting a resize callback.
722 * For the Mac this handles putting files dropped on the Vim icon to
723 * global_alist.
724 */
725 if (gui.in_use)
726 {
727# ifdef FEAT_SUN_WORKSHOP
728 if (!usingSunWorkShop)
729# endif
730 gui_wait_for_chars(50L);
731 TIME_MSG("GUI delay");
732 }
733#endif
734
735#if defined(FEAT_GUI_PHOTON) && defined(FEAT_CLIPBOARD)
736 qnx_clip_init();
737#endif
738
Bram Moolenaarc8bbaa32010-07-14 16:54:21 +0200739#if defined(MACOS_X) && defined(FEAT_CLIPBOARD)
740 clip_init(TRUE);
741#endif
742
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000743#ifdef FEAT_XCLIPBOARD
744 /* Start using the X clipboard, unless the GUI was started. */
745# ifdef FEAT_GUI
746 if (!gui.in_use)
747# endif
748 {
749 setup_term_clip();
750 TIME_MSG("setup clipboard");
751 }
752#endif
753
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000754#ifdef FEAT_CLIENTSERVER
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000755 /* Prepare for being a Vim server. */
756 prepare_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000757#endif
758
759 /*
760 * If "-" argument given: Read file from stdin.
761 * Do this before starting Raw mode, because it may change things that the
762 * writing end of the pipe doesn't like, e.g., in case stdin and stderr
763 * are the same terminal: "cat | vim -".
764 * Using autocommands here may cause trouble...
765 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000766 if (params.edit_type == EDIT_STDIN && !recoverymode)
767 read_stdin();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000768
769#if defined(UNIX) || defined(VMS)
770 /* When switching screens and something caused a message from a vimrc
771 * script, need to output an extra newline on exit. */
772 if ((did_emsg || msg_didout) && *T_TI != NUL)
773 newline_on_exit = TRUE;
774#endif
775
776 /*
777 * When done something that is not allowed or error message call
778 * wait_return. This must be done before starttermcap(), because it may
779 * switch to another screen. It must be done after settmode(TMODE_RAW),
780 * because we want to react on a single key stroke.
781 * Call settmode and starttermcap here, so the T_KS and T_TI may be
Bram Moolenaar49325942007-05-10 19:19:59 +0000782 * defined by termcapinit and redefined in .exrc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000783 */
784 settmode(TMODE_RAW);
785 TIME_MSG("setting raw mode");
786
787 if (need_wait_return || msg_didany)
788 {
789 wait_return(TRUE);
790 TIME_MSG("waiting for return");
791 }
792
793 starttermcap(); /* start termcap if not done by wait_return() */
794 TIME_MSG("start termcap");
795
796#ifdef FEAT_MOUSE
797 setmouse(); /* may start using the mouse */
798#endif
799 if (scroll_region)
800 scroll_region_reset(); /* In case Rows changed */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000801 scroll_start(); /* may scroll the screen to the right position */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000802
803 /*
804 * Don't clear the screen when starting in Ex mode, unless using the GUI.
805 */
806 if (exmode_active
807#ifdef FEAT_GUI
808 && !gui.in_use
809#endif
810 )
811 must_redraw = CLEAR;
812 else
813 {
814 screenclear(); /* clear screen */
815 TIME_MSG("clearing screen");
816 }
817
818#ifdef FEAT_CRYPT
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000819 if (params.ask_for_key)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000820 {
Bram Moolenaar40e6a712010-05-16 22:32:54 +0200821 (void)blowfish_self_test();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000822 (void)get_crypt_key(TRUE, TRUE);
823 TIME_MSG("getting crypt key");
824 }
825#endif
826
827 no_wait_return = TRUE;
828
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000829 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000830 * Create the requested number of windows and edit buffers in them.
831 * Also does recovery if "recoverymode" set.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000832 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000833 create_windows(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000834 TIME_MSG("opening buffers");
835
Bram Moolenaar867a4b72007-03-18 20:51:46 +0000836#ifdef FEAT_EVAL
837 /* clear v:swapcommand */
838 set_vim_var_string(VV_SWAPCOMMAND, NULL, -1);
839#endif
840
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000841 /* Ex starts at last line of the file */
842 if (exmode_active)
843 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
844
845#ifdef FEAT_AUTOCMD
846 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
847 TIME_MSG("BufEnter autocommands");
848#endif
849 setpcmark();
850
851#ifdef FEAT_QUICKFIX
852 /*
853 * When started with "-q errorfile" jump to first error now.
854 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000855 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000856 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000857 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000858 TIME_MSG("jump to first error");
859 }
860#endif
861
862#ifdef FEAT_WINDOWS
863 /*
864 * If opened more than one window, start editing files in the other
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000865 * windows.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000866 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000867 edit_buffers(&params);
868#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000869
870#ifdef FEAT_DIFF
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000871 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000872 {
873 win_T *wp;
874
875 /* set options in each window for "vimdiff". */
876 for (wp = firstwin; wp != NULL; wp = wp->w_next)
877 diff_win_options(wp, TRUE);
878 }
879#endif
880
881 /*
882 * Shorten any of the filenames, but only when absolute.
883 */
884 shorten_fnames(FALSE);
885
886 /*
887 * Need to jump to the tag before executing the '-c command'.
888 * Makes "vim -c '/return' -t main" work.
889 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000890 if (params.tagname != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000891 {
Bram Moolenaar146522e2005-12-16 21:55:46 +0000892#if defined(HAS_SWAP_EXISTS_ACTION)
893 swap_exists_did_quit = FALSE;
894#endif
895
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000896 vim_snprintf((char *)IObuff, IOSIZE, "ta %s", params.tagname);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000897 do_cmdline_cmd(IObuff);
898 TIME_MSG("jumping to tag");
Bram Moolenaar146522e2005-12-16 21:55:46 +0000899
900#if defined(HAS_SWAP_EXISTS_ACTION)
901 /* If the user doesn't want to edit the file then we quit here. */
902 if (swap_exists_did_quit)
903 getout(1);
904#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000905 }
906
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000907 /* Execute any "+", "-c" and "-S" arguments. */
908 if (params.n_commands > 0)
909 exe_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000910
911 RedrawingDisabled = 0;
912 redraw_all_later(NOT_VALID);
913 no_wait_return = FALSE;
914 starting = 0;
915
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000916#ifdef FEAT_TERMRESPONSE
917 /* Requesting the termresponse is postponed until here, so that a "-c q"
918 * argument doesn't make it appear in the shell Vim was started from. */
919 may_req_termresponse();
920#endif
921
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000922 /* start in insert mode */
923 if (p_im)
924 need_start_insertmode = TRUE;
925
926#ifdef FEAT_AUTOCMD
927 apply_autocmds(EVENT_VIMENTER, NULL, NULL, FALSE, curbuf);
928 TIME_MSG("VimEnter autocommands");
929#endif
930
931#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
932 /* When a startup script or session file setup for diff'ing and
933 * scrollbind, sync the scrollbind now. */
934 if (curwin->w_p_diff && curwin->w_p_scb)
935 {
936 update_topline();
937 check_scrollbind((linenr_T)0, 0L);
938 TIME_MSG("diff scrollbinding");
939 }
940#endif
941
942#if defined(WIN3264) && !defined(FEAT_GUI_W32)
943 mch_set_winsize_now(); /* Allow winsize changes from now on */
944#endif
945
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000946#if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
947 /* When tab pages were created, may need to update the tab pages line and
948 * scrollbars. This is skipped while creating them. */
949 if (first_tabpage->tp_next != NULL)
950 {
951 out_flush();
952 gui_init_which_components(NULL);
953 gui_update_scrollbars(TRUE);
954 }
955 need_mouse_correct = TRUE;
956#endif
957
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000958 /* If ":startinsert" command used, stuff a dummy command to be able to
959 * call normal_cmd(), which will then start Insert mode. */
960 if (restart_edit != 0)
Bram Moolenaarebefac62005-12-28 22:39:57 +0000961 stuffcharReadbuff(K_NOP);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000962
963#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200964 if (netbeansArg != NULL && strncmp("-nb", netbeansArg, 3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200965 {
966# ifdef FEAT_GUI
Bram Moolenaar173c9852010-09-29 17:27:01 +0200967# if !defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK) \
Bram Moolenaar67c53842010-05-22 18:28:27 +0200968 && !defined(FEAT_GUI_W32)
969 if (gui.in_use)
970 {
971 mch_errmsg(_("netbeans is not supported with this GUI\n"));
972 mch_exit(2);
973 }
974# endif
975# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000976 /* Tell the client that it can start sending commands. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200977 netbeans_open(netbeansArg + 3, TRUE);
Bram Moolenaar67c53842010-05-22 18:28:27 +0200978 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000979#endif
980
981 TIME_MSG("before starting main loop");
982
983 /*
984 * Call the main command loop. This never returns.
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100985 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000986 main_loop(FALSE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000987
988 return 0;
989}
990#endif /* PROTO */
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100991#endif /* NO_VIM_MAIN */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000992
993/*
994 * Main loop: Execute Normal mode commands until exiting Vim.
995 * Also used to handle commands in the command-line window, until the window
996 * is closed.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000997 * Also used to handle ":visual" command after ":global": execute Normal mode
998 * commands, return when entering Ex mode. "noexmode" is TRUE then.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000999 */
1000 void
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001001main_loop(cmdwin, noexmode)
1002 int cmdwin; /* TRUE when working in the command-line window */
1003 int noexmode; /* TRUE when return on entering Ex mode */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001004{
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001005 oparg_T oa; /* operator arguments */
1006 int previous_got_int = FALSE; /* "got_int" was TRUE */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001007#ifdef FEAT_CONCEAL
1008 linenr_T conceal_old_cursor_line = 0;
1009 linenr_T conceal_new_cursor_line = 0;
1010 int conceal_update_lines = FALSE;
1011#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001012
1013#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
1014 /* Setup to catch a terminating error from the X server. Just ignore
1015 * it, restore the state and continue. This might not always work
1016 * properly, but at least we don't exit unexpectedly when the X server
1017 * exists while Vim is running in a console. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001018 if (!cmdwin && !noexmode && SETJMP(x_jump_env))
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001019 {
1020 State = NORMAL;
1021# ifdef FEAT_VISUAL
1022 VIsual_active = FALSE;
1023# endif
1024 got_int = TRUE;
1025 need_wait_return = FALSE;
1026 global_busy = FALSE;
1027 exmode_active = 0;
1028 skip_redraw = FALSE;
1029 RedrawingDisabled = 0;
1030 no_wait_return = 0;
Bram Moolenaar7701c242011-10-04 16:43:53 +02001031 vgetc_busy = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001032# ifdef FEAT_EVAL
1033 emsg_skip = 0;
1034# endif
1035 emsg_off = 0;
1036# ifdef FEAT_MOUSE
1037 setmouse();
1038# endif
1039 settmode(TMODE_RAW);
1040 starttermcap();
1041 scroll_start();
1042 redraw_later_clear();
1043 }
1044#endif
1045
1046 clear_oparg(&oa);
1047 while (!cmdwin
1048#ifdef FEAT_CMDWIN
1049 || cmdwin_result == 0
1050#endif
1051 )
1052 {
1053 if (stuff_empty())
1054 {
1055 did_check_timestamps = FALSE;
1056 if (need_check_timestamps)
1057 check_timestamps(FALSE);
1058 if (need_wait_return) /* if wait_return still needed ... */
1059 wait_return(FALSE); /* ... call it now */
1060 if (need_start_insertmode && goto_im()
1061#ifdef FEAT_VISUAL
1062 && !VIsual_active
1063#endif
1064 )
1065 {
1066 need_start_insertmode = FALSE;
1067 stuffReadbuff((char_u *)"i"); /* start insert mode next */
1068 /* skip the fileinfo message now, because it would be shown
1069 * after insert mode finishes! */
1070 need_fileinfo = FALSE;
1071 }
1072 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001073
1074 /* Reset "got_int" now that we got back to the main loop. Except when
1075 * inside a ":g/pat/cmd" command, then the "got_int" needs to abort
1076 * the ":g" command.
1077 * For ":g/pat/vi" we reset "got_int" when used once. When used
1078 * a second time we go back to Ex mode and abort the ":g" command. */
1079 if (got_int)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001080 {
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001081 if (noexmode && global_busy && !exmode_active && previous_got_int)
1082 {
1083 /* Typed two CTRL-C in a row: go back to ex mode as if "Q" was
1084 * used and keep "got_int" set, so that it aborts ":g". */
1085 exmode_active = EXMODE_NORMAL;
1086 State = NORMAL;
1087 }
1088 else if (!global_busy || !exmode_active)
1089 {
1090 if (!quit_more)
1091 (void)vgetc(); /* flush all buffers */
1092 got_int = FALSE;
1093 }
1094 previous_got_int = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001095 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001096 else
1097 previous_got_int = FALSE;
1098
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001099 if (!exmode_active)
1100 msg_scroll = FALSE;
1101 quit_more = FALSE;
1102
1103 /*
1104 * If skip redraw is set (for ":" in wait_return()), don't redraw now.
1105 * If there is nothing in the stuff_buffer or do_redraw is TRUE,
1106 * update cursor and redraw.
1107 */
1108 if (skip_redraw || exmode_active)
1109 skip_redraw = FALSE;
1110 else if (do_redraw || stuff_empty())
1111 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001112#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001113 /* Trigger CursorMoved if the cursor moved. */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001114 if (!finish_op && (
1115# ifdef FEAT_AUTOCMD
1116 has_cursormoved()
1117# endif
1118# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
1119 ||
1120# endif
1121# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001122 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001123# endif
1124 )
1125 && !equalpos(last_cursormoved, curwin->w_cursor))
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001126 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001127# ifdef FEAT_AUTOCMD
1128 if (has_cursormoved())
1129 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL,
1130 FALSE, curbuf);
1131# endif
1132# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001133 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001134 {
1135 conceal_old_cursor_line = last_cursormoved.lnum;
1136 conceal_new_cursor_line = curwin->w_cursor.lnum;
1137 conceal_update_lines = TRUE;
1138 }
1139# endif
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001140 last_cursormoved = curwin->w_cursor;
1141 }
1142#endif
1143
Bram Moolenaar33aec762006-01-22 23:30:12 +00001144#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
1145 /* Scroll-binding for diff mode may have been postponed until
1146 * here. Avoids doing it for every change. */
1147 if (diff_need_scrollbind)
1148 {
1149 check_scrollbind((linenr_T)0, 0L);
1150 diff_need_scrollbind = FALSE;
1151 }
1152#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001153#if defined(FEAT_FOLDING) && defined(FEAT_VISUAL)
1154 /* Include a closed fold completely in the Visual area. */
1155 foldAdjustVisual();
1156#endif
1157#ifdef FEAT_FOLDING
1158 /*
1159 * When 'foldclose' is set, apply 'foldlevel' to folds that don't
1160 * contain the cursor.
1161 * When 'foldopen' is "all", open the fold(s) under the cursor.
1162 * This may mark the window for redrawing.
1163 */
1164 if (hasAnyFolding(curwin) && !char_avail())
1165 {
1166 foldCheckClose();
1167 if (fdo_flags & FDO_ALL)
1168 foldOpenCursor();
1169 }
1170#endif
1171
1172 /*
1173 * Before redrawing, make sure w_topline is correct, and w_leftcol
1174 * if lines don't wrap, and w_skipcol if lines wrap.
1175 */
1176 update_topline();
1177 validate_cursor();
1178
1179#ifdef FEAT_VISUAL
1180 if (VIsual_active)
1181 update_curbuf(INVERTED);/* update inverted part */
1182 else
1183#endif
1184 if (must_redraw)
1185 update_screen(0);
1186 else if (redraw_cmdline || clear_cmdline)
1187 showmode();
1188#ifdef FEAT_WINDOWS
1189 redraw_statuslines();
1190#endif
1191#ifdef FEAT_TITLE
1192 if (need_maketitle)
1193 maketitle();
1194#endif
1195 /* display message after redraw */
1196 if (keep_msg != NULL)
1197 {
1198 char_u *p;
1199
1200 /* msg_attr_keep() will set keep_msg to NULL, must free the
1201 * string here. */
1202 p = keep_msg;
Bram Moolenaar238a5642006-02-21 22:12:05 +00001203 keep_msg = NULL;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001204 msg_attr(p, keep_msg_attr);
1205 vim_free(p);
1206 }
1207 if (need_fileinfo) /* show file info after redraw */
1208 {
1209 fileinfo(FALSE, TRUE, FALSE);
1210 need_fileinfo = FALSE;
1211 }
1212
1213 emsg_on_display = FALSE; /* can delete error message now */
1214 did_emsg = FALSE;
1215 msg_didany = FALSE; /* reset lines_left in msg_start() */
Bram Moolenaar661b1822005-07-28 22:36:45 +00001216 may_clear_sb_text(); /* clear scroll-back text on next msg */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001217 showruler(FALSE);
1218
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001219# if defined(FEAT_CONCEAL)
1220 if (conceal_update_lines
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001221 && (conceal_old_cursor_line != conceal_new_cursor_line
1222 || conceal_cursor_line(curwin)
1223 || need_cursor_line_redraw))
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001224 {
Bram Moolenaarc2b4c622011-02-15 16:29:59 +01001225 if (conceal_old_cursor_line != conceal_new_cursor_line
1226 && conceal_old_cursor_line
1227 <= curbuf->b_ml.ml_line_count)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001228 update_single_line(curwin, conceal_old_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001229 update_single_line(curwin, conceal_new_cursor_line);
1230 curwin->w_valid &= ~VALID_CROW;
1231 }
1232# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001233 setcursor();
1234 cursor_on();
1235
1236 do_redraw = FALSE;
Bram Moolenaar3f269672009-11-03 11:11:11 +00001237
1238#ifdef STARTUPTIME
1239 /* Now that we have drawn the first screen all the startup stuff
1240 * has been done, close any file for startup messages. */
1241 if (time_fd != NULL)
1242 {
1243 TIME_MSG("first screen update");
1244 TIME_MSG("--- VIM STARTED ---");
1245 fclose(time_fd);
1246 time_fd = NULL;
1247 }
1248#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001249 }
1250#ifdef FEAT_GUI
1251 if (need_mouse_correct)
1252 gui_mouse_correct();
1253#endif
1254
1255 /*
1256 * Update w_curswant if w_set_curswant has been set.
1257 * Postponed until here to avoid computing w_virtcol too often.
1258 */
1259 update_curswant();
1260
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001261#ifdef FEAT_EVAL
1262 /*
1263 * May perform garbage collection when waiting for a character, but
1264 * only at the very toplevel. Otherwise we may be using a List or
1265 * Dict internally somewhere.
1266 * "may_garbage_collect" is reset in vgetc() which is invoked through
1267 * do_exmode() and normal_cmd().
1268 */
1269 may_garbage_collect = (!cmdwin && !noexmode);
1270#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001271 /*
1272 * If we're invoked as ex, do a round of ex commands.
1273 * Otherwise, get and execute a normal mode command.
1274 */
1275 if (exmode_active)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001276 {
1277 if (noexmode) /* End of ":global/path/visual" commands */
1278 return;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001279 do_exmode(exmode_active == EXMODE_VIM);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001280 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001281 else
1282 normal_cmd(&oa, TRUE);
1283 }
1284}
1285
1286
1287#if defined(USE_XSMP) || defined(FEAT_GUI_MSWIN) || defined(PROTO)
1288/*
1289 * Exit, but leave behind swap files for modified buffers.
1290 */
1291 void
1292getout_preserve_modified(exitval)
1293 int exitval;
1294{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001295# if defined(SIGHUP) && defined(SIG_IGN)
1296 /* Ignore SIGHUP, because a dropped connection causes a read error, which
1297 * makes Vim exit and then handling SIGHUP causes various reentrance
1298 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001299 signal(SIGHUP, SIG_IGN);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001300# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001301
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001302 ml_close_notmod(); /* close all not-modified buffers */
1303 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
1304 ml_close_all(FALSE); /* close all memfiles, without deleting */
1305 getout(exitval); /* exit Vim properly */
1306}
1307#endif
1308
1309
1310/* Exit properly */
1311 void
1312getout(exitval)
1313 int exitval;
1314{
1315#ifdef FEAT_AUTOCMD
1316 buf_T *buf;
1317 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001318 tabpage_T *tp, *next_tp;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001319#endif
1320
1321 exiting = TRUE;
1322
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001323 /* When running in Ex mode an error causes us to exit with a non-zero exit
1324 * code. POSIX requires this, although it's not 100% clear from the
1325 * standard. */
1326 if (exmode_active)
1327 exitval += ex_exitval;
1328
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001329 /* Position the cursor on the last screen line, below all the text */
1330#ifdef FEAT_GUI
1331 if (!gui.in_use)
1332#endif
1333 windgoto((int)Rows - 1, 0);
1334
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +00001335#if defined(FEAT_EVAL) || defined(FEAT_SYN_HL)
1336 /* Optionally print hashtable efficiency. */
1337 hash_debug_results();
1338#endif
1339
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001340#ifdef FEAT_GUI
1341 msg_didany = FALSE;
1342#endif
1343
1344#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001345 if (get_vim_var_nr(VV_DYING) <= 1)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001346 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001347 /* Trigger BufWinLeave for all windows, but only once per buffer. */
1348# if defined FEAT_WINDOWS
1349 for (tp = first_tabpage; tp != NULL; tp = next_tp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001350 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001351 next_tp = tp->tp_next;
1352 for (wp = (tp == curtab)
1353 ? firstwin : tp->tp_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001354 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001355 buf = wp->w_buffer;
1356 if (buf->b_changedtick != -1)
1357 {
1358 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname,
1359 buf->b_fname, FALSE, buf);
1360 buf->b_changedtick = -1; /* note that we did it already */
1361 /* start all over, autocommands may mess up the lists */
1362 next_tp = first_tabpage;
1363 break;
1364 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001365 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001366 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001367# else
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001368 apply_autocmds(EVENT_BUFWINLEAVE, curbuf, curbuf->b_fname,
1369 FALSE, curbuf);
Bram Moolenaarf740b292006-02-16 22:11:02 +00001370# endif
Bram Moolenaar33aec762006-01-22 23:30:12 +00001371
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001372 /* Trigger BufUnload for buffers that are loaded */
1373 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1374 if (buf->b_ml.ml_mfp != NULL)
1375 {
1376 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001377 FALSE, buf);
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001378 if (!buf_valid(buf)) /* autocmd may delete the buffer */
1379 break;
1380 }
1381 apply_autocmds(EVENT_VIMLEAVEPRE, NULL, NULL, FALSE, curbuf);
1382 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001383#endif
1384
1385#ifdef FEAT_VIMINFO
1386 if (*p_viminfo != NUL)
1387 /* Write out the registers, history, marks etc, to the viminfo file */
1388 write_viminfo(NULL, FALSE);
1389#endif
1390
1391#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001392 if (get_vim_var_nr(VV_DYING) <= 1)
1393 apply_autocmds(EVENT_VIMLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001394#endif
1395
Bram Moolenaar05159a02005-02-26 23:04:13 +00001396#ifdef FEAT_PROFILE
1397 profile_dump();
1398#endif
1399
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001400 if (did_emsg
1401#ifdef FEAT_GUI
1402 || (gui.in_use && msg_didany && p_verbose > 0)
1403#endif
1404 )
1405 {
1406 /* give the user a chance to read the (error) message */
1407 no_wait_return = FALSE;
1408 wait_return(FALSE);
1409 }
1410
1411#ifdef FEAT_AUTOCMD
1412 /* Position the cursor again, the autocommands may have moved it */
1413# ifdef FEAT_GUI
1414 if (!gui.in_use)
1415# endif
1416 windgoto((int)Rows - 1, 0);
1417#endif
1418
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001419#ifdef FEAT_LUA
1420 lua_end();
1421#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001422#ifdef FEAT_MZSCHEME
1423 mzscheme_end();
1424#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001425#ifdef FEAT_TCL
1426 tcl_end();
1427#endif
1428#ifdef FEAT_RUBY
1429 ruby_end();
1430#endif
1431#ifdef FEAT_PYTHON
1432 python_end();
1433#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001434#ifdef FEAT_PYTHON3
1435 python3_end();
1436#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001437#ifdef FEAT_PERL
1438 perl_end();
1439#endif
1440#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
1441 iconv_end();
1442#endif
1443#ifdef FEAT_NETBEANS_INTG
1444 netbeans_end();
1445#endif
Bram Moolenaar02b06312007-09-06 15:39:22 +00001446#ifdef FEAT_CSCOPE
1447 cs_end();
1448#endif
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001449#ifdef FEAT_EVAL
1450 if (garbage_collect_at_exit)
1451 garbage_collect();
1452#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001453
1454 mch_exit(exitval);
1455}
1456
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01001457#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001458/*
1459 * Get a (optional) count for a Vim argument.
1460 */
1461 static int
1462get_number_arg(p, idx, def)
1463 char_u *p; /* pointer to argument */
1464 int *idx; /* index in argument, is incremented */
1465 int def; /* default value */
1466{
1467 if (vim_isdigit(p[*idx]))
1468 {
1469 def = atoi((char *)&(p[*idx]));
1470 while (vim_isdigit(p[*idx]))
1471 *idx = *idx + 1;
1472 }
1473 return def;
1474}
1475
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001476#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1477/*
1478 * Setup to use the current locale (for ctype() and many other things).
1479 */
1480 static void
1481init_locale()
1482{
1483 setlocale(LC_ALL, "");
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001484
Bram Moolenaarc6af8122010-05-21 12:04:55 +02001485# ifdef FEAT_GUI_GTK
1486 /* Tell Gtk not to change our locale settings. */
1487 gtk_disable_setlocale();
1488# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001489# if defined(FEAT_FLOAT) && defined(LC_NUMERIC)
1490 /* Make sure strtod() uses a decimal point, not a comma. */
1491 setlocale(LC_NUMERIC, "C");
1492# endif
1493
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001494# ifdef WIN32
1495 /* Apparently MS-Windows printf() may cause a crash when we give it 8-bit
1496 * text while it's expecting text in the current locale. This call avoids
1497 * that. */
1498 setlocale(LC_CTYPE, "C");
1499# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001500
1501# ifdef FEAT_GETTEXT
1502 {
1503 int mustfree = FALSE;
1504 char_u *p;
1505
1506# ifdef DYNAMIC_GETTEXT
1507 /* Initialize the gettext library */
1508 dyn_libintl_init(NULL);
1509# endif
1510 /* expand_env() doesn't work yet, because chartab[] is not initialized
1511 * yet, call vim_getenv() directly */
1512 p = vim_getenv((char_u *)"VIMRUNTIME", &mustfree);
1513 if (p != NULL && *p != NUL)
1514 {
Bram Moolenaar1ad2f132007-06-19 18:27:18 +00001515 vim_snprintf((char *)NameBuff, MAXPATHL, "%s/lang", p);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001516 bindtextdomain(VIMPACKAGE, (char *)NameBuff);
1517 }
1518 if (mustfree)
1519 vim_free(p);
1520 textdomain(VIMPACKAGE);
1521 }
1522# endif
1523}
1524#endif
1525
1526/*
1527 * Check for: [r][e][g][vi|vim|view][diff][ex[im]]
1528 * If the executable name starts with "r" we disable shell commands.
1529 * If the next character is "e" we run in Easy mode.
1530 * If the next character is "g" we run the GUI version.
1531 * If the next characters are "view" we start in readonly mode.
1532 * If the next characters are "diff" or "vimdiff" we start in diff mode.
1533 * If the next characters are "ex" we start in Ex mode. If it's followed
1534 * by "im" use improved Ex mode.
1535 */
1536 static void
1537parse_command_name(parmp)
1538 mparm_T *parmp;
1539{
1540 char_u *initstr;
1541
1542 initstr = gettail((char_u *)parmp->argv[0]);
1543
1544#ifdef MACOS_X_UNIX
1545 /* An issue has been seen when launching Vim in such a way that
1546 * $PWD/$ARGV[0] or $ARGV[0] is not the absolute path to the
1547 * executable or a symbolic link of it. Until this issue is resolved
1548 * we prohibit the GUI from being used.
1549 */
1550 if (STRCMP(initstr, parmp->argv[0]) == 0)
1551 disallow_gui = TRUE;
1552
1553 /* TODO: On MacOS X default to gui if argv[0] ends in:
Bram Moolenaar27dc1952006-03-15 23:06:44 +00001554 * /Vim.app/Contents/MacOS/Vim */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001555#endif
1556
1557#ifdef FEAT_EVAL
1558 set_vim_var_string(VV_PROGNAME, initstr, -1);
1559#endif
1560
1561 if (TOLOWER_ASC(initstr[0]) == 'r')
1562 {
1563 restricted = TRUE;
1564 ++initstr;
1565 }
1566
Bram Moolenaarad249fb2010-05-07 16:35:04 +02001567 /* Use evim mode for "evim" and "egvim", not for "editor". */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001568 if (TOLOWER_ASC(initstr[0]) == 'e'
1569 && (TOLOWER_ASC(initstr[1]) == 'v'
1570 || TOLOWER_ASC(initstr[1]) == 'g'))
1571 {
1572#ifdef FEAT_GUI
1573 gui.starting = TRUE;
1574#endif
1575 parmp->evim_mode = TRUE;
1576 ++initstr;
1577 }
1578
Bram Moolenaar806875d2008-09-18 18:57:10 +00001579 /* "gvim" starts the GUI. Also accept "Gvim" for MS-Windows. */
1580 if (TOLOWER_ASC(initstr[0]) == 'g')
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001581 {
1582 main_start_gui();
1583#ifdef FEAT_GUI
1584 ++initstr;
1585#endif
1586 }
1587
1588 if (STRNICMP(initstr, "view", 4) == 0)
1589 {
1590 readonlymode = TRUE;
1591 curbuf->b_p_ro = TRUE;
1592 p_uc = 10000; /* don't update very often */
1593 initstr += 4;
1594 }
1595 else if (STRNICMP(initstr, "vim", 3) == 0)
1596 initstr += 3;
1597
1598 /* Catch "[r][g]vimdiff" and "[r][g]viewdiff". */
1599 if (STRICMP(initstr, "diff") == 0)
1600 {
1601#ifdef FEAT_DIFF
1602 parmp->diff_mode = TRUE;
1603#else
1604 mch_errmsg(_("This Vim was not compiled with the diff feature."));
1605 mch_errmsg("\n");
1606 mch_exit(2);
1607#endif
1608 }
1609
1610 if (STRNICMP(initstr, "ex", 2) == 0)
1611 {
1612 if (STRNICMP(initstr + 2, "im", 2) == 0)
1613 exmode_active = EXMODE_VIM;
1614 else
1615 exmode_active = EXMODE_NORMAL;
1616 change_compatible(TRUE); /* set 'compatible' */
1617 }
1618}
1619
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001620/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00001621 * Get the name of the display, before gui_prepare() removes it from
1622 * argv[]. Used for the xterm-clipboard display.
1623 *
Bram Moolenaar78e17622007-08-30 10:26:19 +00001624 * Also find the --server... arguments and --socketid and --windowid
Bram Moolenaar58d98232005-07-23 22:25:46 +00001625 */
Bram Moolenaar58d98232005-07-23 22:25:46 +00001626 static void
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001627early_arg_scan(parmp)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00001628 mparm_T *parmp UNUSED;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001629{
Bram Moolenaar03005972008-11-20 13:12:36 +00001630#if defined(FEAT_XCLIPBOARD) || defined(FEAT_CLIENTSERVER) \
1631 || !defined(FEAT_NETBEANS_INTG)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001632 int argc = parmp->argc;
1633 char **argv = parmp->argv;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001634 int i;
1635
1636 for (i = 1; i < argc; i++)
1637 {
1638 if (STRCMP(argv[i], "--") == 0)
1639 break;
1640# ifdef FEAT_XCLIPBOARD
1641 else if (STRICMP(argv[i], "-display") == 0
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001642# if defined(FEAT_GUI_GTK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001643 || STRICMP(argv[i], "--display") == 0
1644# endif
1645 )
1646 {
1647 if (i == argc - 1)
1648 mainerr_arg_missing((char_u *)argv[i]);
1649 xterm_display = argv[++i];
1650 }
1651# endif
1652# ifdef FEAT_CLIENTSERVER
1653 else if (STRICMP(argv[i], "--servername") == 0)
1654 {
1655 if (i == argc - 1)
1656 mainerr_arg_missing((char_u *)argv[i]);
1657 parmp->serverName_arg = (char_u *)argv[++i];
1658 }
Bram Moolenaareb94e552006-03-11 21:35:11 +00001659 else if (STRICMP(argv[i], "--serverlist") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001660 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001661 else if (STRNICMP(argv[i], "--remote", 8) == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001662 {
1663 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001664# ifdef FEAT_GUI
1665 if (strstr(argv[i], "-wait") != 0)
1666 /* don't fork() when starting the GUI to edit files ourself */
1667 gui.dofork = FALSE;
1668# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001669 }
1670# endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001671
1672# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1673# ifdef FEAT_GUI_W32
1674 else if (STRICMP(argv[i], "--windowid") == 0)
1675# else
Bram Moolenaar58d98232005-07-23 22:25:46 +00001676 else if (STRICMP(argv[i], "--socketid") == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001677# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001678 {
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001679 long_u id;
1680 int count;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001681
1682 if (i == argc - 1)
1683 mainerr_arg_missing((char_u *)argv[i]);
1684 if (STRNICMP(argv[i+1], "0x", 2) == 0)
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001685 count = sscanf(&(argv[i + 1][2]), SCANF_HEX_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001686 else
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001687 count = sscanf(argv[i + 1], SCANF_DECIMAL_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001688 if (count != 1)
1689 mainerr(ME_INVALID_ARG, (char_u *)argv[i]);
1690 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00001691# ifdef FEAT_GUI_W32
1692 win_socket_id = id;
1693# else
1694 gtk_socket_id = id;
1695# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001696 i++;
1697 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001698# endif
1699# ifdef FEAT_GUI_GTK
Bram Moolenaar58d98232005-07-23 22:25:46 +00001700 else if (STRICMP(argv[i], "--echo-wid") == 0)
1701 echo_wid_arg = TRUE;
1702# endif
Bram Moolenaar03005972008-11-20 13:12:36 +00001703# ifndef FEAT_NETBEANS_INTG
1704 else if (strncmp(argv[i], "-nb", (size_t)3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +02001705 {
1706 mch_errmsg(_("'-nb' cannot be used: not enabled at compile time\n"));
1707 mch_exit(2);
1708 }
Bram Moolenaar03005972008-11-20 13:12:36 +00001709# endif
1710
Bram Moolenaar58d98232005-07-23 22:25:46 +00001711 }
1712#endif
1713}
1714
1715/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001716 * Scan the command line arguments.
1717 */
1718 static void
1719command_line_scan(parmp)
1720 mparm_T *parmp;
1721{
1722 int argc = parmp->argc;
1723 char **argv = parmp->argv;
1724 int argv_idx; /* index in argv[n][] */
1725 int had_minmin = FALSE; /* found "--" argument */
1726 int want_argument; /* option argument with argument */
1727 int c;
Bram Moolenaar231334e2005-07-25 20:46:57 +00001728 char_u *p = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001729 long n;
1730
1731 --argc;
1732 ++argv;
1733 argv_idx = 1; /* active option letter is argv[0][argv_idx] */
1734 while (argc > 0)
1735 {
1736 /*
1737 * "+" or "+{number}" or "+/{pat}" or "+{command}" argument.
1738 */
1739 if (argv[0][0] == '+' && !had_minmin)
1740 {
1741 if (parmp->n_commands >= MAX_ARG_CMDS)
1742 mainerr(ME_EXTRA_CMD, NULL);
1743 argv_idx = -1; /* skip to next argument */
1744 if (argv[0][1] == NUL)
1745 parmp->commands[parmp->n_commands++] = (char_u *)"$";
1746 else
1747 parmp->commands[parmp->n_commands++] = (char_u *)&(argv[0][1]);
1748 }
1749
1750 /*
1751 * Optional argument.
1752 */
1753 else if (argv[0][0] == '-' && !had_minmin)
1754 {
1755 want_argument = FALSE;
1756 c = argv[0][argv_idx++];
1757#ifdef VMS
1758 /*
1759 * VMS only uses upper case command lines. Interpret "-X" as "-x"
1760 * and "-/X" as "-X".
1761 */
1762 if (c == '/')
1763 {
1764 c = argv[0][argv_idx++];
1765 c = TOUPPER_ASC(c);
1766 }
1767 else
1768 c = TOLOWER_ASC(c);
1769#endif
1770 switch (c)
1771 {
1772 case NUL: /* "vim -" read from stdin */
1773 /* "ex -" silent mode */
1774 if (exmode_active)
1775 silent_mode = TRUE;
1776 else
1777 {
1778 if (parmp->edit_type != EDIT_NONE)
1779 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1780 parmp->edit_type = EDIT_STDIN;
1781 read_cmd_fd = 2; /* read from stderr instead of stdin */
1782 }
1783 argv_idx = -1; /* skip to next argument */
1784 break;
1785
1786 case '-': /* "--" don't take any more option arguments */
1787 /* "--help" give help message */
1788 /* "--version" give version message */
1789 /* "--literal" take files literally */
1790 /* "--nofork" don't fork */
1791 /* "--noplugin[s]" skip plugins */
1792 /* "--cmd <cmd>" execute cmd before vimrc */
1793 if (STRICMP(argv[0] + argv_idx, "help") == 0)
1794 usage();
1795 else if (STRICMP(argv[0] + argv_idx, "version") == 0)
1796 {
1797 Columns = 80; /* need to init Columns */
1798 info_message = TRUE; /* use mch_msg(), not mch_errmsg() */
1799 list_version();
1800 msg_putchar('\n');
1801 msg_didout = FALSE;
1802 mch_exit(0);
1803 }
1804 else if (STRNICMP(argv[0] + argv_idx, "literal", 7) == 0)
1805 {
1806#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
1807 parmp->literal = TRUE;
1808#endif
1809 }
1810 else if (STRNICMP(argv[0] + argv_idx, "nofork", 6) == 0)
1811 {
1812#ifdef FEAT_GUI
1813 gui.dofork = FALSE; /* don't fork() when starting GUI */
1814#endif
1815 }
1816 else if (STRNICMP(argv[0] + argv_idx, "noplugin", 8) == 0)
1817 p_lpl = FALSE;
1818 else if (STRNICMP(argv[0] + argv_idx, "cmd", 3) == 0)
1819 {
1820 want_argument = TRUE;
1821 argv_idx += 3;
1822 }
Bram Moolenaaref94eec2009-11-11 13:22:11 +00001823 else if (STRNICMP(argv[0] + argv_idx, "startuptime", 11) == 0)
1824 {
1825 want_argument = TRUE;
1826 argv_idx += 11;
1827 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001828#ifdef FEAT_CLIENTSERVER
1829 else if (STRNICMP(argv[0] + argv_idx, "serverlist", 10) == 0)
1830 ; /* already processed -- no arg */
1831 else if (STRNICMP(argv[0] + argv_idx, "servername", 10) == 0
1832 || STRNICMP(argv[0] + argv_idx, "serversend", 10) == 0)
1833 {
1834 /* already processed -- snatch the following arg */
1835 if (argc > 1)
1836 {
1837 --argc;
1838 ++argv;
1839 }
1840 }
1841#endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001842#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1843# ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001844 else if (STRNICMP(argv[0] + argv_idx, "socketid", 8) == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001845# else
1846 else if (STRNICMP(argv[0] + argv_idx, "windowid", 8) == 0)
1847# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001848 {
1849 /* already processed -- snatch the following arg */
1850 if (argc > 1)
1851 {
1852 --argc;
1853 ++argv;
1854 }
1855 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001856#endif
1857#ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001858 else if (STRNICMP(argv[0] + argv_idx, "echo-wid", 8) == 0)
1859 {
1860 /* already processed, skip */
1861 }
1862#endif
1863 else
1864 {
1865 if (argv[0][argv_idx])
1866 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
1867 had_minmin = TRUE;
1868 }
1869 if (!want_argument)
1870 argv_idx = -1; /* skip to next argument */
1871 break;
1872
1873 case 'A': /* "-A" start in Arabic mode */
1874#ifdef FEAT_ARABIC
1875 set_option_value((char_u *)"arabic", 1L, NULL, 0);
1876#else
1877 mch_errmsg(_(e_noarabic));
1878 mch_exit(2);
1879#endif
1880 break;
1881
1882 case 'b': /* "-b" binary mode */
Bram Moolenaar231334e2005-07-25 20:46:57 +00001883 /* Needs to be effective before expanding file names, because
1884 * for Win32 this makes us edit a shortcut file itself,
1885 * instead of the file it links to. */
1886 set_options_bin(curbuf->b_p_bin, 1, 0);
1887 curbuf->b_p_bin = 1; /* binary file I/O */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001888 break;
1889
1890 case 'C': /* "-C" Compatible */
1891 change_compatible(TRUE);
1892 break;
1893
1894 case 'e': /* "-e" Ex mode */
1895 exmode_active = EXMODE_NORMAL;
1896 break;
1897
1898 case 'E': /* "-E" Improved Ex mode */
1899 exmode_active = EXMODE_VIM;
1900 break;
1901
1902 case 'f': /* "-f" GUI: run in foreground. Amiga: open
1903 window directly, not with newcli */
1904#ifdef FEAT_GUI
1905 gui.dofork = FALSE; /* don't fork() when starting GUI */
1906#endif
1907 break;
1908
1909 case 'g': /* "-g" start GUI */
1910 main_start_gui();
1911 break;
1912
1913 case 'F': /* "-F" start in Farsi mode: rl + fkmap set */
1914#ifdef FEAT_FKMAP
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00001915 p_fkmap = TRUE;
1916 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001917#else
1918 mch_errmsg(_(e_nofarsi));
1919 mch_exit(2);
1920#endif
1921 break;
1922
1923 case 'h': /* "-h" give help message */
1924#ifdef FEAT_GUI_GNOME
1925 /* Tell usage() to exit for "gvim". */
1926 gui.starting = FALSE;
1927#endif
1928 usage();
1929 break;
1930
1931 case 'H': /* "-H" start in Hebrew mode: rl + hkmap set */
1932#ifdef FEAT_RIGHTLEFT
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00001933 p_hkmap = TRUE;
1934 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001935#else
1936 mch_errmsg(_(e_nohebrew));
1937 mch_exit(2);
1938#endif
1939 break;
1940
1941 case 'l': /* "-l" lisp mode, 'lisp' and 'showmatch' on */
1942#ifdef FEAT_LISP
1943 set_option_value((char_u *)"lisp", 1L, NULL, 0);
1944 p_sm = TRUE;
1945#endif
1946 break;
1947
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001948 case 'M': /* "-M" no changes or writing of files */
1949 reset_modifiable();
1950 /* FALLTHROUGH */
1951
1952 case 'm': /* "-m" no writing of files */
1953 p_write = FALSE;
1954 break;
1955
1956 case 'y': /* "-y" easy mode */
1957#ifdef FEAT_GUI
1958 gui.starting = TRUE; /* start GUI a bit later */
1959#endif
1960 parmp->evim_mode = TRUE;
1961 break;
1962
1963 case 'N': /* "-N" Nocompatible */
1964 change_compatible(FALSE);
1965 break;
1966
1967 case 'n': /* "-n" no swap file */
Bram Moolenaar67c53842010-05-22 18:28:27 +02001968#ifdef FEAT_NETBEANS_INTG
1969 /* checking for "-nb", netbeans parameters */
1970 if (argv[0][argv_idx] == 'b')
1971 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02001972 netbeansArg = argv[0];
1973 argv_idx = -1; /* skip to next argument */
1974 }
1975 else
1976#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001977 parmp->no_swap_file = TRUE;
1978 break;
1979
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001980 case 'p': /* "-p[N]" open N tab pages */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00001981#ifdef TARGET_API_MAC_OSX
1982 /* For some reason on MacOS X, an argument like:
1983 -psn_0_10223617 is passed in when invoke from Finder
1984 or with the 'open' command */
1985 if (argv[0][argv_idx] == 's')
1986 {
1987 argv_idx = -1; /* bypass full -psn */
1988 main_start_gui();
1989 break;
1990 }
1991#endif
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001992#ifdef FEAT_WINDOWS
1993 /* default is 0: open window for each file */
1994 parmp->window_count = get_number_arg((char_u *)argv[0],
1995 &argv_idx, 0);
1996 parmp->window_layout = WIN_TABS;
1997#endif
1998 break;
1999
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002000 case 'o': /* "-o[N]" open N horizontal split windows */
2001#ifdef FEAT_WINDOWS
2002 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002003 parmp->window_count = get_number_arg((char_u *)argv[0],
2004 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002005 parmp->window_layout = WIN_HOR;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002006#endif
2007 break;
2008
2009 case 'O': /* "-O[N]" open N vertical split windows */
2010#if defined(FEAT_VERTSPLIT) && defined(FEAT_WINDOWS)
2011 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002012 parmp->window_count = get_number_arg((char_u *)argv[0],
2013 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002014 parmp->window_layout = WIN_VER;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002015#endif
2016 break;
2017
2018#ifdef FEAT_QUICKFIX
2019 case 'q': /* "-q" QuickFix mode */
2020 if (parmp->edit_type != EDIT_NONE)
2021 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2022 parmp->edit_type = EDIT_QF;
2023 if (argv[0][argv_idx]) /* "-q{errorfile}" */
2024 {
2025 parmp->use_ef = (char_u *)argv[0] + argv_idx;
2026 argv_idx = -1;
2027 }
2028 else if (argc > 1) /* "-q {errorfile}" */
2029 want_argument = TRUE;
2030 break;
2031#endif
2032
2033 case 'R': /* "-R" readonly mode */
2034 readonlymode = TRUE;
2035 curbuf->b_p_ro = TRUE;
2036 p_uc = 10000; /* don't update very often */
2037 break;
2038
2039 case 'r': /* "-r" recovery mode */
2040 case 'L': /* "-L" recovery mode */
2041 recoverymode = 1;
2042 break;
2043
2044 case 's':
2045 if (exmode_active) /* "-s" silent (batch) mode */
2046 silent_mode = TRUE;
2047 else /* "-s {scriptin}" read from script file */
2048 want_argument = TRUE;
2049 break;
2050
2051 case 't': /* "-t {tag}" or "-t{tag}" jump to tag */
2052 if (parmp->edit_type != EDIT_NONE)
2053 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2054 parmp->edit_type = EDIT_TAG;
2055 if (argv[0][argv_idx]) /* "-t{tag}" */
2056 {
2057 parmp->tagname = (char_u *)argv[0] + argv_idx;
2058 argv_idx = -1;
2059 }
2060 else /* "-t {tag}" */
2061 want_argument = TRUE;
2062 break;
2063
2064#ifdef FEAT_EVAL
2065 case 'D': /* "-D" Debugging */
2066 parmp->use_debug_break_level = 9999;
2067 break;
2068#endif
2069#ifdef FEAT_DIFF
2070 case 'd': /* "-d" 'diff' */
2071# ifdef AMIGA
2072 /* check for "-dev {device}" */
2073 if (argv[0][argv_idx] == 'e' && argv[0][argv_idx + 1] == 'v')
2074 want_argument = TRUE;
2075 else
2076# endif
2077 parmp->diff_mode = TRUE;
2078 break;
2079#endif
2080 case 'V': /* "-V{N}" Verbose level */
2081 /* default is 10: a little bit verbose */
2082 p_verbose = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2083 if (argv[0][argv_idx] != NUL)
2084 {
2085 set_option_value((char_u *)"verbosefile", 0L,
2086 (char_u *)argv[0] + argv_idx, 0);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002087 argv_idx = (int)STRLEN(argv[0]);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002088 }
2089 break;
2090
2091 case 'v': /* "-v" Vi-mode (as if called "vi") */
2092 exmode_active = 0;
2093#ifdef FEAT_GUI
2094 gui.starting = FALSE; /* don't start GUI */
2095#endif
2096 break;
2097
2098 case 'w': /* "-w{number}" set window height */
2099 /* "-w {scriptout}" write to script */
2100 if (vim_isdigit(((char_u *)argv[0])[argv_idx]))
2101 {
2102 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2103 set_option_value((char_u *)"window", n, NULL, 0);
2104 break;
2105 }
2106 want_argument = TRUE;
2107 break;
2108
2109#ifdef FEAT_CRYPT
2110 case 'x': /* "-x" encrypted reading/writing of files */
2111 parmp->ask_for_key = TRUE;
2112 break;
2113#endif
2114
2115 case 'X': /* "-X" don't connect to X server */
2116#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
2117 x_no_connect = TRUE;
2118#endif
2119 break;
2120
2121 case 'Z': /* "-Z" restricted mode */
2122 restricted = TRUE;
2123 break;
2124
2125 case 'c': /* "-c{command}" or "-c {command}" execute
2126 command */
2127 if (argv[0][argv_idx] != NUL)
2128 {
2129 if (parmp->n_commands >= MAX_ARG_CMDS)
2130 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar231334e2005-07-25 20:46:57 +00002131 parmp->commands[parmp->n_commands++] = (char_u *)argv[0]
2132 + argv_idx;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002133 argv_idx = -1;
2134 break;
2135 }
2136 /*FALLTHROUGH*/
2137 case 'S': /* "-S {file}" execute Vim script */
2138 case 'i': /* "-i {viminfo}" use for viminfo */
2139#ifndef FEAT_DIFF
2140 case 'd': /* "-d {device}" device (for Amiga) */
2141#endif
2142 case 'T': /* "-T {terminal}" terminal name */
2143 case 'u': /* "-u {vimrc}" vim inits file */
2144 case 'U': /* "-U {gvimrc}" gvim inits file */
2145 case 'W': /* "-W {scriptout}" overwrite */
2146#ifdef FEAT_GUI_W32
2147 case 'P': /* "-P {parent title}" MDI parent */
2148#endif
2149 want_argument = TRUE;
2150 break;
2151
2152 default:
2153 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
2154 }
2155
2156 /*
2157 * Handle option arguments with argument.
2158 */
2159 if (want_argument)
2160 {
2161 /*
2162 * Check for garbage immediately after the option letter.
2163 */
2164 if (argv[0][argv_idx] != NUL)
2165 mainerr(ME_GARBAGE, (char_u *)argv[0]);
2166
2167 --argc;
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002168 if (argc < 1 && c != 'S') /* -S has an optional argument */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002169 mainerr_arg_missing((char_u *)argv[0]);
2170 ++argv;
2171 argv_idx = -1;
2172
2173 switch (c)
2174 {
2175 case 'c': /* "-c {command}" execute command */
2176 case 'S': /* "-S {file}" execute Vim script */
2177 if (parmp->n_commands >= MAX_ARG_CMDS)
2178 mainerr(ME_EXTRA_CMD, NULL);
2179 if (c == 'S')
2180 {
2181 char *a;
2182
2183 if (argc < 1)
2184 /* "-S" without argument: use default session file
2185 * name. */
2186 a = SESSION_FILE;
2187 else if (argv[0][0] == '-')
2188 {
2189 /* "-S" followed by another option: use default
2190 * session file name. */
2191 a = SESSION_FILE;
2192 ++argc;
2193 --argv;
2194 }
2195 else
2196 a = argv[0];
2197 p = alloc((unsigned)(STRLEN(a) + 4));
2198 if (p == NULL)
2199 mch_exit(2);
2200 sprintf((char *)p, "so %s", a);
2201 parmp->cmds_tofree[parmp->n_commands] = TRUE;
2202 parmp->commands[parmp->n_commands++] = p;
2203 }
2204 else
Bram Moolenaar231334e2005-07-25 20:46:57 +00002205 parmp->commands[parmp->n_commands++] =
2206 (char_u *)argv[0];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002207 break;
2208
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002209 case '-':
2210 if (argv[-1][2] == 'c')
2211 {
2212 /* "--cmd {command}" execute command */
2213 if (parmp->n_pre_commands >= MAX_ARG_CMDS)
2214 mainerr(ME_EXTRA_CMD, NULL);
2215 parmp->pre_commands[parmp->n_pre_commands++] =
Bram Moolenaar231334e2005-07-25 20:46:57 +00002216 (char_u *)argv[0];
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002217 }
2218 /* "--startuptime <file>" already handled */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002219 break;
2220
2221 /* case 'd': -d {device} is handled in mch_check_win() for the
2222 * Amiga */
2223
2224#ifdef FEAT_QUICKFIX
2225 case 'q': /* "-q {errorfile}" QuickFix mode */
2226 parmp->use_ef = (char_u *)argv[0];
2227 break;
2228#endif
2229
2230 case 'i': /* "-i {viminfo}" use for viminfo */
2231 use_viminfo = (char_u *)argv[0];
2232 break;
2233
2234 case 's': /* "-s {scriptin}" read from script file */
2235 if (scriptin[0] != NULL)
2236 {
2237scripterror:
2238 mch_errmsg(_("Attempt to open script file again: \""));
2239 mch_errmsg(argv[-1]);
2240 mch_errmsg(" ");
2241 mch_errmsg(argv[0]);
2242 mch_errmsg("\"\n");
2243 mch_exit(2);
2244 }
2245 if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL)
2246 {
2247 mch_errmsg(_("Cannot open for reading: \""));
2248 mch_errmsg(argv[0]);
2249 mch_errmsg("\"\n");
2250 mch_exit(2);
2251 }
2252 if (save_typebuf() == FAIL)
2253 mch_exit(2); /* out of memory */
2254 break;
2255
2256 case 't': /* "-t {tag}" */
2257 parmp->tagname = (char_u *)argv[0];
2258 break;
2259
2260 case 'T': /* "-T {terminal}" terminal name */
2261 /*
2262 * The -T term argument is always available and when
2263 * HAVE_TERMLIB is supported it overrides the environment
2264 * variable TERM.
2265 */
2266#ifdef FEAT_GUI
2267 if (term_is_gui((char_u *)argv[0]))
2268 gui.starting = TRUE; /* start GUI a bit later */
2269 else
2270#endif
2271 parmp->term = (char_u *)argv[0];
2272 break;
2273
2274 case 'u': /* "-u {vimrc}" vim inits file */
2275 parmp->use_vimrc = (char_u *)argv[0];
2276 break;
2277
2278 case 'U': /* "-U {gvimrc}" gvim inits file */
2279#ifdef FEAT_GUI
2280 use_gvimrc = (char_u *)argv[0];
2281#endif
2282 break;
2283
2284 case 'w': /* "-w {nr}" 'window' value */
2285 /* "-w {scriptout}" append to script file */
2286 if (vim_isdigit(*((char_u *)argv[0])))
2287 {
2288 argv_idx = 0;
2289 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2290 set_option_value((char_u *)"window", n, NULL, 0);
2291 argv_idx = -1;
2292 break;
2293 }
2294 /*FALLTHROUGH*/
2295 case 'W': /* "-W {scriptout}" overwrite script file */
2296 if (scriptout != NULL)
2297 goto scripterror;
2298 if ((scriptout = mch_fopen(argv[0],
2299 c == 'w' ? APPENDBIN : WRITEBIN)) == NULL)
2300 {
2301 mch_errmsg(_("Cannot open for script output: \""));
2302 mch_errmsg(argv[0]);
2303 mch_errmsg("\"\n");
2304 mch_exit(2);
2305 }
2306 break;
2307
2308#ifdef FEAT_GUI_W32
2309 case 'P': /* "-P {parent title}" MDI parent */
2310 gui_mch_set_parent(argv[0]);
2311 break;
2312#endif
2313 }
2314 }
2315 }
2316
2317 /*
2318 * File name argument.
2319 */
2320 else
2321 {
2322 argv_idx = -1; /* skip to next argument */
2323
2324 /* Check for only one type of editing. */
2325 if (parmp->edit_type != EDIT_NONE && parmp->edit_type != EDIT_FILE)
2326 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2327 parmp->edit_type = EDIT_FILE;
2328
2329#ifdef MSWIN
2330 /* Remember if the argument was a full path before changing
2331 * slashes to backslashes. */
2332 if (argv[0][0] != NUL && argv[0][1] == ':' && argv[0][2] == '\\')
2333 parmp->full_path = TRUE;
2334#endif
2335
2336 /* Add the file to the global argument list. */
2337 if (ga_grow(&global_alist.al_ga, 1) == FAIL
2338 || (p = vim_strsave((char_u *)argv[0])) == NULL)
2339 mch_exit(2);
2340#ifdef FEAT_DIFF
2341 if (parmp->diff_mode && mch_isdir(p) && GARGCOUNT > 0
2342 && !mch_isdir(alist_name(&GARGLIST[0])))
2343 {
2344 char_u *r;
2345
2346 r = concat_fnames(p, gettail(alist_name(&GARGLIST[0])), TRUE);
2347 if (r != NULL)
2348 {
2349 vim_free(p);
2350 p = r;
2351 }
2352 }
2353#endif
2354#if defined(__CYGWIN32__) && !defined(WIN32)
2355 /*
2356 * If vim is invoked by non-Cygwin tools, convert away any
2357 * DOS paths, so things like .swp files are created correctly.
2358 * Look for evidence of non-Cygwin paths before we bother.
2359 * This is only for when using the Unix files.
2360 */
Bram Moolenaarad249fb2010-05-07 16:35:04 +02002361 if (strpbrk(p, "\\:") != NULL && !path_with_url(p))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002362 {
2363 char posix_path[PATH_MAX];
2364
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002365# if CYGWIN_VERSION_DLL_MAJOR >= 1007
2366 cygwin_conv_path(CCP_WIN_A_TO_POSIX, p, posix_path, PATH_MAX);
2367# else
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002368 cygwin_conv_to_posix_path(p, posix_path);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002369# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002370 vim_free(p);
2371 p = vim_strsave(posix_path);
2372 if (p == NULL)
2373 mch_exit(2);
2374 }
2375#endif
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002376
2377#ifdef USE_FNAME_CASE
2378 /* Make the case of the file name match the actual file. */
2379 fname_case(p, 0);
2380#endif
2381
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002382 alist_add(&global_alist, p,
2383#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
Bram Moolenaar231334e2005-07-25 20:46:57 +00002384 parmp->literal ? 2 : 0 /* add buffer nr after exp. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002385#else
2386 2 /* add buffer number now and use curbuf */
2387#endif
2388 );
2389
2390#if defined(FEAT_MBYTE) && defined(WIN32)
2391 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002392 /* Remember this argument has been added to the argument list.
2393 * Needed when 'encoding' is changed. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002394 used_file_arg(argv[0], parmp->literal, parmp->full_path,
Bram Moolenaar688e5f72008-07-24 11:51:40 +00002395# ifdef FEAT_DIFF
2396 parmp->diff_mode
2397# else
2398 FALSE
2399# endif
2400 );
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002401 }
2402#endif
2403 }
2404
2405 /*
2406 * If there are no more letters after the current "-", go to next
2407 * argument. argv_idx is set to -1 when the current argument is to be
2408 * skipped.
2409 */
2410 if (argv_idx <= 0 || argv[0][argv_idx] == NUL)
2411 {
2412 --argc;
2413 ++argv;
2414 argv_idx = 1;
2415 }
2416 }
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002417
2418#ifdef FEAT_EVAL
2419 /* If there is a "+123" or "-c" command, set v:swapcommand to the first
2420 * one. */
2421 if (parmp->n_commands > 0)
2422 {
2423 p = alloc((unsigned)STRLEN(parmp->commands[0]) + 3);
2424 if (p != NULL)
2425 {
2426 sprintf((char *)p, ":%s\r", parmp->commands[0]);
2427 set_vim_var_string(VV_SWAPCOMMAND, p, -1);
2428 vim_free(p);
2429 }
2430 }
2431#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002432}
2433
2434/*
2435 * Print a warning if stdout is not a terminal.
2436 * When starting in Ex mode and commands come from a file, set Silent mode.
2437 */
2438 static void
2439check_tty(parmp)
2440 mparm_T *parmp;
2441{
2442 int input_isatty; /* is active input a terminal? */
2443
2444 input_isatty = mch_input_isatty();
2445 if (exmode_active)
2446 {
2447 if (!input_isatty)
2448 silent_mode = TRUE;
2449 }
2450 else if (parmp->want_full_screen && (!parmp->stdout_isatty || !input_isatty)
2451#ifdef FEAT_GUI
2452 /* don't want the delay when started from the desktop */
2453 && !gui.starting
2454#endif
2455 )
2456 {
2457#ifdef NBDEBUG
2458 /*
2459 * This shouldn't be necessary. But if I run netbeans with the log
2460 * output coming to the console and XOpenDisplay fails, I get vim
2461 * trying to start with input/output to my console tty. This fills my
2462 * input buffer so fast I can't even kill the process in under 2
Bram Moolenaar49325942007-05-10 19:19:59 +00002463 * minutes (and it beeps continuously the whole time :-)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002464 */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002465 if (netbeans_active() && (!parmp->stdout_isatty || !input_isatty))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002466 {
2467 mch_errmsg(_("Vim: Error: Failure to start gvim from NetBeans\n"));
2468 exit(1);
2469 }
2470#endif
2471 if (!parmp->stdout_isatty)
2472 mch_errmsg(_("Vim: Warning: Output is not to a terminal\n"));
2473 if (!input_isatty)
2474 mch_errmsg(_("Vim: Warning: Input is not from a terminal\n"));
2475 out_flush();
2476 if (scriptin[0] == NULL)
2477 ui_delay(2000L, TRUE);
2478 TIME_MSG("Warning delay");
2479 }
2480}
2481
2482/*
2483 * Read text from stdin.
2484 */
2485 static void
2486read_stdin()
2487{
2488 int i;
2489
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002490#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002491 /* When getting the ATTENTION prompt here, use a dialog */
2492 swap_exists_action = SEA_DIALOG;
2493#endif
2494 no_wait_return = TRUE;
2495 i = msg_didany;
2496 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002497 (void)open_buffer(TRUE, NULL, 0); /* create memfile and read file */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002498 no_wait_return = FALSE;
2499 msg_didany = i;
2500 TIME_MSG("reading stdin");
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002501#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002502 check_swap_exists_action();
2503#endif
2504#if !(defined(AMIGA) || defined(MACOS))
2505 /*
2506 * Close stdin and dup it from stderr. Required for GPM to work
2507 * properly, and for running external commands.
2508 * Is there any other system that cannot do this?
2509 */
2510 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00002511 ignored = dup(2);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002512#endif
2513}
2514
2515/*
2516 * Create the requested number of windows and edit buffers in them.
2517 * Also does recovery if "recoverymode" set.
2518 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002519 static void
2520create_windows(parmp)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002521 mparm_T *parmp UNUSED;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002522{
2523#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002524 int dorewind;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002525 int done = 0;
2526
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002527 /*
2528 * Create the number of windows that was requested.
2529 */
2530 if (parmp->window_count == -1) /* was not set */
2531 parmp->window_count = 1;
2532 if (parmp->window_count == 0)
2533 parmp->window_count = GARGCOUNT;
2534 if (parmp->window_count > 1)
2535 {
2536 /* Don't change the windows if there was a command in .vimrc that
2537 * already split some windows */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002538 if (parmp->window_layout == 0)
2539 parmp->window_layout = WIN_HOR;
2540 if (parmp->window_layout == WIN_TABS)
2541 {
2542 parmp->window_count = make_tabpages(parmp->window_count);
2543 TIME_MSG("making tab pages");
2544 }
2545 else if (firstwin->w_next == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002546 {
2547 parmp->window_count = make_windows(parmp->window_count,
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002548 parmp->window_layout == WIN_VER);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002549 TIME_MSG("making windows");
2550 }
2551 else
2552 parmp->window_count = win_count();
2553 }
2554 else
2555 parmp->window_count = 1;
2556#endif
2557
2558 if (recoverymode) /* do recover */
2559 {
2560 msg_scroll = TRUE; /* scroll message up */
2561 ml_recover();
2562 if (curbuf->b_ml.ml_mfp == NULL) /* failed */
2563 getout(1);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002564 do_modelines(0); /* do modelines */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002565 }
2566 else
2567 {
2568 /*
2569 * Open a buffer for windows that don't have one yet.
2570 * Commands in the .vimrc might have loaded a file or split the window.
2571 * Watch out for autocommands that delete a window.
2572 */
2573#ifdef FEAT_AUTOCMD
2574 /*
2575 * Don't execute Win/Buf Enter/Leave autocommands here
2576 */
2577 ++autocmd_no_enter;
2578 ++autocmd_no_leave;
2579#endif
2580#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002581 dorewind = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002582 while (done++ < 1000)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002583 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002584 if (dorewind)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002585 {
2586 if (parmp->window_layout == WIN_TABS)
2587 goto_tabpage(1);
2588 else
2589 curwin = firstwin;
2590 }
2591 else if (parmp->window_layout == WIN_TABS)
2592 {
2593 if (curtab->tp_next == NULL)
2594 break;
2595 goto_tabpage(0);
2596 }
2597 else
2598 {
2599 if (curwin->w_next == NULL)
2600 break;
2601 curwin = curwin->w_next;
2602 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002603 dorewind = FALSE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002604#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002605 curbuf = curwin->w_buffer;
2606 if (curbuf->b_ml.ml_mfp == NULL)
2607 {
2608#ifdef FEAT_FOLDING
2609 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2610 if (p_fdls >= 0)
2611 curwin->w_p_fdl = p_fdls;
2612#endif
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002613#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002614 /* When getting the ATTENTION prompt here, use a dialog */
2615 swap_exists_action = SEA_DIALOG;
2616#endif
2617 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002618
2619 /* create memfile, read file */
2620 (void)open_buffer(FALSE, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002621
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002622#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar84212822006-11-07 21:59:47 +00002623 if (swap_exists_action == SEA_QUIT)
2624 {
2625 if (got_int || only_one_window())
2626 {
2627 /* abort selected or quit and only one window */
2628 did_emsg = FALSE; /* avoid hit-enter prompt */
2629 getout(1);
2630 }
2631 /* We can't close the window, it would disturb what
2632 * happens next. Clear the file name and set the arg
2633 * index to -1 to delete it later. */
2634 setfname(curbuf, NULL, NULL, FALSE);
2635 curwin->w_arg_idx = -1;
2636 swap_exists_action = SEA_NONE;
2637 }
2638 else
2639 handle_swap_exists(NULL);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002640#endif
2641#ifdef FEAT_AUTOCMD
Bram Moolenaar89d40322006-08-29 15:30:07 +00002642 dorewind = TRUE; /* start again */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002643#endif
2644 }
2645#ifdef FEAT_WINDOWS
2646 ui_breakcheck();
2647 if (got_int)
2648 {
2649 (void)vgetc(); /* only break the file loading, not the rest */
2650 break;
2651 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002652 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002653#endif
2654#ifdef FEAT_WINDOWS
2655 if (parmp->window_layout == WIN_TABS)
2656 goto_tabpage(1);
2657 else
2658 curwin = firstwin;
2659 curbuf = curwin->w_buffer;
2660#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002661#ifdef FEAT_AUTOCMD
2662 --autocmd_no_enter;
2663 --autocmd_no_leave;
2664#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002665 }
2666}
2667
2668#ifdef FEAT_WINDOWS
2669 /*
2670 * If opened more than one window, start editing files in the other
2671 * windows. make_windows() has already opened the windows.
2672 */
2673 static void
2674edit_buffers(parmp)
2675 mparm_T *parmp;
2676{
2677 int arg_idx; /* index in argument list */
2678 int i;
Bram Moolenaar84212822006-11-07 21:59:47 +00002679 int advance = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002680
2681# ifdef FEAT_AUTOCMD
2682 /*
2683 * Don't execute Win/Buf Enter/Leave autocommands here
2684 */
2685 ++autocmd_no_enter;
2686 ++autocmd_no_leave;
2687# endif
Bram Moolenaar84212822006-11-07 21:59:47 +00002688
2689 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2690 if (curwin->w_arg_idx == -1)
2691 {
2692 win_close(curwin, TRUE);
2693 advance = FALSE;
2694 }
2695
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002696 arg_idx = 1;
2697 for (i = 1; i < parmp->window_count; ++i)
2698 {
Bram Moolenaar84212822006-11-07 21:59:47 +00002699 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2700 if (curwin->w_arg_idx == -1)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002701 {
Bram Moolenaar84212822006-11-07 21:59:47 +00002702 ++arg_idx;
2703 win_close(curwin, TRUE);
2704 advance = FALSE;
2705 continue;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002706 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002707
Bram Moolenaar84212822006-11-07 21:59:47 +00002708 if (advance)
2709 {
2710 if (parmp->window_layout == WIN_TABS)
2711 {
2712 if (curtab->tp_next == NULL) /* just checking */
2713 break;
2714 goto_tabpage(0);
2715 }
2716 else
2717 {
2718 if (curwin->w_next == NULL) /* just checking */
2719 break;
2720 win_enter(curwin->w_next, FALSE);
2721 }
2722 }
2723 advance = TRUE;
2724
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002725 /* Only open the file if there is no file in this window yet (that can
Bram Moolenaar84212822006-11-07 21:59:47 +00002726 * happen when .vimrc contains ":sall"). */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002727 if (curbuf == firstwin->w_buffer || curbuf->b_ffname == NULL)
2728 {
2729 curwin->w_arg_idx = arg_idx;
Bram Moolenaar84212822006-11-07 21:59:47 +00002730 /* Edit file from arg list, if there is one. When "Quit" selected
2731 * at the ATTENTION prompt close the window. */
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002732# ifdef HAS_SWAP_EXISTS_ACTION
2733 swap_exists_did_quit = FALSE;
2734# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002735 (void)do_ecmd(0, arg_idx < GARGCOUNT
2736 ? alist_name(&GARGLIST[arg_idx]) : NULL,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002737 NULL, NULL, ECMD_LASTL, ECMD_HIDE, curwin);
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002738# ifdef HAS_SWAP_EXISTS_ACTION
2739 if (swap_exists_did_quit)
Bram Moolenaar84212822006-11-07 21:59:47 +00002740 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002741 /* abort or quit selected */
Bram Moolenaar84212822006-11-07 21:59:47 +00002742 if (got_int || only_one_window())
2743 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002744 /* abort selected and only one window */
Bram Moolenaar84212822006-11-07 21:59:47 +00002745 did_emsg = FALSE; /* avoid hit-enter prompt */
2746 getout(1);
2747 }
2748 win_close(curwin, TRUE);
2749 advance = FALSE;
2750 }
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002751# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002752 if (arg_idx == GARGCOUNT - 1)
2753 arg_had_last = TRUE;
2754 ++arg_idx;
2755 }
2756 ui_breakcheck();
2757 if (got_int)
2758 {
2759 (void)vgetc(); /* only break the file loading, not the rest */
2760 break;
2761 }
2762 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002763
2764 if (parmp->window_layout == WIN_TABS)
2765 goto_tabpage(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002766# ifdef FEAT_AUTOCMD
2767 --autocmd_no_enter;
2768# endif
2769 win_enter(firstwin, FALSE); /* back to first window */
2770# ifdef FEAT_AUTOCMD
2771 --autocmd_no_leave;
2772# endif
2773 TIME_MSG("editing files in windows");
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002774 if (parmp->window_count > 1 && parmp->window_layout != WIN_TABS)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002775 win_equal(curwin, FALSE, 'b'); /* adjust heights */
2776}
2777#endif /* FEAT_WINDOWS */
2778
2779/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002780 * Execute the commands from --cmd arguments "cmds[cnt]".
2781 */
2782 static void
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002783exe_pre_commands(parmp)
2784 mparm_T *parmp;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002785{
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002786 char_u **cmds = parmp->pre_commands;
2787 int cnt = parmp->n_pre_commands;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002788 int i;
2789
2790 if (cnt > 0)
2791 {
2792 curwin->w_cursor.lnum = 0; /* just in case.. */
2793 sourcing_name = (char_u *)_("pre-vimrc command line");
2794# ifdef FEAT_EVAL
2795 current_SID = SID_CMDARG;
2796# endif
2797 for (i = 0; i < cnt; ++i)
2798 do_cmdline_cmd(cmds[i]);
2799 sourcing_name = NULL;
2800# ifdef FEAT_EVAL
2801 current_SID = 0;
2802# endif
2803 TIME_MSG("--cmd commands");
2804 }
2805}
2806
2807/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002808 * Execute "+", "-c" and "-S" arguments.
2809 */
2810 static void
2811exe_commands(parmp)
2812 mparm_T *parmp;
2813{
2814 int i;
2815
2816 /*
2817 * We start commands on line 0, make "vim +/pat file" match a
2818 * pattern on line 1. But don't move the cursor when an autocommand
2819 * with g`" was used.
2820 */
2821 msg_scroll = TRUE;
2822 if (parmp->tagname == NULL && curwin->w_cursor.lnum <= 1)
2823 curwin->w_cursor.lnum = 0;
2824 sourcing_name = (char_u *)"command line";
2825#ifdef FEAT_EVAL
2826 current_SID = SID_CARG;
2827#endif
2828 for (i = 0; i < parmp->n_commands; ++i)
2829 {
2830 do_cmdline_cmd(parmp->commands[i]);
2831 if (parmp->cmds_tofree[i])
2832 vim_free(parmp->commands[i]);
2833 }
2834 sourcing_name = NULL;
2835#ifdef FEAT_EVAL
2836 current_SID = 0;
2837#endif
2838 if (curwin->w_cursor.lnum == 0)
2839 curwin->w_cursor.lnum = 1;
2840
2841 if (!exmode_active)
2842 msg_scroll = FALSE;
2843
2844#ifdef FEAT_QUICKFIX
2845 /* When started with "-q errorfile" jump to first error again. */
2846 if (parmp->edit_type == EDIT_QF)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002847 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002848#endif
2849 TIME_MSG("executing command arguments");
2850}
2851
2852/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002853 * Source startup scripts.
2854 */
2855 static void
2856source_startup_scripts(parmp)
2857 mparm_T *parmp;
2858{
2859 int i;
2860
2861 /*
2862 * For "evim" source evim.vim first of all, so that the user can overrule
2863 * any things he doesn't like.
2864 */
2865 if (parmp->evim_mode)
2866 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002867 (void)do_source((char_u *)EVIM_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002868 TIME_MSG("source evim file");
2869 }
2870
2871 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002872 * If -u argument given, use only the initializations from that file and
Bram Moolenaar58d98232005-07-23 22:25:46 +00002873 * nothing else.
2874 */
2875 if (parmp->use_vimrc != NULL)
2876 {
Bram Moolenaar231334e2005-07-25 20:46:57 +00002877 if (STRCMP(parmp->use_vimrc, "NONE") == 0
2878 || STRCMP(parmp->use_vimrc, "NORC") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002879 {
2880#ifdef FEAT_GUI
2881 if (use_gvimrc == NULL) /* don't load gvimrc either */
2882 use_gvimrc = parmp->use_vimrc;
2883#endif
2884 if (parmp->use_vimrc[2] == 'N')
2885 p_lpl = FALSE; /* don't load plugins either */
2886 }
2887 else
2888 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002889 if (do_source(parmp->use_vimrc, FALSE, DOSO_NONE) != OK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002890 EMSG2(_("E282: Cannot read from \"%s\""), parmp->use_vimrc);
2891 }
2892 }
2893 else if (!silent_mode)
2894 {
2895#ifdef AMIGA
2896 struct Process *proc = (struct Process *)FindTask(0L);
2897 APTR save_winptr = proc->pr_WindowPtr;
2898
2899 /* Avoid a requester here for a volume that doesn't exist. */
2900 proc->pr_WindowPtr = (APTR)-1L;
2901#endif
2902
2903 /*
2904 * Get system wide defaults, if the file name is defined.
2905 */
2906#ifdef SYS_VIMRC_FILE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002907 (void)do_source((char_u *)SYS_VIMRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002908#endif
Bram Moolenaar1056d982006-03-09 22:37:52 +00002909#ifdef MACOS_X
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002910 (void)do_source((char_u *)"$VIMRUNTIME/macmap.vim", FALSE, DOSO_NONE);
Bram Moolenaar1056d982006-03-09 22:37:52 +00002911#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00002912
2913 /*
2914 * Try to read initialization commands from the following places:
2915 * - environment variable VIMINIT
2916 * - user vimrc file (s:.vimrc for Amiga, ~/.vimrc otherwise)
2917 * - second user vimrc file ($VIM/.vimrc for Dos)
2918 * - environment variable EXINIT
2919 * - user exrc file (s:.exrc for Amiga, ~/.exrc otherwise)
2920 * - second user exrc file ($VIM/.exrc for Dos)
2921 * The first that exists is used, the rest is ignored.
2922 */
2923 if (process_env((char_u *)"VIMINIT", TRUE) != OK)
2924 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002925 if (do_source((char_u *)USR_VIMRC_FILE, TRUE, DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00002926#ifdef USR_VIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002927 && do_source((char_u *)USR_VIMRC_FILE2, TRUE,
2928 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00002929#endif
2930#ifdef USR_VIMRC_FILE3
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002931 && do_source((char_u *)USR_VIMRC_FILE3, TRUE,
2932 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00002933#endif
2934 && process_env((char_u *)"EXINIT", FALSE) == FAIL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002935 && do_source((char_u *)USR_EXRC_FILE, FALSE, DOSO_NONE) == FAIL)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002936 {
2937#ifdef USR_EXRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002938 (void)do_source((char_u *)USR_EXRC_FILE2, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002939#endif
2940 }
2941 }
2942
2943 /*
2944 * Read initialization commands from ".vimrc" or ".exrc" in current
2945 * directory. This is only done if the 'exrc' option is set.
2946 * Because of security reasons we disallow shell and write commands
2947 * now, except for unix if the file is owned by the user or 'secure'
2948 * option has been reset in environment of global ".exrc" or ".vimrc".
2949 * Only do this if VIMRC_FILE is not the same as USR_VIMRC_FILE or
2950 * SYS_VIMRC_FILE.
2951 */
2952 if (p_exrc)
2953 {
2954#if defined(UNIX) || defined(VMS)
2955 /* If ".vimrc" file is not owned by user, set 'secure' mode. */
2956 if (!file_owned(VIMRC_FILE))
2957#endif
2958 secure = p_secure;
2959
2960 i = FAIL;
2961 if (fullpathcmp((char_u *)USR_VIMRC_FILE,
2962 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
2963#ifdef USR_VIMRC_FILE2
2964 && fullpathcmp((char_u *)USR_VIMRC_FILE2,
2965 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
2966#endif
2967#ifdef USR_VIMRC_FILE3
2968 && fullpathcmp((char_u *)USR_VIMRC_FILE3,
2969 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
2970#endif
2971#ifdef SYS_VIMRC_FILE
2972 && fullpathcmp((char_u *)SYS_VIMRC_FILE,
2973 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
2974#endif
2975 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002976 i = do_source((char_u *)VIMRC_FILE, TRUE, DOSO_VIMRC);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002977
2978 if (i == FAIL)
2979 {
2980#if defined(UNIX) || defined(VMS)
2981 /* if ".exrc" is not owned by user set 'secure' mode */
2982 if (!file_owned(EXRC_FILE))
2983 secure = p_secure;
2984 else
2985 secure = 0;
2986#endif
2987 if ( fullpathcmp((char_u *)USR_EXRC_FILE,
2988 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
2989#ifdef USR_EXRC_FILE2
2990 && fullpathcmp((char_u *)USR_EXRC_FILE2,
2991 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
2992#endif
2993 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002994 (void)do_source((char_u *)EXRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002995 }
2996 }
2997 if (secure == 2)
2998 need_wait_return = TRUE;
2999 secure = 0;
3000#ifdef AMIGA
3001 proc->pr_WindowPtr = save_winptr;
3002#endif
3003 }
3004 TIME_MSG("sourcing vimrc file(s)");
3005}
3006
3007/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003008 * Setup to start using the GUI. Exit with an error when not available.
3009 */
3010 static void
3011main_start_gui()
3012{
3013#ifdef FEAT_GUI
3014 gui.starting = TRUE; /* start GUI a bit later */
3015#else
3016 mch_errmsg(_(e_nogvim));
3017 mch_errmsg("\n");
3018 mch_exit(2);
3019#endif
3020}
3021
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003022#endif /* NO_VIM_MAIN */
3023
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003024/*
Bram Moolenaar49325942007-05-10 19:19:59 +00003025 * Get an environment variable, and execute it as Ex commands.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003026 * Returns FAIL if the environment variable was not executed, OK otherwise.
3027 */
3028 int
3029process_env(env, is_viminit)
3030 char_u *env;
3031 int is_viminit; /* when TRUE, called for VIMINIT */
3032{
3033 char_u *initstr;
3034 char_u *save_sourcing_name;
3035 linenr_T save_sourcing_lnum;
3036#ifdef FEAT_EVAL
3037 scid_T save_sid;
3038#endif
3039
3040 if ((initstr = mch_getenv(env)) != NULL && *initstr != NUL)
3041 {
3042 if (is_viminit)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003043 vimrc_found(NULL, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003044 save_sourcing_name = sourcing_name;
3045 save_sourcing_lnum = sourcing_lnum;
3046 sourcing_name = env;
3047 sourcing_lnum = 0;
3048#ifdef FEAT_EVAL
3049 save_sid = current_SID;
3050 current_SID = SID_ENV;
3051#endif
3052 do_cmdline_cmd(initstr);
3053 sourcing_name = save_sourcing_name;
3054 sourcing_lnum = save_sourcing_lnum;
3055#ifdef FEAT_EVAL
3056 current_SID = save_sid;;
3057#endif
3058 return OK;
3059 }
3060 return FAIL;
3061}
3062
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003063#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003064/*
3065 * Return TRUE if we are certain the user owns the file "fname".
3066 * Used for ".vimrc" and ".exrc".
3067 * Use both stat() and lstat() for extra security.
3068 */
3069 static int
3070file_owned(fname)
3071 char *fname;
3072{
3073 struct stat s;
3074# ifdef UNIX
3075 uid_t uid = getuid();
3076# else /* VMS */
3077 uid_t uid = ((getgid() << 16) | getuid());
3078# endif
3079
3080 return !(mch_stat(fname, &s) != 0 || s.st_uid != uid
3081# ifdef HAVE_LSTAT
3082 || mch_lstat(fname, &s) != 0 || s.st_uid != uid
3083# endif
3084 );
3085}
3086#endif
3087
3088/*
3089 * Give an error message main_errors["n"] and exit.
3090 */
3091 static void
3092mainerr(n, str)
3093 int n; /* one of the ME_ defines */
3094 char_u *str; /* extra argument or NULL */
3095{
3096#if defined(UNIX) || defined(__EMX__) || defined(VMS)
3097 reset_signals(); /* kill us with CTRL-C here, if you like */
3098#endif
3099
3100 mch_errmsg(longVersion);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003101 mch_errmsg("\n");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003102 mch_errmsg(_(main_errors[n]));
3103 if (str != NULL)
3104 {
3105 mch_errmsg(": \"");
3106 mch_errmsg((char *)str);
3107 mch_errmsg("\"");
3108 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003109 mch_errmsg(_("\nMore info with: \"vim -h\"\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003110
3111 mch_exit(1);
3112}
3113
3114 void
3115mainerr_arg_missing(str)
3116 char_u *str;
3117{
3118 mainerr(ME_ARG_MISSING, str);
3119}
3120
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003121#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003122/*
3123 * print a message with three spaces prepended and '\n' appended.
3124 */
3125 static void
3126main_msg(s)
3127 char *s;
3128{
3129 mch_msg(" ");
3130 mch_msg(s);
3131 mch_msg("\n");
3132}
3133
3134/*
3135 * Print messages for "vim -h" or "vim --help" and exit.
3136 */
3137 static void
3138usage()
3139{
3140 int i;
3141 static char *(use[]) =
3142 {
3143 N_("[file ..] edit specified file(s)"),
3144 N_("- read text from stdin"),
3145 N_("-t tag edit file where tag is defined"),
3146#ifdef FEAT_QUICKFIX
3147 N_("-q [errorfile] edit file with first error")
3148#endif
3149 };
3150
3151#if defined(UNIX) || defined(__EMX__) || defined(VMS)
3152 reset_signals(); /* kill us with CTRL-C here, if you like */
3153#endif
3154
3155 mch_msg(longVersion);
3156 mch_msg(_("\n\nusage:"));
3157 for (i = 0; ; ++i)
3158 {
3159 mch_msg(_(" vim [arguments] "));
3160 mch_msg(_(use[i]));
3161 if (i == (sizeof(use) / sizeof(char_u *)) - 1)
3162 break;
3163 mch_msg(_("\n or:"));
3164 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003165#ifdef VMS
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003166 mch_msg(_("\nWhere case is ignored prepend / to make flag upper case"));
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003167#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003168
3169 mch_msg(_("\n\nArguments:\n"));
3170 main_msg(_("--\t\t\tOnly file names after this"));
3171#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
3172 main_msg(_("--literal\t\tDon't expand wildcards"));
3173#endif
3174#ifdef FEAT_OLE
3175 main_msg(_("-register\t\tRegister this gvim for OLE"));
3176 main_msg(_("-unregister\t\tUnregister gvim for OLE"));
3177#endif
3178#ifdef FEAT_GUI
3179 main_msg(_("-g\t\t\tRun using GUI (like \"gvim\")"));
3180 main_msg(_("-f or --nofork\tForeground: Don't fork when starting GUI"));
3181#endif
3182 main_msg(_("-v\t\t\tVi mode (like \"vi\")"));
3183 main_msg(_("-e\t\t\tEx mode (like \"ex\")"));
3184 main_msg(_("-s\t\t\tSilent (batch) mode (only for \"ex\")"));
3185#ifdef FEAT_DIFF
3186 main_msg(_("-d\t\t\tDiff mode (like \"vimdiff\")"));
3187#endif
3188 main_msg(_("-y\t\t\tEasy mode (like \"evim\", modeless)"));
3189 main_msg(_("-R\t\t\tReadonly mode (like \"view\")"));
3190 main_msg(_("-Z\t\t\tRestricted mode (like \"rvim\")"));
3191 main_msg(_("-m\t\t\tModifications (writing files) not allowed"));
3192 main_msg(_("-M\t\t\tModifications in text not allowed"));
3193 main_msg(_("-b\t\t\tBinary mode"));
3194#ifdef FEAT_LISP
3195 main_msg(_("-l\t\t\tLisp mode"));
3196#endif
3197 main_msg(_("-C\t\t\tCompatible with Vi: 'compatible'"));
3198 main_msg(_("-N\t\t\tNot fully Vi compatible: 'nocompatible'"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003199 main_msg(_("-V[N][fname]\t\tBe verbose [level N] [log messages to fname]"));
3200#ifdef FEAT_EVAL
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003201 main_msg(_("-D\t\t\tDebugging mode"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003202#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003203 main_msg(_("-n\t\t\tNo swap file, use memory only"));
3204 main_msg(_("-r\t\t\tList swap files and exit"));
3205 main_msg(_("-r (with file name)\tRecover crashed session"));
3206 main_msg(_("-L\t\t\tSame as -r"));
3207#ifdef AMIGA
3208 main_msg(_("-f\t\t\tDon't use newcli to open window"));
3209 main_msg(_("-dev <device>\t\tUse <device> for I/O"));
3210#endif
3211#ifdef FEAT_ARABIC
3212 main_msg(_("-A\t\t\tstart in Arabic mode"));
3213#endif
3214#ifdef FEAT_RIGHTLEFT
3215 main_msg(_("-H\t\t\tStart in Hebrew mode"));
3216#endif
3217#ifdef FEAT_FKMAP
3218 main_msg(_("-F\t\t\tStart in Farsi mode"));
3219#endif
3220 main_msg(_("-T <terminal>\tSet terminal type to <terminal>"));
3221 main_msg(_("-u <vimrc>\t\tUse <vimrc> instead of any .vimrc"));
3222#ifdef FEAT_GUI
3223 main_msg(_("-U <gvimrc>\t\tUse <gvimrc> instead of any .gvimrc"));
3224#endif
3225 main_msg(_("--noplugin\t\tDon't load plugin scripts"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003226#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003227 main_msg(_("-p[N]\t\tOpen N tab pages (default: one for each file)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003228 main_msg(_("-o[N]\t\tOpen N windows (default: one for each file)"));
3229 main_msg(_("-O[N]\t\tLike -o but split vertically"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003230#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003231 main_msg(_("+\t\t\tStart at end of file"));
3232 main_msg(_("+<lnum>\t\tStart at line <lnum>"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003233 main_msg(_("--cmd <command>\tExecute <command> before loading any vimrc file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003234 main_msg(_("-c <command>\t\tExecute <command> after loading the first file"));
3235 main_msg(_("-S <session>\t\tSource file <session> after loading the first file"));
3236 main_msg(_("-s <scriptin>\tRead Normal mode commands from file <scriptin>"));
3237 main_msg(_("-w <scriptout>\tAppend all typed commands to file <scriptout>"));
3238 main_msg(_("-W <scriptout>\tWrite all typed commands to file <scriptout>"));
3239#ifdef FEAT_CRYPT
3240 main_msg(_("-x\t\t\tEdit encrypted files"));
3241#endif
3242#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
3243# if defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)
3244 main_msg(_("-display <display>\tConnect vim to this particular X-server"));
3245# endif
3246 main_msg(_("-X\t\t\tDo not connect to X server"));
3247#endif
3248#ifdef FEAT_CLIENTSERVER
3249 main_msg(_("--remote <files>\tEdit <files> in a Vim server if possible"));
3250 main_msg(_("--remote-silent <files> Same, don't complain if there is no server"));
3251 main_msg(_("--remote-wait <files> As --remote but wait for files to have been edited"));
3252 main_msg(_("--remote-wait-silent <files> Same, don't complain if there is no server"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003253# ifdef FEAT_WINDOWS
Bram Moolenaar82ad3242008-01-11 19:26:36 +00003254 main_msg(_("--remote-tab[-wait][-silent] <files> As --remote but use tab page per file"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003255# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003256 main_msg(_("--remote-send <keys>\tSend <keys> to a Vim server and exit"));
3257 main_msg(_("--remote-expr <expr>\tEvaluate <expr> in a Vim server and print result"));
3258 main_msg(_("--serverlist\t\tList available Vim server names and exit"));
3259 main_msg(_("--servername <name>\tSend to/become the Vim server <name>"));
3260#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003261#ifdef STARTUPTIME
Bram Moolenaar34ef52d2009-11-17 11:31:25 +00003262 main_msg(_("--startuptime <file>\tWrite startup timing messages to <file>"));
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003263#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003264#ifdef FEAT_VIMINFO
3265 main_msg(_("-i <viminfo>\t\tUse <viminfo> instead of .viminfo"));
3266#endif
3267 main_msg(_("-h or --help\tPrint Help (this message) and exit"));
3268 main_msg(_("--version\t\tPrint version information and exit"));
3269
3270#ifdef FEAT_GUI_X11
3271# ifdef FEAT_GUI_MOTIF
3272 mch_msg(_("\nArguments recognised by gvim (Motif version):\n"));
3273# else
3274# ifdef FEAT_GUI_ATHENA
3275# ifdef FEAT_GUI_NEXTAW
3276 mch_msg(_("\nArguments recognised by gvim (neXtaw version):\n"));
3277# else
3278 mch_msg(_("\nArguments recognised by gvim (Athena version):\n"));
3279# endif
3280# endif
3281# endif
3282 main_msg(_("-display <display>\tRun vim on <display>"));
3283 main_msg(_("-iconic\t\tStart vim iconified"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003284 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
3285 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
3286 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3287 main_msg(_("-boldfont <font>\tUse <font> for bold text"));
3288 main_msg(_("-italicfont <font>\tUse <font> for italic text"));
3289 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3290 main_msg(_("-borderwidth <width>\tUse a border width of <width> (also: -bw)"));
3291 main_msg(_("-scrollbarwidth <width> Use a scrollbar width of <width> (also: -sw)"));
3292# ifdef FEAT_GUI_ATHENA
3293 main_msg(_("-menuheight <height>\tUse a menu bar height of <height> (also: -mh)"));
3294# endif
3295 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3296 main_msg(_("+reverse\t\tDon't use reverse video (also: +rv)"));
3297 main_msg(_("-xrm <resource>\tSet the specified resource"));
3298#endif /* FEAT_GUI_X11 */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003299#ifdef FEAT_GUI_GTK
3300 mch_msg(_("\nArguments recognised by gvim (GTK+ version):\n"));
3301 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3302 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3303 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3304 main_msg(_("-display <display>\tRun vim on <display> (also: --display)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003305 main_msg(_("--role <role>\tSet a unique role to identify the main window"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003306 main_msg(_("--socketid <xid>\tOpen Vim inside another GTK widget"));
3307#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003308#ifdef FEAT_GUI_W32
3309 main_msg(_("-P <parent title>\tOpen Vim inside parent application"));
Bram Moolenaar78e17622007-08-30 10:26:19 +00003310 main_msg(_("--windowid <HWND>\tOpen Vim inside another win32 widget"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003311#endif
3312
3313#ifdef FEAT_GUI_GNOME
3314 /* Gnome gives extra messages for --help if we continue, but not for -h. */
3315 if (gui.starting)
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003316 {
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003317 mch_msg("\n");
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003318 gui.dofork = FALSE;
3319 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003320 else
3321#endif
3322 mch_exit(0);
3323}
3324
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00003325#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003326/*
3327 * Check the result of the ATTENTION dialog:
3328 * When "Quit" selected, exit Vim.
3329 * When "Recover" selected, recover the file.
3330 */
3331 static void
3332check_swap_exists_action()
3333{
3334 if (swap_exists_action == SEA_QUIT)
3335 getout(1);
3336 handle_swap_exists(NULL);
3337}
3338#endif
3339
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003340#endif
3341
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003342#if defined(STARTUPTIME) || defined(PROTO)
3343static void time_diff __ARGS((struct timeval *then, struct timeval *now));
3344
3345static struct timeval prev_timeval;
3346
Bram Moolenaar3f269672009-11-03 11:11:11 +00003347# ifdef WIN3264
3348/*
3349 * Windows doesn't have gettimeofday(), although it does have struct timeval.
3350 */
3351 static int
3352gettimeofday(struct timeval *tv, char *dummy)
3353{
3354 long t = clock();
3355 tv->tv_sec = t / CLOCKS_PER_SEC;
3356 tv->tv_usec = (t - tv->tv_sec * CLOCKS_PER_SEC) * 1000000 / CLOCKS_PER_SEC;
3357 return 0;
3358}
3359# endif
3360
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003361/*
3362 * Save the previous time before doing something that could nest.
3363 * set "*tv_rel" to the time elapsed so far.
3364 */
3365 void
3366time_push(tv_rel, tv_start)
3367 void *tv_rel, *tv_start;
3368{
3369 *((struct timeval *)tv_rel) = prev_timeval;
3370 gettimeofday(&prev_timeval, NULL);
3371 ((struct timeval *)tv_rel)->tv_usec = prev_timeval.tv_usec
3372 - ((struct timeval *)tv_rel)->tv_usec;
3373 ((struct timeval *)tv_rel)->tv_sec = prev_timeval.tv_sec
3374 - ((struct timeval *)tv_rel)->tv_sec;
3375 if (((struct timeval *)tv_rel)->tv_usec < 0)
3376 {
3377 ((struct timeval *)tv_rel)->tv_usec += 1000000;
3378 --((struct timeval *)tv_rel)->tv_sec;
3379 }
3380 *(struct timeval *)tv_start = prev_timeval;
3381}
3382
3383/*
3384 * Compute the previous time after doing something that could nest.
3385 * Subtract "*tp" from prev_timeval;
3386 * Note: The arguments are (void *) to avoid trouble with systems that don't
3387 * have struct timeval.
3388 */
3389 void
3390time_pop(tp)
3391 void *tp; /* actually (struct timeval *) */
3392{
3393 prev_timeval.tv_usec -= ((struct timeval *)tp)->tv_usec;
3394 prev_timeval.tv_sec -= ((struct timeval *)tp)->tv_sec;
3395 if (prev_timeval.tv_usec < 0)
3396 {
3397 prev_timeval.tv_usec += 1000000;
3398 --prev_timeval.tv_sec;
3399 }
3400}
3401
3402 static void
3403time_diff(then, now)
3404 struct timeval *then;
3405 struct timeval *now;
3406{
3407 long usec;
3408 long msec;
3409
3410 usec = now->tv_usec - then->tv_usec;
3411 msec = (now->tv_sec - then->tv_sec) * 1000L + usec / 1000L,
3412 usec = usec % 1000L;
3413 fprintf(time_fd, "%03ld.%03ld", msec, usec >= 0 ? usec : usec + 1000L);
3414}
3415
3416 void
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003417time_msg(mesg, tv_start)
3418 char *mesg;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003419 void *tv_start; /* only for do_source: start time; actually
3420 (struct timeval *) */
3421{
3422 static struct timeval start;
3423 struct timeval now;
3424
3425 if (time_fd != NULL)
3426 {
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003427 if (strstr(mesg, "STARTING") != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003428 {
3429 gettimeofday(&start, NULL);
3430 prev_timeval = start;
3431 fprintf(time_fd, "\n\ntimes in msec\n");
3432 fprintf(time_fd, " clock self+sourced self: sourced script\n");
3433 fprintf(time_fd, " clock elapsed: other lines\n\n");
3434 }
3435 gettimeofday(&now, NULL);
3436 time_diff(&start, &now);
3437 if (((struct timeval *)tv_start) != NULL)
3438 {
3439 fprintf(time_fd, " ");
3440 time_diff(((struct timeval *)tv_start), &now);
3441 }
3442 fprintf(time_fd, " ");
3443 time_diff(&prev_timeval, &now);
3444 prev_timeval = now;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003445 fprintf(time_fd, ": %s\n", mesg);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003446 }
3447}
3448
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003449#endif
3450
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003451#if (defined(FEAT_CLIENTSERVER) && !defined(NO_VIM_MAIN)) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003452
3453/*
3454 * Common code for the X command server and the Win32 command server.
3455 */
3456
Bram Moolenaareb94e552006-03-11 21:35:11 +00003457static char_u *build_drop_cmd __ARGS((int filec, char **filev, int tabs, int sendReply));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003458
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003459/*
3460 * Do the client-server stuff, unless "--servername ''" was used.
3461 */
3462 static void
3463exec_on_server(parmp)
3464 mparm_T *parmp;
3465{
3466 if (parmp->serverName_arg == NULL || *parmp->serverName_arg != NUL)
3467 {
3468# ifdef WIN32
3469 /* Initialise the client/server messaging infrastructure. */
3470 serverInitMessaging();
3471# endif
3472
3473 /*
3474 * When a command server argument was found, execute it. This may
3475 * exit Vim when it was successful. Otherwise it's executed further
3476 * on. Remember the encoding used here in "serverStrEnc".
3477 */
3478 if (parmp->serverArg)
3479 {
3480 cmdsrv_main(&parmp->argc, parmp->argv,
3481 parmp->serverName_arg, &parmp->serverStr);
3482# ifdef FEAT_MBYTE
3483 parmp->serverStrEnc = vim_strsave(p_enc);
3484# endif
3485 }
3486
3487 /* If we're still running, get the name to register ourselves.
3488 * On Win32 can register right now, for X11 need to setup the
3489 * clipboard first, it's further down. */
3490 parmp->servername = serverMakeName(parmp->serverName_arg,
3491 parmp->argv[0]);
3492# ifdef WIN32
3493 if (parmp->servername != NULL)
3494 {
3495 serverSetName(parmp->servername);
3496 vim_free(parmp->servername);
3497 }
3498# endif
3499 }
3500}
3501
3502/*
3503 * Prepare for running as a Vim server.
3504 */
3505 static void
3506prepare_server(parmp)
3507 mparm_T *parmp;
3508{
3509# if defined(FEAT_X11)
3510 /*
3511 * Register for remote command execution with :serversend and --remote
3512 * unless there was a -X or a --servername '' on the command line.
3513 * Only register nongui-vim's with an explicit --servername argument.
Bram Moolenaar5f402312006-08-15 19:40:35 +00003514 * When running as root --servername is also required.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003515 */
3516 if (X_DISPLAY != NULL && parmp->servername != NULL && (
3517# ifdef FEAT_GUI
Bram Moolenaar5f402312006-08-15 19:40:35 +00003518 (gui.in_use
3519# ifdef UNIX
Bram Moolenaar311d9822007-02-27 15:48:28 +00003520 && getuid() != ROOT_UID
Bram Moolenaar5f402312006-08-15 19:40:35 +00003521# endif
3522 ) ||
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003523# endif
3524 parmp->serverName_arg != NULL))
3525 {
3526 (void)serverRegisterName(X_DISPLAY, parmp->servername);
3527 vim_free(parmp->servername);
3528 TIME_MSG("register server name");
3529 }
3530 else
3531 serverDelayedStartName = parmp->servername;
3532# endif
3533
3534 /*
3535 * Execute command ourselves if we're here because the send failed (or
3536 * else we would have exited above).
3537 */
3538 if (parmp->serverStr != NULL)
3539 {
3540 char_u *p;
3541
3542 server_to_input_buf(serverConvert(parmp->serverStrEnc,
3543 parmp->serverStr, &p));
3544 vim_free(p);
3545 }
3546}
3547
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003548 static void
3549cmdsrv_main(argc, argv, serverName_arg, serverStr)
3550 int *argc;
3551 char **argv;
3552 char_u *serverName_arg;
3553 char_u **serverStr;
3554{
3555 char_u *res;
3556 int i;
3557 char_u *sname;
3558 int ret;
3559 int didone = FALSE;
3560 int exiterr = 0;
3561 char **newArgV = argv + 1;
3562 int newArgC = 1,
3563 Argc = *argc;
3564 int argtype;
3565#define ARGTYPE_OTHER 0
3566#define ARGTYPE_EDIT 1
3567#define ARGTYPE_EDIT_WAIT 2
3568#define ARGTYPE_SEND 3
3569 int silent = FALSE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003570 int tabs = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003571# ifndef FEAT_X11
3572 HWND srv;
3573# else
3574 Window srv;
3575
3576 setup_term_clip();
3577# endif
3578
3579 sname = serverMakeName(serverName_arg, argv[0]);
3580 if (sname == NULL)
3581 return;
3582
3583 /*
3584 * Execute the command server related arguments and remove them
3585 * from the argc/argv array; We may have to return into main()
3586 */
3587 for (i = 1; i < Argc; i++)
3588 {
3589 res = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003590 if (STRCMP(argv[i], "--") == 0) /* end of option arguments */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003591 {
3592 for (; i < *argc; i++)
3593 {
3594 *newArgV++ = argv[i];
3595 newArgC++;
3596 }
3597 break;
3598 }
3599
Bram Moolenaareb94e552006-03-11 21:35:11 +00003600 if (STRICMP(argv[i], "--remote-send") == 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003601 argtype = ARGTYPE_SEND;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003602 else if (STRNICMP(argv[i], "--remote", 8) == 0)
3603 {
3604 char *p = argv[i] + 8;
3605
3606 argtype = ARGTYPE_EDIT;
3607 while (*p != NUL)
3608 {
3609 if (STRNICMP(p, "-wait", 5) == 0)
3610 {
3611 argtype = ARGTYPE_EDIT_WAIT;
3612 p += 5;
3613 }
3614 else if (STRNICMP(p, "-silent", 7) == 0)
3615 {
3616 silent = TRUE;
3617 p += 7;
3618 }
3619 else if (STRNICMP(p, "-tab", 4) == 0)
3620 {
3621 tabs = TRUE;
3622 p += 4;
3623 }
3624 else
3625 {
3626 argtype = ARGTYPE_OTHER;
3627 break;
3628 }
3629 }
3630 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003631 else
3632 argtype = ARGTYPE_OTHER;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003633
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003634 if (argtype != ARGTYPE_OTHER)
3635 {
3636 if (i == *argc - 1)
3637 mainerr_arg_missing((char_u *)argv[i]);
3638 if (argtype == ARGTYPE_SEND)
3639 {
3640 *serverStr = (char_u *)argv[i + 1];
3641 i++;
3642 }
3643 else
3644 {
3645 *serverStr = build_drop_cmd(*argc - i - 1, argv + i + 1,
Bram Moolenaareb94e552006-03-11 21:35:11 +00003646 tabs, argtype == ARGTYPE_EDIT_WAIT);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003647 if (*serverStr == NULL)
3648 {
3649 /* Probably out of memory, exit. */
3650 didone = TRUE;
3651 exiterr = 1;
3652 break;
3653 }
3654 Argc = i;
3655 }
3656# ifdef FEAT_X11
3657 if (xterm_dpy == NULL)
3658 {
3659 mch_errmsg(_("No display"));
3660 ret = -1;
3661 }
3662 else
3663 ret = serverSendToVim(xterm_dpy, sname, *serverStr,
3664 NULL, &srv, 0, 0, silent);
3665# else
3666 /* Win32 always works? */
3667 ret = serverSendToVim(sname, *serverStr, NULL, &srv, 0, silent);
3668# endif
3669 if (ret < 0)
3670 {
3671 if (argtype == ARGTYPE_SEND)
3672 {
3673 /* Failed to send, abort. */
3674 mch_errmsg(_(": Send failed.\n"));
3675 didone = TRUE;
3676 exiterr = 1;
3677 }
3678 else if (!silent)
3679 /* Let vim start normally. */
3680 mch_errmsg(_(": Send failed. Trying to execute locally\n"));
3681 break;
3682 }
3683
3684# ifdef FEAT_GUI_W32
3685 /* Guess that when the server name starts with "g" it's a GUI
3686 * server, which we can bring to the foreground here.
3687 * Foreground() in the server doesn't work very well. */
3688 if (argtype != ARGTYPE_SEND && TOUPPER_ASC(*sname) == 'G')
3689 SetForegroundWindow(srv);
3690# endif
3691
3692 /*
3693 * For --remote-wait: Wait until the server did edit each
3694 * file. Also detect that the server no longer runs.
3695 */
3696 if (ret >= 0 && argtype == ARGTYPE_EDIT_WAIT)
3697 {
3698 int numFiles = *argc - i - 1;
3699 int j;
3700 char_u *done = alloc(numFiles);
3701 char_u *p;
3702# ifdef FEAT_GUI_W32
3703 NOTIFYICONDATA ni;
3704 int count = 0;
3705 extern HWND message_window;
3706# endif
3707
3708 if (numFiles > 0 && argv[i + 1][0] == '+')
3709 /* Skip "+cmd" argument, don't wait for it to be edited. */
3710 --numFiles;
3711
3712# ifdef FEAT_GUI_W32
3713 ni.cbSize = sizeof(ni);
3714 ni.hWnd = message_window;
3715 ni.uID = 0;
3716 ni.uFlags = NIF_ICON|NIF_TIP;
3717 ni.hIcon = LoadIcon((HINSTANCE)GetModuleHandle(0), "IDR_VIM");
3718 sprintf(ni.szTip, _("%d of %d edited"), count, numFiles);
3719 Shell_NotifyIcon(NIM_ADD, &ni);
3720# endif
3721
3722 /* Wait for all files to unload in remote */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02003723 vim_memset(done, 0, numFiles);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003724 while (memchr(done, 0, numFiles) != NULL)
3725 {
3726# ifdef WIN32
3727 p = serverGetReply(srv, NULL, TRUE, TRUE);
3728 if (p == NULL)
3729 break;
3730# else
3731 if (serverReadReply(xterm_dpy, srv, &p, TRUE) < 0)
3732 break;
3733# endif
3734 j = atoi((char *)p);
3735 if (j >= 0 && j < numFiles)
3736 {
3737# ifdef FEAT_GUI_W32
3738 ++count;
3739 sprintf(ni.szTip, _("%d of %d edited"),
3740 count, numFiles);
3741 Shell_NotifyIcon(NIM_MODIFY, &ni);
3742# endif
3743 done[j] = 1;
3744 }
3745 }
3746# ifdef FEAT_GUI_W32
3747 Shell_NotifyIcon(NIM_DELETE, &ni);
3748# endif
3749 }
3750 }
3751 else if (STRICMP(argv[i], "--remote-expr") == 0)
3752 {
3753 if (i == *argc - 1)
3754 mainerr_arg_missing((char_u *)argv[i]);
3755# ifdef WIN32
3756 /* Win32 always works? */
3757 if (serverSendToVim(sname, (char_u *)argv[i + 1],
3758 &res, NULL, 1, FALSE) < 0)
3759# else
3760 if (xterm_dpy == NULL)
3761 mch_errmsg(_("No display: Send expression failed.\n"));
3762 else if (serverSendToVim(xterm_dpy, sname, (char_u *)argv[i + 1],
3763 &res, NULL, 1, 1, FALSE) < 0)
3764# endif
3765 {
3766 if (res != NULL && *res != NUL)
3767 {
3768 /* Output error from remote */
3769 mch_errmsg((char *)res);
3770 vim_free(res);
3771 res = NULL;
3772 }
3773 mch_errmsg(_(": Send expression failed.\n"));
3774 }
3775 }
3776 else if (STRICMP(argv[i], "--serverlist") == 0)
3777 {
3778# ifdef WIN32
3779 /* Win32 always works? */
3780 res = serverGetVimNames();
3781# else
3782 if (xterm_dpy != NULL)
3783 res = serverGetVimNames(xterm_dpy);
3784# endif
3785 if (called_emsg)
3786 mch_errmsg("\n");
3787 }
3788 else if (STRICMP(argv[i], "--servername") == 0)
3789 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +00003790 /* Already processed. Take it out of the command line */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003791 i++;
3792 continue;
3793 }
3794 else
3795 {
3796 *newArgV++ = argv[i];
3797 newArgC++;
3798 continue;
3799 }
3800 didone = TRUE;
3801 if (res != NULL && *res != NUL)
3802 {
3803 mch_msg((char *)res);
3804 if (res[STRLEN(res) - 1] != '\n')
3805 mch_msg("\n");
3806 }
3807 vim_free(res);
3808 }
3809
3810 if (didone)
3811 {
3812 display_errors(); /* display any collected messages */
3813 exit(exiterr); /* Mission accomplished - get out */
3814 }
3815
3816 /* Return back into main() */
3817 *argc = newArgC;
3818 vim_free(sname);
3819}
3820
3821/*
3822 * Build a ":drop" command to send to a Vim server.
3823 */
3824 static char_u *
Bram Moolenaareb94e552006-03-11 21:35:11 +00003825build_drop_cmd(filec, filev, tabs, sendReply)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003826 int filec;
3827 char **filev;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003828 int tabs; /* Use ":tab drop" instead of ":drop". */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003829 int sendReply;
3830{
3831 garray_T ga;
3832 int i;
3833 char_u *inicmd = NULL;
3834 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003835 char_u *cwd;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003836
3837 if (filec > 0 && filev[0][0] == '+')
3838 {
3839 inicmd = (char_u *)filev[0] + 1;
3840 filev++;
3841 filec--;
3842 }
3843 /* Check if we have at least one argument. */
3844 if (filec <= 0)
3845 mainerr_arg_missing((char_u *)filev[-1]);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003846
3847 /* Temporarily cd to the current directory to handle relative file names. */
Bram Moolenaard9462e32011-04-11 21:35:11 +02003848 cwd = alloc(MAXPATHL);
3849 if (cwd == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003850 return NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003851 if (mch_dirname(cwd, MAXPATHL) != OK)
3852 {
3853 vim_free(cwd);
3854 return NULL;
3855 }
3856 p = vim_strsave_escaped_ext(cwd,
Bram Moolenaar02b06312007-09-06 15:39:22 +00003857#ifdef BACKSLASH_IN_FILENAME
3858 "", /* rem_backslash() will tell what chars to escape */
3859#else
3860 PATH_ESC_CHARS,
3861#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +02003862 '\\', TRUE);
3863 vim_free(cwd);
3864 if (p == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003865 return NULL;
3866 ga_init2(&ga, 1, 100);
3867 ga_concat(&ga, (char_u *)"<C-\\><C-N>:cd ");
3868 ga_concat(&ga, p);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003869 vim_free(p);
Bram Moolenaareb94e552006-03-11 21:35:11 +00003870
3871 /* Call inputsave() so that a prompt for an encryption key works. */
3872 ga_concat(&ga, (char_u *)"<CR>:if exists('*inputsave')|call inputsave()|endif|");
3873 if (tabs)
3874 ga_concat(&ga, (char_u *)"tab ");
3875 ga_concat(&ga, (char_u *)"drop");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003876 for (i = 0; i < filec; i++)
3877 {
3878 /* On Unix the shell has already expanded the wildcards, don't want to
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003879 * do it again in the Vim server. On MS-Windows only escape
3880 * non-wildcard characters. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003881 p = vim_strsave_escaped((char_u *)filev[i],
3882#ifdef UNIX
3883 PATH_ESC_CHARS
3884#else
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003885 (char_u *)" \t%#"
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003886#endif
3887 );
3888 if (p == NULL)
3889 {
3890 vim_free(ga.ga_data);
3891 return NULL;
3892 }
3893 ga_concat(&ga, (char_u *)" ");
3894 ga_concat(&ga, p);
3895 vim_free(p);
3896 }
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003897 ga_concat(&ga, (char_u *)"|if exists('*inputrestore')|call inputrestore()|endif<CR>");
3898
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003899 /* The :drop commands goes to Insert mode when 'insertmode' is set, use
3900 * CTRL-\ CTRL-N again. */
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003901 ga_concat(&ga, (char_u *)"<C-\\><C-N>");
3902
3903 /* Switch back to the correct current directory (prior to temporary path
3904 * switch) unless 'autochdir' is set, in which case it will already be
3905 * correct after the :drop command. */
3906 ga_concat(&ga, (char_u *)":if !exists('+acd')||!&acd|cd -|endif<CR>");
3907
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003908 if (sendReply)
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003909 ga_concat(&ga, (char_u *)":call SetupRemoteReplies()<CR>");
3910 ga_concat(&ga, (char_u *)":");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003911 if (inicmd != NULL)
3912 {
3913 /* Can't use <CR> after "inicmd", because an "startinsert" would cause
3914 * the following commands to be inserted as text. Use a "|",
3915 * hopefully "inicmd" does allow this... */
3916 ga_concat(&ga, inicmd);
3917 ga_concat(&ga, (char_u *)"|");
3918 }
3919 /* Bring the window to the foreground, goto Insert mode when 'im' set and
3920 * clear command line. */
Bram Moolenaar567e4de2004-12-31 21:01:02 +00003921 ga_concat(&ga, (char_u *)"cal foreground()|if &im|star|en|redr|f<CR>");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003922 ga_append(&ga, NUL);
3923 return ga.ga_data;
3924}
3925
3926/*
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003927 * Make our basic server name: use the specified "arg" if given, otherwise use
3928 * the tail of the command "cmd" we were started with.
3929 * Return the name in allocated memory. This doesn't include a serial number.
3930 */
3931 static char_u *
3932serverMakeName(arg, cmd)
3933 char_u *arg;
3934 char *cmd;
3935{
3936 char_u *p;
3937
3938 if (arg != NULL && *arg != NUL)
3939 p = vim_strsave_up(arg);
3940 else
3941 {
3942 p = vim_strsave_up(gettail((char_u *)cmd));
3943 /* Remove .exe or .bat from the name. */
3944 if (p != NULL && vim_strchr(p, '.') != NULL)
3945 *vim_strchr(p, '.') = NUL;
3946 }
3947 return p;
3948}
3949#endif /* FEAT_CLIENTSERVER */
3950
3951#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
3952/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003953 * Replace termcodes such as <CR> and insert as key presses if there is room.
3954 */
3955 void
3956server_to_input_buf(str)
3957 char_u *str;
3958{
3959 char_u *ptr = NULL;
3960 char_u *cpo_save = p_cpo;
3961
3962 /* Set 'cpoptions' the way we want it.
3963 * B set - backslashes are *not* treated specially
3964 * k set - keycodes are *not* reverse-engineered
3965 * < unset - <Key> sequences *are* interpreted
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00003966 * The last but one parameter of replace_termcodes() is TRUE so that the
3967 * <lt> sequence is recognised - needed for a real backslash.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003968 */
3969 p_cpo = (char_u *)"Bk";
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00003970 str = replace_termcodes((char_u *)str, &ptr, FALSE, TRUE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003971 p_cpo = cpo_save;
3972
3973 if (*ptr != NUL) /* trailing CTRL-V results in nothing */
3974 {
3975 /*
3976 * Add the string to the input stream.
3977 * Can't use add_to_input_buf() here, we now have K_SPECIAL bytes.
3978 *
3979 * First clear typed characters from the typeahead buffer, there could
3980 * be half a mapping there. Then append to the existing string, so
3981 * that multiple commands from a client are concatenated.
3982 */
3983 if (typebuf.tb_maplen < typebuf.tb_len)
3984 del_typebuf(typebuf.tb_len - typebuf.tb_maplen, typebuf.tb_maplen);
3985 (void)ins_typebuf(str, REMAP_NONE, typebuf.tb_len, TRUE, FALSE);
3986
3987 /* Let input_available() know we inserted text in the typeahead
3988 * buffer. */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003989 typebuf_was_filled = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003990 }
3991 vim_free((char_u *)ptr);
3992}
3993
3994/*
3995 * Evaluate an expression that the client sent to a string.
3996 * Handles disabling error messages and disables debugging, otherwise Vim
3997 * hangs, waiting for "cont" to be typed.
3998 */
3999 char_u *
4000eval_client_expr_to_string(expr)
4001 char_u *expr;
4002{
4003 char_u *res;
4004 int save_dbl = debug_break_level;
4005 int save_ro = redir_off;
4006
4007 debug_break_level = -1;
4008 redir_off = 0;
4009 ++emsg_skip;
4010
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004011 res = eval_to_string(expr, NULL, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004012
4013 debug_break_level = save_dbl;
4014 redir_off = save_ro;
4015 --emsg_skip;
4016
Bram Moolenaar63a121b2005-12-11 21:36:39 +00004017 /* A client can tell us to redraw, but not to display the cursor, so do
4018 * that here. */
4019 setcursor();
4020 out_flush();
4021#ifdef FEAT_GUI
4022 if (gui.in_use)
4023 gui_update_cursor(FALSE, FALSE);
4024#endif
4025
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004026 return res;
4027}
4028
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004029/*
4030 * If conversion is needed, convert "data" from "client_enc" to 'encoding' and
4031 * return an allocated string. Otherwise return "data".
4032 * "*tofree" is set to the result when it needs to be freed later.
4033 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004034 char_u *
4035serverConvert(client_enc, data, tofree)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004036 char_u *client_enc UNUSED;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004037 char_u *data;
4038 char_u **tofree;
4039{
4040 char_u *res = data;
4041
4042 *tofree = NULL;
4043# ifdef FEAT_MBYTE
4044 if (client_enc != NULL && p_enc != NULL)
4045 {
4046 vimconv_T vimconv;
4047
4048 vimconv.vc_type = CONV_NONE;
4049 if (convert_setup(&vimconv, client_enc, p_enc) != FAIL
4050 && vimconv.vc_type != CONV_NONE)
4051 {
4052 res = string_convert(&vimconv, data, NULL);
4053 if (res == NULL)
4054 res = data;
4055 else
4056 *tofree = res;
4057 }
4058 convert_setup(&vimconv, NULL, NULL);
4059 }
4060# endif
4061 return res;
4062}
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004063#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004064
4065/*
4066 * When FEAT_FKMAP is defined, also compile the Farsi source code.
4067 */
4068#if defined(FEAT_FKMAP) || defined(PROTO)
4069# include "farsi.c"
4070#endif
4071
4072/*
4073 * When FEAT_ARABIC is defined, also compile the Arabic source code.
4074 */
4075#if defined(FEAT_ARABIC) || defined(PROTO)
4076# include "arabic.c"
4077#endif