blob: 6581ba93975470465483b952c14f6a7c1bcb4b77 [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 PROTO /* don't want a prototype for main() */
Bram Moolenaar8866d272012-11-28 15:55:42 +0100151#ifndef NO_VIM_MAIN /* skip this for unittests */
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}
Bram Moolenaar8866d272012-11-28 15:55:42 +0100573#endif
574#endif /* NO_VIM_MAIN */
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100575
Bram Moolenaar8866d272012-11-28 15:55:42 +0100576/* vim_main2() needs to be produced when FEAT_MZSCHEME is defined even when
577 * NO_VIM_MAIN is defined. */
578#ifdef FEAT_MZSCHEME
579 int
580vim_main2(int argc UNUSED, char **argv UNUSED)
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100581{
Bram Moolenaar8866d272012-11-28 15:55:42 +0100582# ifndef NO_VIM_MAIN
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100583 char_u *fname = (char_u *)argv[0];
584 mparm_T params;
585
586 memcpy(&params, argv[1], sizeof(params));
Bram Moolenaar8866d272012-11-28 15:55:42 +0100587# else
588 return 0;
589}
590# endif
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100591#endif
592
Bram Moolenaar8866d272012-11-28 15:55:42 +0100593#ifndef NO_VIM_MAIN
Bram Moolenaar58d98232005-07-23 22:25:46 +0000594 /* Execute --cmd arguments. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000595 exe_pre_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000596
Bram Moolenaar58d98232005-07-23 22:25:46 +0000597 /* Source startup scripts. */
598 source_startup_scripts(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000599
600#ifdef FEAT_EVAL
601 /*
602 * Read all the plugin files.
603 * Only when compiled with +eval, since most plugins need it.
604 */
605 if (p_lpl)
606 {
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000607# ifdef VMS /* Somehow VMS doesn't handle the "**". */
608 source_runtime((char_u *)"plugin/*.vim", TRUE);
609# else
Bram Moolenaar07d4d732005-10-03 22:04:08 +0000610 source_runtime((char_u *)"plugin/**/*.vim", TRUE);
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000611# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000612 TIME_MSG("loading plugins");
613 }
614#endif
615
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000616#ifdef FEAT_DIFF
617 /* Decide about window layout for diff mode after reading vimrc. */
618 if (params.diff_mode && params.window_layout == 0)
619 {
620 if (diffopt_horizontal())
621 params.window_layout = WIN_HOR; /* use horizontal split */
622 else
623 params.window_layout = WIN_VER; /* use vertical split */
624 }
625#endif
626
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000627 /*
628 * Recovery mode without a file name: List swap files.
629 * This uses the 'dir' option, therefore it must be after the
630 * initializations.
631 */
632 if (recoverymode && fname == NULL)
633 {
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200634 recover_names(NULL, TRUE, 0, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000635 mch_exit(0);
636 }
637
638 /*
639 * Set a few option defaults after reading .vimrc files:
640 * 'title' and 'icon', Unix: 'shellpipe' and 'shellredir'.
641 */
642 set_init_3();
643 TIME_MSG("inits 3");
644
645 /*
646 * "-n" argument: Disable swap file by setting 'updatecount' to 0.
647 * Note that this overrides anything from a vimrc file.
648 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000649 if (params.no_swap_file)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000650 p_uc = 0;
651
652#ifdef FEAT_FKMAP
653 if (curwin->w_p_rl && p_altkeymap)
654 {
655 p_hkmap = FALSE; /* Reset the Hebrew keymap mode */
656# ifdef FEAT_ARABIC
657 curwin->w_p_arab = FALSE; /* Reset the Arabic keymap mode */
658# endif
659 p_fkmap = TRUE; /* Set the Farsi keymap mode */
660 }
661#endif
662
663#ifdef FEAT_GUI
664 if (gui.starting)
665 {
666#if defined(UNIX) || defined(VMS)
667 /* When something caused a message from a vimrc script, need to output
668 * an extra newline before the shell prompt. */
669 if (did_emsg || msg_didout)
670 putchar('\n');
671#endif
672
673 gui_start(); /* will set full_screen to TRUE */
674 TIME_MSG("starting GUI");
675
676 /* When running "evim" or "gvim -y" we need the menus, exit if we
677 * don't have them. */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000678 if (!gui.in_use && params.evim_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000679 mch_exit(1);
680 }
681#endif
682
683#ifdef SPAWNO /* special MSDOS swapping library */
684 init_SPAWNO("", SWAP_ANY);
685#endif
686
687#ifdef FEAT_VIMINFO
688 /*
Bram Moolenaard812df62008-11-09 12:46:09 +0000689 * Read in registers, history etc, but not marks, from the viminfo file.
690 * This is where v:oldfiles gets filled.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000691 */
692 if (*p_viminfo != NUL)
693 {
Bram Moolenaard812df62008-11-09 12:46:09 +0000694 read_viminfo(NULL, VIF_WANT_INFO | VIF_GET_OLDFILES);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000695 TIME_MSG("reading viminfo");
696 }
697#endif
698
699#ifdef FEAT_QUICKFIX
700 /*
701 * "-q errorfile": Load the error file now.
702 * If the error file can't be read, exit before doing anything else.
703 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000704 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000705 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000706 if (params.use_ef != NULL)
707 set_string_option_direct((char_u *)"ef", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000708 params.use_ef, OPT_FREE, SID_CARG);
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200709 vim_snprintf((char *)IObuff, IOSIZE, "cfile %s", p_ef);
710 if (qf_init(NULL, p_ef, p_efm, TRUE, IObuff) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000711 {
712 out_char('\n');
713 mch_exit(3);
714 }
715 TIME_MSG("reading errorfile");
716 }
717#endif
718
719 /*
720 * Start putting things on the screen.
721 * Scroll screen down before drawing over it
722 * Clear screen now, so file message will not be cleared.
723 */
724 starting = NO_BUFFERS;
725 no_wait_return = FALSE;
726 if (!exmode_active)
727 msg_scroll = FALSE;
728
729#ifdef FEAT_GUI
730 /*
731 * This seems to be required to make callbacks to be called now, instead
732 * of after things have been put on the screen, which then may be deleted
733 * when getting a resize callback.
734 * For the Mac this handles putting files dropped on the Vim icon to
735 * global_alist.
736 */
737 if (gui.in_use)
738 {
739# ifdef FEAT_SUN_WORKSHOP
740 if (!usingSunWorkShop)
741# endif
742 gui_wait_for_chars(50L);
743 TIME_MSG("GUI delay");
744 }
745#endif
746
747#if defined(FEAT_GUI_PHOTON) && defined(FEAT_CLIPBOARD)
748 qnx_clip_init();
749#endif
750
Bram Moolenaarc8bbaa32010-07-14 16:54:21 +0200751#if defined(MACOS_X) && defined(FEAT_CLIPBOARD)
752 clip_init(TRUE);
753#endif
754
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000755#ifdef FEAT_XCLIPBOARD
756 /* Start using the X clipboard, unless the GUI was started. */
757# ifdef FEAT_GUI
758 if (!gui.in_use)
759# endif
760 {
761 setup_term_clip();
762 TIME_MSG("setup clipboard");
763 }
764#endif
765
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000766#ifdef FEAT_CLIENTSERVER
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000767 /* Prepare for being a Vim server. */
768 prepare_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000769#endif
770
771 /*
772 * If "-" argument given: Read file from stdin.
773 * Do this before starting Raw mode, because it may change things that the
774 * writing end of the pipe doesn't like, e.g., in case stdin and stderr
775 * are the same terminal: "cat | vim -".
776 * Using autocommands here may cause trouble...
777 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000778 if (params.edit_type == EDIT_STDIN && !recoverymode)
779 read_stdin();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000780
781#if defined(UNIX) || defined(VMS)
782 /* When switching screens and something caused a message from a vimrc
783 * script, need to output an extra newline on exit. */
784 if ((did_emsg || msg_didout) && *T_TI != NUL)
785 newline_on_exit = TRUE;
786#endif
787
788 /*
789 * When done something that is not allowed or error message call
790 * wait_return. This must be done before starttermcap(), because it may
791 * switch to another screen. It must be done after settmode(TMODE_RAW),
792 * because we want to react on a single key stroke.
793 * Call settmode and starttermcap here, so the T_KS and T_TI may be
Bram Moolenaar49325942007-05-10 19:19:59 +0000794 * defined by termcapinit and redefined in .exrc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000795 */
796 settmode(TMODE_RAW);
797 TIME_MSG("setting raw mode");
798
799 if (need_wait_return || msg_didany)
800 {
801 wait_return(TRUE);
802 TIME_MSG("waiting for return");
803 }
804
805 starttermcap(); /* start termcap if not done by wait_return() */
806 TIME_MSG("start termcap");
807
808#ifdef FEAT_MOUSE
809 setmouse(); /* may start using the mouse */
810#endif
811 if (scroll_region)
812 scroll_region_reset(); /* In case Rows changed */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000813 scroll_start(); /* may scroll the screen to the right position */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000814
815 /*
816 * Don't clear the screen when starting in Ex mode, unless using the GUI.
817 */
818 if (exmode_active
819#ifdef FEAT_GUI
820 && !gui.in_use
821#endif
822 )
823 must_redraw = CLEAR;
824 else
825 {
826 screenclear(); /* clear screen */
827 TIME_MSG("clearing screen");
828 }
829
830#ifdef FEAT_CRYPT
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000831 if (params.ask_for_key)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000832 {
Bram Moolenaar40e6a712010-05-16 22:32:54 +0200833 (void)blowfish_self_test();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000834 (void)get_crypt_key(TRUE, TRUE);
835 TIME_MSG("getting crypt key");
836 }
837#endif
838
839 no_wait_return = TRUE;
840
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000841 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000842 * Create the requested number of windows and edit buffers in them.
843 * Also does recovery if "recoverymode" set.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000844 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000845 create_windows(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000846 TIME_MSG("opening buffers");
847
Bram Moolenaar867a4b72007-03-18 20:51:46 +0000848#ifdef FEAT_EVAL
849 /* clear v:swapcommand */
850 set_vim_var_string(VV_SWAPCOMMAND, NULL, -1);
851#endif
852
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000853 /* Ex starts at last line of the file */
854 if (exmode_active)
855 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
856
857#ifdef FEAT_AUTOCMD
858 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
859 TIME_MSG("BufEnter autocommands");
860#endif
861 setpcmark();
862
863#ifdef FEAT_QUICKFIX
864 /*
865 * When started with "-q errorfile" jump to first error now.
866 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000867 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000868 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000869 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000870 TIME_MSG("jump to first error");
871 }
872#endif
873
874#ifdef FEAT_WINDOWS
875 /*
876 * If opened more than one window, start editing files in the other
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000877 * windows.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000878 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000879 edit_buffers(&params);
880#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000881
882#ifdef FEAT_DIFF
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000883 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000884 {
885 win_T *wp;
886
887 /* set options in each window for "vimdiff". */
888 for (wp = firstwin; wp != NULL; wp = wp->w_next)
889 diff_win_options(wp, TRUE);
890 }
891#endif
892
893 /*
894 * Shorten any of the filenames, but only when absolute.
895 */
896 shorten_fnames(FALSE);
897
898 /*
899 * Need to jump to the tag before executing the '-c command'.
900 * Makes "vim -c '/return' -t main" work.
901 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000902 if (params.tagname != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000903 {
Bram Moolenaar146522e2005-12-16 21:55:46 +0000904#if defined(HAS_SWAP_EXISTS_ACTION)
905 swap_exists_did_quit = FALSE;
906#endif
907
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000908 vim_snprintf((char *)IObuff, IOSIZE, "ta %s", params.tagname);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000909 do_cmdline_cmd(IObuff);
910 TIME_MSG("jumping to tag");
Bram Moolenaar146522e2005-12-16 21:55:46 +0000911
912#if defined(HAS_SWAP_EXISTS_ACTION)
913 /* If the user doesn't want to edit the file then we quit here. */
914 if (swap_exists_did_quit)
915 getout(1);
916#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000917 }
918
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000919 /* Execute any "+", "-c" and "-S" arguments. */
920 if (params.n_commands > 0)
921 exe_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000922
923 RedrawingDisabled = 0;
924 redraw_all_later(NOT_VALID);
925 no_wait_return = FALSE;
926 starting = 0;
927
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000928#ifdef FEAT_TERMRESPONSE
929 /* Requesting the termresponse is postponed until here, so that a "-c q"
930 * argument doesn't make it appear in the shell Vim was started from. */
931 may_req_termresponse();
932#endif
933
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000934 /* start in insert mode */
935 if (p_im)
936 need_start_insertmode = TRUE;
937
938#ifdef FEAT_AUTOCMD
939 apply_autocmds(EVENT_VIMENTER, NULL, NULL, FALSE, curbuf);
940 TIME_MSG("VimEnter autocommands");
941#endif
942
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200943#if defined(FEAT_EVAL) && defined(FEAT_CLIPBOARD)
944 /* Adjust default register name for "unnamed" in 'clipboard'. Can only be
945 * done after the clipboard is available and all initial commands that may
946 * modify the 'clipboard' setting have run; i.e. just before entering the
947 * main loop. */
948 {
949 int default_regname = 0;
950 adjust_clip_reg(&default_regname);
951 set_reg_var(default_regname);
952 }
953#endif
954
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000955#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
956 /* When a startup script or session file setup for diff'ing and
957 * scrollbind, sync the scrollbind now. */
958 if (curwin->w_p_diff && curwin->w_p_scb)
959 {
960 update_topline();
961 check_scrollbind((linenr_T)0, 0L);
962 TIME_MSG("diff scrollbinding");
963 }
964#endif
965
966#if defined(WIN3264) && !defined(FEAT_GUI_W32)
967 mch_set_winsize_now(); /* Allow winsize changes from now on */
968#endif
969
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000970#if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
971 /* When tab pages were created, may need to update the tab pages line and
972 * scrollbars. This is skipped while creating them. */
973 if (first_tabpage->tp_next != NULL)
974 {
975 out_flush();
976 gui_init_which_components(NULL);
977 gui_update_scrollbars(TRUE);
978 }
979 need_mouse_correct = TRUE;
980#endif
981
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000982 /* If ":startinsert" command used, stuff a dummy command to be able to
983 * call normal_cmd(), which will then start Insert mode. */
984 if (restart_edit != 0)
Bram Moolenaarebefac62005-12-28 22:39:57 +0000985 stuffcharReadbuff(K_NOP);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000986
987#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200988 if (netbeansArg != NULL && strncmp("-nb", netbeansArg, 3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200989 {
990# ifdef FEAT_GUI
Bram Moolenaar173c9852010-09-29 17:27:01 +0200991# if !defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK) \
Bram Moolenaar67c53842010-05-22 18:28:27 +0200992 && !defined(FEAT_GUI_W32)
993 if (gui.in_use)
994 {
995 mch_errmsg(_("netbeans is not supported with this GUI\n"));
996 mch_exit(2);
997 }
998# endif
999# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001000 /* Tell the client that it can start sending commands. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001001 netbeans_open(netbeansArg + 3, TRUE);
Bram Moolenaar67c53842010-05-22 18:28:27 +02001002 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001003#endif
1004
1005 TIME_MSG("before starting main loop");
1006
1007 /*
1008 * Call the main command loop. This never returns.
Bram Moolenaarbbc98db2012-02-12 01:55:55 +01001009 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001010 main_loop(FALSE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001011
1012 return 0;
1013}
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01001014#endif /* NO_VIM_MAIN */
Bram Moolenaar8866d272012-11-28 15:55:42 +01001015#endif /* PROTO */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001016
1017/*
1018 * Main loop: Execute Normal mode commands until exiting Vim.
1019 * Also used to handle commands in the command-line window, until the window
1020 * is closed.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001021 * Also used to handle ":visual" command after ":global": execute Normal mode
1022 * commands, return when entering Ex mode. "noexmode" is TRUE then.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001023 */
1024 void
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001025main_loop(cmdwin, noexmode)
1026 int cmdwin; /* TRUE when working in the command-line window */
1027 int noexmode; /* TRUE when return on entering Ex mode */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001028{
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001029 oparg_T oa; /* operator arguments */
1030 int previous_got_int = FALSE; /* "got_int" was TRUE */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001031#ifdef FEAT_CONCEAL
1032 linenr_T conceal_old_cursor_line = 0;
1033 linenr_T conceal_new_cursor_line = 0;
1034 int conceal_update_lines = FALSE;
1035#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001036
1037#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
1038 /* Setup to catch a terminating error from the X server. Just ignore
1039 * it, restore the state and continue. This might not always work
1040 * properly, but at least we don't exit unexpectedly when the X server
1041 * exists while Vim is running in a console. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001042 if (!cmdwin && !noexmode && SETJMP(x_jump_env))
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001043 {
1044 State = NORMAL;
1045# ifdef FEAT_VISUAL
1046 VIsual_active = FALSE;
1047# endif
1048 got_int = TRUE;
1049 need_wait_return = FALSE;
1050 global_busy = FALSE;
1051 exmode_active = 0;
1052 skip_redraw = FALSE;
1053 RedrawingDisabled = 0;
1054 no_wait_return = 0;
Bram Moolenaar7701c242011-10-04 16:43:53 +02001055 vgetc_busy = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001056# ifdef FEAT_EVAL
1057 emsg_skip = 0;
1058# endif
1059 emsg_off = 0;
1060# ifdef FEAT_MOUSE
1061 setmouse();
1062# endif
1063 settmode(TMODE_RAW);
1064 starttermcap();
1065 scroll_start();
1066 redraw_later_clear();
1067 }
1068#endif
1069
1070 clear_oparg(&oa);
1071 while (!cmdwin
1072#ifdef FEAT_CMDWIN
1073 || cmdwin_result == 0
1074#endif
1075 )
1076 {
1077 if (stuff_empty())
1078 {
1079 did_check_timestamps = FALSE;
1080 if (need_check_timestamps)
1081 check_timestamps(FALSE);
1082 if (need_wait_return) /* if wait_return still needed ... */
1083 wait_return(FALSE); /* ... call it now */
1084 if (need_start_insertmode && goto_im()
1085#ifdef FEAT_VISUAL
1086 && !VIsual_active
1087#endif
1088 )
1089 {
1090 need_start_insertmode = FALSE;
1091 stuffReadbuff((char_u *)"i"); /* start insert mode next */
1092 /* skip the fileinfo message now, because it would be shown
1093 * after insert mode finishes! */
1094 need_fileinfo = FALSE;
1095 }
1096 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001097
1098 /* Reset "got_int" now that we got back to the main loop. Except when
1099 * inside a ":g/pat/cmd" command, then the "got_int" needs to abort
1100 * the ":g" command.
1101 * For ":g/pat/vi" we reset "got_int" when used once. When used
1102 * a second time we go back to Ex mode and abort the ":g" command. */
1103 if (got_int)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001104 {
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001105 if (noexmode && global_busy && !exmode_active && previous_got_int)
1106 {
1107 /* Typed two CTRL-C in a row: go back to ex mode as if "Q" was
1108 * used and keep "got_int" set, so that it aborts ":g". */
1109 exmode_active = EXMODE_NORMAL;
1110 State = NORMAL;
1111 }
1112 else if (!global_busy || !exmode_active)
1113 {
1114 if (!quit_more)
1115 (void)vgetc(); /* flush all buffers */
1116 got_int = FALSE;
1117 }
1118 previous_got_int = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001119 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001120 else
1121 previous_got_int = FALSE;
1122
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001123 if (!exmode_active)
1124 msg_scroll = FALSE;
1125 quit_more = FALSE;
1126
1127 /*
1128 * If skip redraw is set (for ":" in wait_return()), don't redraw now.
1129 * If there is nothing in the stuff_buffer or do_redraw is TRUE,
1130 * update cursor and redraw.
1131 */
1132 if (skip_redraw || exmode_active)
1133 skip_redraw = FALSE;
1134 else if (do_redraw || stuff_empty())
1135 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001136#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001137 /* Trigger CursorMoved if the cursor moved. */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001138 if (!finish_op && (
1139# ifdef FEAT_AUTOCMD
1140 has_cursormoved()
1141# endif
1142# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
1143 ||
1144# endif
1145# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001146 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001147# endif
1148 )
1149 && !equalpos(last_cursormoved, curwin->w_cursor))
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001150 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001151# ifdef FEAT_AUTOCMD
1152 if (has_cursormoved())
1153 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL,
1154 FALSE, curbuf);
1155# endif
1156# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001157 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001158 {
1159 conceal_old_cursor_line = last_cursormoved.lnum;
1160 conceal_new_cursor_line = curwin->w_cursor.lnum;
1161 conceal_update_lines = TRUE;
1162 }
1163# endif
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001164 last_cursormoved = curwin->w_cursor;
1165 }
1166#endif
1167
Bram Moolenaar33aec762006-01-22 23:30:12 +00001168#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
1169 /* Scroll-binding for diff mode may have been postponed until
1170 * here. Avoids doing it for every change. */
1171 if (diff_need_scrollbind)
1172 {
1173 check_scrollbind((linenr_T)0, 0L);
1174 diff_need_scrollbind = FALSE;
1175 }
1176#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001177#if defined(FEAT_FOLDING) && defined(FEAT_VISUAL)
1178 /* Include a closed fold completely in the Visual area. */
1179 foldAdjustVisual();
1180#endif
1181#ifdef FEAT_FOLDING
1182 /*
1183 * When 'foldclose' is set, apply 'foldlevel' to folds that don't
1184 * contain the cursor.
1185 * When 'foldopen' is "all", open the fold(s) under the cursor.
1186 * This may mark the window for redrawing.
1187 */
1188 if (hasAnyFolding(curwin) && !char_avail())
1189 {
1190 foldCheckClose();
1191 if (fdo_flags & FDO_ALL)
1192 foldOpenCursor();
1193 }
1194#endif
1195
1196 /*
1197 * Before redrawing, make sure w_topline is correct, and w_leftcol
1198 * if lines don't wrap, and w_skipcol if lines wrap.
1199 */
1200 update_topline();
1201 validate_cursor();
1202
1203#ifdef FEAT_VISUAL
1204 if (VIsual_active)
1205 update_curbuf(INVERTED);/* update inverted part */
1206 else
1207#endif
1208 if (must_redraw)
1209 update_screen(0);
1210 else if (redraw_cmdline || clear_cmdline)
1211 showmode();
1212#ifdef FEAT_WINDOWS
1213 redraw_statuslines();
1214#endif
1215#ifdef FEAT_TITLE
1216 if (need_maketitle)
1217 maketitle();
1218#endif
1219 /* display message after redraw */
1220 if (keep_msg != NULL)
1221 {
1222 char_u *p;
1223
1224 /* msg_attr_keep() will set keep_msg to NULL, must free the
1225 * string here. */
1226 p = keep_msg;
Bram Moolenaar238a5642006-02-21 22:12:05 +00001227 keep_msg = NULL;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001228 msg_attr(p, keep_msg_attr);
1229 vim_free(p);
1230 }
1231 if (need_fileinfo) /* show file info after redraw */
1232 {
1233 fileinfo(FALSE, TRUE, FALSE);
1234 need_fileinfo = FALSE;
1235 }
1236
1237 emsg_on_display = FALSE; /* can delete error message now */
1238 did_emsg = FALSE;
1239 msg_didany = FALSE; /* reset lines_left in msg_start() */
Bram Moolenaar661b1822005-07-28 22:36:45 +00001240 may_clear_sb_text(); /* clear scroll-back text on next msg */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001241 showruler(FALSE);
1242
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001243# if defined(FEAT_CONCEAL)
1244 if (conceal_update_lines
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001245 && (conceal_old_cursor_line != conceal_new_cursor_line
1246 || conceal_cursor_line(curwin)
1247 || need_cursor_line_redraw))
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001248 {
Bram Moolenaarc2b4c622011-02-15 16:29:59 +01001249 if (conceal_old_cursor_line != conceal_new_cursor_line
1250 && conceal_old_cursor_line
1251 <= curbuf->b_ml.ml_line_count)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001252 update_single_line(curwin, conceal_old_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001253 update_single_line(curwin, conceal_new_cursor_line);
1254 curwin->w_valid &= ~VALID_CROW;
1255 }
1256# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001257 setcursor();
1258 cursor_on();
1259
1260 do_redraw = FALSE;
Bram Moolenaar3f269672009-11-03 11:11:11 +00001261
1262#ifdef STARTUPTIME
1263 /* Now that we have drawn the first screen all the startup stuff
1264 * has been done, close any file for startup messages. */
1265 if (time_fd != NULL)
1266 {
1267 TIME_MSG("first screen update");
1268 TIME_MSG("--- VIM STARTED ---");
1269 fclose(time_fd);
1270 time_fd = NULL;
1271 }
1272#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001273 }
1274#ifdef FEAT_GUI
1275 if (need_mouse_correct)
1276 gui_mouse_correct();
1277#endif
1278
1279 /*
1280 * Update w_curswant if w_set_curswant has been set.
1281 * Postponed until here to avoid computing w_virtcol too often.
1282 */
1283 update_curswant();
1284
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001285#ifdef FEAT_EVAL
1286 /*
1287 * May perform garbage collection when waiting for a character, but
1288 * only at the very toplevel. Otherwise we may be using a List or
1289 * Dict internally somewhere.
1290 * "may_garbage_collect" is reset in vgetc() which is invoked through
1291 * do_exmode() and normal_cmd().
1292 */
1293 may_garbage_collect = (!cmdwin && !noexmode);
1294#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001295 /*
1296 * If we're invoked as ex, do a round of ex commands.
1297 * Otherwise, get and execute a normal mode command.
1298 */
1299 if (exmode_active)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001300 {
1301 if (noexmode) /* End of ":global/path/visual" commands */
1302 return;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001303 do_exmode(exmode_active == EXMODE_VIM);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001304 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001305 else
1306 normal_cmd(&oa, TRUE);
1307 }
1308}
1309
1310
1311#if defined(USE_XSMP) || defined(FEAT_GUI_MSWIN) || defined(PROTO)
1312/*
1313 * Exit, but leave behind swap files for modified buffers.
1314 */
1315 void
1316getout_preserve_modified(exitval)
1317 int exitval;
1318{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001319# if defined(SIGHUP) && defined(SIG_IGN)
1320 /* Ignore SIGHUP, because a dropped connection causes a read error, which
1321 * makes Vim exit and then handling SIGHUP causes various reentrance
1322 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001323 signal(SIGHUP, SIG_IGN);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001324# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001325
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001326 ml_close_notmod(); /* close all not-modified buffers */
1327 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
1328 ml_close_all(FALSE); /* close all memfiles, without deleting */
1329 getout(exitval); /* exit Vim properly */
1330}
1331#endif
1332
1333
1334/* Exit properly */
1335 void
1336getout(exitval)
1337 int exitval;
1338{
1339#ifdef FEAT_AUTOCMD
1340 buf_T *buf;
1341 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001342 tabpage_T *tp, *next_tp;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001343#endif
1344
1345 exiting = TRUE;
1346
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001347 /* When running in Ex mode an error causes us to exit with a non-zero exit
1348 * code. POSIX requires this, although it's not 100% clear from the
1349 * standard. */
1350 if (exmode_active)
1351 exitval += ex_exitval;
1352
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001353 /* Position the cursor on the last screen line, below all the text */
1354#ifdef FEAT_GUI
1355 if (!gui.in_use)
1356#endif
1357 windgoto((int)Rows - 1, 0);
1358
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +00001359#if defined(FEAT_EVAL) || defined(FEAT_SYN_HL)
1360 /* Optionally print hashtable efficiency. */
1361 hash_debug_results();
1362#endif
1363
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001364#ifdef FEAT_GUI
1365 msg_didany = FALSE;
1366#endif
1367
1368#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001369 if (get_vim_var_nr(VV_DYING) <= 1)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001370 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001371 /* Trigger BufWinLeave for all windows, but only once per buffer. */
1372# if defined FEAT_WINDOWS
1373 for (tp = first_tabpage; tp != NULL; tp = next_tp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001374 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001375 next_tp = tp->tp_next;
1376 for (wp = (tp == curtab)
1377 ? firstwin : tp->tp_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001378 {
Bram Moolenaar802418d2013-01-17 14:00:11 +01001379 if (wp->w_buffer == NULL)
1380 /* Autocmd must have close the buffer already, skip. */
1381 continue;
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001382 buf = wp->w_buffer;
1383 if (buf->b_changedtick != -1)
1384 {
1385 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname,
1386 buf->b_fname, FALSE, buf);
Bram Moolenaarb429cde2012-04-25 18:24:29 +02001387 buf->b_changedtick = -1; /* note that we did it already */
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001388 /* start all over, autocommands may mess up the lists */
1389 next_tp = first_tabpage;
1390 break;
1391 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001392 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001393 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001394# else
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001395 apply_autocmds(EVENT_BUFWINLEAVE, curbuf, curbuf->b_fname,
1396 FALSE, curbuf);
Bram Moolenaarf740b292006-02-16 22:11:02 +00001397# endif
Bram Moolenaar33aec762006-01-22 23:30:12 +00001398
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001399 /* Trigger BufUnload for buffers that are loaded */
1400 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1401 if (buf->b_ml.ml_mfp != NULL)
1402 {
1403 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001404 FALSE, buf);
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001405 if (!buf_valid(buf)) /* autocmd may delete the buffer */
1406 break;
1407 }
1408 apply_autocmds(EVENT_VIMLEAVEPRE, NULL, NULL, FALSE, curbuf);
1409 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001410#endif
1411
1412#ifdef FEAT_VIMINFO
1413 if (*p_viminfo != NUL)
1414 /* Write out the registers, history, marks etc, to the viminfo file */
1415 write_viminfo(NULL, FALSE);
1416#endif
1417
1418#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001419 if (get_vim_var_nr(VV_DYING) <= 1)
1420 apply_autocmds(EVENT_VIMLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001421#endif
1422
Bram Moolenaar05159a02005-02-26 23:04:13 +00001423#ifdef FEAT_PROFILE
1424 profile_dump();
1425#endif
1426
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001427 if (did_emsg
1428#ifdef FEAT_GUI
1429 || (gui.in_use && msg_didany && p_verbose > 0)
1430#endif
1431 )
1432 {
1433 /* give the user a chance to read the (error) message */
1434 no_wait_return = FALSE;
1435 wait_return(FALSE);
1436 }
1437
1438#ifdef FEAT_AUTOCMD
1439 /* Position the cursor again, the autocommands may have moved it */
1440# ifdef FEAT_GUI
1441 if (!gui.in_use)
1442# endif
1443 windgoto((int)Rows - 1, 0);
1444#endif
1445
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001446#ifdef FEAT_LUA
1447 lua_end();
1448#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001449#ifdef FEAT_MZSCHEME
1450 mzscheme_end();
1451#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001452#ifdef FEAT_TCL
1453 tcl_end();
1454#endif
1455#ifdef FEAT_RUBY
1456 ruby_end();
1457#endif
1458#ifdef FEAT_PYTHON
1459 python_end();
1460#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001461#ifdef FEAT_PYTHON3
1462 python3_end();
1463#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001464#ifdef FEAT_PERL
1465 perl_end();
1466#endif
1467#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
1468 iconv_end();
1469#endif
1470#ifdef FEAT_NETBEANS_INTG
1471 netbeans_end();
1472#endif
Bram Moolenaar02b06312007-09-06 15:39:22 +00001473#ifdef FEAT_CSCOPE
1474 cs_end();
1475#endif
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001476#ifdef FEAT_EVAL
1477 if (garbage_collect_at_exit)
1478 garbage_collect();
1479#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001480
1481 mch_exit(exitval);
1482}
1483
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01001484#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001485/*
1486 * Get a (optional) count for a Vim argument.
1487 */
1488 static int
1489get_number_arg(p, idx, def)
1490 char_u *p; /* pointer to argument */
1491 int *idx; /* index in argument, is incremented */
1492 int def; /* default value */
1493{
1494 if (vim_isdigit(p[*idx]))
1495 {
1496 def = atoi((char *)&(p[*idx]));
1497 while (vim_isdigit(p[*idx]))
1498 *idx = *idx + 1;
1499 }
1500 return def;
1501}
1502
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001503#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1504/*
1505 * Setup to use the current locale (for ctype() and many other things).
1506 */
1507 static void
1508init_locale()
1509{
1510 setlocale(LC_ALL, "");
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001511
Bram Moolenaarc6af8122010-05-21 12:04:55 +02001512# ifdef FEAT_GUI_GTK
1513 /* Tell Gtk not to change our locale settings. */
1514 gtk_disable_setlocale();
1515# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001516# if defined(FEAT_FLOAT) && defined(LC_NUMERIC)
1517 /* Make sure strtod() uses a decimal point, not a comma. */
1518 setlocale(LC_NUMERIC, "C");
1519# endif
1520
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001521# ifdef WIN32
1522 /* Apparently MS-Windows printf() may cause a crash when we give it 8-bit
1523 * text while it's expecting text in the current locale. This call avoids
1524 * that. */
1525 setlocale(LC_CTYPE, "C");
1526# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001527
1528# ifdef FEAT_GETTEXT
1529 {
1530 int mustfree = FALSE;
1531 char_u *p;
1532
1533# ifdef DYNAMIC_GETTEXT
1534 /* Initialize the gettext library */
1535 dyn_libintl_init(NULL);
1536# endif
1537 /* expand_env() doesn't work yet, because chartab[] is not initialized
1538 * yet, call vim_getenv() directly */
1539 p = vim_getenv((char_u *)"VIMRUNTIME", &mustfree);
1540 if (p != NULL && *p != NUL)
1541 {
Bram Moolenaar1ad2f132007-06-19 18:27:18 +00001542 vim_snprintf((char *)NameBuff, MAXPATHL, "%s/lang", p);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001543 bindtextdomain(VIMPACKAGE, (char *)NameBuff);
1544 }
1545 if (mustfree)
1546 vim_free(p);
1547 textdomain(VIMPACKAGE);
1548 }
1549# endif
1550}
1551#endif
1552
1553/*
1554 * Check for: [r][e][g][vi|vim|view][diff][ex[im]]
1555 * If the executable name starts with "r" we disable shell commands.
1556 * If the next character is "e" we run in Easy mode.
1557 * If the next character is "g" we run the GUI version.
1558 * If the next characters are "view" we start in readonly mode.
1559 * If the next characters are "diff" or "vimdiff" we start in diff mode.
1560 * If the next characters are "ex" we start in Ex mode. If it's followed
1561 * by "im" use improved Ex mode.
1562 */
1563 static void
1564parse_command_name(parmp)
1565 mparm_T *parmp;
1566{
1567 char_u *initstr;
1568
1569 initstr = gettail((char_u *)parmp->argv[0]);
1570
1571#ifdef MACOS_X_UNIX
1572 /* An issue has been seen when launching Vim in such a way that
1573 * $PWD/$ARGV[0] or $ARGV[0] is not the absolute path to the
1574 * executable or a symbolic link of it. Until this issue is resolved
1575 * we prohibit the GUI from being used.
1576 */
1577 if (STRCMP(initstr, parmp->argv[0]) == 0)
1578 disallow_gui = TRUE;
1579
1580 /* TODO: On MacOS X default to gui if argv[0] ends in:
Bram Moolenaar27dc1952006-03-15 23:06:44 +00001581 * /Vim.app/Contents/MacOS/Vim */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001582#endif
1583
1584#ifdef FEAT_EVAL
1585 set_vim_var_string(VV_PROGNAME, initstr, -1);
1586#endif
1587
1588 if (TOLOWER_ASC(initstr[0]) == 'r')
1589 {
1590 restricted = TRUE;
1591 ++initstr;
1592 }
1593
Bram Moolenaarad249fb2010-05-07 16:35:04 +02001594 /* Use evim mode for "evim" and "egvim", not for "editor". */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001595 if (TOLOWER_ASC(initstr[0]) == 'e'
1596 && (TOLOWER_ASC(initstr[1]) == 'v'
1597 || TOLOWER_ASC(initstr[1]) == 'g'))
1598 {
1599#ifdef FEAT_GUI
1600 gui.starting = TRUE;
1601#endif
1602 parmp->evim_mode = TRUE;
1603 ++initstr;
1604 }
1605
Bram Moolenaar806875d2008-09-18 18:57:10 +00001606 /* "gvim" starts the GUI. Also accept "Gvim" for MS-Windows. */
1607 if (TOLOWER_ASC(initstr[0]) == 'g')
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001608 {
1609 main_start_gui();
1610#ifdef FEAT_GUI
1611 ++initstr;
1612#endif
1613 }
1614
1615 if (STRNICMP(initstr, "view", 4) == 0)
1616 {
1617 readonlymode = TRUE;
1618 curbuf->b_p_ro = TRUE;
1619 p_uc = 10000; /* don't update very often */
1620 initstr += 4;
1621 }
1622 else if (STRNICMP(initstr, "vim", 3) == 0)
1623 initstr += 3;
1624
1625 /* Catch "[r][g]vimdiff" and "[r][g]viewdiff". */
1626 if (STRICMP(initstr, "diff") == 0)
1627 {
1628#ifdef FEAT_DIFF
1629 parmp->diff_mode = TRUE;
1630#else
1631 mch_errmsg(_("This Vim was not compiled with the diff feature."));
1632 mch_errmsg("\n");
1633 mch_exit(2);
1634#endif
1635 }
1636
1637 if (STRNICMP(initstr, "ex", 2) == 0)
1638 {
1639 if (STRNICMP(initstr + 2, "im", 2) == 0)
1640 exmode_active = EXMODE_VIM;
1641 else
1642 exmode_active = EXMODE_NORMAL;
1643 change_compatible(TRUE); /* set 'compatible' */
1644 }
1645}
1646
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001647/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00001648 * Get the name of the display, before gui_prepare() removes it from
1649 * argv[]. Used for the xterm-clipboard display.
1650 *
Bram Moolenaar78e17622007-08-30 10:26:19 +00001651 * Also find the --server... arguments and --socketid and --windowid
Bram Moolenaar58d98232005-07-23 22:25:46 +00001652 */
Bram Moolenaar58d98232005-07-23 22:25:46 +00001653 static void
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001654early_arg_scan(parmp)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00001655 mparm_T *parmp UNUSED;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001656{
Bram Moolenaar03005972008-11-20 13:12:36 +00001657#if defined(FEAT_XCLIPBOARD) || defined(FEAT_CLIENTSERVER) \
1658 || !defined(FEAT_NETBEANS_INTG)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001659 int argc = parmp->argc;
1660 char **argv = parmp->argv;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001661 int i;
1662
1663 for (i = 1; i < argc; i++)
1664 {
1665 if (STRCMP(argv[i], "--") == 0)
1666 break;
1667# ifdef FEAT_XCLIPBOARD
1668 else if (STRICMP(argv[i], "-display") == 0
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001669# if defined(FEAT_GUI_GTK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001670 || STRICMP(argv[i], "--display") == 0
1671# endif
1672 )
1673 {
1674 if (i == argc - 1)
1675 mainerr_arg_missing((char_u *)argv[i]);
1676 xterm_display = argv[++i];
1677 }
1678# endif
1679# ifdef FEAT_CLIENTSERVER
1680 else if (STRICMP(argv[i], "--servername") == 0)
1681 {
1682 if (i == argc - 1)
1683 mainerr_arg_missing((char_u *)argv[i]);
1684 parmp->serverName_arg = (char_u *)argv[++i];
1685 }
Bram Moolenaareb94e552006-03-11 21:35:11 +00001686 else if (STRICMP(argv[i], "--serverlist") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001687 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001688 else if (STRNICMP(argv[i], "--remote", 8) == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001689 {
1690 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001691# ifdef FEAT_GUI
1692 if (strstr(argv[i], "-wait") != 0)
1693 /* don't fork() when starting the GUI to edit files ourself */
1694 gui.dofork = FALSE;
1695# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001696 }
1697# endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001698
1699# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1700# ifdef FEAT_GUI_W32
1701 else if (STRICMP(argv[i], "--windowid") == 0)
1702# else
Bram Moolenaar58d98232005-07-23 22:25:46 +00001703 else if (STRICMP(argv[i], "--socketid") == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001704# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001705 {
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001706 long_u id;
1707 int count;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001708
1709 if (i == argc - 1)
1710 mainerr_arg_missing((char_u *)argv[i]);
1711 if (STRNICMP(argv[i+1], "0x", 2) == 0)
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001712 count = sscanf(&(argv[i + 1][2]), SCANF_HEX_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001713 else
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001714 count = sscanf(argv[i + 1], SCANF_DECIMAL_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001715 if (count != 1)
1716 mainerr(ME_INVALID_ARG, (char_u *)argv[i]);
1717 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00001718# ifdef FEAT_GUI_W32
1719 win_socket_id = id;
1720# else
1721 gtk_socket_id = id;
1722# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001723 i++;
1724 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001725# endif
1726# ifdef FEAT_GUI_GTK
Bram Moolenaar58d98232005-07-23 22:25:46 +00001727 else if (STRICMP(argv[i], "--echo-wid") == 0)
1728 echo_wid_arg = TRUE;
1729# endif
Bram Moolenaar03005972008-11-20 13:12:36 +00001730# ifndef FEAT_NETBEANS_INTG
1731 else if (strncmp(argv[i], "-nb", (size_t)3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +02001732 {
1733 mch_errmsg(_("'-nb' cannot be used: not enabled at compile time\n"));
1734 mch_exit(2);
1735 }
Bram Moolenaar03005972008-11-20 13:12:36 +00001736# endif
1737
Bram Moolenaar58d98232005-07-23 22:25:46 +00001738 }
1739#endif
1740}
1741
1742/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001743 * Scan the command line arguments.
1744 */
1745 static void
1746command_line_scan(parmp)
1747 mparm_T *parmp;
1748{
1749 int argc = parmp->argc;
1750 char **argv = parmp->argv;
1751 int argv_idx; /* index in argv[n][] */
1752 int had_minmin = FALSE; /* found "--" argument */
1753 int want_argument; /* option argument with argument */
1754 int c;
Bram Moolenaar231334e2005-07-25 20:46:57 +00001755 char_u *p = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001756 long n;
1757
1758 --argc;
1759 ++argv;
1760 argv_idx = 1; /* active option letter is argv[0][argv_idx] */
1761 while (argc > 0)
1762 {
1763 /*
1764 * "+" or "+{number}" or "+/{pat}" or "+{command}" argument.
1765 */
1766 if (argv[0][0] == '+' && !had_minmin)
1767 {
1768 if (parmp->n_commands >= MAX_ARG_CMDS)
1769 mainerr(ME_EXTRA_CMD, NULL);
1770 argv_idx = -1; /* skip to next argument */
1771 if (argv[0][1] == NUL)
1772 parmp->commands[parmp->n_commands++] = (char_u *)"$";
1773 else
1774 parmp->commands[parmp->n_commands++] = (char_u *)&(argv[0][1]);
1775 }
1776
1777 /*
1778 * Optional argument.
1779 */
1780 else if (argv[0][0] == '-' && !had_minmin)
1781 {
1782 want_argument = FALSE;
1783 c = argv[0][argv_idx++];
1784#ifdef VMS
1785 /*
1786 * VMS only uses upper case command lines. Interpret "-X" as "-x"
1787 * and "-/X" as "-X".
1788 */
1789 if (c == '/')
1790 {
1791 c = argv[0][argv_idx++];
1792 c = TOUPPER_ASC(c);
1793 }
1794 else
1795 c = TOLOWER_ASC(c);
1796#endif
1797 switch (c)
1798 {
1799 case NUL: /* "vim -" read from stdin */
1800 /* "ex -" silent mode */
1801 if (exmode_active)
1802 silent_mode = TRUE;
1803 else
1804 {
1805 if (parmp->edit_type != EDIT_NONE)
1806 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1807 parmp->edit_type = EDIT_STDIN;
1808 read_cmd_fd = 2; /* read from stderr instead of stdin */
1809 }
1810 argv_idx = -1; /* skip to next argument */
1811 break;
1812
1813 case '-': /* "--" don't take any more option arguments */
1814 /* "--help" give help message */
1815 /* "--version" give version message */
1816 /* "--literal" take files literally */
1817 /* "--nofork" don't fork */
1818 /* "--noplugin[s]" skip plugins */
1819 /* "--cmd <cmd>" execute cmd before vimrc */
1820 if (STRICMP(argv[0] + argv_idx, "help") == 0)
1821 usage();
1822 else if (STRICMP(argv[0] + argv_idx, "version") == 0)
1823 {
1824 Columns = 80; /* need to init Columns */
1825 info_message = TRUE; /* use mch_msg(), not mch_errmsg() */
1826 list_version();
1827 msg_putchar('\n');
1828 msg_didout = FALSE;
1829 mch_exit(0);
1830 }
1831 else if (STRNICMP(argv[0] + argv_idx, "literal", 7) == 0)
1832 {
1833#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
1834 parmp->literal = TRUE;
1835#endif
1836 }
1837 else if (STRNICMP(argv[0] + argv_idx, "nofork", 6) == 0)
1838 {
1839#ifdef FEAT_GUI
1840 gui.dofork = FALSE; /* don't fork() when starting GUI */
1841#endif
1842 }
1843 else if (STRNICMP(argv[0] + argv_idx, "noplugin", 8) == 0)
1844 p_lpl = FALSE;
1845 else if (STRNICMP(argv[0] + argv_idx, "cmd", 3) == 0)
1846 {
1847 want_argument = TRUE;
1848 argv_idx += 3;
1849 }
Bram Moolenaaref94eec2009-11-11 13:22:11 +00001850 else if (STRNICMP(argv[0] + argv_idx, "startuptime", 11) == 0)
1851 {
1852 want_argument = TRUE;
1853 argv_idx += 11;
1854 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001855#ifdef FEAT_CLIENTSERVER
1856 else if (STRNICMP(argv[0] + argv_idx, "serverlist", 10) == 0)
1857 ; /* already processed -- no arg */
1858 else if (STRNICMP(argv[0] + argv_idx, "servername", 10) == 0
1859 || STRNICMP(argv[0] + argv_idx, "serversend", 10) == 0)
1860 {
1861 /* already processed -- snatch the following arg */
1862 if (argc > 1)
1863 {
1864 --argc;
1865 ++argv;
1866 }
1867 }
1868#endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001869#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1870# ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001871 else if (STRNICMP(argv[0] + argv_idx, "socketid", 8) == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001872# else
1873 else if (STRNICMP(argv[0] + argv_idx, "windowid", 8) == 0)
1874# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001875 {
1876 /* already processed -- snatch the following arg */
1877 if (argc > 1)
1878 {
1879 --argc;
1880 ++argv;
1881 }
1882 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001883#endif
1884#ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001885 else if (STRNICMP(argv[0] + argv_idx, "echo-wid", 8) == 0)
1886 {
1887 /* already processed, skip */
1888 }
1889#endif
1890 else
1891 {
1892 if (argv[0][argv_idx])
1893 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
1894 had_minmin = TRUE;
1895 }
1896 if (!want_argument)
1897 argv_idx = -1; /* skip to next argument */
1898 break;
1899
1900 case 'A': /* "-A" start in Arabic mode */
1901#ifdef FEAT_ARABIC
1902 set_option_value((char_u *)"arabic", 1L, NULL, 0);
1903#else
1904 mch_errmsg(_(e_noarabic));
1905 mch_exit(2);
1906#endif
1907 break;
1908
1909 case 'b': /* "-b" binary mode */
Bram Moolenaar231334e2005-07-25 20:46:57 +00001910 /* Needs to be effective before expanding file names, because
1911 * for Win32 this makes us edit a shortcut file itself,
1912 * instead of the file it links to. */
1913 set_options_bin(curbuf->b_p_bin, 1, 0);
1914 curbuf->b_p_bin = 1; /* binary file I/O */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001915 break;
1916
1917 case 'C': /* "-C" Compatible */
1918 change_compatible(TRUE);
1919 break;
1920
1921 case 'e': /* "-e" Ex mode */
1922 exmode_active = EXMODE_NORMAL;
1923 break;
1924
1925 case 'E': /* "-E" Improved Ex mode */
1926 exmode_active = EXMODE_VIM;
1927 break;
1928
1929 case 'f': /* "-f" GUI: run in foreground. Amiga: open
1930 window directly, not with newcli */
1931#ifdef FEAT_GUI
1932 gui.dofork = FALSE; /* don't fork() when starting GUI */
1933#endif
1934 break;
1935
1936 case 'g': /* "-g" start GUI */
1937 main_start_gui();
1938 break;
1939
1940 case 'F': /* "-F" start in Farsi mode: rl + fkmap set */
1941#ifdef FEAT_FKMAP
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00001942 p_fkmap = TRUE;
1943 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001944#else
1945 mch_errmsg(_(e_nofarsi));
1946 mch_exit(2);
1947#endif
1948 break;
1949
1950 case 'h': /* "-h" give help message */
1951#ifdef FEAT_GUI_GNOME
1952 /* Tell usage() to exit for "gvim". */
1953 gui.starting = FALSE;
1954#endif
1955 usage();
1956 break;
1957
1958 case 'H': /* "-H" start in Hebrew mode: rl + hkmap set */
1959#ifdef FEAT_RIGHTLEFT
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00001960 p_hkmap = TRUE;
1961 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001962#else
1963 mch_errmsg(_(e_nohebrew));
1964 mch_exit(2);
1965#endif
1966 break;
1967
1968 case 'l': /* "-l" lisp mode, 'lisp' and 'showmatch' on */
1969#ifdef FEAT_LISP
1970 set_option_value((char_u *)"lisp", 1L, NULL, 0);
1971 p_sm = TRUE;
1972#endif
1973 break;
1974
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001975 case 'M': /* "-M" no changes or writing of files */
1976 reset_modifiable();
1977 /* FALLTHROUGH */
1978
1979 case 'm': /* "-m" no writing of files */
1980 p_write = FALSE;
1981 break;
1982
1983 case 'y': /* "-y" easy mode */
1984#ifdef FEAT_GUI
1985 gui.starting = TRUE; /* start GUI a bit later */
1986#endif
1987 parmp->evim_mode = TRUE;
1988 break;
1989
1990 case 'N': /* "-N" Nocompatible */
1991 change_compatible(FALSE);
1992 break;
1993
1994 case 'n': /* "-n" no swap file */
Bram Moolenaar67c53842010-05-22 18:28:27 +02001995#ifdef FEAT_NETBEANS_INTG
1996 /* checking for "-nb", netbeans parameters */
1997 if (argv[0][argv_idx] == 'b')
1998 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02001999 netbeansArg = argv[0];
2000 argv_idx = -1; /* skip to next argument */
2001 }
2002 else
2003#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002004 parmp->no_swap_file = TRUE;
2005 break;
2006
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002007 case 'p': /* "-p[N]" open N tab pages */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002008#ifdef TARGET_API_MAC_OSX
2009 /* For some reason on MacOS X, an argument like:
2010 -psn_0_10223617 is passed in when invoke from Finder
2011 or with the 'open' command */
2012 if (argv[0][argv_idx] == 's')
2013 {
2014 argv_idx = -1; /* bypass full -psn */
2015 main_start_gui();
2016 break;
2017 }
2018#endif
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002019#ifdef FEAT_WINDOWS
2020 /* default is 0: open window for each file */
2021 parmp->window_count = get_number_arg((char_u *)argv[0],
2022 &argv_idx, 0);
2023 parmp->window_layout = WIN_TABS;
2024#endif
2025 break;
2026
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002027 case 'o': /* "-o[N]" open N horizontal split windows */
2028#ifdef FEAT_WINDOWS
2029 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002030 parmp->window_count = get_number_arg((char_u *)argv[0],
2031 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002032 parmp->window_layout = WIN_HOR;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002033#endif
2034 break;
2035
2036 case 'O': /* "-O[N]" open N vertical split windows */
2037#if defined(FEAT_VERTSPLIT) && defined(FEAT_WINDOWS)
2038 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002039 parmp->window_count = get_number_arg((char_u *)argv[0],
2040 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002041 parmp->window_layout = WIN_VER;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002042#endif
2043 break;
2044
2045#ifdef FEAT_QUICKFIX
2046 case 'q': /* "-q" QuickFix mode */
2047 if (parmp->edit_type != EDIT_NONE)
2048 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2049 parmp->edit_type = EDIT_QF;
2050 if (argv[0][argv_idx]) /* "-q{errorfile}" */
2051 {
2052 parmp->use_ef = (char_u *)argv[0] + argv_idx;
2053 argv_idx = -1;
2054 }
2055 else if (argc > 1) /* "-q {errorfile}" */
2056 want_argument = TRUE;
2057 break;
2058#endif
2059
2060 case 'R': /* "-R" readonly mode */
2061 readonlymode = TRUE;
2062 curbuf->b_p_ro = TRUE;
2063 p_uc = 10000; /* don't update very often */
2064 break;
2065
2066 case 'r': /* "-r" recovery mode */
2067 case 'L': /* "-L" recovery mode */
2068 recoverymode = 1;
2069 break;
2070
2071 case 's':
2072 if (exmode_active) /* "-s" silent (batch) mode */
2073 silent_mode = TRUE;
2074 else /* "-s {scriptin}" read from script file */
2075 want_argument = TRUE;
2076 break;
2077
2078 case 't': /* "-t {tag}" or "-t{tag}" jump to tag */
2079 if (parmp->edit_type != EDIT_NONE)
2080 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2081 parmp->edit_type = EDIT_TAG;
2082 if (argv[0][argv_idx]) /* "-t{tag}" */
2083 {
2084 parmp->tagname = (char_u *)argv[0] + argv_idx;
2085 argv_idx = -1;
2086 }
2087 else /* "-t {tag}" */
2088 want_argument = TRUE;
2089 break;
2090
2091#ifdef FEAT_EVAL
2092 case 'D': /* "-D" Debugging */
2093 parmp->use_debug_break_level = 9999;
2094 break;
2095#endif
2096#ifdef FEAT_DIFF
2097 case 'd': /* "-d" 'diff' */
2098# ifdef AMIGA
2099 /* check for "-dev {device}" */
2100 if (argv[0][argv_idx] == 'e' && argv[0][argv_idx + 1] == 'v')
2101 want_argument = TRUE;
2102 else
2103# endif
2104 parmp->diff_mode = TRUE;
2105 break;
2106#endif
2107 case 'V': /* "-V{N}" Verbose level */
2108 /* default is 10: a little bit verbose */
2109 p_verbose = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2110 if (argv[0][argv_idx] != NUL)
2111 {
2112 set_option_value((char_u *)"verbosefile", 0L,
2113 (char_u *)argv[0] + argv_idx, 0);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002114 argv_idx = (int)STRLEN(argv[0]);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002115 }
2116 break;
2117
2118 case 'v': /* "-v" Vi-mode (as if called "vi") */
2119 exmode_active = 0;
2120#ifdef FEAT_GUI
2121 gui.starting = FALSE; /* don't start GUI */
2122#endif
2123 break;
2124
2125 case 'w': /* "-w{number}" set window height */
2126 /* "-w {scriptout}" write to script */
2127 if (vim_isdigit(((char_u *)argv[0])[argv_idx]))
2128 {
2129 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2130 set_option_value((char_u *)"window", n, NULL, 0);
2131 break;
2132 }
2133 want_argument = TRUE;
2134 break;
2135
2136#ifdef FEAT_CRYPT
2137 case 'x': /* "-x" encrypted reading/writing of files */
2138 parmp->ask_for_key = TRUE;
2139 break;
2140#endif
2141
2142 case 'X': /* "-X" don't connect to X server */
2143#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
2144 x_no_connect = TRUE;
2145#endif
2146 break;
2147
2148 case 'Z': /* "-Z" restricted mode */
2149 restricted = TRUE;
2150 break;
2151
2152 case 'c': /* "-c{command}" or "-c {command}" execute
2153 command */
2154 if (argv[0][argv_idx] != NUL)
2155 {
2156 if (parmp->n_commands >= MAX_ARG_CMDS)
2157 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar231334e2005-07-25 20:46:57 +00002158 parmp->commands[parmp->n_commands++] = (char_u *)argv[0]
2159 + argv_idx;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002160 argv_idx = -1;
2161 break;
2162 }
2163 /*FALLTHROUGH*/
2164 case 'S': /* "-S {file}" execute Vim script */
2165 case 'i': /* "-i {viminfo}" use for viminfo */
2166#ifndef FEAT_DIFF
2167 case 'd': /* "-d {device}" device (for Amiga) */
2168#endif
2169 case 'T': /* "-T {terminal}" terminal name */
2170 case 'u': /* "-u {vimrc}" vim inits file */
2171 case 'U': /* "-U {gvimrc}" gvim inits file */
2172 case 'W': /* "-W {scriptout}" overwrite */
2173#ifdef FEAT_GUI_W32
2174 case 'P': /* "-P {parent title}" MDI parent */
2175#endif
2176 want_argument = TRUE;
2177 break;
2178
2179 default:
2180 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
2181 }
2182
2183 /*
2184 * Handle option arguments with argument.
2185 */
2186 if (want_argument)
2187 {
2188 /*
2189 * Check for garbage immediately after the option letter.
2190 */
2191 if (argv[0][argv_idx] != NUL)
2192 mainerr(ME_GARBAGE, (char_u *)argv[0]);
2193
2194 --argc;
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002195 if (argc < 1 && c != 'S') /* -S has an optional argument */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002196 mainerr_arg_missing((char_u *)argv[0]);
2197 ++argv;
2198 argv_idx = -1;
2199
2200 switch (c)
2201 {
2202 case 'c': /* "-c {command}" execute command */
2203 case 'S': /* "-S {file}" execute Vim script */
2204 if (parmp->n_commands >= MAX_ARG_CMDS)
2205 mainerr(ME_EXTRA_CMD, NULL);
2206 if (c == 'S')
2207 {
2208 char *a;
2209
2210 if (argc < 1)
2211 /* "-S" without argument: use default session file
2212 * name. */
2213 a = SESSION_FILE;
2214 else if (argv[0][0] == '-')
2215 {
2216 /* "-S" followed by another option: use default
2217 * session file name. */
2218 a = SESSION_FILE;
2219 ++argc;
2220 --argv;
2221 }
2222 else
2223 a = argv[0];
2224 p = alloc((unsigned)(STRLEN(a) + 4));
2225 if (p == NULL)
2226 mch_exit(2);
2227 sprintf((char *)p, "so %s", a);
2228 parmp->cmds_tofree[parmp->n_commands] = TRUE;
2229 parmp->commands[parmp->n_commands++] = p;
2230 }
2231 else
Bram Moolenaar231334e2005-07-25 20:46:57 +00002232 parmp->commands[parmp->n_commands++] =
2233 (char_u *)argv[0];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002234 break;
2235
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002236 case '-':
2237 if (argv[-1][2] == 'c')
2238 {
2239 /* "--cmd {command}" execute command */
2240 if (parmp->n_pre_commands >= MAX_ARG_CMDS)
2241 mainerr(ME_EXTRA_CMD, NULL);
2242 parmp->pre_commands[parmp->n_pre_commands++] =
Bram Moolenaar231334e2005-07-25 20:46:57 +00002243 (char_u *)argv[0];
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002244 }
2245 /* "--startuptime <file>" already handled */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002246 break;
2247
2248 /* case 'd': -d {device} is handled in mch_check_win() for the
2249 * Amiga */
2250
2251#ifdef FEAT_QUICKFIX
2252 case 'q': /* "-q {errorfile}" QuickFix mode */
2253 parmp->use_ef = (char_u *)argv[0];
2254 break;
2255#endif
2256
2257 case 'i': /* "-i {viminfo}" use for viminfo */
2258 use_viminfo = (char_u *)argv[0];
2259 break;
2260
2261 case 's': /* "-s {scriptin}" read from script file */
2262 if (scriptin[0] != NULL)
2263 {
2264scripterror:
2265 mch_errmsg(_("Attempt to open script file again: \""));
2266 mch_errmsg(argv[-1]);
2267 mch_errmsg(" ");
2268 mch_errmsg(argv[0]);
2269 mch_errmsg("\"\n");
2270 mch_exit(2);
2271 }
2272 if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL)
2273 {
2274 mch_errmsg(_("Cannot open for reading: \""));
2275 mch_errmsg(argv[0]);
2276 mch_errmsg("\"\n");
2277 mch_exit(2);
2278 }
2279 if (save_typebuf() == FAIL)
2280 mch_exit(2); /* out of memory */
2281 break;
2282
2283 case 't': /* "-t {tag}" */
2284 parmp->tagname = (char_u *)argv[0];
2285 break;
2286
2287 case 'T': /* "-T {terminal}" terminal name */
2288 /*
2289 * The -T term argument is always available and when
2290 * HAVE_TERMLIB is supported it overrides the environment
2291 * variable TERM.
2292 */
2293#ifdef FEAT_GUI
2294 if (term_is_gui((char_u *)argv[0]))
2295 gui.starting = TRUE; /* start GUI a bit later */
2296 else
2297#endif
2298 parmp->term = (char_u *)argv[0];
2299 break;
2300
2301 case 'u': /* "-u {vimrc}" vim inits file */
2302 parmp->use_vimrc = (char_u *)argv[0];
2303 break;
2304
2305 case 'U': /* "-U {gvimrc}" gvim inits file */
2306#ifdef FEAT_GUI
2307 use_gvimrc = (char_u *)argv[0];
2308#endif
2309 break;
2310
2311 case 'w': /* "-w {nr}" 'window' value */
2312 /* "-w {scriptout}" append to script file */
2313 if (vim_isdigit(*((char_u *)argv[0])))
2314 {
2315 argv_idx = 0;
2316 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2317 set_option_value((char_u *)"window", n, NULL, 0);
2318 argv_idx = -1;
2319 break;
2320 }
2321 /*FALLTHROUGH*/
2322 case 'W': /* "-W {scriptout}" overwrite script file */
2323 if (scriptout != NULL)
2324 goto scripterror;
2325 if ((scriptout = mch_fopen(argv[0],
2326 c == 'w' ? APPENDBIN : WRITEBIN)) == NULL)
2327 {
2328 mch_errmsg(_("Cannot open for script output: \""));
2329 mch_errmsg(argv[0]);
2330 mch_errmsg("\"\n");
2331 mch_exit(2);
2332 }
2333 break;
2334
2335#ifdef FEAT_GUI_W32
2336 case 'P': /* "-P {parent title}" MDI parent */
2337 gui_mch_set_parent(argv[0]);
2338 break;
2339#endif
2340 }
2341 }
2342 }
2343
2344 /*
2345 * File name argument.
2346 */
2347 else
2348 {
2349 argv_idx = -1; /* skip to next argument */
2350
2351 /* Check for only one type of editing. */
2352 if (parmp->edit_type != EDIT_NONE && parmp->edit_type != EDIT_FILE)
2353 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2354 parmp->edit_type = EDIT_FILE;
2355
2356#ifdef MSWIN
2357 /* Remember if the argument was a full path before changing
2358 * slashes to backslashes. */
2359 if (argv[0][0] != NUL && argv[0][1] == ':' && argv[0][2] == '\\')
2360 parmp->full_path = TRUE;
2361#endif
2362
2363 /* Add the file to the global argument list. */
2364 if (ga_grow(&global_alist.al_ga, 1) == FAIL
2365 || (p = vim_strsave((char_u *)argv[0])) == NULL)
2366 mch_exit(2);
2367#ifdef FEAT_DIFF
2368 if (parmp->diff_mode && mch_isdir(p) && GARGCOUNT > 0
2369 && !mch_isdir(alist_name(&GARGLIST[0])))
2370 {
2371 char_u *r;
2372
2373 r = concat_fnames(p, gettail(alist_name(&GARGLIST[0])), TRUE);
2374 if (r != NULL)
2375 {
2376 vim_free(p);
2377 p = r;
2378 }
2379 }
2380#endif
2381#if defined(__CYGWIN32__) && !defined(WIN32)
2382 /*
2383 * If vim is invoked by non-Cygwin tools, convert away any
2384 * DOS paths, so things like .swp files are created correctly.
2385 * Look for evidence of non-Cygwin paths before we bother.
2386 * This is only for when using the Unix files.
2387 */
Bram Moolenaarad249fb2010-05-07 16:35:04 +02002388 if (strpbrk(p, "\\:") != NULL && !path_with_url(p))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002389 {
2390 char posix_path[PATH_MAX];
2391
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002392# if CYGWIN_VERSION_DLL_MAJOR >= 1007
2393 cygwin_conv_path(CCP_WIN_A_TO_POSIX, p, posix_path, PATH_MAX);
2394# else
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002395 cygwin_conv_to_posix_path(p, posix_path);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002396# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002397 vim_free(p);
2398 p = vim_strsave(posix_path);
2399 if (p == NULL)
2400 mch_exit(2);
2401 }
2402#endif
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002403
2404#ifdef USE_FNAME_CASE
2405 /* Make the case of the file name match the actual file. */
2406 fname_case(p, 0);
2407#endif
2408
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002409 alist_add(&global_alist, p,
2410#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
Bram Moolenaar231334e2005-07-25 20:46:57 +00002411 parmp->literal ? 2 : 0 /* add buffer nr after exp. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002412#else
2413 2 /* add buffer number now and use curbuf */
2414#endif
2415 );
2416
2417#if defined(FEAT_MBYTE) && defined(WIN32)
2418 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002419 /* Remember this argument has been added to the argument list.
2420 * Needed when 'encoding' is changed. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002421 used_file_arg(argv[0], parmp->literal, parmp->full_path,
Bram Moolenaar688e5f72008-07-24 11:51:40 +00002422# ifdef FEAT_DIFF
2423 parmp->diff_mode
2424# else
2425 FALSE
2426# endif
2427 );
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002428 }
2429#endif
2430 }
2431
2432 /*
2433 * If there are no more letters after the current "-", go to next
2434 * argument. argv_idx is set to -1 when the current argument is to be
2435 * skipped.
2436 */
2437 if (argv_idx <= 0 || argv[0][argv_idx] == NUL)
2438 {
2439 --argc;
2440 ++argv;
2441 argv_idx = 1;
2442 }
2443 }
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002444
2445#ifdef FEAT_EVAL
2446 /* If there is a "+123" or "-c" command, set v:swapcommand to the first
2447 * one. */
2448 if (parmp->n_commands > 0)
2449 {
2450 p = alloc((unsigned)STRLEN(parmp->commands[0]) + 3);
2451 if (p != NULL)
2452 {
2453 sprintf((char *)p, ":%s\r", parmp->commands[0]);
2454 set_vim_var_string(VV_SWAPCOMMAND, p, -1);
2455 vim_free(p);
2456 }
2457 }
2458#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002459}
2460
2461/*
2462 * Print a warning if stdout is not a terminal.
2463 * When starting in Ex mode and commands come from a file, set Silent mode.
2464 */
2465 static void
2466check_tty(parmp)
2467 mparm_T *parmp;
2468{
2469 int input_isatty; /* is active input a terminal? */
2470
2471 input_isatty = mch_input_isatty();
2472 if (exmode_active)
2473 {
2474 if (!input_isatty)
2475 silent_mode = TRUE;
2476 }
2477 else if (parmp->want_full_screen && (!parmp->stdout_isatty || !input_isatty)
2478#ifdef FEAT_GUI
2479 /* don't want the delay when started from the desktop */
2480 && !gui.starting
2481#endif
2482 )
2483 {
2484#ifdef NBDEBUG
2485 /*
2486 * This shouldn't be necessary. But if I run netbeans with the log
2487 * output coming to the console and XOpenDisplay fails, I get vim
2488 * trying to start with input/output to my console tty. This fills my
2489 * input buffer so fast I can't even kill the process in under 2
Bram Moolenaar49325942007-05-10 19:19:59 +00002490 * minutes (and it beeps continuously the whole time :-)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002491 */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002492 if (netbeans_active() && (!parmp->stdout_isatty || !input_isatty))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002493 {
2494 mch_errmsg(_("Vim: Error: Failure to start gvim from NetBeans\n"));
2495 exit(1);
2496 }
2497#endif
2498 if (!parmp->stdout_isatty)
2499 mch_errmsg(_("Vim: Warning: Output is not to a terminal\n"));
2500 if (!input_isatty)
2501 mch_errmsg(_("Vim: Warning: Input is not from a terminal\n"));
2502 out_flush();
2503 if (scriptin[0] == NULL)
2504 ui_delay(2000L, TRUE);
2505 TIME_MSG("Warning delay");
2506 }
2507}
2508
2509/*
2510 * Read text from stdin.
2511 */
2512 static void
2513read_stdin()
2514{
2515 int i;
2516
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002517#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002518 /* When getting the ATTENTION prompt here, use a dialog */
2519 swap_exists_action = SEA_DIALOG;
2520#endif
2521 no_wait_return = TRUE;
2522 i = msg_didany;
2523 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002524 (void)open_buffer(TRUE, NULL, 0); /* create memfile and read file */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002525 no_wait_return = FALSE;
2526 msg_didany = i;
2527 TIME_MSG("reading stdin");
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002528#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002529 check_swap_exists_action();
2530#endif
2531#if !(defined(AMIGA) || defined(MACOS))
2532 /*
2533 * Close stdin and dup it from stderr. Required for GPM to work
2534 * properly, and for running external commands.
2535 * Is there any other system that cannot do this?
2536 */
2537 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00002538 ignored = dup(2);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002539#endif
2540}
2541
2542/*
2543 * Create the requested number of windows and edit buffers in them.
2544 * Also does recovery if "recoverymode" set.
2545 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002546 static void
2547create_windows(parmp)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002548 mparm_T *parmp UNUSED;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002549{
2550#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002551 int dorewind;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002552 int done = 0;
2553
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002554 /*
2555 * Create the number of windows that was requested.
2556 */
2557 if (parmp->window_count == -1) /* was not set */
2558 parmp->window_count = 1;
2559 if (parmp->window_count == 0)
2560 parmp->window_count = GARGCOUNT;
2561 if (parmp->window_count > 1)
2562 {
2563 /* Don't change the windows if there was a command in .vimrc that
2564 * already split some windows */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002565 if (parmp->window_layout == 0)
2566 parmp->window_layout = WIN_HOR;
2567 if (parmp->window_layout == WIN_TABS)
2568 {
2569 parmp->window_count = make_tabpages(parmp->window_count);
2570 TIME_MSG("making tab pages");
2571 }
2572 else if (firstwin->w_next == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002573 {
2574 parmp->window_count = make_windows(parmp->window_count,
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002575 parmp->window_layout == WIN_VER);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002576 TIME_MSG("making windows");
2577 }
2578 else
2579 parmp->window_count = win_count();
2580 }
2581 else
2582 parmp->window_count = 1;
2583#endif
2584
2585 if (recoverymode) /* do recover */
2586 {
2587 msg_scroll = TRUE; /* scroll message up */
2588 ml_recover();
2589 if (curbuf->b_ml.ml_mfp == NULL) /* failed */
2590 getout(1);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002591 do_modelines(0); /* do modelines */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002592 }
2593 else
2594 {
2595 /*
2596 * Open a buffer for windows that don't have one yet.
2597 * Commands in the .vimrc might have loaded a file or split the window.
2598 * Watch out for autocommands that delete a window.
2599 */
2600#ifdef FEAT_AUTOCMD
2601 /*
2602 * Don't execute Win/Buf Enter/Leave autocommands here
2603 */
2604 ++autocmd_no_enter;
2605 ++autocmd_no_leave;
2606#endif
2607#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002608 dorewind = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002609 while (done++ < 1000)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002610 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002611 if (dorewind)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002612 {
2613 if (parmp->window_layout == WIN_TABS)
2614 goto_tabpage(1);
2615 else
2616 curwin = firstwin;
2617 }
2618 else if (parmp->window_layout == WIN_TABS)
2619 {
2620 if (curtab->tp_next == NULL)
2621 break;
2622 goto_tabpage(0);
2623 }
2624 else
2625 {
2626 if (curwin->w_next == NULL)
2627 break;
2628 curwin = curwin->w_next;
2629 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002630 dorewind = FALSE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002631#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002632 curbuf = curwin->w_buffer;
2633 if (curbuf->b_ml.ml_mfp == NULL)
2634 {
2635#ifdef FEAT_FOLDING
2636 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2637 if (p_fdls >= 0)
2638 curwin->w_p_fdl = p_fdls;
2639#endif
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002640#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002641 /* When getting the ATTENTION prompt here, use a dialog */
2642 swap_exists_action = SEA_DIALOG;
2643#endif
2644 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002645
2646 /* create memfile, read file */
2647 (void)open_buffer(FALSE, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002648
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002649#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar84212822006-11-07 21:59:47 +00002650 if (swap_exists_action == SEA_QUIT)
2651 {
2652 if (got_int || only_one_window())
2653 {
2654 /* abort selected or quit and only one window */
2655 did_emsg = FALSE; /* avoid hit-enter prompt */
2656 getout(1);
2657 }
2658 /* We can't close the window, it would disturb what
2659 * happens next. Clear the file name and set the arg
2660 * index to -1 to delete it later. */
2661 setfname(curbuf, NULL, NULL, FALSE);
2662 curwin->w_arg_idx = -1;
2663 swap_exists_action = SEA_NONE;
2664 }
2665 else
2666 handle_swap_exists(NULL);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002667#endif
2668#ifdef FEAT_AUTOCMD
Bram Moolenaar89d40322006-08-29 15:30:07 +00002669 dorewind = TRUE; /* start again */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002670#endif
2671 }
2672#ifdef FEAT_WINDOWS
2673 ui_breakcheck();
2674 if (got_int)
2675 {
2676 (void)vgetc(); /* only break the file loading, not the rest */
2677 break;
2678 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002679 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002680#endif
2681#ifdef FEAT_WINDOWS
2682 if (parmp->window_layout == WIN_TABS)
2683 goto_tabpage(1);
2684 else
2685 curwin = firstwin;
2686 curbuf = curwin->w_buffer;
2687#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002688#ifdef FEAT_AUTOCMD
2689 --autocmd_no_enter;
2690 --autocmd_no_leave;
2691#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002692 }
2693}
2694
2695#ifdef FEAT_WINDOWS
2696 /*
2697 * If opened more than one window, start editing files in the other
2698 * windows. make_windows() has already opened the windows.
2699 */
2700 static void
2701edit_buffers(parmp)
2702 mparm_T *parmp;
2703{
2704 int arg_idx; /* index in argument list */
2705 int i;
Bram Moolenaar84212822006-11-07 21:59:47 +00002706 int advance = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002707
2708# ifdef FEAT_AUTOCMD
2709 /*
2710 * Don't execute Win/Buf Enter/Leave autocommands here
2711 */
2712 ++autocmd_no_enter;
2713 ++autocmd_no_leave;
2714# endif
Bram Moolenaar84212822006-11-07 21:59:47 +00002715
2716 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2717 if (curwin->w_arg_idx == -1)
2718 {
2719 win_close(curwin, TRUE);
2720 advance = FALSE;
2721 }
2722
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002723 arg_idx = 1;
2724 for (i = 1; i < parmp->window_count; ++i)
2725 {
Bram Moolenaar84212822006-11-07 21:59:47 +00002726 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2727 if (curwin->w_arg_idx == -1)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002728 {
Bram Moolenaar84212822006-11-07 21:59:47 +00002729 ++arg_idx;
2730 win_close(curwin, TRUE);
2731 advance = FALSE;
2732 continue;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002733 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002734
Bram Moolenaar84212822006-11-07 21:59:47 +00002735 if (advance)
2736 {
2737 if (parmp->window_layout == WIN_TABS)
2738 {
2739 if (curtab->tp_next == NULL) /* just checking */
2740 break;
2741 goto_tabpage(0);
2742 }
2743 else
2744 {
2745 if (curwin->w_next == NULL) /* just checking */
2746 break;
2747 win_enter(curwin->w_next, FALSE);
2748 }
2749 }
2750 advance = TRUE;
2751
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002752 /* Only open the file if there is no file in this window yet (that can
Bram Moolenaar84212822006-11-07 21:59:47 +00002753 * happen when .vimrc contains ":sall"). */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002754 if (curbuf == firstwin->w_buffer || curbuf->b_ffname == NULL)
2755 {
2756 curwin->w_arg_idx = arg_idx;
Bram Moolenaar84212822006-11-07 21:59:47 +00002757 /* Edit file from arg list, if there is one. When "Quit" selected
2758 * at the ATTENTION prompt close the window. */
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002759# ifdef HAS_SWAP_EXISTS_ACTION
2760 swap_exists_did_quit = FALSE;
2761# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002762 (void)do_ecmd(0, arg_idx < GARGCOUNT
2763 ? alist_name(&GARGLIST[arg_idx]) : NULL,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002764 NULL, NULL, ECMD_LASTL, ECMD_HIDE, curwin);
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002765# ifdef HAS_SWAP_EXISTS_ACTION
2766 if (swap_exists_did_quit)
Bram Moolenaar84212822006-11-07 21:59:47 +00002767 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002768 /* abort or quit selected */
Bram Moolenaar84212822006-11-07 21:59:47 +00002769 if (got_int || only_one_window())
2770 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002771 /* abort selected and only one window */
Bram Moolenaar84212822006-11-07 21:59:47 +00002772 did_emsg = FALSE; /* avoid hit-enter prompt */
2773 getout(1);
2774 }
2775 win_close(curwin, TRUE);
2776 advance = FALSE;
2777 }
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002778# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002779 if (arg_idx == GARGCOUNT - 1)
2780 arg_had_last = TRUE;
2781 ++arg_idx;
2782 }
2783 ui_breakcheck();
2784 if (got_int)
2785 {
2786 (void)vgetc(); /* only break the file loading, not the rest */
2787 break;
2788 }
2789 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002790
2791 if (parmp->window_layout == WIN_TABS)
2792 goto_tabpage(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002793# ifdef FEAT_AUTOCMD
2794 --autocmd_no_enter;
2795# endif
2796 win_enter(firstwin, FALSE); /* back to first window */
2797# ifdef FEAT_AUTOCMD
2798 --autocmd_no_leave;
2799# endif
2800 TIME_MSG("editing files in windows");
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002801 if (parmp->window_count > 1 && parmp->window_layout != WIN_TABS)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002802 win_equal(curwin, FALSE, 'b'); /* adjust heights */
2803}
2804#endif /* FEAT_WINDOWS */
2805
2806/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002807 * Execute the commands from --cmd arguments "cmds[cnt]".
2808 */
2809 static void
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002810exe_pre_commands(parmp)
2811 mparm_T *parmp;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002812{
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002813 char_u **cmds = parmp->pre_commands;
2814 int cnt = parmp->n_pre_commands;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002815 int i;
2816
2817 if (cnt > 0)
2818 {
2819 curwin->w_cursor.lnum = 0; /* just in case.. */
2820 sourcing_name = (char_u *)_("pre-vimrc command line");
2821# ifdef FEAT_EVAL
2822 current_SID = SID_CMDARG;
2823# endif
2824 for (i = 0; i < cnt; ++i)
2825 do_cmdline_cmd(cmds[i]);
2826 sourcing_name = NULL;
2827# ifdef FEAT_EVAL
2828 current_SID = 0;
2829# endif
2830 TIME_MSG("--cmd commands");
2831 }
2832}
2833
2834/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002835 * Execute "+", "-c" and "-S" arguments.
2836 */
2837 static void
2838exe_commands(parmp)
2839 mparm_T *parmp;
2840{
2841 int i;
2842
2843 /*
2844 * We start commands on line 0, make "vim +/pat file" match a
2845 * pattern on line 1. But don't move the cursor when an autocommand
2846 * with g`" was used.
2847 */
2848 msg_scroll = TRUE;
2849 if (parmp->tagname == NULL && curwin->w_cursor.lnum <= 1)
2850 curwin->w_cursor.lnum = 0;
2851 sourcing_name = (char_u *)"command line";
2852#ifdef FEAT_EVAL
2853 current_SID = SID_CARG;
2854#endif
2855 for (i = 0; i < parmp->n_commands; ++i)
2856 {
2857 do_cmdline_cmd(parmp->commands[i]);
2858 if (parmp->cmds_tofree[i])
2859 vim_free(parmp->commands[i]);
2860 }
2861 sourcing_name = NULL;
2862#ifdef FEAT_EVAL
2863 current_SID = 0;
2864#endif
2865 if (curwin->w_cursor.lnum == 0)
2866 curwin->w_cursor.lnum = 1;
2867
2868 if (!exmode_active)
2869 msg_scroll = FALSE;
2870
2871#ifdef FEAT_QUICKFIX
2872 /* When started with "-q errorfile" jump to first error again. */
2873 if (parmp->edit_type == EDIT_QF)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002874 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002875#endif
2876 TIME_MSG("executing command arguments");
2877}
2878
2879/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002880 * Source startup scripts.
2881 */
2882 static void
2883source_startup_scripts(parmp)
2884 mparm_T *parmp;
2885{
2886 int i;
2887
2888 /*
2889 * For "evim" source evim.vim first of all, so that the user can overrule
2890 * any things he doesn't like.
2891 */
2892 if (parmp->evim_mode)
2893 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002894 (void)do_source((char_u *)EVIM_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002895 TIME_MSG("source evim file");
2896 }
2897
2898 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002899 * If -u argument given, use only the initializations from that file and
Bram Moolenaar58d98232005-07-23 22:25:46 +00002900 * nothing else.
2901 */
2902 if (parmp->use_vimrc != NULL)
2903 {
Bram Moolenaar231334e2005-07-25 20:46:57 +00002904 if (STRCMP(parmp->use_vimrc, "NONE") == 0
2905 || STRCMP(parmp->use_vimrc, "NORC") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002906 {
2907#ifdef FEAT_GUI
2908 if (use_gvimrc == NULL) /* don't load gvimrc either */
2909 use_gvimrc = parmp->use_vimrc;
2910#endif
2911 if (parmp->use_vimrc[2] == 'N')
2912 p_lpl = FALSE; /* don't load plugins either */
2913 }
2914 else
2915 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002916 if (do_source(parmp->use_vimrc, FALSE, DOSO_NONE) != OK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002917 EMSG2(_("E282: Cannot read from \"%s\""), parmp->use_vimrc);
2918 }
2919 }
2920 else if (!silent_mode)
2921 {
2922#ifdef AMIGA
2923 struct Process *proc = (struct Process *)FindTask(0L);
2924 APTR save_winptr = proc->pr_WindowPtr;
2925
2926 /* Avoid a requester here for a volume that doesn't exist. */
2927 proc->pr_WindowPtr = (APTR)-1L;
2928#endif
2929
2930 /*
2931 * Get system wide defaults, if the file name is defined.
2932 */
2933#ifdef SYS_VIMRC_FILE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002934 (void)do_source((char_u *)SYS_VIMRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002935#endif
Bram Moolenaar1056d982006-03-09 22:37:52 +00002936#ifdef MACOS_X
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002937 (void)do_source((char_u *)"$VIMRUNTIME/macmap.vim", FALSE, DOSO_NONE);
Bram Moolenaar1056d982006-03-09 22:37:52 +00002938#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00002939
2940 /*
2941 * Try to read initialization commands from the following places:
2942 * - environment variable VIMINIT
2943 * - user vimrc file (s:.vimrc for Amiga, ~/.vimrc otherwise)
2944 * - second user vimrc file ($VIM/.vimrc for Dos)
2945 * - environment variable EXINIT
2946 * - user exrc file (s:.exrc for Amiga, ~/.exrc otherwise)
2947 * - second user exrc file ($VIM/.exrc for Dos)
2948 * The first that exists is used, the rest is ignored.
2949 */
2950 if (process_env((char_u *)"VIMINIT", TRUE) != OK)
2951 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002952 if (do_source((char_u *)USR_VIMRC_FILE, TRUE, DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00002953#ifdef USR_VIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002954 && do_source((char_u *)USR_VIMRC_FILE2, TRUE,
2955 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00002956#endif
2957#ifdef USR_VIMRC_FILE3
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002958 && do_source((char_u *)USR_VIMRC_FILE3, TRUE,
2959 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00002960#endif
2961 && process_env((char_u *)"EXINIT", FALSE) == FAIL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002962 && do_source((char_u *)USR_EXRC_FILE, FALSE, DOSO_NONE) == FAIL)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002963 {
2964#ifdef USR_EXRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002965 (void)do_source((char_u *)USR_EXRC_FILE2, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002966#endif
2967 }
2968 }
2969
2970 /*
2971 * Read initialization commands from ".vimrc" or ".exrc" in current
2972 * directory. This is only done if the 'exrc' option is set.
2973 * Because of security reasons we disallow shell and write commands
2974 * now, except for unix if the file is owned by the user or 'secure'
2975 * option has been reset in environment of global ".exrc" or ".vimrc".
2976 * Only do this if VIMRC_FILE is not the same as USR_VIMRC_FILE or
2977 * SYS_VIMRC_FILE.
2978 */
2979 if (p_exrc)
2980 {
2981#if defined(UNIX) || defined(VMS)
2982 /* If ".vimrc" file is not owned by user, set 'secure' mode. */
2983 if (!file_owned(VIMRC_FILE))
2984#endif
2985 secure = p_secure;
2986
2987 i = FAIL;
2988 if (fullpathcmp((char_u *)USR_VIMRC_FILE,
2989 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
2990#ifdef USR_VIMRC_FILE2
2991 && fullpathcmp((char_u *)USR_VIMRC_FILE2,
2992 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
2993#endif
2994#ifdef USR_VIMRC_FILE3
2995 && fullpathcmp((char_u *)USR_VIMRC_FILE3,
2996 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
2997#endif
2998#ifdef SYS_VIMRC_FILE
2999 && fullpathcmp((char_u *)SYS_VIMRC_FILE,
3000 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3001#endif
3002 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003003 i = do_source((char_u *)VIMRC_FILE, TRUE, DOSO_VIMRC);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003004
3005 if (i == FAIL)
3006 {
3007#if defined(UNIX) || defined(VMS)
3008 /* if ".exrc" is not owned by user set 'secure' mode */
3009 if (!file_owned(EXRC_FILE))
3010 secure = p_secure;
3011 else
3012 secure = 0;
3013#endif
3014 if ( fullpathcmp((char_u *)USR_EXRC_FILE,
3015 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3016#ifdef USR_EXRC_FILE2
3017 && fullpathcmp((char_u *)USR_EXRC_FILE2,
3018 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3019#endif
3020 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003021 (void)do_source((char_u *)EXRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003022 }
3023 }
3024 if (secure == 2)
3025 need_wait_return = TRUE;
3026 secure = 0;
3027#ifdef AMIGA
3028 proc->pr_WindowPtr = save_winptr;
3029#endif
3030 }
3031 TIME_MSG("sourcing vimrc file(s)");
3032}
3033
3034/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003035 * Setup to start using the GUI. Exit with an error when not available.
3036 */
3037 static void
3038main_start_gui()
3039{
3040#ifdef FEAT_GUI
3041 gui.starting = TRUE; /* start GUI a bit later */
3042#else
3043 mch_errmsg(_(e_nogvim));
3044 mch_errmsg("\n");
3045 mch_exit(2);
3046#endif
3047}
3048
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003049#endif /* NO_VIM_MAIN */
3050
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003051/*
Bram Moolenaar49325942007-05-10 19:19:59 +00003052 * Get an environment variable, and execute it as Ex commands.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003053 * Returns FAIL if the environment variable was not executed, OK otherwise.
3054 */
3055 int
3056process_env(env, is_viminit)
3057 char_u *env;
3058 int is_viminit; /* when TRUE, called for VIMINIT */
3059{
3060 char_u *initstr;
3061 char_u *save_sourcing_name;
3062 linenr_T save_sourcing_lnum;
3063#ifdef FEAT_EVAL
3064 scid_T save_sid;
3065#endif
3066
3067 if ((initstr = mch_getenv(env)) != NULL && *initstr != NUL)
3068 {
3069 if (is_viminit)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003070 vimrc_found(NULL, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003071 save_sourcing_name = sourcing_name;
3072 save_sourcing_lnum = sourcing_lnum;
3073 sourcing_name = env;
3074 sourcing_lnum = 0;
3075#ifdef FEAT_EVAL
3076 save_sid = current_SID;
3077 current_SID = SID_ENV;
3078#endif
3079 do_cmdline_cmd(initstr);
3080 sourcing_name = save_sourcing_name;
3081 sourcing_lnum = save_sourcing_lnum;
3082#ifdef FEAT_EVAL
3083 current_SID = save_sid;;
3084#endif
3085 return OK;
3086 }
3087 return FAIL;
3088}
3089
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003090#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003091/*
3092 * Return TRUE if we are certain the user owns the file "fname".
3093 * Used for ".vimrc" and ".exrc".
3094 * Use both stat() and lstat() for extra security.
3095 */
3096 static int
3097file_owned(fname)
3098 char *fname;
3099{
3100 struct stat s;
3101# ifdef UNIX
3102 uid_t uid = getuid();
3103# else /* VMS */
3104 uid_t uid = ((getgid() << 16) | getuid());
3105# endif
3106
3107 return !(mch_stat(fname, &s) != 0 || s.st_uid != uid
3108# ifdef HAVE_LSTAT
3109 || mch_lstat(fname, &s) != 0 || s.st_uid != uid
3110# endif
3111 );
3112}
3113#endif
3114
3115/*
3116 * Give an error message main_errors["n"] and exit.
3117 */
3118 static void
3119mainerr(n, str)
3120 int n; /* one of the ME_ defines */
3121 char_u *str; /* extra argument or NULL */
3122{
3123#if defined(UNIX) || defined(__EMX__) || defined(VMS)
3124 reset_signals(); /* kill us with CTRL-C here, if you like */
3125#endif
3126
3127 mch_errmsg(longVersion);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003128 mch_errmsg("\n");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003129 mch_errmsg(_(main_errors[n]));
3130 if (str != NULL)
3131 {
3132 mch_errmsg(": \"");
3133 mch_errmsg((char *)str);
3134 mch_errmsg("\"");
3135 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003136 mch_errmsg(_("\nMore info with: \"vim -h\"\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003137
3138 mch_exit(1);
3139}
3140
3141 void
3142mainerr_arg_missing(str)
3143 char_u *str;
3144{
3145 mainerr(ME_ARG_MISSING, str);
3146}
3147
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003148#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003149/*
3150 * print a message with three spaces prepended and '\n' appended.
3151 */
3152 static void
3153main_msg(s)
3154 char *s;
3155{
3156 mch_msg(" ");
3157 mch_msg(s);
3158 mch_msg("\n");
3159}
3160
3161/*
3162 * Print messages for "vim -h" or "vim --help" and exit.
3163 */
3164 static void
3165usage()
3166{
3167 int i;
3168 static char *(use[]) =
3169 {
3170 N_("[file ..] edit specified file(s)"),
3171 N_("- read text from stdin"),
3172 N_("-t tag edit file where tag is defined"),
3173#ifdef FEAT_QUICKFIX
3174 N_("-q [errorfile] edit file with first error")
3175#endif
3176 };
3177
3178#if defined(UNIX) || defined(__EMX__) || defined(VMS)
3179 reset_signals(); /* kill us with CTRL-C here, if you like */
3180#endif
3181
3182 mch_msg(longVersion);
3183 mch_msg(_("\n\nusage:"));
3184 for (i = 0; ; ++i)
3185 {
3186 mch_msg(_(" vim [arguments] "));
3187 mch_msg(_(use[i]));
3188 if (i == (sizeof(use) / sizeof(char_u *)) - 1)
3189 break;
3190 mch_msg(_("\n or:"));
3191 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003192#ifdef VMS
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003193 mch_msg(_("\nWhere case is ignored prepend / to make flag upper case"));
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003194#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003195
3196 mch_msg(_("\n\nArguments:\n"));
3197 main_msg(_("--\t\t\tOnly file names after this"));
3198#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
3199 main_msg(_("--literal\t\tDon't expand wildcards"));
3200#endif
3201#ifdef FEAT_OLE
3202 main_msg(_("-register\t\tRegister this gvim for OLE"));
3203 main_msg(_("-unregister\t\tUnregister gvim for OLE"));
3204#endif
3205#ifdef FEAT_GUI
3206 main_msg(_("-g\t\t\tRun using GUI (like \"gvim\")"));
3207 main_msg(_("-f or --nofork\tForeground: Don't fork when starting GUI"));
3208#endif
3209 main_msg(_("-v\t\t\tVi mode (like \"vi\")"));
3210 main_msg(_("-e\t\t\tEx mode (like \"ex\")"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003211 main_msg(_("-E\t\t\tImproved Ex mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003212 main_msg(_("-s\t\t\tSilent (batch) mode (only for \"ex\")"));
3213#ifdef FEAT_DIFF
3214 main_msg(_("-d\t\t\tDiff mode (like \"vimdiff\")"));
3215#endif
3216 main_msg(_("-y\t\t\tEasy mode (like \"evim\", modeless)"));
3217 main_msg(_("-R\t\t\tReadonly mode (like \"view\")"));
3218 main_msg(_("-Z\t\t\tRestricted mode (like \"rvim\")"));
3219 main_msg(_("-m\t\t\tModifications (writing files) not allowed"));
3220 main_msg(_("-M\t\t\tModifications in text not allowed"));
3221 main_msg(_("-b\t\t\tBinary mode"));
3222#ifdef FEAT_LISP
3223 main_msg(_("-l\t\t\tLisp mode"));
3224#endif
3225 main_msg(_("-C\t\t\tCompatible with Vi: 'compatible'"));
3226 main_msg(_("-N\t\t\tNot fully Vi compatible: 'nocompatible'"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003227 main_msg(_("-V[N][fname]\t\tBe verbose [level N] [log messages to fname]"));
3228#ifdef FEAT_EVAL
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003229 main_msg(_("-D\t\t\tDebugging mode"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003230#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003231 main_msg(_("-n\t\t\tNo swap file, use memory only"));
3232 main_msg(_("-r\t\t\tList swap files and exit"));
3233 main_msg(_("-r (with file name)\tRecover crashed session"));
3234 main_msg(_("-L\t\t\tSame as -r"));
3235#ifdef AMIGA
3236 main_msg(_("-f\t\t\tDon't use newcli to open window"));
3237 main_msg(_("-dev <device>\t\tUse <device> for I/O"));
3238#endif
3239#ifdef FEAT_ARABIC
3240 main_msg(_("-A\t\t\tstart in Arabic mode"));
3241#endif
3242#ifdef FEAT_RIGHTLEFT
3243 main_msg(_("-H\t\t\tStart in Hebrew mode"));
3244#endif
3245#ifdef FEAT_FKMAP
3246 main_msg(_("-F\t\t\tStart in Farsi mode"));
3247#endif
3248 main_msg(_("-T <terminal>\tSet terminal type to <terminal>"));
3249 main_msg(_("-u <vimrc>\t\tUse <vimrc> instead of any .vimrc"));
3250#ifdef FEAT_GUI
3251 main_msg(_("-U <gvimrc>\t\tUse <gvimrc> instead of any .gvimrc"));
3252#endif
3253 main_msg(_("--noplugin\t\tDon't load plugin scripts"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003254#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003255 main_msg(_("-p[N]\t\tOpen N tab pages (default: one for each file)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003256 main_msg(_("-o[N]\t\tOpen N windows (default: one for each file)"));
3257 main_msg(_("-O[N]\t\tLike -o but split vertically"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003258#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003259 main_msg(_("+\t\t\tStart at end of file"));
3260 main_msg(_("+<lnum>\t\tStart at line <lnum>"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003261 main_msg(_("--cmd <command>\tExecute <command> before loading any vimrc file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003262 main_msg(_("-c <command>\t\tExecute <command> after loading the first file"));
3263 main_msg(_("-S <session>\t\tSource file <session> after loading the first file"));
3264 main_msg(_("-s <scriptin>\tRead Normal mode commands from file <scriptin>"));
3265 main_msg(_("-w <scriptout>\tAppend all typed commands to file <scriptout>"));
3266 main_msg(_("-W <scriptout>\tWrite all typed commands to file <scriptout>"));
3267#ifdef FEAT_CRYPT
3268 main_msg(_("-x\t\t\tEdit encrypted files"));
3269#endif
3270#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
3271# if defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)
3272 main_msg(_("-display <display>\tConnect vim to this particular X-server"));
3273# endif
3274 main_msg(_("-X\t\t\tDo not connect to X server"));
3275#endif
3276#ifdef FEAT_CLIENTSERVER
3277 main_msg(_("--remote <files>\tEdit <files> in a Vim server if possible"));
3278 main_msg(_("--remote-silent <files> Same, don't complain if there is no server"));
3279 main_msg(_("--remote-wait <files> As --remote but wait for files to have been edited"));
3280 main_msg(_("--remote-wait-silent <files> Same, don't complain if there is no server"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003281# ifdef FEAT_WINDOWS
Bram Moolenaar82ad3242008-01-11 19:26:36 +00003282 main_msg(_("--remote-tab[-wait][-silent] <files> As --remote but use tab page per file"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003283# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003284 main_msg(_("--remote-send <keys>\tSend <keys> to a Vim server and exit"));
3285 main_msg(_("--remote-expr <expr>\tEvaluate <expr> in a Vim server and print result"));
3286 main_msg(_("--serverlist\t\tList available Vim server names and exit"));
3287 main_msg(_("--servername <name>\tSend to/become the Vim server <name>"));
3288#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003289#ifdef STARTUPTIME
Bram Moolenaar34ef52d2009-11-17 11:31:25 +00003290 main_msg(_("--startuptime <file>\tWrite startup timing messages to <file>"));
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003291#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003292#ifdef FEAT_VIMINFO
3293 main_msg(_("-i <viminfo>\t\tUse <viminfo> instead of .viminfo"));
3294#endif
3295 main_msg(_("-h or --help\tPrint Help (this message) and exit"));
3296 main_msg(_("--version\t\tPrint version information and exit"));
3297
3298#ifdef FEAT_GUI_X11
3299# ifdef FEAT_GUI_MOTIF
3300 mch_msg(_("\nArguments recognised by gvim (Motif version):\n"));
3301# else
3302# ifdef FEAT_GUI_ATHENA
3303# ifdef FEAT_GUI_NEXTAW
3304 mch_msg(_("\nArguments recognised by gvim (neXtaw version):\n"));
3305# else
3306 mch_msg(_("\nArguments recognised by gvim (Athena version):\n"));
3307# endif
3308# endif
3309# endif
3310 main_msg(_("-display <display>\tRun vim on <display>"));
3311 main_msg(_("-iconic\t\tStart vim iconified"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003312 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
3313 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
3314 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3315 main_msg(_("-boldfont <font>\tUse <font> for bold text"));
3316 main_msg(_("-italicfont <font>\tUse <font> for italic text"));
3317 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3318 main_msg(_("-borderwidth <width>\tUse a border width of <width> (also: -bw)"));
3319 main_msg(_("-scrollbarwidth <width> Use a scrollbar width of <width> (also: -sw)"));
3320# ifdef FEAT_GUI_ATHENA
3321 main_msg(_("-menuheight <height>\tUse a menu bar height of <height> (also: -mh)"));
3322# endif
3323 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3324 main_msg(_("+reverse\t\tDon't use reverse video (also: +rv)"));
3325 main_msg(_("-xrm <resource>\tSet the specified resource"));
3326#endif /* FEAT_GUI_X11 */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003327#ifdef FEAT_GUI_GTK
3328 mch_msg(_("\nArguments recognised by gvim (GTK+ version):\n"));
3329 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3330 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3331 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3332 main_msg(_("-display <display>\tRun vim on <display> (also: --display)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003333 main_msg(_("--role <role>\tSet a unique role to identify the main window"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003334 main_msg(_("--socketid <xid>\tOpen Vim inside another GTK widget"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003335 main_msg(_("--echo-wid\t\tMake gvim echo the Window ID on stdout"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003336#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003337#ifdef FEAT_GUI_W32
3338 main_msg(_("-P <parent title>\tOpen Vim inside parent application"));
Bram Moolenaar78e17622007-08-30 10:26:19 +00003339 main_msg(_("--windowid <HWND>\tOpen Vim inside another win32 widget"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003340#endif
3341
3342#ifdef FEAT_GUI_GNOME
3343 /* Gnome gives extra messages for --help if we continue, but not for -h. */
3344 if (gui.starting)
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003345 {
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003346 mch_msg("\n");
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003347 gui.dofork = FALSE;
3348 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003349 else
3350#endif
3351 mch_exit(0);
3352}
3353
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00003354#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003355/*
3356 * Check the result of the ATTENTION dialog:
3357 * When "Quit" selected, exit Vim.
3358 * When "Recover" selected, recover the file.
3359 */
3360 static void
3361check_swap_exists_action()
3362{
3363 if (swap_exists_action == SEA_QUIT)
3364 getout(1);
3365 handle_swap_exists(NULL);
3366}
3367#endif
3368
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003369#endif
3370
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003371#if defined(STARTUPTIME) || defined(PROTO)
3372static void time_diff __ARGS((struct timeval *then, struct timeval *now));
3373
3374static struct timeval prev_timeval;
3375
Bram Moolenaar3f269672009-11-03 11:11:11 +00003376# ifdef WIN3264
3377/*
3378 * Windows doesn't have gettimeofday(), although it does have struct timeval.
3379 */
3380 static int
3381gettimeofday(struct timeval *tv, char *dummy)
3382{
3383 long t = clock();
3384 tv->tv_sec = t / CLOCKS_PER_SEC;
3385 tv->tv_usec = (t - tv->tv_sec * CLOCKS_PER_SEC) * 1000000 / CLOCKS_PER_SEC;
3386 return 0;
3387}
3388# endif
3389
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003390/*
3391 * Save the previous time before doing something that could nest.
3392 * set "*tv_rel" to the time elapsed so far.
3393 */
3394 void
3395time_push(tv_rel, tv_start)
3396 void *tv_rel, *tv_start;
3397{
3398 *((struct timeval *)tv_rel) = prev_timeval;
3399 gettimeofday(&prev_timeval, NULL);
3400 ((struct timeval *)tv_rel)->tv_usec = prev_timeval.tv_usec
3401 - ((struct timeval *)tv_rel)->tv_usec;
3402 ((struct timeval *)tv_rel)->tv_sec = prev_timeval.tv_sec
3403 - ((struct timeval *)tv_rel)->tv_sec;
3404 if (((struct timeval *)tv_rel)->tv_usec < 0)
3405 {
3406 ((struct timeval *)tv_rel)->tv_usec += 1000000;
3407 --((struct timeval *)tv_rel)->tv_sec;
3408 }
3409 *(struct timeval *)tv_start = prev_timeval;
3410}
3411
3412/*
3413 * Compute the previous time after doing something that could nest.
3414 * Subtract "*tp" from prev_timeval;
3415 * Note: The arguments are (void *) to avoid trouble with systems that don't
3416 * have struct timeval.
3417 */
3418 void
3419time_pop(tp)
3420 void *tp; /* actually (struct timeval *) */
3421{
3422 prev_timeval.tv_usec -= ((struct timeval *)tp)->tv_usec;
3423 prev_timeval.tv_sec -= ((struct timeval *)tp)->tv_sec;
3424 if (prev_timeval.tv_usec < 0)
3425 {
3426 prev_timeval.tv_usec += 1000000;
3427 --prev_timeval.tv_sec;
3428 }
3429}
3430
3431 static void
3432time_diff(then, now)
3433 struct timeval *then;
3434 struct timeval *now;
3435{
3436 long usec;
3437 long msec;
3438
3439 usec = now->tv_usec - then->tv_usec;
3440 msec = (now->tv_sec - then->tv_sec) * 1000L + usec / 1000L,
3441 usec = usec % 1000L;
3442 fprintf(time_fd, "%03ld.%03ld", msec, usec >= 0 ? usec : usec + 1000L);
3443}
3444
3445 void
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003446time_msg(mesg, tv_start)
3447 char *mesg;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003448 void *tv_start; /* only for do_source: start time; actually
3449 (struct timeval *) */
3450{
3451 static struct timeval start;
3452 struct timeval now;
3453
3454 if (time_fd != NULL)
3455 {
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003456 if (strstr(mesg, "STARTING") != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003457 {
3458 gettimeofday(&start, NULL);
3459 prev_timeval = start;
3460 fprintf(time_fd, "\n\ntimes in msec\n");
3461 fprintf(time_fd, " clock self+sourced self: sourced script\n");
3462 fprintf(time_fd, " clock elapsed: other lines\n\n");
3463 }
3464 gettimeofday(&now, NULL);
3465 time_diff(&start, &now);
3466 if (((struct timeval *)tv_start) != NULL)
3467 {
3468 fprintf(time_fd, " ");
3469 time_diff(((struct timeval *)tv_start), &now);
3470 }
3471 fprintf(time_fd, " ");
3472 time_diff(&prev_timeval, &now);
3473 prev_timeval = now;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003474 fprintf(time_fd, ": %s\n", mesg);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003475 }
3476}
3477
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003478#endif
3479
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003480#if (defined(FEAT_CLIENTSERVER) && !defined(NO_VIM_MAIN)) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003481
3482/*
3483 * Common code for the X command server and the Win32 command server.
3484 */
3485
Bram Moolenaareb94e552006-03-11 21:35:11 +00003486static char_u *build_drop_cmd __ARGS((int filec, char **filev, int tabs, int sendReply));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003487
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003488/*
3489 * Do the client-server stuff, unless "--servername ''" was used.
3490 */
3491 static void
3492exec_on_server(parmp)
3493 mparm_T *parmp;
3494{
3495 if (parmp->serverName_arg == NULL || *parmp->serverName_arg != NUL)
3496 {
3497# ifdef WIN32
3498 /* Initialise the client/server messaging infrastructure. */
3499 serverInitMessaging();
3500# endif
3501
3502 /*
3503 * When a command server argument was found, execute it. This may
3504 * exit Vim when it was successful. Otherwise it's executed further
3505 * on. Remember the encoding used here in "serverStrEnc".
3506 */
3507 if (parmp->serverArg)
3508 {
3509 cmdsrv_main(&parmp->argc, parmp->argv,
3510 parmp->serverName_arg, &parmp->serverStr);
3511# ifdef FEAT_MBYTE
3512 parmp->serverStrEnc = vim_strsave(p_enc);
3513# endif
3514 }
3515
3516 /* If we're still running, get the name to register ourselves.
3517 * On Win32 can register right now, for X11 need to setup the
3518 * clipboard first, it's further down. */
3519 parmp->servername = serverMakeName(parmp->serverName_arg,
3520 parmp->argv[0]);
3521# ifdef WIN32
3522 if (parmp->servername != NULL)
3523 {
3524 serverSetName(parmp->servername);
3525 vim_free(parmp->servername);
3526 }
3527# endif
3528 }
3529}
3530
3531/*
3532 * Prepare for running as a Vim server.
3533 */
3534 static void
3535prepare_server(parmp)
3536 mparm_T *parmp;
3537{
3538# if defined(FEAT_X11)
3539 /*
3540 * Register for remote command execution with :serversend and --remote
3541 * unless there was a -X or a --servername '' on the command line.
3542 * Only register nongui-vim's with an explicit --servername argument.
Bram Moolenaar5f402312006-08-15 19:40:35 +00003543 * When running as root --servername is also required.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003544 */
3545 if (X_DISPLAY != NULL && parmp->servername != NULL && (
3546# ifdef FEAT_GUI
Bram Moolenaar5f402312006-08-15 19:40:35 +00003547 (gui.in_use
3548# ifdef UNIX
Bram Moolenaar311d9822007-02-27 15:48:28 +00003549 && getuid() != ROOT_UID
Bram Moolenaar5f402312006-08-15 19:40:35 +00003550# endif
3551 ) ||
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003552# endif
3553 parmp->serverName_arg != NULL))
3554 {
3555 (void)serverRegisterName(X_DISPLAY, parmp->servername);
3556 vim_free(parmp->servername);
3557 TIME_MSG("register server name");
3558 }
3559 else
3560 serverDelayedStartName = parmp->servername;
3561# endif
3562
3563 /*
3564 * Execute command ourselves if we're here because the send failed (or
3565 * else we would have exited above).
3566 */
3567 if (parmp->serverStr != NULL)
3568 {
3569 char_u *p;
3570
3571 server_to_input_buf(serverConvert(parmp->serverStrEnc,
3572 parmp->serverStr, &p));
3573 vim_free(p);
3574 }
3575}
3576
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003577 static void
3578cmdsrv_main(argc, argv, serverName_arg, serverStr)
3579 int *argc;
3580 char **argv;
3581 char_u *serverName_arg;
3582 char_u **serverStr;
3583{
3584 char_u *res;
3585 int i;
3586 char_u *sname;
3587 int ret;
3588 int didone = FALSE;
3589 int exiterr = 0;
3590 char **newArgV = argv + 1;
3591 int newArgC = 1,
3592 Argc = *argc;
3593 int argtype;
3594#define ARGTYPE_OTHER 0
3595#define ARGTYPE_EDIT 1
3596#define ARGTYPE_EDIT_WAIT 2
3597#define ARGTYPE_SEND 3
3598 int silent = FALSE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003599 int tabs = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003600# ifndef FEAT_X11
3601 HWND srv;
3602# else
3603 Window srv;
3604
3605 setup_term_clip();
3606# endif
3607
3608 sname = serverMakeName(serverName_arg, argv[0]);
3609 if (sname == NULL)
3610 return;
3611
3612 /*
3613 * Execute the command server related arguments and remove them
3614 * from the argc/argv array; We may have to return into main()
3615 */
3616 for (i = 1; i < Argc; i++)
3617 {
3618 res = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003619 if (STRCMP(argv[i], "--") == 0) /* end of option arguments */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003620 {
3621 for (; i < *argc; i++)
3622 {
3623 *newArgV++ = argv[i];
3624 newArgC++;
3625 }
3626 break;
3627 }
3628
Bram Moolenaareb94e552006-03-11 21:35:11 +00003629 if (STRICMP(argv[i], "--remote-send") == 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003630 argtype = ARGTYPE_SEND;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003631 else if (STRNICMP(argv[i], "--remote", 8) == 0)
3632 {
3633 char *p = argv[i] + 8;
3634
3635 argtype = ARGTYPE_EDIT;
3636 while (*p != NUL)
3637 {
3638 if (STRNICMP(p, "-wait", 5) == 0)
3639 {
3640 argtype = ARGTYPE_EDIT_WAIT;
3641 p += 5;
3642 }
3643 else if (STRNICMP(p, "-silent", 7) == 0)
3644 {
3645 silent = TRUE;
3646 p += 7;
3647 }
3648 else if (STRNICMP(p, "-tab", 4) == 0)
3649 {
3650 tabs = TRUE;
3651 p += 4;
3652 }
3653 else
3654 {
3655 argtype = ARGTYPE_OTHER;
3656 break;
3657 }
3658 }
3659 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003660 else
3661 argtype = ARGTYPE_OTHER;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003662
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003663 if (argtype != ARGTYPE_OTHER)
3664 {
3665 if (i == *argc - 1)
3666 mainerr_arg_missing((char_u *)argv[i]);
3667 if (argtype == ARGTYPE_SEND)
3668 {
3669 *serverStr = (char_u *)argv[i + 1];
3670 i++;
3671 }
3672 else
3673 {
3674 *serverStr = build_drop_cmd(*argc - i - 1, argv + i + 1,
Bram Moolenaareb94e552006-03-11 21:35:11 +00003675 tabs, argtype == ARGTYPE_EDIT_WAIT);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003676 if (*serverStr == NULL)
3677 {
3678 /* Probably out of memory, exit. */
3679 didone = TRUE;
3680 exiterr = 1;
3681 break;
3682 }
3683 Argc = i;
3684 }
3685# ifdef FEAT_X11
3686 if (xterm_dpy == NULL)
3687 {
3688 mch_errmsg(_("No display"));
3689 ret = -1;
3690 }
3691 else
3692 ret = serverSendToVim(xterm_dpy, sname, *serverStr,
3693 NULL, &srv, 0, 0, silent);
3694# else
3695 /* Win32 always works? */
3696 ret = serverSendToVim(sname, *serverStr, NULL, &srv, 0, silent);
3697# endif
3698 if (ret < 0)
3699 {
3700 if (argtype == ARGTYPE_SEND)
3701 {
3702 /* Failed to send, abort. */
3703 mch_errmsg(_(": Send failed.\n"));
3704 didone = TRUE;
3705 exiterr = 1;
3706 }
3707 else if (!silent)
3708 /* Let vim start normally. */
3709 mch_errmsg(_(": Send failed. Trying to execute locally\n"));
3710 break;
3711 }
3712
3713# ifdef FEAT_GUI_W32
3714 /* Guess that when the server name starts with "g" it's a GUI
3715 * server, which we can bring to the foreground here.
3716 * Foreground() in the server doesn't work very well. */
3717 if (argtype != ARGTYPE_SEND && TOUPPER_ASC(*sname) == 'G')
3718 SetForegroundWindow(srv);
3719# endif
3720
3721 /*
3722 * For --remote-wait: Wait until the server did edit each
3723 * file. Also detect that the server no longer runs.
3724 */
3725 if (ret >= 0 && argtype == ARGTYPE_EDIT_WAIT)
3726 {
3727 int numFiles = *argc - i - 1;
3728 int j;
3729 char_u *done = alloc(numFiles);
3730 char_u *p;
3731# ifdef FEAT_GUI_W32
3732 NOTIFYICONDATA ni;
3733 int count = 0;
3734 extern HWND message_window;
3735# endif
3736
3737 if (numFiles > 0 && argv[i + 1][0] == '+')
3738 /* Skip "+cmd" argument, don't wait for it to be edited. */
3739 --numFiles;
3740
3741# ifdef FEAT_GUI_W32
3742 ni.cbSize = sizeof(ni);
3743 ni.hWnd = message_window;
3744 ni.uID = 0;
3745 ni.uFlags = NIF_ICON|NIF_TIP;
3746 ni.hIcon = LoadIcon((HINSTANCE)GetModuleHandle(0), "IDR_VIM");
3747 sprintf(ni.szTip, _("%d of %d edited"), count, numFiles);
3748 Shell_NotifyIcon(NIM_ADD, &ni);
3749# endif
3750
3751 /* Wait for all files to unload in remote */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02003752 vim_memset(done, 0, numFiles);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003753 while (memchr(done, 0, numFiles) != NULL)
3754 {
3755# ifdef WIN32
3756 p = serverGetReply(srv, NULL, TRUE, TRUE);
3757 if (p == NULL)
3758 break;
3759# else
3760 if (serverReadReply(xterm_dpy, srv, &p, TRUE) < 0)
3761 break;
3762# endif
3763 j = atoi((char *)p);
3764 if (j >= 0 && j < numFiles)
3765 {
3766# ifdef FEAT_GUI_W32
3767 ++count;
3768 sprintf(ni.szTip, _("%d of %d edited"),
3769 count, numFiles);
3770 Shell_NotifyIcon(NIM_MODIFY, &ni);
3771# endif
3772 done[j] = 1;
3773 }
3774 }
3775# ifdef FEAT_GUI_W32
3776 Shell_NotifyIcon(NIM_DELETE, &ni);
3777# endif
3778 }
3779 }
3780 else if (STRICMP(argv[i], "--remote-expr") == 0)
3781 {
3782 if (i == *argc - 1)
3783 mainerr_arg_missing((char_u *)argv[i]);
3784# ifdef WIN32
3785 /* Win32 always works? */
3786 if (serverSendToVim(sname, (char_u *)argv[i + 1],
3787 &res, NULL, 1, FALSE) < 0)
3788# else
3789 if (xterm_dpy == NULL)
3790 mch_errmsg(_("No display: Send expression failed.\n"));
3791 else if (serverSendToVim(xterm_dpy, sname, (char_u *)argv[i + 1],
3792 &res, NULL, 1, 1, FALSE) < 0)
3793# endif
3794 {
3795 if (res != NULL && *res != NUL)
3796 {
3797 /* Output error from remote */
3798 mch_errmsg((char *)res);
3799 vim_free(res);
3800 res = NULL;
3801 }
3802 mch_errmsg(_(": Send expression failed.\n"));
3803 }
3804 }
3805 else if (STRICMP(argv[i], "--serverlist") == 0)
3806 {
3807# ifdef WIN32
3808 /* Win32 always works? */
3809 res = serverGetVimNames();
3810# else
3811 if (xterm_dpy != NULL)
3812 res = serverGetVimNames(xterm_dpy);
3813# endif
3814 if (called_emsg)
3815 mch_errmsg("\n");
3816 }
3817 else if (STRICMP(argv[i], "--servername") == 0)
3818 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +00003819 /* Already processed. Take it out of the command line */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003820 i++;
3821 continue;
3822 }
3823 else
3824 {
3825 *newArgV++ = argv[i];
3826 newArgC++;
3827 continue;
3828 }
3829 didone = TRUE;
3830 if (res != NULL && *res != NUL)
3831 {
3832 mch_msg((char *)res);
3833 if (res[STRLEN(res) - 1] != '\n')
3834 mch_msg("\n");
3835 }
3836 vim_free(res);
3837 }
3838
3839 if (didone)
3840 {
3841 display_errors(); /* display any collected messages */
3842 exit(exiterr); /* Mission accomplished - get out */
3843 }
3844
3845 /* Return back into main() */
3846 *argc = newArgC;
3847 vim_free(sname);
3848}
3849
3850/*
3851 * Build a ":drop" command to send to a Vim server.
3852 */
3853 static char_u *
Bram Moolenaareb94e552006-03-11 21:35:11 +00003854build_drop_cmd(filec, filev, tabs, sendReply)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003855 int filec;
3856 char **filev;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003857 int tabs; /* Use ":tab drop" instead of ":drop". */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003858 int sendReply;
3859{
3860 garray_T ga;
3861 int i;
3862 char_u *inicmd = NULL;
3863 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003864 char_u *cwd;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003865
3866 if (filec > 0 && filev[0][0] == '+')
3867 {
3868 inicmd = (char_u *)filev[0] + 1;
3869 filev++;
3870 filec--;
3871 }
3872 /* Check if we have at least one argument. */
3873 if (filec <= 0)
3874 mainerr_arg_missing((char_u *)filev[-1]);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003875
3876 /* Temporarily cd to the current directory to handle relative file names. */
Bram Moolenaard9462e32011-04-11 21:35:11 +02003877 cwd = alloc(MAXPATHL);
3878 if (cwd == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003879 return NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003880 if (mch_dirname(cwd, MAXPATHL) != OK)
3881 {
3882 vim_free(cwd);
3883 return NULL;
3884 }
3885 p = vim_strsave_escaped_ext(cwd,
Bram Moolenaar02b06312007-09-06 15:39:22 +00003886#ifdef BACKSLASH_IN_FILENAME
3887 "", /* rem_backslash() will tell what chars to escape */
3888#else
3889 PATH_ESC_CHARS,
3890#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +02003891 '\\', TRUE);
3892 vim_free(cwd);
3893 if (p == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003894 return NULL;
3895 ga_init2(&ga, 1, 100);
3896 ga_concat(&ga, (char_u *)"<C-\\><C-N>:cd ");
3897 ga_concat(&ga, p);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003898 vim_free(p);
Bram Moolenaareb94e552006-03-11 21:35:11 +00003899
3900 /* Call inputsave() so that a prompt for an encryption key works. */
3901 ga_concat(&ga, (char_u *)"<CR>:if exists('*inputsave')|call inputsave()|endif|");
3902 if (tabs)
3903 ga_concat(&ga, (char_u *)"tab ");
3904 ga_concat(&ga, (char_u *)"drop");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003905 for (i = 0; i < filec; i++)
3906 {
3907 /* On Unix the shell has already expanded the wildcards, don't want to
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003908 * do it again in the Vim server. On MS-Windows only escape
3909 * non-wildcard characters. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003910 p = vim_strsave_escaped((char_u *)filev[i],
3911#ifdef UNIX
3912 PATH_ESC_CHARS
3913#else
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003914 (char_u *)" \t%#"
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003915#endif
3916 );
3917 if (p == NULL)
3918 {
3919 vim_free(ga.ga_data);
3920 return NULL;
3921 }
3922 ga_concat(&ga, (char_u *)" ");
3923 ga_concat(&ga, p);
3924 vim_free(p);
3925 }
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003926 ga_concat(&ga, (char_u *)"|if exists('*inputrestore')|call inputrestore()|endif<CR>");
3927
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003928 /* The :drop commands goes to Insert mode when 'insertmode' is set, use
3929 * CTRL-\ CTRL-N again. */
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003930 ga_concat(&ga, (char_u *)"<C-\\><C-N>");
3931
3932 /* Switch back to the correct current directory (prior to temporary path
3933 * switch) unless 'autochdir' is set, in which case it will already be
3934 * correct after the :drop command. */
3935 ga_concat(&ga, (char_u *)":if !exists('+acd')||!&acd|cd -|endif<CR>");
3936
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003937 if (sendReply)
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003938 ga_concat(&ga, (char_u *)":call SetupRemoteReplies()<CR>");
3939 ga_concat(&ga, (char_u *)":");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003940 if (inicmd != NULL)
3941 {
3942 /* Can't use <CR> after "inicmd", because an "startinsert" would cause
3943 * the following commands to be inserted as text. Use a "|",
3944 * hopefully "inicmd" does allow this... */
3945 ga_concat(&ga, inicmd);
3946 ga_concat(&ga, (char_u *)"|");
3947 }
3948 /* Bring the window to the foreground, goto Insert mode when 'im' set and
3949 * clear command line. */
Bram Moolenaar567e4de2004-12-31 21:01:02 +00003950 ga_concat(&ga, (char_u *)"cal foreground()|if &im|star|en|redr|f<CR>");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003951 ga_append(&ga, NUL);
3952 return ga.ga_data;
3953}
3954
3955/*
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003956 * Make our basic server name: use the specified "arg" if given, otherwise use
3957 * the tail of the command "cmd" we were started with.
3958 * Return the name in allocated memory. This doesn't include a serial number.
3959 */
3960 static char_u *
3961serverMakeName(arg, cmd)
3962 char_u *arg;
3963 char *cmd;
3964{
3965 char_u *p;
3966
3967 if (arg != NULL && *arg != NUL)
3968 p = vim_strsave_up(arg);
3969 else
3970 {
3971 p = vim_strsave_up(gettail((char_u *)cmd));
3972 /* Remove .exe or .bat from the name. */
3973 if (p != NULL && vim_strchr(p, '.') != NULL)
3974 *vim_strchr(p, '.') = NUL;
3975 }
3976 return p;
3977}
3978#endif /* FEAT_CLIENTSERVER */
3979
3980#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
3981/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003982 * Replace termcodes such as <CR> and insert as key presses if there is room.
3983 */
3984 void
3985server_to_input_buf(str)
3986 char_u *str;
3987{
3988 char_u *ptr = NULL;
3989 char_u *cpo_save = p_cpo;
3990
3991 /* Set 'cpoptions' the way we want it.
3992 * B set - backslashes are *not* treated specially
3993 * k set - keycodes are *not* reverse-engineered
3994 * < unset - <Key> sequences *are* interpreted
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00003995 * The last but one parameter of replace_termcodes() is TRUE so that the
3996 * <lt> sequence is recognised - needed for a real backslash.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003997 */
3998 p_cpo = (char_u *)"Bk";
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00003999 str = replace_termcodes((char_u *)str, &ptr, FALSE, TRUE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004000 p_cpo = cpo_save;
4001
4002 if (*ptr != NUL) /* trailing CTRL-V results in nothing */
4003 {
4004 /*
4005 * Add the string to the input stream.
4006 * Can't use add_to_input_buf() here, we now have K_SPECIAL bytes.
4007 *
4008 * First clear typed characters from the typeahead buffer, there could
4009 * be half a mapping there. Then append to the existing string, so
4010 * that multiple commands from a client are concatenated.
4011 */
4012 if (typebuf.tb_maplen < typebuf.tb_len)
4013 del_typebuf(typebuf.tb_len - typebuf.tb_maplen, typebuf.tb_maplen);
4014 (void)ins_typebuf(str, REMAP_NONE, typebuf.tb_len, TRUE, FALSE);
4015
4016 /* Let input_available() know we inserted text in the typeahead
4017 * buffer. */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004018 typebuf_was_filled = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004019 }
4020 vim_free((char_u *)ptr);
4021}
4022
4023/*
4024 * Evaluate an expression that the client sent to a string.
4025 * Handles disabling error messages and disables debugging, otherwise Vim
4026 * hangs, waiting for "cont" to be typed.
4027 */
4028 char_u *
4029eval_client_expr_to_string(expr)
4030 char_u *expr;
4031{
4032 char_u *res;
4033 int save_dbl = debug_break_level;
4034 int save_ro = redir_off;
4035
4036 debug_break_level = -1;
4037 redir_off = 0;
4038 ++emsg_skip;
4039
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004040 res = eval_to_string(expr, NULL, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004041
4042 debug_break_level = save_dbl;
4043 redir_off = save_ro;
4044 --emsg_skip;
4045
Bram Moolenaar63a121b2005-12-11 21:36:39 +00004046 /* A client can tell us to redraw, but not to display the cursor, so do
4047 * that here. */
4048 setcursor();
4049 out_flush();
4050#ifdef FEAT_GUI
4051 if (gui.in_use)
4052 gui_update_cursor(FALSE, FALSE);
4053#endif
4054
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004055 return res;
4056}
4057
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004058/*
4059 * If conversion is needed, convert "data" from "client_enc" to 'encoding' and
4060 * return an allocated string. Otherwise return "data".
4061 * "*tofree" is set to the result when it needs to be freed later.
4062 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004063 char_u *
4064serverConvert(client_enc, data, tofree)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004065 char_u *client_enc UNUSED;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004066 char_u *data;
4067 char_u **tofree;
4068{
4069 char_u *res = data;
4070
4071 *tofree = NULL;
4072# ifdef FEAT_MBYTE
4073 if (client_enc != NULL && p_enc != NULL)
4074 {
4075 vimconv_T vimconv;
4076
4077 vimconv.vc_type = CONV_NONE;
4078 if (convert_setup(&vimconv, client_enc, p_enc) != FAIL
4079 && vimconv.vc_type != CONV_NONE)
4080 {
4081 res = string_convert(&vimconv, data, NULL);
4082 if (res == NULL)
4083 res = data;
4084 else
4085 *tofree = res;
4086 }
4087 convert_setup(&vimconv, NULL, NULL);
4088 }
4089# endif
4090 return res;
4091}
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004092#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004093
4094/*
4095 * When FEAT_FKMAP is defined, also compile the Farsi source code.
4096 */
4097#if defined(FEAT_FKMAP) || defined(PROTO)
4098# include "farsi.c"
4099#endif
4100
4101/*
4102 * When FEAT_ARABIC is defined, also compile the Arabic source code.
4103 */
4104#if defined(FEAT_ARABIC) || defined(PROTO)
4105# include "arabic.c"
4106#endif