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