blob: 31c6f6fbab6fc94aad3cbacf3cddfc17897ff960 [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
1713#ifdef TARGET_API_MAC_OSX
1714 /* For some reason on MacOS X, an argument like:
1715 -psn_0_10223617 is passed in when invoke from Finder
1716 or with the 'open' command */
1717 case 'p':
1718 argv_idx = -1; /* bypass full -psn */
1719 main_start_gui();
1720 break;
1721#endif
1722 case 'M': /* "-M" no changes or writing of files */
1723 reset_modifiable();
1724 /* FALLTHROUGH */
1725
1726 case 'm': /* "-m" no writing of files */
1727 p_write = FALSE;
1728 break;
1729
1730 case 'y': /* "-y" easy mode */
1731#ifdef FEAT_GUI
1732 gui.starting = TRUE; /* start GUI a bit later */
1733#endif
1734 parmp->evim_mode = TRUE;
1735 break;
1736
1737 case 'N': /* "-N" Nocompatible */
1738 change_compatible(FALSE);
1739 break;
1740
1741 case 'n': /* "-n" no swap file */
1742 parmp->no_swap_file = TRUE;
1743 break;
1744
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001745 case 'p': /* "-p[N]" open N tab pages */
1746#ifdef FEAT_WINDOWS
1747 /* default is 0: open window for each file */
1748 parmp->window_count = get_number_arg((char_u *)argv[0],
1749 &argv_idx, 0);
1750 parmp->window_layout = WIN_TABS;
1751#endif
1752 break;
1753
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001754 case 'o': /* "-o[N]" open N horizontal split windows */
1755#ifdef FEAT_WINDOWS
1756 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00001757 parmp->window_count = get_number_arg((char_u *)argv[0],
1758 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001759 parmp->window_layout = WIN_HOR;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001760#endif
1761 break;
1762
1763 case 'O': /* "-O[N]" open N vertical split windows */
1764#if defined(FEAT_VERTSPLIT) && defined(FEAT_WINDOWS)
1765 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00001766 parmp->window_count = get_number_arg((char_u *)argv[0],
1767 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00001768 parmp->window_layout = WIN_VER;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001769#endif
1770 break;
1771
1772#ifdef FEAT_QUICKFIX
1773 case 'q': /* "-q" QuickFix mode */
1774 if (parmp->edit_type != EDIT_NONE)
1775 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1776 parmp->edit_type = EDIT_QF;
1777 if (argv[0][argv_idx]) /* "-q{errorfile}" */
1778 {
1779 parmp->use_ef = (char_u *)argv[0] + argv_idx;
1780 argv_idx = -1;
1781 }
1782 else if (argc > 1) /* "-q {errorfile}" */
1783 want_argument = TRUE;
1784 break;
1785#endif
1786
1787 case 'R': /* "-R" readonly mode */
1788 readonlymode = TRUE;
1789 curbuf->b_p_ro = TRUE;
1790 p_uc = 10000; /* don't update very often */
1791 break;
1792
1793 case 'r': /* "-r" recovery mode */
1794 case 'L': /* "-L" recovery mode */
1795 recoverymode = 1;
1796 break;
1797
1798 case 's':
1799 if (exmode_active) /* "-s" silent (batch) mode */
1800 silent_mode = TRUE;
1801 else /* "-s {scriptin}" read from script file */
1802 want_argument = TRUE;
1803 break;
1804
1805 case 't': /* "-t {tag}" or "-t{tag}" jump to tag */
1806 if (parmp->edit_type != EDIT_NONE)
1807 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1808 parmp->edit_type = EDIT_TAG;
1809 if (argv[0][argv_idx]) /* "-t{tag}" */
1810 {
1811 parmp->tagname = (char_u *)argv[0] + argv_idx;
1812 argv_idx = -1;
1813 }
1814 else /* "-t {tag}" */
1815 want_argument = TRUE;
1816 break;
1817
1818#ifdef FEAT_EVAL
1819 case 'D': /* "-D" Debugging */
1820 parmp->use_debug_break_level = 9999;
1821 break;
1822#endif
1823#ifdef FEAT_DIFF
1824 case 'd': /* "-d" 'diff' */
1825# ifdef AMIGA
1826 /* check for "-dev {device}" */
1827 if (argv[0][argv_idx] == 'e' && argv[0][argv_idx + 1] == 'v')
1828 want_argument = TRUE;
1829 else
1830# endif
1831 parmp->diff_mode = TRUE;
1832 break;
1833#endif
1834 case 'V': /* "-V{N}" Verbose level */
1835 /* default is 10: a little bit verbose */
1836 p_verbose = get_number_arg((char_u *)argv[0], &argv_idx, 10);
1837 if (argv[0][argv_idx] != NUL)
1838 {
1839 set_option_value((char_u *)"verbosefile", 0L,
1840 (char_u *)argv[0] + argv_idx, 0);
1841 argv_idx = STRLEN(argv[0]);
1842 }
1843 break;
1844
1845 case 'v': /* "-v" Vi-mode (as if called "vi") */
1846 exmode_active = 0;
1847#ifdef FEAT_GUI
1848 gui.starting = FALSE; /* don't start GUI */
1849#endif
1850 break;
1851
1852 case 'w': /* "-w{number}" set window height */
1853 /* "-w {scriptout}" write to script */
1854 if (vim_isdigit(((char_u *)argv[0])[argv_idx]))
1855 {
1856 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
1857 set_option_value((char_u *)"window", n, NULL, 0);
1858 break;
1859 }
1860 want_argument = TRUE;
1861 break;
1862
1863#ifdef FEAT_CRYPT
1864 case 'x': /* "-x" encrypted reading/writing of files */
1865 parmp->ask_for_key = TRUE;
1866 break;
1867#endif
1868
1869 case 'X': /* "-X" don't connect to X server */
1870#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
1871 x_no_connect = TRUE;
1872#endif
1873 break;
1874
1875 case 'Z': /* "-Z" restricted mode */
1876 restricted = TRUE;
1877 break;
1878
1879 case 'c': /* "-c{command}" or "-c {command}" execute
1880 command */
1881 if (argv[0][argv_idx] != NUL)
1882 {
1883 if (parmp->n_commands >= MAX_ARG_CMDS)
1884 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar231334e2005-07-25 20:46:57 +00001885 parmp->commands[parmp->n_commands++] = (char_u *)argv[0]
1886 + argv_idx;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001887 argv_idx = -1;
1888 break;
1889 }
1890 /*FALLTHROUGH*/
1891 case 'S': /* "-S {file}" execute Vim script */
1892 case 'i': /* "-i {viminfo}" use for viminfo */
1893#ifndef FEAT_DIFF
1894 case 'd': /* "-d {device}" device (for Amiga) */
1895#endif
1896 case 'T': /* "-T {terminal}" terminal name */
1897 case 'u': /* "-u {vimrc}" vim inits file */
1898 case 'U': /* "-U {gvimrc}" gvim inits file */
1899 case 'W': /* "-W {scriptout}" overwrite */
1900#ifdef FEAT_GUI_W32
1901 case 'P': /* "-P {parent title}" MDI parent */
1902#endif
1903 want_argument = TRUE;
1904 break;
1905
1906 default:
1907 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
1908 }
1909
1910 /*
1911 * Handle option arguments with argument.
1912 */
1913 if (want_argument)
1914 {
1915 /*
1916 * Check for garbage immediately after the option letter.
1917 */
1918 if (argv[0][argv_idx] != NUL)
1919 mainerr(ME_GARBAGE, (char_u *)argv[0]);
1920
1921 --argc;
1922 if (argc < 1 && c != 'S')
1923 mainerr_arg_missing((char_u *)argv[0]);
1924 ++argv;
1925 argv_idx = -1;
1926
1927 switch (c)
1928 {
1929 case 'c': /* "-c {command}" execute command */
1930 case 'S': /* "-S {file}" execute Vim script */
1931 if (parmp->n_commands >= MAX_ARG_CMDS)
1932 mainerr(ME_EXTRA_CMD, NULL);
1933 if (c == 'S')
1934 {
1935 char *a;
1936
1937 if (argc < 1)
1938 /* "-S" without argument: use default session file
1939 * name. */
1940 a = SESSION_FILE;
1941 else if (argv[0][0] == '-')
1942 {
1943 /* "-S" followed by another option: use default
1944 * session file name. */
1945 a = SESSION_FILE;
1946 ++argc;
1947 --argv;
1948 }
1949 else
1950 a = argv[0];
1951 p = alloc((unsigned)(STRLEN(a) + 4));
1952 if (p == NULL)
1953 mch_exit(2);
1954 sprintf((char *)p, "so %s", a);
1955 parmp->cmds_tofree[parmp->n_commands] = TRUE;
1956 parmp->commands[parmp->n_commands++] = p;
1957 }
1958 else
Bram Moolenaar231334e2005-07-25 20:46:57 +00001959 parmp->commands[parmp->n_commands++] =
1960 (char_u *)argv[0];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001961 break;
1962
1963 case '-': /* "--cmd {command}" execute command */
1964 if (parmp->n_pre_commands >= MAX_ARG_CMDS)
1965 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar231334e2005-07-25 20:46:57 +00001966 parmp->pre_commands[parmp->n_pre_commands++] =
1967 (char_u *)argv[0];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001968 break;
1969
1970 /* case 'd': -d {device} is handled in mch_check_win() for the
1971 * Amiga */
1972
1973#ifdef FEAT_QUICKFIX
1974 case 'q': /* "-q {errorfile}" QuickFix mode */
1975 parmp->use_ef = (char_u *)argv[0];
1976 break;
1977#endif
1978
1979 case 'i': /* "-i {viminfo}" use for viminfo */
1980 use_viminfo = (char_u *)argv[0];
1981 break;
1982
1983 case 's': /* "-s {scriptin}" read from script file */
1984 if (scriptin[0] != NULL)
1985 {
1986scripterror:
1987 mch_errmsg(_("Attempt to open script file again: \""));
1988 mch_errmsg(argv[-1]);
1989 mch_errmsg(" ");
1990 mch_errmsg(argv[0]);
1991 mch_errmsg("\"\n");
1992 mch_exit(2);
1993 }
1994 if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL)
1995 {
1996 mch_errmsg(_("Cannot open for reading: \""));
1997 mch_errmsg(argv[0]);
1998 mch_errmsg("\"\n");
1999 mch_exit(2);
2000 }
2001 if (save_typebuf() == FAIL)
2002 mch_exit(2); /* out of memory */
2003 break;
2004
2005 case 't': /* "-t {tag}" */
2006 parmp->tagname = (char_u *)argv[0];
2007 break;
2008
2009 case 'T': /* "-T {terminal}" terminal name */
2010 /*
2011 * The -T term argument is always available and when
2012 * HAVE_TERMLIB is supported it overrides the environment
2013 * variable TERM.
2014 */
2015#ifdef FEAT_GUI
2016 if (term_is_gui((char_u *)argv[0]))
2017 gui.starting = TRUE; /* start GUI a bit later */
2018 else
2019#endif
2020 parmp->term = (char_u *)argv[0];
2021 break;
2022
2023 case 'u': /* "-u {vimrc}" vim inits file */
2024 parmp->use_vimrc = (char_u *)argv[0];
2025 break;
2026
2027 case 'U': /* "-U {gvimrc}" gvim inits file */
2028#ifdef FEAT_GUI
2029 use_gvimrc = (char_u *)argv[0];
2030#endif
2031 break;
2032
2033 case 'w': /* "-w {nr}" 'window' value */
2034 /* "-w {scriptout}" append to script file */
2035 if (vim_isdigit(*((char_u *)argv[0])))
2036 {
2037 argv_idx = 0;
2038 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2039 set_option_value((char_u *)"window", n, NULL, 0);
2040 argv_idx = -1;
2041 break;
2042 }
2043 /*FALLTHROUGH*/
2044 case 'W': /* "-W {scriptout}" overwrite script file */
2045 if (scriptout != NULL)
2046 goto scripterror;
2047 if ((scriptout = mch_fopen(argv[0],
2048 c == 'w' ? APPENDBIN : WRITEBIN)) == NULL)
2049 {
2050 mch_errmsg(_("Cannot open for script output: \""));
2051 mch_errmsg(argv[0]);
2052 mch_errmsg("\"\n");
2053 mch_exit(2);
2054 }
2055 break;
2056
2057#ifdef FEAT_GUI_W32
2058 case 'P': /* "-P {parent title}" MDI parent */
2059 gui_mch_set_parent(argv[0]);
2060 break;
2061#endif
2062 }
2063 }
2064 }
2065
2066 /*
2067 * File name argument.
2068 */
2069 else
2070 {
2071 argv_idx = -1; /* skip to next argument */
2072
2073 /* Check for only one type of editing. */
2074 if (parmp->edit_type != EDIT_NONE && parmp->edit_type != EDIT_FILE)
2075 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2076 parmp->edit_type = EDIT_FILE;
2077
2078#ifdef MSWIN
2079 /* Remember if the argument was a full path before changing
2080 * slashes to backslashes. */
2081 if (argv[0][0] != NUL && argv[0][1] == ':' && argv[0][2] == '\\')
2082 parmp->full_path = TRUE;
2083#endif
2084
2085 /* Add the file to the global argument list. */
2086 if (ga_grow(&global_alist.al_ga, 1) == FAIL
2087 || (p = vim_strsave((char_u *)argv[0])) == NULL)
2088 mch_exit(2);
2089#ifdef FEAT_DIFF
2090 if (parmp->diff_mode && mch_isdir(p) && GARGCOUNT > 0
2091 && !mch_isdir(alist_name(&GARGLIST[0])))
2092 {
2093 char_u *r;
2094
2095 r = concat_fnames(p, gettail(alist_name(&GARGLIST[0])), TRUE);
2096 if (r != NULL)
2097 {
2098 vim_free(p);
2099 p = r;
2100 }
2101 }
2102#endif
2103#if defined(__CYGWIN32__) && !defined(WIN32)
2104 /*
2105 * If vim is invoked by non-Cygwin tools, convert away any
2106 * DOS paths, so things like .swp files are created correctly.
2107 * Look for evidence of non-Cygwin paths before we bother.
2108 * This is only for when using the Unix files.
2109 */
2110 if (strpbrk(p, "\\:") != NULL)
2111 {
2112 char posix_path[PATH_MAX];
2113
2114 cygwin_conv_to_posix_path(p, posix_path);
2115 vim_free(p);
2116 p = vim_strsave(posix_path);
2117 if (p == NULL)
2118 mch_exit(2);
2119 }
2120#endif
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002121
2122#ifdef USE_FNAME_CASE
2123 /* Make the case of the file name match the actual file. */
2124 fname_case(p, 0);
2125#endif
2126
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002127 alist_add(&global_alist, p,
2128#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
Bram Moolenaar231334e2005-07-25 20:46:57 +00002129 parmp->literal ? 2 : 0 /* add buffer nr after exp. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002130#else
2131 2 /* add buffer number now and use curbuf */
2132#endif
2133 );
2134
2135#if defined(FEAT_MBYTE) && defined(WIN32)
2136 {
2137 extern void used_file_arg(char *, int, int);
2138
2139 /* Remember this argument has been added to the argument list.
2140 * Needed when 'encoding' is changed. */
2141 used_file_arg(argv[0], parmp->literal, parmp->full_path);
2142 }
2143#endif
2144 }
2145
2146 /*
2147 * If there are no more letters after the current "-", go to next
2148 * argument. argv_idx is set to -1 when the current argument is to be
2149 * skipped.
2150 */
2151 if (argv_idx <= 0 || argv[0][argv_idx] == NUL)
2152 {
2153 --argc;
2154 ++argv;
2155 argv_idx = 1;
2156 }
2157 }
2158}
2159
2160/*
2161 * Print a warning if stdout is not a terminal.
2162 * When starting in Ex mode and commands come from a file, set Silent mode.
2163 */
2164 static void
2165check_tty(parmp)
2166 mparm_T *parmp;
2167{
2168 int input_isatty; /* is active input a terminal? */
2169
2170 input_isatty = mch_input_isatty();
2171 if (exmode_active)
2172 {
2173 if (!input_isatty)
2174 silent_mode = TRUE;
2175 }
2176 else if (parmp->want_full_screen && (!parmp->stdout_isatty || !input_isatty)
2177#ifdef FEAT_GUI
2178 /* don't want the delay when started from the desktop */
2179 && !gui.starting
2180#endif
2181 )
2182 {
2183#ifdef NBDEBUG
2184 /*
2185 * This shouldn't be necessary. But if I run netbeans with the log
2186 * output coming to the console and XOpenDisplay fails, I get vim
2187 * trying to start with input/output to my console tty. This fills my
2188 * input buffer so fast I can't even kill the process in under 2
2189 * minutes (and it beeps continuosly the whole time :-)
2190 */
2191 if (usingNetbeans && (!parmp->stdout_isatty || !input_isatty))
2192 {
2193 mch_errmsg(_("Vim: Error: Failure to start gvim from NetBeans\n"));
2194 exit(1);
2195 }
2196#endif
2197 if (!parmp->stdout_isatty)
2198 mch_errmsg(_("Vim: Warning: Output is not to a terminal\n"));
2199 if (!input_isatty)
2200 mch_errmsg(_("Vim: Warning: Input is not from a terminal\n"));
2201 out_flush();
2202 if (scriptin[0] == NULL)
2203 ui_delay(2000L, TRUE);
2204 TIME_MSG("Warning delay");
2205 }
2206}
2207
2208/*
2209 * Read text from stdin.
2210 */
2211 static void
2212read_stdin()
2213{
2214 int i;
2215
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002216#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002217 /* When getting the ATTENTION prompt here, use a dialog */
2218 swap_exists_action = SEA_DIALOG;
2219#endif
2220 no_wait_return = TRUE;
2221 i = msg_didany;
2222 set_buflisted(TRUE);
2223 (void)open_buffer(TRUE, NULL); /* create memfile and read file */
2224 no_wait_return = FALSE;
2225 msg_didany = i;
2226 TIME_MSG("reading stdin");
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002227#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002228 check_swap_exists_action();
2229#endif
2230#if !(defined(AMIGA) || defined(MACOS))
2231 /*
2232 * Close stdin and dup it from stderr. Required for GPM to work
2233 * properly, and for running external commands.
2234 * Is there any other system that cannot do this?
2235 */
2236 close(0);
2237 dup(2);
2238#endif
2239}
2240
2241/*
2242 * Create the requested number of windows and edit buffers in them.
2243 * Also does recovery if "recoverymode" set.
2244 */
2245/*ARGSUSED*/
2246 static void
2247create_windows(parmp)
2248 mparm_T *parmp;
2249{
2250#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002251 int rewind;
2252 int done = 0;
2253
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002254 /*
2255 * Create the number of windows that was requested.
2256 */
2257 if (parmp->window_count == -1) /* was not set */
2258 parmp->window_count = 1;
2259 if (parmp->window_count == 0)
2260 parmp->window_count = GARGCOUNT;
2261 if (parmp->window_count > 1)
2262 {
2263 /* Don't change the windows if there was a command in .vimrc that
2264 * already split some windows */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002265 if (parmp->window_layout == 0)
2266 parmp->window_layout = WIN_HOR;
2267 if (parmp->window_layout == WIN_TABS)
2268 {
2269 parmp->window_count = make_tabpages(parmp->window_count);
2270 TIME_MSG("making tab pages");
2271 }
2272 else if (firstwin->w_next == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002273 {
2274 parmp->window_count = make_windows(parmp->window_count,
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002275 parmp->window_layout == WIN_VER);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002276 TIME_MSG("making windows");
2277 }
2278 else
2279 parmp->window_count = win_count();
2280 }
2281 else
2282 parmp->window_count = 1;
2283#endif
2284
2285 if (recoverymode) /* do recover */
2286 {
2287 msg_scroll = TRUE; /* scroll message up */
2288 ml_recover();
2289 if (curbuf->b_ml.ml_mfp == NULL) /* failed */
2290 getout(1);
2291 do_modelines(FALSE); /* do modelines */
2292 }
2293 else
2294 {
2295 /*
2296 * Open a buffer for windows that don't have one yet.
2297 * Commands in the .vimrc might have loaded a file or split the window.
2298 * Watch out for autocommands that delete a window.
2299 */
2300#ifdef FEAT_AUTOCMD
2301 /*
2302 * Don't execute Win/Buf Enter/Leave autocommands here
2303 */
2304 ++autocmd_no_enter;
2305 ++autocmd_no_leave;
2306#endif
2307#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002308 rewind = TRUE;
2309 while (done++ < 1000)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002310 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002311 if (rewind)
2312 {
2313 if (parmp->window_layout == WIN_TABS)
2314 goto_tabpage(1);
2315 else
2316 curwin = firstwin;
2317 }
2318 else if (parmp->window_layout == WIN_TABS)
2319 {
2320 if (curtab->tp_next == NULL)
2321 break;
2322 goto_tabpage(0);
2323 }
2324 else
2325 {
2326 if (curwin->w_next == NULL)
2327 break;
2328 curwin = curwin->w_next;
2329 }
2330 rewind = FALSE;
2331#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002332 curbuf = curwin->w_buffer;
2333 if (curbuf->b_ml.ml_mfp == NULL)
2334 {
2335#ifdef FEAT_FOLDING
2336 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2337 if (p_fdls >= 0)
2338 curwin->w_p_fdl = p_fdls;
2339#endif
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002340#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002341 /* When getting the ATTENTION prompt here, use a dialog */
2342 swap_exists_action = SEA_DIALOG;
2343#endif
2344 set_buflisted(TRUE);
2345 (void)open_buffer(FALSE, NULL); /* create memfile, read file */
2346
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002347#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002348 check_swap_exists_action();
2349#endif
2350#ifdef FEAT_AUTOCMD
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002351 rewind = TRUE; /* start again */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002352#endif
2353 }
2354#ifdef FEAT_WINDOWS
2355 ui_breakcheck();
2356 if (got_int)
2357 {
2358 (void)vgetc(); /* only break the file loading, not the rest */
2359 break;
2360 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002361 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002362#endif
2363#ifdef FEAT_WINDOWS
2364 if (parmp->window_layout == WIN_TABS)
2365 goto_tabpage(1);
2366 else
2367 curwin = firstwin;
2368 curbuf = curwin->w_buffer;
2369#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002370#ifdef FEAT_AUTOCMD
2371 --autocmd_no_enter;
2372 --autocmd_no_leave;
2373#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002374 }
2375}
2376
2377#ifdef FEAT_WINDOWS
2378 /*
2379 * If opened more than one window, start editing files in the other
2380 * windows. make_windows() has already opened the windows.
2381 */
2382 static void
2383edit_buffers(parmp)
2384 mparm_T *parmp;
2385{
2386 int arg_idx; /* index in argument list */
2387 int i;
2388
2389# ifdef FEAT_AUTOCMD
2390 /*
2391 * Don't execute Win/Buf Enter/Leave autocommands here
2392 */
2393 ++autocmd_no_enter;
2394 ++autocmd_no_leave;
2395# endif
2396 arg_idx = 1;
2397 for (i = 1; i < parmp->window_count; ++i)
2398 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002399 if (parmp->window_layout == WIN_TABS)
2400 {
2401 if (curtab->tp_next == NULL) /* just checking */
2402 break;
2403 goto_tabpage(0);
2404 }
2405 else
2406 {
2407 if (curwin->w_next == NULL) /* just checking */
2408 break;
2409 win_enter(curwin->w_next, FALSE);
2410 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002411
2412 /* Only open the file if there is no file in this window yet (that can
2413 * happen when .vimrc contains ":sall") */
2414 if (curbuf == firstwin->w_buffer || curbuf->b_ffname == NULL)
2415 {
2416 curwin->w_arg_idx = arg_idx;
2417 /* edit file from arg list, if there is one */
2418 (void)do_ecmd(0, arg_idx < GARGCOUNT
2419 ? alist_name(&GARGLIST[arg_idx]) : NULL,
2420 NULL, NULL, ECMD_LASTL, ECMD_HIDE);
2421 if (arg_idx == GARGCOUNT - 1)
2422 arg_had_last = TRUE;
2423 ++arg_idx;
2424 }
2425 ui_breakcheck();
2426 if (got_int)
2427 {
2428 (void)vgetc(); /* only break the file loading, not the rest */
2429 break;
2430 }
2431 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002432
2433 if (parmp->window_layout == WIN_TABS)
2434 goto_tabpage(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002435# ifdef FEAT_AUTOCMD
2436 --autocmd_no_enter;
2437# endif
2438 win_enter(firstwin, FALSE); /* back to first window */
2439# ifdef FEAT_AUTOCMD
2440 --autocmd_no_leave;
2441# endif
2442 TIME_MSG("editing files in windows");
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002443 if (parmp->window_count > 1 && parmp->window_layout != WIN_TABS)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002444 win_equal(curwin, FALSE, 'b'); /* adjust heights */
2445}
2446#endif /* FEAT_WINDOWS */
2447
2448/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002449 * Execute the commands from --cmd arguments "cmds[cnt]".
2450 */
2451 static void
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002452exe_pre_commands(parmp)
2453 mparm_T *parmp;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002454{
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002455 char_u **cmds = parmp->pre_commands;
2456 int cnt = parmp->n_pre_commands;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002457 int i;
2458
2459 if (cnt > 0)
2460 {
2461 curwin->w_cursor.lnum = 0; /* just in case.. */
2462 sourcing_name = (char_u *)_("pre-vimrc command line");
2463# ifdef FEAT_EVAL
2464 current_SID = SID_CMDARG;
2465# endif
2466 for (i = 0; i < cnt; ++i)
2467 do_cmdline_cmd(cmds[i]);
2468 sourcing_name = NULL;
2469# ifdef FEAT_EVAL
2470 current_SID = 0;
2471# endif
2472 TIME_MSG("--cmd commands");
2473 }
2474}
2475
2476/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002477 * Execute "+", "-c" and "-S" arguments.
2478 */
2479 static void
2480exe_commands(parmp)
2481 mparm_T *parmp;
2482{
2483 int i;
2484
2485 /*
2486 * We start commands on line 0, make "vim +/pat file" match a
2487 * pattern on line 1. But don't move the cursor when an autocommand
2488 * with g`" was used.
2489 */
2490 msg_scroll = TRUE;
2491 if (parmp->tagname == NULL && curwin->w_cursor.lnum <= 1)
2492 curwin->w_cursor.lnum = 0;
2493 sourcing_name = (char_u *)"command line";
2494#ifdef FEAT_EVAL
2495 current_SID = SID_CARG;
2496#endif
2497 for (i = 0; i < parmp->n_commands; ++i)
2498 {
2499 do_cmdline_cmd(parmp->commands[i]);
2500 if (parmp->cmds_tofree[i])
2501 vim_free(parmp->commands[i]);
2502 }
2503 sourcing_name = NULL;
2504#ifdef FEAT_EVAL
2505 current_SID = 0;
2506#endif
2507 if (curwin->w_cursor.lnum == 0)
2508 curwin->w_cursor.lnum = 1;
2509
2510 if (!exmode_active)
2511 msg_scroll = FALSE;
2512
2513#ifdef FEAT_QUICKFIX
2514 /* When started with "-q errorfile" jump to first error again. */
2515 if (parmp->edit_type == EDIT_QF)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002516 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002517#endif
2518 TIME_MSG("executing command arguments");
2519}
2520
2521/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002522 * Source startup scripts.
2523 */
2524 static void
2525source_startup_scripts(parmp)
2526 mparm_T *parmp;
2527{
2528 int i;
2529
2530 /*
2531 * For "evim" source evim.vim first of all, so that the user can overrule
2532 * any things he doesn't like.
2533 */
2534 if (parmp->evim_mode)
2535 {
2536 (void)do_source((char_u *)EVIM_FILE, FALSE, FALSE);
2537 TIME_MSG("source evim file");
2538 }
2539
2540 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002541 * If -u argument given, use only the initializations from that file and
Bram Moolenaar58d98232005-07-23 22:25:46 +00002542 * nothing else.
2543 */
2544 if (parmp->use_vimrc != NULL)
2545 {
Bram Moolenaar231334e2005-07-25 20:46:57 +00002546 if (STRCMP(parmp->use_vimrc, "NONE") == 0
2547 || STRCMP(parmp->use_vimrc, "NORC") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002548 {
2549#ifdef FEAT_GUI
2550 if (use_gvimrc == NULL) /* don't load gvimrc either */
2551 use_gvimrc = parmp->use_vimrc;
2552#endif
2553 if (parmp->use_vimrc[2] == 'N')
2554 p_lpl = FALSE; /* don't load plugins either */
2555 }
2556 else
2557 {
2558 if (do_source(parmp->use_vimrc, FALSE, FALSE) != OK)
2559 EMSG2(_("E282: Cannot read from \"%s\""), parmp->use_vimrc);
2560 }
2561 }
2562 else if (!silent_mode)
2563 {
2564#ifdef AMIGA
2565 struct Process *proc = (struct Process *)FindTask(0L);
2566 APTR save_winptr = proc->pr_WindowPtr;
2567
2568 /* Avoid a requester here for a volume that doesn't exist. */
2569 proc->pr_WindowPtr = (APTR)-1L;
2570#endif
2571
2572 /*
2573 * Get system wide defaults, if the file name is defined.
2574 */
2575#ifdef SYS_VIMRC_FILE
2576 (void)do_source((char_u *)SYS_VIMRC_FILE, FALSE, FALSE);
2577#endif
2578
2579 /*
2580 * Try to read initialization commands from the following places:
2581 * - environment variable VIMINIT
2582 * - user vimrc file (s:.vimrc for Amiga, ~/.vimrc otherwise)
2583 * - second user vimrc file ($VIM/.vimrc for Dos)
2584 * - environment variable EXINIT
2585 * - user exrc file (s:.exrc for Amiga, ~/.exrc otherwise)
2586 * - second user exrc file ($VIM/.exrc for Dos)
2587 * The first that exists is used, the rest is ignored.
2588 */
2589 if (process_env((char_u *)"VIMINIT", TRUE) != OK)
2590 {
2591 if (do_source((char_u *)USR_VIMRC_FILE, TRUE, TRUE) == FAIL
2592#ifdef USR_VIMRC_FILE2
2593 && do_source((char_u *)USR_VIMRC_FILE2, TRUE, TRUE) == FAIL
2594#endif
2595#ifdef USR_VIMRC_FILE3
2596 && do_source((char_u *)USR_VIMRC_FILE3, TRUE, TRUE) == FAIL
2597#endif
2598 && process_env((char_u *)"EXINIT", FALSE) == FAIL
2599 && do_source((char_u *)USR_EXRC_FILE, FALSE, FALSE) == FAIL)
2600 {
2601#ifdef USR_EXRC_FILE2
2602 (void)do_source((char_u *)USR_EXRC_FILE2, FALSE, FALSE);
2603#endif
2604 }
2605 }
2606
2607 /*
2608 * Read initialization commands from ".vimrc" or ".exrc" in current
2609 * directory. This is only done if the 'exrc' option is set.
2610 * Because of security reasons we disallow shell and write commands
2611 * now, except for unix if the file is owned by the user or 'secure'
2612 * option has been reset in environment of global ".exrc" or ".vimrc".
2613 * Only do this if VIMRC_FILE is not the same as USR_VIMRC_FILE or
2614 * SYS_VIMRC_FILE.
2615 */
2616 if (p_exrc)
2617 {
2618#if defined(UNIX) || defined(VMS)
2619 /* If ".vimrc" file is not owned by user, set 'secure' mode. */
2620 if (!file_owned(VIMRC_FILE))
2621#endif
2622 secure = p_secure;
2623
2624 i = FAIL;
2625 if (fullpathcmp((char_u *)USR_VIMRC_FILE,
2626 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
2627#ifdef USR_VIMRC_FILE2
2628 && fullpathcmp((char_u *)USR_VIMRC_FILE2,
2629 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
2630#endif
2631#ifdef USR_VIMRC_FILE3
2632 && fullpathcmp((char_u *)USR_VIMRC_FILE3,
2633 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
2634#endif
2635#ifdef SYS_VIMRC_FILE
2636 && fullpathcmp((char_u *)SYS_VIMRC_FILE,
2637 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
2638#endif
2639 )
2640 i = do_source((char_u *)VIMRC_FILE, TRUE, TRUE);
2641
2642 if (i == FAIL)
2643 {
2644#if defined(UNIX) || defined(VMS)
2645 /* if ".exrc" is not owned by user set 'secure' mode */
2646 if (!file_owned(EXRC_FILE))
2647 secure = p_secure;
2648 else
2649 secure = 0;
2650#endif
2651 if ( fullpathcmp((char_u *)USR_EXRC_FILE,
2652 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
2653#ifdef USR_EXRC_FILE2
2654 && fullpathcmp((char_u *)USR_EXRC_FILE2,
2655 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
2656#endif
2657 )
2658 (void)do_source((char_u *)EXRC_FILE, FALSE, FALSE);
2659 }
2660 }
2661 if (secure == 2)
2662 need_wait_return = TRUE;
2663 secure = 0;
2664#ifdef AMIGA
2665 proc->pr_WindowPtr = save_winptr;
2666#endif
2667 }
2668 TIME_MSG("sourcing vimrc file(s)");
2669}
2670
2671/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002672 * Setup to start using the GUI. Exit with an error when not available.
2673 */
2674 static void
2675main_start_gui()
2676{
2677#ifdef FEAT_GUI
2678 gui.starting = TRUE; /* start GUI a bit later */
2679#else
2680 mch_errmsg(_(e_nogvim));
2681 mch_errmsg("\n");
2682 mch_exit(2);
2683#endif
2684}
2685
2686/*
2687 * Get an evironment variable, and execute it as Ex commands.
2688 * Returns FAIL if the environment variable was not executed, OK otherwise.
2689 */
2690 int
2691process_env(env, is_viminit)
2692 char_u *env;
2693 int is_viminit; /* when TRUE, called for VIMINIT */
2694{
2695 char_u *initstr;
2696 char_u *save_sourcing_name;
2697 linenr_T save_sourcing_lnum;
2698#ifdef FEAT_EVAL
2699 scid_T save_sid;
2700#endif
2701
2702 if ((initstr = mch_getenv(env)) != NULL && *initstr != NUL)
2703 {
2704 if (is_viminit)
2705 vimrc_found();
2706 save_sourcing_name = sourcing_name;
2707 save_sourcing_lnum = sourcing_lnum;
2708 sourcing_name = env;
2709 sourcing_lnum = 0;
2710#ifdef FEAT_EVAL
2711 save_sid = current_SID;
2712 current_SID = SID_ENV;
2713#endif
2714 do_cmdline_cmd(initstr);
2715 sourcing_name = save_sourcing_name;
2716 sourcing_lnum = save_sourcing_lnum;
2717#ifdef FEAT_EVAL
2718 current_SID = save_sid;;
2719#endif
2720 return OK;
2721 }
2722 return FAIL;
2723}
2724
2725#if defined(UNIX) || defined(VMS)
2726/*
2727 * Return TRUE if we are certain the user owns the file "fname".
2728 * Used for ".vimrc" and ".exrc".
2729 * Use both stat() and lstat() for extra security.
2730 */
2731 static int
2732file_owned(fname)
2733 char *fname;
2734{
2735 struct stat s;
2736# ifdef UNIX
2737 uid_t uid = getuid();
2738# else /* VMS */
2739 uid_t uid = ((getgid() << 16) | getuid());
2740# endif
2741
2742 return !(mch_stat(fname, &s) != 0 || s.st_uid != uid
2743# ifdef HAVE_LSTAT
2744 || mch_lstat(fname, &s) != 0 || s.st_uid != uid
2745# endif
2746 );
2747}
2748#endif
2749
2750/*
2751 * Give an error message main_errors["n"] and exit.
2752 */
2753 static void
2754mainerr(n, str)
2755 int n; /* one of the ME_ defines */
2756 char_u *str; /* extra argument or NULL */
2757{
2758#if defined(UNIX) || defined(__EMX__) || defined(VMS)
2759 reset_signals(); /* kill us with CTRL-C here, if you like */
2760#endif
2761
2762 mch_errmsg(longVersion);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002763 mch_errmsg("\n");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002764 mch_errmsg(_(main_errors[n]));
2765 if (str != NULL)
2766 {
2767 mch_errmsg(": \"");
2768 mch_errmsg((char *)str);
2769 mch_errmsg("\"");
2770 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002771 mch_errmsg(_("\nMore info with: \"vim -h\"\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002772
2773 mch_exit(1);
2774}
2775
2776 void
2777mainerr_arg_missing(str)
2778 char_u *str;
2779{
2780 mainerr(ME_ARG_MISSING, str);
2781}
2782
2783/*
2784 * print a message with three spaces prepended and '\n' appended.
2785 */
2786 static void
2787main_msg(s)
2788 char *s;
2789{
2790 mch_msg(" ");
2791 mch_msg(s);
2792 mch_msg("\n");
2793}
2794
2795/*
2796 * Print messages for "vim -h" or "vim --help" and exit.
2797 */
2798 static void
2799usage()
2800{
2801 int i;
2802 static char *(use[]) =
2803 {
2804 N_("[file ..] edit specified file(s)"),
2805 N_("- read text from stdin"),
2806 N_("-t tag edit file where tag is defined"),
2807#ifdef FEAT_QUICKFIX
2808 N_("-q [errorfile] edit file with first error")
2809#endif
2810 };
2811
2812#if defined(UNIX) || defined(__EMX__) || defined(VMS)
2813 reset_signals(); /* kill us with CTRL-C here, if you like */
2814#endif
2815
2816 mch_msg(longVersion);
2817 mch_msg(_("\n\nusage:"));
2818 for (i = 0; ; ++i)
2819 {
2820 mch_msg(_(" vim [arguments] "));
2821 mch_msg(_(use[i]));
2822 if (i == (sizeof(use) / sizeof(char_u *)) - 1)
2823 break;
2824 mch_msg(_("\n or:"));
2825 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002826#ifdef VMS
2827 mch_msg(_("where case is ignored prepend / to make flag upper case"));
2828#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002829
2830 mch_msg(_("\n\nArguments:\n"));
2831 main_msg(_("--\t\t\tOnly file names after this"));
2832#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
2833 main_msg(_("--literal\t\tDon't expand wildcards"));
2834#endif
2835#ifdef FEAT_OLE
2836 main_msg(_("-register\t\tRegister this gvim for OLE"));
2837 main_msg(_("-unregister\t\tUnregister gvim for OLE"));
2838#endif
2839#ifdef FEAT_GUI
2840 main_msg(_("-g\t\t\tRun using GUI (like \"gvim\")"));
2841 main_msg(_("-f or --nofork\tForeground: Don't fork when starting GUI"));
2842#endif
2843 main_msg(_("-v\t\t\tVi mode (like \"vi\")"));
2844 main_msg(_("-e\t\t\tEx mode (like \"ex\")"));
2845 main_msg(_("-s\t\t\tSilent (batch) mode (only for \"ex\")"));
2846#ifdef FEAT_DIFF
2847 main_msg(_("-d\t\t\tDiff mode (like \"vimdiff\")"));
2848#endif
2849 main_msg(_("-y\t\t\tEasy mode (like \"evim\", modeless)"));
2850 main_msg(_("-R\t\t\tReadonly mode (like \"view\")"));
2851 main_msg(_("-Z\t\t\tRestricted mode (like \"rvim\")"));
2852 main_msg(_("-m\t\t\tModifications (writing files) not allowed"));
2853 main_msg(_("-M\t\t\tModifications in text not allowed"));
2854 main_msg(_("-b\t\t\tBinary mode"));
2855#ifdef FEAT_LISP
2856 main_msg(_("-l\t\t\tLisp mode"));
2857#endif
2858 main_msg(_("-C\t\t\tCompatible with Vi: 'compatible'"));
2859 main_msg(_("-N\t\t\tNot fully Vi compatible: 'nocompatible'"));
2860 main_msg(_("-V[N]\t\tVerbose level"));
2861 main_msg(_("-D\t\t\tDebugging mode"));
2862 main_msg(_("-n\t\t\tNo swap file, use memory only"));
2863 main_msg(_("-r\t\t\tList swap files and exit"));
2864 main_msg(_("-r (with file name)\tRecover crashed session"));
2865 main_msg(_("-L\t\t\tSame as -r"));
2866#ifdef AMIGA
2867 main_msg(_("-f\t\t\tDon't use newcli to open window"));
2868 main_msg(_("-dev <device>\t\tUse <device> for I/O"));
2869#endif
2870#ifdef FEAT_ARABIC
2871 main_msg(_("-A\t\t\tstart in Arabic mode"));
2872#endif
2873#ifdef FEAT_RIGHTLEFT
2874 main_msg(_("-H\t\t\tStart in Hebrew mode"));
2875#endif
2876#ifdef FEAT_FKMAP
2877 main_msg(_("-F\t\t\tStart in Farsi mode"));
2878#endif
2879 main_msg(_("-T <terminal>\tSet terminal type to <terminal>"));
2880 main_msg(_("-u <vimrc>\t\tUse <vimrc> instead of any .vimrc"));
2881#ifdef FEAT_GUI
2882 main_msg(_("-U <gvimrc>\t\tUse <gvimrc> instead of any .gvimrc"));
2883#endif
2884 main_msg(_("--noplugin\t\tDon't load plugin scripts"));
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002885 main_msg(_("-p[N]\t\tOpen N tab pages (default: one for each file)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002886 main_msg(_("-o[N]\t\tOpen N windows (default: one for each file)"));
2887 main_msg(_("-O[N]\t\tLike -o but split vertically"));
2888 main_msg(_("+\t\t\tStart at end of file"));
2889 main_msg(_("+<lnum>\t\tStart at line <lnum>"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002890 main_msg(_("--cmd <command>\tExecute <command> before loading any vimrc file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002891 main_msg(_("-c <command>\t\tExecute <command> after loading the first file"));
2892 main_msg(_("-S <session>\t\tSource file <session> after loading the first file"));
2893 main_msg(_("-s <scriptin>\tRead Normal mode commands from file <scriptin>"));
2894 main_msg(_("-w <scriptout>\tAppend all typed commands to file <scriptout>"));
2895 main_msg(_("-W <scriptout>\tWrite all typed commands to file <scriptout>"));
2896#ifdef FEAT_CRYPT
2897 main_msg(_("-x\t\t\tEdit encrypted files"));
2898#endif
2899#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
2900# if defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)
2901 main_msg(_("-display <display>\tConnect vim to this particular X-server"));
2902# endif
2903 main_msg(_("-X\t\t\tDo not connect to X server"));
2904#endif
2905#ifdef FEAT_CLIENTSERVER
2906 main_msg(_("--remote <files>\tEdit <files> in a Vim server if possible"));
2907 main_msg(_("--remote-silent <files> Same, don't complain if there is no server"));
2908 main_msg(_("--remote-wait <files> As --remote but wait for files to have been edited"));
2909 main_msg(_("--remote-wait-silent <files> Same, don't complain if there is no server"));
2910 main_msg(_("--remote-send <keys>\tSend <keys> to a Vim server and exit"));
2911 main_msg(_("--remote-expr <expr>\tEvaluate <expr> in a Vim server and print result"));
2912 main_msg(_("--serverlist\t\tList available Vim server names and exit"));
2913 main_msg(_("--servername <name>\tSend to/become the Vim server <name>"));
2914#endif
2915#ifdef FEAT_VIMINFO
2916 main_msg(_("-i <viminfo>\t\tUse <viminfo> instead of .viminfo"));
2917#endif
2918 main_msg(_("-h or --help\tPrint Help (this message) and exit"));
2919 main_msg(_("--version\t\tPrint version information and exit"));
2920
2921#ifdef FEAT_GUI_X11
2922# ifdef FEAT_GUI_MOTIF
2923 mch_msg(_("\nArguments recognised by gvim (Motif version):\n"));
2924# else
2925# ifdef FEAT_GUI_ATHENA
2926# ifdef FEAT_GUI_NEXTAW
2927 mch_msg(_("\nArguments recognised by gvim (neXtaw version):\n"));
2928# else
2929 mch_msg(_("\nArguments recognised by gvim (Athena version):\n"));
2930# endif
2931# endif
2932# endif
2933 main_msg(_("-display <display>\tRun vim on <display>"));
2934 main_msg(_("-iconic\t\tStart vim iconified"));
2935# if 0
2936 main_msg(_("-name <name>\t\tUse resource as if vim was <name>"));
2937 mch_msg(_("\t\t\t (Unimplemented)\n"));
2938# endif
2939 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
2940 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
2941 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
2942 main_msg(_("-boldfont <font>\tUse <font> for bold text"));
2943 main_msg(_("-italicfont <font>\tUse <font> for italic text"));
2944 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
2945 main_msg(_("-borderwidth <width>\tUse a border width of <width> (also: -bw)"));
2946 main_msg(_("-scrollbarwidth <width> Use a scrollbar width of <width> (also: -sw)"));
2947# ifdef FEAT_GUI_ATHENA
2948 main_msg(_("-menuheight <height>\tUse a menu bar height of <height> (also: -mh)"));
2949# endif
2950 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
2951 main_msg(_("+reverse\t\tDon't use reverse video (also: +rv)"));
2952 main_msg(_("-xrm <resource>\tSet the specified resource"));
2953#endif /* FEAT_GUI_X11 */
2954#if defined(FEAT_GUI) && defined(RISCOS)
2955 mch_msg(_("\nArguments recognised by gvim (RISC OS version):\n"));
2956 main_msg(_("--columns <number>\tInitial width of window in columns"));
2957 main_msg(_("--rows <number>\tInitial height of window in rows"));
2958#endif
2959#ifdef FEAT_GUI_GTK
2960 mch_msg(_("\nArguments recognised by gvim (GTK+ version):\n"));
2961 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
2962 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
2963 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
2964 main_msg(_("-display <display>\tRun vim on <display> (also: --display)"));
2965# ifdef HAVE_GTK2
2966 main_msg(_("--role <role>\tSet a unique role to identify the main window"));
2967# endif
2968 main_msg(_("--socketid <xid>\tOpen Vim inside another GTK widget"));
2969#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002970#ifdef FEAT_GUI_W32
2971 main_msg(_("-P <parent title>\tOpen Vim inside parent application"));
2972#endif
2973
2974#ifdef FEAT_GUI_GNOME
2975 /* Gnome gives extra messages for --help if we continue, but not for -h. */
2976 if (gui.starting)
2977 mch_msg("\n");
2978 else
2979#endif
2980 mch_exit(0);
2981}
2982
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002983#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002984/*
2985 * Check the result of the ATTENTION dialog:
2986 * When "Quit" selected, exit Vim.
2987 * When "Recover" selected, recover the file.
2988 */
2989 static void
2990check_swap_exists_action()
2991{
2992 if (swap_exists_action == SEA_QUIT)
2993 getout(1);
2994 handle_swap_exists(NULL);
2995}
2996#endif
2997
2998#if defined(STARTUPTIME) || defined(PROTO)
2999static void time_diff __ARGS((struct timeval *then, struct timeval *now));
3000
3001static struct timeval prev_timeval;
3002
3003/*
3004 * Save the previous time before doing something that could nest.
3005 * set "*tv_rel" to the time elapsed so far.
3006 */
3007 void
3008time_push(tv_rel, tv_start)
3009 void *tv_rel, *tv_start;
3010{
3011 *((struct timeval *)tv_rel) = prev_timeval;
3012 gettimeofday(&prev_timeval, NULL);
3013 ((struct timeval *)tv_rel)->tv_usec = prev_timeval.tv_usec
3014 - ((struct timeval *)tv_rel)->tv_usec;
3015 ((struct timeval *)tv_rel)->tv_sec = prev_timeval.tv_sec
3016 - ((struct timeval *)tv_rel)->tv_sec;
3017 if (((struct timeval *)tv_rel)->tv_usec < 0)
3018 {
3019 ((struct timeval *)tv_rel)->tv_usec += 1000000;
3020 --((struct timeval *)tv_rel)->tv_sec;
3021 }
3022 *(struct timeval *)tv_start = prev_timeval;
3023}
3024
3025/*
3026 * Compute the previous time after doing something that could nest.
3027 * Subtract "*tp" from prev_timeval;
3028 * Note: The arguments are (void *) to avoid trouble with systems that don't
3029 * have struct timeval.
3030 */
3031 void
3032time_pop(tp)
3033 void *tp; /* actually (struct timeval *) */
3034{
3035 prev_timeval.tv_usec -= ((struct timeval *)tp)->tv_usec;
3036 prev_timeval.tv_sec -= ((struct timeval *)tp)->tv_sec;
3037 if (prev_timeval.tv_usec < 0)
3038 {
3039 prev_timeval.tv_usec += 1000000;
3040 --prev_timeval.tv_sec;
3041 }
3042}
3043
3044 static void
3045time_diff(then, now)
3046 struct timeval *then;
3047 struct timeval *now;
3048{
3049 long usec;
3050 long msec;
3051
3052 usec = now->tv_usec - then->tv_usec;
3053 msec = (now->tv_sec - then->tv_sec) * 1000L + usec / 1000L,
3054 usec = usec % 1000L;
3055 fprintf(time_fd, "%03ld.%03ld", msec, usec >= 0 ? usec : usec + 1000L);
3056}
3057
3058 void
3059time_msg(msg, tv_start)
3060 char *msg;
3061 void *tv_start; /* only for do_source: start time; actually
3062 (struct timeval *) */
3063{
3064 static struct timeval start;
3065 struct timeval now;
3066
3067 if (time_fd != NULL)
3068 {
3069 if (strstr(msg, "STARTING") != NULL)
3070 {
3071 gettimeofday(&start, NULL);
3072 prev_timeval = start;
3073 fprintf(time_fd, "\n\ntimes in msec\n");
3074 fprintf(time_fd, " clock self+sourced self: sourced script\n");
3075 fprintf(time_fd, " clock elapsed: other lines\n\n");
3076 }
3077 gettimeofday(&now, NULL);
3078 time_diff(&start, &now);
3079 if (((struct timeval *)tv_start) != NULL)
3080 {
3081 fprintf(time_fd, " ");
3082 time_diff(((struct timeval *)tv_start), &now);
3083 }
3084 fprintf(time_fd, " ");
3085 time_diff(&prev_timeval, &now);
3086 prev_timeval = now;
3087 fprintf(time_fd, ": %s\n", msg);
3088 }
3089}
3090
3091# ifdef WIN3264
3092/*
3093 * Windows doesn't have gettimeofday(), although it does have struct timeval.
3094 */
3095 int
3096gettimeofday(struct timeval *tv, char *dummy)
3097{
3098 long t = clock();
3099 tv->tv_sec = t / CLOCKS_PER_SEC;
3100 tv->tv_usec = (t - tv->tv_sec * CLOCKS_PER_SEC) * 1000000 / CLOCKS_PER_SEC;
3101 return 0;
3102}
3103# endif
3104
3105#endif
3106
3107#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
3108
3109/*
3110 * Common code for the X command server and the Win32 command server.
3111 */
3112
3113static char_u *build_drop_cmd __ARGS((int filec, char **filev, int sendReply));
3114
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003115/*
3116 * Do the client-server stuff, unless "--servername ''" was used.
3117 */
3118 static void
3119exec_on_server(parmp)
3120 mparm_T *parmp;
3121{
3122 if (parmp->serverName_arg == NULL || *parmp->serverName_arg != NUL)
3123 {
3124# ifdef WIN32
3125 /* Initialise the client/server messaging infrastructure. */
3126 serverInitMessaging();
3127# endif
3128
3129 /*
3130 * When a command server argument was found, execute it. This may
3131 * exit Vim when it was successful. Otherwise it's executed further
3132 * on. Remember the encoding used here in "serverStrEnc".
3133 */
3134 if (parmp->serverArg)
3135 {
3136 cmdsrv_main(&parmp->argc, parmp->argv,
3137 parmp->serverName_arg, &parmp->serverStr);
3138# ifdef FEAT_MBYTE
3139 parmp->serverStrEnc = vim_strsave(p_enc);
3140# endif
3141 }
3142
3143 /* If we're still running, get the name to register ourselves.
3144 * On Win32 can register right now, for X11 need to setup the
3145 * clipboard first, it's further down. */
3146 parmp->servername = serverMakeName(parmp->serverName_arg,
3147 parmp->argv[0]);
3148# ifdef WIN32
3149 if (parmp->servername != NULL)
3150 {
3151 serverSetName(parmp->servername);
3152 vim_free(parmp->servername);
3153 }
3154# endif
3155 }
3156}
3157
3158/*
3159 * Prepare for running as a Vim server.
3160 */
3161 static void
3162prepare_server(parmp)
3163 mparm_T *parmp;
3164{
3165# if defined(FEAT_X11)
3166 /*
3167 * Register for remote command execution with :serversend and --remote
3168 * unless there was a -X or a --servername '' on the command line.
3169 * Only register nongui-vim's with an explicit --servername argument.
3170 */
3171 if (X_DISPLAY != NULL && parmp->servername != NULL && (
3172# ifdef FEAT_GUI
3173 gui.in_use ||
3174# endif
3175 parmp->serverName_arg != NULL))
3176 {
3177 (void)serverRegisterName(X_DISPLAY, parmp->servername);
3178 vim_free(parmp->servername);
3179 TIME_MSG("register server name");
3180 }
3181 else
3182 serverDelayedStartName = parmp->servername;
3183# endif
3184
3185 /*
3186 * Execute command ourselves if we're here because the send failed (or
3187 * else we would have exited above).
3188 */
3189 if (parmp->serverStr != NULL)
3190 {
3191 char_u *p;
3192
3193 server_to_input_buf(serverConvert(parmp->serverStrEnc,
3194 parmp->serverStr, &p));
3195 vim_free(p);
3196 }
3197}
3198
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003199 static void
3200cmdsrv_main(argc, argv, serverName_arg, serverStr)
3201 int *argc;
3202 char **argv;
3203 char_u *serverName_arg;
3204 char_u **serverStr;
3205{
3206 char_u *res;
3207 int i;
3208 char_u *sname;
3209 int ret;
3210 int didone = FALSE;
3211 int exiterr = 0;
3212 char **newArgV = argv + 1;
3213 int newArgC = 1,
3214 Argc = *argc;
3215 int argtype;
3216#define ARGTYPE_OTHER 0
3217#define ARGTYPE_EDIT 1
3218#define ARGTYPE_EDIT_WAIT 2
3219#define ARGTYPE_SEND 3
3220 int silent = FALSE;
3221# ifndef FEAT_X11
3222 HWND srv;
3223# else
3224 Window srv;
3225
3226 setup_term_clip();
3227# endif
3228
3229 sname = serverMakeName(serverName_arg, argv[0]);
3230 if (sname == NULL)
3231 return;
3232
3233 /*
3234 * Execute the command server related arguments and remove them
3235 * from the argc/argv array; We may have to return into main()
3236 */
3237 for (i = 1; i < Argc; i++)
3238 {
3239 res = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003240 if (STRCMP(argv[i], "--") == 0) /* end of option arguments */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003241 {
3242 for (; i < *argc; i++)
3243 {
3244 *newArgV++ = argv[i];
3245 newArgC++;
3246 }
3247 break;
3248 }
3249
3250 if (STRICMP(argv[i], "--remote") == 0)
3251 argtype = ARGTYPE_EDIT;
3252 else if (STRICMP(argv[i], "--remote-silent") == 0)
3253 {
3254 argtype = ARGTYPE_EDIT;
3255 silent = TRUE;
3256 }
3257 else if (STRICMP(argv[i], "--remote-wait") == 0)
3258 argtype = ARGTYPE_EDIT_WAIT;
3259 else if (STRICMP(argv[i], "--remote-wait-silent") == 0)
3260 {
3261 argtype = ARGTYPE_EDIT_WAIT;
3262 silent = TRUE;
3263 }
3264 else if (STRICMP(argv[i], "--remote-send") == 0)
3265 argtype = ARGTYPE_SEND;
3266 else
3267 argtype = ARGTYPE_OTHER;
3268 if (argtype != ARGTYPE_OTHER)
3269 {
3270 if (i == *argc - 1)
3271 mainerr_arg_missing((char_u *)argv[i]);
3272 if (argtype == ARGTYPE_SEND)
3273 {
3274 *serverStr = (char_u *)argv[i + 1];
3275 i++;
3276 }
3277 else
3278 {
3279 *serverStr = build_drop_cmd(*argc - i - 1, argv + i + 1,
3280 argtype == ARGTYPE_EDIT_WAIT);
3281 if (*serverStr == NULL)
3282 {
3283 /* Probably out of memory, exit. */
3284 didone = TRUE;
3285 exiterr = 1;
3286 break;
3287 }
3288 Argc = i;
3289 }
3290# ifdef FEAT_X11
3291 if (xterm_dpy == NULL)
3292 {
3293 mch_errmsg(_("No display"));
3294 ret = -1;
3295 }
3296 else
3297 ret = serverSendToVim(xterm_dpy, sname, *serverStr,
3298 NULL, &srv, 0, 0, silent);
3299# else
3300 /* Win32 always works? */
3301 ret = serverSendToVim(sname, *serverStr, NULL, &srv, 0, silent);
3302# endif
3303 if (ret < 0)
3304 {
3305 if (argtype == ARGTYPE_SEND)
3306 {
3307 /* Failed to send, abort. */
3308 mch_errmsg(_(": Send failed.\n"));
3309 didone = TRUE;
3310 exiterr = 1;
3311 }
3312 else if (!silent)
3313 /* Let vim start normally. */
3314 mch_errmsg(_(": Send failed. Trying to execute locally\n"));
3315 break;
3316 }
3317
3318# ifdef FEAT_GUI_W32
3319 /* Guess that when the server name starts with "g" it's a GUI
3320 * server, which we can bring to the foreground here.
3321 * Foreground() in the server doesn't work very well. */
3322 if (argtype != ARGTYPE_SEND && TOUPPER_ASC(*sname) == 'G')
3323 SetForegroundWindow(srv);
3324# endif
3325
3326 /*
3327 * For --remote-wait: Wait until the server did edit each
3328 * file. Also detect that the server no longer runs.
3329 */
3330 if (ret >= 0 && argtype == ARGTYPE_EDIT_WAIT)
3331 {
3332 int numFiles = *argc - i - 1;
3333 int j;
3334 char_u *done = alloc(numFiles);
3335 char_u *p;
3336# ifdef FEAT_GUI_W32
3337 NOTIFYICONDATA ni;
3338 int count = 0;
3339 extern HWND message_window;
3340# endif
3341
3342 if (numFiles > 0 && argv[i + 1][0] == '+')
3343 /* Skip "+cmd" argument, don't wait for it to be edited. */
3344 --numFiles;
3345
3346# ifdef FEAT_GUI_W32
3347 ni.cbSize = sizeof(ni);
3348 ni.hWnd = message_window;
3349 ni.uID = 0;
3350 ni.uFlags = NIF_ICON|NIF_TIP;
3351 ni.hIcon = LoadIcon((HINSTANCE)GetModuleHandle(0), "IDR_VIM");
3352 sprintf(ni.szTip, _("%d of %d edited"), count, numFiles);
3353 Shell_NotifyIcon(NIM_ADD, &ni);
3354# endif
3355
3356 /* Wait for all files to unload in remote */
3357 memset(done, 0, numFiles);
3358 while (memchr(done, 0, numFiles) != NULL)
3359 {
3360# ifdef WIN32
3361 p = serverGetReply(srv, NULL, TRUE, TRUE);
3362 if (p == NULL)
3363 break;
3364# else
3365 if (serverReadReply(xterm_dpy, srv, &p, TRUE) < 0)
3366 break;
3367# endif
3368 j = atoi((char *)p);
3369 if (j >= 0 && j < numFiles)
3370 {
3371# ifdef FEAT_GUI_W32
3372 ++count;
3373 sprintf(ni.szTip, _("%d of %d edited"),
3374 count, numFiles);
3375 Shell_NotifyIcon(NIM_MODIFY, &ni);
3376# endif
3377 done[j] = 1;
3378 }
3379 }
3380# ifdef FEAT_GUI_W32
3381 Shell_NotifyIcon(NIM_DELETE, &ni);
3382# endif
3383 }
3384 }
3385 else if (STRICMP(argv[i], "--remote-expr") == 0)
3386 {
3387 if (i == *argc - 1)
3388 mainerr_arg_missing((char_u *)argv[i]);
3389# ifdef WIN32
3390 /* Win32 always works? */
3391 if (serverSendToVim(sname, (char_u *)argv[i + 1],
3392 &res, NULL, 1, FALSE) < 0)
3393# else
3394 if (xterm_dpy == NULL)
3395 mch_errmsg(_("No display: Send expression failed.\n"));
3396 else if (serverSendToVim(xterm_dpy, sname, (char_u *)argv[i + 1],
3397 &res, NULL, 1, 1, FALSE) < 0)
3398# endif
3399 {
3400 if (res != NULL && *res != NUL)
3401 {
3402 /* Output error from remote */
3403 mch_errmsg((char *)res);
3404 vim_free(res);
3405 res = NULL;
3406 }
3407 mch_errmsg(_(": Send expression failed.\n"));
3408 }
3409 }
3410 else if (STRICMP(argv[i], "--serverlist") == 0)
3411 {
3412# ifdef WIN32
3413 /* Win32 always works? */
3414 res = serverGetVimNames();
3415# else
3416 if (xterm_dpy != NULL)
3417 res = serverGetVimNames(xterm_dpy);
3418# endif
3419 if (called_emsg)
3420 mch_errmsg("\n");
3421 }
3422 else if (STRICMP(argv[i], "--servername") == 0)
3423 {
3424 /* Alredy processed. Take it out of the command line */
3425 i++;
3426 continue;
3427 }
3428 else
3429 {
3430 *newArgV++ = argv[i];
3431 newArgC++;
3432 continue;
3433 }
3434 didone = TRUE;
3435 if (res != NULL && *res != NUL)
3436 {
3437 mch_msg((char *)res);
3438 if (res[STRLEN(res) - 1] != '\n')
3439 mch_msg("\n");
3440 }
3441 vim_free(res);
3442 }
3443
3444 if (didone)
3445 {
3446 display_errors(); /* display any collected messages */
3447 exit(exiterr); /* Mission accomplished - get out */
3448 }
3449
3450 /* Return back into main() */
3451 *argc = newArgC;
3452 vim_free(sname);
3453}
3454
3455/*
3456 * Build a ":drop" command to send to a Vim server.
3457 */
3458 static char_u *
3459build_drop_cmd(filec, filev, sendReply)
3460 int filec;
3461 char **filev;
3462 int sendReply;
3463{
3464 garray_T ga;
3465 int i;
3466 char_u *inicmd = NULL;
3467 char_u *p;
3468 char_u cwd[MAXPATHL];
3469
3470 if (filec > 0 && filev[0][0] == '+')
3471 {
3472 inicmd = (char_u *)filev[0] + 1;
3473 filev++;
3474 filec--;
3475 }
3476 /* Check if we have at least one argument. */
3477 if (filec <= 0)
3478 mainerr_arg_missing((char_u *)filev[-1]);
3479 if (mch_dirname(cwd, MAXPATHL) != OK)
3480 return NULL;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003481 if ((p = vim_strsave_escaped_ext(cwd, PATH_ESC_CHARS, '\\', TRUE)) == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003482 return NULL;
3483 ga_init2(&ga, 1, 100);
3484 ga_concat(&ga, (char_u *)"<C-\\><C-N>:cd ");
3485 ga_concat(&ga, p);
3486 /* Call inputsave() so that a prompt for an encryption key works. */
3487 ga_concat(&ga, (char_u *)"<CR>:if exists('*inputsave')|call inputsave()|endif|drop");
3488 vim_free(p);
3489 for (i = 0; i < filec; i++)
3490 {
3491 /* On Unix the shell has already expanded the wildcards, don't want to
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003492 * do it again in the Vim server. On MS-Windows only escape
3493 * non-wildcard characters. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003494 p = vim_strsave_escaped((char_u *)filev[i],
3495#ifdef UNIX
3496 PATH_ESC_CHARS
3497#else
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003498 (char_u *)" \t%#"
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003499#endif
3500 );
3501 if (p == NULL)
3502 {
3503 vim_free(ga.ga_data);
3504 return NULL;
3505 }
3506 ga_concat(&ga, (char_u *)" ");
3507 ga_concat(&ga, p);
3508 vim_free(p);
3509 }
3510 /* The :drop commands goes to Insert mode when 'insertmode' is set, use
3511 * CTRL-\ CTRL-N again. */
3512 ga_concat(&ga, (char_u *)"|if exists('*inputrestore')|call inputrestore()|endif<CR>");
3513 ga_concat(&ga, (char_u *)"<C-\\><C-N>:cd -");
3514 if (sendReply)
3515 ga_concat(&ga, (char_u *)"<CR>:call SetupRemoteReplies()");
3516 ga_concat(&ga, (char_u *)"<CR>:");
3517 if (inicmd != NULL)
3518 {
3519 /* Can't use <CR> after "inicmd", because an "startinsert" would cause
3520 * the following commands to be inserted as text. Use a "|",
3521 * hopefully "inicmd" does allow this... */
3522 ga_concat(&ga, inicmd);
3523 ga_concat(&ga, (char_u *)"|");
3524 }
3525 /* Bring the window to the foreground, goto Insert mode when 'im' set and
3526 * clear command line. */
Bram Moolenaar567e4de2004-12-31 21:01:02 +00003527 ga_concat(&ga, (char_u *)"cal foreground()|if &im|star|en|redr|f<CR>");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003528 ga_append(&ga, NUL);
3529 return ga.ga_data;
3530}
3531
3532/*
3533 * Replace termcodes such as <CR> and insert as key presses if there is room.
3534 */
3535 void
3536server_to_input_buf(str)
3537 char_u *str;
3538{
3539 char_u *ptr = NULL;
3540 char_u *cpo_save = p_cpo;
3541
3542 /* Set 'cpoptions' the way we want it.
3543 * B set - backslashes are *not* treated specially
3544 * k set - keycodes are *not* reverse-engineered
3545 * < unset - <Key> sequences *are* interpreted
3546 * The last parameter of replace_termcodes() is TRUE so that the <lt>
3547 * sequence is recognised - needed for a real backslash.
3548 */
3549 p_cpo = (char_u *)"Bk";
3550 str = replace_termcodes((char_u *)str, &ptr, FALSE, TRUE);
3551 p_cpo = cpo_save;
3552
3553 if (*ptr != NUL) /* trailing CTRL-V results in nothing */
3554 {
3555 /*
3556 * Add the string to the input stream.
3557 * Can't use add_to_input_buf() here, we now have K_SPECIAL bytes.
3558 *
3559 * First clear typed characters from the typeahead buffer, there could
3560 * be half a mapping there. Then append to the existing string, so
3561 * that multiple commands from a client are concatenated.
3562 */
3563 if (typebuf.tb_maplen < typebuf.tb_len)
3564 del_typebuf(typebuf.tb_len - typebuf.tb_maplen, typebuf.tb_maplen);
3565 (void)ins_typebuf(str, REMAP_NONE, typebuf.tb_len, TRUE, FALSE);
3566
3567 /* Let input_available() know we inserted text in the typeahead
3568 * buffer. */
3569 received_from_client = TRUE;
3570 }
3571 vim_free((char_u *)ptr);
3572}
3573
3574/*
3575 * Evaluate an expression that the client sent to a string.
3576 * Handles disabling error messages and disables debugging, otherwise Vim
3577 * hangs, waiting for "cont" to be typed.
3578 */
3579 char_u *
3580eval_client_expr_to_string(expr)
3581 char_u *expr;
3582{
3583 char_u *res;
3584 int save_dbl = debug_break_level;
3585 int save_ro = redir_off;
3586
3587 debug_break_level = -1;
3588 redir_off = 0;
3589 ++emsg_skip;
3590
3591 res = eval_to_string(expr, NULL);
3592
3593 debug_break_level = save_dbl;
3594 redir_off = save_ro;
3595 --emsg_skip;
3596
Bram Moolenaar63a121b2005-12-11 21:36:39 +00003597 /* A client can tell us to redraw, but not to display the cursor, so do
3598 * that here. */
3599 setcursor();
3600 out_flush();
3601#ifdef FEAT_GUI
3602 if (gui.in_use)
3603 gui_update_cursor(FALSE, FALSE);
3604#endif
3605
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003606 return res;
3607}
3608
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003609/*
3610 * If conversion is needed, convert "data" from "client_enc" to 'encoding' and
3611 * return an allocated string. Otherwise return "data".
3612 * "*tofree" is set to the result when it needs to be freed later.
3613 */
3614/*ARGSUSED*/
3615 char_u *
3616serverConvert(client_enc, data, tofree)
3617 char_u *client_enc;
3618 char_u *data;
3619 char_u **tofree;
3620{
3621 char_u *res = data;
3622
3623 *tofree = NULL;
3624# ifdef FEAT_MBYTE
3625 if (client_enc != NULL && p_enc != NULL)
3626 {
3627 vimconv_T vimconv;
3628
3629 vimconv.vc_type = CONV_NONE;
3630 if (convert_setup(&vimconv, client_enc, p_enc) != FAIL
3631 && vimconv.vc_type != CONV_NONE)
3632 {
3633 res = string_convert(&vimconv, data, NULL);
3634 if (res == NULL)
3635 res = data;
3636 else
3637 *tofree = res;
3638 }
3639 convert_setup(&vimconv, NULL, NULL);
3640 }
3641# endif
3642 return res;
3643}
3644
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003645
3646/*
3647 * Make our basic server name: use the specified "arg" if given, otherwise use
3648 * the tail of the command "cmd" we were started with.
3649 * Return the name in allocated memory. This doesn't include a serial number.
3650 */
3651 static char_u *
3652serverMakeName(arg, cmd)
3653 char_u *arg;
3654 char *cmd;
3655{
3656 char_u *p;
3657
3658 if (arg != NULL && *arg != NUL)
3659 p = vim_strsave_up(arg);
3660 else
3661 {
3662 p = vim_strsave_up(gettail((char_u *)cmd));
3663 /* Remove .exe or .bat from the name. */
3664 if (p != NULL && vim_strchr(p, '.') != NULL)
3665 *vim_strchr(p, '.') = NUL;
3666 }
3667 return p;
3668}
3669#endif /* FEAT_CLIENTSERVER */
3670
3671/*
3672 * When FEAT_FKMAP is defined, also compile the Farsi source code.
3673 */
3674#if defined(FEAT_FKMAP) || defined(PROTO)
3675# include "farsi.c"
3676#endif
3677
3678/*
3679 * When FEAT_ARABIC is defined, also compile the Arabic source code.
3680 */
3681#if defined(FEAT_ARABIC) || defined(PROTO)
3682# include "arabic.c"
3683#endif