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