blob: 2a90cc3580573ae414353444e3ba587cfdd5b285 [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);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001875 argv_idx = (int)STRLEN(argv[0]);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001876 }
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 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002171 /* Remember this argument has been added to the argument list.
2172 * Needed when 'encoding' is changed. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002173 used_file_arg(argv[0], parmp->literal, parmp->full_path,
2174 parmp->diff_mode);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002175 }
2176#endif
2177 }
2178
2179 /*
2180 * If there are no more letters after the current "-", go to next
2181 * argument. argv_idx is set to -1 when the current argument is to be
2182 * skipped.
2183 */
2184 if (argv_idx <= 0 || argv[0][argv_idx] == NUL)
2185 {
2186 --argc;
2187 ++argv;
2188 argv_idx = 1;
2189 }
2190 }
2191}
2192
2193/*
2194 * Print a warning if stdout is not a terminal.
2195 * When starting in Ex mode and commands come from a file, set Silent mode.
2196 */
2197 static void
2198check_tty(parmp)
2199 mparm_T *parmp;
2200{
2201 int input_isatty; /* is active input a terminal? */
2202
2203 input_isatty = mch_input_isatty();
2204 if (exmode_active)
2205 {
2206 if (!input_isatty)
2207 silent_mode = TRUE;
2208 }
2209 else if (parmp->want_full_screen && (!parmp->stdout_isatty || !input_isatty)
2210#ifdef FEAT_GUI
2211 /* don't want the delay when started from the desktop */
2212 && !gui.starting
2213#endif
2214 )
2215 {
2216#ifdef NBDEBUG
2217 /*
2218 * This shouldn't be necessary. But if I run netbeans with the log
2219 * output coming to the console and XOpenDisplay fails, I get vim
2220 * trying to start with input/output to my console tty. This fills my
2221 * input buffer so fast I can't even kill the process in under 2
2222 * minutes (and it beeps continuosly the whole time :-)
2223 */
2224 if (usingNetbeans && (!parmp->stdout_isatty || !input_isatty))
2225 {
2226 mch_errmsg(_("Vim: Error: Failure to start gvim from NetBeans\n"));
2227 exit(1);
2228 }
2229#endif
2230 if (!parmp->stdout_isatty)
2231 mch_errmsg(_("Vim: Warning: Output is not to a terminal\n"));
2232 if (!input_isatty)
2233 mch_errmsg(_("Vim: Warning: Input is not from a terminal\n"));
2234 out_flush();
2235 if (scriptin[0] == NULL)
2236 ui_delay(2000L, TRUE);
2237 TIME_MSG("Warning delay");
2238 }
2239}
2240
2241/*
2242 * Read text from stdin.
2243 */
2244 static void
2245read_stdin()
2246{
2247 int i;
2248
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002249#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002250 /* When getting the ATTENTION prompt here, use a dialog */
2251 swap_exists_action = SEA_DIALOG;
2252#endif
2253 no_wait_return = TRUE;
2254 i = msg_didany;
2255 set_buflisted(TRUE);
2256 (void)open_buffer(TRUE, NULL); /* create memfile and read file */
2257 no_wait_return = FALSE;
2258 msg_didany = i;
2259 TIME_MSG("reading stdin");
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002260#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002261 check_swap_exists_action();
2262#endif
2263#if !(defined(AMIGA) || defined(MACOS))
2264 /*
2265 * Close stdin and dup it from stderr. Required for GPM to work
2266 * properly, and for running external commands.
2267 * Is there any other system that cannot do this?
2268 */
2269 close(0);
2270 dup(2);
2271#endif
2272}
2273
2274/*
2275 * Create the requested number of windows and edit buffers in them.
2276 * Also does recovery if "recoverymode" set.
2277 */
2278/*ARGSUSED*/
2279 static void
2280create_windows(parmp)
2281 mparm_T *parmp;
2282{
2283#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002284 int rewind;
2285 int done = 0;
2286
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002287 /*
2288 * Create the number of windows that was requested.
2289 */
2290 if (parmp->window_count == -1) /* was not set */
2291 parmp->window_count = 1;
2292 if (parmp->window_count == 0)
2293 parmp->window_count = GARGCOUNT;
2294 if (parmp->window_count > 1)
2295 {
2296 /* Don't change the windows if there was a command in .vimrc that
2297 * already split some windows */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002298 if (parmp->window_layout == 0)
2299 parmp->window_layout = WIN_HOR;
2300 if (parmp->window_layout == WIN_TABS)
2301 {
2302 parmp->window_count = make_tabpages(parmp->window_count);
2303 TIME_MSG("making tab pages");
2304 }
2305 else if (firstwin->w_next == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002306 {
2307 parmp->window_count = make_windows(parmp->window_count,
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002308 parmp->window_layout == WIN_VER);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002309 TIME_MSG("making windows");
2310 }
2311 else
2312 parmp->window_count = win_count();
2313 }
2314 else
2315 parmp->window_count = 1;
2316#endif
2317
2318 if (recoverymode) /* do recover */
2319 {
2320 msg_scroll = TRUE; /* scroll message up */
2321 ml_recover();
2322 if (curbuf->b_ml.ml_mfp == NULL) /* failed */
2323 getout(1);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002324 do_modelines(0); /* do modelines */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002325 }
2326 else
2327 {
2328 /*
2329 * Open a buffer for windows that don't have one yet.
2330 * Commands in the .vimrc might have loaded a file or split the window.
2331 * Watch out for autocommands that delete a window.
2332 */
2333#ifdef FEAT_AUTOCMD
2334 /*
2335 * Don't execute Win/Buf Enter/Leave autocommands here
2336 */
2337 ++autocmd_no_enter;
2338 ++autocmd_no_leave;
2339#endif
2340#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002341 rewind = TRUE;
2342 while (done++ < 1000)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002343 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002344 if (rewind)
2345 {
2346 if (parmp->window_layout == WIN_TABS)
2347 goto_tabpage(1);
2348 else
2349 curwin = firstwin;
2350 }
2351 else if (parmp->window_layout == WIN_TABS)
2352 {
2353 if (curtab->tp_next == NULL)
2354 break;
2355 goto_tabpage(0);
2356 }
2357 else
2358 {
2359 if (curwin->w_next == NULL)
2360 break;
2361 curwin = curwin->w_next;
2362 }
2363 rewind = FALSE;
2364#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002365 curbuf = curwin->w_buffer;
2366 if (curbuf->b_ml.ml_mfp == NULL)
2367 {
2368#ifdef FEAT_FOLDING
2369 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2370 if (p_fdls >= 0)
2371 curwin->w_p_fdl = p_fdls;
2372#endif
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002373#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002374 /* When getting the ATTENTION prompt here, use a dialog */
2375 swap_exists_action = SEA_DIALOG;
2376#endif
2377 set_buflisted(TRUE);
2378 (void)open_buffer(FALSE, NULL); /* create memfile, read file */
2379
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002380#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002381 check_swap_exists_action();
2382#endif
2383#ifdef FEAT_AUTOCMD
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002384 rewind = TRUE; /* start again */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002385#endif
2386 }
2387#ifdef FEAT_WINDOWS
2388 ui_breakcheck();
2389 if (got_int)
2390 {
2391 (void)vgetc(); /* only break the file loading, not the rest */
2392 break;
2393 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002394 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002395#endif
2396#ifdef FEAT_WINDOWS
2397 if (parmp->window_layout == WIN_TABS)
2398 goto_tabpage(1);
2399 else
2400 curwin = firstwin;
2401 curbuf = curwin->w_buffer;
2402#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002403#ifdef FEAT_AUTOCMD
2404 --autocmd_no_enter;
2405 --autocmd_no_leave;
2406#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002407 }
2408}
2409
2410#ifdef FEAT_WINDOWS
2411 /*
2412 * If opened more than one window, start editing files in the other
2413 * windows. make_windows() has already opened the windows.
2414 */
2415 static void
2416edit_buffers(parmp)
2417 mparm_T *parmp;
2418{
2419 int arg_idx; /* index in argument list */
2420 int i;
2421
2422# ifdef FEAT_AUTOCMD
2423 /*
2424 * Don't execute Win/Buf Enter/Leave autocommands here
2425 */
2426 ++autocmd_no_enter;
2427 ++autocmd_no_leave;
2428# endif
2429 arg_idx = 1;
2430 for (i = 1; i < parmp->window_count; ++i)
2431 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002432 if (parmp->window_layout == WIN_TABS)
2433 {
2434 if (curtab->tp_next == NULL) /* just checking */
2435 break;
2436 goto_tabpage(0);
2437 }
2438 else
2439 {
2440 if (curwin->w_next == NULL) /* just checking */
2441 break;
2442 win_enter(curwin->w_next, FALSE);
2443 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002444
2445 /* Only open the file if there is no file in this window yet (that can
2446 * happen when .vimrc contains ":sall") */
2447 if (curbuf == firstwin->w_buffer || curbuf->b_ffname == NULL)
2448 {
2449 curwin->w_arg_idx = arg_idx;
2450 /* edit file from arg list, if there is one */
2451 (void)do_ecmd(0, arg_idx < GARGCOUNT
2452 ? alist_name(&GARGLIST[arg_idx]) : NULL,
2453 NULL, NULL, ECMD_LASTL, ECMD_HIDE);
2454 if (arg_idx == GARGCOUNT - 1)
2455 arg_had_last = TRUE;
2456 ++arg_idx;
2457 }
2458 ui_breakcheck();
2459 if (got_int)
2460 {
2461 (void)vgetc(); /* only break the file loading, not the rest */
2462 break;
2463 }
2464 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002465
2466 if (parmp->window_layout == WIN_TABS)
2467 goto_tabpage(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002468# ifdef FEAT_AUTOCMD
2469 --autocmd_no_enter;
2470# endif
2471 win_enter(firstwin, FALSE); /* back to first window */
2472# ifdef FEAT_AUTOCMD
2473 --autocmd_no_leave;
2474# endif
2475 TIME_MSG("editing files in windows");
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002476 if (parmp->window_count > 1 && parmp->window_layout != WIN_TABS)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002477 win_equal(curwin, FALSE, 'b'); /* adjust heights */
2478}
2479#endif /* FEAT_WINDOWS */
2480
2481/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002482 * Execute the commands from --cmd arguments "cmds[cnt]".
2483 */
2484 static void
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002485exe_pre_commands(parmp)
2486 mparm_T *parmp;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002487{
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002488 char_u **cmds = parmp->pre_commands;
2489 int cnt = parmp->n_pre_commands;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002490 int i;
2491
2492 if (cnt > 0)
2493 {
2494 curwin->w_cursor.lnum = 0; /* just in case.. */
2495 sourcing_name = (char_u *)_("pre-vimrc command line");
2496# ifdef FEAT_EVAL
2497 current_SID = SID_CMDARG;
2498# endif
2499 for (i = 0; i < cnt; ++i)
2500 do_cmdline_cmd(cmds[i]);
2501 sourcing_name = NULL;
2502# ifdef FEAT_EVAL
2503 current_SID = 0;
2504# endif
2505 TIME_MSG("--cmd commands");
2506 }
2507}
2508
2509/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002510 * Execute "+", "-c" and "-S" arguments.
2511 */
2512 static void
2513exe_commands(parmp)
2514 mparm_T *parmp;
2515{
2516 int i;
2517
2518 /*
2519 * We start commands on line 0, make "vim +/pat file" match a
2520 * pattern on line 1. But don't move the cursor when an autocommand
2521 * with g`" was used.
2522 */
2523 msg_scroll = TRUE;
2524 if (parmp->tagname == NULL && curwin->w_cursor.lnum <= 1)
2525 curwin->w_cursor.lnum = 0;
2526 sourcing_name = (char_u *)"command line";
2527#ifdef FEAT_EVAL
2528 current_SID = SID_CARG;
2529#endif
2530 for (i = 0; i < parmp->n_commands; ++i)
2531 {
2532 do_cmdline_cmd(parmp->commands[i]);
2533 if (parmp->cmds_tofree[i])
2534 vim_free(parmp->commands[i]);
2535 }
2536 sourcing_name = NULL;
2537#ifdef FEAT_EVAL
2538 current_SID = 0;
2539#endif
2540 if (curwin->w_cursor.lnum == 0)
2541 curwin->w_cursor.lnum = 1;
2542
2543 if (!exmode_active)
2544 msg_scroll = FALSE;
2545
2546#ifdef FEAT_QUICKFIX
2547 /* When started with "-q errorfile" jump to first error again. */
2548 if (parmp->edit_type == EDIT_QF)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002549 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002550#endif
2551 TIME_MSG("executing command arguments");
2552}
2553
2554/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002555 * Source startup scripts.
2556 */
2557 static void
2558source_startup_scripts(parmp)
2559 mparm_T *parmp;
2560{
2561 int i;
2562
2563 /*
2564 * For "evim" source evim.vim first of all, so that the user can overrule
2565 * any things he doesn't like.
2566 */
2567 if (parmp->evim_mode)
2568 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002569 (void)do_source((char_u *)EVIM_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002570 TIME_MSG("source evim file");
2571 }
2572
2573 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002574 * If -u argument given, use only the initializations from that file and
Bram Moolenaar58d98232005-07-23 22:25:46 +00002575 * nothing else.
2576 */
2577 if (parmp->use_vimrc != NULL)
2578 {
Bram Moolenaar231334e2005-07-25 20:46:57 +00002579 if (STRCMP(parmp->use_vimrc, "NONE") == 0
2580 || STRCMP(parmp->use_vimrc, "NORC") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002581 {
2582#ifdef FEAT_GUI
2583 if (use_gvimrc == NULL) /* don't load gvimrc either */
2584 use_gvimrc = parmp->use_vimrc;
2585#endif
2586 if (parmp->use_vimrc[2] == 'N')
2587 p_lpl = FALSE; /* don't load plugins either */
2588 }
2589 else
2590 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002591 if (do_source(parmp->use_vimrc, FALSE, DOSO_NONE) != OK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002592 EMSG2(_("E282: Cannot read from \"%s\""), parmp->use_vimrc);
2593 }
2594 }
2595 else if (!silent_mode)
2596 {
2597#ifdef AMIGA
2598 struct Process *proc = (struct Process *)FindTask(0L);
2599 APTR save_winptr = proc->pr_WindowPtr;
2600
2601 /* Avoid a requester here for a volume that doesn't exist. */
2602 proc->pr_WindowPtr = (APTR)-1L;
2603#endif
2604
2605 /*
2606 * Get system wide defaults, if the file name is defined.
2607 */
2608#ifdef SYS_VIMRC_FILE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002609 (void)do_source((char_u *)SYS_VIMRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002610#endif
Bram Moolenaar1056d982006-03-09 22:37:52 +00002611#ifdef MACOS_X
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002612 (void)do_source((char_u *)"$VIMRUNTIME/macmap.vim", FALSE, DOSO_NONE);
Bram Moolenaar1056d982006-03-09 22:37:52 +00002613#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00002614
2615 /*
2616 * Try to read initialization commands from the following places:
2617 * - environment variable VIMINIT
2618 * - user vimrc file (s:.vimrc for Amiga, ~/.vimrc otherwise)
2619 * - second user vimrc file ($VIM/.vimrc for Dos)
2620 * - environment variable EXINIT
2621 * - user exrc file (s:.exrc for Amiga, ~/.exrc otherwise)
2622 * - second user exrc file ($VIM/.exrc for Dos)
2623 * The first that exists is used, the rest is ignored.
2624 */
2625 if (process_env((char_u *)"VIMINIT", TRUE) != OK)
2626 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002627 if (do_source((char_u *)USR_VIMRC_FILE, TRUE, DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00002628#ifdef USR_VIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002629 && do_source((char_u *)USR_VIMRC_FILE2, TRUE,
2630 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00002631#endif
2632#ifdef USR_VIMRC_FILE3
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002633 && do_source((char_u *)USR_VIMRC_FILE3, TRUE,
2634 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00002635#endif
2636 && process_env((char_u *)"EXINIT", FALSE) == FAIL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002637 && do_source((char_u *)USR_EXRC_FILE, FALSE, DOSO_NONE) == FAIL)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002638 {
2639#ifdef USR_EXRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002640 (void)do_source((char_u *)USR_EXRC_FILE2, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002641#endif
2642 }
2643 }
2644
2645 /*
2646 * Read initialization commands from ".vimrc" or ".exrc" in current
2647 * directory. This is only done if the 'exrc' option is set.
2648 * Because of security reasons we disallow shell and write commands
2649 * now, except for unix if the file is owned by the user or 'secure'
2650 * option has been reset in environment of global ".exrc" or ".vimrc".
2651 * Only do this if VIMRC_FILE is not the same as USR_VIMRC_FILE or
2652 * SYS_VIMRC_FILE.
2653 */
2654 if (p_exrc)
2655 {
2656#if defined(UNIX) || defined(VMS)
2657 /* If ".vimrc" file is not owned by user, set 'secure' mode. */
2658 if (!file_owned(VIMRC_FILE))
2659#endif
2660 secure = p_secure;
2661
2662 i = FAIL;
2663 if (fullpathcmp((char_u *)USR_VIMRC_FILE,
2664 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
2665#ifdef USR_VIMRC_FILE2
2666 && fullpathcmp((char_u *)USR_VIMRC_FILE2,
2667 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
2668#endif
2669#ifdef USR_VIMRC_FILE3
2670 && fullpathcmp((char_u *)USR_VIMRC_FILE3,
2671 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
2672#endif
2673#ifdef SYS_VIMRC_FILE
2674 && fullpathcmp((char_u *)SYS_VIMRC_FILE,
2675 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
2676#endif
2677 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002678 i = do_source((char_u *)VIMRC_FILE, TRUE, DOSO_VIMRC);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002679
2680 if (i == FAIL)
2681 {
2682#if defined(UNIX) || defined(VMS)
2683 /* if ".exrc" is not owned by user set 'secure' mode */
2684 if (!file_owned(EXRC_FILE))
2685 secure = p_secure;
2686 else
2687 secure = 0;
2688#endif
2689 if ( fullpathcmp((char_u *)USR_EXRC_FILE,
2690 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
2691#ifdef USR_EXRC_FILE2
2692 && fullpathcmp((char_u *)USR_EXRC_FILE2,
2693 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
2694#endif
2695 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002696 (void)do_source((char_u *)EXRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002697 }
2698 }
2699 if (secure == 2)
2700 need_wait_return = TRUE;
2701 secure = 0;
2702#ifdef AMIGA
2703 proc->pr_WindowPtr = save_winptr;
2704#endif
2705 }
2706 TIME_MSG("sourcing vimrc file(s)");
2707}
2708
2709/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002710 * Setup to start using the GUI. Exit with an error when not available.
2711 */
2712 static void
2713main_start_gui()
2714{
2715#ifdef FEAT_GUI
2716 gui.starting = TRUE; /* start GUI a bit later */
2717#else
2718 mch_errmsg(_(e_nogvim));
2719 mch_errmsg("\n");
2720 mch_exit(2);
2721#endif
2722}
2723
2724/*
2725 * Get an evironment variable, and execute it as Ex commands.
2726 * Returns FAIL if the environment variable was not executed, OK otherwise.
2727 */
2728 int
2729process_env(env, is_viminit)
2730 char_u *env;
2731 int is_viminit; /* when TRUE, called for VIMINIT */
2732{
2733 char_u *initstr;
2734 char_u *save_sourcing_name;
2735 linenr_T save_sourcing_lnum;
2736#ifdef FEAT_EVAL
2737 scid_T save_sid;
2738#endif
2739
2740 if ((initstr = mch_getenv(env)) != NULL && *initstr != NUL)
2741 {
2742 if (is_viminit)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002743 vimrc_found(NULL, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002744 save_sourcing_name = sourcing_name;
2745 save_sourcing_lnum = sourcing_lnum;
2746 sourcing_name = env;
2747 sourcing_lnum = 0;
2748#ifdef FEAT_EVAL
2749 save_sid = current_SID;
2750 current_SID = SID_ENV;
2751#endif
2752 do_cmdline_cmd(initstr);
2753 sourcing_name = save_sourcing_name;
2754 sourcing_lnum = save_sourcing_lnum;
2755#ifdef FEAT_EVAL
2756 current_SID = save_sid;;
2757#endif
2758 return OK;
2759 }
2760 return FAIL;
2761}
2762
2763#if defined(UNIX) || defined(VMS)
2764/*
2765 * Return TRUE if we are certain the user owns the file "fname".
2766 * Used for ".vimrc" and ".exrc".
2767 * Use both stat() and lstat() for extra security.
2768 */
2769 static int
2770file_owned(fname)
2771 char *fname;
2772{
2773 struct stat s;
2774# ifdef UNIX
2775 uid_t uid = getuid();
2776# else /* VMS */
2777 uid_t uid = ((getgid() << 16) | getuid());
2778# endif
2779
2780 return !(mch_stat(fname, &s) != 0 || s.st_uid != uid
2781# ifdef HAVE_LSTAT
2782 || mch_lstat(fname, &s) != 0 || s.st_uid != uid
2783# endif
2784 );
2785}
2786#endif
2787
2788/*
2789 * Give an error message main_errors["n"] and exit.
2790 */
2791 static void
2792mainerr(n, str)
2793 int n; /* one of the ME_ defines */
2794 char_u *str; /* extra argument or NULL */
2795{
2796#if defined(UNIX) || defined(__EMX__) || defined(VMS)
2797 reset_signals(); /* kill us with CTRL-C here, if you like */
2798#endif
2799
2800 mch_errmsg(longVersion);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002801 mch_errmsg("\n");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002802 mch_errmsg(_(main_errors[n]));
2803 if (str != NULL)
2804 {
2805 mch_errmsg(": \"");
2806 mch_errmsg((char *)str);
2807 mch_errmsg("\"");
2808 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002809 mch_errmsg(_("\nMore info with: \"vim -h\"\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002810
2811 mch_exit(1);
2812}
2813
2814 void
2815mainerr_arg_missing(str)
2816 char_u *str;
2817{
2818 mainerr(ME_ARG_MISSING, str);
2819}
2820
2821/*
2822 * print a message with three spaces prepended and '\n' appended.
2823 */
2824 static void
2825main_msg(s)
2826 char *s;
2827{
2828 mch_msg(" ");
2829 mch_msg(s);
2830 mch_msg("\n");
2831}
2832
2833/*
2834 * Print messages for "vim -h" or "vim --help" and exit.
2835 */
2836 static void
2837usage()
2838{
2839 int i;
2840 static char *(use[]) =
2841 {
2842 N_("[file ..] edit specified file(s)"),
2843 N_("- read text from stdin"),
2844 N_("-t tag edit file where tag is defined"),
2845#ifdef FEAT_QUICKFIX
2846 N_("-q [errorfile] edit file with first error")
2847#endif
2848 };
2849
2850#if defined(UNIX) || defined(__EMX__) || defined(VMS)
2851 reset_signals(); /* kill us with CTRL-C here, if you like */
2852#endif
2853
2854 mch_msg(longVersion);
2855 mch_msg(_("\n\nusage:"));
2856 for (i = 0; ; ++i)
2857 {
2858 mch_msg(_(" vim [arguments] "));
2859 mch_msg(_(use[i]));
2860 if (i == (sizeof(use) / sizeof(char_u *)) - 1)
2861 break;
2862 mch_msg(_("\n or:"));
2863 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002864#ifdef VMS
2865 mch_msg(_("where case is ignored prepend / to make flag upper case"));
2866#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002867
2868 mch_msg(_("\n\nArguments:\n"));
2869 main_msg(_("--\t\t\tOnly file names after this"));
2870#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
2871 main_msg(_("--literal\t\tDon't expand wildcards"));
2872#endif
2873#ifdef FEAT_OLE
2874 main_msg(_("-register\t\tRegister this gvim for OLE"));
2875 main_msg(_("-unregister\t\tUnregister gvim for OLE"));
2876#endif
2877#ifdef FEAT_GUI
2878 main_msg(_("-g\t\t\tRun using GUI (like \"gvim\")"));
2879 main_msg(_("-f or --nofork\tForeground: Don't fork when starting GUI"));
2880#endif
2881 main_msg(_("-v\t\t\tVi mode (like \"vi\")"));
2882 main_msg(_("-e\t\t\tEx mode (like \"ex\")"));
2883 main_msg(_("-s\t\t\tSilent (batch) mode (only for \"ex\")"));
2884#ifdef FEAT_DIFF
2885 main_msg(_("-d\t\t\tDiff mode (like \"vimdiff\")"));
2886#endif
2887 main_msg(_("-y\t\t\tEasy mode (like \"evim\", modeless)"));
2888 main_msg(_("-R\t\t\tReadonly mode (like \"view\")"));
2889 main_msg(_("-Z\t\t\tRestricted mode (like \"rvim\")"));
2890 main_msg(_("-m\t\t\tModifications (writing files) not allowed"));
2891 main_msg(_("-M\t\t\tModifications in text not allowed"));
2892 main_msg(_("-b\t\t\tBinary mode"));
2893#ifdef FEAT_LISP
2894 main_msg(_("-l\t\t\tLisp mode"));
2895#endif
2896 main_msg(_("-C\t\t\tCompatible with Vi: 'compatible'"));
2897 main_msg(_("-N\t\t\tNot fully Vi compatible: 'nocompatible'"));
2898 main_msg(_("-V[N]\t\tVerbose level"));
2899 main_msg(_("-D\t\t\tDebugging mode"));
2900 main_msg(_("-n\t\t\tNo swap file, use memory only"));
2901 main_msg(_("-r\t\t\tList swap files and exit"));
2902 main_msg(_("-r (with file name)\tRecover crashed session"));
2903 main_msg(_("-L\t\t\tSame as -r"));
2904#ifdef AMIGA
2905 main_msg(_("-f\t\t\tDon't use newcli to open window"));
2906 main_msg(_("-dev <device>\t\tUse <device> for I/O"));
2907#endif
2908#ifdef FEAT_ARABIC
2909 main_msg(_("-A\t\t\tstart in Arabic mode"));
2910#endif
2911#ifdef FEAT_RIGHTLEFT
2912 main_msg(_("-H\t\t\tStart in Hebrew mode"));
2913#endif
2914#ifdef FEAT_FKMAP
2915 main_msg(_("-F\t\t\tStart in Farsi mode"));
2916#endif
2917 main_msg(_("-T <terminal>\tSet terminal type to <terminal>"));
2918 main_msg(_("-u <vimrc>\t\tUse <vimrc> instead of any .vimrc"));
2919#ifdef FEAT_GUI
2920 main_msg(_("-U <gvimrc>\t\tUse <gvimrc> instead of any .gvimrc"));
2921#endif
2922 main_msg(_("--noplugin\t\tDon't load plugin scripts"));
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002923 main_msg(_("-p[N]\t\tOpen N tab pages (default: one for each file)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002924 main_msg(_("-o[N]\t\tOpen N windows (default: one for each file)"));
2925 main_msg(_("-O[N]\t\tLike -o but split vertically"));
2926 main_msg(_("+\t\t\tStart at end of file"));
2927 main_msg(_("+<lnum>\t\tStart at line <lnum>"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002928 main_msg(_("--cmd <command>\tExecute <command> before loading any vimrc file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002929 main_msg(_("-c <command>\t\tExecute <command> after loading the first file"));
2930 main_msg(_("-S <session>\t\tSource file <session> after loading the first file"));
2931 main_msg(_("-s <scriptin>\tRead Normal mode commands from file <scriptin>"));
2932 main_msg(_("-w <scriptout>\tAppend all typed commands to file <scriptout>"));
2933 main_msg(_("-W <scriptout>\tWrite all typed commands to file <scriptout>"));
2934#ifdef FEAT_CRYPT
2935 main_msg(_("-x\t\t\tEdit encrypted files"));
2936#endif
2937#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
2938# if defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)
2939 main_msg(_("-display <display>\tConnect vim to this particular X-server"));
2940# endif
2941 main_msg(_("-X\t\t\tDo not connect to X server"));
2942#endif
2943#ifdef FEAT_CLIENTSERVER
2944 main_msg(_("--remote <files>\tEdit <files> in a Vim server if possible"));
2945 main_msg(_("--remote-silent <files> Same, don't complain if there is no server"));
2946 main_msg(_("--remote-wait <files> As --remote but wait for files to have been edited"));
2947 main_msg(_("--remote-wait-silent <files> Same, don't complain if there is no server"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00002948# ifdef FEAT_WINDOWS
2949 main_msg(_("--remote-tab <files> As --remote but open tab page for each file"));
2950# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002951 main_msg(_("--remote-send <keys>\tSend <keys> to a Vim server and exit"));
2952 main_msg(_("--remote-expr <expr>\tEvaluate <expr> in a Vim server and print result"));
2953 main_msg(_("--serverlist\t\tList available Vim server names and exit"));
2954 main_msg(_("--servername <name>\tSend to/become the Vim server <name>"));
2955#endif
2956#ifdef FEAT_VIMINFO
2957 main_msg(_("-i <viminfo>\t\tUse <viminfo> instead of .viminfo"));
2958#endif
2959 main_msg(_("-h or --help\tPrint Help (this message) and exit"));
2960 main_msg(_("--version\t\tPrint version information and exit"));
2961
2962#ifdef FEAT_GUI_X11
2963# ifdef FEAT_GUI_MOTIF
2964 mch_msg(_("\nArguments recognised by gvim (Motif version):\n"));
2965# else
2966# ifdef FEAT_GUI_ATHENA
2967# ifdef FEAT_GUI_NEXTAW
2968 mch_msg(_("\nArguments recognised by gvim (neXtaw version):\n"));
2969# else
2970 mch_msg(_("\nArguments recognised by gvim (Athena version):\n"));
2971# endif
2972# endif
2973# endif
2974 main_msg(_("-display <display>\tRun vim on <display>"));
2975 main_msg(_("-iconic\t\tStart vim iconified"));
2976# if 0
2977 main_msg(_("-name <name>\t\tUse resource as if vim was <name>"));
2978 mch_msg(_("\t\t\t (Unimplemented)\n"));
2979# endif
2980 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
2981 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
2982 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
2983 main_msg(_("-boldfont <font>\tUse <font> for bold text"));
2984 main_msg(_("-italicfont <font>\tUse <font> for italic text"));
2985 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
2986 main_msg(_("-borderwidth <width>\tUse a border width of <width> (also: -bw)"));
2987 main_msg(_("-scrollbarwidth <width> Use a scrollbar width of <width> (also: -sw)"));
2988# ifdef FEAT_GUI_ATHENA
2989 main_msg(_("-menuheight <height>\tUse a menu bar height of <height> (also: -mh)"));
2990# endif
2991 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
2992 main_msg(_("+reverse\t\tDon't use reverse video (also: +rv)"));
2993 main_msg(_("-xrm <resource>\tSet the specified resource"));
2994#endif /* FEAT_GUI_X11 */
2995#if defined(FEAT_GUI) && defined(RISCOS)
2996 mch_msg(_("\nArguments recognised by gvim (RISC OS version):\n"));
2997 main_msg(_("--columns <number>\tInitial width of window in columns"));
2998 main_msg(_("--rows <number>\tInitial height of window in rows"));
2999#endif
3000#ifdef FEAT_GUI_GTK
3001 mch_msg(_("\nArguments recognised by gvim (GTK+ version):\n"));
3002 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3003 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3004 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3005 main_msg(_("-display <display>\tRun vim on <display> (also: --display)"));
3006# ifdef HAVE_GTK2
3007 main_msg(_("--role <role>\tSet a unique role to identify the main window"));
3008# endif
3009 main_msg(_("--socketid <xid>\tOpen Vim inside another GTK widget"));
3010#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003011#ifdef FEAT_GUI_W32
3012 main_msg(_("-P <parent title>\tOpen Vim inside parent application"));
3013#endif
3014
3015#ifdef FEAT_GUI_GNOME
3016 /* Gnome gives extra messages for --help if we continue, but not for -h. */
3017 if (gui.starting)
3018 mch_msg("\n");
3019 else
3020#endif
3021 mch_exit(0);
3022}
3023
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00003024#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003025/*
3026 * Check the result of the ATTENTION dialog:
3027 * When "Quit" selected, exit Vim.
3028 * When "Recover" selected, recover the file.
3029 */
3030 static void
3031check_swap_exists_action()
3032{
3033 if (swap_exists_action == SEA_QUIT)
3034 getout(1);
3035 handle_swap_exists(NULL);
3036}
3037#endif
3038
3039#if defined(STARTUPTIME) || defined(PROTO)
3040static void time_diff __ARGS((struct timeval *then, struct timeval *now));
3041
3042static struct timeval prev_timeval;
3043
3044/*
3045 * Save the previous time before doing something that could nest.
3046 * set "*tv_rel" to the time elapsed so far.
3047 */
3048 void
3049time_push(tv_rel, tv_start)
3050 void *tv_rel, *tv_start;
3051{
3052 *((struct timeval *)tv_rel) = prev_timeval;
3053 gettimeofday(&prev_timeval, NULL);
3054 ((struct timeval *)tv_rel)->tv_usec = prev_timeval.tv_usec
3055 - ((struct timeval *)tv_rel)->tv_usec;
3056 ((struct timeval *)tv_rel)->tv_sec = prev_timeval.tv_sec
3057 - ((struct timeval *)tv_rel)->tv_sec;
3058 if (((struct timeval *)tv_rel)->tv_usec < 0)
3059 {
3060 ((struct timeval *)tv_rel)->tv_usec += 1000000;
3061 --((struct timeval *)tv_rel)->tv_sec;
3062 }
3063 *(struct timeval *)tv_start = prev_timeval;
3064}
3065
3066/*
3067 * Compute the previous time after doing something that could nest.
3068 * Subtract "*tp" from prev_timeval;
3069 * Note: The arguments are (void *) to avoid trouble with systems that don't
3070 * have struct timeval.
3071 */
3072 void
3073time_pop(tp)
3074 void *tp; /* actually (struct timeval *) */
3075{
3076 prev_timeval.tv_usec -= ((struct timeval *)tp)->tv_usec;
3077 prev_timeval.tv_sec -= ((struct timeval *)tp)->tv_sec;
3078 if (prev_timeval.tv_usec < 0)
3079 {
3080 prev_timeval.tv_usec += 1000000;
3081 --prev_timeval.tv_sec;
3082 }
3083}
3084
3085 static void
3086time_diff(then, now)
3087 struct timeval *then;
3088 struct timeval *now;
3089{
3090 long usec;
3091 long msec;
3092
3093 usec = now->tv_usec - then->tv_usec;
3094 msec = (now->tv_sec - then->tv_sec) * 1000L + usec / 1000L,
3095 usec = usec % 1000L;
3096 fprintf(time_fd, "%03ld.%03ld", msec, usec >= 0 ? usec : usec + 1000L);
3097}
3098
3099 void
3100time_msg(msg, tv_start)
3101 char *msg;
3102 void *tv_start; /* only for do_source: start time; actually
3103 (struct timeval *) */
3104{
3105 static struct timeval start;
3106 struct timeval now;
3107
3108 if (time_fd != NULL)
3109 {
3110 if (strstr(msg, "STARTING") != NULL)
3111 {
3112 gettimeofday(&start, NULL);
3113 prev_timeval = start;
3114 fprintf(time_fd, "\n\ntimes in msec\n");
3115 fprintf(time_fd, " clock self+sourced self: sourced script\n");
3116 fprintf(time_fd, " clock elapsed: other lines\n\n");
3117 }
3118 gettimeofday(&now, NULL);
3119 time_diff(&start, &now);
3120 if (((struct timeval *)tv_start) != NULL)
3121 {
3122 fprintf(time_fd, " ");
3123 time_diff(((struct timeval *)tv_start), &now);
3124 }
3125 fprintf(time_fd, " ");
3126 time_diff(&prev_timeval, &now);
3127 prev_timeval = now;
3128 fprintf(time_fd, ": %s\n", msg);
3129 }
3130}
3131
3132# ifdef WIN3264
3133/*
3134 * Windows doesn't have gettimeofday(), although it does have struct timeval.
3135 */
3136 int
3137gettimeofday(struct timeval *tv, char *dummy)
3138{
3139 long t = clock();
3140 tv->tv_sec = t / CLOCKS_PER_SEC;
3141 tv->tv_usec = (t - tv->tv_sec * CLOCKS_PER_SEC) * 1000000 / CLOCKS_PER_SEC;
3142 return 0;
3143}
3144# endif
3145
3146#endif
3147
3148#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
3149
3150/*
3151 * Common code for the X command server and the Win32 command server.
3152 */
3153
Bram Moolenaareb94e552006-03-11 21:35:11 +00003154static char_u *build_drop_cmd __ARGS((int filec, char **filev, int tabs, int sendReply));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003155
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003156/*
3157 * Do the client-server stuff, unless "--servername ''" was used.
3158 */
3159 static void
3160exec_on_server(parmp)
3161 mparm_T *parmp;
3162{
3163 if (parmp->serverName_arg == NULL || *parmp->serverName_arg != NUL)
3164 {
3165# ifdef WIN32
3166 /* Initialise the client/server messaging infrastructure. */
3167 serverInitMessaging();
3168# endif
3169
3170 /*
3171 * When a command server argument was found, execute it. This may
3172 * exit Vim when it was successful. Otherwise it's executed further
3173 * on. Remember the encoding used here in "serverStrEnc".
3174 */
3175 if (parmp->serverArg)
3176 {
3177 cmdsrv_main(&parmp->argc, parmp->argv,
3178 parmp->serverName_arg, &parmp->serverStr);
3179# ifdef FEAT_MBYTE
3180 parmp->serverStrEnc = vim_strsave(p_enc);
3181# endif
3182 }
3183
3184 /* If we're still running, get the name to register ourselves.
3185 * On Win32 can register right now, for X11 need to setup the
3186 * clipboard first, it's further down. */
3187 parmp->servername = serverMakeName(parmp->serverName_arg,
3188 parmp->argv[0]);
3189# ifdef WIN32
3190 if (parmp->servername != NULL)
3191 {
3192 serverSetName(parmp->servername);
3193 vim_free(parmp->servername);
3194 }
3195# endif
3196 }
3197}
3198
3199/*
3200 * Prepare for running as a Vim server.
3201 */
3202 static void
3203prepare_server(parmp)
3204 mparm_T *parmp;
3205{
3206# if defined(FEAT_X11)
3207 /*
3208 * Register for remote command execution with :serversend and --remote
3209 * unless there was a -X or a --servername '' on the command line.
3210 * Only register nongui-vim's with an explicit --servername argument.
3211 */
3212 if (X_DISPLAY != NULL && parmp->servername != NULL && (
3213# ifdef FEAT_GUI
3214 gui.in_use ||
3215# endif
3216 parmp->serverName_arg != NULL))
3217 {
3218 (void)serverRegisterName(X_DISPLAY, parmp->servername);
3219 vim_free(parmp->servername);
3220 TIME_MSG("register server name");
3221 }
3222 else
3223 serverDelayedStartName = parmp->servername;
3224# endif
3225
3226 /*
3227 * Execute command ourselves if we're here because the send failed (or
3228 * else we would have exited above).
3229 */
3230 if (parmp->serverStr != NULL)
3231 {
3232 char_u *p;
3233
3234 server_to_input_buf(serverConvert(parmp->serverStrEnc,
3235 parmp->serverStr, &p));
3236 vim_free(p);
3237 }
3238}
3239
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003240 static void
3241cmdsrv_main(argc, argv, serverName_arg, serverStr)
3242 int *argc;
3243 char **argv;
3244 char_u *serverName_arg;
3245 char_u **serverStr;
3246{
3247 char_u *res;
3248 int i;
3249 char_u *sname;
3250 int ret;
3251 int didone = FALSE;
3252 int exiterr = 0;
3253 char **newArgV = argv + 1;
3254 int newArgC = 1,
3255 Argc = *argc;
3256 int argtype;
3257#define ARGTYPE_OTHER 0
3258#define ARGTYPE_EDIT 1
3259#define ARGTYPE_EDIT_WAIT 2
3260#define ARGTYPE_SEND 3
3261 int silent = FALSE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003262 int tabs = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003263# ifndef FEAT_X11
3264 HWND srv;
3265# else
3266 Window srv;
3267
3268 setup_term_clip();
3269# endif
3270
3271 sname = serverMakeName(serverName_arg, argv[0]);
3272 if (sname == NULL)
3273 return;
3274
3275 /*
3276 * Execute the command server related arguments and remove them
3277 * from the argc/argv array; We may have to return into main()
3278 */
3279 for (i = 1; i < Argc; i++)
3280 {
3281 res = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003282 if (STRCMP(argv[i], "--") == 0) /* end of option arguments */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003283 {
3284 for (; i < *argc; i++)
3285 {
3286 *newArgV++ = argv[i];
3287 newArgC++;
3288 }
3289 break;
3290 }
3291
Bram Moolenaareb94e552006-03-11 21:35:11 +00003292 if (STRICMP(argv[i], "--remote-send") == 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003293 argtype = ARGTYPE_SEND;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003294 else if (STRNICMP(argv[i], "--remote", 8) == 0)
3295 {
3296 char *p = argv[i] + 8;
3297
3298 argtype = ARGTYPE_EDIT;
3299 while (*p != NUL)
3300 {
3301 if (STRNICMP(p, "-wait", 5) == 0)
3302 {
3303 argtype = ARGTYPE_EDIT_WAIT;
3304 p += 5;
3305 }
3306 else if (STRNICMP(p, "-silent", 7) == 0)
3307 {
3308 silent = TRUE;
3309 p += 7;
3310 }
3311 else if (STRNICMP(p, "-tab", 4) == 0)
3312 {
3313 tabs = TRUE;
3314 p += 4;
3315 }
3316 else
3317 {
3318 argtype = ARGTYPE_OTHER;
3319 break;
3320 }
3321 }
3322 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003323 else
3324 argtype = ARGTYPE_OTHER;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003325
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003326 if (argtype != ARGTYPE_OTHER)
3327 {
3328 if (i == *argc - 1)
3329 mainerr_arg_missing((char_u *)argv[i]);
3330 if (argtype == ARGTYPE_SEND)
3331 {
3332 *serverStr = (char_u *)argv[i + 1];
3333 i++;
3334 }
3335 else
3336 {
3337 *serverStr = build_drop_cmd(*argc - i - 1, argv + i + 1,
Bram Moolenaareb94e552006-03-11 21:35:11 +00003338 tabs, argtype == ARGTYPE_EDIT_WAIT);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003339 if (*serverStr == NULL)
3340 {
3341 /* Probably out of memory, exit. */
3342 didone = TRUE;
3343 exiterr = 1;
3344 break;
3345 }
3346 Argc = i;
3347 }
3348# ifdef FEAT_X11
3349 if (xterm_dpy == NULL)
3350 {
3351 mch_errmsg(_("No display"));
3352 ret = -1;
3353 }
3354 else
3355 ret = serverSendToVim(xterm_dpy, sname, *serverStr,
3356 NULL, &srv, 0, 0, silent);
3357# else
3358 /* Win32 always works? */
3359 ret = serverSendToVim(sname, *serverStr, NULL, &srv, 0, silent);
3360# endif
3361 if (ret < 0)
3362 {
3363 if (argtype == ARGTYPE_SEND)
3364 {
3365 /* Failed to send, abort. */
3366 mch_errmsg(_(": Send failed.\n"));
3367 didone = TRUE;
3368 exiterr = 1;
3369 }
3370 else if (!silent)
3371 /* Let vim start normally. */
3372 mch_errmsg(_(": Send failed. Trying to execute locally\n"));
3373 break;
3374 }
3375
3376# ifdef FEAT_GUI_W32
3377 /* Guess that when the server name starts with "g" it's a GUI
3378 * server, which we can bring to the foreground here.
3379 * Foreground() in the server doesn't work very well. */
3380 if (argtype != ARGTYPE_SEND && TOUPPER_ASC(*sname) == 'G')
3381 SetForegroundWindow(srv);
3382# endif
3383
3384 /*
3385 * For --remote-wait: Wait until the server did edit each
3386 * file. Also detect that the server no longer runs.
3387 */
3388 if (ret >= 0 && argtype == ARGTYPE_EDIT_WAIT)
3389 {
3390 int numFiles = *argc - i - 1;
3391 int j;
3392 char_u *done = alloc(numFiles);
3393 char_u *p;
3394# ifdef FEAT_GUI_W32
3395 NOTIFYICONDATA ni;
3396 int count = 0;
3397 extern HWND message_window;
3398# endif
3399
3400 if (numFiles > 0 && argv[i + 1][0] == '+')
3401 /* Skip "+cmd" argument, don't wait for it to be edited. */
3402 --numFiles;
3403
3404# ifdef FEAT_GUI_W32
3405 ni.cbSize = sizeof(ni);
3406 ni.hWnd = message_window;
3407 ni.uID = 0;
3408 ni.uFlags = NIF_ICON|NIF_TIP;
3409 ni.hIcon = LoadIcon((HINSTANCE)GetModuleHandle(0), "IDR_VIM");
3410 sprintf(ni.szTip, _("%d of %d edited"), count, numFiles);
3411 Shell_NotifyIcon(NIM_ADD, &ni);
3412# endif
3413
3414 /* Wait for all files to unload in remote */
3415 memset(done, 0, numFiles);
3416 while (memchr(done, 0, numFiles) != NULL)
3417 {
3418# ifdef WIN32
3419 p = serverGetReply(srv, NULL, TRUE, TRUE);
3420 if (p == NULL)
3421 break;
3422# else
3423 if (serverReadReply(xterm_dpy, srv, &p, TRUE) < 0)
3424 break;
3425# endif
3426 j = atoi((char *)p);
3427 if (j >= 0 && j < numFiles)
3428 {
3429# ifdef FEAT_GUI_W32
3430 ++count;
3431 sprintf(ni.szTip, _("%d of %d edited"),
3432 count, numFiles);
3433 Shell_NotifyIcon(NIM_MODIFY, &ni);
3434# endif
3435 done[j] = 1;
3436 }
3437 }
3438# ifdef FEAT_GUI_W32
3439 Shell_NotifyIcon(NIM_DELETE, &ni);
3440# endif
3441 }
3442 }
3443 else if (STRICMP(argv[i], "--remote-expr") == 0)
3444 {
3445 if (i == *argc - 1)
3446 mainerr_arg_missing((char_u *)argv[i]);
3447# ifdef WIN32
3448 /* Win32 always works? */
3449 if (serverSendToVim(sname, (char_u *)argv[i + 1],
3450 &res, NULL, 1, FALSE) < 0)
3451# else
3452 if (xterm_dpy == NULL)
3453 mch_errmsg(_("No display: Send expression failed.\n"));
3454 else if (serverSendToVim(xterm_dpy, sname, (char_u *)argv[i + 1],
3455 &res, NULL, 1, 1, FALSE) < 0)
3456# endif
3457 {
3458 if (res != NULL && *res != NUL)
3459 {
3460 /* Output error from remote */
3461 mch_errmsg((char *)res);
3462 vim_free(res);
3463 res = NULL;
3464 }
3465 mch_errmsg(_(": Send expression failed.\n"));
3466 }
3467 }
3468 else if (STRICMP(argv[i], "--serverlist") == 0)
3469 {
3470# ifdef WIN32
3471 /* Win32 always works? */
3472 res = serverGetVimNames();
3473# else
3474 if (xterm_dpy != NULL)
3475 res = serverGetVimNames(xterm_dpy);
3476# endif
3477 if (called_emsg)
3478 mch_errmsg("\n");
3479 }
3480 else if (STRICMP(argv[i], "--servername") == 0)
3481 {
3482 /* Alredy processed. Take it out of the command line */
3483 i++;
3484 continue;
3485 }
3486 else
3487 {
3488 *newArgV++ = argv[i];
3489 newArgC++;
3490 continue;
3491 }
3492 didone = TRUE;
3493 if (res != NULL && *res != NUL)
3494 {
3495 mch_msg((char *)res);
3496 if (res[STRLEN(res) - 1] != '\n')
3497 mch_msg("\n");
3498 }
3499 vim_free(res);
3500 }
3501
3502 if (didone)
3503 {
3504 display_errors(); /* display any collected messages */
3505 exit(exiterr); /* Mission accomplished - get out */
3506 }
3507
3508 /* Return back into main() */
3509 *argc = newArgC;
3510 vim_free(sname);
3511}
3512
3513/*
3514 * Build a ":drop" command to send to a Vim server.
3515 */
3516 static char_u *
Bram Moolenaareb94e552006-03-11 21:35:11 +00003517build_drop_cmd(filec, filev, tabs, sendReply)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003518 int filec;
3519 char **filev;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003520 int tabs; /* Use ":tab drop" instead of ":drop". */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003521 int sendReply;
3522{
3523 garray_T ga;
3524 int i;
3525 char_u *inicmd = NULL;
3526 char_u *p;
3527 char_u cwd[MAXPATHL];
3528
3529 if (filec > 0 && filev[0][0] == '+')
3530 {
3531 inicmd = (char_u *)filev[0] + 1;
3532 filev++;
3533 filec--;
3534 }
3535 /* Check if we have at least one argument. */
3536 if (filec <= 0)
3537 mainerr_arg_missing((char_u *)filev[-1]);
3538 if (mch_dirname(cwd, MAXPATHL) != OK)
3539 return NULL;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003540 if ((p = vim_strsave_escaped_ext(cwd, PATH_ESC_CHARS, '\\', TRUE)) == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003541 return NULL;
3542 ga_init2(&ga, 1, 100);
3543 ga_concat(&ga, (char_u *)"<C-\\><C-N>:cd ");
3544 ga_concat(&ga, p);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003545 vim_free(p);
Bram Moolenaareb94e552006-03-11 21:35:11 +00003546
3547 /* Call inputsave() so that a prompt for an encryption key works. */
3548 ga_concat(&ga, (char_u *)"<CR>:if exists('*inputsave')|call inputsave()|endif|");
3549 if (tabs)
3550 ga_concat(&ga, (char_u *)"tab ");
3551 ga_concat(&ga, (char_u *)"drop");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003552 for (i = 0; i < filec; i++)
3553 {
3554 /* On Unix the shell has already expanded the wildcards, don't want to
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003555 * do it again in the Vim server. On MS-Windows only escape
3556 * non-wildcard characters. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003557 p = vim_strsave_escaped((char_u *)filev[i],
3558#ifdef UNIX
3559 PATH_ESC_CHARS
3560#else
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003561 (char_u *)" \t%#"
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003562#endif
3563 );
3564 if (p == NULL)
3565 {
3566 vim_free(ga.ga_data);
3567 return NULL;
3568 }
3569 ga_concat(&ga, (char_u *)" ");
3570 ga_concat(&ga, p);
3571 vim_free(p);
3572 }
3573 /* The :drop commands goes to Insert mode when 'insertmode' is set, use
3574 * CTRL-\ CTRL-N again. */
3575 ga_concat(&ga, (char_u *)"|if exists('*inputrestore')|call inputrestore()|endif<CR>");
3576 ga_concat(&ga, (char_u *)"<C-\\><C-N>:cd -");
3577 if (sendReply)
3578 ga_concat(&ga, (char_u *)"<CR>:call SetupRemoteReplies()");
3579 ga_concat(&ga, (char_u *)"<CR>:");
3580 if (inicmd != NULL)
3581 {
3582 /* Can't use <CR> after "inicmd", because an "startinsert" would cause
3583 * the following commands to be inserted as text. Use a "|",
3584 * hopefully "inicmd" does allow this... */
3585 ga_concat(&ga, inicmd);
3586 ga_concat(&ga, (char_u *)"|");
3587 }
3588 /* Bring the window to the foreground, goto Insert mode when 'im' set and
3589 * clear command line. */
Bram Moolenaar567e4de2004-12-31 21:01:02 +00003590 ga_concat(&ga, (char_u *)"cal foreground()|if &im|star|en|redr|f<CR>");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003591 ga_append(&ga, NUL);
3592 return ga.ga_data;
3593}
3594
3595/*
3596 * Replace termcodes such as <CR> and insert as key presses if there is room.
3597 */
3598 void
3599server_to_input_buf(str)
3600 char_u *str;
3601{
3602 char_u *ptr = NULL;
3603 char_u *cpo_save = p_cpo;
3604
3605 /* Set 'cpoptions' the way we want it.
3606 * B set - backslashes are *not* treated specially
3607 * k set - keycodes are *not* reverse-engineered
3608 * < unset - <Key> sequences *are* interpreted
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00003609 * The last but one parameter of replace_termcodes() is TRUE so that the
3610 * <lt> sequence is recognised - needed for a real backslash.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003611 */
3612 p_cpo = (char_u *)"Bk";
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00003613 str = replace_termcodes((char_u *)str, &ptr, FALSE, TRUE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003614 p_cpo = cpo_save;
3615
3616 if (*ptr != NUL) /* trailing CTRL-V results in nothing */
3617 {
3618 /*
3619 * Add the string to the input stream.
3620 * Can't use add_to_input_buf() here, we now have K_SPECIAL bytes.
3621 *
3622 * First clear typed characters from the typeahead buffer, there could
3623 * be half a mapping there. Then append to the existing string, so
3624 * that multiple commands from a client are concatenated.
3625 */
3626 if (typebuf.tb_maplen < typebuf.tb_len)
3627 del_typebuf(typebuf.tb_len - typebuf.tb_maplen, typebuf.tb_maplen);
3628 (void)ins_typebuf(str, REMAP_NONE, typebuf.tb_len, TRUE, FALSE);
3629
3630 /* Let input_available() know we inserted text in the typeahead
3631 * buffer. */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003632 typebuf_was_filled = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003633 }
3634 vim_free((char_u *)ptr);
3635}
3636
3637/*
3638 * Evaluate an expression that the client sent to a string.
3639 * Handles disabling error messages and disables debugging, otherwise Vim
3640 * hangs, waiting for "cont" to be typed.
3641 */
3642 char_u *
3643eval_client_expr_to_string(expr)
3644 char_u *expr;
3645{
3646 char_u *res;
3647 int save_dbl = debug_break_level;
3648 int save_ro = redir_off;
3649
3650 debug_break_level = -1;
3651 redir_off = 0;
3652 ++emsg_skip;
3653
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003654 res = eval_to_string(expr, NULL, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003655
3656 debug_break_level = save_dbl;
3657 redir_off = save_ro;
3658 --emsg_skip;
3659
Bram Moolenaar63a121b2005-12-11 21:36:39 +00003660 /* A client can tell us to redraw, but not to display the cursor, so do
3661 * that here. */
3662 setcursor();
3663 out_flush();
3664#ifdef FEAT_GUI
3665 if (gui.in_use)
3666 gui_update_cursor(FALSE, FALSE);
3667#endif
3668
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003669 return res;
3670}
3671
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003672/*
3673 * If conversion is needed, convert "data" from "client_enc" to 'encoding' and
3674 * return an allocated string. Otherwise return "data".
3675 * "*tofree" is set to the result when it needs to be freed later.
3676 */
3677/*ARGSUSED*/
3678 char_u *
3679serverConvert(client_enc, data, tofree)
3680 char_u *client_enc;
3681 char_u *data;
3682 char_u **tofree;
3683{
3684 char_u *res = data;
3685
3686 *tofree = NULL;
3687# ifdef FEAT_MBYTE
3688 if (client_enc != NULL && p_enc != NULL)
3689 {
3690 vimconv_T vimconv;
3691
3692 vimconv.vc_type = CONV_NONE;
3693 if (convert_setup(&vimconv, client_enc, p_enc) != FAIL
3694 && vimconv.vc_type != CONV_NONE)
3695 {
3696 res = string_convert(&vimconv, data, NULL);
3697 if (res == NULL)
3698 res = data;
3699 else
3700 *tofree = res;
3701 }
3702 convert_setup(&vimconv, NULL, NULL);
3703 }
3704# endif
3705 return res;
3706}
3707
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003708
3709/*
3710 * Make our basic server name: use the specified "arg" if given, otherwise use
3711 * the tail of the command "cmd" we were started with.
3712 * Return the name in allocated memory. This doesn't include a serial number.
3713 */
3714 static char_u *
3715serverMakeName(arg, cmd)
3716 char_u *arg;
3717 char *cmd;
3718{
3719 char_u *p;
3720
3721 if (arg != NULL && *arg != NUL)
3722 p = vim_strsave_up(arg);
3723 else
3724 {
3725 p = vim_strsave_up(gettail((char_u *)cmd));
3726 /* Remove .exe or .bat from the name. */
3727 if (p != NULL && vim_strchr(p, '.') != NULL)
3728 *vim_strchr(p, '.') = NUL;
3729 }
3730 return p;
3731}
3732#endif /* FEAT_CLIENTSERVER */
3733
3734/*
3735 * When FEAT_FKMAP is defined, also compile the Farsi source code.
3736 */
3737#if defined(FEAT_FKMAP) || defined(PROTO)
3738# include "farsi.c"
3739#endif
3740
3741/*
3742 * When FEAT_ARABIC is defined, also compile the Arabic source code.
3743 */
3744#if defined(FEAT_ARABIC) || defined(PROTO)
3745# include "arabic.c"
3746#endif