blob: aff8b3fb3f0ac00b044ddeca056e20124e9b2755 [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
10#if defined(MSDOS) || 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
21#ifdef HAVE_FCNTL_H
22# include <fcntl.h>
23#endif
24
25#ifdef __CYGWIN__
26# ifndef WIN32
27# include <sys/cygwin.h> /* for cygwin_conv_to_posix_path() */
28# endif
29# include <limits.h>
30#endif
31
Bram Moolenaarc013cb62005-07-24 21:18:31 +000032/* Maximum number of commands from + or -c arguments. */
33#define MAX_ARG_CMDS 10
34
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000035/* values for "window_layout" */
36#define WIN_HOR 1 /* "-o" horizontally split windows */
37#define WIN_VER 2 /* "-O" vertically split windows */
38#define WIN_TABS 3 /* "-p" windows on tab pages */
39
Bram Moolenaar58d98232005-07-23 22:25:46 +000040/* Struct for various parameters passed between main() and other functions. */
41typedef struct
42{
Bram Moolenaarc013cb62005-07-24 21:18:31 +000043 int argc;
44 char **argv;
45
46 int evim_mode; /* started as "evim" */
Bram Moolenaarc013cb62005-07-24 21:18:31 +000047 char_u *use_vimrc; /* vimrc from -u argument */
48
49 int n_commands; /* no. of commands from + or -c */
50 char_u *commands[MAX_ARG_CMDS]; /* commands from + or -c arg. */
51 char_u cmds_tofree[MAX_ARG_CMDS]; /* commands that need free() */
52 int n_pre_commands; /* no. of commands from --cmd */
53 char_u *pre_commands[MAX_ARG_CMDS]; /* commands from --cmd argument */
54
55 int edit_type; /* type of editing to do */
56 char_u *tagname; /* tag from -t argument */
57#ifdef FEAT_QUICKFIX
58 char_u *use_ef; /* 'errorfile' from -q argument */
59#endif
60
61 int want_full_screen;
62 int stdout_isatty; /* is stdout a terminal? */
63 char_u *term; /* specified terminal name */
64#ifdef FEAT_CRYPT
65 int ask_for_key; /* -x argument */
66#endif
67 int no_swap_file; /* "-n" argument used */
68#ifdef FEAT_EVAL
69 int use_debug_break_level;
70#endif
71#ifdef FEAT_WINDOWS
72 int window_count; /* number of windows to use */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000073 int window_layout; /* 0, WIN_HOR, WIN_VER or WIN_TABS */
Bram Moolenaarc013cb62005-07-24 21:18:31 +000074#endif
75
76#ifdef FEAT_CLIENTSERVER
Bram Moolenaar58d98232005-07-23 22:25:46 +000077 int serverArg; /* TRUE when argument for a server */
78 char_u *serverName_arg; /* cmdline arg for server name */
Bram Moolenaarc013cb62005-07-24 21:18:31 +000079 char_u *serverStr; /* remote server command */
80 char_u *serverStrEnc; /* encoding of serverStr */
81 char_u *servername; /* allocated name for our server */
82#endif
83#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
84 int literal; /* don't expand file names */
85#endif
86#ifdef MSWIN
87 int full_path; /* file name argument was full path */
88#endif
89#ifdef FEAT_DIFF
90 int diff_mode; /* start with 'diff' set */
91#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +000092} mparm_T;
93
Bram Moolenaarc013cb62005-07-24 21:18:31 +000094/* Values for edit_type. */
95#define EDIT_NONE 0 /* no edit type yet */
96#define EDIT_FILE 1 /* file name argument[s] given, use argument list */
97#define EDIT_STDIN 2 /* read file from stdin */
98#define EDIT_TAG 3 /* tag name argument given, use tagname */
99#define EDIT_QF 4 /* start in quickfix mode */
100
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000101#if defined(UNIX) || defined(VMS)
102static int file_owned __ARGS((char *fname));
103#endif
104static void mainerr __ARGS((int, char_u *));
105static void main_msg __ARGS((char *s));
106static void usage __ARGS((void));
107static int get_number_arg __ARGS((char_u *p, int *idx, int def));
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000108#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
109static void init_locale __ARGS((void));
110#endif
111static void parse_command_name __ARGS((mparm_T *parmp));
112static void early_arg_scan __ARGS((mparm_T *parmp));
113static void command_line_scan __ARGS((mparm_T *parmp));
114static void check_tty __ARGS((mparm_T *parmp));
115static void read_stdin __ARGS((void));
116static void create_windows __ARGS((mparm_T *parmp));
117#ifdef FEAT_WINDOWS
118static void edit_buffers __ARGS((mparm_T *parmp));
119#endif
120static void exe_pre_commands __ARGS((mparm_T *parmp));
121static void exe_commands __ARGS((mparm_T *parmp));
Bram Moolenaar58d98232005-07-23 22:25:46 +0000122static void source_startup_scripts __ARGS((mparm_T *parmp));
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000123static void main_start_gui __ARGS((void));
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000124#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000125static void check_swap_exists_action __ARGS((void));
126#endif
127#ifdef FEAT_CLIENTSERVER
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000128static void exec_on_server __ARGS((mparm_T *parmp));
129static void prepare_server __ARGS((mparm_T *parmp));
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000130static void cmdsrv_main __ARGS((int *argc, char **argv, char_u *serverName_arg, char_u **serverStr));
131static char_u *serverMakeName __ARGS((char_u *arg, char *cmd));
132#endif
133
134
135#ifdef STARTUPTIME
136static FILE *time_fd = NULL;
137#endif
138
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000139/*
140 * Different types of error messages.
141 */
142static char *(main_errors[]) =
143{
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000144 N_("Unknown option argument"),
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000145#define ME_UNKNOWN_OPTION 0
146 N_("Too many edit arguments"),
147#define ME_TOO_MANY_ARGS 1
148 N_("Argument missing after"),
149#define ME_ARG_MISSING 2
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000150 N_("Garbage after option argument"),
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000151#define ME_GARBAGE 3
152 N_("Too many \"+command\", \"-c command\" or \"--cmd command\" arguments"),
153#define ME_EXTRA_CMD 4
154 N_("Invalid argument for"),
155#define ME_INVALID_ARG 5
156};
157
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000158#ifndef PROTO /* don't want a prototype for main() */
159 int
160# ifdef VIMDLL
161_export
162# endif
163# ifdef FEAT_GUI_MSWIN
164# ifdef __BORLANDC__
165_cdecl
166# endif
167VimMain
168# else
169main
170# endif
171(argc, argv)
172 int argc;
173 char **argv;
174{
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000175 char_u *fname = NULL; /* file name from command line */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000176 mparm_T params; /* various parameters passed between
177 * main() and other functions. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000178
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000179 /*
180 * Do any system-specific initialisations. These can NOT use IObuff or
181 * NameBuff. Thus emsg2() cannot be called!
182 */
183 mch_early_init();
184
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000185 /* Many variables are in "params" so that we can pass them to invoked
186 * functions without a lot of arguments. "argc" and "argv" are also
187 * copied, so that they can be changed. */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000188 vim_memset(&params, 0, sizeof(params));
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000189 params.argc = argc;
190 params.argv = argv;
191 params.want_full_screen = TRUE;
192#ifdef FEAT_EVAL
193 params.use_debug_break_level = -1;
194#endif
195#ifdef FEAT_WINDOWS
196 params.window_count = -1;
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000197#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +0000198
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000199#ifdef FEAT_TCL
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000200 vim_tcl_init(params.argv[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000201#endif
202
203#ifdef MEM_PROFILE
204 atexit(vim_mem_profile_dump);
205#endif
206
207#ifdef STARTUPTIME
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000208 time_fd = mch_fopen(STARTUPTIME, "a");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000209 TIME_MSG("--- VIM STARTING ---");
210#endif
Bram Moolenaarca003e12006-03-17 23:19:38 +0000211 starttime = time(NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000212
213#ifdef __EMX__
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000214 _wildcard(&params.argc, &params.argv);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000215#endif
216
217#ifdef FEAT_MBYTE
218 (void)mb_init(); /* init mb_bytelen_tab[] to ones */
219#endif
Bram Moolenaardcaf10e2005-01-21 11:55:25 +0000220#ifdef FEAT_EVAL
221 eval_init(); /* init global variables */
222#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000223
224#ifdef __QNXNTO__
225 qnx_init(); /* PhAttach() for clipboard, (and gui) */
226#endif
227
228#ifdef MAC_OS_CLASSIC
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000229 /* Prepare for possibly starting GUI sometime */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000230 /* Macintosh needs this before any memory is allocated. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000231 gui_prepare(&params.argc, params.argv);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000232 TIME_MSG("GUI prepared");
233#endif
234
235 /* Init the table of Normal mode commands. */
236 init_normal_cmds();
237
238#if defined(HAVE_DATE_TIME) && defined(VMS) && defined(VAXC)
Bram Moolenaar58d98232005-07-23 22:25:46 +0000239 make_version(); /* Construct the long version string. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000240#endif
241
242 /*
243 * Allocate space for the generic buffers (needed for set_init_1() and
244 * EMSG2()).
245 */
246 if ((IObuff = alloc(IOSIZE)) == NULL
247 || (NameBuff = alloc(MAXPATHL)) == NULL)
248 mch_exit(0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000249 TIME_MSG("Allocated generic buffers");
250
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000251#ifdef NBDEBUG
252 /* Wait a moment for debugging NetBeans. Must be after allocating
253 * NameBuff. */
254 nbdebug_log_init("SPRO_GVIM_DEBUG", "SPRO_GVIM_DLEVEL");
255 nbdebug_wait(WT_ENV | WT_WAIT | WT_STOP, "SPRO_GVIM_WAIT", 20);
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000256 TIME_MSG("NetBeans debug wait");
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000257#endif
258
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000259#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
260 /*
261 * Setup to use the current locale (for ctype() and many other things).
262 * NOTE: Translated messages with encodings other than latin1 will not
263 * work until set_init_1() has been called!
264 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000265 init_locale();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000266 TIME_MSG("locale set");
267#endif
268
269#ifdef FEAT_GUI
270 gui.dofork = TRUE; /* default is to use fork() */
271#endif
272
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000273 /*
Bram Moolenaar58d98232005-07-23 22:25:46 +0000274 * Do a first scan of the arguments in "argv[]":
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000275 * -display or --display
Bram Moolenaar58d98232005-07-23 22:25:46 +0000276 * --server...
277 * --socketid
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000278 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000279 early_arg_scan(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000280
281#ifdef FEAT_SUN_WORKSHOP
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000282 findYourself(params.argv[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000283#endif
284#if defined(FEAT_GUI) && !defined(MAC_OS_CLASSIC)
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000285 /* Prepare for possibly starting GUI sometime */
286 gui_prepare(&params.argc, params.argv);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000287 TIME_MSG("GUI prepared");
288#endif
289
290#ifdef FEAT_CLIPBOARD
291 clip_init(FALSE); /* Initialise clipboard stuff */
292 TIME_MSG("clipboard setup");
293#endif
294
295 /*
296 * Check if we have an interactive window.
297 * On the Amiga: If there is no window, we open one with a newcli command
298 * (needed for :! to * work). mch_check_win() will also handle the -d or
299 * -dev argument.
300 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000301 params.stdout_isatty = (mch_check_win(params.argc, params.argv) != FAIL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000302 TIME_MSG("window checked");
303
304 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000305 * Allocate the first window and buffer.
306 * Can't do anything without it, exit when it fails.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000307 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000308 if (win_alloc_first() == FAIL)
309 mch_exit(0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000310
311 init_yank(); /* init yank buffers */
312
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000313 alist_init(&global_alist); /* Init the argument list to empty. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000314
315 /*
316 * Set the default values for the options.
317 * NOTE: Non-latin1 translated messages are working only after this,
318 * because this is where "has_mbyte" will be set, which is used by
319 * msg_outtrans_len_attr().
320 * First find out the home directory, needed to expand "~" in options.
321 */
322 init_homedir(); /* find real value of $HOME */
323 set_init_1();
324 TIME_MSG("inits 1");
325
326#ifdef FEAT_EVAL
327 set_lang_var(); /* set v:lang and v:ctype */
328#endif
329
330#ifdef FEAT_CLIENTSERVER
331 /*
332 * Do the client-server stuff, unless "--servername ''" was used.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000333 * This may exit Vim if the command was sent to the server.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000334 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000335 exec_on_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000336#endif
337
338 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000339 * Figure out the way to work from the command name argv[0].
340 * "vimdiff" starts diff mode, "rvim" sets "restricted", etc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000341 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000342 parse_command_name(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000343
344 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000345 * Process the command line arguments. File names are put in the global
346 * argument list "global_alist".
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000347 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000348 command_line_scan(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000349 TIME_MSG("parsing arguments");
350
351 /*
352 * On some systems, when we compile with the GUI, we always use it. On Mac
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000353 * there is no terminal version, and on Windows we can't fork one off with
354 * :gui.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000355 */
356#ifdef ALWAYS_USE_GUI
357 gui.starting = TRUE;
358#else
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000359# if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000360 /*
361 * Check if the GUI can be started. Reset gui.starting if not.
362 * Don't know about other systems, stay on the safe side and don't check.
363 */
364 if (gui.starting && gui_init_check() == FAIL)
365 {
366 gui.starting = FALSE;
367
368 /* When running "evim" or "gvim -y" we need the menus, exit if we
369 * don't have them. */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000370 if (params.evim_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000371 mch_exit(1);
372 }
373# endif
374#endif
375
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000376 if (GARGCOUNT > 0)
377 {
378#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
379 /*
380 * Expand wildcards in file names.
381 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000382 if (!params.literal)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000383 {
384 /* Temporarily add '(' and ')' to 'isfname'. These are valid
385 * filename characters but are excluded from 'isfname' to make
386 * "gf" work on a file name in parenthesis (e.g.: see vim.h). */
387 do_cmdline_cmd((char_u *)":set isf+=(,)");
Bram Moolenaar86b68352004-12-27 21:59:20 +0000388 alist_expand(NULL, 0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000389 do_cmdline_cmd((char_u *)":set isf&");
390 }
391#endif
392 fname = alist_name(&GARGLIST[0]);
393 }
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000394
395#if defined(WIN32) && defined(FEAT_MBYTE)
396 {
397 extern void set_alist_count(void);
398
399 /* Remember the number of entries in the argument list. If it changes
400 * we don't react on setting 'encoding'. */
401 set_alist_count();
402 }
403#endif
404
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000405#ifdef MSWIN
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000406 if (GARGCOUNT == 1 && params.full_path)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000407 {
408 /*
409 * If there is one filename, fully qualified, we have very probably
410 * been invoked from explorer, so change to the file's directory.
411 * Hint: to avoid this when typing a command use a forward slash.
412 * If the cd fails, it doesn't matter.
413 */
414 (void)vim_chdirfile(fname);
415 }
416#endif
417 TIME_MSG("expanding arguments");
418
419#ifdef FEAT_DIFF
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000420 if (params.diff_mode && params.window_count == -1)
421 params.window_count = 0; /* open up to 3 windows */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000422#endif
423
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000424 /* Don't redraw until much later. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000425 ++RedrawingDisabled;
426
427 /*
428 * When listing swap file names, don't do cursor positioning et. al.
429 */
430 if (recoverymode && fname == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000431 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000432
433 /*
434 * When certain to start the GUI, don't check capabilities of terminal.
435 * For GTK we can't be sure, but when started from the desktop it doesn't
436 * make sense to try using a terminal.
437 */
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000438#if defined(ALWAYS_USE_GUI) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000439 if (gui.starting
440# ifdef FEAT_GUI_GTK
441 && !isatty(2)
442# endif
443 )
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000444 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000445#endif
446
447#if defined(FEAT_GUI_MAC) && defined(MACOS_X_UNIX)
448 /* When the GUI is started from Finder, need to display messages in a
449 * message box. isatty(2) returns TRUE anyway, thus we need to check the
450 * name to know we're not started from a terminal. */
451 if (gui.starting && (!isatty(2) || strcmp("/dev/console", ttyname(2)) == 0))
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000452 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000453#endif
454
455 /*
456 * mch_init() sets up the terminal (window) for use. This must be
457 * done after resetting full_screen, otherwise it may move the cursor
458 * (MSDOS).
459 * Note that we may use mch_exit() before mch_init()!
460 */
461 mch_init();
462 TIME_MSG("shell init");
463
464#ifdef USE_XSMP
465 /*
466 * For want of anywhere else to do it, try to connect to xsmp here.
467 * Fitting it in after gui_mch_init, but before gui_init (via termcapinit).
468 * Hijacking -X 'no X connection' to also disable XSMP connection as that
469 * has a similar delay upon failure.
470 * Only try if SESSION_MANAGER is set to something non-null.
471 */
472 if (!x_no_connect)
473 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000474 char *p = getenv("SESSION_MANAGER");
475
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000476 if (p != NULL && *p != NUL)
477 {
478 xsmp_init();
479 TIME_MSG("xsmp init");
480 }
481 }
482#endif
483
484 /*
485 * Print a warning if stdout is not a terminal.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000486 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000487 check_tty(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000488
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000489 /* This message comes before term inits, but after setting "silent_mode"
490 * when the input is not a tty. */
491 if (GARGCOUNT > 1 && !silent_mode)
492 printf(_("%d files to edit\n"), GARGCOUNT);
493
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000494 if (params.want_full_screen && !silent_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000495 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000496 termcapinit(params.term); /* set terminal name and get terminal
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000497 capabilities (will set full_screen) */
498 screen_start(); /* don't know where cursor is now */
499 TIME_MSG("Termcap init");
500 }
501
502 /*
503 * Set the default values for the options that use Rows and Columns.
504 */
505 ui_get_shellsize(); /* inits Rows and Columns */
506#ifdef FEAT_NETBEANS_INTG
507 if (usingNetbeans)
508 Columns += 2; /* leave room for glyph gutter */
509#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000510 win_init_size();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000511#ifdef FEAT_DIFF
512 /* Set the 'diff' option now, so that it can be checked for in a .vimrc
513 * file. There is no buffer yet though. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000514 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000515 diff_win_options(firstwin, FALSE);
516#endif
517
518 cmdline_row = Rows - p_ch;
519 msg_row = cmdline_row;
520 screenalloc(FALSE); /* allocate screen buffers */
521 set_init_2();
522 TIME_MSG("inits 2");
523
524 msg_scroll = TRUE;
525 no_wait_return = TRUE;
526
527 init_mappings(); /* set up initial mappings */
528
529 init_highlight(TRUE, FALSE); /* set the default highlight groups */
530 TIME_MSG("init highlight");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000531
532#ifdef FEAT_EVAL
533 /* Set the break level after the terminal is initialized. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000534 debug_break_level = params.use_debug_break_level;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000535#endif
536
Bram Moolenaar58d98232005-07-23 22:25:46 +0000537 /* Execute --cmd arguments. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000538 exe_pre_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000539
Bram Moolenaar58d98232005-07-23 22:25:46 +0000540 /* Source startup scripts. */
541 source_startup_scripts(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000542
543#ifdef FEAT_EVAL
544 /*
545 * Read all the plugin files.
546 * Only when compiled with +eval, since most plugins need it.
547 */
548 if (p_lpl)
549 {
Bram Moolenaar07d4d732005-10-03 22:04:08 +0000550 source_runtime((char_u *)"plugin/**/*.vim", TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000551 TIME_MSG("loading plugins");
552 }
553#endif
554
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000555#ifdef FEAT_DIFF
556 /* Decide about window layout for diff mode after reading vimrc. */
557 if (params.diff_mode && params.window_layout == 0)
558 {
559 if (diffopt_horizontal())
560 params.window_layout = WIN_HOR; /* use horizontal split */
561 else
562 params.window_layout = WIN_VER; /* use vertical split */
563 }
564#endif
565
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000566 /*
567 * Recovery mode without a file name: List swap files.
568 * This uses the 'dir' option, therefore it must be after the
569 * initializations.
570 */
571 if (recoverymode && fname == NULL)
572 {
573 recover_names(NULL, TRUE, 0);
574 mch_exit(0);
575 }
576
577 /*
578 * Set a few option defaults after reading .vimrc files:
579 * 'title' and 'icon', Unix: 'shellpipe' and 'shellredir'.
580 */
581 set_init_3();
582 TIME_MSG("inits 3");
583
584 /*
585 * "-n" argument: Disable swap file by setting 'updatecount' to 0.
586 * Note that this overrides anything from a vimrc file.
587 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000588 if (params.no_swap_file)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000589 p_uc = 0;
590
591#ifdef FEAT_FKMAP
592 if (curwin->w_p_rl && p_altkeymap)
593 {
594 p_hkmap = FALSE; /* Reset the Hebrew keymap mode */
595# ifdef FEAT_ARABIC
596 curwin->w_p_arab = FALSE; /* Reset the Arabic keymap mode */
597# endif
598 p_fkmap = TRUE; /* Set the Farsi keymap mode */
599 }
600#endif
601
602#ifdef FEAT_GUI
603 if (gui.starting)
604 {
605#if defined(UNIX) || defined(VMS)
606 /* When something caused a message from a vimrc script, need to output
607 * an extra newline before the shell prompt. */
608 if (did_emsg || msg_didout)
609 putchar('\n');
610#endif
611
612 gui_start(); /* will set full_screen to TRUE */
613 TIME_MSG("starting GUI");
614
615 /* When running "evim" or "gvim -y" we need the menus, exit if we
616 * don't have them. */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000617 if (!gui.in_use && params.evim_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000618 mch_exit(1);
619 }
620#endif
621
622#ifdef SPAWNO /* special MSDOS swapping library */
623 init_SPAWNO("", SWAP_ANY);
624#endif
625
626#ifdef FEAT_VIMINFO
627 /*
628 * Read in registers, history etc, but not marks, from the viminfo file
629 */
630 if (*p_viminfo != NUL)
631 {
632 read_viminfo(NULL, TRUE, FALSE, FALSE);
633 TIME_MSG("reading viminfo");
634 }
635#endif
636
637#ifdef FEAT_QUICKFIX
638 /*
639 * "-q errorfile": Load the error file now.
640 * If the error file can't be read, exit before doing anything else.
641 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000642 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000643 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000644 if (params.use_ef != NULL)
645 set_string_option_direct((char_u *)"ef", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000646 params.use_ef, OPT_FREE, SID_CARG);
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000647 if (qf_init(NULL, p_ef, p_efm, TRUE) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000648 {
649 out_char('\n');
650 mch_exit(3);
651 }
652 TIME_MSG("reading errorfile");
653 }
654#endif
655
656 /*
657 * Start putting things on the screen.
658 * Scroll screen down before drawing over it
659 * Clear screen now, so file message will not be cleared.
660 */
661 starting = NO_BUFFERS;
662 no_wait_return = FALSE;
663 if (!exmode_active)
664 msg_scroll = FALSE;
665
666#ifdef FEAT_GUI
667 /*
668 * This seems to be required to make callbacks to be called now, instead
669 * of after things have been put on the screen, which then may be deleted
670 * when getting a resize callback.
671 * For the Mac this handles putting files dropped on the Vim icon to
672 * global_alist.
673 */
674 if (gui.in_use)
675 {
676# ifdef FEAT_SUN_WORKSHOP
677 if (!usingSunWorkShop)
678# endif
679 gui_wait_for_chars(50L);
680 TIME_MSG("GUI delay");
681 }
682#endif
683
684#if defined(FEAT_GUI_PHOTON) && defined(FEAT_CLIPBOARD)
685 qnx_clip_init();
686#endif
687
688#ifdef FEAT_XCLIPBOARD
689 /* Start using the X clipboard, unless the GUI was started. */
690# ifdef FEAT_GUI
691 if (!gui.in_use)
692# endif
693 {
694 setup_term_clip();
695 TIME_MSG("setup clipboard");
696 }
697#endif
698
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000699#ifdef FEAT_CLIENTSERVER
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000700 /* Prepare for being a Vim server. */
701 prepare_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000702#endif
703
704 /*
705 * If "-" argument given: Read file from stdin.
706 * Do this before starting Raw mode, because it may change things that the
707 * writing end of the pipe doesn't like, e.g., in case stdin and stderr
708 * are the same terminal: "cat | vim -".
709 * Using autocommands here may cause trouble...
710 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000711 if (params.edit_type == EDIT_STDIN && !recoverymode)
712 read_stdin();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000713
714#if defined(UNIX) || defined(VMS)
715 /* When switching screens and something caused a message from a vimrc
716 * script, need to output an extra newline on exit. */
717 if ((did_emsg || msg_didout) && *T_TI != NUL)
718 newline_on_exit = TRUE;
719#endif
720
721 /*
722 * When done something that is not allowed or error message call
723 * wait_return. This must be done before starttermcap(), because it may
724 * switch to another screen. It must be done after settmode(TMODE_RAW),
725 * because we want to react on a single key stroke.
726 * Call settmode and starttermcap here, so the T_KS and T_TI may be
727 * defined by termcapinit and redifined in .exrc.
728 */
729 settmode(TMODE_RAW);
730 TIME_MSG("setting raw mode");
731
732 if (need_wait_return || msg_didany)
733 {
734 wait_return(TRUE);
735 TIME_MSG("waiting for return");
736 }
737
738 starttermcap(); /* start termcap if not done by wait_return() */
739 TIME_MSG("start termcap");
740
741#ifdef FEAT_MOUSE
742 setmouse(); /* may start using the mouse */
743#endif
744 if (scroll_region)
745 scroll_region_reset(); /* In case Rows changed */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000746 scroll_start(); /* may scroll the screen to the right position */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000747
748 /*
749 * Don't clear the screen when starting in Ex mode, unless using the GUI.
750 */
751 if (exmode_active
752#ifdef FEAT_GUI
753 && !gui.in_use
754#endif
755 )
756 must_redraw = CLEAR;
757 else
758 {
759 screenclear(); /* clear screen */
760 TIME_MSG("clearing screen");
761 }
762
763#ifdef FEAT_CRYPT
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000764 if (params.ask_for_key)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000765 {
766 (void)get_crypt_key(TRUE, TRUE);
767 TIME_MSG("getting crypt key");
768 }
769#endif
770
771 no_wait_return = TRUE;
772
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000773 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000774 * Create the requested number of windows and edit buffers in them.
775 * Also does recovery if "recoverymode" set.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000776 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000777 create_windows(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000778 TIME_MSG("opening buffers");
779
780 /* Ex starts at last line of the file */
781 if (exmode_active)
782 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
783
784#ifdef FEAT_AUTOCMD
785 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
786 TIME_MSG("BufEnter autocommands");
787#endif
788 setpcmark();
789
790#ifdef FEAT_QUICKFIX
791 /*
792 * When started with "-q errorfile" jump to first error now.
793 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000794 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000795 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000796 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000797 TIME_MSG("jump to first error");
798 }
799#endif
800
801#ifdef FEAT_WINDOWS
802 /*
803 * If opened more than one window, start editing files in the other
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000804 * windows.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000805 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000806 edit_buffers(&params);
807#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000808
809#ifdef FEAT_DIFF
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000810 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000811 {
812 win_T *wp;
813
814 /* set options in each window for "vimdiff". */
815 for (wp = firstwin; wp != NULL; wp = wp->w_next)
816 diff_win_options(wp, TRUE);
817 }
818#endif
819
820 /*
821 * Shorten any of the filenames, but only when absolute.
822 */
823 shorten_fnames(FALSE);
824
825 /*
826 * Need to jump to the tag before executing the '-c command'.
827 * Makes "vim -c '/return' -t main" work.
828 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000829 if (params.tagname != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000830 {
Bram Moolenaar146522e2005-12-16 21:55:46 +0000831#if defined(HAS_SWAP_EXISTS_ACTION)
832 swap_exists_did_quit = FALSE;
833#endif
834
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000835 vim_snprintf((char *)IObuff, IOSIZE, "ta %s", params.tagname);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000836 do_cmdline_cmd(IObuff);
837 TIME_MSG("jumping to tag");
Bram Moolenaar146522e2005-12-16 21:55:46 +0000838
839#if defined(HAS_SWAP_EXISTS_ACTION)
840 /* If the user doesn't want to edit the file then we quit here. */
841 if (swap_exists_did_quit)
842 getout(1);
843#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000844 }
845
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000846 /* Execute any "+", "-c" and "-S" arguments. */
847 if (params.n_commands > 0)
848 exe_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000849
850 RedrawingDisabled = 0;
851 redraw_all_later(NOT_VALID);
852 no_wait_return = FALSE;
853 starting = 0;
854
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000855#ifdef FEAT_TERMRESPONSE
856 /* Requesting the termresponse is postponed until here, so that a "-c q"
857 * argument doesn't make it appear in the shell Vim was started from. */
858 may_req_termresponse();
859#endif
860
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000861 /* start in insert mode */
862 if (p_im)
863 need_start_insertmode = TRUE;
864
865#ifdef FEAT_AUTOCMD
866 apply_autocmds(EVENT_VIMENTER, NULL, NULL, FALSE, curbuf);
867 TIME_MSG("VimEnter autocommands");
868#endif
869
870#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
871 /* When a startup script or session file setup for diff'ing and
872 * scrollbind, sync the scrollbind now. */
873 if (curwin->w_p_diff && curwin->w_p_scb)
874 {
875 update_topline();
876 check_scrollbind((linenr_T)0, 0L);
877 TIME_MSG("diff scrollbinding");
878 }
879#endif
880
881#if defined(WIN3264) && !defined(FEAT_GUI_W32)
882 mch_set_winsize_now(); /* Allow winsize changes from now on */
883#endif
884
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000885#if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
886 /* When tab pages were created, may need to update the tab pages line and
887 * scrollbars. This is skipped while creating them. */
888 if (first_tabpage->tp_next != NULL)
889 {
890 out_flush();
891 gui_init_which_components(NULL);
892 gui_update_scrollbars(TRUE);
893 }
894 need_mouse_correct = TRUE;
895#endif
896
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000897 /* If ":startinsert" command used, stuff a dummy command to be able to
898 * call normal_cmd(), which will then start Insert mode. */
899 if (restart_edit != 0)
Bram Moolenaarebefac62005-12-28 22:39:57 +0000900 stuffcharReadbuff(K_NOP);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000901
902#ifdef FEAT_NETBEANS_INTG
903 if (usingNetbeans)
904 /* Tell the client that it can start sending commands. */
905 netbeans_startup_done();
906#endif
907
908 TIME_MSG("before starting main loop");
909
910 /*
911 * Call the main command loop. This never returns.
912 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000913 main_loop(FALSE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000914
915 return 0;
916}
917#endif /* PROTO */
918
919/*
920 * Main loop: Execute Normal mode commands until exiting Vim.
921 * Also used to handle commands in the command-line window, until the window
922 * is closed.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000923 * Also used to handle ":visual" command after ":global": execute Normal mode
924 * commands, return when entering Ex mode. "noexmode" is TRUE then.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000925 */
926 void
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000927main_loop(cmdwin, noexmode)
928 int cmdwin; /* TRUE when working in the command-line window */
929 int noexmode; /* TRUE when return on entering Ex mode */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000930{
931 oparg_T oa; /* operator arguments */
932
933#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
934 /* Setup to catch a terminating error from the X server. Just ignore
935 * it, restore the state and continue. This might not always work
936 * properly, but at least we don't exit unexpectedly when the X server
937 * exists while Vim is running in a console. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000938 if (!cmdwin && !noexmode && SETJMP(x_jump_env))
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000939 {
940 State = NORMAL;
941# ifdef FEAT_VISUAL
942 VIsual_active = FALSE;
943# endif
944 got_int = TRUE;
945 need_wait_return = FALSE;
946 global_busy = FALSE;
947 exmode_active = 0;
948 skip_redraw = FALSE;
949 RedrawingDisabled = 0;
950 no_wait_return = 0;
951# ifdef FEAT_EVAL
952 emsg_skip = 0;
953# endif
954 emsg_off = 0;
955# ifdef FEAT_MOUSE
956 setmouse();
957# endif
958 settmode(TMODE_RAW);
959 starttermcap();
960 scroll_start();
961 redraw_later_clear();
962 }
963#endif
964
965 clear_oparg(&oa);
966 while (!cmdwin
967#ifdef FEAT_CMDWIN
968 || cmdwin_result == 0
969#endif
970 )
971 {
972 if (stuff_empty())
973 {
974 did_check_timestamps = FALSE;
975 if (need_check_timestamps)
976 check_timestamps(FALSE);
977 if (need_wait_return) /* if wait_return still needed ... */
978 wait_return(FALSE); /* ... call it now */
979 if (need_start_insertmode && goto_im()
980#ifdef FEAT_VISUAL
981 && !VIsual_active
982#endif
983 )
984 {
985 need_start_insertmode = FALSE;
986 stuffReadbuff((char_u *)"i"); /* start insert mode next */
987 /* skip the fileinfo message now, because it would be shown
988 * after insert mode finishes! */
989 need_fileinfo = FALSE;
990 }
991 }
992 if (got_int && !global_busy)
993 {
994 if (!quit_more)
995 (void)vgetc(); /* flush all buffers */
996 got_int = FALSE;
997 }
998 if (!exmode_active)
999 msg_scroll = FALSE;
1000 quit_more = FALSE;
1001
1002 /*
1003 * If skip redraw is set (for ":" in wait_return()), don't redraw now.
1004 * If there is nothing in the stuff_buffer or do_redraw is TRUE,
1005 * update cursor and redraw.
1006 */
1007 if (skip_redraw || exmode_active)
1008 skip_redraw = FALSE;
1009 else if (do_redraw || stuff_empty())
1010 {
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001011#ifdef FEAT_AUTOCMD
1012 /* Trigger CursorMoved if the cursor moved. */
1013 if (!finish_op && has_cursormoved()
1014 && !equalpos(last_cursormoved, curwin->w_cursor))
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001015 {
1016 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL, FALSE, curbuf);
1017 last_cursormoved = curwin->w_cursor;
1018 }
1019#endif
1020
Bram Moolenaar33aec762006-01-22 23:30:12 +00001021#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
1022 /* Scroll-binding for diff mode may have been postponed until
1023 * here. Avoids doing it for every change. */
1024 if (diff_need_scrollbind)
1025 {
1026 check_scrollbind((linenr_T)0, 0L);
1027 diff_need_scrollbind = FALSE;
1028 }
1029#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001030#if defined(FEAT_FOLDING) && defined(FEAT_VISUAL)
1031 /* Include a closed fold completely in the Visual area. */
1032 foldAdjustVisual();
1033#endif
1034#ifdef FEAT_FOLDING
1035 /*
1036 * When 'foldclose' is set, apply 'foldlevel' to folds that don't
1037 * contain the cursor.
1038 * When 'foldopen' is "all", open the fold(s) under the cursor.
1039 * This may mark the window for redrawing.
1040 */
1041 if (hasAnyFolding(curwin) && !char_avail())
1042 {
1043 foldCheckClose();
1044 if (fdo_flags & FDO_ALL)
1045 foldOpenCursor();
1046 }
1047#endif
1048
1049 /*
1050 * Before redrawing, make sure w_topline is correct, and w_leftcol
1051 * if lines don't wrap, and w_skipcol if lines wrap.
1052 */
1053 update_topline();
1054 validate_cursor();
1055
1056#ifdef FEAT_VISUAL
1057 if (VIsual_active)
1058 update_curbuf(INVERTED);/* update inverted part */
1059 else
1060#endif
1061 if (must_redraw)
1062 update_screen(0);
1063 else if (redraw_cmdline || clear_cmdline)
1064 showmode();
1065#ifdef FEAT_WINDOWS
1066 redraw_statuslines();
1067#endif
1068#ifdef FEAT_TITLE
1069 if (need_maketitle)
1070 maketitle();
1071#endif
1072 /* display message after redraw */
1073 if (keep_msg != NULL)
1074 {
1075 char_u *p;
1076
1077 /* msg_attr_keep() will set keep_msg to NULL, must free the
1078 * string here. */
1079 p = keep_msg;
Bram Moolenaar238a5642006-02-21 22:12:05 +00001080 keep_msg = NULL;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001081 msg_attr(p, keep_msg_attr);
1082 vim_free(p);
1083 }
1084 if (need_fileinfo) /* show file info after redraw */
1085 {
1086 fileinfo(FALSE, TRUE, FALSE);
1087 need_fileinfo = FALSE;
1088 }
1089
1090 emsg_on_display = FALSE; /* can delete error message now */
1091 did_emsg = FALSE;
1092 msg_didany = FALSE; /* reset lines_left in msg_start() */
Bram Moolenaar661b1822005-07-28 22:36:45 +00001093 may_clear_sb_text(); /* clear scroll-back text on next msg */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001094 showruler(FALSE);
1095
1096 setcursor();
1097 cursor_on();
1098
1099 do_redraw = FALSE;
1100 }
1101#ifdef FEAT_GUI
1102 if (need_mouse_correct)
1103 gui_mouse_correct();
1104#endif
1105
1106 /*
1107 * Update w_curswant if w_set_curswant has been set.
1108 * Postponed until here to avoid computing w_virtcol too often.
1109 */
1110 update_curswant();
1111
1112 /*
1113 * If we're invoked as ex, do a round of ex commands.
1114 * Otherwise, get and execute a normal mode command.
1115 */
1116 if (exmode_active)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001117 {
1118 if (noexmode) /* End of ":global/path/visual" commands */
1119 return;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001120 do_exmode(exmode_active == EXMODE_VIM);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001121 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001122 else
1123 normal_cmd(&oa, TRUE);
1124 }
1125}
1126
1127
1128#if defined(USE_XSMP) || defined(FEAT_GUI_MSWIN) || defined(PROTO)
1129/*
1130 * Exit, but leave behind swap files for modified buffers.
1131 */
1132 void
1133getout_preserve_modified(exitval)
1134 int exitval;
1135{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001136# if defined(SIGHUP) && defined(SIG_IGN)
1137 /* Ignore SIGHUP, because a dropped connection causes a read error, which
1138 * makes Vim exit and then handling SIGHUP causes various reentrance
1139 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001140 signal(SIGHUP, SIG_IGN);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001141# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001142
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001143 ml_close_notmod(); /* close all not-modified buffers */
1144 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
1145 ml_close_all(FALSE); /* close all memfiles, without deleting */
1146 getout(exitval); /* exit Vim properly */
1147}
1148#endif
1149
1150
1151/* Exit properly */
1152 void
1153getout(exitval)
1154 int exitval;
1155{
1156#ifdef FEAT_AUTOCMD
1157 buf_T *buf;
1158 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001159 tabpage_T *tp, *next_tp;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001160#endif
1161
1162 exiting = TRUE;
1163
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001164 /* When running in Ex mode an error causes us to exit with a non-zero exit
1165 * code. POSIX requires this, although it's not 100% clear from the
1166 * standard. */
1167 if (exmode_active)
1168 exitval += ex_exitval;
1169
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001170 /* Position the cursor on the last screen line, below all the text */
1171#ifdef FEAT_GUI
1172 if (!gui.in_use)
1173#endif
1174 windgoto((int)Rows - 1, 0);
1175
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +00001176#if defined(FEAT_EVAL) || defined(FEAT_SYN_HL)
1177 /* Optionally print hashtable efficiency. */
1178 hash_debug_results();
1179#endif
1180
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001181#ifdef FEAT_GUI
1182 msg_didany = FALSE;
1183#endif
1184
1185#ifdef FEAT_AUTOCMD
1186 /* Trigger BufWinLeave for all windows, but only once per buffer. */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001187# if defined FEAT_WINDOWS
1188 for (tp = first_tabpage; tp != NULL; tp = next_tp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001189 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001190 next_tp = tp->tp_next;
Bram Moolenaar238a5642006-02-21 22:12:05 +00001191 for (wp = (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001192 ? firstwin : tp->tp_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001193 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001194 buf = wp->w_buffer;
1195 if (buf->b_changedtick != -1)
1196 {
1197 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001198 FALSE, buf);
Bram Moolenaarf740b292006-02-16 22:11:02 +00001199 buf->b_changedtick = -1; /* note that we did it already */
1200 /* start all over, autocommands may mess up the lists */
1201 next_tp = first_tabpage;
1202 break;
1203 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001204 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001205 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001206# else
1207 apply_autocmds(EVENT_BUFWINLEAVE, curbuf, curbuf->b_fname, FALSE, curbuf);
1208# endif
Bram Moolenaar33aec762006-01-22 23:30:12 +00001209
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001210 /* Trigger BufUnload for buffers that are loaded */
1211 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1212 if (buf->b_ml.ml_mfp != NULL)
1213 {
1214 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
1215 FALSE, buf);
1216 if (!buf_valid(buf)) /* autocmd may delete the buffer */
1217 break;
1218 }
1219 apply_autocmds(EVENT_VIMLEAVEPRE, NULL, NULL, FALSE, curbuf);
1220#endif
1221
1222#ifdef FEAT_VIMINFO
1223 if (*p_viminfo != NUL)
1224 /* Write out the registers, history, marks etc, to the viminfo file */
1225 write_viminfo(NULL, FALSE);
1226#endif
1227
1228#ifdef FEAT_AUTOCMD
1229 apply_autocmds(EVENT_VIMLEAVE, NULL, NULL, FALSE, curbuf);
1230#endif
1231
Bram Moolenaar05159a02005-02-26 23:04:13 +00001232#ifdef FEAT_PROFILE
1233 profile_dump();
1234#endif
1235
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001236 if (did_emsg
1237#ifdef FEAT_GUI
1238 || (gui.in_use && msg_didany && p_verbose > 0)
1239#endif
1240 )
1241 {
1242 /* give the user a chance to read the (error) message */
1243 no_wait_return = FALSE;
1244 wait_return(FALSE);
1245 }
1246
1247#ifdef FEAT_AUTOCMD
1248 /* Position the cursor again, the autocommands may have moved it */
1249# ifdef FEAT_GUI
1250 if (!gui.in_use)
1251# endif
1252 windgoto((int)Rows - 1, 0);
1253#endif
1254
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001255#ifdef FEAT_MZSCHEME
1256 mzscheme_end();
1257#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001258#ifdef FEAT_TCL
1259 tcl_end();
1260#endif
1261#ifdef FEAT_RUBY
1262 ruby_end();
1263#endif
1264#ifdef FEAT_PYTHON
1265 python_end();
1266#endif
1267#ifdef FEAT_PERL
1268 perl_end();
1269#endif
1270#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
1271 iconv_end();
1272#endif
1273#ifdef FEAT_NETBEANS_INTG
1274 netbeans_end();
1275#endif
1276
1277 mch_exit(exitval);
1278}
1279
1280/*
1281 * Get a (optional) count for a Vim argument.
1282 */
1283 static int
1284get_number_arg(p, idx, def)
1285 char_u *p; /* pointer to argument */
1286 int *idx; /* index in argument, is incremented */
1287 int def; /* default value */
1288{
1289 if (vim_isdigit(p[*idx]))
1290 {
1291 def = atoi((char *)&(p[*idx]));
1292 while (vim_isdigit(p[*idx]))
1293 *idx = *idx + 1;
1294 }
1295 return def;
1296}
1297
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001298#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1299/*
1300 * Setup to use the current locale (for ctype() and many other things).
1301 */
1302 static void
1303init_locale()
1304{
1305 setlocale(LC_ALL, "");
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001306# ifdef WIN32
1307 /* Apparently MS-Windows printf() may cause a crash when we give it 8-bit
1308 * text while it's expecting text in the current locale. This call avoids
1309 * that. */
1310 setlocale(LC_CTYPE, "C");
1311# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001312
1313# ifdef FEAT_GETTEXT
1314 {
1315 int mustfree = FALSE;
1316 char_u *p;
1317
1318# ifdef DYNAMIC_GETTEXT
1319 /* Initialize the gettext library */
1320 dyn_libintl_init(NULL);
1321# endif
1322 /* expand_env() doesn't work yet, because chartab[] is not initialized
1323 * yet, call vim_getenv() directly */
1324 p = vim_getenv((char_u *)"VIMRUNTIME", &mustfree);
1325 if (p != NULL && *p != NUL)
1326 {
1327 STRCPY(NameBuff, p);
1328 STRCAT(NameBuff, "/lang");
1329 bindtextdomain(VIMPACKAGE, (char *)NameBuff);
1330 }
1331 if (mustfree)
1332 vim_free(p);
1333 textdomain(VIMPACKAGE);
1334 }
1335# endif
1336}
1337#endif
1338
1339/*
1340 * Check for: [r][e][g][vi|vim|view][diff][ex[im]]
1341 * If the executable name starts with "r" we disable shell commands.
1342 * If the next character is "e" we run in Easy mode.
1343 * If the next character is "g" we run the GUI version.
1344 * If the next characters are "view" we start in readonly mode.
1345 * If the next characters are "diff" or "vimdiff" we start in diff mode.
1346 * If the next characters are "ex" we start in Ex mode. If it's followed
1347 * by "im" use improved Ex mode.
1348 */
1349 static void
1350parse_command_name(parmp)
1351 mparm_T *parmp;
1352{
1353 char_u *initstr;
1354
1355 initstr = gettail((char_u *)parmp->argv[0]);
1356
1357#ifdef MACOS_X_UNIX
1358 /* An issue has been seen when launching Vim in such a way that
1359 * $PWD/$ARGV[0] or $ARGV[0] is not the absolute path to the
1360 * executable or a symbolic link of it. Until this issue is resolved
1361 * we prohibit the GUI from being used.
1362 */
1363 if (STRCMP(initstr, parmp->argv[0]) == 0)
1364 disallow_gui = TRUE;
1365
1366 /* TODO: On MacOS X default to gui if argv[0] ends in:
Bram Moolenaar27dc1952006-03-15 23:06:44 +00001367 * /Vim.app/Contents/MacOS/Vim */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001368#endif
1369
1370#ifdef FEAT_EVAL
1371 set_vim_var_string(VV_PROGNAME, initstr, -1);
1372#endif
1373
1374 if (TOLOWER_ASC(initstr[0]) == 'r')
1375 {
1376 restricted = TRUE;
1377 ++initstr;
1378 }
1379
1380 /* Avoid using evim mode for "editor". */
1381 if (TOLOWER_ASC(initstr[0]) == 'e'
1382 && (TOLOWER_ASC(initstr[1]) == 'v'
1383 || TOLOWER_ASC(initstr[1]) == 'g'))
1384 {
1385#ifdef FEAT_GUI
1386 gui.starting = TRUE;
1387#endif
1388 parmp->evim_mode = TRUE;
1389 ++initstr;
1390 }
1391
1392 if (TOLOWER_ASC(initstr[0]) == 'g' || initstr[0] == 'k')
1393 {
1394 main_start_gui();
1395#ifdef FEAT_GUI
1396 ++initstr;
1397#endif
1398 }
1399
1400 if (STRNICMP(initstr, "view", 4) == 0)
1401 {
1402 readonlymode = TRUE;
1403 curbuf->b_p_ro = TRUE;
1404 p_uc = 10000; /* don't update very often */
1405 initstr += 4;
1406 }
1407 else if (STRNICMP(initstr, "vim", 3) == 0)
1408 initstr += 3;
1409
1410 /* Catch "[r][g]vimdiff" and "[r][g]viewdiff". */
1411 if (STRICMP(initstr, "diff") == 0)
1412 {
1413#ifdef FEAT_DIFF
1414 parmp->diff_mode = TRUE;
1415#else
1416 mch_errmsg(_("This Vim was not compiled with the diff feature."));
1417 mch_errmsg("\n");
1418 mch_exit(2);
1419#endif
1420 }
1421
1422 if (STRNICMP(initstr, "ex", 2) == 0)
1423 {
1424 if (STRNICMP(initstr + 2, "im", 2) == 0)
1425 exmode_active = EXMODE_VIM;
1426 else
1427 exmode_active = EXMODE_NORMAL;
1428 change_compatible(TRUE); /* set 'compatible' */
1429 }
1430}
1431
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001432/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00001433 * Get the name of the display, before gui_prepare() removes it from
1434 * argv[]. Used for the xterm-clipboard display.
1435 *
1436 * Also find the --server... arguments and --socketid
1437 */
1438/*ARGSUSED*/
1439 static void
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001440early_arg_scan(parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001441 mparm_T *parmp;
1442{
1443#if defined(FEAT_XCLIPBOARD) || defined(FEAT_CLIENTSERVER)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001444 int argc = parmp->argc;
1445 char **argv = parmp->argv;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001446 int i;
1447
1448 for (i = 1; i < argc; i++)
1449 {
1450 if (STRCMP(argv[i], "--") == 0)
1451 break;
1452# ifdef FEAT_XCLIPBOARD
1453 else if (STRICMP(argv[i], "-display") == 0
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001454# if defined(FEAT_GUI_GTK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001455 || STRICMP(argv[i], "--display") == 0
1456# endif
1457 )
1458 {
1459 if (i == argc - 1)
1460 mainerr_arg_missing((char_u *)argv[i]);
1461 xterm_display = argv[++i];
1462 }
1463# endif
1464# ifdef FEAT_CLIENTSERVER
1465 else if (STRICMP(argv[i], "--servername") == 0)
1466 {
1467 if (i == argc - 1)
1468 mainerr_arg_missing((char_u *)argv[i]);
1469 parmp->serverName_arg = (char_u *)argv[++i];
1470 }
Bram Moolenaareb94e552006-03-11 21:35:11 +00001471 else if (STRICMP(argv[i], "--serverlist") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001472 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001473 else if (STRNICMP(argv[i], "--remote", 8) == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001474 {
1475 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001476# ifdef FEAT_GUI
1477 if (strstr(argv[i], "-wait") != 0)
1478 /* don't fork() when starting the GUI to edit files ourself */
1479 gui.dofork = FALSE;
1480# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001481 }
1482# endif
1483# ifdef FEAT_GUI_GTK
1484 else if (STRICMP(argv[i], "--socketid") == 0)
1485 {
1486 unsigned int socket_id;
1487 int count;
1488
1489 if (i == argc - 1)
1490 mainerr_arg_missing((char_u *)argv[i]);
1491 if (STRNICMP(argv[i+1], "0x", 2) == 0)
1492 count = sscanf(&(argv[i + 1][2]), "%x", &socket_id);
1493 else
1494 count = sscanf(argv[i+1], "%u", &socket_id);
1495 if (count != 1)
1496 mainerr(ME_INVALID_ARG, (char_u *)argv[i]);
1497 else
1498 gtk_socket_id = socket_id;
1499 i++;
1500 }
1501 else if (STRICMP(argv[i], "--echo-wid") == 0)
1502 echo_wid_arg = TRUE;
1503# endif
1504 }
1505#endif
1506}
1507
1508/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001509 * Scan the command line arguments.
1510 */
1511 static void
1512command_line_scan(parmp)
1513 mparm_T *parmp;
1514{
1515 int argc = parmp->argc;
1516 char **argv = parmp->argv;
1517 int argv_idx; /* index in argv[n][] */
1518 int had_minmin = FALSE; /* found "--" argument */
1519 int want_argument; /* option argument with argument */
1520 int c;
Bram Moolenaar231334e2005-07-25 20:46:57 +00001521 char_u *p = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001522 long n;
1523
1524 --argc;
1525 ++argv;
1526 argv_idx = 1; /* active option letter is argv[0][argv_idx] */
1527 while (argc > 0)
1528 {
1529 /*
1530 * "+" or "+{number}" or "+/{pat}" or "+{command}" argument.
1531 */
1532 if (argv[0][0] == '+' && !had_minmin)
1533 {
1534 if (parmp->n_commands >= MAX_ARG_CMDS)
1535 mainerr(ME_EXTRA_CMD, NULL);
1536 argv_idx = -1; /* skip to next argument */
1537 if (argv[0][1] == NUL)
1538 parmp->commands[parmp->n_commands++] = (char_u *)"$";
1539 else
1540 parmp->commands[parmp->n_commands++] = (char_u *)&(argv[0][1]);
1541 }
1542
1543 /*
1544 * Optional argument.
1545 */
1546 else if (argv[0][0] == '-' && !had_minmin)
1547 {
1548 want_argument = FALSE;
1549 c = argv[0][argv_idx++];
1550#ifdef VMS
1551 /*
1552 * VMS only uses upper case command lines. Interpret "-X" as "-x"
1553 * and "-/X" as "-X".
1554 */
1555 if (c == '/')
1556 {
1557 c = argv[0][argv_idx++];
1558 c = TOUPPER_ASC(c);
1559 }
1560 else
1561 c = TOLOWER_ASC(c);
1562#endif
1563 switch (c)
1564 {
1565 case NUL: /* "vim -" read from stdin */
1566 /* "ex -" silent mode */
1567 if (exmode_active)
1568 silent_mode = TRUE;
1569 else
1570 {
1571 if (parmp->edit_type != EDIT_NONE)
1572 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1573 parmp->edit_type = EDIT_STDIN;
1574 read_cmd_fd = 2; /* read from stderr instead of stdin */
1575 }
1576 argv_idx = -1; /* skip to next argument */
1577 break;
1578
1579 case '-': /* "--" don't take any more option arguments */
1580 /* "--help" give help message */
1581 /* "--version" give version message */
1582 /* "--literal" take files literally */
1583 /* "--nofork" don't fork */
1584 /* "--noplugin[s]" skip plugins */
1585 /* "--cmd <cmd>" execute cmd before vimrc */
1586 if (STRICMP(argv[0] + argv_idx, "help") == 0)
1587 usage();
1588 else if (STRICMP(argv[0] + argv_idx, "version") == 0)
1589 {
1590 Columns = 80; /* need to init Columns */
1591 info_message = TRUE; /* use mch_msg(), not mch_errmsg() */
1592 list_version();
1593 msg_putchar('\n');
1594 msg_didout = FALSE;
1595 mch_exit(0);
1596 }
1597 else if (STRNICMP(argv[0] + argv_idx, "literal", 7) == 0)
1598 {
1599#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
1600 parmp->literal = TRUE;
1601#endif
1602 }
1603 else if (STRNICMP(argv[0] + argv_idx, "nofork", 6) == 0)
1604 {
1605#ifdef FEAT_GUI
1606 gui.dofork = FALSE; /* don't fork() when starting GUI */
1607#endif
1608 }
1609 else if (STRNICMP(argv[0] + argv_idx, "noplugin", 8) == 0)
1610 p_lpl = FALSE;
1611 else if (STRNICMP(argv[0] + argv_idx, "cmd", 3) == 0)
1612 {
1613 want_argument = TRUE;
1614 argv_idx += 3;
1615 }
1616#ifdef FEAT_CLIENTSERVER
1617 else if (STRNICMP(argv[0] + argv_idx, "serverlist", 10) == 0)
1618 ; /* already processed -- no arg */
1619 else if (STRNICMP(argv[0] + argv_idx, "servername", 10) == 0
1620 || STRNICMP(argv[0] + argv_idx, "serversend", 10) == 0)
1621 {
1622 /* already processed -- snatch the following arg */
1623 if (argc > 1)
1624 {
1625 --argc;
1626 ++argv;
1627 }
1628 }
1629#endif
1630#ifdef FEAT_GUI_GTK
1631 else if (STRNICMP(argv[0] + argv_idx, "socketid", 8) == 0)
1632 {
1633 /* already processed -- snatch the following arg */
1634 if (argc > 1)
1635 {
1636 --argc;
1637 ++argv;
1638 }
1639 }
1640 else if (STRNICMP(argv[0] + argv_idx, "echo-wid", 8) == 0)
1641 {
1642 /* already processed, skip */
1643 }
1644#endif
1645 else
1646 {
1647 if (argv[0][argv_idx])
1648 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
1649 had_minmin = TRUE;
1650 }
1651 if (!want_argument)
1652 argv_idx = -1; /* skip to next argument */
1653 break;
1654
1655 case 'A': /* "-A" start in Arabic mode */
1656#ifdef FEAT_ARABIC
1657 set_option_value((char_u *)"arabic", 1L, NULL, 0);
1658#else
1659 mch_errmsg(_(e_noarabic));
1660 mch_exit(2);
1661#endif
1662 break;
1663
1664 case 'b': /* "-b" binary mode */
Bram Moolenaar231334e2005-07-25 20:46:57 +00001665 /* Needs to be effective before expanding file names, because
1666 * for Win32 this makes us edit a shortcut file itself,
1667 * instead of the file it links to. */
1668 set_options_bin(curbuf->b_p_bin, 1, 0);
1669 curbuf->b_p_bin = 1; /* binary file I/O */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001670 break;
1671
1672 case 'C': /* "-C" Compatible */
1673 change_compatible(TRUE);
1674 break;
1675
1676 case 'e': /* "-e" Ex mode */
1677 exmode_active = EXMODE_NORMAL;
1678 break;
1679
1680 case 'E': /* "-E" Improved Ex mode */
1681 exmode_active = EXMODE_VIM;
1682 break;
1683
1684 case 'f': /* "-f" GUI: run in foreground. Amiga: open
1685 window directly, not with newcli */
1686#ifdef FEAT_GUI
1687 gui.dofork = FALSE; /* don't fork() when starting GUI */
1688#endif
1689 break;
1690
1691 case 'g': /* "-g" start GUI */
1692 main_start_gui();
1693 break;
1694
1695 case 'F': /* "-F" start in Farsi mode: rl + fkmap set */
1696#ifdef FEAT_FKMAP
1697 curwin->w_p_rl = p_fkmap = TRUE;
1698#else
1699 mch_errmsg(_(e_nofarsi));
1700 mch_exit(2);
1701#endif
1702 break;
1703
1704 case 'h': /* "-h" give help message */
1705#ifdef FEAT_GUI_GNOME
1706 /* Tell usage() to exit for "gvim". */
1707 gui.starting = FALSE;
1708#endif
1709 usage();
1710 break;
1711
1712 case 'H': /* "-H" start in Hebrew mode: rl + hkmap set */
1713#ifdef FEAT_RIGHTLEFT
1714 curwin->w_p_rl = p_hkmap = TRUE;
1715#else
1716 mch_errmsg(_(e_nohebrew));
1717 mch_exit(2);
1718#endif
1719 break;
1720
1721 case 'l': /* "-l" lisp mode, 'lisp' and 'showmatch' on */
1722#ifdef FEAT_LISP
1723 set_option_value((char_u *)"lisp", 1L, NULL, 0);
1724 p_sm = TRUE;
1725#endif
1726 break;
1727
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001728 case 'M': /* "-M" no changes or writing of files */
1729 reset_modifiable();
1730 /* FALLTHROUGH */
1731
1732 case 'm': /* "-m" no writing of files */
1733 p_write = FALSE;
1734 break;
1735
1736 case 'y': /* "-y" easy mode */
1737#ifdef FEAT_GUI
1738 gui.starting = TRUE; /* start GUI a bit later */
1739#endif
1740 parmp->evim_mode = TRUE;
1741 break;
1742
1743 case 'N': /* "-N" Nocompatible */
1744 change_compatible(FALSE);
1745 break;
1746
1747 case 'n': /* "-n" no swap file */
1748 parmp->no_swap_file = TRUE;
1749 break;
1750
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001751 case 'p': /* "-p[N]" open N tab pages */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00001752#ifdef TARGET_API_MAC_OSX
1753 /* For some reason on MacOS X, an argument like:
1754 -psn_0_10223617 is passed in when invoke from Finder
1755 or with the 'open' command */
1756 if (argv[0][argv_idx] == 's')
1757 {
1758 argv_idx = -1; /* bypass full -psn */
1759 main_start_gui();
1760 break;
1761 }
1762#endif
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001763#ifdef FEAT_WINDOWS
1764 /* default is 0: open window for each file */
1765 parmp->window_count = get_number_arg((char_u *)argv[0],
1766 &argv_idx, 0);
1767 parmp->window_layout = WIN_TABS;
1768#endif
1769 break;
1770
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001771 case 'o': /* "-o[N]" open N horizontal split windows */
1772#ifdef FEAT_WINDOWS
1773 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00001774 parmp->window_count = get_number_arg((char_u *)argv[0],
1775 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001776 parmp->window_layout = WIN_HOR;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001777#endif
1778 break;
1779
1780 case 'O': /* "-O[N]" open N vertical split windows */
1781#if defined(FEAT_VERTSPLIT) && defined(FEAT_WINDOWS)
1782 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00001783 parmp->window_count = get_number_arg((char_u *)argv[0],
1784 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001785 parmp->window_layout = WIN_VER;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001786#endif
1787 break;
1788
1789#ifdef FEAT_QUICKFIX
1790 case 'q': /* "-q" QuickFix mode */
1791 if (parmp->edit_type != EDIT_NONE)
1792 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1793 parmp->edit_type = EDIT_QF;
1794 if (argv[0][argv_idx]) /* "-q{errorfile}" */
1795 {
1796 parmp->use_ef = (char_u *)argv[0] + argv_idx;
1797 argv_idx = -1;
1798 }
1799 else if (argc > 1) /* "-q {errorfile}" */
1800 want_argument = TRUE;
1801 break;
1802#endif
1803
1804 case 'R': /* "-R" readonly mode */
1805 readonlymode = TRUE;
1806 curbuf->b_p_ro = TRUE;
1807 p_uc = 10000; /* don't update very often */
1808 break;
1809
1810 case 'r': /* "-r" recovery mode */
1811 case 'L': /* "-L" recovery mode */
1812 recoverymode = 1;
1813 break;
1814
1815 case 's':
1816 if (exmode_active) /* "-s" silent (batch) mode */
1817 silent_mode = TRUE;
1818 else /* "-s {scriptin}" read from script file */
1819 want_argument = TRUE;
1820 break;
1821
1822 case 't': /* "-t {tag}" or "-t{tag}" jump to tag */
1823 if (parmp->edit_type != EDIT_NONE)
1824 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1825 parmp->edit_type = EDIT_TAG;
1826 if (argv[0][argv_idx]) /* "-t{tag}" */
1827 {
1828 parmp->tagname = (char_u *)argv[0] + argv_idx;
1829 argv_idx = -1;
1830 }
1831 else /* "-t {tag}" */
1832 want_argument = TRUE;
1833 break;
1834
1835#ifdef FEAT_EVAL
1836 case 'D': /* "-D" Debugging */
1837 parmp->use_debug_break_level = 9999;
1838 break;
1839#endif
1840#ifdef FEAT_DIFF
1841 case 'd': /* "-d" 'diff' */
1842# ifdef AMIGA
1843 /* check for "-dev {device}" */
1844 if (argv[0][argv_idx] == 'e' && argv[0][argv_idx + 1] == 'v')
1845 want_argument = TRUE;
1846 else
1847# endif
1848 parmp->diff_mode = TRUE;
1849 break;
1850#endif
1851 case 'V': /* "-V{N}" Verbose level */
1852 /* default is 10: a little bit verbose */
1853 p_verbose = get_number_arg((char_u *)argv[0], &argv_idx, 10);
1854 if (argv[0][argv_idx] != NUL)
1855 {
1856 set_option_value((char_u *)"verbosefile", 0L,
1857 (char_u *)argv[0] + argv_idx, 0);
1858 argv_idx = STRLEN(argv[0]);
1859 }
1860 break;
1861
1862 case 'v': /* "-v" Vi-mode (as if called "vi") */
1863 exmode_active = 0;
1864#ifdef FEAT_GUI
1865 gui.starting = FALSE; /* don't start GUI */
1866#endif
1867 break;
1868
1869 case 'w': /* "-w{number}" set window height */
1870 /* "-w {scriptout}" write to script */
1871 if (vim_isdigit(((char_u *)argv[0])[argv_idx]))
1872 {
1873 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
1874 set_option_value((char_u *)"window", n, NULL, 0);
1875 break;
1876 }
1877 want_argument = TRUE;
1878 break;
1879
1880#ifdef FEAT_CRYPT
1881 case 'x': /* "-x" encrypted reading/writing of files */
1882 parmp->ask_for_key = TRUE;
1883 break;
1884#endif
1885
1886 case 'X': /* "-X" don't connect to X server */
1887#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
1888 x_no_connect = TRUE;
1889#endif
1890 break;
1891
1892 case 'Z': /* "-Z" restricted mode */
1893 restricted = TRUE;
1894 break;
1895
1896 case 'c': /* "-c{command}" or "-c {command}" execute
1897 command */
1898 if (argv[0][argv_idx] != NUL)
1899 {
1900 if (parmp->n_commands >= MAX_ARG_CMDS)
1901 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar231334e2005-07-25 20:46:57 +00001902 parmp->commands[parmp->n_commands++] = (char_u *)argv[0]
1903 + argv_idx;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001904 argv_idx = -1;
1905 break;
1906 }
1907 /*FALLTHROUGH*/
1908 case 'S': /* "-S {file}" execute Vim script */
1909 case 'i': /* "-i {viminfo}" use for viminfo */
1910#ifndef FEAT_DIFF
1911 case 'd': /* "-d {device}" device (for Amiga) */
1912#endif
1913 case 'T': /* "-T {terminal}" terminal name */
1914 case 'u': /* "-u {vimrc}" vim inits file */
1915 case 'U': /* "-U {gvimrc}" gvim inits file */
1916 case 'W': /* "-W {scriptout}" overwrite */
1917#ifdef FEAT_GUI_W32
1918 case 'P': /* "-P {parent title}" MDI parent */
1919#endif
1920 want_argument = TRUE;
1921 break;
1922
1923 default:
1924 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
1925 }
1926
1927 /*
1928 * Handle option arguments with argument.
1929 */
1930 if (want_argument)
1931 {
1932 /*
1933 * Check for garbage immediately after the option letter.
1934 */
1935 if (argv[0][argv_idx] != NUL)
1936 mainerr(ME_GARBAGE, (char_u *)argv[0]);
1937
1938 --argc;
1939 if (argc < 1 && c != 'S')
1940 mainerr_arg_missing((char_u *)argv[0]);
1941 ++argv;
1942 argv_idx = -1;
1943
1944 switch (c)
1945 {
1946 case 'c': /* "-c {command}" execute command */
1947 case 'S': /* "-S {file}" execute Vim script */
1948 if (parmp->n_commands >= MAX_ARG_CMDS)
1949 mainerr(ME_EXTRA_CMD, NULL);
1950 if (c == 'S')
1951 {
1952 char *a;
1953
1954 if (argc < 1)
1955 /* "-S" without argument: use default session file
1956 * name. */
1957 a = SESSION_FILE;
1958 else if (argv[0][0] == '-')
1959 {
1960 /* "-S" followed by another option: use default
1961 * session file name. */
1962 a = SESSION_FILE;
1963 ++argc;
1964 --argv;
1965 }
1966 else
1967 a = argv[0];
1968 p = alloc((unsigned)(STRLEN(a) + 4));
1969 if (p == NULL)
1970 mch_exit(2);
1971 sprintf((char *)p, "so %s", a);
1972 parmp->cmds_tofree[parmp->n_commands] = TRUE;
1973 parmp->commands[parmp->n_commands++] = p;
1974 }
1975 else
Bram Moolenaar231334e2005-07-25 20:46:57 +00001976 parmp->commands[parmp->n_commands++] =
1977 (char_u *)argv[0];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001978 break;
1979
1980 case '-': /* "--cmd {command}" execute command */
1981 if (parmp->n_pre_commands >= MAX_ARG_CMDS)
1982 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar231334e2005-07-25 20:46:57 +00001983 parmp->pre_commands[parmp->n_pre_commands++] =
1984 (char_u *)argv[0];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001985 break;
1986
1987 /* case 'd': -d {device} is handled in mch_check_win() for the
1988 * Amiga */
1989
1990#ifdef FEAT_QUICKFIX
1991 case 'q': /* "-q {errorfile}" QuickFix mode */
1992 parmp->use_ef = (char_u *)argv[0];
1993 break;
1994#endif
1995
1996 case 'i': /* "-i {viminfo}" use for viminfo */
1997 use_viminfo = (char_u *)argv[0];
1998 break;
1999
2000 case 's': /* "-s {scriptin}" read from script file */
2001 if (scriptin[0] != NULL)
2002 {
2003scripterror:
2004 mch_errmsg(_("Attempt to open script file again: \""));
2005 mch_errmsg(argv[-1]);
2006 mch_errmsg(" ");
2007 mch_errmsg(argv[0]);
2008 mch_errmsg("\"\n");
2009 mch_exit(2);
2010 }
2011 if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL)
2012 {
2013 mch_errmsg(_("Cannot open for reading: \""));
2014 mch_errmsg(argv[0]);
2015 mch_errmsg("\"\n");
2016 mch_exit(2);
2017 }
2018 if (save_typebuf() == FAIL)
2019 mch_exit(2); /* out of memory */
2020 break;
2021
2022 case 't': /* "-t {tag}" */
2023 parmp->tagname = (char_u *)argv[0];
2024 break;
2025
2026 case 'T': /* "-T {terminal}" terminal name */
2027 /*
2028 * The -T term argument is always available and when
2029 * HAVE_TERMLIB is supported it overrides the environment
2030 * variable TERM.
2031 */
2032#ifdef FEAT_GUI
2033 if (term_is_gui((char_u *)argv[0]))
2034 gui.starting = TRUE; /* start GUI a bit later */
2035 else
2036#endif
2037 parmp->term = (char_u *)argv[0];
2038 break;
2039
2040 case 'u': /* "-u {vimrc}" vim inits file */
2041 parmp->use_vimrc = (char_u *)argv[0];
2042 break;
2043
2044 case 'U': /* "-U {gvimrc}" gvim inits file */
2045#ifdef FEAT_GUI
2046 use_gvimrc = (char_u *)argv[0];
2047#endif
2048 break;
2049
2050 case 'w': /* "-w {nr}" 'window' value */
2051 /* "-w {scriptout}" append to script file */
2052 if (vim_isdigit(*((char_u *)argv[0])))
2053 {
2054 argv_idx = 0;
2055 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2056 set_option_value((char_u *)"window", n, NULL, 0);
2057 argv_idx = -1;
2058 break;
2059 }
2060 /*FALLTHROUGH*/
2061 case 'W': /* "-W {scriptout}" overwrite script file */
2062 if (scriptout != NULL)
2063 goto scripterror;
2064 if ((scriptout = mch_fopen(argv[0],
2065 c == 'w' ? APPENDBIN : WRITEBIN)) == NULL)
2066 {
2067 mch_errmsg(_("Cannot open for script output: \""));
2068 mch_errmsg(argv[0]);
2069 mch_errmsg("\"\n");
2070 mch_exit(2);
2071 }
2072 break;
2073
2074#ifdef FEAT_GUI_W32
2075 case 'P': /* "-P {parent title}" MDI parent */
2076 gui_mch_set_parent(argv[0]);
2077 break;
2078#endif
2079 }
2080 }
2081 }
2082
2083 /*
2084 * File name argument.
2085 */
2086 else
2087 {
2088 argv_idx = -1; /* skip to next argument */
2089
2090 /* Check for only one type of editing. */
2091 if (parmp->edit_type != EDIT_NONE && parmp->edit_type != EDIT_FILE)
2092 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2093 parmp->edit_type = EDIT_FILE;
2094
2095#ifdef MSWIN
2096 /* Remember if the argument was a full path before changing
2097 * slashes to backslashes. */
2098 if (argv[0][0] != NUL && argv[0][1] == ':' && argv[0][2] == '\\')
2099 parmp->full_path = TRUE;
2100#endif
2101
2102 /* Add the file to the global argument list. */
2103 if (ga_grow(&global_alist.al_ga, 1) == FAIL
2104 || (p = vim_strsave((char_u *)argv[0])) == NULL)
2105 mch_exit(2);
2106#ifdef FEAT_DIFF
2107 if (parmp->diff_mode && mch_isdir(p) && GARGCOUNT > 0
2108 && !mch_isdir(alist_name(&GARGLIST[0])))
2109 {
2110 char_u *r;
2111
2112 r = concat_fnames(p, gettail(alist_name(&GARGLIST[0])), TRUE);
2113 if (r != NULL)
2114 {
2115 vim_free(p);
2116 p = r;
2117 }
2118 }
2119#endif
2120#if defined(__CYGWIN32__) && !defined(WIN32)
2121 /*
2122 * If vim is invoked by non-Cygwin tools, convert away any
2123 * DOS paths, so things like .swp files are created correctly.
2124 * Look for evidence of non-Cygwin paths before we bother.
2125 * This is only for when using the Unix files.
2126 */
2127 if (strpbrk(p, "\\:") != NULL)
2128 {
2129 char posix_path[PATH_MAX];
2130
2131 cygwin_conv_to_posix_path(p, posix_path);
2132 vim_free(p);
2133 p = vim_strsave(posix_path);
2134 if (p == NULL)
2135 mch_exit(2);
2136 }
2137#endif
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002138
2139#ifdef USE_FNAME_CASE
2140 /* Make the case of the file name match the actual file. */
2141 fname_case(p, 0);
2142#endif
2143
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002144 alist_add(&global_alist, p,
2145#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
Bram Moolenaar231334e2005-07-25 20:46:57 +00002146 parmp->literal ? 2 : 0 /* add buffer nr after exp. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002147#else
2148 2 /* add buffer number now and use curbuf */
2149#endif
2150 );
2151
2152#if defined(FEAT_MBYTE) && defined(WIN32)
2153 {
2154 extern void used_file_arg(char *, int, int);
2155
2156 /* Remember this argument has been added to the argument list.
2157 * Needed when 'encoding' is changed. */
2158 used_file_arg(argv[0], parmp->literal, parmp->full_path);
2159 }
2160#endif
2161 }
2162
2163 /*
2164 * If there are no more letters after the current "-", go to next
2165 * argument. argv_idx is set to -1 when the current argument is to be
2166 * skipped.
2167 */
2168 if (argv_idx <= 0 || argv[0][argv_idx] == NUL)
2169 {
2170 --argc;
2171 ++argv;
2172 argv_idx = 1;
2173 }
2174 }
2175}
2176
2177/*
2178 * Print a warning if stdout is not a terminal.
2179 * When starting in Ex mode and commands come from a file, set Silent mode.
2180 */
2181 static void
2182check_tty(parmp)
2183 mparm_T *parmp;
2184{
2185 int input_isatty; /* is active input a terminal? */
2186
2187 input_isatty = mch_input_isatty();
2188 if (exmode_active)
2189 {
2190 if (!input_isatty)
2191 silent_mode = TRUE;
2192 }
2193 else if (parmp->want_full_screen && (!parmp->stdout_isatty || !input_isatty)
2194#ifdef FEAT_GUI
2195 /* don't want the delay when started from the desktop */
2196 && !gui.starting
2197#endif
2198 )
2199 {
2200#ifdef NBDEBUG
2201 /*
2202 * This shouldn't be necessary. But if I run netbeans with the log
2203 * output coming to the console and XOpenDisplay fails, I get vim
2204 * trying to start with input/output to my console tty. This fills my
2205 * input buffer so fast I can't even kill the process in under 2
2206 * minutes (and it beeps continuosly the whole time :-)
2207 */
2208 if (usingNetbeans && (!parmp->stdout_isatty || !input_isatty))
2209 {
2210 mch_errmsg(_("Vim: Error: Failure to start gvim from NetBeans\n"));
2211 exit(1);
2212 }
2213#endif
2214 if (!parmp->stdout_isatty)
2215 mch_errmsg(_("Vim: Warning: Output is not to a terminal\n"));
2216 if (!input_isatty)
2217 mch_errmsg(_("Vim: Warning: Input is not from a terminal\n"));
2218 out_flush();
2219 if (scriptin[0] == NULL)
2220 ui_delay(2000L, TRUE);
2221 TIME_MSG("Warning delay");
2222 }
2223}
2224
2225/*
2226 * Read text from stdin.
2227 */
2228 static void
2229read_stdin()
2230{
2231 int i;
2232
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002233#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002234 /* When getting the ATTENTION prompt here, use a dialog */
2235 swap_exists_action = SEA_DIALOG;
2236#endif
2237 no_wait_return = TRUE;
2238 i = msg_didany;
2239 set_buflisted(TRUE);
2240 (void)open_buffer(TRUE, NULL); /* create memfile and read file */
2241 no_wait_return = FALSE;
2242 msg_didany = i;
2243 TIME_MSG("reading stdin");
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002244#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002245 check_swap_exists_action();
2246#endif
2247#if !(defined(AMIGA) || defined(MACOS))
2248 /*
2249 * Close stdin and dup it from stderr. Required for GPM to work
2250 * properly, and for running external commands.
2251 * Is there any other system that cannot do this?
2252 */
2253 close(0);
2254 dup(2);
2255#endif
2256}
2257
2258/*
2259 * Create the requested number of windows and edit buffers in them.
2260 * Also does recovery if "recoverymode" set.
2261 */
2262/*ARGSUSED*/
2263 static void
2264create_windows(parmp)
2265 mparm_T *parmp;
2266{
2267#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002268 int rewind;
2269 int done = 0;
2270
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002271 /*
2272 * Create the number of windows that was requested.
2273 */
2274 if (parmp->window_count == -1) /* was not set */
2275 parmp->window_count = 1;
2276 if (parmp->window_count == 0)
2277 parmp->window_count = GARGCOUNT;
2278 if (parmp->window_count > 1)
2279 {
2280 /* Don't change the windows if there was a command in .vimrc that
2281 * already split some windows */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002282 if (parmp->window_layout == 0)
2283 parmp->window_layout = WIN_HOR;
2284 if (parmp->window_layout == WIN_TABS)
2285 {
2286 parmp->window_count = make_tabpages(parmp->window_count);
2287 TIME_MSG("making tab pages");
2288 }
2289 else if (firstwin->w_next == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002290 {
2291 parmp->window_count = make_windows(parmp->window_count,
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002292 parmp->window_layout == WIN_VER);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002293 TIME_MSG("making windows");
2294 }
2295 else
2296 parmp->window_count = win_count();
2297 }
2298 else
2299 parmp->window_count = 1;
2300#endif
2301
2302 if (recoverymode) /* do recover */
2303 {
2304 msg_scroll = TRUE; /* scroll message up */
2305 ml_recover();
2306 if (curbuf->b_ml.ml_mfp == NULL) /* failed */
2307 getout(1);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002308 do_modelines(0); /* do modelines */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002309 }
2310 else
2311 {
2312 /*
2313 * Open a buffer for windows that don't have one yet.
2314 * Commands in the .vimrc might have loaded a file or split the window.
2315 * Watch out for autocommands that delete a window.
2316 */
2317#ifdef FEAT_AUTOCMD
2318 /*
2319 * Don't execute Win/Buf Enter/Leave autocommands here
2320 */
2321 ++autocmd_no_enter;
2322 ++autocmd_no_leave;
2323#endif
2324#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002325 rewind = TRUE;
2326 while (done++ < 1000)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002327 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002328 if (rewind)
2329 {
2330 if (parmp->window_layout == WIN_TABS)
2331 goto_tabpage(1);
2332 else
2333 curwin = firstwin;
2334 }
2335 else if (parmp->window_layout == WIN_TABS)
2336 {
2337 if (curtab->tp_next == NULL)
2338 break;
2339 goto_tabpage(0);
2340 }
2341 else
2342 {
2343 if (curwin->w_next == NULL)
2344 break;
2345 curwin = curwin->w_next;
2346 }
2347 rewind = FALSE;
2348#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002349 curbuf = curwin->w_buffer;
2350 if (curbuf->b_ml.ml_mfp == NULL)
2351 {
2352#ifdef FEAT_FOLDING
2353 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2354 if (p_fdls >= 0)
2355 curwin->w_p_fdl = p_fdls;
2356#endif
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002357#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002358 /* When getting the ATTENTION prompt here, use a dialog */
2359 swap_exists_action = SEA_DIALOG;
2360#endif
2361 set_buflisted(TRUE);
2362 (void)open_buffer(FALSE, NULL); /* create memfile, read file */
2363
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002364#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002365 check_swap_exists_action();
2366#endif
2367#ifdef FEAT_AUTOCMD
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002368 rewind = TRUE; /* start again */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002369#endif
2370 }
2371#ifdef FEAT_WINDOWS
2372 ui_breakcheck();
2373 if (got_int)
2374 {
2375 (void)vgetc(); /* only break the file loading, not the rest */
2376 break;
2377 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002378 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002379#endif
2380#ifdef FEAT_WINDOWS
2381 if (parmp->window_layout == WIN_TABS)
2382 goto_tabpage(1);
2383 else
2384 curwin = firstwin;
2385 curbuf = curwin->w_buffer;
2386#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002387#ifdef FEAT_AUTOCMD
2388 --autocmd_no_enter;
2389 --autocmd_no_leave;
2390#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002391 }
2392}
2393
2394#ifdef FEAT_WINDOWS
2395 /*
2396 * If opened more than one window, start editing files in the other
2397 * windows. make_windows() has already opened the windows.
2398 */
2399 static void
2400edit_buffers(parmp)
2401 mparm_T *parmp;
2402{
2403 int arg_idx; /* index in argument list */
2404 int i;
2405
2406# ifdef FEAT_AUTOCMD
2407 /*
2408 * Don't execute Win/Buf Enter/Leave autocommands here
2409 */
2410 ++autocmd_no_enter;
2411 ++autocmd_no_leave;
2412# endif
2413 arg_idx = 1;
2414 for (i = 1; i < parmp->window_count; ++i)
2415 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002416 if (parmp->window_layout == WIN_TABS)
2417 {
2418 if (curtab->tp_next == NULL) /* just checking */
2419 break;
2420 goto_tabpage(0);
2421 }
2422 else
2423 {
2424 if (curwin->w_next == NULL) /* just checking */
2425 break;
2426 win_enter(curwin->w_next, FALSE);
2427 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002428
2429 /* Only open the file if there is no file in this window yet (that can
2430 * happen when .vimrc contains ":sall") */
2431 if (curbuf == firstwin->w_buffer || curbuf->b_ffname == NULL)
2432 {
2433 curwin->w_arg_idx = arg_idx;
2434 /* edit file from arg list, if there is one */
2435 (void)do_ecmd(0, arg_idx < GARGCOUNT
2436 ? alist_name(&GARGLIST[arg_idx]) : NULL,
2437 NULL, NULL, ECMD_LASTL, ECMD_HIDE);
2438 if (arg_idx == GARGCOUNT - 1)
2439 arg_had_last = TRUE;
2440 ++arg_idx;
2441 }
2442 ui_breakcheck();
2443 if (got_int)
2444 {
2445 (void)vgetc(); /* only break the file loading, not the rest */
2446 break;
2447 }
2448 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002449
2450 if (parmp->window_layout == WIN_TABS)
2451 goto_tabpage(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002452# ifdef FEAT_AUTOCMD
2453 --autocmd_no_enter;
2454# endif
2455 win_enter(firstwin, FALSE); /* back to first window */
2456# ifdef FEAT_AUTOCMD
2457 --autocmd_no_leave;
2458# endif
2459 TIME_MSG("editing files in windows");
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002460 if (parmp->window_count > 1 && parmp->window_layout != WIN_TABS)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002461 win_equal(curwin, FALSE, 'b'); /* adjust heights */
2462}
2463#endif /* FEAT_WINDOWS */
2464
2465/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002466 * Execute the commands from --cmd arguments "cmds[cnt]".
2467 */
2468 static void
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002469exe_pre_commands(parmp)
2470 mparm_T *parmp;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002471{
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002472 char_u **cmds = parmp->pre_commands;
2473 int cnt = parmp->n_pre_commands;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002474 int i;
2475
2476 if (cnt > 0)
2477 {
2478 curwin->w_cursor.lnum = 0; /* just in case.. */
2479 sourcing_name = (char_u *)_("pre-vimrc command line");
2480# ifdef FEAT_EVAL
2481 current_SID = SID_CMDARG;
2482# endif
2483 for (i = 0; i < cnt; ++i)
2484 do_cmdline_cmd(cmds[i]);
2485 sourcing_name = NULL;
2486# ifdef FEAT_EVAL
2487 current_SID = 0;
2488# endif
2489 TIME_MSG("--cmd commands");
2490 }
2491}
2492
2493/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002494 * Execute "+", "-c" and "-S" arguments.
2495 */
2496 static void
2497exe_commands(parmp)
2498 mparm_T *parmp;
2499{
2500 int i;
2501
2502 /*
2503 * We start commands on line 0, make "vim +/pat file" match a
2504 * pattern on line 1. But don't move the cursor when an autocommand
2505 * with g`" was used.
2506 */
2507 msg_scroll = TRUE;
2508 if (parmp->tagname == NULL && curwin->w_cursor.lnum <= 1)
2509 curwin->w_cursor.lnum = 0;
2510 sourcing_name = (char_u *)"command line";
2511#ifdef FEAT_EVAL
2512 current_SID = SID_CARG;
2513#endif
2514 for (i = 0; i < parmp->n_commands; ++i)
2515 {
2516 do_cmdline_cmd(parmp->commands[i]);
2517 if (parmp->cmds_tofree[i])
2518 vim_free(parmp->commands[i]);
2519 }
2520 sourcing_name = NULL;
2521#ifdef FEAT_EVAL
2522 current_SID = 0;
2523#endif
2524 if (curwin->w_cursor.lnum == 0)
2525 curwin->w_cursor.lnum = 1;
2526
2527 if (!exmode_active)
2528 msg_scroll = FALSE;
2529
2530#ifdef FEAT_QUICKFIX
2531 /* When started with "-q errorfile" jump to first error again. */
2532 if (parmp->edit_type == EDIT_QF)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002533 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002534#endif
2535 TIME_MSG("executing command arguments");
2536}
2537
2538/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002539 * Source startup scripts.
2540 */
2541 static void
2542source_startup_scripts(parmp)
2543 mparm_T *parmp;
2544{
2545 int i;
2546
2547 /*
2548 * For "evim" source evim.vim first of all, so that the user can overrule
2549 * any things he doesn't like.
2550 */
2551 if (parmp->evim_mode)
2552 {
2553 (void)do_source((char_u *)EVIM_FILE, FALSE, FALSE);
2554 TIME_MSG("source evim file");
2555 }
2556
2557 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002558 * If -u argument given, use only the initializations from that file and
Bram Moolenaar58d98232005-07-23 22:25:46 +00002559 * nothing else.
2560 */
2561 if (parmp->use_vimrc != NULL)
2562 {
Bram Moolenaar231334e2005-07-25 20:46:57 +00002563 if (STRCMP(parmp->use_vimrc, "NONE") == 0
2564 || STRCMP(parmp->use_vimrc, "NORC") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002565 {
2566#ifdef FEAT_GUI
2567 if (use_gvimrc == NULL) /* don't load gvimrc either */
2568 use_gvimrc = parmp->use_vimrc;
2569#endif
2570 if (parmp->use_vimrc[2] == 'N')
2571 p_lpl = FALSE; /* don't load plugins either */
2572 }
2573 else
2574 {
2575 if (do_source(parmp->use_vimrc, FALSE, FALSE) != OK)
2576 EMSG2(_("E282: Cannot read from \"%s\""), parmp->use_vimrc);
2577 }
2578 }
2579 else if (!silent_mode)
2580 {
2581#ifdef AMIGA
2582 struct Process *proc = (struct Process *)FindTask(0L);
2583 APTR save_winptr = proc->pr_WindowPtr;
2584
2585 /* Avoid a requester here for a volume that doesn't exist. */
2586 proc->pr_WindowPtr = (APTR)-1L;
2587#endif
2588
2589 /*
2590 * Get system wide defaults, if the file name is defined.
2591 */
2592#ifdef SYS_VIMRC_FILE
2593 (void)do_source((char_u *)SYS_VIMRC_FILE, FALSE, FALSE);
2594#endif
Bram Moolenaar1056d982006-03-09 22:37:52 +00002595#ifdef MACOS_X
2596 (void)do_source((char_u *)"$VIMRUNTIME/macmap.vim", FALSE, FALSE);
2597#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00002598
2599 /*
2600 * Try to read initialization commands from the following places:
2601 * - environment variable VIMINIT
2602 * - user vimrc file (s:.vimrc for Amiga, ~/.vimrc otherwise)
2603 * - second user vimrc file ($VIM/.vimrc for Dos)
2604 * - environment variable EXINIT
2605 * - user exrc file (s:.exrc for Amiga, ~/.exrc otherwise)
2606 * - second user exrc file ($VIM/.exrc for Dos)
2607 * The first that exists is used, the rest is ignored.
2608 */
2609 if (process_env((char_u *)"VIMINIT", TRUE) != OK)
2610 {
2611 if (do_source((char_u *)USR_VIMRC_FILE, TRUE, TRUE) == FAIL
2612#ifdef USR_VIMRC_FILE2
2613 && do_source((char_u *)USR_VIMRC_FILE2, TRUE, TRUE) == FAIL
2614#endif
2615#ifdef USR_VIMRC_FILE3
2616 && do_source((char_u *)USR_VIMRC_FILE3, TRUE, TRUE) == FAIL
2617#endif
2618 && process_env((char_u *)"EXINIT", FALSE) == FAIL
2619 && do_source((char_u *)USR_EXRC_FILE, FALSE, FALSE) == FAIL)
2620 {
2621#ifdef USR_EXRC_FILE2
2622 (void)do_source((char_u *)USR_EXRC_FILE2, FALSE, FALSE);
2623#endif
2624 }
2625 }
2626
2627 /*
2628 * Read initialization commands from ".vimrc" or ".exrc" in current
2629 * directory. This is only done if the 'exrc' option is set.
2630 * Because of security reasons we disallow shell and write commands
2631 * now, except for unix if the file is owned by the user or 'secure'
2632 * option has been reset in environment of global ".exrc" or ".vimrc".
2633 * Only do this if VIMRC_FILE is not the same as USR_VIMRC_FILE or
2634 * SYS_VIMRC_FILE.
2635 */
2636 if (p_exrc)
2637 {
2638#if defined(UNIX) || defined(VMS)
2639 /* If ".vimrc" file is not owned by user, set 'secure' mode. */
2640 if (!file_owned(VIMRC_FILE))
2641#endif
2642 secure = p_secure;
2643
2644 i = FAIL;
2645 if (fullpathcmp((char_u *)USR_VIMRC_FILE,
2646 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
2647#ifdef USR_VIMRC_FILE2
2648 && fullpathcmp((char_u *)USR_VIMRC_FILE2,
2649 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
2650#endif
2651#ifdef USR_VIMRC_FILE3
2652 && fullpathcmp((char_u *)USR_VIMRC_FILE3,
2653 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
2654#endif
2655#ifdef SYS_VIMRC_FILE
2656 && fullpathcmp((char_u *)SYS_VIMRC_FILE,
2657 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
2658#endif
2659 )
2660 i = do_source((char_u *)VIMRC_FILE, TRUE, TRUE);
2661
2662 if (i == FAIL)
2663 {
2664#if defined(UNIX) || defined(VMS)
2665 /* if ".exrc" is not owned by user set 'secure' mode */
2666 if (!file_owned(EXRC_FILE))
2667 secure = p_secure;
2668 else
2669 secure = 0;
2670#endif
2671 if ( fullpathcmp((char_u *)USR_EXRC_FILE,
2672 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
2673#ifdef USR_EXRC_FILE2
2674 && fullpathcmp((char_u *)USR_EXRC_FILE2,
2675 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
2676#endif
2677 )
2678 (void)do_source((char_u *)EXRC_FILE, FALSE, FALSE);
2679 }
2680 }
2681 if (secure == 2)
2682 need_wait_return = TRUE;
2683 secure = 0;
2684#ifdef AMIGA
2685 proc->pr_WindowPtr = save_winptr;
2686#endif
2687 }
2688 TIME_MSG("sourcing vimrc file(s)");
2689}
2690
2691/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002692 * Setup to start using the GUI. Exit with an error when not available.
2693 */
2694 static void
2695main_start_gui()
2696{
2697#ifdef FEAT_GUI
2698 gui.starting = TRUE; /* start GUI a bit later */
2699#else
2700 mch_errmsg(_(e_nogvim));
2701 mch_errmsg("\n");
2702 mch_exit(2);
2703#endif
2704}
2705
2706/*
2707 * Get an evironment variable, and execute it as Ex commands.
2708 * Returns FAIL if the environment variable was not executed, OK otherwise.
2709 */
2710 int
2711process_env(env, is_viminit)
2712 char_u *env;
2713 int is_viminit; /* when TRUE, called for VIMINIT */
2714{
2715 char_u *initstr;
2716 char_u *save_sourcing_name;
2717 linenr_T save_sourcing_lnum;
2718#ifdef FEAT_EVAL
2719 scid_T save_sid;
2720#endif
2721
2722 if ((initstr = mch_getenv(env)) != NULL && *initstr != NUL)
2723 {
2724 if (is_viminit)
2725 vimrc_found();
2726 save_sourcing_name = sourcing_name;
2727 save_sourcing_lnum = sourcing_lnum;
2728 sourcing_name = env;
2729 sourcing_lnum = 0;
2730#ifdef FEAT_EVAL
2731 save_sid = current_SID;
2732 current_SID = SID_ENV;
2733#endif
2734 do_cmdline_cmd(initstr);
2735 sourcing_name = save_sourcing_name;
2736 sourcing_lnum = save_sourcing_lnum;
2737#ifdef FEAT_EVAL
2738 current_SID = save_sid;;
2739#endif
2740 return OK;
2741 }
2742 return FAIL;
2743}
2744
2745#if defined(UNIX) || defined(VMS)
2746/*
2747 * Return TRUE if we are certain the user owns the file "fname".
2748 * Used for ".vimrc" and ".exrc".
2749 * Use both stat() and lstat() for extra security.
2750 */
2751 static int
2752file_owned(fname)
2753 char *fname;
2754{
2755 struct stat s;
2756# ifdef UNIX
2757 uid_t uid = getuid();
2758# else /* VMS */
2759 uid_t uid = ((getgid() << 16) | getuid());
2760# endif
2761
2762 return !(mch_stat(fname, &s) != 0 || s.st_uid != uid
2763# ifdef HAVE_LSTAT
2764 || mch_lstat(fname, &s) != 0 || s.st_uid != uid
2765# endif
2766 );
2767}
2768#endif
2769
2770/*
2771 * Give an error message main_errors["n"] and exit.
2772 */
2773 static void
2774mainerr(n, str)
2775 int n; /* one of the ME_ defines */
2776 char_u *str; /* extra argument or NULL */
2777{
2778#if defined(UNIX) || defined(__EMX__) || defined(VMS)
2779 reset_signals(); /* kill us with CTRL-C here, if you like */
2780#endif
2781
2782 mch_errmsg(longVersion);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002783 mch_errmsg("\n");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002784 mch_errmsg(_(main_errors[n]));
2785 if (str != NULL)
2786 {
2787 mch_errmsg(": \"");
2788 mch_errmsg((char *)str);
2789 mch_errmsg("\"");
2790 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002791 mch_errmsg(_("\nMore info with: \"vim -h\"\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002792
2793 mch_exit(1);
2794}
2795
2796 void
2797mainerr_arg_missing(str)
2798 char_u *str;
2799{
2800 mainerr(ME_ARG_MISSING, str);
2801}
2802
2803/*
2804 * print a message with three spaces prepended and '\n' appended.
2805 */
2806 static void
2807main_msg(s)
2808 char *s;
2809{
2810 mch_msg(" ");
2811 mch_msg(s);
2812 mch_msg("\n");
2813}
2814
2815/*
2816 * Print messages for "vim -h" or "vim --help" and exit.
2817 */
2818 static void
2819usage()
2820{
2821 int i;
2822 static char *(use[]) =
2823 {
2824 N_("[file ..] edit specified file(s)"),
2825 N_("- read text from stdin"),
2826 N_("-t tag edit file where tag is defined"),
2827#ifdef FEAT_QUICKFIX
2828 N_("-q [errorfile] edit file with first error")
2829#endif
2830 };
2831
2832#if defined(UNIX) || defined(__EMX__) || defined(VMS)
2833 reset_signals(); /* kill us with CTRL-C here, if you like */
2834#endif
2835
2836 mch_msg(longVersion);
2837 mch_msg(_("\n\nusage:"));
2838 for (i = 0; ; ++i)
2839 {
2840 mch_msg(_(" vim [arguments] "));
2841 mch_msg(_(use[i]));
2842 if (i == (sizeof(use) / sizeof(char_u *)) - 1)
2843 break;
2844 mch_msg(_("\n or:"));
2845 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002846#ifdef VMS
2847 mch_msg(_("where case is ignored prepend / to make flag upper case"));
2848#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002849
2850 mch_msg(_("\n\nArguments:\n"));
2851 main_msg(_("--\t\t\tOnly file names after this"));
2852#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
2853 main_msg(_("--literal\t\tDon't expand wildcards"));
2854#endif
2855#ifdef FEAT_OLE
2856 main_msg(_("-register\t\tRegister this gvim for OLE"));
2857 main_msg(_("-unregister\t\tUnregister gvim for OLE"));
2858#endif
2859#ifdef FEAT_GUI
2860 main_msg(_("-g\t\t\tRun using GUI (like \"gvim\")"));
2861 main_msg(_("-f or --nofork\tForeground: Don't fork when starting GUI"));
2862#endif
2863 main_msg(_("-v\t\t\tVi mode (like \"vi\")"));
2864 main_msg(_("-e\t\t\tEx mode (like \"ex\")"));
2865 main_msg(_("-s\t\t\tSilent (batch) mode (only for \"ex\")"));
2866#ifdef FEAT_DIFF
2867 main_msg(_("-d\t\t\tDiff mode (like \"vimdiff\")"));
2868#endif
2869 main_msg(_("-y\t\t\tEasy mode (like \"evim\", modeless)"));
2870 main_msg(_("-R\t\t\tReadonly mode (like \"view\")"));
2871 main_msg(_("-Z\t\t\tRestricted mode (like \"rvim\")"));
2872 main_msg(_("-m\t\t\tModifications (writing files) not allowed"));
2873 main_msg(_("-M\t\t\tModifications in text not allowed"));
2874 main_msg(_("-b\t\t\tBinary mode"));
2875#ifdef FEAT_LISP
2876 main_msg(_("-l\t\t\tLisp mode"));
2877#endif
2878 main_msg(_("-C\t\t\tCompatible with Vi: 'compatible'"));
2879 main_msg(_("-N\t\t\tNot fully Vi compatible: 'nocompatible'"));
2880 main_msg(_("-V[N]\t\tVerbose level"));
2881 main_msg(_("-D\t\t\tDebugging mode"));
2882 main_msg(_("-n\t\t\tNo swap file, use memory only"));
2883 main_msg(_("-r\t\t\tList swap files and exit"));
2884 main_msg(_("-r (with file name)\tRecover crashed session"));
2885 main_msg(_("-L\t\t\tSame as -r"));
2886#ifdef AMIGA
2887 main_msg(_("-f\t\t\tDon't use newcli to open window"));
2888 main_msg(_("-dev <device>\t\tUse <device> for I/O"));
2889#endif
2890#ifdef FEAT_ARABIC
2891 main_msg(_("-A\t\t\tstart in Arabic mode"));
2892#endif
2893#ifdef FEAT_RIGHTLEFT
2894 main_msg(_("-H\t\t\tStart in Hebrew mode"));
2895#endif
2896#ifdef FEAT_FKMAP
2897 main_msg(_("-F\t\t\tStart in Farsi mode"));
2898#endif
2899 main_msg(_("-T <terminal>\tSet terminal type to <terminal>"));
2900 main_msg(_("-u <vimrc>\t\tUse <vimrc> instead of any .vimrc"));
2901#ifdef FEAT_GUI
2902 main_msg(_("-U <gvimrc>\t\tUse <gvimrc> instead of any .gvimrc"));
2903#endif
2904 main_msg(_("--noplugin\t\tDon't load plugin scripts"));
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002905 main_msg(_("-p[N]\t\tOpen N tab pages (default: one for each file)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002906 main_msg(_("-o[N]\t\tOpen N windows (default: one for each file)"));
2907 main_msg(_("-O[N]\t\tLike -o but split vertically"));
2908 main_msg(_("+\t\t\tStart at end of file"));
2909 main_msg(_("+<lnum>\t\tStart at line <lnum>"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002910 main_msg(_("--cmd <command>\tExecute <command> before loading any vimrc file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002911 main_msg(_("-c <command>\t\tExecute <command> after loading the first file"));
2912 main_msg(_("-S <session>\t\tSource file <session> after loading the first file"));
2913 main_msg(_("-s <scriptin>\tRead Normal mode commands from file <scriptin>"));
2914 main_msg(_("-w <scriptout>\tAppend all typed commands to file <scriptout>"));
2915 main_msg(_("-W <scriptout>\tWrite all typed commands to file <scriptout>"));
2916#ifdef FEAT_CRYPT
2917 main_msg(_("-x\t\t\tEdit encrypted files"));
2918#endif
2919#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
2920# if defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)
2921 main_msg(_("-display <display>\tConnect vim to this particular X-server"));
2922# endif
2923 main_msg(_("-X\t\t\tDo not connect to X server"));
2924#endif
2925#ifdef FEAT_CLIENTSERVER
2926 main_msg(_("--remote <files>\tEdit <files> in a Vim server if possible"));
2927 main_msg(_("--remote-silent <files> Same, don't complain if there is no server"));
2928 main_msg(_("--remote-wait <files> As --remote but wait for files to have been edited"));
2929 main_msg(_("--remote-wait-silent <files> Same, don't complain if there is no server"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00002930# ifdef FEAT_WINDOWS
2931 main_msg(_("--remote-tab <files> As --remote but open tab page for each file"));
2932# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002933 main_msg(_("--remote-send <keys>\tSend <keys> to a Vim server and exit"));
2934 main_msg(_("--remote-expr <expr>\tEvaluate <expr> in a Vim server and print result"));
2935 main_msg(_("--serverlist\t\tList available Vim server names and exit"));
2936 main_msg(_("--servername <name>\tSend to/become the Vim server <name>"));
2937#endif
2938#ifdef FEAT_VIMINFO
2939 main_msg(_("-i <viminfo>\t\tUse <viminfo> instead of .viminfo"));
2940#endif
2941 main_msg(_("-h or --help\tPrint Help (this message) and exit"));
2942 main_msg(_("--version\t\tPrint version information and exit"));
2943
2944#ifdef FEAT_GUI_X11
2945# ifdef FEAT_GUI_MOTIF
2946 mch_msg(_("\nArguments recognised by gvim (Motif version):\n"));
2947# else
2948# ifdef FEAT_GUI_ATHENA
2949# ifdef FEAT_GUI_NEXTAW
2950 mch_msg(_("\nArguments recognised by gvim (neXtaw version):\n"));
2951# else
2952 mch_msg(_("\nArguments recognised by gvim (Athena version):\n"));
2953# endif
2954# endif
2955# endif
2956 main_msg(_("-display <display>\tRun vim on <display>"));
2957 main_msg(_("-iconic\t\tStart vim iconified"));
2958# if 0
2959 main_msg(_("-name <name>\t\tUse resource as if vim was <name>"));
2960 mch_msg(_("\t\t\t (Unimplemented)\n"));
2961# endif
2962 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
2963 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
2964 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
2965 main_msg(_("-boldfont <font>\tUse <font> for bold text"));
2966 main_msg(_("-italicfont <font>\tUse <font> for italic text"));
2967 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
2968 main_msg(_("-borderwidth <width>\tUse a border width of <width> (also: -bw)"));
2969 main_msg(_("-scrollbarwidth <width> Use a scrollbar width of <width> (also: -sw)"));
2970# ifdef FEAT_GUI_ATHENA
2971 main_msg(_("-menuheight <height>\tUse a menu bar height of <height> (also: -mh)"));
2972# endif
2973 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
2974 main_msg(_("+reverse\t\tDon't use reverse video (also: +rv)"));
2975 main_msg(_("-xrm <resource>\tSet the specified resource"));
2976#endif /* FEAT_GUI_X11 */
2977#if defined(FEAT_GUI) && defined(RISCOS)
2978 mch_msg(_("\nArguments recognised by gvim (RISC OS version):\n"));
2979 main_msg(_("--columns <number>\tInitial width of window in columns"));
2980 main_msg(_("--rows <number>\tInitial height of window in rows"));
2981#endif
2982#ifdef FEAT_GUI_GTK
2983 mch_msg(_("\nArguments recognised by gvim (GTK+ version):\n"));
2984 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
2985 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
2986 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
2987 main_msg(_("-display <display>\tRun vim on <display> (also: --display)"));
2988# ifdef HAVE_GTK2
2989 main_msg(_("--role <role>\tSet a unique role to identify the main window"));
2990# endif
2991 main_msg(_("--socketid <xid>\tOpen Vim inside another GTK widget"));
2992#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002993#ifdef FEAT_GUI_W32
2994 main_msg(_("-P <parent title>\tOpen Vim inside parent application"));
2995#endif
2996
2997#ifdef FEAT_GUI_GNOME
2998 /* Gnome gives extra messages for --help if we continue, but not for -h. */
2999 if (gui.starting)
3000 mch_msg("\n");
3001 else
3002#endif
3003 mch_exit(0);
3004}
3005
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00003006#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003007/*
3008 * Check the result of the ATTENTION dialog:
3009 * When "Quit" selected, exit Vim.
3010 * When "Recover" selected, recover the file.
3011 */
3012 static void
3013check_swap_exists_action()
3014{
3015 if (swap_exists_action == SEA_QUIT)
3016 getout(1);
3017 handle_swap_exists(NULL);
3018}
3019#endif
3020
3021#if defined(STARTUPTIME) || defined(PROTO)
3022static void time_diff __ARGS((struct timeval *then, struct timeval *now));
3023
3024static struct timeval prev_timeval;
3025
3026/*
3027 * Save the previous time before doing something that could nest.
3028 * set "*tv_rel" to the time elapsed so far.
3029 */
3030 void
3031time_push(tv_rel, tv_start)
3032 void *tv_rel, *tv_start;
3033{
3034 *((struct timeval *)tv_rel) = prev_timeval;
3035 gettimeofday(&prev_timeval, NULL);
3036 ((struct timeval *)tv_rel)->tv_usec = prev_timeval.tv_usec
3037 - ((struct timeval *)tv_rel)->tv_usec;
3038 ((struct timeval *)tv_rel)->tv_sec = prev_timeval.tv_sec
3039 - ((struct timeval *)tv_rel)->tv_sec;
3040 if (((struct timeval *)tv_rel)->tv_usec < 0)
3041 {
3042 ((struct timeval *)tv_rel)->tv_usec += 1000000;
3043 --((struct timeval *)tv_rel)->tv_sec;
3044 }
3045 *(struct timeval *)tv_start = prev_timeval;
3046}
3047
3048/*
3049 * Compute the previous time after doing something that could nest.
3050 * Subtract "*tp" from prev_timeval;
3051 * Note: The arguments are (void *) to avoid trouble with systems that don't
3052 * have struct timeval.
3053 */
3054 void
3055time_pop(tp)
3056 void *tp; /* actually (struct timeval *) */
3057{
3058 prev_timeval.tv_usec -= ((struct timeval *)tp)->tv_usec;
3059 prev_timeval.tv_sec -= ((struct timeval *)tp)->tv_sec;
3060 if (prev_timeval.tv_usec < 0)
3061 {
3062 prev_timeval.tv_usec += 1000000;
3063 --prev_timeval.tv_sec;
3064 }
3065}
3066
3067 static void
3068time_diff(then, now)
3069 struct timeval *then;
3070 struct timeval *now;
3071{
3072 long usec;
3073 long msec;
3074
3075 usec = now->tv_usec - then->tv_usec;
3076 msec = (now->tv_sec - then->tv_sec) * 1000L + usec / 1000L,
3077 usec = usec % 1000L;
3078 fprintf(time_fd, "%03ld.%03ld", msec, usec >= 0 ? usec : usec + 1000L);
3079}
3080
3081 void
3082time_msg(msg, tv_start)
3083 char *msg;
3084 void *tv_start; /* only for do_source: start time; actually
3085 (struct timeval *) */
3086{
3087 static struct timeval start;
3088 struct timeval now;
3089
3090 if (time_fd != NULL)
3091 {
3092 if (strstr(msg, "STARTING") != NULL)
3093 {
3094 gettimeofday(&start, NULL);
3095 prev_timeval = start;
3096 fprintf(time_fd, "\n\ntimes in msec\n");
3097 fprintf(time_fd, " clock self+sourced self: sourced script\n");
3098 fprintf(time_fd, " clock elapsed: other lines\n\n");
3099 }
3100 gettimeofday(&now, NULL);
3101 time_diff(&start, &now);
3102 if (((struct timeval *)tv_start) != NULL)
3103 {
3104 fprintf(time_fd, " ");
3105 time_diff(((struct timeval *)tv_start), &now);
3106 }
3107 fprintf(time_fd, " ");
3108 time_diff(&prev_timeval, &now);
3109 prev_timeval = now;
3110 fprintf(time_fd, ": %s\n", msg);
3111 }
3112}
3113
3114# ifdef WIN3264
3115/*
3116 * Windows doesn't have gettimeofday(), although it does have struct timeval.
3117 */
3118 int
3119gettimeofday(struct timeval *tv, char *dummy)
3120{
3121 long t = clock();
3122 tv->tv_sec = t / CLOCKS_PER_SEC;
3123 tv->tv_usec = (t - tv->tv_sec * CLOCKS_PER_SEC) * 1000000 / CLOCKS_PER_SEC;
3124 return 0;
3125}
3126# endif
3127
3128#endif
3129
3130#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
3131
3132/*
3133 * Common code for the X command server and the Win32 command server.
3134 */
3135
Bram Moolenaareb94e552006-03-11 21:35:11 +00003136static char_u *build_drop_cmd __ARGS((int filec, char **filev, int tabs, int sendReply));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003137
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003138/*
3139 * Do the client-server stuff, unless "--servername ''" was used.
3140 */
3141 static void
3142exec_on_server(parmp)
3143 mparm_T *parmp;
3144{
3145 if (parmp->serverName_arg == NULL || *parmp->serverName_arg != NUL)
3146 {
3147# ifdef WIN32
3148 /* Initialise the client/server messaging infrastructure. */
3149 serverInitMessaging();
3150# endif
3151
3152 /*
3153 * When a command server argument was found, execute it. This may
3154 * exit Vim when it was successful. Otherwise it's executed further
3155 * on. Remember the encoding used here in "serverStrEnc".
3156 */
3157 if (parmp->serverArg)
3158 {
3159 cmdsrv_main(&parmp->argc, parmp->argv,
3160 parmp->serverName_arg, &parmp->serverStr);
3161# ifdef FEAT_MBYTE
3162 parmp->serverStrEnc = vim_strsave(p_enc);
3163# endif
3164 }
3165
3166 /* If we're still running, get the name to register ourselves.
3167 * On Win32 can register right now, for X11 need to setup the
3168 * clipboard first, it's further down. */
3169 parmp->servername = serverMakeName(parmp->serverName_arg,
3170 parmp->argv[0]);
3171# ifdef WIN32
3172 if (parmp->servername != NULL)
3173 {
3174 serverSetName(parmp->servername);
3175 vim_free(parmp->servername);
3176 }
3177# endif
3178 }
3179}
3180
3181/*
3182 * Prepare for running as a Vim server.
3183 */
3184 static void
3185prepare_server(parmp)
3186 mparm_T *parmp;
3187{
3188# if defined(FEAT_X11)
3189 /*
3190 * Register for remote command execution with :serversend and --remote
3191 * unless there was a -X or a --servername '' on the command line.
3192 * Only register nongui-vim's with an explicit --servername argument.
3193 */
3194 if (X_DISPLAY != NULL && parmp->servername != NULL && (
3195# ifdef FEAT_GUI
3196 gui.in_use ||
3197# endif
3198 parmp->serverName_arg != NULL))
3199 {
3200 (void)serverRegisterName(X_DISPLAY, parmp->servername);
3201 vim_free(parmp->servername);
3202 TIME_MSG("register server name");
3203 }
3204 else
3205 serverDelayedStartName = parmp->servername;
3206# endif
3207
3208 /*
3209 * Execute command ourselves if we're here because the send failed (or
3210 * else we would have exited above).
3211 */
3212 if (parmp->serverStr != NULL)
3213 {
3214 char_u *p;
3215
3216 server_to_input_buf(serverConvert(parmp->serverStrEnc,
3217 parmp->serverStr, &p));
3218 vim_free(p);
3219 }
3220}
3221
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003222 static void
3223cmdsrv_main(argc, argv, serverName_arg, serverStr)
3224 int *argc;
3225 char **argv;
3226 char_u *serverName_arg;
3227 char_u **serverStr;
3228{
3229 char_u *res;
3230 int i;
3231 char_u *sname;
3232 int ret;
3233 int didone = FALSE;
3234 int exiterr = 0;
3235 char **newArgV = argv + 1;
3236 int newArgC = 1,
3237 Argc = *argc;
3238 int argtype;
3239#define ARGTYPE_OTHER 0
3240#define ARGTYPE_EDIT 1
3241#define ARGTYPE_EDIT_WAIT 2
3242#define ARGTYPE_SEND 3
3243 int silent = FALSE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003244 int tabs = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003245# ifndef FEAT_X11
3246 HWND srv;
3247# else
3248 Window srv;
3249
3250 setup_term_clip();
3251# endif
3252
3253 sname = serverMakeName(serverName_arg, argv[0]);
3254 if (sname == NULL)
3255 return;
3256
3257 /*
3258 * Execute the command server related arguments and remove them
3259 * from the argc/argv array; We may have to return into main()
3260 */
3261 for (i = 1; i < Argc; i++)
3262 {
3263 res = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003264 if (STRCMP(argv[i], "--") == 0) /* end of option arguments */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003265 {
3266 for (; i < *argc; i++)
3267 {
3268 *newArgV++ = argv[i];
3269 newArgC++;
3270 }
3271 break;
3272 }
3273
Bram Moolenaareb94e552006-03-11 21:35:11 +00003274 if (STRICMP(argv[i], "--remote-send") == 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003275 argtype = ARGTYPE_SEND;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003276 else if (STRNICMP(argv[i], "--remote", 8) == 0)
3277 {
3278 char *p = argv[i] + 8;
3279
3280 argtype = ARGTYPE_EDIT;
3281 while (*p != NUL)
3282 {
3283 if (STRNICMP(p, "-wait", 5) == 0)
3284 {
3285 argtype = ARGTYPE_EDIT_WAIT;
3286 p += 5;
3287 }
3288 else if (STRNICMP(p, "-silent", 7) == 0)
3289 {
3290 silent = TRUE;
3291 p += 7;
3292 }
3293 else if (STRNICMP(p, "-tab", 4) == 0)
3294 {
3295 tabs = TRUE;
3296 p += 4;
3297 }
3298 else
3299 {
3300 argtype = ARGTYPE_OTHER;
3301 break;
3302 }
3303 }
3304 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003305 else
3306 argtype = ARGTYPE_OTHER;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003307
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003308 if (argtype != ARGTYPE_OTHER)
3309 {
3310 if (i == *argc - 1)
3311 mainerr_arg_missing((char_u *)argv[i]);
3312 if (argtype == ARGTYPE_SEND)
3313 {
3314 *serverStr = (char_u *)argv[i + 1];
3315 i++;
3316 }
3317 else
3318 {
3319 *serverStr = build_drop_cmd(*argc - i - 1, argv + i + 1,
Bram Moolenaareb94e552006-03-11 21:35:11 +00003320 tabs, argtype == ARGTYPE_EDIT_WAIT);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003321 if (*serverStr == NULL)
3322 {
3323 /* Probably out of memory, exit. */
3324 didone = TRUE;
3325 exiterr = 1;
3326 break;
3327 }
3328 Argc = i;
3329 }
3330# ifdef FEAT_X11
3331 if (xterm_dpy == NULL)
3332 {
3333 mch_errmsg(_("No display"));
3334 ret = -1;
3335 }
3336 else
3337 ret = serverSendToVim(xterm_dpy, sname, *serverStr,
3338 NULL, &srv, 0, 0, silent);
3339# else
3340 /* Win32 always works? */
3341 ret = serverSendToVim(sname, *serverStr, NULL, &srv, 0, silent);
3342# endif
3343 if (ret < 0)
3344 {
3345 if (argtype == ARGTYPE_SEND)
3346 {
3347 /* Failed to send, abort. */
3348 mch_errmsg(_(": Send failed.\n"));
3349 didone = TRUE;
3350 exiterr = 1;
3351 }
3352 else if (!silent)
3353 /* Let vim start normally. */
3354 mch_errmsg(_(": Send failed. Trying to execute locally\n"));
3355 break;
3356 }
3357
3358# ifdef FEAT_GUI_W32
3359 /* Guess that when the server name starts with "g" it's a GUI
3360 * server, which we can bring to the foreground here.
3361 * Foreground() in the server doesn't work very well. */
3362 if (argtype != ARGTYPE_SEND && TOUPPER_ASC(*sname) == 'G')
3363 SetForegroundWindow(srv);
3364# endif
3365
3366 /*
3367 * For --remote-wait: Wait until the server did edit each
3368 * file. Also detect that the server no longer runs.
3369 */
3370 if (ret >= 0 && argtype == ARGTYPE_EDIT_WAIT)
3371 {
3372 int numFiles = *argc - i - 1;
3373 int j;
3374 char_u *done = alloc(numFiles);
3375 char_u *p;
3376# ifdef FEAT_GUI_W32
3377 NOTIFYICONDATA ni;
3378 int count = 0;
3379 extern HWND message_window;
3380# endif
3381
3382 if (numFiles > 0 && argv[i + 1][0] == '+')
3383 /* Skip "+cmd" argument, don't wait for it to be edited. */
3384 --numFiles;
3385
3386# ifdef FEAT_GUI_W32
3387 ni.cbSize = sizeof(ni);
3388 ni.hWnd = message_window;
3389 ni.uID = 0;
3390 ni.uFlags = NIF_ICON|NIF_TIP;
3391 ni.hIcon = LoadIcon((HINSTANCE)GetModuleHandle(0), "IDR_VIM");
3392 sprintf(ni.szTip, _("%d of %d edited"), count, numFiles);
3393 Shell_NotifyIcon(NIM_ADD, &ni);
3394# endif
3395
3396 /* Wait for all files to unload in remote */
3397 memset(done, 0, numFiles);
3398 while (memchr(done, 0, numFiles) != NULL)
3399 {
3400# ifdef WIN32
3401 p = serverGetReply(srv, NULL, TRUE, TRUE);
3402 if (p == NULL)
3403 break;
3404# else
3405 if (serverReadReply(xterm_dpy, srv, &p, TRUE) < 0)
3406 break;
3407# endif
3408 j = atoi((char *)p);
3409 if (j >= 0 && j < numFiles)
3410 {
3411# ifdef FEAT_GUI_W32
3412 ++count;
3413 sprintf(ni.szTip, _("%d of %d edited"),
3414 count, numFiles);
3415 Shell_NotifyIcon(NIM_MODIFY, &ni);
3416# endif
3417 done[j] = 1;
3418 }
3419 }
3420# ifdef FEAT_GUI_W32
3421 Shell_NotifyIcon(NIM_DELETE, &ni);
3422# endif
3423 }
3424 }
3425 else if (STRICMP(argv[i], "--remote-expr") == 0)
3426 {
3427 if (i == *argc - 1)
3428 mainerr_arg_missing((char_u *)argv[i]);
3429# ifdef WIN32
3430 /* Win32 always works? */
3431 if (serverSendToVim(sname, (char_u *)argv[i + 1],
3432 &res, NULL, 1, FALSE) < 0)
3433# else
3434 if (xterm_dpy == NULL)
3435 mch_errmsg(_("No display: Send expression failed.\n"));
3436 else if (serverSendToVim(xterm_dpy, sname, (char_u *)argv[i + 1],
3437 &res, NULL, 1, 1, FALSE) < 0)
3438# endif
3439 {
3440 if (res != NULL && *res != NUL)
3441 {
3442 /* Output error from remote */
3443 mch_errmsg((char *)res);
3444 vim_free(res);
3445 res = NULL;
3446 }
3447 mch_errmsg(_(": Send expression failed.\n"));
3448 }
3449 }
3450 else if (STRICMP(argv[i], "--serverlist") == 0)
3451 {
3452# ifdef WIN32
3453 /* Win32 always works? */
3454 res = serverGetVimNames();
3455# else
3456 if (xterm_dpy != NULL)
3457 res = serverGetVimNames(xterm_dpy);
3458# endif
3459 if (called_emsg)
3460 mch_errmsg("\n");
3461 }
3462 else if (STRICMP(argv[i], "--servername") == 0)
3463 {
3464 /* Alredy processed. Take it out of the command line */
3465 i++;
3466 continue;
3467 }
3468 else
3469 {
3470 *newArgV++ = argv[i];
3471 newArgC++;
3472 continue;
3473 }
3474 didone = TRUE;
3475 if (res != NULL && *res != NUL)
3476 {
3477 mch_msg((char *)res);
3478 if (res[STRLEN(res) - 1] != '\n')
3479 mch_msg("\n");
3480 }
3481 vim_free(res);
3482 }
3483
3484 if (didone)
3485 {
3486 display_errors(); /* display any collected messages */
3487 exit(exiterr); /* Mission accomplished - get out */
3488 }
3489
3490 /* Return back into main() */
3491 *argc = newArgC;
3492 vim_free(sname);
3493}
3494
3495/*
3496 * Build a ":drop" command to send to a Vim server.
3497 */
3498 static char_u *
Bram Moolenaareb94e552006-03-11 21:35:11 +00003499build_drop_cmd(filec, filev, tabs, sendReply)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003500 int filec;
3501 char **filev;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003502 int tabs; /* Use ":tab drop" instead of ":drop". */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003503 int sendReply;
3504{
3505 garray_T ga;
3506 int i;
3507 char_u *inicmd = NULL;
3508 char_u *p;
3509 char_u cwd[MAXPATHL];
3510
3511 if (filec > 0 && filev[0][0] == '+')
3512 {
3513 inicmd = (char_u *)filev[0] + 1;
3514 filev++;
3515 filec--;
3516 }
3517 /* Check if we have at least one argument. */
3518 if (filec <= 0)
3519 mainerr_arg_missing((char_u *)filev[-1]);
3520 if (mch_dirname(cwd, MAXPATHL) != OK)
3521 return NULL;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003522 if ((p = vim_strsave_escaped_ext(cwd, PATH_ESC_CHARS, '\\', TRUE)) == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003523 return NULL;
3524 ga_init2(&ga, 1, 100);
3525 ga_concat(&ga, (char_u *)"<C-\\><C-N>:cd ");
3526 ga_concat(&ga, p);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003527 vim_free(p);
Bram Moolenaareb94e552006-03-11 21:35:11 +00003528
3529 /* Call inputsave() so that a prompt for an encryption key works. */
3530 ga_concat(&ga, (char_u *)"<CR>:if exists('*inputsave')|call inputsave()|endif|");
3531 if (tabs)
3532 ga_concat(&ga, (char_u *)"tab ");
3533 ga_concat(&ga, (char_u *)"drop");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003534 for (i = 0; i < filec; i++)
3535 {
3536 /* On Unix the shell has already expanded the wildcards, don't want to
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003537 * do it again in the Vim server. On MS-Windows only escape
3538 * non-wildcard characters. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003539 p = vim_strsave_escaped((char_u *)filev[i],
3540#ifdef UNIX
3541 PATH_ESC_CHARS
3542#else
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003543 (char_u *)" \t%#"
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003544#endif
3545 );
3546 if (p == NULL)
3547 {
3548 vim_free(ga.ga_data);
3549 return NULL;
3550 }
3551 ga_concat(&ga, (char_u *)" ");
3552 ga_concat(&ga, p);
3553 vim_free(p);
3554 }
3555 /* The :drop commands goes to Insert mode when 'insertmode' is set, use
3556 * CTRL-\ CTRL-N again. */
3557 ga_concat(&ga, (char_u *)"|if exists('*inputrestore')|call inputrestore()|endif<CR>");
3558 ga_concat(&ga, (char_u *)"<C-\\><C-N>:cd -");
3559 if (sendReply)
3560 ga_concat(&ga, (char_u *)"<CR>:call SetupRemoteReplies()");
3561 ga_concat(&ga, (char_u *)"<CR>:");
3562 if (inicmd != NULL)
3563 {
3564 /* Can't use <CR> after "inicmd", because an "startinsert" would cause
3565 * the following commands to be inserted as text. Use a "|",
3566 * hopefully "inicmd" does allow this... */
3567 ga_concat(&ga, inicmd);
3568 ga_concat(&ga, (char_u *)"|");
3569 }
3570 /* Bring the window to the foreground, goto Insert mode when 'im' set and
3571 * clear command line. */
Bram Moolenaar567e4de2004-12-31 21:01:02 +00003572 ga_concat(&ga, (char_u *)"cal foreground()|if &im|star|en|redr|f<CR>");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003573 ga_append(&ga, NUL);
3574 return ga.ga_data;
3575}
3576
3577/*
3578 * Replace termcodes such as <CR> and insert as key presses if there is room.
3579 */
3580 void
3581server_to_input_buf(str)
3582 char_u *str;
3583{
3584 char_u *ptr = NULL;
3585 char_u *cpo_save = p_cpo;
3586
3587 /* Set 'cpoptions' the way we want it.
3588 * B set - backslashes are *not* treated specially
3589 * k set - keycodes are *not* reverse-engineered
3590 * < unset - <Key> sequences *are* interpreted
3591 * The last parameter of replace_termcodes() is TRUE so that the <lt>
3592 * sequence is recognised - needed for a real backslash.
3593 */
3594 p_cpo = (char_u *)"Bk";
3595 str = replace_termcodes((char_u *)str, &ptr, FALSE, TRUE);
3596 p_cpo = cpo_save;
3597
3598 if (*ptr != NUL) /* trailing CTRL-V results in nothing */
3599 {
3600 /*
3601 * Add the string to the input stream.
3602 * Can't use add_to_input_buf() here, we now have K_SPECIAL bytes.
3603 *
3604 * First clear typed characters from the typeahead buffer, there could
3605 * be half a mapping there. Then append to the existing string, so
3606 * that multiple commands from a client are concatenated.
3607 */
3608 if (typebuf.tb_maplen < typebuf.tb_len)
3609 del_typebuf(typebuf.tb_len - typebuf.tb_maplen, typebuf.tb_maplen);
3610 (void)ins_typebuf(str, REMAP_NONE, typebuf.tb_len, TRUE, FALSE);
3611
3612 /* Let input_available() know we inserted text in the typeahead
3613 * buffer. */
3614 received_from_client = TRUE;
3615 }
3616 vim_free((char_u *)ptr);
3617}
3618
3619/*
3620 * Evaluate an expression that the client sent to a string.
3621 * Handles disabling error messages and disables debugging, otherwise Vim
3622 * hangs, waiting for "cont" to be typed.
3623 */
3624 char_u *
3625eval_client_expr_to_string(expr)
3626 char_u *expr;
3627{
3628 char_u *res;
3629 int save_dbl = debug_break_level;
3630 int save_ro = redir_off;
3631
3632 debug_break_level = -1;
3633 redir_off = 0;
3634 ++emsg_skip;
3635
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003636 res = eval_to_string(expr, NULL, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003637
3638 debug_break_level = save_dbl;
3639 redir_off = save_ro;
3640 --emsg_skip;
3641
Bram Moolenaar63a121b2005-12-11 21:36:39 +00003642 /* A client can tell us to redraw, but not to display the cursor, so do
3643 * that here. */
3644 setcursor();
3645 out_flush();
3646#ifdef FEAT_GUI
3647 if (gui.in_use)
3648 gui_update_cursor(FALSE, FALSE);
3649#endif
3650
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003651 return res;
3652}
3653
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003654/*
3655 * If conversion is needed, convert "data" from "client_enc" to 'encoding' and
3656 * return an allocated string. Otherwise return "data".
3657 * "*tofree" is set to the result when it needs to be freed later.
3658 */
3659/*ARGSUSED*/
3660 char_u *
3661serverConvert(client_enc, data, tofree)
3662 char_u *client_enc;
3663 char_u *data;
3664 char_u **tofree;
3665{
3666 char_u *res = data;
3667
3668 *tofree = NULL;
3669# ifdef FEAT_MBYTE
3670 if (client_enc != NULL && p_enc != NULL)
3671 {
3672 vimconv_T vimconv;
3673
3674 vimconv.vc_type = CONV_NONE;
3675 if (convert_setup(&vimconv, client_enc, p_enc) != FAIL
3676 && vimconv.vc_type != CONV_NONE)
3677 {
3678 res = string_convert(&vimconv, data, NULL);
3679 if (res == NULL)
3680 res = data;
3681 else
3682 *tofree = res;
3683 }
3684 convert_setup(&vimconv, NULL, NULL);
3685 }
3686# endif
3687 return res;
3688}
3689
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003690
3691/*
3692 * Make our basic server name: use the specified "arg" if given, otherwise use
3693 * the tail of the command "cmd" we were started with.
3694 * Return the name in allocated memory. This doesn't include a serial number.
3695 */
3696 static char_u *
3697serverMakeName(arg, cmd)
3698 char_u *arg;
3699 char *cmd;
3700{
3701 char_u *p;
3702
3703 if (arg != NULL && *arg != NUL)
3704 p = vim_strsave_up(arg);
3705 else
3706 {
3707 p = vim_strsave_up(gettail((char_u *)cmd));
3708 /* Remove .exe or .bat from the name. */
3709 if (p != NULL && vim_strchr(p, '.') != NULL)
3710 *vim_strchr(p, '.') = NUL;
3711 }
3712 return p;
3713}
3714#endif /* FEAT_CLIENTSERVER */
3715
3716/*
3717 * When FEAT_FKMAP is defined, also compile the Farsi source code.
3718 */
3719#if defined(FEAT_FKMAP) || defined(PROTO)
3720# include "farsi.c"
3721#endif
3722
3723/*
3724 * When FEAT_ARABIC is defined, also compile the Arabic source code.
3725 */
3726#if defined(FEAT_ARABIC) || defined(PROTO)
3727# include "arabic.c"
3728#endif