blob: 32f91156d111a434ddaade6c9a7cdda0e0fcea9b [file] [log] [blame]
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10#if defined(MSDOS) || defined(WIN32) || defined(_WIN64)
Bram Moolenaar362e1a32006-03-06 23:29:24 +000011# include "vimio.h" /* for close() and dup() */
Bram Moolenaarb4210b32004-06-13 14:51:16 +000012#endif
13
14#define EXTERN
15#include "vim.h"
16
17#ifdef SPAWNO
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +000018# include <spawno.h> /* special MS-DOS swapping library */
Bram Moolenaarb4210b32004-06-13 14:51:16 +000019#endif
20
21#ifdef HAVE_FCNTL_H
22# include <fcntl.h>
23#endif
24
25#ifdef __CYGWIN__
26# ifndef WIN32
27# include <sys/cygwin.h> /* for cygwin_conv_to_posix_path() */
28# endif
29# include <limits.h>
30#endif
31
Bram Moolenaarc013cb62005-07-24 21:18:31 +000032/* Maximum number of commands from + or -c arguments. */
33#define MAX_ARG_CMDS 10
34
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000035/* values for "window_layout" */
36#define WIN_HOR 1 /* "-o" horizontally split windows */
37#define WIN_VER 2 /* "-O" vertically split windows */
38#define WIN_TABS 3 /* "-p" windows on tab pages */
39
Bram Moolenaar58d98232005-07-23 22:25:46 +000040/* Struct for various parameters passed between main() and other functions. */
41typedef struct
42{
Bram Moolenaarc013cb62005-07-24 21:18:31 +000043 int argc;
44 char **argv;
45
46 int evim_mode; /* started as "evim" */
Bram Moolenaarc013cb62005-07-24 21:18:31 +000047 char_u *use_vimrc; /* vimrc from -u argument */
48
49 int n_commands; /* no. of commands from + or -c */
50 char_u *commands[MAX_ARG_CMDS]; /* commands from + or -c arg. */
51 char_u cmds_tofree[MAX_ARG_CMDS]; /* commands that need free() */
52 int n_pre_commands; /* no. of commands from --cmd */
53 char_u *pre_commands[MAX_ARG_CMDS]; /* commands from --cmd argument */
54
55 int edit_type; /* type of editing to do */
56 char_u *tagname; /* tag from -t argument */
57#ifdef FEAT_QUICKFIX
58 char_u *use_ef; /* 'errorfile' from -q argument */
59#endif
60
61 int want_full_screen;
62 int stdout_isatty; /* is stdout a terminal? */
63 char_u *term; /* specified terminal name */
64#ifdef FEAT_CRYPT
65 int ask_for_key; /* -x argument */
66#endif
67 int no_swap_file; /* "-n" argument used */
68#ifdef FEAT_EVAL
69 int use_debug_break_level;
70#endif
71#ifdef FEAT_WINDOWS
72 int window_count; /* number of windows to use */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000073 int window_layout; /* 0, WIN_HOR, WIN_VER or WIN_TABS */
Bram Moolenaarc013cb62005-07-24 21:18:31 +000074#endif
75
76#ifdef FEAT_CLIENTSERVER
Bram Moolenaar58d98232005-07-23 22:25:46 +000077 int serverArg; /* TRUE when argument for a server */
78 char_u *serverName_arg; /* cmdline arg for server name */
Bram Moolenaarc013cb62005-07-24 21:18:31 +000079 char_u *serverStr; /* remote server command */
80 char_u *serverStrEnc; /* encoding of serverStr */
81 char_u *servername; /* allocated name for our server */
82#endif
83#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
84 int literal; /* don't expand file names */
85#endif
86#ifdef MSWIN
87 int full_path; /* file name argument was full path */
88#endif
89#ifdef FEAT_DIFF
90 int diff_mode; /* start with 'diff' set */
91#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +000092} mparm_T;
93
Bram Moolenaarc013cb62005-07-24 21:18:31 +000094/* Values for edit_type. */
95#define EDIT_NONE 0 /* no edit type yet */
96#define EDIT_FILE 1 /* file name argument[s] given, use argument list */
97#define EDIT_STDIN 2 /* read file from stdin */
98#define EDIT_TAG 3 /* tag name argument given, use tagname */
99#define EDIT_QF 4 /* start in quickfix mode */
100
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000101#if defined(UNIX) || defined(VMS)
102static int file_owned __ARGS((char *fname));
103#endif
104static void mainerr __ARGS((int, char_u *));
105static void main_msg __ARGS((char *s));
106static void usage __ARGS((void));
107static int get_number_arg __ARGS((char_u *p, int *idx, int def));
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000108#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
109static void init_locale __ARGS((void));
110#endif
111static void parse_command_name __ARGS((mparm_T *parmp));
112static void early_arg_scan __ARGS((mparm_T *parmp));
113static void command_line_scan __ARGS((mparm_T *parmp));
114static void check_tty __ARGS((mparm_T *parmp));
115static void read_stdin __ARGS((void));
116static void create_windows __ARGS((mparm_T *parmp));
117#ifdef FEAT_WINDOWS
118static void edit_buffers __ARGS((mparm_T *parmp));
119#endif
120static void exe_pre_commands __ARGS((mparm_T *parmp));
121static void exe_commands __ARGS((mparm_T *parmp));
Bram Moolenaar58d98232005-07-23 22:25:46 +0000122static void source_startup_scripts __ARGS((mparm_T *parmp));
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000123static void main_start_gui __ARGS((void));
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000124#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000125static void check_swap_exists_action __ARGS((void));
126#endif
127#ifdef FEAT_CLIENTSERVER
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000128static void exec_on_server __ARGS((mparm_T *parmp));
129static void prepare_server __ARGS((mparm_T *parmp));
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000130static void cmdsrv_main __ARGS((int *argc, char **argv, char_u *serverName_arg, char_u **serverStr));
131static char_u *serverMakeName __ARGS((char_u *arg, char *cmd));
132#endif
133
134
135#ifdef STARTUPTIME
136static FILE *time_fd = NULL;
137#endif
138
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000139/*
140 * Different types of error messages.
141 */
142static char *(main_errors[]) =
143{
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000144 N_("Unknown option argument"),
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000145#define ME_UNKNOWN_OPTION 0
146 N_("Too many edit arguments"),
147#define ME_TOO_MANY_ARGS 1
148 N_("Argument missing after"),
149#define ME_ARG_MISSING 2
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000150 N_("Garbage after option argument"),
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000151#define ME_GARBAGE 3
152 N_("Too many \"+command\", \"-c command\" or \"--cmd command\" arguments"),
153#define ME_EXTRA_CMD 4
154 N_("Invalid argument for"),
155#define ME_INVALID_ARG 5
156};
157
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000158#ifndef PROTO /* don't want a prototype for main() */
159 int
160# ifdef VIMDLL
161_export
162# endif
163# ifdef FEAT_GUI_MSWIN
164# ifdef __BORLANDC__
165_cdecl
166# endif
167VimMain
168# else
169main
170# endif
171(argc, argv)
172 int argc;
173 char **argv;
174{
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000175 char_u *fname = NULL; /* file name from command line */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000176 mparm_T params; /* various parameters passed between
177 * main() and other functions. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000178
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000179 /*
180 * Do any system-specific initialisations. These can NOT use IObuff or
181 * NameBuff. Thus emsg2() cannot be called!
182 */
183 mch_early_init();
184
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000185 /* Many variables are in "params" so that we can pass them to invoked
186 * functions without a lot of arguments. "argc" and "argv" are also
187 * copied, so that they can be changed. */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000188 vim_memset(&params, 0, sizeof(params));
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000189 params.argc = argc;
190 params.argv = argv;
191 params.want_full_screen = TRUE;
192#ifdef FEAT_EVAL
193 params.use_debug_break_level = -1;
194#endif
195#ifdef FEAT_WINDOWS
196 params.window_count = -1;
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000197#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +0000198
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000199#ifdef FEAT_TCL
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000200 vim_tcl_init(params.argv[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000201#endif
202
203#ifdef MEM_PROFILE
204 atexit(vim_mem_profile_dump);
205#endif
206
207#ifdef STARTUPTIME
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000208 time_fd = mch_fopen(STARTUPTIME, "a");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000209 TIME_MSG("--- VIM STARTING ---");
210#endif
Bram Moolenaarca003e12006-03-17 23:19:38 +0000211 starttime = time(NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000212
213#ifdef __EMX__
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000214 _wildcard(&params.argc, &params.argv);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000215#endif
216
217#ifdef FEAT_MBYTE
218 (void)mb_init(); /* init mb_bytelen_tab[] to ones */
219#endif
Bram Moolenaardcaf10e2005-01-21 11:55:25 +0000220#ifdef FEAT_EVAL
221 eval_init(); /* init global variables */
222#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000223
224#ifdef __QNXNTO__
225 qnx_init(); /* PhAttach() for clipboard, (and gui) */
226#endif
227
228#ifdef MAC_OS_CLASSIC
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000229 /* Prepare for possibly starting GUI sometime */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000230 /* Macintosh needs this before any memory is allocated. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000231 gui_prepare(&params.argc, params.argv);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000232 TIME_MSG("GUI prepared");
233#endif
234
235 /* Init the table of Normal mode commands. */
236 init_normal_cmds();
237
238#if defined(HAVE_DATE_TIME) && defined(VMS) && defined(VAXC)
Bram Moolenaar58d98232005-07-23 22:25:46 +0000239 make_version(); /* Construct the long version string. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000240#endif
241
242 /*
243 * Allocate space for the generic buffers (needed for set_init_1() and
244 * EMSG2()).
245 */
246 if ((IObuff = alloc(IOSIZE)) == NULL
247 || (NameBuff = alloc(MAXPATHL)) == NULL)
248 mch_exit(0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000249 TIME_MSG("Allocated generic buffers");
250
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000251#ifdef NBDEBUG
252 /* Wait a moment for debugging NetBeans. Must be after allocating
253 * NameBuff. */
254 nbdebug_log_init("SPRO_GVIM_DEBUG", "SPRO_GVIM_DLEVEL");
255 nbdebug_wait(WT_ENV | WT_WAIT | WT_STOP, "SPRO_GVIM_WAIT", 20);
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000256 TIME_MSG("NetBeans debug wait");
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000257#endif
258
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000259#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
260 /*
261 * Setup to use the current locale (for ctype() and many other things).
262 * NOTE: Translated messages with encodings other than latin1 will not
263 * work until set_init_1() has been called!
264 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000265 init_locale();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000266 TIME_MSG("locale set");
267#endif
268
269#ifdef FEAT_GUI
270 gui.dofork = TRUE; /* default is to use fork() */
271#endif
272
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000273 /*
Bram Moolenaar58d98232005-07-23 22:25:46 +0000274 * Do a first scan of the arguments in "argv[]":
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000275 * -display or --display
Bram Moolenaar58d98232005-07-23 22:25:46 +0000276 * --server...
277 * --socketid
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000278 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000279 early_arg_scan(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000280
281#ifdef FEAT_SUN_WORKSHOP
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000282 findYourself(params.argv[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000283#endif
284#if defined(FEAT_GUI) && !defined(MAC_OS_CLASSIC)
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000285 /* Prepare for possibly starting GUI sometime */
286 gui_prepare(&params.argc, params.argv);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000287 TIME_MSG("GUI prepared");
288#endif
289
290#ifdef FEAT_CLIPBOARD
291 clip_init(FALSE); /* Initialise clipboard stuff */
292 TIME_MSG("clipboard setup");
293#endif
294
295 /*
296 * Check if we have an interactive window.
297 * On the Amiga: If there is no window, we open one with a newcli command
298 * (needed for :! to * work). mch_check_win() will also handle the -d or
299 * -dev argument.
300 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000301 params.stdout_isatty = (mch_check_win(params.argc, params.argv) != FAIL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000302 TIME_MSG("window checked");
303
304 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000305 * Allocate the first window and buffer.
306 * Can't do anything without it, exit when it fails.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000307 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000308 if (win_alloc_first() == FAIL)
309 mch_exit(0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000310
311 init_yank(); /* init yank buffers */
312
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000313 alist_init(&global_alist); /* Init the argument list to empty. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000314
315 /*
316 * Set the default values for the options.
317 * NOTE: Non-latin1 translated messages are working only after this,
318 * because this is where "has_mbyte" will be set, which is used by
319 * msg_outtrans_len_attr().
320 * First find out the home directory, needed to expand "~" in options.
321 */
322 init_homedir(); /* find real value of $HOME */
323 set_init_1();
324 TIME_MSG("inits 1");
325
326#ifdef FEAT_EVAL
327 set_lang_var(); /* set v:lang and v:ctype */
328#endif
329
330#ifdef FEAT_CLIENTSERVER
331 /*
332 * Do the client-server stuff, unless "--servername ''" was used.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000333 * This may exit Vim if the command was sent to the server.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000334 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000335 exec_on_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000336#endif
337
338 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000339 * Figure out the way to work from the command name argv[0].
340 * "vimdiff" starts diff mode, "rvim" sets "restricted", etc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000341 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000342 parse_command_name(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000343
344 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000345 * Process the command line arguments. File names are put in the global
346 * argument list "global_alist".
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000347 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000348 command_line_scan(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000349 TIME_MSG("parsing arguments");
350
351 /*
352 * On some systems, when we compile with the GUI, we always use it. On Mac
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000353 * there is no terminal version, and on Windows we can't fork one off with
354 * :gui.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000355 */
356#ifdef ALWAYS_USE_GUI
357 gui.starting = TRUE;
358#else
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000359# if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000360 /*
361 * Check if the GUI can be started. Reset gui.starting if not.
362 * Don't know about other systems, stay on the safe side and don't check.
363 */
364 if (gui.starting && gui_init_check() == FAIL)
365 {
366 gui.starting = FALSE;
367
368 /* When running "evim" or "gvim -y" we need the menus, exit if we
369 * don't have them. */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000370 if (params.evim_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000371 mch_exit(1);
372 }
373# endif
374#endif
375
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000376 if (GARGCOUNT > 0)
377 {
378#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
379 /*
380 * Expand wildcards in file names.
381 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000382 if (!params.literal)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000383 {
384 /* Temporarily add '(' and ')' to 'isfname'. These are valid
385 * filename characters but are excluded from 'isfname' to make
386 * "gf" work on a file name in parenthesis (e.g.: see vim.h). */
387 do_cmdline_cmd((char_u *)":set isf+=(,)");
Bram Moolenaar86b68352004-12-27 21:59:20 +0000388 alist_expand(NULL, 0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000389 do_cmdline_cmd((char_u *)":set isf&");
390 }
391#endif
392 fname = alist_name(&GARGLIST[0]);
393 }
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000394
395#if defined(WIN32) && defined(FEAT_MBYTE)
396 {
397 extern void set_alist_count(void);
398
399 /* Remember the number of entries in the argument list. If it changes
400 * we don't react on setting 'encoding'. */
401 set_alist_count();
402 }
403#endif
404
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000405#ifdef MSWIN
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000406 if (GARGCOUNT == 1 && params.full_path)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000407 {
408 /*
409 * If there is one filename, fully qualified, we have very probably
410 * been invoked from explorer, so change to the file's directory.
411 * Hint: to avoid this when typing a command use a forward slash.
412 * If the cd fails, it doesn't matter.
413 */
414 (void)vim_chdirfile(fname);
415 }
416#endif
417 TIME_MSG("expanding arguments");
418
419#ifdef FEAT_DIFF
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000420 if (params.diff_mode && params.window_count == -1)
421 params.window_count = 0; /* open up to 3 windows */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000422#endif
423
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000424 /* Don't redraw until much later. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000425 ++RedrawingDisabled;
426
427 /*
428 * When listing swap file names, don't do cursor positioning et. al.
429 */
430 if (recoverymode && fname == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000431 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000432
433 /*
434 * When certain to start the GUI, don't check capabilities of terminal.
435 * For GTK we can't be sure, but when started from the desktop it doesn't
436 * make sense to try using a terminal.
437 */
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000438#if defined(ALWAYS_USE_GUI) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000439 if (gui.starting
440# ifdef FEAT_GUI_GTK
441 && !isatty(2)
442# endif
443 )
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000444 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000445#endif
446
447#if defined(FEAT_GUI_MAC) && defined(MACOS_X_UNIX)
448 /* When the GUI is started from Finder, need to display messages in a
449 * message box. isatty(2) returns TRUE anyway, thus we need to check the
450 * name to know we're not started from a terminal. */
451 if (gui.starting && (!isatty(2) || strcmp("/dev/console", ttyname(2)) == 0))
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000452 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000453 params.want_full_screen = FALSE;
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000454
455 /* Avoid always using "/" as the current directory. Note that when
456 * started from Finder the arglist will be filled later in
457 * HandleODocAE() and "fname" will be NULL. */
458 if (getcwd((char *)NameBuff, MAXPATHL) != NULL
459 && STRCMP(NameBuff, "/") == 0)
460 {
461 if (fname != NULL)
462 (void)vim_chdirfile(fname);
463 else
464 {
465 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
466 vim_chdir(NameBuff);
467 }
468 }
469 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000470#endif
471
472 /*
473 * mch_init() sets up the terminal (window) for use. This must be
474 * done after resetting full_screen, otherwise it may move the cursor
475 * (MSDOS).
476 * Note that we may use mch_exit() before mch_init()!
477 */
478 mch_init();
479 TIME_MSG("shell init");
480
481#ifdef USE_XSMP
482 /*
483 * For want of anywhere else to do it, try to connect to xsmp here.
484 * Fitting it in after gui_mch_init, but before gui_init (via termcapinit).
485 * Hijacking -X 'no X connection' to also disable XSMP connection as that
486 * has a similar delay upon failure.
487 * Only try if SESSION_MANAGER is set to something non-null.
488 */
489 if (!x_no_connect)
490 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000491 char *p = getenv("SESSION_MANAGER");
492
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000493 if (p != NULL && *p != NUL)
494 {
495 xsmp_init();
496 TIME_MSG("xsmp init");
497 }
498 }
499#endif
500
501 /*
502 * Print a warning if stdout is not a terminal.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000503 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000504 check_tty(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000505
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000506 /* This message comes before term inits, but after setting "silent_mode"
507 * when the input is not a tty. */
508 if (GARGCOUNT > 1 && !silent_mode)
509 printf(_("%d files to edit\n"), GARGCOUNT);
510
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000511 if (params.want_full_screen && !silent_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000512 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000513 termcapinit(params.term); /* set terminal name and get terminal
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000514 capabilities (will set full_screen) */
515 screen_start(); /* don't know where cursor is now */
516 TIME_MSG("Termcap init");
517 }
518
519 /*
520 * Set the default values for the options that use Rows and Columns.
521 */
522 ui_get_shellsize(); /* inits Rows and Columns */
523#ifdef FEAT_NETBEANS_INTG
524 if (usingNetbeans)
525 Columns += 2; /* leave room for glyph gutter */
526#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000527 win_init_size();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000528#ifdef FEAT_DIFF
529 /* Set the 'diff' option now, so that it can be checked for in a .vimrc
530 * file. There is no buffer yet though. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000531 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000532 diff_win_options(firstwin, FALSE);
533#endif
534
535 cmdline_row = Rows - p_ch;
536 msg_row = cmdline_row;
537 screenalloc(FALSE); /* allocate screen buffers */
538 set_init_2();
539 TIME_MSG("inits 2");
540
541 msg_scroll = TRUE;
542 no_wait_return = TRUE;
543
544 init_mappings(); /* set up initial mappings */
545
546 init_highlight(TRUE, FALSE); /* set the default highlight groups */
547 TIME_MSG("init highlight");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000548
549#ifdef FEAT_EVAL
550 /* Set the break level after the terminal is initialized. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000551 debug_break_level = params.use_debug_break_level;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000552#endif
553
Bram Moolenaar58d98232005-07-23 22:25:46 +0000554 /* Execute --cmd arguments. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000555 exe_pre_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000556
Bram Moolenaar58d98232005-07-23 22:25:46 +0000557 /* Source startup scripts. */
558 source_startup_scripts(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000559
560#ifdef FEAT_EVAL
561 /*
562 * Read all the plugin files.
563 * Only when compiled with +eval, since most plugins need it.
564 */
565 if (p_lpl)
566 {
Bram Moolenaar07d4d732005-10-03 22:04:08 +0000567 source_runtime((char_u *)"plugin/**/*.vim", TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000568 TIME_MSG("loading plugins");
569 }
570#endif
571
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000572#ifdef FEAT_DIFF
573 /* Decide about window layout for diff mode after reading vimrc. */
574 if (params.diff_mode && params.window_layout == 0)
575 {
576 if (diffopt_horizontal())
577 params.window_layout = WIN_HOR; /* use horizontal split */
578 else
579 params.window_layout = WIN_VER; /* use vertical split */
580 }
581#endif
582
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000583 /*
584 * Recovery mode without a file name: List swap files.
585 * This uses the 'dir' option, therefore it must be after the
586 * initializations.
587 */
588 if (recoverymode && fname == NULL)
589 {
590 recover_names(NULL, TRUE, 0);
591 mch_exit(0);
592 }
593
594 /*
595 * Set a few option defaults after reading .vimrc files:
596 * 'title' and 'icon', Unix: 'shellpipe' and 'shellredir'.
597 */
598 set_init_3();
599 TIME_MSG("inits 3");
600
601 /*
602 * "-n" argument: Disable swap file by setting 'updatecount' to 0.
603 * Note that this overrides anything from a vimrc file.
604 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000605 if (params.no_swap_file)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000606 p_uc = 0;
607
608#ifdef FEAT_FKMAP
609 if (curwin->w_p_rl && p_altkeymap)
610 {
611 p_hkmap = FALSE; /* Reset the Hebrew keymap mode */
612# ifdef FEAT_ARABIC
613 curwin->w_p_arab = FALSE; /* Reset the Arabic keymap mode */
614# endif
615 p_fkmap = TRUE; /* Set the Farsi keymap mode */
616 }
617#endif
618
619#ifdef FEAT_GUI
620 if (gui.starting)
621 {
622#if defined(UNIX) || defined(VMS)
623 /* When something caused a message from a vimrc script, need to output
624 * an extra newline before the shell prompt. */
625 if (did_emsg || msg_didout)
626 putchar('\n');
627#endif
628
629 gui_start(); /* will set full_screen to TRUE */
630 TIME_MSG("starting GUI");
631
632 /* When running "evim" or "gvim -y" we need the menus, exit if we
633 * don't have them. */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000634 if (!gui.in_use && params.evim_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000635 mch_exit(1);
636 }
637#endif
638
639#ifdef SPAWNO /* special MSDOS swapping library */
640 init_SPAWNO("", SWAP_ANY);
641#endif
642
643#ifdef FEAT_VIMINFO
644 /*
645 * Read in registers, history etc, but not marks, from the viminfo file
646 */
647 if (*p_viminfo != NUL)
648 {
649 read_viminfo(NULL, TRUE, FALSE, FALSE);
650 TIME_MSG("reading viminfo");
651 }
652#endif
653
654#ifdef FEAT_QUICKFIX
655 /*
656 * "-q errorfile": Load the error file now.
657 * If the error file can't be read, exit before doing anything else.
658 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000659 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000660 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000661 if (params.use_ef != NULL)
662 set_string_option_direct((char_u *)"ef", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000663 params.use_ef, OPT_FREE, SID_CARG);
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000664 if (qf_init(NULL, p_ef, p_efm, TRUE) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000665 {
666 out_char('\n');
667 mch_exit(3);
668 }
669 TIME_MSG("reading errorfile");
670 }
671#endif
672
673 /*
674 * Start putting things on the screen.
675 * Scroll screen down before drawing over it
676 * Clear screen now, so file message will not be cleared.
677 */
678 starting = NO_BUFFERS;
679 no_wait_return = FALSE;
680 if (!exmode_active)
681 msg_scroll = FALSE;
682
683#ifdef FEAT_GUI
684 /*
685 * This seems to be required to make callbacks to be called now, instead
686 * of after things have been put on the screen, which then may be deleted
687 * when getting a resize callback.
688 * For the Mac this handles putting files dropped on the Vim icon to
689 * global_alist.
690 */
691 if (gui.in_use)
692 {
693# ifdef FEAT_SUN_WORKSHOP
694 if (!usingSunWorkShop)
695# endif
696 gui_wait_for_chars(50L);
697 TIME_MSG("GUI delay");
698 }
699#endif
700
701#if defined(FEAT_GUI_PHOTON) && defined(FEAT_CLIPBOARD)
702 qnx_clip_init();
703#endif
704
705#ifdef FEAT_XCLIPBOARD
706 /* Start using the X clipboard, unless the GUI was started. */
707# ifdef FEAT_GUI
708 if (!gui.in_use)
709# endif
710 {
711 setup_term_clip();
712 TIME_MSG("setup clipboard");
713 }
714#endif
715
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000716#ifdef FEAT_CLIENTSERVER
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000717 /* Prepare for being a Vim server. */
718 prepare_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000719#endif
720
721 /*
722 * If "-" argument given: Read file from stdin.
723 * Do this before starting Raw mode, because it may change things that the
724 * writing end of the pipe doesn't like, e.g., in case stdin and stderr
725 * are the same terminal: "cat | vim -".
726 * Using autocommands here may cause trouble...
727 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000728 if (params.edit_type == EDIT_STDIN && !recoverymode)
729 read_stdin();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000730
731#if defined(UNIX) || defined(VMS)
732 /* When switching screens and something caused a message from a vimrc
733 * script, need to output an extra newline on exit. */
734 if ((did_emsg || msg_didout) && *T_TI != NUL)
735 newline_on_exit = TRUE;
736#endif
737
738 /*
739 * When done something that is not allowed or error message call
740 * wait_return. This must be done before starttermcap(), because it may
741 * switch to another screen. It must be done after settmode(TMODE_RAW),
742 * because we want to react on a single key stroke.
743 * Call settmode and starttermcap here, so the T_KS and T_TI may be
744 * defined by termcapinit and redifined in .exrc.
745 */
746 settmode(TMODE_RAW);
747 TIME_MSG("setting raw mode");
748
749 if (need_wait_return || msg_didany)
750 {
751 wait_return(TRUE);
752 TIME_MSG("waiting for return");
753 }
754
755 starttermcap(); /* start termcap if not done by wait_return() */
756 TIME_MSG("start termcap");
757
758#ifdef FEAT_MOUSE
759 setmouse(); /* may start using the mouse */
760#endif
761 if (scroll_region)
762 scroll_region_reset(); /* In case Rows changed */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000763 scroll_start(); /* may scroll the screen to the right position */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000764
765 /*
766 * Don't clear the screen when starting in Ex mode, unless using the GUI.
767 */
768 if (exmode_active
769#ifdef FEAT_GUI
770 && !gui.in_use
771#endif
772 )
773 must_redraw = CLEAR;
774 else
775 {
776 screenclear(); /* clear screen */
777 TIME_MSG("clearing screen");
778 }
779
780#ifdef FEAT_CRYPT
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000781 if (params.ask_for_key)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000782 {
783 (void)get_crypt_key(TRUE, TRUE);
784 TIME_MSG("getting crypt key");
785 }
786#endif
787
788 no_wait_return = TRUE;
789
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000790 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000791 * Create the requested number of windows and edit buffers in them.
792 * Also does recovery if "recoverymode" set.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000793 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000794 create_windows(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000795 TIME_MSG("opening buffers");
796
797 /* Ex starts at last line of the file */
798 if (exmode_active)
799 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
800
801#ifdef FEAT_AUTOCMD
802 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
803 TIME_MSG("BufEnter autocommands");
804#endif
805 setpcmark();
806
807#ifdef FEAT_QUICKFIX
808 /*
809 * When started with "-q errorfile" jump to first error now.
810 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000811 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000812 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000813 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000814 TIME_MSG("jump to first error");
815 }
816#endif
817
818#ifdef FEAT_WINDOWS
819 /*
820 * If opened more than one window, start editing files in the other
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000821 * windows.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000822 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000823 edit_buffers(&params);
824#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000825
826#ifdef FEAT_DIFF
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000827 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000828 {
829 win_T *wp;
830
831 /* set options in each window for "vimdiff". */
832 for (wp = firstwin; wp != NULL; wp = wp->w_next)
833 diff_win_options(wp, TRUE);
834 }
835#endif
836
837 /*
838 * Shorten any of the filenames, but only when absolute.
839 */
840 shorten_fnames(FALSE);
841
842 /*
843 * Need to jump to the tag before executing the '-c command'.
844 * Makes "vim -c '/return' -t main" work.
845 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000846 if (params.tagname != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000847 {
Bram Moolenaar146522e2005-12-16 21:55:46 +0000848#if defined(HAS_SWAP_EXISTS_ACTION)
849 swap_exists_did_quit = FALSE;
850#endif
851
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000852 vim_snprintf((char *)IObuff, IOSIZE, "ta %s", params.tagname);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000853 do_cmdline_cmd(IObuff);
854 TIME_MSG("jumping to tag");
Bram Moolenaar146522e2005-12-16 21:55:46 +0000855
856#if defined(HAS_SWAP_EXISTS_ACTION)
857 /* If the user doesn't want to edit the file then we quit here. */
858 if (swap_exists_did_quit)
859 getout(1);
860#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000861 }
862
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000863 /* Execute any "+", "-c" and "-S" arguments. */
864 if (params.n_commands > 0)
865 exe_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000866
867 RedrawingDisabled = 0;
868 redraw_all_later(NOT_VALID);
869 no_wait_return = FALSE;
870 starting = 0;
871
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000872#ifdef FEAT_TERMRESPONSE
873 /* Requesting the termresponse is postponed until here, so that a "-c q"
874 * argument doesn't make it appear in the shell Vim was started from. */
875 may_req_termresponse();
876#endif
877
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000878 /* start in insert mode */
879 if (p_im)
880 need_start_insertmode = TRUE;
881
882#ifdef FEAT_AUTOCMD
883 apply_autocmds(EVENT_VIMENTER, NULL, NULL, FALSE, curbuf);
884 TIME_MSG("VimEnter autocommands");
885#endif
886
887#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
888 /* When a startup script or session file setup for diff'ing and
889 * scrollbind, sync the scrollbind now. */
890 if (curwin->w_p_diff && curwin->w_p_scb)
891 {
892 update_topline();
893 check_scrollbind((linenr_T)0, 0L);
894 TIME_MSG("diff scrollbinding");
895 }
896#endif
897
898#if defined(WIN3264) && !defined(FEAT_GUI_W32)
899 mch_set_winsize_now(); /* Allow winsize changes from now on */
900#endif
901
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000902#if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
903 /* When tab pages were created, may need to update the tab pages line and
904 * scrollbars. This is skipped while creating them. */
905 if (first_tabpage->tp_next != NULL)
906 {
907 out_flush();
908 gui_init_which_components(NULL);
909 gui_update_scrollbars(TRUE);
910 }
911 need_mouse_correct = TRUE;
912#endif
913
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000914 /* If ":startinsert" command used, stuff a dummy command to be able to
915 * call normal_cmd(), which will then start Insert mode. */
916 if (restart_edit != 0)
Bram Moolenaarebefac62005-12-28 22:39:57 +0000917 stuffcharReadbuff(K_NOP);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000918
919#ifdef FEAT_NETBEANS_INTG
920 if (usingNetbeans)
921 /* Tell the client that it can start sending commands. */
922 netbeans_startup_done();
923#endif
924
925 TIME_MSG("before starting main loop");
926
927 /*
928 * Call the main command loop. This never returns.
929 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000930 main_loop(FALSE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000931
932 return 0;
933}
934#endif /* PROTO */
935
936/*
937 * Main loop: Execute Normal mode commands until exiting Vim.
938 * Also used to handle commands in the command-line window, until the window
939 * is closed.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000940 * Also used to handle ":visual" command after ":global": execute Normal mode
941 * commands, return when entering Ex mode. "noexmode" is TRUE then.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000942 */
943 void
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000944main_loop(cmdwin, noexmode)
945 int cmdwin; /* TRUE when working in the command-line window */
946 int noexmode; /* TRUE when return on entering Ex mode */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000947{
948 oparg_T oa; /* operator arguments */
949
950#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
951 /* Setup to catch a terminating error from the X server. Just ignore
952 * it, restore the state and continue. This might not always work
953 * properly, but at least we don't exit unexpectedly when the X server
954 * exists while Vim is running in a console. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000955 if (!cmdwin && !noexmode && SETJMP(x_jump_env))
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000956 {
957 State = NORMAL;
958# ifdef FEAT_VISUAL
959 VIsual_active = FALSE;
960# endif
961 got_int = TRUE;
962 need_wait_return = FALSE;
963 global_busy = FALSE;
964 exmode_active = 0;
965 skip_redraw = FALSE;
966 RedrawingDisabled = 0;
967 no_wait_return = 0;
968# ifdef FEAT_EVAL
969 emsg_skip = 0;
970# endif
971 emsg_off = 0;
972# ifdef FEAT_MOUSE
973 setmouse();
974# endif
975 settmode(TMODE_RAW);
976 starttermcap();
977 scroll_start();
978 redraw_later_clear();
979 }
980#endif
981
982 clear_oparg(&oa);
983 while (!cmdwin
984#ifdef FEAT_CMDWIN
985 || cmdwin_result == 0
986#endif
987 )
988 {
989 if (stuff_empty())
990 {
991 did_check_timestamps = FALSE;
992 if (need_check_timestamps)
993 check_timestamps(FALSE);
994 if (need_wait_return) /* if wait_return still needed ... */
995 wait_return(FALSE); /* ... call it now */
996 if (need_start_insertmode && goto_im()
997#ifdef FEAT_VISUAL
998 && !VIsual_active
999#endif
1000 )
1001 {
1002 need_start_insertmode = FALSE;
1003 stuffReadbuff((char_u *)"i"); /* start insert mode next */
1004 /* skip the fileinfo message now, because it would be shown
1005 * after insert mode finishes! */
1006 need_fileinfo = FALSE;
1007 }
1008 }
1009 if (got_int && !global_busy)
1010 {
1011 if (!quit_more)
1012 (void)vgetc(); /* flush all buffers */
1013 got_int = FALSE;
1014 }
1015 if (!exmode_active)
1016 msg_scroll = FALSE;
1017 quit_more = FALSE;
1018
1019 /*
1020 * If skip redraw is set (for ":" in wait_return()), don't redraw now.
1021 * If there is nothing in the stuff_buffer or do_redraw is TRUE,
1022 * update cursor and redraw.
1023 */
1024 if (skip_redraw || exmode_active)
1025 skip_redraw = FALSE;
1026 else if (do_redraw || stuff_empty())
1027 {
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001028#ifdef FEAT_AUTOCMD
1029 /* Trigger CursorMoved if the cursor moved. */
1030 if (!finish_op && has_cursormoved()
1031 && !equalpos(last_cursormoved, curwin->w_cursor))
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001032 {
1033 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL, FALSE, curbuf);
1034 last_cursormoved = curwin->w_cursor;
1035 }
1036#endif
1037
Bram Moolenaar33aec762006-01-22 23:30:12 +00001038#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
1039 /* Scroll-binding for diff mode may have been postponed until
1040 * here. Avoids doing it for every change. */
1041 if (diff_need_scrollbind)
1042 {
1043 check_scrollbind((linenr_T)0, 0L);
1044 diff_need_scrollbind = FALSE;
1045 }
1046#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001047#if defined(FEAT_FOLDING) && defined(FEAT_VISUAL)
1048 /* Include a closed fold completely in the Visual area. */
1049 foldAdjustVisual();
1050#endif
1051#ifdef FEAT_FOLDING
1052 /*
1053 * When 'foldclose' is set, apply 'foldlevel' to folds that don't
1054 * contain the cursor.
1055 * When 'foldopen' is "all", open the fold(s) under the cursor.
1056 * This may mark the window for redrawing.
1057 */
1058 if (hasAnyFolding(curwin) && !char_avail())
1059 {
1060 foldCheckClose();
1061 if (fdo_flags & FDO_ALL)
1062 foldOpenCursor();
1063 }
1064#endif
1065
1066 /*
1067 * Before redrawing, make sure w_topline is correct, and w_leftcol
1068 * if lines don't wrap, and w_skipcol if lines wrap.
1069 */
1070 update_topline();
1071 validate_cursor();
1072
1073#ifdef FEAT_VISUAL
1074 if (VIsual_active)
1075 update_curbuf(INVERTED);/* update inverted part */
1076 else
1077#endif
1078 if (must_redraw)
1079 update_screen(0);
1080 else if (redraw_cmdline || clear_cmdline)
1081 showmode();
1082#ifdef FEAT_WINDOWS
1083 redraw_statuslines();
1084#endif
1085#ifdef FEAT_TITLE
1086 if (need_maketitle)
1087 maketitle();
1088#endif
1089 /* display message after redraw */
1090 if (keep_msg != NULL)
1091 {
1092 char_u *p;
1093
1094 /* msg_attr_keep() will set keep_msg to NULL, must free the
1095 * string here. */
1096 p = keep_msg;
Bram Moolenaar238a5642006-02-21 22:12:05 +00001097 keep_msg = NULL;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001098 msg_attr(p, keep_msg_attr);
1099 vim_free(p);
1100 }
1101 if (need_fileinfo) /* show file info after redraw */
1102 {
1103 fileinfo(FALSE, TRUE, FALSE);
1104 need_fileinfo = FALSE;
1105 }
1106
1107 emsg_on_display = FALSE; /* can delete error message now */
1108 did_emsg = FALSE;
1109 msg_didany = FALSE; /* reset lines_left in msg_start() */
Bram Moolenaar661b1822005-07-28 22:36:45 +00001110 may_clear_sb_text(); /* clear scroll-back text on next msg */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001111 showruler(FALSE);
1112
1113 setcursor();
1114 cursor_on();
1115
1116 do_redraw = FALSE;
1117 }
1118#ifdef FEAT_GUI
1119 if (need_mouse_correct)
1120 gui_mouse_correct();
1121#endif
1122
1123 /*
1124 * Update w_curswant if w_set_curswant has been set.
1125 * Postponed until here to avoid computing w_virtcol too often.
1126 */
1127 update_curswant();
1128
1129 /*
1130 * If we're invoked as ex, do a round of ex commands.
1131 * Otherwise, get and execute a normal mode command.
1132 */
1133 if (exmode_active)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001134 {
1135 if (noexmode) /* End of ":global/path/visual" commands */
1136 return;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001137 do_exmode(exmode_active == EXMODE_VIM);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001138 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001139 else
1140 normal_cmd(&oa, TRUE);
1141 }
1142}
1143
1144
1145#if defined(USE_XSMP) || defined(FEAT_GUI_MSWIN) || defined(PROTO)
1146/*
1147 * Exit, but leave behind swap files for modified buffers.
1148 */
1149 void
1150getout_preserve_modified(exitval)
1151 int exitval;
1152{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001153# if defined(SIGHUP) && defined(SIG_IGN)
1154 /* Ignore SIGHUP, because a dropped connection causes a read error, which
1155 * makes Vim exit and then handling SIGHUP causes various reentrance
1156 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001157 signal(SIGHUP, SIG_IGN);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001158# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001159
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001160 ml_close_notmod(); /* close all not-modified buffers */
1161 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
1162 ml_close_all(FALSE); /* close all memfiles, without deleting */
1163 getout(exitval); /* exit Vim properly */
1164}
1165#endif
1166
1167
1168/* Exit properly */
1169 void
1170getout(exitval)
1171 int exitval;
1172{
1173#ifdef FEAT_AUTOCMD
1174 buf_T *buf;
1175 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001176 tabpage_T *tp, *next_tp;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001177#endif
1178
1179 exiting = TRUE;
1180
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001181 /* When running in Ex mode an error causes us to exit with a non-zero exit
1182 * code. POSIX requires this, although it's not 100% clear from the
1183 * standard. */
1184 if (exmode_active)
1185 exitval += ex_exitval;
1186
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001187 /* Position the cursor on the last screen line, below all the text */
1188#ifdef FEAT_GUI
1189 if (!gui.in_use)
1190#endif
1191 windgoto((int)Rows - 1, 0);
1192
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +00001193#if defined(FEAT_EVAL) || defined(FEAT_SYN_HL)
1194 /* Optionally print hashtable efficiency. */
1195 hash_debug_results();
1196#endif
1197
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001198#ifdef FEAT_GUI
1199 msg_didany = FALSE;
1200#endif
1201
1202#ifdef FEAT_AUTOCMD
1203 /* Trigger BufWinLeave for all windows, but only once per buffer. */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001204# if defined FEAT_WINDOWS
1205 for (tp = first_tabpage; tp != NULL; tp = next_tp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001206 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001207 next_tp = tp->tp_next;
Bram Moolenaar238a5642006-02-21 22:12:05 +00001208 for (wp = (tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001209 ? firstwin : tp->tp_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001210 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001211 buf = wp->w_buffer;
1212 if (buf->b_changedtick != -1)
1213 {
1214 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001215 FALSE, buf);
Bram Moolenaarf740b292006-02-16 22:11:02 +00001216 buf->b_changedtick = -1; /* note that we did it already */
1217 /* start all over, autocommands may mess up the lists */
1218 next_tp = first_tabpage;
1219 break;
1220 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001221 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001222 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001223# else
1224 apply_autocmds(EVENT_BUFWINLEAVE, curbuf, curbuf->b_fname, FALSE, curbuf);
1225# endif
Bram Moolenaar33aec762006-01-22 23:30:12 +00001226
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001227 /* Trigger BufUnload for buffers that are loaded */
1228 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1229 if (buf->b_ml.ml_mfp != NULL)
1230 {
1231 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
1232 FALSE, buf);
1233 if (!buf_valid(buf)) /* autocmd may delete the buffer */
1234 break;
1235 }
1236 apply_autocmds(EVENT_VIMLEAVEPRE, NULL, NULL, FALSE, curbuf);
1237#endif
1238
1239#ifdef FEAT_VIMINFO
1240 if (*p_viminfo != NUL)
1241 /* Write out the registers, history, marks etc, to the viminfo file */
1242 write_viminfo(NULL, FALSE);
1243#endif
1244
1245#ifdef FEAT_AUTOCMD
1246 apply_autocmds(EVENT_VIMLEAVE, NULL, NULL, FALSE, curbuf);
1247#endif
1248
Bram Moolenaar05159a02005-02-26 23:04:13 +00001249#ifdef FEAT_PROFILE
1250 profile_dump();
1251#endif
1252
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001253 if (did_emsg
1254#ifdef FEAT_GUI
1255 || (gui.in_use && msg_didany && p_verbose > 0)
1256#endif
1257 )
1258 {
1259 /* give the user a chance to read the (error) message */
1260 no_wait_return = FALSE;
1261 wait_return(FALSE);
1262 }
1263
1264#ifdef FEAT_AUTOCMD
1265 /* Position the cursor again, the autocommands may have moved it */
1266# ifdef FEAT_GUI
1267 if (!gui.in_use)
1268# endif
1269 windgoto((int)Rows - 1, 0);
1270#endif
1271
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001272#ifdef FEAT_MZSCHEME
1273 mzscheme_end();
1274#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001275#ifdef FEAT_TCL
1276 tcl_end();
1277#endif
1278#ifdef FEAT_RUBY
1279 ruby_end();
1280#endif
1281#ifdef FEAT_PYTHON
1282 python_end();
1283#endif
1284#ifdef FEAT_PERL
1285 perl_end();
1286#endif
1287#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
1288 iconv_end();
1289#endif
1290#ifdef FEAT_NETBEANS_INTG
1291 netbeans_end();
1292#endif
1293
1294 mch_exit(exitval);
1295}
1296
1297/*
1298 * Get a (optional) count for a Vim argument.
1299 */
1300 static int
1301get_number_arg(p, idx, def)
1302 char_u *p; /* pointer to argument */
1303 int *idx; /* index in argument, is incremented */
1304 int def; /* default value */
1305{
1306 if (vim_isdigit(p[*idx]))
1307 {
1308 def = atoi((char *)&(p[*idx]));
1309 while (vim_isdigit(p[*idx]))
1310 *idx = *idx + 1;
1311 }
1312 return def;
1313}
1314
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001315#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1316/*
1317 * Setup to use the current locale (for ctype() and many other things).
1318 */
1319 static void
1320init_locale()
1321{
1322 setlocale(LC_ALL, "");
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001323# ifdef WIN32
1324 /* Apparently MS-Windows printf() may cause a crash when we give it 8-bit
1325 * text while it's expecting text in the current locale. This call avoids
1326 * that. */
1327 setlocale(LC_CTYPE, "C");
1328# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001329
1330# ifdef FEAT_GETTEXT
1331 {
1332 int mustfree = FALSE;
1333 char_u *p;
1334
1335# ifdef DYNAMIC_GETTEXT
1336 /* Initialize the gettext library */
1337 dyn_libintl_init(NULL);
1338# endif
1339 /* expand_env() doesn't work yet, because chartab[] is not initialized
1340 * yet, call vim_getenv() directly */
1341 p = vim_getenv((char_u *)"VIMRUNTIME", &mustfree);
1342 if (p != NULL && *p != NUL)
1343 {
1344 STRCPY(NameBuff, p);
1345 STRCAT(NameBuff, "/lang");
1346 bindtextdomain(VIMPACKAGE, (char *)NameBuff);
1347 }
1348 if (mustfree)
1349 vim_free(p);
1350 textdomain(VIMPACKAGE);
1351 }
1352# endif
1353}
1354#endif
1355
1356/*
1357 * Check for: [r][e][g][vi|vim|view][diff][ex[im]]
1358 * If the executable name starts with "r" we disable shell commands.
1359 * If the next character is "e" we run in Easy mode.
1360 * If the next character is "g" we run the GUI version.
1361 * If the next characters are "view" we start in readonly mode.
1362 * If the next characters are "diff" or "vimdiff" we start in diff mode.
1363 * If the next characters are "ex" we start in Ex mode. If it's followed
1364 * by "im" use improved Ex mode.
1365 */
1366 static void
1367parse_command_name(parmp)
1368 mparm_T *parmp;
1369{
1370 char_u *initstr;
1371
1372 initstr = gettail((char_u *)parmp->argv[0]);
1373
1374#ifdef MACOS_X_UNIX
1375 /* An issue has been seen when launching Vim in such a way that
1376 * $PWD/$ARGV[0] or $ARGV[0] is not the absolute path to the
1377 * executable or a symbolic link of it. Until this issue is resolved
1378 * we prohibit the GUI from being used.
1379 */
1380 if (STRCMP(initstr, parmp->argv[0]) == 0)
1381 disallow_gui = TRUE;
1382
1383 /* TODO: On MacOS X default to gui if argv[0] ends in:
Bram Moolenaar27dc1952006-03-15 23:06:44 +00001384 * /Vim.app/Contents/MacOS/Vim */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001385#endif
1386
1387#ifdef FEAT_EVAL
1388 set_vim_var_string(VV_PROGNAME, initstr, -1);
1389#endif
1390
1391 if (TOLOWER_ASC(initstr[0]) == 'r')
1392 {
1393 restricted = TRUE;
1394 ++initstr;
1395 }
1396
1397 /* Avoid using evim mode for "editor". */
1398 if (TOLOWER_ASC(initstr[0]) == 'e'
1399 && (TOLOWER_ASC(initstr[1]) == 'v'
1400 || TOLOWER_ASC(initstr[1]) == 'g'))
1401 {
1402#ifdef FEAT_GUI
1403 gui.starting = TRUE;
1404#endif
1405 parmp->evim_mode = TRUE;
1406 ++initstr;
1407 }
1408
1409 if (TOLOWER_ASC(initstr[0]) == 'g' || initstr[0] == 'k')
1410 {
1411 main_start_gui();
1412#ifdef FEAT_GUI
1413 ++initstr;
1414#endif
1415 }
1416
1417 if (STRNICMP(initstr, "view", 4) == 0)
1418 {
1419 readonlymode = TRUE;
1420 curbuf->b_p_ro = TRUE;
1421 p_uc = 10000; /* don't update very often */
1422 initstr += 4;
1423 }
1424 else if (STRNICMP(initstr, "vim", 3) == 0)
1425 initstr += 3;
1426
1427 /* Catch "[r][g]vimdiff" and "[r][g]viewdiff". */
1428 if (STRICMP(initstr, "diff") == 0)
1429 {
1430#ifdef FEAT_DIFF
1431 parmp->diff_mode = TRUE;
1432#else
1433 mch_errmsg(_("This Vim was not compiled with the diff feature."));
1434 mch_errmsg("\n");
1435 mch_exit(2);
1436#endif
1437 }
1438
1439 if (STRNICMP(initstr, "ex", 2) == 0)
1440 {
1441 if (STRNICMP(initstr + 2, "im", 2) == 0)
1442 exmode_active = EXMODE_VIM;
1443 else
1444 exmode_active = EXMODE_NORMAL;
1445 change_compatible(TRUE); /* set 'compatible' */
1446 }
1447}
1448
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001449/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00001450 * Get the name of the display, before gui_prepare() removes it from
1451 * argv[]. Used for the xterm-clipboard display.
1452 *
1453 * Also find the --server... arguments and --socketid
1454 */
1455/*ARGSUSED*/
1456 static void
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001457early_arg_scan(parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001458 mparm_T *parmp;
1459{
1460#if defined(FEAT_XCLIPBOARD) || defined(FEAT_CLIENTSERVER)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001461 int argc = parmp->argc;
1462 char **argv = parmp->argv;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001463 int i;
1464
1465 for (i = 1; i < argc; i++)
1466 {
1467 if (STRCMP(argv[i], "--") == 0)
1468 break;
1469# ifdef FEAT_XCLIPBOARD
1470 else if (STRICMP(argv[i], "-display") == 0
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001471# if defined(FEAT_GUI_GTK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001472 || STRICMP(argv[i], "--display") == 0
1473# endif
1474 )
1475 {
1476 if (i == argc - 1)
1477 mainerr_arg_missing((char_u *)argv[i]);
1478 xterm_display = argv[++i];
1479 }
1480# endif
1481# ifdef FEAT_CLIENTSERVER
1482 else if (STRICMP(argv[i], "--servername") == 0)
1483 {
1484 if (i == argc - 1)
1485 mainerr_arg_missing((char_u *)argv[i]);
1486 parmp->serverName_arg = (char_u *)argv[++i];
1487 }
Bram Moolenaareb94e552006-03-11 21:35:11 +00001488 else if (STRICMP(argv[i], "--serverlist") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001489 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001490 else if (STRNICMP(argv[i], "--remote", 8) == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001491 {
1492 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001493# ifdef FEAT_GUI
1494 if (strstr(argv[i], "-wait") != 0)
1495 /* don't fork() when starting the GUI to edit files ourself */
1496 gui.dofork = FALSE;
1497# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001498 }
1499# endif
1500# ifdef FEAT_GUI_GTK
1501 else if (STRICMP(argv[i], "--socketid") == 0)
1502 {
1503 unsigned int socket_id;
1504 int count;
1505
1506 if (i == argc - 1)
1507 mainerr_arg_missing((char_u *)argv[i]);
1508 if (STRNICMP(argv[i+1], "0x", 2) == 0)
1509 count = sscanf(&(argv[i + 1][2]), "%x", &socket_id);
1510 else
1511 count = sscanf(argv[i+1], "%u", &socket_id);
1512 if (count != 1)
1513 mainerr(ME_INVALID_ARG, (char_u *)argv[i]);
1514 else
1515 gtk_socket_id = socket_id;
1516 i++;
1517 }
1518 else if (STRICMP(argv[i], "--echo-wid") == 0)
1519 echo_wid_arg = TRUE;
1520# endif
1521 }
1522#endif
1523}
1524
1525/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001526 * Scan the command line arguments.
1527 */
1528 static void
1529command_line_scan(parmp)
1530 mparm_T *parmp;
1531{
1532 int argc = parmp->argc;
1533 char **argv = parmp->argv;
1534 int argv_idx; /* index in argv[n][] */
1535 int had_minmin = FALSE; /* found "--" argument */
1536 int want_argument; /* option argument with argument */
1537 int c;
Bram Moolenaar231334e2005-07-25 20:46:57 +00001538 char_u *p = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001539 long n;
1540
1541 --argc;
1542 ++argv;
1543 argv_idx = 1; /* active option letter is argv[0][argv_idx] */
1544 while (argc > 0)
1545 {
1546 /*
1547 * "+" or "+{number}" or "+/{pat}" or "+{command}" argument.
1548 */
1549 if (argv[0][0] == '+' && !had_minmin)
1550 {
1551 if (parmp->n_commands >= MAX_ARG_CMDS)
1552 mainerr(ME_EXTRA_CMD, NULL);
1553 argv_idx = -1; /* skip to next argument */
1554 if (argv[0][1] == NUL)
1555 parmp->commands[parmp->n_commands++] = (char_u *)"$";
1556 else
1557 parmp->commands[parmp->n_commands++] = (char_u *)&(argv[0][1]);
1558 }
1559
1560 /*
1561 * Optional argument.
1562 */
1563 else if (argv[0][0] == '-' && !had_minmin)
1564 {
1565 want_argument = FALSE;
1566 c = argv[0][argv_idx++];
1567#ifdef VMS
1568 /*
1569 * VMS only uses upper case command lines. Interpret "-X" as "-x"
1570 * and "-/X" as "-X".
1571 */
1572 if (c == '/')
1573 {
1574 c = argv[0][argv_idx++];
1575 c = TOUPPER_ASC(c);
1576 }
1577 else
1578 c = TOLOWER_ASC(c);
1579#endif
1580 switch (c)
1581 {
1582 case NUL: /* "vim -" read from stdin */
1583 /* "ex -" silent mode */
1584 if (exmode_active)
1585 silent_mode = TRUE;
1586 else
1587 {
1588 if (parmp->edit_type != EDIT_NONE)
1589 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1590 parmp->edit_type = EDIT_STDIN;
1591 read_cmd_fd = 2; /* read from stderr instead of stdin */
1592 }
1593 argv_idx = -1; /* skip to next argument */
1594 break;
1595
1596 case '-': /* "--" don't take any more option arguments */
1597 /* "--help" give help message */
1598 /* "--version" give version message */
1599 /* "--literal" take files literally */
1600 /* "--nofork" don't fork */
1601 /* "--noplugin[s]" skip plugins */
1602 /* "--cmd <cmd>" execute cmd before vimrc */
1603 if (STRICMP(argv[0] + argv_idx, "help") == 0)
1604 usage();
1605 else if (STRICMP(argv[0] + argv_idx, "version") == 0)
1606 {
1607 Columns = 80; /* need to init Columns */
1608 info_message = TRUE; /* use mch_msg(), not mch_errmsg() */
1609 list_version();
1610 msg_putchar('\n');
1611 msg_didout = FALSE;
1612 mch_exit(0);
1613 }
1614 else if (STRNICMP(argv[0] + argv_idx, "literal", 7) == 0)
1615 {
1616#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
1617 parmp->literal = TRUE;
1618#endif
1619 }
1620 else if (STRNICMP(argv[0] + argv_idx, "nofork", 6) == 0)
1621 {
1622#ifdef FEAT_GUI
1623 gui.dofork = FALSE; /* don't fork() when starting GUI */
1624#endif
1625 }
1626 else if (STRNICMP(argv[0] + argv_idx, "noplugin", 8) == 0)
1627 p_lpl = FALSE;
1628 else if (STRNICMP(argv[0] + argv_idx, "cmd", 3) == 0)
1629 {
1630 want_argument = TRUE;
1631 argv_idx += 3;
1632 }
1633#ifdef FEAT_CLIENTSERVER
1634 else if (STRNICMP(argv[0] + argv_idx, "serverlist", 10) == 0)
1635 ; /* already processed -- no arg */
1636 else if (STRNICMP(argv[0] + argv_idx, "servername", 10) == 0
1637 || STRNICMP(argv[0] + argv_idx, "serversend", 10) == 0)
1638 {
1639 /* already processed -- snatch the following arg */
1640 if (argc > 1)
1641 {
1642 --argc;
1643 ++argv;
1644 }
1645 }
1646#endif
1647#ifdef FEAT_GUI_GTK
1648 else if (STRNICMP(argv[0] + argv_idx, "socketid", 8) == 0)
1649 {
1650 /* already processed -- snatch the following arg */
1651 if (argc > 1)
1652 {
1653 --argc;
1654 ++argv;
1655 }
1656 }
1657 else if (STRNICMP(argv[0] + argv_idx, "echo-wid", 8) == 0)
1658 {
1659 /* already processed, skip */
1660 }
1661#endif
1662 else
1663 {
1664 if (argv[0][argv_idx])
1665 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
1666 had_minmin = TRUE;
1667 }
1668 if (!want_argument)
1669 argv_idx = -1; /* skip to next argument */
1670 break;
1671
1672 case 'A': /* "-A" start in Arabic mode */
1673#ifdef FEAT_ARABIC
1674 set_option_value((char_u *)"arabic", 1L, NULL, 0);
1675#else
1676 mch_errmsg(_(e_noarabic));
1677 mch_exit(2);
1678#endif
1679 break;
1680
1681 case 'b': /* "-b" binary mode */
Bram Moolenaar231334e2005-07-25 20:46:57 +00001682 /* Needs to be effective before expanding file names, because
1683 * for Win32 this makes us edit a shortcut file itself,
1684 * instead of the file it links to. */
1685 set_options_bin(curbuf->b_p_bin, 1, 0);
1686 curbuf->b_p_bin = 1; /* binary file I/O */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001687 break;
1688
1689 case 'C': /* "-C" Compatible */
1690 change_compatible(TRUE);
1691 break;
1692
1693 case 'e': /* "-e" Ex mode */
1694 exmode_active = EXMODE_NORMAL;
1695 break;
1696
1697 case 'E': /* "-E" Improved Ex mode */
1698 exmode_active = EXMODE_VIM;
1699 break;
1700
1701 case 'f': /* "-f" GUI: run in foreground. Amiga: open
1702 window directly, not with newcli */
1703#ifdef FEAT_GUI
1704 gui.dofork = FALSE; /* don't fork() when starting GUI */
1705#endif
1706 break;
1707
1708 case 'g': /* "-g" start GUI */
1709 main_start_gui();
1710 break;
1711
1712 case 'F': /* "-F" start in Farsi mode: rl + fkmap set */
1713#ifdef FEAT_FKMAP
1714 curwin->w_p_rl = p_fkmap = TRUE;
1715#else
1716 mch_errmsg(_(e_nofarsi));
1717 mch_exit(2);
1718#endif
1719 break;
1720
1721 case 'h': /* "-h" give help message */
1722#ifdef FEAT_GUI_GNOME
1723 /* Tell usage() to exit for "gvim". */
1724 gui.starting = FALSE;
1725#endif
1726 usage();
1727 break;
1728
1729 case 'H': /* "-H" start in Hebrew mode: rl + hkmap set */
1730#ifdef FEAT_RIGHTLEFT
1731 curwin->w_p_rl = p_hkmap = TRUE;
1732#else
1733 mch_errmsg(_(e_nohebrew));
1734 mch_exit(2);
1735#endif
1736 break;
1737
1738 case 'l': /* "-l" lisp mode, 'lisp' and 'showmatch' on */
1739#ifdef FEAT_LISP
1740 set_option_value((char_u *)"lisp", 1L, NULL, 0);
1741 p_sm = TRUE;
1742#endif
1743 break;
1744
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001745 case 'M': /* "-M" no changes or writing of files */
1746 reset_modifiable();
1747 /* FALLTHROUGH */
1748
1749 case 'm': /* "-m" no writing of files */
1750 p_write = FALSE;
1751 break;
1752
1753 case 'y': /* "-y" easy mode */
1754#ifdef FEAT_GUI
1755 gui.starting = TRUE; /* start GUI a bit later */
1756#endif
1757 parmp->evim_mode = TRUE;
1758 break;
1759
1760 case 'N': /* "-N" Nocompatible */
1761 change_compatible(FALSE);
1762 break;
1763
1764 case 'n': /* "-n" no swap file */
1765 parmp->no_swap_file = TRUE;
1766 break;
1767
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001768 case 'p': /* "-p[N]" open N tab pages */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00001769#ifdef TARGET_API_MAC_OSX
1770 /* For some reason on MacOS X, an argument like:
1771 -psn_0_10223617 is passed in when invoke from Finder
1772 or with the 'open' command */
1773 if (argv[0][argv_idx] == 's')
1774 {
1775 argv_idx = -1; /* bypass full -psn */
1776 main_start_gui();
1777 break;
1778 }
1779#endif
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001780#ifdef FEAT_WINDOWS
1781 /* default is 0: open window for each file */
1782 parmp->window_count = get_number_arg((char_u *)argv[0],
1783 &argv_idx, 0);
1784 parmp->window_layout = WIN_TABS;
1785#endif
1786 break;
1787
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001788 case 'o': /* "-o[N]" open N horizontal split windows */
1789#ifdef FEAT_WINDOWS
1790 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00001791 parmp->window_count = get_number_arg((char_u *)argv[0],
1792 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001793 parmp->window_layout = WIN_HOR;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001794#endif
1795 break;
1796
1797 case 'O': /* "-O[N]" open N vertical split windows */
1798#if defined(FEAT_VERTSPLIT) && defined(FEAT_WINDOWS)
1799 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00001800 parmp->window_count = get_number_arg((char_u *)argv[0],
1801 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001802 parmp->window_layout = WIN_VER;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001803#endif
1804 break;
1805
1806#ifdef FEAT_QUICKFIX
1807 case 'q': /* "-q" QuickFix mode */
1808 if (parmp->edit_type != EDIT_NONE)
1809 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1810 parmp->edit_type = EDIT_QF;
1811 if (argv[0][argv_idx]) /* "-q{errorfile}" */
1812 {
1813 parmp->use_ef = (char_u *)argv[0] + argv_idx;
1814 argv_idx = -1;
1815 }
1816 else if (argc > 1) /* "-q {errorfile}" */
1817 want_argument = TRUE;
1818 break;
1819#endif
1820
1821 case 'R': /* "-R" readonly mode */
1822 readonlymode = TRUE;
1823 curbuf->b_p_ro = TRUE;
1824 p_uc = 10000; /* don't update very often */
1825 break;
1826
1827 case 'r': /* "-r" recovery mode */
1828 case 'L': /* "-L" recovery mode */
1829 recoverymode = 1;
1830 break;
1831
1832 case 's':
1833 if (exmode_active) /* "-s" silent (batch) mode */
1834 silent_mode = TRUE;
1835 else /* "-s {scriptin}" read from script file */
1836 want_argument = TRUE;
1837 break;
1838
1839 case 't': /* "-t {tag}" or "-t{tag}" jump to tag */
1840 if (parmp->edit_type != EDIT_NONE)
1841 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1842 parmp->edit_type = EDIT_TAG;
1843 if (argv[0][argv_idx]) /* "-t{tag}" */
1844 {
1845 parmp->tagname = (char_u *)argv[0] + argv_idx;
1846 argv_idx = -1;
1847 }
1848 else /* "-t {tag}" */
1849 want_argument = TRUE;
1850 break;
1851
1852#ifdef FEAT_EVAL
1853 case 'D': /* "-D" Debugging */
1854 parmp->use_debug_break_level = 9999;
1855 break;
1856#endif
1857#ifdef FEAT_DIFF
1858 case 'd': /* "-d" 'diff' */
1859# ifdef AMIGA
1860 /* check for "-dev {device}" */
1861 if (argv[0][argv_idx] == 'e' && argv[0][argv_idx + 1] == 'v')
1862 want_argument = TRUE;
1863 else
1864# endif
1865 parmp->diff_mode = TRUE;
1866 break;
1867#endif
1868 case 'V': /* "-V{N}" Verbose level */
1869 /* default is 10: a little bit verbose */
1870 p_verbose = get_number_arg((char_u *)argv[0], &argv_idx, 10);
1871 if (argv[0][argv_idx] != NUL)
1872 {
1873 set_option_value((char_u *)"verbosefile", 0L,
1874 (char_u *)argv[0] + argv_idx, 0);
1875 argv_idx = STRLEN(argv[0]);
1876 }
1877 break;
1878
1879 case 'v': /* "-v" Vi-mode (as if called "vi") */
1880 exmode_active = 0;
1881#ifdef FEAT_GUI
1882 gui.starting = FALSE; /* don't start GUI */
1883#endif
1884 break;
1885
1886 case 'w': /* "-w{number}" set window height */
1887 /* "-w {scriptout}" write to script */
1888 if (vim_isdigit(((char_u *)argv[0])[argv_idx]))
1889 {
1890 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
1891 set_option_value((char_u *)"window", n, NULL, 0);
1892 break;
1893 }
1894 want_argument = TRUE;
1895 break;
1896
1897#ifdef FEAT_CRYPT
1898 case 'x': /* "-x" encrypted reading/writing of files */
1899 parmp->ask_for_key = TRUE;
1900 break;
1901#endif
1902
1903 case 'X': /* "-X" don't connect to X server */
1904#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
1905 x_no_connect = TRUE;
1906#endif
1907 break;
1908
1909 case 'Z': /* "-Z" restricted mode */
1910 restricted = TRUE;
1911 break;
1912
1913 case 'c': /* "-c{command}" or "-c {command}" execute
1914 command */
1915 if (argv[0][argv_idx] != NUL)
1916 {
1917 if (parmp->n_commands >= MAX_ARG_CMDS)
1918 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar231334e2005-07-25 20:46:57 +00001919 parmp->commands[parmp->n_commands++] = (char_u *)argv[0]
1920 + argv_idx;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001921 argv_idx = -1;
1922 break;
1923 }
1924 /*FALLTHROUGH*/
1925 case 'S': /* "-S {file}" execute Vim script */
1926 case 'i': /* "-i {viminfo}" use for viminfo */
1927#ifndef FEAT_DIFF
1928 case 'd': /* "-d {device}" device (for Amiga) */
1929#endif
1930 case 'T': /* "-T {terminal}" terminal name */
1931 case 'u': /* "-u {vimrc}" vim inits file */
1932 case 'U': /* "-U {gvimrc}" gvim inits file */
1933 case 'W': /* "-W {scriptout}" overwrite */
1934#ifdef FEAT_GUI_W32
1935 case 'P': /* "-P {parent title}" MDI parent */
1936#endif
1937 want_argument = TRUE;
1938 break;
1939
1940 default:
1941 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
1942 }
1943
1944 /*
1945 * Handle option arguments with argument.
1946 */
1947 if (want_argument)
1948 {
1949 /*
1950 * Check for garbage immediately after the option letter.
1951 */
1952 if (argv[0][argv_idx] != NUL)
1953 mainerr(ME_GARBAGE, (char_u *)argv[0]);
1954
1955 --argc;
1956 if (argc < 1 && c != 'S')
1957 mainerr_arg_missing((char_u *)argv[0]);
1958 ++argv;
1959 argv_idx = -1;
1960
1961 switch (c)
1962 {
1963 case 'c': /* "-c {command}" execute command */
1964 case 'S': /* "-S {file}" execute Vim script */
1965 if (parmp->n_commands >= MAX_ARG_CMDS)
1966 mainerr(ME_EXTRA_CMD, NULL);
1967 if (c == 'S')
1968 {
1969 char *a;
1970
1971 if (argc < 1)
1972 /* "-S" without argument: use default session file
1973 * name. */
1974 a = SESSION_FILE;
1975 else if (argv[0][0] == '-')
1976 {
1977 /* "-S" followed by another option: use default
1978 * session file name. */
1979 a = SESSION_FILE;
1980 ++argc;
1981 --argv;
1982 }
1983 else
1984 a = argv[0];
1985 p = alloc((unsigned)(STRLEN(a) + 4));
1986 if (p == NULL)
1987 mch_exit(2);
1988 sprintf((char *)p, "so %s", a);
1989 parmp->cmds_tofree[parmp->n_commands] = TRUE;
1990 parmp->commands[parmp->n_commands++] = p;
1991 }
1992 else
Bram Moolenaar231334e2005-07-25 20:46:57 +00001993 parmp->commands[parmp->n_commands++] =
1994 (char_u *)argv[0];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001995 break;
1996
1997 case '-': /* "--cmd {command}" execute command */
1998 if (parmp->n_pre_commands >= MAX_ARG_CMDS)
1999 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar231334e2005-07-25 20:46:57 +00002000 parmp->pre_commands[parmp->n_pre_commands++] =
2001 (char_u *)argv[0];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002002 break;
2003
2004 /* case 'd': -d {device} is handled in mch_check_win() for the
2005 * Amiga */
2006
2007#ifdef FEAT_QUICKFIX
2008 case 'q': /* "-q {errorfile}" QuickFix mode */
2009 parmp->use_ef = (char_u *)argv[0];
2010 break;
2011#endif
2012
2013 case 'i': /* "-i {viminfo}" use for viminfo */
2014 use_viminfo = (char_u *)argv[0];
2015 break;
2016
2017 case 's': /* "-s {scriptin}" read from script file */
2018 if (scriptin[0] != NULL)
2019 {
2020scripterror:
2021 mch_errmsg(_("Attempt to open script file again: \""));
2022 mch_errmsg(argv[-1]);
2023 mch_errmsg(" ");
2024 mch_errmsg(argv[0]);
2025 mch_errmsg("\"\n");
2026 mch_exit(2);
2027 }
2028 if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL)
2029 {
2030 mch_errmsg(_("Cannot open for reading: \""));
2031 mch_errmsg(argv[0]);
2032 mch_errmsg("\"\n");
2033 mch_exit(2);
2034 }
2035 if (save_typebuf() == FAIL)
2036 mch_exit(2); /* out of memory */
2037 break;
2038
2039 case 't': /* "-t {tag}" */
2040 parmp->tagname = (char_u *)argv[0];
2041 break;
2042
2043 case 'T': /* "-T {terminal}" terminal name */
2044 /*
2045 * The -T term argument is always available and when
2046 * HAVE_TERMLIB is supported it overrides the environment
2047 * variable TERM.
2048 */
2049#ifdef FEAT_GUI
2050 if (term_is_gui((char_u *)argv[0]))
2051 gui.starting = TRUE; /* start GUI a bit later */
2052 else
2053#endif
2054 parmp->term = (char_u *)argv[0];
2055 break;
2056
2057 case 'u': /* "-u {vimrc}" vim inits file */
2058 parmp->use_vimrc = (char_u *)argv[0];
2059 break;
2060
2061 case 'U': /* "-U {gvimrc}" gvim inits file */
2062#ifdef FEAT_GUI
2063 use_gvimrc = (char_u *)argv[0];
2064#endif
2065 break;
2066
2067 case 'w': /* "-w {nr}" 'window' value */
2068 /* "-w {scriptout}" append to script file */
2069 if (vim_isdigit(*((char_u *)argv[0])))
2070 {
2071 argv_idx = 0;
2072 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2073 set_option_value((char_u *)"window", n, NULL, 0);
2074 argv_idx = -1;
2075 break;
2076 }
2077 /*FALLTHROUGH*/
2078 case 'W': /* "-W {scriptout}" overwrite script file */
2079 if (scriptout != NULL)
2080 goto scripterror;
2081 if ((scriptout = mch_fopen(argv[0],
2082 c == 'w' ? APPENDBIN : WRITEBIN)) == NULL)
2083 {
2084 mch_errmsg(_("Cannot open for script output: \""));
2085 mch_errmsg(argv[0]);
2086 mch_errmsg("\"\n");
2087 mch_exit(2);
2088 }
2089 break;
2090
2091#ifdef FEAT_GUI_W32
2092 case 'P': /* "-P {parent title}" MDI parent */
2093 gui_mch_set_parent(argv[0]);
2094 break;
2095#endif
2096 }
2097 }
2098 }
2099
2100 /*
2101 * File name argument.
2102 */
2103 else
2104 {
2105 argv_idx = -1; /* skip to next argument */
2106
2107 /* Check for only one type of editing. */
2108 if (parmp->edit_type != EDIT_NONE && parmp->edit_type != EDIT_FILE)
2109 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2110 parmp->edit_type = EDIT_FILE;
2111
2112#ifdef MSWIN
2113 /* Remember if the argument was a full path before changing
2114 * slashes to backslashes. */
2115 if (argv[0][0] != NUL && argv[0][1] == ':' && argv[0][2] == '\\')
2116 parmp->full_path = TRUE;
2117#endif
2118
2119 /* Add the file to the global argument list. */
2120 if (ga_grow(&global_alist.al_ga, 1) == FAIL
2121 || (p = vim_strsave((char_u *)argv[0])) == NULL)
2122 mch_exit(2);
2123#ifdef FEAT_DIFF
2124 if (parmp->diff_mode && mch_isdir(p) && GARGCOUNT > 0
2125 && !mch_isdir(alist_name(&GARGLIST[0])))
2126 {
2127 char_u *r;
2128
2129 r = concat_fnames(p, gettail(alist_name(&GARGLIST[0])), TRUE);
2130 if (r != NULL)
2131 {
2132 vim_free(p);
2133 p = r;
2134 }
2135 }
2136#endif
2137#if defined(__CYGWIN32__) && !defined(WIN32)
2138 /*
2139 * If vim is invoked by non-Cygwin tools, convert away any
2140 * DOS paths, so things like .swp files are created correctly.
2141 * Look for evidence of non-Cygwin paths before we bother.
2142 * This is only for when using the Unix files.
2143 */
2144 if (strpbrk(p, "\\:") != NULL)
2145 {
2146 char posix_path[PATH_MAX];
2147
2148 cygwin_conv_to_posix_path(p, posix_path);
2149 vim_free(p);
2150 p = vim_strsave(posix_path);
2151 if (p == NULL)
2152 mch_exit(2);
2153 }
2154#endif
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002155
2156#ifdef USE_FNAME_CASE
2157 /* Make the case of the file name match the actual file. */
2158 fname_case(p, 0);
2159#endif
2160
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002161 alist_add(&global_alist, p,
2162#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
Bram Moolenaar231334e2005-07-25 20:46:57 +00002163 parmp->literal ? 2 : 0 /* add buffer nr after exp. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002164#else
2165 2 /* add buffer number now and use curbuf */
2166#endif
2167 );
2168
2169#if defined(FEAT_MBYTE) && defined(WIN32)
2170 {
2171 extern void used_file_arg(char *, int, int);
2172
2173 /* Remember this argument has been added to the argument list.
2174 * Needed when 'encoding' is changed. */
2175 used_file_arg(argv[0], parmp->literal, parmp->full_path);
2176 }
2177#endif
2178 }
2179
2180 /*
2181 * If there are no more letters after the current "-", go to next
2182 * argument. argv_idx is set to -1 when the current argument is to be
2183 * skipped.
2184 */
2185 if (argv_idx <= 0 || argv[0][argv_idx] == NUL)
2186 {
2187 --argc;
2188 ++argv;
2189 argv_idx = 1;
2190 }
2191 }
2192}
2193
2194/*
2195 * Print a warning if stdout is not a terminal.
2196 * When starting in Ex mode and commands come from a file, set Silent mode.
2197 */
2198 static void
2199check_tty(parmp)
2200 mparm_T *parmp;
2201{
2202 int input_isatty; /* is active input a terminal? */
2203
2204 input_isatty = mch_input_isatty();
2205 if (exmode_active)
2206 {
2207 if (!input_isatty)
2208 silent_mode = TRUE;
2209 }
2210 else if (parmp->want_full_screen && (!parmp->stdout_isatty || !input_isatty)
2211#ifdef FEAT_GUI
2212 /* don't want the delay when started from the desktop */
2213 && !gui.starting
2214#endif
2215 )
2216 {
2217#ifdef NBDEBUG
2218 /*
2219 * This shouldn't be necessary. But if I run netbeans with the log
2220 * output coming to the console and XOpenDisplay fails, I get vim
2221 * trying to start with input/output to my console tty. This fills my
2222 * input buffer so fast I can't even kill the process in under 2
2223 * minutes (and it beeps continuosly the whole time :-)
2224 */
2225 if (usingNetbeans && (!parmp->stdout_isatty || !input_isatty))
2226 {
2227 mch_errmsg(_("Vim: Error: Failure to start gvim from NetBeans\n"));
2228 exit(1);
2229 }
2230#endif
2231 if (!parmp->stdout_isatty)
2232 mch_errmsg(_("Vim: Warning: Output is not to a terminal\n"));
2233 if (!input_isatty)
2234 mch_errmsg(_("Vim: Warning: Input is not from a terminal\n"));
2235 out_flush();
2236 if (scriptin[0] == NULL)
2237 ui_delay(2000L, TRUE);
2238 TIME_MSG("Warning delay");
2239 }
2240}
2241
2242/*
2243 * Read text from stdin.
2244 */
2245 static void
2246read_stdin()
2247{
2248 int i;
2249
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002250#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002251 /* When getting the ATTENTION prompt here, use a dialog */
2252 swap_exists_action = SEA_DIALOG;
2253#endif
2254 no_wait_return = TRUE;
2255 i = msg_didany;
2256 set_buflisted(TRUE);
2257 (void)open_buffer(TRUE, NULL); /* create memfile and read file */
2258 no_wait_return = FALSE;
2259 msg_didany = i;
2260 TIME_MSG("reading stdin");
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002261#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002262 check_swap_exists_action();
2263#endif
2264#if !(defined(AMIGA) || defined(MACOS))
2265 /*
2266 * Close stdin and dup it from stderr. Required for GPM to work
2267 * properly, and for running external commands.
2268 * Is there any other system that cannot do this?
2269 */
2270 close(0);
2271 dup(2);
2272#endif
2273}
2274
2275/*
2276 * Create the requested number of windows and edit buffers in them.
2277 * Also does recovery if "recoverymode" set.
2278 */
2279/*ARGSUSED*/
2280 static void
2281create_windows(parmp)
2282 mparm_T *parmp;
2283{
2284#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002285 int rewind;
2286 int done = 0;
2287
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002288 /*
2289 * Create the number of windows that was requested.
2290 */
2291 if (parmp->window_count == -1) /* was not set */
2292 parmp->window_count = 1;
2293 if (parmp->window_count == 0)
2294 parmp->window_count = GARGCOUNT;
2295 if (parmp->window_count > 1)
2296 {
2297 /* Don't change the windows if there was a command in .vimrc that
2298 * already split some windows */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002299 if (parmp->window_layout == 0)
2300 parmp->window_layout = WIN_HOR;
2301 if (parmp->window_layout == WIN_TABS)
2302 {
2303 parmp->window_count = make_tabpages(parmp->window_count);
2304 TIME_MSG("making tab pages");
2305 }
2306 else if (firstwin->w_next == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002307 {
2308 parmp->window_count = make_windows(parmp->window_count,
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002309 parmp->window_layout == WIN_VER);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002310 TIME_MSG("making windows");
2311 }
2312 else
2313 parmp->window_count = win_count();
2314 }
2315 else
2316 parmp->window_count = 1;
2317#endif
2318
2319 if (recoverymode) /* do recover */
2320 {
2321 msg_scroll = TRUE; /* scroll message up */
2322 ml_recover();
2323 if (curbuf->b_ml.ml_mfp == NULL) /* failed */
2324 getout(1);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002325 do_modelines(0); /* do modelines */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002326 }
2327 else
2328 {
2329 /*
2330 * Open a buffer for windows that don't have one yet.
2331 * Commands in the .vimrc might have loaded a file or split the window.
2332 * Watch out for autocommands that delete a window.
2333 */
2334#ifdef FEAT_AUTOCMD
2335 /*
2336 * Don't execute Win/Buf Enter/Leave autocommands here
2337 */
2338 ++autocmd_no_enter;
2339 ++autocmd_no_leave;
2340#endif
2341#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002342 rewind = TRUE;
2343 while (done++ < 1000)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002344 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002345 if (rewind)
2346 {
2347 if (parmp->window_layout == WIN_TABS)
2348 goto_tabpage(1);
2349 else
2350 curwin = firstwin;
2351 }
2352 else if (parmp->window_layout == WIN_TABS)
2353 {
2354 if (curtab->tp_next == NULL)
2355 break;
2356 goto_tabpage(0);
2357 }
2358 else
2359 {
2360 if (curwin->w_next == NULL)
2361 break;
2362 curwin = curwin->w_next;
2363 }
2364 rewind = FALSE;
2365#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002366 curbuf = curwin->w_buffer;
2367 if (curbuf->b_ml.ml_mfp == NULL)
2368 {
2369#ifdef FEAT_FOLDING
2370 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2371 if (p_fdls >= 0)
2372 curwin->w_p_fdl = p_fdls;
2373#endif
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002374#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002375 /* When getting the ATTENTION prompt here, use a dialog */
2376 swap_exists_action = SEA_DIALOG;
2377#endif
2378 set_buflisted(TRUE);
2379 (void)open_buffer(FALSE, NULL); /* create memfile, read file */
2380
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002381#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002382 check_swap_exists_action();
2383#endif
2384#ifdef FEAT_AUTOCMD
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002385 rewind = TRUE; /* start again */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002386#endif
2387 }
2388#ifdef FEAT_WINDOWS
2389 ui_breakcheck();
2390 if (got_int)
2391 {
2392 (void)vgetc(); /* only break the file loading, not the rest */
2393 break;
2394 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002395 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002396#endif
2397#ifdef FEAT_WINDOWS
2398 if (parmp->window_layout == WIN_TABS)
2399 goto_tabpage(1);
2400 else
2401 curwin = firstwin;
2402 curbuf = curwin->w_buffer;
2403#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002404#ifdef FEAT_AUTOCMD
2405 --autocmd_no_enter;
2406 --autocmd_no_leave;
2407#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002408 }
2409}
2410
2411#ifdef FEAT_WINDOWS
2412 /*
2413 * If opened more than one window, start editing files in the other
2414 * windows. make_windows() has already opened the windows.
2415 */
2416 static void
2417edit_buffers(parmp)
2418 mparm_T *parmp;
2419{
2420 int arg_idx; /* index in argument list */
2421 int i;
2422
2423# ifdef FEAT_AUTOCMD
2424 /*
2425 * Don't execute Win/Buf Enter/Leave autocommands here
2426 */
2427 ++autocmd_no_enter;
2428 ++autocmd_no_leave;
2429# endif
2430 arg_idx = 1;
2431 for (i = 1; i < parmp->window_count; ++i)
2432 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002433 if (parmp->window_layout == WIN_TABS)
2434 {
2435 if (curtab->tp_next == NULL) /* just checking */
2436 break;
2437 goto_tabpage(0);
2438 }
2439 else
2440 {
2441 if (curwin->w_next == NULL) /* just checking */
2442 break;
2443 win_enter(curwin->w_next, FALSE);
2444 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002445
2446 /* Only open the file if there is no file in this window yet (that can
2447 * happen when .vimrc contains ":sall") */
2448 if (curbuf == firstwin->w_buffer || curbuf->b_ffname == NULL)
2449 {
2450 curwin->w_arg_idx = arg_idx;
2451 /* edit file from arg list, if there is one */
2452 (void)do_ecmd(0, arg_idx < GARGCOUNT
2453 ? alist_name(&GARGLIST[arg_idx]) : NULL,
2454 NULL, NULL, ECMD_LASTL, ECMD_HIDE);
2455 if (arg_idx == GARGCOUNT - 1)
2456 arg_had_last = TRUE;
2457 ++arg_idx;
2458 }
2459 ui_breakcheck();
2460 if (got_int)
2461 {
2462 (void)vgetc(); /* only break the file loading, not the rest */
2463 break;
2464 }
2465 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002466
2467 if (parmp->window_layout == WIN_TABS)
2468 goto_tabpage(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002469# ifdef FEAT_AUTOCMD
2470 --autocmd_no_enter;
2471# endif
2472 win_enter(firstwin, FALSE); /* back to first window */
2473# ifdef FEAT_AUTOCMD
2474 --autocmd_no_leave;
2475# endif
2476 TIME_MSG("editing files in windows");
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002477 if (parmp->window_count > 1 && parmp->window_layout != WIN_TABS)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002478 win_equal(curwin, FALSE, 'b'); /* adjust heights */
2479}
2480#endif /* FEAT_WINDOWS */
2481
2482/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002483 * Execute the commands from --cmd arguments "cmds[cnt]".
2484 */
2485 static void
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002486exe_pre_commands(parmp)
2487 mparm_T *parmp;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002488{
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002489 char_u **cmds = parmp->pre_commands;
2490 int cnt = parmp->n_pre_commands;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002491 int i;
2492
2493 if (cnt > 0)
2494 {
2495 curwin->w_cursor.lnum = 0; /* just in case.. */
2496 sourcing_name = (char_u *)_("pre-vimrc command line");
2497# ifdef FEAT_EVAL
2498 current_SID = SID_CMDARG;
2499# endif
2500 for (i = 0; i < cnt; ++i)
2501 do_cmdline_cmd(cmds[i]);
2502 sourcing_name = NULL;
2503# ifdef FEAT_EVAL
2504 current_SID = 0;
2505# endif
2506 TIME_MSG("--cmd commands");
2507 }
2508}
2509
2510/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002511 * Execute "+", "-c" and "-S" arguments.
2512 */
2513 static void
2514exe_commands(parmp)
2515 mparm_T *parmp;
2516{
2517 int i;
2518
2519 /*
2520 * We start commands on line 0, make "vim +/pat file" match a
2521 * pattern on line 1. But don't move the cursor when an autocommand
2522 * with g`" was used.
2523 */
2524 msg_scroll = TRUE;
2525 if (parmp->tagname == NULL && curwin->w_cursor.lnum <= 1)
2526 curwin->w_cursor.lnum = 0;
2527 sourcing_name = (char_u *)"command line";
2528#ifdef FEAT_EVAL
2529 current_SID = SID_CARG;
2530#endif
2531 for (i = 0; i < parmp->n_commands; ++i)
2532 {
2533 do_cmdline_cmd(parmp->commands[i]);
2534 if (parmp->cmds_tofree[i])
2535 vim_free(parmp->commands[i]);
2536 }
2537 sourcing_name = NULL;
2538#ifdef FEAT_EVAL
2539 current_SID = 0;
2540#endif
2541 if (curwin->w_cursor.lnum == 0)
2542 curwin->w_cursor.lnum = 1;
2543
2544 if (!exmode_active)
2545 msg_scroll = FALSE;
2546
2547#ifdef FEAT_QUICKFIX
2548 /* When started with "-q errorfile" jump to first error again. */
2549 if (parmp->edit_type == EDIT_QF)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002550 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002551#endif
2552 TIME_MSG("executing command arguments");
2553}
2554
2555/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002556 * Source startup scripts.
2557 */
2558 static void
2559source_startup_scripts(parmp)
2560 mparm_T *parmp;
2561{
2562 int i;
2563
2564 /*
2565 * For "evim" source evim.vim first of all, so that the user can overrule
2566 * any things he doesn't like.
2567 */
2568 if (parmp->evim_mode)
2569 {
2570 (void)do_source((char_u *)EVIM_FILE, FALSE, FALSE);
2571 TIME_MSG("source evim file");
2572 }
2573
2574 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002575 * If -u argument given, use only the initializations from that file and
Bram Moolenaar58d98232005-07-23 22:25:46 +00002576 * nothing else.
2577 */
2578 if (parmp->use_vimrc != NULL)
2579 {
Bram Moolenaar231334e2005-07-25 20:46:57 +00002580 if (STRCMP(parmp->use_vimrc, "NONE") == 0
2581 || STRCMP(parmp->use_vimrc, "NORC") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002582 {
2583#ifdef FEAT_GUI
2584 if (use_gvimrc == NULL) /* don't load gvimrc either */
2585 use_gvimrc = parmp->use_vimrc;
2586#endif
2587 if (parmp->use_vimrc[2] == 'N')
2588 p_lpl = FALSE; /* don't load plugins either */
2589 }
2590 else
2591 {
2592 if (do_source(parmp->use_vimrc, FALSE, FALSE) != OK)
2593 EMSG2(_("E282: Cannot read from \"%s\""), parmp->use_vimrc);
2594 }
2595 }
2596 else if (!silent_mode)
2597 {
2598#ifdef AMIGA
2599 struct Process *proc = (struct Process *)FindTask(0L);
2600 APTR save_winptr = proc->pr_WindowPtr;
2601
2602 /* Avoid a requester here for a volume that doesn't exist. */
2603 proc->pr_WindowPtr = (APTR)-1L;
2604#endif
2605
2606 /*
2607 * Get system wide defaults, if the file name is defined.
2608 */
2609#ifdef SYS_VIMRC_FILE
2610 (void)do_source((char_u *)SYS_VIMRC_FILE, FALSE, FALSE);
2611#endif
Bram Moolenaar1056d982006-03-09 22:37:52 +00002612#ifdef MACOS_X
2613 (void)do_source((char_u *)"$VIMRUNTIME/macmap.vim", FALSE, FALSE);
2614#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00002615
2616 /*
2617 * Try to read initialization commands from the following places:
2618 * - environment variable VIMINIT
2619 * - user vimrc file (s:.vimrc for Amiga, ~/.vimrc otherwise)
2620 * - second user vimrc file ($VIM/.vimrc for Dos)
2621 * - environment variable EXINIT
2622 * - user exrc file (s:.exrc for Amiga, ~/.exrc otherwise)
2623 * - second user exrc file ($VIM/.exrc for Dos)
2624 * The first that exists is used, the rest is ignored.
2625 */
2626 if (process_env((char_u *)"VIMINIT", TRUE) != OK)
2627 {
2628 if (do_source((char_u *)USR_VIMRC_FILE, TRUE, TRUE) == FAIL
2629#ifdef USR_VIMRC_FILE2
2630 && do_source((char_u *)USR_VIMRC_FILE2, TRUE, TRUE) == FAIL
2631#endif
2632#ifdef USR_VIMRC_FILE3
2633 && do_source((char_u *)USR_VIMRC_FILE3, TRUE, TRUE) == FAIL
2634#endif
2635 && process_env((char_u *)"EXINIT", FALSE) == FAIL
2636 && do_source((char_u *)USR_EXRC_FILE, FALSE, FALSE) == FAIL)
2637 {
2638#ifdef USR_EXRC_FILE2
2639 (void)do_source((char_u *)USR_EXRC_FILE2, FALSE, FALSE);
2640#endif
2641 }
2642 }
2643
2644 /*
2645 * Read initialization commands from ".vimrc" or ".exrc" in current
2646 * directory. This is only done if the 'exrc' option is set.
2647 * Because of security reasons we disallow shell and write commands
2648 * now, except for unix if the file is owned by the user or 'secure'
2649 * option has been reset in environment of global ".exrc" or ".vimrc".
2650 * Only do this if VIMRC_FILE is not the same as USR_VIMRC_FILE or
2651 * SYS_VIMRC_FILE.
2652 */
2653 if (p_exrc)
2654 {
2655#if defined(UNIX) || defined(VMS)
2656 /* If ".vimrc" file is not owned by user, set 'secure' mode. */
2657 if (!file_owned(VIMRC_FILE))
2658#endif
2659 secure = p_secure;
2660
2661 i = FAIL;
2662 if (fullpathcmp((char_u *)USR_VIMRC_FILE,
2663 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
2664#ifdef USR_VIMRC_FILE2
2665 && fullpathcmp((char_u *)USR_VIMRC_FILE2,
2666 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
2667#endif
2668#ifdef USR_VIMRC_FILE3
2669 && fullpathcmp((char_u *)USR_VIMRC_FILE3,
2670 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
2671#endif
2672#ifdef SYS_VIMRC_FILE
2673 && fullpathcmp((char_u *)SYS_VIMRC_FILE,
2674 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
2675#endif
2676 )
2677 i = do_source((char_u *)VIMRC_FILE, TRUE, TRUE);
2678
2679 if (i == FAIL)
2680 {
2681#if defined(UNIX) || defined(VMS)
2682 /* if ".exrc" is not owned by user set 'secure' mode */
2683 if (!file_owned(EXRC_FILE))
2684 secure = p_secure;
2685 else
2686 secure = 0;
2687#endif
2688 if ( fullpathcmp((char_u *)USR_EXRC_FILE,
2689 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
2690#ifdef USR_EXRC_FILE2
2691 && fullpathcmp((char_u *)USR_EXRC_FILE2,
2692 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
2693#endif
2694 )
2695 (void)do_source((char_u *)EXRC_FILE, FALSE, FALSE);
2696 }
2697 }
2698 if (secure == 2)
2699 need_wait_return = TRUE;
2700 secure = 0;
2701#ifdef AMIGA
2702 proc->pr_WindowPtr = save_winptr;
2703#endif
2704 }
2705 TIME_MSG("sourcing vimrc file(s)");
2706}
2707
2708/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002709 * Setup to start using the GUI. Exit with an error when not available.
2710 */
2711 static void
2712main_start_gui()
2713{
2714#ifdef FEAT_GUI
2715 gui.starting = TRUE; /* start GUI a bit later */
2716#else
2717 mch_errmsg(_(e_nogvim));
2718 mch_errmsg("\n");
2719 mch_exit(2);
2720#endif
2721}
2722
2723/*
2724 * Get an evironment variable, and execute it as Ex commands.
2725 * Returns FAIL if the environment variable was not executed, OK otherwise.
2726 */
2727 int
2728process_env(env, is_viminit)
2729 char_u *env;
2730 int is_viminit; /* when TRUE, called for VIMINIT */
2731{
2732 char_u *initstr;
2733 char_u *save_sourcing_name;
2734 linenr_T save_sourcing_lnum;
2735#ifdef FEAT_EVAL
2736 scid_T save_sid;
2737#endif
2738
2739 if ((initstr = mch_getenv(env)) != NULL && *initstr != NUL)
2740 {
2741 if (is_viminit)
2742 vimrc_found();
2743 save_sourcing_name = sourcing_name;
2744 save_sourcing_lnum = sourcing_lnum;
2745 sourcing_name = env;
2746 sourcing_lnum = 0;
2747#ifdef FEAT_EVAL
2748 save_sid = current_SID;
2749 current_SID = SID_ENV;
2750#endif
2751 do_cmdline_cmd(initstr);
2752 sourcing_name = save_sourcing_name;
2753 sourcing_lnum = save_sourcing_lnum;
2754#ifdef FEAT_EVAL
2755 current_SID = save_sid;;
2756#endif
2757 return OK;
2758 }
2759 return FAIL;
2760}
2761
2762#if defined(UNIX) || defined(VMS)
2763/*
2764 * Return TRUE if we are certain the user owns the file "fname".
2765 * Used for ".vimrc" and ".exrc".
2766 * Use both stat() and lstat() for extra security.
2767 */
2768 static int
2769file_owned(fname)
2770 char *fname;
2771{
2772 struct stat s;
2773# ifdef UNIX
2774 uid_t uid = getuid();
2775# else /* VMS */
2776 uid_t uid = ((getgid() << 16) | getuid());
2777# endif
2778
2779 return !(mch_stat(fname, &s) != 0 || s.st_uid != uid
2780# ifdef HAVE_LSTAT
2781 || mch_lstat(fname, &s) != 0 || s.st_uid != uid
2782# endif
2783 );
2784}
2785#endif
2786
2787/*
2788 * Give an error message main_errors["n"] and exit.
2789 */
2790 static void
2791mainerr(n, str)
2792 int n; /* one of the ME_ defines */
2793 char_u *str; /* extra argument or NULL */
2794{
2795#if defined(UNIX) || defined(__EMX__) || defined(VMS)
2796 reset_signals(); /* kill us with CTRL-C here, if you like */
2797#endif
2798
2799 mch_errmsg(longVersion);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002800 mch_errmsg("\n");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002801 mch_errmsg(_(main_errors[n]));
2802 if (str != NULL)
2803 {
2804 mch_errmsg(": \"");
2805 mch_errmsg((char *)str);
2806 mch_errmsg("\"");
2807 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002808 mch_errmsg(_("\nMore info with: \"vim -h\"\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002809
2810 mch_exit(1);
2811}
2812
2813 void
2814mainerr_arg_missing(str)
2815 char_u *str;
2816{
2817 mainerr(ME_ARG_MISSING, str);
2818}
2819
2820/*
2821 * print a message with three spaces prepended and '\n' appended.
2822 */
2823 static void
2824main_msg(s)
2825 char *s;
2826{
2827 mch_msg(" ");
2828 mch_msg(s);
2829 mch_msg("\n");
2830}
2831
2832/*
2833 * Print messages for "vim -h" or "vim --help" and exit.
2834 */
2835 static void
2836usage()
2837{
2838 int i;
2839 static char *(use[]) =
2840 {
2841 N_("[file ..] edit specified file(s)"),
2842 N_("- read text from stdin"),
2843 N_("-t tag edit file where tag is defined"),
2844#ifdef FEAT_QUICKFIX
2845 N_("-q [errorfile] edit file with first error")
2846#endif
2847 };
2848
2849#if defined(UNIX) || defined(__EMX__) || defined(VMS)
2850 reset_signals(); /* kill us with CTRL-C here, if you like */
2851#endif
2852
2853 mch_msg(longVersion);
2854 mch_msg(_("\n\nusage:"));
2855 for (i = 0; ; ++i)
2856 {
2857 mch_msg(_(" vim [arguments] "));
2858 mch_msg(_(use[i]));
2859 if (i == (sizeof(use) / sizeof(char_u *)) - 1)
2860 break;
2861 mch_msg(_("\n or:"));
2862 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002863#ifdef VMS
2864 mch_msg(_("where case is ignored prepend / to make flag upper case"));
2865#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002866
2867 mch_msg(_("\n\nArguments:\n"));
2868 main_msg(_("--\t\t\tOnly file names after this"));
2869#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
2870 main_msg(_("--literal\t\tDon't expand wildcards"));
2871#endif
2872#ifdef FEAT_OLE
2873 main_msg(_("-register\t\tRegister this gvim for OLE"));
2874 main_msg(_("-unregister\t\tUnregister gvim for OLE"));
2875#endif
2876#ifdef FEAT_GUI
2877 main_msg(_("-g\t\t\tRun using GUI (like \"gvim\")"));
2878 main_msg(_("-f or --nofork\tForeground: Don't fork when starting GUI"));
2879#endif
2880 main_msg(_("-v\t\t\tVi mode (like \"vi\")"));
2881 main_msg(_("-e\t\t\tEx mode (like \"ex\")"));
2882 main_msg(_("-s\t\t\tSilent (batch) mode (only for \"ex\")"));
2883#ifdef FEAT_DIFF
2884 main_msg(_("-d\t\t\tDiff mode (like \"vimdiff\")"));
2885#endif
2886 main_msg(_("-y\t\t\tEasy mode (like \"evim\", modeless)"));
2887 main_msg(_("-R\t\t\tReadonly mode (like \"view\")"));
2888 main_msg(_("-Z\t\t\tRestricted mode (like \"rvim\")"));
2889 main_msg(_("-m\t\t\tModifications (writing files) not allowed"));
2890 main_msg(_("-M\t\t\tModifications in text not allowed"));
2891 main_msg(_("-b\t\t\tBinary mode"));
2892#ifdef FEAT_LISP
2893 main_msg(_("-l\t\t\tLisp mode"));
2894#endif
2895 main_msg(_("-C\t\t\tCompatible with Vi: 'compatible'"));
2896 main_msg(_("-N\t\t\tNot fully Vi compatible: 'nocompatible'"));
2897 main_msg(_("-V[N]\t\tVerbose level"));
2898 main_msg(_("-D\t\t\tDebugging mode"));
2899 main_msg(_("-n\t\t\tNo swap file, use memory only"));
2900 main_msg(_("-r\t\t\tList swap files and exit"));
2901 main_msg(_("-r (with file name)\tRecover crashed session"));
2902 main_msg(_("-L\t\t\tSame as -r"));
2903#ifdef AMIGA
2904 main_msg(_("-f\t\t\tDon't use newcli to open window"));
2905 main_msg(_("-dev <device>\t\tUse <device> for I/O"));
2906#endif
2907#ifdef FEAT_ARABIC
2908 main_msg(_("-A\t\t\tstart in Arabic mode"));
2909#endif
2910#ifdef FEAT_RIGHTLEFT
2911 main_msg(_("-H\t\t\tStart in Hebrew mode"));
2912#endif
2913#ifdef FEAT_FKMAP
2914 main_msg(_("-F\t\t\tStart in Farsi mode"));
2915#endif
2916 main_msg(_("-T <terminal>\tSet terminal type to <terminal>"));
2917 main_msg(_("-u <vimrc>\t\tUse <vimrc> instead of any .vimrc"));
2918#ifdef FEAT_GUI
2919 main_msg(_("-U <gvimrc>\t\tUse <gvimrc> instead of any .gvimrc"));
2920#endif
2921 main_msg(_("--noplugin\t\tDon't load plugin scripts"));
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002922 main_msg(_("-p[N]\t\tOpen N tab pages (default: one for each file)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002923 main_msg(_("-o[N]\t\tOpen N windows (default: one for each file)"));
2924 main_msg(_("-O[N]\t\tLike -o but split vertically"));
2925 main_msg(_("+\t\t\tStart at end of file"));
2926 main_msg(_("+<lnum>\t\tStart at line <lnum>"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002927 main_msg(_("--cmd <command>\tExecute <command> before loading any vimrc file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002928 main_msg(_("-c <command>\t\tExecute <command> after loading the first file"));
2929 main_msg(_("-S <session>\t\tSource file <session> after loading the first file"));
2930 main_msg(_("-s <scriptin>\tRead Normal mode commands from file <scriptin>"));
2931 main_msg(_("-w <scriptout>\tAppend all typed commands to file <scriptout>"));
2932 main_msg(_("-W <scriptout>\tWrite all typed commands to file <scriptout>"));
2933#ifdef FEAT_CRYPT
2934 main_msg(_("-x\t\t\tEdit encrypted files"));
2935#endif
2936#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
2937# if defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)
2938 main_msg(_("-display <display>\tConnect vim to this particular X-server"));
2939# endif
2940 main_msg(_("-X\t\t\tDo not connect to X server"));
2941#endif
2942#ifdef FEAT_CLIENTSERVER
2943 main_msg(_("--remote <files>\tEdit <files> in a Vim server if possible"));
2944 main_msg(_("--remote-silent <files> Same, don't complain if there is no server"));
2945 main_msg(_("--remote-wait <files> As --remote but wait for files to have been edited"));
2946 main_msg(_("--remote-wait-silent <files> Same, don't complain if there is no server"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00002947# ifdef FEAT_WINDOWS
2948 main_msg(_("--remote-tab <files> As --remote but open tab page for each file"));
2949# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002950 main_msg(_("--remote-send <keys>\tSend <keys> to a Vim server and exit"));
2951 main_msg(_("--remote-expr <expr>\tEvaluate <expr> in a Vim server and print result"));
2952 main_msg(_("--serverlist\t\tList available Vim server names and exit"));
2953 main_msg(_("--servername <name>\tSend to/become the Vim server <name>"));
2954#endif
2955#ifdef FEAT_VIMINFO
2956 main_msg(_("-i <viminfo>\t\tUse <viminfo> instead of .viminfo"));
2957#endif
2958 main_msg(_("-h or --help\tPrint Help (this message) and exit"));
2959 main_msg(_("--version\t\tPrint version information and exit"));
2960
2961#ifdef FEAT_GUI_X11
2962# ifdef FEAT_GUI_MOTIF
2963 mch_msg(_("\nArguments recognised by gvim (Motif version):\n"));
2964# else
2965# ifdef FEAT_GUI_ATHENA
2966# ifdef FEAT_GUI_NEXTAW
2967 mch_msg(_("\nArguments recognised by gvim (neXtaw version):\n"));
2968# else
2969 mch_msg(_("\nArguments recognised by gvim (Athena version):\n"));
2970# endif
2971# endif
2972# endif
2973 main_msg(_("-display <display>\tRun vim on <display>"));
2974 main_msg(_("-iconic\t\tStart vim iconified"));
2975# if 0
2976 main_msg(_("-name <name>\t\tUse resource as if vim was <name>"));
2977 mch_msg(_("\t\t\t (Unimplemented)\n"));
2978# endif
2979 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
2980 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
2981 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
2982 main_msg(_("-boldfont <font>\tUse <font> for bold text"));
2983 main_msg(_("-italicfont <font>\tUse <font> for italic text"));
2984 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
2985 main_msg(_("-borderwidth <width>\tUse a border width of <width> (also: -bw)"));
2986 main_msg(_("-scrollbarwidth <width> Use a scrollbar width of <width> (also: -sw)"));
2987# ifdef FEAT_GUI_ATHENA
2988 main_msg(_("-menuheight <height>\tUse a menu bar height of <height> (also: -mh)"));
2989# endif
2990 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
2991 main_msg(_("+reverse\t\tDon't use reverse video (also: +rv)"));
2992 main_msg(_("-xrm <resource>\tSet the specified resource"));
2993#endif /* FEAT_GUI_X11 */
2994#if defined(FEAT_GUI) && defined(RISCOS)
2995 mch_msg(_("\nArguments recognised by gvim (RISC OS version):\n"));
2996 main_msg(_("--columns <number>\tInitial width of window in columns"));
2997 main_msg(_("--rows <number>\tInitial height of window in rows"));
2998#endif
2999#ifdef FEAT_GUI_GTK
3000 mch_msg(_("\nArguments recognised by gvim (GTK+ version):\n"));
3001 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3002 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3003 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3004 main_msg(_("-display <display>\tRun vim on <display> (also: --display)"));
3005# ifdef HAVE_GTK2
3006 main_msg(_("--role <role>\tSet a unique role to identify the main window"));
3007# endif
3008 main_msg(_("--socketid <xid>\tOpen Vim inside another GTK widget"));
3009#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003010#ifdef FEAT_GUI_W32
3011 main_msg(_("-P <parent title>\tOpen Vim inside parent application"));
3012#endif
3013
3014#ifdef FEAT_GUI_GNOME
3015 /* Gnome gives extra messages for --help if we continue, but not for -h. */
3016 if (gui.starting)
3017 mch_msg("\n");
3018 else
3019#endif
3020 mch_exit(0);
3021}
3022
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00003023#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003024/*
3025 * Check the result of the ATTENTION dialog:
3026 * When "Quit" selected, exit Vim.
3027 * When "Recover" selected, recover the file.
3028 */
3029 static void
3030check_swap_exists_action()
3031{
3032 if (swap_exists_action == SEA_QUIT)
3033 getout(1);
3034 handle_swap_exists(NULL);
3035}
3036#endif
3037
3038#if defined(STARTUPTIME) || defined(PROTO)
3039static void time_diff __ARGS((struct timeval *then, struct timeval *now));
3040
3041static struct timeval prev_timeval;
3042
3043/*
3044 * Save the previous time before doing something that could nest.
3045 * set "*tv_rel" to the time elapsed so far.
3046 */
3047 void
3048time_push(tv_rel, tv_start)
3049 void *tv_rel, *tv_start;
3050{
3051 *((struct timeval *)tv_rel) = prev_timeval;
3052 gettimeofday(&prev_timeval, NULL);
3053 ((struct timeval *)tv_rel)->tv_usec = prev_timeval.tv_usec
3054 - ((struct timeval *)tv_rel)->tv_usec;
3055 ((struct timeval *)tv_rel)->tv_sec = prev_timeval.tv_sec
3056 - ((struct timeval *)tv_rel)->tv_sec;
3057 if (((struct timeval *)tv_rel)->tv_usec < 0)
3058 {
3059 ((struct timeval *)tv_rel)->tv_usec += 1000000;
3060 --((struct timeval *)tv_rel)->tv_sec;
3061 }
3062 *(struct timeval *)tv_start = prev_timeval;
3063}
3064
3065/*
3066 * Compute the previous time after doing something that could nest.
3067 * Subtract "*tp" from prev_timeval;
3068 * Note: The arguments are (void *) to avoid trouble with systems that don't
3069 * have struct timeval.
3070 */
3071 void
3072time_pop(tp)
3073 void *tp; /* actually (struct timeval *) */
3074{
3075 prev_timeval.tv_usec -= ((struct timeval *)tp)->tv_usec;
3076 prev_timeval.tv_sec -= ((struct timeval *)tp)->tv_sec;
3077 if (prev_timeval.tv_usec < 0)
3078 {
3079 prev_timeval.tv_usec += 1000000;
3080 --prev_timeval.tv_sec;
3081 }
3082}
3083
3084 static void
3085time_diff(then, now)
3086 struct timeval *then;
3087 struct timeval *now;
3088{
3089 long usec;
3090 long msec;
3091
3092 usec = now->tv_usec - then->tv_usec;
3093 msec = (now->tv_sec - then->tv_sec) * 1000L + usec / 1000L,
3094 usec = usec % 1000L;
3095 fprintf(time_fd, "%03ld.%03ld", msec, usec >= 0 ? usec : usec + 1000L);
3096}
3097
3098 void
3099time_msg(msg, tv_start)
3100 char *msg;
3101 void *tv_start; /* only for do_source: start time; actually
3102 (struct timeval *) */
3103{
3104 static struct timeval start;
3105 struct timeval now;
3106
3107 if (time_fd != NULL)
3108 {
3109 if (strstr(msg, "STARTING") != NULL)
3110 {
3111 gettimeofday(&start, NULL);
3112 prev_timeval = start;
3113 fprintf(time_fd, "\n\ntimes in msec\n");
3114 fprintf(time_fd, " clock self+sourced self: sourced script\n");
3115 fprintf(time_fd, " clock elapsed: other lines\n\n");
3116 }
3117 gettimeofday(&now, NULL);
3118 time_diff(&start, &now);
3119 if (((struct timeval *)tv_start) != NULL)
3120 {
3121 fprintf(time_fd, " ");
3122 time_diff(((struct timeval *)tv_start), &now);
3123 }
3124 fprintf(time_fd, " ");
3125 time_diff(&prev_timeval, &now);
3126 prev_timeval = now;
3127 fprintf(time_fd, ": %s\n", msg);
3128 }
3129}
3130
3131# ifdef WIN3264
3132/*
3133 * Windows doesn't have gettimeofday(), although it does have struct timeval.
3134 */
3135 int
3136gettimeofday(struct timeval *tv, char *dummy)
3137{
3138 long t = clock();
3139 tv->tv_sec = t / CLOCKS_PER_SEC;
3140 tv->tv_usec = (t - tv->tv_sec * CLOCKS_PER_SEC) * 1000000 / CLOCKS_PER_SEC;
3141 return 0;
3142}
3143# endif
3144
3145#endif
3146
3147#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
3148
3149/*
3150 * Common code for the X command server and the Win32 command server.
3151 */
3152
Bram Moolenaareb94e552006-03-11 21:35:11 +00003153static char_u *build_drop_cmd __ARGS((int filec, char **filev, int tabs, int sendReply));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003154
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003155/*
3156 * Do the client-server stuff, unless "--servername ''" was used.
3157 */
3158 static void
3159exec_on_server(parmp)
3160 mparm_T *parmp;
3161{
3162 if (parmp->serverName_arg == NULL || *parmp->serverName_arg != NUL)
3163 {
3164# ifdef WIN32
3165 /* Initialise the client/server messaging infrastructure. */
3166 serverInitMessaging();
3167# endif
3168
3169 /*
3170 * When a command server argument was found, execute it. This may
3171 * exit Vim when it was successful. Otherwise it's executed further
3172 * on. Remember the encoding used here in "serverStrEnc".
3173 */
3174 if (parmp->serverArg)
3175 {
3176 cmdsrv_main(&parmp->argc, parmp->argv,
3177 parmp->serverName_arg, &parmp->serverStr);
3178# ifdef FEAT_MBYTE
3179 parmp->serverStrEnc = vim_strsave(p_enc);
3180# endif
3181 }
3182
3183 /* If we're still running, get the name to register ourselves.
3184 * On Win32 can register right now, for X11 need to setup the
3185 * clipboard first, it's further down. */
3186 parmp->servername = serverMakeName(parmp->serverName_arg,
3187 parmp->argv[0]);
3188# ifdef WIN32
3189 if (parmp->servername != NULL)
3190 {
3191 serverSetName(parmp->servername);
3192 vim_free(parmp->servername);
3193 }
3194# endif
3195 }
3196}
3197
3198/*
3199 * Prepare for running as a Vim server.
3200 */
3201 static void
3202prepare_server(parmp)
3203 mparm_T *parmp;
3204{
3205# if defined(FEAT_X11)
3206 /*
3207 * Register for remote command execution with :serversend and --remote
3208 * unless there was a -X or a --servername '' on the command line.
3209 * Only register nongui-vim's with an explicit --servername argument.
3210 */
3211 if (X_DISPLAY != NULL && parmp->servername != NULL && (
3212# ifdef FEAT_GUI
3213 gui.in_use ||
3214# endif
3215 parmp->serverName_arg != NULL))
3216 {
3217 (void)serverRegisterName(X_DISPLAY, parmp->servername);
3218 vim_free(parmp->servername);
3219 TIME_MSG("register server name");
3220 }
3221 else
3222 serverDelayedStartName = parmp->servername;
3223# endif
3224
3225 /*
3226 * Execute command ourselves if we're here because the send failed (or
3227 * else we would have exited above).
3228 */
3229 if (parmp->serverStr != NULL)
3230 {
3231 char_u *p;
3232
3233 server_to_input_buf(serverConvert(parmp->serverStrEnc,
3234 parmp->serverStr, &p));
3235 vim_free(p);
3236 }
3237}
3238
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003239 static void
3240cmdsrv_main(argc, argv, serverName_arg, serverStr)
3241 int *argc;
3242 char **argv;
3243 char_u *serverName_arg;
3244 char_u **serverStr;
3245{
3246 char_u *res;
3247 int i;
3248 char_u *sname;
3249 int ret;
3250 int didone = FALSE;
3251 int exiterr = 0;
3252 char **newArgV = argv + 1;
3253 int newArgC = 1,
3254 Argc = *argc;
3255 int argtype;
3256#define ARGTYPE_OTHER 0
3257#define ARGTYPE_EDIT 1
3258#define ARGTYPE_EDIT_WAIT 2
3259#define ARGTYPE_SEND 3
3260 int silent = FALSE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003261 int tabs = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003262# ifndef FEAT_X11
3263 HWND srv;
3264# else
3265 Window srv;
3266
3267 setup_term_clip();
3268# endif
3269
3270 sname = serverMakeName(serverName_arg, argv[0]);
3271 if (sname == NULL)
3272 return;
3273
3274 /*
3275 * Execute the command server related arguments and remove them
3276 * from the argc/argv array; We may have to return into main()
3277 */
3278 for (i = 1; i < Argc; i++)
3279 {
3280 res = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003281 if (STRCMP(argv[i], "--") == 0) /* end of option arguments */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003282 {
3283 for (; i < *argc; i++)
3284 {
3285 *newArgV++ = argv[i];
3286 newArgC++;
3287 }
3288 break;
3289 }
3290
Bram Moolenaareb94e552006-03-11 21:35:11 +00003291 if (STRICMP(argv[i], "--remote-send") == 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003292 argtype = ARGTYPE_SEND;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003293 else if (STRNICMP(argv[i], "--remote", 8) == 0)
3294 {
3295 char *p = argv[i] + 8;
3296
3297 argtype = ARGTYPE_EDIT;
3298 while (*p != NUL)
3299 {
3300 if (STRNICMP(p, "-wait", 5) == 0)
3301 {
3302 argtype = ARGTYPE_EDIT_WAIT;
3303 p += 5;
3304 }
3305 else if (STRNICMP(p, "-silent", 7) == 0)
3306 {
3307 silent = TRUE;
3308 p += 7;
3309 }
3310 else if (STRNICMP(p, "-tab", 4) == 0)
3311 {
3312 tabs = TRUE;
3313 p += 4;
3314 }
3315 else
3316 {
3317 argtype = ARGTYPE_OTHER;
3318 break;
3319 }
3320 }
3321 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003322 else
3323 argtype = ARGTYPE_OTHER;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003324
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003325 if (argtype != ARGTYPE_OTHER)
3326 {
3327 if (i == *argc - 1)
3328 mainerr_arg_missing((char_u *)argv[i]);
3329 if (argtype == ARGTYPE_SEND)
3330 {
3331 *serverStr = (char_u *)argv[i + 1];
3332 i++;
3333 }
3334 else
3335 {
3336 *serverStr = build_drop_cmd(*argc - i - 1, argv + i + 1,
Bram Moolenaareb94e552006-03-11 21:35:11 +00003337 tabs, argtype == ARGTYPE_EDIT_WAIT);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003338 if (*serverStr == NULL)
3339 {
3340 /* Probably out of memory, exit. */
3341 didone = TRUE;
3342 exiterr = 1;
3343 break;
3344 }
3345 Argc = i;
3346 }
3347# ifdef FEAT_X11
3348 if (xterm_dpy == NULL)
3349 {
3350 mch_errmsg(_("No display"));
3351 ret = -1;
3352 }
3353 else
3354 ret = serverSendToVim(xterm_dpy, sname, *serverStr,
3355 NULL, &srv, 0, 0, silent);
3356# else
3357 /* Win32 always works? */
3358 ret = serverSendToVim(sname, *serverStr, NULL, &srv, 0, silent);
3359# endif
3360 if (ret < 0)
3361 {
3362 if (argtype == ARGTYPE_SEND)
3363 {
3364 /* Failed to send, abort. */
3365 mch_errmsg(_(": Send failed.\n"));
3366 didone = TRUE;
3367 exiterr = 1;
3368 }
3369 else if (!silent)
3370 /* Let vim start normally. */
3371 mch_errmsg(_(": Send failed. Trying to execute locally\n"));
3372 break;
3373 }
3374
3375# ifdef FEAT_GUI_W32
3376 /* Guess that when the server name starts with "g" it's a GUI
3377 * server, which we can bring to the foreground here.
3378 * Foreground() in the server doesn't work very well. */
3379 if (argtype != ARGTYPE_SEND && TOUPPER_ASC(*sname) == 'G')
3380 SetForegroundWindow(srv);
3381# endif
3382
3383 /*
3384 * For --remote-wait: Wait until the server did edit each
3385 * file. Also detect that the server no longer runs.
3386 */
3387 if (ret >= 0 && argtype == ARGTYPE_EDIT_WAIT)
3388 {
3389 int numFiles = *argc - i - 1;
3390 int j;
3391 char_u *done = alloc(numFiles);
3392 char_u *p;
3393# ifdef FEAT_GUI_W32
3394 NOTIFYICONDATA ni;
3395 int count = 0;
3396 extern HWND message_window;
3397# endif
3398
3399 if (numFiles > 0 && argv[i + 1][0] == '+')
3400 /* Skip "+cmd" argument, don't wait for it to be edited. */
3401 --numFiles;
3402
3403# ifdef FEAT_GUI_W32
3404 ni.cbSize = sizeof(ni);
3405 ni.hWnd = message_window;
3406 ni.uID = 0;
3407 ni.uFlags = NIF_ICON|NIF_TIP;
3408 ni.hIcon = LoadIcon((HINSTANCE)GetModuleHandle(0), "IDR_VIM");
3409 sprintf(ni.szTip, _("%d of %d edited"), count, numFiles);
3410 Shell_NotifyIcon(NIM_ADD, &ni);
3411# endif
3412
3413 /* Wait for all files to unload in remote */
3414 memset(done, 0, numFiles);
3415 while (memchr(done, 0, numFiles) != NULL)
3416 {
3417# ifdef WIN32
3418 p = serverGetReply(srv, NULL, TRUE, TRUE);
3419 if (p == NULL)
3420 break;
3421# else
3422 if (serverReadReply(xterm_dpy, srv, &p, TRUE) < 0)
3423 break;
3424# endif
3425 j = atoi((char *)p);
3426 if (j >= 0 && j < numFiles)
3427 {
3428# ifdef FEAT_GUI_W32
3429 ++count;
3430 sprintf(ni.szTip, _("%d of %d edited"),
3431 count, numFiles);
3432 Shell_NotifyIcon(NIM_MODIFY, &ni);
3433# endif
3434 done[j] = 1;
3435 }
3436 }
3437# ifdef FEAT_GUI_W32
3438 Shell_NotifyIcon(NIM_DELETE, &ni);
3439# endif
3440 }
3441 }
3442 else if (STRICMP(argv[i], "--remote-expr") == 0)
3443 {
3444 if (i == *argc - 1)
3445 mainerr_arg_missing((char_u *)argv[i]);
3446# ifdef WIN32
3447 /* Win32 always works? */
3448 if (serverSendToVim(sname, (char_u *)argv[i + 1],
3449 &res, NULL, 1, FALSE) < 0)
3450# else
3451 if (xterm_dpy == NULL)
3452 mch_errmsg(_("No display: Send expression failed.\n"));
3453 else if (serverSendToVim(xterm_dpy, sname, (char_u *)argv[i + 1],
3454 &res, NULL, 1, 1, FALSE) < 0)
3455# endif
3456 {
3457 if (res != NULL && *res != NUL)
3458 {
3459 /* Output error from remote */
3460 mch_errmsg((char *)res);
3461 vim_free(res);
3462 res = NULL;
3463 }
3464 mch_errmsg(_(": Send expression failed.\n"));
3465 }
3466 }
3467 else if (STRICMP(argv[i], "--serverlist") == 0)
3468 {
3469# ifdef WIN32
3470 /* Win32 always works? */
3471 res = serverGetVimNames();
3472# else
3473 if (xterm_dpy != NULL)
3474 res = serverGetVimNames(xterm_dpy);
3475# endif
3476 if (called_emsg)
3477 mch_errmsg("\n");
3478 }
3479 else if (STRICMP(argv[i], "--servername") == 0)
3480 {
3481 /* Alredy processed. Take it out of the command line */
3482 i++;
3483 continue;
3484 }
3485 else
3486 {
3487 *newArgV++ = argv[i];
3488 newArgC++;
3489 continue;
3490 }
3491 didone = TRUE;
3492 if (res != NULL && *res != NUL)
3493 {
3494 mch_msg((char *)res);
3495 if (res[STRLEN(res) - 1] != '\n')
3496 mch_msg("\n");
3497 }
3498 vim_free(res);
3499 }
3500
3501 if (didone)
3502 {
3503 display_errors(); /* display any collected messages */
3504 exit(exiterr); /* Mission accomplished - get out */
3505 }
3506
3507 /* Return back into main() */
3508 *argc = newArgC;
3509 vim_free(sname);
3510}
3511
3512/*
3513 * Build a ":drop" command to send to a Vim server.
3514 */
3515 static char_u *
Bram Moolenaareb94e552006-03-11 21:35:11 +00003516build_drop_cmd(filec, filev, tabs, sendReply)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003517 int filec;
3518 char **filev;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003519 int tabs; /* Use ":tab drop" instead of ":drop". */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003520 int sendReply;
3521{
3522 garray_T ga;
3523 int i;
3524 char_u *inicmd = NULL;
3525 char_u *p;
3526 char_u cwd[MAXPATHL];
3527
3528 if (filec > 0 && filev[0][0] == '+')
3529 {
3530 inicmd = (char_u *)filev[0] + 1;
3531 filev++;
3532 filec--;
3533 }
3534 /* Check if we have at least one argument. */
3535 if (filec <= 0)
3536 mainerr_arg_missing((char_u *)filev[-1]);
3537 if (mch_dirname(cwd, MAXPATHL) != OK)
3538 return NULL;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003539 if ((p = vim_strsave_escaped_ext(cwd, PATH_ESC_CHARS, '\\', TRUE)) == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003540 return NULL;
3541 ga_init2(&ga, 1, 100);
3542 ga_concat(&ga, (char_u *)"<C-\\><C-N>:cd ");
3543 ga_concat(&ga, p);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003544 vim_free(p);
Bram Moolenaareb94e552006-03-11 21:35:11 +00003545
3546 /* Call inputsave() so that a prompt for an encryption key works. */
3547 ga_concat(&ga, (char_u *)"<CR>:if exists('*inputsave')|call inputsave()|endif|");
3548 if (tabs)
3549 ga_concat(&ga, (char_u *)"tab ");
3550 ga_concat(&ga, (char_u *)"drop");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003551 for (i = 0; i < filec; i++)
3552 {
3553 /* On Unix the shell has already expanded the wildcards, don't want to
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003554 * do it again in the Vim server. On MS-Windows only escape
3555 * non-wildcard characters. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003556 p = vim_strsave_escaped((char_u *)filev[i],
3557#ifdef UNIX
3558 PATH_ESC_CHARS
3559#else
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003560 (char_u *)" \t%#"
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003561#endif
3562 );
3563 if (p == NULL)
3564 {
3565 vim_free(ga.ga_data);
3566 return NULL;
3567 }
3568 ga_concat(&ga, (char_u *)" ");
3569 ga_concat(&ga, p);
3570 vim_free(p);
3571 }
3572 /* The :drop commands goes to Insert mode when 'insertmode' is set, use
3573 * CTRL-\ CTRL-N again. */
3574 ga_concat(&ga, (char_u *)"|if exists('*inputrestore')|call inputrestore()|endif<CR>");
3575 ga_concat(&ga, (char_u *)"<C-\\><C-N>:cd -");
3576 if (sendReply)
3577 ga_concat(&ga, (char_u *)"<CR>:call SetupRemoteReplies()");
3578 ga_concat(&ga, (char_u *)"<CR>:");
3579 if (inicmd != NULL)
3580 {
3581 /* Can't use <CR> after "inicmd", because an "startinsert" would cause
3582 * the following commands to be inserted as text. Use a "|",
3583 * hopefully "inicmd" does allow this... */
3584 ga_concat(&ga, inicmd);
3585 ga_concat(&ga, (char_u *)"|");
3586 }
3587 /* Bring the window to the foreground, goto Insert mode when 'im' set and
3588 * clear command line. */
Bram Moolenaar567e4de2004-12-31 21:01:02 +00003589 ga_concat(&ga, (char_u *)"cal foreground()|if &im|star|en|redr|f<CR>");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003590 ga_append(&ga, NUL);
3591 return ga.ga_data;
3592}
3593
3594/*
3595 * Replace termcodes such as <CR> and insert as key presses if there is room.
3596 */
3597 void
3598server_to_input_buf(str)
3599 char_u *str;
3600{
3601 char_u *ptr = NULL;
3602 char_u *cpo_save = p_cpo;
3603
3604 /* Set 'cpoptions' the way we want it.
3605 * B set - backslashes are *not* treated specially
3606 * k set - keycodes are *not* reverse-engineered
3607 * < unset - <Key> sequences *are* interpreted
3608 * The last parameter of replace_termcodes() is TRUE so that the <lt>
3609 * sequence is recognised - needed for a real backslash.
3610 */
3611 p_cpo = (char_u *)"Bk";
3612 str = replace_termcodes((char_u *)str, &ptr, FALSE, TRUE);
3613 p_cpo = cpo_save;
3614
3615 if (*ptr != NUL) /* trailing CTRL-V results in nothing */
3616 {
3617 /*
3618 * Add the string to the input stream.
3619 * Can't use add_to_input_buf() here, we now have K_SPECIAL bytes.
3620 *
3621 * First clear typed characters from the typeahead buffer, there could
3622 * be half a mapping there. Then append to the existing string, so
3623 * that multiple commands from a client are concatenated.
3624 */
3625 if (typebuf.tb_maplen < typebuf.tb_len)
3626 del_typebuf(typebuf.tb_len - typebuf.tb_maplen, typebuf.tb_maplen);
3627 (void)ins_typebuf(str, REMAP_NONE, typebuf.tb_len, TRUE, FALSE);
3628
3629 /* Let input_available() know we inserted text in the typeahead
3630 * buffer. */
3631 received_from_client = TRUE;
3632 }
3633 vim_free((char_u *)ptr);
3634}
3635
3636/*
3637 * Evaluate an expression that the client sent to a string.
3638 * Handles disabling error messages and disables debugging, otherwise Vim
3639 * hangs, waiting for "cont" to be typed.
3640 */
3641 char_u *
3642eval_client_expr_to_string(expr)
3643 char_u *expr;
3644{
3645 char_u *res;
3646 int save_dbl = debug_break_level;
3647 int save_ro = redir_off;
3648
3649 debug_break_level = -1;
3650 redir_off = 0;
3651 ++emsg_skip;
3652
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003653 res = eval_to_string(expr, NULL, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003654
3655 debug_break_level = save_dbl;
3656 redir_off = save_ro;
3657 --emsg_skip;
3658
Bram Moolenaar63a121b2005-12-11 21:36:39 +00003659 /* A client can tell us to redraw, but not to display the cursor, so do
3660 * that here. */
3661 setcursor();
3662 out_flush();
3663#ifdef FEAT_GUI
3664 if (gui.in_use)
3665 gui_update_cursor(FALSE, FALSE);
3666#endif
3667
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003668 return res;
3669}
3670
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003671/*
3672 * If conversion is needed, convert "data" from "client_enc" to 'encoding' and
3673 * return an allocated string. Otherwise return "data".
3674 * "*tofree" is set to the result when it needs to be freed later.
3675 */
3676/*ARGSUSED*/
3677 char_u *
3678serverConvert(client_enc, data, tofree)
3679 char_u *client_enc;
3680 char_u *data;
3681 char_u **tofree;
3682{
3683 char_u *res = data;
3684
3685 *tofree = NULL;
3686# ifdef FEAT_MBYTE
3687 if (client_enc != NULL && p_enc != NULL)
3688 {
3689 vimconv_T vimconv;
3690
3691 vimconv.vc_type = CONV_NONE;
3692 if (convert_setup(&vimconv, client_enc, p_enc) != FAIL
3693 && vimconv.vc_type != CONV_NONE)
3694 {
3695 res = string_convert(&vimconv, data, NULL);
3696 if (res == NULL)
3697 res = data;
3698 else
3699 *tofree = res;
3700 }
3701 convert_setup(&vimconv, NULL, NULL);
3702 }
3703# endif
3704 return res;
3705}
3706
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003707
3708/*
3709 * Make our basic server name: use the specified "arg" if given, otherwise use
3710 * the tail of the command "cmd" we were started with.
3711 * Return the name in allocated memory. This doesn't include a serial number.
3712 */
3713 static char_u *
3714serverMakeName(arg, cmd)
3715 char_u *arg;
3716 char *cmd;
3717{
3718 char_u *p;
3719
3720 if (arg != NULL && *arg != NUL)
3721 p = vim_strsave_up(arg);
3722 else
3723 {
3724 p = vim_strsave_up(gettail((char_u *)cmd));
3725 /* Remove .exe or .bat from the name. */
3726 if (p != NULL && vim_strchr(p, '.') != NULL)
3727 *vim_strchr(p, '.') = NUL;
3728 }
3729 return p;
3730}
3731#endif /* FEAT_CLIENTSERVER */
3732
3733/*
3734 * When FEAT_FKMAP is defined, also compile the Farsi source code.
3735 */
3736#if defined(FEAT_FKMAP) || defined(PROTO)
3737# include "farsi.c"
3738#endif
3739
3740/*
3741 * When FEAT_ARABIC is defined, also compile the Arabic source code.
3742 */
3743#if defined(FEAT_ARABIC) || defined(PROTO)
3744# include "arabic.c"
3745#endif