blob: 60aebf9ff8a6a207c010170c2789355f52525efe [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
2592
2593 /*
2594 * Try to read initialization commands from the following places:
2595 * - environment variable VIMINIT
2596 * - user vimrc file (s:.vimrc for Amiga, ~/.vimrc otherwise)
2597 * - second user vimrc file ($VIM/.vimrc for Dos)
2598 * - environment variable EXINIT
2599 * - user exrc file (s:.exrc for Amiga, ~/.exrc otherwise)
2600 * - second user exrc file ($VIM/.exrc for Dos)
2601 * The first that exists is used, the rest is ignored.
2602 */
2603 if (process_env((char_u *)"VIMINIT", TRUE) != OK)
2604 {
2605 if (do_source((char_u *)USR_VIMRC_FILE, TRUE, TRUE) == FAIL
2606#ifdef USR_VIMRC_FILE2
2607 && do_source((char_u *)USR_VIMRC_FILE2, TRUE, TRUE) == FAIL
2608#endif
2609#ifdef USR_VIMRC_FILE3
2610 && do_source((char_u *)USR_VIMRC_FILE3, TRUE, TRUE) == FAIL
2611#endif
2612 && process_env((char_u *)"EXINIT", FALSE) == FAIL
2613 && do_source((char_u *)USR_EXRC_FILE, FALSE, FALSE) == FAIL)
2614 {
2615#ifdef USR_EXRC_FILE2
2616 (void)do_source((char_u *)USR_EXRC_FILE2, FALSE, FALSE);
2617#endif
2618 }
2619 }
2620
2621 /*
2622 * Read initialization commands from ".vimrc" or ".exrc" in current
2623 * directory. This is only done if the 'exrc' option is set.
2624 * Because of security reasons we disallow shell and write commands
2625 * now, except for unix if the file is owned by the user or 'secure'
2626 * option has been reset in environment of global ".exrc" or ".vimrc".
2627 * Only do this if VIMRC_FILE is not the same as USR_VIMRC_FILE or
2628 * SYS_VIMRC_FILE.
2629 */
2630 if (p_exrc)
2631 {
2632#if defined(UNIX) || defined(VMS)
2633 /* If ".vimrc" file is not owned by user, set 'secure' mode. */
2634 if (!file_owned(VIMRC_FILE))
2635#endif
2636 secure = p_secure;
2637
2638 i = FAIL;
2639 if (fullpathcmp((char_u *)USR_VIMRC_FILE,
2640 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
2641#ifdef USR_VIMRC_FILE2
2642 && fullpathcmp((char_u *)USR_VIMRC_FILE2,
2643 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
2644#endif
2645#ifdef USR_VIMRC_FILE3
2646 && fullpathcmp((char_u *)USR_VIMRC_FILE3,
2647 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
2648#endif
2649#ifdef SYS_VIMRC_FILE
2650 && fullpathcmp((char_u *)SYS_VIMRC_FILE,
2651 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
2652#endif
2653 )
2654 i = do_source((char_u *)VIMRC_FILE, TRUE, TRUE);
2655
2656 if (i == FAIL)
2657 {
2658#if defined(UNIX) || defined(VMS)
2659 /* if ".exrc" is not owned by user set 'secure' mode */
2660 if (!file_owned(EXRC_FILE))
2661 secure = p_secure;
2662 else
2663 secure = 0;
2664#endif
2665 if ( fullpathcmp((char_u *)USR_EXRC_FILE,
2666 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
2667#ifdef USR_EXRC_FILE2
2668 && fullpathcmp((char_u *)USR_EXRC_FILE2,
2669 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
2670#endif
2671 )
2672 (void)do_source((char_u *)EXRC_FILE, FALSE, FALSE);
2673 }
2674 }
2675 if (secure == 2)
2676 need_wait_return = TRUE;
2677 secure = 0;
2678#ifdef AMIGA
2679 proc->pr_WindowPtr = save_winptr;
2680#endif
2681 }
2682 TIME_MSG("sourcing vimrc file(s)");
2683}
2684
2685/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002686 * Setup to start using the GUI. Exit with an error when not available.
2687 */
2688 static void
2689main_start_gui()
2690{
2691#ifdef FEAT_GUI
2692 gui.starting = TRUE; /* start GUI a bit later */
2693#else
2694 mch_errmsg(_(e_nogvim));
2695 mch_errmsg("\n");
2696 mch_exit(2);
2697#endif
2698}
2699
2700/*
2701 * Get an evironment variable, and execute it as Ex commands.
2702 * Returns FAIL if the environment variable was not executed, OK otherwise.
2703 */
2704 int
2705process_env(env, is_viminit)
2706 char_u *env;
2707 int is_viminit; /* when TRUE, called for VIMINIT */
2708{
2709 char_u *initstr;
2710 char_u *save_sourcing_name;
2711 linenr_T save_sourcing_lnum;
2712#ifdef FEAT_EVAL
2713 scid_T save_sid;
2714#endif
2715
2716 if ((initstr = mch_getenv(env)) != NULL && *initstr != NUL)
2717 {
2718 if (is_viminit)
2719 vimrc_found();
2720 save_sourcing_name = sourcing_name;
2721 save_sourcing_lnum = sourcing_lnum;
2722 sourcing_name = env;
2723 sourcing_lnum = 0;
2724#ifdef FEAT_EVAL
2725 save_sid = current_SID;
2726 current_SID = SID_ENV;
2727#endif
2728 do_cmdline_cmd(initstr);
2729 sourcing_name = save_sourcing_name;
2730 sourcing_lnum = save_sourcing_lnum;
2731#ifdef FEAT_EVAL
2732 current_SID = save_sid;;
2733#endif
2734 return OK;
2735 }
2736 return FAIL;
2737}
2738
2739#if defined(UNIX) || defined(VMS)
2740/*
2741 * Return TRUE if we are certain the user owns the file "fname".
2742 * Used for ".vimrc" and ".exrc".
2743 * Use both stat() and lstat() for extra security.
2744 */
2745 static int
2746file_owned(fname)
2747 char *fname;
2748{
2749 struct stat s;
2750# ifdef UNIX
2751 uid_t uid = getuid();
2752# else /* VMS */
2753 uid_t uid = ((getgid() << 16) | getuid());
2754# endif
2755
2756 return !(mch_stat(fname, &s) != 0 || s.st_uid != uid
2757# ifdef HAVE_LSTAT
2758 || mch_lstat(fname, &s) != 0 || s.st_uid != uid
2759# endif
2760 );
2761}
2762#endif
2763
2764/*
2765 * Give an error message main_errors["n"] and exit.
2766 */
2767 static void
2768mainerr(n, str)
2769 int n; /* one of the ME_ defines */
2770 char_u *str; /* extra argument or NULL */
2771{
2772#if defined(UNIX) || defined(__EMX__) || defined(VMS)
2773 reset_signals(); /* kill us with CTRL-C here, if you like */
2774#endif
2775
2776 mch_errmsg(longVersion);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002777 mch_errmsg("\n");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002778 mch_errmsg(_(main_errors[n]));
2779 if (str != NULL)
2780 {
2781 mch_errmsg(": \"");
2782 mch_errmsg((char *)str);
2783 mch_errmsg("\"");
2784 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002785 mch_errmsg(_("\nMore info with: \"vim -h\"\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002786
2787 mch_exit(1);
2788}
2789
2790 void
2791mainerr_arg_missing(str)
2792 char_u *str;
2793{
2794 mainerr(ME_ARG_MISSING, str);
2795}
2796
2797/*
2798 * print a message with three spaces prepended and '\n' appended.
2799 */
2800 static void
2801main_msg(s)
2802 char *s;
2803{
2804 mch_msg(" ");
2805 mch_msg(s);
2806 mch_msg("\n");
2807}
2808
2809/*
2810 * Print messages for "vim -h" or "vim --help" and exit.
2811 */
2812 static void
2813usage()
2814{
2815 int i;
2816 static char *(use[]) =
2817 {
2818 N_("[file ..] edit specified file(s)"),
2819 N_("- read text from stdin"),
2820 N_("-t tag edit file where tag is defined"),
2821#ifdef FEAT_QUICKFIX
2822 N_("-q [errorfile] edit file with first error")
2823#endif
2824 };
2825
2826#if defined(UNIX) || defined(__EMX__) || defined(VMS)
2827 reset_signals(); /* kill us with CTRL-C here, if you like */
2828#endif
2829
2830 mch_msg(longVersion);
2831 mch_msg(_("\n\nusage:"));
2832 for (i = 0; ; ++i)
2833 {
2834 mch_msg(_(" vim [arguments] "));
2835 mch_msg(_(use[i]));
2836 if (i == (sizeof(use) / sizeof(char_u *)) - 1)
2837 break;
2838 mch_msg(_("\n or:"));
2839 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002840#ifdef VMS
2841 mch_msg(_("where case is ignored prepend / to make flag upper case"));
2842#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002843
2844 mch_msg(_("\n\nArguments:\n"));
2845 main_msg(_("--\t\t\tOnly file names after this"));
2846#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
2847 main_msg(_("--literal\t\tDon't expand wildcards"));
2848#endif
2849#ifdef FEAT_OLE
2850 main_msg(_("-register\t\tRegister this gvim for OLE"));
2851 main_msg(_("-unregister\t\tUnregister gvim for OLE"));
2852#endif
2853#ifdef FEAT_GUI
2854 main_msg(_("-g\t\t\tRun using GUI (like \"gvim\")"));
2855 main_msg(_("-f or --nofork\tForeground: Don't fork when starting GUI"));
2856#endif
2857 main_msg(_("-v\t\t\tVi mode (like \"vi\")"));
2858 main_msg(_("-e\t\t\tEx mode (like \"ex\")"));
2859 main_msg(_("-s\t\t\tSilent (batch) mode (only for \"ex\")"));
2860#ifdef FEAT_DIFF
2861 main_msg(_("-d\t\t\tDiff mode (like \"vimdiff\")"));
2862#endif
2863 main_msg(_("-y\t\t\tEasy mode (like \"evim\", modeless)"));
2864 main_msg(_("-R\t\t\tReadonly mode (like \"view\")"));
2865 main_msg(_("-Z\t\t\tRestricted mode (like \"rvim\")"));
2866 main_msg(_("-m\t\t\tModifications (writing files) not allowed"));
2867 main_msg(_("-M\t\t\tModifications in text not allowed"));
2868 main_msg(_("-b\t\t\tBinary mode"));
2869#ifdef FEAT_LISP
2870 main_msg(_("-l\t\t\tLisp mode"));
2871#endif
2872 main_msg(_("-C\t\t\tCompatible with Vi: 'compatible'"));
2873 main_msg(_("-N\t\t\tNot fully Vi compatible: 'nocompatible'"));
2874 main_msg(_("-V[N]\t\tVerbose level"));
2875 main_msg(_("-D\t\t\tDebugging mode"));
2876 main_msg(_("-n\t\t\tNo swap file, use memory only"));
2877 main_msg(_("-r\t\t\tList swap files and exit"));
2878 main_msg(_("-r (with file name)\tRecover crashed session"));
2879 main_msg(_("-L\t\t\tSame as -r"));
2880#ifdef AMIGA
2881 main_msg(_("-f\t\t\tDon't use newcli to open window"));
2882 main_msg(_("-dev <device>\t\tUse <device> for I/O"));
2883#endif
2884#ifdef FEAT_ARABIC
2885 main_msg(_("-A\t\t\tstart in Arabic mode"));
2886#endif
2887#ifdef FEAT_RIGHTLEFT
2888 main_msg(_("-H\t\t\tStart in Hebrew mode"));
2889#endif
2890#ifdef FEAT_FKMAP
2891 main_msg(_("-F\t\t\tStart in Farsi mode"));
2892#endif
2893 main_msg(_("-T <terminal>\tSet terminal type to <terminal>"));
2894 main_msg(_("-u <vimrc>\t\tUse <vimrc> instead of any .vimrc"));
2895#ifdef FEAT_GUI
2896 main_msg(_("-U <gvimrc>\t\tUse <gvimrc> instead of any .gvimrc"));
2897#endif
2898 main_msg(_("--noplugin\t\tDon't load plugin scripts"));
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002899 main_msg(_("-p[N]\t\tOpen N tab pages (default: one for each file)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002900 main_msg(_("-o[N]\t\tOpen N windows (default: one for each file)"));
2901 main_msg(_("-O[N]\t\tLike -o but split vertically"));
2902 main_msg(_("+\t\t\tStart at end of file"));
2903 main_msg(_("+<lnum>\t\tStart at line <lnum>"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002904 main_msg(_("--cmd <command>\tExecute <command> before loading any vimrc file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002905 main_msg(_("-c <command>\t\tExecute <command> after loading the first file"));
2906 main_msg(_("-S <session>\t\tSource file <session> after loading the first file"));
2907 main_msg(_("-s <scriptin>\tRead Normal mode commands from file <scriptin>"));
2908 main_msg(_("-w <scriptout>\tAppend all typed commands to file <scriptout>"));
2909 main_msg(_("-W <scriptout>\tWrite all typed commands to file <scriptout>"));
2910#ifdef FEAT_CRYPT
2911 main_msg(_("-x\t\t\tEdit encrypted files"));
2912#endif
2913#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
2914# if defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)
2915 main_msg(_("-display <display>\tConnect vim to this particular X-server"));
2916# endif
2917 main_msg(_("-X\t\t\tDo not connect to X server"));
2918#endif
2919#ifdef FEAT_CLIENTSERVER
2920 main_msg(_("--remote <files>\tEdit <files> in a Vim server if possible"));
2921 main_msg(_("--remote-silent <files> Same, don't complain if there is no server"));
2922 main_msg(_("--remote-wait <files> As --remote but wait for files to have been edited"));
2923 main_msg(_("--remote-wait-silent <files> Same, don't complain if there is no server"));
2924 main_msg(_("--remote-send <keys>\tSend <keys> to a Vim server and exit"));
2925 main_msg(_("--remote-expr <expr>\tEvaluate <expr> in a Vim server and print result"));
2926 main_msg(_("--serverlist\t\tList available Vim server names and exit"));
2927 main_msg(_("--servername <name>\tSend to/become the Vim server <name>"));
2928#endif
2929#ifdef FEAT_VIMINFO
2930 main_msg(_("-i <viminfo>\t\tUse <viminfo> instead of .viminfo"));
2931#endif
2932 main_msg(_("-h or --help\tPrint Help (this message) and exit"));
2933 main_msg(_("--version\t\tPrint version information and exit"));
2934
2935#ifdef FEAT_GUI_X11
2936# ifdef FEAT_GUI_MOTIF
2937 mch_msg(_("\nArguments recognised by gvim (Motif version):\n"));
2938# else
2939# ifdef FEAT_GUI_ATHENA
2940# ifdef FEAT_GUI_NEXTAW
2941 mch_msg(_("\nArguments recognised by gvim (neXtaw version):\n"));
2942# else
2943 mch_msg(_("\nArguments recognised by gvim (Athena version):\n"));
2944# endif
2945# endif
2946# endif
2947 main_msg(_("-display <display>\tRun vim on <display>"));
2948 main_msg(_("-iconic\t\tStart vim iconified"));
2949# if 0
2950 main_msg(_("-name <name>\t\tUse resource as if vim was <name>"));
2951 mch_msg(_("\t\t\t (Unimplemented)\n"));
2952# endif
2953 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
2954 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
2955 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
2956 main_msg(_("-boldfont <font>\tUse <font> for bold text"));
2957 main_msg(_("-italicfont <font>\tUse <font> for italic text"));
2958 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
2959 main_msg(_("-borderwidth <width>\tUse a border width of <width> (also: -bw)"));
2960 main_msg(_("-scrollbarwidth <width> Use a scrollbar width of <width> (also: -sw)"));
2961# ifdef FEAT_GUI_ATHENA
2962 main_msg(_("-menuheight <height>\tUse a menu bar height of <height> (also: -mh)"));
2963# endif
2964 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
2965 main_msg(_("+reverse\t\tDon't use reverse video (also: +rv)"));
2966 main_msg(_("-xrm <resource>\tSet the specified resource"));
2967#endif /* FEAT_GUI_X11 */
2968#if defined(FEAT_GUI) && defined(RISCOS)
2969 mch_msg(_("\nArguments recognised by gvim (RISC OS version):\n"));
2970 main_msg(_("--columns <number>\tInitial width of window in columns"));
2971 main_msg(_("--rows <number>\tInitial height of window in rows"));
2972#endif
2973#ifdef FEAT_GUI_GTK
2974 mch_msg(_("\nArguments recognised by gvim (GTK+ version):\n"));
2975 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
2976 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
2977 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
2978 main_msg(_("-display <display>\tRun vim on <display> (also: --display)"));
2979# ifdef HAVE_GTK2
2980 main_msg(_("--role <role>\tSet a unique role to identify the main window"));
2981# endif
2982 main_msg(_("--socketid <xid>\tOpen Vim inside another GTK widget"));
2983#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002984#ifdef FEAT_GUI_W32
2985 main_msg(_("-P <parent title>\tOpen Vim inside parent application"));
2986#endif
2987
2988#ifdef FEAT_GUI_GNOME
2989 /* Gnome gives extra messages for --help if we continue, but not for -h. */
2990 if (gui.starting)
2991 mch_msg("\n");
2992 else
2993#endif
2994 mch_exit(0);
2995}
2996
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002997#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002998/*
2999 * Check the result of the ATTENTION dialog:
3000 * When "Quit" selected, exit Vim.
3001 * When "Recover" selected, recover the file.
3002 */
3003 static void
3004check_swap_exists_action()
3005{
3006 if (swap_exists_action == SEA_QUIT)
3007 getout(1);
3008 handle_swap_exists(NULL);
3009}
3010#endif
3011
3012#if defined(STARTUPTIME) || defined(PROTO)
3013static void time_diff __ARGS((struct timeval *then, struct timeval *now));
3014
3015static struct timeval prev_timeval;
3016
3017/*
3018 * Save the previous time before doing something that could nest.
3019 * set "*tv_rel" to the time elapsed so far.
3020 */
3021 void
3022time_push(tv_rel, tv_start)
3023 void *tv_rel, *tv_start;
3024{
3025 *((struct timeval *)tv_rel) = prev_timeval;
3026 gettimeofday(&prev_timeval, NULL);
3027 ((struct timeval *)tv_rel)->tv_usec = prev_timeval.tv_usec
3028 - ((struct timeval *)tv_rel)->tv_usec;
3029 ((struct timeval *)tv_rel)->tv_sec = prev_timeval.tv_sec
3030 - ((struct timeval *)tv_rel)->tv_sec;
3031 if (((struct timeval *)tv_rel)->tv_usec < 0)
3032 {
3033 ((struct timeval *)tv_rel)->tv_usec += 1000000;
3034 --((struct timeval *)tv_rel)->tv_sec;
3035 }
3036 *(struct timeval *)tv_start = prev_timeval;
3037}
3038
3039/*
3040 * Compute the previous time after doing something that could nest.
3041 * Subtract "*tp" from prev_timeval;
3042 * Note: The arguments are (void *) to avoid trouble with systems that don't
3043 * have struct timeval.
3044 */
3045 void
3046time_pop(tp)
3047 void *tp; /* actually (struct timeval *) */
3048{
3049 prev_timeval.tv_usec -= ((struct timeval *)tp)->tv_usec;
3050 prev_timeval.tv_sec -= ((struct timeval *)tp)->tv_sec;
3051 if (prev_timeval.tv_usec < 0)
3052 {
3053 prev_timeval.tv_usec += 1000000;
3054 --prev_timeval.tv_sec;
3055 }
3056}
3057
3058 static void
3059time_diff(then, now)
3060 struct timeval *then;
3061 struct timeval *now;
3062{
3063 long usec;
3064 long msec;
3065
3066 usec = now->tv_usec - then->tv_usec;
3067 msec = (now->tv_sec - then->tv_sec) * 1000L + usec / 1000L,
3068 usec = usec % 1000L;
3069 fprintf(time_fd, "%03ld.%03ld", msec, usec >= 0 ? usec : usec + 1000L);
3070}
3071
3072 void
3073time_msg(msg, tv_start)
3074 char *msg;
3075 void *tv_start; /* only for do_source: start time; actually
3076 (struct timeval *) */
3077{
3078 static struct timeval start;
3079 struct timeval now;
3080
3081 if (time_fd != NULL)
3082 {
3083 if (strstr(msg, "STARTING") != NULL)
3084 {
3085 gettimeofday(&start, NULL);
3086 prev_timeval = start;
3087 fprintf(time_fd, "\n\ntimes in msec\n");
3088 fprintf(time_fd, " clock self+sourced self: sourced script\n");
3089 fprintf(time_fd, " clock elapsed: other lines\n\n");
3090 }
3091 gettimeofday(&now, NULL);
3092 time_diff(&start, &now);
3093 if (((struct timeval *)tv_start) != NULL)
3094 {
3095 fprintf(time_fd, " ");
3096 time_diff(((struct timeval *)tv_start), &now);
3097 }
3098 fprintf(time_fd, " ");
3099 time_diff(&prev_timeval, &now);
3100 prev_timeval = now;
3101 fprintf(time_fd, ": %s\n", msg);
3102 }
3103}
3104
3105# ifdef WIN3264
3106/*
3107 * Windows doesn't have gettimeofday(), although it does have struct timeval.
3108 */
3109 int
3110gettimeofday(struct timeval *tv, char *dummy)
3111{
3112 long t = clock();
3113 tv->tv_sec = t / CLOCKS_PER_SEC;
3114 tv->tv_usec = (t - tv->tv_sec * CLOCKS_PER_SEC) * 1000000 / CLOCKS_PER_SEC;
3115 return 0;
3116}
3117# endif
3118
3119#endif
3120
3121#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
3122
3123/*
3124 * Common code for the X command server and the Win32 command server.
3125 */
3126
3127static char_u *build_drop_cmd __ARGS((int filec, char **filev, int sendReply));
3128
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003129/*
3130 * Do the client-server stuff, unless "--servername ''" was used.
3131 */
3132 static void
3133exec_on_server(parmp)
3134 mparm_T *parmp;
3135{
3136 if (parmp->serverName_arg == NULL || *parmp->serverName_arg != NUL)
3137 {
3138# ifdef WIN32
3139 /* Initialise the client/server messaging infrastructure. */
3140 serverInitMessaging();
3141# endif
3142
3143 /*
3144 * When a command server argument was found, execute it. This may
3145 * exit Vim when it was successful. Otherwise it's executed further
3146 * on. Remember the encoding used here in "serverStrEnc".
3147 */
3148 if (parmp->serverArg)
3149 {
3150 cmdsrv_main(&parmp->argc, parmp->argv,
3151 parmp->serverName_arg, &parmp->serverStr);
3152# ifdef FEAT_MBYTE
3153 parmp->serverStrEnc = vim_strsave(p_enc);
3154# endif
3155 }
3156
3157 /* If we're still running, get the name to register ourselves.
3158 * On Win32 can register right now, for X11 need to setup the
3159 * clipboard first, it's further down. */
3160 parmp->servername = serverMakeName(parmp->serverName_arg,
3161 parmp->argv[0]);
3162# ifdef WIN32
3163 if (parmp->servername != NULL)
3164 {
3165 serverSetName(parmp->servername);
3166 vim_free(parmp->servername);
3167 }
3168# endif
3169 }
3170}
3171
3172/*
3173 * Prepare for running as a Vim server.
3174 */
3175 static void
3176prepare_server(parmp)
3177 mparm_T *parmp;
3178{
3179# if defined(FEAT_X11)
3180 /*
3181 * Register for remote command execution with :serversend and --remote
3182 * unless there was a -X or a --servername '' on the command line.
3183 * Only register nongui-vim's with an explicit --servername argument.
3184 */
3185 if (X_DISPLAY != NULL && parmp->servername != NULL && (
3186# ifdef FEAT_GUI
3187 gui.in_use ||
3188# endif
3189 parmp->serverName_arg != NULL))
3190 {
3191 (void)serverRegisterName(X_DISPLAY, parmp->servername);
3192 vim_free(parmp->servername);
3193 TIME_MSG("register server name");
3194 }
3195 else
3196 serverDelayedStartName = parmp->servername;
3197# endif
3198
3199 /*
3200 * Execute command ourselves if we're here because the send failed (or
3201 * else we would have exited above).
3202 */
3203 if (parmp->serverStr != NULL)
3204 {
3205 char_u *p;
3206
3207 server_to_input_buf(serverConvert(parmp->serverStrEnc,
3208 parmp->serverStr, &p));
3209 vim_free(p);
3210 }
3211}
3212
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003213 static void
3214cmdsrv_main(argc, argv, serverName_arg, serverStr)
3215 int *argc;
3216 char **argv;
3217 char_u *serverName_arg;
3218 char_u **serverStr;
3219{
3220 char_u *res;
3221 int i;
3222 char_u *sname;
3223 int ret;
3224 int didone = FALSE;
3225 int exiterr = 0;
3226 char **newArgV = argv + 1;
3227 int newArgC = 1,
3228 Argc = *argc;
3229 int argtype;
3230#define ARGTYPE_OTHER 0
3231#define ARGTYPE_EDIT 1
3232#define ARGTYPE_EDIT_WAIT 2
3233#define ARGTYPE_SEND 3
3234 int silent = FALSE;
3235# ifndef FEAT_X11
3236 HWND srv;
3237# else
3238 Window srv;
3239
3240 setup_term_clip();
3241# endif
3242
3243 sname = serverMakeName(serverName_arg, argv[0]);
3244 if (sname == NULL)
3245 return;
3246
3247 /*
3248 * Execute the command server related arguments and remove them
3249 * from the argc/argv array; We may have to return into main()
3250 */
3251 for (i = 1; i < Argc; i++)
3252 {
3253 res = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003254 if (STRCMP(argv[i], "--") == 0) /* end of option arguments */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003255 {
3256 for (; i < *argc; i++)
3257 {
3258 *newArgV++ = argv[i];
3259 newArgC++;
3260 }
3261 break;
3262 }
3263
3264 if (STRICMP(argv[i], "--remote") == 0)
3265 argtype = ARGTYPE_EDIT;
3266 else if (STRICMP(argv[i], "--remote-silent") == 0)
3267 {
3268 argtype = ARGTYPE_EDIT;
3269 silent = TRUE;
3270 }
3271 else if (STRICMP(argv[i], "--remote-wait") == 0)
3272 argtype = ARGTYPE_EDIT_WAIT;
3273 else if (STRICMP(argv[i], "--remote-wait-silent") == 0)
3274 {
3275 argtype = ARGTYPE_EDIT_WAIT;
3276 silent = TRUE;
3277 }
3278 else if (STRICMP(argv[i], "--remote-send") == 0)
3279 argtype = ARGTYPE_SEND;
3280 else
3281 argtype = ARGTYPE_OTHER;
3282 if (argtype != ARGTYPE_OTHER)
3283 {
3284 if (i == *argc - 1)
3285 mainerr_arg_missing((char_u *)argv[i]);
3286 if (argtype == ARGTYPE_SEND)
3287 {
3288 *serverStr = (char_u *)argv[i + 1];
3289 i++;
3290 }
3291 else
3292 {
3293 *serverStr = build_drop_cmd(*argc - i - 1, argv + i + 1,
3294 argtype == ARGTYPE_EDIT_WAIT);
3295 if (*serverStr == NULL)
3296 {
3297 /* Probably out of memory, exit. */
3298 didone = TRUE;
3299 exiterr = 1;
3300 break;
3301 }
3302 Argc = i;
3303 }
3304# ifdef FEAT_X11
3305 if (xterm_dpy == NULL)
3306 {
3307 mch_errmsg(_("No display"));
3308 ret = -1;
3309 }
3310 else
3311 ret = serverSendToVim(xterm_dpy, sname, *serverStr,
3312 NULL, &srv, 0, 0, silent);
3313# else
3314 /* Win32 always works? */
3315 ret = serverSendToVim(sname, *serverStr, NULL, &srv, 0, silent);
3316# endif
3317 if (ret < 0)
3318 {
3319 if (argtype == ARGTYPE_SEND)
3320 {
3321 /* Failed to send, abort. */
3322 mch_errmsg(_(": Send failed.\n"));
3323 didone = TRUE;
3324 exiterr = 1;
3325 }
3326 else if (!silent)
3327 /* Let vim start normally. */
3328 mch_errmsg(_(": Send failed. Trying to execute locally\n"));
3329 break;
3330 }
3331
3332# ifdef FEAT_GUI_W32
3333 /* Guess that when the server name starts with "g" it's a GUI
3334 * server, which we can bring to the foreground here.
3335 * Foreground() in the server doesn't work very well. */
3336 if (argtype != ARGTYPE_SEND && TOUPPER_ASC(*sname) == 'G')
3337 SetForegroundWindow(srv);
3338# endif
3339
3340 /*
3341 * For --remote-wait: Wait until the server did edit each
3342 * file. Also detect that the server no longer runs.
3343 */
3344 if (ret >= 0 && argtype == ARGTYPE_EDIT_WAIT)
3345 {
3346 int numFiles = *argc - i - 1;
3347 int j;
3348 char_u *done = alloc(numFiles);
3349 char_u *p;
3350# ifdef FEAT_GUI_W32
3351 NOTIFYICONDATA ni;
3352 int count = 0;
3353 extern HWND message_window;
3354# endif
3355
3356 if (numFiles > 0 && argv[i + 1][0] == '+')
3357 /* Skip "+cmd" argument, don't wait for it to be edited. */
3358 --numFiles;
3359
3360# ifdef FEAT_GUI_W32
3361 ni.cbSize = sizeof(ni);
3362 ni.hWnd = message_window;
3363 ni.uID = 0;
3364 ni.uFlags = NIF_ICON|NIF_TIP;
3365 ni.hIcon = LoadIcon((HINSTANCE)GetModuleHandle(0), "IDR_VIM");
3366 sprintf(ni.szTip, _("%d of %d edited"), count, numFiles);
3367 Shell_NotifyIcon(NIM_ADD, &ni);
3368# endif
3369
3370 /* Wait for all files to unload in remote */
3371 memset(done, 0, numFiles);
3372 while (memchr(done, 0, numFiles) != NULL)
3373 {
3374# ifdef WIN32
3375 p = serverGetReply(srv, NULL, TRUE, TRUE);
3376 if (p == NULL)
3377 break;
3378# else
3379 if (serverReadReply(xterm_dpy, srv, &p, TRUE) < 0)
3380 break;
3381# endif
3382 j = atoi((char *)p);
3383 if (j >= 0 && j < numFiles)
3384 {
3385# ifdef FEAT_GUI_W32
3386 ++count;
3387 sprintf(ni.szTip, _("%d of %d edited"),
3388 count, numFiles);
3389 Shell_NotifyIcon(NIM_MODIFY, &ni);
3390# endif
3391 done[j] = 1;
3392 }
3393 }
3394# ifdef FEAT_GUI_W32
3395 Shell_NotifyIcon(NIM_DELETE, &ni);
3396# endif
3397 }
3398 }
3399 else if (STRICMP(argv[i], "--remote-expr") == 0)
3400 {
3401 if (i == *argc - 1)
3402 mainerr_arg_missing((char_u *)argv[i]);
3403# ifdef WIN32
3404 /* Win32 always works? */
3405 if (serverSendToVim(sname, (char_u *)argv[i + 1],
3406 &res, NULL, 1, FALSE) < 0)
3407# else
3408 if (xterm_dpy == NULL)
3409 mch_errmsg(_("No display: Send expression failed.\n"));
3410 else if (serverSendToVim(xterm_dpy, sname, (char_u *)argv[i + 1],
3411 &res, NULL, 1, 1, FALSE) < 0)
3412# endif
3413 {
3414 if (res != NULL && *res != NUL)
3415 {
3416 /* Output error from remote */
3417 mch_errmsg((char *)res);
3418 vim_free(res);
3419 res = NULL;
3420 }
3421 mch_errmsg(_(": Send expression failed.\n"));
3422 }
3423 }
3424 else if (STRICMP(argv[i], "--serverlist") == 0)
3425 {
3426# ifdef WIN32
3427 /* Win32 always works? */
3428 res = serverGetVimNames();
3429# else
3430 if (xterm_dpy != NULL)
3431 res = serverGetVimNames(xterm_dpy);
3432# endif
3433 if (called_emsg)
3434 mch_errmsg("\n");
3435 }
3436 else if (STRICMP(argv[i], "--servername") == 0)
3437 {
3438 /* Alredy processed. Take it out of the command line */
3439 i++;
3440 continue;
3441 }
3442 else
3443 {
3444 *newArgV++ = argv[i];
3445 newArgC++;
3446 continue;
3447 }
3448 didone = TRUE;
3449 if (res != NULL && *res != NUL)
3450 {
3451 mch_msg((char *)res);
3452 if (res[STRLEN(res) - 1] != '\n')
3453 mch_msg("\n");
3454 }
3455 vim_free(res);
3456 }
3457
3458 if (didone)
3459 {
3460 display_errors(); /* display any collected messages */
3461 exit(exiterr); /* Mission accomplished - get out */
3462 }
3463
3464 /* Return back into main() */
3465 *argc = newArgC;
3466 vim_free(sname);
3467}
3468
3469/*
3470 * Build a ":drop" command to send to a Vim server.
3471 */
3472 static char_u *
3473build_drop_cmd(filec, filev, sendReply)
3474 int filec;
3475 char **filev;
3476 int sendReply;
3477{
3478 garray_T ga;
3479 int i;
3480 char_u *inicmd = NULL;
3481 char_u *p;
3482 char_u cwd[MAXPATHL];
3483
3484 if (filec > 0 && filev[0][0] == '+')
3485 {
3486 inicmd = (char_u *)filev[0] + 1;
3487 filev++;
3488 filec--;
3489 }
3490 /* Check if we have at least one argument. */
3491 if (filec <= 0)
3492 mainerr_arg_missing((char_u *)filev[-1]);
3493 if (mch_dirname(cwd, MAXPATHL) != OK)
3494 return NULL;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003495 if ((p = vim_strsave_escaped_ext(cwd, PATH_ESC_CHARS, '\\', TRUE)) == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003496 return NULL;
3497 ga_init2(&ga, 1, 100);
3498 ga_concat(&ga, (char_u *)"<C-\\><C-N>:cd ");
3499 ga_concat(&ga, p);
3500 /* Call inputsave() so that a prompt for an encryption key works. */
3501 ga_concat(&ga, (char_u *)"<CR>:if exists('*inputsave')|call inputsave()|endif|drop");
3502 vim_free(p);
3503 for (i = 0; i < filec; i++)
3504 {
3505 /* On Unix the shell has already expanded the wildcards, don't want to
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003506 * do it again in the Vim server. On MS-Windows only escape
3507 * non-wildcard characters. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003508 p = vim_strsave_escaped((char_u *)filev[i],
3509#ifdef UNIX
3510 PATH_ESC_CHARS
3511#else
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003512 (char_u *)" \t%#"
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003513#endif
3514 );
3515 if (p == NULL)
3516 {
3517 vim_free(ga.ga_data);
3518 return NULL;
3519 }
3520 ga_concat(&ga, (char_u *)" ");
3521 ga_concat(&ga, p);
3522 vim_free(p);
3523 }
3524 /* The :drop commands goes to Insert mode when 'insertmode' is set, use
3525 * CTRL-\ CTRL-N again. */
3526 ga_concat(&ga, (char_u *)"|if exists('*inputrestore')|call inputrestore()|endif<CR>");
3527 ga_concat(&ga, (char_u *)"<C-\\><C-N>:cd -");
3528 if (sendReply)
3529 ga_concat(&ga, (char_u *)"<CR>:call SetupRemoteReplies()");
3530 ga_concat(&ga, (char_u *)"<CR>:");
3531 if (inicmd != NULL)
3532 {
3533 /* Can't use <CR> after "inicmd", because an "startinsert" would cause
3534 * the following commands to be inserted as text. Use a "|",
3535 * hopefully "inicmd" does allow this... */
3536 ga_concat(&ga, inicmd);
3537 ga_concat(&ga, (char_u *)"|");
3538 }
3539 /* Bring the window to the foreground, goto Insert mode when 'im' set and
3540 * clear command line. */
Bram Moolenaar567e4de2004-12-31 21:01:02 +00003541 ga_concat(&ga, (char_u *)"cal foreground()|if &im|star|en|redr|f<CR>");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003542 ga_append(&ga, NUL);
3543 return ga.ga_data;
3544}
3545
3546/*
3547 * Replace termcodes such as <CR> and insert as key presses if there is room.
3548 */
3549 void
3550server_to_input_buf(str)
3551 char_u *str;
3552{
3553 char_u *ptr = NULL;
3554 char_u *cpo_save = p_cpo;
3555
3556 /* Set 'cpoptions' the way we want it.
3557 * B set - backslashes are *not* treated specially
3558 * k set - keycodes are *not* reverse-engineered
3559 * < unset - <Key> sequences *are* interpreted
3560 * The last parameter of replace_termcodes() is TRUE so that the <lt>
3561 * sequence is recognised - needed for a real backslash.
3562 */
3563 p_cpo = (char_u *)"Bk";
3564 str = replace_termcodes((char_u *)str, &ptr, FALSE, TRUE);
3565 p_cpo = cpo_save;
3566
3567 if (*ptr != NUL) /* trailing CTRL-V results in nothing */
3568 {
3569 /*
3570 * Add the string to the input stream.
3571 * Can't use add_to_input_buf() here, we now have K_SPECIAL bytes.
3572 *
3573 * First clear typed characters from the typeahead buffer, there could
3574 * be half a mapping there. Then append to the existing string, so
3575 * that multiple commands from a client are concatenated.
3576 */
3577 if (typebuf.tb_maplen < typebuf.tb_len)
3578 del_typebuf(typebuf.tb_len - typebuf.tb_maplen, typebuf.tb_maplen);
3579 (void)ins_typebuf(str, REMAP_NONE, typebuf.tb_len, TRUE, FALSE);
3580
3581 /* Let input_available() know we inserted text in the typeahead
3582 * buffer. */
3583 received_from_client = TRUE;
3584 }
3585 vim_free((char_u *)ptr);
3586}
3587
3588/*
3589 * Evaluate an expression that the client sent to a string.
3590 * Handles disabling error messages and disables debugging, otherwise Vim
3591 * hangs, waiting for "cont" to be typed.
3592 */
3593 char_u *
3594eval_client_expr_to_string(expr)
3595 char_u *expr;
3596{
3597 char_u *res;
3598 int save_dbl = debug_break_level;
3599 int save_ro = redir_off;
3600
3601 debug_break_level = -1;
3602 redir_off = 0;
3603 ++emsg_skip;
3604
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003605 res = eval_to_string(expr, NULL, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003606
3607 debug_break_level = save_dbl;
3608 redir_off = save_ro;
3609 --emsg_skip;
3610
Bram Moolenaar63a121b2005-12-11 21:36:39 +00003611 /* A client can tell us to redraw, but not to display the cursor, so do
3612 * that here. */
3613 setcursor();
3614 out_flush();
3615#ifdef FEAT_GUI
3616 if (gui.in_use)
3617 gui_update_cursor(FALSE, FALSE);
3618#endif
3619
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003620 return res;
3621}
3622
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003623/*
3624 * If conversion is needed, convert "data" from "client_enc" to 'encoding' and
3625 * return an allocated string. Otherwise return "data".
3626 * "*tofree" is set to the result when it needs to be freed later.
3627 */
3628/*ARGSUSED*/
3629 char_u *
3630serverConvert(client_enc, data, tofree)
3631 char_u *client_enc;
3632 char_u *data;
3633 char_u **tofree;
3634{
3635 char_u *res = data;
3636
3637 *tofree = NULL;
3638# ifdef FEAT_MBYTE
3639 if (client_enc != NULL && p_enc != NULL)
3640 {
3641 vimconv_T vimconv;
3642
3643 vimconv.vc_type = CONV_NONE;
3644 if (convert_setup(&vimconv, client_enc, p_enc) != FAIL
3645 && vimconv.vc_type != CONV_NONE)
3646 {
3647 res = string_convert(&vimconv, data, NULL);
3648 if (res == NULL)
3649 res = data;
3650 else
3651 *tofree = res;
3652 }
3653 convert_setup(&vimconv, NULL, NULL);
3654 }
3655# endif
3656 return res;
3657}
3658
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003659
3660/*
3661 * Make our basic server name: use the specified "arg" if given, otherwise use
3662 * the tail of the command "cmd" we were started with.
3663 * Return the name in allocated memory. This doesn't include a serial number.
3664 */
3665 static char_u *
3666serverMakeName(arg, cmd)
3667 char_u *arg;
3668 char *cmd;
3669{
3670 char_u *p;
3671
3672 if (arg != NULL && *arg != NUL)
3673 p = vim_strsave_up(arg);
3674 else
3675 {
3676 p = vim_strsave_up(gettail((char_u *)cmd));
3677 /* Remove .exe or .bat from the name. */
3678 if (p != NULL && vim_strchr(p, '.') != NULL)
3679 *vim_strchr(p, '.') = NUL;
3680 }
3681 return p;
3682}
3683#endif /* FEAT_CLIENTSERVER */
3684
3685/*
3686 * When FEAT_FKMAP is defined, also compile the Farsi source code.
3687 */
3688#if defined(FEAT_FKMAP) || defined(PROTO)
3689# include "farsi.c"
3690#endif
3691
3692/*
3693 * When FEAT_ARABIC is defined, also compile the Arabic source code.
3694 */
3695#if defined(FEAT_ARABIC) || defined(PROTO)
3696# include "arabic.c"
3697#endif