blob: 50901ce906a1abae5d3a23dad5603d1b319224be [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 Moolenaar231334e2005-07-25 20:46:57 +0000419 if (params.diff_mode)
420 {
421 if (params.window_count == -1)
422 params.window_count = 0; /* open up to 3 windows */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +0000423 if (params.window_layout == 0)
424 params.window_layout = WIN_VER; /* use vertical split */
Bram Moolenaar231334e2005-07-25 20:46:57 +0000425 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000426#endif
427
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000428 /* Don't redraw until much later. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000429 ++RedrawingDisabled;
430
431 /*
432 * When listing swap file names, don't do cursor positioning et. al.
433 */
434 if (recoverymode && fname == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000435 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000436
437 /*
438 * When certain to start the GUI, don't check capabilities of terminal.
439 * For GTK we can't be sure, but when started from the desktop it doesn't
440 * make sense to try using a terminal.
441 */
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000442#if defined(ALWAYS_USE_GUI) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000443 if (gui.starting
444# ifdef FEAT_GUI_GTK
445 && !isatty(2)
446# endif
447 )
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000448 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000449#endif
450
451#if defined(FEAT_GUI_MAC) && defined(MACOS_X_UNIX)
452 /* When the GUI is started from Finder, need to display messages in a
453 * message box. isatty(2) returns TRUE anyway, thus we need to check the
454 * name to know we're not started from a terminal. */
455 if (gui.starting && (!isatty(2) || strcmp("/dev/console", ttyname(2)) == 0))
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000456 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000457#endif
458
459 /*
460 * mch_init() sets up the terminal (window) for use. This must be
461 * done after resetting full_screen, otherwise it may move the cursor
462 * (MSDOS).
463 * Note that we may use mch_exit() before mch_init()!
464 */
465 mch_init();
466 TIME_MSG("shell init");
467
468#ifdef USE_XSMP
469 /*
470 * For want of anywhere else to do it, try to connect to xsmp here.
471 * Fitting it in after gui_mch_init, but before gui_init (via termcapinit).
472 * Hijacking -X 'no X connection' to also disable XSMP connection as that
473 * has a similar delay upon failure.
474 * Only try if SESSION_MANAGER is set to something non-null.
475 */
476 if (!x_no_connect)
477 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000478 char *p = getenv("SESSION_MANAGER");
479
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000480 if (p != NULL && *p != NUL)
481 {
482 xsmp_init();
483 TIME_MSG("xsmp init");
484 }
485 }
486#endif
487
488 /*
489 * Print a warning if stdout is not a terminal.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000490 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000491 check_tty(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000492
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000493 /* This message comes before term inits, but after setting "silent_mode"
494 * when the input is not a tty. */
495 if (GARGCOUNT > 1 && !silent_mode)
496 printf(_("%d files to edit\n"), GARGCOUNT);
497
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000498 if (params.want_full_screen && !silent_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000499 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000500 termcapinit(params.term); /* set terminal name and get terminal
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000501 capabilities (will set full_screen) */
502 screen_start(); /* don't know where cursor is now */
503 TIME_MSG("Termcap init");
504 }
505
506 /*
507 * Set the default values for the options that use Rows and Columns.
508 */
509 ui_get_shellsize(); /* inits Rows and Columns */
510#ifdef FEAT_NETBEANS_INTG
511 if (usingNetbeans)
512 Columns += 2; /* leave room for glyph gutter */
513#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000514 win_init_size();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000515#ifdef FEAT_DIFF
516 /* Set the 'diff' option now, so that it can be checked for in a .vimrc
517 * file. There is no buffer yet though. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000518 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000519 diff_win_options(firstwin, FALSE);
520#endif
521
522 cmdline_row = Rows - p_ch;
523 msg_row = cmdline_row;
524 screenalloc(FALSE); /* allocate screen buffers */
525 set_init_2();
526 TIME_MSG("inits 2");
527
528 msg_scroll = TRUE;
529 no_wait_return = TRUE;
530
531 init_mappings(); /* set up initial mappings */
532
533 init_highlight(TRUE, FALSE); /* set the default highlight groups */
534 TIME_MSG("init highlight");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000535
536#ifdef FEAT_EVAL
537 /* Set the break level after the terminal is initialized. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000538 debug_break_level = params.use_debug_break_level;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000539#endif
540
Bram Moolenaar58d98232005-07-23 22:25:46 +0000541 /* Execute --cmd arguments. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000542 exe_pre_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000543
Bram Moolenaar58d98232005-07-23 22:25:46 +0000544 /* Source startup scripts. */
545 source_startup_scripts(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000546
547#ifdef FEAT_EVAL
548 /*
549 * Read all the plugin files.
550 * Only when compiled with +eval, since most plugins need it.
551 */
552 if (p_lpl)
553 {
Bram Moolenaar07d4d732005-10-03 22:04:08 +0000554 source_runtime((char_u *)"plugin/**/*.vim", TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000555 TIME_MSG("loading plugins");
556 }
557#endif
558
559 /*
560 * Recovery mode without a file name: List swap files.
561 * This uses the 'dir' option, therefore it must be after the
562 * initializations.
563 */
564 if (recoverymode && fname == NULL)
565 {
566 recover_names(NULL, TRUE, 0);
567 mch_exit(0);
568 }
569
570 /*
571 * Set a few option defaults after reading .vimrc files:
572 * 'title' and 'icon', Unix: 'shellpipe' and 'shellredir'.
573 */
574 set_init_3();
575 TIME_MSG("inits 3");
576
577 /*
578 * "-n" argument: Disable swap file by setting 'updatecount' to 0.
579 * Note that this overrides anything from a vimrc file.
580 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000581 if (params.no_swap_file)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000582 p_uc = 0;
583
584#ifdef FEAT_FKMAP
585 if (curwin->w_p_rl && p_altkeymap)
586 {
587 p_hkmap = FALSE; /* Reset the Hebrew keymap mode */
588# ifdef FEAT_ARABIC
589 curwin->w_p_arab = FALSE; /* Reset the Arabic keymap mode */
590# endif
591 p_fkmap = TRUE; /* Set the Farsi keymap mode */
592 }
593#endif
594
595#ifdef FEAT_GUI
596 if (gui.starting)
597 {
598#if defined(UNIX) || defined(VMS)
599 /* When something caused a message from a vimrc script, need to output
600 * an extra newline before the shell prompt. */
601 if (did_emsg || msg_didout)
602 putchar('\n');
603#endif
604
605 gui_start(); /* will set full_screen to TRUE */
606 TIME_MSG("starting GUI");
607
608 /* When running "evim" or "gvim -y" we need the menus, exit if we
609 * don't have them. */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000610 if (!gui.in_use && params.evim_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000611 mch_exit(1);
612 }
613#endif
614
615#ifdef SPAWNO /* special MSDOS swapping library */
616 init_SPAWNO("", SWAP_ANY);
617#endif
618
619#ifdef FEAT_VIMINFO
620 /*
621 * Read in registers, history etc, but not marks, from the viminfo file
622 */
623 if (*p_viminfo != NUL)
624 {
625 read_viminfo(NULL, TRUE, FALSE, FALSE);
626 TIME_MSG("reading viminfo");
627 }
628#endif
629
630#ifdef FEAT_QUICKFIX
631 /*
632 * "-q errorfile": Load the error file now.
633 * If the error file can't be read, exit before doing anything else.
634 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000635 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000636 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000637 if (params.use_ef != NULL)
638 set_string_option_direct((char_u *)"ef", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000639 params.use_ef, OPT_FREE, SID_CARG);
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000640 if (qf_init(NULL, p_ef, p_efm, TRUE) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000641 {
642 out_char('\n');
643 mch_exit(3);
644 }
645 TIME_MSG("reading errorfile");
646 }
647#endif
648
649 /*
650 * Start putting things on the screen.
651 * Scroll screen down before drawing over it
652 * Clear screen now, so file message will not be cleared.
653 */
654 starting = NO_BUFFERS;
655 no_wait_return = FALSE;
656 if (!exmode_active)
657 msg_scroll = FALSE;
658
659#ifdef FEAT_GUI
660 /*
661 * This seems to be required to make callbacks to be called now, instead
662 * of after things have been put on the screen, which then may be deleted
663 * when getting a resize callback.
664 * For the Mac this handles putting files dropped on the Vim icon to
665 * global_alist.
666 */
667 if (gui.in_use)
668 {
669# ifdef FEAT_SUN_WORKSHOP
670 if (!usingSunWorkShop)
671# endif
672 gui_wait_for_chars(50L);
673 TIME_MSG("GUI delay");
674 }
675#endif
676
677#if defined(FEAT_GUI_PHOTON) && defined(FEAT_CLIPBOARD)
678 qnx_clip_init();
679#endif
680
681#ifdef FEAT_XCLIPBOARD
682 /* Start using the X clipboard, unless the GUI was started. */
683# ifdef FEAT_GUI
684 if (!gui.in_use)
685# endif
686 {
687 setup_term_clip();
688 TIME_MSG("setup clipboard");
689 }
690#endif
691
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000692#ifdef FEAT_CLIENTSERVER
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000693 /* Prepare for being a Vim server. */
694 prepare_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000695#endif
696
697 /*
698 * If "-" argument given: Read file from stdin.
699 * Do this before starting Raw mode, because it may change things that the
700 * writing end of the pipe doesn't like, e.g., in case stdin and stderr
701 * are the same terminal: "cat | vim -".
702 * Using autocommands here may cause trouble...
703 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000704 if (params.edit_type == EDIT_STDIN && !recoverymode)
705 read_stdin();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000706
707#if defined(UNIX) || defined(VMS)
708 /* When switching screens and something caused a message from a vimrc
709 * script, need to output an extra newline on exit. */
710 if ((did_emsg || msg_didout) && *T_TI != NUL)
711 newline_on_exit = TRUE;
712#endif
713
714 /*
715 * When done something that is not allowed or error message call
716 * wait_return. This must be done before starttermcap(), because it may
717 * switch to another screen. It must be done after settmode(TMODE_RAW),
718 * because we want to react on a single key stroke.
719 * Call settmode and starttermcap here, so the T_KS and T_TI may be
720 * defined by termcapinit and redifined in .exrc.
721 */
722 settmode(TMODE_RAW);
723 TIME_MSG("setting raw mode");
724
725 if (need_wait_return || msg_didany)
726 {
727 wait_return(TRUE);
728 TIME_MSG("waiting for return");
729 }
730
731 starttermcap(); /* start termcap if not done by wait_return() */
732 TIME_MSG("start termcap");
733
734#ifdef FEAT_MOUSE
735 setmouse(); /* may start using the mouse */
736#endif
737 if (scroll_region)
738 scroll_region_reset(); /* In case Rows changed */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000739 scroll_start(); /* may scroll the screen to the right position */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000740
741 /*
742 * Don't clear the screen when starting in Ex mode, unless using the GUI.
743 */
744 if (exmode_active
745#ifdef FEAT_GUI
746 && !gui.in_use
747#endif
748 )
749 must_redraw = CLEAR;
750 else
751 {
752 screenclear(); /* clear screen */
753 TIME_MSG("clearing screen");
754 }
755
756#ifdef FEAT_CRYPT
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000757 if (params.ask_for_key)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000758 {
759 (void)get_crypt_key(TRUE, TRUE);
760 TIME_MSG("getting crypt key");
761 }
762#endif
763
764 no_wait_return = TRUE;
765
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000766 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000767 * Create the requested number of windows and edit buffers in them.
768 * Also does recovery if "recoverymode" set.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000769 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000770 create_windows(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000771 TIME_MSG("opening buffers");
772
773 /* Ex starts at last line of the file */
774 if (exmode_active)
775 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
776
777#ifdef FEAT_AUTOCMD
778 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
779 TIME_MSG("BufEnter autocommands");
780#endif
781 setpcmark();
782
783#ifdef FEAT_QUICKFIX
784 /*
785 * When started with "-q errorfile" jump to first error now.
786 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000787 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000788 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000789 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000790 TIME_MSG("jump to first error");
791 }
792#endif
793
794#ifdef FEAT_WINDOWS
795 /*
796 * If opened more than one window, start editing files in the other
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000797 * windows.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000798 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000799 edit_buffers(&params);
800#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000801
802#ifdef FEAT_DIFF
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000803 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000804 {
805 win_T *wp;
806
807 /* set options in each window for "vimdiff". */
808 for (wp = firstwin; wp != NULL; wp = wp->w_next)
809 diff_win_options(wp, TRUE);
810 }
811#endif
812
813 /*
814 * Shorten any of the filenames, but only when absolute.
815 */
816 shorten_fnames(FALSE);
817
818 /*
819 * Need to jump to the tag before executing the '-c command'.
820 * Makes "vim -c '/return' -t main" work.
821 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000822 if (params.tagname != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000823 {
Bram Moolenaar146522e2005-12-16 21:55:46 +0000824#if defined(HAS_SWAP_EXISTS_ACTION)
825 swap_exists_did_quit = FALSE;
826#endif
827
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000828 vim_snprintf((char *)IObuff, IOSIZE, "ta %s", params.tagname);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000829 do_cmdline_cmd(IObuff);
830 TIME_MSG("jumping to tag");
Bram Moolenaar146522e2005-12-16 21:55:46 +0000831
832#if defined(HAS_SWAP_EXISTS_ACTION)
833 /* If the user doesn't want to edit the file then we quit here. */
834 if (swap_exists_did_quit)
835 getout(1);
836#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000837 }
838
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000839 /* Execute any "+", "-c" and "-S" arguments. */
840 if (params.n_commands > 0)
841 exe_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000842
843 RedrawingDisabled = 0;
844 redraw_all_later(NOT_VALID);
845 no_wait_return = FALSE;
846 starting = 0;
847
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000848#ifdef FEAT_TERMRESPONSE
849 /* Requesting the termresponse is postponed until here, so that a "-c q"
850 * argument doesn't make it appear in the shell Vim was started from. */
851 may_req_termresponse();
852#endif
853
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000854 /* start in insert mode */
855 if (p_im)
856 need_start_insertmode = TRUE;
857
858#ifdef FEAT_AUTOCMD
859 apply_autocmds(EVENT_VIMENTER, NULL, NULL, FALSE, curbuf);
860 TIME_MSG("VimEnter autocommands");
861#endif
862
863#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
864 /* When a startup script or session file setup for diff'ing and
865 * scrollbind, sync the scrollbind now. */
866 if (curwin->w_p_diff && curwin->w_p_scb)
867 {
868 update_topline();
869 check_scrollbind((linenr_T)0, 0L);
870 TIME_MSG("diff scrollbinding");
871 }
872#endif
873
874#if defined(WIN3264) && !defined(FEAT_GUI_W32)
875 mch_set_winsize_now(); /* Allow winsize changes from now on */
876#endif
877
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000878#if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
879 /* When tab pages were created, may need to update the tab pages line and
880 * scrollbars. This is skipped while creating them. */
881 if (first_tabpage->tp_next != NULL)
882 {
883 out_flush();
884 gui_init_which_components(NULL);
885 gui_update_scrollbars(TRUE);
886 }
887 need_mouse_correct = TRUE;
888#endif
889
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000890 /* If ":startinsert" command used, stuff a dummy command to be able to
891 * call normal_cmd(), which will then start Insert mode. */
892 if (restart_edit != 0)
Bram Moolenaarebefac62005-12-28 22:39:57 +0000893 stuffcharReadbuff(K_NOP);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000894
895#ifdef FEAT_NETBEANS_INTG
896 if (usingNetbeans)
897 /* Tell the client that it can start sending commands. */
898 netbeans_startup_done();
899#endif
900
901 TIME_MSG("before starting main loop");
902
903 /*
904 * Call the main command loop. This never returns.
905 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000906 main_loop(FALSE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000907
908 return 0;
909}
910#endif /* PROTO */
911
912/*
913 * Main loop: Execute Normal mode commands until exiting Vim.
914 * Also used to handle commands in the command-line window, until the window
915 * is closed.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000916 * Also used to handle ":visual" command after ":global": execute Normal mode
917 * commands, return when entering Ex mode. "noexmode" is TRUE then.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000918 */
919 void
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000920main_loop(cmdwin, noexmode)
921 int cmdwin; /* TRUE when working in the command-line window */
922 int noexmode; /* TRUE when return on entering Ex mode */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000923{
924 oparg_T oa; /* operator arguments */
925
926#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
927 /* Setup to catch a terminating error from the X server. Just ignore
928 * it, restore the state and continue. This might not always work
929 * properly, but at least we don't exit unexpectedly when the X server
930 * exists while Vim is running in a console. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000931 if (!cmdwin && !noexmode && SETJMP(x_jump_env))
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000932 {
933 State = NORMAL;
934# ifdef FEAT_VISUAL
935 VIsual_active = FALSE;
936# endif
937 got_int = TRUE;
938 need_wait_return = FALSE;
939 global_busy = FALSE;
940 exmode_active = 0;
941 skip_redraw = FALSE;
942 RedrawingDisabled = 0;
943 no_wait_return = 0;
944# ifdef FEAT_EVAL
945 emsg_skip = 0;
946# endif
947 emsg_off = 0;
948# ifdef FEAT_MOUSE
949 setmouse();
950# endif
951 settmode(TMODE_RAW);
952 starttermcap();
953 scroll_start();
954 redraw_later_clear();
955 }
956#endif
957
958 clear_oparg(&oa);
959 while (!cmdwin
960#ifdef FEAT_CMDWIN
961 || cmdwin_result == 0
962#endif
963 )
964 {
965 if (stuff_empty())
966 {
967 did_check_timestamps = FALSE;
968 if (need_check_timestamps)
969 check_timestamps(FALSE);
970 if (need_wait_return) /* if wait_return still needed ... */
971 wait_return(FALSE); /* ... call it now */
972 if (need_start_insertmode && goto_im()
973#ifdef FEAT_VISUAL
974 && !VIsual_active
975#endif
976 )
977 {
978 need_start_insertmode = FALSE;
979 stuffReadbuff((char_u *)"i"); /* start insert mode next */
980 /* skip the fileinfo message now, because it would be shown
981 * after insert mode finishes! */
982 need_fileinfo = FALSE;
983 }
984 }
985 if (got_int && !global_busy)
986 {
987 if (!quit_more)
988 (void)vgetc(); /* flush all buffers */
989 got_int = FALSE;
990 }
991 if (!exmode_active)
992 msg_scroll = FALSE;
993 quit_more = FALSE;
994
995 /*
996 * If skip redraw is set (for ":" in wait_return()), don't redraw now.
997 * If there is nothing in the stuff_buffer or do_redraw is TRUE,
998 * update cursor and redraw.
999 */
1000 if (skip_redraw || exmode_active)
1001 skip_redraw = FALSE;
1002 else if (do_redraw || stuff_empty())
1003 {
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001004#ifdef FEAT_AUTOCMD
1005 /* Trigger CursorMoved if the cursor moved. */
1006 if (!finish_op && has_cursormoved()
1007 && !equalpos(last_cursormoved, curwin->w_cursor))
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001008 {
1009 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL, FALSE, curbuf);
1010 last_cursormoved = curwin->w_cursor;
1011 }
1012#endif
1013
Bram Moolenaar33aec762006-01-22 23:30:12 +00001014#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
1015 /* Scroll-binding for diff mode may have been postponed until
1016 * here. Avoids doing it for every change. */
1017 if (diff_need_scrollbind)
1018 {
1019 check_scrollbind((linenr_T)0, 0L);
1020 diff_need_scrollbind = FALSE;
1021 }
1022#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001023#if defined(FEAT_FOLDING) && defined(FEAT_VISUAL)
1024 /* Include a closed fold completely in the Visual area. */
1025 foldAdjustVisual();
1026#endif
1027#ifdef FEAT_FOLDING
1028 /*
1029 * When 'foldclose' is set, apply 'foldlevel' to folds that don't
1030 * contain the cursor.
1031 * When 'foldopen' is "all", open the fold(s) under the cursor.
1032 * This may mark the window for redrawing.
1033 */
1034 if (hasAnyFolding(curwin) && !char_avail())
1035 {
1036 foldCheckClose();
1037 if (fdo_flags & FDO_ALL)
1038 foldOpenCursor();
1039 }
1040#endif
1041
1042 /*
1043 * Before redrawing, make sure w_topline is correct, and w_leftcol
1044 * if lines don't wrap, and w_skipcol if lines wrap.
1045 */
1046 update_topline();
1047 validate_cursor();
1048
1049#ifdef FEAT_VISUAL
1050 if (VIsual_active)
1051 update_curbuf(INVERTED);/* update inverted part */
1052 else
1053#endif
1054 if (must_redraw)
1055 update_screen(0);
1056 else if (redraw_cmdline || clear_cmdline)
1057 showmode();
1058#ifdef FEAT_WINDOWS
1059 redraw_statuslines();
1060#endif
1061#ifdef FEAT_TITLE
1062 if (need_maketitle)
1063 maketitle();
1064#endif
1065 /* display message after redraw */
1066 if (keep_msg != NULL)
1067 {
1068 char_u *p;
1069
1070 /* msg_attr_keep() will set keep_msg to NULL, must free the
1071 * string here. */
1072 p = keep_msg;
Bram Moolenaar238a5642006-02-21 22:12:05 +00001073 keep_msg = NULL;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001074 msg_attr(p, keep_msg_attr);
1075 vim_free(p);
1076 }
1077 if (need_fileinfo) /* show file info after redraw */
1078 {
1079 fileinfo(FALSE, TRUE, FALSE);
1080 need_fileinfo = FALSE;
1081 }
1082
1083 emsg_on_display = FALSE; /* can delete error message now */
1084 did_emsg = FALSE;
1085 msg_didany = FALSE; /* reset lines_left in msg_start() */
Bram Moolenaar661b1822005-07-28 22:36:45 +00001086 may_clear_sb_text(); /* clear scroll-back text on next msg */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001087 showruler(FALSE);
1088
1089 setcursor();
1090 cursor_on();
1091
1092 do_redraw = FALSE;
1093 }
1094#ifdef FEAT_GUI
1095 if (need_mouse_correct)
1096 gui_mouse_correct();
1097#endif
1098
1099 /*
1100 * Update w_curswant if w_set_curswant has been set.
1101 * Postponed until here to avoid computing w_virtcol too often.
1102 */
1103 update_curswant();
1104
1105 /*
1106 * If we're invoked as ex, do a round of ex commands.
1107 * Otherwise, get and execute a normal mode command.
1108 */
1109 if (exmode_active)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001110 {
1111 if (noexmode) /* End of ":global/path/visual" commands */
1112 return;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001113 do_exmode(exmode_active == EXMODE_VIM);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001114 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001115 else
1116 normal_cmd(&oa, TRUE);
1117 }
1118}
1119
1120
1121#if defined(USE_XSMP) || defined(FEAT_GUI_MSWIN) || defined(PROTO)
1122/*
1123 * Exit, but leave behind swap files for modified buffers.
1124 */
1125 void
1126getout_preserve_modified(exitval)
1127 int exitval;
1128{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001129# if defined(SIGHUP) && defined(SIG_IGN)
1130 /* Ignore SIGHUP, because a dropped connection causes a read error, which
1131 * makes Vim exit and then handling SIGHUP causes various reentrance
1132 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001133 signal(SIGHUP, SIG_IGN);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001134# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001135
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001136 ml_close_notmod(); /* close all not-modified buffers */
1137 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
1138 ml_close_all(FALSE); /* close all memfiles, without deleting */
1139 getout(exitval); /* exit Vim properly */
1140}
1141#endif
1142
1143
1144/* Exit properly */
1145 void
1146getout(exitval)
1147 int exitval;
1148{
1149#ifdef FEAT_AUTOCMD
1150 buf_T *buf;
1151 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001152 tabpage_T *tp, *next_tp;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001153#endif
1154
1155 exiting = TRUE;
1156
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001157 /* When running in Ex mode an error causes us to exit with a non-zero exit
1158 * code. POSIX requires this, although it's not 100% clear from the
1159 * standard. */
1160 if (exmode_active)
1161 exitval += ex_exitval;
1162
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001163 /* Position the cursor on the last screen line, below all the text */
1164#ifdef FEAT_GUI
1165 if (!gui.in_use)
1166#endif
1167 windgoto((int)Rows - 1, 0);
1168
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +00001169#if defined(FEAT_EVAL) || defined(FEAT_SYN_HL)
1170 /* Optionally print hashtable efficiency. */
1171 hash_debug_results();
1172#endif
1173
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001174#ifdef FEAT_GUI
1175 msg_didany = FALSE;
1176#endif
1177
1178#ifdef FEAT_AUTOCMD
1179 /* Trigger BufWinLeave for all windows, but only once per buffer. */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001180# if defined FEAT_WINDOWS
1181 for (tp = first_tabpage; tp != NULL; tp = next_tp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001182 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001183 next_tp = tp->tp_next;
Bram Moolenaar238a5642006-02-21 22:12:05 +00001184 for (wp = (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001185 ? firstwin : tp->tp_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001186 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001187 buf = wp->w_buffer;
1188 if (buf->b_changedtick != -1)
1189 {
1190 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001191 FALSE, buf);
Bram Moolenaarf740b292006-02-16 22:11:02 +00001192 buf->b_changedtick = -1; /* note that we did it already */
1193 /* start all over, autocommands may mess up the lists */
1194 next_tp = first_tabpage;
1195 break;
1196 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001197 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001198 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001199# else
1200 apply_autocmds(EVENT_BUFWINLEAVE, curbuf, curbuf->b_fname, FALSE, curbuf);
1201# endif
Bram Moolenaar33aec762006-01-22 23:30:12 +00001202
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001203 /* Trigger BufUnload for buffers that are loaded */
1204 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1205 if (buf->b_ml.ml_mfp != NULL)
1206 {
1207 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
1208 FALSE, buf);
1209 if (!buf_valid(buf)) /* autocmd may delete the buffer */
1210 break;
1211 }
1212 apply_autocmds(EVENT_VIMLEAVEPRE, NULL, NULL, FALSE, curbuf);
1213#endif
1214
1215#ifdef FEAT_VIMINFO
1216 if (*p_viminfo != NUL)
1217 /* Write out the registers, history, marks etc, to the viminfo file */
1218 write_viminfo(NULL, FALSE);
1219#endif
1220
1221#ifdef FEAT_AUTOCMD
1222 apply_autocmds(EVENT_VIMLEAVE, NULL, NULL, FALSE, curbuf);
1223#endif
1224
Bram Moolenaar05159a02005-02-26 23:04:13 +00001225#ifdef FEAT_PROFILE
1226 profile_dump();
1227#endif
1228
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001229 if (did_emsg
1230#ifdef FEAT_GUI
1231 || (gui.in_use && msg_didany && p_verbose > 0)
1232#endif
1233 )
1234 {
1235 /* give the user a chance to read the (error) message */
1236 no_wait_return = FALSE;
1237 wait_return(FALSE);
1238 }
1239
1240#ifdef FEAT_AUTOCMD
1241 /* Position the cursor again, the autocommands may have moved it */
1242# ifdef FEAT_GUI
1243 if (!gui.in_use)
1244# endif
1245 windgoto((int)Rows - 1, 0);
1246#endif
1247
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001248#ifdef FEAT_MZSCHEME
1249 mzscheme_end();
1250#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001251#ifdef FEAT_TCL
1252 tcl_end();
1253#endif
1254#ifdef FEAT_RUBY
1255 ruby_end();
1256#endif
1257#ifdef FEAT_PYTHON
1258 python_end();
1259#endif
1260#ifdef FEAT_PERL
1261 perl_end();
1262#endif
1263#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
1264 iconv_end();
1265#endif
1266#ifdef FEAT_NETBEANS_INTG
1267 netbeans_end();
1268#endif
1269
1270 mch_exit(exitval);
1271}
1272
1273/*
1274 * Get a (optional) count for a Vim argument.
1275 */
1276 static int
1277get_number_arg(p, idx, def)
1278 char_u *p; /* pointer to argument */
1279 int *idx; /* index in argument, is incremented */
1280 int def; /* default value */
1281{
1282 if (vim_isdigit(p[*idx]))
1283 {
1284 def = atoi((char *)&(p[*idx]));
1285 while (vim_isdigit(p[*idx]))
1286 *idx = *idx + 1;
1287 }
1288 return def;
1289}
1290
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001291#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1292/*
1293 * Setup to use the current locale (for ctype() and many other things).
1294 */
1295 static void
1296init_locale()
1297{
1298 setlocale(LC_ALL, "");
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001299# ifdef WIN32
1300 /* Apparently MS-Windows printf() may cause a crash when we give it 8-bit
1301 * text while it's expecting text in the current locale. This call avoids
1302 * that. */
1303 setlocale(LC_CTYPE, "C");
1304# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001305
1306# ifdef FEAT_GETTEXT
1307 {
1308 int mustfree = FALSE;
1309 char_u *p;
1310
1311# ifdef DYNAMIC_GETTEXT
1312 /* Initialize the gettext library */
1313 dyn_libintl_init(NULL);
1314# endif
1315 /* expand_env() doesn't work yet, because chartab[] is not initialized
1316 * yet, call vim_getenv() directly */
1317 p = vim_getenv((char_u *)"VIMRUNTIME", &mustfree);
1318 if (p != NULL && *p != NUL)
1319 {
1320 STRCPY(NameBuff, p);
1321 STRCAT(NameBuff, "/lang");
1322 bindtextdomain(VIMPACKAGE, (char *)NameBuff);
1323 }
1324 if (mustfree)
1325 vim_free(p);
1326 textdomain(VIMPACKAGE);
1327 }
1328# endif
1329}
1330#endif
1331
1332/*
1333 * Check for: [r][e][g][vi|vim|view][diff][ex[im]]
1334 * If the executable name starts with "r" we disable shell commands.
1335 * If the next character is "e" we run in Easy mode.
1336 * If the next character is "g" we run the GUI version.
1337 * If the next characters are "view" we start in readonly mode.
1338 * If the next characters are "diff" or "vimdiff" we start in diff mode.
1339 * If the next characters are "ex" we start in Ex mode. If it's followed
1340 * by "im" use improved Ex mode.
1341 */
1342 static void
1343parse_command_name(parmp)
1344 mparm_T *parmp;
1345{
1346 char_u *initstr;
1347
1348 initstr = gettail((char_u *)parmp->argv[0]);
1349
1350#ifdef MACOS_X_UNIX
1351 /* An issue has been seen when launching Vim in such a way that
1352 * $PWD/$ARGV[0] or $ARGV[0] is not the absolute path to the
1353 * executable or a symbolic link of it. Until this issue is resolved
1354 * we prohibit the GUI from being used.
1355 */
1356 if (STRCMP(initstr, parmp->argv[0]) == 0)
1357 disallow_gui = TRUE;
1358
1359 /* TODO: On MacOS X default to gui if argv[0] ends in:
1360 * /vim.app/Contents/MacOS/Vim */
1361#endif
1362
1363#ifdef FEAT_EVAL
1364 set_vim_var_string(VV_PROGNAME, initstr, -1);
1365#endif
1366
1367 if (TOLOWER_ASC(initstr[0]) == 'r')
1368 {
1369 restricted = TRUE;
1370 ++initstr;
1371 }
1372
1373 /* Avoid using evim mode for "editor". */
1374 if (TOLOWER_ASC(initstr[0]) == 'e'
1375 && (TOLOWER_ASC(initstr[1]) == 'v'
1376 || TOLOWER_ASC(initstr[1]) == 'g'))
1377 {
1378#ifdef FEAT_GUI
1379 gui.starting = TRUE;
1380#endif
1381 parmp->evim_mode = TRUE;
1382 ++initstr;
1383 }
1384
1385 if (TOLOWER_ASC(initstr[0]) == 'g' || initstr[0] == 'k')
1386 {
1387 main_start_gui();
1388#ifdef FEAT_GUI
1389 ++initstr;
1390#endif
1391 }
1392
1393 if (STRNICMP(initstr, "view", 4) == 0)
1394 {
1395 readonlymode = TRUE;
1396 curbuf->b_p_ro = TRUE;
1397 p_uc = 10000; /* don't update very often */
1398 initstr += 4;
1399 }
1400 else if (STRNICMP(initstr, "vim", 3) == 0)
1401 initstr += 3;
1402
1403 /* Catch "[r][g]vimdiff" and "[r][g]viewdiff". */
1404 if (STRICMP(initstr, "diff") == 0)
1405 {
1406#ifdef FEAT_DIFF
1407 parmp->diff_mode = TRUE;
1408#else
1409 mch_errmsg(_("This Vim was not compiled with the diff feature."));
1410 mch_errmsg("\n");
1411 mch_exit(2);
1412#endif
1413 }
1414
1415 if (STRNICMP(initstr, "ex", 2) == 0)
1416 {
1417 if (STRNICMP(initstr + 2, "im", 2) == 0)
1418 exmode_active = EXMODE_VIM;
1419 else
1420 exmode_active = EXMODE_NORMAL;
1421 change_compatible(TRUE); /* set 'compatible' */
1422 }
1423}
1424
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001425/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00001426 * Get the name of the display, before gui_prepare() removes it from
1427 * argv[]. Used for the xterm-clipboard display.
1428 *
1429 * Also find the --server... arguments and --socketid
1430 */
1431/*ARGSUSED*/
1432 static void
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001433early_arg_scan(parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001434 mparm_T *parmp;
1435{
1436#if defined(FEAT_XCLIPBOARD) || defined(FEAT_CLIENTSERVER)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001437 int argc = parmp->argc;
1438 char **argv = parmp->argv;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001439 int i;
1440
1441 for (i = 1; i < argc; i++)
1442 {
1443 if (STRCMP(argv[i], "--") == 0)
1444 break;
1445# ifdef FEAT_XCLIPBOARD
1446 else if (STRICMP(argv[i], "-display") == 0
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001447# if defined(FEAT_GUI_GTK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001448 || STRICMP(argv[i], "--display") == 0
1449# endif
1450 )
1451 {
1452 if (i == argc - 1)
1453 mainerr_arg_missing((char_u *)argv[i]);
1454 xterm_display = argv[++i];
1455 }
1456# endif
1457# ifdef FEAT_CLIENTSERVER
1458 else if (STRICMP(argv[i], "--servername") == 0)
1459 {
1460 if (i == argc - 1)
1461 mainerr_arg_missing((char_u *)argv[i]);
1462 parmp->serverName_arg = (char_u *)argv[++i];
1463 }
Bram Moolenaareb94e552006-03-11 21:35:11 +00001464 else if (STRICMP(argv[i], "--serverlist") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001465 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001466 else if (STRNICMP(argv[i], "--remote", 8) == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001467 {
1468 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001469# ifdef FEAT_GUI
1470 if (strstr(argv[i], "-wait") != 0)
1471 /* don't fork() when starting the GUI to edit files ourself */
1472 gui.dofork = FALSE;
1473# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001474 }
1475# endif
1476# ifdef FEAT_GUI_GTK
1477 else if (STRICMP(argv[i], "--socketid") == 0)
1478 {
1479 unsigned int socket_id;
1480 int count;
1481
1482 if (i == argc - 1)
1483 mainerr_arg_missing((char_u *)argv[i]);
1484 if (STRNICMP(argv[i+1], "0x", 2) == 0)
1485 count = sscanf(&(argv[i + 1][2]), "%x", &socket_id);
1486 else
1487 count = sscanf(argv[i+1], "%u", &socket_id);
1488 if (count != 1)
1489 mainerr(ME_INVALID_ARG, (char_u *)argv[i]);
1490 else
1491 gtk_socket_id = socket_id;
1492 i++;
1493 }
1494 else if (STRICMP(argv[i], "--echo-wid") == 0)
1495 echo_wid_arg = TRUE;
1496# endif
1497 }
1498#endif
1499}
1500
1501/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001502 * Scan the command line arguments.
1503 */
1504 static void
1505command_line_scan(parmp)
1506 mparm_T *parmp;
1507{
1508 int argc = parmp->argc;
1509 char **argv = parmp->argv;
1510 int argv_idx; /* index in argv[n][] */
1511 int had_minmin = FALSE; /* found "--" argument */
1512 int want_argument; /* option argument with argument */
1513 int c;
Bram Moolenaar231334e2005-07-25 20:46:57 +00001514 char_u *p = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001515 long n;
1516
1517 --argc;
1518 ++argv;
1519 argv_idx = 1; /* active option letter is argv[0][argv_idx] */
1520 while (argc > 0)
1521 {
1522 /*
1523 * "+" or "+{number}" or "+/{pat}" or "+{command}" argument.
1524 */
1525 if (argv[0][0] == '+' && !had_minmin)
1526 {
1527 if (parmp->n_commands >= MAX_ARG_CMDS)
1528 mainerr(ME_EXTRA_CMD, NULL);
1529 argv_idx = -1; /* skip to next argument */
1530 if (argv[0][1] == NUL)
1531 parmp->commands[parmp->n_commands++] = (char_u *)"$";
1532 else
1533 parmp->commands[parmp->n_commands++] = (char_u *)&(argv[0][1]);
1534 }
1535
1536 /*
1537 * Optional argument.
1538 */
1539 else if (argv[0][0] == '-' && !had_minmin)
1540 {
1541 want_argument = FALSE;
1542 c = argv[0][argv_idx++];
1543#ifdef VMS
1544 /*
1545 * VMS only uses upper case command lines. Interpret "-X" as "-x"
1546 * and "-/X" as "-X".
1547 */
1548 if (c == '/')
1549 {
1550 c = argv[0][argv_idx++];
1551 c = TOUPPER_ASC(c);
1552 }
1553 else
1554 c = TOLOWER_ASC(c);
1555#endif
1556 switch (c)
1557 {
1558 case NUL: /* "vim -" read from stdin */
1559 /* "ex -" silent mode */
1560 if (exmode_active)
1561 silent_mode = TRUE;
1562 else
1563 {
1564 if (parmp->edit_type != EDIT_NONE)
1565 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1566 parmp->edit_type = EDIT_STDIN;
1567 read_cmd_fd = 2; /* read from stderr instead of stdin */
1568 }
1569 argv_idx = -1; /* skip to next argument */
1570 break;
1571
1572 case '-': /* "--" don't take any more option arguments */
1573 /* "--help" give help message */
1574 /* "--version" give version message */
1575 /* "--literal" take files literally */
1576 /* "--nofork" don't fork */
1577 /* "--noplugin[s]" skip plugins */
1578 /* "--cmd <cmd>" execute cmd before vimrc */
1579 if (STRICMP(argv[0] + argv_idx, "help") == 0)
1580 usage();
1581 else if (STRICMP(argv[0] + argv_idx, "version") == 0)
1582 {
1583 Columns = 80; /* need to init Columns */
1584 info_message = TRUE; /* use mch_msg(), not mch_errmsg() */
1585 list_version();
1586 msg_putchar('\n');
1587 msg_didout = FALSE;
1588 mch_exit(0);
1589 }
1590 else if (STRNICMP(argv[0] + argv_idx, "literal", 7) == 0)
1591 {
1592#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
1593 parmp->literal = TRUE;
1594#endif
1595 }
1596 else if (STRNICMP(argv[0] + argv_idx, "nofork", 6) == 0)
1597 {
1598#ifdef FEAT_GUI
1599 gui.dofork = FALSE; /* don't fork() when starting GUI */
1600#endif
1601 }
1602 else if (STRNICMP(argv[0] + argv_idx, "noplugin", 8) == 0)
1603 p_lpl = FALSE;
1604 else if (STRNICMP(argv[0] + argv_idx, "cmd", 3) == 0)
1605 {
1606 want_argument = TRUE;
1607 argv_idx += 3;
1608 }
1609#ifdef FEAT_CLIENTSERVER
1610 else if (STRNICMP(argv[0] + argv_idx, "serverlist", 10) == 0)
1611 ; /* already processed -- no arg */
1612 else if (STRNICMP(argv[0] + argv_idx, "servername", 10) == 0
1613 || STRNICMP(argv[0] + argv_idx, "serversend", 10) == 0)
1614 {
1615 /* already processed -- snatch the following arg */
1616 if (argc > 1)
1617 {
1618 --argc;
1619 ++argv;
1620 }
1621 }
1622#endif
1623#ifdef FEAT_GUI_GTK
1624 else if (STRNICMP(argv[0] + argv_idx, "socketid", 8) == 0)
1625 {
1626 /* already processed -- snatch the following arg */
1627 if (argc > 1)
1628 {
1629 --argc;
1630 ++argv;
1631 }
1632 }
1633 else if (STRNICMP(argv[0] + argv_idx, "echo-wid", 8) == 0)
1634 {
1635 /* already processed, skip */
1636 }
1637#endif
1638 else
1639 {
1640 if (argv[0][argv_idx])
1641 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
1642 had_minmin = TRUE;
1643 }
1644 if (!want_argument)
1645 argv_idx = -1; /* skip to next argument */
1646 break;
1647
1648 case 'A': /* "-A" start in Arabic mode */
1649#ifdef FEAT_ARABIC
1650 set_option_value((char_u *)"arabic", 1L, NULL, 0);
1651#else
1652 mch_errmsg(_(e_noarabic));
1653 mch_exit(2);
1654#endif
1655 break;
1656
1657 case 'b': /* "-b" binary mode */
Bram Moolenaar231334e2005-07-25 20:46:57 +00001658 /* Needs to be effective before expanding file names, because
1659 * for Win32 this makes us edit a shortcut file itself,
1660 * instead of the file it links to. */
1661 set_options_bin(curbuf->b_p_bin, 1, 0);
1662 curbuf->b_p_bin = 1; /* binary file I/O */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001663 break;
1664
1665 case 'C': /* "-C" Compatible */
1666 change_compatible(TRUE);
1667 break;
1668
1669 case 'e': /* "-e" Ex mode */
1670 exmode_active = EXMODE_NORMAL;
1671 break;
1672
1673 case 'E': /* "-E" Improved Ex mode */
1674 exmode_active = EXMODE_VIM;
1675 break;
1676
1677 case 'f': /* "-f" GUI: run in foreground. Amiga: open
1678 window directly, not with newcli */
1679#ifdef FEAT_GUI
1680 gui.dofork = FALSE; /* don't fork() when starting GUI */
1681#endif
1682 break;
1683
1684 case 'g': /* "-g" start GUI */
1685 main_start_gui();
1686 break;
1687
1688 case 'F': /* "-F" start in Farsi mode: rl + fkmap set */
1689#ifdef FEAT_FKMAP
1690 curwin->w_p_rl = p_fkmap = TRUE;
1691#else
1692 mch_errmsg(_(e_nofarsi));
1693 mch_exit(2);
1694#endif
1695 break;
1696
1697 case 'h': /* "-h" give help message */
1698#ifdef FEAT_GUI_GNOME
1699 /* Tell usage() to exit for "gvim". */
1700 gui.starting = FALSE;
1701#endif
1702 usage();
1703 break;
1704
1705 case 'H': /* "-H" start in Hebrew mode: rl + hkmap set */
1706#ifdef FEAT_RIGHTLEFT
1707 curwin->w_p_rl = p_hkmap = TRUE;
1708#else
1709 mch_errmsg(_(e_nohebrew));
1710 mch_exit(2);
1711#endif
1712 break;
1713
1714 case 'l': /* "-l" lisp mode, 'lisp' and 'showmatch' on */
1715#ifdef FEAT_LISP
1716 set_option_value((char_u *)"lisp", 1L, NULL, 0);
1717 p_sm = TRUE;
1718#endif
1719 break;
1720
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001721 case 'M': /* "-M" no changes or writing of files */
1722 reset_modifiable();
1723 /* FALLTHROUGH */
1724
1725 case 'm': /* "-m" no writing of files */
1726 p_write = FALSE;
1727 break;
1728
1729 case 'y': /* "-y" easy mode */
1730#ifdef FEAT_GUI
1731 gui.starting = TRUE; /* start GUI a bit later */
1732#endif
1733 parmp->evim_mode = TRUE;
1734 break;
1735
1736 case 'N': /* "-N" Nocompatible */
1737 change_compatible(FALSE);
1738 break;
1739
1740 case 'n': /* "-n" no swap file */
1741 parmp->no_swap_file = TRUE;
1742 break;
1743
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001744 case 'p': /* "-p[N]" open N tab pages */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00001745#ifdef TARGET_API_MAC_OSX
1746 /* For some reason on MacOS X, an argument like:
1747 -psn_0_10223617 is passed in when invoke from Finder
1748 or with the 'open' command */
1749 if (argv[0][argv_idx] == 's')
1750 {
1751 argv_idx = -1; /* bypass full -psn */
1752 main_start_gui();
1753 break;
1754 }
1755#endif
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001756#ifdef FEAT_WINDOWS
1757 /* default is 0: open window for each file */
1758 parmp->window_count = get_number_arg((char_u *)argv[0],
1759 &argv_idx, 0);
1760 parmp->window_layout = WIN_TABS;
1761#endif
1762 break;
1763
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001764 case 'o': /* "-o[N]" open N horizontal split windows */
1765#ifdef FEAT_WINDOWS
1766 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00001767 parmp->window_count = get_number_arg((char_u *)argv[0],
1768 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001769 parmp->window_layout = WIN_HOR;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001770#endif
1771 break;
1772
1773 case 'O': /* "-O[N]" open N vertical split windows */
1774#if defined(FEAT_VERTSPLIT) && defined(FEAT_WINDOWS)
1775 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00001776 parmp->window_count = get_number_arg((char_u *)argv[0],
1777 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001778 parmp->window_layout = WIN_VER;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001779#endif
1780 break;
1781
1782#ifdef FEAT_QUICKFIX
1783 case 'q': /* "-q" QuickFix mode */
1784 if (parmp->edit_type != EDIT_NONE)
1785 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1786 parmp->edit_type = EDIT_QF;
1787 if (argv[0][argv_idx]) /* "-q{errorfile}" */
1788 {
1789 parmp->use_ef = (char_u *)argv[0] + argv_idx;
1790 argv_idx = -1;
1791 }
1792 else if (argc > 1) /* "-q {errorfile}" */
1793 want_argument = TRUE;
1794 break;
1795#endif
1796
1797 case 'R': /* "-R" readonly mode */
1798 readonlymode = TRUE;
1799 curbuf->b_p_ro = TRUE;
1800 p_uc = 10000; /* don't update very often */
1801 break;
1802
1803 case 'r': /* "-r" recovery mode */
1804 case 'L': /* "-L" recovery mode */
1805 recoverymode = 1;
1806 break;
1807
1808 case 's':
1809 if (exmode_active) /* "-s" silent (batch) mode */
1810 silent_mode = TRUE;
1811 else /* "-s {scriptin}" read from script file */
1812 want_argument = TRUE;
1813 break;
1814
1815 case 't': /* "-t {tag}" or "-t{tag}" jump to tag */
1816 if (parmp->edit_type != EDIT_NONE)
1817 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1818 parmp->edit_type = EDIT_TAG;
1819 if (argv[0][argv_idx]) /* "-t{tag}" */
1820 {
1821 parmp->tagname = (char_u *)argv[0] + argv_idx;
1822 argv_idx = -1;
1823 }
1824 else /* "-t {tag}" */
1825 want_argument = TRUE;
1826 break;
1827
1828#ifdef FEAT_EVAL
1829 case 'D': /* "-D" Debugging */
1830 parmp->use_debug_break_level = 9999;
1831 break;
1832#endif
1833#ifdef FEAT_DIFF
1834 case 'd': /* "-d" 'diff' */
1835# ifdef AMIGA
1836 /* check for "-dev {device}" */
1837 if (argv[0][argv_idx] == 'e' && argv[0][argv_idx + 1] == 'v')
1838 want_argument = TRUE;
1839 else
1840# endif
1841 parmp->diff_mode = TRUE;
1842 break;
1843#endif
1844 case 'V': /* "-V{N}" Verbose level */
1845 /* default is 10: a little bit verbose */
1846 p_verbose = get_number_arg((char_u *)argv[0], &argv_idx, 10);
1847 if (argv[0][argv_idx] != NUL)
1848 {
1849 set_option_value((char_u *)"verbosefile", 0L,
1850 (char_u *)argv[0] + argv_idx, 0);
1851 argv_idx = STRLEN(argv[0]);
1852 }
1853 break;
1854
1855 case 'v': /* "-v" Vi-mode (as if called "vi") */
1856 exmode_active = 0;
1857#ifdef FEAT_GUI
1858 gui.starting = FALSE; /* don't start GUI */
1859#endif
1860 break;
1861
1862 case 'w': /* "-w{number}" set window height */
1863 /* "-w {scriptout}" write to script */
1864 if (vim_isdigit(((char_u *)argv[0])[argv_idx]))
1865 {
1866 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
1867 set_option_value((char_u *)"window", n, NULL, 0);
1868 break;
1869 }
1870 want_argument = TRUE;
1871 break;
1872
1873#ifdef FEAT_CRYPT
1874 case 'x': /* "-x" encrypted reading/writing of files */
1875 parmp->ask_for_key = TRUE;
1876 break;
1877#endif
1878
1879 case 'X': /* "-X" don't connect to X server */
1880#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
1881 x_no_connect = TRUE;
1882#endif
1883 break;
1884
1885 case 'Z': /* "-Z" restricted mode */
1886 restricted = TRUE;
1887 break;
1888
1889 case 'c': /* "-c{command}" or "-c {command}" execute
1890 command */
1891 if (argv[0][argv_idx] != NUL)
1892 {
1893 if (parmp->n_commands >= MAX_ARG_CMDS)
1894 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar231334e2005-07-25 20:46:57 +00001895 parmp->commands[parmp->n_commands++] = (char_u *)argv[0]
1896 + argv_idx;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001897 argv_idx = -1;
1898 break;
1899 }
1900 /*FALLTHROUGH*/
1901 case 'S': /* "-S {file}" execute Vim script */
1902 case 'i': /* "-i {viminfo}" use for viminfo */
1903#ifndef FEAT_DIFF
1904 case 'd': /* "-d {device}" device (for Amiga) */
1905#endif
1906 case 'T': /* "-T {terminal}" terminal name */
1907 case 'u': /* "-u {vimrc}" vim inits file */
1908 case 'U': /* "-U {gvimrc}" gvim inits file */
1909 case 'W': /* "-W {scriptout}" overwrite */
1910#ifdef FEAT_GUI_W32
1911 case 'P': /* "-P {parent title}" MDI parent */
1912#endif
1913 want_argument = TRUE;
1914 break;
1915
1916 default:
1917 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
1918 }
1919
1920 /*
1921 * Handle option arguments with argument.
1922 */
1923 if (want_argument)
1924 {
1925 /*
1926 * Check for garbage immediately after the option letter.
1927 */
1928 if (argv[0][argv_idx] != NUL)
1929 mainerr(ME_GARBAGE, (char_u *)argv[0]);
1930
1931 --argc;
1932 if (argc < 1 && c != 'S')
1933 mainerr_arg_missing((char_u *)argv[0]);
1934 ++argv;
1935 argv_idx = -1;
1936
1937 switch (c)
1938 {
1939 case 'c': /* "-c {command}" execute command */
1940 case 'S': /* "-S {file}" execute Vim script */
1941 if (parmp->n_commands >= MAX_ARG_CMDS)
1942 mainerr(ME_EXTRA_CMD, NULL);
1943 if (c == 'S')
1944 {
1945 char *a;
1946
1947 if (argc < 1)
1948 /* "-S" without argument: use default session file
1949 * name. */
1950 a = SESSION_FILE;
1951 else if (argv[0][0] == '-')
1952 {
1953 /* "-S" followed by another option: use default
1954 * session file name. */
1955 a = SESSION_FILE;
1956 ++argc;
1957 --argv;
1958 }
1959 else
1960 a = argv[0];
1961 p = alloc((unsigned)(STRLEN(a) + 4));
1962 if (p == NULL)
1963 mch_exit(2);
1964 sprintf((char *)p, "so %s", a);
1965 parmp->cmds_tofree[parmp->n_commands] = TRUE;
1966 parmp->commands[parmp->n_commands++] = p;
1967 }
1968 else
Bram Moolenaar231334e2005-07-25 20:46:57 +00001969 parmp->commands[parmp->n_commands++] =
1970 (char_u *)argv[0];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001971 break;
1972
1973 case '-': /* "--cmd {command}" execute command */
1974 if (parmp->n_pre_commands >= MAX_ARG_CMDS)
1975 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar231334e2005-07-25 20:46:57 +00001976 parmp->pre_commands[parmp->n_pre_commands++] =
1977 (char_u *)argv[0];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001978 break;
1979
1980 /* case 'd': -d {device} is handled in mch_check_win() for the
1981 * Amiga */
1982
1983#ifdef FEAT_QUICKFIX
1984 case 'q': /* "-q {errorfile}" QuickFix mode */
1985 parmp->use_ef = (char_u *)argv[0];
1986 break;
1987#endif
1988
1989 case 'i': /* "-i {viminfo}" use for viminfo */
1990 use_viminfo = (char_u *)argv[0];
1991 break;
1992
1993 case 's': /* "-s {scriptin}" read from script file */
1994 if (scriptin[0] != NULL)
1995 {
1996scripterror:
1997 mch_errmsg(_("Attempt to open script file again: \""));
1998 mch_errmsg(argv[-1]);
1999 mch_errmsg(" ");
2000 mch_errmsg(argv[0]);
2001 mch_errmsg("\"\n");
2002 mch_exit(2);
2003 }
2004 if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL)
2005 {
2006 mch_errmsg(_("Cannot open for reading: \""));
2007 mch_errmsg(argv[0]);
2008 mch_errmsg("\"\n");
2009 mch_exit(2);
2010 }
2011 if (save_typebuf() == FAIL)
2012 mch_exit(2); /* out of memory */
2013 break;
2014
2015 case 't': /* "-t {tag}" */
2016 parmp->tagname = (char_u *)argv[0];
2017 break;
2018
2019 case 'T': /* "-T {terminal}" terminal name */
2020 /*
2021 * The -T term argument is always available and when
2022 * HAVE_TERMLIB is supported it overrides the environment
2023 * variable TERM.
2024 */
2025#ifdef FEAT_GUI
2026 if (term_is_gui((char_u *)argv[0]))
2027 gui.starting = TRUE; /* start GUI a bit later */
2028 else
2029#endif
2030 parmp->term = (char_u *)argv[0];
2031 break;
2032
2033 case 'u': /* "-u {vimrc}" vim inits file */
2034 parmp->use_vimrc = (char_u *)argv[0];
2035 break;
2036
2037 case 'U': /* "-U {gvimrc}" gvim inits file */
2038#ifdef FEAT_GUI
2039 use_gvimrc = (char_u *)argv[0];
2040#endif
2041 break;
2042
2043 case 'w': /* "-w {nr}" 'window' value */
2044 /* "-w {scriptout}" append to script file */
2045 if (vim_isdigit(*((char_u *)argv[0])))
2046 {
2047 argv_idx = 0;
2048 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2049 set_option_value((char_u *)"window", n, NULL, 0);
2050 argv_idx = -1;
2051 break;
2052 }
2053 /*FALLTHROUGH*/
2054 case 'W': /* "-W {scriptout}" overwrite script file */
2055 if (scriptout != NULL)
2056 goto scripterror;
2057 if ((scriptout = mch_fopen(argv[0],
2058 c == 'w' ? APPENDBIN : WRITEBIN)) == NULL)
2059 {
2060 mch_errmsg(_("Cannot open for script output: \""));
2061 mch_errmsg(argv[0]);
2062 mch_errmsg("\"\n");
2063 mch_exit(2);
2064 }
2065 break;
2066
2067#ifdef FEAT_GUI_W32
2068 case 'P': /* "-P {parent title}" MDI parent */
2069 gui_mch_set_parent(argv[0]);
2070 break;
2071#endif
2072 }
2073 }
2074 }
2075
2076 /*
2077 * File name argument.
2078 */
2079 else
2080 {
2081 argv_idx = -1; /* skip to next argument */
2082
2083 /* Check for only one type of editing. */
2084 if (parmp->edit_type != EDIT_NONE && parmp->edit_type != EDIT_FILE)
2085 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2086 parmp->edit_type = EDIT_FILE;
2087
2088#ifdef MSWIN
2089 /* Remember if the argument was a full path before changing
2090 * slashes to backslashes. */
2091 if (argv[0][0] != NUL && argv[0][1] == ':' && argv[0][2] == '\\')
2092 parmp->full_path = TRUE;
2093#endif
2094
2095 /* Add the file to the global argument list. */
2096 if (ga_grow(&global_alist.al_ga, 1) == FAIL
2097 || (p = vim_strsave((char_u *)argv[0])) == NULL)
2098 mch_exit(2);
2099#ifdef FEAT_DIFF
2100 if (parmp->diff_mode && mch_isdir(p) && GARGCOUNT > 0
2101 && !mch_isdir(alist_name(&GARGLIST[0])))
2102 {
2103 char_u *r;
2104
2105 r = concat_fnames(p, gettail(alist_name(&GARGLIST[0])), TRUE);
2106 if (r != NULL)
2107 {
2108 vim_free(p);
2109 p = r;
2110 }
2111 }
2112#endif
2113#if defined(__CYGWIN32__) && !defined(WIN32)
2114 /*
2115 * If vim is invoked by non-Cygwin tools, convert away any
2116 * DOS paths, so things like .swp files are created correctly.
2117 * Look for evidence of non-Cygwin paths before we bother.
2118 * This is only for when using the Unix files.
2119 */
2120 if (strpbrk(p, "\\:") != NULL)
2121 {
2122 char posix_path[PATH_MAX];
2123
2124 cygwin_conv_to_posix_path(p, posix_path);
2125 vim_free(p);
2126 p = vim_strsave(posix_path);
2127 if (p == NULL)
2128 mch_exit(2);
2129 }
2130#endif
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002131
2132#ifdef USE_FNAME_CASE
2133 /* Make the case of the file name match the actual file. */
2134 fname_case(p, 0);
2135#endif
2136
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002137 alist_add(&global_alist, p,
2138#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
Bram Moolenaar231334e2005-07-25 20:46:57 +00002139 parmp->literal ? 2 : 0 /* add buffer nr after exp. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002140#else
2141 2 /* add buffer number now and use curbuf */
2142#endif
2143 );
2144
2145#if defined(FEAT_MBYTE) && defined(WIN32)
2146 {
2147 extern void used_file_arg(char *, int, int);
2148
2149 /* Remember this argument has been added to the argument list.
2150 * Needed when 'encoding' is changed. */
2151 used_file_arg(argv[0], parmp->literal, parmp->full_path);
2152 }
2153#endif
2154 }
2155
2156 /*
2157 * If there are no more letters after the current "-", go to next
2158 * argument. argv_idx is set to -1 when the current argument is to be
2159 * skipped.
2160 */
2161 if (argv_idx <= 0 || argv[0][argv_idx] == NUL)
2162 {
2163 --argc;
2164 ++argv;
2165 argv_idx = 1;
2166 }
2167 }
2168}
2169
2170/*
2171 * Print a warning if stdout is not a terminal.
2172 * When starting in Ex mode and commands come from a file, set Silent mode.
2173 */
2174 static void
2175check_tty(parmp)
2176 mparm_T *parmp;
2177{
2178 int input_isatty; /* is active input a terminal? */
2179
2180 input_isatty = mch_input_isatty();
2181 if (exmode_active)
2182 {
2183 if (!input_isatty)
2184 silent_mode = TRUE;
2185 }
2186 else if (parmp->want_full_screen && (!parmp->stdout_isatty || !input_isatty)
2187#ifdef FEAT_GUI
2188 /* don't want the delay when started from the desktop */
2189 && !gui.starting
2190#endif
2191 )
2192 {
2193#ifdef NBDEBUG
2194 /*
2195 * This shouldn't be necessary. But if I run netbeans with the log
2196 * output coming to the console and XOpenDisplay fails, I get vim
2197 * trying to start with input/output to my console tty. This fills my
2198 * input buffer so fast I can't even kill the process in under 2
2199 * minutes (and it beeps continuosly the whole time :-)
2200 */
2201 if (usingNetbeans && (!parmp->stdout_isatty || !input_isatty))
2202 {
2203 mch_errmsg(_("Vim: Error: Failure to start gvim from NetBeans\n"));
2204 exit(1);
2205 }
2206#endif
2207 if (!parmp->stdout_isatty)
2208 mch_errmsg(_("Vim: Warning: Output is not to a terminal\n"));
2209 if (!input_isatty)
2210 mch_errmsg(_("Vim: Warning: Input is not from a terminal\n"));
2211 out_flush();
2212 if (scriptin[0] == NULL)
2213 ui_delay(2000L, TRUE);
2214 TIME_MSG("Warning delay");
2215 }
2216}
2217
2218/*
2219 * Read text from stdin.
2220 */
2221 static void
2222read_stdin()
2223{
2224 int i;
2225
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002226#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002227 /* When getting the ATTENTION prompt here, use a dialog */
2228 swap_exists_action = SEA_DIALOG;
2229#endif
2230 no_wait_return = TRUE;
2231 i = msg_didany;
2232 set_buflisted(TRUE);
2233 (void)open_buffer(TRUE, NULL); /* create memfile and read file */
2234 no_wait_return = FALSE;
2235 msg_didany = i;
2236 TIME_MSG("reading stdin");
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002237#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002238 check_swap_exists_action();
2239#endif
2240#if !(defined(AMIGA) || defined(MACOS))
2241 /*
2242 * Close stdin and dup it from stderr. Required for GPM to work
2243 * properly, and for running external commands.
2244 * Is there any other system that cannot do this?
2245 */
2246 close(0);
2247 dup(2);
2248#endif
2249}
2250
2251/*
2252 * Create the requested number of windows and edit buffers in them.
2253 * Also does recovery if "recoverymode" set.
2254 */
2255/*ARGSUSED*/
2256 static void
2257create_windows(parmp)
2258 mparm_T *parmp;
2259{
2260#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002261 int rewind;
2262 int done = 0;
2263
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002264 /*
2265 * Create the number of windows that was requested.
2266 */
2267 if (parmp->window_count == -1) /* was not set */
2268 parmp->window_count = 1;
2269 if (parmp->window_count == 0)
2270 parmp->window_count = GARGCOUNT;
2271 if (parmp->window_count > 1)
2272 {
2273 /* Don't change the windows if there was a command in .vimrc that
2274 * already split some windows */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002275 if (parmp->window_layout == 0)
2276 parmp->window_layout = WIN_HOR;
2277 if (parmp->window_layout == WIN_TABS)
2278 {
2279 parmp->window_count = make_tabpages(parmp->window_count);
2280 TIME_MSG("making tab pages");
2281 }
2282 else if (firstwin->w_next == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002283 {
2284 parmp->window_count = make_windows(parmp->window_count,
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002285 parmp->window_layout == WIN_VER);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002286 TIME_MSG("making windows");
2287 }
2288 else
2289 parmp->window_count = win_count();
2290 }
2291 else
2292 parmp->window_count = 1;
2293#endif
2294
2295 if (recoverymode) /* do recover */
2296 {
2297 msg_scroll = TRUE; /* scroll message up */
2298 ml_recover();
2299 if (curbuf->b_ml.ml_mfp == NULL) /* failed */
2300 getout(1);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002301 do_modelines(0); /* do modelines */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002302 }
2303 else
2304 {
2305 /*
2306 * Open a buffer for windows that don't have one yet.
2307 * Commands in the .vimrc might have loaded a file or split the window.
2308 * Watch out for autocommands that delete a window.
2309 */
2310#ifdef FEAT_AUTOCMD
2311 /*
2312 * Don't execute Win/Buf Enter/Leave autocommands here
2313 */
2314 ++autocmd_no_enter;
2315 ++autocmd_no_leave;
2316#endif
2317#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002318 rewind = TRUE;
2319 while (done++ < 1000)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002320 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002321 if (rewind)
2322 {
2323 if (parmp->window_layout == WIN_TABS)
2324 goto_tabpage(1);
2325 else
2326 curwin = firstwin;
2327 }
2328 else if (parmp->window_layout == WIN_TABS)
2329 {
2330 if (curtab->tp_next == NULL)
2331 break;
2332 goto_tabpage(0);
2333 }
2334 else
2335 {
2336 if (curwin->w_next == NULL)
2337 break;
2338 curwin = curwin->w_next;
2339 }
2340 rewind = FALSE;
2341#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002342 curbuf = curwin->w_buffer;
2343 if (curbuf->b_ml.ml_mfp == NULL)
2344 {
2345#ifdef FEAT_FOLDING
2346 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2347 if (p_fdls >= 0)
2348 curwin->w_p_fdl = p_fdls;
2349#endif
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002350#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002351 /* When getting the ATTENTION prompt here, use a dialog */
2352 swap_exists_action = SEA_DIALOG;
2353#endif
2354 set_buflisted(TRUE);
2355 (void)open_buffer(FALSE, NULL); /* create memfile, read file */
2356
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002357#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002358 check_swap_exists_action();
2359#endif
2360#ifdef FEAT_AUTOCMD
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002361 rewind = TRUE; /* start again */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002362#endif
2363 }
2364#ifdef FEAT_WINDOWS
2365 ui_breakcheck();
2366 if (got_int)
2367 {
2368 (void)vgetc(); /* only break the file loading, not the rest */
2369 break;
2370 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002371 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002372#endif
2373#ifdef FEAT_WINDOWS
2374 if (parmp->window_layout == WIN_TABS)
2375 goto_tabpage(1);
2376 else
2377 curwin = firstwin;
2378 curbuf = curwin->w_buffer;
2379#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002380#ifdef FEAT_AUTOCMD
2381 --autocmd_no_enter;
2382 --autocmd_no_leave;
2383#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002384 }
2385}
2386
2387#ifdef FEAT_WINDOWS
2388 /*
2389 * If opened more than one window, start editing files in the other
2390 * windows. make_windows() has already opened the windows.
2391 */
2392 static void
2393edit_buffers(parmp)
2394 mparm_T *parmp;
2395{
2396 int arg_idx; /* index in argument list */
2397 int i;
2398
2399# ifdef FEAT_AUTOCMD
2400 /*
2401 * Don't execute Win/Buf Enter/Leave autocommands here
2402 */
2403 ++autocmd_no_enter;
2404 ++autocmd_no_leave;
2405# endif
2406 arg_idx = 1;
2407 for (i = 1; i < parmp->window_count; ++i)
2408 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002409 if (parmp->window_layout == WIN_TABS)
2410 {
2411 if (curtab->tp_next == NULL) /* just checking */
2412 break;
2413 goto_tabpage(0);
2414 }
2415 else
2416 {
2417 if (curwin->w_next == NULL) /* just checking */
2418 break;
2419 win_enter(curwin->w_next, FALSE);
2420 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002421
2422 /* Only open the file if there is no file in this window yet (that can
2423 * happen when .vimrc contains ":sall") */
2424 if (curbuf == firstwin->w_buffer || curbuf->b_ffname == NULL)
2425 {
2426 curwin->w_arg_idx = arg_idx;
2427 /* edit file from arg list, if there is one */
2428 (void)do_ecmd(0, arg_idx < GARGCOUNT
2429 ? alist_name(&GARGLIST[arg_idx]) : NULL,
2430 NULL, NULL, ECMD_LASTL, ECMD_HIDE);
2431 if (arg_idx == GARGCOUNT - 1)
2432 arg_had_last = TRUE;
2433 ++arg_idx;
2434 }
2435 ui_breakcheck();
2436 if (got_int)
2437 {
2438 (void)vgetc(); /* only break the file loading, not the rest */
2439 break;
2440 }
2441 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002442
2443 if (parmp->window_layout == WIN_TABS)
2444 goto_tabpage(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002445# ifdef FEAT_AUTOCMD
2446 --autocmd_no_enter;
2447# endif
2448 win_enter(firstwin, FALSE); /* back to first window */
2449# ifdef FEAT_AUTOCMD
2450 --autocmd_no_leave;
2451# endif
2452 TIME_MSG("editing files in windows");
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002453 if (parmp->window_count > 1 && parmp->window_layout != WIN_TABS)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002454 win_equal(curwin, FALSE, 'b'); /* adjust heights */
2455}
2456#endif /* FEAT_WINDOWS */
2457
2458/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002459 * Execute the commands from --cmd arguments "cmds[cnt]".
2460 */
2461 static void
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002462exe_pre_commands(parmp)
2463 mparm_T *parmp;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002464{
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002465 char_u **cmds = parmp->pre_commands;
2466 int cnt = parmp->n_pre_commands;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002467 int i;
2468
2469 if (cnt > 0)
2470 {
2471 curwin->w_cursor.lnum = 0; /* just in case.. */
2472 sourcing_name = (char_u *)_("pre-vimrc command line");
2473# ifdef FEAT_EVAL
2474 current_SID = SID_CMDARG;
2475# endif
2476 for (i = 0; i < cnt; ++i)
2477 do_cmdline_cmd(cmds[i]);
2478 sourcing_name = NULL;
2479# ifdef FEAT_EVAL
2480 current_SID = 0;
2481# endif
2482 TIME_MSG("--cmd commands");
2483 }
2484}
2485
2486/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002487 * Execute "+", "-c" and "-S" arguments.
2488 */
2489 static void
2490exe_commands(parmp)
2491 mparm_T *parmp;
2492{
2493 int i;
2494
2495 /*
2496 * We start commands on line 0, make "vim +/pat file" match a
2497 * pattern on line 1. But don't move the cursor when an autocommand
2498 * with g`" was used.
2499 */
2500 msg_scroll = TRUE;
2501 if (parmp->tagname == NULL && curwin->w_cursor.lnum <= 1)
2502 curwin->w_cursor.lnum = 0;
2503 sourcing_name = (char_u *)"command line";
2504#ifdef FEAT_EVAL
2505 current_SID = SID_CARG;
2506#endif
2507 for (i = 0; i < parmp->n_commands; ++i)
2508 {
2509 do_cmdline_cmd(parmp->commands[i]);
2510 if (parmp->cmds_tofree[i])
2511 vim_free(parmp->commands[i]);
2512 }
2513 sourcing_name = NULL;
2514#ifdef FEAT_EVAL
2515 current_SID = 0;
2516#endif
2517 if (curwin->w_cursor.lnum == 0)
2518 curwin->w_cursor.lnum = 1;
2519
2520 if (!exmode_active)
2521 msg_scroll = FALSE;
2522
2523#ifdef FEAT_QUICKFIX
2524 /* When started with "-q errorfile" jump to first error again. */
2525 if (parmp->edit_type == EDIT_QF)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002526 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002527#endif
2528 TIME_MSG("executing command arguments");
2529}
2530
2531/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002532 * Source startup scripts.
2533 */
2534 static void
2535source_startup_scripts(parmp)
2536 mparm_T *parmp;
2537{
2538 int i;
2539
2540 /*
2541 * For "evim" source evim.vim first of all, so that the user can overrule
2542 * any things he doesn't like.
2543 */
2544 if (parmp->evim_mode)
2545 {
2546 (void)do_source((char_u *)EVIM_FILE, FALSE, FALSE);
2547 TIME_MSG("source evim file");
2548 }
2549
2550 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002551 * If -u argument given, use only the initializations from that file and
Bram Moolenaar58d98232005-07-23 22:25:46 +00002552 * nothing else.
2553 */
2554 if (parmp->use_vimrc != NULL)
2555 {
Bram Moolenaar231334e2005-07-25 20:46:57 +00002556 if (STRCMP(parmp->use_vimrc, "NONE") == 0
2557 || STRCMP(parmp->use_vimrc, "NORC") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002558 {
2559#ifdef FEAT_GUI
2560 if (use_gvimrc == NULL) /* don't load gvimrc either */
2561 use_gvimrc = parmp->use_vimrc;
2562#endif
2563 if (parmp->use_vimrc[2] == 'N')
2564 p_lpl = FALSE; /* don't load plugins either */
2565 }
2566 else
2567 {
2568 if (do_source(parmp->use_vimrc, FALSE, FALSE) != OK)
2569 EMSG2(_("E282: Cannot read from \"%s\""), parmp->use_vimrc);
2570 }
2571 }
2572 else if (!silent_mode)
2573 {
2574#ifdef AMIGA
2575 struct Process *proc = (struct Process *)FindTask(0L);
2576 APTR save_winptr = proc->pr_WindowPtr;
2577
2578 /* Avoid a requester here for a volume that doesn't exist. */
2579 proc->pr_WindowPtr = (APTR)-1L;
2580#endif
2581
2582 /*
2583 * Get system wide defaults, if the file name is defined.
2584 */
2585#ifdef SYS_VIMRC_FILE
2586 (void)do_source((char_u *)SYS_VIMRC_FILE, FALSE, FALSE);
2587#endif
Bram Moolenaar1056d982006-03-09 22:37:52 +00002588#ifdef MACOS_X
2589 (void)do_source((char_u *)"$VIMRUNTIME/macmap.vim", FALSE, FALSE);
2590#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00002591
2592 /*
2593 * Try to read initialization commands from the following places:
2594 * - environment variable VIMINIT
2595 * - user vimrc file (s:.vimrc for Amiga, ~/.vimrc otherwise)
2596 * - second user vimrc file ($VIM/.vimrc for Dos)
2597 * - environment variable EXINIT
2598 * - user exrc file (s:.exrc for Amiga, ~/.exrc otherwise)
2599 * - second user exrc file ($VIM/.exrc for Dos)
2600 * The first that exists is used, the rest is ignored.
2601 */
2602 if (process_env((char_u *)"VIMINIT", TRUE) != OK)
2603 {
2604 if (do_source((char_u *)USR_VIMRC_FILE, TRUE, TRUE) == FAIL
2605#ifdef USR_VIMRC_FILE2
2606 && do_source((char_u *)USR_VIMRC_FILE2, TRUE, TRUE) == FAIL
2607#endif
2608#ifdef USR_VIMRC_FILE3
2609 && do_source((char_u *)USR_VIMRC_FILE3, TRUE, TRUE) == FAIL
2610#endif
2611 && process_env((char_u *)"EXINIT", FALSE) == FAIL
2612 && do_source((char_u *)USR_EXRC_FILE, FALSE, FALSE) == FAIL)
2613 {
2614#ifdef USR_EXRC_FILE2
2615 (void)do_source((char_u *)USR_EXRC_FILE2, FALSE, FALSE);
2616#endif
2617 }
2618 }
2619
2620 /*
2621 * Read initialization commands from ".vimrc" or ".exrc" in current
2622 * directory. This is only done if the 'exrc' option is set.
2623 * Because of security reasons we disallow shell and write commands
2624 * now, except for unix if the file is owned by the user or 'secure'
2625 * option has been reset in environment of global ".exrc" or ".vimrc".
2626 * Only do this if VIMRC_FILE is not the same as USR_VIMRC_FILE or
2627 * SYS_VIMRC_FILE.
2628 */
2629 if (p_exrc)
2630 {
2631#if defined(UNIX) || defined(VMS)
2632 /* If ".vimrc" file is not owned by user, set 'secure' mode. */
2633 if (!file_owned(VIMRC_FILE))
2634#endif
2635 secure = p_secure;
2636
2637 i = FAIL;
2638 if (fullpathcmp((char_u *)USR_VIMRC_FILE,
2639 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
2640#ifdef USR_VIMRC_FILE2
2641 && fullpathcmp((char_u *)USR_VIMRC_FILE2,
2642 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
2643#endif
2644#ifdef USR_VIMRC_FILE3
2645 && fullpathcmp((char_u *)USR_VIMRC_FILE3,
2646 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
2647#endif
2648#ifdef SYS_VIMRC_FILE
2649 && fullpathcmp((char_u *)SYS_VIMRC_FILE,
2650 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
2651#endif
2652 )
2653 i = do_source((char_u *)VIMRC_FILE, TRUE, TRUE);
2654
2655 if (i == FAIL)
2656 {
2657#if defined(UNIX) || defined(VMS)
2658 /* if ".exrc" is not owned by user set 'secure' mode */
2659 if (!file_owned(EXRC_FILE))
2660 secure = p_secure;
2661 else
2662 secure = 0;
2663#endif
2664 if ( fullpathcmp((char_u *)USR_EXRC_FILE,
2665 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
2666#ifdef USR_EXRC_FILE2
2667 && fullpathcmp((char_u *)USR_EXRC_FILE2,
2668 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
2669#endif
2670 )
2671 (void)do_source((char_u *)EXRC_FILE, FALSE, FALSE);
2672 }
2673 }
2674 if (secure == 2)
2675 need_wait_return = TRUE;
2676 secure = 0;
2677#ifdef AMIGA
2678 proc->pr_WindowPtr = save_winptr;
2679#endif
2680 }
2681 TIME_MSG("sourcing vimrc file(s)");
2682}
2683
2684/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002685 * Setup to start using the GUI. Exit with an error when not available.
2686 */
2687 static void
2688main_start_gui()
2689{
2690#ifdef FEAT_GUI
2691 gui.starting = TRUE; /* start GUI a bit later */
2692#else
2693 mch_errmsg(_(e_nogvim));
2694 mch_errmsg("\n");
2695 mch_exit(2);
2696#endif
2697}
2698
2699/*
2700 * Get an evironment variable, and execute it as Ex commands.
2701 * Returns FAIL if the environment variable was not executed, OK otherwise.
2702 */
2703 int
2704process_env(env, is_viminit)
2705 char_u *env;
2706 int is_viminit; /* when TRUE, called for VIMINIT */
2707{
2708 char_u *initstr;
2709 char_u *save_sourcing_name;
2710 linenr_T save_sourcing_lnum;
2711#ifdef FEAT_EVAL
2712 scid_T save_sid;
2713#endif
2714
2715 if ((initstr = mch_getenv(env)) != NULL && *initstr != NUL)
2716 {
2717 if (is_viminit)
2718 vimrc_found();
2719 save_sourcing_name = sourcing_name;
2720 save_sourcing_lnum = sourcing_lnum;
2721 sourcing_name = env;
2722 sourcing_lnum = 0;
2723#ifdef FEAT_EVAL
2724 save_sid = current_SID;
2725 current_SID = SID_ENV;
2726#endif
2727 do_cmdline_cmd(initstr);
2728 sourcing_name = save_sourcing_name;
2729 sourcing_lnum = save_sourcing_lnum;
2730#ifdef FEAT_EVAL
2731 current_SID = save_sid;;
2732#endif
2733 return OK;
2734 }
2735 return FAIL;
2736}
2737
2738#if defined(UNIX) || defined(VMS)
2739/*
2740 * Return TRUE if we are certain the user owns the file "fname".
2741 * Used for ".vimrc" and ".exrc".
2742 * Use both stat() and lstat() for extra security.
2743 */
2744 static int
2745file_owned(fname)
2746 char *fname;
2747{
2748 struct stat s;
2749# ifdef UNIX
2750 uid_t uid = getuid();
2751# else /* VMS */
2752 uid_t uid = ((getgid() << 16) | getuid());
2753# endif
2754
2755 return !(mch_stat(fname, &s) != 0 || s.st_uid != uid
2756# ifdef HAVE_LSTAT
2757 || mch_lstat(fname, &s) != 0 || s.st_uid != uid
2758# endif
2759 );
2760}
2761#endif
2762
2763/*
2764 * Give an error message main_errors["n"] and exit.
2765 */
2766 static void
2767mainerr(n, str)
2768 int n; /* one of the ME_ defines */
2769 char_u *str; /* extra argument or NULL */
2770{
2771#if defined(UNIX) || defined(__EMX__) || defined(VMS)
2772 reset_signals(); /* kill us with CTRL-C here, if you like */
2773#endif
2774
2775 mch_errmsg(longVersion);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002776 mch_errmsg("\n");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002777 mch_errmsg(_(main_errors[n]));
2778 if (str != NULL)
2779 {
2780 mch_errmsg(": \"");
2781 mch_errmsg((char *)str);
2782 mch_errmsg("\"");
2783 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002784 mch_errmsg(_("\nMore info with: \"vim -h\"\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002785
2786 mch_exit(1);
2787}
2788
2789 void
2790mainerr_arg_missing(str)
2791 char_u *str;
2792{
2793 mainerr(ME_ARG_MISSING, str);
2794}
2795
2796/*
2797 * print a message with three spaces prepended and '\n' appended.
2798 */
2799 static void
2800main_msg(s)
2801 char *s;
2802{
2803 mch_msg(" ");
2804 mch_msg(s);
2805 mch_msg("\n");
2806}
2807
2808/*
2809 * Print messages for "vim -h" or "vim --help" and exit.
2810 */
2811 static void
2812usage()
2813{
2814 int i;
2815 static char *(use[]) =
2816 {
2817 N_("[file ..] edit specified file(s)"),
2818 N_("- read text from stdin"),
2819 N_("-t tag edit file where tag is defined"),
2820#ifdef FEAT_QUICKFIX
2821 N_("-q [errorfile] edit file with first error")
2822#endif
2823 };
2824
2825#if defined(UNIX) || defined(__EMX__) || defined(VMS)
2826 reset_signals(); /* kill us with CTRL-C here, if you like */
2827#endif
2828
2829 mch_msg(longVersion);
2830 mch_msg(_("\n\nusage:"));
2831 for (i = 0; ; ++i)
2832 {
2833 mch_msg(_(" vim [arguments] "));
2834 mch_msg(_(use[i]));
2835 if (i == (sizeof(use) / sizeof(char_u *)) - 1)
2836 break;
2837 mch_msg(_("\n or:"));
2838 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002839#ifdef VMS
2840 mch_msg(_("where case is ignored prepend / to make flag upper case"));
2841#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002842
2843 mch_msg(_("\n\nArguments:\n"));
2844 main_msg(_("--\t\t\tOnly file names after this"));
2845#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
2846 main_msg(_("--literal\t\tDon't expand wildcards"));
2847#endif
2848#ifdef FEAT_OLE
2849 main_msg(_("-register\t\tRegister this gvim for OLE"));
2850 main_msg(_("-unregister\t\tUnregister gvim for OLE"));
2851#endif
2852#ifdef FEAT_GUI
2853 main_msg(_("-g\t\t\tRun using GUI (like \"gvim\")"));
2854 main_msg(_("-f or --nofork\tForeground: Don't fork when starting GUI"));
2855#endif
2856 main_msg(_("-v\t\t\tVi mode (like \"vi\")"));
2857 main_msg(_("-e\t\t\tEx mode (like \"ex\")"));
2858 main_msg(_("-s\t\t\tSilent (batch) mode (only for \"ex\")"));
2859#ifdef FEAT_DIFF
2860 main_msg(_("-d\t\t\tDiff mode (like \"vimdiff\")"));
2861#endif
2862 main_msg(_("-y\t\t\tEasy mode (like \"evim\", modeless)"));
2863 main_msg(_("-R\t\t\tReadonly mode (like \"view\")"));
2864 main_msg(_("-Z\t\t\tRestricted mode (like \"rvim\")"));
2865 main_msg(_("-m\t\t\tModifications (writing files) not allowed"));
2866 main_msg(_("-M\t\t\tModifications in text not allowed"));
2867 main_msg(_("-b\t\t\tBinary mode"));
2868#ifdef FEAT_LISP
2869 main_msg(_("-l\t\t\tLisp mode"));
2870#endif
2871 main_msg(_("-C\t\t\tCompatible with Vi: 'compatible'"));
2872 main_msg(_("-N\t\t\tNot fully Vi compatible: 'nocompatible'"));
2873 main_msg(_("-V[N]\t\tVerbose level"));
2874 main_msg(_("-D\t\t\tDebugging mode"));
2875 main_msg(_("-n\t\t\tNo swap file, use memory only"));
2876 main_msg(_("-r\t\t\tList swap files and exit"));
2877 main_msg(_("-r (with file name)\tRecover crashed session"));
2878 main_msg(_("-L\t\t\tSame as -r"));
2879#ifdef AMIGA
2880 main_msg(_("-f\t\t\tDon't use newcli to open window"));
2881 main_msg(_("-dev <device>\t\tUse <device> for I/O"));
2882#endif
2883#ifdef FEAT_ARABIC
2884 main_msg(_("-A\t\t\tstart in Arabic mode"));
2885#endif
2886#ifdef FEAT_RIGHTLEFT
2887 main_msg(_("-H\t\t\tStart in Hebrew mode"));
2888#endif
2889#ifdef FEAT_FKMAP
2890 main_msg(_("-F\t\t\tStart in Farsi mode"));
2891#endif
2892 main_msg(_("-T <terminal>\tSet terminal type to <terminal>"));
2893 main_msg(_("-u <vimrc>\t\tUse <vimrc> instead of any .vimrc"));
2894#ifdef FEAT_GUI
2895 main_msg(_("-U <gvimrc>\t\tUse <gvimrc> instead of any .gvimrc"));
2896#endif
2897 main_msg(_("--noplugin\t\tDon't load plugin scripts"));
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002898 main_msg(_("-p[N]\t\tOpen N tab pages (default: one for each file)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002899 main_msg(_("-o[N]\t\tOpen N windows (default: one for each file)"));
2900 main_msg(_("-O[N]\t\tLike -o but split vertically"));
2901 main_msg(_("+\t\t\tStart at end of file"));
2902 main_msg(_("+<lnum>\t\tStart at line <lnum>"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002903 main_msg(_("--cmd <command>\tExecute <command> before loading any vimrc file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002904 main_msg(_("-c <command>\t\tExecute <command> after loading the first file"));
2905 main_msg(_("-S <session>\t\tSource file <session> after loading the first file"));
2906 main_msg(_("-s <scriptin>\tRead Normal mode commands from file <scriptin>"));
2907 main_msg(_("-w <scriptout>\tAppend all typed commands to file <scriptout>"));
2908 main_msg(_("-W <scriptout>\tWrite all typed commands to file <scriptout>"));
2909#ifdef FEAT_CRYPT
2910 main_msg(_("-x\t\t\tEdit encrypted files"));
2911#endif
2912#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
2913# if defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)
2914 main_msg(_("-display <display>\tConnect vim to this particular X-server"));
2915# endif
2916 main_msg(_("-X\t\t\tDo not connect to X server"));
2917#endif
2918#ifdef FEAT_CLIENTSERVER
2919 main_msg(_("--remote <files>\tEdit <files> in a Vim server if possible"));
2920 main_msg(_("--remote-silent <files> Same, don't complain if there is no server"));
2921 main_msg(_("--remote-wait <files> As --remote but wait for files to have been edited"));
2922 main_msg(_("--remote-wait-silent <files> Same, don't complain if there is no server"));
2923 main_msg(_("--remote-send <keys>\tSend <keys> to a Vim server and exit"));
2924 main_msg(_("--remote-expr <expr>\tEvaluate <expr> in a Vim server and print result"));
2925 main_msg(_("--serverlist\t\tList available Vim server names and exit"));
2926 main_msg(_("--servername <name>\tSend to/become the Vim server <name>"));
2927#endif
2928#ifdef FEAT_VIMINFO
2929 main_msg(_("-i <viminfo>\t\tUse <viminfo> instead of .viminfo"));
2930#endif
2931 main_msg(_("-h or --help\tPrint Help (this message) and exit"));
2932 main_msg(_("--version\t\tPrint version information and exit"));
2933
2934#ifdef FEAT_GUI_X11
2935# ifdef FEAT_GUI_MOTIF
2936 mch_msg(_("\nArguments recognised by gvim (Motif version):\n"));
2937# else
2938# ifdef FEAT_GUI_ATHENA
2939# ifdef FEAT_GUI_NEXTAW
2940 mch_msg(_("\nArguments recognised by gvim (neXtaw version):\n"));
2941# else
2942 mch_msg(_("\nArguments recognised by gvim (Athena version):\n"));
2943# endif
2944# endif
2945# endif
2946 main_msg(_("-display <display>\tRun vim on <display>"));
2947 main_msg(_("-iconic\t\tStart vim iconified"));
2948# if 0
2949 main_msg(_("-name <name>\t\tUse resource as if vim was <name>"));
2950 mch_msg(_("\t\t\t (Unimplemented)\n"));
2951# endif
2952 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
2953 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
2954 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
2955 main_msg(_("-boldfont <font>\tUse <font> for bold text"));
2956 main_msg(_("-italicfont <font>\tUse <font> for italic text"));
2957 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
2958 main_msg(_("-borderwidth <width>\tUse a border width of <width> (also: -bw)"));
2959 main_msg(_("-scrollbarwidth <width> Use a scrollbar width of <width> (also: -sw)"));
2960# ifdef FEAT_GUI_ATHENA
2961 main_msg(_("-menuheight <height>\tUse a menu bar height of <height> (also: -mh)"));
2962# endif
2963 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
2964 main_msg(_("+reverse\t\tDon't use reverse video (also: +rv)"));
2965 main_msg(_("-xrm <resource>\tSet the specified resource"));
2966#endif /* FEAT_GUI_X11 */
2967#if defined(FEAT_GUI) && defined(RISCOS)
2968 mch_msg(_("\nArguments recognised by gvim (RISC OS version):\n"));
2969 main_msg(_("--columns <number>\tInitial width of window in columns"));
2970 main_msg(_("--rows <number>\tInitial height of window in rows"));
2971#endif
2972#ifdef FEAT_GUI_GTK
2973 mch_msg(_("\nArguments recognised by gvim (GTK+ version):\n"));
2974 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
2975 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
2976 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
2977 main_msg(_("-display <display>\tRun vim on <display> (also: --display)"));
2978# ifdef HAVE_GTK2
2979 main_msg(_("--role <role>\tSet a unique role to identify the main window"));
2980# endif
2981 main_msg(_("--socketid <xid>\tOpen Vim inside another GTK widget"));
2982#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002983#ifdef FEAT_GUI_W32
2984 main_msg(_("-P <parent title>\tOpen Vim inside parent application"));
2985#endif
2986
2987#ifdef FEAT_GUI_GNOME
2988 /* Gnome gives extra messages for --help if we continue, but not for -h. */
2989 if (gui.starting)
2990 mch_msg("\n");
2991 else
2992#endif
2993 mch_exit(0);
2994}
2995
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002996#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002997/*
2998 * Check the result of the ATTENTION dialog:
2999 * When "Quit" selected, exit Vim.
3000 * When "Recover" selected, recover the file.
3001 */
3002 static void
3003check_swap_exists_action()
3004{
3005 if (swap_exists_action == SEA_QUIT)
3006 getout(1);
3007 handle_swap_exists(NULL);
3008}
3009#endif
3010
3011#if defined(STARTUPTIME) || defined(PROTO)
3012static void time_diff __ARGS((struct timeval *then, struct timeval *now));
3013
3014static struct timeval prev_timeval;
3015
3016/*
3017 * Save the previous time before doing something that could nest.
3018 * set "*tv_rel" to the time elapsed so far.
3019 */
3020 void
3021time_push(tv_rel, tv_start)
3022 void *tv_rel, *tv_start;
3023{
3024 *((struct timeval *)tv_rel) = prev_timeval;
3025 gettimeofday(&prev_timeval, NULL);
3026 ((struct timeval *)tv_rel)->tv_usec = prev_timeval.tv_usec
3027 - ((struct timeval *)tv_rel)->tv_usec;
3028 ((struct timeval *)tv_rel)->tv_sec = prev_timeval.tv_sec
3029 - ((struct timeval *)tv_rel)->tv_sec;
3030 if (((struct timeval *)tv_rel)->tv_usec < 0)
3031 {
3032 ((struct timeval *)tv_rel)->tv_usec += 1000000;
3033 --((struct timeval *)tv_rel)->tv_sec;
3034 }
3035 *(struct timeval *)tv_start = prev_timeval;
3036}
3037
3038/*
3039 * Compute the previous time after doing something that could nest.
3040 * Subtract "*tp" from prev_timeval;
3041 * Note: The arguments are (void *) to avoid trouble with systems that don't
3042 * have struct timeval.
3043 */
3044 void
3045time_pop(tp)
3046 void *tp; /* actually (struct timeval *) */
3047{
3048 prev_timeval.tv_usec -= ((struct timeval *)tp)->tv_usec;
3049 prev_timeval.tv_sec -= ((struct timeval *)tp)->tv_sec;
3050 if (prev_timeval.tv_usec < 0)
3051 {
3052 prev_timeval.tv_usec += 1000000;
3053 --prev_timeval.tv_sec;
3054 }
3055}
3056
3057 static void
3058time_diff(then, now)
3059 struct timeval *then;
3060 struct timeval *now;
3061{
3062 long usec;
3063 long msec;
3064
3065 usec = now->tv_usec - then->tv_usec;
3066 msec = (now->tv_sec - then->tv_sec) * 1000L + usec / 1000L,
3067 usec = usec % 1000L;
3068 fprintf(time_fd, "%03ld.%03ld", msec, usec >= 0 ? usec : usec + 1000L);
3069}
3070
3071 void
3072time_msg(msg, tv_start)
3073 char *msg;
3074 void *tv_start; /* only for do_source: start time; actually
3075 (struct timeval *) */
3076{
3077 static struct timeval start;
3078 struct timeval now;
3079
3080 if (time_fd != NULL)
3081 {
3082 if (strstr(msg, "STARTING") != NULL)
3083 {
3084 gettimeofday(&start, NULL);
3085 prev_timeval = start;
3086 fprintf(time_fd, "\n\ntimes in msec\n");
3087 fprintf(time_fd, " clock self+sourced self: sourced script\n");
3088 fprintf(time_fd, " clock elapsed: other lines\n\n");
3089 }
3090 gettimeofday(&now, NULL);
3091 time_diff(&start, &now);
3092 if (((struct timeval *)tv_start) != NULL)
3093 {
3094 fprintf(time_fd, " ");
3095 time_diff(((struct timeval *)tv_start), &now);
3096 }
3097 fprintf(time_fd, " ");
3098 time_diff(&prev_timeval, &now);
3099 prev_timeval = now;
3100 fprintf(time_fd, ": %s\n", msg);
3101 }
3102}
3103
3104# ifdef WIN3264
3105/*
3106 * Windows doesn't have gettimeofday(), although it does have struct timeval.
3107 */
3108 int
3109gettimeofday(struct timeval *tv, char *dummy)
3110{
3111 long t = clock();
3112 tv->tv_sec = t / CLOCKS_PER_SEC;
3113 tv->tv_usec = (t - tv->tv_sec * CLOCKS_PER_SEC) * 1000000 / CLOCKS_PER_SEC;
3114 return 0;
3115}
3116# endif
3117
3118#endif
3119
3120#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
3121
3122/*
3123 * Common code for the X command server and the Win32 command server.
3124 */
3125
Bram Moolenaareb94e552006-03-11 21:35:11 +00003126static char_u *build_drop_cmd __ARGS((int filec, char **filev, int tabs, int sendReply));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003127
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003128/*
3129 * Do the client-server stuff, unless "--servername ''" was used.
3130 */
3131 static void
3132exec_on_server(parmp)
3133 mparm_T *parmp;
3134{
3135 if (parmp->serverName_arg == NULL || *parmp->serverName_arg != NUL)
3136 {
3137# ifdef WIN32
3138 /* Initialise the client/server messaging infrastructure. */
3139 serverInitMessaging();
3140# endif
3141
3142 /*
3143 * When a command server argument was found, execute it. This may
3144 * exit Vim when it was successful. Otherwise it's executed further
3145 * on. Remember the encoding used here in "serverStrEnc".
3146 */
3147 if (parmp->serverArg)
3148 {
3149 cmdsrv_main(&parmp->argc, parmp->argv,
3150 parmp->serverName_arg, &parmp->serverStr);
3151# ifdef FEAT_MBYTE
3152 parmp->serverStrEnc = vim_strsave(p_enc);
3153# endif
3154 }
3155
3156 /* If we're still running, get the name to register ourselves.
3157 * On Win32 can register right now, for X11 need to setup the
3158 * clipboard first, it's further down. */
3159 parmp->servername = serverMakeName(parmp->serverName_arg,
3160 parmp->argv[0]);
3161# ifdef WIN32
3162 if (parmp->servername != NULL)
3163 {
3164 serverSetName(parmp->servername);
3165 vim_free(parmp->servername);
3166 }
3167# endif
3168 }
3169}
3170
3171/*
3172 * Prepare for running as a Vim server.
3173 */
3174 static void
3175prepare_server(parmp)
3176 mparm_T *parmp;
3177{
3178# if defined(FEAT_X11)
3179 /*
3180 * Register for remote command execution with :serversend and --remote
3181 * unless there was a -X or a --servername '' on the command line.
3182 * Only register nongui-vim's with an explicit --servername argument.
3183 */
3184 if (X_DISPLAY != NULL && parmp->servername != NULL && (
3185# ifdef FEAT_GUI
3186 gui.in_use ||
3187# endif
3188 parmp->serverName_arg != NULL))
3189 {
3190 (void)serverRegisterName(X_DISPLAY, parmp->servername);
3191 vim_free(parmp->servername);
3192 TIME_MSG("register server name");
3193 }
3194 else
3195 serverDelayedStartName = parmp->servername;
3196# endif
3197
3198 /*
3199 * Execute command ourselves if we're here because the send failed (or
3200 * else we would have exited above).
3201 */
3202 if (parmp->serverStr != NULL)
3203 {
3204 char_u *p;
3205
3206 server_to_input_buf(serverConvert(parmp->serverStrEnc,
3207 parmp->serverStr, &p));
3208 vim_free(p);
3209 }
3210}
3211
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003212 static void
3213cmdsrv_main(argc, argv, serverName_arg, serverStr)
3214 int *argc;
3215 char **argv;
3216 char_u *serverName_arg;
3217 char_u **serverStr;
3218{
3219 char_u *res;
3220 int i;
3221 char_u *sname;
3222 int ret;
3223 int didone = FALSE;
3224 int exiterr = 0;
3225 char **newArgV = argv + 1;
3226 int newArgC = 1,
3227 Argc = *argc;
3228 int argtype;
3229#define ARGTYPE_OTHER 0
3230#define ARGTYPE_EDIT 1
3231#define ARGTYPE_EDIT_WAIT 2
3232#define ARGTYPE_SEND 3
3233 int silent = FALSE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003234 int tabs = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003235# ifndef FEAT_X11
3236 HWND srv;
3237# else
3238 Window srv;
3239
3240 setup_term_clip();
3241# endif
3242
3243 sname = serverMakeName(serverName_arg, argv[0]);
3244 if (sname == NULL)
3245 return;
3246
3247 /*
3248 * Execute the command server related arguments and remove them
3249 * from the argc/argv array; We may have to return into main()
3250 */
3251 for (i = 1; i < Argc; i++)
3252 {
3253 res = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003254 if (STRCMP(argv[i], "--") == 0) /* end of option arguments */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003255 {
3256 for (; i < *argc; i++)
3257 {
3258 *newArgV++ = argv[i];
3259 newArgC++;
3260 }
3261 break;
3262 }
3263
Bram Moolenaareb94e552006-03-11 21:35:11 +00003264 if (STRICMP(argv[i], "--remote-send") == 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003265 argtype = ARGTYPE_SEND;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003266 else if (STRNICMP(argv[i], "--remote", 8) == 0)
3267 {
3268 char *p = argv[i] + 8;
3269
3270 argtype = ARGTYPE_EDIT;
3271 while (*p != NUL)
3272 {
3273 if (STRNICMP(p, "-wait", 5) == 0)
3274 {
3275 argtype = ARGTYPE_EDIT_WAIT;
3276 p += 5;
3277 }
3278 else if (STRNICMP(p, "-silent", 7) == 0)
3279 {
3280 silent = TRUE;
3281 p += 7;
3282 }
3283 else if (STRNICMP(p, "-tab", 4) == 0)
3284 {
3285 tabs = TRUE;
3286 p += 4;
3287 }
3288 else
3289 {
3290 argtype = ARGTYPE_OTHER;
3291 break;
3292 }
3293 }
3294 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003295 else
3296 argtype = ARGTYPE_OTHER;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003297
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003298 if (argtype != ARGTYPE_OTHER)
3299 {
3300 if (i == *argc - 1)
3301 mainerr_arg_missing((char_u *)argv[i]);
3302 if (argtype == ARGTYPE_SEND)
3303 {
3304 *serverStr = (char_u *)argv[i + 1];
3305 i++;
3306 }
3307 else
3308 {
3309 *serverStr = build_drop_cmd(*argc - i - 1, argv + i + 1,
Bram Moolenaareb94e552006-03-11 21:35:11 +00003310 tabs, argtype == ARGTYPE_EDIT_WAIT);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003311 if (*serverStr == NULL)
3312 {
3313 /* Probably out of memory, exit. */
3314 didone = TRUE;
3315 exiterr = 1;
3316 break;
3317 }
3318 Argc = i;
3319 }
3320# ifdef FEAT_X11
3321 if (xterm_dpy == NULL)
3322 {
3323 mch_errmsg(_("No display"));
3324 ret = -1;
3325 }
3326 else
3327 ret = serverSendToVim(xterm_dpy, sname, *serverStr,
3328 NULL, &srv, 0, 0, silent);
3329# else
3330 /* Win32 always works? */
3331 ret = serverSendToVim(sname, *serverStr, NULL, &srv, 0, silent);
3332# endif
3333 if (ret < 0)
3334 {
3335 if (argtype == ARGTYPE_SEND)
3336 {
3337 /* Failed to send, abort. */
3338 mch_errmsg(_(": Send failed.\n"));
3339 didone = TRUE;
3340 exiterr = 1;
3341 }
3342 else if (!silent)
3343 /* Let vim start normally. */
3344 mch_errmsg(_(": Send failed. Trying to execute locally\n"));
3345 break;
3346 }
3347
3348# ifdef FEAT_GUI_W32
3349 /* Guess that when the server name starts with "g" it's a GUI
3350 * server, which we can bring to the foreground here.
3351 * Foreground() in the server doesn't work very well. */
3352 if (argtype != ARGTYPE_SEND && TOUPPER_ASC(*sname) == 'G')
3353 SetForegroundWindow(srv);
3354# endif
3355
3356 /*
3357 * For --remote-wait: Wait until the server did edit each
3358 * file. Also detect that the server no longer runs.
3359 */
3360 if (ret >= 0 && argtype == ARGTYPE_EDIT_WAIT)
3361 {
3362 int numFiles = *argc - i - 1;
3363 int j;
3364 char_u *done = alloc(numFiles);
3365 char_u *p;
3366# ifdef FEAT_GUI_W32
3367 NOTIFYICONDATA ni;
3368 int count = 0;
3369 extern HWND message_window;
3370# endif
3371
3372 if (numFiles > 0 && argv[i + 1][0] == '+')
3373 /* Skip "+cmd" argument, don't wait for it to be edited. */
3374 --numFiles;
3375
3376# ifdef FEAT_GUI_W32
3377 ni.cbSize = sizeof(ni);
3378 ni.hWnd = message_window;
3379 ni.uID = 0;
3380 ni.uFlags = NIF_ICON|NIF_TIP;
3381 ni.hIcon = LoadIcon((HINSTANCE)GetModuleHandle(0), "IDR_VIM");
3382 sprintf(ni.szTip, _("%d of %d edited"), count, numFiles);
3383 Shell_NotifyIcon(NIM_ADD, &ni);
3384# endif
3385
3386 /* Wait for all files to unload in remote */
3387 memset(done, 0, numFiles);
3388 while (memchr(done, 0, numFiles) != NULL)
3389 {
3390# ifdef WIN32
3391 p = serverGetReply(srv, NULL, TRUE, TRUE);
3392 if (p == NULL)
3393 break;
3394# else
3395 if (serverReadReply(xterm_dpy, srv, &p, TRUE) < 0)
3396 break;
3397# endif
3398 j = atoi((char *)p);
3399 if (j >= 0 && j < numFiles)
3400 {
3401# ifdef FEAT_GUI_W32
3402 ++count;
3403 sprintf(ni.szTip, _("%d of %d edited"),
3404 count, numFiles);
3405 Shell_NotifyIcon(NIM_MODIFY, &ni);
3406# endif
3407 done[j] = 1;
3408 }
3409 }
3410# ifdef FEAT_GUI_W32
3411 Shell_NotifyIcon(NIM_DELETE, &ni);
3412# endif
3413 }
3414 }
3415 else if (STRICMP(argv[i], "--remote-expr") == 0)
3416 {
3417 if (i == *argc - 1)
3418 mainerr_arg_missing((char_u *)argv[i]);
3419# ifdef WIN32
3420 /* Win32 always works? */
3421 if (serverSendToVim(sname, (char_u *)argv[i + 1],
3422 &res, NULL, 1, FALSE) < 0)
3423# else
3424 if (xterm_dpy == NULL)
3425 mch_errmsg(_("No display: Send expression failed.\n"));
3426 else if (serverSendToVim(xterm_dpy, sname, (char_u *)argv[i + 1],
3427 &res, NULL, 1, 1, FALSE) < 0)
3428# endif
3429 {
3430 if (res != NULL && *res != NUL)
3431 {
3432 /* Output error from remote */
3433 mch_errmsg((char *)res);
3434 vim_free(res);
3435 res = NULL;
3436 }
3437 mch_errmsg(_(": Send expression failed.\n"));
3438 }
3439 }
3440 else if (STRICMP(argv[i], "--serverlist") == 0)
3441 {
3442# ifdef WIN32
3443 /* Win32 always works? */
3444 res = serverGetVimNames();
3445# else
3446 if (xterm_dpy != NULL)
3447 res = serverGetVimNames(xterm_dpy);
3448# endif
3449 if (called_emsg)
3450 mch_errmsg("\n");
3451 }
3452 else if (STRICMP(argv[i], "--servername") == 0)
3453 {
3454 /* Alredy processed. Take it out of the command line */
3455 i++;
3456 continue;
3457 }
3458 else
3459 {
3460 *newArgV++ = argv[i];
3461 newArgC++;
3462 continue;
3463 }
3464 didone = TRUE;
3465 if (res != NULL && *res != NUL)
3466 {
3467 mch_msg((char *)res);
3468 if (res[STRLEN(res) - 1] != '\n')
3469 mch_msg("\n");
3470 }
3471 vim_free(res);
3472 }
3473
3474 if (didone)
3475 {
3476 display_errors(); /* display any collected messages */
3477 exit(exiterr); /* Mission accomplished - get out */
3478 }
3479
3480 /* Return back into main() */
3481 *argc = newArgC;
3482 vim_free(sname);
3483}
3484
3485/*
3486 * Build a ":drop" command to send to a Vim server.
3487 */
3488 static char_u *
Bram Moolenaareb94e552006-03-11 21:35:11 +00003489build_drop_cmd(filec, filev, tabs, sendReply)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003490 int filec;
3491 char **filev;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003492 int tabs; /* Use ":tab drop" instead of ":drop". */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003493 int sendReply;
3494{
3495 garray_T ga;
3496 int i;
3497 char_u *inicmd = NULL;
3498 char_u *p;
3499 char_u cwd[MAXPATHL];
3500
3501 if (filec > 0 && filev[0][0] == '+')
3502 {
3503 inicmd = (char_u *)filev[0] + 1;
3504 filev++;
3505 filec--;
3506 }
3507 /* Check if we have at least one argument. */
3508 if (filec <= 0)
3509 mainerr_arg_missing((char_u *)filev[-1]);
3510 if (mch_dirname(cwd, MAXPATHL) != OK)
3511 return NULL;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003512 if ((p = vim_strsave_escaped_ext(cwd, PATH_ESC_CHARS, '\\', TRUE)) == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003513 return NULL;
3514 ga_init2(&ga, 1, 100);
3515 ga_concat(&ga, (char_u *)"<C-\\><C-N>:cd ");
3516 ga_concat(&ga, p);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003517 vim_free(p);
Bram Moolenaareb94e552006-03-11 21:35:11 +00003518
3519 /* Call inputsave() so that a prompt for an encryption key works. */
3520 ga_concat(&ga, (char_u *)"<CR>:if exists('*inputsave')|call inputsave()|endif|");
3521 if (tabs)
3522 ga_concat(&ga, (char_u *)"tab ");
3523 ga_concat(&ga, (char_u *)"drop");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003524 for (i = 0; i < filec; i++)
3525 {
3526 /* On Unix the shell has already expanded the wildcards, don't want to
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003527 * do it again in the Vim server. On MS-Windows only escape
3528 * non-wildcard characters. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003529 p = vim_strsave_escaped((char_u *)filev[i],
3530#ifdef UNIX
3531 PATH_ESC_CHARS
3532#else
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003533 (char_u *)" \t%#"
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003534#endif
3535 );
3536 if (p == NULL)
3537 {
3538 vim_free(ga.ga_data);
3539 return NULL;
3540 }
3541 ga_concat(&ga, (char_u *)" ");
3542 ga_concat(&ga, p);
3543 vim_free(p);
3544 }
3545 /* The :drop commands goes to Insert mode when 'insertmode' is set, use
3546 * CTRL-\ CTRL-N again. */
3547 ga_concat(&ga, (char_u *)"|if exists('*inputrestore')|call inputrestore()|endif<CR>");
3548 ga_concat(&ga, (char_u *)"<C-\\><C-N>:cd -");
3549 if (sendReply)
3550 ga_concat(&ga, (char_u *)"<CR>:call SetupRemoteReplies()");
3551 ga_concat(&ga, (char_u *)"<CR>:");
3552 if (inicmd != NULL)
3553 {
3554 /* Can't use <CR> after "inicmd", because an "startinsert" would cause
3555 * the following commands to be inserted as text. Use a "|",
3556 * hopefully "inicmd" does allow this... */
3557 ga_concat(&ga, inicmd);
3558 ga_concat(&ga, (char_u *)"|");
3559 }
3560 /* Bring the window to the foreground, goto Insert mode when 'im' set and
3561 * clear command line. */
Bram Moolenaar567e4de2004-12-31 21:01:02 +00003562 ga_concat(&ga, (char_u *)"cal foreground()|if &im|star|en|redr|f<CR>");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003563 ga_append(&ga, NUL);
3564 return ga.ga_data;
3565}
3566
3567/*
3568 * Replace termcodes such as <CR> and insert as key presses if there is room.
3569 */
3570 void
3571server_to_input_buf(str)
3572 char_u *str;
3573{
3574 char_u *ptr = NULL;
3575 char_u *cpo_save = p_cpo;
3576
3577 /* Set 'cpoptions' the way we want it.
3578 * B set - backslashes are *not* treated specially
3579 * k set - keycodes are *not* reverse-engineered
3580 * < unset - <Key> sequences *are* interpreted
3581 * The last parameter of replace_termcodes() is TRUE so that the <lt>
3582 * sequence is recognised - needed for a real backslash.
3583 */
3584 p_cpo = (char_u *)"Bk";
3585 str = replace_termcodes((char_u *)str, &ptr, FALSE, TRUE);
3586 p_cpo = cpo_save;
3587
3588 if (*ptr != NUL) /* trailing CTRL-V results in nothing */
3589 {
3590 /*
3591 * Add the string to the input stream.
3592 * Can't use add_to_input_buf() here, we now have K_SPECIAL bytes.
3593 *
3594 * First clear typed characters from the typeahead buffer, there could
3595 * be half a mapping there. Then append to the existing string, so
3596 * that multiple commands from a client are concatenated.
3597 */
3598 if (typebuf.tb_maplen < typebuf.tb_len)
3599 del_typebuf(typebuf.tb_len - typebuf.tb_maplen, typebuf.tb_maplen);
3600 (void)ins_typebuf(str, REMAP_NONE, typebuf.tb_len, TRUE, FALSE);
3601
3602 /* Let input_available() know we inserted text in the typeahead
3603 * buffer. */
3604 received_from_client = TRUE;
3605 }
3606 vim_free((char_u *)ptr);
3607}
3608
3609/*
3610 * Evaluate an expression that the client sent to a string.
3611 * Handles disabling error messages and disables debugging, otherwise Vim
3612 * hangs, waiting for "cont" to be typed.
3613 */
3614 char_u *
3615eval_client_expr_to_string(expr)
3616 char_u *expr;
3617{
3618 char_u *res;
3619 int save_dbl = debug_break_level;
3620 int save_ro = redir_off;
3621
3622 debug_break_level = -1;
3623 redir_off = 0;
3624 ++emsg_skip;
3625
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003626 res = eval_to_string(expr, NULL, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003627
3628 debug_break_level = save_dbl;
3629 redir_off = save_ro;
3630 --emsg_skip;
3631
Bram Moolenaar63a121b2005-12-11 21:36:39 +00003632 /* A client can tell us to redraw, but not to display the cursor, so do
3633 * that here. */
3634 setcursor();
3635 out_flush();
3636#ifdef FEAT_GUI
3637 if (gui.in_use)
3638 gui_update_cursor(FALSE, FALSE);
3639#endif
3640
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003641 return res;
3642}
3643
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003644/*
3645 * If conversion is needed, convert "data" from "client_enc" to 'encoding' and
3646 * return an allocated string. Otherwise return "data".
3647 * "*tofree" is set to the result when it needs to be freed later.
3648 */
3649/*ARGSUSED*/
3650 char_u *
3651serverConvert(client_enc, data, tofree)
3652 char_u *client_enc;
3653 char_u *data;
3654 char_u **tofree;
3655{
3656 char_u *res = data;
3657
3658 *tofree = NULL;
3659# ifdef FEAT_MBYTE
3660 if (client_enc != NULL && p_enc != NULL)
3661 {
3662 vimconv_T vimconv;
3663
3664 vimconv.vc_type = CONV_NONE;
3665 if (convert_setup(&vimconv, client_enc, p_enc) != FAIL
3666 && vimconv.vc_type != CONV_NONE)
3667 {
3668 res = string_convert(&vimconv, data, NULL);
3669 if (res == NULL)
3670 res = data;
3671 else
3672 *tofree = res;
3673 }
3674 convert_setup(&vimconv, NULL, NULL);
3675 }
3676# endif
3677 return res;
3678}
3679
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003680
3681/*
3682 * Make our basic server name: use the specified "arg" if given, otherwise use
3683 * the tail of the command "cmd" we were started with.
3684 * Return the name in allocated memory. This doesn't include a serial number.
3685 */
3686 static char_u *
3687serverMakeName(arg, cmd)
3688 char_u *arg;
3689 char *cmd;
3690{
3691 char_u *p;
3692
3693 if (arg != NULL && *arg != NUL)
3694 p = vim_strsave_up(arg);
3695 else
3696 {
3697 p = vim_strsave_up(gettail((char_u *)cmd));
3698 /* Remove .exe or .bat from the name. */
3699 if (p != NULL && vim_strchr(p, '.') != NULL)
3700 *vim_strchr(p, '.') = NUL;
3701 }
3702 return p;
3703}
3704#endif /* FEAT_CLIENTSERVER */
3705
3706/*
3707 * When FEAT_FKMAP is defined, also compile the Farsi source code.
3708 */
3709#if defined(FEAT_FKMAP) || defined(PROTO)
3710# include "farsi.c"
3711#endif
3712
3713/*
3714 * When FEAT_ARABIC is defined, also compile the Arabic source code.
3715 */
3716#if defined(FEAT_ARABIC) || defined(PROTO)
3717# include "arabic.c"
3718#endif