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