blob: 3dc72e15dae2f410d2f942b1d410ec9f8015bfaf [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;
1062 msg_attr(p, keep_msg_attr);
1063 vim_free(p);
1064 }
1065 if (need_fileinfo) /* show file info after redraw */
1066 {
1067 fileinfo(FALSE, TRUE, FALSE);
1068 need_fileinfo = FALSE;
1069 }
1070
1071 emsg_on_display = FALSE; /* can delete error message now */
1072 did_emsg = FALSE;
1073 msg_didany = FALSE; /* reset lines_left in msg_start() */
Bram Moolenaar661b1822005-07-28 22:36:45 +00001074 may_clear_sb_text(); /* clear scroll-back text on next msg */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001075 showruler(FALSE);
1076
1077 setcursor();
1078 cursor_on();
1079
1080 do_redraw = FALSE;
1081 }
1082#ifdef FEAT_GUI
1083 if (need_mouse_correct)
1084 gui_mouse_correct();
1085#endif
1086
1087 /*
1088 * Update w_curswant if w_set_curswant has been set.
1089 * Postponed until here to avoid computing w_virtcol too often.
1090 */
1091 update_curswant();
1092
1093 /*
1094 * If we're invoked as ex, do a round of ex commands.
1095 * Otherwise, get and execute a normal mode command.
1096 */
1097 if (exmode_active)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001098 {
1099 if (noexmode) /* End of ":global/path/visual" commands */
1100 return;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001101 do_exmode(exmode_active == EXMODE_VIM);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001102 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001103 else
1104 normal_cmd(&oa, TRUE);
1105 }
1106}
1107
1108
1109#if defined(USE_XSMP) || defined(FEAT_GUI_MSWIN) || defined(PROTO)
1110/*
1111 * Exit, but leave behind swap files for modified buffers.
1112 */
1113 void
1114getout_preserve_modified(exitval)
1115 int exitval;
1116{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001117# if defined(SIGHUP) && defined(SIG_IGN)
1118 /* Ignore SIGHUP, because a dropped connection causes a read error, which
1119 * makes Vim exit and then handling SIGHUP causes various reentrance
1120 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001121 signal(SIGHUP, SIG_IGN);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001122# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001123
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001124 ml_close_notmod(); /* close all not-modified buffers */
1125 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
1126 ml_close_all(FALSE); /* close all memfiles, without deleting */
1127 getout(exitval); /* exit Vim properly */
1128}
1129#endif
1130
1131
1132/* Exit properly */
1133 void
1134getout(exitval)
1135 int exitval;
1136{
1137#ifdef FEAT_AUTOCMD
1138 buf_T *buf;
1139 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001140 tabpage_T *tp, *next_tp;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001141#endif
1142
1143 exiting = TRUE;
1144
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001145 /* When running in Ex mode an error causes us to exit with a non-zero exit
1146 * code. POSIX requires this, although it's not 100% clear from the
1147 * standard. */
1148 if (exmode_active)
1149 exitval += ex_exitval;
1150
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001151 /* Position the cursor on the last screen line, below all the text */
1152#ifdef FEAT_GUI
1153 if (!gui.in_use)
1154#endif
1155 windgoto((int)Rows - 1, 0);
1156
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +00001157#if defined(FEAT_EVAL) || defined(FEAT_SYN_HL)
1158 /* Optionally print hashtable efficiency. */
1159 hash_debug_results();
1160#endif
1161
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001162#ifdef FEAT_GUI
1163 msg_didany = FALSE;
1164#endif
1165
1166#ifdef FEAT_AUTOCMD
1167 /* Trigger BufWinLeave for all windows, but only once per buffer. */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001168# if defined FEAT_WINDOWS
1169 for (tp = first_tabpage; tp != NULL; tp = next_tp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001170 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001171 next_tp = tp->tp_next;
1172 for (wp = (tp->tp_topframe == topframe)
1173 ? firstwin : tp->tp_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001174 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001175 buf = wp->w_buffer;
1176 if (buf->b_changedtick != -1)
1177 {
1178 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001179 FALSE, buf);
Bram Moolenaarf740b292006-02-16 22:11:02 +00001180 buf->b_changedtick = -1; /* note that we did it already */
1181 /* start all over, autocommands may mess up the lists */
1182 next_tp = first_tabpage;
1183 break;
1184 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001185 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001186 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001187# else
1188 apply_autocmds(EVENT_BUFWINLEAVE, curbuf, curbuf->b_fname, FALSE, curbuf);
1189# endif
Bram Moolenaar33aec762006-01-22 23:30:12 +00001190
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001191 /* Trigger BufUnload for buffers that are loaded */
1192 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1193 if (buf->b_ml.ml_mfp != NULL)
1194 {
1195 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
1196 FALSE, buf);
1197 if (!buf_valid(buf)) /* autocmd may delete the buffer */
1198 break;
1199 }
1200 apply_autocmds(EVENT_VIMLEAVEPRE, NULL, NULL, FALSE, curbuf);
1201#endif
1202
1203#ifdef FEAT_VIMINFO
1204 if (*p_viminfo != NUL)
1205 /* Write out the registers, history, marks etc, to the viminfo file */
1206 write_viminfo(NULL, FALSE);
1207#endif
1208
1209#ifdef FEAT_AUTOCMD
1210 apply_autocmds(EVENT_VIMLEAVE, NULL, NULL, FALSE, curbuf);
1211#endif
1212
Bram Moolenaar05159a02005-02-26 23:04:13 +00001213#ifdef FEAT_PROFILE
1214 profile_dump();
1215#endif
1216
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001217 if (did_emsg
1218#ifdef FEAT_GUI
1219 || (gui.in_use && msg_didany && p_verbose > 0)
1220#endif
1221 )
1222 {
1223 /* give the user a chance to read the (error) message */
1224 no_wait_return = FALSE;
1225 wait_return(FALSE);
1226 }
1227
1228#ifdef FEAT_AUTOCMD
1229 /* Position the cursor again, the autocommands may have moved it */
1230# ifdef FEAT_GUI
1231 if (!gui.in_use)
1232# endif
1233 windgoto((int)Rows - 1, 0);
1234#endif
1235
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001236#ifdef FEAT_MZSCHEME
1237 mzscheme_end();
1238#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001239#ifdef FEAT_TCL
1240 tcl_end();
1241#endif
1242#ifdef FEAT_RUBY
1243 ruby_end();
1244#endif
1245#ifdef FEAT_PYTHON
1246 python_end();
1247#endif
1248#ifdef FEAT_PERL
1249 perl_end();
1250#endif
1251#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
1252 iconv_end();
1253#endif
1254#ifdef FEAT_NETBEANS_INTG
1255 netbeans_end();
1256#endif
1257
1258 mch_exit(exitval);
1259}
1260
1261/*
1262 * Get a (optional) count for a Vim argument.
1263 */
1264 static int
1265get_number_arg(p, idx, def)
1266 char_u *p; /* pointer to argument */
1267 int *idx; /* index in argument, is incremented */
1268 int def; /* default value */
1269{
1270 if (vim_isdigit(p[*idx]))
1271 {
1272 def = atoi((char *)&(p[*idx]));
1273 while (vim_isdigit(p[*idx]))
1274 *idx = *idx + 1;
1275 }
1276 return def;
1277}
1278
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001279#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1280/*
1281 * Setup to use the current locale (for ctype() and many other things).
1282 */
1283 static void
1284init_locale()
1285{
1286 setlocale(LC_ALL, "");
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001287# ifdef WIN32
1288 /* Apparently MS-Windows printf() may cause a crash when we give it 8-bit
1289 * text while it's expecting text in the current locale. This call avoids
1290 * that. */
1291 setlocale(LC_CTYPE, "C");
1292# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001293
1294# ifdef FEAT_GETTEXT
1295 {
1296 int mustfree = FALSE;
1297 char_u *p;
1298
1299# ifdef DYNAMIC_GETTEXT
1300 /* Initialize the gettext library */
1301 dyn_libintl_init(NULL);
1302# endif
1303 /* expand_env() doesn't work yet, because chartab[] is not initialized
1304 * yet, call vim_getenv() directly */
1305 p = vim_getenv((char_u *)"VIMRUNTIME", &mustfree);
1306 if (p != NULL && *p != NUL)
1307 {
1308 STRCPY(NameBuff, p);
1309 STRCAT(NameBuff, "/lang");
1310 bindtextdomain(VIMPACKAGE, (char *)NameBuff);
1311 }
1312 if (mustfree)
1313 vim_free(p);
1314 textdomain(VIMPACKAGE);
1315 }
1316# endif
1317}
1318#endif
1319
1320/*
1321 * Check for: [r][e][g][vi|vim|view][diff][ex[im]]
1322 * If the executable name starts with "r" we disable shell commands.
1323 * If the next character is "e" we run in Easy mode.
1324 * If the next character is "g" we run the GUI version.
1325 * If the next characters are "view" we start in readonly mode.
1326 * If the next characters are "diff" or "vimdiff" we start in diff mode.
1327 * If the next characters are "ex" we start in Ex mode. If it's followed
1328 * by "im" use improved Ex mode.
1329 */
1330 static void
1331parse_command_name(parmp)
1332 mparm_T *parmp;
1333{
1334 char_u *initstr;
1335
1336 initstr = gettail((char_u *)parmp->argv[0]);
1337
1338#ifdef MACOS_X_UNIX
1339 /* An issue has been seen when launching Vim in such a way that
1340 * $PWD/$ARGV[0] or $ARGV[0] is not the absolute path to the
1341 * executable or a symbolic link of it. Until this issue is resolved
1342 * we prohibit the GUI from being used.
1343 */
1344 if (STRCMP(initstr, parmp->argv[0]) == 0)
1345 disallow_gui = TRUE;
1346
1347 /* TODO: On MacOS X default to gui if argv[0] ends in:
1348 * /vim.app/Contents/MacOS/Vim */
1349#endif
1350
1351#ifdef FEAT_EVAL
1352 set_vim_var_string(VV_PROGNAME, initstr, -1);
1353#endif
1354
1355 if (TOLOWER_ASC(initstr[0]) == 'r')
1356 {
1357 restricted = TRUE;
1358 ++initstr;
1359 }
1360
1361 /* Avoid using evim mode for "editor". */
1362 if (TOLOWER_ASC(initstr[0]) == 'e'
1363 && (TOLOWER_ASC(initstr[1]) == 'v'
1364 || TOLOWER_ASC(initstr[1]) == 'g'))
1365 {
1366#ifdef FEAT_GUI
1367 gui.starting = TRUE;
1368#endif
1369 parmp->evim_mode = TRUE;
1370 ++initstr;
1371 }
1372
1373 if (TOLOWER_ASC(initstr[0]) == 'g' || initstr[0] == 'k')
1374 {
1375 main_start_gui();
1376#ifdef FEAT_GUI
1377 ++initstr;
1378#endif
1379 }
1380
1381 if (STRNICMP(initstr, "view", 4) == 0)
1382 {
1383 readonlymode = TRUE;
1384 curbuf->b_p_ro = TRUE;
1385 p_uc = 10000; /* don't update very often */
1386 initstr += 4;
1387 }
1388 else if (STRNICMP(initstr, "vim", 3) == 0)
1389 initstr += 3;
1390
1391 /* Catch "[r][g]vimdiff" and "[r][g]viewdiff". */
1392 if (STRICMP(initstr, "diff") == 0)
1393 {
1394#ifdef FEAT_DIFF
1395 parmp->diff_mode = TRUE;
1396#else
1397 mch_errmsg(_("This Vim was not compiled with the diff feature."));
1398 mch_errmsg("\n");
1399 mch_exit(2);
1400#endif
1401 }
1402
1403 if (STRNICMP(initstr, "ex", 2) == 0)
1404 {
1405 if (STRNICMP(initstr + 2, "im", 2) == 0)
1406 exmode_active = EXMODE_VIM;
1407 else
1408 exmode_active = EXMODE_NORMAL;
1409 change_compatible(TRUE); /* set 'compatible' */
1410 }
1411}
1412
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001413/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00001414 * Get the name of the display, before gui_prepare() removes it from
1415 * argv[]. Used for the xterm-clipboard display.
1416 *
1417 * Also find the --server... arguments and --socketid
1418 */
1419/*ARGSUSED*/
1420 static void
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001421early_arg_scan(parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001422 mparm_T *parmp;
1423{
1424#if defined(FEAT_XCLIPBOARD) || defined(FEAT_CLIENTSERVER)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001425 int argc = parmp->argc;
1426 char **argv = parmp->argv;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001427 int i;
1428
1429 for (i = 1; i < argc; i++)
1430 {
1431 if (STRCMP(argv[i], "--") == 0)
1432 break;
1433# ifdef FEAT_XCLIPBOARD
1434 else if (STRICMP(argv[i], "-display") == 0
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001435# if defined(FEAT_GUI_GTK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001436 || STRICMP(argv[i], "--display") == 0
1437# endif
1438 )
1439 {
1440 if (i == argc - 1)
1441 mainerr_arg_missing((char_u *)argv[i]);
1442 xterm_display = argv[++i];
1443 }
1444# endif
1445# ifdef FEAT_CLIENTSERVER
1446 else if (STRICMP(argv[i], "--servername") == 0)
1447 {
1448 if (i == argc - 1)
1449 mainerr_arg_missing((char_u *)argv[i]);
1450 parmp->serverName_arg = (char_u *)argv[++i];
1451 }
1452 else if (STRICMP(argv[i], "--serverlist") == 0
1453 || STRICMP(argv[i], "--remote-send") == 0
1454 || STRICMP(argv[i], "--remote-expr") == 0
1455 || STRICMP(argv[i], "--remote") == 0
1456 || STRICMP(argv[i], "--remote-silent") == 0)
1457 parmp->serverArg = TRUE;
1458 else if (STRICMP(argv[i], "--remote-wait") == 0
1459 || STRICMP(argv[i], "--remote-wait-silent") == 0)
1460 {
1461 parmp->serverArg = TRUE;
1462#ifdef FEAT_GUI
1463 /* don't fork() when starting the GUI to edit the files ourself */
1464 gui.dofork = FALSE;
1465#endif
1466 }
1467# endif
1468# ifdef FEAT_GUI_GTK
1469 else if (STRICMP(argv[i], "--socketid") == 0)
1470 {
1471 unsigned int socket_id;
1472 int count;
1473
1474 if (i == argc - 1)
1475 mainerr_arg_missing((char_u *)argv[i]);
1476 if (STRNICMP(argv[i+1], "0x", 2) == 0)
1477 count = sscanf(&(argv[i + 1][2]), "%x", &socket_id);
1478 else
1479 count = sscanf(argv[i+1], "%u", &socket_id);
1480 if (count != 1)
1481 mainerr(ME_INVALID_ARG, (char_u *)argv[i]);
1482 else
1483 gtk_socket_id = socket_id;
1484 i++;
1485 }
1486 else if (STRICMP(argv[i], "--echo-wid") == 0)
1487 echo_wid_arg = TRUE;
1488# endif
1489 }
1490#endif
1491}
1492
1493/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001494 * Scan the command line arguments.
1495 */
1496 static void
1497command_line_scan(parmp)
1498 mparm_T *parmp;
1499{
1500 int argc = parmp->argc;
1501 char **argv = parmp->argv;
1502 int argv_idx; /* index in argv[n][] */
1503 int had_minmin = FALSE; /* found "--" argument */
1504 int want_argument; /* option argument with argument */
1505 int c;
Bram Moolenaar231334e2005-07-25 20:46:57 +00001506 char_u *p = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001507 long n;
1508
1509 --argc;
1510 ++argv;
1511 argv_idx = 1; /* active option letter is argv[0][argv_idx] */
1512 while (argc > 0)
1513 {
1514 /*
1515 * "+" or "+{number}" or "+/{pat}" or "+{command}" argument.
1516 */
1517 if (argv[0][0] == '+' && !had_minmin)
1518 {
1519 if (parmp->n_commands >= MAX_ARG_CMDS)
1520 mainerr(ME_EXTRA_CMD, NULL);
1521 argv_idx = -1; /* skip to next argument */
1522 if (argv[0][1] == NUL)
1523 parmp->commands[parmp->n_commands++] = (char_u *)"$";
1524 else
1525 parmp->commands[parmp->n_commands++] = (char_u *)&(argv[0][1]);
1526 }
1527
1528 /*
1529 * Optional argument.
1530 */
1531 else if (argv[0][0] == '-' && !had_minmin)
1532 {
1533 want_argument = FALSE;
1534 c = argv[0][argv_idx++];
1535#ifdef VMS
1536 /*
1537 * VMS only uses upper case command lines. Interpret "-X" as "-x"
1538 * and "-/X" as "-X".
1539 */
1540 if (c == '/')
1541 {
1542 c = argv[0][argv_idx++];
1543 c = TOUPPER_ASC(c);
1544 }
1545 else
1546 c = TOLOWER_ASC(c);
1547#endif
1548 switch (c)
1549 {
1550 case NUL: /* "vim -" read from stdin */
1551 /* "ex -" silent mode */
1552 if (exmode_active)
1553 silent_mode = TRUE;
1554 else
1555 {
1556 if (parmp->edit_type != EDIT_NONE)
1557 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1558 parmp->edit_type = EDIT_STDIN;
1559 read_cmd_fd = 2; /* read from stderr instead of stdin */
1560 }
1561 argv_idx = -1; /* skip to next argument */
1562 break;
1563
1564 case '-': /* "--" don't take any more option arguments */
1565 /* "--help" give help message */
1566 /* "--version" give version message */
1567 /* "--literal" take files literally */
1568 /* "--nofork" don't fork */
1569 /* "--noplugin[s]" skip plugins */
1570 /* "--cmd <cmd>" execute cmd before vimrc */
1571 if (STRICMP(argv[0] + argv_idx, "help") == 0)
1572 usage();
1573 else if (STRICMP(argv[0] + argv_idx, "version") == 0)
1574 {
1575 Columns = 80; /* need to init Columns */
1576 info_message = TRUE; /* use mch_msg(), not mch_errmsg() */
1577 list_version();
1578 msg_putchar('\n');
1579 msg_didout = FALSE;
1580 mch_exit(0);
1581 }
1582 else if (STRNICMP(argv[0] + argv_idx, "literal", 7) == 0)
1583 {
1584#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
1585 parmp->literal = TRUE;
1586#endif
1587 }
1588 else if (STRNICMP(argv[0] + argv_idx, "nofork", 6) == 0)
1589 {
1590#ifdef FEAT_GUI
1591 gui.dofork = FALSE; /* don't fork() when starting GUI */
1592#endif
1593 }
1594 else if (STRNICMP(argv[0] + argv_idx, "noplugin", 8) == 0)
1595 p_lpl = FALSE;
1596 else if (STRNICMP(argv[0] + argv_idx, "cmd", 3) == 0)
1597 {
1598 want_argument = TRUE;
1599 argv_idx += 3;
1600 }
1601#ifdef FEAT_CLIENTSERVER
1602 else if (STRNICMP(argv[0] + argv_idx, "serverlist", 10) == 0)
1603 ; /* already processed -- no arg */
1604 else if (STRNICMP(argv[0] + argv_idx, "servername", 10) == 0
1605 || STRNICMP(argv[0] + argv_idx, "serversend", 10) == 0)
1606 {
1607 /* already processed -- snatch the following arg */
1608 if (argc > 1)
1609 {
1610 --argc;
1611 ++argv;
1612 }
1613 }
1614#endif
1615#ifdef FEAT_GUI_GTK
1616 else if (STRNICMP(argv[0] + argv_idx, "socketid", 8) == 0)
1617 {
1618 /* already processed -- snatch the following arg */
1619 if (argc > 1)
1620 {
1621 --argc;
1622 ++argv;
1623 }
1624 }
1625 else if (STRNICMP(argv[0] + argv_idx, "echo-wid", 8) == 0)
1626 {
1627 /* already processed, skip */
1628 }
1629#endif
1630 else
1631 {
1632 if (argv[0][argv_idx])
1633 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
1634 had_minmin = TRUE;
1635 }
1636 if (!want_argument)
1637 argv_idx = -1; /* skip to next argument */
1638 break;
1639
1640 case 'A': /* "-A" start in Arabic mode */
1641#ifdef FEAT_ARABIC
1642 set_option_value((char_u *)"arabic", 1L, NULL, 0);
1643#else
1644 mch_errmsg(_(e_noarabic));
1645 mch_exit(2);
1646#endif
1647 break;
1648
1649 case 'b': /* "-b" binary mode */
Bram Moolenaar231334e2005-07-25 20:46:57 +00001650 /* Needs to be effective before expanding file names, because
1651 * for Win32 this makes us edit a shortcut file itself,
1652 * instead of the file it links to. */
1653 set_options_bin(curbuf->b_p_bin, 1, 0);
1654 curbuf->b_p_bin = 1; /* binary file I/O */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001655 break;
1656
1657 case 'C': /* "-C" Compatible */
1658 change_compatible(TRUE);
1659 break;
1660
1661 case 'e': /* "-e" Ex mode */
1662 exmode_active = EXMODE_NORMAL;
1663 break;
1664
1665 case 'E': /* "-E" Improved Ex mode */
1666 exmode_active = EXMODE_VIM;
1667 break;
1668
1669 case 'f': /* "-f" GUI: run in foreground. Amiga: open
1670 window directly, not with newcli */
1671#ifdef FEAT_GUI
1672 gui.dofork = FALSE; /* don't fork() when starting GUI */
1673#endif
1674 break;
1675
1676 case 'g': /* "-g" start GUI */
1677 main_start_gui();
1678 break;
1679
1680 case 'F': /* "-F" start in Farsi mode: rl + fkmap set */
1681#ifdef FEAT_FKMAP
1682 curwin->w_p_rl = p_fkmap = TRUE;
1683#else
1684 mch_errmsg(_(e_nofarsi));
1685 mch_exit(2);
1686#endif
1687 break;
1688
1689 case 'h': /* "-h" give help message */
1690#ifdef FEAT_GUI_GNOME
1691 /* Tell usage() to exit for "gvim". */
1692 gui.starting = FALSE;
1693#endif
1694 usage();
1695 break;
1696
1697 case 'H': /* "-H" start in Hebrew mode: rl + hkmap set */
1698#ifdef FEAT_RIGHTLEFT
1699 curwin->w_p_rl = p_hkmap = TRUE;
1700#else
1701 mch_errmsg(_(e_nohebrew));
1702 mch_exit(2);
1703#endif
1704 break;
1705
1706 case 'l': /* "-l" lisp mode, 'lisp' and 'showmatch' on */
1707#ifdef FEAT_LISP
1708 set_option_value((char_u *)"lisp", 1L, NULL, 0);
1709 p_sm = TRUE;
1710#endif
1711 break;
1712
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001713 case 'M': /* "-M" no changes or writing of files */
1714 reset_modifiable();
1715 /* FALLTHROUGH */
1716
1717 case 'm': /* "-m" no writing of files */
1718 p_write = FALSE;
1719 break;
1720
1721 case 'y': /* "-y" easy mode */
1722#ifdef FEAT_GUI
1723 gui.starting = TRUE; /* start GUI a bit later */
1724#endif
1725 parmp->evim_mode = TRUE;
1726 break;
1727
1728 case 'N': /* "-N" Nocompatible */
1729 change_compatible(FALSE);
1730 break;
1731
1732 case 'n': /* "-n" no swap file */
1733 parmp->no_swap_file = TRUE;
1734 break;
1735
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001736 case 'p': /* "-p[N]" open N tab pages */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00001737#ifdef TARGET_API_MAC_OSX
1738 /* For some reason on MacOS X, an argument like:
1739 -psn_0_10223617 is passed in when invoke from Finder
1740 or with the 'open' command */
1741 if (argv[0][argv_idx] == 's')
1742 {
1743 argv_idx = -1; /* bypass full -psn */
1744 main_start_gui();
1745 break;
1746 }
1747#endif
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001748#ifdef FEAT_WINDOWS
1749 /* default is 0: open window for each file */
1750 parmp->window_count = get_number_arg((char_u *)argv[0],
1751 &argv_idx, 0);
1752 parmp->window_layout = WIN_TABS;
1753#endif
1754 break;
1755
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001756 case 'o': /* "-o[N]" open N horizontal split windows */
1757#ifdef FEAT_WINDOWS
1758 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00001759 parmp->window_count = get_number_arg((char_u *)argv[0],
1760 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001761 parmp->window_layout = WIN_HOR;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001762#endif
1763 break;
1764
1765 case 'O': /* "-O[N]" open N vertical split windows */
1766#if defined(FEAT_VERTSPLIT) && defined(FEAT_WINDOWS)
1767 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00001768 parmp->window_count = get_number_arg((char_u *)argv[0],
1769 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001770 parmp->window_layout = WIN_VER;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001771#endif
1772 break;
1773
1774#ifdef FEAT_QUICKFIX
1775 case 'q': /* "-q" QuickFix mode */
1776 if (parmp->edit_type != EDIT_NONE)
1777 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1778 parmp->edit_type = EDIT_QF;
1779 if (argv[0][argv_idx]) /* "-q{errorfile}" */
1780 {
1781 parmp->use_ef = (char_u *)argv[0] + argv_idx;
1782 argv_idx = -1;
1783 }
1784 else if (argc > 1) /* "-q {errorfile}" */
1785 want_argument = TRUE;
1786 break;
1787#endif
1788
1789 case 'R': /* "-R" readonly mode */
1790 readonlymode = TRUE;
1791 curbuf->b_p_ro = TRUE;
1792 p_uc = 10000; /* don't update very often */
1793 break;
1794
1795 case 'r': /* "-r" recovery mode */
1796 case 'L': /* "-L" recovery mode */
1797 recoverymode = 1;
1798 break;
1799
1800 case 's':
1801 if (exmode_active) /* "-s" silent (batch) mode */
1802 silent_mode = TRUE;
1803 else /* "-s {scriptin}" read from script file */
1804 want_argument = TRUE;
1805 break;
1806
1807 case 't': /* "-t {tag}" or "-t{tag}" jump to tag */
1808 if (parmp->edit_type != EDIT_NONE)
1809 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1810 parmp->edit_type = EDIT_TAG;
1811 if (argv[0][argv_idx]) /* "-t{tag}" */
1812 {
1813 parmp->tagname = (char_u *)argv[0] + argv_idx;
1814 argv_idx = -1;
1815 }
1816 else /* "-t {tag}" */
1817 want_argument = TRUE;
1818 break;
1819
1820#ifdef FEAT_EVAL
1821 case 'D': /* "-D" Debugging */
1822 parmp->use_debug_break_level = 9999;
1823 break;
1824#endif
1825#ifdef FEAT_DIFF
1826 case 'd': /* "-d" 'diff' */
1827# ifdef AMIGA
1828 /* check for "-dev {device}" */
1829 if (argv[0][argv_idx] == 'e' && argv[0][argv_idx + 1] == 'v')
1830 want_argument = TRUE;
1831 else
1832# endif
1833 parmp->diff_mode = TRUE;
1834 break;
1835#endif
1836 case 'V': /* "-V{N}" Verbose level */
1837 /* default is 10: a little bit verbose */
1838 p_verbose = get_number_arg((char_u *)argv[0], &argv_idx, 10);
1839 if (argv[0][argv_idx] != NUL)
1840 {
1841 set_option_value((char_u *)"verbosefile", 0L,
1842 (char_u *)argv[0] + argv_idx, 0);
1843 argv_idx = STRLEN(argv[0]);
1844 }
1845 break;
1846
1847 case 'v': /* "-v" Vi-mode (as if called "vi") */
1848 exmode_active = 0;
1849#ifdef FEAT_GUI
1850 gui.starting = FALSE; /* don't start GUI */
1851#endif
1852 break;
1853
1854 case 'w': /* "-w{number}" set window height */
1855 /* "-w {scriptout}" write to script */
1856 if (vim_isdigit(((char_u *)argv[0])[argv_idx]))
1857 {
1858 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
1859 set_option_value((char_u *)"window", n, NULL, 0);
1860 break;
1861 }
1862 want_argument = TRUE;
1863 break;
1864
1865#ifdef FEAT_CRYPT
1866 case 'x': /* "-x" encrypted reading/writing of files */
1867 parmp->ask_for_key = TRUE;
1868 break;
1869#endif
1870
1871 case 'X': /* "-X" don't connect to X server */
1872#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
1873 x_no_connect = TRUE;
1874#endif
1875 break;
1876
1877 case 'Z': /* "-Z" restricted mode */
1878 restricted = TRUE;
1879 break;
1880
1881 case 'c': /* "-c{command}" or "-c {command}" execute
1882 command */
1883 if (argv[0][argv_idx] != NUL)
1884 {
1885 if (parmp->n_commands >= MAX_ARG_CMDS)
1886 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar231334e2005-07-25 20:46:57 +00001887 parmp->commands[parmp->n_commands++] = (char_u *)argv[0]
1888 + argv_idx;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001889 argv_idx = -1;
1890 break;
1891 }
1892 /*FALLTHROUGH*/
1893 case 'S': /* "-S {file}" execute Vim script */
1894 case 'i': /* "-i {viminfo}" use for viminfo */
1895#ifndef FEAT_DIFF
1896 case 'd': /* "-d {device}" device (for Amiga) */
1897#endif
1898 case 'T': /* "-T {terminal}" terminal name */
1899 case 'u': /* "-u {vimrc}" vim inits file */
1900 case 'U': /* "-U {gvimrc}" gvim inits file */
1901 case 'W': /* "-W {scriptout}" overwrite */
1902#ifdef FEAT_GUI_W32
1903 case 'P': /* "-P {parent title}" MDI parent */
1904#endif
1905 want_argument = TRUE;
1906 break;
1907
1908 default:
1909 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
1910 }
1911
1912 /*
1913 * Handle option arguments with argument.
1914 */
1915 if (want_argument)
1916 {
1917 /*
1918 * Check for garbage immediately after the option letter.
1919 */
1920 if (argv[0][argv_idx] != NUL)
1921 mainerr(ME_GARBAGE, (char_u *)argv[0]);
1922
1923 --argc;
1924 if (argc < 1 && c != 'S')
1925 mainerr_arg_missing((char_u *)argv[0]);
1926 ++argv;
1927 argv_idx = -1;
1928
1929 switch (c)
1930 {
1931 case 'c': /* "-c {command}" execute command */
1932 case 'S': /* "-S {file}" execute Vim script */
1933 if (parmp->n_commands >= MAX_ARG_CMDS)
1934 mainerr(ME_EXTRA_CMD, NULL);
1935 if (c == 'S')
1936 {
1937 char *a;
1938
1939 if (argc < 1)
1940 /* "-S" without argument: use default session file
1941 * name. */
1942 a = SESSION_FILE;
1943 else if (argv[0][0] == '-')
1944 {
1945 /* "-S" followed by another option: use default
1946 * session file name. */
1947 a = SESSION_FILE;
1948 ++argc;
1949 --argv;
1950 }
1951 else
1952 a = argv[0];
1953 p = alloc((unsigned)(STRLEN(a) + 4));
1954 if (p == NULL)
1955 mch_exit(2);
1956 sprintf((char *)p, "so %s", a);
1957 parmp->cmds_tofree[parmp->n_commands] = TRUE;
1958 parmp->commands[parmp->n_commands++] = p;
1959 }
1960 else
Bram Moolenaar231334e2005-07-25 20:46:57 +00001961 parmp->commands[parmp->n_commands++] =
1962 (char_u *)argv[0];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001963 break;
1964
1965 case '-': /* "--cmd {command}" execute command */
1966 if (parmp->n_pre_commands >= MAX_ARG_CMDS)
1967 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar231334e2005-07-25 20:46:57 +00001968 parmp->pre_commands[parmp->n_pre_commands++] =
1969 (char_u *)argv[0];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001970 break;
1971
1972 /* case 'd': -d {device} is handled in mch_check_win() for the
1973 * Amiga */
1974
1975#ifdef FEAT_QUICKFIX
1976 case 'q': /* "-q {errorfile}" QuickFix mode */
1977 parmp->use_ef = (char_u *)argv[0];
1978 break;
1979#endif
1980
1981 case 'i': /* "-i {viminfo}" use for viminfo */
1982 use_viminfo = (char_u *)argv[0];
1983 break;
1984
1985 case 's': /* "-s {scriptin}" read from script file */
1986 if (scriptin[0] != NULL)
1987 {
1988scripterror:
1989 mch_errmsg(_("Attempt to open script file again: \""));
1990 mch_errmsg(argv[-1]);
1991 mch_errmsg(" ");
1992 mch_errmsg(argv[0]);
1993 mch_errmsg("\"\n");
1994 mch_exit(2);
1995 }
1996 if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL)
1997 {
1998 mch_errmsg(_("Cannot open for reading: \""));
1999 mch_errmsg(argv[0]);
2000 mch_errmsg("\"\n");
2001 mch_exit(2);
2002 }
2003 if (save_typebuf() == FAIL)
2004 mch_exit(2); /* out of memory */
2005 break;
2006
2007 case 't': /* "-t {tag}" */
2008 parmp->tagname = (char_u *)argv[0];
2009 break;
2010
2011 case 'T': /* "-T {terminal}" terminal name */
2012 /*
2013 * The -T term argument is always available and when
2014 * HAVE_TERMLIB is supported it overrides the environment
2015 * variable TERM.
2016 */
2017#ifdef FEAT_GUI
2018 if (term_is_gui((char_u *)argv[0]))
2019 gui.starting = TRUE; /* start GUI a bit later */
2020 else
2021#endif
2022 parmp->term = (char_u *)argv[0];
2023 break;
2024
2025 case 'u': /* "-u {vimrc}" vim inits file */
2026 parmp->use_vimrc = (char_u *)argv[0];
2027 break;
2028
2029 case 'U': /* "-U {gvimrc}" gvim inits file */
2030#ifdef FEAT_GUI
2031 use_gvimrc = (char_u *)argv[0];
2032#endif
2033 break;
2034
2035 case 'w': /* "-w {nr}" 'window' value */
2036 /* "-w {scriptout}" append to script file */
2037 if (vim_isdigit(*((char_u *)argv[0])))
2038 {
2039 argv_idx = 0;
2040 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2041 set_option_value((char_u *)"window", n, NULL, 0);
2042 argv_idx = -1;
2043 break;
2044 }
2045 /*FALLTHROUGH*/
2046 case 'W': /* "-W {scriptout}" overwrite script file */
2047 if (scriptout != NULL)
2048 goto scripterror;
2049 if ((scriptout = mch_fopen(argv[0],
2050 c == 'w' ? APPENDBIN : WRITEBIN)) == NULL)
2051 {
2052 mch_errmsg(_("Cannot open for script output: \""));
2053 mch_errmsg(argv[0]);
2054 mch_errmsg("\"\n");
2055 mch_exit(2);
2056 }
2057 break;
2058
2059#ifdef FEAT_GUI_W32
2060 case 'P': /* "-P {parent title}" MDI parent */
2061 gui_mch_set_parent(argv[0]);
2062 break;
2063#endif
2064 }
2065 }
2066 }
2067
2068 /*
2069 * File name argument.
2070 */
2071 else
2072 {
2073 argv_idx = -1; /* skip to next argument */
2074
2075 /* Check for only one type of editing. */
2076 if (parmp->edit_type != EDIT_NONE && parmp->edit_type != EDIT_FILE)
2077 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2078 parmp->edit_type = EDIT_FILE;
2079
2080#ifdef MSWIN
2081 /* Remember if the argument was a full path before changing
2082 * slashes to backslashes. */
2083 if (argv[0][0] != NUL && argv[0][1] == ':' && argv[0][2] == '\\')
2084 parmp->full_path = TRUE;
2085#endif
2086
2087 /* Add the file to the global argument list. */
2088 if (ga_grow(&global_alist.al_ga, 1) == FAIL
2089 || (p = vim_strsave((char_u *)argv[0])) == NULL)
2090 mch_exit(2);
2091#ifdef FEAT_DIFF
2092 if (parmp->diff_mode && mch_isdir(p) && GARGCOUNT > 0
2093 && !mch_isdir(alist_name(&GARGLIST[0])))
2094 {
2095 char_u *r;
2096
2097 r = concat_fnames(p, gettail(alist_name(&GARGLIST[0])), TRUE);
2098 if (r != NULL)
2099 {
2100 vim_free(p);
2101 p = r;
2102 }
2103 }
2104#endif
2105#if defined(__CYGWIN32__) && !defined(WIN32)
2106 /*
2107 * If vim is invoked by non-Cygwin tools, convert away any
2108 * DOS paths, so things like .swp files are created correctly.
2109 * Look for evidence of non-Cygwin paths before we bother.
2110 * This is only for when using the Unix files.
2111 */
2112 if (strpbrk(p, "\\:") != NULL)
2113 {
2114 char posix_path[PATH_MAX];
2115
2116 cygwin_conv_to_posix_path(p, posix_path);
2117 vim_free(p);
2118 p = vim_strsave(posix_path);
2119 if (p == NULL)
2120 mch_exit(2);
2121 }
2122#endif
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002123
2124#ifdef USE_FNAME_CASE
2125 /* Make the case of the file name match the actual file. */
2126 fname_case(p, 0);
2127#endif
2128
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002129 alist_add(&global_alist, p,
2130#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
Bram Moolenaar231334e2005-07-25 20:46:57 +00002131 parmp->literal ? 2 : 0 /* add buffer nr after exp. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002132#else
2133 2 /* add buffer number now and use curbuf */
2134#endif
2135 );
2136
2137#if defined(FEAT_MBYTE) && defined(WIN32)
2138 {
2139 extern void used_file_arg(char *, int, int);
2140
2141 /* Remember this argument has been added to the argument list.
2142 * Needed when 'encoding' is changed. */
2143 used_file_arg(argv[0], parmp->literal, parmp->full_path);
2144 }
2145#endif
2146 }
2147
2148 /*
2149 * If there are no more letters after the current "-", go to next
2150 * argument. argv_idx is set to -1 when the current argument is to be
2151 * skipped.
2152 */
2153 if (argv_idx <= 0 || argv[0][argv_idx] == NUL)
2154 {
2155 --argc;
2156 ++argv;
2157 argv_idx = 1;
2158 }
2159 }
2160}
2161
2162/*
2163 * Print a warning if stdout is not a terminal.
2164 * When starting in Ex mode and commands come from a file, set Silent mode.
2165 */
2166 static void
2167check_tty(parmp)
2168 mparm_T *parmp;
2169{
2170 int input_isatty; /* is active input a terminal? */
2171
2172 input_isatty = mch_input_isatty();
2173 if (exmode_active)
2174 {
2175 if (!input_isatty)
2176 silent_mode = TRUE;
2177 }
2178 else if (parmp->want_full_screen && (!parmp->stdout_isatty || !input_isatty)
2179#ifdef FEAT_GUI
2180 /* don't want the delay when started from the desktop */
2181 && !gui.starting
2182#endif
2183 )
2184 {
2185#ifdef NBDEBUG
2186 /*
2187 * This shouldn't be necessary. But if I run netbeans with the log
2188 * output coming to the console and XOpenDisplay fails, I get vim
2189 * trying to start with input/output to my console tty. This fills my
2190 * input buffer so fast I can't even kill the process in under 2
2191 * minutes (and it beeps continuosly the whole time :-)
2192 */
2193 if (usingNetbeans && (!parmp->stdout_isatty || !input_isatty))
2194 {
2195 mch_errmsg(_("Vim: Error: Failure to start gvim from NetBeans\n"));
2196 exit(1);
2197 }
2198#endif
2199 if (!parmp->stdout_isatty)
2200 mch_errmsg(_("Vim: Warning: Output is not to a terminal\n"));
2201 if (!input_isatty)
2202 mch_errmsg(_("Vim: Warning: Input is not from a terminal\n"));
2203 out_flush();
2204 if (scriptin[0] == NULL)
2205 ui_delay(2000L, TRUE);
2206 TIME_MSG("Warning delay");
2207 }
2208}
2209
2210/*
2211 * Read text from stdin.
2212 */
2213 static void
2214read_stdin()
2215{
2216 int i;
2217
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002218#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002219 /* When getting the ATTENTION prompt here, use a dialog */
2220 swap_exists_action = SEA_DIALOG;
2221#endif
2222 no_wait_return = TRUE;
2223 i = msg_didany;
2224 set_buflisted(TRUE);
2225 (void)open_buffer(TRUE, NULL); /* create memfile and read file */
2226 no_wait_return = FALSE;
2227 msg_didany = i;
2228 TIME_MSG("reading stdin");
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002229#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002230 check_swap_exists_action();
2231#endif
2232#if !(defined(AMIGA) || defined(MACOS))
2233 /*
2234 * Close stdin and dup it from stderr. Required for GPM to work
2235 * properly, and for running external commands.
2236 * Is there any other system that cannot do this?
2237 */
2238 close(0);
2239 dup(2);
2240#endif
2241}
2242
2243/*
2244 * Create the requested number of windows and edit buffers in them.
2245 * Also does recovery if "recoverymode" set.
2246 */
2247/*ARGSUSED*/
2248 static void
2249create_windows(parmp)
2250 mparm_T *parmp;
2251{
2252#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002253 int rewind;
2254 int done = 0;
2255
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002256 /*
2257 * Create the number of windows that was requested.
2258 */
2259 if (parmp->window_count == -1) /* was not set */
2260 parmp->window_count = 1;
2261 if (parmp->window_count == 0)
2262 parmp->window_count = GARGCOUNT;
2263 if (parmp->window_count > 1)
2264 {
2265 /* Don't change the windows if there was a command in .vimrc that
2266 * already split some windows */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002267 if (parmp->window_layout == 0)
2268 parmp->window_layout = WIN_HOR;
2269 if (parmp->window_layout == WIN_TABS)
2270 {
2271 parmp->window_count = make_tabpages(parmp->window_count);
2272 TIME_MSG("making tab pages");
2273 }
2274 else if (firstwin->w_next == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002275 {
2276 parmp->window_count = make_windows(parmp->window_count,
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002277 parmp->window_layout == WIN_VER);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002278 TIME_MSG("making windows");
2279 }
2280 else
2281 parmp->window_count = win_count();
2282 }
2283 else
2284 parmp->window_count = 1;
2285#endif
2286
2287 if (recoverymode) /* do recover */
2288 {
2289 msg_scroll = TRUE; /* scroll message up */
2290 ml_recover();
2291 if (curbuf->b_ml.ml_mfp == NULL) /* failed */
2292 getout(1);
2293 do_modelines(FALSE); /* do modelines */
2294 }
2295 else
2296 {
2297 /*
2298 * Open a buffer for windows that don't have one yet.
2299 * Commands in the .vimrc might have loaded a file or split the window.
2300 * Watch out for autocommands that delete a window.
2301 */
2302#ifdef FEAT_AUTOCMD
2303 /*
2304 * Don't execute Win/Buf Enter/Leave autocommands here
2305 */
2306 ++autocmd_no_enter;
2307 ++autocmd_no_leave;
2308#endif
2309#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002310 rewind = TRUE;
2311 while (done++ < 1000)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002312 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002313 if (rewind)
2314 {
2315 if (parmp->window_layout == WIN_TABS)
2316 goto_tabpage(1);
2317 else
2318 curwin = firstwin;
2319 }
2320 else if (parmp->window_layout == WIN_TABS)
2321 {
2322 if (curtab->tp_next == NULL)
2323 break;
2324 goto_tabpage(0);
2325 }
2326 else
2327 {
2328 if (curwin->w_next == NULL)
2329 break;
2330 curwin = curwin->w_next;
2331 }
2332 rewind = FALSE;
2333#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002334 curbuf = curwin->w_buffer;
2335 if (curbuf->b_ml.ml_mfp == NULL)
2336 {
2337#ifdef FEAT_FOLDING
2338 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2339 if (p_fdls >= 0)
2340 curwin->w_p_fdl = p_fdls;
2341#endif
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002342#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002343 /* When getting the ATTENTION prompt here, use a dialog */
2344 swap_exists_action = SEA_DIALOG;
2345#endif
2346 set_buflisted(TRUE);
2347 (void)open_buffer(FALSE, NULL); /* create memfile, read file */
2348
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002349#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002350 check_swap_exists_action();
2351#endif
2352#ifdef FEAT_AUTOCMD
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002353 rewind = TRUE; /* start again */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002354#endif
2355 }
2356#ifdef FEAT_WINDOWS
2357 ui_breakcheck();
2358 if (got_int)
2359 {
2360 (void)vgetc(); /* only break the file loading, not the rest */
2361 break;
2362 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002363 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002364#endif
2365#ifdef FEAT_WINDOWS
2366 if (parmp->window_layout == WIN_TABS)
2367 goto_tabpage(1);
2368 else
2369 curwin = firstwin;
2370 curbuf = curwin->w_buffer;
2371#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002372#ifdef FEAT_AUTOCMD
2373 --autocmd_no_enter;
2374 --autocmd_no_leave;
2375#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002376 }
2377}
2378
2379#ifdef FEAT_WINDOWS
2380 /*
2381 * If opened more than one window, start editing files in the other
2382 * windows. make_windows() has already opened the windows.
2383 */
2384 static void
2385edit_buffers(parmp)
2386 mparm_T *parmp;
2387{
2388 int arg_idx; /* index in argument list */
2389 int i;
2390
2391# ifdef FEAT_AUTOCMD
2392 /*
2393 * Don't execute Win/Buf Enter/Leave autocommands here
2394 */
2395 ++autocmd_no_enter;
2396 ++autocmd_no_leave;
2397# endif
2398 arg_idx = 1;
2399 for (i = 1; i < parmp->window_count; ++i)
2400 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002401 if (parmp->window_layout == WIN_TABS)
2402 {
2403 if (curtab->tp_next == NULL) /* just checking */
2404 break;
2405 goto_tabpage(0);
2406 }
2407 else
2408 {
2409 if (curwin->w_next == NULL) /* just checking */
2410 break;
2411 win_enter(curwin->w_next, FALSE);
2412 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002413
2414 /* Only open the file if there is no file in this window yet (that can
2415 * happen when .vimrc contains ":sall") */
2416 if (curbuf == firstwin->w_buffer || curbuf->b_ffname == NULL)
2417 {
2418 curwin->w_arg_idx = arg_idx;
2419 /* edit file from arg list, if there is one */
2420 (void)do_ecmd(0, arg_idx < GARGCOUNT
2421 ? alist_name(&GARGLIST[arg_idx]) : NULL,
2422 NULL, NULL, ECMD_LASTL, ECMD_HIDE);
2423 if (arg_idx == GARGCOUNT - 1)
2424 arg_had_last = TRUE;
2425 ++arg_idx;
2426 }
2427 ui_breakcheck();
2428 if (got_int)
2429 {
2430 (void)vgetc(); /* only break the file loading, not the rest */
2431 break;
2432 }
2433 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002434
2435 if (parmp->window_layout == WIN_TABS)
2436 goto_tabpage(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002437# ifdef FEAT_AUTOCMD
2438 --autocmd_no_enter;
2439# endif
2440 win_enter(firstwin, FALSE); /* back to first window */
2441# ifdef FEAT_AUTOCMD
2442 --autocmd_no_leave;
2443# endif
2444 TIME_MSG("editing files in windows");
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002445 if (parmp->window_count > 1 && parmp->window_layout != WIN_TABS)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002446 win_equal(curwin, FALSE, 'b'); /* adjust heights */
2447}
2448#endif /* FEAT_WINDOWS */
2449
2450/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002451 * Execute the commands from --cmd arguments "cmds[cnt]".
2452 */
2453 static void
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002454exe_pre_commands(parmp)
2455 mparm_T *parmp;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002456{
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002457 char_u **cmds = parmp->pre_commands;
2458 int cnt = parmp->n_pre_commands;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002459 int i;
2460
2461 if (cnt > 0)
2462 {
2463 curwin->w_cursor.lnum = 0; /* just in case.. */
2464 sourcing_name = (char_u *)_("pre-vimrc command line");
2465# ifdef FEAT_EVAL
2466 current_SID = SID_CMDARG;
2467# endif
2468 for (i = 0; i < cnt; ++i)
2469 do_cmdline_cmd(cmds[i]);
2470 sourcing_name = NULL;
2471# ifdef FEAT_EVAL
2472 current_SID = 0;
2473# endif
2474 TIME_MSG("--cmd commands");
2475 }
2476}
2477
2478/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002479 * Execute "+", "-c" and "-S" arguments.
2480 */
2481 static void
2482exe_commands(parmp)
2483 mparm_T *parmp;
2484{
2485 int i;
2486
2487 /*
2488 * We start commands on line 0, make "vim +/pat file" match a
2489 * pattern on line 1. But don't move the cursor when an autocommand
2490 * with g`" was used.
2491 */
2492 msg_scroll = TRUE;
2493 if (parmp->tagname == NULL && curwin->w_cursor.lnum <= 1)
2494 curwin->w_cursor.lnum = 0;
2495 sourcing_name = (char_u *)"command line";
2496#ifdef FEAT_EVAL
2497 current_SID = SID_CARG;
2498#endif
2499 for (i = 0; i < parmp->n_commands; ++i)
2500 {
2501 do_cmdline_cmd(parmp->commands[i]);
2502 if (parmp->cmds_tofree[i])
2503 vim_free(parmp->commands[i]);
2504 }
2505 sourcing_name = NULL;
2506#ifdef FEAT_EVAL
2507 current_SID = 0;
2508#endif
2509 if (curwin->w_cursor.lnum == 0)
2510 curwin->w_cursor.lnum = 1;
2511
2512 if (!exmode_active)
2513 msg_scroll = FALSE;
2514
2515#ifdef FEAT_QUICKFIX
2516 /* When started with "-q errorfile" jump to first error again. */
2517 if (parmp->edit_type == EDIT_QF)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002518 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002519#endif
2520 TIME_MSG("executing command arguments");
2521}
2522
2523/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002524 * Source startup scripts.
2525 */
2526 static void
2527source_startup_scripts(parmp)
2528 mparm_T *parmp;
2529{
2530 int i;
2531
2532 /*
2533 * For "evim" source evim.vim first of all, so that the user can overrule
2534 * any things he doesn't like.
2535 */
2536 if (parmp->evim_mode)
2537 {
2538 (void)do_source((char_u *)EVIM_FILE, FALSE, FALSE);
2539 TIME_MSG("source evim file");
2540 }
2541
2542 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002543 * If -u argument given, use only the initializations from that file and
Bram Moolenaar58d98232005-07-23 22:25:46 +00002544 * nothing else.
2545 */
2546 if (parmp->use_vimrc != NULL)
2547 {
Bram Moolenaar231334e2005-07-25 20:46:57 +00002548 if (STRCMP(parmp->use_vimrc, "NONE") == 0
2549 || STRCMP(parmp->use_vimrc, "NORC") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002550 {
2551#ifdef FEAT_GUI
2552 if (use_gvimrc == NULL) /* don't load gvimrc either */
2553 use_gvimrc = parmp->use_vimrc;
2554#endif
2555 if (parmp->use_vimrc[2] == 'N')
2556 p_lpl = FALSE; /* don't load plugins either */
2557 }
2558 else
2559 {
2560 if (do_source(parmp->use_vimrc, FALSE, FALSE) != OK)
2561 EMSG2(_("E282: Cannot read from \"%s\""), parmp->use_vimrc);
2562 }
2563 }
2564 else if (!silent_mode)
2565 {
2566#ifdef AMIGA
2567 struct Process *proc = (struct Process *)FindTask(0L);
2568 APTR save_winptr = proc->pr_WindowPtr;
2569
2570 /* Avoid a requester here for a volume that doesn't exist. */
2571 proc->pr_WindowPtr = (APTR)-1L;
2572#endif
2573
2574 /*
2575 * Get system wide defaults, if the file name is defined.
2576 */
2577#ifdef SYS_VIMRC_FILE
2578 (void)do_source((char_u *)SYS_VIMRC_FILE, FALSE, FALSE);
2579#endif
2580
2581 /*
2582 * Try to read initialization commands from the following places:
2583 * - environment variable VIMINIT
2584 * - user vimrc file (s:.vimrc for Amiga, ~/.vimrc otherwise)
2585 * - second user vimrc file ($VIM/.vimrc for Dos)
2586 * - environment variable EXINIT
2587 * - user exrc file (s:.exrc for Amiga, ~/.exrc otherwise)
2588 * - second user exrc file ($VIM/.exrc for Dos)
2589 * The first that exists is used, the rest is ignored.
2590 */
2591 if (process_env((char_u *)"VIMINIT", TRUE) != OK)
2592 {
2593 if (do_source((char_u *)USR_VIMRC_FILE, TRUE, TRUE) == FAIL
2594#ifdef USR_VIMRC_FILE2
2595 && do_source((char_u *)USR_VIMRC_FILE2, TRUE, TRUE) == FAIL
2596#endif
2597#ifdef USR_VIMRC_FILE3
2598 && do_source((char_u *)USR_VIMRC_FILE3, TRUE, TRUE) == FAIL
2599#endif
2600 && process_env((char_u *)"EXINIT", FALSE) == FAIL
2601 && do_source((char_u *)USR_EXRC_FILE, FALSE, FALSE) == FAIL)
2602 {
2603#ifdef USR_EXRC_FILE2
2604 (void)do_source((char_u *)USR_EXRC_FILE2, FALSE, FALSE);
2605#endif
2606 }
2607 }
2608
2609 /*
2610 * Read initialization commands from ".vimrc" or ".exrc" in current
2611 * directory. This is only done if the 'exrc' option is set.
2612 * Because of security reasons we disallow shell and write commands
2613 * now, except for unix if the file is owned by the user or 'secure'
2614 * option has been reset in environment of global ".exrc" or ".vimrc".
2615 * Only do this if VIMRC_FILE is not the same as USR_VIMRC_FILE or
2616 * SYS_VIMRC_FILE.
2617 */
2618 if (p_exrc)
2619 {
2620#if defined(UNIX) || defined(VMS)
2621 /* If ".vimrc" file is not owned by user, set 'secure' mode. */
2622 if (!file_owned(VIMRC_FILE))
2623#endif
2624 secure = p_secure;
2625
2626 i = FAIL;
2627 if (fullpathcmp((char_u *)USR_VIMRC_FILE,
2628 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
2629#ifdef USR_VIMRC_FILE2
2630 && fullpathcmp((char_u *)USR_VIMRC_FILE2,
2631 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
2632#endif
2633#ifdef USR_VIMRC_FILE3
2634 && fullpathcmp((char_u *)USR_VIMRC_FILE3,
2635 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
2636#endif
2637#ifdef SYS_VIMRC_FILE
2638 && fullpathcmp((char_u *)SYS_VIMRC_FILE,
2639 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
2640#endif
2641 )
2642 i = do_source((char_u *)VIMRC_FILE, TRUE, TRUE);
2643
2644 if (i == FAIL)
2645 {
2646#if defined(UNIX) || defined(VMS)
2647 /* if ".exrc" is not owned by user set 'secure' mode */
2648 if (!file_owned(EXRC_FILE))
2649 secure = p_secure;
2650 else
2651 secure = 0;
2652#endif
2653 if ( fullpathcmp((char_u *)USR_EXRC_FILE,
2654 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
2655#ifdef USR_EXRC_FILE2
2656 && fullpathcmp((char_u *)USR_EXRC_FILE2,
2657 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
2658#endif
2659 )
2660 (void)do_source((char_u *)EXRC_FILE, FALSE, FALSE);
2661 }
2662 }
2663 if (secure == 2)
2664 need_wait_return = TRUE;
2665 secure = 0;
2666#ifdef AMIGA
2667 proc->pr_WindowPtr = save_winptr;
2668#endif
2669 }
2670 TIME_MSG("sourcing vimrc file(s)");
2671}
2672
2673/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002674 * Setup to start using the GUI. Exit with an error when not available.
2675 */
2676 static void
2677main_start_gui()
2678{
2679#ifdef FEAT_GUI
2680 gui.starting = TRUE; /* start GUI a bit later */
2681#else
2682 mch_errmsg(_(e_nogvim));
2683 mch_errmsg("\n");
2684 mch_exit(2);
2685#endif
2686}
2687
2688/*
2689 * Get an evironment variable, and execute it as Ex commands.
2690 * Returns FAIL if the environment variable was not executed, OK otherwise.
2691 */
2692 int
2693process_env(env, is_viminit)
2694 char_u *env;
2695 int is_viminit; /* when TRUE, called for VIMINIT */
2696{
2697 char_u *initstr;
2698 char_u *save_sourcing_name;
2699 linenr_T save_sourcing_lnum;
2700#ifdef FEAT_EVAL
2701 scid_T save_sid;
2702#endif
2703
2704 if ((initstr = mch_getenv(env)) != NULL && *initstr != NUL)
2705 {
2706 if (is_viminit)
2707 vimrc_found();
2708 save_sourcing_name = sourcing_name;
2709 save_sourcing_lnum = sourcing_lnum;
2710 sourcing_name = env;
2711 sourcing_lnum = 0;
2712#ifdef FEAT_EVAL
2713 save_sid = current_SID;
2714 current_SID = SID_ENV;
2715#endif
2716 do_cmdline_cmd(initstr);
2717 sourcing_name = save_sourcing_name;
2718 sourcing_lnum = save_sourcing_lnum;
2719#ifdef FEAT_EVAL
2720 current_SID = save_sid;;
2721#endif
2722 return OK;
2723 }
2724 return FAIL;
2725}
2726
2727#if defined(UNIX) || defined(VMS)
2728/*
2729 * Return TRUE if we are certain the user owns the file "fname".
2730 * Used for ".vimrc" and ".exrc".
2731 * Use both stat() and lstat() for extra security.
2732 */
2733 static int
2734file_owned(fname)
2735 char *fname;
2736{
2737 struct stat s;
2738# ifdef UNIX
2739 uid_t uid = getuid();
2740# else /* VMS */
2741 uid_t uid = ((getgid() << 16) | getuid());
2742# endif
2743
2744 return !(mch_stat(fname, &s) != 0 || s.st_uid != uid
2745# ifdef HAVE_LSTAT
2746 || mch_lstat(fname, &s) != 0 || s.st_uid != uid
2747# endif
2748 );
2749}
2750#endif
2751
2752/*
2753 * Give an error message main_errors["n"] and exit.
2754 */
2755 static void
2756mainerr(n, str)
2757 int n; /* one of the ME_ defines */
2758 char_u *str; /* extra argument or NULL */
2759{
2760#if defined(UNIX) || defined(__EMX__) || defined(VMS)
2761 reset_signals(); /* kill us with CTRL-C here, if you like */
2762#endif
2763
2764 mch_errmsg(longVersion);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002765 mch_errmsg("\n");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002766 mch_errmsg(_(main_errors[n]));
2767 if (str != NULL)
2768 {
2769 mch_errmsg(": \"");
2770 mch_errmsg((char *)str);
2771 mch_errmsg("\"");
2772 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002773 mch_errmsg(_("\nMore info with: \"vim -h\"\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002774
2775 mch_exit(1);
2776}
2777
2778 void
2779mainerr_arg_missing(str)
2780 char_u *str;
2781{
2782 mainerr(ME_ARG_MISSING, str);
2783}
2784
2785/*
2786 * print a message with three spaces prepended and '\n' appended.
2787 */
2788 static void
2789main_msg(s)
2790 char *s;
2791{
2792 mch_msg(" ");
2793 mch_msg(s);
2794 mch_msg("\n");
2795}
2796
2797/*
2798 * Print messages for "vim -h" or "vim --help" and exit.
2799 */
2800 static void
2801usage()
2802{
2803 int i;
2804 static char *(use[]) =
2805 {
2806 N_("[file ..] edit specified file(s)"),
2807 N_("- read text from stdin"),
2808 N_("-t tag edit file where tag is defined"),
2809#ifdef FEAT_QUICKFIX
2810 N_("-q [errorfile] edit file with first error")
2811#endif
2812 };
2813
2814#if defined(UNIX) || defined(__EMX__) || defined(VMS)
2815 reset_signals(); /* kill us with CTRL-C here, if you like */
2816#endif
2817
2818 mch_msg(longVersion);
2819 mch_msg(_("\n\nusage:"));
2820 for (i = 0; ; ++i)
2821 {
2822 mch_msg(_(" vim [arguments] "));
2823 mch_msg(_(use[i]));
2824 if (i == (sizeof(use) / sizeof(char_u *)) - 1)
2825 break;
2826 mch_msg(_("\n or:"));
2827 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002828#ifdef VMS
2829 mch_msg(_("where case is ignored prepend / to make flag upper case"));
2830#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002831
2832 mch_msg(_("\n\nArguments:\n"));
2833 main_msg(_("--\t\t\tOnly file names after this"));
2834#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
2835 main_msg(_("--literal\t\tDon't expand wildcards"));
2836#endif
2837#ifdef FEAT_OLE
2838 main_msg(_("-register\t\tRegister this gvim for OLE"));
2839 main_msg(_("-unregister\t\tUnregister gvim for OLE"));
2840#endif
2841#ifdef FEAT_GUI
2842 main_msg(_("-g\t\t\tRun using GUI (like \"gvim\")"));
2843 main_msg(_("-f or --nofork\tForeground: Don't fork when starting GUI"));
2844#endif
2845 main_msg(_("-v\t\t\tVi mode (like \"vi\")"));
2846 main_msg(_("-e\t\t\tEx mode (like \"ex\")"));
2847 main_msg(_("-s\t\t\tSilent (batch) mode (only for \"ex\")"));
2848#ifdef FEAT_DIFF
2849 main_msg(_("-d\t\t\tDiff mode (like \"vimdiff\")"));
2850#endif
2851 main_msg(_("-y\t\t\tEasy mode (like \"evim\", modeless)"));
2852 main_msg(_("-R\t\t\tReadonly mode (like \"view\")"));
2853 main_msg(_("-Z\t\t\tRestricted mode (like \"rvim\")"));
2854 main_msg(_("-m\t\t\tModifications (writing files) not allowed"));
2855 main_msg(_("-M\t\t\tModifications in text not allowed"));
2856 main_msg(_("-b\t\t\tBinary mode"));
2857#ifdef FEAT_LISP
2858 main_msg(_("-l\t\t\tLisp mode"));
2859#endif
2860 main_msg(_("-C\t\t\tCompatible with Vi: 'compatible'"));
2861 main_msg(_("-N\t\t\tNot fully Vi compatible: 'nocompatible'"));
2862 main_msg(_("-V[N]\t\tVerbose level"));
2863 main_msg(_("-D\t\t\tDebugging mode"));
2864 main_msg(_("-n\t\t\tNo swap file, use memory only"));
2865 main_msg(_("-r\t\t\tList swap files and exit"));
2866 main_msg(_("-r (with file name)\tRecover crashed session"));
2867 main_msg(_("-L\t\t\tSame as -r"));
2868#ifdef AMIGA
2869 main_msg(_("-f\t\t\tDon't use newcli to open window"));
2870 main_msg(_("-dev <device>\t\tUse <device> for I/O"));
2871#endif
2872#ifdef FEAT_ARABIC
2873 main_msg(_("-A\t\t\tstart in Arabic mode"));
2874#endif
2875#ifdef FEAT_RIGHTLEFT
2876 main_msg(_("-H\t\t\tStart in Hebrew mode"));
2877#endif
2878#ifdef FEAT_FKMAP
2879 main_msg(_("-F\t\t\tStart in Farsi mode"));
2880#endif
2881 main_msg(_("-T <terminal>\tSet terminal type to <terminal>"));
2882 main_msg(_("-u <vimrc>\t\tUse <vimrc> instead of any .vimrc"));
2883#ifdef FEAT_GUI
2884 main_msg(_("-U <gvimrc>\t\tUse <gvimrc> instead of any .gvimrc"));
2885#endif
2886 main_msg(_("--noplugin\t\tDon't load plugin scripts"));
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002887 main_msg(_("-p[N]\t\tOpen N tab pages (default: one for each file)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002888 main_msg(_("-o[N]\t\tOpen N windows (default: one for each file)"));
2889 main_msg(_("-O[N]\t\tLike -o but split vertically"));
2890 main_msg(_("+\t\t\tStart at end of file"));
2891 main_msg(_("+<lnum>\t\tStart at line <lnum>"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002892 main_msg(_("--cmd <command>\tExecute <command> before loading any vimrc file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002893 main_msg(_("-c <command>\t\tExecute <command> after loading the first file"));
2894 main_msg(_("-S <session>\t\tSource file <session> after loading the first file"));
2895 main_msg(_("-s <scriptin>\tRead Normal mode commands from file <scriptin>"));
2896 main_msg(_("-w <scriptout>\tAppend all typed commands to file <scriptout>"));
2897 main_msg(_("-W <scriptout>\tWrite all typed commands to file <scriptout>"));
2898#ifdef FEAT_CRYPT
2899 main_msg(_("-x\t\t\tEdit encrypted files"));
2900#endif
2901#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
2902# if defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)
2903 main_msg(_("-display <display>\tConnect vim to this particular X-server"));
2904# endif
2905 main_msg(_("-X\t\t\tDo not connect to X server"));
2906#endif
2907#ifdef FEAT_CLIENTSERVER
2908 main_msg(_("--remote <files>\tEdit <files> in a Vim server if possible"));
2909 main_msg(_("--remote-silent <files> Same, don't complain if there is no server"));
2910 main_msg(_("--remote-wait <files> As --remote but wait for files to have been edited"));
2911 main_msg(_("--remote-wait-silent <files> Same, don't complain if there is no server"));
2912 main_msg(_("--remote-send <keys>\tSend <keys> to a Vim server and exit"));
2913 main_msg(_("--remote-expr <expr>\tEvaluate <expr> in a Vim server and print result"));
2914 main_msg(_("--serverlist\t\tList available Vim server names and exit"));
2915 main_msg(_("--servername <name>\tSend to/become the Vim server <name>"));
2916#endif
2917#ifdef FEAT_VIMINFO
2918 main_msg(_("-i <viminfo>\t\tUse <viminfo> instead of .viminfo"));
2919#endif
2920 main_msg(_("-h or --help\tPrint Help (this message) and exit"));
2921 main_msg(_("--version\t\tPrint version information and exit"));
2922
2923#ifdef FEAT_GUI_X11
2924# ifdef FEAT_GUI_MOTIF
2925 mch_msg(_("\nArguments recognised by gvim (Motif version):\n"));
2926# else
2927# ifdef FEAT_GUI_ATHENA
2928# ifdef FEAT_GUI_NEXTAW
2929 mch_msg(_("\nArguments recognised by gvim (neXtaw version):\n"));
2930# else
2931 mch_msg(_("\nArguments recognised by gvim (Athena version):\n"));
2932# endif
2933# endif
2934# endif
2935 main_msg(_("-display <display>\tRun vim on <display>"));
2936 main_msg(_("-iconic\t\tStart vim iconified"));
2937# if 0
2938 main_msg(_("-name <name>\t\tUse resource as if vim was <name>"));
2939 mch_msg(_("\t\t\t (Unimplemented)\n"));
2940# endif
2941 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
2942 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
2943 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
2944 main_msg(_("-boldfont <font>\tUse <font> for bold text"));
2945 main_msg(_("-italicfont <font>\tUse <font> for italic text"));
2946 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
2947 main_msg(_("-borderwidth <width>\tUse a border width of <width> (also: -bw)"));
2948 main_msg(_("-scrollbarwidth <width> Use a scrollbar width of <width> (also: -sw)"));
2949# ifdef FEAT_GUI_ATHENA
2950 main_msg(_("-menuheight <height>\tUse a menu bar height of <height> (also: -mh)"));
2951# endif
2952 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
2953 main_msg(_("+reverse\t\tDon't use reverse video (also: +rv)"));
2954 main_msg(_("-xrm <resource>\tSet the specified resource"));
2955#endif /* FEAT_GUI_X11 */
2956#if defined(FEAT_GUI) && defined(RISCOS)
2957 mch_msg(_("\nArguments recognised by gvim (RISC OS version):\n"));
2958 main_msg(_("--columns <number>\tInitial width of window in columns"));
2959 main_msg(_("--rows <number>\tInitial height of window in rows"));
2960#endif
2961#ifdef FEAT_GUI_GTK
2962 mch_msg(_("\nArguments recognised by gvim (GTK+ version):\n"));
2963 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
2964 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
2965 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
2966 main_msg(_("-display <display>\tRun vim on <display> (also: --display)"));
2967# ifdef HAVE_GTK2
2968 main_msg(_("--role <role>\tSet a unique role to identify the main window"));
2969# endif
2970 main_msg(_("--socketid <xid>\tOpen Vim inside another GTK widget"));
2971#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002972#ifdef FEAT_GUI_W32
2973 main_msg(_("-P <parent title>\tOpen Vim inside parent application"));
2974#endif
2975
2976#ifdef FEAT_GUI_GNOME
2977 /* Gnome gives extra messages for --help if we continue, but not for -h. */
2978 if (gui.starting)
2979 mch_msg("\n");
2980 else
2981#endif
2982 mch_exit(0);
2983}
2984
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002985#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002986/*
2987 * Check the result of the ATTENTION dialog:
2988 * When "Quit" selected, exit Vim.
2989 * When "Recover" selected, recover the file.
2990 */
2991 static void
2992check_swap_exists_action()
2993{
2994 if (swap_exists_action == SEA_QUIT)
2995 getout(1);
2996 handle_swap_exists(NULL);
2997}
2998#endif
2999
3000#if defined(STARTUPTIME) || defined(PROTO)
3001static void time_diff __ARGS((struct timeval *then, struct timeval *now));
3002
3003static struct timeval prev_timeval;
3004
3005/*
3006 * Save the previous time before doing something that could nest.
3007 * set "*tv_rel" to the time elapsed so far.
3008 */
3009 void
3010time_push(tv_rel, tv_start)
3011 void *tv_rel, *tv_start;
3012{
3013 *((struct timeval *)tv_rel) = prev_timeval;
3014 gettimeofday(&prev_timeval, NULL);
3015 ((struct timeval *)tv_rel)->tv_usec = prev_timeval.tv_usec
3016 - ((struct timeval *)tv_rel)->tv_usec;
3017 ((struct timeval *)tv_rel)->tv_sec = prev_timeval.tv_sec
3018 - ((struct timeval *)tv_rel)->tv_sec;
3019 if (((struct timeval *)tv_rel)->tv_usec < 0)
3020 {
3021 ((struct timeval *)tv_rel)->tv_usec += 1000000;
3022 --((struct timeval *)tv_rel)->tv_sec;
3023 }
3024 *(struct timeval *)tv_start = prev_timeval;
3025}
3026
3027/*
3028 * Compute the previous time after doing something that could nest.
3029 * Subtract "*tp" from prev_timeval;
3030 * Note: The arguments are (void *) to avoid trouble with systems that don't
3031 * have struct timeval.
3032 */
3033 void
3034time_pop(tp)
3035 void *tp; /* actually (struct timeval *) */
3036{
3037 prev_timeval.tv_usec -= ((struct timeval *)tp)->tv_usec;
3038 prev_timeval.tv_sec -= ((struct timeval *)tp)->tv_sec;
3039 if (prev_timeval.tv_usec < 0)
3040 {
3041 prev_timeval.tv_usec += 1000000;
3042 --prev_timeval.tv_sec;
3043 }
3044}
3045
3046 static void
3047time_diff(then, now)
3048 struct timeval *then;
3049 struct timeval *now;
3050{
3051 long usec;
3052 long msec;
3053
3054 usec = now->tv_usec - then->tv_usec;
3055 msec = (now->tv_sec - then->tv_sec) * 1000L + usec / 1000L,
3056 usec = usec % 1000L;
3057 fprintf(time_fd, "%03ld.%03ld", msec, usec >= 0 ? usec : usec + 1000L);
3058}
3059
3060 void
3061time_msg(msg, tv_start)
3062 char *msg;
3063 void *tv_start; /* only for do_source: start time; actually
3064 (struct timeval *) */
3065{
3066 static struct timeval start;
3067 struct timeval now;
3068
3069 if (time_fd != NULL)
3070 {
3071 if (strstr(msg, "STARTING") != NULL)
3072 {
3073 gettimeofday(&start, NULL);
3074 prev_timeval = start;
3075 fprintf(time_fd, "\n\ntimes in msec\n");
3076 fprintf(time_fd, " clock self+sourced self: sourced script\n");
3077 fprintf(time_fd, " clock elapsed: other lines\n\n");
3078 }
3079 gettimeofday(&now, NULL);
3080 time_diff(&start, &now);
3081 if (((struct timeval *)tv_start) != NULL)
3082 {
3083 fprintf(time_fd, " ");
3084 time_diff(((struct timeval *)tv_start), &now);
3085 }
3086 fprintf(time_fd, " ");
3087 time_diff(&prev_timeval, &now);
3088 prev_timeval = now;
3089 fprintf(time_fd, ": %s\n", msg);
3090 }
3091}
3092
3093# ifdef WIN3264
3094/*
3095 * Windows doesn't have gettimeofday(), although it does have struct timeval.
3096 */
3097 int
3098gettimeofday(struct timeval *tv, char *dummy)
3099{
3100 long t = clock();
3101 tv->tv_sec = t / CLOCKS_PER_SEC;
3102 tv->tv_usec = (t - tv->tv_sec * CLOCKS_PER_SEC) * 1000000 / CLOCKS_PER_SEC;
3103 return 0;
3104}
3105# endif
3106
3107#endif
3108
3109#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
3110
3111/*
3112 * Common code for the X command server and the Win32 command server.
3113 */
3114
3115static char_u *build_drop_cmd __ARGS((int filec, char **filev, int sendReply));
3116
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003117/*
3118 * Do the client-server stuff, unless "--servername ''" was used.
3119 */
3120 static void
3121exec_on_server(parmp)
3122 mparm_T *parmp;
3123{
3124 if (parmp->serverName_arg == NULL || *parmp->serverName_arg != NUL)
3125 {
3126# ifdef WIN32
3127 /* Initialise the client/server messaging infrastructure. */
3128 serverInitMessaging();
3129# endif
3130
3131 /*
3132 * When a command server argument was found, execute it. This may
3133 * exit Vim when it was successful. Otherwise it's executed further
3134 * on. Remember the encoding used here in "serverStrEnc".
3135 */
3136 if (parmp->serverArg)
3137 {
3138 cmdsrv_main(&parmp->argc, parmp->argv,
3139 parmp->serverName_arg, &parmp->serverStr);
3140# ifdef FEAT_MBYTE
3141 parmp->serverStrEnc = vim_strsave(p_enc);
3142# endif
3143 }
3144
3145 /* If we're still running, get the name to register ourselves.
3146 * On Win32 can register right now, for X11 need to setup the
3147 * clipboard first, it's further down. */
3148 parmp->servername = serverMakeName(parmp->serverName_arg,
3149 parmp->argv[0]);
3150# ifdef WIN32
3151 if (parmp->servername != NULL)
3152 {
3153 serverSetName(parmp->servername);
3154 vim_free(parmp->servername);
3155 }
3156# endif
3157 }
3158}
3159
3160/*
3161 * Prepare for running as a Vim server.
3162 */
3163 static void
3164prepare_server(parmp)
3165 mparm_T *parmp;
3166{
3167# if defined(FEAT_X11)
3168 /*
3169 * Register for remote command execution with :serversend and --remote
3170 * unless there was a -X or a --servername '' on the command line.
3171 * Only register nongui-vim's with an explicit --servername argument.
3172 */
3173 if (X_DISPLAY != NULL && parmp->servername != NULL && (
3174# ifdef FEAT_GUI
3175 gui.in_use ||
3176# endif
3177 parmp->serverName_arg != NULL))
3178 {
3179 (void)serverRegisterName(X_DISPLAY, parmp->servername);
3180 vim_free(parmp->servername);
3181 TIME_MSG("register server name");
3182 }
3183 else
3184 serverDelayedStartName = parmp->servername;
3185# endif
3186
3187 /*
3188 * Execute command ourselves if we're here because the send failed (or
3189 * else we would have exited above).
3190 */
3191 if (parmp->serverStr != NULL)
3192 {
3193 char_u *p;
3194
3195 server_to_input_buf(serverConvert(parmp->serverStrEnc,
3196 parmp->serverStr, &p));
3197 vim_free(p);
3198 }
3199}
3200
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003201 static void
3202cmdsrv_main(argc, argv, serverName_arg, serverStr)
3203 int *argc;
3204 char **argv;
3205 char_u *serverName_arg;
3206 char_u **serverStr;
3207{
3208 char_u *res;
3209 int i;
3210 char_u *sname;
3211 int ret;
3212 int didone = FALSE;
3213 int exiterr = 0;
3214 char **newArgV = argv + 1;
3215 int newArgC = 1,
3216 Argc = *argc;
3217 int argtype;
3218#define ARGTYPE_OTHER 0
3219#define ARGTYPE_EDIT 1
3220#define ARGTYPE_EDIT_WAIT 2
3221#define ARGTYPE_SEND 3
3222 int silent = FALSE;
3223# ifndef FEAT_X11
3224 HWND srv;
3225# else
3226 Window srv;
3227
3228 setup_term_clip();
3229# endif
3230
3231 sname = serverMakeName(serverName_arg, argv[0]);
3232 if (sname == NULL)
3233 return;
3234
3235 /*
3236 * Execute the command server related arguments and remove them
3237 * from the argc/argv array; We may have to return into main()
3238 */
3239 for (i = 1; i < Argc; i++)
3240 {
3241 res = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003242 if (STRCMP(argv[i], "--") == 0) /* end of option arguments */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003243 {
3244 for (; i < *argc; i++)
3245 {
3246 *newArgV++ = argv[i];
3247 newArgC++;
3248 }
3249 break;
3250 }
3251
3252 if (STRICMP(argv[i], "--remote") == 0)
3253 argtype = ARGTYPE_EDIT;
3254 else if (STRICMP(argv[i], "--remote-silent") == 0)
3255 {
3256 argtype = ARGTYPE_EDIT;
3257 silent = TRUE;
3258 }
3259 else if (STRICMP(argv[i], "--remote-wait") == 0)
3260 argtype = ARGTYPE_EDIT_WAIT;
3261 else if (STRICMP(argv[i], "--remote-wait-silent") == 0)
3262 {
3263 argtype = ARGTYPE_EDIT_WAIT;
3264 silent = TRUE;
3265 }
3266 else if (STRICMP(argv[i], "--remote-send") == 0)
3267 argtype = ARGTYPE_SEND;
3268 else
3269 argtype = ARGTYPE_OTHER;
3270 if (argtype != ARGTYPE_OTHER)
3271 {
3272 if (i == *argc - 1)
3273 mainerr_arg_missing((char_u *)argv[i]);
3274 if (argtype == ARGTYPE_SEND)
3275 {
3276 *serverStr = (char_u *)argv[i + 1];
3277 i++;
3278 }
3279 else
3280 {
3281 *serverStr = build_drop_cmd(*argc - i - 1, argv + i + 1,
3282 argtype == ARGTYPE_EDIT_WAIT);
3283 if (*serverStr == NULL)
3284 {
3285 /* Probably out of memory, exit. */
3286 didone = TRUE;
3287 exiterr = 1;
3288 break;
3289 }
3290 Argc = i;
3291 }
3292# ifdef FEAT_X11
3293 if (xterm_dpy == NULL)
3294 {
3295 mch_errmsg(_("No display"));
3296 ret = -1;
3297 }
3298 else
3299 ret = serverSendToVim(xterm_dpy, sname, *serverStr,
3300 NULL, &srv, 0, 0, silent);
3301# else
3302 /* Win32 always works? */
3303 ret = serverSendToVim(sname, *serverStr, NULL, &srv, 0, silent);
3304# endif
3305 if (ret < 0)
3306 {
3307 if (argtype == ARGTYPE_SEND)
3308 {
3309 /* Failed to send, abort. */
3310 mch_errmsg(_(": Send failed.\n"));
3311 didone = TRUE;
3312 exiterr = 1;
3313 }
3314 else if (!silent)
3315 /* Let vim start normally. */
3316 mch_errmsg(_(": Send failed. Trying to execute locally\n"));
3317 break;
3318 }
3319
3320# ifdef FEAT_GUI_W32
3321 /* Guess that when the server name starts with "g" it's a GUI
3322 * server, which we can bring to the foreground here.
3323 * Foreground() in the server doesn't work very well. */
3324 if (argtype != ARGTYPE_SEND && TOUPPER_ASC(*sname) == 'G')
3325 SetForegroundWindow(srv);
3326# endif
3327
3328 /*
3329 * For --remote-wait: Wait until the server did edit each
3330 * file. Also detect that the server no longer runs.
3331 */
3332 if (ret >= 0 && argtype == ARGTYPE_EDIT_WAIT)
3333 {
3334 int numFiles = *argc - i - 1;
3335 int j;
3336 char_u *done = alloc(numFiles);
3337 char_u *p;
3338# ifdef FEAT_GUI_W32
3339 NOTIFYICONDATA ni;
3340 int count = 0;
3341 extern HWND message_window;
3342# endif
3343
3344 if (numFiles > 0 && argv[i + 1][0] == '+')
3345 /* Skip "+cmd" argument, don't wait for it to be edited. */
3346 --numFiles;
3347
3348# ifdef FEAT_GUI_W32
3349 ni.cbSize = sizeof(ni);
3350 ni.hWnd = message_window;
3351 ni.uID = 0;
3352 ni.uFlags = NIF_ICON|NIF_TIP;
3353 ni.hIcon = LoadIcon((HINSTANCE)GetModuleHandle(0), "IDR_VIM");
3354 sprintf(ni.szTip, _("%d of %d edited"), count, numFiles);
3355 Shell_NotifyIcon(NIM_ADD, &ni);
3356# endif
3357
3358 /* Wait for all files to unload in remote */
3359 memset(done, 0, numFiles);
3360 while (memchr(done, 0, numFiles) != NULL)
3361 {
3362# ifdef WIN32
3363 p = serverGetReply(srv, NULL, TRUE, TRUE);
3364 if (p == NULL)
3365 break;
3366# else
3367 if (serverReadReply(xterm_dpy, srv, &p, TRUE) < 0)
3368 break;
3369# endif
3370 j = atoi((char *)p);
3371 if (j >= 0 && j < numFiles)
3372 {
3373# ifdef FEAT_GUI_W32
3374 ++count;
3375 sprintf(ni.szTip, _("%d of %d edited"),
3376 count, numFiles);
3377 Shell_NotifyIcon(NIM_MODIFY, &ni);
3378# endif
3379 done[j] = 1;
3380 }
3381 }
3382# ifdef FEAT_GUI_W32
3383 Shell_NotifyIcon(NIM_DELETE, &ni);
3384# endif
3385 }
3386 }
3387 else if (STRICMP(argv[i], "--remote-expr") == 0)
3388 {
3389 if (i == *argc - 1)
3390 mainerr_arg_missing((char_u *)argv[i]);
3391# ifdef WIN32
3392 /* Win32 always works? */
3393 if (serverSendToVim(sname, (char_u *)argv[i + 1],
3394 &res, NULL, 1, FALSE) < 0)
3395# else
3396 if (xterm_dpy == NULL)
3397 mch_errmsg(_("No display: Send expression failed.\n"));
3398 else if (serverSendToVim(xterm_dpy, sname, (char_u *)argv[i + 1],
3399 &res, NULL, 1, 1, FALSE) < 0)
3400# endif
3401 {
3402 if (res != NULL && *res != NUL)
3403 {
3404 /* Output error from remote */
3405 mch_errmsg((char *)res);
3406 vim_free(res);
3407 res = NULL;
3408 }
3409 mch_errmsg(_(": Send expression failed.\n"));
3410 }
3411 }
3412 else if (STRICMP(argv[i], "--serverlist") == 0)
3413 {
3414# ifdef WIN32
3415 /* Win32 always works? */
3416 res = serverGetVimNames();
3417# else
3418 if (xterm_dpy != NULL)
3419 res = serverGetVimNames(xterm_dpy);
3420# endif
3421 if (called_emsg)
3422 mch_errmsg("\n");
3423 }
3424 else if (STRICMP(argv[i], "--servername") == 0)
3425 {
3426 /* Alredy processed. Take it out of the command line */
3427 i++;
3428 continue;
3429 }
3430 else
3431 {
3432 *newArgV++ = argv[i];
3433 newArgC++;
3434 continue;
3435 }
3436 didone = TRUE;
3437 if (res != NULL && *res != NUL)
3438 {
3439 mch_msg((char *)res);
3440 if (res[STRLEN(res) - 1] != '\n')
3441 mch_msg("\n");
3442 }
3443 vim_free(res);
3444 }
3445
3446 if (didone)
3447 {
3448 display_errors(); /* display any collected messages */
3449 exit(exiterr); /* Mission accomplished - get out */
3450 }
3451
3452 /* Return back into main() */
3453 *argc = newArgC;
3454 vim_free(sname);
3455}
3456
3457/*
3458 * Build a ":drop" command to send to a Vim server.
3459 */
3460 static char_u *
3461build_drop_cmd(filec, filev, sendReply)
3462 int filec;
3463 char **filev;
3464 int sendReply;
3465{
3466 garray_T ga;
3467 int i;
3468 char_u *inicmd = NULL;
3469 char_u *p;
3470 char_u cwd[MAXPATHL];
3471
3472 if (filec > 0 && filev[0][0] == '+')
3473 {
3474 inicmd = (char_u *)filev[0] + 1;
3475 filev++;
3476 filec--;
3477 }
3478 /* Check if we have at least one argument. */
3479 if (filec <= 0)
3480 mainerr_arg_missing((char_u *)filev[-1]);
3481 if (mch_dirname(cwd, MAXPATHL) != OK)
3482 return NULL;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003483 if ((p = vim_strsave_escaped_ext(cwd, PATH_ESC_CHARS, '\\', TRUE)) == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003484 return NULL;
3485 ga_init2(&ga, 1, 100);
3486 ga_concat(&ga, (char_u *)"<C-\\><C-N>:cd ");
3487 ga_concat(&ga, p);
3488 /* Call inputsave() so that a prompt for an encryption key works. */
3489 ga_concat(&ga, (char_u *)"<CR>:if exists('*inputsave')|call inputsave()|endif|drop");
3490 vim_free(p);
3491 for (i = 0; i < filec; i++)
3492 {
3493 /* On Unix the shell has already expanded the wildcards, don't want to
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003494 * do it again in the Vim server. On MS-Windows only escape
3495 * non-wildcard characters. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003496 p = vim_strsave_escaped((char_u *)filev[i],
3497#ifdef UNIX
3498 PATH_ESC_CHARS
3499#else
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003500 (char_u *)" \t%#"
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003501#endif
3502 );
3503 if (p == NULL)
3504 {
3505 vim_free(ga.ga_data);
3506 return NULL;
3507 }
3508 ga_concat(&ga, (char_u *)" ");
3509 ga_concat(&ga, p);
3510 vim_free(p);
3511 }
3512 /* The :drop commands goes to Insert mode when 'insertmode' is set, use
3513 * CTRL-\ CTRL-N again. */
3514 ga_concat(&ga, (char_u *)"|if exists('*inputrestore')|call inputrestore()|endif<CR>");
3515 ga_concat(&ga, (char_u *)"<C-\\><C-N>:cd -");
3516 if (sendReply)
3517 ga_concat(&ga, (char_u *)"<CR>:call SetupRemoteReplies()");
3518 ga_concat(&ga, (char_u *)"<CR>:");
3519 if (inicmd != NULL)
3520 {
3521 /* Can't use <CR> after "inicmd", because an "startinsert" would cause
3522 * the following commands to be inserted as text. Use a "|",
3523 * hopefully "inicmd" does allow this... */
3524 ga_concat(&ga, inicmd);
3525 ga_concat(&ga, (char_u *)"|");
3526 }
3527 /* Bring the window to the foreground, goto Insert mode when 'im' set and
3528 * clear command line. */
Bram Moolenaar567e4de2004-12-31 21:01:02 +00003529 ga_concat(&ga, (char_u *)"cal foreground()|if &im|star|en|redr|f<CR>");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003530 ga_append(&ga, NUL);
3531 return ga.ga_data;
3532}
3533
3534/*
3535 * Replace termcodes such as <CR> and insert as key presses if there is room.
3536 */
3537 void
3538server_to_input_buf(str)
3539 char_u *str;
3540{
3541 char_u *ptr = NULL;
3542 char_u *cpo_save = p_cpo;
3543
3544 /* Set 'cpoptions' the way we want it.
3545 * B set - backslashes are *not* treated specially
3546 * k set - keycodes are *not* reverse-engineered
3547 * < unset - <Key> sequences *are* interpreted
3548 * The last parameter of replace_termcodes() is TRUE so that the <lt>
3549 * sequence is recognised - needed for a real backslash.
3550 */
3551 p_cpo = (char_u *)"Bk";
3552 str = replace_termcodes((char_u *)str, &ptr, FALSE, TRUE);
3553 p_cpo = cpo_save;
3554
3555 if (*ptr != NUL) /* trailing CTRL-V results in nothing */
3556 {
3557 /*
3558 * Add the string to the input stream.
3559 * Can't use add_to_input_buf() here, we now have K_SPECIAL bytes.
3560 *
3561 * First clear typed characters from the typeahead buffer, there could
3562 * be half a mapping there. Then append to the existing string, so
3563 * that multiple commands from a client are concatenated.
3564 */
3565 if (typebuf.tb_maplen < typebuf.tb_len)
3566 del_typebuf(typebuf.tb_len - typebuf.tb_maplen, typebuf.tb_maplen);
3567 (void)ins_typebuf(str, REMAP_NONE, typebuf.tb_len, TRUE, FALSE);
3568
3569 /* Let input_available() know we inserted text in the typeahead
3570 * buffer. */
3571 received_from_client = TRUE;
3572 }
3573 vim_free((char_u *)ptr);
3574}
3575
3576/*
3577 * Evaluate an expression that the client sent to a string.
3578 * Handles disabling error messages and disables debugging, otherwise Vim
3579 * hangs, waiting for "cont" to be typed.
3580 */
3581 char_u *
3582eval_client_expr_to_string(expr)
3583 char_u *expr;
3584{
3585 char_u *res;
3586 int save_dbl = debug_break_level;
3587 int save_ro = redir_off;
3588
3589 debug_break_level = -1;
3590 redir_off = 0;
3591 ++emsg_skip;
3592
3593 res = eval_to_string(expr, NULL);
3594
3595 debug_break_level = save_dbl;
3596 redir_off = save_ro;
3597 --emsg_skip;
3598
Bram Moolenaar63a121b2005-12-11 21:36:39 +00003599 /* A client can tell us to redraw, but not to display the cursor, so do
3600 * that here. */
3601 setcursor();
3602 out_flush();
3603#ifdef FEAT_GUI
3604 if (gui.in_use)
3605 gui_update_cursor(FALSE, FALSE);
3606#endif
3607
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003608 return res;
3609}
3610
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003611/*
3612 * If conversion is needed, convert "data" from "client_enc" to 'encoding' and
3613 * return an allocated string. Otherwise return "data".
3614 * "*tofree" is set to the result when it needs to be freed later.
3615 */
3616/*ARGSUSED*/
3617 char_u *
3618serverConvert(client_enc, data, tofree)
3619 char_u *client_enc;
3620 char_u *data;
3621 char_u **tofree;
3622{
3623 char_u *res = data;
3624
3625 *tofree = NULL;
3626# ifdef FEAT_MBYTE
3627 if (client_enc != NULL && p_enc != NULL)
3628 {
3629 vimconv_T vimconv;
3630
3631 vimconv.vc_type = CONV_NONE;
3632 if (convert_setup(&vimconv, client_enc, p_enc) != FAIL
3633 && vimconv.vc_type != CONV_NONE)
3634 {
3635 res = string_convert(&vimconv, data, NULL);
3636 if (res == NULL)
3637 res = data;
3638 else
3639 *tofree = res;
3640 }
3641 convert_setup(&vimconv, NULL, NULL);
3642 }
3643# endif
3644 return res;
3645}
3646
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003647
3648/*
3649 * Make our basic server name: use the specified "arg" if given, otherwise use
3650 * the tail of the command "cmd" we were started with.
3651 * Return the name in allocated memory. This doesn't include a serial number.
3652 */
3653 static char_u *
3654serverMakeName(arg, cmd)
3655 char_u *arg;
3656 char *cmd;
3657{
3658 char_u *p;
3659
3660 if (arg != NULL && *arg != NUL)
3661 p = vim_strsave_up(arg);
3662 else
3663 {
3664 p = vim_strsave_up(gettail((char_u *)cmd));
3665 /* Remove .exe or .bat from the name. */
3666 if (p != NULL && vim_strchr(p, '.') != NULL)
3667 *vim_strchr(p, '.') = NUL;
3668 }
3669 return p;
3670}
3671#endif /* FEAT_CLIENTSERVER */
3672
3673/*
3674 * When FEAT_FKMAP is defined, also compile the Farsi source code.
3675 */
3676#if defined(FEAT_FKMAP) || defined(PROTO)
3677# include "farsi.c"
3678#endif
3679
3680/*
3681 * When FEAT_ARABIC is defined, also compile the Arabic source code.
3682 */
3683#if defined(FEAT_ARABIC) || defined(PROTO)
3684# include "arabic.c"
3685#endif