blob: 37d2e94379a7aae83618922fa8ab35167568bf1a [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 }
1464 else if (STRICMP(argv[i], "--serverlist") == 0
1465 || STRICMP(argv[i], "--remote-send") == 0
1466 || STRICMP(argv[i], "--remote-expr") == 0
1467 || STRICMP(argv[i], "--remote") == 0
1468 || STRICMP(argv[i], "--remote-silent") == 0)
1469 parmp->serverArg = TRUE;
1470 else if (STRICMP(argv[i], "--remote-wait") == 0
1471 || STRICMP(argv[i], "--remote-wait-silent") == 0)
1472 {
1473 parmp->serverArg = TRUE;
1474#ifdef FEAT_GUI
1475 /* don't fork() when starting the GUI to edit the files ourself */
1476 gui.dofork = FALSE;
1477#endif
1478 }
1479# endif
1480# ifdef FEAT_GUI_GTK
1481 else if (STRICMP(argv[i], "--socketid") == 0)
1482 {
1483 unsigned int socket_id;
1484 int count;
1485
1486 if (i == argc - 1)
1487 mainerr_arg_missing((char_u *)argv[i]);
1488 if (STRNICMP(argv[i+1], "0x", 2) == 0)
1489 count = sscanf(&(argv[i + 1][2]), "%x", &socket_id);
1490 else
1491 count = sscanf(argv[i+1], "%u", &socket_id);
1492 if (count != 1)
1493 mainerr(ME_INVALID_ARG, (char_u *)argv[i]);
1494 else
1495 gtk_socket_id = socket_id;
1496 i++;
1497 }
1498 else if (STRICMP(argv[i], "--echo-wid") == 0)
1499 echo_wid_arg = TRUE;
1500# endif
1501 }
1502#endif
1503}
1504
1505/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001506 * Scan the command line arguments.
1507 */
1508 static void
1509command_line_scan(parmp)
1510 mparm_T *parmp;
1511{
1512 int argc = parmp->argc;
1513 char **argv = parmp->argv;
1514 int argv_idx; /* index in argv[n][] */
1515 int had_minmin = FALSE; /* found "--" argument */
1516 int want_argument; /* option argument with argument */
1517 int c;
Bram Moolenaar231334e2005-07-25 20:46:57 +00001518 char_u *p = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001519 long n;
1520
1521 --argc;
1522 ++argv;
1523 argv_idx = 1; /* active option letter is argv[0][argv_idx] */
1524 while (argc > 0)
1525 {
1526 /*
1527 * "+" or "+{number}" or "+/{pat}" or "+{command}" argument.
1528 */
1529 if (argv[0][0] == '+' && !had_minmin)
1530 {
1531 if (parmp->n_commands >= MAX_ARG_CMDS)
1532 mainerr(ME_EXTRA_CMD, NULL);
1533 argv_idx = -1; /* skip to next argument */
1534 if (argv[0][1] == NUL)
1535 parmp->commands[parmp->n_commands++] = (char_u *)"$";
1536 else
1537 parmp->commands[parmp->n_commands++] = (char_u *)&(argv[0][1]);
1538 }
1539
1540 /*
1541 * Optional argument.
1542 */
1543 else if (argv[0][0] == '-' && !had_minmin)
1544 {
1545 want_argument = FALSE;
1546 c = argv[0][argv_idx++];
1547#ifdef VMS
1548 /*
1549 * VMS only uses upper case command lines. Interpret "-X" as "-x"
1550 * and "-/X" as "-X".
1551 */
1552 if (c == '/')
1553 {
1554 c = argv[0][argv_idx++];
1555 c = TOUPPER_ASC(c);
1556 }
1557 else
1558 c = TOLOWER_ASC(c);
1559#endif
1560 switch (c)
1561 {
1562 case NUL: /* "vim -" read from stdin */
1563 /* "ex -" silent mode */
1564 if (exmode_active)
1565 silent_mode = TRUE;
1566 else
1567 {
1568 if (parmp->edit_type != EDIT_NONE)
1569 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1570 parmp->edit_type = EDIT_STDIN;
1571 read_cmd_fd = 2; /* read from stderr instead of stdin */
1572 }
1573 argv_idx = -1; /* skip to next argument */
1574 break;
1575
1576 case '-': /* "--" don't take any more option arguments */
1577 /* "--help" give help message */
1578 /* "--version" give version message */
1579 /* "--literal" take files literally */
1580 /* "--nofork" don't fork */
1581 /* "--noplugin[s]" skip plugins */
1582 /* "--cmd <cmd>" execute cmd before vimrc */
1583 if (STRICMP(argv[0] + argv_idx, "help") == 0)
1584 usage();
1585 else if (STRICMP(argv[0] + argv_idx, "version") == 0)
1586 {
1587 Columns = 80; /* need to init Columns */
1588 info_message = TRUE; /* use mch_msg(), not mch_errmsg() */
1589 list_version();
1590 msg_putchar('\n');
1591 msg_didout = FALSE;
1592 mch_exit(0);
1593 }
1594 else if (STRNICMP(argv[0] + argv_idx, "literal", 7) == 0)
1595 {
1596#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
1597 parmp->literal = TRUE;
1598#endif
1599 }
1600 else if (STRNICMP(argv[0] + argv_idx, "nofork", 6) == 0)
1601 {
1602#ifdef FEAT_GUI
1603 gui.dofork = FALSE; /* don't fork() when starting GUI */
1604#endif
1605 }
1606 else if (STRNICMP(argv[0] + argv_idx, "noplugin", 8) == 0)
1607 p_lpl = FALSE;
1608 else if (STRNICMP(argv[0] + argv_idx, "cmd", 3) == 0)
1609 {
1610 want_argument = TRUE;
1611 argv_idx += 3;
1612 }
1613#ifdef FEAT_CLIENTSERVER
1614 else if (STRNICMP(argv[0] + argv_idx, "serverlist", 10) == 0)
1615 ; /* already processed -- no arg */
1616 else if (STRNICMP(argv[0] + argv_idx, "servername", 10) == 0
1617 || STRNICMP(argv[0] + argv_idx, "serversend", 10) == 0)
1618 {
1619 /* already processed -- snatch the following arg */
1620 if (argc > 1)
1621 {
1622 --argc;
1623 ++argv;
1624 }
1625 }
1626#endif
1627#ifdef FEAT_GUI_GTK
1628 else if (STRNICMP(argv[0] + argv_idx, "socketid", 8) == 0)
1629 {
1630 /* already processed -- snatch the following arg */
1631 if (argc > 1)
1632 {
1633 --argc;
1634 ++argv;
1635 }
1636 }
1637 else if (STRNICMP(argv[0] + argv_idx, "echo-wid", 8) == 0)
1638 {
1639 /* already processed, skip */
1640 }
1641#endif
1642 else
1643 {
1644 if (argv[0][argv_idx])
1645 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
1646 had_minmin = TRUE;
1647 }
1648 if (!want_argument)
1649 argv_idx = -1; /* skip to next argument */
1650 break;
1651
1652 case 'A': /* "-A" start in Arabic mode */
1653#ifdef FEAT_ARABIC
1654 set_option_value((char_u *)"arabic", 1L, NULL, 0);
1655#else
1656 mch_errmsg(_(e_noarabic));
1657 mch_exit(2);
1658#endif
1659 break;
1660
1661 case 'b': /* "-b" binary mode */
Bram Moolenaar231334e2005-07-25 20:46:57 +00001662 /* Needs to be effective before expanding file names, because
1663 * for Win32 this makes us edit a shortcut file itself,
1664 * instead of the file it links to. */
1665 set_options_bin(curbuf->b_p_bin, 1, 0);
1666 curbuf->b_p_bin = 1; /* binary file I/O */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001667 break;
1668
1669 case 'C': /* "-C" Compatible */
1670 change_compatible(TRUE);
1671 break;
1672
1673 case 'e': /* "-e" Ex mode */
1674 exmode_active = EXMODE_NORMAL;
1675 break;
1676
1677 case 'E': /* "-E" Improved Ex mode */
1678 exmode_active = EXMODE_VIM;
1679 break;
1680
1681 case 'f': /* "-f" GUI: run in foreground. Amiga: open
1682 window directly, not with newcli */
1683#ifdef FEAT_GUI
1684 gui.dofork = FALSE; /* don't fork() when starting GUI */
1685#endif
1686 break;
1687
1688 case 'g': /* "-g" start GUI */
1689 main_start_gui();
1690 break;
1691
1692 case 'F': /* "-F" start in Farsi mode: rl + fkmap set */
1693#ifdef FEAT_FKMAP
1694 curwin->w_p_rl = p_fkmap = TRUE;
1695#else
1696 mch_errmsg(_(e_nofarsi));
1697 mch_exit(2);
1698#endif
1699 break;
1700
1701 case 'h': /* "-h" give help message */
1702#ifdef FEAT_GUI_GNOME
1703 /* Tell usage() to exit for "gvim". */
1704 gui.starting = FALSE;
1705#endif
1706 usage();
1707 break;
1708
1709 case 'H': /* "-H" start in Hebrew mode: rl + hkmap set */
1710#ifdef FEAT_RIGHTLEFT
1711 curwin->w_p_rl = p_hkmap = TRUE;
1712#else
1713 mch_errmsg(_(e_nohebrew));
1714 mch_exit(2);
1715#endif
1716 break;
1717
1718 case 'l': /* "-l" lisp mode, 'lisp' and 'showmatch' on */
1719#ifdef FEAT_LISP
1720 set_option_value((char_u *)"lisp", 1L, NULL, 0);
1721 p_sm = TRUE;
1722#endif
1723 break;
1724
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001725 case 'M': /* "-M" no changes or writing of files */
1726 reset_modifiable();
1727 /* FALLTHROUGH */
1728
1729 case 'm': /* "-m" no writing of files */
1730 p_write = FALSE;
1731 break;
1732
1733 case 'y': /* "-y" easy mode */
1734#ifdef FEAT_GUI
1735 gui.starting = TRUE; /* start GUI a bit later */
1736#endif
1737 parmp->evim_mode = TRUE;
1738 break;
1739
1740 case 'N': /* "-N" Nocompatible */
1741 change_compatible(FALSE);
1742 break;
1743
1744 case 'n': /* "-n" no swap file */
1745 parmp->no_swap_file = TRUE;
1746 break;
1747
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001748 case 'p': /* "-p[N]" open N tab pages */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00001749#ifdef TARGET_API_MAC_OSX
1750 /* For some reason on MacOS X, an argument like:
1751 -psn_0_10223617 is passed in when invoke from Finder
1752 or with the 'open' command */
1753 if (argv[0][argv_idx] == 's')
1754 {
1755 argv_idx = -1; /* bypass full -psn */
1756 main_start_gui();
1757 break;
1758 }
1759#endif
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001760#ifdef FEAT_WINDOWS
1761 /* default is 0: open window for each file */
1762 parmp->window_count = get_number_arg((char_u *)argv[0],
1763 &argv_idx, 0);
1764 parmp->window_layout = WIN_TABS;
1765#endif
1766 break;
1767
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001768 case 'o': /* "-o[N]" open N horizontal split windows */
1769#ifdef FEAT_WINDOWS
1770 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00001771 parmp->window_count = get_number_arg((char_u *)argv[0],
1772 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001773 parmp->window_layout = WIN_HOR;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001774#endif
1775 break;
1776
1777 case 'O': /* "-O[N]" open N vertical split windows */
1778#if defined(FEAT_VERTSPLIT) && defined(FEAT_WINDOWS)
1779 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00001780 parmp->window_count = get_number_arg((char_u *)argv[0],
1781 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001782 parmp->window_layout = WIN_VER;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001783#endif
1784 break;
1785
1786#ifdef FEAT_QUICKFIX
1787 case 'q': /* "-q" QuickFix mode */
1788 if (parmp->edit_type != EDIT_NONE)
1789 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1790 parmp->edit_type = EDIT_QF;
1791 if (argv[0][argv_idx]) /* "-q{errorfile}" */
1792 {
1793 parmp->use_ef = (char_u *)argv[0] + argv_idx;
1794 argv_idx = -1;
1795 }
1796 else if (argc > 1) /* "-q {errorfile}" */
1797 want_argument = TRUE;
1798 break;
1799#endif
1800
1801 case 'R': /* "-R" readonly mode */
1802 readonlymode = TRUE;
1803 curbuf->b_p_ro = TRUE;
1804 p_uc = 10000; /* don't update very often */
1805 break;
1806
1807 case 'r': /* "-r" recovery mode */
1808 case 'L': /* "-L" recovery mode */
1809 recoverymode = 1;
1810 break;
1811
1812 case 's':
1813 if (exmode_active) /* "-s" silent (batch) mode */
1814 silent_mode = TRUE;
1815 else /* "-s {scriptin}" read from script file */
1816 want_argument = TRUE;
1817 break;
1818
1819 case 't': /* "-t {tag}" or "-t{tag}" jump to tag */
1820 if (parmp->edit_type != EDIT_NONE)
1821 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1822 parmp->edit_type = EDIT_TAG;
1823 if (argv[0][argv_idx]) /* "-t{tag}" */
1824 {
1825 parmp->tagname = (char_u *)argv[0] + argv_idx;
1826 argv_idx = -1;
1827 }
1828 else /* "-t {tag}" */
1829 want_argument = TRUE;
1830 break;
1831
1832#ifdef FEAT_EVAL
1833 case 'D': /* "-D" Debugging */
1834 parmp->use_debug_break_level = 9999;
1835 break;
1836#endif
1837#ifdef FEAT_DIFF
1838 case 'd': /* "-d" 'diff' */
1839# ifdef AMIGA
1840 /* check for "-dev {device}" */
1841 if (argv[0][argv_idx] == 'e' && argv[0][argv_idx + 1] == 'v')
1842 want_argument = TRUE;
1843 else
1844# endif
1845 parmp->diff_mode = TRUE;
1846 break;
1847#endif
1848 case 'V': /* "-V{N}" Verbose level */
1849 /* default is 10: a little bit verbose */
1850 p_verbose = get_number_arg((char_u *)argv[0], &argv_idx, 10);
1851 if (argv[0][argv_idx] != NUL)
1852 {
1853 set_option_value((char_u *)"verbosefile", 0L,
1854 (char_u *)argv[0] + argv_idx, 0);
1855 argv_idx = STRLEN(argv[0]);
1856 }
1857 break;
1858
1859 case 'v': /* "-v" Vi-mode (as if called "vi") */
1860 exmode_active = 0;
1861#ifdef FEAT_GUI
1862 gui.starting = FALSE; /* don't start GUI */
1863#endif
1864 break;
1865
1866 case 'w': /* "-w{number}" set window height */
1867 /* "-w {scriptout}" write to script */
1868 if (vim_isdigit(((char_u *)argv[0])[argv_idx]))
1869 {
1870 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
1871 set_option_value((char_u *)"window", n, NULL, 0);
1872 break;
1873 }
1874 want_argument = TRUE;
1875 break;
1876
1877#ifdef FEAT_CRYPT
1878 case 'x': /* "-x" encrypted reading/writing of files */
1879 parmp->ask_for_key = TRUE;
1880 break;
1881#endif
1882
1883 case 'X': /* "-X" don't connect to X server */
1884#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
1885 x_no_connect = TRUE;
1886#endif
1887 break;
1888
1889 case 'Z': /* "-Z" restricted mode */
1890 restricted = TRUE;
1891 break;
1892
1893 case 'c': /* "-c{command}" or "-c {command}" execute
1894 command */
1895 if (argv[0][argv_idx] != NUL)
1896 {
1897 if (parmp->n_commands >= MAX_ARG_CMDS)
1898 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar231334e2005-07-25 20:46:57 +00001899 parmp->commands[parmp->n_commands++] = (char_u *)argv[0]
1900 + argv_idx;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001901 argv_idx = -1;
1902 break;
1903 }
1904 /*FALLTHROUGH*/
1905 case 'S': /* "-S {file}" execute Vim script */
1906 case 'i': /* "-i {viminfo}" use for viminfo */
1907#ifndef FEAT_DIFF
1908 case 'd': /* "-d {device}" device (for Amiga) */
1909#endif
1910 case 'T': /* "-T {terminal}" terminal name */
1911 case 'u': /* "-u {vimrc}" vim inits file */
1912 case 'U': /* "-U {gvimrc}" gvim inits file */
1913 case 'W': /* "-W {scriptout}" overwrite */
1914#ifdef FEAT_GUI_W32
1915 case 'P': /* "-P {parent title}" MDI parent */
1916#endif
1917 want_argument = TRUE;
1918 break;
1919
1920 default:
1921 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
1922 }
1923
1924 /*
1925 * Handle option arguments with argument.
1926 */
1927 if (want_argument)
1928 {
1929 /*
1930 * Check for garbage immediately after the option letter.
1931 */
1932 if (argv[0][argv_idx] != NUL)
1933 mainerr(ME_GARBAGE, (char_u *)argv[0]);
1934
1935 --argc;
1936 if (argc < 1 && c != 'S')
1937 mainerr_arg_missing((char_u *)argv[0]);
1938 ++argv;
1939 argv_idx = -1;
1940
1941 switch (c)
1942 {
1943 case 'c': /* "-c {command}" execute command */
1944 case 'S': /* "-S {file}" execute Vim script */
1945 if (parmp->n_commands >= MAX_ARG_CMDS)
1946 mainerr(ME_EXTRA_CMD, NULL);
1947 if (c == 'S')
1948 {
1949 char *a;
1950
1951 if (argc < 1)
1952 /* "-S" without argument: use default session file
1953 * name. */
1954 a = SESSION_FILE;
1955 else if (argv[0][0] == '-')
1956 {
1957 /* "-S" followed by another option: use default
1958 * session file name. */
1959 a = SESSION_FILE;
1960 ++argc;
1961 --argv;
1962 }
1963 else
1964 a = argv[0];
1965 p = alloc((unsigned)(STRLEN(a) + 4));
1966 if (p == NULL)
1967 mch_exit(2);
1968 sprintf((char *)p, "so %s", a);
1969 parmp->cmds_tofree[parmp->n_commands] = TRUE;
1970 parmp->commands[parmp->n_commands++] = p;
1971 }
1972 else
Bram Moolenaar231334e2005-07-25 20:46:57 +00001973 parmp->commands[parmp->n_commands++] =
1974 (char_u *)argv[0];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001975 break;
1976
1977 case '-': /* "--cmd {command}" execute command */
1978 if (parmp->n_pre_commands >= MAX_ARG_CMDS)
1979 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar231334e2005-07-25 20:46:57 +00001980 parmp->pre_commands[parmp->n_pre_commands++] =
1981 (char_u *)argv[0];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001982 break;
1983
1984 /* case 'd': -d {device} is handled in mch_check_win() for the
1985 * Amiga */
1986
1987#ifdef FEAT_QUICKFIX
1988 case 'q': /* "-q {errorfile}" QuickFix mode */
1989 parmp->use_ef = (char_u *)argv[0];
1990 break;
1991#endif
1992
1993 case 'i': /* "-i {viminfo}" use for viminfo */
1994 use_viminfo = (char_u *)argv[0];
1995 break;
1996
1997 case 's': /* "-s {scriptin}" read from script file */
1998 if (scriptin[0] != NULL)
1999 {
2000scripterror:
2001 mch_errmsg(_("Attempt to open script file again: \""));
2002 mch_errmsg(argv[-1]);
2003 mch_errmsg(" ");
2004 mch_errmsg(argv[0]);
2005 mch_errmsg("\"\n");
2006 mch_exit(2);
2007 }
2008 if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL)
2009 {
2010 mch_errmsg(_("Cannot open for reading: \""));
2011 mch_errmsg(argv[0]);
2012 mch_errmsg("\"\n");
2013 mch_exit(2);
2014 }
2015 if (save_typebuf() == FAIL)
2016 mch_exit(2); /* out of memory */
2017 break;
2018
2019 case 't': /* "-t {tag}" */
2020 parmp->tagname = (char_u *)argv[0];
2021 break;
2022
2023 case 'T': /* "-T {terminal}" terminal name */
2024 /*
2025 * The -T term argument is always available and when
2026 * HAVE_TERMLIB is supported it overrides the environment
2027 * variable TERM.
2028 */
2029#ifdef FEAT_GUI
2030 if (term_is_gui((char_u *)argv[0]))
2031 gui.starting = TRUE; /* start GUI a bit later */
2032 else
2033#endif
2034 parmp->term = (char_u *)argv[0];
2035 break;
2036
2037 case 'u': /* "-u {vimrc}" vim inits file */
2038 parmp->use_vimrc = (char_u *)argv[0];
2039 break;
2040
2041 case 'U': /* "-U {gvimrc}" gvim inits file */
2042#ifdef FEAT_GUI
2043 use_gvimrc = (char_u *)argv[0];
2044#endif
2045 break;
2046
2047 case 'w': /* "-w {nr}" 'window' value */
2048 /* "-w {scriptout}" append to script file */
2049 if (vim_isdigit(*((char_u *)argv[0])))
2050 {
2051 argv_idx = 0;
2052 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2053 set_option_value((char_u *)"window", n, NULL, 0);
2054 argv_idx = -1;
2055 break;
2056 }
2057 /*FALLTHROUGH*/
2058 case 'W': /* "-W {scriptout}" overwrite script file */
2059 if (scriptout != NULL)
2060 goto scripterror;
2061 if ((scriptout = mch_fopen(argv[0],
2062 c == 'w' ? APPENDBIN : WRITEBIN)) == NULL)
2063 {
2064 mch_errmsg(_("Cannot open for script output: \""));
2065 mch_errmsg(argv[0]);
2066 mch_errmsg("\"\n");
2067 mch_exit(2);
2068 }
2069 break;
2070
2071#ifdef FEAT_GUI_W32
2072 case 'P': /* "-P {parent title}" MDI parent */
2073 gui_mch_set_parent(argv[0]);
2074 break;
2075#endif
2076 }
2077 }
2078 }
2079
2080 /*
2081 * File name argument.
2082 */
2083 else
2084 {
2085 argv_idx = -1; /* skip to next argument */
2086
2087 /* Check for only one type of editing. */
2088 if (parmp->edit_type != EDIT_NONE && parmp->edit_type != EDIT_FILE)
2089 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2090 parmp->edit_type = EDIT_FILE;
2091
2092#ifdef MSWIN
2093 /* Remember if the argument was a full path before changing
2094 * slashes to backslashes. */
2095 if (argv[0][0] != NUL && argv[0][1] == ':' && argv[0][2] == '\\')
2096 parmp->full_path = TRUE;
2097#endif
2098
2099 /* Add the file to the global argument list. */
2100 if (ga_grow(&global_alist.al_ga, 1) == FAIL
2101 || (p = vim_strsave((char_u *)argv[0])) == NULL)
2102 mch_exit(2);
2103#ifdef FEAT_DIFF
2104 if (parmp->diff_mode && mch_isdir(p) && GARGCOUNT > 0
2105 && !mch_isdir(alist_name(&GARGLIST[0])))
2106 {
2107 char_u *r;
2108
2109 r = concat_fnames(p, gettail(alist_name(&GARGLIST[0])), TRUE);
2110 if (r != NULL)
2111 {
2112 vim_free(p);
2113 p = r;
2114 }
2115 }
2116#endif
2117#if defined(__CYGWIN32__) && !defined(WIN32)
2118 /*
2119 * If vim is invoked by non-Cygwin tools, convert away any
2120 * DOS paths, so things like .swp files are created correctly.
2121 * Look for evidence of non-Cygwin paths before we bother.
2122 * This is only for when using the Unix files.
2123 */
2124 if (strpbrk(p, "\\:") != NULL)
2125 {
2126 char posix_path[PATH_MAX];
2127
2128 cygwin_conv_to_posix_path(p, posix_path);
2129 vim_free(p);
2130 p = vim_strsave(posix_path);
2131 if (p == NULL)
2132 mch_exit(2);
2133 }
2134#endif
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002135
2136#ifdef USE_FNAME_CASE
2137 /* Make the case of the file name match the actual file. */
2138 fname_case(p, 0);
2139#endif
2140
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002141 alist_add(&global_alist, p,
2142#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
Bram Moolenaar231334e2005-07-25 20:46:57 +00002143 parmp->literal ? 2 : 0 /* add buffer nr after exp. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002144#else
2145 2 /* add buffer number now and use curbuf */
2146#endif
2147 );
2148
2149#if defined(FEAT_MBYTE) && defined(WIN32)
2150 {
2151 extern void used_file_arg(char *, int, int);
2152
2153 /* Remember this argument has been added to the argument list.
2154 * Needed when 'encoding' is changed. */
2155 used_file_arg(argv[0], parmp->literal, parmp->full_path);
2156 }
2157#endif
2158 }
2159
2160 /*
2161 * If there are no more letters after the current "-", go to next
2162 * argument. argv_idx is set to -1 when the current argument is to be
2163 * skipped.
2164 */
2165 if (argv_idx <= 0 || argv[0][argv_idx] == NUL)
2166 {
2167 --argc;
2168 ++argv;
2169 argv_idx = 1;
2170 }
2171 }
2172}
2173
2174/*
2175 * Print a warning if stdout is not a terminal.
2176 * When starting in Ex mode and commands come from a file, set Silent mode.
2177 */
2178 static void
2179check_tty(parmp)
2180 mparm_T *parmp;
2181{
2182 int input_isatty; /* is active input a terminal? */
2183
2184 input_isatty = mch_input_isatty();
2185 if (exmode_active)
2186 {
2187 if (!input_isatty)
2188 silent_mode = TRUE;
2189 }
2190 else if (parmp->want_full_screen && (!parmp->stdout_isatty || !input_isatty)
2191#ifdef FEAT_GUI
2192 /* don't want the delay when started from the desktop */
2193 && !gui.starting
2194#endif
2195 )
2196 {
2197#ifdef NBDEBUG
2198 /*
2199 * This shouldn't be necessary. But if I run netbeans with the log
2200 * output coming to the console and XOpenDisplay fails, I get vim
2201 * trying to start with input/output to my console tty. This fills my
2202 * input buffer so fast I can't even kill the process in under 2
2203 * minutes (and it beeps continuosly the whole time :-)
2204 */
2205 if (usingNetbeans && (!parmp->stdout_isatty || !input_isatty))
2206 {
2207 mch_errmsg(_("Vim: Error: Failure to start gvim from NetBeans\n"));
2208 exit(1);
2209 }
2210#endif
2211 if (!parmp->stdout_isatty)
2212 mch_errmsg(_("Vim: Warning: Output is not to a terminal\n"));
2213 if (!input_isatty)
2214 mch_errmsg(_("Vim: Warning: Input is not from a terminal\n"));
2215 out_flush();
2216 if (scriptin[0] == NULL)
2217 ui_delay(2000L, TRUE);
2218 TIME_MSG("Warning delay");
2219 }
2220}
2221
2222/*
2223 * Read text from stdin.
2224 */
2225 static void
2226read_stdin()
2227{
2228 int i;
2229
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002230#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002231 /* When getting the ATTENTION prompt here, use a dialog */
2232 swap_exists_action = SEA_DIALOG;
2233#endif
2234 no_wait_return = TRUE;
2235 i = msg_didany;
2236 set_buflisted(TRUE);
2237 (void)open_buffer(TRUE, NULL); /* create memfile and read file */
2238 no_wait_return = FALSE;
2239 msg_didany = i;
2240 TIME_MSG("reading stdin");
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002241#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002242 check_swap_exists_action();
2243#endif
2244#if !(defined(AMIGA) || defined(MACOS))
2245 /*
2246 * Close stdin and dup it from stderr. Required for GPM to work
2247 * properly, and for running external commands.
2248 * Is there any other system that cannot do this?
2249 */
2250 close(0);
2251 dup(2);
2252#endif
2253}
2254
2255/*
2256 * Create the requested number of windows and edit buffers in them.
2257 * Also does recovery if "recoverymode" set.
2258 */
2259/*ARGSUSED*/
2260 static void
2261create_windows(parmp)
2262 mparm_T *parmp;
2263{
2264#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002265 int rewind;
2266 int done = 0;
2267
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002268 /*
2269 * Create the number of windows that was requested.
2270 */
2271 if (parmp->window_count == -1) /* was not set */
2272 parmp->window_count = 1;
2273 if (parmp->window_count == 0)
2274 parmp->window_count = GARGCOUNT;
2275 if (parmp->window_count > 1)
2276 {
2277 /* Don't change the windows if there was a command in .vimrc that
2278 * already split some windows */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002279 if (parmp->window_layout == 0)
2280 parmp->window_layout = WIN_HOR;
2281 if (parmp->window_layout == WIN_TABS)
2282 {
2283 parmp->window_count = make_tabpages(parmp->window_count);
2284 TIME_MSG("making tab pages");
2285 }
2286 else if (firstwin->w_next == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002287 {
2288 parmp->window_count = make_windows(parmp->window_count,
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002289 parmp->window_layout == WIN_VER);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002290 TIME_MSG("making windows");
2291 }
2292 else
2293 parmp->window_count = win_count();
2294 }
2295 else
2296 parmp->window_count = 1;
2297#endif
2298
2299 if (recoverymode) /* do recover */
2300 {
2301 msg_scroll = TRUE; /* scroll message up */
2302 ml_recover();
2303 if (curbuf->b_ml.ml_mfp == NULL) /* failed */
2304 getout(1);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002305 do_modelines(0); /* do modelines */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002306 }
2307 else
2308 {
2309 /*
2310 * Open a buffer for windows that don't have one yet.
2311 * Commands in the .vimrc might have loaded a file or split the window.
2312 * Watch out for autocommands that delete a window.
2313 */
2314#ifdef FEAT_AUTOCMD
2315 /*
2316 * Don't execute Win/Buf Enter/Leave autocommands here
2317 */
2318 ++autocmd_no_enter;
2319 ++autocmd_no_leave;
2320#endif
2321#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002322 rewind = TRUE;
2323 while (done++ < 1000)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002324 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002325 if (rewind)
2326 {
2327 if (parmp->window_layout == WIN_TABS)
2328 goto_tabpage(1);
2329 else
2330 curwin = firstwin;
2331 }
2332 else if (parmp->window_layout == WIN_TABS)
2333 {
2334 if (curtab->tp_next == NULL)
2335 break;
2336 goto_tabpage(0);
2337 }
2338 else
2339 {
2340 if (curwin->w_next == NULL)
2341 break;
2342 curwin = curwin->w_next;
2343 }
2344 rewind = FALSE;
2345#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002346 curbuf = curwin->w_buffer;
2347 if (curbuf->b_ml.ml_mfp == NULL)
2348 {
2349#ifdef FEAT_FOLDING
2350 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2351 if (p_fdls >= 0)
2352 curwin->w_p_fdl = p_fdls;
2353#endif
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002354#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002355 /* When getting the ATTENTION prompt here, use a dialog */
2356 swap_exists_action = SEA_DIALOG;
2357#endif
2358 set_buflisted(TRUE);
2359 (void)open_buffer(FALSE, NULL); /* create memfile, read file */
2360
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002361#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002362 check_swap_exists_action();
2363#endif
2364#ifdef FEAT_AUTOCMD
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002365 rewind = TRUE; /* start again */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002366#endif
2367 }
2368#ifdef FEAT_WINDOWS
2369 ui_breakcheck();
2370 if (got_int)
2371 {
2372 (void)vgetc(); /* only break the file loading, not the rest */
2373 break;
2374 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002375 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002376#endif
2377#ifdef FEAT_WINDOWS
2378 if (parmp->window_layout == WIN_TABS)
2379 goto_tabpage(1);
2380 else
2381 curwin = firstwin;
2382 curbuf = curwin->w_buffer;
2383#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002384#ifdef FEAT_AUTOCMD
2385 --autocmd_no_enter;
2386 --autocmd_no_leave;
2387#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002388 }
2389}
2390
2391#ifdef FEAT_WINDOWS
2392 /*
2393 * If opened more than one window, start editing files in the other
2394 * windows. make_windows() has already opened the windows.
2395 */
2396 static void
2397edit_buffers(parmp)
2398 mparm_T *parmp;
2399{
2400 int arg_idx; /* index in argument list */
2401 int i;
2402
2403# ifdef FEAT_AUTOCMD
2404 /*
2405 * Don't execute Win/Buf Enter/Leave autocommands here
2406 */
2407 ++autocmd_no_enter;
2408 ++autocmd_no_leave;
2409# endif
2410 arg_idx = 1;
2411 for (i = 1; i < parmp->window_count; ++i)
2412 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002413 if (parmp->window_layout == WIN_TABS)
2414 {
2415 if (curtab->tp_next == NULL) /* just checking */
2416 break;
2417 goto_tabpage(0);
2418 }
2419 else
2420 {
2421 if (curwin->w_next == NULL) /* just checking */
2422 break;
2423 win_enter(curwin->w_next, FALSE);
2424 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002425
2426 /* Only open the file if there is no file in this window yet (that can
2427 * happen when .vimrc contains ":sall") */
2428 if (curbuf == firstwin->w_buffer || curbuf->b_ffname == NULL)
2429 {
2430 curwin->w_arg_idx = arg_idx;
2431 /* edit file from arg list, if there is one */
2432 (void)do_ecmd(0, arg_idx < GARGCOUNT
2433 ? alist_name(&GARGLIST[arg_idx]) : NULL,
2434 NULL, NULL, ECMD_LASTL, ECMD_HIDE);
2435 if (arg_idx == GARGCOUNT - 1)
2436 arg_had_last = TRUE;
2437 ++arg_idx;
2438 }
2439 ui_breakcheck();
2440 if (got_int)
2441 {
2442 (void)vgetc(); /* only break the file loading, not the rest */
2443 break;
2444 }
2445 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002446
2447 if (parmp->window_layout == WIN_TABS)
2448 goto_tabpage(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002449# ifdef FEAT_AUTOCMD
2450 --autocmd_no_enter;
2451# endif
2452 win_enter(firstwin, FALSE); /* back to first window */
2453# ifdef FEAT_AUTOCMD
2454 --autocmd_no_leave;
2455# endif
2456 TIME_MSG("editing files in windows");
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002457 if (parmp->window_count > 1 && parmp->window_layout != WIN_TABS)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002458 win_equal(curwin, FALSE, 'b'); /* adjust heights */
2459}
2460#endif /* FEAT_WINDOWS */
2461
2462/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002463 * Execute the commands from --cmd arguments "cmds[cnt]".
2464 */
2465 static void
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002466exe_pre_commands(parmp)
2467 mparm_T *parmp;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002468{
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002469 char_u **cmds = parmp->pre_commands;
2470 int cnt = parmp->n_pre_commands;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002471 int i;
2472
2473 if (cnt > 0)
2474 {
2475 curwin->w_cursor.lnum = 0; /* just in case.. */
2476 sourcing_name = (char_u *)_("pre-vimrc command line");
2477# ifdef FEAT_EVAL
2478 current_SID = SID_CMDARG;
2479# endif
2480 for (i = 0; i < cnt; ++i)
2481 do_cmdline_cmd(cmds[i]);
2482 sourcing_name = NULL;
2483# ifdef FEAT_EVAL
2484 current_SID = 0;
2485# endif
2486 TIME_MSG("--cmd commands");
2487 }
2488}
2489
2490/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002491 * Execute "+", "-c" and "-S" arguments.
2492 */
2493 static void
2494exe_commands(parmp)
2495 mparm_T *parmp;
2496{
2497 int i;
2498
2499 /*
2500 * We start commands on line 0, make "vim +/pat file" match a
2501 * pattern on line 1. But don't move the cursor when an autocommand
2502 * with g`" was used.
2503 */
2504 msg_scroll = TRUE;
2505 if (parmp->tagname == NULL && curwin->w_cursor.lnum <= 1)
2506 curwin->w_cursor.lnum = 0;
2507 sourcing_name = (char_u *)"command line";
2508#ifdef FEAT_EVAL
2509 current_SID = SID_CARG;
2510#endif
2511 for (i = 0; i < parmp->n_commands; ++i)
2512 {
2513 do_cmdline_cmd(parmp->commands[i]);
2514 if (parmp->cmds_tofree[i])
2515 vim_free(parmp->commands[i]);
2516 }
2517 sourcing_name = NULL;
2518#ifdef FEAT_EVAL
2519 current_SID = 0;
2520#endif
2521 if (curwin->w_cursor.lnum == 0)
2522 curwin->w_cursor.lnum = 1;
2523
2524 if (!exmode_active)
2525 msg_scroll = FALSE;
2526
2527#ifdef FEAT_QUICKFIX
2528 /* When started with "-q errorfile" jump to first error again. */
2529 if (parmp->edit_type == EDIT_QF)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002530 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002531#endif
2532 TIME_MSG("executing command arguments");
2533}
2534
2535/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002536 * Source startup scripts.
2537 */
2538 static void
2539source_startup_scripts(parmp)
2540 mparm_T *parmp;
2541{
2542 int i;
2543
2544 /*
2545 * For "evim" source evim.vim first of all, so that the user can overrule
2546 * any things he doesn't like.
2547 */
2548 if (parmp->evim_mode)
2549 {
2550 (void)do_source((char_u *)EVIM_FILE, FALSE, FALSE);
2551 TIME_MSG("source evim file");
2552 }
2553
2554 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002555 * If -u argument given, use only the initializations from that file and
Bram Moolenaar58d98232005-07-23 22:25:46 +00002556 * nothing else.
2557 */
2558 if (parmp->use_vimrc != NULL)
2559 {
Bram Moolenaar231334e2005-07-25 20:46:57 +00002560 if (STRCMP(parmp->use_vimrc, "NONE") == 0
2561 || STRCMP(parmp->use_vimrc, "NORC") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002562 {
2563#ifdef FEAT_GUI
2564 if (use_gvimrc == NULL) /* don't load gvimrc either */
2565 use_gvimrc = parmp->use_vimrc;
2566#endif
2567 if (parmp->use_vimrc[2] == 'N')
2568 p_lpl = FALSE; /* don't load plugins either */
2569 }
2570 else
2571 {
2572 if (do_source(parmp->use_vimrc, FALSE, FALSE) != OK)
2573 EMSG2(_("E282: Cannot read from \"%s\""), parmp->use_vimrc);
2574 }
2575 }
2576 else if (!silent_mode)
2577 {
2578#ifdef AMIGA
2579 struct Process *proc = (struct Process *)FindTask(0L);
2580 APTR save_winptr = proc->pr_WindowPtr;
2581
2582 /* Avoid a requester here for a volume that doesn't exist. */
2583 proc->pr_WindowPtr = (APTR)-1L;
2584#endif
2585
2586 /*
2587 * Get system wide defaults, if the file name is defined.
2588 */
2589#ifdef SYS_VIMRC_FILE
2590 (void)do_source((char_u *)SYS_VIMRC_FILE, FALSE, FALSE);
2591#endif
Bram Moolenaar1056d982006-03-09 22:37:52 +00002592#ifdef MACOS_X
2593 (void)do_source((char_u *)"$VIMRUNTIME/macmap.vim", FALSE, FALSE);
2594#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00002595
2596 /*
2597 * Try to read initialization commands from the following places:
2598 * - environment variable VIMINIT
2599 * - user vimrc file (s:.vimrc for Amiga, ~/.vimrc otherwise)
2600 * - second user vimrc file ($VIM/.vimrc for Dos)
2601 * - environment variable EXINIT
2602 * - user exrc file (s:.exrc for Amiga, ~/.exrc otherwise)
2603 * - second user exrc file ($VIM/.exrc for Dos)
2604 * The first that exists is used, the rest is ignored.
2605 */
2606 if (process_env((char_u *)"VIMINIT", TRUE) != OK)
2607 {
2608 if (do_source((char_u *)USR_VIMRC_FILE, TRUE, TRUE) == FAIL
2609#ifdef USR_VIMRC_FILE2
2610 && do_source((char_u *)USR_VIMRC_FILE2, TRUE, TRUE) == FAIL
2611#endif
2612#ifdef USR_VIMRC_FILE3
2613 && do_source((char_u *)USR_VIMRC_FILE3, TRUE, TRUE) == FAIL
2614#endif
2615 && process_env((char_u *)"EXINIT", FALSE) == FAIL
2616 && do_source((char_u *)USR_EXRC_FILE, FALSE, FALSE) == FAIL)
2617 {
2618#ifdef USR_EXRC_FILE2
2619 (void)do_source((char_u *)USR_EXRC_FILE2, FALSE, FALSE);
2620#endif
2621 }
2622 }
2623
2624 /*
2625 * Read initialization commands from ".vimrc" or ".exrc" in current
2626 * directory. This is only done if the 'exrc' option is set.
2627 * Because of security reasons we disallow shell and write commands
2628 * now, except for unix if the file is owned by the user or 'secure'
2629 * option has been reset in environment of global ".exrc" or ".vimrc".
2630 * Only do this if VIMRC_FILE is not the same as USR_VIMRC_FILE or
2631 * SYS_VIMRC_FILE.
2632 */
2633 if (p_exrc)
2634 {
2635#if defined(UNIX) || defined(VMS)
2636 /* If ".vimrc" file is not owned by user, set 'secure' mode. */
2637 if (!file_owned(VIMRC_FILE))
2638#endif
2639 secure = p_secure;
2640
2641 i = FAIL;
2642 if (fullpathcmp((char_u *)USR_VIMRC_FILE,
2643 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
2644#ifdef USR_VIMRC_FILE2
2645 && fullpathcmp((char_u *)USR_VIMRC_FILE2,
2646 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
2647#endif
2648#ifdef USR_VIMRC_FILE3
2649 && fullpathcmp((char_u *)USR_VIMRC_FILE3,
2650 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
2651#endif
2652#ifdef SYS_VIMRC_FILE
2653 && fullpathcmp((char_u *)SYS_VIMRC_FILE,
2654 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
2655#endif
2656 )
2657 i = do_source((char_u *)VIMRC_FILE, TRUE, TRUE);
2658
2659 if (i == FAIL)
2660 {
2661#if defined(UNIX) || defined(VMS)
2662 /* if ".exrc" is not owned by user set 'secure' mode */
2663 if (!file_owned(EXRC_FILE))
2664 secure = p_secure;
2665 else
2666 secure = 0;
2667#endif
2668 if ( fullpathcmp((char_u *)USR_EXRC_FILE,
2669 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
2670#ifdef USR_EXRC_FILE2
2671 && fullpathcmp((char_u *)USR_EXRC_FILE2,
2672 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
2673#endif
2674 )
2675 (void)do_source((char_u *)EXRC_FILE, FALSE, FALSE);
2676 }
2677 }
2678 if (secure == 2)
2679 need_wait_return = TRUE;
2680 secure = 0;
2681#ifdef AMIGA
2682 proc->pr_WindowPtr = save_winptr;
2683#endif
2684 }
2685 TIME_MSG("sourcing vimrc file(s)");
2686}
2687
2688/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002689 * Setup to start using the GUI. Exit with an error when not available.
2690 */
2691 static void
2692main_start_gui()
2693{
2694#ifdef FEAT_GUI
2695 gui.starting = TRUE; /* start GUI a bit later */
2696#else
2697 mch_errmsg(_(e_nogvim));
2698 mch_errmsg("\n");
2699 mch_exit(2);
2700#endif
2701}
2702
2703/*
2704 * Get an evironment variable, and execute it as Ex commands.
2705 * Returns FAIL if the environment variable was not executed, OK otherwise.
2706 */
2707 int
2708process_env(env, is_viminit)
2709 char_u *env;
2710 int is_viminit; /* when TRUE, called for VIMINIT */
2711{
2712 char_u *initstr;
2713 char_u *save_sourcing_name;
2714 linenr_T save_sourcing_lnum;
2715#ifdef FEAT_EVAL
2716 scid_T save_sid;
2717#endif
2718
2719 if ((initstr = mch_getenv(env)) != NULL && *initstr != NUL)
2720 {
2721 if (is_viminit)
2722 vimrc_found();
2723 save_sourcing_name = sourcing_name;
2724 save_sourcing_lnum = sourcing_lnum;
2725 sourcing_name = env;
2726 sourcing_lnum = 0;
2727#ifdef FEAT_EVAL
2728 save_sid = current_SID;
2729 current_SID = SID_ENV;
2730#endif
2731 do_cmdline_cmd(initstr);
2732 sourcing_name = save_sourcing_name;
2733 sourcing_lnum = save_sourcing_lnum;
2734#ifdef FEAT_EVAL
2735 current_SID = save_sid;;
2736#endif
2737 return OK;
2738 }
2739 return FAIL;
2740}
2741
2742#if defined(UNIX) || defined(VMS)
2743/*
2744 * Return TRUE if we are certain the user owns the file "fname".
2745 * Used for ".vimrc" and ".exrc".
2746 * Use both stat() and lstat() for extra security.
2747 */
2748 static int
2749file_owned(fname)
2750 char *fname;
2751{
2752 struct stat s;
2753# ifdef UNIX
2754 uid_t uid = getuid();
2755# else /* VMS */
2756 uid_t uid = ((getgid() << 16) | getuid());
2757# endif
2758
2759 return !(mch_stat(fname, &s) != 0 || s.st_uid != uid
2760# ifdef HAVE_LSTAT
2761 || mch_lstat(fname, &s) != 0 || s.st_uid != uid
2762# endif
2763 );
2764}
2765#endif
2766
2767/*
2768 * Give an error message main_errors["n"] and exit.
2769 */
2770 static void
2771mainerr(n, str)
2772 int n; /* one of the ME_ defines */
2773 char_u *str; /* extra argument or NULL */
2774{
2775#if defined(UNIX) || defined(__EMX__) || defined(VMS)
2776 reset_signals(); /* kill us with CTRL-C here, if you like */
2777#endif
2778
2779 mch_errmsg(longVersion);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002780 mch_errmsg("\n");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002781 mch_errmsg(_(main_errors[n]));
2782 if (str != NULL)
2783 {
2784 mch_errmsg(": \"");
2785 mch_errmsg((char *)str);
2786 mch_errmsg("\"");
2787 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002788 mch_errmsg(_("\nMore info with: \"vim -h\"\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002789
2790 mch_exit(1);
2791}
2792
2793 void
2794mainerr_arg_missing(str)
2795 char_u *str;
2796{
2797 mainerr(ME_ARG_MISSING, str);
2798}
2799
2800/*
2801 * print a message with three spaces prepended and '\n' appended.
2802 */
2803 static void
2804main_msg(s)
2805 char *s;
2806{
2807 mch_msg(" ");
2808 mch_msg(s);
2809 mch_msg("\n");
2810}
2811
2812/*
2813 * Print messages for "vim -h" or "vim --help" and exit.
2814 */
2815 static void
2816usage()
2817{
2818 int i;
2819 static char *(use[]) =
2820 {
2821 N_("[file ..] edit specified file(s)"),
2822 N_("- read text from stdin"),
2823 N_("-t tag edit file where tag is defined"),
2824#ifdef FEAT_QUICKFIX
2825 N_("-q [errorfile] edit file with first error")
2826#endif
2827 };
2828
2829#if defined(UNIX) || defined(__EMX__) || defined(VMS)
2830 reset_signals(); /* kill us with CTRL-C here, if you like */
2831#endif
2832
2833 mch_msg(longVersion);
2834 mch_msg(_("\n\nusage:"));
2835 for (i = 0; ; ++i)
2836 {
2837 mch_msg(_(" vim [arguments] "));
2838 mch_msg(_(use[i]));
2839 if (i == (sizeof(use) / sizeof(char_u *)) - 1)
2840 break;
2841 mch_msg(_("\n or:"));
2842 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002843#ifdef VMS
2844 mch_msg(_("where case is ignored prepend / to make flag upper case"));
2845#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002846
2847 mch_msg(_("\n\nArguments:\n"));
2848 main_msg(_("--\t\t\tOnly file names after this"));
2849#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
2850 main_msg(_("--literal\t\tDon't expand wildcards"));
2851#endif
2852#ifdef FEAT_OLE
2853 main_msg(_("-register\t\tRegister this gvim for OLE"));
2854 main_msg(_("-unregister\t\tUnregister gvim for OLE"));
2855#endif
2856#ifdef FEAT_GUI
2857 main_msg(_("-g\t\t\tRun using GUI (like \"gvim\")"));
2858 main_msg(_("-f or --nofork\tForeground: Don't fork when starting GUI"));
2859#endif
2860 main_msg(_("-v\t\t\tVi mode (like \"vi\")"));
2861 main_msg(_("-e\t\t\tEx mode (like \"ex\")"));
2862 main_msg(_("-s\t\t\tSilent (batch) mode (only for \"ex\")"));
2863#ifdef FEAT_DIFF
2864 main_msg(_("-d\t\t\tDiff mode (like \"vimdiff\")"));
2865#endif
2866 main_msg(_("-y\t\t\tEasy mode (like \"evim\", modeless)"));
2867 main_msg(_("-R\t\t\tReadonly mode (like \"view\")"));
2868 main_msg(_("-Z\t\t\tRestricted mode (like \"rvim\")"));
2869 main_msg(_("-m\t\t\tModifications (writing files) not allowed"));
2870 main_msg(_("-M\t\t\tModifications in text not allowed"));
2871 main_msg(_("-b\t\t\tBinary mode"));
2872#ifdef FEAT_LISP
2873 main_msg(_("-l\t\t\tLisp mode"));
2874#endif
2875 main_msg(_("-C\t\t\tCompatible with Vi: 'compatible'"));
2876 main_msg(_("-N\t\t\tNot fully Vi compatible: 'nocompatible'"));
2877 main_msg(_("-V[N]\t\tVerbose level"));
2878 main_msg(_("-D\t\t\tDebugging mode"));
2879 main_msg(_("-n\t\t\tNo swap file, use memory only"));
2880 main_msg(_("-r\t\t\tList swap files and exit"));
2881 main_msg(_("-r (with file name)\tRecover crashed session"));
2882 main_msg(_("-L\t\t\tSame as -r"));
2883#ifdef AMIGA
2884 main_msg(_("-f\t\t\tDon't use newcli to open window"));
2885 main_msg(_("-dev <device>\t\tUse <device> for I/O"));
2886#endif
2887#ifdef FEAT_ARABIC
2888 main_msg(_("-A\t\t\tstart in Arabic mode"));
2889#endif
2890#ifdef FEAT_RIGHTLEFT
2891 main_msg(_("-H\t\t\tStart in Hebrew mode"));
2892#endif
2893#ifdef FEAT_FKMAP
2894 main_msg(_("-F\t\t\tStart in Farsi mode"));
2895#endif
2896 main_msg(_("-T <terminal>\tSet terminal type to <terminal>"));
2897 main_msg(_("-u <vimrc>\t\tUse <vimrc> instead of any .vimrc"));
2898#ifdef FEAT_GUI
2899 main_msg(_("-U <gvimrc>\t\tUse <gvimrc> instead of any .gvimrc"));
2900#endif
2901 main_msg(_("--noplugin\t\tDon't load plugin scripts"));
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002902 main_msg(_("-p[N]\t\tOpen N tab pages (default: one for each file)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002903 main_msg(_("-o[N]\t\tOpen N windows (default: one for each file)"));
2904 main_msg(_("-O[N]\t\tLike -o but split vertically"));
2905 main_msg(_("+\t\t\tStart at end of file"));
2906 main_msg(_("+<lnum>\t\tStart at line <lnum>"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002907 main_msg(_("--cmd <command>\tExecute <command> before loading any vimrc file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002908 main_msg(_("-c <command>\t\tExecute <command> after loading the first file"));
2909 main_msg(_("-S <session>\t\tSource file <session> after loading the first file"));
2910 main_msg(_("-s <scriptin>\tRead Normal mode commands from file <scriptin>"));
2911 main_msg(_("-w <scriptout>\tAppend all typed commands to file <scriptout>"));
2912 main_msg(_("-W <scriptout>\tWrite all typed commands to file <scriptout>"));
2913#ifdef FEAT_CRYPT
2914 main_msg(_("-x\t\t\tEdit encrypted files"));
2915#endif
2916#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
2917# if defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)
2918 main_msg(_("-display <display>\tConnect vim to this particular X-server"));
2919# endif
2920 main_msg(_("-X\t\t\tDo not connect to X server"));
2921#endif
2922#ifdef FEAT_CLIENTSERVER
2923 main_msg(_("--remote <files>\tEdit <files> in a Vim server if possible"));
2924 main_msg(_("--remote-silent <files> Same, don't complain if there is no server"));
2925 main_msg(_("--remote-wait <files> As --remote but wait for files to have been edited"));
2926 main_msg(_("--remote-wait-silent <files> Same, don't complain if there is no server"));
2927 main_msg(_("--remote-send <keys>\tSend <keys> to a Vim server and exit"));
2928 main_msg(_("--remote-expr <expr>\tEvaluate <expr> in a Vim server and print result"));
2929 main_msg(_("--serverlist\t\tList available Vim server names and exit"));
2930 main_msg(_("--servername <name>\tSend to/become the Vim server <name>"));
2931#endif
2932#ifdef FEAT_VIMINFO
2933 main_msg(_("-i <viminfo>\t\tUse <viminfo> instead of .viminfo"));
2934#endif
2935 main_msg(_("-h or --help\tPrint Help (this message) and exit"));
2936 main_msg(_("--version\t\tPrint version information and exit"));
2937
2938#ifdef FEAT_GUI_X11
2939# ifdef FEAT_GUI_MOTIF
2940 mch_msg(_("\nArguments recognised by gvim (Motif version):\n"));
2941# else
2942# ifdef FEAT_GUI_ATHENA
2943# ifdef FEAT_GUI_NEXTAW
2944 mch_msg(_("\nArguments recognised by gvim (neXtaw version):\n"));
2945# else
2946 mch_msg(_("\nArguments recognised by gvim (Athena version):\n"));
2947# endif
2948# endif
2949# endif
2950 main_msg(_("-display <display>\tRun vim on <display>"));
2951 main_msg(_("-iconic\t\tStart vim iconified"));
2952# if 0
2953 main_msg(_("-name <name>\t\tUse resource as if vim was <name>"));
2954 mch_msg(_("\t\t\t (Unimplemented)\n"));
2955# endif
2956 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
2957 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
2958 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
2959 main_msg(_("-boldfont <font>\tUse <font> for bold text"));
2960 main_msg(_("-italicfont <font>\tUse <font> for italic text"));
2961 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
2962 main_msg(_("-borderwidth <width>\tUse a border width of <width> (also: -bw)"));
2963 main_msg(_("-scrollbarwidth <width> Use a scrollbar width of <width> (also: -sw)"));
2964# ifdef FEAT_GUI_ATHENA
2965 main_msg(_("-menuheight <height>\tUse a menu bar height of <height> (also: -mh)"));
2966# endif
2967 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
2968 main_msg(_("+reverse\t\tDon't use reverse video (also: +rv)"));
2969 main_msg(_("-xrm <resource>\tSet the specified resource"));
2970#endif /* FEAT_GUI_X11 */
2971#if defined(FEAT_GUI) && defined(RISCOS)
2972 mch_msg(_("\nArguments recognised by gvim (RISC OS version):\n"));
2973 main_msg(_("--columns <number>\tInitial width of window in columns"));
2974 main_msg(_("--rows <number>\tInitial height of window in rows"));
2975#endif
2976#ifdef FEAT_GUI_GTK
2977 mch_msg(_("\nArguments recognised by gvim (GTK+ version):\n"));
2978 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
2979 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
2980 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
2981 main_msg(_("-display <display>\tRun vim on <display> (also: --display)"));
2982# ifdef HAVE_GTK2
2983 main_msg(_("--role <role>\tSet a unique role to identify the main window"));
2984# endif
2985 main_msg(_("--socketid <xid>\tOpen Vim inside another GTK widget"));
2986#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002987#ifdef FEAT_GUI_W32
2988 main_msg(_("-P <parent title>\tOpen Vim inside parent application"));
2989#endif
2990
2991#ifdef FEAT_GUI_GNOME
2992 /* Gnome gives extra messages for --help if we continue, but not for -h. */
2993 if (gui.starting)
2994 mch_msg("\n");
2995 else
2996#endif
2997 mch_exit(0);
2998}
2999
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00003000#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003001/*
3002 * Check the result of the ATTENTION dialog:
3003 * When "Quit" selected, exit Vim.
3004 * When "Recover" selected, recover the file.
3005 */
3006 static void
3007check_swap_exists_action()
3008{
3009 if (swap_exists_action == SEA_QUIT)
3010 getout(1);
3011 handle_swap_exists(NULL);
3012}
3013#endif
3014
3015#if defined(STARTUPTIME) || defined(PROTO)
3016static void time_diff __ARGS((struct timeval *then, struct timeval *now));
3017
3018static struct timeval prev_timeval;
3019
3020/*
3021 * Save the previous time before doing something that could nest.
3022 * set "*tv_rel" to the time elapsed so far.
3023 */
3024 void
3025time_push(tv_rel, tv_start)
3026 void *tv_rel, *tv_start;
3027{
3028 *((struct timeval *)tv_rel) = prev_timeval;
3029 gettimeofday(&prev_timeval, NULL);
3030 ((struct timeval *)tv_rel)->tv_usec = prev_timeval.tv_usec
3031 - ((struct timeval *)tv_rel)->tv_usec;
3032 ((struct timeval *)tv_rel)->tv_sec = prev_timeval.tv_sec
3033 - ((struct timeval *)tv_rel)->tv_sec;
3034 if (((struct timeval *)tv_rel)->tv_usec < 0)
3035 {
3036 ((struct timeval *)tv_rel)->tv_usec += 1000000;
3037 --((struct timeval *)tv_rel)->tv_sec;
3038 }
3039 *(struct timeval *)tv_start = prev_timeval;
3040}
3041
3042/*
3043 * Compute the previous time after doing something that could nest.
3044 * Subtract "*tp" from prev_timeval;
3045 * Note: The arguments are (void *) to avoid trouble with systems that don't
3046 * have struct timeval.
3047 */
3048 void
3049time_pop(tp)
3050 void *tp; /* actually (struct timeval *) */
3051{
3052 prev_timeval.tv_usec -= ((struct timeval *)tp)->tv_usec;
3053 prev_timeval.tv_sec -= ((struct timeval *)tp)->tv_sec;
3054 if (prev_timeval.tv_usec < 0)
3055 {
3056 prev_timeval.tv_usec += 1000000;
3057 --prev_timeval.tv_sec;
3058 }
3059}
3060
3061 static void
3062time_diff(then, now)
3063 struct timeval *then;
3064 struct timeval *now;
3065{
3066 long usec;
3067 long msec;
3068
3069 usec = now->tv_usec - then->tv_usec;
3070 msec = (now->tv_sec - then->tv_sec) * 1000L + usec / 1000L,
3071 usec = usec % 1000L;
3072 fprintf(time_fd, "%03ld.%03ld", msec, usec >= 0 ? usec : usec + 1000L);
3073}
3074
3075 void
3076time_msg(msg, tv_start)
3077 char *msg;
3078 void *tv_start; /* only for do_source: start time; actually
3079 (struct timeval *) */
3080{
3081 static struct timeval start;
3082 struct timeval now;
3083
3084 if (time_fd != NULL)
3085 {
3086 if (strstr(msg, "STARTING") != NULL)
3087 {
3088 gettimeofday(&start, NULL);
3089 prev_timeval = start;
3090 fprintf(time_fd, "\n\ntimes in msec\n");
3091 fprintf(time_fd, " clock self+sourced self: sourced script\n");
3092 fprintf(time_fd, " clock elapsed: other lines\n\n");
3093 }
3094 gettimeofday(&now, NULL);
3095 time_diff(&start, &now);
3096 if (((struct timeval *)tv_start) != NULL)
3097 {
3098 fprintf(time_fd, " ");
3099 time_diff(((struct timeval *)tv_start), &now);
3100 }
3101 fprintf(time_fd, " ");
3102 time_diff(&prev_timeval, &now);
3103 prev_timeval = now;
3104 fprintf(time_fd, ": %s\n", msg);
3105 }
3106}
3107
3108# ifdef WIN3264
3109/*
3110 * Windows doesn't have gettimeofday(), although it does have struct timeval.
3111 */
3112 int
3113gettimeofday(struct timeval *tv, char *dummy)
3114{
3115 long t = clock();
3116 tv->tv_sec = t / CLOCKS_PER_SEC;
3117 tv->tv_usec = (t - tv->tv_sec * CLOCKS_PER_SEC) * 1000000 / CLOCKS_PER_SEC;
3118 return 0;
3119}
3120# endif
3121
3122#endif
3123
3124#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
3125
3126/*
3127 * Common code for the X command server and the Win32 command server.
3128 */
3129
3130static char_u *build_drop_cmd __ARGS((int filec, char **filev, int sendReply));
3131
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003132/*
3133 * Do the client-server stuff, unless "--servername ''" was used.
3134 */
3135 static void
3136exec_on_server(parmp)
3137 mparm_T *parmp;
3138{
3139 if (parmp->serverName_arg == NULL || *parmp->serverName_arg != NUL)
3140 {
3141# ifdef WIN32
3142 /* Initialise the client/server messaging infrastructure. */
3143 serverInitMessaging();
3144# endif
3145
3146 /*
3147 * When a command server argument was found, execute it. This may
3148 * exit Vim when it was successful. Otherwise it's executed further
3149 * on. Remember the encoding used here in "serverStrEnc".
3150 */
3151 if (parmp->serverArg)
3152 {
3153 cmdsrv_main(&parmp->argc, parmp->argv,
3154 parmp->serverName_arg, &parmp->serverStr);
3155# ifdef FEAT_MBYTE
3156 parmp->serverStrEnc = vim_strsave(p_enc);
3157# endif
3158 }
3159
3160 /* If we're still running, get the name to register ourselves.
3161 * On Win32 can register right now, for X11 need to setup the
3162 * clipboard first, it's further down. */
3163 parmp->servername = serverMakeName(parmp->serverName_arg,
3164 parmp->argv[0]);
3165# ifdef WIN32
3166 if (parmp->servername != NULL)
3167 {
3168 serverSetName(parmp->servername);
3169 vim_free(parmp->servername);
3170 }
3171# endif
3172 }
3173}
3174
3175/*
3176 * Prepare for running as a Vim server.
3177 */
3178 static void
3179prepare_server(parmp)
3180 mparm_T *parmp;
3181{
3182# if defined(FEAT_X11)
3183 /*
3184 * Register for remote command execution with :serversend and --remote
3185 * unless there was a -X or a --servername '' on the command line.
3186 * Only register nongui-vim's with an explicit --servername argument.
3187 */
3188 if (X_DISPLAY != NULL && parmp->servername != NULL && (
3189# ifdef FEAT_GUI
3190 gui.in_use ||
3191# endif
3192 parmp->serverName_arg != NULL))
3193 {
3194 (void)serverRegisterName(X_DISPLAY, parmp->servername);
3195 vim_free(parmp->servername);
3196 TIME_MSG("register server name");
3197 }
3198 else
3199 serverDelayedStartName = parmp->servername;
3200# endif
3201
3202 /*
3203 * Execute command ourselves if we're here because the send failed (or
3204 * else we would have exited above).
3205 */
3206 if (parmp->serverStr != NULL)
3207 {
3208 char_u *p;
3209
3210 server_to_input_buf(serverConvert(parmp->serverStrEnc,
3211 parmp->serverStr, &p));
3212 vim_free(p);
3213 }
3214}
3215
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003216 static void
3217cmdsrv_main(argc, argv, serverName_arg, serverStr)
3218 int *argc;
3219 char **argv;
3220 char_u *serverName_arg;
3221 char_u **serverStr;
3222{
3223 char_u *res;
3224 int i;
3225 char_u *sname;
3226 int ret;
3227 int didone = FALSE;
3228 int exiterr = 0;
3229 char **newArgV = argv + 1;
3230 int newArgC = 1,
3231 Argc = *argc;
3232 int argtype;
3233#define ARGTYPE_OTHER 0
3234#define ARGTYPE_EDIT 1
3235#define ARGTYPE_EDIT_WAIT 2
3236#define ARGTYPE_SEND 3
3237 int silent = FALSE;
3238# ifndef FEAT_X11
3239 HWND srv;
3240# else
3241 Window srv;
3242
3243 setup_term_clip();
3244# endif
3245
3246 sname = serverMakeName(serverName_arg, argv[0]);
3247 if (sname == NULL)
3248 return;
3249
3250 /*
3251 * Execute the command server related arguments and remove them
3252 * from the argc/argv array; We may have to return into main()
3253 */
3254 for (i = 1; i < Argc; i++)
3255 {
3256 res = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003257 if (STRCMP(argv[i], "--") == 0) /* end of option arguments */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003258 {
3259 for (; i < *argc; i++)
3260 {
3261 *newArgV++ = argv[i];
3262 newArgC++;
3263 }
3264 break;
3265 }
3266
3267 if (STRICMP(argv[i], "--remote") == 0)
3268 argtype = ARGTYPE_EDIT;
3269 else if (STRICMP(argv[i], "--remote-silent") == 0)
3270 {
3271 argtype = ARGTYPE_EDIT;
3272 silent = TRUE;
3273 }
3274 else if (STRICMP(argv[i], "--remote-wait") == 0)
3275 argtype = ARGTYPE_EDIT_WAIT;
3276 else if (STRICMP(argv[i], "--remote-wait-silent") == 0)
3277 {
3278 argtype = ARGTYPE_EDIT_WAIT;
3279 silent = TRUE;
3280 }
3281 else if (STRICMP(argv[i], "--remote-send") == 0)
3282 argtype = ARGTYPE_SEND;
3283 else
3284 argtype = ARGTYPE_OTHER;
3285 if (argtype != ARGTYPE_OTHER)
3286 {
3287 if (i == *argc - 1)
3288 mainerr_arg_missing((char_u *)argv[i]);
3289 if (argtype == ARGTYPE_SEND)
3290 {
3291 *serverStr = (char_u *)argv[i + 1];
3292 i++;
3293 }
3294 else
3295 {
3296 *serverStr = build_drop_cmd(*argc - i - 1, argv + i + 1,
3297 argtype == ARGTYPE_EDIT_WAIT);
3298 if (*serverStr == NULL)
3299 {
3300 /* Probably out of memory, exit. */
3301 didone = TRUE;
3302 exiterr = 1;
3303 break;
3304 }
3305 Argc = i;
3306 }
3307# ifdef FEAT_X11
3308 if (xterm_dpy == NULL)
3309 {
3310 mch_errmsg(_("No display"));
3311 ret = -1;
3312 }
3313 else
3314 ret = serverSendToVim(xterm_dpy, sname, *serverStr,
3315 NULL, &srv, 0, 0, silent);
3316# else
3317 /* Win32 always works? */
3318 ret = serverSendToVim(sname, *serverStr, NULL, &srv, 0, silent);
3319# endif
3320 if (ret < 0)
3321 {
3322 if (argtype == ARGTYPE_SEND)
3323 {
3324 /* Failed to send, abort. */
3325 mch_errmsg(_(": Send failed.\n"));
3326 didone = TRUE;
3327 exiterr = 1;
3328 }
3329 else if (!silent)
3330 /* Let vim start normally. */
3331 mch_errmsg(_(": Send failed. Trying to execute locally\n"));
3332 break;
3333 }
3334
3335# ifdef FEAT_GUI_W32
3336 /* Guess that when the server name starts with "g" it's a GUI
3337 * server, which we can bring to the foreground here.
3338 * Foreground() in the server doesn't work very well. */
3339 if (argtype != ARGTYPE_SEND && TOUPPER_ASC(*sname) == 'G')
3340 SetForegroundWindow(srv);
3341# endif
3342
3343 /*
3344 * For --remote-wait: Wait until the server did edit each
3345 * file. Also detect that the server no longer runs.
3346 */
3347 if (ret >= 0 && argtype == ARGTYPE_EDIT_WAIT)
3348 {
3349 int numFiles = *argc - i - 1;
3350 int j;
3351 char_u *done = alloc(numFiles);
3352 char_u *p;
3353# ifdef FEAT_GUI_W32
3354 NOTIFYICONDATA ni;
3355 int count = 0;
3356 extern HWND message_window;
3357# endif
3358
3359 if (numFiles > 0 && argv[i + 1][0] == '+')
3360 /* Skip "+cmd" argument, don't wait for it to be edited. */
3361 --numFiles;
3362
3363# ifdef FEAT_GUI_W32
3364 ni.cbSize = sizeof(ni);
3365 ni.hWnd = message_window;
3366 ni.uID = 0;
3367 ni.uFlags = NIF_ICON|NIF_TIP;
3368 ni.hIcon = LoadIcon((HINSTANCE)GetModuleHandle(0), "IDR_VIM");
3369 sprintf(ni.szTip, _("%d of %d edited"), count, numFiles);
3370 Shell_NotifyIcon(NIM_ADD, &ni);
3371# endif
3372
3373 /* Wait for all files to unload in remote */
3374 memset(done, 0, numFiles);
3375 while (memchr(done, 0, numFiles) != NULL)
3376 {
3377# ifdef WIN32
3378 p = serverGetReply(srv, NULL, TRUE, TRUE);
3379 if (p == NULL)
3380 break;
3381# else
3382 if (serverReadReply(xterm_dpy, srv, &p, TRUE) < 0)
3383 break;
3384# endif
3385 j = atoi((char *)p);
3386 if (j >= 0 && j < numFiles)
3387 {
3388# ifdef FEAT_GUI_W32
3389 ++count;
3390 sprintf(ni.szTip, _("%d of %d edited"),
3391 count, numFiles);
3392 Shell_NotifyIcon(NIM_MODIFY, &ni);
3393# endif
3394 done[j] = 1;
3395 }
3396 }
3397# ifdef FEAT_GUI_W32
3398 Shell_NotifyIcon(NIM_DELETE, &ni);
3399# endif
3400 }
3401 }
3402 else if (STRICMP(argv[i], "--remote-expr") == 0)
3403 {
3404 if (i == *argc - 1)
3405 mainerr_arg_missing((char_u *)argv[i]);
3406# ifdef WIN32
3407 /* Win32 always works? */
3408 if (serverSendToVim(sname, (char_u *)argv[i + 1],
3409 &res, NULL, 1, FALSE) < 0)
3410# else
3411 if (xterm_dpy == NULL)
3412 mch_errmsg(_("No display: Send expression failed.\n"));
3413 else if (serverSendToVim(xterm_dpy, sname, (char_u *)argv[i + 1],
3414 &res, NULL, 1, 1, FALSE) < 0)
3415# endif
3416 {
3417 if (res != NULL && *res != NUL)
3418 {
3419 /* Output error from remote */
3420 mch_errmsg((char *)res);
3421 vim_free(res);
3422 res = NULL;
3423 }
3424 mch_errmsg(_(": Send expression failed.\n"));
3425 }
3426 }
3427 else if (STRICMP(argv[i], "--serverlist") == 0)
3428 {
3429# ifdef WIN32
3430 /* Win32 always works? */
3431 res = serverGetVimNames();
3432# else
3433 if (xterm_dpy != NULL)
3434 res = serverGetVimNames(xterm_dpy);
3435# endif
3436 if (called_emsg)
3437 mch_errmsg("\n");
3438 }
3439 else if (STRICMP(argv[i], "--servername") == 0)
3440 {
3441 /* Alredy processed. Take it out of the command line */
3442 i++;
3443 continue;
3444 }
3445 else
3446 {
3447 *newArgV++ = argv[i];
3448 newArgC++;
3449 continue;
3450 }
3451 didone = TRUE;
3452 if (res != NULL && *res != NUL)
3453 {
3454 mch_msg((char *)res);
3455 if (res[STRLEN(res) - 1] != '\n')
3456 mch_msg("\n");
3457 }
3458 vim_free(res);
3459 }
3460
3461 if (didone)
3462 {
3463 display_errors(); /* display any collected messages */
3464 exit(exiterr); /* Mission accomplished - get out */
3465 }
3466
3467 /* Return back into main() */
3468 *argc = newArgC;
3469 vim_free(sname);
3470}
3471
3472/*
3473 * Build a ":drop" command to send to a Vim server.
3474 */
3475 static char_u *
3476build_drop_cmd(filec, filev, sendReply)
3477 int filec;
3478 char **filev;
3479 int sendReply;
3480{
3481 garray_T ga;
3482 int i;
3483 char_u *inicmd = NULL;
3484 char_u *p;
3485 char_u cwd[MAXPATHL];
3486
3487 if (filec > 0 && filev[0][0] == '+')
3488 {
3489 inicmd = (char_u *)filev[0] + 1;
3490 filev++;
3491 filec--;
3492 }
3493 /* Check if we have at least one argument. */
3494 if (filec <= 0)
3495 mainerr_arg_missing((char_u *)filev[-1]);
3496 if (mch_dirname(cwd, MAXPATHL) != OK)
3497 return NULL;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003498 if ((p = vim_strsave_escaped_ext(cwd, PATH_ESC_CHARS, '\\', TRUE)) == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003499 return NULL;
3500 ga_init2(&ga, 1, 100);
3501 ga_concat(&ga, (char_u *)"<C-\\><C-N>:cd ");
3502 ga_concat(&ga, p);
3503 /* Call inputsave() so that a prompt for an encryption key works. */
3504 ga_concat(&ga, (char_u *)"<CR>:if exists('*inputsave')|call inputsave()|endif|drop");
3505 vim_free(p);
3506 for (i = 0; i < filec; i++)
3507 {
3508 /* On Unix the shell has already expanded the wildcards, don't want to
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003509 * do it again in the Vim server. On MS-Windows only escape
3510 * non-wildcard characters. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003511 p = vim_strsave_escaped((char_u *)filev[i],
3512#ifdef UNIX
3513 PATH_ESC_CHARS
3514#else
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003515 (char_u *)" \t%#"
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003516#endif
3517 );
3518 if (p == NULL)
3519 {
3520 vim_free(ga.ga_data);
3521 return NULL;
3522 }
3523 ga_concat(&ga, (char_u *)" ");
3524 ga_concat(&ga, p);
3525 vim_free(p);
3526 }
3527 /* The :drop commands goes to Insert mode when 'insertmode' is set, use
3528 * CTRL-\ CTRL-N again. */
3529 ga_concat(&ga, (char_u *)"|if exists('*inputrestore')|call inputrestore()|endif<CR>");
3530 ga_concat(&ga, (char_u *)"<C-\\><C-N>:cd -");
3531 if (sendReply)
3532 ga_concat(&ga, (char_u *)"<CR>:call SetupRemoteReplies()");
3533 ga_concat(&ga, (char_u *)"<CR>:");
3534 if (inicmd != NULL)
3535 {
3536 /* Can't use <CR> after "inicmd", because an "startinsert" would cause
3537 * the following commands to be inserted as text. Use a "|",
3538 * hopefully "inicmd" does allow this... */
3539 ga_concat(&ga, inicmd);
3540 ga_concat(&ga, (char_u *)"|");
3541 }
3542 /* Bring the window to the foreground, goto Insert mode when 'im' set and
3543 * clear command line. */
Bram Moolenaar567e4de2004-12-31 21:01:02 +00003544 ga_concat(&ga, (char_u *)"cal foreground()|if &im|star|en|redr|f<CR>");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003545 ga_append(&ga, NUL);
3546 return ga.ga_data;
3547}
3548
3549/*
3550 * Replace termcodes such as <CR> and insert as key presses if there is room.
3551 */
3552 void
3553server_to_input_buf(str)
3554 char_u *str;
3555{
3556 char_u *ptr = NULL;
3557 char_u *cpo_save = p_cpo;
3558
3559 /* Set 'cpoptions' the way we want it.
3560 * B set - backslashes are *not* treated specially
3561 * k set - keycodes are *not* reverse-engineered
3562 * < unset - <Key> sequences *are* interpreted
3563 * The last parameter of replace_termcodes() is TRUE so that the <lt>
3564 * sequence is recognised - needed for a real backslash.
3565 */
3566 p_cpo = (char_u *)"Bk";
3567 str = replace_termcodes((char_u *)str, &ptr, FALSE, TRUE);
3568 p_cpo = cpo_save;
3569
3570 if (*ptr != NUL) /* trailing CTRL-V results in nothing */
3571 {
3572 /*
3573 * Add the string to the input stream.
3574 * Can't use add_to_input_buf() here, we now have K_SPECIAL bytes.
3575 *
3576 * First clear typed characters from the typeahead buffer, there could
3577 * be half a mapping there. Then append to the existing string, so
3578 * that multiple commands from a client are concatenated.
3579 */
3580 if (typebuf.tb_maplen < typebuf.tb_len)
3581 del_typebuf(typebuf.tb_len - typebuf.tb_maplen, typebuf.tb_maplen);
3582 (void)ins_typebuf(str, REMAP_NONE, typebuf.tb_len, TRUE, FALSE);
3583
3584 /* Let input_available() know we inserted text in the typeahead
3585 * buffer. */
3586 received_from_client = TRUE;
3587 }
3588 vim_free((char_u *)ptr);
3589}
3590
3591/*
3592 * Evaluate an expression that the client sent to a string.
3593 * Handles disabling error messages and disables debugging, otherwise Vim
3594 * hangs, waiting for "cont" to be typed.
3595 */
3596 char_u *
3597eval_client_expr_to_string(expr)
3598 char_u *expr;
3599{
3600 char_u *res;
3601 int save_dbl = debug_break_level;
3602 int save_ro = redir_off;
3603
3604 debug_break_level = -1;
3605 redir_off = 0;
3606 ++emsg_skip;
3607
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003608 res = eval_to_string(expr, NULL, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003609
3610 debug_break_level = save_dbl;
3611 redir_off = save_ro;
3612 --emsg_skip;
3613
Bram Moolenaar63a121b2005-12-11 21:36:39 +00003614 /* A client can tell us to redraw, but not to display the cursor, so do
3615 * that here. */
3616 setcursor();
3617 out_flush();
3618#ifdef FEAT_GUI
3619 if (gui.in_use)
3620 gui_update_cursor(FALSE, FALSE);
3621#endif
3622
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003623 return res;
3624}
3625
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003626/*
3627 * If conversion is needed, convert "data" from "client_enc" to 'encoding' and
3628 * return an allocated string. Otherwise return "data".
3629 * "*tofree" is set to the result when it needs to be freed later.
3630 */
3631/*ARGSUSED*/
3632 char_u *
3633serverConvert(client_enc, data, tofree)
3634 char_u *client_enc;
3635 char_u *data;
3636 char_u **tofree;
3637{
3638 char_u *res = data;
3639
3640 *tofree = NULL;
3641# ifdef FEAT_MBYTE
3642 if (client_enc != NULL && p_enc != NULL)
3643 {
3644 vimconv_T vimconv;
3645
3646 vimconv.vc_type = CONV_NONE;
3647 if (convert_setup(&vimconv, client_enc, p_enc) != FAIL
3648 && vimconv.vc_type != CONV_NONE)
3649 {
3650 res = string_convert(&vimconv, data, NULL);
3651 if (res == NULL)
3652 res = data;
3653 else
3654 *tofree = res;
3655 }
3656 convert_setup(&vimconv, NULL, NULL);
3657 }
3658# endif
3659 return res;
3660}
3661
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003662
3663/*
3664 * Make our basic server name: use the specified "arg" if given, otherwise use
3665 * the tail of the command "cmd" we were started with.
3666 * Return the name in allocated memory. This doesn't include a serial number.
3667 */
3668 static char_u *
3669serverMakeName(arg, cmd)
3670 char_u *arg;
3671 char *cmd;
3672{
3673 char_u *p;
3674
3675 if (arg != NULL && *arg != NUL)
3676 p = vim_strsave_up(arg);
3677 else
3678 {
3679 p = vim_strsave_up(gettail((char_u *)cmd));
3680 /* Remove .exe or .bat from the name. */
3681 if (p != NULL && vim_strchr(p, '.') != NULL)
3682 *vim_strchr(p, '.') = NUL;
3683 }
3684 return p;
3685}
3686#endif /* FEAT_CLIENTSERVER */
3687
3688/*
3689 * When FEAT_FKMAP is defined, also compile the Farsi source code.
3690 */
3691#if defined(FEAT_FKMAP) || defined(PROTO)
3692# include "farsi.c"
3693#endif
3694
3695/*
3696 * When FEAT_ARABIC is defined, also compile the Arabic source code.
3697 */
3698#if defined(FEAT_ARABIC) || defined(PROTO)
3699# include "arabic.c"
3700#endif