blob: 81e41b692f144c690f63667ef10626a4000a018a [file] [log] [blame]
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10#if defined(MSDOS) || defined(WIN32) || defined(_WIN64)
11# include <io.h> /* for close() and dup() */
12#endif
13
14#define EXTERN
15#include "vim.h"
16
17#ifdef SPAWNO
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +000018# include <spawno.h> /* special MS-DOS swapping library */
Bram Moolenaarb4210b32004-06-13 14:51:16 +000019#endif
20
21#ifdef HAVE_FCNTL_H
22# include <fcntl.h>
23#endif
24
25#ifdef __CYGWIN__
26# ifndef WIN32
27# include <sys/cygwin.h> /* for cygwin_conv_to_posix_path() */
28# endif
29# include <limits.h>
30#endif
31
Bram Moolenaarc013cb62005-07-24 21:18:31 +000032/* Maximum number of commands from + or -c arguments. */
33#define MAX_ARG_CMDS 10
34
Bram Moolenaar58d98232005-07-23 22:25:46 +000035/* Struct for various parameters passed between main() and other functions. */
36typedef struct
37{
Bram Moolenaarc013cb62005-07-24 21:18:31 +000038 int argc;
39 char **argv;
40
41 int evim_mode; /* started as "evim" */
Bram Moolenaarc013cb62005-07-24 21:18:31 +000042 char_u *use_vimrc; /* vimrc from -u argument */
43
44 int n_commands; /* no. of commands from + or -c */
45 char_u *commands[MAX_ARG_CMDS]; /* commands from + or -c arg. */
46 char_u cmds_tofree[MAX_ARG_CMDS]; /* commands that need free() */
47 int n_pre_commands; /* no. of commands from --cmd */
48 char_u *pre_commands[MAX_ARG_CMDS]; /* commands from --cmd argument */
49
50 int edit_type; /* type of editing to do */
51 char_u *tagname; /* tag from -t argument */
52#ifdef FEAT_QUICKFIX
53 char_u *use_ef; /* 'errorfile' from -q argument */
54#endif
55
56 int want_full_screen;
57 int stdout_isatty; /* is stdout a terminal? */
58 char_u *term; /* specified terminal name */
59#ifdef FEAT_CRYPT
60 int ask_for_key; /* -x argument */
61#endif
62 int no_swap_file; /* "-n" argument used */
63#ifdef FEAT_EVAL
64 int use_debug_break_level;
65#endif
66#ifdef FEAT_WINDOWS
67 int window_count; /* number of windows to use */
68 int vert_windows; /* "-O" used instead of "-o" */
69#endif
70
71#ifdef FEAT_CLIENTSERVER
Bram Moolenaar58d98232005-07-23 22:25:46 +000072 int serverArg; /* TRUE when argument for a server */
73 char_u *serverName_arg; /* cmdline arg for server name */
Bram Moolenaarc013cb62005-07-24 21:18:31 +000074 char_u *serverStr; /* remote server command */
75 char_u *serverStrEnc; /* encoding of serverStr */
76 char_u *servername; /* allocated name for our server */
77#endif
78#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
79 int literal; /* don't expand file names */
80#endif
81#ifdef MSWIN
82 int full_path; /* file name argument was full path */
83#endif
84#ifdef FEAT_DIFF
85 int diff_mode; /* start with 'diff' set */
86#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +000087} mparm_T;
88
Bram Moolenaarc013cb62005-07-24 21:18:31 +000089/* Values for edit_type. */
90#define EDIT_NONE 0 /* no edit type yet */
91#define EDIT_FILE 1 /* file name argument[s] given, use argument list */
92#define EDIT_STDIN 2 /* read file from stdin */
93#define EDIT_TAG 3 /* tag name argument given, use tagname */
94#define EDIT_QF 4 /* start in quickfix mode */
95
Bram Moolenaarb4210b32004-06-13 14:51:16 +000096#if defined(UNIX) || defined(VMS)
97static int file_owned __ARGS((char *fname));
98#endif
99static void mainerr __ARGS((int, char_u *));
100static void main_msg __ARGS((char *s));
101static void usage __ARGS((void));
102static int get_number_arg __ARGS((char_u *p, int *idx, int def));
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000103#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
104static void init_locale __ARGS((void));
105#endif
106static void parse_command_name __ARGS((mparm_T *parmp));
107static void early_arg_scan __ARGS((mparm_T *parmp));
108static void command_line_scan __ARGS((mparm_T *parmp));
109static void check_tty __ARGS((mparm_T *parmp));
110static void read_stdin __ARGS((void));
111static void create_windows __ARGS((mparm_T *parmp));
112#ifdef FEAT_WINDOWS
113static void edit_buffers __ARGS((mparm_T *parmp));
114#endif
115static void exe_pre_commands __ARGS((mparm_T *parmp));
116static void exe_commands __ARGS((mparm_T *parmp));
Bram Moolenaar58d98232005-07-23 22:25:46 +0000117static void source_startup_scripts __ARGS((mparm_T *parmp));
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000118static void main_start_gui __ARGS((void));
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000119#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000120static void check_swap_exists_action __ARGS((void));
121#endif
122#ifdef FEAT_CLIENTSERVER
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000123static void exec_on_server __ARGS((mparm_T *parmp));
124static void prepare_server __ARGS((mparm_T *parmp));
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000125static void cmdsrv_main __ARGS((int *argc, char **argv, char_u *serverName_arg, char_u **serverStr));
126static char_u *serverMakeName __ARGS((char_u *arg, char *cmd));
127#endif
128
129
130#ifdef STARTUPTIME
131static FILE *time_fd = NULL;
132#endif
133
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000134/*
135 * Different types of error messages.
136 */
137static char *(main_errors[]) =
138{
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000139 N_("Unknown option argument"),
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000140#define ME_UNKNOWN_OPTION 0
141 N_("Too many edit arguments"),
142#define ME_TOO_MANY_ARGS 1
143 N_("Argument missing after"),
144#define ME_ARG_MISSING 2
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000145 N_("Garbage after option argument"),
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000146#define ME_GARBAGE 3
147 N_("Too many \"+command\", \"-c command\" or \"--cmd command\" arguments"),
148#define ME_EXTRA_CMD 4
149 N_("Invalid argument for"),
150#define ME_INVALID_ARG 5
151};
152
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000153#ifndef PROTO /* don't want a prototype for main() */
154 int
155# ifdef VIMDLL
156_export
157# endif
158# ifdef FEAT_GUI_MSWIN
159# ifdef __BORLANDC__
160_cdecl
161# endif
162VimMain
163# else
164main
165# endif
166(argc, argv)
167 int argc;
168 char **argv;
169{
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000170 char_u *fname = NULL; /* file name from command line */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000171 mparm_T params; /* various parameters passed between
172 * main() and other functions. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000173
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000174 /*
175 * Do any system-specific initialisations. These can NOT use IObuff or
176 * NameBuff. Thus emsg2() cannot be called!
177 */
178 mch_early_init();
179
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000180 /* Many variables are in "params" so that we can pass them to invoked
181 * functions without a lot of arguments. "argc" and "argv" are also
182 * copied, so that they can be changed. */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000183 vim_memset(&params, 0, sizeof(params));
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000184 params.argc = argc;
185 params.argv = argv;
186 params.want_full_screen = TRUE;
187#ifdef FEAT_EVAL
188 params.use_debug_break_level = -1;
189#endif
190#ifdef FEAT_WINDOWS
191 params.window_count = -1;
192 params.vert_windows = MAYBE;
193#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +0000194
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000195#ifdef FEAT_TCL
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000196 vim_tcl_init(params.argv[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000197#endif
198
199#ifdef MEM_PROFILE
200 atexit(vim_mem_profile_dump);
201#endif
202
203#ifdef STARTUPTIME
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000204 time_fd = mch_fopen(STARTUPTIME, "a");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000205 TIME_MSG("--- VIM STARTING ---");
206#endif
207
208#ifdef __EMX__
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000209 _wildcard(&params.argc, &params.argv);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000210#endif
211
212#ifdef FEAT_MBYTE
213 (void)mb_init(); /* init mb_bytelen_tab[] to ones */
214#endif
Bram Moolenaardcaf10e2005-01-21 11:55:25 +0000215#ifdef FEAT_EVAL
216 eval_init(); /* init global variables */
217#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000218
219#ifdef __QNXNTO__
220 qnx_init(); /* PhAttach() for clipboard, (and gui) */
221#endif
222
223#ifdef MAC_OS_CLASSIC
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000224 /* Prepare for possibly starting GUI sometime */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000225 /* Macintosh needs this before any memory is allocated. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000226 gui_prepare(&params.argc, params.argv);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000227 TIME_MSG("GUI prepared");
228#endif
229
230 /* Init the table of Normal mode commands. */
231 init_normal_cmds();
232
233#if defined(HAVE_DATE_TIME) && defined(VMS) && defined(VAXC)
Bram Moolenaar58d98232005-07-23 22:25:46 +0000234 make_version(); /* Construct the long version string. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000235#endif
236
237 /*
238 * Allocate space for the generic buffers (needed for set_init_1() and
239 * EMSG2()).
240 */
241 if ((IObuff = alloc(IOSIZE)) == NULL
242 || (NameBuff = alloc(MAXPATHL)) == NULL)
243 mch_exit(0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000244 TIME_MSG("Allocated generic buffers");
245
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000246#ifdef NBDEBUG
247 /* Wait a moment for debugging NetBeans. Must be after allocating
248 * NameBuff. */
249 nbdebug_log_init("SPRO_GVIM_DEBUG", "SPRO_GVIM_DLEVEL");
250 nbdebug_wait(WT_ENV | WT_WAIT | WT_STOP, "SPRO_GVIM_WAIT", 20);
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000251 TIME_MSG("NetBeans debug wait");
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000252#endif
253
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000254#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
255 /*
256 * Setup to use the current locale (for ctype() and many other things).
257 * NOTE: Translated messages with encodings other than latin1 will not
258 * work until set_init_1() has been called!
259 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000260 init_locale();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000261 TIME_MSG("locale set");
262#endif
263
264#ifdef FEAT_GUI
265 gui.dofork = TRUE; /* default is to use fork() */
266#endif
267
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000268 /*
Bram Moolenaar58d98232005-07-23 22:25:46 +0000269 * Do a first scan of the arguments in "argv[]":
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000270 * -display or --display
Bram Moolenaar58d98232005-07-23 22:25:46 +0000271 * --server...
272 * --socketid
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000273 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000274 early_arg_scan(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000275
276#ifdef FEAT_SUN_WORKSHOP
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000277 findYourself(params.argv[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000278#endif
279#if defined(FEAT_GUI) && !defined(MAC_OS_CLASSIC)
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000280 /* Prepare for possibly starting GUI sometime */
281 gui_prepare(&params.argc, params.argv);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000282 TIME_MSG("GUI prepared");
283#endif
284
285#ifdef FEAT_CLIPBOARD
286 clip_init(FALSE); /* Initialise clipboard stuff */
287 TIME_MSG("clipboard setup");
288#endif
289
290 /*
291 * Check if we have an interactive window.
292 * On the Amiga: If there is no window, we open one with a newcli command
293 * (needed for :! to * work). mch_check_win() will also handle the -d or
294 * -dev argument.
295 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000296 params.stdout_isatty = (mch_check_win(params.argc, params.argv) != FAIL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000297 TIME_MSG("window checked");
298
299 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000300 * Allocate the first window and buffer.
301 * Can't do anything without it, exit when it fails.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000302 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000303 if (win_alloc_first() == FAIL)
304 mch_exit(0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000305
306 init_yank(); /* init yank buffers */
307
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000308 alist_init(&global_alist); /* Init the argument list to empty. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000309
310 /*
311 * Set the default values for the options.
312 * NOTE: Non-latin1 translated messages are working only after this,
313 * because this is where "has_mbyte" will be set, which is used by
314 * msg_outtrans_len_attr().
315 * First find out the home directory, needed to expand "~" in options.
316 */
317 init_homedir(); /* find real value of $HOME */
318 set_init_1();
319 TIME_MSG("inits 1");
320
321#ifdef FEAT_EVAL
322 set_lang_var(); /* set v:lang and v:ctype */
323#endif
324
325#ifdef FEAT_CLIENTSERVER
326 /*
327 * Do the client-server stuff, unless "--servername ''" was used.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000328 * This may exit Vim if the command was sent to the server.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000329 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000330 exec_on_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000331#endif
332
333 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000334 * Figure out the way to work from the command name argv[0].
335 * "vimdiff" starts diff mode, "rvim" sets "restricted", etc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000336 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000337 parse_command_name(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000338
339 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000340 * Process the command line arguments. File names are put in the global
341 * argument list "global_alist".
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000342 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000343 command_line_scan(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000344 TIME_MSG("parsing arguments");
345
346 /*
347 * On some systems, when we compile with the GUI, we always use it. On Mac
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000348 * there is no terminal version, and on Windows we can't fork one off with
349 * :gui.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000350 */
351#ifdef ALWAYS_USE_GUI
352 gui.starting = TRUE;
353#else
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000354# if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000355 /*
356 * Check if the GUI can be started. Reset gui.starting if not.
357 * Don't know about other systems, stay on the safe side and don't check.
358 */
359 if (gui.starting && gui_init_check() == FAIL)
360 {
361 gui.starting = FALSE;
362
363 /* When running "evim" or "gvim -y" we need the menus, exit if we
364 * don't have them. */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000365 if (params.evim_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000366 mch_exit(1);
367 }
368# endif
369#endif
370
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000371 if (GARGCOUNT > 0)
372 {
373#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
374 /*
375 * Expand wildcards in file names.
376 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000377 if (!params.literal)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000378 {
379 /* Temporarily add '(' and ')' to 'isfname'. These are valid
380 * filename characters but are excluded from 'isfname' to make
381 * "gf" work on a file name in parenthesis (e.g.: see vim.h). */
382 do_cmdline_cmd((char_u *)":set isf+=(,)");
Bram Moolenaar86b68352004-12-27 21:59:20 +0000383 alist_expand(NULL, 0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000384 do_cmdline_cmd((char_u *)":set isf&");
385 }
386#endif
387 fname = alist_name(&GARGLIST[0]);
388 }
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000389
390#if defined(WIN32) && defined(FEAT_MBYTE)
391 {
392 extern void set_alist_count(void);
393
394 /* Remember the number of entries in the argument list. If it changes
395 * we don't react on setting 'encoding'. */
396 set_alist_count();
397 }
398#endif
399
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000400#ifdef MSWIN
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000401 if (GARGCOUNT == 1 && params.full_path)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000402 {
403 /*
404 * If there is one filename, fully qualified, we have very probably
405 * been invoked from explorer, so change to the file's directory.
406 * Hint: to avoid this when typing a command use a forward slash.
407 * If the cd fails, it doesn't matter.
408 */
409 (void)vim_chdirfile(fname);
410 }
411#endif
412 TIME_MSG("expanding arguments");
413
414#ifdef FEAT_DIFF
Bram Moolenaar231334e2005-07-25 20:46:57 +0000415 if (params.diff_mode)
416 {
417 if (params.window_count == -1)
418 params.window_count = 0; /* open up to 3 windows */
419 if (params.vert_windows == MAYBE)
420 params.vert_windows = TRUE; /* use vertical split */
421 }
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 Moolenaarc013cb62005-07-24 21:18:31 +0000452 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000453#endif
454
455 /*
456 * mch_init() sets up the terminal (window) for use. This must be
457 * done after resetting full_screen, otherwise it may move the cursor
458 * (MSDOS).
459 * Note that we may use mch_exit() before mch_init()!
460 */
461 mch_init();
462 TIME_MSG("shell init");
463
464#ifdef USE_XSMP
465 /*
466 * For want of anywhere else to do it, try to connect to xsmp here.
467 * Fitting it in after gui_mch_init, but before gui_init (via termcapinit).
468 * Hijacking -X 'no X connection' to also disable XSMP connection as that
469 * has a similar delay upon failure.
470 * Only try if SESSION_MANAGER is set to something non-null.
471 */
472 if (!x_no_connect)
473 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000474 char *p = getenv("SESSION_MANAGER");
475
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000476 if (p != NULL && *p != NUL)
477 {
478 xsmp_init();
479 TIME_MSG("xsmp init");
480 }
481 }
482#endif
483
484 /*
485 * Print a warning if stdout is not a terminal.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000486 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000487 check_tty(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000488
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000489 /* This message comes before term inits, but after setting "silent_mode"
490 * when the input is not a tty. */
491 if (GARGCOUNT > 1 && !silent_mode)
492 printf(_("%d files to edit\n"), GARGCOUNT);
493
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000494 if (params.want_full_screen && !silent_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000495 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000496 termcapinit(params.term); /* set terminal name and get terminal
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000497 capabilities (will set full_screen) */
498 screen_start(); /* don't know where cursor is now */
499 TIME_MSG("Termcap init");
500 }
501
502 /*
503 * Set the default values for the options that use Rows and Columns.
504 */
505 ui_get_shellsize(); /* inits Rows and Columns */
506#ifdef FEAT_NETBEANS_INTG
507 if (usingNetbeans)
508 Columns += 2; /* leave room for glyph gutter */
509#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000510 win_init_size();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000511#ifdef FEAT_DIFF
512 /* Set the 'diff' option now, so that it can be checked for in a .vimrc
513 * file. There is no buffer yet though. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000514 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000515 diff_win_options(firstwin, FALSE);
516#endif
517
518 cmdline_row = Rows - p_ch;
519 msg_row = cmdline_row;
520 screenalloc(FALSE); /* allocate screen buffers */
521 set_init_2();
522 TIME_MSG("inits 2");
523
524 msg_scroll = TRUE;
525 no_wait_return = TRUE;
526
527 init_mappings(); /* set up initial mappings */
528
529 init_highlight(TRUE, FALSE); /* set the default highlight groups */
530 TIME_MSG("init highlight");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000531
532#ifdef FEAT_EVAL
533 /* Set the break level after the terminal is initialized. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000534 debug_break_level = params.use_debug_break_level;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000535#endif
536
Bram Moolenaar58d98232005-07-23 22:25:46 +0000537 /* Execute --cmd arguments. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000538 exe_pre_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000539
Bram Moolenaar58d98232005-07-23 22:25:46 +0000540 /* Source startup scripts. */
541 source_startup_scripts(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000542
543#ifdef FEAT_EVAL
544 /*
545 * Read all the plugin files.
546 * Only when compiled with +eval, since most plugins need it.
547 */
548 if (p_lpl)
549 {
Bram Moolenaar07d4d732005-10-03 22:04:08 +0000550 source_runtime((char_u *)"plugin/**/*.vim", TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000551 TIME_MSG("loading plugins");
552 }
553#endif
554
555 /*
556 * Recovery mode without a file name: List swap files.
557 * This uses the 'dir' option, therefore it must be after the
558 * initializations.
559 */
560 if (recoverymode && fname == NULL)
561 {
562 recover_names(NULL, TRUE, 0);
563 mch_exit(0);
564 }
565
566 /*
567 * Set a few option defaults after reading .vimrc files:
568 * 'title' and 'icon', Unix: 'shellpipe' and 'shellredir'.
569 */
570 set_init_3();
571 TIME_MSG("inits 3");
572
573 /*
574 * "-n" argument: Disable swap file by setting 'updatecount' to 0.
575 * Note that this overrides anything from a vimrc file.
576 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000577 if (params.no_swap_file)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000578 p_uc = 0;
579
580#ifdef FEAT_FKMAP
581 if (curwin->w_p_rl && p_altkeymap)
582 {
583 p_hkmap = FALSE; /* Reset the Hebrew keymap mode */
584# ifdef FEAT_ARABIC
585 curwin->w_p_arab = FALSE; /* Reset the Arabic keymap mode */
586# endif
587 p_fkmap = TRUE; /* Set the Farsi keymap mode */
588 }
589#endif
590
591#ifdef FEAT_GUI
592 if (gui.starting)
593 {
594#if defined(UNIX) || defined(VMS)
595 /* When something caused a message from a vimrc script, need to output
596 * an extra newline before the shell prompt. */
597 if (did_emsg || msg_didout)
598 putchar('\n');
599#endif
600
601 gui_start(); /* will set full_screen to TRUE */
602 TIME_MSG("starting GUI");
603
604 /* When running "evim" or "gvim -y" we need the menus, exit if we
605 * don't have them. */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000606 if (!gui.in_use && params.evim_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000607 mch_exit(1);
608 }
609#endif
610
611#ifdef SPAWNO /* special MSDOS swapping library */
612 init_SPAWNO("", SWAP_ANY);
613#endif
614
615#ifdef FEAT_VIMINFO
616 /*
617 * Read in registers, history etc, but not marks, from the viminfo file
618 */
619 if (*p_viminfo != NUL)
620 {
621 read_viminfo(NULL, TRUE, FALSE, FALSE);
622 TIME_MSG("reading viminfo");
623 }
624#endif
625
626#ifdef FEAT_QUICKFIX
627 /*
628 * "-q errorfile": Load the error file now.
629 * If the error file can't be read, exit before doing anything else.
630 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000631 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000632 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000633 if (params.use_ef != NULL)
634 set_string_option_direct((char_u *)"ef", -1,
635 params.use_ef, OPT_FREE);
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000636 if (qf_init(NULL, p_ef, p_efm, TRUE) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000637 {
638 out_char('\n');
639 mch_exit(3);
640 }
641 TIME_MSG("reading errorfile");
642 }
643#endif
644
645 /*
646 * Start putting things on the screen.
647 * Scroll screen down before drawing over it
648 * Clear screen now, so file message will not be cleared.
649 */
650 starting = NO_BUFFERS;
651 no_wait_return = FALSE;
652 if (!exmode_active)
653 msg_scroll = FALSE;
654
655#ifdef FEAT_GUI
656 /*
657 * This seems to be required to make callbacks to be called now, instead
658 * of after things have been put on the screen, which then may be deleted
659 * when getting a resize callback.
660 * For the Mac this handles putting files dropped on the Vim icon to
661 * global_alist.
662 */
663 if (gui.in_use)
664 {
665# ifdef FEAT_SUN_WORKSHOP
666 if (!usingSunWorkShop)
667# endif
668 gui_wait_for_chars(50L);
669 TIME_MSG("GUI delay");
670 }
671#endif
672
673#if defined(FEAT_GUI_PHOTON) && defined(FEAT_CLIPBOARD)
674 qnx_clip_init();
675#endif
676
677#ifdef FEAT_XCLIPBOARD
678 /* Start using the X clipboard, unless the GUI was started. */
679# ifdef FEAT_GUI
680 if (!gui.in_use)
681# endif
682 {
683 setup_term_clip();
684 TIME_MSG("setup clipboard");
685 }
686#endif
687
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000688#ifdef FEAT_CLIENTSERVER
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000689 /* Prepare for being a Vim server. */
690 prepare_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000691#endif
692
693 /*
694 * If "-" argument given: Read file from stdin.
695 * Do this before starting Raw mode, because it may change things that the
696 * writing end of the pipe doesn't like, e.g., in case stdin and stderr
697 * are the same terminal: "cat | vim -".
698 * Using autocommands here may cause trouble...
699 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000700 if (params.edit_type == EDIT_STDIN && !recoverymode)
701 read_stdin();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000702
703#if defined(UNIX) || defined(VMS)
704 /* When switching screens and something caused a message from a vimrc
705 * script, need to output an extra newline on exit. */
706 if ((did_emsg || msg_didout) && *T_TI != NUL)
707 newline_on_exit = TRUE;
708#endif
709
710 /*
711 * When done something that is not allowed or error message call
712 * wait_return. This must be done before starttermcap(), because it may
713 * switch to another screen. It must be done after settmode(TMODE_RAW),
714 * because we want to react on a single key stroke.
715 * Call settmode and starttermcap here, so the T_KS and T_TI may be
716 * defined by termcapinit and redifined in .exrc.
717 */
718 settmode(TMODE_RAW);
719 TIME_MSG("setting raw mode");
720
721 if (need_wait_return || msg_didany)
722 {
723 wait_return(TRUE);
724 TIME_MSG("waiting for return");
725 }
726
727 starttermcap(); /* start termcap if not done by wait_return() */
728 TIME_MSG("start termcap");
729
730#ifdef FEAT_MOUSE
731 setmouse(); /* may start using the mouse */
732#endif
733 if (scroll_region)
734 scroll_region_reset(); /* In case Rows changed */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000735 scroll_start(); /* may scroll the screen to the right position */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000736
737 /*
738 * Don't clear the screen when starting in Ex mode, unless using the GUI.
739 */
740 if (exmode_active
741#ifdef FEAT_GUI
742 && !gui.in_use
743#endif
744 )
745 must_redraw = CLEAR;
746 else
747 {
748 screenclear(); /* clear screen */
749 TIME_MSG("clearing screen");
750 }
751
752#ifdef FEAT_CRYPT
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000753 if (params.ask_for_key)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000754 {
755 (void)get_crypt_key(TRUE, TRUE);
756 TIME_MSG("getting crypt key");
757 }
758#endif
759
760 no_wait_return = TRUE;
761
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000762 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000763 * Create the requested number of windows and edit buffers in them.
764 * Also does recovery if "recoverymode" set.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000765 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000766 create_windows(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000767 TIME_MSG("opening buffers");
768
769 /* Ex starts at last line of the file */
770 if (exmode_active)
771 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
772
773#ifdef FEAT_AUTOCMD
774 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
775 TIME_MSG("BufEnter autocommands");
776#endif
777 setpcmark();
778
779#ifdef FEAT_QUICKFIX
780 /*
781 * When started with "-q errorfile" jump to first error now.
782 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000783 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000784 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000785 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000786 TIME_MSG("jump to first error");
787 }
788#endif
789
790#ifdef FEAT_WINDOWS
791 /*
792 * If opened more than one window, start editing files in the other
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000793 * windows.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000794 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000795 edit_buffers(&params);
796#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000797
798#ifdef FEAT_DIFF
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000799 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000800 {
801 win_T *wp;
802
803 /* set options in each window for "vimdiff". */
804 for (wp = firstwin; wp != NULL; wp = wp->w_next)
805 diff_win_options(wp, TRUE);
806 }
807#endif
808
809 /*
810 * Shorten any of the filenames, but only when absolute.
811 */
812 shorten_fnames(FALSE);
813
814 /*
815 * Need to jump to the tag before executing the '-c command'.
816 * Makes "vim -c '/return' -t main" work.
817 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000818 if (params.tagname != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000819 {
Bram Moolenaar146522e2005-12-16 21:55:46 +0000820#if defined(HAS_SWAP_EXISTS_ACTION)
821 swap_exists_did_quit = FALSE;
822#endif
823
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000824 vim_snprintf((char *)IObuff, IOSIZE, "ta %s", params.tagname);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000825 do_cmdline_cmd(IObuff);
826 TIME_MSG("jumping to tag");
Bram Moolenaar146522e2005-12-16 21:55:46 +0000827
828#if defined(HAS_SWAP_EXISTS_ACTION)
829 /* If the user doesn't want to edit the file then we quit here. */
830 if (swap_exists_did_quit)
831 getout(1);
832#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000833 }
834
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000835 /* Execute any "+", "-c" and "-S" arguments. */
836 if (params.n_commands > 0)
837 exe_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000838
839 RedrawingDisabled = 0;
840 redraw_all_later(NOT_VALID);
841 no_wait_return = FALSE;
842 starting = 0;
843
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000844#ifdef FEAT_TERMRESPONSE
845 /* Requesting the termresponse is postponed until here, so that a "-c q"
846 * argument doesn't make it appear in the shell Vim was started from. */
847 may_req_termresponse();
848#endif
849
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000850 /* start in insert mode */
851 if (p_im)
852 need_start_insertmode = TRUE;
853
854#ifdef FEAT_AUTOCMD
855 apply_autocmds(EVENT_VIMENTER, NULL, NULL, FALSE, curbuf);
856 TIME_MSG("VimEnter autocommands");
857#endif
858
859#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
860 /* When a startup script or session file setup for diff'ing and
861 * scrollbind, sync the scrollbind now. */
862 if (curwin->w_p_diff && curwin->w_p_scb)
863 {
864 update_topline();
865 check_scrollbind((linenr_T)0, 0L);
866 TIME_MSG("diff scrollbinding");
867 }
868#endif
869
870#if defined(WIN3264) && !defined(FEAT_GUI_W32)
871 mch_set_winsize_now(); /* Allow winsize changes from now on */
872#endif
873
874 /* If ":startinsert" command used, stuff a dummy command to be able to
875 * call normal_cmd(), which will then start Insert mode. */
876 if (restart_edit != 0)
Bram Moolenaarebefac62005-12-28 22:39:57 +0000877 stuffcharReadbuff(K_NOP);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000878
879#ifdef FEAT_NETBEANS_INTG
880 if (usingNetbeans)
881 /* Tell the client that it can start sending commands. */
882 netbeans_startup_done();
883#endif
884
885 TIME_MSG("before starting main loop");
886
887 /*
888 * Call the main command loop. This never returns.
889 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000890 main_loop(FALSE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000891
892 return 0;
893}
894#endif /* PROTO */
895
896/*
897 * Main loop: Execute Normal mode commands until exiting Vim.
898 * Also used to handle commands in the command-line window, until the window
899 * is closed.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000900 * Also used to handle ":visual" command after ":global": execute Normal mode
901 * commands, return when entering Ex mode. "noexmode" is TRUE then.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000902 */
903 void
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000904main_loop(cmdwin, noexmode)
905 int cmdwin; /* TRUE when working in the command-line window */
906 int noexmode; /* TRUE when return on entering Ex mode */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000907{
908 oparg_T oa; /* operator arguments */
909
910#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
911 /* Setup to catch a terminating error from the X server. Just ignore
912 * it, restore the state and continue. This might not always work
913 * properly, but at least we don't exit unexpectedly when the X server
914 * exists while Vim is running in a console. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000915 if (!cmdwin && !noexmode && SETJMP(x_jump_env))
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000916 {
917 State = NORMAL;
918# ifdef FEAT_VISUAL
919 VIsual_active = FALSE;
920# endif
921 got_int = TRUE;
922 need_wait_return = FALSE;
923 global_busy = FALSE;
924 exmode_active = 0;
925 skip_redraw = FALSE;
926 RedrawingDisabled = 0;
927 no_wait_return = 0;
928# ifdef FEAT_EVAL
929 emsg_skip = 0;
930# endif
931 emsg_off = 0;
932# ifdef FEAT_MOUSE
933 setmouse();
934# endif
935 settmode(TMODE_RAW);
936 starttermcap();
937 scroll_start();
938 redraw_later_clear();
939 }
940#endif
941
942 clear_oparg(&oa);
943 while (!cmdwin
944#ifdef FEAT_CMDWIN
945 || cmdwin_result == 0
946#endif
947 )
948 {
949 if (stuff_empty())
950 {
951 did_check_timestamps = FALSE;
952 if (need_check_timestamps)
953 check_timestamps(FALSE);
954 if (need_wait_return) /* if wait_return still needed ... */
955 wait_return(FALSE); /* ... call it now */
956 if (need_start_insertmode && goto_im()
957#ifdef FEAT_VISUAL
958 && !VIsual_active
959#endif
960 )
961 {
962 need_start_insertmode = FALSE;
963 stuffReadbuff((char_u *)"i"); /* start insert mode next */
964 /* skip the fileinfo message now, because it would be shown
965 * after insert mode finishes! */
966 need_fileinfo = FALSE;
967 }
968 }
969 if (got_int && !global_busy)
970 {
971 if (!quit_more)
972 (void)vgetc(); /* flush all buffers */
973 got_int = FALSE;
974 }
975 if (!exmode_active)
976 msg_scroll = FALSE;
977 quit_more = FALSE;
978
979 /*
980 * If skip redraw is set (for ":" in wait_return()), don't redraw now.
981 * If there is nothing in the stuff_buffer or do_redraw is TRUE,
982 * update cursor and redraw.
983 */
984 if (skip_redraw || exmode_active)
985 skip_redraw = FALSE;
986 else if (do_redraw || stuff_empty())
987 {
Bram Moolenaar3d0a6032006-02-09 23:54:54 +0000988#ifdef FEAT_AUTOCMD
989 /* Trigger CursorMoved if the cursor moved. */
990 if (!finish_op && has_cursormoved()
991 && !equalpos(last_cursormoved, curwin->w_cursor))
992
993 {
994 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL, FALSE, curbuf);
995 last_cursormoved = curwin->w_cursor;
996 }
997#endif
998
Bram Moolenaar33aec762006-01-22 23:30:12 +0000999#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
1000 /* Scroll-binding for diff mode may have been postponed until
1001 * here. Avoids doing it for every change. */
1002 if (diff_need_scrollbind)
1003 {
1004 check_scrollbind((linenr_T)0, 0L);
1005 diff_need_scrollbind = FALSE;
1006 }
1007#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001008#if defined(FEAT_FOLDING) && defined(FEAT_VISUAL)
1009 /* Include a closed fold completely in the Visual area. */
1010 foldAdjustVisual();
1011#endif
1012#ifdef FEAT_FOLDING
1013 /*
1014 * When 'foldclose' is set, apply 'foldlevel' to folds that don't
1015 * contain the cursor.
1016 * When 'foldopen' is "all", open the fold(s) under the cursor.
1017 * This may mark the window for redrawing.
1018 */
1019 if (hasAnyFolding(curwin) && !char_avail())
1020 {
1021 foldCheckClose();
1022 if (fdo_flags & FDO_ALL)
1023 foldOpenCursor();
1024 }
1025#endif
1026
1027 /*
1028 * Before redrawing, make sure w_topline is correct, and w_leftcol
1029 * if lines don't wrap, and w_skipcol if lines wrap.
1030 */
1031 update_topline();
1032 validate_cursor();
1033
1034#ifdef FEAT_VISUAL
1035 if (VIsual_active)
1036 update_curbuf(INVERTED);/* update inverted part */
1037 else
1038#endif
1039 if (must_redraw)
1040 update_screen(0);
1041 else if (redraw_cmdline || clear_cmdline)
1042 showmode();
1043#ifdef FEAT_WINDOWS
1044 redraw_statuslines();
1045#endif
1046#ifdef FEAT_TITLE
1047 if (need_maketitle)
1048 maketitle();
1049#endif
1050 /* display message after redraw */
1051 if (keep_msg != NULL)
1052 {
1053 char_u *p;
1054
1055 /* msg_attr_keep() will set keep_msg to NULL, must free the
1056 * string here. */
1057 p = keep_msg;
1058 msg_attr(p, keep_msg_attr);
1059 vim_free(p);
1060 }
1061 if (need_fileinfo) /* show file info after redraw */
1062 {
1063 fileinfo(FALSE, TRUE, FALSE);
1064 need_fileinfo = FALSE;
1065 }
1066
1067 emsg_on_display = FALSE; /* can delete error message now */
1068 did_emsg = FALSE;
1069 msg_didany = FALSE; /* reset lines_left in msg_start() */
Bram Moolenaar661b1822005-07-28 22:36:45 +00001070 may_clear_sb_text(); /* clear scroll-back text on next msg */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001071 showruler(FALSE);
1072
1073 setcursor();
1074 cursor_on();
1075
1076 do_redraw = FALSE;
1077 }
1078#ifdef FEAT_GUI
1079 if (need_mouse_correct)
1080 gui_mouse_correct();
1081#endif
1082
1083 /*
1084 * Update w_curswant if w_set_curswant has been set.
1085 * Postponed until here to avoid computing w_virtcol too often.
1086 */
1087 update_curswant();
1088
1089 /*
1090 * If we're invoked as ex, do a round of ex commands.
1091 * Otherwise, get and execute a normal mode command.
1092 */
1093 if (exmode_active)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001094 {
1095 if (noexmode) /* End of ":global/path/visual" commands */
1096 return;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001097 do_exmode(exmode_active == EXMODE_VIM);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001098 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001099 else
1100 normal_cmd(&oa, TRUE);
1101 }
1102}
1103
1104
1105#if defined(USE_XSMP) || defined(FEAT_GUI_MSWIN) || defined(PROTO)
1106/*
1107 * Exit, but leave behind swap files for modified buffers.
1108 */
1109 void
1110getout_preserve_modified(exitval)
1111 int exitval;
1112{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001113# if defined(SIGHUP) && defined(SIG_IGN)
1114 /* Ignore SIGHUP, because a dropped connection causes a read error, which
1115 * makes Vim exit and then handling SIGHUP causes various reentrance
1116 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001117 signal(SIGHUP, SIG_IGN);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001118# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001119
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001120 ml_close_notmod(); /* close all not-modified buffers */
1121 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
1122 ml_close_all(FALSE); /* close all memfiles, without deleting */
1123 getout(exitval); /* exit Vim properly */
1124}
1125#endif
1126
1127
1128/* Exit properly */
1129 void
1130getout(exitval)
1131 int exitval;
1132{
1133#ifdef FEAT_AUTOCMD
1134 buf_T *buf;
1135 win_T *wp;
1136#endif
1137
1138 exiting = TRUE;
1139
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001140 /* When running in Ex mode an error causes us to exit with a non-zero exit
1141 * code. POSIX requires this, although it's not 100% clear from the
1142 * standard. */
1143 if (exmode_active)
1144 exitval += ex_exitval;
1145
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001146 /* Position the cursor on the last screen line, below all the text */
1147#ifdef FEAT_GUI
1148 if (!gui.in_use)
1149#endif
1150 windgoto((int)Rows - 1, 0);
1151
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +00001152#if defined(FEAT_EVAL) || defined(FEAT_SYN_HL)
1153 /* Optionally print hashtable efficiency. */
1154 hash_debug_results();
1155#endif
1156
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001157#ifdef FEAT_GUI
1158 msg_didany = FALSE;
1159#endif
1160
1161#ifdef FEAT_AUTOCMD
1162 /* Trigger BufWinLeave for all windows, but only once per buffer. */
1163 for (wp = firstwin; wp != NULL; )
1164 {
1165 buf = wp->w_buffer;
1166 if (buf->b_changedtick != -1)
1167 {
1168 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
1169 FALSE, buf);
1170 buf->b_changedtick = -1; /* note that we did it already */
1171 wp = firstwin; /* restart, window may be closed */
1172 }
Bram Moolenaar33aec762006-01-22 23:30:12 +00001173# ifdef FEAT_WINDOWS
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001174 else
1175 wp = wp->w_next;
Bram Moolenaar33aec762006-01-22 23:30:12 +00001176# else
1177 break;
1178# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001179 }
Bram Moolenaar33aec762006-01-22 23:30:12 +00001180
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001181 /* Trigger BufUnload for buffers that are loaded */
1182 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1183 if (buf->b_ml.ml_mfp != NULL)
1184 {
1185 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
1186 FALSE, buf);
1187 if (!buf_valid(buf)) /* autocmd may delete the buffer */
1188 break;
1189 }
1190 apply_autocmds(EVENT_VIMLEAVEPRE, NULL, NULL, FALSE, curbuf);
1191#endif
1192
1193#ifdef FEAT_VIMINFO
1194 if (*p_viminfo != NUL)
1195 /* Write out the registers, history, marks etc, to the viminfo file */
1196 write_viminfo(NULL, FALSE);
1197#endif
1198
1199#ifdef FEAT_AUTOCMD
1200 apply_autocmds(EVENT_VIMLEAVE, NULL, NULL, FALSE, curbuf);
1201#endif
1202
Bram Moolenaar05159a02005-02-26 23:04:13 +00001203#ifdef FEAT_PROFILE
1204 profile_dump();
1205#endif
1206
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001207 if (did_emsg
1208#ifdef FEAT_GUI
1209 || (gui.in_use && msg_didany && p_verbose > 0)
1210#endif
1211 )
1212 {
1213 /* give the user a chance to read the (error) message */
1214 no_wait_return = FALSE;
1215 wait_return(FALSE);
1216 }
1217
1218#ifdef FEAT_AUTOCMD
1219 /* Position the cursor again, the autocommands may have moved it */
1220# ifdef FEAT_GUI
1221 if (!gui.in_use)
1222# endif
1223 windgoto((int)Rows - 1, 0);
1224#endif
1225
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001226#ifdef FEAT_MZSCHEME
1227 mzscheme_end();
1228#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001229#ifdef FEAT_TCL
1230 tcl_end();
1231#endif
1232#ifdef FEAT_RUBY
1233 ruby_end();
1234#endif
1235#ifdef FEAT_PYTHON
1236 python_end();
1237#endif
1238#ifdef FEAT_PERL
1239 perl_end();
1240#endif
1241#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
1242 iconv_end();
1243#endif
1244#ifdef FEAT_NETBEANS_INTG
1245 netbeans_end();
1246#endif
1247
1248 mch_exit(exitval);
1249}
1250
1251/*
1252 * Get a (optional) count for a Vim argument.
1253 */
1254 static int
1255get_number_arg(p, idx, def)
1256 char_u *p; /* pointer to argument */
1257 int *idx; /* index in argument, is incremented */
1258 int def; /* default value */
1259{
1260 if (vim_isdigit(p[*idx]))
1261 {
1262 def = atoi((char *)&(p[*idx]));
1263 while (vim_isdigit(p[*idx]))
1264 *idx = *idx + 1;
1265 }
1266 return def;
1267}
1268
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001269#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1270/*
1271 * Setup to use the current locale (for ctype() and many other things).
1272 */
1273 static void
1274init_locale()
1275{
1276 setlocale(LC_ALL, "");
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001277# ifdef WIN32
1278 /* Apparently MS-Windows printf() may cause a crash when we give it 8-bit
1279 * text while it's expecting text in the current locale. This call avoids
1280 * that. */
1281 setlocale(LC_CTYPE, "C");
1282# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001283
1284# ifdef FEAT_GETTEXT
1285 {
1286 int mustfree = FALSE;
1287 char_u *p;
1288
1289# ifdef DYNAMIC_GETTEXT
1290 /* Initialize the gettext library */
1291 dyn_libintl_init(NULL);
1292# endif
1293 /* expand_env() doesn't work yet, because chartab[] is not initialized
1294 * yet, call vim_getenv() directly */
1295 p = vim_getenv((char_u *)"VIMRUNTIME", &mustfree);
1296 if (p != NULL && *p != NUL)
1297 {
1298 STRCPY(NameBuff, p);
1299 STRCAT(NameBuff, "/lang");
1300 bindtextdomain(VIMPACKAGE, (char *)NameBuff);
1301 }
1302 if (mustfree)
1303 vim_free(p);
1304 textdomain(VIMPACKAGE);
1305 }
1306# endif
1307}
1308#endif
1309
1310/*
1311 * Check for: [r][e][g][vi|vim|view][diff][ex[im]]
1312 * If the executable name starts with "r" we disable shell commands.
1313 * If the next character is "e" we run in Easy mode.
1314 * If the next character is "g" we run the GUI version.
1315 * If the next characters are "view" we start in readonly mode.
1316 * If the next characters are "diff" or "vimdiff" we start in diff mode.
1317 * If the next characters are "ex" we start in Ex mode. If it's followed
1318 * by "im" use improved Ex mode.
1319 */
1320 static void
1321parse_command_name(parmp)
1322 mparm_T *parmp;
1323{
1324 char_u *initstr;
1325
1326 initstr = gettail((char_u *)parmp->argv[0]);
1327
1328#ifdef MACOS_X_UNIX
1329 /* An issue has been seen when launching Vim in such a way that
1330 * $PWD/$ARGV[0] or $ARGV[0] is not the absolute path to the
1331 * executable or a symbolic link of it. Until this issue is resolved
1332 * we prohibit the GUI from being used.
1333 */
1334 if (STRCMP(initstr, parmp->argv[0]) == 0)
1335 disallow_gui = TRUE;
1336
1337 /* TODO: On MacOS X default to gui if argv[0] ends in:
1338 * /vim.app/Contents/MacOS/Vim */
1339#endif
1340
1341#ifdef FEAT_EVAL
1342 set_vim_var_string(VV_PROGNAME, initstr, -1);
1343#endif
1344
1345 if (TOLOWER_ASC(initstr[0]) == 'r')
1346 {
1347 restricted = TRUE;
1348 ++initstr;
1349 }
1350
1351 /* Avoid using evim mode for "editor". */
1352 if (TOLOWER_ASC(initstr[0]) == 'e'
1353 && (TOLOWER_ASC(initstr[1]) == 'v'
1354 || TOLOWER_ASC(initstr[1]) == 'g'))
1355 {
1356#ifdef FEAT_GUI
1357 gui.starting = TRUE;
1358#endif
1359 parmp->evim_mode = TRUE;
1360 ++initstr;
1361 }
1362
1363 if (TOLOWER_ASC(initstr[0]) == 'g' || initstr[0] == 'k')
1364 {
1365 main_start_gui();
1366#ifdef FEAT_GUI
1367 ++initstr;
1368#endif
1369 }
1370
1371 if (STRNICMP(initstr, "view", 4) == 0)
1372 {
1373 readonlymode = TRUE;
1374 curbuf->b_p_ro = TRUE;
1375 p_uc = 10000; /* don't update very often */
1376 initstr += 4;
1377 }
1378 else if (STRNICMP(initstr, "vim", 3) == 0)
1379 initstr += 3;
1380
1381 /* Catch "[r][g]vimdiff" and "[r][g]viewdiff". */
1382 if (STRICMP(initstr, "diff") == 0)
1383 {
1384#ifdef FEAT_DIFF
1385 parmp->diff_mode = TRUE;
1386#else
1387 mch_errmsg(_("This Vim was not compiled with the diff feature."));
1388 mch_errmsg("\n");
1389 mch_exit(2);
1390#endif
1391 }
1392
1393 if (STRNICMP(initstr, "ex", 2) == 0)
1394 {
1395 if (STRNICMP(initstr + 2, "im", 2) == 0)
1396 exmode_active = EXMODE_VIM;
1397 else
1398 exmode_active = EXMODE_NORMAL;
1399 change_compatible(TRUE); /* set 'compatible' */
1400 }
1401}
1402
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001403/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00001404 * Get the name of the display, before gui_prepare() removes it from
1405 * argv[]. Used for the xterm-clipboard display.
1406 *
1407 * Also find the --server... arguments and --socketid
1408 */
1409/*ARGSUSED*/
1410 static void
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001411early_arg_scan(parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001412 mparm_T *parmp;
1413{
1414#if defined(FEAT_XCLIPBOARD) || defined(FEAT_CLIENTSERVER)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001415 int argc = parmp->argc;
1416 char **argv = parmp->argv;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001417 int i;
1418
1419 for (i = 1; i < argc; i++)
1420 {
1421 if (STRCMP(argv[i], "--") == 0)
1422 break;
1423# ifdef FEAT_XCLIPBOARD
1424 else if (STRICMP(argv[i], "-display") == 0
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001425# if defined(FEAT_GUI_GTK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001426 || STRICMP(argv[i], "--display") == 0
1427# endif
1428 )
1429 {
1430 if (i == argc - 1)
1431 mainerr_arg_missing((char_u *)argv[i]);
1432 xterm_display = argv[++i];
1433 }
1434# endif
1435# ifdef FEAT_CLIENTSERVER
1436 else if (STRICMP(argv[i], "--servername") == 0)
1437 {
1438 if (i == argc - 1)
1439 mainerr_arg_missing((char_u *)argv[i]);
1440 parmp->serverName_arg = (char_u *)argv[++i];
1441 }
1442 else if (STRICMP(argv[i], "--serverlist") == 0
1443 || STRICMP(argv[i], "--remote-send") == 0
1444 || STRICMP(argv[i], "--remote-expr") == 0
1445 || STRICMP(argv[i], "--remote") == 0
1446 || STRICMP(argv[i], "--remote-silent") == 0)
1447 parmp->serverArg = TRUE;
1448 else if (STRICMP(argv[i], "--remote-wait") == 0
1449 || STRICMP(argv[i], "--remote-wait-silent") == 0)
1450 {
1451 parmp->serverArg = TRUE;
1452#ifdef FEAT_GUI
1453 /* don't fork() when starting the GUI to edit the files ourself */
1454 gui.dofork = FALSE;
1455#endif
1456 }
1457# endif
1458# ifdef FEAT_GUI_GTK
1459 else if (STRICMP(argv[i], "--socketid") == 0)
1460 {
1461 unsigned int socket_id;
1462 int count;
1463
1464 if (i == argc - 1)
1465 mainerr_arg_missing((char_u *)argv[i]);
1466 if (STRNICMP(argv[i+1], "0x", 2) == 0)
1467 count = sscanf(&(argv[i + 1][2]), "%x", &socket_id);
1468 else
1469 count = sscanf(argv[i+1], "%u", &socket_id);
1470 if (count != 1)
1471 mainerr(ME_INVALID_ARG, (char_u *)argv[i]);
1472 else
1473 gtk_socket_id = socket_id;
1474 i++;
1475 }
1476 else if (STRICMP(argv[i], "--echo-wid") == 0)
1477 echo_wid_arg = TRUE;
1478# endif
1479 }
1480#endif
1481}
1482
1483/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001484 * Scan the command line arguments.
1485 */
1486 static void
1487command_line_scan(parmp)
1488 mparm_T *parmp;
1489{
1490 int argc = parmp->argc;
1491 char **argv = parmp->argv;
1492 int argv_idx; /* index in argv[n][] */
1493 int had_minmin = FALSE; /* found "--" argument */
1494 int want_argument; /* option argument with argument */
1495 int c;
Bram Moolenaar231334e2005-07-25 20:46:57 +00001496 char_u *p = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001497 long n;
1498
1499 --argc;
1500 ++argv;
1501 argv_idx = 1; /* active option letter is argv[0][argv_idx] */
1502 while (argc > 0)
1503 {
1504 /*
1505 * "+" or "+{number}" or "+/{pat}" or "+{command}" argument.
1506 */
1507 if (argv[0][0] == '+' && !had_minmin)
1508 {
1509 if (parmp->n_commands >= MAX_ARG_CMDS)
1510 mainerr(ME_EXTRA_CMD, NULL);
1511 argv_idx = -1; /* skip to next argument */
1512 if (argv[0][1] == NUL)
1513 parmp->commands[parmp->n_commands++] = (char_u *)"$";
1514 else
1515 parmp->commands[parmp->n_commands++] = (char_u *)&(argv[0][1]);
1516 }
1517
1518 /*
1519 * Optional argument.
1520 */
1521 else if (argv[0][0] == '-' && !had_minmin)
1522 {
1523 want_argument = FALSE;
1524 c = argv[0][argv_idx++];
1525#ifdef VMS
1526 /*
1527 * VMS only uses upper case command lines. Interpret "-X" as "-x"
1528 * and "-/X" as "-X".
1529 */
1530 if (c == '/')
1531 {
1532 c = argv[0][argv_idx++];
1533 c = TOUPPER_ASC(c);
1534 }
1535 else
1536 c = TOLOWER_ASC(c);
1537#endif
1538 switch (c)
1539 {
1540 case NUL: /* "vim -" read from stdin */
1541 /* "ex -" silent mode */
1542 if (exmode_active)
1543 silent_mode = TRUE;
1544 else
1545 {
1546 if (parmp->edit_type != EDIT_NONE)
1547 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1548 parmp->edit_type = EDIT_STDIN;
1549 read_cmd_fd = 2; /* read from stderr instead of stdin */
1550 }
1551 argv_idx = -1; /* skip to next argument */
1552 break;
1553
1554 case '-': /* "--" don't take any more option arguments */
1555 /* "--help" give help message */
1556 /* "--version" give version message */
1557 /* "--literal" take files literally */
1558 /* "--nofork" don't fork */
1559 /* "--noplugin[s]" skip plugins */
1560 /* "--cmd <cmd>" execute cmd before vimrc */
1561 if (STRICMP(argv[0] + argv_idx, "help") == 0)
1562 usage();
1563 else if (STRICMP(argv[0] + argv_idx, "version") == 0)
1564 {
1565 Columns = 80; /* need to init Columns */
1566 info_message = TRUE; /* use mch_msg(), not mch_errmsg() */
1567 list_version();
1568 msg_putchar('\n');
1569 msg_didout = FALSE;
1570 mch_exit(0);
1571 }
1572 else if (STRNICMP(argv[0] + argv_idx, "literal", 7) == 0)
1573 {
1574#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
1575 parmp->literal = TRUE;
1576#endif
1577 }
1578 else if (STRNICMP(argv[0] + argv_idx, "nofork", 6) == 0)
1579 {
1580#ifdef FEAT_GUI
1581 gui.dofork = FALSE; /* don't fork() when starting GUI */
1582#endif
1583 }
1584 else if (STRNICMP(argv[0] + argv_idx, "noplugin", 8) == 0)
1585 p_lpl = FALSE;
1586 else if (STRNICMP(argv[0] + argv_idx, "cmd", 3) == 0)
1587 {
1588 want_argument = TRUE;
1589 argv_idx += 3;
1590 }
1591#ifdef FEAT_CLIENTSERVER
1592 else if (STRNICMP(argv[0] + argv_idx, "serverlist", 10) == 0)
1593 ; /* already processed -- no arg */
1594 else if (STRNICMP(argv[0] + argv_idx, "servername", 10) == 0
1595 || STRNICMP(argv[0] + argv_idx, "serversend", 10) == 0)
1596 {
1597 /* already processed -- snatch the following arg */
1598 if (argc > 1)
1599 {
1600 --argc;
1601 ++argv;
1602 }
1603 }
1604#endif
1605#ifdef FEAT_GUI_GTK
1606 else if (STRNICMP(argv[0] + argv_idx, "socketid", 8) == 0)
1607 {
1608 /* already processed -- snatch the following arg */
1609 if (argc > 1)
1610 {
1611 --argc;
1612 ++argv;
1613 }
1614 }
1615 else if (STRNICMP(argv[0] + argv_idx, "echo-wid", 8) == 0)
1616 {
1617 /* already processed, skip */
1618 }
1619#endif
1620 else
1621 {
1622 if (argv[0][argv_idx])
1623 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
1624 had_minmin = TRUE;
1625 }
1626 if (!want_argument)
1627 argv_idx = -1; /* skip to next argument */
1628 break;
1629
1630 case 'A': /* "-A" start in Arabic mode */
1631#ifdef FEAT_ARABIC
1632 set_option_value((char_u *)"arabic", 1L, NULL, 0);
1633#else
1634 mch_errmsg(_(e_noarabic));
1635 mch_exit(2);
1636#endif
1637 break;
1638
1639 case 'b': /* "-b" binary mode */
Bram Moolenaar231334e2005-07-25 20:46:57 +00001640 /* Needs to be effective before expanding file names, because
1641 * for Win32 this makes us edit a shortcut file itself,
1642 * instead of the file it links to. */
1643 set_options_bin(curbuf->b_p_bin, 1, 0);
1644 curbuf->b_p_bin = 1; /* binary file I/O */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001645 break;
1646
1647 case 'C': /* "-C" Compatible */
1648 change_compatible(TRUE);
1649 break;
1650
1651 case 'e': /* "-e" Ex mode */
1652 exmode_active = EXMODE_NORMAL;
1653 break;
1654
1655 case 'E': /* "-E" Improved Ex mode */
1656 exmode_active = EXMODE_VIM;
1657 break;
1658
1659 case 'f': /* "-f" GUI: run in foreground. Amiga: open
1660 window directly, not with newcli */
1661#ifdef FEAT_GUI
1662 gui.dofork = FALSE; /* don't fork() when starting GUI */
1663#endif
1664 break;
1665
1666 case 'g': /* "-g" start GUI */
1667 main_start_gui();
1668 break;
1669
1670 case 'F': /* "-F" start in Farsi mode: rl + fkmap set */
1671#ifdef FEAT_FKMAP
1672 curwin->w_p_rl = p_fkmap = TRUE;
1673#else
1674 mch_errmsg(_(e_nofarsi));
1675 mch_exit(2);
1676#endif
1677 break;
1678
1679 case 'h': /* "-h" give help message */
1680#ifdef FEAT_GUI_GNOME
1681 /* Tell usage() to exit for "gvim". */
1682 gui.starting = FALSE;
1683#endif
1684 usage();
1685 break;
1686
1687 case 'H': /* "-H" start in Hebrew mode: rl + hkmap set */
1688#ifdef FEAT_RIGHTLEFT
1689 curwin->w_p_rl = p_hkmap = TRUE;
1690#else
1691 mch_errmsg(_(e_nohebrew));
1692 mch_exit(2);
1693#endif
1694 break;
1695
1696 case 'l': /* "-l" lisp mode, 'lisp' and 'showmatch' on */
1697#ifdef FEAT_LISP
1698 set_option_value((char_u *)"lisp", 1L, NULL, 0);
1699 p_sm = TRUE;
1700#endif
1701 break;
1702
1703#ifdef TARGET_API_MAC_OSX
1704 /* For some reason on MacOS X, an argument like:
1705 -psn_0_10223617 is passed in when invoke from Finder
1706 or with the 'open' command */
1707 case 'p':
1708 argv_idx = -1; /* bypass full -psn */
1709 main_start_gui();
1710 break;
1711#endif
1712 case 'M': /* "-M" no changes or writing of files */
1713 reset_modifiable();
1714 /* FALLTHROUGH */
1715
1716 case 'm': /* "-m" no writing of files */
1717 p_write = FALSE;
1718 break;
1719
1720 case 'y': /* "-y" easy mode */
1721#ifdef FEAT_GUI
1722 gui.starting = TRUE; /* start GUI a bit later */
1723#endif
1724 parmp->evim_mode = TRUE;
1725 break;
1726
1727 case 'N': /* "-N" Nocompatible */
1728 change_compatible(FALSE);
1729 break;
1730
1731 case 'n': /* "-n" no swap file */
1732 parmp->no_swap_file = TRUE;
1733 break;
1734
1735 case 'o': /* "-o[N]" open N horizontal split windows */
1736#ifdef FEAT_WINDOWS
1737 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00001738 parmp->window_count = get_number_arg((char_u *)argv[0],
1739 &argv_idx, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001740 parmp->vert_windows = FALSE;
1741#endif
1742 break;
1743
1744 case 'O': /* "-O[N]" open N vertical split windows */
1745#if defined(FEAT_VERTSPLIT) && defined(FEAT_WINDOWS)
1746 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00001747 parmp->window_count = get_number_arg((char_u *)argv[0],
1748 &argv_idx, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001749 parmp->vert_windows = TRUE;
1750#endif
1751 break;
1752
1753#ifdef FEAT_QUICKFIX
1754 case 'q': /* "-q" QuickFix mode */
1755 if (parmp->edit_type != EDIT_NONE)
1756 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1757 parmp->edit_type = EDIT_QF;
1758 if (argv[0][argv_idx]) /* "-q{errorfile}" */
1759 {
1760 parmp->use_ef = (char_u *)argv[0] + argv_idx;
1761 argv_idx = -1;
1762 }
1763 else if (argc > 1) /* "-q {errorfile}" */
1764 want_argument = TRUE;
1765 break;
1766#endif
1767
1768 case 'R': /* "-R" readonly mode */
1769 readonlymode = TRUE;
1770 curbuf->b_p_ro = TRUE;
1771 p_uc = 10000; /* don't update very often */
1772 break;
1773
1774 case 'r': /* "-r" recovery mode */
1775 case 'L': /* "-L" recovery mode */
1776 recoverymode = 1;
1777 break;
1778
1779 case 's':
1780 if (exmode_active) /* "-s" silent (batch) mode */
1781 silent_mode = TRUE;
1782 else /* "-s {scriptin}" read from script file */
1783 want_argument = TRUE;
1784 break;
1785
1786 case 't': /* "-t {tag}" or "-t{tag}" jump to tag */
1787 if (parmp->edit_type != EDIT_NONE)
1788 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1789 parmp->edit_type = EDIT_TAG;
1790 if (argv[0][argv_idx]) /* "-t{tag}" */
1791 {
1792 parmp->tagname = (char_u *)argv[0] + argv_idx;
1793 argv_idx = -1;
1794 }
1795 else /* "-t {tag}" */
1796 want_argument = TRUE;
1797 break;
1798
1799#ifdef FEAT_EVAL
1800 case 'D': /* "-D" Debugging */
1801 parmp->use_debug_break_level = 9999;
1802 break;
1803#endif
1804#ifdef FEAT_DIFF
1805 case 'd': /* "-d" 'diff' */
1806# ifdef AMIGA
1807 /* check for "-dev {device}" */
1808 if (argv[0][argv_idx] == 'e' && argv[0][argv_idx + 1] == 'v')
1809 want_argument = TRUE;
1810 else
1811# endif
1812 parmp->diff_mode = TRUE;
1813 break;
1814#endif
1815 case 'V': /* "-V{N}" Verbose level */
1816 /* default is 10: a little bit verbose */
1817 p_verbose = get_number_arg((char_u *)argv[0], &argv_idx, 10);
1818 if (argv[0][argv_idx] != NUL)
1819 {
1820 set_option_value((char_u *)"verbosefile", 0L,
1821 (char_u *)argv[0] + argv_idx, 0);
1822 argv_idx = STRLEN(argv[0]);
1823 }
1824 break;
1825
1826 case 'v': /* "-v" Vi-mode (as if called "vi") */
1827 exmode_active = 0;
1828#ifdef FEAT_GUI
1829 gui.starting = FALSE; /* don't start GUI */
1830#endif
1831 break;
1832
1833 case 'w': /* "-w{number}" set window height */
1834 /* "-w {scriptout}" write to script */
1835 if (vim_isdigit(((char_u *)argv[0])[argv_idx]))
1836 {
1837 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
1838 set_option_value((char_u *)"window", n, NULL, 0);
1839 break;
1840 }
1841 want_argument = TRUE;
1842 break;
1843
1844#ifdef FEAT_CRYPT
1845 case 'x': /* "-x" encrypted reading/writing of files */
1846 parmp->ask_for_key = TRUE;
1847 break;
1848#endif
1849
1850 case 'X': /* "-X" don't connect to X server */
1851#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
1852 x_no_connect = TRUE;
1853#endif
1854 break;
1855
1856 case 'Z': /* "-Z" restricted mode */
1857 restricted = TRUE;
1858 break;
1859
1860 case 'c': /* "-c{command}" or "-c {command}" execute
1861 command */
1862 if (argv[0][argv_idx] != NUL)
1863 {
1864 if (parmp->n_commands >= MAX_ARG_CMDS)
1865 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar231334e2005-07-25 20:46:57 +00001866 parmp->commands[parmp->n_commands++] = (char_u *)argv[0]
1867 + argv_idx;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001868 argv_idx = -1;
1869 break;
1870 }
1871 /*FALLTHROUGH*/
1872 case 'S': /* "-S {file}" execute Vim script */
1873 case 'i': /* "-i {viminfo}" use for viminfo */
1874#ifndef FEAT_DIFF
1875 case 'd': /* "-d {device}" device (for Amiga) */
1876#endif
1877 case 'T': /* "-T {terminal}" terminal name */
1878 case 'u': /* "-u {vimrc}" vim inits file */
1879 case 'U': /* "-U {gvimrc}" gvim inits file */
1880 case 'W': /* "-W {scriptout}" overwrite */
1881#ifdef FEAT_GUI_W32
1882 case 'P': /* "-P {parent title}" MDI parent */
1883#endif
1884 want_argument = TRUE;
1885 break;
1886
1887 default:
1888 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
1889 }
1890
1891 /*
1892 * Handle option arguments with argument.
1893 */
1894 if (want_argument)
1895 {
1896 /*
1897 * Check for garbage immediately after the option letter.
1898 */
1899 if (argv[0][argv_idx] != NUL)
1900 mainerr(ME_GARBAGE, (char_u *)argv[0]);
1901
1902 --argc;
1903 if (argc < 1 && c != 'S')
1904 mainerr_arg_missing((char_u *)argv[0]);
1905 ++argv;
1906 argv_idx = -1;
1907
1908 switch (c)
1909 {
1910 case 'c': /* "-c {command}" execute command */
1911 case 'S': /* "-S {file}" execute Vim script */
1912 if (parmp->n_commands >= MAX_ARG_CMDS)
1913 mainerr(ME_EXTRA_CMD, NULL);
1914 if (c == 'S')
1915 {
1916 char *a;
1917
1918 if (argc < 1)
1919 /* "-S" without argument: use default session file
1920 * name. */
1921 a = SESSION_FILE;
1922 else if (argv[0][0] == '-')
1923 {
1924 /* "-S" followed by another option: use default
1925 * session file name. */
1926 a = SESSION_FILE;
1927 ++argc;
1928 --argv;
1929 }
1930 else
1931 a = argv[0];
1932 p = alloc((unsigned)(STRLEN(a) + 4));
1933 if (p == NULL)
1934 mch_exit(2);
1935 sprintf((char *)p, "so %s", a);
1936 parmp->cmds_tofree[parmp->n_commands] = TRUE;
1937 parmp->commands[parmp->n_commands++] = p;
1938 }
1939 else
Bram Moolenaar231334e2005-07-25 20:46:57 +00001940 parmp->commands[parmp->n_commands++] =
1941 (char_u *)argv[0];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001942 break;
1943
1944 case '-': /* "--cmd {command}" execute command */
1945 if (parmp->n_pre_commands >= MAX_ARG_CMDS)
1946 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar231334e2005-07-25 20:46:57 +00001947 parmp->pre_commands[parmp->n_pre_commands++] =
1948 (char_u *)argv[0];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001949 break;
1950
1951 /* case 'd': -d {device} is handled in mch_check_win() for the
1952 * Amiga */
1953
1954#ifdef FEAT_QUICKFIX
1955 case 'q': /* "-q {errorfile}" QuickFix mode */
1956 parmp->use_ef = (char_u *)argv[0];
1957 break;
1958#endif
1959
1960 case 'i': /* "-i {viminfo}" use for viminfo */
1961 use_viminfo = (char_u *)argv[0];
1962 break;
1963
1964 case 's': /* "-s {scriptin}" read from script file */
1965 if (scriptin[0] != NULL)
1966 {
1967scripterror:
1968 mch_errmsg(_("Attempt to open script file again: \""));
1969 mch_errmsg(argv[-1]);
1970 mch_errmsg(" ");
1971 mch_errmsg(argv[0]);
1972 mch_errmsg("\"\n");
1973 mch_exit(2);
1974 }
1975 if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL)
1976 {
1977 mch_errmsg(_("Cannot open for reading: \""));
1978 mch_errmsg(argv[0]);
1979 mch_errmsg("\"\n");
1980 mch_exit(2);
1981 }
1982 if (save_typebuf() == FAIL)
1983 mch_exit(2); /* out of memory */
1984 break;
1985
1986 case 't': /* "-t {tag}" */
1987 parmp->tagname = (char_u *)argv[0];
1988 break;
1989
1990 case 'T': /* "-T {terminal}" terminal name */
1991 /*
1992 * The -T term argument is always available and when
1993 * HAVE_TERMLIB is supported it overrides the environment
1994 * variable TERM.
1995 */
1996#ifdef FEAT_GUI
1997 if (term_is_gui((char_u *)argv[0]))
1998 gui.starting = TRUE; /* start GUI a bit later */
1999 else
2000#endif
2001 parmp->term = (char_u *)argv[0];
2002 break;
2003
2004 case 'u': /* "-u {vimrc}" vim inits file */
2005 parmp->use_vimrc = (char_u *)argv[0];
2006 break;
2007
2008 case 'U': /* "-U {gvimrc}" gvim inits file */
2009#ifdef FEAT_GUI
2010 use_gvimrc = (char_u *)argv[0];
2011#endif
2012 break;
2013
2014 case 'w': /* "-w {nr}" 'window' value */
2015 /* "-w {scriptout}" append to script file */
2016 if (vim_isdigit(*((char_u *)argv[0])))
2017 {
2018 argv_idx = 0;
2019 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2020 set_option_value((char_u *)"window", n, NULL, 0);
2021 argv_idx = -1;
2022 break;
2023 }
2024 /*FALLTHROUGH*/
2025 case 'W': /* "-W {scriptout}" overwrite script file */
2026 if (scriptout != NULL)
2027 goto scripterror;
2028 if ((scriptout = mch_fopen(argv[0],
2029 c == 'w' ? APPENDBIN : WRITEBIN)) == NULL)
2030 {
2031 mch_errmsg(_("Cannot open for script output: \""));
2032 mch_errmsg(argv[0]);
2033 mch_errmsg("\"\n");
2034 mch_exit(2);
2035 }
2036 break;
2037
2038#ifdef FEAT_GUI_W32
2039 case 'P': /* "-P {parent title}" MDI parent */
2040 gui_mch_set_parent(argv[0]);
2041 break;
2042#endif
2043 }
2044 }
2045 }
2046
2047 /*
2048 * File name argument.
2049 */
2050 else
2051 {
2052 argv_idx = -1; /* skip to next argument */
2053
2054 /* Check for only one type of editing. */
2055 if (parmp->edit_type != EDIT_NONE && parmp->edit_type != EDIT_FILE)
2056 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2057 parmp->edit_type = EDIT_FILE;
2058
2059#ifdef MSWIN
2060 /* Remember if the argument was a full path before changing
2061 * slashes to backslashes. */
2062 if (argv[0][0] != NUL && argv[0][1] == ':' && argv[0][2] == '\\')
2063 parmp->full_path = TRUE;
2064#endif
2065
2066 /* Add the file to the global argument list. */
2067 if (ga_grow(&global_alist.al_ga, 1) == FAIL
2068 || (p = vim_strsave((char_u *)argv[0])) == NULL)
2069 mch_exit(2);
2070#ifdef FEAT_DIFF
2071 if (parmp->diff_mode && mch_isdir(p) && GARGCOUNT > 0
2072 && !mch_isdir(alist_name(&GARGLIST[0])))
2073 {
2074 char_u *r;
2075
2076 r = concat_fnames(p, gettail(alist_name(&GARGLIST[0])), TRUE);
2077 if (r != NULL)
2078 {
2079 vim_free(p);
2080 p = r;
2081 }
2082 }
2083#endif
2084#if defined(__CYGWIN32__) && !defined(WIN32)
2085 /*
2086 * If vim is invoked by non-Cygwin tools, convert away any
2087 * DOS paths, so things like .swp files are created correctly.
2088 * Look for evidence of non-Cygwin paths before we bother.
2089 * This is only for when using the Unix files.
2090 */
2091 if (strpbrk(p, "\\:") != NULL)
2092 {
2093 char posix_path[PATH_MAX];
2094
2095 cygwin_conv_to_posix_path(p, posix_path);
2096 vim_free(p);
2097 p = vim_strsave(posix_path);
2098 if (p == NULL)
2099 mch_exit(2);
2100 }
2101#endif
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002102
2103#ifdef USE_FNAME_CASE
2104 /* Make the case of the file name match the actual file. */
2105 fname_case(p, 0);
2106#endif
2107
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002108 alist_add(&global_alist, p,
2109#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
Bram Moolenaar231334e2005-07-25 20:46:57 +00002110 parmp->literal ? 2 : 0 /* add buffer nr after exp. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002111#else
2112 2 /* add buffer number now and use curbuf */
2113#endif
2114 );
2115
2116#if defined(FEAT_MBYTE) && defined(WIN32)
2117 {
2118 extern void used_file_arg(char *, int, int);
2119
2120 /* Remember this argument has been added to the argument list.
2121 * Needed when 'encoding' is changed. */
2122 used_file_arg(argv[0], parmp->literal, parmp->full_path);
2123 }
2124#endif
2125 }
2126
2127 /*
2128 * If there are no more letters after the current "-", go to next
2129 * argument. argv_idx is set to -1 when the current argument is to be
2130 * skipped.
2131 */
2132 if (argv_idx <= 0 || argv[0][argv_idx] == NUL)
2133 {
2134 --argc;
2135 ++argv;
2136 argv_idx = 1;
2137 }
2138 }
2139}
2140
2141/*
2142 * Print a warning if stdout is not a terminal.
2143 * When starting in Ex mode and commands come from a file, set Silent mode.
2144 */
2145 static void
2146check_tty(parmp)
2147 mparm_T *parmp;
2148{
2149 int input_isatty; /* is active input a terminal? */
2150
2151 input_isatty = mch_input_isatty();
2152 if (exmode_active)
2153 {
2154 if (!input_isatty)
2155 silent_mode = TRUE;
2156 }
2157 else if (parmp->want_full_screen && (!parmp->stdout_isatty || !input_isatty)
2158#ifdef FEAT_GUI
2159 /* don't want the delay when started from the desktop */
2160 && !gui.starting
2161#endif
2162 )
2163 {
2164#ifdef NBDEBUG
2165 /*
2166 * This shouldn't be necessary. But if I run netbeans with the log
2167 * output coming to the console and XOpenDisplay fails, I get vim
2168 * trying to start with input/output to my console tty. This fills my
2169 * input buffer so fast I can't even kill the process in under 2
2170 * minutes (and it beeps continuosly the whole time :-)
2171 */
2172 if (usingNetbeans && (!parmp->stdout_isatty || !input_isatty))
2173 {
2174 mch_errmsg(_("Vim: Error: Failure to start gvim from NetBeans\n"));
2175 exit(1);
2176 }
2177#endif
2178 if (!parmp->stdout_isatty)
2179 mch_errmsg(_("Vim: Warning: Output is not to a terminal\n"));
2180 if (!input_isatty)
2181 mch_errmsg(_("Vim: Warning: Input is not from a terminal\n"));
2182 out_flush();
2183 if (scriptin[0] == NULL)
2184 ui_delay(2000L, TRUE);
2185 TIME_MSG("Warning delay");
2186 }
2187}
2188
2189/*
2190 * Read text from stdin.
2191 */
2192 static void
2193read_stdin()
2194{
2195 int i;
2196
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002197#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002198 /* When getting the ATTENTION prompt here, use a dialog */
2199 swap_exists_action = SEA_DIALOG;
2200#endif
2201 no_wait_return = TRUE;
2202 i = msg_didany;
2203 set_buflisted(TRUE);
2204 (void)open_buffer(TRUE, NULL); /* create memfile and read file */
2205 no_wait_return = FALSE;
2206 msg_didany = i;
2207 TIME_MSG("reading stdin");
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002208#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002209 check_swap_exists_action();
2210#endif
2211#if !(defined(AMIGA) || defined(MACOS))
2212 /*
2213 * Close stdin and dup it from stderr. Required for GPM to work
2214 * properly, and for running external commands.
2215 * Is there any other system that cannot do this?
2216 */
2217 close(0);
2218 dup(2);
2219#endif
2220}
2221
2222/*
2223 * Create the requested number of windows and edit buffers in them.
2224 * Also does recovery if "recoverymode" set.
2225 */
2226/*ARGSUSED*/
2227 static void
2228create_windows(parmp)
2229 mparm_T *parmp;
2230{
2231#ifdef FEAT_WINDOWS
2232 /*
2233 * Create the number of windows that was requested.
2234 */
2235 if (parmp->window_count == -1) /* was not set */
2236 parmp->window_count = 1;
2237 if (parmp->window_count == 0)
2238 parmp->window_count = GARGCOUNT;
2239 if (parmp->window_count > 1)
2240 {
2241 /* Don't change the windows if there was a command in .vimrc that
2242 * already split some windows */
2243 if (parmp->vert_windows == MAYBE)
2244 parmp->vert_windows = FALSE;
2245 if (firstwin->w_next == NULL)
2246 {
2247 parmp->window_count = make_windows(parmp->window_count,
2248 parmp->vert_windows);
2249 TIME_MSG("making windows");
2250 }
2251 else
2252 parmp->window_count = win_count();
2253 }
2254 else
2255 parmp->window_count = 1;
2256#endif
2257
2258 if (recoverymode) /* do recover */
2259 {
2260 msg_scroll = TRUE; /* scroll message up */
2261 ml_recover();
2262 if (curbuf->b_ml.ml_mfp == NULL) /* failed */
2263 getout(1);
2264 do_modelines(FALSE); /* do modelines */
2265 }
2266 else
2267 {
2268 /*
2269 * Open a buffer for windows that don't have one yet.
2270 * Commands in the .vimrc might have loaded a file or split the window.
2271 * Watch out for autocommands that delete a window.
2272 */
2273#ifdef FEAT_AUTOCMD
2274 /*
2275 * Don't execute Win/Buf Enter/Leave autocommands here
2276 */
2277 ++autocmd_no_enter;
2278 ++autocmd_no_leave;
2279#endif
2280#ifdef FEAT_WINDOWS
2281 for (curwin = firstwin; curwin != NULL; curwin = W_NEXT(curwin))
2282#endif
2283 {
2284 curbuf = curwin->w_buffer;
2285 if (curbuf->b_ml.ml_mfp == NULL)
2286 {
2287#ifdef FEAT_FOLDING
2288 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2289 if (p_fdls >= 0)
2290 curwin->w_p_fdl = p_fdls;
2291#endif
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002292#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002293 /* When getting the ATTENTION prompt here, use a dialog */
2294 swap_exists_action = SEA_DIALOG;
2295#endif
2296 set_buflisted(TRUE);
2297 (void)open_buffer(FALSE, NULL); /* create memfile, read file */
2298
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002299#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002300 check_swap_exists_action();
2301#endif
2302#ifdef FEAT_AUTOCMD
2303 curwin = firstwin; /* start again */
2304#endif
2305 }
2306#ifdef FEAT_WINDOWS
2307 ui_breakcheck();
2308 if (got_int)
2309 {
2310 (void)vgetc(); /* only break the file loading, not the rest */
2311 break;
2312 }
2313#endif
2314 }
2315#ifdef FEAT_AUTOCMD
2316 --autocmd_no_enter;
2317 --autocmd_no_leave;
2318#endif
2319#ifdef FEAT_WINDOWS
2320 curwin = firstwin;
2321 curbuf = curwin->w_buffer;
2322#endif
2323 }
2324}
2325
2326#ifdef FEAT_WINDOWS
2327 /*
2328 * If opened more than one window, start editing files in the other
2329 * windows. make_windows() has already opened the windows.
2330 */
2331 static void
2332edit_buffers(parmp)
2333 mparm_T *parmp;
2334{
2335 int arg_idx; /* index in argument list */
2336 int i;
2337
2338# ifdef FEAT_AUTOCMD
2339 /*
2340 * Don't execute Win/Buf Enter/Leave autocommands here
2341 */
2342 ++autocmd_no_enter;
2343 ++autocmd_no_leave;
2344# endif
2345 arg_idx = 1;
2346 for (i = 1; i < parmp->window_count; ++i)
2347 {
2348 if (curwin->w_next == NULL) /* just checking */
2349 break;
2350 win_enter(curwin->w_next, FALSE);
2351
2352 /* Only open the file if there is no file in this window yet (that can
2353 * happen when .vimrc contains ":sall") */
2354 if (curbuf == firstwin->w_buffer || curbuf->b_ffname == NULL)
2355 {
2356 curwin->w_arg_idx = arg_idx;
2357 /* edit file from arg list, if there is one */
2358 (void)do_ecmd(0, arg_idx < GARGCOUNT
2359 ? alist_name(&GARGLIST[arg_idx]) : NULL,
2360 NULL, NULL, ECMD_LASTL, ECMD_HIDE);
2361 if (arg_idx == GARGCOUNT - 1)
2362 arg_had_last = TRUE;
2363 ++arg_idx;
2364 }
2365 ui_breakcheck();
2366 if (got_int)
2367 {
2368 (void)vgetc(); /* only break the file loading, not the rest */
2369 break;
2370 }
2371 }
2372# ifdef FEAT_AUTOCMD
2373 --autocmd_no_enter;
2374# endif
2375 win_enter(firstwin, FALSE); /* back to first window */
2376# ifdef FEAT_AUTOCMD
2377 --autocmd_no_leave;
2378# endif
2379 TIME_MSG("editing files in windows");
2380 if (parmp->window_count > 1)
2381 win_equal(curwin, FALSE, 'b'); /* adjust heights */
2382}
2383#endif /* FEAT_WINDOWS */
2384
2385/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002386 * Execute the commands from --cmd arguments "cmds[cnt]".
2387 */
2388 static void
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002389exe_pre_commands(parmp)
2390 mparm_T *parmp;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002391{
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002392 char_u **cmds = parmp->pre_commands;
2393 int cnt = parmp->n_pre_commands;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002394 int i;
2395
2396 if (cnt > 0)
2397 {
2398 curwin->w_cursor.lnum = 0; /* just in case.. */
2399 sourcing_name = (char_u *)_("pre-vimrc command line");
2400# ifdef FEAT_EVAL
2401 current_SID = SID_CMDARG;
2402# endif
2403 for (i = 0; i < cnt; ++i)
2404 do_cmdline_cmd(cmds[i]);
2405 sourcing_name = NULL;
2406# ifdef FEAT_EVAL
2407 current_SID = 0;
2408# endif
2409 TIME_MSG("--cmd commands");
2410 }
2411}
2412
2413/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002414 * Execute "+", "-c" and "-S" arguments.
2415 */
2416 static void
2417exe_commands(parmp)
2418 mparm_T *parmp;
2419{
2420 int i;
2421
2422 /*
2423 * We start commands on line 0, make "vim +/pat file" match a
2424 * pattern on line 1. But don't move the cursor when an autocommand
2425 * with g`" was used.
2426 */
2427 msg_scroll = TRUE;
2428 if (parmp->tagname == NULL && curwin->w_cursor.lnum <= 1)
2429 curwin->w_cursor.lnum = 0;
2430 sourcing_name = (char_u *)"command line";
2431#ifdef FEAT_EVAL
2432 current_SID = SID_CARG;
2433#endif
2434 for (i = 0; i < parmp->n_commands; ++i)
2435 {
2436 do_cmdline_cmd(parmp->commands[i]);
2437 if (parmp->cmds_tofree[i])
2438 vim_free(parmp->commands[i]);
2439 }
2440 sourcing_name = NULL;
2441#ifdef FEAT_EVAL
2442 current_SID = 0;
2443#endif
2444 if (curwin->w_cursor.lnum == 0)
2445 curwin->w_cursor.lnum = 1;
2446
2447 if (!exmode_active)
2448 msg_scroll = FALSE;
2449
2450#ifdef FEAT_QUICKFIX
2451 /* When started with "-q errorfile" jump to first error again. */
2452 if (parmp->edit_type == EDIT_QF)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002453 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002454#endif
2455 TIME_MSG("executing command arguments");
2456}
2457
2458/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002459 * Source startup scripts.
2460 */
2461 static void
2462source_startup_scripts(parmp)
2463 mparm_T *parmp;
2464{
2465 int i;
2466
2467 /*
2468 * For "evim" source evim.vim first of all, so that the user can overrule
2469 * any things he doesn't like.
2470 */
2471 if (parmp->evim_mode)
2472 {
2473 (void)do_source((char_u *)EVIM_FILE, FALSE, FALSE);
2474 TIME_MSG("source evim file");
2475 }
2476
2477 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002478 * If -u argument given, use only the initializations from that file and
Bram Moolenaar58d98232005-07-23 22:25:46 +00002479 * nothing else.
2480 */
2481 if (parmp->use_vimrc != NULL)
2482 {
Bram Moolenaar231334e2005-07-25 20:46:57 +00002483 if (STRCMP(parmp->use_vimrc, "NONE") == 0
2484 || STRCMP(parmp->use_vimrc, "NORC") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002485 {
2486#ifdef FEAT_GUI
2487 if (use_gvimrc == NULL) /* don't load gvimrc either */
2488 use_gvimrc = parmp->use_vimrc;
2489#endif
2490 if (parmp->use_vimrc[2] == 'N')
2491 p_lpl = FALSE; /* don't load plugins either */
2492 }
2493 else
2494 {
2495 if (do_source(parmp->use_vimrc, FALSE, FALSE) != OK)
2496 EMSG2(_("E282: Cannot read from \"%s\""), parmp->use_vimrc);
2497 }
2498 }
2499 else if (!silent_mode)
2500 {
2501#ifdef AMIGA
2502 struct Process *proc = (struct Process *)FindTask(0L);
2503 APTR save_winptr = proc->pr_WindowPtr;
2504
2505 /* Avoid a requester here for a volume that doesn't exist. */
2506 proc->pr_WindowPtr = (APTR)-1L;
2507#endif
2508
2509 /*
2510 * Get system wide defaults, if the file name is defined.
2511 */
2512#ifdef SYS_VIMRC_FILE
2513 (void)do_source((char_u *)SYS_VIMRC_FILE, FALSE, FALSE);
2514#endif
2515
2516 /*
2517 * Try to read initialization commands from the following places:
2518 * - environment variable VIMINIT
2519 * - user vimrc file (s:.vimrc for Amiga, ~/.vimrc otherwise)
2520 * - second user vimrc file ($VIM/.vimrc for Dos)
2521 * - environment variable EXINIT
2522 * - user exrc file (s:.exrc for Amiga, ~/.exrc otherwise)
2523 * - second user exrc file ($VIM/.exrc for Dos)
2524 * The first that exists is used, the rest is ignored.
2525 */
2526 if (process_env((char_u *)"VIMINIT", TRUE) != OK)
2527 {
2528 if (do_source((char_u *)USR_VIMRC_FILE, TRUE, TRUE) == FAIL
2529#ifdef USR_VIMRC_FILE2
2530 && do_source((char_u *)USR_VIMRC_FILE2, TRUE, TRUE) == FAIL
2531#endif
2532#ifdef USR_VIMRC_FILE3
2533 && do_source((char_u *)USR_VIMRC_FILE3, TRUE, TRUE) == FAIL
2534#endif
2535 && process_env((char_u *)"EXINIT", FALSE) == FAIL
2536 && do_source((char_u *)USR_EXRC_FILE, FALSE, FALSE) == FAIL)
2537 {
2538#ifdef USR_EXRC_FILE2
2539 (void)do_source((char_u *)USR_EXRC_FILE2, FALSE, FALSE);
2540#endif
2541 }
2542 }
2543
2544 /*
2545 * Read initialization commands from ".vimrc" or ".exrc" in current
2546 * directory. This is only done if the 'exrc' option is set.
2547 * Because of security reasons we disallow shell and write commands
2548 * now, except for unix if the file is owned by the user or 'secure'
2549 * option has been reset in environment of global ".exrc" or ".vimrc".
2550 * Only do this if VIMRC_FILE is not the same as USR_VIMRC_FILE or
2551 * SYS_VIMRC_FILE.
2552 */
2553 if (p_exrc)
2554 {
2555#if defined(UNIX) || defined(VMS)
2556 /* If ".vimrc" file is not owned by user, set 'secure' mode. */
2557 if (!file_owned(VIMRC_FILE))
2558#endif
2559 secure = p_secure;
2560
2561 i = FAIL;
2562 if (fullpathcmp((char_u *)USR_VIMRC_FILE,
2563 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
2564#ifdef USR_VIMRC_FILE2
2565 && fullpathcmp((char_u *)USR_VIMRC_FILE2,
2566 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
2567#endif
2568#ifdef USR_VIMRC_FILE3
2569 && fullpathcmp((char_u *)USR_VIMRC_FILE3,
2570 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
2571#endif
2572#ifdef SYS_VIMRC_FILE
2573 && fullpathcmp((char_u *)SYS_VIMRC_FILE,
2574 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
2575#endif
2576 )
2577 i = do_source((char_u *)VIMRC_FILE, TRUE, TRUE);
2578
2579 if (i == FAIL)
2580 {
2581#if defined(UNIX) || defined(VMS)
2582 /* if ".exrc" is not owned by user set 'secure' mode */
2583 if (!file_owned(EXRC_FILE))
2584 secure = p_secure;
2585 else
2586 secure = 0;
2587#endif
2588 if ( fullpathcmp((char_u *)USR_EXRC_FILE,
2589 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
2590#ifdef USR_EXRC_FILE2
2591 && fullpathcmp((char_u *)USR_EXRC_FILE2,
2592 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
2593#endif
2594 )
2595 (void)do_source((char_u *)EXRC_FILE, FALSE, FALSE);
2596 }
2597 }
2598 if (secure == 2)
2599 need_wait_return = TRUE;
2600 secure = 0;
2601#ifdef AMIGA
2602 proc->pr_WindowPtr = save_winptr;
2603#endif
2604 }
2605 TIME_MSG("sourcing vimrc file(s)");
2606}
2607
2608/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002609 * Setup to start using the GUI. Exit with an error when not available.
2610 */
2611 static void
2612main_start_gui()
2613{
2614#ifdef FEAT_GUI
2615 gui.starting = TRUE; /* start GUI a bit later */
2616#else
2617 mch_errmsg(_(e_nogvim));
2618 mch_errmsg("\n");
2619 mch_exit(2);
2620#endif
2621}
2622
2623/*
2624 * Get an evironment variable, and execute it as Ex commands.
2625 * Returns FAIL if the environment variable was not executed, OK otherwise.
2626 */
2627 int
2628process_env(env, is_viminit)
2629 char_u *env;
2630 int is_viminit; /* when TRUE, called for VIMINIT */
2631{
2632 char_u *initstr;
2633 char_u *save_sourcing_name;
2634 linenr_T save_sourcing_lnum;
2635#ifdef FEAT_EVAL
2636 scid_T save_sid;
2637#endif
2638
2639 if ((initstr = mch_getenv(env)) != NULL && *initstr != NUL)
2640 {
2641 if (is_viminit)
2642 vimrc_found();
2643 save_sourcing_name = sourcing_name;
2644 save_sourcing_lnum = sourcing_lnum;
2645 sourcing_name = env;
2646 sourcing_lnum = 0;
2647#ifdef FEAT_EVAL
2648 save_sid = current_SID;
2649 current_SID = SID_ENV;
2650#endif
2651 do_cmdline_cmd(initstr);
2652 sourcing_name = save_sourcing_name;
2653 sourcing_lnum = save_sourcing_lnum;
2654#ifdef FEAT_EVAL
2655 current_SID = save_sid;;
2656#endif
2657 return OK;
2658 }
2659 return FAIL;
2660}
2661
2662#if defined(UNIX) || defined(VMS)
2663/*
2664 * Return TRUE if we are certain the user owns the file "fname".
2665 * Used for ".vimrc" and ".exrc".
2666 * Use both stat() and lstat() for extra security.
2667 */
2668 static int
2669file_owned(fname)
2670 char *fname;
2671{
2672 struct stat s;
2673# ifdef UNIX
2674 uid_t uid = getuid();
2675# else /* VMS */
2676 uid_t uid = ((getgid() << 16) | getuid());
2677# endif
2678
2679 return !(mch_stat(fname, &s) != 0 || s.st_uid != uid
2680# ifdef HAVE_LSTAT
2681 || mch_lstat(fname, &s) != 0 || s.st_uid != uid
2682# endif
2683 );
2684}
2685#endif
2686
2687/*
2688 * Give an error message main_errors["n"] and exit.
2689 */
2690 static void
2691mainerr(n, str)
2692 int n; /* one of the ME_ defines */
2693 char_u *str; /* extra argument or NULL */
2694{
2695#if defined(UNIX) || defined(__EMX__) || defined(VMS)
2696 reset_signals(); /* kill us with CTRL-C here, if you like */
2697#endif
2698
2699 mch_errmsg(longVersion);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002700 mch_errmsg("\n");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002701 mch_errmsg(_(main_errors[n]));
2702 if (str != NULL)
2703 {
2704 mch_errmsg(": \"");
2705 mch_errmsg((char *)str);
2706 mch_errmsg("\"");
2707 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002708 mch_errmsg(_("\nMore info with: \"vim -h\"\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002709
2710 mch_exit(1);
2711}
2712
2713 void
2714mainerr_arg_missing(str)
2715 char_u *str;
2716{
2717 mainerr(ME_ARG_MISSING, str);
2718}
2719
2720/*
2721 * print a message with three spaces prepended and '\n' appended.
2722 */
2723 static void
2724main_msg(s)
2725 char *s;
2726{
2727 mch_msg(" ");
2728 mch_msg(s);
2729 mch_msg("\n");
2730}
2731
2732/*
2733 * Print messages for "vim -h" or "vim --help" and exit.
2734 */
2735 static void
2736usage()
2737{
2738 int i;
2739 static char *(use[]) =
2740 {
2741 N_("[file ..] edit specified file(s)"),
2742 N_("- read text from stdin"),
2743 N_("-t tag edit file where tag is defined"),
2744#ifdef FEAT_QUICKFIX
2745 N_("-q [errorfile] edit file with first error")
2746#endif
2747 };
2748
2749#if defined(UNIX) || defined(__EMX__) || defined(VMS)
2750 reset_signals(); /* kill us with CTRL-C here, if you like */
2751#endif
2752
2753 mch_msg(longVersion);
2754 mch_msg(_("\n\nusage:"));
2755 for (i = 0; ; ++i)
2756 {
2757 mch_msg(_(" vim [arguments] "));
2758 mch_msg(_(use[i]));
2759 if (i == (sizeof(use) / sizeof(char_u *)) - 1)
2760 break;
2761 mch_msg(_("\n or:"));
2762 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002763#ifdef VMS
2764 mch_msg(_("where case is ignored prepend / to make flag upper case"));
2765#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002766
2767 mch_msg(_("\n\nArguments:\n"));
2768 main_msg(_("--\t\t\tOnly file names after this"));
2769#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
2770 main_msg(_("--literal\t\tDon't expand wildcards"));
2771#endif
2772#ifdef FEAT_OLE
2773 main_msg(_("-register\t\tRegister this gvim for OLE"));
2774 main_msg(_("-unregister\t\tUnregister gvim for OLE"));
2775#endif
2776#ifdef FEAT_GUI
2777 main_msg(_("-g\t\t\tRun using GUI (like \"gvim\")"));
2778 main_msg(_("-f or --nofork\tForeground: Don't fork when starting GUI"));
2779#endif
2780 main_msg(_("-v\t\t\tVi mode (like \"vi\")"));
2781 main_msg(_("-e\t\t\tEx mode (like \"ex\")"));
2782 main_msg(_("-s\t\t\tSilent (batch) mode (only for \"ex\")"));
2783#ifdef FEAT_DIFF
2784 main_msg(_("-d\t\t\tDiff mode (like \"vimdiff\")"));
2785#endif
2786 main_msg(_("-y\t\t\tEasy mode (like \"evim\", modeless)"));
2787 main_msg(_("-R\t\t\tReadonly mode (like \"view\")"));
2788 main_msg(_("-Z\t\t\tRestricted mode (like \"rvim\")"));
2789 main_msg(_("-m\t\t\tModifications (writing files) not allowed"));
2790 main_msg(_("-M\t\t\tModifications in text not allowed"));
2791 main_msg(_("-b\t\t\tBinary mode"));
2792#ifdef FEAT_LISP
2793 main_msg(_("-l\t\t\tLisp mode"));
2794#endif
2795 main_msg(_("-C\t\t\tCompatible with Vi: 'compatible'"));
2796 main_msg(_("-N\t\t\tNot fully Vi compatible: 'nocompatible'"));
2797 main_msg(_("-V[N]\t\tVerbose level"));
2798 main_msg(_("-D\t\t\tDebugging mode"));
2799 main_msg(_("-n\t\t\tNo swap file, use memory only"));
2800 main_msg(_("-r\t\t\tList swap files and exit"));
2801 main_msg(_("-r (with file name)\tRecover crashed session"));
2802 main_msg(_("-L\t\t\tSame as -r"));
2803#ifdef AMIGA
2804 main_msg(_("-f\t\t\tDon't use newcli to open window"));
2805 main_msg(_("-dev <device>\t\tUse <device> for I/O"));
2806#endif
2807#ifdef FEAT_ARABIC
2808 main_msg(_("-A\t\t\tstart in Arabic mode"));
2809#endif
2810#ifdef FEAT_RIGHTLEFT
2811 main_msg(_("-H\t\t\tStart in Hebrew mode"));
2812#endif
2813#ifdef FEAT_FKMAP
2814 main_msg(_("-F\t\t\tStart in Farsi mode"));
2815#endif
2816 main_msg(_("-T <terminal>\tSet terminal type to <terminal>"));
2817 main_msg(_("-u <vimrc>\t\tUse <vimrc> instead of any .vimrc"));
2818#ifdef FEAT_GUI
2819 main_msg(_("-U <gvimrc>\t\tUse <gvimrc> instead of any .gvimrc"));
2820#endif
2821 main_msg(_("--noplugin\t\tDon't load plugin scripts"));
2822 main_msg(_("-o[N]\t\tOpen N windows (default: one for each file)"));
2823 main_msg(_("-O[N]\t\tLike -o but split vertically"));
2824 main_msg(_("+\t\t\tStart at end of file"));
2825 main_msg(_("+<lnum>\t\tStart at line <lnum>"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002826 main_msg(_("--cmd <command>\tExecute <command> before loading any vimrc file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002827 main_msg(_("-c <command>\t\tExecute <command> after loading the first file"));
2828 main_msg(_("-S <session>\t\tSource file <session> after loading the first file"));
2829 main_msg(_("-s <scriptin>\tRead Normal mode commands from file <scriptin>"));
2830 main_msg(_("-w <scriptout>\tAppend all typed commands to file <scriptout>"));
2831 main_msg(_("-W <scriptout>\tWrite all typed commands to file <scriptout>"));
2832#ifdef FEAT_CRYPT
2833 main_msg(_("-x\t\t\tEdit encrypted files"));
2834#endif
2835#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
2836# if defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)
2837 main_msg(_("-display <display>\tConnect vim to this particular X-server"));
2838# endif
2839 main_msg(_("-X\t\t\tDo not connect to X server"));
2840#endif
2841#ifdef FEAT_CLIENTSERVER
2842 main_msg(_("--remote <files>\tEdit <files> in a Vim server if possible"));
2843 main_msg(_("--remote-silent <files> Same, don't complain if there is no server"));
2844 main_msg(_("--remote-wait <files> As --remote but wait for files to have been edited"));
2845 main_msg(_("--remote-wait-silent <files> Same, don't complain if there is no server"));
2846 main_msg(_("--remote-send <keys>\tSend <keys> to a Vim server and exit"));
2847 main_msg(_("--remote-expr <expr>\tEvaluate <expr> in a Vim server and print result"));
2848 main_msg(_("--serverlist\t\tList available Vim server names and exit"));
2849 main_msg(_("--servername <name>\tSend to/become the Vim server <name>"));
2850#endif
2851#ifdef FEAT_VIMINFO
2852 main_msg(_("-i <viminfo>\t\tUse <viminfo> instead of .viminfo"));
2853#endif
2854 main_msg(_("-h or --help\tPrint Help (this message) and exit"));
2855 main_msg(_("--version\t\tPrint version information and exit"));
2856
2857#ifdef FEAT_GUI_X11
2858# ifdef FEAT_GUI_MOTIF
2859 mch_msg(_("\nArguments recognised by gvim (Motif version):\n"));
2860# else
2861# ifdef FEAT_GUI_ATHENA
2862# ifdef FEAT_GUI_NEXTAW
2863 mch_msg(_("\nArguments recognised by gvim (neXtaw version):\n"));
2864# else
2865 mch_msg(_("\nArguments recognised by gvim (Athena version):\n"));
2866# endif
2867# endif
2868# endif
2869 main_msg(_("-display <display>\tRun vim on <display>"));
2870 main_msg(_("-iconic\t\tStart vim iconified"));
2871# if 0
2872 main_msg(_("-name <name>\t\tUse resource as if vim was <name>"));
2873 mch_msg(_("\t\t\t (Unimplemented)\n"));
2874# endif
2875 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
2876 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
2877 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
2878 main_msg(_("-boldfont <font>\tUse <font> for bold text"));
2879 main_msg(_("-italicfont <font>\tUse <font> for italic text"));
2880 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
2881 main_msg(_("-borderwidth <width>\tUse a border width of <width> (also: -bw)"));
2882 main_msg(_("-scrollbarwidth <width> Use a scrollbar width of <width> (also: -sw)"));
2883# ifdef FEAT_GUI_ATHENA
2884 main_msg(_("-menuheight <height>\tUse a menu bar height of <height> (also: -mh)"));
2885# endif
2886 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
2887 main_msg(_("+reverse\t\tDon't use reverse video (also: +rv)"));
2888 main_msg(_("-xrm <resource>\tSet the specified resource"));
2889#endif /* FEAT_GUI_X11 */
2890#if defined(FEAT_GUI) && defined(RISCOS)
2891 mch_msg(_("\nArguments recognised by gvim (RISC OS version):\n"));
2892 main_msg(_("--columns <number>\tInitial width of window in columns"));
2893 main_msg(_("--rows <number>\tInitial height of window in rows"));
2894#endif
2895#ifdef FEAT_GUI_GTK
2896 mch_msg(_("\nArguments recognised by gvim (GTK+ version):\n"));
2897 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
2898 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
2899 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
2900 main_msg(_("-display <display>\tRun vim on <display> (also: --display)"));
2901# ifdef HAVE_GTK2
2902 main_msg(_("--role <role>\tSet a unique role to identify the main window"));
2903# endif
2904 main_msg(_("--socketid <xid>\tOpen Vim inside another GTK widget"));
2905#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002906#ifdef FEAT_GUI_W32
2907 main_msg(_("-P <parent title>\tOpen Vim inside parent application"));
2908#endif
2909
2910#ifdef FEAT_GUI_GNOME
2911 /* Gnome gives extra messages for --help if we continue, but not for -h. */
2912 if (gui.starting)
2913 mch_msg("\n");
2914 else
2915#endif
2916 mch_exit(0);
2917}
2918
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002919#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002920/*
2921 * Check the result of the ATTENTION dialog:
2922 * When "Quit" selected, exit Vim.
2923 * When "Recover" selected, recover the file.
2924 */
2925 static void
2926check_swap_exists_action()
2927{
2928 if (swap_exists_action == SEA_QUIT)
2929 getout(1);
2930 handle_swap_exists(NULL);
2931}
2932#endif
2933
2934#if defined(STARTUPTIME) || defined(PROTO)
2935static void time_diff __ARGS((struct timeval *then, struct timeval *now));
2936
2937static struct timeval prev_timeval;
2938
2939/*
2940 * Save the previous time before doing something that could nest.
2941 * set "*tv_rel" to the time elapsed so far.
2942 */
2943 void
2944time_push(tv_rel, tv_start)
2945 void *tv_rel, *tv_start;
2946{
2947 *((struct timeval *)tv_rel) = prev_timeval;
2948 gettimeofday(&prev_timeval, NULL);
2949 ((struct timeval *)tv_rel)->tv_usec = prev_timeval.tv_usec
2950 - ((struct timeval *)tv_rel)->tv_usec;
2951 ((struct timeval *)tv_rel)->tv_sec = prev_timeval.tv_sec
2952 - ((struct timeval *)tv_rel)->tv_sec;
2953 if (((struct timeval *)tv_rel)->tv_usec < 0)
2954 {
2955 ((struct timeval *)tv_rel)->tv_usec += 1000000;
2956 --((struct timeval *)tv_rel)->tv_sec;
2957 }
2958 *(struct timeval *)tv_start = prev_timeval;
2959}
2960
2961/*
2962 * Compute the previous time after doing something that could nest.
2963 * Subtract "*tp" from prev_timeval;
2964 * Note: The arguments are (void *) to avoid trouble with systems that don't
2965 * have struct timeval.
2966 */
2967 void
2968time_pop(tp)
2969 void *tp; /* actually (struct timeval *) */
2970{
2971 prev_timeval.tv_usec -= ((struct timeval *)tp)->tv_usec;
2972 prev_timeval.tv_sec -= ((struct timeval *)tp)->tv_sec;
2973 if (prev_timeval.tv_usec < 0)
2974 {
2975 prev_timeval.tv_usec += 1000000;
2976 --prev_timeval.tv_sec;
2977 }
2978}
2979
2980 static void
2981time_diff(then, now)
2982 struct timeval *then;
2983 struct timeval *now;
2984{
2985 long usec;
2986 long msec;
2987
2988 usec = now->tv_usec - then->tv_usec;
2989 msec = (now->tv_sec - then->tv_sec) * 1000L + usec / 1000L,
2990 usec = usec % 1000L;
2991 fprintf(time_fd, "%03ld.%03ld", msec, usec >= 0 ? usec : usec + 1000L);
2992}
2993
2994 void
2995time_msg(msg, tv_start)
2996 char *msg;
2997 void *tv_start; /* only for do_source: start time; actually
2998 (struct timeval *) */
2999{
3000 static struct timeval start;
3001 struct timeval now;
3002
3003 if (time_fd != NULL)
3004 {
3005 if (strstr(msg, "STARTING") != NULL)
3006 {
3007 gettimeofday(&start, NULL);
3008 prev_timeval = start;
3009 fprintf(time_fd, "\n\ntimes in msec\n");
3010 fprintf(time_fd, " clock self+sourced self: sourced script\n");
3011 fprintf(time_fd, " clock elapsed: other lines\n\n");
3012 }
3013 gettimeofday(&now, NULL);
3014 time_diff(&start, &now);
3015 if (((struct timeval *)tv_start) != NULL)
3016 {
3017 fprintf(time_fd, " ");
3018 time_diff(((struct timeval *)tv_start), &now);
3019 }
3020 fprintf(time_fd, " ");
3021 time_diff(&prev_timeval, &now);
3022 prev_timeval = now;
3023 fprintf(time_fd, ": %s\n", msg);
3024 }
3025}
3026
3027# ifdef WIN3264
3028/*
3029 * Windows doesn't have gettimeofday(), although it does have struct timeval.
3030 */
3031 int
3032gettimeofday(struct timeval *tv, char *dummy)
3033{
3034 long t = clock();
3035 tv->tv_sec = t / CLOCKS_PER_SEC;
3036 tv->tv_usec = (t - tv->tv_sec * CLOCKS_PER_SEC) * 1000000 / CLOCKS_PER_SEC;
3037 return 0;
3038}
3039# endif
3040
3041#endif
3042
3043#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
3044
3045/*
3046 * Common code for the X command server and the Win32 command server.
3047 */
3048
3049static char_u *build_drop_cmd __ARGS((int filec, char **filev, int sendReply));
3050
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003051/*
3052 * Do the client-server stuff, unless "--servername ''" was used.
3053 */
3054 static void
3055exec_on_server(parmp)
3056 mparm_T *parmp;
3057{
3058 if (parmp->serverName_arg == NULL || *parmp->serverName_arg != NUL)
3059 {
3060# ifdef WIN32
3061 /* Initialise the client/server messaging infrastructure. */
3062 serverInitMessaging();
3063# endif
3064
3065 /*
3066 * When a command server argument was found, execute it. This may
3067 * exit Vim when it was successful. Otherwise it's executed further
3068 * on. Remember the encoding used here in "serverStrEnc".
3069 */
3070 if (parmp->serverArg)
3071 {
3072 cmdsrv_main(&parmp->argc, parmp->argv,
3073 parmp->serverName_arg, &parmp->serverStr);
3074# ifdef FEAT_MBYTE
3075 parmp->serverStrEnc = vim_strsave(p_enc);
3076# endif
3077 }
3078
3079 /* If we're still running, get the name to register ourselves.
3080 * On Win32 can register right now, for X11 need to setup the
3081 * clipboard first, it's further down. */
3082 parmp->servername = serverMakeName(parmp->serverName_arg,
3083 parmp->argv[0]);
3084# ifdef WIN32
3085 if (parmp->servername != NULL)
3086 {
3087 serverSetName(parmp->servername);
3088 vim_free(parmp->servername);
3089 }
3090# endif
3091 }
3092}
3093
3094/*
3095 * Prepare for running as a Vim server.
3096 */
3097 static void
3098prepare_server(parmp)
3099 mparm_T *parmp;
3100{
3101# if defined(FEAT_X11)
3102 /*
3103 * Register for remote command execution with :serversend and --remote
3104 * unless there was a -X or a --servername '' on the command line.
3105 * Only register nongui-vim's with an explicit --servername argument.
3106 */
3107 if (X_DISPLAY != NULL && parmp->servername != NULL && (
3108# ifdef FEAT_GUI
3109 gui.in_use ||
3110# endif
3111 parmp->serverName_arg != NULL))
3112 {
3113 (void)serverRegisterName(X_DISPLAY, parmp->servername);
3114 vim_free(parmp->servername);
3115 TIME_MSG("register server name");
3116 }
3117 else
3118 serverDelayedStartName = parmp->servername;
3119# endif
3120
3121 /*
3122 * Execute command ourselves if we're here because the send failed (or
3123 * else we would have exited above).
3124 */
3125 if (parmp->serverStr != NULL)
3126 {
3127 char_u *p;
3128
3129 server_to_input_buf(serverConvert(parmp->serverStrEnc,
3130 parmp->serverStr, &p));
3131 vim_free(p);
3132 }
3133}
3134
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003135 static void
3136cmdsrv_main(argc, argv, serverName_arg, serverStr)
3137 int *argc;
3138 char **argv;
3139 char_u *serverName_arg;
3140 char_u **serverStr;
3141{
3142 char_u *res;
3143 int i;
3144 char_u *sname;
3145 int ret;
3146 int didone = FALSE;
3147 int exiterr = 0;
3148 char **newArgV = argv + 1;
3149 int newArgC = 1,
3150 Argc = *argc;
3151 int argtype;
3152#define ARGTYPE_OTHER 0
3153#define ARGTYPE_EDIT 1
3154#define ARGTYPE_EDIT_WAIT 2
3155#define ARGTYPE_SEND 3
3156 int silent = FALSE;
3157# ifndef FEAT_X11
3158 HWND srv;
3159# else
3160 Window srv;
3161
3162 setup_term_clip();
3163# endif
3164
3165 sname = serverMakeName(serverName_arg, argv[0]);
3166 if (sname == NULL)
3167 return;
3168
3169 /*
3170 * Execute the command server related arguments and remove them
3171 * from the argc/argv array; We may have to return into main()
3172 */
3173 for (i = 1; i < Argc; i++)
3174 {
3175 res = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003176 if (STRCMP(argv[i], "--") == 0) /* end of option arguments */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003177 {
3178 for (; i < *argc; i++)
3179 {
3180 *newArgV++ = argv[i];
3181 newArgC++;
3182 }
3183 break;
3184 }
3185
3186 if (STRICMP(argv[i], "--remote") == 0)
3187 argtype = ARGTYPE_EDIT;
3188 else if (STRICMP(argv[i], "--remote-silent") == 0)
3189 {
3190 argtype = ARGTYPE_EDIT;
3191 silent = TRUE;
3192 }
3193 else if (STRICMP(argv[i], "--remote-wait") == 0)
3194 argtype = ARGTYPE_EDIT_WAIT;
3195 else if (STRICMP(argv[i], "--remote-wait-silent") == 0)
3196 {
3197 argtype = ARGTYPE_EDIT_WAIT;
3198 silent = TRUE;
3199 }
3200 else if (STRICMP(argv[i], "--remote-send") == 0)
3201 argtype = ARGTYPE_SEND;
3202 else
3203 argtype = ARGTYPE_OTHER;
3204 if (argtype != ARGTYPE_OTHER)
3205 {
3206 if (i == *argc - 1)
3207 mainerr_arg_missing((char_u *)argv[i]);
3208 if (argtype == ARGTYPE_SEND)
3209 {
3210 *serverStr = (char_u *)argv[i + 1];
3211 i++;
3212 }
3213 else
3214 {
3215 *serverStr = build_drop_cmd(*argc - i - 1, argv + i + 1,
3216 argtype == ARGTYPE_EDIT_WAIT);
3217 if (*serverStr == NULL)
3218 {
3219 /* Probably out of memory, exit. */
3220 didone = TRUE;
3221 exiterr = 1;
3222 break;
3223 }
3224 Argc = i;
3225 }
3226# ifdef FEAT_X11
3227 if (xterm_dpy == NULL)
3228 {
3229 mch_errmsg(_("No display"));
3230 ret = -1;
3231 }
3232 else
3233 ret = serverSendToVim(xterm_dpy, sname, *serverStr,
3234 NULL, &srv, 0, 0, silent);
3235# else
3236 /* Win32 always works? */
3237 ret = serverSendToVim(sname, *serverStr, NULL, &srv, 0, silent);
3238# endif
3239 if (ret < 0)
3240 {
3241 if (argtype == ARGTYPE_SEND)
3242 {
3243 /* Failed to send, abort. */
3244 mch_errmsg(_(": Send failed.\n"));
3245 didone = TRUE;
3246 exiterr = 1;
3247 }
3248 else if (!silent)
3249 /* Let vim start normally. */
3250 mch_errmsg(_(": Send failed. Trying to execute locally\n"));
3251 break;
3252 }
3253
3254# ifdef FEAT_GUI_W32
3255 /* Guess that when the server name starts with "g" it's a GUI
3256 * server, which we can bring to the foreground here.
3257 * Foreground() in the server doesn't work very well. */
3258 if (argtype != ARGTYPE_SEND && TOUPPER_ASC(*sname) == 'G')
3259 SetForegroundWindow(srv);
3260# endif
3261
3262 /*
3263 * For --remote-wait: Wait until the server did edit each
3264 * file. Also detect that the server no longer runs.
3265 */
3266 if (ret >= 0 && argtype == ARGTYPE_EDIT_WAIT)
3267 {
3268 int numFiles = *argc - i - 1;
3269 int j;
3270 char_u *done = alloc(numFiles);
3271 char_u *p;
3272# ifdef FEAT_GUI_W32
3273 NOTIFYICONDATA ni;
3274 int count = 0;
3275 extern HWND message_window;
3276# endif
3277
3278 if (numFiles > 0 && argv[i + 1][0] == '+')
3279 /* Skip "+cmd" argument, don't wait for it to be edited. */
3280 --numFiles;
3281
3282# ifdef FEAT_GUI_W32
3283 ni.cbSize = sizeof(ni);
3284 ni.hWnd = message_window;
3285 ni.uID = 0;
3286 ni.uFlags = NIF_ICON|NIF_TIP;
3287 ni.hIcon = LoadIcon((HINSTANCE)GetModuleHandle(0), "IDR_VIM");
3288 sprintf(ni.szTip, _("%d of %d edited"), count, numFiles);
3289 Shell_NotifyIcon(NIM_ADD, &ni);
3290# endif
3291
3292 /* Wait for all files to unload in remote */
3293 memset(done, 0, numFiles);
3294 while (memchr(done, 0, numFiles) != NULL)
3295 {
3296# ifdef WIN32
3297 p = serverGetReply(srv, NULL, TRUE, TRUE);
3298 if (p == NULL)
3299 break;
3300# else
3301 if (serverReadReply(xterm_dpy, srv, &p, TRUE) < 0)
3302 break;
3303# endif
3304 j = atoi((char *)p);
3305 if (j >= 0 && j < numFiles)
3306 {
3307# ifdef FEAT_GUI_W32
3308 ++count;
3309 sprintf(ni.szTip, _("%d of %d edited"),
3310 count, numFiles);
3311 Shell_NotifyIcon(NIM_MODIFY, &ni);
3312# endif
3313 done[j] = 1;
3314 }
3315 }
3316# ifdef FEAT_GUI_W32
3317 Shell_NotifyIcon(NIM_DELETE, &ni);
3318# endif
3319 }
3320 }
3321 else if (STRICMP(argv[i], "--remote-expr") == 0)
3322 {
3323 if (i == *argc - 1)
3324 mainerr_arg_missing((char_u *)argv[i]);
3325# ifdef WIN32
3326 /* Win32 always works? */
3327 if (serverSendToVim(sname, (char_u *)argv[i + 1],
3328 &res, NULL, 1, FALSE) < 0)
3329# else
3330 if (xterm_dpy == NULL)
3331 mch_errmsg(_("No display: Send expression failed.\n"));
3332 else if (serverSendToVim(xterm_dpy, sname, (char_u *)argv[i + 1],
3333 &res, NULL, 1, 1, FALSE) < 0)
3334# endif
3335 {
3336 if (res != NULL && *res != NUL)
3337 {
3338 /* Output error from remote */
3339 mch_errmsg((char *)res);
3340 vim_free(res);
3341 res = NULL;
3342 }
3343 mch_errmsg(_(": Send expression failed.\n"));
3344 }
3345 }
3346 else if (STRICMP(argv[i], "--serverlist") == 0)
3347 {
3348# ifdef WIN32
3349 /* Win32 always works? */
3350 res = serverGetVimNames();
3351# else
3352 if (xterm_dpy != NULL)
3353 res = serverGetVimNames(xterm_dpy);
3354# endif
3355 if (called_emsg)
3356 mch_errmsg("\n");
3357 }
3358 else if (STRICMP(argv[i], "--servername") == 0)
3359 {
3360 /* Alredy processed. Take it out of the command line */
3361 i++;
3362 continue;
3363 }
3364 else
3365 {
3366 *newArgV++ = argv[i];
3367 newArgC++;
3368 continue;
3369 }
3370 didone = TRUE;
3371 if (res != NULL && *res != NUL)
3372 {
3373 mch_msg((char *)res);
3374 if (res[STRLEN(res) - 1] != '\n')
3375 mch_msg("\n");
3376 }
3377 vim_free(res);
3378 }
3379
3380 if (didone)
3381 {
3382 display_errors(); /* display any collected messages */
3383 exit(exiterr); /* Mission accomplished - get out */
3384 }
3385
3386 /* Return back into main() */
3387 *argc = newArgC;
3388 vim_free(sname);
3389}
3390
3391/*
3392 * Build a ":drop" command to send to a Vim server.
3393 */
3394 static char_u *
3395build_drop_cmd(filec, filev, sendReply)
3396 int filec;
3397 char **filev;
3398 int sendReply;
3399{
3400 garray_T ga;
3401 int i;
3402 char_u *inicmd = NULL;
3403 char_u *p;
3404 char_u cwd[MAXPATHL];
3405
3406 if (filec > 0 && filev[0][0] == '+')
3407 {
3408 inicmd = (char_u *)filev[0] + 1;
3409 filev++;
3410 filec--;
3411 }
3412 /* Check if we have at least one argument. */
3413 if (filec <= 0)
3414 mainerr_arg_missing((char_u *)filev[-1]);
3415 if (mch_dirname(cwd, MAXPATHL) != OK)
3416 return NULL;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003417 if ((p = vim_strsave_escaped_ext(cwd, PATH_ESC_CHARS, '\\', TRUE)) == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003418 return NULL;
3419 ga_init2(&ga, 1, 100);
3420 ga_concat(&ga, (char_u *)"<C-\\><C-N>:cd ");
3421 ga_concat(&ga, p);
3422 /* Call inputsave() so that a prompt for an encryption key works. */
3423 ga_concat(&ga, (char_u *)"<CR>:if exists('*inputsave')|call inputsave()|endif|drop");
3424 vim_free(p);
3425 for (i = 0; i < filec; i++)
3426 {
3427 /* On Unix the shell has already expanded the wildcards, don't want to
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003428 * do it again in the Vim server. On MS-Windows only escape
3429 * non-wildcard characters. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003430 p = vim_strsave_escaped((char_u *)filev[i],
3431#ifdef UNIX
3432 PATH_ESC_CHARS
3433#else
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003434 (char_u *)" \t%#"
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003435#endif
3436 );
3437 if (p == NULL)
3438 {
3439 vim_free(ga.ga_data);
3440 return NULL;
3441 }
3442 ga_concat(&ga, (char_u *)" ");
3443 ga_concat(&ga, p);
3444 vim_free(p);
3445 }
3446 /* The :drop commands goes to Insert mode when 'insertmode' is set, use
3447 * CTRL-\ CTRL-N again. */
3448 ga_concat(&ga, (char_u *)"|if exists('*inputrestore')|call inputrestore()|endif<CR>");
3449 ga_concat(&ga, (char_u *)"<C-\\><C-N>:cd -");
3450 if (sendReply)
3451 ga_concat(&ga, (char_u *)"<CR>:call SetupRemoteReplies()");
3452 ga_concat(&ga, (char_u *)"<CR>:");
3453 if (inicmd != NULL)
3454 {
3455 /* Can't use <CR> after "inicmd", because an "startinsert" would cause
3456 * the following commands to be inserted as text. Use a "|",
3457 * hopefully "inicmd" does allow this... */
3458 ga_concat(&ga, inicmd);
3459 ga_concat(&ga, (char_u *)"|");
3460 }
3461 /* Bring the window to the foreground, goto Insert mode when 'im' set and
3462 * clear command line. */
Bram Moolenaar567e4de2004-12-31 21:01:02 +00003463 ga_concat(&ga, (char_u *)"cal foreground()|if &im|star|en|redr|f<CR>");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003464 ga_append(&ga, NUL);
3465 return ga.ga_data;
3466}
3467
3468/*
3469 * Replace termcodes such as <CR> and insert as key presses if there is room.
3470 */
3471 void
3472server_to_input_buf(str)
3473 char_u *str;
3474{
3475 char_u *ptr = NULL;
3476 char_u *cpo_save = p_cpo;
3477
3478 /* Set 'cpoptions' the way we want it.
3479 * B set - backslashes are *not* treated specially
3480 * k set - keycodes are *not* reverse-engineered
3481 * < unset - <Key> sequences *are* interpreted
3482 * The last parameter of replace_termcodes() is TRUE so that the <lt>
3483 * sequence is recognised - needed for a real backslash.
3484 */
3485 p_cpo = (char_u *)"Bk";
3486 str = replace_termcodes((char_u *)str, &ptr, FALSE, TRUE);
3487 p_cpo = cpo_save;
3488
3489 if (*ptr != NUL) /* trailing CTRL-V results in nothing */
3490 {
3491 /*
3492 * Add the string to the input stream.
3493 * Can't use add_to_input_buf() here, we now have K_SPECIAL bytes.
3494 *
3495 * First clear typed characters from the typeahead buffer, there could
3496 * be half a mapping there. Then append to the existing string, so
3497 * that multiple commands from a client are concatenated.
3498 */
3499 if (typebuf.tb_maplen < typebuf.tb_len)
3500 del_typebuf(typebuf.tb_len - typebuf.tb_maplen, typebuf.tb_maplen);
3501 (void)ins_typebuf(str, REMAP_NONE, typebuf.tb_len, TRUE, FALSE);
3502
3503 /* Let input_available() know we inserted text in the typeahead
3504 * buffer. */
3505 received_from_client = TRUE;
3506 }
3507 vim_free((char_u *)ptr);
3508}
3509
3510/*
3511 * Evaluate an expression that the client sent to a string.
3512 * Handles disabling error messages and disables debugging, otherwise Vim
3513 * hangs, waiting for "cont" to be typed.
3514 */
3515 char_u *
3516eval_client_expr_to_string(expr)
3517 char_u *expr;
3518{
3519 char_u *res;
3520 int save_dbl = debug_break_level;
3521 int save_ro = redir_off;
3522
3523 debug_break_level = -1;
3524 redir_off = 0;
3525 ++emsg_skip;
3526
3527 res = eval_to_string(expr, NULL);
3528
3529 debug_break_level = save_dbl;
3530 redir_off = save_ro;
3531 --emsg_skip;
3532
Bram Moolenaar63a121b2005-12-11 21:36:39 +00003533 /* A client can tell us to redraw, but not to display the cursor, so do
3534 * that here. */
3535 setcursor();
3536 out_flush();
3537#ifdef FEAT_GUI
3538 if (gui.in_use)
3539 gui_update_cursor(FALSE, FALSE);
3540#endif
3541
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003542 return res;
3543}
3544
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003545/*
3546 * If conversion is needed, convert "data" from "client_enc" to 'encoding' and
3547 * return an allocated string. Otherwise return "data".
3548 * "*tofree" is set to the result when it needs to be freed later.
3549 */
3550/*ARGSUSED*/
3551 char_u *
3552serverConvert(client_enc, data, tofree)
3553 char_u *client_enc;
3554 char_u *data;
3555 char_u **tofree;
3556{
3557 char_u *res = data;
3558
3559 *tofree = NULL;
3560# ifdef FEAT_MBYTE
3561 if (client_enc != NULL && p_enc != NULL)
3562 {
3563 vimconv_T vimconv;
3564
3565 vimconv.vc_type = CONV_NONE;
3566 if (convert_setup(&vimconv, client_enc, p_enc) != FAIL
3567 && vimconv.vc_type != CONV_NONE)
3568 {
3569 res = string_convert(&vimconv, data, NULL);
3570 if (res == NULL)
3571 res = data;
3572 else
3573 *tofree = res;
3574 }
3575 convert_setup(&vimconv, NULL, NULL);
3576 }
3577# endif
3578 return res;
3579}
3580
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003581
3582/*
3583 * Make our basic server name: use the specified "arg" if given, otherwise use
3584 * the tail of the command "cmd" we were started with.
3585 * Return the name in allocated memory. This doesn't include a serial number.
3586 */
3587 static char_u *
3588serverMakeName(arg, cmd)
3589 char_u *arg;
3590 char *cmd;
3591{
3592 char_u *p;
3593
3594 if (arg != NULL && *arg != NUL)
3595 p = vim_strsave_up(arg);
3596 else
3597 {
3598 p = vim_strsave_up(gettail((char_u *)cmd));
3599 /* Remove .exe or .bat from the name. */
3600 if (p != NULL && vim_strchr(p, '.') != NULL)
3601 *vim_strchr(p, '.') = NUL;
3602 }
3603 return p;
3604}
3605#endif /* FEAT_CLIENTSERVER */
3606
3607/*
3608 * When FEAT_FKMAP is defined, also compile the Farsi source code.
3609 */
3610#if defined(FEAT_FKMAP) || defined(PROTO)
3611# include "farsi.c"
3612#endif
3613
3614/*
3615 * When FEAT_ARABIC is defined, also compile the Arabic source code.
3616 */
3617#if defined(FEAT_ARABIC) || defined(PROTO)
3618# include "arabic.c"
3619#endif