blob: 5bb81a6003333a9656a7faed611d28e53eb82048 [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 Moolenaarc236c162008-07-13 17:41:49 +000010#if defined(MSDOS) || defined(WIN16) || defined(WIN32) || defined(_WIN64)
Bram Moolenaar362e1a32006-03-06 23:29:24 +000011# include "vimio.h" /* for close() and dup() */
Bram Moolenaarb4210b32004-06-13 14:51:16 +000012#endif
13
14#define EXTERN
15#include "vim.h"
16
17#ifdef SPAWNO
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +000018# include <spawno.h> /* special MS-DOS swapping library */
Bram Moolenaarb4210b32004-06-13 14:51:16 +000019#endif
20
Bram Moolenaarb4210b32004-06-13 14:51:16 +000021#ifdef __CYGWIN__
22# ifndef WIN32
Bram Moolenaar0d1498e2008-06-29 12:00:49 +000023# include <cygwin/version.h>
24# include <sys/cygwin.h> /* for cygwin_conv_to_posix_path() and/or
25 * cygwin_conv_path() */
Bram Moolenaarb4210b32004-06-13 14:51:16 +000026# endif
27# include <limits.h>
28#endif
29
Bram Moolenaarc013cb62005-07-24 21:18:31 +000030/* Maximum number of commands from + or -c arguments. */
31#define MAX_ARG_CMDS 10
32
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000033/* values for "window_layout" */
34#define WIN_HOR 1 /* "-o" horizontally split windows */
35#define WIN_VER 2 /* "-O" vertically split windows */
36#define WIN_TABS 3 /* "-p" windows on tab pages */
37
Bram Moolenaar58d98232005-07-23 22:25:46 +000038/* Struct for various parameters passed between main() and other functions. */
39typedef struct
40{
Bram Moolenaarc013cb62005-07-24 21:18:31 +000041 int argc;
42 char **argv;
43
44 int evim_mode; /* started as "evim" */
Bram Moolenaarc013cb62005-07-24 21:18:31 +000045 char_u *use_vimrc; /* vimrc from -u argument */
46
47 int n_commands; /* no. of commands from + or -c */
48 char_u *commands[MAX_ARG_CMDS]; /* commands from + or -c arg. */
49 char_u cmds_tofree[MAX_ARG_CMDS]; /* commands that need free() */
50 int n_pre_commands; /* no. of commands from --cmd */
51 char_u *pre_commands[MAX_ARG_CMDS]; /* commands from --cmd argument */
52
53 int edit_type; /* type of editing to do */
54 char_u *tagname; /* tag from -t argument */
55#ifdef FEAT_QUICKFIX
56 char_u *use_ef; /* 'errorfile' from -q argument */
57#endif
58
59 int want_full_screen;
60 int stdout_isatty; /* is stdout a terminal? */
61 char_u *term; /* specified terminal name */
62#ifdef FEAT_CRYPT
63 int ask_for_key; /* -x argument */
64#endif
65 int no_swap_file; /* "-n" argument used */
66#ifdef FEAT_EVAL
67 int use_debug_break_level;
68#endif
69#ifdef FEAT_WINDOWS
70 int window_count; /* number of windows to use */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000071 int window_layout; /* 0, WIN_HOR, WIN_VER or WIN_TABS */
Bram Moolenaarc013cb62005-07-24 21:18:31 +000072#endif
73
74#ifdef FEAT_CLIENTSERVER
Bram Moolenaar58d98232005-07-23 22:25:46 +000075 int serverArg; /* TRUE when argument for a server */
76 char_u *serverName_arg; /* cmdline arg for server name */
Bram Moolenaarc013cb62005-07-24 21:18:31 +000077 char_u *serverStr; /* remote server command */
78 char_u *serverStrEnc; /* encoding of serverStr */
79 char_u *servername; /* allocated name for our server */
80#endif
81#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
82 int literal; /* don't expand file names */
83#endif
84#ifdef MSWIN
85 int full_path; /* file name argument was full path */
86#endif
87#ifdef FEAT_DIFF
88 int diff_mode; /* start with 'diff' set */
89#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +000090} mparm_T;
91
Bram Moolenaarc013cb62005-07-24 21:18:31 +000092/* Values for edit_type. */
93#define EDIT_NONE 0 /* no edit type yet */
94#define EDIT_FILE 1 /* file name argument[s] given, use argument list */
95#define EDIT_STDIN 2 /* read file from stdin */
96#define EDIT_TAG 3 /* tag name argument given, use tagname */
97#define EDIT_QF 4 /* start in quickfix mode */
98
Bram Moolenaarb4210b32004-06-13 14:51:16 +000099#if defined(UNIX) || defined(VMS)
100static int file_owned __ARGS((char *fname));
101#endif
102static void mainerr __ARGS((int, char_u *));
103static void main_msg __ARGS((char *s));
104static void usage __ARGS((void));
105static int get_number_arg __ARGS((char_u *p, int *idx, int def));
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000106#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
107static void init_locale __ARGS((void));
108#endif
109static void parse_command_name __ARGS((mparm_T *parmp));
110static void early_arg_scan __ARGS((mparm_T *parmp));
111static void command_line_scan __ARGS((mparm_T *parmp));
112static void check_tty __ARGS((mparm_T *parmp));
113static void read_stdin __ARGS((void));
114static void create_windows __ARGS((mparm_T *parmp));
115#ifdef FEAT_WINDOWS
116static void edit_buffers __ARGS((mparm_T *parmp));
117#endif
118static void exe_pre_commands __ARGS((mparm_T *parmp));
119static void exe_commands __ARGS((mparm_T *parmp));
Bram Moolenaar58d98232005-07-23 22:25:46 +0000120static void source_startup_scripts __ARGS((mparm_T *parmp));
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000121static void main_start_gui __ARGS((void));
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000122#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000123static void check_swap_exists_action __ARGS((void));
124#endif
125#ifdef FEAT_CLIENTSERVER
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000126static void exec_on_server __ARGS((mparm_T *parmp));
127static void prepare_server __ARGS((mparm_T *parmp));
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000128static void cmdsrv_main __ARGS((int *argc, char **argv, char_u *serverName_arg, char_u **serverStr));
129static char_u *serverMakeName __ARGS((char_u *arg, char *cmd));
130#endif
131
132
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000133/*
134 * Different types of error messages.
135 */
136static char *(main_errors[]) =
137{
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000138 N_("Unknown option argument"),
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000139#define ME_UNKNOWN_OPTION 0
140 N_("Too many edit arguments"),
141#define ME_TOO_MANY_ARGS 1
142 N_("Argument missing after"),
143#define ME_ARG_MISSING 2
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000144 N_("Garbage after option argument"),
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000145#define ME_GARBAGE 3
146 N_("Too many \"+command\", \"-c command\" or \"--cmd command\" arguments"),
147#define ME_EXTRA_CMD 4
148 N_("Invalid argument for"),
149#define ME_INVALID_ARG 5
150};
151
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000152#ifndef PROTO /* don't want a prototype for main() */
153 int
154# ifdef VIMDLL
155_export
156# endif
157# ifdef FEAT_GUI_MSWIN
158# ifdef __BORLANDC__
159_cdecl
160# endif
161VimMain
162# else
163main
164# endif
165(argc, argv)
166 int argc;
167 char **argv;
168{
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000169 char_u *fname = NULL; /* file name from command line */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000170 mparm_T params; /* various parameters passed between
171 * main() and other functions. */
Bram Moolenaar3f269672009-11-03 11:11:11 +0000172#ifdef STARTUPTIME
173 int i;
174#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000175
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000176 /*
177 * Do any system-specific initialisations. These can NOT use IObuff or
178 * NameBuff. Thus emsg2() cannot be called!
179 */
180 mch_early_init();
181
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000182 /* Many variables are in "params" so that we can pass them to invoked
183 * functions without a lot of arguments. "argc" and "argv" are also
184 * copied, so that they can be changed. */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000185 vim_memset(&params, 0, sizeof(params));
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000186 params.argc = argc;
187 params.argv = argv;
188 params.want_full_screen = TRUE;
189#ifdef FEAT_EVAL
190 params.use_debug_break_level = -1;
191#endif
192#ifdef FEAT_WINDOWS
193 params.window_count = -1;
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000194#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +0000195
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000196#ifdef FEAT_TCL
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000197 vim_tcl_init(params.argv[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000198#endif
199
200#ifdef MEM_PROFILE
201 atexit(vim_mem_profile_dump);
202#endif
203
204#ifdef STARTUPTIME
Bram Moolenaar3f269672009-11-03 11:11:11 +0000205 for (i = 1; i < argc; ++i)
206 {
207 if (STRNICMP(argv[i], "--startuptime=", 14) == 0)
208 {
209 time_fd = mch_fopen(argv[i] + 14, "a");
210 TIME_MSG("--- VIM STARTING ---");
211 break;
212 }
213 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000214#endif
Bram Moolenaarca003e12006-03-17 23:19:38 +0000215 starttime = time(NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000216
217#ifdef __EMX__
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000218 _wildcard(&params.argc, &params.argv);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000219#endif
220
221#ifdef FEAT_MBYTE
222 (void)mb_init(); /* init mb_bytelen_tab[] to ones */
223#endif
Bram Moolenaardcaf10e2005-01-21 11:55:25 +0000224#ifdef FEAT_EVAL
225 eval_init(); /* init global variables */
226#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000227
228#ifdef __QNXNTO__
229 qnx_init(); /* PhAttach() for clipboard, (and gui) */
230#endif
231
232#ifdef MAC_OS_CLASSIC
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000233 /* Prepare for possibly starting GUI sometime */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000234 /* Macintosh needs this before any memory is allocated. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000235 gui_prepare(&params.argc, params.argv);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000236 TIME_MSG("GUI prepared");
237#endif
238
239 /* Init the table of Normal mode commands. */
240 init_normal_cmds();
241
242#if defined(HAVE_DATE_TIME) && defined(VMS) && defined(VAXC)
Bram Moolenaar58d98232005-07-23 22:25:46 +0000243 make_version(); /* Construct the long version string. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000244#endif
245
246 /*
247 * Allocate space for the generic buffers (needed for set_init_1() and
248 * EMSG2()).
249 */
250 if ((IObuff = alloc(IOSIZE)) == NULL
251 || (NameBuff = alloc(MAXPATHL)) == NULL)
252 mch_exit(0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000253 TIME_MSG("Allocated generic buffers");
254
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000255#ifdef NBDEBUG
256 /* Wait a moment for debugging NetBeans. Must be after allocating
257 * NameBuff. */
258 nbdebug_log_init("SPRO_GVIM_DEBUG", "SPRO_GVIM_DLEVEL");
259 nbdebug_wait(WT_ENV | WT_WAIT | WT_STOP, "SPRO_GVIM_WAIT", 20);
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000260 TIME_MSG("NetBeans debug wait");
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000261#endif
262
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000263#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
264 /*
265 * Setup to use the current locale (for ctype() and many other things).
266 * NOTE: Translated messages with encodings other than latin1 will not
267 * work until set_init_1() has been called!
268 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000269 init_locale();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000270 TIME_MSG("locale set");
271#endif
272
273#ifdef FEAT_GUI
274 gui.dofork = TRUE; /* default is to use fork() */
275#endif
276
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000277 /*
Bram Moolenaar58d98232005-07-23 22:25:46 +0000278 * Do a first scan of the arguments in "argv[]":
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000279 * -display or --display
Bram Moolenaar58d98232005-07-23 22:25:46 +0000280 * --server...
281 * --socketid
Bram Moolenaar78e17622007-08-30 10:26:19 +0000282 * --windowid
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000283 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000284 early_arg_scan(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000285
286#ifdef FEAT_SUN_WORKSHOP
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000287 findYourself(params.argv[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000288#endif
289#if defined(FEAT_GUI) && !defined(MAC_OS_CLASSIC)
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000290 /* Prepare for possibly starting GUI sometime */
291 gui_prepare(&params.argc, params.argv);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000292 TIME_MSG("GUI prepared");
293#endif
294
295#ifdef FEAT_CLIPBOARD
296 clip_init(FALSE); /* Initialise clipboard stuff */
297 TIME_MSG("clipboard setup");
298#endif
299
300 /*
301 * Check if we have an interactive window.
302 * On the Amiga: If there is no window, we open one with a newcli command
303 * (needed for :! to * work). mch_check_win() will also handle the -d or
304 * -dev argument.
305 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000306 params.stdout_isatty = (mch_check_win(params.argc, params.argv) != FAIL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000307 TIME_MSG("window checked");
308
309 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000310 * Allocate the first window and buffer.
311 * Can't do anything without it, exit when it fails.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000312 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000313 if (win_alloc_first() == FAIL)
314 mch_exit(0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000315
316 init_yank(); /* init yank buffers */
317
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000318 alist_init(&global_alist); /* Init the argument list to empty. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000319
320 /*
321 * Set the default values for the options.
322 * NOTE: Non-latin1 translated messages are working only after this,
323 * because this is where "has_mbyte" will be set, which is used by
324 * msg_outtrans_len_attr().
325 * First find out the home directory, needed to expand "~" in options.
326 */
327 init_homedir(); /* find real value of $HOME */
328 set_init_1();
329 TIME_MSG("inits 1");
330
331#ifdef FEAT_EVAL
332 set_lang_var(); /* set v:lang and v:ctype */
333#endif
334
335#ifdef FEAT_CLIENTSERVER
336 /*
337 * Do the client-server stuff, unless "--servername ''" was used.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000338 * This may exit Vim if the command was sent to the server.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000339 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000340 exec_on_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000341#endif
342
343 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000344 * Figure out the way to work from the command name argv[0].
345 * "vimdiff" starts diff mode, "rvim" sets "restricted", etc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000346 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000347 parse_command_name(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000348
349 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000350 * Process the command line arguments. File names are put in the global
351 * argument list "global_alist".
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000352 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000353 command_line_scan(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000354 TIME_MSG("parsing arguments");
355
356 /*
357 * On some systems, when we compile with the GUI, we always use it. On Mac
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000358 * there is no terminal version, and on Windows we can't fork one off with
359 * :gui.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000360 */
361#ifdef ALWAYS_USE_GUI
362 gui.starting = TRUE;
363#else
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000364# if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000365 /*
366 * Check if the GUI can be started. Reset gui.starting if not.
367 * Don't know about other systems, stay on the safe side and don't check.
368 */
369 if (gui.starting && gui_init_check() == FAIL)
370 {
371 gui.starting = FALSE;
372
373 /* When running "evim" or "gvim -y" we need the menus, exit if we
374 * don't have them. */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000375 if (params.evim_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000376 mch_exit(1);
377 }
378# endif
379#endif
380
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000381 if (GARGCOUNT > 0)
382 {
383#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
384 /*
385 * Expand wildcards in file names.
386 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000387 if (!params.literal)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000388 {
389 /* Temporarily add '(' and ')' to 'isfname'. These are valid
390 * filename characters but are excluded from 'isfname' to make
391 * "gf" work on a file name in parenthesis (e.g.: see vim.h). */
392 do_cmdline_cmd((char_u *)":set isf+=(,)");
Bram Moolenaar86b68352004-12-27 21:59:20 +0000393 alist_expand(NULL, 0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000394 do_cmdline_cmd((char_u *)":set isf&");
395 }
396#endif
397 fname = alist_name(&GARGLIST[0]);
398 }
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000399
400#if defined(WIN32) && defined(FEAT_MBYTE)
401 {
402 extern void set_alist_count(void);
403
404 /* Remember the number of entries in the argument list. If it changes
405 * we don't react on setting 'encoding'. */
406 set_alist_count();
407 }
408#endif
409
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000410#ifdef MSWIN
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000411 if (GARGCOUNT == 1 && params.full_path)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000412 {
413 /*
414 * If there is one filename, fully qualified, we have very probably
415 * been invoked from explorer, so change to the file's directory.
416 * Hint: to avoid this when typing a command use a forward slash.
417 * If the cd fails, it doesn't matter.
418 */
419 (void)vim_chdirfile(fname);
420 }
421#endif
422 TIME_MSG("expanding arguments");
423
424#ifdef FEAT_DIFF
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000425 if (params.diff_mode && params.window_count == -1)
426 params.window_count = 0; /* open up to 3 windows */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000427#endif
428
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000429 /* Don't redraw until much later. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000430 ++RedrawingDisabled;
431
432 /*
433 * When listing swap file names, don't do cursor positioning et. al.
434 */
435 if (recoverymode && fname == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000436 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000437
438 /*
439 * When certain to start the GUI, don't check capabilities of terminal.
440 * For GTK we can't be sure, but when started from the desktop it doesn't
441 * make sense to try using a terminal.
442 */
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000443#if defined(ALWAYS_USE_GUI) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000444 if (gui.starting
445# ifdef FEAT_GUI_GTK
446 && !isatty(2)
447# endif
448 )
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000449 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000450#endif
451
452#if defined(FEAT_GUI_MAC) && defined(MACOS_X_UNIX)
453 /* When the GUI is started from Finder, need to display messages in a
454 * message box. isatty(2) returns TRUE anyway, thus we need to check the
455 * name to know we're not started from a terminal. */
456 if (gui.starting && (!isatty(2) || strcmp("/dev/console", ttyname(2)) == 0))
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000457 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000458 params.want_full_screen = FALSE;
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000459
460 /* Avoid always using "/" as the current directory. Note that when
461 * started from Finder the arglist will be filled later in
462 * HandleODocAE() and "fname" will be NULL. */
463 if (getcwd((char *)NameBuff, MAXPATHL) != NULL
464 && STRCMP(NameBuff, "/") == 0)
465 {
466 if (fname != NULL)
467 (void)vim_chdirfile(fname);
468 else
469 {
470 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
471 vim_chdir(NameBuff);
472 }
473 }
474 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000475#endif
476
477 /*
478 * mch_init() sets up the terminal (window) for use. This must be
479 * done after resetting full_screen, otherwise it may move the cursor
480 * (MSDOS).
481 * Note that we may use mch_exit() before mch_init()!
482 */
483 mch_init();
484 TIME_MSG("shell init");
485
486#ifdef USE_XSMP
487 /*
488 * For want of anywhere else to do it, try to connect to xsmp here.
489 * Fitting it in after gui_mch_init, but before gui_init (via termcapinit).
490 * Hijacking -X 'no X connection' to also disable XSMP connection as that
491 * has a similar delay upon failure.
492 * Only try if SESSION_MANAGER is set to something non-null.
493 */
494 if (!x_no_connect)
495 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000496 char *p = getenv("SESSION_MANAGER");
497
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000498 if (p != NULL && *p != NUL)
499 {
500 xsmp_init();
501 TIME_MSG("xsmp init");
502 }
503 }
504#endif
505
506 /*
507 * Print a warning if stdout is not a terminal.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000508 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000509 check_tty(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000510
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000511 /* This message comes before term inits, but after setting "silent_mode"
512 * when the input is not a tty. */
513 if (GARGCOUNT > 1 && !silent_mode)
514 printf(_("%d files to edit\n"), GARGCOUNT);
515
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000516 if (params.want_full_screen && !silent_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000517 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000518 termcapinit(params.term); /* set terminal name and get terminal
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000519 capabilities (will set full_screen) */
520 screen_start(); /* don't know where cursor is now */
521 TIME_MSG("Termcap init");
522 }
523
524 /*
525 * Set the default values for the options that use Rows and Columns.
526 */
527 ui_get_shellsize(); /* inits Rows and Columns */
528#ifdef FEAT_NETBEANS_INTG
529 if (usingNetbeans)
530 Columns += 2; /* leave room for glyph gutter */
531#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000532 win_init_size();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000533#ifdef FEAT_DIFF
534 /* Set the 'diff' option now, so that it can be checked for in a .vimrc
535 * file. There is no buffer yet though. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000536 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000537 diff_win_options(firstwin, FALSE);
538#endif
539
540 cmdline_row = Rows - p_ch;
541 msg_row = cmdline_row;
542 screenalloc(FALSE); /* allocate screen buffers */
543 set_init_2();
544 TIME_MSG("inits 2");
545
546 msg_scroll = TRUE;
547 no_wait_return = TRUE;
548
549 init_mappings(); /* set up initial mappings */
550
551 init_highlight(TRUE, FALSE); /* set the default highlight groups */
552 TIME_MSG("init highlight");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000553
554#ifdef FEAT_EVAL
555 /* Set the break level after the terminal is initialized. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000556 debug_break_level = params.use_debug_break_level;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000557#endif
558
Bram Moolenaar58d98232005-07-23 22:25:46 +0000559 /* Execute --cmd arguments. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000560 exe_pre_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000561
Bram Moolenaar58d98232005-07-23 22:25:46 +0000562 /* Source startup scripts. */
563 source_startup_scripts(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000564
565#ifdef FEAT_EVAL
566 /*
567 * Read all the plugin files.
568 * Only when compiled with +eval, since most plugins need it.
569 */
570 if (p_lpl)
571 {
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000572# ifdef VMS /* Somehow VMS doesn't handle the "**". */
573 source_runtime((char_u *)"plugin/*.vim", TRUE);
574# else
Bram Moolenaar07d4d732005-10-03 22:04:08 +0000575 source_runtime((char_u *)"plugin/**/*.vim", TRUE);
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000576# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000577 TIME_MSG("loading plugins");
578 }
579#endif
580
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000581#ifdef FEAT_DIFF
582 /* Decide about window layout for diff mode after reading vimrc. */
583 if (params.diff_mode && params.window_layout == 0)
584 {
585 if (diffopt_horizontal())
586 params.window_layout = WIN_HOR; /* use horizontal split */
587 else
588 params.window_layout = WIN_VER; /* use vertical split */
589 }
590#endif
591
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000592 /*
593 * Recovery mode without a file name: List swap files.
594 * This uses the 'dir' option, therefore it must be after the
595 * initializations.
596 */
597 if (recoverymode && fname == NULL)
598 {
599 recover_names(NULL, TRUE, 0);
600 mch_exit(0);
601 }
602
603 /*
604 * Set a few option defaults after reading .vimrc files:
605 * 'title' and 'icon', Unix: 'shellpipe' and 'shellredir'.
606 */
607 set_init_3();
608 TIME_MSG("inits 3");
609
610 /*
611 * "-n" argument: Disable swap file by setting 'updatecount' to 0.
612 * Note that this overrides anything from a vimrc file.
613 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000614 if (params.no_swap_file)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000615 p_uc = 0;
616
617#ifdef FEAT_FKMAP
618 if (curwin->w_p_rl && p_altkeymap)
619 {
620 p_hkmap = FALSE; /* Reset the Hebrew keymap mode */
621# ifdef FEAT_ARABIC
622 curwin->w_p_arab = FALSE; /* Reset the Arabic keymap mode */
623# endif
624 p_fkmap = TRUE; /* Set the Farsi keymap mode */
625 }
626#endif
627
628#ifdef FEAT_GUI
629 if (gui.starting)
630 {
631#if defined(UNIX) || defined(VMS)
632 /* When something caused a message from a vimrc script, need to output
633 * an extra newline before the shell prompt. */
634 if (did_emsg || msg_didout)
635 putchar('\n');
636#endif
637
638 gui_start(); /* will set full_screen to TRUE */
639 TIME_MSG("starting GUI");
640
641 /* When running "evim" or "gvim -y" we need the menus, exit if we
642 * don't have them. */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000643 if (!gui.in_use && params.evim_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000644 mch_exit(1);
645 }
646#endif
647
648#ifdef SPAWNO /* special MSDOS swapping library */
649 init_SPAWNO("", SWAP_ANY);
650#endif
651
652#ifdef FEAT_VIMINFO
653 /*
Bram Moolenaard812df62008-11-09 12:46:09 +0000654 * Read in registers, history etc, but not marks, from the viminfo file.
655 * This is where v:oldfiles gets filled.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000656 */
657 if (*p_viminfo != NUL)
658 {
Bram Moolenaard812df62008-11-09 12:46:09 +0000659 read_viminfo(NULL, VIF_WANT_INFO | VIF_GET_OLDFILES);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000660 TIME_MSG("reading viminfo");
661 }
662#endif
663
664#ifdef FEAT_QUICKFIX
665 /*
666 * "-q errorfile": Load the error file now.
667 * If the error file can't be read, exit before doing anything else.
668 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000669 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000670 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000671 if (params.use_ef != NULL)
672 set_string_option_direct((char_u *)"ef", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000673 params.use_ef, OPT_FREE, SID_CARG);
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000674 if (qf_init(NULL, p_ef, p_efm, TRUE) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000675 {
676 out_char('\n');
677 mch_exit(3);
678 }
679 TIME_MSG("reading errorfile");
680 }
681#endif
682
683 /*
684 * Start putting things on the screen.
685 * Scroll screen down before drawing over it
686 * Clear screen now, so file message will not be cleared.
687 */
688 starting = NO_BUFFERS;
689 no_wait_return = FALSE;
690 if (!exmode_active)
691 msg_scroll = FALSE;
692
693#ifdef FEAT_GUI
694 /*
695 * This seems to be required to make callbacks to be called now, instead
696 * of after things have been put on the screen, which then may be deleted
697 * when getting a resize callback.
698 * For the Mac this handles putting files dropped on the Vim icon to
699 * global_alist.
700 */
701 if (gui.in_use)
702 {
703# ifdef FEAT_SUN_WORKSHOP
704 if (!usingSunWorkShop)
705# endif
706 gui_wait_for_chars(50L);
707 TIME_MSG("GUI delay");
708 }
709#endif
710
711#if defined(FEAT_GUI_PHOTON) && defined(FEAT_CLIPBOARD)
712 qnx_clip_init();
713#endif
714
715#ifdef FEAT_XCLIPBOARD
716 /* Start using the X clipboard, unless the GUI was started. */
717# ifdef FEAT_GUI
718 if (!gui.in_use)
719# endif
720 {
721 setup_term_clip();
722 TIME_MSG("setup clipboard");
723 }
724#endif
725
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000726#ifdef FEAT_CLIENTSERVER
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000727 /* Prepare for being a Vim server. */
728 prepare_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000729#endif
730
731 /*
732 * If "-" argument given: Read file from stdin.
733 * Do this before starting Raw mode, because it may change things that the
734 * writing end of the pipe doesn't like, e.g., in case stdin and stderr
735 * are the same terminal: "cat | vim -".
736 * Using autocommands here may cause trouble...
737 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000738 if (params.edit_type == EDIT_STDIN && !recoverymode)
739 read_stdin();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000740
741#if defined(UNIX) || defined(VMS)
742 /* When switching screens and something caused a message from a vimrc
743 * script, need to output an extra newline on exit. */
744 if ((did_emsg || msg_didout) && *T_TI != NUL)
745 newline_on_exit = TRUE;
746#endif
747
748 /*
749 * When done something that is not allowed or error message call
750 * wait_return. This must be done before starttermcap(), because it may
751 * switch to another screen. It must be done after settmode(TMODE_RAW),
752 * because we want to react on a single key stroke.
753 * Call settmode and starttermcap here, so the T_KS and T_TI may be
Bram Moolenaar49325942007-05-10 19:19:59 +0000754 * defined by termcapinit and redefined in .exrc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000755 */
756 settmode(TMODE_RAW);
757 TIME_MSG("setting raw mode");
758
759 if (need_wait_return || msg_didany)
760 {
761 wait_return(TRUE);
762 TIME_MSG("waiting for return");
763 }
764
765 starttermcap(); /* start termcap if not done by wait_return() */
766 TIME_MSG("start termcap");
767
768#ifdef FEAT_MOUSE
769 setmouse(); /* may start using the mouse */
770#endif
771 if (scroll_region)
772 scroll_region_reset(); /* In case Rows changed */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000773 scroll_start(); /* may scroll the screen to the right position */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000774
775 /*
776 * Don't clear the screen when starting in Ex mode, unless using the GUI.
777 */
778 if (exmode_active
779#ifdef FEAT_GUI
780 && !gui.in_use
781#endif
782 )
783 must_redraw = CLEAR;
784 else
785 {
786 screenclear(); /* clear screen */
787 TIME_MSG("clearing screen");
788 }
789
790#ifdef FEAT_CRYPT
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000791 if (params.ask_for_key)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000792 {
793 (void)get_crypt_key(TRUE, TRUE);
794 TIME_MSG("getting crypt key");
795 }
796#endif
797
798 no_wait_return = TRUE;
799
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000800 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000801 * Create the requested number of windows and edit buffers in them.
802 * Also does recovery if "recoverymode" set.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000803 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000804 create_windows(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000805 TIME_MSG("opening buffers");
806
Bram Moolenaar867a4b72007-03-18 20:51:46 +0000807#ifdef FEAT_EVAL
808 /* clear v:swapcommand */
809 set_vim_var_string(VV_SWAPCOMMAND, NULL, -1);
810#endif
811
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000812 /* Ex starts at last line of the file */
813 if (exmode_active)
814 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
815
816#ifdef FEAT_AUTOCMD
817 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
818 TIME_MSG("BufEnter autocommands");
819#endif
820 setpcmark();
821
822#ifdef FEAT_QUICKFIX
823 /*
824 * When started with "-q errorfile" jump to first error now.
825 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000826 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000827 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000828 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000829 TIME_MSG("jump to first error");
830 }
831#endif
832
833#ifdef FEAT_WINDOWS
834 /*
835 * If opened more than one window, start editing files in the other
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000836 * windows.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000837 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000838 edit_buffers(&params);
839#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000840
841#ifdef FEAT_DIFF
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000842 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000843 {
844 win_T *wp;
845
846 /* set options in each window for "vimdiff". */
847 for (wp = firstwin; wp != NULL; wp = wp->w_next)
848 diff_win_options(wp, TRUE);
849 }
850#endif
851
852 /*
853 * Shorten any of the filenames, but only when absolute.
854 */
855 shorten_fnames(FALSE);
856
857 /*
858 * Need to jump to the tag before executing the '-c command'.
859 * Makes "vim -c '/return' -t main" work.
860 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000861 if (params.tagname != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000862 {
Bram Moolenaar146522e2005-12-16 21:55:46 +0000863#if defined(HAS_SWAP_EXISTS_ACTION)
864 swap_exists_did_quit = FALSE;
865#endif
866
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000867 vim_snprintf((char *)IObuff, IOSIZE, "ta %s", params.tagname);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000868 do_cmdline_cmd(IObuff);
869 TIME_MSG("jumping to tag");
Bram Moolenaar146522e2005-12-16 21:55:46 +0000870
871#if defined(HAS_SWAP_EXISTS_ACTION)
872 /* If the user doesn't want to edit the file then we quit here. */
873 if (swap_exists_did_quit)
874 getout(1);
875#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000876 }
877
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000878 /* Execute any "+", "-c" and "-S" arguments. */
879 if (params.n_commands > 0)
880 exe_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000881
882 RedrawingDisabled = 0;
883 redraw_all_later(NOT_VALID);
884 no_wait_return = FALSE;
885 starting = 0;
886
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000887#ifdef FEAT_TERMRESPONSE
888 /* Requesting the termresponse is postponed until here, so that a "-c q"
889 * argument doesn't make it appear in the shell Vim was started from. */
890 may_req_termresponse();
891#endif
892
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000893 /* start in insert mode */
894 if (p_im)
895 need_start_insertmode = TRUE;
896
897#ifdef FEAT_AUTOCMD
898 apply_autocmds(EVENT_VIMENTER, NULL, NULL, FALSE, curbuf);
899 TIME_MSG("VimEnter autocommands");
900#endif
901
902#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
903 /* When a startup script or session file setup for diff'ing and
904 * scrollbind, sync the scrollbind now. */
905 if (curwin->w_p_diff && curwin->w_p_scb)
906 {
907 update_topline();
908 check_scrollbind((linenr_T)0, 0L);
909 TIME_MSG("diff scrollbinding");
910 }
911#endif
912
913#if defined(WIN3264) && !defined(FEAT_GUI_W32)
914 mch_set_winsize_now(); /* Allow winsize changes from now on */
915#endif
916
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000917#if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
918 /* When tab pages were created, may need to update the tab pages line and
919 * scrollbars. This is skipped while creating them. */
920 if (first_tabpage->tp_next != NULL)
921 {
922 out_flush();
923 gui_init_which_components(NULL);
924 gui_update_scrollbars(TRUE);
925 }
926 need_mouse_correct = TRUE;
927#endif
928
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000929 /* If ":startinsert" command used, stuff a dummy command to be able to
930 * call normal_cmd(), which will then start Insert mode. */
931 if (restart_edit != 0)
Bram Moolenaarebefac62005-12-28 22:39:57 +0000932 stuffcharReadbuff(K_NOP);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000933
934#ifdef FEAT_NETBEANS_INTG
935 if (usingNetbeans)
936 /* Tell the client that it can start sending commands. */
937 netbeans_startup_done();
938#endif
939
940 TIME_MSG("before starting main loop");
941
942 /*
943 * Call the main command loop. This never returns.
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000944 * For embedded MzScheme the main_loop will be called by Scheme
945 * for proper stack tracking
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000946 */
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000947#ifndef FEAT_MZSCHEME
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000948 main_loop(FALSE, FALSE);
Bram Moolenaar9e70cf12009-05-26 20:59:55 +0000949#else
950 mzscheme_main();
951#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000952
953 return 0;
954}
955#endif /* PROTO */
956
957/*
958 * Main loop: Execute Normal mode commands until exiting Vim.
959 * Also used to handle commands in the command-line window, until the window
960 * is closed.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000961 * Also used to handle ":visual" command after ":global": execute Normal mode
962 * commands, return when entering Ex mode. "noexmode" is TRUE then.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000963 */
964 void
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000965main_loop(cmdwin, noexmode)
966 int cmdwin; /* TRUE when working in the command-line window */
967 int noexmode; /* TRUE when return on entering Ex mode */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000968{
Bram Moolenaar225d32b2007-08-10 19:33:47 +0000969 oparg_T oa; /* operator arguments */
970 int previous_got_int = FALSE; /* "got_int" was TRUE */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000971
972#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
973 /* Setup to catch a terminating error from the X server. Just ignore
974 * it, restore the state and continue. This might not always work
975 * properly, but at least we don't exit unexpectedly when the X server
976 * exists while Vim is running in a console. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000977 if (!cmdwin && !noexmode && SETJMP(x_jump_env))
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000978 {
979 State = NORMAL;
980# ifdef FEAT_VISUAL
981 VIsual_active = FALSE;
982# endif
983 got_int = TRUE;
984 need_wait_return = FALSE;
985 global_busy = FALSE;
986 exmode_active = 0;
987 skip_redraw = FALSE;
988 RedrawingDisabled = 0;
989 no_wait_return = 0;
990# ifdef FEAT_EVAL
991 emsg_skip = 0;
992# endif
993 emsg_off = 0;
994# ifdef FEAT_MOUSE
995 setmouse();
996# endif
997 settmode(TMODE_RAW);
998 starttermcap();
999 scroll_start();
1000 redraw_later_clear();
1001 }
1002#endif
1003
1004 clear_oparg(&oa);
1005 while (!cmdwin
1006#ifdef FEAT_CMDWIN
1007 || cmdwin_result == 0
1008#endif
1009 )
1010 {
1011 if (stuff_empty())
1012 {
1013 did_check_timestamps = FALSE;
1014 if (need_check_timestamps)
1015 check_timestamps(FALSE);
1016 if (need_wait_return) /* if wait_return still needed ... */
1017 wait_return(FALSE); /* ... call it now */
1018 if (need_start_insertmode && goto_im()
1019#ifdef FEAT_VISUAL
1020 && !VIsual_active
1021#endif
1022 )
1023 {
1024 need_start_insertmode = FALSE;
1025 stuffReadbuff((char_u *)"i"); /* start insert mode next */
1026 /* skip the fileinfo message now, because it would be shown
1027 * after insert mode finishes! */
1028 need_fileinfo = FALSE;
1029 }
1030 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001031
1032 /* Reset "got_int" now that we got back to the main loop. Except when
1033 * inside a ":g/pat/cmd" command, then the "got_int" needs to abort
1034 * the ":g" command.
1035 * For ":g/pat/vi" we reset "got_int" when used once. When used
1036 * a second time we go back to Ex mode and abort the ":g" command. */
1037 if (got_int)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001038 {
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001039 if (noexmode && global_busy && !exmode_active && previous_got_int)
1040 {
1041 /* Typed two CTRL-C in a row: go back to ex mode as if "Q" was
1042 * used and keep "got_int" set, so that it aborts ":g". */
1043 exmode_active = EXMODE_NORMAL;
1044 State = NORMAL;
1045 }
1046 else if (!global_busy || !exmode_active)
1047 {
1048 if (!quit_more)
1049 (void)vgetc(); /* flush all buffers */
1050 got_int = FALSE;
1051 }
1052 previous_got_int = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001053 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001054 else
1055 previous_got_int = FALSE;
1056
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001057 if (!exmode_active)
1058 msg_scroll = FALSE;
1059 quit_more = FALSE;
1060
1061 /*
1062 * If skip redraw is set (for ":" in wait_return()), don't redraw now.
1063 * If there is nothing in the stuff_buffer or do_redraw is TRUE,
1064 * update cursor and redraw.
1065 */
1066 if (skip_redraw || exmode_active)
1067 skip_redraw = FALSE;
1068 else if (do_redraw || stuff_empty())
1069 {
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001070#ifdef FEAT_AUTOCMD
1071 /* Trigger CursorMoved if the cursor moved. */
1072 if (!finish_op && has_cursormoved()
1073 && !equalpos(last_cursormoved, curwin->w_cursor))
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001074 {
1075 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL, FALSE, curbuf);
1076 last_cursormoved = curwin->w_cursor;
1077 }
1078#endif
1079
Bram Moolenaar33aec762006-01-22 23:30:12 +00001080#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
1081 /* Scroll-binding for diff mode may have been postponed until
1082 * here. Avoids doing it for every change. */
1083 if (diff_need_scrollbind)
1084 {
1085 check_scrollbind((linenr_T)0, 0L);
1086 diff_need_scrollbind = FALSE;
1087 }
1088#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001089#if defined(FEAT_FOLDING) && defined(FEAT_VISUAL)
1090 /* Include a closed fold completely in the Visual area. */
1091 foldAdjustVisual();
1092#endif
1093#ifdef FEAT_FOLDING
1094 /*
1095 * When 'foldclose' is set, apply 'foldlevel' to folds that don't
1096 * contain the cursor.
1097 * When 'foldopen' is "all", open the fold(s) under the cursor.
1098 * This may mark the window for redrawing.
1099 */
1100 if (hasAnyFolding(curwin) && !char_avail())
1101 {
1102 foldCheckClose();
1103 if (fdo_flags & FDO_ALL)
1104 foldOpenCursor();
1105 }
1106#endif
1107
1108 /*
1109 * Before redrawing, make sure w_topline is correct, and w_leftcol
1110 * if lines don't wrap, and w_skipcol if lines wrap.
1111 */
1112 update_topline();
1113 validate_cursor();
1114
1115#ifdef FEAT_VISUAL
1116 if (VIsual_active)
1117 update_curbuf(INVERTED);/* update inverted part */
1118 else
1119#endif
1120 if (must_redraw)
1121 update_screen(0);
1122 else if (redraw_cmdline || clear_cmdline)
1123 showmode();
1124#ifdef FEAT_WINDOWS
1125 redraw_statuslines();
1126#endif
1127#ifdef FEAT_TITLE
1128 if (need_maketitle)
1129 maketitle();
1130#endif
1131 /* display message after redraw */
1132 if (keep_msg != NULL)
1133 {
1134 char_u *p;
1135
1136 /* msg_attr_keep() will set keep_msg to NULL, must free the
1137 * string here. */
1138 p = keep_msg;
Bram Moolenaar238a5642006-02-21 22:12:05 +00001139 keep_msg = NULL;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001140 msg_attr(p, keep_msg_attr);
1141 vim_free(p);
1142 }
1143 if (need_fileinfo) /* show file info after redraw */
1144 {
1145 fileinfo(FALSE, TRUE, FALSE);
1146 need_fileinfo = FALSE;
1147 }
1148
1149 emsg_on_display = FALSE; /* can delete error message now */
1150 did_emsg = FALSE;
1151 msg_didany = FALSE; /* reset lines_left in msg_start() */
Bram Moolenaar661b1822005-07-28 22:36:45 +00001152 may_clear_sb_text(); /* clear scroll-back text on next msg */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001153 showruler(FALSE);
1154
1155 setcursor();
1156 cursor_on();
1157
1158 do_redraw = FALSE;
Bram Moolenaar3f269672009-11-03 11:11:11 +00001159
1160#ifdef STARTUPTIME
1161 /* Now that we have drawn the first screen all the startup stuff
1162 * has been done, close any file for startup messages. */
1163 if (time_fd != NULL)
1164 {
1165 TIME_MSG("first screen update");
1166 TIME_MSG("--- VIM STARTED ---");
1167 fclose(time_fd);
1168 time_fd = NULL;
1169 }
1170#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001171 }
1172#ifdef FEAT_GUI
1173 if (need_mouse_correct)
1174 gui_mouse_correct();
1175#endif
1176
1177 /*
1178 * Update w_curswant if w_set_curswant has been set.
1179 * Postponed until here to avoid computing w_virtcol too often.
1180 */
1181 update_curswant();
1182
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001183#ifdef FEAT_EVAL
1184 /*
1185 * May perform garbage collection when waiting for a character, but
1186 * only at the very toplevel. Otherwise we may be using a List or
1187 * Dict internally somewhere.
1188 * "may_garbage_collect" is reset in vgetc() which is invoked through
1189 * do_exmode() and normal_cmd().
1190 */
1191 may_garbage_collect = (!cmdwin && !noexmode);
1192#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001193 /*
1194 * If we're invoked as ex, do a round of ex commands.
1195 * Otherwise, get and execute a normal mode command.
1196 */
1197 if (exmode_active)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001198 {
1199 if (noexmode) /* End of ":global/path/visual" commands */
1200 return;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001201 do_exmode(exmode_active == EXMODE_VIM);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001202 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001203 else
1204 normal_cmd(&oa, TRUE);
1205 }
1206}
1207
1208
1209#if defined(USE_XSMP) || defined(FEAT_GUI_MSWIN) || defined(PROTO)
1210/*
1211 * Exit, but leave behind swap files for modified buffers.
1212 */
1213 void
1214getout_preserve_modified(exitval)
1215 int exitval;
1216{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001217# if defined(SIGHUP) && defined(SIG_IGN)
1218 /* Ignore SIGHUP, because a dropped connection causes a read error, which
1219 * makes Vim exit and then handling SIGHUP causes various reentrance
1220 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001221 signal(SIGHUP, SIG_IGN);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001222# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001223
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001224 ml_close_notmod(); /* close all not-modified buffers */
1225 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
1226 ml_close_all(FALSE); /* close all memfiles, without deleting */
1227 getout(exitval); /* exit Vim properly */
1228}
1229#endif
1230
1231
1232/* Exit properly */
1233 void
1234getout(exitval)
1235 int exitval;
1236{
1237#ifdef FEAT_AUTOCMD
1238 buf_T *buf;
1239 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001240 tabpage_T *tp, *next_tp;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001241#endif
1242
1243 exiting = TRUE;
1244
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001245 /* When running in Ex mode an error causes us to exit with a non-zero exit
1246 * code. POSIX requires this, although it's not 100% clear from the
1247 * standard. */
1248 if (exmode_active)
1249 exitval += ex_exitval;
1250
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001251 /* Position the cursor on the last screen line, below all the text */
1252#ifdef FEAT_GUI
1253 if (!gui.in_use)
1254#endif
1255 windgoto((int)Rows - 1, 0);
1256
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +00001257#if defined(FEAT_EVAL) || defined(FEAT_SYN_HL)
1258 /* Optionally print hashtable efficiency. */
1259 hash_debug_results();
1260#endif
1261
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001262#ifdef FEAT_GUI
1263 msg_didany = FALSE;
1264#endif
1265
1266#ifdef FEAT_AUTOCMD
1267 /* Trigger BufWinLeave for all windows, but only once per buffer. */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001268# if defined FEAT_WINDOWS
1269 for (tp = first_tabpage; tp != NULL; tp = next_tp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001270 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001271 next_tp = tp->tp_next;
Bram Moolenaar238a5642006-02-21 22:12:05 +00001272 for (wp = (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001273 ? firstwin : tp->tp_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001274 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001275 buf = wp->w_buffer;
1276 if (buf->b_changedtick != -1)
1277 {
1278 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001279 FALSE, buf);
Bram Moolenaarf740b292006-02-16 22:11:02 +00001280 buf->b_changedtick = -1; /* note that we did it already */
1281 /* start all over, autocommands may mess up the lists */
1282 next_tp = first_tabpage;
1283 break;
1284 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001285 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001286 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001287# else
1288 apply_autocmds(EVENT_BUFWINLEAVE, curbuf, curbuf->b_fname, FALSE, curbuf);
1289# endif
Bram Moolenaar33aec762006-01-22 23:30:12 +00001290
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001291 /* Trigger BufUnload for buffers that are loaded */
1292 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1293 if (buf->b_ml.ml_mfp != NULL)
1294 {
1295 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
1296 FALSE, buf);
1297 if (!buf_valid(buf)) /* autocmd may delete the buffer */
1298 break;
1299 }
1300 apply_autocmds(EVENT_VIMLEAVEPRE, NULL, NULL, FALSE, curbuf);
1301#endif
1302
1303#ifdef FEAT_VIMINFO
1304 if (*p_viminfo != NUL)
1305 /* Write out the registers, history, marks etc, to the viminfo file */
1306 write_viminfo(NULL, FALSE);
1307#endif
1308
1309#ifdef FEAT_AUTOCMD
1310 apply_autocmds(EVENT_VIMLEAVE, NULL, NULL, FALSE, curbuf);
1311#endif
1312
Bram Moolenaar05159a02005-02-26 23:04:13 +00001313#ifdef FEAT_PROFILE
1314 profile_dump();
1315#endif
1316
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001317 if (did_emsg
1318#ifdef FEAT_GUI
1319 || (gui.in_use && msg_didany && p_verbose > 0)
1320#endif
1321 )
1322 {
1323 /* give the user a chance to read the (error) message */
1324 no_wait_return = FALSE;
1325 wait_return(FALSE);
1326 }
1327
1328#ifdef FEAT_AUTOCMD
1329 /* Position the cursor again, the autocommands may have moved it */
1330# ifdef FEAT_GUI
1331 if (!gui.in_use)
1332# endif
1333 windgoto((int)Rows - 1, 0);
1334#endif
1335
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001336#ifdef FEAT_MZSCHEME
1337 mzscheme_end();
1338#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001339#ifdef FEAT_TCL
1340 tcl_end();
1341#endif
1342#ifdef FEAT_RUBY
1343 ruby_end();
1344#endif
1345#ifdef FEAT_PYTHON
1346 python_end();
1347#endif
1348#ifdef FEAT_PERL
1349 perl_end();
1350#endif
1351#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
1352 iconv_end();
1353#endif
1354#ifdef FEAT_NETBEANS_INTG
1355 netbeans_end();
1356#endif
Bram Moolenaar02b06312007-09-06 15:39:22 +00001357#ifdef FEAT_CSCOPE
1358 cs_end();
1359#endif
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001360#ifdef FEAT_EVAL
1361 if (garbage_collect_at_exit)
1362 garbage_collect();
1363#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001364
1365 mch_exit(exitval);
1366}
1367
1368/*
1369 * Get a (optional) count for a Vim argument.
1370 */
1371 static int
1372get_number_arg(p, idx, def)
1373 char_u *p; /* pointer to argument */
1374 int *idx; /* index in argument, is incremented */
1375 int def; /* default value */
1376{
1377 if (vim_isdigit(p[*idx]))
1378 {
1379 def = atoi((char *)&(p[*idx]));
1380 while (vim_isdigit(p[*idx]))
1381 *idx = *idx + 1;
1382 }
1383 return def;
1384}
1385
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001386#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1387/*
1388 * Setup to use the current locale (for ctype() and many other things).
1389 */
1390 static void
1391init_locale()
1392{
1393 setlocale(LC_ALL, "");
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001394
1395# if defined(FEAT_FLOAT) && defined(LC_NUMERIC)
1396 /* Make sure strtod() uses a decimal point, not a comma. */
1397 setlocale(LC_NUMERIC, "C");
1398# endif
1399
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001400# ifdef WIN32
1401 /* Apparently MS-Windows printf() may cause a crash when we give it 8-bit
1402 * text while it's expecting text in the current locale. This call avoids
1403 * that. */
1404 setlocale(LC_CTYPE, "C");
1405# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001406
1407# ifdef FEAT_GETTEXT
1408 {
1409 int mustfree = FALSE;
1410 char_u *p;
1411
1412# ifdef DYNAMIC_GETTEXT
1413 /* Initialize the gettext library */
1414 dyn_libintl_init(NULL);
1415# endif
1416 /* expand_env() doesn't work yet, because chartab[] is not initialized
1417 * yet, call vim_getenv() directly */
1418 p = vim_getenv((char_u *)"VIMRUNTIME", &mustfree);
1419 if (p != NULL && *p != NUL)
1420 {
Bram Moolenaar1ad2f132007-06-19 18:27:18 +00001421 vim_snprintf((char *)NameBuff, MAXPATHL, "%s/lang", p);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001422 bindtextdomain(VIMPACKAGE, (char *)NameBuff);
1423 }
1424 if (mustfree)
1425 vim_free(p);
1426 textdomain(VIMPACKAGE);
1427 }
1428# endif
1429}
1430#endif
1431
1432/*
1433 * Check for: [r][e][g][vi|vim|view][diff][ex[im]]
1434 * If the executable name starts with "r" we disable shell commands.
1435 * If the next character is "e" we run in Easy mode.
1436 * If the next character is "g" we run the GUI version.
1437 * If the next characters are "view" we start in readonly mode.
1438 * If the next characters are "diff" or "vimdiff" we start in diff mode.
1439 * If the next characters are "ex" we start in Ex mode. If it's followed
1440 * by "im" use improved Ex mode.
1441 */
1442 static void
1443parse_command_name(parmp)
1444 mparm_T *parmp;
1445{
1446 char_u *initstr;
1447
1448 initstr = gettail((char_u *)parmp->argv[0]);
1449
1450#ifdef MACOS_X_UNIX
1451 /* An issue has been seen when launching Vim in such a way that
1452 * $PWD/$ARGV[0] or $ARGV[0] is not the absolute path to the
1453 * executable or a symbolic link of it. Until this issue is resolved
1454 * we prohibit the GUI from being used.
1455 */
1456 if (STRCMP(initstr, parmp->argv[0]) == 0)
1457 disallow_gui = TRUE;
1458
1459 /* TODO: On MacOS X default to gui if argv[0] ends in:
Bram Moolenaar27dc1952006-03-15 23:06:44 +00001460 * /Vim.app/Contents/MacOS/Vim */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001461#endif
1462
1463#ifdef FEAT_EVAL
1464 set_vim_var_string(VV_PROGNAME, initstr, -1);
1465#endif
1466
1467 if (TOLOWER_ASC(initstr[0]) == 'r')
1468 {
1469 restricted = TRUE;
1470 ++initstr;
1471 }
1472
1473 /* Avoid using evim mode for "editor". */
1474 if (TOLOWER_ASC(initstr[0]) == 'e'
1475 && (TOLOWER_ASC(initstr[1]) == 'v'
1476 || TOLOWER_ASC(initstr[1]) == 'g'))
1477 {
1478#ifdef FEAT_GUI
1479 gui.starting = TRUE;
1480#endif
1481 parmp->evim_mode = TRUE;
1482 ++initstr;
1483 }
1484
Bram Moolenaar806875d2008-09-18 18:57:10 +00001485 /* "gvim" starts the GUI. Also accept "Gvim" for MS-Windows. */
1486 if (TOLOWER_ASC(initstr[0]) == 'g')
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001487 {
1488 main_start_gui();
1489#ifdef FEAT_GUI
1490 ++initstr;
1491#endif
1492 }
1493
1494 if (STRNICMP(initstr, "view", 4) == 0)
1495 {
1496 readonlymode = TRUE;
1497 curbuf->b_p_ro = TRUE;
1498 p_uc = 10000; /* don't update very often */
1499 initstr += 4;
1500 }
1501 else if (STRNICMP(initstr, "vim", 3) == 0)
1502 initstr += 3;
1503
1504 /* Catch "[r][g]vimdiff" and "[r][g]viewdiff". */
1505 if (STRICMP(initstr, "diff") == 0)
1506 {
1507#ifdef FEAT_DIFF
1508 parmp->diff_mode = TRUE;
1509#else
1510 mch_errmsg(_("This Vim was not compiled with the diff feature."));
1511 mch_errmsg("\n");
1512 mch_exit(2);
1513#endif
1514 }
1515
1516 if (STRNICMP(initstr, "ex", 2) == 0)
1517 {
1518 if (STRNICMP(initstr + 2, "im", 2) == 0)
1519 exmode_active = EXMODE_VIM;
1520 else
1521 exmode_active = EXMODE_NORMAL;
1522 change_compatible(TRUE); /* set 'compatible' */
1523 }
1524}
1525
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001526/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00001527 * Get the name of the display, before gui_prepare() removes it from
1528 * argv[]. Used for the xterm-clipboard display.
1529 *
Bram Moolenaar78e17622007-08-30 10:26:19 +00001530 * Also find the --server... arguments and --socketid and --windowid
Bram Moolenaar58d98232005-07-23 22:25:46 +00001531 */
Bram Moolenaar58d98232005-07-23 22:25:46 +00001532 static void
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001533early_arg_scan(parmp)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00001534 mparm_T *parmp UNUSED;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001535{
Bram Moolenaar03005972008-11-20 13:12:36 +00001536#if defined(FEAT_XCLIPBOARD) || defined(FEAT_CLIENTSERVER) \
1537 || !defined(FEAT_NETBEANS_INTG)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001538 int argc = parmp->argc;
1539 char **argv = parmp->argv;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001540 int i;
1541
1542 for (i = 1; i < argc; i++)
1543 {
1544 if (STRCMP(argv[i], "--") == 0)
1545 break;
1546# ifdef FEAT_XCLIPBOARD
1547 else if (STRICMP(argv[i], "-display") == 0
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001548# if defined(FEAT_GUI_GTK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001549 || STRICMP(argv[i], "--display") == 0
1550# endif
1551 )
1552 {
1553 if (i == argc - 1)
1554 mainerr_arg_missing((char_u *)argv[i]);
1555 xterm_display = argv[++i];
1556 }
1557# endif
1558# ifdef FEAT_CLIENTSERVER
1559 else if (STRICMP(argv[i], "--servername") == 0)
1560 {
1561 if (i == argc - 1)
1562 mainerr_arg_missing((char_u *)argv[i]);
1563 parmp->serverName_arg = (char_u *)argv[++i];
1564 }
Bram Moolenaareb94e552006-03-11 21:35:11 +00001565 else if (STRICMP(argv[i], "--serverlist") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001566 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001567 else if (STRNICMP(argv[i], "--remote", 8) == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001568 {
1569 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001570# ifdef FEAT_GUI
1571 if (strstr(argv[i], "-wait") != 0)
1572 /* don't fork() when starting the GUI to edit files ourself */
1573 gui.dofork = FALSE;
1574# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001575 }
1576# endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001577
1578# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1579# ifdef FEAT_GUI_W32
1580 else if (STRICMP(argv[i], "--windowid") == 0)
1581# else
Bram Moolenaar58d98232005-07-23 22:25:46 +00001582 else if (STRICMP(argv[i], "--socketid") == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001583# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001584 {
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001585 long_u id;
1586 int count;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001587
1588 if (i == argc - 1)
1589 mainerr_arg_missing((char_u *)argv[i]);
1590 if (STRNICMP(argv[i+1], "0x", 2) == 0)
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001591 count = sscanf(&(argv[i + 1][2]), SCANF_HEX_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001592 else
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001593 count = sscanf(argv[i + 1], SCANF_DECIMAL_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001594 if (count != 1)
1595 mainerr(ME_INVALID_ARG, (char_u *)argv[i]);
1596 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00001597# ifdef FEAT_GUI_W32
1598 win_socket_id = id;
1599# else
1600 gtk_socket_id = id;
1601# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001602 i++;
1603 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001604# endif
1605# ifdef FEAT_GUI_GTK
Bram Moolenaar58d98232005-07-23 22:25:46 +00001606 else if (STRICMP(argv[i], "--echo-wid") == 0)
1607 echo_wid_arg = TRUE;
1608# endif
Bram Moolenaar03005972008-11-20 13:12:36 +00001609# ifndef FEAT_NETBEANS_INTG
1610 else if (strncmp(argv[i], "-nb", (size_t)3) == 0)
1611 {
1612 mch_errmsg(_("'-nb' cannot be used: not enabled at compile time\n"));
1613 mch_exit(2);
1614 }
1615# endif
1616
Bram Moolenaar58d98232005-07-23 22:25:46 +00001617 }
1618#endif
1619}
1620
1621/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001622 * Scan the command line arguments.
1623 */
1624 static void
1625command_line_scan(parmp)
1626 mparm_T *parmp;
1627{
1628 int argc = parmp->argc;
1629 char **argv = parmp->argv;
1630 int argv_idx; /* index in argv[n][] */
1631 int had_minmin = FALSE; /* found "--" argument */
1632 int want_argument; /* option argument with argument */
1633 int c;
Bram Moolenaar231334e2005-07-25 20:46:57 +00001634 char_u *p = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001635 long n;
1636
1637 --argc;
1638 ++argv;
1639 argv_idx = 1; /* active option letter is argv[0][argv_idx] */
1640 while (argc > 0)
1641 {
1642 /*
1643 * "+" or "+{number}" or "+/{pat}" or "+{command}" argument.
1644 */
1645 if (argv[0][0] == '+' && !had_minmin)
1646 {
1647 if (parmp->n_commands >= MAX_ARG_CMDS)
1648 mainerr(ME_EXTRA_CMD, NULL);
1649 argv_idx = -1; /* skip to next argument */
1650 if (argv[0][1] == NUL)
1651 parmp->commands[parmp->n_commands++] = (char_u *)"$";
1652 else
1653 parmp->commands[parmp->n_commands++] = (char_u *)&(argv[0][1]);
1654 }
1655
1656 /*
1657 * Optional argument.
1658 */
1659 else if (argv[0][0] == '-' && !had_minmin)
1660 {
1661 want_argument = FALSE;
1662 c = argv[0][argv_idx++];
1663#ifdef VMS
1664 /*
1665 * VMS only uses upper case command lines. Interpret "-X" as "-x"
1666 * and "-/X" as "-X".
1667 */
1668 if (c == '/')
1669 {
1670 c = argv[0][argv_idx++];
1671 c = TOUPPER_ASC(c);
1672 }
1673 else
1674 c = TOLOWER_ASC(c);
1675#endif
1676 switch (c)
1677 {
1678 case NUL: /* "vim -" read from stdin */
1679 /* "ex -" silent mode */
1680 if (exmode_active)
1681 silent_mode = TRUE;
1682 else
1683 {
1684 if (parmp->edit_type != EDIT_NONE)
1685 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1686 parmp->edit_type = EDIT_STDIN;
1687 read_cmd_fd = 2; /* read from stderr instead of stdin */
1688 }
1689 argv_idx = -1; /* skip to next argument */
1690 break;
1691
1692 case '-': /* "--" don't take any more option arguments */
1693 /* "--help" give help message */
1694 /* "--version" give version message */
1695 /* "--literal" take files literally */
1696 /* "--nofork" don't fork */
1697 /* "--noplugin[s]" skip plugins */
1698 /* "--cmd <cmd>" execute cmd before vimrc */
1699 if (STRICMP(argv[0] + argv_idx, "help") == 0)
1700 usage();
1701 else if (STRICMP(argv[0] + argv_idx, "version") == 0)
1702 {
1703 Columns = 80; /* need to init Columns */
1704 info_message = TRUE; /* use mch_msg(), not mch_errmsg() */
1705 list_version();
1706 msg_putchar('\n');
1707 msg_didout = FALSE;
1708 mch_exit(0);
1709 }
1710 else if (STRNICMP(argv[0] + argv_idx, "literal", 7) == 0)
1711 {
1712#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
1713 parmp->literal = TRUE;
1714#endif
1715 }
1716 else if (STRNICMP(argv[0] + argv_idx, "nofork", 6) == 0)
1717 {
1718#ifdef FEAT_GUI
1719 gui.dofork = FALSE; /* don't fork() when starting GUI */
1720#endif
1721 }
1722 else if (STRNICMP(argv[0] + argv_idx, "noplugin", 8) == 0)
1723 p_lpl = FALSE;
1724 else if (STRNICMP(argv[0] + argv_idx, "cmd", 3) == 0)
1725 {
1726 want_argument = TRUE;
1727 argv_idx += 3;
1728 }
1729#ifdef FEAT_CLIENTSERVER
1730 else if (STRNICMP(argv[0] + argv_idx, "serverlist", 10) == 0)
1731 ; /* already processed -- no arg */
1732 else if (STRNICMP(argv[0] + argv_idx, "servername", 10) == 0
1733 || STRNICMP(argv[0] + argv_idx, "serversend", 10) == 0)
1734 {
1735 /* already processed -- snatch the following arg */
1736 if (argc > 1)
1737 {
1738 --argc;
1739 ++argv;
1740 }
1741 }
1742#endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001743#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1744# ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001745 else if (STRNICMP(argv[0] + argv_idx, "socketid", 8) == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001746# else
1747 else if (STRNICMP(argv[0] + argv_idx, "windowid", 8) == 0)
1748# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001749 {
1750 /* already processed -- snatch the following arg */
1751 if (argc > 1)
1752 {
1753 --argc;
1754 ++argv;
1755 }
1756 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001757#endif
1758#ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001759 else if (STRNICMP(argv[0] + argv_idx, "echo-wid", 8) == 0)
1760 {
1761 /* already processed, skip */
1762 }
1763#endif
Bram Moolenaar3f269672009-11-03 11:11:11 +00001764 else if (STRNICMP(argv[0] + argv_idx, "startuptime", 11) == 0)
1765 {
1766 /* already processed, skip */
1767 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001768 else
1769 {
1770 if (argv[0][argv_idx])
1771 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
1772 had_minmin = TRUE;
1773 }
1774 if (!want_argument)
1775 argv_idx = -1; /* skip to next argument */
1776 break;
1777
1778 case 'A': /* "-A" start in Arabic mode */
1779#ifdef FEAT_ARABIC
1780 set_option_value((char_u *)"arabic", 1L, NULL, 0);
1781#else
1782 mch_errmsg(_(e_noarabic));
1783 mch_exit(2);
1784#endif
1785 break;
1786
1787 case 'b': /* "-b" binary mode */
Bram Moolenaar231334e2005-07-25 20:46:57 +00001788 /* Needs to be effective before expanding file names, because
1789 * for Win32 this makes us edit a shortcut file itself,
1790 * instead of the file it links to. */
1791 set_options_bin(curbuf->b_p_bin, 1, 0);
1792 curbuf->b_p_bin = 1; /* binary file I/O */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001793 break;
1794
1795 case 'C': /* "-C" Compatible */
1796 change_compatible(TRUE);
1797 break;
1798
1799 case 'e': /* "-e" Ex mode */
1800 exmode_active = EXMODE_NORMAL;
1801 break;
1802
1803 case 'E': /* "-E" Improved Ex mode */
1804 exmode_active = EXMODE_VIM;
1805 break;
1806
1807 case 'f': /* "-f" GUI: run in foreground. Amiga: open
1808 window directly, not with newcli */
1809#ifdef FEAT_GUI
1810 gui.dofork = FALSE; /* don't fork() when starting GUI */
1811#endif
1812 break;
1813
1814 case 'g': /* "-g" start GUI */
1815 main_start_gui();
1816 break;
1817
1818 case 'F': /* "-F" start in Farsi mode: rl + fkmap set */
1819#ifdef FEAT_FKMAP
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00001820 p_fkmap = TRUE;
1821 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001822#else
1823 mch_errmsg(_(e_nofarsi));
1824 mch_exit(2);
1825#endif
1826 break;
1827
1828 case 'h': /* "-h" give help message */
1829#ifdef FEAT_GUI_GNOME
1830 /* Tell usage() to exit for "gvim". */
1831 gui.starting = FALSE;
1832#endif
1833 usage();
1834 break;
1835
1836 case 'H': /* "-H" start in Hebrew mode: rl + hkmap set */
1837#ifdef FEAT_RIGHTLEFT
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00001838 p_hkmap = TRUE;
1839 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001840#else
1841 mch_errmsg(_(e_nohebrew));
1842 mch_exit(2);
1843#endif
1844 break;
1845
1846 case 'l': /* "-l" lisp mode, 'lisp' and 'showmatch' on */
1847#ifdef FEAT_LISP
1848 set_option_value((char_u *)"lisp", 1L, NULL, 0);
1849 p_sm = TRUE;
1850#endif
1851 break;
1852
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001853 case 'M': /* "-M" no changes or writing of files */
1854 reset_modifiable();
1855 /* FALLTHROUGH */
1856
1857 case 'm': /* "-m" no writing of files */
1858 p_write = FALSE;
1859 break;
1860
1861 case 'y': /* "-y" easy mode */
1862#ifdef FEAT_GUI
1863 gui.starting = TRUE; /* start GUI a bit later */
1864#endif
1865 parmp->evim_mode = TRUE;
1866 break;
1867
1868 case 'N': /* "-N" Nocompatible */
1869 change_compatible(FALSE);
1870 break;
1871
1872 case 'n': /* "-n" no swap file */
1873 parmp->no_swap_file = TRUE;
1874 break;
1875
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001876 case 'p': /* "-p[N]" open N tab pages */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00001877#ifdef TARGET_API_MAC_OSX
1878 /* For some reason on MacOS X, an argument like:
1879 -psn_0_10223617 is passed in when invoke from Finder
1880 or with the 'open' command */
1881 if (argv[0][argv_idx] == 's')
1882 {
1883 argv_idx = -1; /* bypass full -psn */
1884 main_start_gui();
1885 break;
1886 }
1887#endif
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001888#ifdef FEAT_WINDOWS
1889 /* default is 0: open window for each file */
1890 parmp->window_count = get_number_arg((char_u *)argv[0],
1891 &argv_idx, 0);
1892 parmp->window_layout = WIN_TABS;
1893#endif
1894 break;
1895
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001896 case 'o': /* "-o[N]" open N horizontal split windows */
1897#ifdef FEAT_WINDOWS
1898 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00001899 parmp->window_count = get_number_arg((char_u *)argv[0],
1900 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001901 parmp->window_layout = WIN_HOR;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001902#endif
1903 break;
1904
1905 case 'O': /* "-O[N]" open N vertical split windows */
1906#if defined(FEAT_VERTSPLIT) && defined(FEAT_WINDOWS)
1907 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00001908 parmp->window_count = get_number_arg((char_u *)argv[0],
1909 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001910 parmp->window_layout = WIN_VER;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001911#endif
1912 break;
1913
1914#ifdef FEAT_QUICKFIX
1915 case 'q': /* "-q" QuickFix mode */
1916 if (parmp->edit_type != EDIT_NONE)
1917 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1918 parmp->edit_type = EDIT_QF;
1919 if (argv[0][argv_idx]) /* "-q{errorfile}" */
1920 {
1921 parmp->use_ef = (char_u *)argv[0] + argv_idx;
1922 argv_idx = -1;
1923 }
1924 else if (argc > 1) /* "-q {errorfile}" */
1925 want_argument = TRUE;
1926 break;
1927#endif
1928
1929 case 'R': /* "-R" readonly mode */
1930 readonlymode = TRUE;
1931 curbuf->b_p_ro = TRUE;
1932 p_uc = 10000; /* don't update very often */
1933 break;
1934
1935 case 'r': /* "-r" recovery mode */
1936 case 'L': /* "-L" recovery mode */
1937 recoverymode = 1;
1938 break;
1939
1940 case 's':
1941 if (exmode_active) /* "-s" silent (batch) mode */
1942 silent_mode = TRUE;
1943 else /* "-s {scriptin}" read from script file */
1944 want_argument = TRUE;
1945 break;
1946
1947 case 't': /* "-t {tag}" or "-t{tag}" jump to tag */
1948 if (parmp->edit_type != EDIT_NONE)
1949 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1950 parmp->edit_type = EDIT_TAG;
1951 if (argv[0][argv_idx]) /* "-t{tag}" */
1952 {
1953 parmp->tagname = (char_u *)argv[0] + argv_idx;
1954 argv_idx = -1;
1955 }
1956 else /* "-t {tag}" */
1957 want_argument = TRUE;
1958 break;
1959
1960#ifdef FEAT_EVAL
1961 case 'D': /* "-D" Debugging */
1962 parmp->use_debug_break_level = 9999;
1963 break;
1964#endif
1965#ifdef FEAT_DIFF
1966 case 'd': /* "-d" 'diff' */
1967# ifdef AMIGA
1968 /* check for "-dev {device}" */
1969 if (argv[0][argv_idx] == 'e' && argv[0][argv_idx + 1] == 'v')
1970 want_argument = TRUE;
1971 else
1972# endif
1973 parmp->diff_mode = TRUE;
1974 break;
1975#endif
1976 case 'V': /* "-V{N}" Verbose level */
1977 /* default is 10: a little bit verbose */
1978 p_verbose = get_number_arg((char_u *)argv[0], &argv_idx, 10);
1979 if (argv[0][argv_idx] != NUL)
1980 {
1981 set_option_value((char_u *)"verbosefile", 0L,
1982 (char_u *)argv[0] + argv_idx, 0);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001983 argv_idx = (int)STRLEN(argv[0]);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001984 }
1985 break;
1986
1987 case 'v': /* "-v" Vi-mode (as if called "vi") */
1988 exmode_active = 0;
1989#ifdef FEAT_GUI
1990 gui.starting = FALSE; /* don't start GUI */
1991#endif
1992 break;
1993
1994 case 'w': /* "-w{number}" set window height */
1995 /* "-w {scriptout}" write to script */
1996 if (vim_isdigit(((char_u *)argv[0])[argv_idx]))
1997 {
1998 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
1999 set_option_value((char_u *)"window", n, NULL, 0);
2000 break;
2001 }
2002 want_argument = TRUE;
2003 break;
2004
2005#ifdef FEAT_CRYPT
2006 case 'x': /* "-x" encrypted reading/writing of files */
2007 parmp->ask_for_key = TRUE;
2008 break;
2009#endif
2010
2011 case 'X': /* "-X" don't connect to X server */
2012#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
2013 x_no_connect = TRUE;
2014#endif
2015 break;
2016
2017 case 'Z': /* "-Z" restricted mode */
2018 restricted = TRUE;
2019 break;
2020
2021 case 'c': /* "-c{command}" or "-c {command}" execute
2022 command */
2023 if (argv[0][argv_idx] != NUL)
2024 {
2025 if (parmp->n_commands >= MAX_ARG_CMDS)
2026 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar231334e2005-07-25 20:46:57 +00002027 parmp->commands[parmp->n_commands++] = (char_u *)argv[0]
2028 + argv_idx;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002029 argv_idx = -1;
2030 break;
2031 }
2032 /*FALLTHROUGH*/
2033 case 'S': /* "-S {file}" execute Vim script */
2034 case 'i': /* "-i {viminfo}" use for viminfo */
2035#ifndef FEAT_DIFF
2036 case 'd': /* "-d {device}" device (for Amiga) */
2037#endif
2038 case 'T': /* "-T {terminal}" terminal name */
2039 case 'u': /* "-u {vimrc}" vim inits file */
2040 case 'U': /* "-U {gvimrc}" gvim inits file */
2041 case 'W': /* "-W {scriptout}" overwrite */
2042#ifdef FEAT_GUI_W32
2043 case 'P': /* "-P {parent title}" MDI parent */
2044#endif
2045 want_argument = TRUE;
2046 break;
2047
2048 default:
2049 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
2050 }
2051
2052 /*
2053 * Handle option arguments with argument.
2054 */
2055 if (want_argument)
2056 {
2057 /*
2058 * Check for garbage immediately after the option letter.
2059 */
2060 if (argv[0][argv_idx] != NUL)
2061 mainerr(ME_GARBAGE, (char_u *)argv[0]);
2062
2063 --argc;
2064 if (argc < 1 && c != 'S')
2065 mainerr_arg_missing((char_u *)argv[0]);
2066 ++argv;
2067 argv_idx = -1;
2068
2069 switch (c)
2070 {
2071 case 'c': /* "-c {command}" execute command */
2072 case 'S': /* "-S {file}" execute Vim script */
2073 if (parmp->n_commands >= MAX_ARG_CMDS)
2074 mainerr(ME_EXTRA_CMD, NULL);
2075 if (c == 'S')
2076 {
2077 char *a;
2078
2079 if (argc < 1)
2080 /* "-S" without argument: use default session file
2081 * name. */
2082 a = SESSION_FILE;
2083 else if (argv[0][0] == '-')
2084 {
2085 /* "-S" followed by another option: use default
2086 * session file name. */
2087 a = SESSION_FILE;
2088 ++argc;
2089 --argv;
2090 }
2091 else
2092 a = argv[0];
2093 p = alloc((unsigned)(STRLEN(a) + 4));
2094 if (p == NULL)
2095 mch_exit(2);
2096 sprintf((char *)p, "so %s", a);
2097 parmp->cmds_tofree[parmp->n_commands] = TRUE;
2098 parmp->commands[parmp->n_commands++] = p;
2099 }
2100 else
Bram Moolenaar231334e2005-07-25 20:46:57 +00002101 parmp->commands[parmp->n_commands++] =
2102 (char_u *)argv[0];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002103 break;
2104
2105 case '-': /* "--cmd {command}" execute command */
2106 if (parmp->n_pre_commands >= MAX_ARG_CMDS)
2107 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar231334e2005-07-25 20:46:57 +00002108 parmp->pre_commands[parmp->n_pre_commands++] =
2109 (char_u *)argv[0];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002110 break;
2111
2112 /* case 'd': -d {device} is handled in mch_check_win() for the
2113 * Amiga */
2114
2115#ifdef FEAT_QUICKFIX
2116 case 'q': /* "-q {errorfile}" QuickFix mode */
2117 parmp->use_ef = (char_u *)argv[0];
2118 break;
2119#endif
2120
2121 case 'i': /* "-i {viminfo}" use for viminfo */
2122 use_viminfo = (char_u *)argv[0];
2123 break;
2124
2125 case 's': /* "-s {scriptin}" read from script file */
2126 if (scriptin[0] != NULL)
2127 {
2128scripterror:
2129 mch_errmsg(_("Attempt to open script file again: \""));
2130 mch_errmsg(argv[-1]);
2131 mch_errmsg(" ");
2132 mch_errmsg(argv[0]);
2133 mch_errmsg("\"\n");
2134 mch_exit(2);
2135 }
2136 if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL)
2137 {
2138 mch_errmsg(_("Cannot open for reading: \""));
2139 mch_errmsg(argv[0]);
2140 mch_errmsg("\"\n");
2141 mch_exit(2);
2142 }
2143 if (save_typebuf() == FAIL)
2144 mch_exit(2); /* out of memory */
2145 break;
2146
2147 case 't': /* "-t {tag}" */
2148 parmp->tagname = (char_u *)argv[0];
2149 break;
2150
2151 case 'T': /* "-T {terminal}" terminal name */
2152 /*
2153 * The -T term argument is always available and when
2154 * HAVE_TERMLIB is supported it overrides the environment
2155 * variable TERM.
2156 */
2157#ifdef FEAT_GUI
2158 if (term_is_gui((char_u *)argv[0]))
2159 gui.starting = TRUE; /* start GUI a bit later */
2160 else
2161#endif
2162 parmp->term = (char_u *)argv[0];
2163 break;
2164
2165 case 'u': /* "-u {vimrc}" vim inits file */
2166 parmp->use_vimrc = (char_u *)argv[0];
2167 break;
2168
2169 case 'U': /* "-U {gvimrc}" gvim inits file */
2170#ifdef FEAT_GUI
2171 use_gvimrc = (char_u *)argv[0];
2172#endif
2173 break;
2174
2175 case 'w': /* "-w {nr}" 'window' value */
2176 /* "-w {scriptout}" append to script file */
2177 if (vim_isdigit(*((char_u *)argv[0])))
2178 {
2179 argv_idx = 0;
2180 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2181 set_option_value((char_u *)"window", n, NULL, 0);
2182 argv_idx = -1;
2183 break;
2184 }
2185 /*FALLTHROUGH*/
2186 case 'W': /* "-W {scriptout}" overwrite script file */
2187 if (scriptout != NULL)
2188 goto scripterror;
2189 if ((scriptout = mch_fopen(argv[0],
2190 c == 'w' ? APPENDBIN : WRITEBIN)) == NULL)
2191 {
2192 mch_errmsg(_("Cannot open for script output: \""));
2193 mch_errmsg(argv[0]);
2194 mch_errmsg("\"\n");
2195 mch_exit(2);
2196 }
2197 break;
2198
2199#ifdef FEAT_GUI_W32
2200 case 'P': /* "-P {parent title}" MDI parent */
2201 gui_mch_set_parent(argv[0]);
2202 break;
2203#endif
2204 }
2205 }
2206 }
2207
2208 /*
2209 * File name argument.
2210 */
2211 else
2212 {
2213 argv_idx = -1; /* skip to next argument */
2214
2215 /* Check for only one type of editing. */
2216 if (parmp->edit_type != EDIT_NONE && parmp->edit_type != EDIT_FILE)
2217 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2218 parmp->edit_type = EDIT_FILE;
2219
2220#ifdef MSWIN
2221 /* Remember if the argument was a full path before changing
2222 * slashes to backslashes. */
2223 if (argv[0][0] != NUL && argv[0][1] == ':' && argv[0][2] == '\\')
2224 parmp->full_path = TRUE;
2225#endif
2226
2227 /* Add the file to the global argument list. */
2228 if (ga_grow(&global_alist.al_ga, 1) == FAIL
2229 || (p = vim_strsave((char_u *)argv[0])) == NULL)
2230 mch_exit(2);
2231#ifdef FEAT_DIFF
2232 if (parmp->diff_mode && mch_isdir(p) && GARGCOUNT > 0
2233 && !mch_isdir(alist_name(&GARGLIST[0])))
2234 {
2235 char_u *r;
2236
2237 r = concat_fnames(p, gettail(alist_name(&GARGLIST[0])), TRUE);
2238 if (r != NULL)
2239 {
2240 vim_free(p);
2241 p = r;
2242 }
2243 }
2244#endif
2245#if defined(__CYGWIN32__) && !defined(WIN32)
2246 /*
2247 * If vim is invoked by non-Cygwin tools, convert away any
2248 * DOS paths, so things like .swp files are created correctly.
2249 * Look for evidence of non-Cygwin paths before we bother.
2250 * This is only for when using the Unix files.
2251 */
2252 if (strpbrk(p, "\\:") != NULL)
2253 {
2254 char posix_path[PATH_MAX];
2255
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002256# if CYGWIN_VERSION_DLL_MAJOR >= 1007
2257 cygwin_conv_path(CCP_WIN_A_TO_POSIX, p, posix_path, PATH_MAX);
2258# else
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002259 cygwin_conv_to_posix_path(p, posix_path);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002260# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002261 vim_free(p);
2262 p = vim_strsave(posix_path);
2263 if (p == NULL)
2264 mch_exit(2);
2265 }
2266#endif
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002267
2268#ifdef USE_FNAME_CASE
2269 /* Make the case of the file name match the actual file. */
2270 fname_case(p, 0);
2271#endif
2272
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002273 alist_add(&global_alist, p,
2274#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
Bram Moolenaar231334e2005-07-25 20:46:57 +00002275 parmp->literal ? 2 : 0 /* add buffer nr after exp. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002276#else
2277 2 /* add buffer number now and use curbuf */
2278#endif
2279 );
2280
2281#if defined(FEAT_MBYTE) && defined(WIN32)
2282 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002283 /* Remember this argument has been added to the argument list.
2284 * Needed when 'encoding' is changed. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002285 used_file_arg(argv[0], parmp->literal, parmp->full_path,
Bram Moolenaar688e5f72008-07-24 11:51:40 +00002286# ifdef FEAT_DIFF
2287 parmp->diff_mode
2288# else
2289 FALSE
2290# endif
2291 );
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002292 }
2293#endif
2294 }
2295
2296 /*
2297 * If there are no more letters after the current "-", go to next
2298 * argument. argv_idx is set to -1 when the current argument is to be
2299 * skipped.
2300 */
2301 if (argv_idx <= 0 || argv[0][argv_idx] == NUL)
2302 {
2303 --argc;
2304 ++argv;
2305 argv_idx = 1;
2306 }
2307 }
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002308
2309#ifdef FEAT_EVAL
2310 /* If there is a "+123" or "-c" command, set v:swapcommand to the first
2311 * one. */
2312 if (parmp->n_commands > 0)
2313 {
2314 p = alloc((unsigned)STRLEN(parmp->commands[0]) + 3);
2315 if (p != NULL)
2316 {
2317 sprintf((char *)p, ":%s\r", parmp->commands[0]);
2318 set_vim_var_string(VV_SWAPCOMMAND, p, -1);
2319 vim_free(p);
2320 }
2321 }
2322#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002323}
2324
2325/*
2326 * Print a warning if stdout is not a terminal.
2327 * When starting in Ex mode and commands come from a file, set Silent mode.
2328 */
2329 static void
2330check_tty(parmp)
2331 mparm_T *parmp;
2332{
2333 int input_isatty; /* is active input a terminal? */
2334
2335 input_isatty = mch_input_isatty();
2336 if (exmode_active)
2337 {
2338 if (!input_isatty)
2339 silent_mode = TRUE;
2340 }
2341 else if (parmp->want_full_screen && (!parmp->stdout_isatty || !input_isatty)
2342#ifdef FEAT_GUI
2343 /* don't want the delay when started from the desktop */
2344 && !gui.starting
2345#endif
2346 )
2347 {
2348#ifdef NBDEBUG
2349 /*
2350 * This shouldn't be necessary. But if I run netbeans with the log
2351 * output coming to the console and XOpenDisplay fails, I get vim
2352 * trying to start with input/output to my console tty. This fills my
2353 * input buffer so fast I can't even kill the process in under 2
Bram Moolenaar49325942007-05-10 19:19:59 +00002354 * minutes (and it beeps continuously the whole time :-)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002355 */
2356 if (usingNetbeans && (!parmp->stdout_isatty || !input_isatty))
2357 {
2358 mch_errmsg(_("Vim: Error: Failure to start gvim from NetBeans\n"));
2359 exit(1);
2360 }
2361#endif
2362 if (!parmp->stdout_isatty)
2363 mch_errmsg(_("Vim: Warning: Output is not to a terminal\n"));
2364 if (!input_isatty)
2365 mch_errmsg(_("Vim: Warning: Input is not from a terminal\n"));
2366 out_flush();
2367 if (scriptin[0] == NULL)
2368 ui_delay(2000L, TRUE);
2369 TIME_MSG("Warning delay");
2370 }
2371}
2372
2373/*
2374 * Read text from stdin.
2375 */
2376 static void
2377read_stdin()
2378{
2379 int i;
2380
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002381#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002382 /* When getting the ATTENTION prompt here, use a dialog */
2383 swap_exists_action = SEA_DIALOG;
2384#endif
2385 no_wait_return = TRUE;
2386 i = msg_didany;
2387 set_buflisted(TRUE);
2388 (void)open_buffer(TRUE, NULL); /* create memfile and read file */
2389 no_wait_return = FALSE;
2390 msg_didany = i;
2391 TIME_MSG("reading stdin");
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002392#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002393 check_swap_exists_action();
2394#endif
2395#if !(defined(AMIGA) || defined(MACOS))
2396 /*
2397 * Close stdin and dup it from stderr. Required for GPM to work
2398 * properly, and for running external commands.
2399 * Is there any other system that cannot do this?
2400 */
2401 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00002402 ignored = dup(2);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002403#endif
2404}
2405
2406/*
2407 * Create the requested number of windows and edit buffers in them.
2408 * Also does recovery if "recoverymode" set.
2409 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002410 static void
2411create_windows(parmp)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002412 mparm_T *parmp UNUSED;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002413{
2414#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002415 int dorewind;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002416 int done = 0;
2417
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002418 /*
2419 * Create the number of windows that was requested.
2420 */
2421 if (parmp->window_count == -1) /* was not set */
2422 parmp->window_count = 1;
2423 if (parmp->window_count == 0)
2424 parmp->window_count = GARGCOUNT;
2425 if (parmp->window_count > 1)
2426 {
2427 /* Don't change the windows if there was a command in .vimrc that
2428 * already split some windows */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002429 if (parmp->window_layout == 0)
2430 parmp->window_layout = WIN_HOR;
2431 if (parmp->window_layout == WIN_TABS)
2432 {
2433 parmp->window_count = make_tabpages(parmp->window_count);
2434 TIME_MSG("making tab pages");
2435 }
2436 else if (firstwin->w_next == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002437 {
2438 parmp->window_count = make_windows(parmp->window_count,
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002439 parmp->window_layout == WIN_VER);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002440 TIME_MSG("making windows");
2441 }
2442 else
2443 parmp->window_count = win_count();
2444 }
2445 else
2446 parmp->window_count = 1;
2447#endif
2448
2449 if (recoverymode) /* do recover */
2450 {
2451 msg_scroll = TRUE; /* scroll message up */
2452 ml_recover();
2453 if (curbuf->b_ml.ml_mfp == NULL) /* failed */
2454 getout(1);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002455 do_modelines(0); /* do modelines */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002456 }
2457 else
2458 {
2459 /*
2460 * Open a buffer for windows that don't have one yet.
2461 * Commands in the .vimrc might have loaded a file or split the window.
2462 * Watch out for autocommands that delete a window.
2463 */
2464#ifdef FEAT_AUTOCMD
2465 /*
2466 * Don't execute Win/Buf Enter/Leave autocommands here
2467 */
2468 ++autocmd_no_enter;
2469 ++autocmd_no_leave;
2470#endif
2471#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002472 dorewind = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002473 while (done++ < 1000)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002474 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002475 if (dorewind)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002476 {
2477 if (parmp->window_layout == WIN_TABS)
2478 goto_tabpage(1);
2479 else
2480 curwin = firstwin;
2481 }
2482 else if (parmp->window_layout == WIN_TABS)
2483 {
2484 if (curtab->tp_next == NULL)
2485 break;
2486 goto_tabpage(0);
2487 }
2488 else
2489 {
2490 if (curwin->w_next == NULL)
2491 break;
2492 curwin = curwin->w_next;
2493 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002494 dorewind = FALSE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002495#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002496 curbuf = curwin->w_buffer;
2497 if (curbuf->b_ml.ml_mfp == NULL)
2498 {
2499#ifdef FEAT_FOLDING
2500 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2501 if (p_fdls >= 0)
2502 curwin->w_p_fdl = p_fdls;
2503#endif
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002504#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002505 /* When getting the ATTENTION prompt here, use a dialog */
2506 swap_exists_action = SEA_DIALOG;
2507#endif
2508 set_buflisted(TRUE);
2509 (void)open_buffer(FALSE, NULL); /* create memfile, read file */
2510
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002511#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar84212822006-11-07 21:59:47 +00002512 if (swap_exists_action == SEA_QUIT)
2513 {
2514 if (got_int || only_one_window())
2515 {
2516 /* abort selected or quit and only one window */
2517 did_emsg = FALSE; /* avoid hit-enter prompt */
2518 getout(1);
2519 }
2520 /* We can't close the window, it would disturb what
2521 * happens next. Clear the file name and set the arg
2522 * index to -1 to delete it later. */
2523 setfname(curbuf, NULL, NULL, FALSE);
2524 curwin->w_arg_idx = -1;
2525 swap_exists_action = SEA_NONE;
2526 }
2527 else
2528 handle_swap_exists(NULL);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002529#endif
2530#ifdef FEAT_AUTOCMD
Bram Moolenaar89d40322006-08-29 15:30:07 +00002531 dorewind = TRUE; /* start again */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002532#endif
2533 }
2534#ifdef FEAT_WINDOWS
2535 ui_breakcheck();
2536 if (got_int)
2537 {
2538 (void)vgetc(); /* only break the file loading, not the rest */
2539 break;
2540 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002541 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002542#endif
2543#ifdef FEAT_WINDOWS
2544 if (parmp->window_layout == WIN_TABS)
2545 goto_tabpage(1);
2546 else
2547 curwin = firstwin;
2548 curbuf = curwin->w_buffer;
2549#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002550#ifdef FEAT_AUTOCMD
2551 --autocmd_no_enter;
2552 --autocmd_no_leave;
2553#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002554 }
2555}
2556
2557#ifdef FEAT_WINDOWS
2558 /*
2559 * If opened more than one window, start editing files in the other
2560 * windows. make_windows() has already opened the windows.
2561 */
2562 static void
2563edit_buffers(parmp)
2564 mparm_T *parmp;
2565{
2566 int arg_idx; /* index in argument list */
2567 int i;
Bram Moolenaar84212822006-11-07 21:59:47 +00002568 int advance = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002569
2570# ifdef FEAT_AUTOCMD
2571 /*
2572 * Don't execute Win/Buf Enter/Leave autocommands here
2573 */
2574 ++autocmd_no_enter;
2575 ++autocmd_no_leave;
2576# endif
Bram Moolenaar84212822006-11-07 21:59:47 +00002577
2578 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2579 if (curwin->w_arg_idx == -1)
2580 {
2581 win_close(curwin, TRUE);
2582 advance = FALSE;
2583 }
2584
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002585 arg_idx = 1;
2586 for (i = 1; i < parmp->window_count; ++i)
2587 {
Bram Moolenaar84212822006-11-07 21:59:47 +00002588 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2589 if (curwin->w_arg_idx == -1)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002590 {
Bram Moolenaar84212822006-11-07 21:59:47 +00002591 ++arg_idx;
2592 win_close(curwin, TRUE);
2593 advance = FALSE;
2594 continue;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002595 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002596
Bram Moolenaar84212822006-11-07 21:59:47 +00002597 if (advance)
2598 {
2599 if (parmp->window_layout == WIN_TABS)
2600 {
2601 if (curtab->tp_next == NULL) /* just checking */
2602 break;
2603 goto_tabpage(0);
2604 }
2605 else
2606 {
2607 if (curwin->w_next == NULL) /* just checking */
2608 break;
2609 win_enter(curwin->w_next, FALSE);
2610 }
2611 }
2612 advance = TRUE;
2613
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002614 /* Only open the file if there is no file in this window yet (that can
Bram Moolenaar84212822006-11-07 21:59:47 +00002615 * happen when .vimrc contains ":sall"). */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002616 if (curbuf == firstwin->w_buffer || curbuf->b_ffname == NULL)
2617 {
2618 curwin->w_arg_idx = arg_idx;
Bram Moolenaar84212822006-11-07 21:59:47 +00002619 /* Edit file from arg list, if there is one. When "Quit" selected
2620 * at the ATTENTION prompt close the window. */
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002621# ifdef HAS_SWAP_EXISTS_ACTION
2622 swap_exists_did_quit = FALSE;
2623# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002624 (void)do_ecmd(0, arg_idx < GARGCOUNT
2625 ? alist_name(&GARGLIST[arg_idx]) : NULL,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002626 NULL, NULL, ECMD_LASTL, ECMD_HIDE, curwin);
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002627# ifdef HAS_SWAP_EXISTS_ACTION
2628 if (swap_exists_did_quit)
Bram Moolenaar84212822006-11-07 21:59:47 +00002629 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002630 /* abort or quit selected */
Bram Moolenaar84212822006-11-07 21:59:47 +00002631 if (got_int || only_one_window())
2632 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002633 /* abort selected and only one window */
Bram Moolenaar84212822006-11-07 21:59:47 +00002634 did_emsg = FALSE; /* avoid hit-enter prompt */
2635 getout(1);
2636 }
2637 win_close(curwin, TRUE);
2638 advance = FALSE;
2639 }
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002640# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002641 if (arg_idx == GARGCOUNT - 1)
2642 arg_had_last = TRUE;
2643 ++arg_idx;
2644 }
2645 ui_breakcheck();
2646 if (got_int)
2647 {
2648 (void)vgetc(); /* only break the file loading, not the rest */
2649 break;
2650 }
2651 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002652
2653 if (parmp->window_layout == WIN_TABS)
2654 goto_tabpage(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002655# ifdef FEAT_AUTOCMD
2656 --autocmd_no_enter;
2657# endif
2658 win_enter(firstwin, FALSE); /* back to first window */
2659# ifdef FEAT_AUTOCMD
2660 --autocmd_no_leave;
2661# endif
2662 TIME_MSG("editing files in windows");
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002663 if (parmp->window_count > 1 && parmp->window_layout != WIN_TABS)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002664 win_equal(curwin, FALSE, 'b'); /* adjust heights */
2665}
2666#endif /* FEAT_WINDOWS */
2667
2668/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002669 * Execute the commands from --cmd arguments "cmds[cnt]".
2670 */
2671 static void
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002672exe_pre_commands(parmp)
2673 mparm_T *parmp;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002674{
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002675 char_u **cmds = parmp->pre_commands;
2676 int cnt = parmp->n_pre_commands;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002677 int i;
2678
2679 if (cnt > 0)
2680 {
2681 curwin->w_cursor.lnum = 0; /* just in case.. */
2682 sourcing_name = (char_u *)_("pre-vimrc command line");
2683# ifdef FEAT_EVAL
2684 current_SID = SID_CMDARG;
2685# endif
2686 for (i = 0; i < cnt; ++i)
2687 do_cmdline_cmd(cmds[i]);
2688 sourcing_name = NULL;
2689# ifdef FEAT_EVAL
2690 current_SID = 0;
2691# endif
2692 TIME_MSG("--cmd commands");
2693 }
2694}
2695
2696/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002697 * Execute "+", "-c" and "-S" arguments.
2698 */
2699 static void
2700exe_commands(parmp)
2701 mparm_T *parmp;
2702{
2703 int i;
2704
2705 /*
2706 * We start commands on line 0, make "vim +/pat file" match a
2707 * pattern on line 1. But don't move the cursor when an autocommand
2708 * with g`" was used.
2709 */
2710 msg_scroll = TRUE;
2711 if (parmp->tagname == NULL && curwin->w_cursor.lnum <= 1)
2712 curwin->w_cursor.lnum = 0;
2713 sourcing_name = (char_u *)"command line";
2714#ifdef FEAT_EVAL
2715 current_SID = SID_CARG;
2716#endif
2717 for (i = 0; i < parmp->n_commands; ++i)
2718 {
2719 do_cmdline_cmd(parmp->commands[i]);
2720 if (parmp->cmds_tofree[i])
2721 vim_free(parmp->commands[i]);
2722 }
2723 sourcing_name = NULL;
2724#ifdef FEAT_EVAL
2725 current_SID = 0;
2726#endif
2727 if (curwin->w_cursor.lnum == 0)
2728 curwin->w_cursor.lnum = 1;
2729
2730 if (!exmode_active)
2731 msg_scroll = FALSE;
2732
2733#ifdef FEAT_QUICKFIX
2734 /* When started with "-q errorfile" jump to first error again. */
2735 if (parmp->edit_type == EDIT_QF)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002736 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002737#endif
2738 TIME_MSG("executing command arguments");
2739}
2740
2741/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002742 * Source startup scripts.
2743 */
2744 static void
2745source_startup_scripts(parmp)
2746 mparm_T *parmp;
2747{
2748 int i;
2749
2750 /*
2751 * For "evim" source evim.vim first of all, so that the user can overrule
2752 * any things he doesn't like.
2753 */
2754 if (parmp->evim_mode)
2755 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002756 (void)do_source((char_u *)EVIM_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002757 TIME_MSG("source evim file");
2758 }
2759
2760 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002761 * If -u argument given, use only the initializations from that file and
Bram Moolenaar58d98232005-07-23 22:25:46 +00002762 * nothing else.
2763 */
2764 if (parmp->use_vimrc != NULL)
2765 {
Bram Moolenaar231334e2005-07-25 20:46:57 +00002766 if (STRCMP(parmp->use_vimrc, "NONE") == 0
2767 || STRCMP(parmp->use_vimrc, "NORC") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002768 {
2769#ifdef FEAT_GUI
2770 if (use_gvimrc == NULL) /* don't load gvimrc either */
2771 use_gvimrc = parmp->use_vimrc;
2772#endif
2773 if (parmp->use_vimrc[2] == 'N')
2774 p_lpl = FALSE; /* don't load plugins either */
2775 }
2776 else
2777 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002778 if (do_source(parmp->use_vimrc, FALSE, DOSO_NONE) != OK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002779 EMSG2(_("E282: Cannot read from \"%s\""), parmp->use_vimrc);
2780 }
2781 }
2782 else if (!silent_mode)
2783 {
2784#ifdef AMIGA
2785 struct Process *proc = (struct Process *)FindTask(0L);
2786 APTR save_winptr = proc->pr_WindowPtr;
2787
2788 /* Avoid a requester here for a volume that doesn't exist. */
2789 proc->pr_WindowPtr = (APTR)-1L;
2790#endif
2791
2792 /*
2793 * Get system wide defaults, if the file name is defined.
2794 */
2795#ifdef SYS_VIMRC_FILE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002796 (void)do_source((char_u *)SYS_VIMRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002797#endif
Bram Moolenaar1056d982006-03-09 22:37:52 +00002798#ifdef MACOS_X
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002799 (void)do_source((char_u *)"$VIMRUNTIME/macmap.vim", FALSE, DOSO_NONE);
Bram Moolenaar1056d982006-03-09 22:37:52 +00002800#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00002801
2802 /*
2803 * Try to read initialization commands from the following places:
2804 * - environment variable VIMINIT
2805 * - user vimrc file (s:.vimrc for Amiga, ~/.vimrc otherwise)
2806 * - second user vimrc file ($VIM/.vimrc for Dos)
2807 * - environment variable EXINIT
2808 * - user exrc file (s:.exrc for Amiga, ~/.exrc otherwise)
2809 * - second user exrc file ($VIM/.exrc for Dos)
2810 * The first that exists is used, the rest is ignored.
2811 */
2812 if (process_env((char_u *)"VIMINIT", TRUE) != OK)
2813 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002814 if (do_source((char_u *)USR_VIMRC_FILE, TRUE, DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00002815#ifdef USR_VIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002816 && do_source((char_u *)USR_VIMRC_FILE2, TRUE,
2817 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00002818#endif
2819#ifdef USR_VIMRC_FILE3
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002820 && do_source((char_u *)USR_VIMRC_FILE3, TRUE,
2821 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00002822#endif
2823 && process_env((char_u *)"EXINIT", FALSE) == FAIL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002824 && do_source((char_u *)USR_EXRC_FILE, FALSE, DOSO_NONE) == FAIL)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002825 {
2826#ifdef USR_EXRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002827 (void)do_source((char_u *)USR_EXRC_FILE2, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002828#endif
2829 }
2830 }
2831
2832 /*
2833 * Read initialization commands from ".vimrc" or ".exrc" in current
2834 * directory. This is only done if the 'exrc' option is set.
2835 * Because of security reasons we disallow shell and write commands
2836 * now, except for unix if the file is owned by the user or 'secure'
2837 * option has been reset in environment of global ".exrc" or ".vimrc".
2838 * Only do this if VIMRC_FILE is not the same as USR_VIMRC_FILE or
2839 * SYS_VIMRC_FILE.
2840 */
2841 if (p_exrc)
2842 {
2843#if defined(UNIX) || defined(VMS)
2844 /* If ".vimrc" file is not owned by user, set 'secure' mode. */
2845 if (!file_owned(VIMRC_FILE))
2846#endif
2847 secure = p_secure;
2848
2849 i = FAIL;
2850 if (fullpathcmp((char_u *)USR_VIMRC_FILE,
2851 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
2852#ifdef USR_VIMRC_FILE2
2853 && fullpathcmp((char_u *)USR_VIMRC_FILE2,
2854 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
2855#endif
2856#ifdef USR_VIMRC_FILE3
2857 && fullpathcmp((char_u *)USR_VIMRC_FILE3,
2858 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
2859#endif
2860#ifdef SYS_VIMRC_FILE
2861 && fullpathcmp((char_u *)SYS_VIMRC_FILE,
2862 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
2863#endif
2864 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002865 i = do_source((char_u *)VIMRC_FILE, TRUE, DOSO_VIMRC);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002866
2867 if (i == FAIL)
2868 {
2869#if defined(UNIX) || defined(VMS)
2870 /* if ".exrc" is not owned by user set 'secure' mode */
2871 if (!file_owned(EXRC_FILE))
2872 secure = p_secure;
2873 else
2874 secure = 0;
2875#endif
2876 if ( fullpathcmp((char_u *)USR_EXRC_FILE,
2877 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
2878#ifdef USR_EXRC_FILE2
2879 && fullpathcmp((char_u *)USR_EXRC_FILE2,
2880 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
2881#endif
2882 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002883 (void)do_source((char_u *)EXRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002884 }
2885 }
2886 if (secure == 2)
2887 need_wait_return = TRUE;
2888 secure = 0;
2889#ifdef AMIGA
2890 proc->pr_WindowPtr = save_winptr;
2891#endif
2892 }
2893 TIME_MSG("sourcing vimrc file(s)");
2894}
2895
2896/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002897 * Setup to start using the GUI. Exit with an error when not available.
2898 */
2899 static void
2900main_start_gui()
2901{
2902#ifdef FEAT_GUI
2903 gui.starting = TRUE; /* start GUI a bit later */
2904#else
2905 mch_errmsg(_(e_nogvim));
2906 mch_errmsg("\n");
2907 mch_exit(2);
2908#endif
2909}
2910
2911/*
Bram Moolenaar49325942007-05-10 19:19:59 +00002912 * Get an environment variable, and execute it as Ex commands.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002913 * Returns FAIL if the environment variable was not executed, OK otherwise.
2914 */
2915 int
2916process_env(env, is_viminit)
2917 char_u *env;
2918 int is_viminit; /* when TRUE, called for VIMINIT */
2919{
2920 char_u *initstr;
2921 char_u *save_sourcing_name;
2922 linenr_T save_sourcing_lnum;
2923#ifdef FEAT_EVAL
2924 scid_T save_sid;
2925#endif
2926
2927 if ((initstr = mch_getenv(env)) != NULL && *initstr != NUL)
2928 {
2929 if (is_viminit)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002930 vimrc_found(NULL, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002931 save_sourcing_name = sourcing_name;
2932 save_sourcing_lnum = sourcing_lnum;
2933 sourcing_name = env;
2934 sourcing_lnum = 0;
2935#ifdef FEAT_EVAL
2936 save_sid = current_SID;
2937 current_SID = SID_ENV;
2938#endif
2939 do_cmdline_cmd(initstr);
2940 sourcing_name = save_sourcing_name;
2941 sourcing_lnum = save_sourcing_lnum;
2942#ifdef FEAT_EVAL
2943 current_SID = save_sid;;
2944#endif
2945 return OK;
2946 }
2947 return FAIL;
2948}
2949
2950#if defined(UNIX) || defined(VMS)
2951/*
2952 * Return TRUE if we are certain the user owns the file "fname".
2953 * Used for ".vimrc" and ".exrc".
2954 * Use both stat() and lstat() for extra security.
2955 */
2956 static int
2957file_owned(fname)
2958 char *fname;
2959{
2960 struct stat s;
2961# ifdef UNIX
2962 uid_t uid = getuid();
2963# else /* VMS */
2964 uid_t uid = ((getgid() << 16) | getuid());
2965# endif
2966
2967 return !(mch_stat(fname, &s) != 0 || s.st_uid != uid
2968# ifdef HAVE_LSTAT
2969 || mch_lstat(fname, &s) != 0 || s.st_uid != uid
2970# endif
2971 );
2972}
2973#endif
2974
2975/*
2976 * Give an error message main_errors["n"] and exit.
2977 */
2978 static void
2979mainerr(n, str)
2980 int n; /* one of the ME_ defines */
2981 char_u *str; /* extra argument or NULL */
2982{
2983#if defined(UNIX) || defined(__EMX__) || defined(VMS)
2984 reset_signals(); /* kill us with CTRL-C here, if you like */
2985#endif
2986
2987 mch_errmsg(longVersion);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002988 mch_errmsg("\n");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002989 mch_errmsg(_(main_errors[n]));
2990 if (str != NULL)
2991 {
2992 mch_errmsg(": \"");
2993 mch_errmsg((char *)str);
2994 mch_errmsg("\"");
2995 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002996 mch_errmsg(_("\nMore info with: \"vim -h\"\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002997
2998 mch_exit(1);
2999}
3000
3001 void
3002mainerr_arg_missing(str)
3003 char_u *str;
3004{
3005 mainerr(ME_ARG_MISSING, str);
3006}
3007
3008/*
3009 * print a message with three spaces prepended and '\n' appended.
3010 */
3011 static void
3012main_msg(s)
3013 char *s;
3014{
3015 mch_msg(" ");
3016 mch_msg(s);
3017 mch_msg("\n");
3018}
3019
3020/*
3021 * Print messages for "vim -h" or "vim --help" and exit.
3022 */
3023 static void
3024usage()
3025{
3026 int i;
3027 static char *(use[]) =
3028 {
3029 N_("[file ..] edit specified file(s)"),
3030 N_("- read text from stdin"),
3031 N_("-t tag edit file where tag is defined"),
3032#ifdef FEAT_QUICKFIX
3033 N_("-q [errorfile] edit file with first error")
3034#endif
3035 };
3036
3037#if defined(UNIX) || defined(__EMX__) || defined(VMS)
3038 reset_signals(); /* kill us with CTRL-C here, if you like */
3039#endif
3040
3041 mch_msg(longVersion);
3042 mch_msg(_("\n\nusage:"));
3043 for (i = 0; ; ++i)
3044 {
3045 mch_msg(_(" vim [arguments] "));
3046 mch_msg(_(use[i]));
3047 if (i == (sizeof(use) / sizeof(char_u *)) - 1)
3048 break;
3049 mch_msg(_("\n or:"));
3050 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003051#ifdef VMS
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003052 mch_msg(_("\nWhere case is ignored prepend / to make flag upper case"));
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003053#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003054
3055 mch_msg(_("\n\nArguments:\n"));
3056 main_msg(_("--\t\t\tOnly file names after this"));
3057#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
3058 main_msg(_("--literal\t\tDon't expand wildcards"));
3059#endif
3060#ifdef FEAT_OLE
3061 main_msg(_("-register\t\tRegister this gvim for OLE"));
3062 main_msg(_("-unregister\t\tUnregister gvim for OLE"));
3063#endif
3064#ifdef FEAT_GUI
3065 main_msg(_("-g\t\t\tRun using GUI (like \"gvim\")"));
3066 main_msg(_("-f or --nofork\tForeground: Don't fork when starting GUI"));
3067#endif
3068 main_msg(_("-v\t\t\tVi mode (like \"vi\")"));
3069 main_msg(_("-e\t\t\tEx mode (like \"ex\")"));
3070 main_msg(_("-s\t\t\tSilent (batch) mode (only for \"ex\")"));
3071#ifdef FEAT_DIFF
3072 main_msg(_("-d\t\t\tDiff mode (like \"vimdiff\")"));
3073#endif
3074 main_msg(_("-y\t\t\tEasy mode (like \"evim\", modeless)"));
3075 main_msg(_("-R\t\t\tReadonly mode (like \"view\")"));
3076 main_msg(_("-Z\t\t\tRestricted mode (like \"rvim\")"));
3077 main_msg(_("-m\t\t\tModifications (writing files) not allowed"));
3078 main_msg(_("-M\t\t\tModifications in text not allowed"));
3079 main_msg(_("-b\t\t\tBinary mode"));
3080#ifdef FEAT_LISP
3081 main_msg(_("-l\t\t\tLisp mode"));
3082#endif
3083 main_msg(_("-C\t\t\tCompatible with Vi: 'compatible'"));
3084 main_msg(_("-N\t\t\tNot fully Vi compatible: 'nocompatible'"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003085 main_msg(_("-V[N][fname]\t\tBe verbose [level N] [log messages to fname]"));
3086#ifdef FEAT_EVAL
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003087 main_msg(_("-D\t\t\tDebugging mode"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003088#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003089 main_msg(_("-n\t\t\tNo swap file, use memory only"));
3090 main_msg(_("-r\t\t\tList swap files and exit"));
3091 main_msg(_("-r (with file name)\tRecover crashed session"));
3092 main_msg(_("-L\t\t\tSame as -r"));
3093#ifdef AMIGA
3094 main_msg(_("-f\t\t\tDon't use newcli to open window"));
3095 main_msg(_("-dev <device>\t\tUse <device> for I/O"));
3096#endif
3097#ifdef FEAT_ARABIC
3098 main_msg(_("-A\t\t\tstart in Arabic mode"));
3099#endif
3100#ifdef FEAT_RIGHTLEFT
3101 main_msg(_("-H\t\t\tStart in Hebrew mode"));
3102#endif
3103#ifdef FEAT_FKMAP
3104 main_msg(_("-F\t\t\tStart in Farsi mode"));
3105#endif
3106 main_msg(_("-T <terminal>\tSet terminal type to <terminal>"));
3107 main_msg(_("-u <vimrc>\t\tUse <vimrc> instead of any .vimrc"));
3108#ifdef FEAT_GUI
3109 main_msg(_("-U <gvimrc>\t\tUse <gvimrc> instead of any .gvimrc"));
3110#endif
3111 main_msg(_("--noplugin\t\tDon't load plugin scripts"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003112#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003113 main_msg(_("-p[N]\t\tOpen N tab pages (default: one for each file)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003114 main_msg(_("-o[N]\t\tOpen N windows (default: one for each file)"));
3115 main_msg(_("-O[N]\t\tLike -o but split vertically"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003116#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003117 main_msg(_("+\t\t\tStart at end of file"));
3118 main_msg(_("+<lnum>\t\tStart at line <lnum>"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003119 main_msg(_("--cmd <command>\tExecute <command> before loading any vimrc file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003120 main_msg(_("-c <command>\t\tExecute <command> after loading the first file"));
3121 main_msg(_("-S <session>\t\tSource file <session> after loading the first file"));
3122 main_msg(_("-s <scriptin>\tRead Normal mode commands from file <scriptin>"));
3123 main_msg(_("-w <scriptout>\tAppend all typed commands to file <scriptout>"));
3124 main_msg(_("-W <scriptout>\tWrite all typed commands to file <scriptout>"));
3125#ifdef FEAT_CRYPT
3126 main_msg(_("-x\t\t\tEdit encrypted files"));
3127#endif
3128#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
3129# if defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)
3130 main_msg(_("-display <display>\tConnect vim to this particular X-server"));
3131# endif
3132 main_msg(_("-X\t\t\tDo not connect to X server"));
3133#endif
3134#ifdef FEAT_CLIENTSERVER
3135 main_msg(_("--remote <files>\tEdit <files> in a Vim server if possible"));
3136 main_msg(_("--remote-silent <files> Same, don't complain if there is no server"));
3137 main_msg(_("--remote-wait <files> As --remote but wait for files to have been edited"));
3138 main_msg(_("--remote-wait-silent <files> Same, don't complain if there is no server"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003139# ifdef FEAT_WINDOWS
Bram Moolenaar82ad3242008-01-11 19:26:36 +00003140 main_msg(_("--remote-tab[-wait][-silent] <files> As --remote but use tab page per file"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003141# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003142 main_msg(_("--remote-send <keys>\tSend <keys> to a Vim server and exit"));
3143 main_msg(_("--remote-expr <expr>\tEvaluate <expr> in a Vim server and print result"));
3144 main_msg(_("--serverlist\t\tList available Vim server names and exit"));
3145 main_msg(_("--servername <name>\tSend to/become the Vim server <name>"));
3146#endif
3147#ifdef FEAT_VIMINFO
3148 main_msg(_("-i <viminfo>\t\tUse <viminfo> instead of .viminfo"));
3149#endif
3150 main_msg(_("-h or --help\tPrint Help (this message) and exit"));
3151 main_msg(_("--version\t\tPrint version information and exit"));
3152
3153#ifdef FEAT_GUI_X11
3154# ifdef FEAT_GUI_MOTIF
3155 mch_msg(_("\nArguments recognised by gvim (Motif version):\n"));
3156# else
3157# ifdef FEAT_GUI_ATHENA
3158# ifdef FEAT_GUI_NEXTAW
3159 mch_msg(_("\nArguments recognised by gvim (neXtaw version):\n"));
3160# else
3161 mch_msg(_("\nArguments recognised by gvim (Athena version):\n"));
3162# endif
3163# endif
3164# endif
3165 main_msg(_("-display <display>\tRun vim on <display>"));
3166 main_msg(_("-iconic\t\tStart vim iconified"));
3167# if 0
3168 main_msg(_("-name <name>\t\tUse resource as if vim was <name>"));
3169 mch_msg(_("\t\t\t (Unimplemented)\n"));
3170# endif
3171 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
3172 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
3173 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3174 main_msg(_("-boldfont <font>\tUse <font> for bold text"));
3175 main_msg(_("-italicfont <font>\tUse <font> for italic text"));
3176 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3177 main_msg(_("-borderwidth <width>\tUse a border width of <width> (also: -bw)"));
3178 main_msg(_("-scrollbarwidth <width> Use a scrollbar width of <width> (also: -sw)"));
3179# ifdef FEAT_GUI_ATHENA
3180 main_msg(_("-menuheight <height>\tUse a menu bar height of <height> (also: -mh)"));
3181# endif
3182 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3183 main_msg(_("+reverse\t\tDon't use reverse video (also: +rv)"));
3184 main_msg(_("-xrm <resource>\tSet the specified resource"));
3185#endif /* FEAT_GUI_X11 */
3186#if defined(FEAT_GUI) && defined(RISCOS)
3187 mch_msg(_("\nArguments recognised by gvim (RISC OS version):\n"));
3188 main_msg(_("--columns <number>\tInitial width of window in columns"));
3189 main_msg(_("--rows <number>\tInitial height of window in rows"));
3190#endif
3191#ifdef FEAT_GUI_GTK
3192 mch_msg(_("\nArguments recognised by gvim (GTK+ version):\n"));
3193 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3194 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3195 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3196 main_msg(_("-display <display>\tRun vim on <display> (also: --display)"));
3197# ifdef HAVE_GTK2
3198 main_msg(_("--role <role>\tSet a unique role to identify the main window"));
3199# endif
3200 main_msg(_("--socketid <xid>\tOpen Vim inside another GTK widget"));
3201#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003202#ifdef FEAT_GUI_W32
3203 main_msg(_("-P <parent title>\tOpen Vim inside parent application"));
Bram Moolenaar78e17622007-08-30 10:26:19 +00003204 main_msg(_("--windowid <HWND>\tOpen Vim inside another win32 widget"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003205#endif
3206
3207#ifdef FEAT_GUI_GNOME
3208 /* Gnome gives extra messages for --help if we continue, but not for -h. */
3209 if (gui.starting)
3210 mch_msg("\n");
3211 else
3212#endif
3213 mch_exit(0);
3214}
3215
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00003216#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003217/*
3218 * Check the result of the ATTENTION dialog:
3219 * When "Quit" selected, exit Vim.
3220 * When "Recover" selected, recover the file.
3221 */
3222 static void
3223check_swap_exists_action()
3224{
3225 if (swap_exists_action == SEA_QUIT)
3226 getout(1);
3227 handle_swap_exists(NULL);
3228}
3229#endif
3230
3231#if defined(STARTUPTIME) || defined(PROTO)
3232static void time_diff __ARGS((struct timeval *then, struct timeval *now));
3233
3234static struct timeval prev_timeval;
3235
Bram Moolenaar3f269672009-11-03 11:11:11 +00003236# ifdef WIN3264
3237/*
3238 * Windows doesn't have gettimeofday(), although it does have struct timeval.
3239 */
3240 static int
3241gettimeofday(struct timeval *tv, char *dummy)
3242{
3243 long t = clock();
3244 tv->tv_sec = t / CLOCKS_PER_SEC;
3245 tv->tv_usec = (t - tv->tv_sec * CLOCKS_PER_SEC) * 1000000 / CLOCKS_PER_SEC;
3246 return 0;
3247}
3248# endif
3249
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003250/*
3251 * Save the previous time before doing something that could nest.
3252 * set "*tv_rel" to the time elapsed so far.
3253 */
3254 void
3255time_push(tv_rel, tv_start)
3256 void *tv_rel, *tv_start;
3257{
3258 *((struct timeval *)tv_rel) = prev_timeval;
3259 gettimeofday(&prev_timeval, NULL);
3260 ((struct timeval *)tv_rel)->tv_usec = prev_timeval.tv_usec
3261 - ((struct timeval *)tv_rel)->tv_usec;
3262 ((struct timeval *)tv_rel)->tv_sec = prev_timeval.tv_sec
3263 - ((struct timeval *)tv_rel)->tv_sec;
3264 if (((struct timeval *)tv_rel)->tv_usec < 0)
3265 {
3266 ((struct timeval *)tv_rel)->tv_usec += 1000000;
3267 --((struct timeval *)tv_rel)->tv_sec;
3268 }
3269 *(struct timeval *)tv_start = prev_timeval;
3270}
3271
3272/*
3273 * Compute the previous time after doing something that could nest.
3274 * Subtract "*tp" from prev_timeval;
3275 * Note: The arguments are (void *) to avoid trouble with systems that don't
3276 * have struct timeval.
3277 */
3278 void
3279time_pop(tp)
3280 void *tp; /* actually (struct timeval *) */
3281{
3282 prev_timeval.tv_usec -= ((struct timeval *)tp)->tv_usec;
3283 prev_timeval.tv_sec -= ((struct timeval *)tp)->tv_sec;
3284 if (prev_timeval.tv_usec < 0)
3285 {
3286 prev_timeval.tv_usec += 1000000;
3287 --prev_timeval.tv_sec;
3288 }
3289}
3290
3291 static void
3292time_diff(then, now)
3293 struct timeval *then;
3294 struct timeval *now;
3295{
3296 long usec;
3297 long msec;
3298
3299 usec = now->tv_usec - then->tv_usec;
3300 msec = (now->tv_sec - then->tv_sec) * 1000L + usec / 1000L,
3301 usec = usec % 1000L;
3302 fprintf(time_fd, "%03ld.%03ld", msec, usec >= 0 ? usec : usec + 1000L);
3303}
3304
3305 void
3306time_msg(msg, tv_start)
3307 char *msg;
3308 void *tv_start; /* only for do_source: start time; actually
3309 (struct timeval *) */
3310{
3311 static struct timeval start;
3312 struct timeval now;
3313
3314 if (time_fd != NULL)
3315 {
3316 if (strstr(msg, "STARTING") != NULL)
3317 {
3318 gettimeofday(&start, NULL);
3319 prev_timeval = start;
3320 fprintf(time_fd, "\n\ntimes in msec\n");
3321 fprintf(time_fd, " clock self+sourced self: sourced script\n");
3322 fprintf(time_fd, " clock elapsed: other lines\n\n");
3323 }
3324 gettimeofday(&now, NULL);
3325 time_diff(&start, &now);
3326 if (((struct timeval *)tv_start) != NULL)
3327 {
3328 fprintf(time_fd, " ");
3329 time_diff(((struct timeval *)tv_start), &now);
3330 }
3331 fprintf(time_fd, " ");
3332 time_diff(&prev_timeval, &now);
3333 prev_timeval = now;
3334 fprintf(time_fd, ": %s\n", msg);
3335 }
3336}
3337
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003338#endif
3339
3340#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
3341
3342/*
3343 * Common code for the X command server and the Win32 command server.
3344 */
3345
Bram Moolenaareb94e552006-03-11 21:35:11 +00003346static char_u *build_drop_cmd __ARGS((int filec, char **filev, int tabs, int sendReply));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003347
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003348/*
3349 * Do the client-server stuff, unless "--servername ''" was used.
3350 */
3351 static void
3352exec_on_server(parmp)
3353 mparm_T *parmp;
3354{
3355 if (parmp->serverName_arg == NULL || *parmp->serverName_arg != NUL)
3356 {
3357# ifdef WIN32
3358 /* Initialise the client/server messaging infrastructure. */
3359 serverInitMessaging();
3360# endif
3361
3362 /*
3363 * When a command server argument was found, execute it. This may
3364 * exit Vim when it was successful. Otherwise it's executed further
3365 * on. Remember the encoding used here in "serverStrEnc".
3366 */
3367 if (parmp->serverArg)
3368 {
3369 cmdsrv_main(&parmp->argc, parmp->argv,
3370 parmp->serverName_arg, &parmp->serverStr);
3371# ifdef FEAT_MBYTE
3372 parmp->serverStrEnc = vim_strsave(p_enc);
3373# endif
3374 }
3375
3376 /* If we're still running, get the name to register ourselves.
3377 * On Win32 can register right now, for X11 need to setup the
3378 * clipboard first, it's further down. */
3379 parmp->servername = serverMakeName(parmp->serverName_arg,
3380 parmp->argv[0]);
3381# ifdef WIN32
3382 if (parmp->servername != NULL)
3383 {
3384 serverSetName(parmp->servername);
3385 vim_free(parmp->servername);
3386 }
3387# endif
3388 }
3389}
3390
3391/*
3392 * Prepare for running as a Vim server.
3393 */
3394 static void
3395prepare_server(parmp)
3396 mparm_T *parmp;
3397{
3398# if defined(FEAT_X11)
3399 /*
3400 * Register for remote command execution with :serversend and --remote
3401 * unless there was a -X or a --servername '' on the command line.
3402 * Only register nongui-vim's with an explicit --servername argument.
Bram Moolenaar5f402312006-08-15 19:40:35 +00003403 * When running as root --servername is also required.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003404 */
3405 if (X_DISPLAY != NULL && parmp->servername != NULL && (
3406# ifdef FEAT_GUI
Bram Moolenaar5f402312006-08-15 19:40:35 +00003407 (gui.in_use
3408# ifdef UNIX
Bram Moolenaar311d9822007-02-27 15:48:28 +00003409 && getuid() != ROOT_UID
Bram Moolenaar5f402312006-08-15 19:40:35 +00003410# endif
3411 ) ||
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003412# endif
3413 parmp->serverName_arg != NULL))
3414 {
3415 (void)serverRegisterName(X_DISPLAY, parmp->servername);
3416 vim_free(parmp->servername);
3417 TIME_MSG("register server name");
3418 }
3419 else
3420 serverDelayedStartName = parmp->servername;
3421# endif
3422
3423 /*
3424 * Execute command ourselves if we're here because the send failed (or
3425 * else we would have exited above).
3426 */
3427 if (parmp->serverStr != NULL)
3428 {
3429 char_u *p;
3430
3431 server_to_input_buf(serverConvert(parmp->serverStrEnc,
3432 parmp->serverStr, &p));
3433 vim_free(p);
3434 }
3435}
3436
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003437 static void
3438cmdsrv_main(argc, argv, serverName_arg, serverStr)
3439 int *argc;
3440 char **argv;
3441 char_u *serverName_arg;
3442 char_u **serverStr;
3443{
3444 char_u *res;
3445 int i;
3446 char_u *sname;
3447 int ret;
3448 int didone = FALSE;
3449 int exiterr = 0;
3450 char **newArgV = argv + 1;
3451 int newArgC = 1,
3452 Argc = *argc;
3453 int argtype;
3454#define ARGTYPE_OTHER 0
3455#define ARGTYPE_EDIT 1
3456#define ARGTYPE_EDIT_WAIT 2
3457#define ARGTYPE_SEND 3
3458 int silent = FALSE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003459 int tabs = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003460# ifndef FEAT_X11
3461 HWND srv;
3462# else
3463 Window srv;
3464
3465 setup_term_clip();
3466# endif
3467
3468 sname = serverMakeName(serverName_arg, argv[0]);
3469 if (sname == NULL)
3470 return;
3471
3472 /*
3473 * Execute the command server related arguments and remove them
3474 * from the argc/argv array; We may have to return into main()
3475 */
3476 for (i = 1; i < Argc; i++)
3477 {
3478 res = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003479 if (STRCMP(argv[i], "--") == 0) /* end of option arguments */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003480 {
3481 for (; i < *argc; i++)
3482 {
3483 *newArgV++ = argv[i];
3484 newArgC++;
3485 }
3486 break;
3487 }
3488
Bram Moolenaareb94e552006-03-11 21:35:11 +00003489 if (STRICMP(argv[i], "--remote-send") == 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003490 argtype = ARGTYPE_SEND;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003491 else if (STRNICMP(argv[i], "--remote", 8) == 0)
3492 {
3493 char *p = argv[i] + 8;
3494
3495 argtype = ARGTYPE_EDIT;
3496 while (*p != NUL)
3497 {
3498 if (STRNICMP(p, "-wait", 5) == 0)
3499 {
3500 argtype = ARGTYPE_EDIT_WAIT;
3501 p += 5;
3502 }
3503 else if (STRNICMP(p, "-silent", 7) == 0)
3504 {
3505 silent = TRUE;
3506 p += 7;
3507 }
3508 else if (STRNICMP(p, "-tab", 4) == 0)
3509 {
3510 tabs = TRUE;
3511 p += 4;
3512 }
3513 else
3514 {
3515 argtype = ARGTYPE_OTHER;
3516 break;
3517 }
3518 }
3519 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003520 else
3521 argtype = ARGTYPE_OTHER;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003522
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003523 if (argtype != ARGTYPE_OTHER)
3524 {
3525 if (i == *argc - 1)
3526 mainerr_arg_missing((char_u *)argv[i]);
3527 if (argtype == ARGTYPE_SEND)
3528 {
3529 *serverStr = (char_u *)argv[i + 1];
3530 i++;
3531 }
3532 else
3533 {
3534 *serverStr = build_drop_cmd(*argc - i - 1, argv + i + 1,
Bram Moolenaareb94e552006-03-11 21:35:11 +00003535 tabs, argtype == ARGTYPE_EDIT_WAIT);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003536 if (*serverStr == NULL)
3537 {
3538 /* Probably out of memory, exit. */
3539 didone = TRUE;
3540 exiterr = 1;
3541 break;
3542 }
3543 Argc = i;
3544 }
3545# ifdef FEAT_X11
3546 if (xterm_dpy == NULL)
3547 {
3548 mch_errmsg(_("No display"));
3549 ret = -1;
3550 }
3551 else
3552 ret = serverSendToVim(xterm_dpy, sname, *serverStr,
3553 NULL, &srv, 0, 0, silent);
3554# else
3555 /* Win32 always works? */
3556 ret = serverSendToVim(sname, *serverStr, NULL, &srv, 0, silent);
3557# endif
3558 if (ret < 0)
3559 {
3560 if (argtype == ARGTYPE_SEND)
3561 {
3562 /* Failed to send, abort. */
3563 mch_errmsg(_(": Send failed.\n"));
3564 didone = TRUE;
3565 exiterr = 1;
3566 }
3567 else if (!silent)
3568 /* Let vim start normally. */
3569 mch_errmsg(_(": Send failed. Trying to execute locally\n"));
3570 break;
3571 }
3572
3573# ifdef FEAT_GUI_W32
3574 /* Guess that when the server name starts with "g" it's a GUI
3575 * server, which we can bring to the foreground here.
3576 * Foreground() in the server doesn't work very well. */
3577 if (argtype != ARGTYPE_SEND && TOUPPER_ASC(*sname) == 'G')
3578 SetForegroundWindow(srv);
3579# endif
3580
3581 /*
3582 * For --remote-wait: Wait until the server did edit each
3583 * file. Also detect that the server no longer runs.
3584 */
3585 if (ret >= 0 && argtype == ARGTYPE_EDIT_WAIT)
3586 {
3587 int numFiles = *argc - i - 1;
3588 int j;
3589 char_u *done = alloc(numFiles);
3590 char_u *p;
3591# ifdef FEAT_GUI_W32
3592 NOTIFYICONDATA ni;
3593 int count = 0;
3594 extern HWND message_window;
3595# endif
3596
3597 if (numFiles > 0 && argv[i + 1][0] == '+')
3598 /* Skip "+cmd" argument, don't wait for it to be edited. */
3599 --numFiles;
3600
3601# ifdef FEAT_GUI_W32
3602 ni.cbSize = sizeof(ni);
3603 ni.hWnd = message_window;
3604 ni.uID = 0;
3605 ni.uFlags = NIF_ICON|NIF_TIP;
3606 ni.hIcon = LoadIcon((HINSTANCE)GetModuleHandle(0), "IDR_VIM");
3607 sprintf(ni.szTip, _("%d of %d edited"), count, numFiles);
3608 Shell_NotifyIcon(NIM_ADD, &ni);
3609# endif
3610
3611 /* Wait for all files to unload in remote */
3612 memset(done, 0, numFiles);
3613 while (memchr(done, 0, numFiles) != NULL)
3614 {
3615# ifdef WIN32
3616 p = serverGetReply(srv, NULL, TRUE, TRUE);
3617 if (p == NULL)
3618 break;
3619# else
3620 if (serverReadReply(xterm_dpy, srv, &p, TRUE) < 0)
3621 break;
3622# endif
3623 j = atoi((char *)p);
3624 if (j >= 0 && j < numFiles)
3625 {
3626# ifdef FEAT_GUI_W32
3627 ++count;
3628 sprintf(ni.szTip, _("%d of %d edited"),
3629 count, numFiles);
3630 Shell_NotifyIcon(NIM_MODIFY, &ni);
3631# endif
3632 done[j] = 1;
3633 }
3634 }
3635# ifdef FEAT_GUI_W32
3636 Shell_NotifyIcon(NIM_DELETE, &ni);
3637# endif
3638 }
3639 }
3640 else if (STRICMP(argv[i], "--remote-expr") == 0)
3641 {
3642 if (i == *argc - 1)
3643 mainerr_arg_missing((char_u *)argv[i]);
3644# ifdef WIN32
3645 /* Win32 always works? */
3646 if (serverSendToVim(sname, (char_u *)argv[i + 1],
3647 &res, NULL, 1, FALSE) < 0)
3648# else
3649 if (xterm_dpy == NULL)
3650 mch_errmsg(_("No display: Send expression failed.\n"));
3651 else if (serverSendToVim(xterm_dpy, sname, (char_u *)argv[i + 1],
3652 &res, NULL, 1, 1, FALSE) < 0)
3653# endif
3654 {
3655 if (res != NULL && *res != NUL)
3656 {
3657 /* Output error from remote */
3658 mch_errmsg((char *)res);
3659 vim_free(res);
3660 res = NULL;
3661 }
3662 mch_errmsg(_(": Send expression failed.\n"));
3663 }
3664 }
3665 else if (STRICMP(argv[i], "--serverlist") == 0)
3666 {
3667# ifdef WIN32
3668 /* Win32 always works? */
3669 res = serverGetVimNames();
3670# else
3671 if (xterm_dpy != NULL)
3672 res = serverGetVimNames(xterm_dpy);
3673# endif
3674 if (called_emsg)
3675 mch_errmsg("\n");
3676 }
3677 else if (STRICMP(argv[i], "--servername") == 0)
3678 {
3679 /* Alredy processed. Take it out of the command line */
3680 i++;
3681 continue;
3682 }
3683 else
3684 {
3685 *newArgV++ = argv[i];
3686 newArgC++;
3687 continue;
3688 }
3689 didone = TRUE;
3690 if (res != NULL && *res != NUL)
3691 {
3692 mch_msg((char *)res);
3693 if (res[STRLEN(res) - 1] != '\n')
3694 mch_msg("\n");
3695 }
3696 vim_free(res);
3697 }
3698
3699 if (didone)
3700 {
3701 display_errors(); /* display any collected messages */
3702 exit(exiterr); /* Mission accomplished - get out */
3703 }
3704
3705 /* Return back into main() */
3706 *argc = newArgC;
3707 vim_free(sname);
3708}
3709
3710/*
3711 * Build a ":drop" command to send to a Vim server.
3712 */
3713 static char_u *
Bram Moolenaareb94e552006-03-11 21:35:11 +00003714build_drop_cmd(filec, filev, tabs, sendReply)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003715 int filec;
3716 char **filev;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003717 int tabs; /* Use ":tab drop" instead of ":drop". */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003718 int sendReply;
3719{
3720 garray_T ga;
3721 int i;
3722 char_u *inicmd = NULL;
3723 char_u *p;
3724 char_u cwd[MAXPATHL];
3725
3726 if (filec > 0 && filev[0][0] == '+')
3727 {
3728 inicmd = (char_u *)filev[0] + 1;
3729 filev++;
3730 filec--;
3731 }
3732 /* Check if we have at least one argument. */
3733 if (filec <= 0)
3734 mainerr_arg_missing((char_u *)filev[-1]);
3735 if (mch_dirname(cwd, MAXPATHL) != OK)
3736 return NULL;
Bram Moolenaar02b06312007-09-06 15:39:22 +00003737 if ((p = vim_strsave_escaped_ext(cwd,
3738#ifdef BACKSLASH_IN_FILENAME
3739 "", /* rem_backslash() will tell what chars to escape */
3740#else
3741 PATH_ESC_CHARS,
3742#endif
3743 '\\', TRUE)) == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003744 return NULL;
3745 ga_init2(&ga, 1, 100);
3746 ga_concat(&ga, (char_u *)"<C-\\><C-N>:cd ");
3747 ga_concat(&ga, p);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003748 vim_free(p);
Bram Moolenaareb94e552006-03-11 21:35:11 +00003749
3750 /* Call inputsave() so that a prompt for an encryption key works. */
3751 ga_concat(&ga, (char_u *)"<CR>:if exists('*inputsave')|call inputsave()|endif|");
3752 if (tabs)
3753 ga_concat(&ga, (char_u *)"tab ");
3754 ga_concat(&ga, (char_u *)"drop");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003755 for (i = 0; i < filec; i++)
3756 {
3757 /* On Unix the shell has already expanded the wildcards, don't want to
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003758 * do it again in the Vim server. On MS-Windows only escape
3759 * non-wildcard characters. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003760 p = vim_strsave_escaped((char_u *)filev[i],
3761#ifdef UNIX
3762 PATH_ESC_CHARS
3763#else
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003764 (char_u *)" \t%#"
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003765#endif
3766 );
3767 if (p == NULL)
3768 {
3769 vim_free(ga.ga_data);
3770 return NULL;
3771 }
3772 ga_concat(&ga, (char_u *)" ");
3773 ga_concat(&ga, p);
3774 vim_free(p);
3775 }
3776 /* The :drop commands goes to Insert mode when 'insertmode' is set, use
3777 * CTRL-\ CTRL-N again. */
3778 ga_concat(&ga, (char_u *)"|if exists('*inputrestore')|call inputrestore()|endif<CR>");
3779 ga_concat(&ga, (char_u *)"<C-\\><C-N>:cd -");
3780 if (sendReply)
3781 ga_concat(&ga, (char_u *)"<CR>:call SetupRemoteReplies()");
3782 ga_concat(&ga, (char_u *)"<CR>:");
3783 if (inicmd != NULL)
3784 {
3785 /* Can't use <CR> after "inicmd", because an "startinsert" would cause
3786 * the following commands to be inserted as text. Use a "|",
3787 * hopefully "inicmd" does allow this... */
3788 ga_concat(&ga, inicmd);
3789 ga_concat(&ga, (char_u *)"|");
3790 }
3791 /* Bring the window to the foreground, goto Insert mode when 'im' set and
3792 * clear command line. */
Bram Moolenaar567e4de2004-12-31 21:01:02 +00003793 ga_concat(&ga, (char_u *)"cal foreground()|if &im|star|en|redr|f<CR>");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003794 ga_append(&ga, NUL);
3795 return ga.ga_data;
3796}
3797
3798/*
3799 * Replace termcodes such as <CR> and insert as key presses if there is room.
3800 */
3801 void
3802server_to_input_buf(str)
3803 char_u *str;
3804{
3805 char_u *ptr = NULL;
3806 char_u *cpo_save = p_cpo;
3807
3808 /* Set 'cpoptions' the way we want it.
3809 * B set - backslashes are *not* treated specially
3810 * k set - keycodes are *not* reverse-engineered
3811 * < unset - <Key> sequences *are* interpreted
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00003812 * The last but one parameter of replace_termcodes() is TRUE so that the
3813 * <lt> sequence is recognised - needed for a real backslash.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003814 */
3815 p_cpo = (char_u *)"Bk";
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00003816 str = replace_termcodes((char_u *)str, &ptr, FALSE, TRUE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003817 p_cpo = cpo_save;
3818
3819 if (*ptr != NUL) /* trailing CTRL-V results in nothing */
3820 {
3821 /*
3822 * Add the string to the input stream.
3823 * Can't use add_to_input_buf() here, we now have K_SPECIAL bytes.
3824 *
3825 * First clear typed characters from the typeahead buffer, there could
3826 * be half a mapping there. Then append to the existing string, so
3827 * that multiple commands from a client are concatenated.
3828 */
3829 if (typebuf.tb_maplen < typebuf.tb_len)
3830 del_typebuf(typebuf.tb_len - typebuf.tb_maplen, typebuf.tb_maplen);
3831 (void)ins_typebuf(str, REMAP_NONE, typebuf.tb_len, TRUE, FALSE);
3832
3833 /* Let input_available() know we inserted text in the typeahead
3834 * buffer. */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003835 typebuf_was_filled = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003836 }
3837 vim_free((char_u *)ptr);
3838}
3839
3840/*
3841 * Evaluate an expression that the client sent to a string.
3842 * Handles disabling error messages and disables debugging, otherwise Vim
3843 * hangs, waiting for "cont" to be typed.
3844 */
3845 char_u *
3846eval_client_expr_to_string(expr)
3847 char_u *expr;
3848{
3849 char_u *res;
3850 int save_dbl = debug_break_level;
3851 int save_ro = redir_off;
3852
3853 debug_break_level = -1;
3854 redir_off = 0;
3855 ++emsg_skip;
3856
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003857 res = eval_to_string(expr, NULL, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003858
3859 debug_break_level = save_dbl;
3860 redir_off = save_ro;
3861 --emsg_skip;
3862
Bram Moolenaar63a121b2005-12-11 21:36:39 +00003863 /* A client can tell us to redraw, but not to display the cursor, so do
3864 * that here. */
3865 setcursor();
3866 out_flush();
3867#ifdef FEAT_GUI
3868 if (gui.in_use)
3869 gui_update_cursor(FALSE, FALSE);
3870#endif
3871
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003872 return res;
3873}
3874
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003875/*
3876 * If conversion is needed, convert "data" from "client_enc" to 'encoding' and
3877 * return an allocated string. Otherwise return "data".
3878 * "*tofree" is set to the result when it needs to be freed later.
3879 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003880 char_u *
3881serverConvert(client_enc, data, tofree)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003882 char_u *client_enc UNUSED;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003883 char_u *data;
3884 char_u **tofree;
3885{
3886 char_u *res = data;
3887
3888 *tofree = NULL;
3889# ifdef FEAT_MBYTE
3890 if (client_enc != NULL && p_enc != NULL)
3891 {
3892 vimconv_T vimconv;
3893
3894 vimconv.vc_type = CONV_NONE;
3895 if (convert_setup(&vimconv, client_enc, p_enc) != FAIL
3896 && vimconv.vc_type != CONV_NONE)
3897 {
3898 res = string_convert(&vimconv, data, NULL);
3899 if (res == NULL)
3900 res = data;
3901 else
3902 *tofree = res;
3903 }
3904 convert_setup(&vimconv, NULL, NULL);
3905 }
3906# endif
3907 return res;
3908}
3909
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003910
3911/*
3912 * Make our basic server name: use the specified "arg" if given, otherwise use
3913 * the tail of the command "cmd" we were started with.
3914 * Return the name in allocated memory. This doesn't include a serial number.
3915 */
3916 static char_u *
3917serverMakeName(arg, cmd)
3918 char_u *arg;
3919 char *cmd;
3920{
3921 char_u *p;
3922
3923 if (arg != NULL && *arg != NUL)
3924 p = vim_strsave_up(arg);
3925 else
3926 {
3927 p = vim_strsave_up(gettail((char_u *)cmd));
3928 /* Remove .exe or .bat from the name. */
3929 if (p != NULL && vim_strchr(p, '.') != NULL)
3930 *vim_strchr(p, '.') = NUL;
3931 }
3932 return p;
3933}
3934#endif /* FEAT_CLIENTSERVER */
3935
3936/*
3937 * When FEAT_FKMAP is defined, also compile the Farsi source code.
3938 */
3939#if defined(FEAT_FKMAP) || defined(PROTO)
3940# include "farsi.c"
3941#endif
3942
3943/*
3944 * When FEAT_ARABIC is defined, also compile the Arabic source code.
3945 */
3946#if defined(FEAT_ARABIC) || defined(PROTO)
3947# include "arabic.c"
3948#endif