blob: 2fb2edcb59d2a19c449d78bee396251273126b0c [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
Bram Moolenaarb4210b32004-06-13 14:51:16 +000010#define EXTERN
11#include "vim.h"
12
13#ifdef SPAWNO
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +000014# include <spawno.h> /* special MS-DOS swapping library */
Bram Moolenaarb4210b32004-06-13 14:51:16 +000015#endif
16
Bram Moolenaarb4210b32004-06-13 14:51:16 +000017#ifdef __CYGWIN__
18# ifndef WIN32
Bram Moolenaar0d1498e2008-06-29 12:00:49 +000019# include <cygwin/version.h>
20# include <sys/cygwin.h> /* for cygwin_conv_to_posix_path() and/or
21 * cygwin_conv_path() */
Bram Moolenaarb4210b32004-06-13 14:51:16 +000022# endif
23# include <limits.h>
24#endif
25
Bram Moolenaarc013cb62005-07-24 21:18:31 +000026/* Maximum number of commands from + or -c arguments. */
27#define MAX_ARG_CMDS 10
28
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000029/* values for "window_layout" */
30#define WIN_HOR 1 /* "-o" horizontally split windows */
31#define WIN_VER 2 /* "-O" vertically split windows */
32#define WIN_TABS 3 /* "-p" windows on tab pages */
33
Bram Moolenaar58d98232005-07-23 22:25:46 +000034/* Struct for various parameters passed between main() and other functions. */
35typedef struct
36{
Bram Moolenaarc013cb62005-07-24 21:18:31 +000037 int argc;
38 char **argv;
39
40 int evim_mode; /* started as "evim" */
Bram Moolenaarc013cb62005-07-24 21:18:31 +000041 char_u *use_vimrc; /* vimrc from -u argument */
42
43 int n_commands; /* no. of commands from + or -c */
44 char_u *commands[MAX_ARG_CMDS]; /* commands from + or -c arg. */
45 char_u cmds_tofree[MAX_ARG_CMDS]; /* commands that need free() */
46 int n_pre_commands; /* no. of commands from --cmd */
47 char_u *pre_commands[MAX_ARG_CMDS]; /* commands from --cmd argument */
48
49 int edit_type; /* type of editing to do */
50 char_u *tagname; /* tag from -t argument */
51#ifdef FEAT_QUICKFIX
52 char_u *use_ef; /* 'errorfile' from -q argument */
53#endif
54
55 int want_full_screen;
56 int stdout_isatty; /* is stdout a terminal? */
57 char_u *term; /* specified terminal name */
58#ifdef FEAT_CRYPT
59 int ask_for_key; /* -x argument */
60#endif
61 int no_swap_file; /* "-n" argument used */
62#ifdef FEAT_EVAL
63 int use_debug_break_level;
64#endif
65#ifdef FEAT_WINDOWS
66 int window_count; /* number of windows to use */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000067 int window_layout; /* 0, WIN_HOR, WIN_VER or WIN_TABS */
Bram Moolenaarc013cb62005-07-24 21:18:31 +000068#endif
69
70#ifdef FEAT_CLIENTSERVER
Bram Moolenaar58d98232005-07-23 22:25:46 +000071 int serverArg; /* TRUE when argument for a server */
72 char_u *serverName_arg; /* cmdline arg for server name */
Bram Moolenaarc013cb62005-07-24 21:18:31 +000073 char_u *serverStr; /* remote server command */
74 char_u *serverStrEnc; /* encoding of serverStr */
75 char_u *servername; /* allocated name for our server */
76#endif
77#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
78 int literal; /* don't expand file names */
79#endif
80#ifdef MSWIN
81 int full_path; /* file name argument was full path */
82#endif
83#ifdef FEAT_DIFF
84 int diff_mode; /* start with 'diff' set */
85#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +000086} mparm_T;
87
Bram Moolenaarc013cb62005-07-24 21:18:31 +000088/* Values for edit_type. */
89#define EDIT_NONE 0 /* no edit type yet */
90#define EDIT_FILE 1 /* file name argument[s] given, use argument list */
91#define EDIT_STDIN 2 /* read file from stdin */
92#define EDIT_TAG 3 /* tag name argument given, use tagname */
93#define EDIT_QF 4 /* start in quickfix mode */
94
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010095#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
Bram Moolenaarb4210b32004-06-13 14:51:16 +000096static int file_owned __ARGS((char *fname));
97#endif
98static void mainerr __ARGS((int, char_u *));
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010099#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000100static void main_msg __ARGS((char *s));
101static void usage __ARGS((void));
102static int get_number_arg __ARGS((char_u *p, int *idx, int def));
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100103# if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000104static void init_locale __ARGS((void));
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100105# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000106static void parse_command_name __ARGS((mparm_T *parmp));
107static void early_arg_scan __ARGS((mparm_T *parmp));
108static void command_line_scan __ARGS((mparm_T *parmp));
109static void check_tty __ARGS((mparm_T *parmp));
110static void read_stdin __ARGS((void));
111static void create_windows __ARGS((mparm_T *parmp));
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100112# ifdef FEAT_WINDOWS
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000113static void edit_buffers __ARGS((mparm_T *parmp));
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100114# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000115static void exe_pre_commands __ARGS((mparm_T *parmp));
116static void exe_commands __ARGS((mparm_T *parmp));
Bram Moolenaar58d98232005-07-23 22:25:46 +0000117static void source_startup_scripts __ARGS((mparm_T *parmp));
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000118static void main_start_gui __ARGS((void));
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100119# if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000120static void check_swap_exists_action __ARGS((void));
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100121# endif
122# if defined(FEAT_CLIENTSERVER) || defined(PROTO)
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000123static void exec_on_server __ARGS((mparm_T *parmp));
124static void prepare_server __ARGS((mparm_T *parmp));
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000125static void cmdsrv_main __ARGS((int *argc, char **argv, char_u *serverName_arg, char_u **serverStr));
126static char_u *serverMakeName __ARGS((char_u *arg, char *cmd));
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100127# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000128#endif
129
130
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000131/*
132 * Different types of error messages.
133 */
134static char *(main_errors[]) =
135{
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000136 N_("Unknown option argument"),
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000137#define ME_UNKNOWN_OPTION 0
138 N_("Too many edit arguments"),
139#define ME_TOO_MANY_ARGS 1
140 N_("Argument missing after"),
141#define ME_ARG_MISSING 2
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000142 N_("Garbage after option argument"),
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000143#define ME_GARBAGE 3
144 N_("Too many \"+command\", \"-c command\" or \"--cmd command\" arguments"),
145#define ME_EXTRA_CMD 4
146 N_("Invalid argument for"),
147#define ME_INVALID_ARG 5
148};
149
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100150#ifndef PROTO /* don't want a prototype for main() */
Bram Moolenaar8866d272012-11-28 15:55:42 +0100151#ifndef NO_VIM_MAIN /* skip this for unittests */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000152 int
153# ifdef VIMDLL
154_export
155# endif
156# ifdef FEAT_GUI_MSWIN
157# ifdef __BORLANDC__
158_cdecl
159# endif
160VimMain
161# else
162main
163# endif
164(argc, argv)
165 int argc;
166 char **argv;
167{
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000168 char_u *fname = NULL; /* file name from command line */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000169 mparm_T params; /* various parameters passed between
170 * main() and other functions. */
Bram Moolenaar3f269672009-11-03 11:11:11 +0000171#ifdef STARTUPTIME
172 int i;
173#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000174
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000175 /*
176 * Do any system-specific initialisations. These can NOT use IObuff or
177 * NameBuff. Thus emsg2() cannot be called!
178 */
179 mch_early_init();
180
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000181 /* Many variables are in "params" so that we can pass them to invoked
182 * functions without a lot of arguments. "argc" and "argv" are also
183 * copied, so that they can be changed. */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000184 vim_memset(&params, 0, sizeof(params));
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000185 params.argc = argc;
186 params.argv = argv;
187 params.want_full_screen = TRUE;
188#ifdef FEAT_EVAL
189 params.use_debug_break_level = -1;
190#endif
191#ifdef FEAT_WINDOWS
192 params.window_count = -1;
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000193#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +0000194
Bram Moolenaar99685e62013-05-11 13:56:18 +0200195#ifdef FEAT_RUBY
196 {
197 int ruby_stack_start;
198 vim_ruby_init((void *)&ruby_stack_start);
199 }
200#endif
201
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000202#ifdef FEAT_TCL
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000203 vim_tcl_init(params.argv[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000204#endif
205
206#ifdef MEM_PROFILE
207 atexit(vim_mem_profile_dump);
208#endif
209
210#ifdef STARTUPTIME
Bram Moolenaar3f269672009-11-03 11:11:11 +0000211 for (i = 1; i < argc; ++i)
212 {
Bram Moolenaaref94eec2009-11-11 13:22:11 +0000213 if (STRICMP(argv[i], "--startuptime") == 0 && i + 1 < argc)
Bram Moolenaar3f269672009-11-03 11:11:11 +0000214 {
Bram Moolenaaref94eec2009-11-11 13:22:11 +0000215 time_fd = mch_fopen(argv[i + 1], "a");
Bram Moolenaar3f269672009-11-03 11:11:11 +0000216 TIME_MSG("--- VIM STARTING ---");
217 break;
218 }
219 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000220#endif
Bram Moolenaarca003e12006-03-17 23:19:38 +0000221 starttime = time(NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000222
223#ifdef __EMX__
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000224 _wildcard(&params.argc, &params.argv);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000225#endif
226
227#ifdef FEAT_MBYTE
228 (void)mb_init(); /* init mb_bytelen_tab[] to ones */
229#endif
Bram Moolenaardcaf10e2005-01-21 11:55:25 +0000230#ifdef FEAT_EVAL
231 eval_init(); /* init global variables */
232#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000233
234#ifdef __QNXNTO__
235 qnx_init(); /* PhAttach() for clipboard, (and gui) */
236#endif
237
238#ifdef MAC_OS_CLASSIC
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000239 /* Prepare for possibly starting GUI sometime */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000240 /* Macintosh needs this before any memory is allocated. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000241 gui_prepare(&params.argc, params.argv);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000242 TIME_MSG("GUI prepared");
243#endif
244
245 /* Init the table of Normal mode commands. */
246 init_normal_cmds();
247
248#if defined(HAVE_DATE_TIME) && defined(VMS) && defined(VAXC)
Bram Moolenaar58d98232005-07-23 22:25:46 +0000249 make_version(); /* Construct the long version string. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000250#endif
251
252 /*
253 * Allocate space for the generic buffers (needed for set_init_1() and
254 * EMSG2()).
255 */
256 if ((IObuff = alloc(IOSIZE)) == NULL
257 || (NameBuff = alloc(MAXPATHL)) == NULL)
258 mch_exit(0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000259 TIME_MSG("Allocated generic buffers");
260
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000261#ifdef NBDEBUG
262 /* Wait a moment for debugging NetBeans. Must be after allocating
263 * NameBuff. */
264 nbdebug_log_init("SPRO_GVIM_DEBUG", "SPRO_GVIM_DLEVEL");
265 nbdebug_wait(WT_ENV | WT_WAIT | WT_STOP, "SPRO_GVIM_WAIT", 20);
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000266 TIME_MSG("NetBeans debug wait");
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000267#endif
268
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000269#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
270 /*
271 * Setup to use the current locale (for ctype() and many other things).
272 * NOTE: Translated messages with encodings other than latin1 will not
273 * work until set_init_1() has been called!
274 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000275 init_locale();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000276 TIME_MSG("locale set");
277#endif
278
279#ifdef FEAT_GUI
280 gui.dofork = TRUE; /* default is to use fork() */
281#endif
282
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000283 /*
Bram Moolenaar58d98232005-07-23 22:25:46 +0000284 * Do a first scan of the arguments in "argv[]":
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000285 * -display or --display
Bram Moolenaar58d98232005-07-23 22:25:46 +0000286 * --server...
287 * --socketid
Bram Moolenaar78e17622007-08-30 10:26:19 +0000288 * --windowid
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000289 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000290 early_arg_scan(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000291
292#ifdef FEAT_SUN_WORKSHOP
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000293 findYourself(params.argv[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000294#endif
295#if defined(FEAT_GUI) && !defined(MAC_OS_CLASSIC)
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000296 /* Prepare for possibly starting GUI sometime */
297 gui_prepare(&params.argc, params.argv);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000298 TIME_MSG("GUI prepared");
299#endif
300
301#ifdef FEAT_CLIPBOARD
302 clip_init(FALSE); /* Initialise clipboard stuff */
303 TIME_MSG("clipboard setup");
304#endif
305
306 /*
307 * Check if we have an interactive window.
308 * On the Amiga: If there is no window, we open one with a newcli command
309 * (needed for :! to * work). mch_check_win() will also handle the -d or
310 * -dev argument.
311 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000312 params.stdout_isatty = (mch_check_win(params.argc, params.argv) != FAIL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000313 TIME_MSG("window checked");
314
315 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000316 * Allocate the first window and buffer.
317 * Can't do anything without it, exit when it fails.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000318 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000319 if (win_alloc_first() == FAIL)
320 mch_exit(0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000321
322 init_yank(); /* init yank buffers */
323
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000324 alist_init(&global_alist); /* Init the argument list to empty. */
Bram Moolenaar2d1fe052014-05-28 18:22:57 +0200325 global_alist.id = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000326
327 /*
328 * Set the default values for the options.
329 * NOTE: Non-latin1 translated messages are working only after this,
330 * because this is where "has_mbyte" will be set, which is used by
331 * msg_outtrans_len_attr().
332 * First find out the home directory, needed to expand "~" in options.
333 */
334 init_homedir(); /* find real value of $HOME */
335 set_init_1();
336 TIME_MSG("inits 1");
337
338#ifdef FEAT_EVAL
339 set_lang_var(); /* set v:lang and v:ctype */
340#endif
341
342#ifdef FEAT_CLIENTSERVER
343 /*
344 * Do the client-server stuff, unless "--servername ''" was used.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000345 * This may exit Vim if the command was sent to the server.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000346 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000347 exec_on_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000348#endif
349
350 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000351 * Figure out the way to work from the command name argv[0].
352 * "vimdiff" starts diff mode, "rvim" sets "restricted", etc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000353 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000354 parse_command_name(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000355
356 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000357 * Process the command line arguments. File names are put in the global
358 * argument list "global_alist".
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000359 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000360 command_line_scan(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000361 TIME_MSG("parsing arguments");
362
363 /*
364 * On some systems, when we compile with the GUI, we always use it. On Mac
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000365 * there is no terminal version, and on Windows we can't fork one off with
366 * :gui.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000367 */
368#ifdef ALWAYS_USE_GUI
369 gui.starting = TRUE;
370#else
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000371# if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000372 /*
373 * Check if the GUI can be started. Reset gui.starting if not.
374 * Don't know about other systems, stay on the safe side and don't check.
375 */
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000376 if (gui.starting)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000377 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000378 if (gui_init_check() == FAIL)
379 {
380 gui.starting = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000381
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000382 /* When running "evim" or "gvim -y" we need the menus, exit if we
383 * don't have them. */
384 if (params.evim_mode)
385 mch_exit(1);
386 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000387 }
388# endif
389#endif
390
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000391 if (GARGCOUNT > 0)
392 {
393#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
394 /*
395 * Expand wildcards in file names.
396 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000397 if (!params.literal)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000398 {
399 /* Temporarily add '(' and ')' to 'isfname'. These are valid
400 * filename characters but are excluded from 'isfname' to make
401 * "gf" work on a file name in parenthesis (e.g.: see vim.h). */
402 do_cmdline_cmd((char_u *)":set isf+=(,)");
Bram Moolenaar86b68352004-12-27 21:59:20 +0000403 alist_expand(NULL, 0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000404 do_cmdline_cmd((char_u *)":set isf&");
405 }
406#endif
407 fname = alist_name(&GARGLIST[0]);
408 }
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000409
410#if defined(WIN32) && defined(FEAT_MBYTE)
411 {
412 extern void set_alist_count(void);
413
414 /* Remember the number of entries in the argument list. If it changes
415 * we don't react on setting 'encoding'. */
416 set_alist_count();
417 }
418#endif
419
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000420#ifdef MSWIN
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000421 if (GARGCOUNT == 1 && params.full_path)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000422 {
423 /*
424 * If there is one filename, fully qualified, we have very probably
425 * been invoked from explorer, so change to the file's directory.
426 * Hint: to avoid this when typing a command use a forward slash.
427 * If the cd fails, it doesn't matter.
428 */
429 (void)vim_chdirfile(fname);
430 }
431#endif
432 TIME_MSG("expanding arguments");
433
434#ifdef FEAT_DIFF
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000435 if (params.diff_mode && params.window_count == -1)
436 params.window_count = 0; /* open up to 3 windows */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000437#endif
438
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000439 /* Don't redraw until much later. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000440 ++RedrawingDisabled;
441
442 /*
443 * When listing swap file names, don't do cursor positioning et. al.
444 */
445 if (recoverymode && fname == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000446 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000447
448 /*
449 * When certain to start the GUI, don't check capabilities of terminal.
450 * For GTK we can't be sure, but when started from the desktop it doesn't
451 * make sense to try using a terminal.
452 */
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000453#if defined(ALWAYS_USE_GUI) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000454 if (gui.starting
455# ifdef FEAT_GUI_GTK
456 && !isatty(2)
457# endif
458 )
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000459 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000460#endif
461
462#if defined(FEAT_GUI_MAC) && defined(MACOS_X_UNIX)
463 /* When the GUI is started from Finder, need to display messages in a
464 * message box. isatty(2) returns TRUE anyway, thus we need to check the
465 * name to know we're not started from a terminal. */
466 if (gui.starting && (!isatty(2) || strcmp("/dev/console", ttyname(2)) == 0))
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000467 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000468 params.want_full_screen = FALSE;
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000469
470 /* Avoid always using "/" as the current directory. Note that when
471 * started from Finder the arglist will be filled later in
472 * HandleODocAE() and "fname" will be NULL. */
473 if (getcwd((char *)NameBuff, MAXPATHL) != NULL
474 && STRCMP(NameBuff, "/") == 0)
475 {
476 if (fname != NULL)
477 (void)vim_chdirfile(fname);
478 else
479 {
480 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
481 vim_chdir(NameBuff);
482 }
483 }
484 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000485#endif
486
487 /*
488 * mch_init() sets up the terminal (window) for use. This must be
489 * done after resetting full_screen, otherwise it may move the cursor
490 * (MSDOS).
491 * Note that we may use mch_exit() before mch_init()!
492 */
493 mch_init();
494 TIME_MSG("shell init");
495
496#ifdef USE_XSMP
497 /*
498 * For want of anywhere else to do it, try to connect to xsmp here.
499 * Fitting it in after gui_mch_init, but before gui_init (via termcapinit).
500 * Hijacking -X 'no X connection' to also disable XSMP connection as that
501 * has a similar delay upon failure.
502 * Only try if SESSION_MANAGER is set to something non-null.
503 */
504 if (!x_no_connect)
505 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000506 char *p = getenv("SESSION_MANAGER");
507
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000508 if (p != NULL && *p != NUL)
509 {
510 xsmp_init();
511 TIME_MSG("xsmp init");
512 }
513 }
514#endif
515
516 /*
517 * Print a warning if stdout is not a terminal.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000518 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000519 check_tty(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000520
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000521 /* This message comes before term inits, but after setting "silent_mode"
522 * when the input is not a tty. */
523 if (GARGCOUNT > 1 && !silent_mode)
524 printf(_("%d files to edit\n"), GARGCOUNT);
525
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000526 if (params.want_full_screen && !silent_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000527 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000528 termcapinit(params.term); /* set terminal name and get terminal
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000529 capabilities (will set full_screen) */
530 screen_start(); /* don't know where cursor is now */
531 TIME_MSG("Termcap init");
532 }
533
534 /*
535 * Set the default values for the options that use Rows and Columns.
536 */
537 ui_get_shellsize(); /* inits Rows and Columns */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000538 win_init_size();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000539#ifdef FEAT_DIFF
540 /* Set the 'diff' option now, so that it can be checked for in a .vimrc
541 * file. There is no buffer yet though. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000542 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000543 diff_win_options(firstwin, FALSE);
544#endif
545
546 cmdline_row = Rows - p_ch;
547 msg_row = cmdline_row;
548 screenalloc(FALSE); /* allocate screen buffers */
549 set_init_2();
550 TIME_MSG("inits 2");
551
552 msg_scroll = TRUE;
553 no_wait_return = TRUE;
554
555 init_mappings(); /* set up initial mappings */
556
557 init_highlight(TRUE, FALSE); /* set the default highlight groups */
558 TIME_MSG("init highlight");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000559
560#ifdef FEAT_EVAL
561 /* Set the break level after the terminal is initialized. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000562 debug_break_level = params.use_debug_break_level;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000563#endif
564
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100565#ifdef FEAT_MZSCHEME
566 /*
567 * Newer version of MzScheme (Racket) require earlier (trampolined)
568 * initialisation via scheme_main_setup.
569 * Implement this by initialising it as early as possible
570 * and splitting off remaining Vim main into vim_main2
571 */
572 {
573 /* Pack up preprocessed command line arguments.
574 * It is safe because Scheme does not access argc/argv. */
575 char *args[2];
576 args[0] = (char *)fname;
577 args[1] = (char *)&params;
578 return mzscheme_main(2, args);
579 }
580}
Bram Moolenaar8866d272012-11-28 15:55:42 +0100581#endif
582#endif /* NO_VIM_MAIN */
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100583
Bram Moolenaar8866d272012-11-28 15:55:42 +0100584/* vim_main2() needs to be produced when FEAT_MZSCHEME is defined even when
585 * NO_VIM_MAIN is defined. */
586#ifdef FEAT_MZSCHEME
587 int
588vim_main2(int argc UNUSED, char **argv UNUSED)
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100589{
Bram Moolenaar8866d272012-11-28 15:55:42 +0100590# ifndef NO_VIM_MAIN
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100591 char_u *fname = (char_u *)argv[0];
592 mparm_T params;
593
594 memcpy(&params, argv[1], sizeof(params));
Bram Moolenaar8866d272012-11-28 15:55:42 +0100595# else
596 return 0;
597}
598# endif
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100599#endif
600
Bram Moolenaar8866d272012-11-28 15:55:42 +0100601#ifndef NO_VIM_MAIN
Bram Moolenaar58d98232005-07-23 22:25:46 +0000602 /* Execute --cmd arguments. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000603 exe_pre_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000604
Bram Moolenaar58d98232005-07-23 22:25:46 +0000605 /* Source startup scripts. */
606 source_startup_scripts(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000607
608#ifdef FEAT_EVAL
609 /*
610 * Read all the plugin files.
611 * Only when compiled with +eval, since most plugins need it.
612 */
613 if (p_lpl)
614 {
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000615# ifdef VMS /* Somehow VMS doesn't handle the "**". */
616 source_runtime((char_u *)"plugin/*.vim", TRUE);
617# else
Bram Moolenaar07d4d732005-10-03 22:04:08 +0000618 source_runtime((char_u *)"plugin/**/*.vim", TRUE);
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000619# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000620 TIME_MSG("loading plugins");
621 }
622#endif
623
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000624#ifdef FEAT_DIFF
625 /* Decide about window layout for diff mode after reading vimrc. */
626 if (params.diff_mode && params.window_layout == 0)
627 {
628 if (diffopt_horizontal())
629 params.window_layout = WIN_HOR; /* use horizontal split */
630 else
631 params.window_layout = WIN_VER; /* use vertical split */
632 }
633#endif
634
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000635 /*
636 * Recovery mode without a file name: List swap files.
637 * This uses the 'dir' option, therefore it must be after the
638 * initializations.
639 */
640 if (recoverymode && fname == NULL)
641 {
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200642 recover_names(NULL, TRUE, 0, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000643 mch_exit(0);
644 }
645
646 /*
647 * Set a few option defaults after reading .vimrc files:
648 * 'title' and 'icon', Unix: 'shellpipe' and 'shellredir'.
649 */
650 set_init_3();
651 TIME_MSG("inits 3");
652
653 /*
654 * "-n" argument: Disable swap file by setting 'updatecount' to 0.
655 * Note that this overrides anything from a vimrc file.
656 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000657 if (params.no_swap_file)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000658 p_uc = 0;
659
660#ifdef FEAT_FKMAP
661 if (curwin->w_p_rl && p_altkeymap)
662 {
663 p_hkmap = FALSE; /* Reset the Hebrew keymap mode */
664# ifdef FEAT_ARABIC
665 curwin->w_p_arab = FALSE; /* Reset the Arabic keymap mode */
666# endif
667 p_fkmap = TRUE; /* Set the Farsi keymap mode */
668 }
669#endif
670
671#ifdef FEAT_GUI
672 if (gui.starting)
673 {
674#if defined(UNIX) || defined(VMS)
675 /* When something caused a message from a vimrc script, need to output
676 * an extra newline before the shell prompt. */
677 if (did_emsg || msg_didout)
678 putchar('\n');
679#endif
680
681 gui_start(); /* will set full_screen to TRUE */
682 TIME_MSG("starting GUI");
683
684 /* When running "evim" or "gvim -y" we need the menus, exit if we
685 * don't have them. */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000686 if (!gui.in_use && params.evim_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000687 mch_exit(1);
688 }
689#endif
690
691#ifdef SPAWNO /* special MSDOS swapping library */
692 init_SPAWNO("", SWAP_ANY);
693#endif
694
695#ifdef FEAT_VIMINFO
696 /*
Bram Moolenaard812df62008-11-09 12:46:09 +0000697 * Read in registers, history etc, but not marks, from the viminfo file.
698 * This is where v:oldfiles gets filled.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000699 */
700 if (*p_viminfo != NUL)
701 {
Bram Moolenaard812df62008-11-09 12:46:09 +0000702 read_viminfo(NULL, VIF_WANT_INFO | VIF_GET_OLDFILES);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000703 TIME_MSG("reading viminfo");
704 }
705#endif
Bram Moolenaar2cd36962014-01-14 12:57:05 +0100706#ifdef FEAT_EVAL
707 /* It's better to make v:oldfiles an empty list than NULL. */
708 if (get_vim_var_list(VV_OLDFILES) == NULL)
709 set_vim_var_list(VV_OLDFILES, list_alloc());
710#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000711
712#ifdef FEAT_QUICKFIX
713 /*
714 * "-q errorfile": Load the error file now.
715 * If the error file can't be read, exit before doing anything else.
716 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000717 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000718 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000719 if (params.use_ef != NULL)
720 set_string_option_direct((char_u *)"ef", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000721 params.use_ef, OPT_FREE, SID_CARG);
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200722 vim_snprintf((char *)IObuff, IOSIZE, "cfile %s", p_ef);
723 if (qf_init(NULL, p_ef, p_efm, TRUE, IObuff) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000724 {
725 out_char('\n');
726 mch_exit(3);
727 }
728 TIME_MSG("reading errorfile");
729 }
730#endif
731
732 /*
733 * Start putting things on the screen.
734 * Scroll screen down before drawing over it
735 * Clear screen now, so file message will not be cleared.
736 */
737 starting = NO_BUFFERS;
738 no_wait_return = FALSE;
739 if (!exmode_active)
740 msg_scroll = FALSE;
741
742#ifdef FEAT_GUI
743 /*
744 * This seems to be required to make callbacks to be called now, instead
745 * of after things have been put on the screen, which then may be deleted
746 * when getting a resize callback.
747 * For the Mac this handles putting files dropped on the Vim icon to
748 * global_alist.
749 */
750 if (gui.in_use)
751 {
752# ifdef FEAT_SUN_WORKSHOP
753 if (!usingSunWorkShop)
754# endif
755 gui_wait_for_chars(50L);
756 TIME_MSG("GUI delay");
757 }
758#endif
759
760#if defined(FEAT_GUI_PHOTON) && defined(FEAT_CLIPBOARD)
761 qnx_clip_init();
762#endif
763
Bram Moolenaarc8bbaa32010-07-14 16:54:21 +0200764#if defined(MACOS_X) && defined(FEAT_CLIPBOARD)
765 clip_init(TRUE);
766#endif
767
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000768#ifdef FEAT_XCLIPBOARD
769 /* Start using the X clipboard, unless the GUI was started. */
770# ifdef FEAT_GUI
771 if (!gui.in_use)
772# endif
773 {
774 setup_term_clip();
775 TIME_MSG("setup clipboard");
776 }
777#endif
778
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000779#ifdef FEAT_CLIENTSERVER
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000780 /* Prepare for being a Vim server. */
781 prepare_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000782#endif
783
784 /*
785 * If "-" argument given: Read file from stdin.
786 * Do this before starting Raw mode, because it may change things that the
787 * writing end of the pipe doesn't like, e.g., in case stdin and stderr
788 * are the same terminal: "cat | vim -".
789 * Using autocommands here may cause trouble...
790 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000791 if (params.edit_type == EDIT_STDIN && !recoverymode)
792 read_stdin();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000793
794#if defined(UNIX) || defined(VMS)
795 /* When switching screens and something caused a message from a vimrc
796 * script, need to output an extra newline on exit. */
797 if ((did_emsg || msg_didout) && *T_TI != NUL)
798 newline_on_exit = TRUE;
799#endif
800
801 /*
802 * When done something that is not allowed or error message call
803 * wait_return. This must be done before starttermcap(), because it may
804 * switch to another screen. It must be done after settmode(TMODE_RAW),
805 * because we want to react on a single key stroke.
806 * Call settmode and starttermcap here, so the T_KS and T_TI may be
Bram Moolenaar49325942007-05-10 19:19:59 +0000807 * defined by termcapinit and redefined in .exrc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000808 */
809 settmode(TMODE_RAW);
810 TIME_MSG("setting raw mode");
811
812 if (need_wait_return || msg_didany)
813 {
814 wait_return(TRUE);
815 TIME_MSG("waiting for return");
816 }
817
818 starttermcap(); /* start termcap if not done by wait_return() */
819 TIME_MSG("start termcap");
Bram Moolenaar9584b312013-03-13 19:29:28 +0100820#if defined(FEAT_TERMRESPONSE) && defined(FEAT_MBYTE)
Bram Moolenaar386dcde2013-09-29 16:27:47 +0200821 may_req_ambiguous_char_width();
Bram Moolenaar9584b312013-03-13 19:29:28 +0100822#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000823
824#ifdef FEAT_MOUSE
825 setmouse(); /* may start using the mouse */
826#endif
827 if (scroll_region)
828 scroll_region_reset(); /* In case Rows changed */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000829 scroll_start(); /* may scroll the screen to the right position */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000830
831 /*
832 * Don't clear the screen when starting in Ex mode, unless using the GUI.
833 */
834 if (exmode_active
835#ifdef FEAT_GUI
836 && !gui.in_use
837#endif
838 )
839 must_redraw = CLEAR;
840 else
841 {
842 screenclear(); /* clear screen */
843 TIME_MSG("clearing screen");
844 }
845
846#ifdef FEAT_CRYPT
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000847 if (params.ask_for_key)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000848 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200849 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000850 TIME_MSG("getting crypt key");
851 }
852#endif
853
854 no_wait_return = TRUE;
855
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000856 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000857 * Create the requested number of windows and edit buffers in them.
858 * Also does recovery if "recoverymode" set.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000859 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000860 create_windows(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000861 TIME_MSG("opening buffers");
862
Bram Moolenaar867a4b72007-03-18 20:51:46 +0000863#ifdef FEAT_EVAL
864 /* clear v:swapcommand */
865 set_vim_var_string(VV_SWAPCOMMAND, NULL, -1);
866#endif
867
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000868 /* Ex starts at last line of the file */
869 if (exmode_active)
870 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
871
872#ifdef FEAT_AUTOCMD
873 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
874 TIME_MSG("BufEnter autocommands");
875#endif
876 setpcmark();
877
878#ifdef FEAT_QUICKFIX
879 /*
880 * When started with "-q errorfile" jump to first error now.
881 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000882 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000883 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000884 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000885 TIME_MSG("jump to first error");
886 }
887#endif
888
889#ifdef FEAT_WINDOWS
890 /*
891 * If opened more than one window, start editing files in the other
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000892 * windows.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000893 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000894 edit_buffers(&params);
895#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000896
897#ifdef FEAT_DIFF
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000898 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000899 {
900 win_T *wp;
901
902 /* set options in each window for "vimdiff". */
903 for (wp = firstwin; wp != NULL; wp = wp->w_next)
904 diff_win_options(wp, TRUE);
905 }
906#endif
907
908 /*
909 * Shorten any of the filenames, but only when absolute.
910 */
911 shorten_fnames(FALSE);
912
913 /*
914 * Need to jump to the tag before executing the '-c command'.
915 * Makes "vim -c '/return' -t main" work.
916 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000917 if (params.tagname != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000918 {
Bram Moolenaar146522e2005-12-16 21:55:46 +0000919#if defined(HAS_SWAP_EXISTS_ACTION)
920 swap_exists_did_quit = FALSE;
921#endif
922
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000923 vim_snprintf((char *)IObuff, IOSIZE, "ta %s", params.tagname);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000924 do_cmdline_cmd(IObuff);
925 TIME_MSG("jumping to tag");
Bram Moolenaar146522e2005-12-16 21:55:46 +0000926
927#if defined(HAS_SWAP_EXISTS_ACTION)
928 /* If the user doesn't want to edit the file then we quit here. */
929 if (swap_exists_did_quit)
930 getout(1);
931#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000932 }
933
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000934 /* Execute any "+", "-c" and "-S" arguments. */
935 if (params.n_commands > 0)
936 exe_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000937
938 RedrawingDisabled = 0;
939 redraw_all_later(NOT_VALID);
940 no_wait_return = FALSE;
941 starting = 0;
942
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000943#ifdef FEAT_TERMRESPONSE
944 /* Requesting the termresponse is postponed until here, so that a "-c q"
945 * argument doesn't make it appear in the shell Vim was started from. */
946 may_req_termresponse();
947#endif
948
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000949 /* start in insert mode */
950 if (p_im)
951 need_start_insertmode = TRUE;
952
953#ifdef FEAT_AUTOCMD
954 apply_autocmds(EVENT_VIMENTER, NULL, NULL, FALSE, curbuf);
955 TIME_MSG("VimEnter autocommands");
956#endif
957
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200958#if defined(FEAT_EVAL) && defined(FEAT_CLIPBOARD)
959 /* Adjust default register name for "unnamed" in 'clipboard'. Can only be
960 * done after the clipboard is available and all initial commands that may
961 * modify the 'clipboard' setting have run; i.e. just before entering the
962 * main loop. */
963 {
964 int default_regname = 0;
965 adjust_clip_reg(&default_regname);
966 set_reg_var(default_regname);
967 }
968#endif
969
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000970#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
971 /* When a startup script or session file setup for diff'ing and
972 * scrollbind, sync the scrollbind now. */
973 if (curwin->w_p_diff && curwin->w_p_scb)
974 {
975 update_topline();
976 check_scrollbind((linenr_T)0, 0L);
977 TIME_MSG("diff scrollbinding");
978 }
979#endif
980
981#if defined(WIN3264) && !defined(FEAT_GUI_W32)
982 mch_set_winsize_now(); /* Allow winsize changes from now on */
983#endif
984
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000985#if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
986 /* When tab pages were created, may need to update the tab pages line and
987 * scrollbars. This is skipped while creating them. */
988 if (first_tabpage->tp_next != NULL)
989 {
990 out_flush();
991 gui_init_which_components(NULL);
992 gui_update_scrollbars(TRUE);
993 }
994 need_mouse_correct = TRUE;
995#endif
996
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000997 /* If ":startinsert" command used, stuff a dummy command to be able to
998 * call normal_cmd(), which will then start Insert mode. */
999 if (restart_edit != 0)
Bram Moolenaarebefac62005-12-28 22:39:57 +00001000 stuffcharReadbuff(K_NOP);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001001
1002#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001003 if (netbeansArg != NULL && strncmp("-nb", netbeansArg, 3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +02001004 {
1005# ifdef FEAT_GUI
Bram Moolenaar173c9852010-09-29 17:27:01 +02001006# if !defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK) \
Bram Moolenaar67c53842010-05-22 18:28:27 +02001007 && !defined(FEAT_GUI_W32)
1008 if (gui.in_use)
1009 {
1010 mch_errmsg(_("netbeans is not supported with this GUI\n"));
1011 mch_exit(2);
1012 }
1013# endif
1014# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001015 /* Tell the client that it can start sending commands. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001016 netbeans_open(netbeansArg + 3, TRUE);
Bram Moolenaar67c53842010-05-22 18:28:27 +02001017 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001018#endif
1019
1020 TIME_MSG("before starting main loop");
1021
1022 /*
1023 * Call the main command loop. This never returns.
Bram Moolenaarbbc98db2012-02-12 01:55:55 +01001024 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001025 main_loop(FALSE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001026
1027 return 0;
1028}
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01001029#endif /* NO_VIM_MAIN */
Bram Moolenaar8866d272012-11-28 15:55:42 +01001030#endif /* PROTO */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001031
1032/*
1033 * Main loop: Execute Normal mode commands until exiting Vim.
1034 * Also used to handle commands in the command-line window, until the window
1035 * is closed.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001036 * Also used to handle ":visual" command after ":global": execute Normal mode
1037 * commands, return when entering Ex mode. "noexmode" is TRUE then.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001038 */
1039 void
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001040main_loop(cmdwin, noexmode)
1041 int cmdwin; /* TRUE when working in the command-line window */
1042 int noexmode; /* TRUE when return on entering Ex mode */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001043{
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001044 oparg_T oa; /* operator arguments */
1045 int previous_got_int = FALSE; /* "got_int" was TRUE */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001046#ifdef FEAT_CONCEAL
1047 linenr_T conceal_old_cursor_line = 0;
1048 linenr_T conceal_new_cursor_line = 0;
1049 int conceal_update_lines = FALSE;
1050#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001051
1052#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
1053 /* Setup to catch a terminating error from the X server. Just ignore
1054 * it, restore the state and continue. This might not always work
1055 * properly, but at least we don't exit unexpectedly when the X server
Bram Moolenaar2cd36962014-01-14 12:57:05 +01001056 * exits while Vim is running in a console. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001057 if (!cmdwin && !noexmode && SETJMP(x_jump_env))
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001058 {
1059 State = NORMAL;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001060 VIsual_active = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001061 got_int = TRUE;
1062 need_wait_return = FALSE;
1063 global_busy = FALSE;
1064 exmode_active = 0;
1065 skip_redraw = FALSE;
1066 RedrawingDisabled = 0;
1067 no_wait_return = 0;
Bram Moolenaar7701c242011-10-04 16:43:53 +02001068 vgetc_busy = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001069# ifdef FEAT_EVAL
1070 emsg_skip = 0;
1071# endif
1072 emsg_off = 0;
1073# ifdef FEAT_MOUSE
1074 setmouse();
1075# endif
1076 settmode(TMODE_RAW);
1077 starttermcap();
1078 scroll_start();
1079 redraw_later_clear();
1080 }
1081#endif
1082
1083 clear_oparg(&oa);
1084 while (!cmdwin
1085#ifdef FEAT_CMDWIN
1086 || cmdwin_result == 0
1087#endif
1088 )
1089 {
1090 if (stuff_empty())
1091 {
1092 did_check_timestamps = FALSE;
1093 if (need_check_timestamps)
1094 check_timestamps(FALSE);
1095 if (need_wait_return) /* if wait_return still needed ... */
1096 wait_return(FALSE); /* ... call it now */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001097 if (need_start_insertmode && goto_im() && !VIsual_active)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001098 {
1099 need_start_insertmode = FALSE;
1100 stuffReadbuff((char_u *)"i"); /* start insert mode next */
1101 /* skip the fileinfo message now, because it would be shown
1102 * after insert mode finishes! */
1103 need_fileinfo = FALSE;
1104 }
1105 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001106
1107 /* Reset "got_int" now that we got back to the main loop. Except when
1108 * inside a ":g/pat/cmd" command, then the "got_int" needs to abort
1109 * the ":g" command.
1110 * For ":g/pat/vi" we reset "got_int" when used once. When used
1111 * a second time we go back to Ex mode and abort the ":g" command. */
1112 if (got_int)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001113 {
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001114 if (noexmode && global_busy && !exmode_active && previous_got_int)
1115 {
1116 /* Typed two CTRL-C in a row: go back to ex mode as if "Q" was
1117 * used and keep "got_int" set, so that it aborts ":g". */
1118 exmode_active = EXMODE_NORMAL;
1119 State = NORMAL;
1120 }
1121 else if (!global_busy || !exmode_active)
1122 {
1123 if (!quit_more)
1124 (void)vgetc(); /* flush all buffers */
1125 got_int = FALSE;
1126 }
1127 previous_got_int = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001128 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001129 else
1130 previous_got_int = FALSE;
1131
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001132 if (!exmode_active)
1133 msg_scroll = FALSE;
1134 quit_more = FALSE;
1135
1136 /*
1137 * If skip redraw is set (for ":" in wait_return()), don't redraw now.
1138 * If there is nothing in the stuff_buffer or do_redraw is TRUE,
1139 * update cursor and redraw.
1140 */
1141 if (skip_redraw || exmode_active)
1142 skip_redraw = FALSE;
1143 else if (do_redraw || stuff_empty())
1144 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001145#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001146 /* Trigger CursorMoved if the cursor moved. */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001147 if (!finish_op && (
1148# ifdef FEAT_AUTOCMD
1149 has_cursormoved()
1150# endif
1151# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
1152 ||
1153# endif
1154# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001155 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001156# endif
1157 )
1158 && !equalpos(last_cursormoved, curwin->w_cursor))
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001159 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001160# ifdef FEAT_AUTOCMD
1161 if (has_cursormoved())
1162 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL,
1163 FALSE, curbuf);
1164# endif
1165# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001166 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001167 {
1168 conceal_old_cursor_line = last_cursormoved.lnum;
1169 conceal_new_cursor_line = curwin->w_cursor.lnum;
1170 conceal_update_lines = TRUE;
1171 }
1172# endif
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001173 last_cursormoved = curwin->w_cursor;
1174 }
1175#endif
1176
Bram Moolenaar186628f2013-03-19 13:33:23 +01001177#ifdef FEAT_AUTOCMD
1178 /* Trigger TextChanged if b_changedtick differs. */
1179 if (!finish_op && has_textchanged()
1180 && last_changedtick != curbuf->b_changedtick)
1181 {
1182 if (last_changedtick_buf == curbuf)
1183 apply_autocmds(EVENT_TEXTCHANGED, NULL, NULL,
1184 FALSE, curbuf);
1185 last_changedtick_buf = curbuf;
1186 last_changedtick = curbuf->b_changedtick;
1187 }
1188#endif
1189
Bram Moolenaar33aec762006-01-22 23:30:12 +00001190#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
1191 /* Scroll-binding for diff mode may have been postponed until
1192 * here. Avoids doing it for every change. */
1193 if (diff_need_scrollbind)
1194 {
1195 check_scrollbind((linenr_T)0, 0L);
1196 diff_need_scrollbind = FALSE;
1197 }
1198#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001199#if defined(FEAT_FOLDING)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001200 /* Include a closed fold completely in the Visual area. */
1201 foldAdjustVisual();
1202#endif
1203#ifdef FEAT_FOLDING
1204 /*
1205 * When 'foldclose' is set, apply 'foldlevel' to folds that don't
1206 * contain the cursor.
1207 * When 'foldopen' is "all", open the fold(s) under the cursor.
1208 * This may mark the window for redrawing.
1209 */
1210 if (hasAnyFolding(curwin) && !char_avail())
1211 {
1212 foldCheckClose();
1213 if (fdo_flags & FDO_ALL)
1214 foldOpenCursor();
1215 }
1216#endif
1217
1218 /*
1219 * Before redrawing, make sure w_topline is correct, and w_leftcol
1220 * if lines don't wrap, and w_skipcol if lines wrap.
1221 */
1222 update_topline();
1223 validate_cursor();
1224
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001225 if (VIsual_active)
1226 update_curbuf(INVERTED);/* update inverted part */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001227 else if (must_redraw)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001228 update_screen(0);
1229 else if (redraw_cmdline || clear_cmdline)
1230 showmode();
1231#ifdef FEAT_WINDOWS
1232 redraw_statuslines();
1233#endif
1234#ifdef FEAT_TITLE
1235 if (need_maketitle)
1236 maketitle();
1237#endif
1238 /* display message after redraw */
1239 if (keep_msg != NULL)
1240 {
1241 char_u *p;
1242
1243 /* msg_attr_keep() will set keep_msg to NULL, must free the
1244 * string here. */
1245 p = keep_msg;
Bram Moolenaar238a5642006-02-21 22:12:05 +00001246 keep_msg = NULL;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001247 msg_attr(p, keep_msg_attr);
1248 vim_free(p);
1249 }
1250 if (need_fileinfo) /* show file info after redraw */
1251 {
1252 fileinfo(FALSE, TRUE, FALSE);
1253 need_fileinfo = FALSE;
1254 }
1255
1256 emsg_on_display = FALSE; /* can delete error message now */
1257 did_emsg = FALSE;
1258 msg_didany = FALSE; /* reset lines_left in msg_start() */
Bram Moolenaar661b1822005-07-28 22:36:45 +00001259 may_clear_sb_text(); /* clear scroll-back text on next msg */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001260 showruler(FALSE);
1261
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001262# if defined(FEAT_CONCEAL)
1263 if (conceal_update_lines
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001264 && (conceal_old_cursor_line != conceal_new_cursor_line
1265 || conceal_cursor_line(curwin)
1266 || need_cursor_line_redraw))
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001267 {
Bram Moolenaarc2b4c622011-02-15 16:29:59 +01001268 if (conceal_old_cursor_line != conceal_new_cursor_line
1269 && conceal_old_cursor_line
1270 <= curbuf->b_ml.ml_line_count)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001271 update_single_line(curwin, conceal_old_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001272 update_single_line(curwin, conceal_new_cursor_line);
1273 curwin->w_valid &= ~VALID_CROW;
1274 }
1275# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001276 setcursor();
1277 cursor_on();
1278
1279 do_redraw = FALSE;
Bram Moolenaar3f269672009-11-03 11:11:11 +00001280
1281#ifdef STARTUPTIME
1282 /* Now that we have drawn the first screen all the startup stuff
1283 * has been done, close any file for startup messages. */
1284 if (time_fd != NULL)
1285 {
1286 TIME_MSG("first screen update");
1287 TIME_MSG("--- VIM STARTED ---");
1288 fclose(time_fd);
1289 time_fd = NULL;
1290 }
1291#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001292 }
1293#ifdef FEAT_GUI
1294 if (need_mouse_correct)
1295 gui_mouse_correct();
1296#endif
1297
1298 /*
1299 * Update w_curswant if w_set_curswant has been set.
1300 * Postponed until here to avoid computing w_virtcol too often.
1301 */
1302 update_curswant();
1303
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001304#ifdef FEAT_EVAL
1305 /*
1306 * May perform garbage collection when waiting for a character, but
1307 * only at the very toplevel. Otherwise we may be using a List or
1308 * Dict internally somewhere.
1309 * "may_garbage_collect" is reset in vgetc() which is invoked through
1310 * do_exmode() and normal_cmd().
1311 */
1312 may_garbage_collect = (!cmdwin && !noexmode);
1313#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001314 /*
1315 * If we're invoked as ex, do a round of ex commands.
1316 * Otherwise, get and execute a normal mode command.
1317 */
1318 if (exmode_active)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001319 {
1320 if (noexmode) /* End of ":global/path/visual" commands */
1321 return;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001322 do_exmode(exmode_active == EXMODE_VIM);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001323 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001324 else
1325 normal_cmd(&oa, TRUE);
1326 }
1327}
1328
1329
1330#if defined(USE_XSMP) || defined(FEAT_GUI_MSWIN) || defined(PROTO)
1331/*
1332 * Exit, but leave behind swap files for modified buffers.
1333 */
1334 void
1335getout_preserve_modified(exitval)
1336 int exitval;
1337{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001338# if defined(SIGHUP) && defined(SIG_IGN)
1339 /* Ignore SIGHUP, because a dropped connection causes a read error, which
1340 * makes Vim exit and then handling SIGHUP causes various reentrance
1341 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001342 signal(SIGHUP, SIG_IGN);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001343# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001344
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001345 ml_close_notmod(); /* close all not-modified buffers */
1346 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
1347 ml_close_all(FALSE); /* close all memfiles, without deleting */
1348 getout(exitval); /* exit Vim properly */
1349}
1350#endif
1351
1352
1353/* Exit properly */
1354 void
1355getout(exitval)
1356 int exitval;
1357{
1358#ifdef FEAT_AUTOCMD
1359 buf_T *buf;
1360 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001361 tabpage_T *tp, *next_tp;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001362#endif
1363
1364 exiting = TRUE;
1365
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001366 /* When running in Ex mode an error causes us to exit with a non-zero exit
1367 * code. POSIX requires this, although it's not 100% clear from the
1368 * standard. */
1369 if (exmode_active)
1370 exitval += ex_exitval;
1371
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001372 /* Position the cursor on the last screen line, below all the text */
1373#ifdef FEAT_GUI
1374 if (!gui.in_use)
1375#endif
1376 windgoto((int)Rows - 1, 0);
1377
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +00001378#if defined(FEAT_EVAL) || defined(FEAT_SYN_HL)
1379 /* Optionally print hashtable efficiency. */
1380 hash_debug_results();
1381#endif
1382
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001383#ifdef FEAT_GUI
1384 msg_didany = FALSE;
1385#endif
1386
1387#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001388 if (get_vim_var_nr(VV_DYING) <= 1)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001389 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001390 /* Trigger BufWinLeave for all windows, but only once per buffer. */
1391# if defined FEAT_WINDOWS
1392 for (tp = first_tabpage; tp != NULL; tp = next_tp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001393 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001394 next_tp = tp->tp_next;
1395 for (wp = (tp == curtab)
1396 ? firstwin : tp->tp_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001397 {
Bram Moolenaar802418d2013-01-17 14:00:11 +01001398 if (wp->w_buffer == NULL)
1399 /* Autocmd must have close the buffer already, skip. */
1400 continue;
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001401 buf = wp->w_buffer;
1402 if (buf->b_changedtick != -1)
1403 {
1404 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname,
1405 buf->b_fname, FALSE, buf);
Bram Moolenaarb429cde2012-04-25 18:24:29 +02001406 buf->b_changedtick = -1; /* note that we did it already */
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001407 /* start all over, autocommands may mess up the lists */
1408 next_tp = first_tabpage;
1409 break;
1410 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001411 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001412 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001413# else
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001414 apply_autocmds(EVENT_BUFWINLEAVE, curbuf, curbuf->b_fname,
1415 FALSE, curbuf);
Bram Moolenaarf740b292006-02-16 22:11:02 +00001416# endif
Bram Moolenaar33aec762006-01-22 23:30:12 +00001417
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001418 /* Trigger BufUnload for buffers that are loaded */
1419 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1420 if (buf->b_ml.ml_mfp != NULL)
1421 {
1422 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001423 FALSE, buf);
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001424 if (!buf_valid(buf)) /* autocmd may delete the buffer */
1425 break;
1426 }
1427 apply_autocmds(EVENT_VIMLEAVEPRE, NULL, NULL, FALSE, curbuf);
1428 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001429#endif
1430
1431#ifdef FEAT_VIMINFO
1432 if (*p_viminfo != NUL)
1433 /* Write out the registers, history, marks etc, to the viminfo file */
1434 write_viminfo(NULL, FALSE);
1435#endif
1436
1437#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001438 if (get_vim_var_nr(VV_DYING) <= 1)
1439 apply_autocmds(EVENT_VIMLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001440#endif
1441
Bram Moolenaar05159a02005-02-26 23:04:13 +00001442#ifdef FEAT_PROFILE
1443 profile_dump();
1444#endif
1445
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001446 if (did_emsg
1447#ifdef FEAT_GUI
1448 || (gui.in_use && msg_didany && p_verbose > 0)
1449#endif
1450 )
1451 {
1452 /* give the user a chance to read the (error) message */
1453 no_wait_return = FALSE;
1454 wait_return(FALSE);
1455 }
1456
1457#ifdef FEAT_AUTOCMD
1458 /* Position the cursor again, the autocommands may have moved it */
1459# ifdef FEAT_GUI
1460 if (!gui.in_use)
1461# endif
1462 windgoto((int)Rows - 1, 0);
1463#endif
1464
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001465#ifdef FEAT_LUA
1466 lua_end();
1467#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001468#ifdef FEAT_MZSCHEME
1469 mzscheme_end();
1470#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001471#ifdef FEAT_TCL
1472 tcl_end();
1473#endif
1474#ifdef FEAT_RUBY
1475 ruby_end();
1476#endif
1477#ifdef FEAT_PYTHON
1478 python_end();
1479#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001480#ifdef FEAT_PYTHON3
1481 python3_end();
1482#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001483#ifdef FEAT_PERL
1484 perl_end();
1485#endif
1486#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
1487 iconv_end();
1488#endif
1489#ifdef FEAT_NETBEANS_INTG
1490 netbeans_end();
1491#endif
Bram Moolenaar02b06312007-09-06 15:39:22 +00001492#ifdef FEAT_CSCOPE
1493 cs_end();
1494#endif
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001495#ifdef FEAT_EVAL
1496 if (garbage_collect_at_exit)
1497 garbage_collect();
1498#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001499
1500 mch_exit(exitval);
1501}
1502
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01001503#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001504/*
1505 * Get a (optional) count for a Vim argument.
1506 */
1507 static int
1508get_number_arg(p, idx, def)
1509 char_u *p; /* pointer to argument */
1510 int *idx; /* index in argument, is incremented */
1511 int def; /* default value */
1512{
1513 if (vim_isdigit(p[*idx]))
1514 {
1515 def = atoi((char *)&(p[*idx]));
1516 while (vim_isdigit(p[*idx]))
1517 *idx = *idx + 1;
1518 }
1519 return def;
1520}
1521
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001522#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1523/*
1524 * Setup to use the current locale (for ctype() and many other things).
1525 */
1526 static void
1527init_locale()
1528{
1529 setlocale(LC_ALL, "");
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001530
Bram Moolenaarc6af8122010-05-21 12:04:55 +02001531# ifdef FEAT_GUI_GTK
1532 /* Tell Gtk not to change our locale settings. */
1533 gtk_disable_setlocale();
1534# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001535# if defined(FEAT_FLOAT) && defined(LC_NUMERIC)
1536 /* Make sure strtod() uses a decimal point, not a comma. */
1537 setlocale(LC_NUMERIC, "C");
1538# endif
1539
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001540# ifdef WIN32
1541 /* Apparently MS-Windows printf() may cause a crash when we give it 8-bit
1542 * text while it's expecting text in the current locale. This call avoids
1543 * that. */
1544 setlocale(LC_CTYPE, "C");
1545# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001546
1547# ifdef FEAT_GETTEXT
1548 {
1549 int mustfree = FALSE;
1550 char_u *p;
1551
1552# ifdef DYNAMIC_GETTEXT
1553 /* Initialize the gettext library */
1554 dyn_libintl_init(NULL);
1555# endif
1556 /* expand_env() doesn't work yet, because chartab[] is not initialized
1557 * yet, call vim_getenv() directly */
1558 p = vim_getenv((char_u *)"VIMRUNTIME", &mustfree);
1559 if (p != NULL && *p != NUL)
1560 {
Bram Moolenaar1ad2f132007-06-19 18:27:18 +00001561 vim_snprintf((char *)NameBuff, MAXPATHL, "%s/lang", p);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001562 bindtextdomain(VIMPACKAGE, (char *)NameBuff);
1563 }
1564 if (mustfree)
1565 vim_free(p);
1566 textdomain(VIMPACKAGE);
1567 }
1568# endif
1569}
1570#endif
1571
1572/*
1573 * Check for: [r][e][g][vi|vim|view][diff][ex[im]]
1574 * If the executable name starts with "r" we disable shell commands.
1575 * If the next character is "e" we run in Easy mode.
1576 * If the next character is "g" we run the GUI version.
1577 * If the next characters are "view" we start in readonly mode.
1578 * If the next characters are "diff" or "vimdiff" we start in diff mode.
1579 * If the next characters are "ex" we start in Ex mode. If it's followed
1580 * by "im" use improved Ex mode.
1581 */
1582 static void
1583parse_command_name(parmp)
1584 mparm_T *parmp;
1585{
1586 char_u *initstr;
1587
1588 initstr = gettail((char_u *)parmp->argv[0]);
1589
1590#ifdef MACOS_X_UNIX
1591 /* An issue has been seen when launching Vim in such a way that
1592 * $PWD/$ARGV[0] or $ARGV[0] is not the absolute path to the
1593 * executable or a symbolic link of it. Until this issue is resolved
1594 * we prohibit the GUI from being used.
1595 */
1596 if (STRCMP(initstr, parmp->argv[0]) == 0)
1597 disallow_gui = TRUE;
1598
1599 /* TODO: On MacOS X default to gui if argv[0] ends in:
Bram Moolenaar27dc1952006-03-15 23:06:44 +00001600 * /Vim.app/Contents/MacOS/Vim */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001601#endif
1602
1603#ifdef FEAT_EVAL
1604 set_vim_var_string(VV_PROGNAME, initstr, -1);
Bram Moolenaara1706c92014-04-01 19:55:49 +02001605 set_vim_var_string(VV_PROGPATH, (char_u *)parmp->argv[0], -1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001606#endif
1607
1608 if (TOLOWER_ASC(initstr[0]) == 'r')
1609 {
1610 restricted = TRUE;
1611 ++initstr;
1612 }
1613
Bram Moolenaarad249fb2010-05-07 16:35:04 +02001614 /* Use evim mode for "evim" and "egvim", not for "editor". */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001615 if (TOLOWER_ASC(initstr[0]) == 'e'
1616 && (TOLOWER_ASC(initstr[1]) == 'v'
1617 || TOLOWER_ASC(initstr[1]) == 'g'))
1618 {
1619#ifdef FEAT_GUI
1620 gui.starting = TRUE;
1621#endif
1622 parmp->evim_mode = TRUE;
1623 ++initstr;
1624 }
1625
Bram Moolenaar806875d2008-09-18 18:57:10 +00001626 /* "gvim" starts the GUI. Also accept "Gvim" for MS-Windows. */
1627 if (TOLOWER_ASC(initstr[0]) == 'g')
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001628 {
1629 main_start_gui();
1630#ifdef FEAT_GUI
1631 ++initstr;
1632#endif
1633 }
1634
1635 if (STRNICMP(initstr, "view", 4) == 0)
1636 {
1637 readonlymode = TRUE;
1638 curbuf->b_p_ro = TRUE;
1639 p_uc = 10000; /* don't update very often */
1640 initstr += 4;
1641 }
1642 else if (STRNICMP(initstr, "vim", 3) == 0)
1643 initstr += 3;
1644
1645 /* Catch "[r][g]vimdiff" and "[r][g]viewdiff". */
1646 if (STRICMP(initstr, "diff") == 0)
1647 {
1648#ifdef FEAT_DIFF
1649 parmp->diff_mode = TRUE;
1650#else
1651 mch_errmsg(_("This Vim was not compiled with the diff feature."));
1652 mch_errmsg("\n");
1653 mch_exit(2);
1654#endif
1655 }
1656
1657 if (STRNICMP(initstr, "ex", 2) == 0)
1658 {
1659 if (STRNICMP(initstr + 2, "im", 2) == 0)
1660 exmode_active = EXMODE_VIM;
1661 else
1662 exmode_active = EXMODE_NORMAL;
1663 change_compatible(TRUE); /* set 'compatible' */
1664 }
1665}
1666
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001667/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00001668 * Get the name of the display, before gui_prepare() removes it from
1669 * argv[]. Used for the xterm-clipboard display.
1670 *
Bram Moolenaar78e17622007-08-30 10:26:19 +00001671 * Also find the --server... arguments and --socketid and --windowid
Bram Moolenaar58d98232005-07-23 22:25:46 +00001672 */
Bram Moolenaar58d98232005-07-23 22:25:46 +00001673 static void
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001674early_arg_scan(parmp)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00001675 mparm_T *parmp UNUSED;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001676{
Bram Moolenaar03005972008-11-20 13:12:36 +00001677#if defined(FEAT_XCLIPBOARD) || defined(FEAT_CLIENTSERVER) \
1678 || !defined(FEAT_NETBEANS_INTG)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001679 int argc = parmp->argc;
1680 char **argv = parmp->argv;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001681 int i;
1682
1683 for (i = 1; i < argc; i++)
1684 {
1685 if (STRCMP(argv[i], "--") == 0)
1686 break;
1687# ifdef FEAT_XCLIPBOARD
1688 else if (STRICMP(argv[i], "-display") == 0
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001689# if defined(FEAT_GUI_GTK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001690 || STRICMP(argv[i], "--display") == 0
1691# endif
1692 )
1693 {
1694 if (i == argc - 1)
1695 mainerr_arg_missing((char_u *)argv[i]);
1696 xterm_display = argv[++i];
1697 }
1698# endif
1699# ifdef FEAT_CLIENTSERVER
1700 else if (STRICMP(argv[i], "--servername") == 0)
1701 {
1702 if (i == argc - 1)
1703 mainerr_arg_missing((char_u *)argv[i]);
1704 parmp->serverName_arg = (char_u *)argv[++i];
1705 }
Bram Moolenaareb94e552006-03-11 21:35:11 +00001706 else if (STRICMP(argv[i], "--serverlist") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001707 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001708 else if (STRNICMP(argv[i], "--remote", 8) == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001709 {
1710 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001711# ifdef FEAT_GUI
1712 if (strstr(argv[i], "-wait") != 0)
1713 /* don't fork() when starting the GUI to edit files ourself */
1714 gui.dofork = FALSE;
1715# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001716 }
1717# endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001718
1719# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1720# ifdef FEAT_GUI_W32
1721 else if (STRICMP(argv[i], "--windowid") == 0)
1722# else
Bram Moolenaar58d98232005-07-23 22:25:46 +00001723 else if (STRICMP(argv[i], "--socketid") == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001724# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001725 {
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001726 long_u id;
1727 int count;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001728
1729 if (i == argc - 1)
1730 mainerr_arg_missing((char_u *)argv[i]);
1731 if (STRNICMP(argv[i+1], "0x", 2) == 0)
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001732 count = sscanf(&(argv[i + 1][2]), SCANF_HEX_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001733 else
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001734 count = sscanf(argv[i + 1], SCANF_DECIMAL_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001735 if (count != 1)
1736 mainerr(ME_INVALID_ARG, (char_u *)argv[i]);
1737 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00001738# ifdef FEAT_GUI_W32
1739 win_socket_id = id;
1740# else
1741 gtk_socket_id = id;
1742# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001743 i++;
1744 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001745# endif
1746# ifdef FEAT_GUI_GTK
Bram Moolenaar58d98232005-07-23 22:25:46 +00001747 else if (STRICMP(argv[i], "--echo-wid") == 0)
1748 echo_wid_arg = TRUE;
1749# endif
Bram Moolenaar03005972008-11-20 13:12:36 +00001750# ifndef FEAT_NETBEANS_INTG
1751 else if (strncmp(argv[i], "-nb", (size_t)3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +02001752 {
1753 mch_errmsg(_("'-nb' cannot be used: not enabled at compile time\n"));
1754 mch_exit(2);
1755 }
Bram Moolenaar03005972008-11-20 13:12:36 +00001756# endif
1757
Bram Moolenaar58d98232005-07-23 22:25:46 +00001758 }
1759#endif
1760}
1761
1762/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001763 * Scan the command line arguments.
1764 */
1765 static void
1766command_line_scan(parmp)
1767 mparm_T *parmp;
1768{
1769 int argc = parmp->argc;
1770 char **argv = parmp->argv;
1771 int argv_idx; /* index in argv[n][] */
1772 int had_minmin = FALSE; /* found "--" argument */
1773 int want_argument; /* option argument with argument */
1774 int c;
Bram Moolenaar231334e2005-07-25 20:46:57 +00001775 char_u *p = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001776 long n;
1777
1778 --argc;
1779 ++argv;
1780 argv_idx = 1; /* active option letter is argv[0][argv_idx] */
1781 while (argc > 0)
1782 {
1783 /*
1784 * "+" or "+{number}" or "+/{pat}" or "+{command}" argument.
1785 */
1786 if (argv[0][0] == '+' && !had_minmin)
1787 {
1788 if (parmp->n_commands >= MAX_ARG_CMDS)
1789 mainerr(ME_EXTRA_CMD, NULL);
1790 argv_idx = -1; /* skip to next argument */
1791 if (argv[0][1] == NUL)
1792 parmp->commands[parmp->n_commands++] = (char_u *)"$";
1793 else
1794 parmp->commands[parmp->n_commands++] = (char_u *)&(argv[0][1]);
1795 }
1796
1797 /*
1798 * Optional argument.
1799 */
1800 else if (argv[0][0] == '-' && !had_minmin)
1801 {
1802 want_argument = FALSE;
1803 c = argv[0][argv_idx++];
1804#ifdef VMS
1805 /*
1806 * VMS only uses upper case command lines. Interpret "-X" as "-x"
1807 * and "-/X" as "-X".
1808 */
1809 if (c == '/')
1810 {
1811 c = argv[0][argv_idx++];
1812 c = TOUPPER_ASC(c);
1813 }
1814 else
1815 c = TOLOWER_ASC(c);
1816#endif
1817 switch (c)
1818 {
1819 case NUL: /* "vim -" read from stdin */
1820 /* "ex -" silent mode */
1821 if (exmode_active)
1822 silent_mode = TRUE;
1823 else
1824 {
1825 if (parmp->edit_type != EDIT_NONE)
1826 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1827 parmp->edit_type = EDIT_STDIN;
1828 read_cmd_fd = 2; /* read from stderr instead of stdin */
1829 }
1830 argv_idx = -1; /* skip to next argument */
1831 break;
1832
1833 case '-': /* "--" don't take any more option arguments */
1834 /* "--help" give help message */
1835 /* "--version" give version message */
1836 /* "--literal" take files literally */
1837 /* "--nofork" don't fork */
1838 /* "--noplugin[s]" skip plugins */
1839 /* "--cmd <cmd>" execute cmd before vimrc */
1840 if (STRICMP(argv[0] + argv_idx, "help") == 0)
1841 usage();
1842 else if (STRICMP(argv[0] + argv_idx, "version") == 0)
1843 {
1844 Columns = 80; /* need to init Columns */
1845 info_message = TRUE; /* use mch_msg(), not mch_errmsg() */
1846 list_version();
1847 msg_putchar('\n');
1848 msg_didout = FALSE;
1849 mch_exit(0);
1850 }
1851 else if (STRNICMP(argv[0] + argv_idx, "literal", 7) == 0)
1852 {
1853#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
1854 parmp->literal = TRUE;
1855#endif
1856 }
1857 else if (STRNICMP(argv[0] + argv_idx, "nofork", 6) == 0)
1858 {
1859#ifdef FEAT_GUI
1860 gui.dofork = FALSE; /* don't fork() when starting GUI */
1861#endif
1862 }
1863 else if (STRNICMP(argv[0] + argv_idx, "noplugin", 8) == 0)
1864 p_lpl = FALSE;
1865 else if (STRNICMP(argv[0] + argv_idx, "cmd", 3) == 0)
1866 {
1867 want_argument = TRUE;
1868 argv_idx += 3;
1869 }
Bram Moolenaaref94eec2009-11-11 13:22:11 +00001870 else if (STRNICMP(argv[0] + argv_idx, "startuptime", 11) == 0)
1871 {
1872 want_argument = TRUE;
1873 argv_idx += 11;
1874 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001875#ifdef FEAT_CLIENTSERVER
1876 else if (STRNICMP(argv[0] + argv_idx, "serverlist", 10) == 0)
1877 ; /* already processed -- no arg */
1878 else if (STRNICMP(argv[0] + argv_idx, "servername", 10) == 0
1879 || STRNICMP(argv[0] + argv_idx, "serversend", 10) == 0)
1880 {
1881 /* already processed -- snatch the following arg */
1882 if (argc > 1)
1883 {
1884 --argc;
1885 ++argv;
1886 }
1887 }
1888#endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001889#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1890# ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001891 else if (STRNICMP(argv[0] + argv_idx, "socketid", 8) == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001892# else
1893 else if (STRNICMP(argv[0] + argv_idx, "windowid", 8) == 0)
1894# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001895 {
1896 /* already processed -- snatch the following arg */
1897 if (argc > 1)
1898 {
1899 --argc;
1900 ++argv;
1901 }
1902 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001903#endif
1904#ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001905 else if (STRNICMP(argv[0] + argv_idx, "echo-wid", 8) == 0)
1906 {
1907 /* already processed, skip */
1908 }
1909#endif
1910 else
1911 {
1912 if (argv[0][argv_idx])
1913 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
1914 had_minmin = TRUE;
1915 }
1916 if (!want_argument)
1917 argv_idx = -1; /* skip to next argument */
1918 break;
1919
1920 case 'A': /* "-A" start in Arabic mode */
1921#ifdef FEAT_ARABIC
1922 set_option_value((char_u *)"arabic", 1L, NULL, 0);
1923#else
1924 mch_errmsg(_(e_noarabic));
1925 mch_exit(2);
1926#endif
1927 break;
1928
1929 case 'b': /* "-b" binary mode */
Bram Moolenaar231334e2005-07-25 20:46:57 +00001930 /* Needs to be effective before expanding file names, because
1931 * for Win32 this makes us edit a shortcut file itself,
1932 * instead of the file it links to. */
1933 set_options_bin(curbuf->b_p_bin, 1, 0);
1934 curbuf->b_p_bin = 1; /* binary file I/O */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001935 break;
1936
1937 case 'C': /* "-C" Compatible */
1938 change_compatible(TRUE);
1939 break;
1940
1941 case 'e': /* "-e" Ex mode */
1942 exmode_active = EXMODE_NORMAL;
1943 break;
1944
1945 case 'E': /* "-E" Improved Ex mode */
1946 exmode_active = EXMODE_VIM;
1947 break;
1948
1949 case 'f': /* "-f" GUI: run in foreground. Amiga: open
1950 window directly, not with newcli */
1951#ifdef FEAT_GUI
1952 gui.dofork = FALSE; /* don't fork() when starting GUI */
1953#endif
1954 break;
1955
1956 case 'g': /* "-g" start GUI */
1957 main_start_gui();
1958 break;
1959
1960 case 'F': /* "-F" start in Farsi mode: rl + fkmap set */
1961#ifdef FEAT_FKMAP
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00001962 p_fkmap = TRUE;
1963 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001964#else
1965 mch_errmsg(_(e_nofarsi));
1966 mch_exit(2);
1967#endif
1968 break;
1969
1970 case 'h': /* "-h" give help message */
1971#ifdef FEAT_GUI_GNOME
1972 /* Tell usage() to exit for "gvim". */
1973 gui.starting = FALSE;
1974#endif
1975 usage();
1976 break;
1977
1978 case 'H': /* "-H" start in Hebrew mode: rl + hkmap set */
1979#ifdef FEAT_RIGHTLEFT
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00001980 p_hkmap = TRUE;
1981 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001982#else
1983 mch_errmsg(_(e_nohebrew));
1984 mch_exit(2);
1985#endif
1986 break;
1987
1988 case 'l': /* "-l" lisp mode, 'lisp' and 'showmatch' on */
1989#ifdef FEAT_LISP
1990 set_option_value((char_u *)"lisp", 1L, NULL, 0);
1991 p_sm = TRUE;
1992#endif
1993 break;
1994
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001995 case 'M': /* "-M" no changes or writing of files */
1996 reset_modifiable();
1997 /* FALLTHROUGH */
1998
1999 case 'm': /* "-m" no writing of files */
2000 p_write = FALSE;
2001 break;
2002
2003 case 'y': /* "-y" easy mode */
2004#ifdef FEAT_GUI
2005 gui.starting = TRUE; /* start GUI a bit later */
2006#endif
2007 parmp->evim_mode = TRUE;
2008 break;
2009
2010 case 'N': /* "-N" Nocompatible */
2011 change_compatible(FALSE);
2012 break;
2013
2014 case 'n': /* "-n" no swap file */
Bram Moolenaar67c53842010-05-22 18:28:27 +02002015#ifdef FEAT_NETBEANS_INTG
2016 /* checking for "-nb", netbeans parameters */
2017 if (argv[0][argv_idx] == 'b')
2018 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002019 netbeansArg = argv[0];
2020 argv_idx = -1; /* skip to next argument */
2021 }
2022 else
2023#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002024 parmp->no_swap_file = TRUE;
2025 break;
2026
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002027 case 'p': /* "-p[N]" open N tab pages */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002028#ifdef TARGET_API_MAC_OSX
2029 /* For some reason on MacOS X, an argument like:
2030 -psn_0_10223617 is passed in when invoke from Finder
2031 or with the 'open' command */
2032 if (argv[0][argv_idx] == 's')
2033 {
2034 argv_idx = -1; /* bypass full -psn */
2035 main_start_gui();
2036 break;
2037 }
2038#endif
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002039#ifdef FEAT_WINDOWS
2040 /* default is 0: open window for each file */
2041 parmp->window_count = get_number_arg((char_u *)argv[0],
2042 &argv_idx, 0);
2043 parmp->window_layout = WIN_TABS;
2044#endif
2045 break;
2046
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002047 case 'o': /* "-o[N]" open N horizontal split windows */
2048#ifdef FEAT_WINDOWS
2049 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002050 parmp->window_count = get_number_arg((char_u *)argv[0],
2051 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002052 parmp->window_layout = WIN_HOR;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002053#endif
2054 break;
2055
2056 case 'O': /* "-O[N]" open N vertical split windows */
2057#if defined(FEAT_VERTSPLIT) && defined(FEAT_WINDOWS)
2058 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002059 parmp->window_count = get_number_arg((char_u *)argv[0],
2060 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002061 parmp->window_layout = WIN_VER;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002062#endif
2063 break;
2064
2065#ifdef FEAT_QUICKFIX
2066 case 'q': /* "-q" QuickFix mode */
2067 if (parmp->edit_type != EDIT_NONE)
2068 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2069 parmp->edit_type = EDIT_QF;
2070 if (argv[0][argv_idx]) /* "-q{errorfile}" */
2071 {
2072 parmp->use_ef = (char_u *)argv[0] + argv_idx;
2073 argv_idx = -1;
2074 }
2075 else if (argc > 1) /* "-q {errorfile}" */
2076 want_argument = TRUE;
2077 break;
2078#endif
2079
2080 case 'R': /* "-R" readonly mode */
2081 readonlymode = TRUE;
2082 curbuf->b_p_ro = TRUE;
2083 p_uc = 10000; /* don't update very often */
2084 break;
2085
2086 case 'r': /* "-r" recovery mode */
2087 case 'L': /* "-L" recovery mode */
2088 recoverymode = 1;
2089 break;
2090
2091 case 's':
2092 if (exmode_active) /* "-s" silent (batch) mode */
2093 silent_mode = TRUE;
2094 else /* "-s {scriptin}" read from script file */
2095 want_argument = TRUE;
2096 break;
2097
2098 case 't': /* "-t {tag}" or "-t{tag}" jump to tag */
2099 if (parmp->edit_type != EDIT_NONE)
2100 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2101 parmp->edit_type = EDIT_TAG;
2102 if (argv[0][argv_idx]) /* "-t{tag}" */
2103 {
2104 parmp->tagname = (char_u *)argv[0] + argv_idx;
2105 argv_idx = -1;
2106 }
2107 else /* "-t {tag}" */
2108 want_argument = TRUE;
2109 break;
2110
2111#ifdef FEAT_EVAL
2112 case 'D': /* "-D" Debugging */
2113 parmp->use_debug_break_level = 9999;
2114 break;
2115#endif
2116#ifdef FEAT_DIFF
2117 case 'd': /* "-d" 'diff' */
2118# ifdef AMIGA
2119 /* check for "-dev {device}" */
2120 if (argv[0][argv_idx] == 'e' && argv[0][argv_idx + 1] == 'v')
2121 want_argument = TRUE;
2122 else
2123# endif
2124 parmp->diff_mode = TRUE;
2125 break;
2126#endif
2127 case 'V': /* "-V{N}" Verbose level */
2128 /* default is 10: a little bit verbose */
2129 p_verbose = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2130 if (argv[0][argv_idx] != NUL)
2131 {
2132 set_option_value((char_u *)"verbosefile", 0L,
2133 (char_u *)argv[0] + argv_idx, 0);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002134 argv_idx = (int)STRLEN(argv[0]);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002135 }
2136 break;
2137
2138 case 'v': /* "-v" Vi-mode (as if called "vi") */
2139 exmode_active = 0;
2140#ifdef FEAT_GUI
2141 gui.starting = FALSE; /* don't start GUI */
2142#endif
2143 break;
2144
2145 case 'w': /* "-w{number}" set window height */
2146 /* "-w {scriptout}" write to script */
2147 if (vim_isdigit(((char_u *)argv[0])[argv_idx]))
2148 {
2149 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2150 set_option_value((char_u *)"window", n, NULL, 0);
2151 break;
2152 }
2153 want_argument = TRUE;
2154 break;
2155
2156#ifdef FEAT_CRYPT
2157 case 'x': /* "-x" encrypted reading/writing of files */
2158 parmp->ask_for_key = TRUE;
2159 break;
2160#endif
2161
2162 case 'X': /* "-X" don't connect to X server */
2163#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
2164 x_no_connect = TRUE;
2165#endif
2166 break;
2167
2168 case 'Z': /* "-Z" restricted mode */
2169 restricted = TRUE;
2170 break;
2171
2172 case 'c': /* "-c{command}" or "-c {command}" execute
2173 command */
2174 if (argv[0][argv_idx] != NUL)
2175 {
2176 if (parmp->n_commands >= MAX_ARG_CMDS)
2177 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar231334e2005-07-25 20:46:57 +00002178 parmp->commands[parmp->n_commands++] = (char_u *)argv[0]
2179 + argv_idx;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002180 argv_idx = -1;
2181 break;
2182 }
2183 /*FALLTHROUGH*/
2184 case 'S': /* "-S {file}" execute Vim script */
2185 case 'i': /* "-i {viminfo}" use for viminfo */
2186#ifndef FEAT_DIFF
2187 case 'd': /* "-d {device}" device (for Amiga) */
2188#endif
2189 case 'T': /* "-T {terminal}" terminal name */
2190 case 'u': /* "-u {vimrc}" vim inits file */
2191 case 'U': /* "-U {gvimrc}" gvim inits file */
2192 case 'W': /* "-W {scriptout}" overwrite */
2193#ifdef FEAT_GUI_W32
2194 case 'P': /* "-P {parent title}" MDI parent */
2195#endif
2196 want_argument = TRUE;
2197 break;
2198
2199 default:
2200 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
2201 }
2202
2203 /*
2204 * Handle option arguments with argument.
2205 */
2206 if (want_argument)
2207 {
2208 /*
2209 * Check for garbage immediately after the option letter.
2210 */
2211 if (argv[0][argv_idx] != NUL)
2212 mainerr(ME_GARBAGE, (char_u *)argv[0]);
2213
2214 --argc;
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002215 if (argc < 1 && c != 'S') /* -S has an optional argument */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002216 mainerr_arg_missing((char_u *)argv[0]);
2217 ++argv;
2218 argv_idx = -1;
2219
2220 switch (c)
2221 {
2222 case 'c': /* "-c {command}" execute command */
2223 case 'S': /* "-S {file}" execute Vim script */
2224 if (parmp->n_commands >= MAX_ARG_CMDS)
2225 mainerr(ME_EXTRA_CMD, NULL);
2226 if (c == 'S')
2227 {
2228 char *a;
2229
2230 if (argc < 1)
2231 /* "-S" without argument: use default session file
2232 * name. */
2233 a = SESSION_FILE;
2234 else if (argv[0][0] == '-')
2235 {
2236 /* "-S" followed by another option: use default
2237 * session file name. */
2238 a = SESSION_FILE;
2239 ++argc;
2240 --argv;
2241 }
2242 else
2243 a = argv[0];
2244 p = alloc((unsigned)(STRLEN(a) + 4));
2245 if (p == NULL)
2246 mch_exit(2);
2247 sprintf((char *)p, "so %s", a);
2248 parmp->cmds_tofree[parmp->n_commands] = TRUE;
2249 parmp->commands[parmp->n_commands++] = p;
2250 }
2251 else
Bram Moolenaar231334e2005-07-25 20:46:57 +00002252 parmp->commands[parmp->n_commands++] =
2253 (char_u *)argv[0];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002254 break;
2255
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002256 case '-':
2257 if (argv[-1][2] == 'c')
2258 {
2259 /* "--cmd {command}" execute command */
2260 if (parmp->n_pre_commands >= MAX_ARG_CMDS)
2261 mainerr(ME_EXTRA_CMD, NULL);
2262 parmp->pre_commands[parmp->n_pre_commands++] =
Bram Moolenaar231334e2005-07-25 20:46:57 +00002263 (char_u *)argv[0];
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002264 }
2265 /* "--startuptime <file>" already handled */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002266 break;
2267
2268 /* case 'd': -d {device} is handled in mch_check_win() for the
2269 * Amiga */
2270
2271#ifdef FEAT_QUICKFIX
2272 case 'q': /* "-q {errorfile}" QuickFix mode */
2273 parmp->use_ef = (char_u *)argv[0];
2274 break;
2275#endif
2276
2277 case 'i': /* "-i {viminfo}" use for viminfo */
2278 use_viminfo = (char_u *)argv[0];
2279 break;
2280
2281 case 's': /* "-s {scriptin}" read from script file */
2282 if (scriptin[0] != NULL)
2283 {
2284scripterror:
2285 mch_errmsg(_("Attempt to open script file again: \""));
2286 mch_errmsg(argv[-1]);
2287 mch_errmsg(" ");
2288 mch_errmsg(argv[0]);
2289 mch_errmsg("\"\n");
2290 mch_exit(2);
2291 }
2292 if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL)
2293 {
2294 mch_errmsg(_("Cannot open for reading: \""));
2295 mch_errmsg(argv[0]);
2296 mch_errmsg("\"\n");
2297 mch_exit(2);
2298 }
2299 if (save_typebuf() == FAIL)
2300 mch_exit(2); /* out of memory */
2301 break;
2302
2303 case 't': /* "-t {tag}" */
2304 parmp->tagname = (char_u *)argv[0];
2305 break;
2306
2307 case 'T': /* "-T {terminal}" terminal name */
2308 /*
2309 * The -T term argument is always available and when
2310 * HAVE_TERMLIB is supported it overrides the environment
2311 * variable TERM.
2312 */
2313#ifdef FEAT_GUI
2314 if (term_is_gui((char_u *)argv[0]))
2315 gui.starting = TRUE; /* start GUI a bit later */
2316 else
2317#endif
2318 parmp->term = (char_u *)argv[0];
2319 break;
2320
2321 case 'u': /* "-u {vimrc}" vim inits file */
2322 parmp->use_vimrc = (char_u *)argv[0];
2323 break;
2324
2325 case 'U': /* "-U {gvimrc}" gvim inits file */
2326#ifdef FEAT_GUI
2327 use_gvimrc = (char_u *)argv[0];
2328#endif
2329 break;
2330
2331 case 'w': /* "-w {nr}" 'window' value */
2332 /* "-w {scriptout}" append to script file */
2333 if (vim_isdigit(*((char_u *)argv[0])))
2334 {
2335 argv_idx = 0;
2336 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2337 set_option_value((char_u *)"window", n, NULL, 0);
2338 argv_idx = -1;
2339 break;
2340 }
2341 /*FALLTHROUGH*/
2342 case 'W': /* "-W {scriptout}" overwrite script file */
2343 if (scriptout != NULL)
2344 goto scripterror;
2345 if ((scriptout = mch_fopen(argv[0],
2346 c == 'w' ? APPENDBIN : WRITEBIN)) == NULL)
2347 {
2348 mch_errmsg(_("Cannot open for script output: \""));
2349 mch_errmsg(argv[0]);
2350 mch_errmsg("\"\n");
2351 mch_exit(2);
2352 }
2353 break;
2354
2355#ifdef FEAT_GUI_W32
2356 case 'P': /* "-P {parent title}" MDI parent */
2357 gui_mch_set_parent(argv[0]);
2358 break;
2359#endif
2360 }
2361 }
2362 }
2363
2364 /*
2365 * File name argument.
2366 */
2367 else
2368 {
2369 argv_idx = -1; /* skip to next argument */
2370
2371 /* Check for only one type of editing. */
2372 if (parmp->edit_type != EDIT_NONE && parmp->edit_type != EDIT_FILE)
2373 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2374 parmp->edit_type = EDIT_FILE;
2375
2376#ifdef MSWIN
2377 /* Remember if the argument was a full path before changing
2378 * slashes to backslashes. */
2379 if (argv[0][0] != NUL && argv[0][1] == ':' && argv[0][2] == '\\')
2380 parmp->full_path = TRUE;
2381#endif
2382
2383 /* Add the file to the global argument list. */
2384 if (ga_grow(&global_alist.al_ga, 1) == FAIL
2385 || (p = vim_strsave((char_u *)argv[0])) == NULL)
2386 mch_exit(2);
2387#ifdef FEAT_DIFF
2388 if (parmp->diff_mode && mch_isdir(p) && GARGCOUNT > 0
2389 && !mch_isdir(alist_name(&GARGLIST[0])))
2390 {
2391 char_u *r;
2392
2393 r = concat_fnames(p, gettail(alist_name(&GARGLIST[0])), TRUE);
2394 if (r != NULL)
2395 {
2396 vim_free(p);
2397 p = r;
2398 }
2399 }
2400#endif
2401#if defined(__CYGWIN32__) && !defined(WIN32)
2402 /*
2403 * If vim is invoked by non-Cygwin tools, convert away any
2404 * DOS paths, so things like .swp files are created correctly.
2405 * Look for evidence of non-Cygwin paths before we bother.
2406 * This is only for when using the Unix files.
2407 */
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002408 if (vim_strpbrk(p, "\\:") != NULL && !path_with_url(p))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002409 {
2410 char posix_path[PATH_MAX];
2411
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002412# if CYGWIN_VERSION_DLL_MAJOR >= 1007
2413 cygwin_conv_path(CCP_WIN_A_TO_POSIX, p, posix_path, PATH_MAX);
2414# else
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002415 cygwin_conv_to_posix_path(p, posix_path);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002416# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002417 vim_free(p);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002418 p = vim_strsave((char_u *)posix_path);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002419 if (p == NULL)
2420 mch_exit(2);
2421 }
2422#endif
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002423
2424#ifdef USE_FNAME_CASE
2425 /* Make the case of the file name match the actual file. */
2426 fname_case(p, 0);
2427#endif
2428
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002429 alist_add(&global_alist, p,
2430#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
Bram Moolenaar231334e2005-07-25 20:46:57 +00002431 parmp->literal ? 2 : 0 /* add buffer nr after exp. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002432#else
2433 2 /* add buffer number now and use curbuf */
2434#endif
2435 );
2436
2437#if defined(FEAT_MBYTE) && defined(WIN32)
2438 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002439 /* Remember this argument has been added to the argument list.
2440 * Needed when 'encoding' is changed. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002441 used_file_arg(argv[0], parmp->literal, parmp->full_path,
Bram Moolenaar688e5f72008-07-24 11:51:40 +00002442# ifdef FEAT_DIFF
2443 parmp->diff_mode
2444# else
2445 FALSE
2446# endif
2447 );
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002448 }
2449#endif
2450 }
2451
2452 /*
2453 * If there are no more letters after the current "-", go to next
2454 * argument. argv_idx is set to -1 when the current argument is to be
2455 * skipped.
2456 */
2457 if (argv_idx <= 0 || argv[0][argv_idx] == NUL)
2458 {
2459 --argc;
2460 ++argv;
2461 argv_idx = 1;
2462 }
2463 }
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002464
2465#ifdef FEAT_EVAL
2466 /* If there is a "+123" or "-c" command, set v:swapcommand to the first
2467 * one. */
2468 if (parmp->n_commands > 0)
2469 {
2470 p = alloc((unsigned)STRLEN(parmp->commands[0]) + 3);
2471 if (p != NULL)
2472 {
2473 sprintf((char *)p, ":%s\r", parmp->commands[0]);
2474 set_vim_var_string(VV_SWAPCOMMAND, p, -1);
2475 vim_free(p);
2476 }
2477 }
2478#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002479}
2480
2481/*
2482 * Print a warning if stdout is not a terminal.
2483 * When starting in Ex mode and commands come from a file, set Silent mode.
2484 */
2485 static void
2486check_tty(parmp)
2487 mparm_T *parmp;
2488{
2489 int input_isatty; /* is active input a terminal? */
2490
2491 input_isatty = mch_input_isatty();
2492 if (exmode_active)
2493 {
2494 if (!input_isatty)
2495 silent_mode = TRUE;
2496 }
2497 else if (parmp->want_full_screen && (!parmp->stdout_isatty || !input_isatty)
2498#ifdef FEAT_GUI
2499 /* don't want the delay when started from the desktop */
2500 && !gui.starting
2501#endif
2502 )
2503 {
2504#ifdef NBDEBUG
2505 /*
2506 * This shouldn't be necessary. But if I run netbeans with the log
2507 * output coming to the console and XOpenDisplay fails, I get vim
2508 * trying to start with input/output to my console tty. This fills my
2509 * input buffer so fast I can't even kill the process in under 2
Bram Moolenaar49325942007-05-10 19:19:59 +00002510 * minutes (and it beeps continuously the whole time :-)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002511 */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002512 if (netbeans_active() && (!parmp->stdout_isatty || !input_isatty))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002513 {
2514 mch_errmsg(_("Vim: Error: Failure to start gvim from NetBeans\n"));
2515 exit(1);
2516 }
2517#endif
2518 if (!parmp->stdout_isatty)
2519 mch_errmsg(_("Vim: Warning: Output is not to a terminal\n"));
2520 if (!input_isatty)
2521 mch_errmsg(_("Vim: Warning: Input is not from a terminal\n"));
2522 out_flush();
2523 if (scriptin[0] == NULL)
2524 ui_delay(2000L, TRUE);
2525 TIME_MSG("Warning delay");
2526 }
2527}
2528
2529/*
2530 * Read text from stdin.
2531 */
2532 static void
2533read_stdin()
2534{
2535 int i;
2536
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002537#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002538 /* When getting the ATTENTION prompt here, use a dialog */
2539 swap_exists_action = SEA_DIALOG;
2540#endif
2541 no_wait_return = TRUE;
2542 i = msg_didany;
2543 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002544 (void)open_buffer(TRUE, NULL, 0); /* create memfile and read file */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002545 no_wait_return = FALSE;
2546 msg_didany = i;
2547 TIME_MSG("reading stdin");
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002548#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002549 check_swap_exists_action();
2550#endif
2551#if !(defined(AMIGA) || defined(MACOS))
2552 /*
2553 * Close stdin and dup it from stderr. Required for GPM to work
2554 * properly, and for running external commands.
2555 * Is there any other system that cannot do this?
2556 */
2557 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00002558 ignored = dup(2);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002559#endif
2560}
2561
2562/*
2563 * Create the requested number of windows and edit buffers in them.
2564 * Also does recovery if "recoverymode" set.
2565 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002566 static void
2567create_windows(parmp)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002568 mparm_T *parmp UNUSED;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002569{
2570#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002571 int dorewind;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002572 int done = 0;
2573
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002574 /*
2575 * Create the number of windows that was requested.
2576 */
2577 if (parmp->window_count == -1) /* was not set */
2578 parmp->window_count = 1;
2579 if (parmp->window_count == 0)
2580 parmp->window_count = GARGCOUNT;
2581 if (parmp->window_count > 1)
2582 {
2583 /* Don't change the windows if there was a command in .vimrc that
2584 * already split some windows */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002585 if (parmp->window_layout == 0)
2586 parmp->window_layout = WIN_HOR;
2587 if (parmp->window_layout == WIN_TABS)
2588 {
2589 parmp->window_count = make_tabpages(parmp->window_count);
2590 TIME_MSG("making tab pages");
2591 }
2592 else if (firstwin->w_next == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002593 {
2594 parmp->window_count = make_windows(parmp->window_count,
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002595 parmp->window_layout == WIN_VER);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002596 TIME_MSG("making windows");
2597 }
2598 else
2599 parmp->window_count = win_count();
2600 }
2601 else
2602 parmp->window_count = 1;
2603#endif
2604
2605 if (recoverymode) /* do recover */
2606 {
2607 msg_scroll = TRUE; /* scroll message up */
2608 ml_recover();
2609 if (curbuf->b_ml.ml_mfp == NULL) /* failed */
2610 getout(1);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002611 do_modelines(0); /* do modelines */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002612 }
2613 else
2614 {
2615 /*
2616 * Open a buffer for windows that don't have one yet.
2617 * Commands in the .vimrc might have loaded a file or split the window.
2618 * Watch out for autocommands that delete a window.
2619 */
2620#ifdef FEAT_AUTOCMD
2621 /*
2622 * Don't execute Win/Buf Enter/Leave autocommands here
2623 */
2624 ++autocmd_no_enter;
2625 ++autocmd_no_leave;
2626#endif
2627#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002628 dorewind = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002629 while (done++ < 1000)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002630 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002631 if (dorewind)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002632 {
2633 if (parmp->window_layout == WIN_TABS)
2634 goto_tabpage(1);
2635 else
2636 curwin = firstwin;
2637 }
2638 else if (parmp->window_layout == WIN_TABS)
2639 {
2640 if (curtab->tp_next == NULL)
2641 break;
2642 goto_tabpage(0);
2643 }
2644 else
2645 {
2646 if (curwin->w_next == NULL)
2647 break;
2648 curwin = curwin->w_next;
2649 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002650 dorewind = FALSE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002651#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002652 curbuf = curwin->w_buffer;
2653 if (curbuf->b_ml.ml_mfp == NULL)
2654 {
2655#ifdef FEAT_FOLDING
2656 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2657 if (p_fdls >= 0)
2658 curwin->w_p_fdl = p_fdls;
2659#endif
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002660#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002661 /* When getting the ATTENTION prompt here, use a dialog */
2662 swap_exists_action = SEA_DIALOG;
2663#endif
2664 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002665
2666 /* create memfile, read file */
2667 (void)open_buffer(FALSE, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002668
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002669#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar84212822006-11-07 21:59:47 +00002670 if (swap_exists_action == SEA_QUIT)
2671 {
2672 if (got_int || only_one_window())
2673 {
2674 /* abort selected or quit and only one window */
2675 did_emsg = FALSE; /* avoid hit-enter prompt */
2676 getout(1);
2677 }
2678 /* We can't close the window, it would disturb what
2679 * happens next. Clear the file name and set the arg
2680 * index to -1 to delete it later. */
2681 setfname(curbuf, NULL, NULL, FALSE);
2682 curwin->w_arg_idx = -1;
2683 swap_exists_action = SEA_NONE;
2684 }
2685 else
2686 handle_swap_exists(NULL);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002687#endif
2688#ifdef FEAT_AUTOCMD
Bram Moolenaar89d40322006-08-29 15:30:07 +00002689 dorewind = TRUE; /* start again */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002690#endif
2691 }
2692#ifdef FEAT_WINDOWS
2693 ui_breakcheck();
2694 if (got_int)
2695 {
2696 (void)vgetc(); /* only break the file loading, not the rest */
2697 break;
2698 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002699 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002700#endif
2701#ifdef FEAT_WINDOWS
2702 if (parmp->window_layout == WIN_TABS)
2703 goto_tabpage(1);
2704 else
2705 curwin = firstwin;
2706 curbuf = curwin->w_buffer;
2707#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002708#ifdef FEAT_AUTOCMD
2709 --autocmd_no_enter;
2710 --autocmd_no_leave;
2711#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002712 }
2713}
2714
2715#ifdef FEAT_WINDOWS
2716 /*
2717 * If opened more than one window, start editing files in the other
2718 * windows. make_windows() has already opened the windows.
2719 */
2720 static void
2721edit_buffers(parmp)
2722 mparm_T *parmp;
2723{
2724 int arg_idx; /* index in argument list */
2725 int i;
Bram Moolenaar84212822006-11-07 21:59:47 +00002726 int advance = TRUE;
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002727 win_T *win;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002728
2729# ifdef FEAT_AUTOCMD
2730 /*
2731 * Don't execute Win/Buf Enter/Leave autocommands here
2732 */
2733 ++autocmd_no_enter;
2734 ++autocmd_no_leave;
2735# endif
Bram Moolenaar84212822006-11-07 21:59:47 +00002736
2737 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2738 if (curwin->w_arg_idx == -1)
2739 {
2740 win_close(curwin, TRUE);
2741 advance = FALSE;
2742 }
2743
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002744 arg_idx = 1;
2745 for (i = 1; i < parmp->window_count; ++i)
2746 {
Bram Moolenaar84212822006-11-07 21:59:47 +00002747 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2748 if (curwin->w_arg_idx == -1)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002749 {
Bram Moolenaar84212822006-11-07 21:59:47 +00002750 ++arg_idx;
2751 win_close(curwin, TRUE);
2752 advance = FALSE;
2753 continue;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002754 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002755
Bram Moolenaar84212822006-11-07 21:59:47 +00002756 if (advance)
2757 {
2758 if (parmp->window_layout == WIN_TABS)
2759 {
2760 if (curtab->tp_next == NULL) /* just checking */
2761 break;
2762 goto_tabpage(0);
2763 }
2764 else
2765 {
2766 if (curwin->w_next == NULL) /* just checking */
2767 break;
2768 win_enter(curwin->w_next, FALSE);
2769 }
2770 }
2771 advance = TRUE;
2772
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002773 /* Only open the file if there is no file in this window yet (that can
Bram Moolenaar84212822006-11-07 21:59:47 +00002774 * happen when .vimrc contains ":sall"). */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002775 if (curbuf == firstwin->w_buffer || curbuf->b_ffname == NULL)
2776 {
2777 curwin->w_arg_idx = arg_idx;
Bram Moolenaar84212822006-11-07 21:59:47 +00002778 /* Edit file from arg list, if there is one. When "Quit" selected
2779 * at the ATTENTION prompt close the window. */
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002780# ifdef HAS_SWAP_EXISTS_ACTION
2781 swap_exists_did_quit = FALSE;
2782# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002783 (void)do_ecmd(0, arg_idx < GARGCOUNT
2784 ? alist_name(&GARGLIST[arg_idx]) : NULL,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002785 NULL, NULL, ECMD_LASTL, ECMD_HIDE, curwin);
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002786# ifdef HAS_SWAP_EXISTS_ACTION
2787 if (swap_exists_did_quit)
Bram Moolenaar84212822006-11-07 21:59:47 +00002788 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002789 /* abort or quit selected */
Bram Moolenaar84212822006-11-07 21:59:47 +00002790 if (got_int || only_one_window())
2791 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002792 /* abort selected and only one window */
Bram Moolenaar84212822006-11-07 21:59:47 +00002793 did_emsg = FALSE; /* avoid hit-enter prompt */
2794 getout(1);
2795 }
2796 win_close(curwin, TRUE);
2797 advance = FALSE;
2798 }
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002799# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002800 if (arg_idx == GARGCOUNT - 1)
2801 arg_had_last = TRUE;
2802 ++arg_idx;
2803 }
2804 ui_breakcheck();
2805 if (got_int)
2806 {
2807 (void)vgetc(); /* only break the file loading, not the rest */
2808 break;
2809 }
2810 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002811
2812 if (parmp->window_layout == WIN_TABS)
2813 goto_tabpage(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002814# ifdef FEAT_AUTOCMD
2815 --autocmd_no_enter;
2816# endif
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002817
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002818 /* make the first window the current window */
2819 win = firstwin;
2820#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
2821 /* Avoid making a preview window the current window. */
2822 while (win->w_p_pvw)
2823 {
2824 win = win->w_next;
2825 if (win == NULL)
2826 {
2827 win = firstwin;
2828 break;
2829 }
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002830 }
2831#endif
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002832 win_enter(win, FALSE);
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002833
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002834# ifdef FEAT_AUTOCMD
2835 --autocmd_no_leave;
2836# endif
2837 TIME_MSG("editing files in windows");
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002838 if (parmp->window_count > 1 && parmp->window_layout != WIN_TABS)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002839 win_equal(curwin, FALSE, 'b'); /* adjust heights */
2840}
2841#endif /* FEAT_WINDOWS */
2842
2843/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002844 * Execute the commands from --cmd arguments "cmds[cnt]".
2845 */
2846 static void
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002847exe_pre_commands(parmp)
2848 mparm_T *parmp;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002849{
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002850 char_u **cmds = parmp->pre_commands;
2851 int cnt = parmp->n_pre_commands;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002852 int i;
2853
2854 if (cnt > 0)
2855 {
2856 curwin->w_cursor.lnum = 0; /* just in case.. */
2857 sourcing_name = (char_u *)_("pre-vimrc command line");
2858# ifdef FEAT_EVAL
2859 current_SID = SID_CMDARG;
2860# endif
2861 for (i = 0; i < cnt; ++i)
2862 do_cmdline_cmd(cmds[i]);
2863 sourcing_name = NULL;
2864# ifdef FEAT_EVAL
2865 current_SID = 0;
2866# endif
2867 TIME_MSG("--cmd commands");
2868 }
2869}
2870
2871/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002872 * Execute "+", "-c" and "-S" arguments.
2873 */
2874 static void
2875exe_commands(parmp)
2876 mparm_T *parmp;
2877{
2878 int i;
2879
2880 /*
2881 * We start commands on line 0, make "vim +/pat file" match a
2882 * pattern on line 1. But don't move the cursor when an autocommand
2883 * with g`" was used.
2884 */
2885 msg_scroll = TRUE;
2886 if (parmp->tagname == NULL && curwin->w_cursor.lnum <= 1)
2887 curwin->w_cursor.lnum = 0;
2888 sourcing_name = (char_u *)"command line";
2889#ifdef FEAT_EVAL
2890 current_SID = SID_CARG;
2891#endif
2892 for (i = 0; i < parmp->n_commands; ++i)
2893 {
2894 do_cmdline_cmd(parmp->commands[i]);
2895 if (parmp->cmds_tofree[i])
2896 vim_free(parmp->commands[i]);
2897 }
2898 sourcing_name = NULL;
2899#ifdef FEAT_EVAL
2900 current_SID = 0;
2901#endif
2902 if (curwin->w_cursor.lnum == 0)
2903 curwin->w_cursor.lnum = 1;
2904
2905 if (!exmode_active)
2906 msg_scroll = FALSE;
2907
2908#ifdef FEAT_QUICKFIX
2909 /* When started with "-q errorfile" jump to first error again. */
2910 if (parmp->edit_type == EDIT_QF)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002911 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002912#endif
2913 TIME_MSG("executing command arguments");
2914}
2915
2916/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002917 * Source startup scripts.
2918 */
2919 static void
2920source_startup_scripts(parmp)
2921 mparm_T *parmp;
2922{
2923 int i;
2924
2925 /*
2926 * For "evim" source evim.vim first of all, so that the user can overrule
2927 * any things he doesn't like.
2928 */
2929 if (parmp->evim_mode)
2930 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002931 (void)do_source((char_u *)EVIM_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002932 TIME_MSG("source evim file");
2933 }
2934
2935 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002936 * If -u argument given, use only the initializations from that file and
Bram Moolenaar58d98232005-07-23 22:25:46 +00002937 * nothing else.
2938 */
2939 if (parmp->use_vimrc != NULL)
2940 {
Bram Moolenaar231334e2005-07-25 20:46:57 +00002941 if (STRCMP(parmp->use_vimrc, "NONE") == 0
2942 || STRCMP(parmp->use_vimrc, "NORC") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002943 {
2944#ifdef FEAT_GUI
2945 if (use_gvimrc == NULL) /* don't load gvimrc either */
2946 use_gvimrc = parmp->use_vimrc;
2947#endif
2948 if (parmp->use_vimrc[2] == 'N')
2949 p_lpl = FALSE; /* don't load plugins either */
2950 }
2951 else
2952 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002953 if (do_source(parmp->use_vimrc, FALSE, DOSO_NONE) != OK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002954 EMSG2(_("E282: Cannot read from \"%s\""), parmp->use_vimrc);
2955 }
2956 }
2957 else if (!silent_mode)
2958 {
2959#ifdef AMIGA
2960 struct Process *proc = (struct Process *)FindTask(0L);
2961 APTR save_winptr = proc->pr_WindowPtr;
2962
2963 /* Avoid a requester here for a volume that doesn't exist. */
2964 proc->pr_WindowPtr = (APTR)-1L;
2965#endif
2966
2967 /*
2968 * Get system wide defaults, if the file name is defined.
2969 */
2970#ifdef SYS_VIMRC_FILE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002971 (void)do_source((char_u *)SYS_VIMRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002972#endif
Bram Moolenaar1056d982006-03-09 22:37:52 +00002973#ifdef MACOS_X
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002974 (void)do_source((char_u *)"$VIMRUNTIME/macmap.vim", FALSE, DOSO_NONE);
Bram Moolenaar1056d982006-03-09 22:37:52 +00002975#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00002976
2977 /*
2978 * Try to read initialization commands from the following places:
2979 * - environment variable VIMINIT
2980 * - user vimrc file (s:.vimrc for Amiga, ~/.vimrc otherwise)
2981 * - second user vimrc file ($VIM/.vimrc for Dos)
2982 * - environment variable EXINIT
2983 * - user exrc file (s:.exrc for Amiga, ~/.exrc otherwise)
2984 * - second user exrc file ($VIM/.exrc for Dos)
2985 * The first that exists is used, the rest is ignored.
2986 */
2987 if (process_env((char_u *)"VIMINIT", TRUE) != OK)
2988 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002989 if (do_source((char_u *)USR_VIMRC_FILE, TRUE, DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00002990#ifdef USR_VIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002991 && do_source((char_u *)USR_VIMRC_FILE2, TRUE,
2992 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00002993#endif
2994#ifdef USR_VIMRC_FILE3
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002995 && do_source((char_u *)USR_VIMRC_FILE3, TRUE,
2996 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00002997#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +02002998#ifdef USR_VIMRC_FILE4
2999 && do_source((char_u *)USR_VIMRC_FILE4, TRUE,
3000 DOSO_VIMRC) == FAIL
3001#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003002 && process_env((char_u *)"EXINIT", FALSE) == FAIL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003003 && do_source((char_u *)USR_EXRC_FILE, FALSE, DOSO_NONE) == FAIL)
Bram Moolenaar58d98232005-07-23 22:25:46 +00003004 {
3005#ifdef USR_EXRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003006 (void)do_source((char_u *)USR_EXRC_FILE2, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003007#endif
3008 }
3009 }
3010
3011 /*
3012 * Read initialization commands from ".vimrc" or ".exrc" in current
3013 * directory. This is only done if the 'exrc' option is set.
3014 * Because of security reasons we disallow shell and write commands
3015 * now, except for unix if the file is owned by the user or 'secure'
3016 * option has been reset in environment of global ".exrc" or ".vimrc".
3017 * Only do this if VIMRC_FILE is not the same as USR_VIMRC_FILE or
3018 * SYS_VIMRC_FILE.
3019 */
3020 if (p_exrc)
3021 {
3022#if defined(UNIX) || defined(VMS)
3023 /* If ".vimrc" file is not owned by user, set 'secure' mode. */
3024 if (!file_owned(VIMRC_FILE))
3025#endif
3026 secure = p_secure;
3027
3028 i = FAIL;
3029 if (fullpathcmp((char_u *)USR_VIMRC_FILE,
3030 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3031#ifdef USR_VIMRC_FILE2
3032 && fullpathcmp((char_u *)USR_VIMRC_FILE2,
3033 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3034#endif
3035#ifdef USR_VIMRC_FILE3
3036 && fullpathcmp((char_u *)USR_VIMRC_FILE3,
3037 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3038#endif
3039#ifdef SYS_VIMRC_FILE
3040 && fullpathcmp((char_u *)SYS_VIMRC_FILE,
3041 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3042#endif
3043 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003044 i = do_source((char_u *)VIMRC_FILE, TRUE, DOSO_VIMRC);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003045
3046 if (i == FAIL)
3047 {
3048#if defined(UNIX) || defined(VMS)
3049 /* if ".exrc" is not owned by user set 'secure' mode */
3050 if (!file_owned(EXRC_FILE))
3051 secure = p_secure;
3052 else
3053 secure = 0;
3054#endif
3055 if ( fullpathcmp((char_u *)USR_EXRC_FILE,
3056 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3057#ifdef USR_EXRC_FILE2
3058 && fullpathcmp((char_u *)USR_EXRC_FILE2,
3059 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3060#endif
3061 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003062 (void)do_source((char_u *)EXRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003063 }
3064 }
3065 if (secure == 2)
3066 need_wait_return = TRUE;
3067 secure = 0;
3068#ifdef AMIGA
3069 proc->pr_WindowPtr = save_winptr;
3070#endif
3071 }
3072 TIME_MSG("sourcing vimrc file(s)");
3073}
3074
3075/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003076 * Setup to start using the GUI. Exit with an error when not available.
3077 */
3078 static void
3079main_start_gui()
3080{
3081#ifdef FEAT_GUI
3082 gui.starting = TRUE; /* start GUI a bit later */
3083#else
3084 mch_errmsg(_(e_nogvim));
3085 mch_errmsg("\n");
3086 mch_exit(2);
3087#endif
3088}
3089
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003090#endif /* NO_VIM_MAIN */
3091
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003092/*
Bram Moolenaar49325942007-05-10 19:19:59 +00003093 * Get an environment variable, and execute it as Ex commands.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003094 * Returns FAIL if the environment variable was not executed, OK otherwise.
3095 */
3096 int
3097process_env(env, is_viminit)
3098 char_u *env;
3099 int is_viminit; /* when TRUE, called for VIMINIT */
3100{
3101 char_u *initstr;
3102 char_u *save_sourcing_name;
3103 linenr_T save_sourcing_lnum;
3104#ifdef FEAT_EVAL
3105 scid_T save_sid;
3106#endif
3107
3108 if ((initstr = mch_getenv(env)) != NULL && *initstr != NUL)
3109 {
3110 if (is_viminit)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003111 vimrc_found(NULL, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003112 save_sourcing_name = sourcing_name;
3113 save_sourcing_lnum = sourcing_lnum;
3114 sourcing_name = env;
3115 sourcing_lnum = 0;
3116#ifdef FEAT_EVAL
3117 save_sid = current_SID;
3118 current_SID = SID_ENV;
3119#endif
3120 do_cmdline_cmd(initstr);
3121 sourcing_name = save_sourcing_name;
3122 sourcing_lnum = save_sourcing_lnum;
3123#ifdef FEAT_EVAL
3124 current_SID = save_sid;;
3125#endif
3126 return OK;
3127 }
3128 return FAIL;
3129}
3130
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003131#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003132/*
3133 * Return TRUE if we are certain the user owns the file "fname".
3134 * Used for ".vimrc" and ".exrc".
3135 * Use both stat() and lstat() for extra security.
3136 */
3137 static int
3138file_owned(fname)
3139 char *fname;
3140{
3141 struct stat s;
3142# ifdef UNIX
3143 uid_t uid = getuid();
3144# else /* VMS */
3145 uid_t uid = ((getgid() << 16) | getuid());
3146# endif
3147
3148 return !(mch_stat(fname, &s) != 0 || s.st_uid != uid
3149# ifdef HAVE_LSTAT
3150 || mch_lstat(fname, &s) != 0 || s.st_uid != uid
3151# endif
3152 );
3153}
3154#endif
3155
3156/*
3157 * Give an error message main_errors["n"] and exit.
3158 */
3159 static void
3160mainerr(n, str)
3161 int n; /* one of the ME_ defines */
3162 char_u *str; /* extra argument or NULL */
3163{
3164#if defined(UNIX) || defined(__EMX__) || defined(VMS)
3165 reset_signals(); /* kill us with CTRL-C here, if you like */
3166#endif
3167
3168 mch_errmsg(longVersion);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003169 mch_errmsg("\n");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003170 mch_errmsg(_(main_errors[n]));
3171 if (str != NULL)
3172 {
3173 mch_errmsg(": \"");
3174 mch_errmsg((char *)str);
3175 mch_errmsg("\"");
3176 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003177 mch_errmsg(_("\nMore info with: \"vim -h\"\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003178
3179 mch_exit(1);
3180}
3181
3182 void
3183mainerr_arg_missing(str)
3184 char_u *str;
3185{
3186 mainerr(ME_ARG_MISSING, str);
3187}
3188
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003189#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003190/*
3191 * print a message with three spaces prepended and '\n' appended.
3192 */
3193 static void
3194main_msg(s)
3195 char *s;
3196{
3197 mch_msg(" ");
3198 mch_msg(s);
3199 mch_msg("\n");
3200}
3201
3202/*
3203 * Print messages for "vim -h" or "vim --help" and exit.
3204 */
3205 static void
3206usage()
3207{
3208 int i;
3209 static char *(use[]) =
3210 {
3211 N_("[file ..] edit specified file(s)"),
3212 N_("- read text from stdin"),
3213 N_("-t tag edit file where tag is defined"),
3214#ifdef FEAT_QUICKFIX
3215 N_("-q [errorfile] edit file with first error")
3216#endif
3217 };
3218
3219#if defined(UNIX) || defined(__EMX__) || defined(VMS)
3220 reset_signals(); /* kill us with CTRL-C here, if you like */
3221#endif
3222
3223 mch_msg(longVersion);
3224 mch_msg(_("\n\nusage:"));
3225 for (i = 0; ; ++i)
3226 {
3227 mch_msg(_(" vim [arguments] "));
3228 mch_msg(_(use[i]));
3229 if (i == (sizeof(use) / sizeof(char_u *)) - 1)
3230 break;
3231 mch_msg(_("\n or:"));
3232 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003233#ifdef VMS
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003234 mch_msg(_("\nWhere case is ignored prepend / to make flag upper case"));
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003235#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003236
3237 mch_msg(_("\n\nArguments:\n"));
3238 main_msg(_("--\t\t\tOnly file names after this"));
3239#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
3240 main_msg(_("--literal\t\tDon't expand wildcards"));
3241#endif
3242#ifdef FEAT_OLE
3243 main_msg(_("-register\t\tRegister this gvim for OLE"));
3244 main_msg(_("-unregister\t\tUnregister gvim for OLE"));
3245#endif
3246#ifdef FEAT_GUI
3247 main_msg(_("-g\t\t\tRun using GUI (like \"gvim\")"));
3248 main_msg(_("-f or --nofork\tForeground: Don't fork when starting GUI"));
3249#endif
3250 main_msg(_("-v\t\t\tVi mode (like \"vi\")"));
3251 main_msg(_("-e\t\t\tEx mode (like \"ex\")"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003252 main_msg(_("-E\t\t\tImproved Ex mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003253 main_msg(_("-s\t\t\tSilent (batch) mode (only for \"ex\")"));
3254#ifdef FEAT_DIFF
3255 main_msg(_("-d\t\t\tDiff mode (like \"vimdiff\")"));
3256#endif
3257 main_msg(_("-y\t\t\tEasy mode (like \"evim\", modeless)"));
3258 main_msg(_("-R\t\t\tReadonly mode (like \"view\")"));
3259 main_msg(_("-Z\t\t\tRestricted mode (like \"rvim\")"));
3260 main_msg(_("-m\t\t\tModifications (writing files) not allowed"));
3261 main_msg(_("-M\t\t\tModifications in text not allowed"));
3262 main_msg(_("-b\t\t\tBinary mode"));
3263#ifdef FEAT_LISP
3264 main_msg(_("-l\t\t\tLisp mode"));
3265#endif
3266 main_msg(_("-C\t\t\tCompatible with Vi: 'compatible'"));
3267 main_msg(_("-N\t\t\tNot fully Vi compatible: 'nocompatible'"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003268 main_msg(_("-V[N][fname]\t\tBe verbose [level N] [log messages to fname]"));
3269#ifdef FEAT_EVAL
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003270 main_msg(_("-D\t\t\tDebugging mode"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003271#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003272 main_msg(_("-n\t\t\tNo swap file, use memory only"));
3273 main_msg(_("-r\t\t\tList swap files and exit"));
3274 main_msg(_("-r (with file name)\tRecover crashed session"));
3275 main_msg(_("-L\t\t\tSame as -r"));
3276#ifdef AMIGA
3277 main_msg(_("-f\t\t\tDon't use newcli to open window"));
3278 main_msg(_("-dev <device>\t\tUse <device> for I/O"));
3279#endif
3280#ifdef FEAT_ARABIC
3281 main_msg(_("-A\t\t\tstart in Arabic mode"));
3282#endif
3283#ifdef FEAT_RIGHTLEFT
3284 main_msg(_("-H\t\t\tStart in Hebrew mode"));
3285#endif
3286#ifdef FEAT_FKMAP
3287 main_msg(_("-F\t\t\tStart in Farsi mode"));
3288#endif
3289 main_msg(_("-T <terminal>\tSet terminal type to <terminal>"));
3290 main_msg(_("-u <vimrc>\t\tUse <vimrc> instead of any .vimrc"));
3291#ifdef FEAT_GUI
3292 main_msg(_("-U <gvimrc>\t\tUse <gvimrc> instead of any .gvimrc"));
3293#endif
3294 main_msg(_("--noplugin\t\tDon't load plugin scripts"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003295#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003296 main_msg(_("-p[N]\t\tOpen N tab pages (default: one for each file)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003297 main_msg(_("-o[N]\t\tOpen N windows (default: one for each file)"));
3298 main_msg(_("-O[N]\t\tLike -o but split vertically"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003299#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003300 main_msg(_("+\t\t\tStart at end of file"));
3301 main_msg(_("+<lnum>\t\tStart at line <lnum>"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003302 main_msg(_("--cmd <command>\tExecute <command> before loading any vimrc file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003303 main_msg(_("-c <command>\t\tExecute <command> after loading the first file"));
3304 main_msg(_("-S <session>\t\tSource file <session> after loading the first file"));
3305 main_msg(_("-s <scriptin>\tRead Normal mode commands from file <scriptin>"));
3306 main_msg(_("-w <scriptout>\tAppend all typed commands to file <scriptout>"));
3307 main_msg(_("-W <scriptout>\tWrite all typed commands to file <scriptout>"));
3308#ifdef FEAT_CRYPT
3309 main_msg(_("-x\t\t\tEdit encrypted files"));
3310#endif
3311#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
3312# if defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)
3313 main_msg(_("-display <display>\tConnect vim to this particular X-server"));
3314# endif
3315 main_msg(_("-X\t\t\tDo not connect to X server"));
3316#endif
3317#ifdef FEAT_CLIENTSERVER
3318 main_msg(_("--remote <files>\tEdit <files> in a Vim server if possible"));
3319 main_msg(_("--remote-silent <files> Same, don't complain if there is no server"));
3320 main_msg(_("--remote-wait <files> As --remote but wait for files to have been edited"));
3321 main_msg(_("--remote-wait-silent <files> Same, don't complain if there is no server"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003322# ifdef FEAT_WINDOWS
Bram Moolenaar82ad3242008-01-11 19:26:36 +00003323 main_msg(_("--remote-tab[-wait][-silent] <files> As --remote but use tab page per file"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003324# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003325 main_msg(_("--remote-send <keys>\tSend <keys> to a Vim server and exit"));
3326 main_msg(_("--remote-expr <expr>\tEvaluate <expr> in a Vim server and print result"));
3327 main_msg(_("--serverlist\t\tList available Vim server names and exit"));
3328 main_msg(_("--servername <name>\tSend to/become the Vim server <name>"));
3329#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003330#ifdef STARTUPTIME
Bram Moolenaar34ef52d2009-11-17 11:31:25 +00003331 main_msg(_("--startuptime <file>\tWrite startup timing messages to <file>"));
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003332#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003333#ifdef FEAT_VIMINFO
3334 main_msg(_("-i <viminfo>\t\tUse <viminfo> instead of .viminfo"));
3335#endif
3336 main_msg(_("-h or --help\tPrint Help (this message) and exit"));
3337 main_msg(_("--version\t\tPrint version information and exit"));
3338
3339#ifdef FEAT_GUI_X11
3340# ifdef FEAT_GUI_MOTIF
3341 mch_msg(_("\nArguments recognised by gvim (Motif version):\n"));
3342# else
3343# ifdef FEAT_GUI_ATHENA
3344# ifdef FEAT_GUI_NEXTAW
3345 mch_msg(_("\nArguments recognised by gvim (neXtaw version):\n"));
3346# else
3347 mch_msg(_("\nArguments recognised by gvim (Athena version):\n"));
3348# endif
3349# endif
3350# endif
3351 main_msg(_("-display <display>\tRun vim on <display>"));
3352 main_msg(_("-iconic\t\tStart vim iconified"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003353 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
3354 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
3355 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3356 main_msg(_("-boldfont <font>\tUse <font> for bold text"));
3357 main_msg(_("-italicfont <font>\tUse <font> for italic text"));
3358 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3359 main_msg(_("-borderwidth <width>\tUse a border width of <width> (also: -bw)"));
3360 main_msg(_("-scrollbarwidth <width> Use a scrollbar width of <width> (also: -sw)"));
3361# ifdef FEAT_GUI_ATHENA
3362 main_msg(_("-menuheight <height>\tUse a menu bar height of <height> (also: -mh)"));
3363# endif
3364 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3365 main_msg(_("+reverse\t\tDon't use reverse video (also: +rv)"));
3366 main_msg(_("-xrm <resource>\tSet the specified resource"));
3367#endif /* FEAT_GUI_X11 */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003368#ifdef FEAT_GUI_GTK
3369 mch_msg(_("\nArguments recognised by gvim (GTK+ version):\n"));
3370 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3371 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3372 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3373 main_msg(_("-display <display>\tRun vim on <display> (also: --display)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003374 main_msg(_("--role <role>\tSet a unique role to identify the main window"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003375 main_msg(_("--socketid <xid>\tOpen Vim inside another GTK widget"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003376 main_msg(_("--echo-wid\t\tMake gvim echo the Window ID on stdout"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003377#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003378#ifdef FEAT_GUI_W32
3379 main_msg(_("-P <parent title>\tOpen Vim inside parent application"));
Bram Moolenaar78e17622007-08-30 10:26:19 +00003380 main_msg(_("--windowid <HWND>\tOpen Vim inside another win32 widget"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003381#endif
3382
3383#ifdef FEAT_GUI_GNOME
3384 /* Gnome gives extra messages for --help if we continue, but not for -h. */
3385 if (gui.starting)
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003386 {
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003387 mch_msg("\n");
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003388 gui.dofork = FALSE;
3389 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003390 else
3391#endif
3392 mch_exit(0);
3393}
3394
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00003395#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003396/*
3397 * Check the result of the ATTENTION dialog:
3398 * When "Quit" selected, exit Vim.
3399 * When "Recover" selected, recover the file.
3400 */
3401 static void
3402check_swap_exists_action()
3403{
3404 if (swap_exists_action == SEA_QUIT)
3405 getout(1);
3406 handle_swap_exists(NULL);
3407}
3408#endif
3409
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003410#endif
3411
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003412#if defined(STARTUPTIME) || defined(PROTO)
3413static void time_diff __ARGS((struct timeval *then, struct timeval *now));
3414
3415static struct timeval prev_timeval;
3416
Bram Moolenaar3f269672009-11-03 11:11:11 +00003417# ifdef WIN3264
3418/*
3419 * Windows doesn't have gettimeofday(), although it does have struct timeval.
3420 */
3421 static int
3422gettimeofday(struct timeval *tv, char *dummy)
3423{
3424 long t = clock();
3425 tv->tv_sec = t / CLOCKS_PER_SEC;
3426 tv->tv_usec = (t - tv->tv_sec * CLOCKS_PER_SEC) * 1000000 / CLOCKS_PER_SEC;
3427 return 0;
3428}
3429# endif
3430
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003431/*
3432 * Save the previous time before doing something that could nest.
3433 * set "*tv_rel" to the time elapsed so far.
3434 */
3435 void
3436time_push(tv_rel, tv_start)
3437 void *tv_rel, *tv_start;
3438{
3439 *((struct timeval *)tv_rel) = prev_timeval;
3440 gettimeofday(&prev_timeval, NULL);
3441 ((struct timeval *)tv_rel)->tv_usec = prev_timeval.tv_usec
3442 - ((struct timeval *)tv_rel)->tv_usec;
3443 ((struct timeval *)tv_rel)->tv_sec = prev_timeval.tv_sec
3444 - ((struct timeval *)tv_rel)->tv_sec;
3445 if (((struct timeval *)tv_rel)->tv_usec < 0)
3446 {
3447 ((struct timeval *)tv_rel)->tv_usec += 1000000;
3448 --((struct timeval *)tv_rel)->tv_sec;
3449 }
3450 *(struct timeval *)tv_start = prev_timeval;
3451}
3452
3453/*
3454 * Compute the previous time after doing something that could nest.
3455 * Subtract "*tp" from prev_timeval;
3456 * Note: The arguments are (void *) to avoid trouble with systems that don't
3457 * have struct timeval.
3458 */
3459 void
3460time_pop(tp)
3461 void *tp; /* actually (struct timeval *) */
3462{
3463 prev_timeval.tv_usec -= ((struct timeval *)tp)->tv_usec;
3464 prev_timeval.tv_sec -= ((struct timeval *)tp)->tv_sec;
3465 if (prev_timeval.tv_usec < 0)
3466 {
3467 prev_timeval.tv_usec += 1000000;
3468 --prev_timeval.tv_sec;
3469 }
3470}
3471
3472 static void
3473time_diff(then, now)
3474 struct timeval *then;
3475 struct timeval *now;
3476{
3477 long usec;
3478 long msec;
3479
3480 usec = now->tv_usec - then->tv_usec;
3481 msec = (now->tv_sec - then->tv_sec) * 1000L + usec / 1000L,
3482 usec = usec % 1000L;
3483 fprintf(time_fd, "%03ld.%03ld", msec, usec >= 0 ? usec : usec + 1000L);
3484}
3485
3486 void
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003487time_msg(mesg, tv_start)
3488 char *mesg;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003489 void *tv_start; /* only for do_source: start time; actually
3490 (struct timeval *) */
3491{
3492 static struct timeval start;
3493 struct timeval now;
3494
3495 if (time_fd != NULL)
3496 {
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003497 if (strstr(mesg, "STARTING") != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003498 {
3499 gettimeofday(&start, NULL);
3500 prev_timeval = start;
3501 fprintf(time_fd, "\n\ntimes in msec\n");
3502 fprintf(time_fd, " clock self+sourced self: sourced script\n");
3503 fprintf(time_fd, " clock elapsed: other lines\n\n");
3504 }
3505 gettimeofday(&now, NULL);
3506 time_diff(&start, &now);
3507 if (((struct timeval *)tv_start) != NULL)
3508 {
3509 fprintf(time_fd, " ");
3510 time_diff(((struct timeval *)tv_start), &now);
3511 }
3512 fprintf(time_fd, " ");
3513 time_diff(&prev_timeval, &now);
3514 prev_timeval = now;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003515 fprintf(time_fd, ": %s\n", mesg);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003516 }
3517}
3518
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003519#endif
3520
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003521#if (defined(FEAT_CLIENTSERVER) && !defined(NO_VIM_MAIN)) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003522
3523/*
3524 * Common code for the X command server and the Win32 command server.
3525 */
3526
Bram Moolenaareb94e552006-03-11 21:35:11 +00003527static char_u *build_drop_cmd __ARGS((int filec, char **filev, int tabs, int sendReply));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003528
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003529/*
3530 * Do the client-server stuff, unless "--servername ''" was used.
3531 */
3532 static void
3533exec_on_server(parmp)
3534 mparm_T *parmp;
3535{
3536 if (parmp->serverName_arg == NULL || *parmp->serverName_arg != NUL)
3537 {
3538# ifdef WIN32
3539 /* Initialise the client/server messaging infrastructure. */
3540 serverInitMessaging();
3541# endif
3542
3543 /*
3544 * When a command server argument was found, execute it. This may
3545 * exit Vim when it was successful. Otherwise it's executed further
3546 * on. Remember the encoding used here in "serverStrEnc".
3547 */
3548 if (parmp->serverArg)
3549 {
3550 cmdsrv_main(&parmp->argc, parmp->argv,
3551 parmp->serverName_arg, &parmp->serverStr);
3552# ifdef FEAT_MBYTE
3553 parmp->serverStrEnc = vim_strsave(p_enc);
3554# endif
3555 }
3556
3557 /* If we're still running, get the name to register ourselves.
3558 * On Win32 can register right now, for X11 need to setup the
3559 * clipboard first, it's further down. */
3560 parmp->servername = serverMakeName(parmp->serverName_arg,
3561 parmp->argv[0]);
3562# ifdef WIN32
3563 if (parmp->servername != NULL)
3564 {
3565 serverSetName(parmp->servername);
3566 vim_free(parmp->servername);
3567 }
3568# endif
3569 }
3570}
3571
3572/*
3573 * Prepare for running as a Vim server.
3574 */
3575 static void
3576prepare_server(parmp)
3577 mparm_T *parmp;
3578{
3579# if defined(FEAT_X11)
3580 /*
3581 * Register for remote command execution with :serversend and --remote
3582 * unless there was a -X or a --servername '' on the command line.
3583 * Only register nongui-vim's with an explicit --servername argument.
Bram Moolenaar5f402312006-08-15 19:40:35 +00003584 * When running as root --servername is also required.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003585 */
3586 if (X_DISPLAY != NULL && parmp->servername != NULL && (
3587# ifdef FEAT_GUI
Bram Moolenaar5f402312006-08-15 19:40:35 +00003588 (gui.in_use
3589# ifdef UNIX
Bram Moolenaar311d9822007-02-27 15:48:28 +00003590 && getuid() != ROOT_UID
Bram Moolenaar5f402312006-08-15 19:40:35 +00003591# endif
3592 ) ||
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003593# endif
3594 parmp->serverName_arg != NULL))
3595 {
3596 (void)serverRegisterName(X_DISPLAY, parmp->servername);
3597 vim_free(parmp->servername);
3598 TIME_MSG("register server name");
3599 }
3600 else
3601 serverDelayedStartName = parmp->servername;
3602# endif
3603
3604 /*
3605 * Execute command ourselves if we're here because the send failed (or
3606 * else we would have exited above).
3607 */
3608 if (parmp->serverStr != NULL)
3609 {
3610 char_u *p;
3611
3612 server_to_input_buf(serverConvert(parmp->serverStrEnc,
3613 parmp->serverStr, &p));
3614 vim_free(p);
3615 }
3616}
3617
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003618 static void
3619cmdsrv_main(argc, argv, serverName_arg, serverStr)
3620 int *argc;
3621 char **argv;
3622 char_u *serverName_arg;
3623 char_u **serverStr;
3624{
3625 char_u *res;
3626 int i;
3627 char_u *sname;
3628 int ret;
3629 int didone = FALSE;
3630 int exiterr = 0;
3631 char **newArgV = argv + 1;
3632 int newArgC = 1,
3633 Argc = *argc;
3634 int argtype;
3635#define ARGTYPE_OTHER 0
3636#define ARGTYPE_EDIT 1
3637#define ARGTYPE_EDIT_WAIT 2
3638#define ARGTYPE_SEND 3
3639 int silent = FALSE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003640 int tabs = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003641# ifndef FEAT_X11
3642 HWND srv;
3643# else
3644 Window srv;
3645
3646 setup_term_clip();
3647# endif
3648
3649 sname = serverMakeName(serverName_arg, argv[0]);
3650 if (sname == NULL)
3651 return;
3652
3653 /*
3654 * Execute the command server related arguments and remove them
3655 * from the argc/argv array; We may have to return into main()
3656 */
3657 for (i = 1; i < Argc; i++)
3658 {
3659 res = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003660 if (STRCMP(argv[i], "--") == 0) /* end of option arguments */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003661 {
3662 for (; i < *argc; i++)
3663 {
3664 *newArgV++ = argv[i];
3665 newArgC++;
3666 }
3667 break;
3668 }
3669
Bram Moolenaareb94e552006-03-11 21:35:11 +00003670 if (STRICMP(argv[i], "--remote-send") == 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003671 argtype = ARGTYPE_SEND;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003672 else if (STRNICMP(argv[i], "--remote", 8) == 0)
3673 {
3674 char *p = argv[i] + 8;
3675
3676 argtype = ARGTYPE_EDIT;
3677 while (*p != NUL)
3678 {
3679 if (STRNICMP(p, "-wait", 5) == 0)
3680 {
3681 argtype = ARGTYPE_EDIT_WAIT;
3682 p += 5;
3683 }
3684 else if (STRNICMP(p, "-silent", 7) == 0)
3685 {
3686 silent = TRUE;
3687 p += 7;
3688 }
3689 else if (STRNICMP(p, "-tab", 4) == 0)
3690 {
3691 tabs = TRUE;
3692 p += 4;
3693 }
3694 else
3695 {
3696 argtype = ARGTYPE_OTHER;
3697 break;
3698 }
3699 }
3700 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003701 else
3702 argtype = ARGTYPE_OTHER;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003703
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003704 if (argtype != ARGTYPE_OTHER)
3705 {
3706 if (i == *argc - 1)
3707 mainerr_arg_missing((char_u *)argv[i]);
3708 if (argtype == ARGTYPE_SEND)
3709 {
3710 *serverStr = (char_u *)argv[i + 1];
3711 i++;
3712 }
3713 else
3714 {
3715 *serverStr = build_drop_cmd(*argc - i - 1, argv + i + 1,
Bram Moolenaareb94e552006-03-11 21:35:11 +00003716 tabs, argtype == ARGTYPE_EDIT_WAIT);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003717 if (*serverStr == NULL)
3718 {
3719 /* Probably out of memory, exit. */
3720 didone = TRUE;
3721 exiterr = 1;
3722 break;
3723 }
3724 Argc = i;
3725 }
3726# ifdef FEAT_X11
3727 if (xterm_dpy == NULL)
3728 {
3729 mch_errmsg(_("No display"));
3730 ret = -1;
3731 }
3732 else
3733 ret = serverSendToVim(xterm_dpy, sname, *serverStr,
3734 NULL, &srv, 0, 0, silent);
3735# else
3736 /* Win32 always works? */
3737 ret = serverSendToVim(sname, *serverStr, NULL, &srv, 0, silent);
3738# endif
3739 if (ret < 0)
3740 {
3741 if (argtype == ARGTYPE_SEND)
3742 {
3743 /* Failed to send, abort. */
3744 mch_errmsg(_(": Send failed.\n"));
3745 didone = TRUE;
3746 exiterr = 1;
3747 }
3748 else if (!silent)
3749 /* Let vim start normally. */
3750 mch_errmsg(_(": Send failed. Trying to execute locally\n"));
3751 break;
3752 }
3753
3754# ifdef FEAT_GUI_W32
3755 /* Guess that when the server name starts with "g" it's a GUI
3756 * server, which we can bring to the foreground here.
3757 * Foreground() in the server doesn't work very well. */
3758 if (argtype != ARGTYPE_SEND && TOUPPER_ASC(*sname) == 'G')
3759 SetForegroundWindow(srv);
3760# endif
3761
3762 /*
3763 * For --remote-wait: Wait until the server did edit each
3764 * file. Also detect that the server no longer runs.
3765 */
3766 if (ret >= 0 && argtype == ARGTYPE_EDIT_WAIT)
3767 {
3768 int numFiles = *argc - i - 1;
3769 int j;
3770 char_u *done = alloc(numFiles);
3771 char_u *p;
3772# ifdef FEAT_GUI_W32
3773 NOTIFYICONDATA ni;
3774 int count = 0;
3775 extern HWND message_window;
3776# endif
3777
3778 if (numFiles > 0 && argv[i + 1][0] == '+')
3779 /* Skip "+cmd" argument, don't wait for it to be edited. */
3780 --numFiles;
3781
3782# ifdef FEAT_GUI_W32
3783 ni.cbSize = sizeof(ni);
3784 ni.hWnd = message_window;
3785 ni.uID = 0;
3786 ni.uFlags = NIF_ICON|NIF_TIP;
3787 ni.hIcon = LoadIcon((HINSTANCE)GetModuleHandle(0), "IDR_VIM");
3788 sprintf(ni.szTip, _("%d of %d edited"), count, numFiles);
3789 Shell_NotifyIcon(NIM_ADD, &ni);
3790# endif
3791
3792 /* Wait for all files to unload in remote */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02003793 vim_memset(done, 0, numFiles);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003794 while (memchr(done, 0, numFiles) != NULL)
3795 {
3796# ifdef WIN32
3797 p = serverGetReply(srv, NULL, TRUE, TRUE);
3798 if (p == NULL)
3799 break;
3800# else
3801 if (serverReadReply(xterm_dpy, srv, &p, TRUE) < 0)
3802 break;
3803# endif
3804 j = atoi((char *)p);
3805 if (j >= 0 && j < numFiles)
3806 {
3807# ifdef FEAT_GUI_W32
3808 ++count;
3809 sprintf(ni.szTip, _("%d of %d edited"),
3810 count, numFiles);
3811 Shell_NotifyIcon(NIM_MODIFY, &ni);
3812# endif
3813 done[j] = 1;
3814 }
3815 }
3816# ifdef FEAT_GUI_W32
3817 Shell_NotifyIcon(NIM_DELETE, &ni);
3818# endif
3819 }
3820 }
3821 else if (STRICMP(argv[i], "--remote-expr") == 0)
3822 {
3823 if (i == *argc - 1)
3824 mainerr_arg_missing((char_u *)argv[i]);
3825# ifdef WIN32
3826 /* Win32 always works? */
3827 if (serverSendToVim(sname, (char_u *)argv[i + 1],
3828 &res, NULL, 1, FALSE) < 0)
3829# else
3830 if (xterm_dpy == NULL)
3831 mch_errmsg(_("No display: Send expression failed.\n"));
3832 else if (serverSendToVim(xterm_dpy, sname, (char_u *)argv[i + 1],
3833 &res, NULL, 1, 1, FALSE) < 0)
3834# endif
3835 {
3836 if (res != NULL && *res != NUL)
3837 {
3838 /* Output error from remote */
3839 mch_errmsg((char *)res);
3840 vim_free(res);
3841 res = NULL;
3842 }
3843 mch_errmsg(_(": Send expression failed.\n"));
3844 }
3845 }
3846 else if (STRICMP(argv[i], "--serverlist") == 0)
3847 {
3848# ifdef WIN32
3849 /* Win32 always works? */
3850 res = serverGetVimNames();
3851# else
3852 if (xterm_dpy != NULL)
3853 res = serverGetVimNames(xterm_dpy);
3854# endif
3855 if (called_emsg)
3856 mch_errmsg("\n");
3857 }
3858 else if (STRICMP(argv[i], "--servername") == 0)
3859 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +00003860 /* Already processed. Take it out of the command line */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003861 i++;
3862 continue;
3863 }
3864 else
3865 {
3866 *newArgV++ = argv[i];
3867 newArgC++;
3868 continue;
3869 }
3870 didone = TRUE;
3871 if (res != NULL && *res != NUL)
3872 {
3873 mch_msg((char *)res);
3874 if (res[STRLEN(res) - 1] != '\n')
3875 mch_msg("\n");
3876 }
3877 vim_free(res);
3878 }
3879
3880 if (didone)
3881 {
3882 display_errors(); /* display any collected messages */
3883 exit(exiterr); /* Mission accomplished - get out */
3884 }
3885
3886 /* Return back into main() */
3887 *argc = newArgC;
3888 vim_free(sname);
3889}
3890
3891/*
3892 * Build a ":drop" command to send to a Vim server.
3893 */
3894 static char_u *
Bram Moolenaareb94e552006-03-11 21:35:11 +00003895build_drop_cmd(filec, filev, tabs, sendReply)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003896 int filec;
3897 char **filev;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003898 int tabs; /* Use ":tab drop" instead of ":drop". */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003899 int sendReply;
3900{
3901 garray_T ga;
3902 int i;
3903 char_u *inicmd = NULL;
3904 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003905 char_u *cwd;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003906
3907 if (filec > 0 && filev[0][0] == '+')
3908 {
3909 inicmd = (char_u *)filev[0] + 1;
3910 filev++;
3911 filec--;
3912 }
3913 /* Check if we have at least one argument. */
3914 if (filec <= 0)
3915 mainerr_arg_missing((char_u *)filev[-1]);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003916
3917 /* Temporarily cd to the current directory to handle relative file names. */
Bram Moolenaard9462e32011-04-11 21:35:11 +02003918 cwd = alloc(MAXPATHL);
3919 if (cwd == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003920 return NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003921 if (mch_dirname(cwd, MAXPATHL) != OK)
3922 {
3923 vim_free(cwd);
3924 return NULL;
3925 }
3926 p = vim_strsave_escaped_ext(cwd,
Bram Moolenaar02b06312007-09-06 15:39:22 +00003927#ifdef BACKSLASH_IN_FILENAME
3928 "", /* rem_backslash() will tell what chars to escape */
3929#else
3930 PATH_ESC_CHARS,
3931#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +02003932 '\\', TRUE);
3933 vim_free(cwd);
3934 if (p == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003935 return NULL;
3936 ga_init2(&ga, 1, 100);
3937 ga_concat(&ga, (char_u *)"<C-\\><C-N>:cd ");
3938 ga_concat(&ga, p);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003939 vim_free(p);
Bram Moolenaareb94e552006-03-11 21:35:11 +00003940
3941 /* Call inputsave() so that a prompt for an encryption key works. */
3942 ga_concat(&ga, (char_u *)"<CR>:if exists('*inputsave')|call inputsave()|endif|");
3943 if (tabs)
3944 ga_concat(&ga, (char_u *)"tab ");
3945 ga_concat(&ga, (char_u *)"drop");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003946 for (i = 0; i < filec; i++)
3947 {
3948 /* On Unix the shell has already expanded the wildcards, don't want to
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003949 * do it again in the Vim server. On MS-Windows only escape
3950 * non-wildcard characters. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003951 p = vim_strsave_escaped((char_u *)filev[i],
3952#ifdef UNIX
3953 PATH_ESC_CHARS
3954#else
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003955 (char_u *)" \t%#"
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003956#endif
3957 );
3958 if (p == NULL)
3959 {
3960 vim_free(ga.ga_data);
3961 return NULL;
3962 }
3963 ga_concat(&ga, (char_u *)" ");
3964 ga_concat(&ga, p);
3965 vim_free(p);
3966 }
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003967 ga_concat(&ga, (char_u *)"|if exists('*inputrestore')|call inputrestore()|endif<CR>");
3968
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003969 /* The :drop commands goes to Insert mode when 'insertmode' is set, use
3970 * CTRL-\ CTRL-N again. */
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003971 ga_concat(&ga, (char_u *)"<C-\\><C-N>");
3972
3973 /* Switch back to the correct current directory (prior to temporary path
3974 * switch) unless 'autochdir' is set, in which case it will already be
3975 * correct after the :drop command. */
3976 ga_concat(&ga, (char_u *)":if !exists('+acd')||!&acd|cd -|endif<CR>");
3977
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003978 if (sendReply)
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003979 ga_concat(&ga, (char_u *)":call SetupRemoteReplies()<CR>");
3980 ga_concat(&ga, (char_u *)":");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003981 if (inicmd != NULL)
3982 {
3983 /* Can't use <CR> after "inicmd", because an "startinsert" would cause
3984 * the following commands to be inserted as text. Use a "|",
3985 * hopefully "inicmd" does allow this... */
3986 ga_concat(&ga, inicmd);
3987 ga_concat(&ga, (char_u *)"|");
3988 }
3989 /* Bring the window to the foreground, goto Insert mode when 'im' set and
3990 * clear command line. */
Bram Moolenaar567e4de2004-12-31 21:01:02 +00003991 ga_concat(&ga, (char_u *)"cal foreground()|if &im|star|en|redr|f<CR>");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003992 ga_append(&ga, NUL);
3993 return ga.ga_data;
3994}
3995
3996/*
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003997 * Make our basic server name: use the specified "arg" if given, otherwise use
3998 * the tail of the command "cmd" we were started with.
3999 * Return the name in allocated memory. This doesn't include a serial number.
4000 */
4001 static char_u *
4002serverMakeName(arg, cmd)
4003 char_u *arg;
4004 char *cmd;
4005{
4006 char_u *p;
4007
4008 if (arg != NULL && *arg != NUL)
4009 p = vim_strsave_up(arg);
4010 else
4011 {
4012 p = vim_strsave_up(gettail((char_u *)cmd));
4013 /* Remove .exe or .bat from the name. */
4014 if (p != NULL && vim_strchr(p, '.') != NULL)
4015 *vim_strchr(p, '.') = NUL;
4016 }
4017 return p;
4018}
4019#endif /* FEAT_CLIENTSERVER */
4020
4021#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
4022/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004023 * Replace termcodes such as <CR> and insert as key presses if there is room.
4024 */
4025 void
4026server_to_input_buf(str)
4027 char_u *str;
4028{
4029 char_u *ptr = NULL;
4030 char_u *cpo_save = p_cpo;
4031
4032 /* Set 'cpoptions' the way we want it.
4033 * B set - backslashes are *not* treated specially
4034 * k set - keycodes are *not* reverse-engineered
4035 * < unset - <Key> sequences *are* interpreted
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004036 * The last but one parameter of replace_termcodes() is TRUE so that the
4037 * <lt> sequence is recognised - needed for a real backslash.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004038 */
4039 p_cpo = (char_u *)"Bk";
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004040 str = replace_termcodes((char_u *)str, &ptr, FALSE, TRUE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004041 p_cpo = cpo_save;
4042
4043 if (*ptr != NUL) /* trailing CTRL-V results in nothing */
4044 {
4045 /*
4046 * Add the string to the input stream.
4047 * Can't use add_to_input_buf() here, we now have K_SPECIAL bytes.
4048 *
4049 * First clear typed characters from the typeahead buffer, there could
4050 * be half a mapping there. Then append to the existing string, so
4051 * that multiple commands from a client are concatenated.
4052 */
4053 if (typebuf.tb_maplen < typebuf.tb_len)
4054 del_typebuf(typebuf.tb_len - typebuf.tb_maplen, typebuf.tb_maplen);
4055 (void)ins_typebuf(str, REMAP_NONE, typebuf.tb_len, TRUE, FALSE);
4056
4057 /* Let input_available() know we inserted text in the typeahead
4058 * buffer. */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004059 typebuf_was_filled = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004060 }
4061 vim_free((char_u *)ptr);
4062}
4063
4064/*
4065 * Evaluate an expression that the client sent to a string.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004066 */
4067 char_u *
4068eval_client_expr_to_string(expr)
4069 char_u *expr;
4070{
4071 char_u *res;
4072 int save_dbl = debug_break_level;
4073 int save_ro = redir_off;
4074
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004075 /* Disable debugging, otherwise Vim hangs, waiting for "cont" to be
4076 * typed. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004077 debug_break_level = -1;
4078 redir_off = 0;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004079 /* Do not display error message, otherwise Vim hangs, waiting for "cont"
4080 * to be typed. Do generate errors so that try/catch works. */
4081 ++emsg_silent;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004082
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004083 res = eval_to_string(expr, NULL, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004084
4085 debug_break_level = save_dbl;
4086 redir_off = save_ro;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004087 --emsg_silent;
4088 if (emsg_silent < 0)
4089 emsg_silent = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004090
Bram Moolenaar63a121b2005-12-11 21:36:39 +00004091 /* A client can tell us to redraw, but not to display the cursor, so do
4092 * that here. */
4093 setcursor();
4094 out_flush();
4095#ifdef FEAT_GUI
4096 if (gui.in_use)
4097 gui_update_cursor(FALSE, FALSE);
4098#endif
4099
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004100 return res;
4101}
4102
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004103/*
4104 * If conversion is needed, convert "data" from "client_enc" to 'encoding' and
4105 * return an allocated string. Otherwise return "data".
4106 * "*tofree" is set to the result when it needs to be freed later.
4107 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004108 char_u *
4109serverConvert(client_enc, data, tofree)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004110 char_u *client_enc UNUSED;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004111 char_u *data;
4112 char_u **tofree;
4113{
4114 char_u *res = data;
4115
4116 *tofree = NULL;
4117# ifdef FEAT_MBYTE
4118 if (client_enc != NULL && p_enc != NULL)
4119 {
4120 vimconv_T vimconv;
4121
4122 vimconv.vc_type = CONV_NONE;
4123 if (convert_setup(&vimconv, client_enc, p_enc) != FAIL
4124 && vimconv.vc_type != CONV_NONE)
4125 {
4126 res = string_convert(&vimconv, data, NULL);
4127 if (res == NULL)
4128 res = data;
4129 else
4130 *tofree = res;
4131 }
4132 convert_setup(&vimconv, NULL, NULL);
4133 }
4134# endif
4135 return res;
4136}
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004137#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004138
4139/*
4140 * When FEAT_FKMAP is defined, also compile the Farsi source code.
4141 */
4142#if defined(FEAT_FKMAP) || defined(PROTO)
4143# include "farsi.c"
4144#endif
4145
4146/*
4147 * When FEAT_ARABIC is defined, also compile the Arabic source code.
4148 */
4149#if defined(FEAT_ARABIC) || defined(PROTO)
4150# include "arabic.c"
4151#endif