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