blob: 9c92f7a844fa6d76911188c0ef678e198d10ba82 [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 Moolenaar40e6a712010-05-16 22:32:54 +0200849 (void)blowfish_self_test();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000850 (void)get_crypt_key(TRUE, TRUE);
851 TIME_MSG("getting crypt key");
852 }
853#endif
854
855 no_wait_return = TRUE;
856
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000857 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000858 * Create the requested number of windows and edit buffers in them.
859 * Also does recovery if "recoverymode" set.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000860 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000861 create_windows(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000862 TIME_MSG("opening buffers");
863
Bram Moolenaar867a4b72007-03-18 20:51:46 +0000864#ifdef FEAT_EVAL
865 /* clear v:swapcommand */
866 set_vim_var_string(VV_SWAPCOMMAND, NULL, -1);
867#endif
868
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000869 /* Ex starts at last line of the file */
870 if (exmode_active)
871 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
872
873#ifdef FEAT_AUTOCMD
874 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
875 TIME_MSG("BufEnter autocommands");
876#endif
877 setpcmark();
878
879#ifdef FEAT_QUICKFIX
880 /*
881 * When started with "-q errorfile" jump to first error now.
882 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000883 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000884 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000885 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000886 TIME_MSG("jump to first error");
887 }
888#endif
889
890#ifdef FEAT_WINDOWS
891 /*
892 * If opened more than one window, start editing files in the other
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000893 * windows.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000894 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000895 edit_buffers(&params);
896#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000897
898#ifdef FEAT_DIFF
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000899 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000900 {
901 win_T *wp;
902
903 /* set options in each window for "vimdiff". */
904 for (wp = firstwin; wp != NULL; wp = wp->w_next)
905 diff_win_options(wp, TRUE);
906 }
907#endif
908
909 /*
910 * Shorten any of the filenames, but only when absolute.
911 */
912 shorten_fnames(FALSE);
913
914 /*
915 * Need to jump to the tag before executing the '-c command'.
916 * Makes "vim -c '/return' -t main" work.
917 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000918 if (params.tagname != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000919 {
Bram Moolenaar146522e2005-12-16 21:55:46 +0000920#if defined(HAS_SWAP_EXISTS_ACTION)
921 swap_exists_did_quit = FALSE;
922#endif
923
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000924 vim_snprintf((char *)IObuff, IOSIZE, "ta %s", params.tagname);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000925 do_cmdline_cmd(IObuff);
926 TIME_MSG("jumping to tag");
Bram Moolenaar146522e2005-12-16 21:55:46 +0000927
928#if defined(HAS_SWAP_EXISTS_ACTION)
929 /* If the user doesn't want to edit the file then we quit here. */
930 if (swap_exists_did_quit)
931 getout(1);
932#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000933 }
934
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000935 /* Execute any "+", "-c" and "-S" arguments. */
936 if (params.n_commands > 0)
937 exe_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000938
939 RedrawingDisabled = 0;
940 redraw_all_later(NOT_VALID);
941 no_wait_return = FALSE;
942 starting = 0;
943
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000944#ifdef FEAT_TERMRESPONSE
945 /* Requesting the termresponse is postponed until here, so that a "-c q"
946 * argument doesn't make it appear in the shell Vim was started from. */
947 may_req_termresponse();
948#endif
949
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000950 /* start in insert mode */
951 if (p_im)
952 need_start_insertmode = TRUE;
953
954#ifdef FEAT_AUTOCMD
955 apply_autocmds(EVENT_VIMENTER, NULL, NULL, FALSE, curbuf);
956 TIME_MSG("VimEnter autocommands");
957#endif
958
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200959#if defined(FEAT_EVAL) && defined(FEAT_CLIPBOARD)
960 /* Adjust default register name for "unnamed" in 'clipboard'. Can only be
961 * done after the clipboard is available and all initial commands that may
962 * modify the 'clipboard' setting have run; i.e. just before entering the
963 * main loop. */
964 {
965 int default_regname = 0;
966 adjust_clip_reg(&default_regname);
967 set_reg_var(default_regname);
968 }
969#endif
970
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000971#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
972 /* When a startup script or session file setup for diff'ing and
973 * scrollbind, sync the scrollbind now. */
974 if (curwin->w_p_diff && curwin->w_p_scb)
975 {
976 update_topline();
977 check_scrollbind((linenr_T)0, 0L);
978 TIME_MSG("diff scrollbinding");
979 }
980#endif
981
982#if defined(WIN3264) && !defined(FEAT_GUI_W32)
983 mch_set_winsize_now(); /* Allow winsize changes from now on */
984#endif
985
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000986#if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
987 /* When tab pages were created, may need to update the tab pages line and
988 * scrollbars. This is skipped while creating them. */
989 if (first_tabpage->tp_next != NULL)
990 {
991 out_flush();
992 gui_init_which_components(NULL);
993 gui_update_scrollbars(TRUE);
994 }
995 need_mouse_correct = TRUE;
996#endif
997
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000998 /* If ":startinsert" command used, stuff a dummy command to be able to
999 * call normal_cmd(), which will then start Insert mode. */
1000 if (restart_edit != 0)
Bram Moolenaarebefac62005-12-28 22:39:57 +00001001 stuffcharReadbuff(K_NOP);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001002
1003#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001004 if (netbeansArg != NULL && strncmp("-nb", netbeansArg, 3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +02001005 {
1006# ifdef FEAT_GUI
Bram Moolenaar173c9852010-09-29 17:27:01 +02001007# if !defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK) \
Bram Moolenaar67c53842010-05-22 18:28:27 +02001008 && !defined(FEAT_GUI_W32)
1009 if (gui.in_use)
1010 {
1011 mch_errmsg(_("netbeans is not supported with this GUI\n"));
1012 mch_exit(2);
1013 }
1014# endif
1015# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001016 /* Tell the client that it can start sending commands. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001017 netbeans_open(netbeansArg + 3, TRUE);
Bram Moolenaar67c53842010-05-22 18:28:27 +02001018 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001019#endif
1020
1021 TIME_MSG("before starting main loop");
1022
1023 /*
1024 * Call the main command loop. This never returns.
Bram Moolenaarbbc98db2012-02-12 01:55:55 +01001025 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001026 main_loop(FALSE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001027
1028 return 0;
1029}
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01001030#endif /* NO_VIM_MAIN */
Bram Moolenaar8866d272012-11-28 15:55:42 +01001031#endif /* PROTO */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001032
1033/*
1034 * Main loop: Execute Normal mode commands until exiting Vim.
1035 * Also used to handle commands in the command-line window, until the window
1036 * is closed.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001037 * Also used to handle ":visual" command after ":global": execute Normal mode
1038 * commands, return when entering Ex mode. "noexmode" is TRUE then.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001039 */
1040 void
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001041main_loop(cmdwin, noexmode)
1042 int cmdwin; /* TRUE when working in the command-line window */
1043 int noexmode; /* TRUE when return on entering Ex mode */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001044{
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001045 oparg_T oa; /* operator arguments */
1046 int previous_got_int = FALSE; /* "got_int" was TRUE */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001047#ifdef FEAT_CONCEAL
1048 linenr_T conceal_old_cursor_line = 0;
1049 linenr_T conceal_new_cursor_line = 0;
1050 int conceal_update_lines = FALSE;
1051#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001052
1053#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
1054 /* Setup to catch a terminating error from the X server. Just ignore
1055 * it, restore the state and continue. This might not always work
1056 * properly, but at least we don't exit unexpectedly when the X server
Bram Moolenaar2cd36962014-01-14 12:57:05 +01001057 * exits while Vim is running in a console. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001058 if (!cmdwin && !noexmode && SETJMP(x_jump_env))
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001059 {
1060 State = NORMAL;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001061 VIsual_active = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001062 got_int = TRUE;
1063 need_wait_return = FALSE;
1064 global_busy = FALSE;
1065 exmode_active = 0;
1066 skip_redraw = FALSE;
1067 RedrawingDisabled = 0;
1068 no_wait_return = 0;
Bram Moolenaar7701c242011-10-04 16:43:53 +02001069 vgetc_busy = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001070# ifdef FEAT_EVAL
1071 emsg_skip = 0;
1072# endif
1073 emsg_off = 0;
1074# ifdef FEAT_MOUSE
1075 setmouse();
1076# endif
1077 settmode(TMODE_RAW);
1078 starttermcap();
1079 scroll_start();
1080 redraw_later_clear();
1081 }
1082#endif
1083
1084 clear_oparg(&oa);
1085 while (!cmdwin
1086#ifdef FEAT_CMDWIN
1087 || cmdwin_result == 0
1088#endif
1089 )
1090 {
1091 if (stuff_empty())
1092 {
1093 did_check_timestamps = FALSE;
1094 if (need_check_timestamps)
1095 check_timestamps(FALSE);
1096 if (need_wait_return) /* if wait_return still needed ... */
1097 wait_return(FALSE); /* ... call it now */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001098 if (need_start_insertmode && goto_im() && !VIsual_active)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001099 {
1100 need_start_insertmode = FALSE;
1101 stuffReadbuff((char_u *)"i"); /* start insert mode next */
1102 /* skip the fileinfo message now, because it would be shown
1103 * after insert mode finishes! */
1104 need_fileinfo = FALSE;
1105 }
1106 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001107
1108 /* Reset "got_int" now that we got back to the main loop. Except when
1109 * inside a ":g/pat/cmd" command, then the "got_int" needs to abort
1110 * the ":g" command.
1111 * For ":g/pat/vi" we reset "got_int" when used once. When used
1112 * a second time we go back to Ex mode and abort the ":g" command. */
1113 if (got_int)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001114 {
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001115 if (noexmode && global_busy && !exmode_active && previous_got_int)
1116 {
1117 /* Typed two CTRL-C in a row: go back to ex mode as if "Q" was
1118 * used and keep "got_int" set, so that it aborts ":g". */
1119 exmode_active = EXMODE_NORMAL;
1120 State = NORMAL;
1121 }
1122 else if (!global_busy || !exmode_active)
1123 {
1124 if (!quit_more)
1125 (void)vgetc(); /* flush all buffers */
1126 got_int = FALSE;
1127 }
1128 previous_got_int = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001129 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001130 else
1131 previous_got_int = FALSE;
1132
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001133 if (!exmode_active)
1134 msg_scroll = FALSE;
1135 quit_more = FALSE;
1136
1137 /*
1138 * If skip redraw is set (for ":" in wait_return()), don't redraw now.
1139 * If there is nothing in the stuff_buffer or do_redraw is TRUE,
1140 * update cursor and redraw.
1141 */
1142 if (skip_redraw || exmode_active)
1143 skip_redraw = FALSE;
1144 else if (do_redraw || stuff_empty())
1145 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001146#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001147 /* Trigger CursorMoved if the cursor moved. */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001148 if (!finish_op && (
1149# ifdef FEAT_AUTOCMD
1150 has_cursormoved()
1151# endif
1152# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
1153 ||
1154# endif
1155# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001156 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001157# endif
1158 )
1159 && !equalpos(last_cursormoved, curwin->w_cursor))
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001160 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001161# ifdef FEAT_AUTOCMD
1162 if (has_cursormoved())
1163 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL,
1164 FALSE, curbuf);
1165# endif
1166# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001167 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001168 {
1169 conceal_old_cursor_line = last_cursormoved.lnum;
1170 conceal_new_cursor_line = curwin->w_cursor.lnum;
1171 conceal_update_lines = TRUE;
1172 }
1173# endif
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001174 last_cursormoved = curwin->w_cursor;
1175 }
1176#endif
1177
Bram Moolenaar186628f2013-03-19 13:33:23 +01001178#ifdef FEAT_AUTOCMD
1179 /* Trigger TextChanged if b_changedtick differs. */
1180 if (!finish_op && has_textchanged()
1181 && last_changedtick != curbuf->b_changedtick)
1182 {
1183 if (last_changedtick_buf == curbuf)
1184 apply_autocmds(EVENT_TEXTCHANGED, NULL, NULL,
1185 FALSE, curbuf);
1186 last_changedtick_buf = curbuf;
1187 last_changedtick = curbuf->b_changedtick;
1188 }
1189#endif
1190
Bram Moolenaar33aec762006-01-22 23:30:12 +00001191#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
1192 /* Scroll-binding for diff mode may have been postponed until
1193 * here. Avoids doing it for every change. */
1194 if (diff_need_scrollbind)
1195 {
1196 check_scrollbind((linenr_T)0, 0L);
1197 diff_need_scrollbind = FALSE;
1198 }
1199#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001200#if defined(FEAT_FOLDING)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001201 /* Include a closed fold completely in the Visual area. */
1202 foldAdjustVisual();
1203#endif
1204#ifdef FEAT_FOLDING
1205 /*
1206 * When 'foldclose' is set, apply 'foldlevel' to folds that don't
1207 * contain the cursor.
1208 * When 'foldopen' is "all", open the fold(s) under the cursor.
1209 * This may mark the window for redrawing.
1210 */
1211 if (hasAnyFolding(curwin) && !char_avail())
1212 {
1213 foldCheckClose();
1214 if (fdo_flags & FDO_ALL)
1215 foldOpenCursor();
1216 }
1217#endif
1218
1219 /*
1220 * Before redrawing, make sure w_topline is correct, and w_leftcol
1221 * if lines don't wrap, and w_skipcol if lines wrap.
1222 */
1223 update_topline();
1224 validate_cursor();
1225
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001226 if (VIsual_active)
1227 update_curbuf(INVERTED);/* update inverted part */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001228 else if (must_redraw)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001229 update_screen(0);
1230 else if (redraw_cmdline || clear_cmdline)
1231 showmode();
1232#ifdef FEAT_WINDOWS
1233 redraw_statuslines();
1234#endif
1235#ifdef FEAT_TITLE
1236 if (need_maketitle)
1237 maketitle();
1238#endif
1239 /* display message after redraw */
1240 if (keep_msg != NULL)
1241 {
1242 char_u *p;
1243
1244 /* msg_attr_keep() will set keep_msg to NULL, must free the
1245 * string here. */
1246 p = keep_msg;
Bram Moolenaar238a5642006-02-21 22:12:05 +00001247 keep_msg = NULL;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001248 msg_attr(p, keep_msg_attr);
1249 vim_free(p);
1250 }
1251 if (need_fileinfo) /* show file info after redraw */
1252 {
1253 fileinfo(FALSE, TRUE, FALSE);
1254 need_fileinfo = FALSE;
1255 }
1256
1257 emsg_on_display = FALSE; /* can delete error message now */
1258 did_emsg = FALSE;
1259 msg_didany = FALSE; /* reset lines_left in msg_start() */
Bram Moolenaar661b1822005-07-28 22:36:45 +00001260 may_clear_sb_text(); /* clear scroll-back text on next msg */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001261 showruler(FALSE);
1262
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001263# if defined(FEAT_CONCEAL)
1264 if (conceal_update_lines
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001265 && (conceal_old_cursor_line != conceal_new_cursor_line
1266 || conceal_cursor_line(curwin)
1267 || need_cursor_line_redraw))
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001268 {
Bram Moolenaarc2b4c622011-02-15 16:29:59 +01001269 if (conceal_old_cursor_line != conceal_new_cursor_line
1270 && conceal_old_cursor_line
1271 <= curbuf->b_ml.ml_line_count)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001272 update_single_line(curwin, conceal_old_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001273 update_single_line(curwin, conceal_new_cursor_line);
1274 curwin->w_valid &= ~VALID_CROW;
1275 }
1276# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001277 setcursor();
1278 cursor_on();
1279
1280 do_redraw = FALSE;
Bram Moolenaar3f269672009-11-03 11:11:11 +00001281
1282#ifdef STARTUPTIME
1283 /* Now that we have drawn the first screen all the startup stuff
1284 * has been done, close any file for startup messages. */
1285 if (time_fd != NULL)
1286 {
1287 TIME_MSG("first screen update");
1288 TIME_MSG("--- VIM STARTED ---");
1289 fclose(time_fd);
1290 time_fd = NULL;
1291 }
1292#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001293 }
1294#ifdef FEAT_GUI
1295 if (need_mouse_correct)
1296 gui_mouse_correct();
1297#endif
1298
1299 /*
1300 * Update w_curswant if w_set_curswant has been set.
1301 * Postponed until here to avoid computing w_virtcol too often.
1302 */
1303 update_curswant();
1304
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001305#ifdef FEAT_EVAL
1306 /*
1307 * May perform garbage collection when waiting for a character, but
1308 * only at the very toplevel. Otherwise we may be using a List or
1309 * Dict internally somewhere.
1310 * "may_garbage_collect" is reset in vgetc() which is invoked through
1311 * do_exmode() and normal_cmd().
1312 */
1313 may_garbage_collect = (!cmdwin && !noexmode);
1314#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001315 /*
1316 * If we're invoked as ex, do a round of ex commands.
1317 * Otherwise, get and execute a normal mode command.
1318 */
1319 if (exmode_active)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001320 {
1321 if (noexmode) /* End of ":global/path/visual" commands */
1322 return;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001323 do_exmode(exmode_active == EXMODE_VIM);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001324 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001325 else
1326 normal_cmd(&oa, TRUE);
1327 }
1328}
1329
1330
1331#if defined(USE_XSMP) || defined(FEAT_GUI_MSWIN) || defined(PROTO)
1332/*
1333 * Exit, but leave behind swap files for modified buffers.
1334 */
1335 void
1336getout_preserve_modified(exitval)
1337 int exitval;
1338{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001339# if defined(SIGHUP) && defined(SIG_IGN)
1340 /* Ignore SIGHUP, because a dropped connection causes a read error, which
1341 * makes Vim exit and then handling SIGHUP causes various reentrance
1342 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001343 signal(SIGHUP, SIG_IGN);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001344# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001345
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001346 ml_close_notmod(); /* close all not-modified buffers */
1347 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
1348 ml_close_all(FALSE); /* close all memfiles, without deleting */
1349 getout(exitval); /* exit Vim properly */
1350}
1351#endif
1352
1353
1354/* Exit properly */
1355 void
1356getout(exitval)
1357 int exitval;
1358{
1359#ifdef FEAT_AUTOCMD
1360 buf_T *buf;
1361 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001362 tabpage_T *tp, *next_tp;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001363#endif
1364
1365 exiting = TRUE;
1366
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001367 /* When running in Ex mode an error causes us to exit with a non-zero exit
1368 * code. POSIX requires this, although it's not 100% clear from the
1369 * standard. */
1370 if (exmode_active)
1371 exitval += ex_exitval;
1372
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001373 /* Position the cursor on the last screen line, below all the text */
1374#ifdef FEAT_GUI
1375 if (!gui.in_use)
1376#endif
1377 windgoto((int)Rows - 1, 0);
1378
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +00001379#if defined(FEAT_EVAL) || defined(FEAT_SYN_HL)
1380 /* Optionally print hashtable efficiency. */
1381 hash_debug_results();
1382#endif
1383
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001384#ifdef FEAT_GUI
1385 msg_didany = FALSE;
1386#endif
1387
1388#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001389 if (get_vim_var_nr(VV_DYING) <= 1)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001390 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001391 /* Trigger BufWinLeave for all windows, but only once per buffer. */
1392# if defined FEAT_WINDOWS
1393 for (tp = first_tabpage; tp != NULL; tp = next_tp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001394 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001395 next_tp = tp->tp_next;
1396 for (wp = (tp == curtab)
1397 ? firstwin : tp->tp_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001398 {
Bram Moolenaar802418d2013-01-17 14:00:11 +01001399 if (wp->w_buffer == NULL)
1400 /* Autocmd must have close the buffer already, skip. */
1401 continue;
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001402 buf = wp->w_buffer;
1403 if (buf->b_changedtick != -1)
1404 {
1405 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname,
1406 buf->b_fname, FALSE, buf);
Bram Moolenaarb429cde2012-04-25 18:24:29 +02001407 buf->b_changedtick = -1; /* note that we did it already */
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001408 /* start all over, autocommands may mess up the lists */
1409 next_tp = first_tabpage;
1410 break;
1411 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001412 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001413 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001414# else
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001415 apply_autocmds(EVENT_BUFWINLEAVE, curbuf, curbuf->b_fname,
1416 FALSE, curbuf);
Bram Moolenaarf740b292006-02-16 22:11:02 +00001417# endif
Bram Moolenaar33aec762006-01-22 23:30:12 +00001418
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001419 /* Trigger BufUnload for buffers that are loaded */
1420 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1421 if (buf->b_ml.ml_mfp != NULL)
1422 {
1423 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001424 FALSE, buf);
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001425 if (!buf_valid(buf)) /* autocmd may delete the buffer */
1426 break;
1427 }
1428 apply_autocmds(EVENT_VIMLEAVEPRE, NULL, NULL, FALSE, curbuf);
1429 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001430#endif
1431
1432#ifdef FEAT_VIMINFO
1433 if (*p_viminfo != NUL)
1434 /* Write out the registers, history, marks etc, to the viminfo file */
1435 write_viminfo(NULL, FALSE);
1436#endif
1437
1438#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001439 if (get_vim_var_nr(VV_DYING) <= 1)
1440 apply_autocmds(EVENT_VIMLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001441#endif
1442
Bram Moolenaar05159a02005-02-26 23:04:13 +00001443#ifdef FEAT_PROFILE
1444 profile_dump();
1445#endif
1446
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001447 if (did_emsg
1448#ifdef FEAT_GUI
1449 || (gui.in_use && msg_didany && p_verbose > 0)
1450#endif
1451 )
1452 {
1453 /* give the user a chance to read the (error) message */
1454 no_wait_return = FALSE;
1455 wait_return(FALSE);
1456 }
1457
1458#ifdef FEAT_AUTOCMD
1459 /* Position the cursor again, the autocommands may have moved it */
1460# ifdef FEAT_GUI
1461 if (!gui.in_use)
1462# endif
1463 windgoto((int)Rows - 1, 0);
1464#endif
1465
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001466#ifdef FEAT_LUA
1467 lua_end();
1468#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001469#ifdef FEAT_MZSCHEME
1470 mzscheme_end();
1471#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001472#ifdef FEAT_TCL
1473 tcl_end();
1474#endif
1475#ifdef FEAT_RUBY
1476 ruby_end();
1477#endif
1478#ifdef FEAT_PYTHON
1479 python_end();
1480#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001481#ifdef FEAT_PYTHON3
1482 python3_end();
1483#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001484#ifdef FEAT_PERL
1485 perl_end();
1486#endif
1487#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
1488 iconv_end();
1489#endif
1490#ifdef FEAT_NETBEANS_INTG
1491 netbeans_end();
1492#endif
Bram Moolenaar02b06312007-09-06 15:39:22 +00001493#ifdef FEAT_CSCOPE
1494 cs_end();
1495#endif
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001496#ifdef FEAT_EVAL
1497 if (garbage_collect_at_exit)
1498 garbage_collect();
1499#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001500
1501 mch_exit(exitval);
1502}
1503
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01001504#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001505/*
1506 * Get a (optional) count for a Vim argument.
1507 */
1508 static int
1509get_number_arg(p, idx, def)
1510 char_u *p; /* pointer to argument */
1511 int *idx; /* index in argument, is incremented */
1512 int def; /* default value */
1513{
1514 if (vim_isdigit(p[*idx]))
1515 {
1516 def = atoi((char *)&(p[*idx]));
1517 while (vim_isdigit(p[*idx]))
1518 *idx = *idx + 1;
1519 }
1520 return def;
1521}
1522
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001523#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1524/*
1525 * Setup to use the current locale (for ctype() and many other things).
1526 */
1527 static void
1528init_locale()
1529{
1530 setlocale(LC_ALL, "");
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001531
Bram Moolenaarc6af8122010-05-21 12:04:55 +02001532# ifdef FEAT_GUI_GTK
1533 /* Tell Gtk not to change our locale settings. */
1534 gtk_disable_setlocale();
1535# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001536# if defined(FEAT_FLOAT) && defined(LC_NUMERIC)
1537 /* Make sure strtod() uses a decimal point, not a comma. */
1538 setlocale(LC_NUMERIC, "C");
1539# endif
1540
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001541# ifdef WIN32
1542 /* Apparently MS-Windows printf() may cause a crash when we give it 8-bit
1543 * text while it's expecting text in the current locale. This call avoids
1544 * that. */
1545 setlocale(LC_CTYPE, "C");
1546# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001547
1548# ifdef FEAT_GETTEXT
1549 {
1550 int mustfree = FALSE;
1551 char_u *p;
1552
1553# ifdef DYNAMIC_GETTEXT
1554 /* Initialize the gettext library */
1555 dyn_libintl_init(NULL);
1556# endif
1557 /* expand_env() doesn't work yet, because chartab[] is not initialized
1558 * yet, call vim_getenv() directly */
1559 p = vim_getenv((char_u *)"VIMRUNTIME", &mustfree);
1560 if (p != NULL && *p != NUL)
1561 {
Bram Moolenaar1ad2f132007-06-19 18:27:18 +00001562 vim_snprintf((char *)NameBuff, MAXPATHL, "%s/lang", p);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001563 bindtextdomain(VIMPACKAGE, (char *)NameBuff);
1564 }
1565 if (mustfree)
1566 vim_free(p);
1567 textdomain(VIMPACKAGE);
1568 }
1569# endif
1570}
1571#endif
1572
1573/*
1574 * Check for: [r][e][g][vi|vim|view][diff][ex[im]]
1575 * If the executable name starts with "r" we disable shell commands.
1576 * If the next character is "e" we run in Easy mode.
1577 * If the next character is "g" we run the GUI version.
1578 * If the next characters are "view" we start in readonly mode.
1579 * If the next characters are "diff" or "vimdiff" we start in diff mode.
1580 * If the next characters are "ex" we start in Ex mode. If it's followed
1581 * by "im" use improved Ex mode.
1582 */
1583 static void
1584parse_command_name(parmp)
1585 mparm_T *parmp;
1586{
1587 char_u *initstr;
1588
1589 initstr = gettail((char_u *)parmp->argv[0]);
1590
1591#ifdef MACOS_X_UNIX
1592 /* An issue has been seen when launching Vim in such a way that
1593 * $PWD/$ARGV[0] or $ARGV[0] is not the absolute path to the
1594 * executable or a symbolic link of it. Until this issue is resolved
1595 * we prohibit the GUI from being used.
1596 */
1597 if (STRCMP(initstr, parmp->argv[0]) == 0)
1598 disallow_gui = TRUE;
1599
1600 /* TODO: On MacOS X default to gui if argv[0] ends in:
Bram Moolenaar27dc1952006-03-15 23:06:44 +00001601 * /Vim.app/Contents/MacOS/Vim */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001602#endif
1603
1604#ifdef FEAT_EVAL
1605 set_vim_var_string(VV_PROGNAME, initstr, -1);
Bram Moolenaara1706c92014-04-01 19:55:49 +02001606 set_vim_var_string(VV_PROGPATH, (char_u *)parmp->argv[0], -1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001607#endif
1608
1609 if (TOLOWER_ASC(initstr[0]) == 'r')
1610 {
1611 restricted = TRUE;
1612 ++initstr;
1613 }
1614
Bram Moolenaarad249fb2010-05-07 16:35:04 +02001615 /* Use evim mode for "evim" and "egvim", not for "editor". */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001616 if (TOLOWER_ASC(initstr[0]) == 'e'
1617 && (TOLOWER_ASC(initstr[1]) == 'v'
1618 || TOLOWER_ASC(initstr[1]) == 'g'))
1619 {
1620#ifdef FEAT_GUI
1621 gui.starting = TRUE;
1622#endif
1623 parmp->evim_mode = TRUE;
1624 ++initstr;
1625 }
1626
Bram Moolenaar806875d2008-09-18 18:57:10 +00001627 /* "gvim" starts the GUI. Also accept "Gvim" for MS-Windows. */
1628 if (TOLOWER_ASC(initstr[0]) == 'g')
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001629 {
1630 main_start_gui();
1631#ifdef FEAT_GUI
1632 ++initstr;
1633#endif
1634 }
1635
1636 if (STRNICMP(initstr, "view", 4) == 0)
1637 {
1638 readonlymode = TRUE;
1639 curbuf->b_p_ro = TRUE;
1640 p_uc = 10000; /* don't update very often */
1641 initstr += 4;
1642 }
1643 else if (STRNICMP(initstr, "vim", 3) == 0)
1644 initstr += 3;
1645
1646 /* Catch "[r][g]vimdiff" and "[r][g]viewdiff". */
1647 if (STRICMP(initstr, "diff") == 0)
1648 {
1649#ifdef FEAT_DIFF
1650 parmp->diff_mode = TRUE;
1651#else
1652 mch_errmsg(_("This Vim was not compiled with the diff feature."));
1653 mch_errmsg("\n");
1654 mch_exit(2);
1655#endif
1656 }
1657
1658 if (STRNICMP(initstr, "ex", 2) == 0)
1659 {
1660 if (STRNICMP(initstr + 2, "im", 2) == 0)
1661 exmode_active = EXMODE_VIM;
1662 else
1663 exmode_active = EXMODE_NORMAL;
1664 change_compatible(TRUE); /* set 'compatible' */
1665 }
1666}
1667
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001668/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00001669 * Get the name of the display, before gui_prepare() removes it from
1670 * argv[]. Used for the xterm-clipboard display.
1671 *
Bram Moolenaar78e17622007-08-30 10:26:19 +00001672 * Also find the --server... arguments and --socketid and --windowid
Bram Moolenaar58d98232005-07-23 22:25:46 +00001673 */
Bram Moolenaar58d98232005-07-23 22:25:46 +00001674 static void
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001675early_arg_scan(parmp)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00001676 mparm_T *parmp UNUSED;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001677{
Bram Moolenaar03005972008-11-20 13:12:36 +00001678#if defined(FEAT_XCLIPBOARD) || defined(FEAT_CLIENTSERVER) \
1679 || !defined(FEAT_NETBEANS_INTG)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001680 int argc = parmp->argc;
1681 char **argv = parmp->argv;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001682 int i;
1683
1684 for (i = 1; i < argc; i++)
1685 {
1686 if (STRCMP(argv[i], "--") == 0)
1687 break;
1688# ifdef FEAT_XCLIPBOARD
1689 else if (STRICMP(argv[i], "-display") == 0
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001690# if defined(FEAT_GUI_GTK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001691 || STRICMP(argv[i], "--display") == 0
1692# endif
1693 )
1694 {
1695 if (i == argc - 1)
1696 mainerr_arg_missing((char_u *)argv[i]);
1697 xterm_display = argv[++i];
1698 }
1699# endif
1700# ifdef FEAT_CLIENTSERVER
1701 else if (STRICMP(argv[i], "--servername") == 0)
1702 {
1703 if (i == argc - 1)
1704 mainerr_arg_missing((char_u *)argv[i]);
1705 parmp->serverName_arg = (char_u *)argv[++i];
1706 }
Bram Moolenaareb94e552006-03-11 21:35:11 +00001707 else if (STRICMP(argv[i], "--serverlist") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001708 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001709 else if (STRNICMP(argv[i], "--remote", 8) == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001710 {
1711 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001712# ifdef FEAT_GUI
1713 if (strstr(argv[i], "-wait") != 0)
1714 /* don't fork() when starting the GUI to edit files ourself */
1715 gui.dofork = FALSE;
1716# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001717 }
1718# endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001719
1720# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1721# ifdef FEAT_GUI_W32
1722 else if (STRICMP(argv[i], "--windowid") == 0)
1723# else
Bram Moolenaar58d98232005-07-23 22:25:46 +00001724 else if (STRICMP(argv[i], "--socketid") == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001725# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001726 {
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001727 long_u id;
1728 int count;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001729
1730 if (i == argc - 1)
1731 mainerr_arg_missing((char_u *)argv[i]);
1732 if (STRNICMP(argv[i+1], "0x", 2) == 0)
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001733 count = sscanf(&(argv[i + 1][2]), SCANF_HEX_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001734 else
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001735 count = sscanf(argv[i + 1], SCANF_DECIMAL_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001736 if (count != 1)
1737 mainerr(ME_INVALID_ARG, (char_u *)argv[i]);
1738 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00001739# ifdef FEAT_GUI_W32
1740 win_socket_id = id;
1741# else
1742 gtk_socket_id = id;
1743# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001744 i++;
1745 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001746# endif
1747# ifdef FEAT_GUI_GTK
Bram Moolenaar58d98232005-07-23 22:25:46 +00001748 else if (STRICMP(argv[i], "--echo-wid") == 0)
1749 echo_wid_arg = TRUE;
1750# endif
Bram Moolenaar03005972008-11-20 13:12:36 +00001751# ifndef FEAT_NETBEANS_INTG
1752 else if (strncmp(argv[i], "-nb", (size_t)3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +02001753 {
1754 mch_errmsg(_("'-nb' cannot be used: not enabled at compile time\n"));
1755 mch_exit(2);
1756 }
Bram Moolenaar03005972008-11-20 13:12:36 +00001757# endif
1758
Bram Moolenaar58d98232005-07-23 22:25:46 +00001759 }
1760#endif
1761}
1762
1763/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001764 * Scan the command line arguments.
1765 */
1766 static void
1767command_line_scan(parmp)
1768 mparm_T *parmp;
1769{
1770 int argc = parmp->argc;
1771 char **argv = parmp->argv;
1772 int argv_idx; /* index in argv[n][] */
1773 int had_minmin = FALSE; /* found "--" argument */
1774 int want_argument; /* option argument with argument */
1775 int c;
Bram Moolenaar231334e2005-07-25 20:46:57 +00001776 char_u *p = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001777 long n;
1778
1779 --argc;
1780 ++argv;
1781 argv_idx = 1; /* active option letter is argv[0][argv_idx] */
1782 while (argc > 0)
1783 {
1784 /*
1785 * "+" or "+{number}" or "+/{pat}" or "+{command}" argument.
1786 */
1787 if (argv[0][0] == '+' && !had_minmin)
1788 {
1789 if (parmp->n_commands >= MAX_ARG_CMDS)
1790 mainerr(ME_EXTRA_CMD, NULL);
1791 argv_idx = -1; /* skip to next argument */
1792 if (argv[0][1] == NUL)
1793 parmp->commands[parmp->n_commands++] = (char_u *)"$";
1794 else
1795 parmp->commands[parmp->n_commands++] = (char_u *)&(argv[0][1]);
1796 }
1797
1798 /*
1799 * Optional argument.
1800 */
1801 else if (argv[0][0] == '-' && !had_minmin)
1802 {
1803 want_argument = FALSE;
1804 c = argv[0][argv_idx++];
1805#ifdef VMS
1806 /*
1807 * VMS only uses upper case command lines. Interpret "-X" as "-x"
1808 * and "-/X" as "-X".
1809 */
1810 if (c == '/')
1811 {
1812 c = argv[0][argv_idx++];
1813 c = TOUPPER_ASC(c);
1814 }
1815 else
1816 c = TOLOWER_ASC(c);
1817#endif
1818 switch (c)
1819 {
1820 case NUL: /* "vim -" read from stdin */
1821 /* "ex -" silent mode */
1822 if (exmode_active)
1823 silent_mode = TRUE;
1824 else
1825 {
1826 if (parmp->edit_type != EDIT_NONE)
1827 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1828 parmp->edit_type = EDIT_STDIN;
1829 read_cmd_fd = 2; /* read from stderr instead of stdin */
1830 }
1831 argv_idx = -1; /* skip to next argument */
1832 break;
1833
1834 case '-': /* "--" don't take any more option arguments */
1835 /* "--help" give help message */
1836 /* "--version" give version message */
1837 /* "--literal" take files literally */
1838 /* "--nofork" don't fork */
1839 /* "--noplugin[s]" skip plugins */
1840 /* "--cmd <cmd>" execute cmd before vimrc */
1841 if (STRICMP(argv[0] + argv_idx, "help") == 0)
1842 usage();
1843 else if (STRICMP(argv[0] + argv_idx, "version") == 0)
1844 {
1845 Columns = 80; /* need to init Columns */
1846 info_message = TRUE; /* use mch_msg(), not mch_errmsg() */
1847 list_version();
1848 msg_putchar('\n');
1849 msg_didout = FALSE;
1850 mch_exit(0);
1851 }
1852 else if (STRNICMP(argv[0] + argv_idx, "literal", 7) == 0)
1853 {
1854#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
1855 parmp->literal = TRUE;
1856#endif
1857 }
1858 else if (STRNICMP(argv[0] + argv_idx, "nofork", 6) == 0)
1859 {
1860#ifdef FEAT_GUI
1861 gui.dofork = FALSE; /* don't fork() when starting GUI */
1862#endif
1863 }
1864 else if (STRNICMP(argv[0] + argv_idx, "noplugin", 8) == 0)
1865 p_lpl = FALSE;
1866 else if (STRNICMP(argv[0] + argv_idx, "cmd", 3) == 0)
1867 {
1868 want_argument = TRUE;
1869 argv_idx += 3;
1870 }
Bram Moolenaaref94eec2009-11-11 13:22:11 +00001871 else if (STRNICMP(argv[0] + argv_idx, "startuptime", 11) == 0)
1872 {
1873 want_argument = TRUE;
1874 argv_idx += 11;
1875 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001876#ifdef FEAT_CLIENTSERVER
1877 else if (STRNICMP(argv[0] + argv_idx, "serverlist", 10) == 0)
1878 ; /* already processed -- no arg */
1879 else if (STRNICMP(argv[0] + argv_idx, "servername", 10) == 0
1880 || STRNICMP(argv[0] + argv_idx, "serversend", 10) == 0)
1881 {
1882 /* already processed -- snatch the following arg */
1883 if (argc > 1)
1884 {
1885 --argc;
1886 ++argv;
1887 }
1888 }
1889#endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001890#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1891# ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001892 else if (STRNICMP(argv[0] + argv_idx, "socketid", 8) == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001893# else
1894 else if (STRNICMP(argv[0] + argv_idx, "windowid", 8) == 0)
1895# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001896 {
1897 /* already processed -- snatch the following arg */
1898 if (argc > 1)
1899 {
1900 --argc;
1901 ++argv;
1902 }
1903 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001904#endif
1905#ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001906 else if (STRNICMP(argv[0] + argv_idx, "echo-wid", 8) == 0)
1907 {
1908 /* already processed, skip */
1909 }
1910#endif
1911 else
1912 {
1913 if (argv[0][argv_idx])
1914 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
1915 had_minmin = TRUE;
1916 }
1917 if (!want_argument)
1918 argv_idx = -1; /* skip to next argument */
1919 break;
1920
1921 case 'A': /* "-A" start in Arabic mode */
1922#ifdef FEAT_ARABIC
1923 set_option_value((char_u *)"arabic", 1L, NULL, 0);
1924#else
1925 mch_errmsg(_(e_noarabic));
1926 mch_exit(2);
1927#endif
1928 break;
1929
1930 case 'b': /* "-b" binary mode */
Bram Moolenaar231334e2005-07-25 20:46:57 +00001931 /* Needs to be effective before expanding file names, because
1932 * for Win32 this makes us edit a shortcut file itself,
1933 * instead of the file it links to. */
1934 set_options_bin(curbuf->b_p_bin, 1, 0);
1935 curbuf->b_p_bin = 1; /* binary file I/O */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001936 break;
1937
1938 case 'C': /* "-C" Compatible */
1939 change_compatible(TRUE);
1940 break;
1941
1942 case 'e': /* "-e" Ex mode */
1943 exmode_active = EXMODE_NORMAL;
1944 break;
1945
1946 case 'E': /* "-E" Improved Ex mode */
1947 exmode_active = EXMODE_VIM;
1948 break;
1949
1950 case 'f': /* "-f" GUI: run in foreground. Amiga: open
1951 window directly, not with newcli */
1952#ifdef FEAT_GUI
1953 gui.dofork = FALSE; /* don't fork() when starting GUI */
1954#endif
1955 break;
1956
1957 case 'g': /* "-g" start GUI */
1958 main_start_gui();
1959 break;
1960
1961 case 'F': /* "-F" start in Farsi mode: rl + fkmap set */
1962#ifdef FEAT_FKMAP
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00001963 p_fkmap = TRUE;
1964 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001965#else
1966 mch_errmsg(_(e_nofarsi));
1967 mch_exit(2);
1968#endif
1969 break;
1970
1971 case 'h': /* "-h" give help message */
1972#ifdef FEAT_GUI_GNOME
1973 /* Tell usage() to exit for "gvim". */
1974 gui.starting = FALSE;
1975#endif
1976 usage();
1977 break;
1978
1979 case 'H': /* "-H" start in Hebrew mode: rl + hkmap set */
1980#ifdef FEAT_RIGHTLEFT
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00001981 p_hkmap = TRUE;
1982 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001983#else
1984 mch_errmsg(_(e_nohebrew));
1985 mch_exit(2);
1986#endif
1987 break;
1988
1989 case 'l': /* "-l" lisp mode, 'lisp' and 'showmatch' on */
1990#ifdef FEAT_LISP
1991 set_option_value((char_u *)"lisp", 1L, NULL, 0);
1992 p_sm = TRUE;
1993#endif
1994 break;
1995
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001996 case 'M': /* "-M" no changes or writing of files */
1997 reset_modifiable();
1998 /* FALLTHROUGH */
1999
2000 case 'm': /* "-m" no writing of files */
2001 p_write = FALSE;
2002 break;
2003
2004 case 'y': /* "-y" easy mode */
2005#ifdef FEAT_GUI
2006 gui.starting = TRUE; /* start GUI a bit later */
2007#endif
2008 parmp->evim_mode = TRUE;
2009 break;
2010
2011 case 'N': /* "-N" Nocompatible */
2012 change_compatible(FALSE);
2013 break;
2014
2015 case 'n': /* "-n" no swap file */
Bram Moolenaar67c53842010-05-22 18:28:27 +02002016#ifdef FEAT_NETBEANS_INTG
2017 /* checking for "-nb", netbeans parameters */
2018 if (argv[0][argv_idx] == 'b')
2019 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002020 netbeansArg = argv[0];
2021 argv_idx = -1; /* skip to next argument */
2022 }
2023 else
2024#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002025 parmp->no_swap_file = TRUE;
2026 break;
2027
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002028 case 'p': /* "-p[N]" open N tab pages */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002029#ifdef TARGET_API_MAC_OSX
2030 /* For some reason on MacOS X, an argument like:
2031 -psn_0_10223617 is passed in when invoke from Finder
2032 or with the 'open' command */
2033 if (argv[0][argv_idx] == 's')
2034 {
2035 argv_idx = -1; /* bypass full -psn */
2036 main_start_gui();
2037 break;
2038 }
2039#endif
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002040#ifdef FEAT_WINDOWS
2041 /* default is 0: open window for each file */
2042 parmp->window_count = get_number_arg((char_u *)argv[0],
2043 &argv_idx, 0);
2044 parmp->window_layout = WIN_TABS;
2045#endif
2046 break;
2047
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002048 case 'o': /* "-o[N]" open N horizontal split windows */
2049#ifdef FEAT_WINDOWS
2050 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002051 parmp->window_count = get_number_arg((char_u *)argv[0],
2052 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002053 parmp->window_layout = WIN_HOR;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002054#endif
2055 break;
2056
2057 case 'O': /* "-O[N]" open N vertical split windows */
2058#if defined(FEAT_VERTSPLIT) && defined(FEAT_WINDOWS)
2059 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002060 parmp->window_count = get_number_arg((char_u *)argv[0],
2061 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002062 parmp->window_layout = WIN_VER;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002063#endif
2064 break;
2065
2066#ifdef FEAT_QUICKFIX
2067 case 'q': /* "-q" QuickFix mode */
2068 if (parmp->edit_type != EDIT_NONE)
2069 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2070 parmp->edit_type = EDIT_QF;
2071 if (argv[0][argv_idx]) /* "-q{errorfile}" */
2072 {
2073 parmp->use_ef = (char_u *)argv[0] + argv_idx;
2074 argv_idx = -1;
2075 }
2076 else if (argc > 1) /* "-q {errorfile}" */
2077 want_argument = TRUE;
2078 break;
2079#endif
2080
2081 case 'R': /* "-R" readonly mode */
2082 readonlymode = TRUE;
2083 curbuf->b_p_ro = TRUE;
2084 p_uc = 10000; /* don't update very often */
2085 break;
2086
2087 case 'r': /* "-r" recovery mode */
2088 case 'L': /* "-L" recovery mode */
2089 recoverymode = 1;
2090 break;
2091
2092 case 's':
2093 if (exmode_active) /* "-s" silent (batch) mode */
2094 silent_mode = TRUE;
2095 else /* "-s {scriptin}" read from script file */
2096 want_argument = TRUE;
2097 break;
2098
2099 case 't': /* "-t {tag}" or "-t{tag}" jump to tag */
2100 if (parmp->edit_type != EDIT_NONE)
2101 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2102 parmp->edit_type = EDIT_TAG;
2103 if (argv[0][argv_idx]) /* "-t{tag}" */
2104 {
2105 parmp->tagname = (char_u *)argv[0] + argv_idx;
2106 argv_idx = -1;
2107 }
2108 else /* "-t {tag}" */
2109 want_argument = TRUE;
2110 break;
2111
2112#ifdef FEAT_EVAL
2113 case 'D': /* "-D" Debugging */
2114 parmp->use_debug_break_level = 9999;
2115 break;
2116#endif
2117#ifdef FEAT_DIFF
2118 case 'd': /* "-d" 'diff' */
2119# ifdef AMIGA
2120 /* check for "-dev {device}" */
2121 if (argv[0][argv_idx] == 'e' && argv[0][argv_idx + 1] == 'v')
2122 want_argument = TRUE;
2123 else
2124# endif
2125 parmp->diff_mode = TRUE;
2126 break;
2127#endif
2128 case 'V': /* "-V{N}" Verbose level */
2129 /* default is 10: a little bit verbose */
2130 p_verbose = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2131 if (argv[0][argv_idx] != NUL)
2132 {
2133 set_option_value((char_u *)"verbosefile", 0L,
2134 (char_u *)argv[0] + argv_idx, 0);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002135 argv_idx = (int)STRLEN(argv[0]);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002136 }
2137 break;
2138
2139 case 'v': /* "-v" Vi-mode (as if called "vi") */
2140 exmode_active = 0;
2141#ifdef FEAT_GUI
2142 gui.starting = FALSE; /* don't start GUI */
2143#endif
2144 break;
2145
2146 case 'w': /* "-w{number}" set window height */
2147 /* "-w {scriptout}" write to script */
2148 if (vim_isdigit(((char_u *)argv[0])[argv_idx]))
2149 {
2150 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2151 set_option_value((char_u *)"window", n, NULL, 0);
2152 break;
2153 }
2154 want_argument = TRUE;
2155 break;
2156
2157#ifdef FEAT_CRYPT
2158 case 'x': /* "-x" encrypted reading/writing of files */
2159 parmp->ask_for_key = TRUE;
2160 break;
2161#endif
2162
2163 case 'X': /* "-X" don't connect to X server */
2164#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
2165 x_no_connect = TRUE;
2166#endif
2167 break;
2168
2169 case 'Z': /* "-Z" restricted mode */
2170 restricted = TRUE;
2171 break;
2172
2173 case 'c': /* "-c{command}" or "-c {command}" execute
2174 command */
2175 if (argv[0][argv_idx] != NUL)
2176 {
2177 if (parmp->n_commands >= MAX_ARG_CMDS)
2178 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar231334e2005-07-25 20:46:57 +00002179 parmp->commands[parmp->n_commands++] = (char_u *)argv[0]
2180 + argv_idx;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002181 argv_idx = -1;
2182 break;
2183 }
2184 /*FALLTHROUGH*/
2185 case 'S': /* "-S {file}" execute Vim script */
2186 case 'i': /* "-i {viminfo}" use for viminfo */
2187#ifndef FEAT_DIFF
2188 case 'd': /* "-d {device}" device (for Amiga) */
2189#endif
2190 case 'T': /* "-T {terminal}" terminal name */
2191 case 'u': /* "-u {vimrc}" vim inits file */
2192 case 'U': /* "-U {gvimrc}" gvim inits file */
2193 case 'W': /* "-W {scriptout}" overwrite */
2194#ifdef FEAT_GUI_W32
2195 case 'P': /* "-P {parent title}" MDI parent */
2196#endif
2197 want_argument = TRUE;
2198 break;
2199
2200 default:
2201 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
2202 }
2203
2204 /*
2205 * Handle option arguments with argument.
2206 */
2207 if (want_argument)
2208 {
2209 /*
2210 * Check for garbage immediately after the option letter.
2211 */
2212 if (argv[0][argv_idx] != NUL)
2213 mainerr(ME_GARBAGE, (char_u *)argv[0]);
2214
2215 --argc;
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002216 if (argc < 1 && c != 'S') /* -S has an optional argument */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002217 mainerr_arg_missing((char_u *)argv[0]);
2218 ++argv;
2219 argv_idx = -1;
2220
2221 switch (c)
2222 {
2223 case 'c': /* "-c {command}" execute command */
2224 case 'S': /* "-S {file}" execute Vim script */
2225 if (parmp->n_commands >= MAX_ARG_CMDS)
2226 mainerr(ME_EXTRA_CMD, NULL);
2227 if (c == 'S')
2228 {
2229 char *a;
2230
2231 if (argc < 1)
2232 /* "-S" without argument: use default session file
2233 * name. */
2234 a = SESSION_FILE;
2235 else if (argv[0][0] == '-')
2236 {
2237 /* "-S" followed by another option: use default
2238 * session file name. */
2239 a = SESSION_FILE;
2240 ++argc;
2241 --argv;
2242 }
2243 else
2244 a = argv[0];
2245 p = alloc((unsigned)(STRLEN(a) + 4));
2246 if (p == NULL)
2247 mch_exit(2);
2248 sprintf((char *)p, "so %s", a);
2249 parmp->cmds_tofree[parmp->n_commands] = TRUE;
2250 parmp->commands[parmp->n_commands++] = p;
2251 }
2252 else
Bram Moolenaar231334e2005-07-25 20:46:57 +00002253 parmp->commands[parmp->n_commands++] =
2254 (char_u *)argv[0];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002255 break;
2256
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002257 case '-':
2258 if (argv[-1][2] == 'c')
2259 {
2260 /* "--cmd {command}" execute command */
2261 if (parmp->n_pre_commands >= MAX_ARG_CMDS)
2262 mainerr(ME_EXTRA_CMD, NULL);
2263 parmp->pre_commands[parmp->n_pre_commands++] =
Bram Moolenaar231334e2005-07-25 20:46:57 +00002264 (char_u *)argv[0];
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002265 }
2266 /* "--startuptime <file>" already handled */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002267 break;
2268
2269 /* case 'd': -d {device} is handled in mch_check_win() for the
2270 * Amiga */
2271
2272#ifdef FEAT_QUICKFIX
2273 case 'q': /* "-q {errorfile}" QuickFix mode */
2274 parmp->use_ef = (char_u *)argv[0];
2275 break;
2276#endif
2277
2278 case 'i': /* "-i {viminfo}" use for viminfo */
2279 use_viminfo = (char_u *)argv[0];
2280 break;
2281
2282 case 's': /* "-s {scriptin}" read from script file */
2283 if (scriptin[0] != NULL)
2284 {
2285scripterror:
2286 mch_errmsg(_("Attempt to open script file again: \""));
2287 mch_errmsg(argv[-1]);
2288 mch_errmsg(" ");
2289 mch_errmsg(argv[0]);
2290 mch_errmsg("\"\n");
2291 mch_exit(2);
2292 }
2293 if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL)
2294 {
2295 mch_errmsg(_("Cannot open for reading: \""));
2296 mch_errmsg(argv[0]);
2297 mch_errmsg("\"\n");
2298 mch_exit(2);
2299 }
2300 if (save_typebuf() == FAIL)
2301 mch_exit(2); /* out of memory */
2302 break;
2303
2304 case 't': /* "-t {tag}" */
2305 parmp->tagname = (char_u *)argv[0];
2306 break;
2307
2308 case 'T': /* "-T {terminal}" terminal name */
2309 /*
2310 * The -T term argument is always available and when
2311 * HAVE_TERMLIB is supported it overrides the environment
2312 * variable TERM.
2313 */
2314#ifdef FEAT_GUI
2315 if (term_is_gui((char_u *)argv[0]))
2316 gui.starting = TRUE; /* start GUI a bit later */
2317 else
2318#endif
2319 parmp->term = (char_u *)argv[0];
2320 break;
2321
2322 case 'u': /* "-u {vimrc}" vim inits file */
2323 parmp->use_vimrc = (char_u *)argv[0];
2324 break;
2325
2326 case 'U': /* "-U {gvimrc}" gvim inits file */
2327#ifdef FEAT_GUI
2328 use_gvimrc = (char_u *)argv[0];
2329#endif
2330 break;
2331
2332 case 'w': /* "-w {nr}" 'window' value */
2333 /* "-w {scriptout}" append to script file */
2334 if (vim_isdigit(*((char_u *)argv[0])))
2335 {
2336 argv_idx = 0;
2337 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2338 set_option_value((char_u *)"window", n, NULL, 0);
2339 argv_idx = -1;
2340 break;
2341 }
2342 /*FALLTHROUGH*/
2343 case 'W': /* "-W {scriptout}" overwrite script file */
2344 if (scriptout != NULL)
2345 goto scripterror;
2346 if ((scriptout = mch_fopen(argv[0],
2347 c == 'w' ? APPENDBIN : WRITEBIN)) == NULL)
2348 {
2349 mch_errmsg(_("Cannot open for script output: \""));
2350 mch_errmsg(argv[0]);
2351 mch_errmsg("\"\n");
2352 mch_exit(2);
2353 }
2354 break;
2355
2356#ifdef FEAT_GUI_W32
2357 case 'P': /* "-P {parent title}" MDI parent */
2358 gui_mch_set_parent(argv[0]);
2359 break;
2360#endif
2361 }
2362 }
2363 }
2364
2365 /*
2366 * File name argument.
2367 */
2368 else
2369 {
2370 argv_idx = -1; /* skip to next argument */
2371
2372 /* Check for only one type of editing. */
2373 if (parmp->edit_type != EDIT_NONE && parmp->edit_type != EDIT_FILE)
2374 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2375 parmp->edit_type = EDIT_FILE;
2376
2377#ifdef MSWIN
2378 /* Remember if the argument was a full path before changing
2379 * slashes to backslashes. */
2380 if (argv[0][0] != NUL && argv[0][1] == ':' && argv[0][2] == '\\')
2381 parmp->full_path = TRUE;
2382#endif
2383
2384 /* Add the file to the global argument list. */
2385 if (ga_grow(&global_alist.al_ga, 1) == FAIL
2386 || (p = vim_strsave((char_u *)argv[0])) == NULL)
2387 mch_exit(2);
2388#ifdef FEAT_DIFF
2389 if (parmp->diff_mode && mch_isdir(p) && GARGCOUNT > 0
2390 && !mch_isdir(alist_name(&GARGLIST[0])))
2391 {
2392 char_u *r;
2393
2394 r = concat_fnames(p, gettail(alist_name(&GARGLIST[0])), TRUE);
2395 if (r != NULL)
2396 {
2397 vim_free(p);
2398 p = r;
2399 }
2400 }
2401#endif
2402#if defined(__CYGWIN32__) && !defined(WIN32)
2403 /*
2404 * If vim is invoked by non-Cygwin tools, convert away any
2405 * DOS paths, so things like .swp files are created correctly.
2406 * Look for evidence of non-Cygwin paths before we bother.
2407 * This is only for when using the Unix files.
2408 */
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002409 if (vim_strpbrk(p, "\\:") != NULL && !path_with_url(p))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002410 {
2411 char posix_path[PATH_MAX];
2412
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002413# if CYGWIN_VERSION_DLL_MAJOR >= 1007
2414 cygwin_conv_path(CCP_WIN_A_TO_POSIX, p, posix_path, PATH_MAX);
2415# else
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002416 cygwin_conv_to_posix_path(p, posix_path);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002417# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002418 vim_free(p);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002419 p = vim_strsave((char_u *)posix_path);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002420 if (p == NULL)
2421 mch_exit(2);
2422 }
2423#endif
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002424
2425#ifdef USE_FNAME_CASE
2426 /* Make the case of the file name match the actual file. */
2427 fname_case(p, 0);
2428#endif
2429
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002430 alist_add(&global_alist, p,
2431#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
Bram Moolenaar231334e2005-07-25 20:46:57 +00002432 parmp->literal ? 2 : 0 /* add buffer nr after exp. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002433#else
2434 2 /* add buffer number now and use curbuf */
2435#endif
2436 );
2437
2438#if defined(FEAT_MBYTE) && defined(WIN32)
2439 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002440 /* Remember this argument has been added to the argument list.
2441 * Needed when 'encoding' is changed. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002442 used_file_arg(argv[0], parmp->literal, parmp->full_path,
Bram Moolenaar688e5f72008-07-24 11:51:40 +00002443# ifdef FEAT_DIFF
2444 parmp->diff_mode
2445# else
2446 FALSE
2447# endif
2448 );
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002449 }
2450#endif
2451 }
2452
2453 /*
2454 * If there are no more letters after the current "-", go to next
2455 * argument. argv_idx is set to -1 when the current argument is to be
2456 * skipped.
2457 */
2458 if (argv_idx <= 0 || argv[0][argv_idx] == NUL)
2459 {
2460 --argc;
2461 ++argv;
2462 argv_idx = 1;
2463 }
2464 }
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002465
2466#ifdef FEAT_EVAL
2467 /* If there is a "+123" or "-c" command, set v:swapcommand to the first
2468 * one. */
2469 if (parmp->n_commands > 0)
2470 {
2471 p = alloc((unsigned)STRLEN(parmp->commands[0]) + 3);
2472 if (p != NULL)
2473 {
2474 sprintf((char *)p, ":%s\r", parmp->commands[0]);
2475 set_vim_var_string(VV_SWAPCOMMAND, p, -1);
2476 vim_free(p);
2477 }
2478 }
2479#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002480}
2481
2482/*
2483 * Print a warning if stdout is not a terminal.
2484 * When starting in Ex mode and commands come from a file, set Silent mode.
2485 */
2486 static void
2487check_tty(parmp)
2488 mparm_T *parmp;
2489{
2490 int input_isatty; /* is active input a terminal? */
2491
2492 input_isatty = mch_input_isatty();
2493 if (exmode_active)
2494 {
2495 if (!input_isatty)
2496 silent_mode = TRUE;
2497 }
2498 else if (parmp->want_full_screen && (!parmp->stdout_isatty || !input_isatty)
2499#ifdef FEAT_GUI
2500 /* don't want the delay when started from the desktop */
2501 && !gui.starting
2502#endif
2503 )
2504 {
2505#ifdef NBDEBUG
2506 /*
2507 * This shouldn't be necessary. But if I run netbeans with the log
2508 * output coming to the console and XOpenDisplay fails, I get vim
2509 * trying to start with input/output to my console tty. This fills my
2510 * input buffer so fast I can't even kill the process in under 2
Bram Moolenaar49325942007-05-10 19:19:59 +00002511 * minutes (and it beeps continuously the whole time :-)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002512 */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002513 if (netbeans_active() && (!parmp->stdout_isatty || !input_isatty))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002514 {
2515 mch_errmsg(_("Vim: Error: Failure to start gvim from NetBeans\n"));
2516 exit(1);
2517 }
2518#endif
2519 if (!parmp->stdout_isatty)
2520 mch_errmsg(_("Vim: Warning: Output is not to a terminal\n"));
2521 if (!input_isatty)
2522 mch_errmsg(_("Vim: Warning: Input is not from a terminal\n"));
2523 out_flush();
2524 if (scriptin[0] == NULL)
2525 ui_delay(2000L, TRUE);
2526 TIME_MSG("Warning delay");
2527 }
2528}
2529
2530/*
2531 * Read text from stdin.
2532 */
2533 static void
2534read_stdin()
2535{
2536 int i;
2537
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002538#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002539 /* When getting the ATTENTION prompt here, use a dialog */
2540 swap_exists_action = SEA_DIALOG;
2541#endif
2542 no_wait_return = TRUE;
2543 i = msg_didany;
2544 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002545 (void)open_buffer(TRUE, NULL, 0); /* create memfile and read file */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002546 no_wait_return = FALSE;
2547 msg_didany = i;
2548 TIME_MSG("reading stdin");
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002549#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002550 check_swap_exists_action();
2551#endif
2552#if !(defined(AMIGA) || defined(MACOS))
2553 /*
2554 * Close stdin and dup it from stderr. Required for GPM to work
2555 * properly, and for running external commands.
2556 * Is there any other system that cannot do this?
2557 */
2558 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00002559 ignored = dup(2);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002560#endif
2561}
2562
2563/*
2564 * Create the requested number of windows and edit buffers in them.
2565 * Also does recovery if "recoverymode" set.
2566 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002567 static void
2568create_windows(parmp)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002569 mparm_T *parmp UNUSED;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002570{
2571#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002572 int dorewind;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002573 int done = 0;
2574
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002575 /*
2576 * Create the number of windows that was requested.
2577 */
2578 if (parmp->window_count == -1) /* was not set */
2579 parmp->window_count = 1;
2580 if (parmp->window_count == 0)
2581 parmp->window_count = GARGCOUNT;
2582 if (parmp->window_count > 1)
2583 {
2584 /* Don't change the windows if there was a command in .vimrc that
2585 * already split some windows */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002586 if (parmp->window_layout == 0)
2587 parmp->window_layout = WIN_HOR;
2588 if (parmp->window_layout == WIN_TABS)
2589 {
2590 parmp->window_count = make_tabpages(parmp->window_count);
2591 TIME_MSG("making tab pages");
2592 }
2593 else if (firstwin->w_next == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002594 {
2595 parmp->window_count = make_windows(parmp->window_count,
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002596 parmp->window_layout == WIN_VER);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002597 TIME_MSG("making windows");
2598 }
2599 else
2600 parmp->window_count = win_count();
2601 }
2602 else
2603 parmp->window_count = 1;
2604#endif
2605
2606 if (recoverymode) /* do recover */
2607 {
2608 msg_scroll = TRUE; /* scroll message up */
2609 ml_recover();
2610 if (curbuf->b_ml.ml_mfp == NULL) /* failed */
2611 getout(1);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002612 do_modelines(0); /* do modelines */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002613 }
2614 else
2615 {
2616 /*
2617 * Open a buffer for windows that don't have one yet.
2618 * Commands in the .vimrc might have loaded a file or split the window.
2619 * Watch out for autocommands that delete a window.
2620 */
2621#ifdef FEAT_AUTOCMD
2622 /*
2623 * Don't execute Win/Buf Enter/Leave autocommands here
2624 */
2625 ++autocmd_no_enter;
2626 ++autocmd_no_leave;
2627#endif
2628#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002629 dorewind = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002630 while (done++ < 1000)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002631 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002632 if (dorewind)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002633 {
2634 if (parmp->window_layout == WIN_TABS)
2635 goto_tabpage(1);
2636 else
2637 curwin = firstwin;
2638 }
2639 else if (parmp->window_layout == WIN_TABS)
2640 {
2641 if (curtab->tp_next == NULL)
2642 break;
2643 goto_tabpage(0);
2644 }
2645 else
2646 {
2647 if (curwin->w_next == NULL)
2648 break;
2649 curwin = curwin->w_next;
2650 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002651 dorewind = FALSE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002652#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002653 curbuf = curwin->w_buffer;
2654 if (curbuf->b_ml.ml_mfp == NULL)
2655 {
2656#ifdef FEAT_FOLDING
2657 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2658 if (p_fdls >= 0)
2659 curwin->w_p_fdl = p_fdls;
2660#endif
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002661#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002662 /* When getting the ATTENTION prompt here, use a dialog */
2663 swap_exists_action = SEA_DIALOG;
2664#endif
2665 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002666
2667 /* create memfile, read file */
2668 (void)open_buffer(FALSE, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002669
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002670#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar84212822006-11-07 21:59:47 +00002671 if (swap_exists_action == SEA_QUIT)
2672 {
2673 if (got_int || only_one_window())
2674 {
2675 /* abort selected or quit and only one window */
2676 did_emsg = FALSE; /* avoid hit-enter prompt */
2677 getout(1);
2678 }
2679 /* We can't close the window, it would disturb what
2680 * happens next. Clear the file name and set the arg
2681 * index to -1 to delete it later. */
2682 setfname(curbuf, NULL, NULL, FALSE);
2683 curwin->w_arg_idx = -1;
2684 swap_exists_action = SEA_NONE;
2685 }
2686 else
2687 handle_swap_exists(NULL);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002688#endif
2689#ifdef FEAT_AUTOCMD
Bram Moolenaar89d40322006-08-29 15:30:07 +00002690 dorewind = TRUE; /* start again */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002691#endif
2692 }
2693#ifdef FEAT_WINDOWS
2694 ui_breakcheck();
2695 if (got_int)
2696 {
2697 (void)vgetc(); /* only break the file loading, not the rest */
2698 break;
2699 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002700 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002701#endif
2702#ifdef FEAT_WINDOWS
2703 if (parmp->window_layout == WIN_TABS)
2704 goto_tabpage(1);
2705 else
2706 curwin = firstwin;
2707 curbuf = curwin->w_buffer;
2708#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002709#ifdef FEAT_AUTOCMD
2710 --autocmd_no_enter;
2711 --autocmd_no_leave;
2712#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002713 }
2714}
2715
2716#ifdef FEAT_WINDOWS
2717 /*
2718 * If opened more than one window, start editing files in the other
2719 * windows. make_windows() has already opened the windows.
2720 */
2721 static void
2722edit_buffers(parmp)
2723 mparm_T *parmp;
2724{
2725 int arg_idx; /* index in argument list */
2726 int i;
Bram Moolenaar84212822006-11-07 21:59:47 +00002727 int advance = TRUE;
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002728 win_T *win;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002729
2730# ifdef FEAT_AUTOCMD
2731 /*
2732 * Don't execute Win/Buf Enter/Leave autocommands here
2733 */
2734 ++autocmd_no_enter;
2735 ++autocmd_no_leave;
2736# endif
Bram Moolenaar84212822006-11-07 21:59:47 +00002737
2738 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2739 if (curwin->w_arg_idx == -1)
2740 {
2741 win_close(curwin, TRUE);
2742 advance = FALSE;
2743 }
2744
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002745 arg_idx = 1;
2746 for (i = 1; i < parmp->window_count; ++i)
2747 {
Bram Moolenaar84212822006-11-07 21:59:47 +00002748 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2749 if (curwin->w_arg_idx == -1)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002750 {
Bram Moolenaar84212822006-11-07 21:59:47 +00002751 ++arg_idx;
2752 win_close(curwin, TRUE);
2753 advance = FALSE;
2754 continue;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002755 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002756
Bram Moolenaar84212822006-11-07 21:59:47 +00002757 if (advance)
2758 {
2759 if (parmp->window_layout == WIN_TABS)
2760 {
2761 if (curtab->tp_next == NULL) /* just checking */
2762 break;
2763 goto_tabpage(0);
2764 }
2765 else
2766 {
2767 if (curwin->w_next == NULL) /* just checking */
2768 break;
2769 win_enter(curwin->w_next, FALSE);
2770 }
2771 }
2772 advance = TRUE;
2773
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002774 /* Only open the file if there is no file in this window yet (that can
Bram Moolenaar84212822006-11-07 21:59:47 +00002775 * happen when .vimrc contains ":sall"). */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002776 if (curbuf == firstwin->w_buffer || curbuf->b_ffname == NULL)
2777 {
2778 curwin->w_arg_idx = arg_idx;
Bram Moolenaar84212822006-11-07 21:59:47 +00002779 /* Edit file from arg list, if there is one. When "Quit" selected
2780 * at the ATTENTION prompt close the window. */
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002781# ifdef HAS_SWAP_EXISTS_ACTION
2782 swap_exists_did_quit = FALSE;
2783# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002784 (void)do_ecmd(0, arg_idx < GARGCOUNT
2785 ? alist_name(&GARGLIST[arg_idx]) : NULL,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002786 NULL, NULL, ECMD_LASTL, ECMD_HIDE, curwin);
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002787# ifdef HAS_SWAP_EXISTS_ACTION
2788 if (swap_exists_did_quit)
Bram Moolenaar84212822006-11-07 21:59:47 +00002789 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002790 /* abort or quit selected */
Bram Moolenaar84212822006-11-07 21:59:47 +00002791 if (got_int || only_one_window())
2792 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002793 /* abort selected and only one window */
Bram Moolenaar84212822006-11-07 21:59:47 +00002794 did_emsg = FALSE; /* avoid hit-enter prompt */
2795 getout(1);
2796 }
2797 win_close(curwin, TRUE);
2798 advance = FALSE;
2799 }
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002800# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002801 if (arg_idx == GARGCOUNT - 1)
2802 arg_had_last = TRUE;
2803 ++arg_idx;
2804 }
2805 ui_breakcheck();
2806 if (got_int)
2807 {
2808 (void)vgetc(); /* only break the file loading, not the rest */
2809 break;
2810 }
2811 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002812
2813 if (parmp->window_layout == WIN_TABS)
2814 goto_tabpage(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002815# ifdef FEAT_AUTOCMD
2816 --autocmd_no_enter;
2817# endif
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002818
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002819 /* make the first window the current window */
2820 win = firstwin;
2821#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
2822 /* Avoid making a preview window the current window. */
2823 while (win->w_p_pvw)
2824 {
2825 win = win->w_next;
2826 if (win == NULL)
2827 {
2828 win = firstwin;
2829 break;
2830 }
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002831 }
2832#endif
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002833 win_enter(win, FALSE);
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002834
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002835# ifdef FEAT_AUTOCMD
2836 --autocmd_no_leave;
2837# endif
2838 TIME_MSG("editing files in windows");
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002839 if (parmp->window_count > 1 && parmp->window_layout != WIN_TABS)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002840 win_equal(curwin, FALSE, 'b'); /* adjust heights */
2841}
2842#endif /* FEAT_WINDOWS */
2843
2844/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002845 * Execute the commands from --cmd arguments "cmds[cnt]".
2846 */
2847 static void
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002848exe_pre_commands(parmp)
2849 mparm_T *parmp;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002850{
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002851 char_u **cmds = parmp->pre_commands;
2852 int cnt = parmp->n_pre_commands;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002853 int i;
2854
2855 if (cnt > 0)
2856 {
2857 curwin->w_cursor.lnum = 0; /* just in case.. */
2858 sourcing_name = (char_u *)_("pre-vimrc command line");
2859# ifdef FEAT_EVAL
2860 current_SID = SID_CMDARG;
2861# endif
2862 for (i = 0; i < cnt; ++i)
2863 do_cmdline_cmd(cmds[i]);
2864 sourcing_name = NULL;
2865# ifdef FEAT_EVAL
2866 current_SID = 0;
2867# endif
2868 TIME_MSG("--cmd commands");
2869 }
2870}
2871
2872/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002873 * Execute "+", "-c" and "-S" arguments.
2874 */
2875 static void
2876exe_commands(parmp)
2877 mparm_T *parmp;
2878{
2879 int i;
2880
2881 /*
2882 * We start commands on line 0, make "vim +/pat file" match a
2883 * pattern on line 1. But don't move the cursor when an autocommand
2884 * with g`" was used.
2885 */
2886 msg_scroll = TRUE;
2887 if (parmp->tagname == NULL && curwin->w_cursor.lnum <= 1)
2888 curwin->w_cursor.lnum = 0;
2889 sourcing_name = (char_u *)"command line";
2890#ifdef FEAT_EVAL
2891 current_SID = SID_CARG;
2892#endif
2893 for (i = 0; i < parmp->n_commands; ++i)
2894 {
2895 do_cmdline_cmd(parmp->commands[i]);
2896 if (parmp->cmds_tofree[i])
2897 vim_free(parmp->commands[i]);
2898 }
2899 sourcing_name = NULL;
2900#ifdef FEAT_EVAL
2901 current_SID = 0;
2902#endif
2903 if (curwin->w_cursor.lnum == 0)
2904 curwin->w_cursor.lnum = 1;
2905
2906 if (!exmode_active)
2907 msg_scroll = FALSE;
2908
2909#ifdef FEAT_QUICKFIX
2910 /* When started with "-q errorfile" jump to first error again. */
2911 if (parmp->edit_type == EDIT_QF)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002912 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002913#endif
2914 TIME_MSG("executing command arguments");
2915}
2916
2917/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002918 * Source startup scripts.
2919 */
2920 static void
2921source_startup_scripts(parmp)
2922 mparm_T *parmp;
2923{
2924 int i;
2925
2926 /*
2927 * For "evim" source evim.vim first of all, so that the user can overrule
2928 * any things he doesn't like.
2929 */
2930 if (parmp->evim_mode)
2931 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002932 (void)do_source((char_u *)EVIM_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002933 TIME_MSG("source evim file");
2934 }
2935
2936 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002937 * If -u argument given, use only the initializations from that file and
Bram Moolenaar58d98232005-07-23 22:25:46 +00002938 * nothing else.
2939 */
2940 if (parmp->use_vimrc != NULL)
2941 {
Bram Moolenaar231334e2005-07-25 20:46:57 +00002942 if (STRCMP(parmp->use_vimrc, "NONE") == 0
2943 || STRCMP(parmp->use_vimrc, "NORC") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002944 {
2945#ifdef FEAT_GUI
2946 if (use_gvimrc == NULL) /* don't load gvimrc either */
2947 use_gvimrc = parmp->use_vimrc;
2948#endif
2949 if (parmp->use_vimrc[2] == 'N')
2950 p_lpl = FALSE; /* don't load plugins either */
2951 }
2952 else
2953 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002954 if (do_source(parmp->use_vimrc, FALSE, DOSO_NONE) != OK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002955 EMSG2(_("E282: Cannot read from \"%s\""), parmp->use_vimrc);
2956 }
2957 }
2958 else if (!silent_mode)
2959 {
2960#ifdef AMIGA
2961 struct Process *proc = (struct Process *)FindTask(0L);
2962 APTR save_winptr = proc->pr_WindowPtr;
2963
2964 /* Avoid a requester here for a volume that doesn't exist. */
2965 proc->pr_WindowPtr = (APTR)-1L;
2966#endif
2967
2968 /*
2969 * Get system wide defaults, if the file name is defined.
2970 */
2971#ifdef SYS_VIMRC_FILE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002972 (void)do_source((char_u *)SYS_VIMRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002973#endif
Bram Moolenaar1056d982006-03-09 22:37:52 +00002974#ifdef MACOS_X
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002975 (void)do_source((char_u *)"$VIMRUNTIME/macmap.vim", FALSE, DOSO_NONE);
Bram Moolenaar1056d982006-03-09 22:37:52 +00002976#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00002977
2978 /*
2979 * Try to read initialization commands from the following places:
2980 * - environment variable VIMINIT
2981 * - user vimrc file (s:.vimrc for Amiga, ~/.vimrc otherwise)
2982 * - second user vimrc file ($VIM/.vimrc for Dos)
2983 * - environment variable EXINIT
2984 * - user exrc file (s:.exrc for Amiga, ~/.exrc otherwise)
2985 * - second user exrc file ($VIM/.exrc for Dos)
2986 * The first that exists is used, the rest is ignored.
2987 */
2988 if (process_env((char_u *)"VIMINIT", TRUE) != OK)
2989 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002990 if (do_source((char_u *)USR_VIMRC_FILE, TRUE, DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00002991#ifdef USR_VIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002992 && do_source((char_u *)USR_VIMRC_FILE2, TRUE,
2993 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00002994#endif
2995#ifdef USR_VIMRC_FILE3
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002996 && do_source((char_u *)USR_VIMRC_FILE3, TRUE,
2997 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00002998#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +02002999#ifdef USR_VIMRC_FILE4
3000 && do_source((char_u *)USR_VIMRC_FILE4, TRUE,
3001 DOSO_VIMRC) == FAIL
3002#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003003 && process_env((char_u *)"EXINIT", FALSE) == FAIL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003004 && do_source((char_u *)USR_EXRC_FILE, FALSE, DOSO_NONE) == FAIL)
Bram Moolenaar58d98232005-07-23 22:25:46 +00003005 {
3006#ifdef USR_EXRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003007 (void)do_source((char_u *)USR_EXRC_FILE2, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003008#endif
3009 }
3010 }
3011
3012 /*
3013 * Read initialization commands from ".vimrc" or ".exrc" in current
3014 * directory. This is only done if the 'exrc' option is set.
3015 * Because of security reasons we disallow shell and write commands
3016 * now, except for unix if the file is owned by the user or 'secure'
3017 * option has been reset in environment of global ".exrc" or ".vimrc".
3018 * Only do this if VIMRC_FILE is not the same as USR_VIMRC_FILE or
3019 * SYS_VIMRC_FILE.
3020 */
3021 if (p_exrc)
3022 {
3023#if defined(UNIX) || defined(VMS)
3024 /* If ".vimrc" file is not owned by user, set 'secure' mode. */
3025 if (!file_owned(VIMRC_FILE))
3026#endif
3027 secure = p_secure;
3028
3029 i = FAIL;
3030 if (fullpathcmp((char_u *)USR_VIMRC_FILE,
3031 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3032#ifdef USR_VIMRC_FILE2
3033 && fullpathcmp((char_u *)USR_VIMRC_FILE2,
3034 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3035#endif
3036#ifdef USR_VIMRC_FILE3
3037 && fullpathcmp((char_u *)USR_VIMRC_FILE3,
3038 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3039#endif
3040#ifdef SYS_VIMRC_FILE
3041 && fullpathcmp((char_u *)SYS_VIMRC_FILE,
3042 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3043#endif
3044 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003045 i = do_source((char_u *)VIMRC_FILE, TRUE, DOSO_VIMRC);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003046
3047 if (i == FAIL)
3048 {
3049#if defined(UNIX) || defined(VMS)
3050 /* if ".exrc" is not owned by user set 'secure' mode */
3051 if (!file_owned(EXRC_FILE))
3052 secure = p_secure;
3053 else
3054 secure = 0;
3055#endif
3056 if ( fullpathcmp((char_u *)USR_EXRC_FILE,
3057 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3058#ifdef USR_EXRC_FILE2
3059 && fullpathcmp((char_u *)USR_EXRC_FILE2,
3060 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3061#endif
3062 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003063 (void)do_source((char_u *)EXRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003064 }
3065 }
3066 if (secure == 2)
3067 need_wait_return = TRUE;
3068 secure = 0;
3069#ifdef AMIGA
3070 proc->pr_WindowPtr = save_winptr;
3071#endif
3072 }
3073 TIME_MSG("sourcing vimrc file(s)");
3074}
3075
3076/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003077 * Setup to start using the GUI. Exit with an error when not available.
3078 */
3079 static void
3080main_start_gui()
3081{
3082#ifdef FEAT_GUI
3083 gui.starting = TRUE; /* start GUI a bit later */
3084#else
3085 mch_errmsg(_(e_nogvim));
3086 mch_errmsg("\n");
3087 mch_exit(2);
3088#endif
3089}
3090
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003091#endif /* NO_VIM_MAIN */
3092
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003093/*
Bram Moolenaar49325942007-05-10 19:19:59 +00003094 * Get an environment variable, and execute it as Ex commands.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003095 * Returns FAIL if the environment variable was not executed, OK otherwise.
3096 */
3097 int
3098process_env(env, is_viminit)
3099 char_u *env;
3100 int is_viminit; /* when TRUE, called for VIMINIT */
3101{
3102 char_u *initstr;
3103 char_u *save_sourcing_name;
3104 linenr_T save_sourcing_lnum;
3105#ifdef FEAT_EVAL
3106 scid_T save_sid;
3107#endif
3108
3109 if ((initstr = mch_getenv(env)) != NULL && *initstr != NUL)
3110 {
3111 if (is_viminit)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003112 vimrc_found(NULL, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003113 save_sourcing_name = sourcing_name;
3114 save_sourcing_lnum = sourcing_lnum;
3115 sourcing_name = env;
3116 sourcing_lnum = 0;
3117#ifdef FEAT_EVAL
3118 save_sid = current_SID;
3119 current_SID = SID_ENV;
3120#endif
3121 do_cmdline_cmd(initstr);
3122 sourcing_name = save_sourcing_name;
3123 sourcing_lnum = save_sourcing_lnum;
3124#ifdef FEAT_EVAL
3125 current_SID = save_sid;;
3126#endif
3127 return OK;
3128 }
3129 return FAIL;
3130}
3131
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003132#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003133/*
3134 * Return TRUE if we are certain the user owns the file "fname".
3135 * Used for ".vimrc" and ".exrc".
3136 * Use both stat() and lstat() for extra security.
3137 */
3138 static int
3139file_owned(fname)
3140 char *fname;
3141{
3142 struct stat s;
3143# ifdef UNIX
3144 uid_t uid = getuid();
3145# else /* VMS */
3146 uid_t uid = ((getgid() << 16) | getuid());
3147# endif
3148
3149 return !(mch_stat(fname, &s) != 0 || s.st_uid != uid
3150# ifdef HAVE_LSTAT
3151 || mch_lstat(fname, &s) != 0 || s.st_uid != uid
3152# endif
3153 );
3154}
3155#endif
3156
3157/*
3158 * Give an error message main_errors["n"] and exit.
3159 */
3160 static void
3161mainerr(n, str)
3162 int n; /* one of the ME_ defines */
3163 char_u *str; /* extra argument or NULL */
3164{
3165#if defined(UNIX) || defined(__EMX__) || defined(VMS)
3166 reset_signals(); /* kill us with CTRL-C here, if you like */
3167#endif
3168
3169 mch_errmsg(longVersion);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003170 mch_errmsg("\n");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003171 mch_errmsg(_(main_errors[n]));
3172 if (str != NULL)
3173 {
3174 mch_errmsg(": \"");
3175 mch_errmsg((char *)str);
3176 mch_errmsg("\"");
3177 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003178 mch_errmsg(_("\nMore info with: \"vim -h\"\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003179
3180 mch_exit(1);
3181}
3182
3183 void
3184mainerr_arg_missing(str)
3185 char_u *str;
3186{
3187 mainerr(ME_ARG_MISSING, str);
3188}
3189
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003190#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003191/*
3192 * print a message with three spaces prepended and '\n' appended.
3193 */
3194 static void
3195main_msg(s)
3196 char *s;
3197{
3198 mch_msg(" ");
3199 mch_msg(s);
3200 mch_msg("\n");
3201}
3202
3203/*
3204 * Print messages for "vim -h" or "vim --help" and exit.
3205 */
3206 static void
3207usage()
3208{
3209 int i;
3210 static char *(use[]) =
3211 {
3212 N_("[file ..] edit specified file(s)"),
3213 N_("- read text from stdin"),
3214 N_("-t tag edit file where tag is defined"),
3215#ifdef FEAT_QUICKFIX
3216 N_("-q [errorfile] edit file with first error")
3217#endif
3218 };
3219
3220#if defined(UNIX) || defined(__EMX__) || defined(VMS)
3221 reset_signals(); /* kill us with CTRL-C here, if you like */
3222#endif
3223
3224 mch_msg(longVersion);
3225 mch_msg(_("\n\nusage:"));
3226 for (i = 0; ; ++i)
3227 {
3228 mch_msg(_(" vim [arguments] "));
3229 mch_msg(_(use[i]));
3230 if (i == (sizeof(use) / sizeof(char_u *)) - 1)
3231 break;
3232 mch_msg(_("\n or:"));
3233 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003234#ifdef VMS
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003235 mch_msg(_("\nWhere case is ignored prepend / to make flag upper case"));
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003236#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003237
3238 mch_msg(_("\n\nArguments:\n"));
3239 main_msg(_("--\t\t\tOnly file names after this"));
3240#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
3241 main_msg(_("--literal\t\tDon't expand wildcards"));
3242#endif
3243#ifdef FEAT_OLE
3244 main_msg(_("-register\t\tRegister this gvim for OLE"));
3245 main_msg(_("-unregister\t\tUnregister gvim for OLE"));
3246#endif
3247#ifdef FEAT_GUI
3248 main_msg(_("-g\t\t\tRun using GUI (like \"gvim\")"));
3249 main_msg(_("-f or --nofork\tForeground: Don't fork when starting GUI"));
3250#endif
3251 main_msg(_("-v\t\t\tVi mode (like \"vi\")"));
3252 main_msg(_("-e\t\t\tEx mode (like \"ex\")"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003253 main_msg(_("-E\t\t\tImproved Ex mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003254 main_msg(_("-s\t\t\tSilent (batch) mode (only for \"ex\")"));
3255#ifdef FEAT_DIFF
3256 main_msg(_("-d\t\t\tDiff mode (like \"vimdiff\")"));
3257#endif
3258 main_msg(_("-y\t\t\tEasy mode (like \"evim\", modeless)"));
3259 main_msg(_("-R\t\t\tReadonly mode (like \"view\")"));
3260 main_msg(_("-Z\t\t\tRestricted mode (like \"rvim\")"));
3261 main_msg(_("-m\t\t\tModifications (writing files) not allowed"));
3262 main_msg(_("-M\t\t\tModifications in text not allowed"));
3263 main_msg(_("-b\t\t\tBinary mode"));
3264#ifdef FEAT_LISP
3265 main_msg(_("-l\t\t\tLisp mode"));
3266#endif
3267 main_msg(_("-C\t\t\tCompatible with Vi: 'compatible'"));
3268 main_msg(_("-N\t\t\tNot fully Vi compatible: 'nocompatible'"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003269 main_msg(_("-V[N][fname]\t\tBe verbose [level N] [log messages to fname]"));
3270#ifdef FEAT_EVAL
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003271 main_msg(_("-D\t\t\tDebugging mode"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003272#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003273 main_msg(_("-n\t\t\tNo swap file, use memory only"));
3274 main_msg(_("-r\t\t\tList swap files and exit"));
3275 main_msg(_("-r (with file name)\tRecover crashed session"));
3276 main_msg(_("-L\t\t\tSame as -r"));
3277#ifdef AMIGA
3278 main_msg(_("-f\t\t\tDon't use newcli to open window"));
3279 main_msg(_("-dev <device>\t\tUse <device> for I/O"));
3280#endif
3281#ifdef FEAT_ARABIC
3282 main_msg(_("-A\t\t\tstart in Arabic mode"));
3283#endif
3284#ifdef FEAT_RIGHTLEFT
3285 main_msg(_("-H\t\t\tStart in Hebrew mode"));
3286#endif
3287#ifdef FEAT_FKMAP
3288 main_msg(_("-F\t\t\tStart in Farsi mode"));
3289#endif
3290 main_msg(_("-T <terminal>\tSet terminal type to <terminal>"));
3291 main_msg(_("-u <vimrc>\t\tUse <vimrc> instead of any .vimrc"));
3292#ifdef FEAT_GUI
3293 main_msg(_("-U <gvimrc>\t\tUse <gvimrc> instead of any .gvimrc"));
3294#endif
3295 main_msg(_("--noplugin\t\tDon't load plugin scripts"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003296#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003297 main_msg(_("-p[N]\t\tOpen N tab pages (default: one for each file)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003298 main_msg(_("-o[N]\t\tOpen N windows (default: one for each file)"));
3299 main_msg(_("-O[N]\t\tLike -o but split vertically"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003300#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003301 main_msg(_("+\t\t\tStart at end of file"));
3302 main_msg(_("+<lnum>\t\tStart at line <lnum>"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003303 main_msg(_("--cmd <command>\tExecute <command> before loading any vimrc file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003304 main_msg(_("-c <command>\t\tExecute <command> after loading the first file"));
3305 main_msg(_("-S <session>\t\tSource file <session> after loading the first file"));
3306 main_msg(_("-s <scriptin>\tRead Normal mode commands from file <scriptin>"));
3307 main_msg(_("-w <scriptout>\tAppend all typed commands to file <scriptout>"));
3308 main_msg(_("-W <scriptout>\tWrite all typed commands to file <scriptout>"));
3309#ifdef FEAT_CRYPT
3310 main_msg(_("-x\t\t\tEdit encrypted files"));
3311#endif
3312#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
3313# if defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)
3314 main_msg(_("-display <display>\tConnect vim to this particular X-server"));
3315# endif
3316 main_msg(_("-X\t\t\tDo not connect to X server"));
3317#endif
3318#ifdef FEAT_CLIENTSERVER
3319 main_msg(_("--remote <files>\tEdit <files> in a Vim server if possible"));
3320 main_msg(_("--remote-silent <files> Same, don't complain if there is no server"));
3321 main_msg(_("--remote-wait <files> As --remote but wait for files to have been edited"));
3322 main_msg(_("--remote-wait-silent <files> Same, don't complain if there is no server"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003323# ifdef FEAT_WINDOWS
Bram Moolenaar82ad3242008-01-11 19:26:36 +00003324 main_msg(_("--remote-tab[-wait][-silent] <files> As --remote but use tab page per file"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003325# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003326 main_msg(_("--remote-send <keys>\tSend <keys> to a Vim server and exit"));
3327 main_msg(_("--remote-expr <expr>\tEvaluate <expr> in a Vim server and print result"));
3328 main_msg(_("--serverlist\t\tList available Vim server names and exit"));
3329 main_msg(_("--servername <name>\tSend to/become the Vim server <name>"));
3330#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003331#ifdef STARTUPTIME
Bram Moolenaar34ef52d2009-11-17 11:31:25 +00003332 main_msg(_("--startuptime <file>\tWrite startup timing messages to <file>"));
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003333#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003334#ifdef FEAT_VIMINFO
3335 main_msg(_("-i <viminfo>\t\tUse <viminfo> instead of .viminfo"));
3336#endif
3337 main_msg(_("-h or --help\tPrint Help (this message) and exit"));
3338 main_msg(_("--version\t\tPrint version information and exit"));
3339
3340#ifdef FEAT_GUI_X11
3341# ifdef FEAT_GUI_MOTIF
3342 mch_msg(_("\nArguments recognised by gvim (Motif version):\n"));
3343# else
3344# ifdef FEAT_GUI_ATHENA
3345# ifdef FEAT_GUI_NEXTAW
3346 mch_msg(_("\nArguments recognised by gvim (neXtaw version):\n"));
3347# else
3348 mch_msg(_("\nArguments recognised by gvim (Athena version):\n"));
3349# endif
3350# endif
3351# endif
3352 main_msg(_("-display <display>\tRun vim on <display>"));
3353 main_msg(_("-iconic\t\tStart vim iconified"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003354 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
3355 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
3356 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3357 main_msg(_("-boldfont <font>\tUse <font> for bold text"));
3358 main_msg(_("-italicfont <font>\tUse <font> for italic text"));
3359 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3360 main_msg(_("-borderwidth <width>\tUse a border width of <width> (also: -bw)"));
3361 main_msg(_("-scrollbarwidth <width> Use a scrollbar width of <width> (also: -sw)"));
3362# ifdef FEAT_GUI_ATHENA
3363 main_msg(_("-menuheight <height>\tUse a menu bar height of <height> (also: -mh)"));
3364# endif
3365 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3366 main_msg(_("+reverse\t\tDon't use reverse video (also: +rv)"));
3367 main_msg(_("-xrm <resource>\tSet the specified resource"));
3368#endif /* FEAT_GUI_X11 */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003369#ifdef FEAT_GUI_GTK
3370 mch_msg(_("\nArguments recognised by gvim (GTK+ version):\n"));
3371 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3372 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3373 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3374 main_msg(_("-display <display>\tRun vim on <display> (also: --display)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003375 main_msg(_("--role <role>\tSet a unique role to identify the main window"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003376 main_msg(_("--socketid <xid>\tOpen Vim inside another GTK widget"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003377 main_msg(_("--echo-wid\t\tMake gvim echo the Window ID on stdout"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003378#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003379#ifdef FEAT_GUI_W32
3380 main_msg(_("-P <parent title>\tOpen Vim inside parent application"));
Bram Moolenaar78e17622007-08-30 10:26:19 +00003381 main_msg(_("--windowid <HWND>\tOpen Vim inside another win32 widget"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003382#endif
3383
3384#ifdef FEAT_GUI_GNOME
3385 /* Gnome gives extra messages for --help if we continue, but not for -h. */
3386 if (gui.starting)
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003387 {
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003388 mch_msg("\n");
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003389 gui.dofork = FALSE;
3390 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003391 else
3392#endif
3393 mch_exit(0);
3394}
3395
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00003396#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003397/*
3398 * Check the result of the ATTENTION dialog:
3399 * When "Quit" selected, exit Vim.
3400 * When "Recover" selected, recover the file.
3401 */
3402 static void
3403check_swap_exists_action()
3404{
3405 if (swap_exists_action == SEA_QUIT)
3406 getout(1);
3407 handle_swap_exists(NULL);
3408}
3409#endif
3410
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003411#endif
3412
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003413#if defined(STARTUPTIME) || defined(PROTO)
3414static void time_diff __ARGS((struct timeval *then, struct timeval *now));
3415
3416static struct timeval prev_timeval;
3417
Bram Moolenaar3f269672009-11-03 11:11:11 +00003418# ifdef WIN3264
3419/*
3420 * Windows doesn't have gettimeofday(), although it does have struct timeval.
3421 */
3422 static int
3423gettimeofday(struct timeval *tv, char *dummy)
3424{
3425 long t = clock();
3426 tv->tv_sec = t / CLOCKS_PER_SEC;
3427 tv->tv_usec = (t - tv->tv_sec * CLOCKS_PER_SEC) * 1000000 / CLOCKS_PER_SEC;
3428 return 0;
3429}
3430# endif
3431
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003432/*
3433 * Save the previous time before doing something that could nest.
3434 * set "*tv_rel" to the time elapsed so far.
3435 */
3436 void
3437time_push(tv_rel, tv_start)
3438 void *tv_rel, *tv_start;
3439{
3440 *((struct timeval *)tv_rel) = prev_timeval;
3441 gettimeofday(&prev_timeval, NULL);
3442 ((struct timeval *)tv_rel)->tv_usec = prev_timeval.tv_usec
3443 - ((struct timeval *)tv_rel)->tv_usec;
3444 ((struct timeval *)tv_rel)->tv_sec = prev_timeval.tv_sec
3445 - ((struct timeval *)tv_rel)->tv_sec;
3446 if (((struct timeval *)tv_rel)->tv_usec < 0)
3447 {
3448 ((struct timeval *)tv_rel)->tv_usec += 1000000;
3449 --((struct timeval *)tv_rel)->tv_sec;
3450 }
3451 *(struct timeval *)tv_start = prev_timeval;
3452}
3453
3454/*
3455 * Compute the previous time after doing something that could nest.
3456 * Subtract "*tp" from prev_timeval;
3457 * Note: The arguments are (void *) to avoid trouble with systems that don't
3458 * have struct timeval.
3459 */
3460 void
3461time_pop(tp)
3462 void *tp; /* actually (struct timeval *) */
3463{
3464 prev_timeval.tv_usec -= ((struct timeval *)tp)->tv_usec;
3465 prev_timeval.tv_sec -= ((struct timeval *)tp)->tv_sec;
3466 if (prev_timeval.tv_usec < 0)
3467 {
3468 prev_timeval.tv_usec += 1000000;
3469 --prev_timeval.tv_sec;
3470 }
3471}
3472
3473 static void
3474time_diff(then, now)
3475 struct timeval *then;
3476 struct timeval *now;
3477{
3478 long usec;
3479 long msec;
3480
3481 usec = now->tv_usec - then->tv_usec;
3482 msec = (now->tv_sec - then->tv_sec) * 1000L + usec / 1000L,
3483 usec = usec % 1000L;
3484 fprintf(time_fd, "%03ld.%03ld", msec, usec >= 0 ? usec : usec + 1000L);
3485}
3486
3487 void
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003488time_msg(mesg, tv_start)
3489 char *mesg;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003490 void *tv_start; /* only for do_source: start time; actually
3491 (struct timeval *) */
3492{
3493 static struct timeval start;
3494 struct timeval now;
3495
3496 if (time_fd != NULL)
3497 {
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003498 if (strstr(mesg, "STARTING") != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003499 {
3500 gettimeofday(&start, NULL);
3501 prev_timeval = start;
3502 fprintf(time_fd, "\n\ntimes in msec\n");
3503 fprintf(time_fd, " clock self+sourced self: sourced script\n");
3504 fprintf(time_fd, " clock elapsed: other lines\n\n");
3505 }
3506 gettimeofday(&now, NULL);
3507 time_diff(&start, &now);
3508 if (((struct timeval *)tv_start) != NULL)
3509 {
3510 fprintf(time_fd, " ");
3511 time_diff(((struct timeval *)tv_start), &now);
3512 }
3513 fprintf(time_fd, " ");
3514 time_diff(&prev_timeval, &now);
3515 prev_timeval = now;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003516 fprintf(time_fd, ": %s\n", mesg);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003517 }
3518}
3519
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003520#endif
3521
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003522#if (defined(FEAT_CLIENTSERVER) && !defined(NO_VIM_MAIN)) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003523
3524/*
3525 * Common code for the X command server and the Win32 command server.
3526 */
3527
Bram Moolenaareb94e552006-03-11 21:35:11 +00003528static char_u *build_drop_cmd __ARGS((int filec, char **filev, int tabs, int sendReply));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003529
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003530/*
3531 * Do the client-server stuff, unless "--servername ''" was used.
3532 */
3533 static void
3534exec_on_server(parmp)
3535 mparm_T *parmp;
3536{
3537 if (parmp->serverName_arg == NULL || *parmp->serverName_arg != NUL)
3538 {
3539# ifdef WIN32
3540 /* Initialise the client/server messaging infrastructure. */
3541 serverInitMessaging();
3542# endif
3543
3544 /*
3545 * When a command server argument was found, execute it. This may
3546 * exit Vim when it was successful. Otherwise it's executed further
3547 * on. Remember the encoding used here in "serverStrEnc".
3548 */
3549 if (parmp->serverArg)
3550 {
3551 cmdsrv_main(&parmp->argc, parmp->argv,
3552 parmp->serverName_arg, &parmp->serverStr);
3553# ifdef FEAT_MBYTE
3554 parmp->serverStrEnc = vim_strsave(p_enc);
3555# endif
3556 }
3557
3558 /* If we're still running, get the name to register ourselves.
3559 * On Win32 can register right now, for X11 need to setup the
3560 * clipboard first, it's further down. */
3561 parmp->servername = serverMakeName(parmp->serverName_arg,
3562 parmp->argv[0]);
3563# ifdef WIN32
3564 if (parmp->servername != NULL)
3565 {
3566 serverSetName(parmp->servername);
3567 vim_free(parmp->servername);
3568 }
3569# endif
3570 }
3571}
3572
3573/*
3574 * Prepare for running as a Vim server.
3575 */
3576 static void
3577prepare_server(parmp)
3578 mparm_T *parmp;
3579{
3580# if defined(FEAT_X11)
3581 /*
3582 * Register for remote command execution with :serversend and --remote
3583 * unless there was a -X or a --servername '' on the command line.
3584 * Only register nongui-vim's with an explicit --servername argument.
Bram Moolenaar5f402312006-08-15 19:40:35 +00003585 * When running as root --servername is also required.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003586 */
3587 if (X_DISPLAY != NULL && parmp->servername != NULL && (
3588# ifdef FEAT_GUI
Bram Moolenaar5f402312006-08-15 19:40:35 +00003589 (gui.in_use
3590# ifdef UNIX
Bram Moolenaar311d9822007-02-27 15:48:28 +00003591 && getuid() != ROOT_UID
Bram Moolenaar5f402312006-08-15 19:40:35 +00003592# endif
3593 ) ||
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003594# endif
3595 parmp->serverName_arg != NULL))
3596 {
3597 (void)serverRegisterName(X_DISPLAY, parmp->servername);
3598 vim_free(parmp->servername);
3599 TIME_MSG("register server name");
3600 }
3601 else
3602 serverDelayedStartName = parmp->servername;
3603# endif
3604
3605 /*
3606 * Execute command ourselves if we're here because the send failed (or
3607 * else we would have exited above).
3608 */
3609 if (parmp->serverStr != NULL)
3610 {
3611 char_u *p;
3612
3613 server_to_input_buf(serverConvert(parmp->serverStrEnc,
3614 parmp->serverStr, &p));
3615 vim_free(p);
3616 }
3617}
3618
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003619 static void
3620cmdsrv_main(argc, argv, serverName_arg, serverStr)
3621 int *argc;
3622 char **argv;
3623 char_u *serverName_arg;
3624 char_u **serverStr;
3625{
3626 char_u *res;
3627 int i;
3628 char_u *sname;
3629 int ret;
3630 int didone = FALSE;
3631 int exiterr = 0;
3632 char **newArgV = argv + 1;
3633 int newArgC = 1,
3634 Argc = *argc;
3635 int argtype;
3636#define ARGTYPE_OTHER 0
3637#define ARGTYPE_EDIT 1
3638#define ARGTYPE_EDIT_WAIT 2
3639#define ARGTYPE_SEND 3
3640 int silent = FALSE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003641 int tabs = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003642# ifndef FEAT_X11
3643 HWND srv;
3644# else
3645 Window srv;
3646
3647 setup_term_clip();
3648# endif
3649
3650 sname = serverMakeName(serverName_arg, argv[0]);
3651 if (sname == NULL)
3652 return;
3653
3654 /*
3655 * Execute the command server related arguments and remove them
3656 * from the argc/argv array; We may have to return into main()
3657 */
3658 for (i = 1; i < Argc; i++)
3659 {
3660 res = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003661 if (STRCMP(argv[i], "--") == 0) /* end of option arguments */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003662 {
3663 for (; i < *argc; i++)
3664 {
3665 *newArgV++ = argv[i];
3666 newArgC++;
3667 }
3668 break;
3669 }
3670
Bram Moolenaareb94e552006-03-11 21:35:11 +00003671 if (STRICMP(argv[i], "--remote-send") == 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003672 argtype = ARGTYPE_SEND;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003673 else if (STRNICMP(argv[i], "--remote", 8) == 0)
3674 {
3675 char *p = argv[i] + 8;
3676
3677 argtype = ARGTYPE_EDIT;
3678 while (*p != NUL)
3679 {
3680 if (STRNICMP(p, "-wait", 5) == 0)
3681 {
3682 argtype = ARGTYPE_EDIT_WAIT;
3683 p += 5;
3684 }
3685 else if (STRNICMP(p, "-silent", 7) == 0)
3686 {
3687 silent = TRUE;
3688 p += 7;
3689 }
3690 else if (STRNICMP(p, "-tab", 4) == 0)
3691 {
3692 tabs = TRUE;
3693 p += 4;
3694 }
3695 else
3696 {
3697 argtype = ARGTYPE_OTHER;
3698 break;
3699 }
3700 }
3701 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003702 else
3703 argtype = ARGTYPE_OTHER;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003704
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003705 if (argtype != ARGTYPE_OTHER)
3706 {
3707 if (i == *argc - 1)
3708 mainerr_arg_missing((char_u *)argv[i]);
3709 if (argtype == ARGTYPE_SEND)
3710 {
3711 *serverStr = (char_u *)argv[i + 1];
3712 i++;
3713 }
3714 else
3715 {
3716 *serverStr = build_drop_cmd(*argc - i - 1, argv + i + 1,
Bram Moolenaareb94e552006-03-11 21:35:11 +00003717 tabs, argtype == ARGTYPE_EDIT_WAIT);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003718 if (*serverStr == NULL)
3719 {
3720 /* Probably out of memory, exit. */
3721 didone = TRUE;
3722 exiterr = 1;
3723 break;
3724 }
3725 Argc = i;
3726 }
3727# ifdef FEAT_X11
3728 if (xterm_dpy == NULL)
3729 {
3730 mch_errmsg(_("No display"));
3731 ret = -1;
3732 }
3733 else
3734 ret = serverSendToVim(xterm_dpy, sname, *serverStr,
3735 NULL, &srv, 0, 0, silent);
3736# else
3737 /* Win32 always works? */
3738 ret = serverSendToVim(sname, *serverStr, NULL, &srv, 0, silent);
3739# endif
3740 if (ret < 0)
3741 {
3742 if (argtype == ARGTYPE_SEND)
3743 {
3744 /* Failed to send, abort. */
3745 mch_errmsg(_(": Send failed.\n"));
3746 didone = TRUE;
3747 exiterr = 1;
3748 }
3749 else if (!silent)
3750 /* Let vim start normally. */
3751 mch_errmsg(_(": Send failed. Trying to execute locally\n"));
3752 break;
3753 }
3754
3755# ifdef FEAT_GUI_W32
3756 /* Guess that when the server name starts with "g" it's a GUI
3757 * server, which we can bring to the foreground here.
3758 * Foreground() in the server doesn't work very well. */
3759 if (argtype != ARGTYPE_SEND && TOUPPER_ASC(*sname) == 'G')
3760 SetForegroundWindow(srv);
3761# endif
3762
3763 /*
3764 * For --remote-wait: Wait until the server did edit each
3765 * file. Also detect that the server no longer runs.
3766 */
3767 if (ret >= 0 && argtype == ARGTYPE_EDIT_WAIT)
3768 {
3769 int numFiles = *argc - i - 1;
3770 int j;
3771 char_u *done = alloc(numFiles);
3772 char_u *p;
3773# ifdef FEAT_GUI_W32
3774 NOTIFYICONDATA ni;
3775 int count = 0;
3776 extern HWND message_window;
3777# endif
3778
3779 if (numFiles > 0 && argv[i + 1][0] == '+')
3780 /* Skip "+cmd" argument, don't wait for it to be edited. */
3781 --numFiles;
3782
3783# ifdef FEAT_GUI_W32
3784 ni.cbSize = sizeof(ni);
3785 ni.hWnd = message_window;
3786 ni.uID = 0;
3787 ni.uFlags = NIF_ICON|NIF_TIP;
3788 ni.hIcon = LoadIcon((HINSTANCE)GetModuleHandle(0), "IDR_VIM");
3789 sprintf(ni.szTip, _("%d of %d edited"), count, numFiles);
3790 Shell_NotifyIcon(NIM_ADD, &ni);
3791# endif
3792
3793 /* Wait for all files to unload in remote */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02003794 vim_memset(done, 0, numFiles);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003795 while (memchr(done, 0, numFiles) != NULL)
3796 {
3797# ifdef WIN32
3798 p = serverGetReply(srv, NULL, TRUE, TRUE);
3799 if (p == NULL)
3800 break;
3801# else
3802 if (serverReadReply(xterm_dpy, srv, &p, TRUE) < 0)
3803 break;
3804# endif
3805 j = atoi((char *)p);
3806 if (j >= 0 && j < numFiles)
3807 {
3808# ifdef FEAT_GUI_W32
3809 ++count;
3810 sprintf(ni.szTip, _("%d of %d edited"),
3811 count, numFiles);
3812 Shell_NotifyIcon(NIM_MODIFY, &ni);
3813# endif
3814 done[j] = 1;
3815 }
3816 }
3817# ifdef FEAT_GUI_W32
3818 Shell_NotifyIcon(NIM_DELETE, &ni);
3819# endif
3820 }
3821 }
3822 else if (STRICMP(argv[i], "--remote-expr") == 0)
3823 {
3824 if (i == *argc - 1)
3825 mainerr_arg_missing((char_u *)argv[i]);
3826# ifdef WIN32
3827 /* Win32 always works? */
3828 if (serverSendToVim(sname, (char_u *)argv[i + 1],
3829 &res, NULL, 1, FALSE) < 0)
3830# else
3831 if (xterm_dpy == NULL)
3832 mch_errmsg(_("No display: Send expression failed.\n"));
3833 else if (serverSendToVim(xterm_dpy, sname, (char_u *)argv[i + 1],
3834 &res, NULL, 1, 1, FALSE) < 0)
3835# endif
3836 {
3837 if (res != NULL && *res != NUL)
3838 {
3839 /* Output error from remote */
3840 mch_errmsg((char *)res);
3841 vim_free(res);
3842 res = NULL;
3843 }
3844 mch_errmsg(_(": Send expression failed.\n"));
3845 }
3846 }
3847 else if (STRICMP(argv[i], "--serverlist") == 0)
3848 {
3849# ifdef WIN32
3850 /* Win32 always works? */
3851 res = serverGetVimNames();
3852# else
3853 if (xterm_dpy != NULL)
3854 res = serverGetVimNames(xterm_dpy);
3855# endif
3856 if (called_emsg)
3857 mch_errmsg("\n");
3858 }
3859 else if (STRICMP(argv[i], "--servername") == 0)
3860 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +00003861 /* Already processed. Take it out of the command line */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003862 i++;
3863 continue;
3864 }
3865 else
3866 {
3867 *newArgV++ = argv[i];
3868 newArgC++;
3869 continue;
3870 }
3871 didone = TRUE;
3872 if (res != NULL && *res != NUL)
3873 {
3874 mch_msg((char *)res);
3875 if (res[STRLEN(res) - 1] != '\n')
3876 mch_msg("\n");
3877 }
3878 vim_free(res);
3879 }
3880
3881 if (didone)
3882 {
3883 display_errors(); /* display any collected messages */
3884 exit(exiterr); /* Mission accomplished - get out */
3885 }
3886
3887 /* Return back into main() */
3888 *argc = newArgC;
3889 vim_free(sname);
3890}
3891
3892/*
3893 * Build a ":drop" command to send to a Vim server.
3894 */
3895 static char_u *
Bram Moolenaareb94e552006-03-11 21:35:11 +00003896build_drop_cmd(filec, filev, tabs, sendReply)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003897 int filec;
3898 char **filev;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003899 int tabs; /* Use ":tab drop" instead of ":drop". */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003900 int sendReply;
3901{
3902 garray_T ga;
3903 int i;
3904 char_u *inicmd = NULL;
3905 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003906 char_u *cwd;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003907
3908 if (filec > 0 && filev[0][0] == '+')
3909 {
3910 inicmd = (char_u *)filev[0] + 1;
3911 filev++;
3912 filec--;
3913 }
3914 /* Check if we have at least one argument. */
3915 if (filec <= 0)
3916 mainerr_arg_missing((char_u *)filev[-1]);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003917
3918 /* Temporarily cd to the current directory to handle relative file names. */
Bram Moolenaard9462e32011-04-11 21:35:11 +02003919 cwd = alloc(MAXPATHL);
3920 if (cwd == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003921 return NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003922 if (mch_dirname(cwd, MAXPATHL) != OK)
3923 {
3924 vim_free(cwd);
3925 return NULL;
3926 }
3927 p = vim_strsave_escaped_ext(cwd,
Bram Moolenaar02b06312007-09-06 15:39:22 +00003928#ifdef BACKSLASH_IN_FILENAME
3929 "", /* rem_backslash() will tell what chars to escape */
3930#else
3931 PATH_ESC_CHARS,
3932#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +02003933 '\\', TRUE);
3934 vim_free(cwd);
3935 if (p == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003936 return NULL;
3937 ga_init2(&ga, 1, 100);
3938 ga_concat(&ga, (char_u *)"<C-\\><C-N>:cd ");
3939 ga_concat(&ga, p);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003940 vim_free(p);
Bram Moolenaareb94e552006-03-11 21:35:11 +00003941
3942 /* Call inputsave() so that a prompt for an encryption key works. */
3943 ga_concat(&ga, (char_u *)"<CR>:if exists('*inputsave')|call inputsave()|endif|");
3944 if (tabs)
3945 ga_concat(&ga, (char_u *)"tab ");
3946 ga_concat(&ga, (char_u *)"drop");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003947 for (i = 0; i < filec; i++)
3948 {
3949 /* On Unix the shell has already expanded the wildcards, don't want to
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003950 * do it again in the Vim server. On MS-Windows only escape
3951 * non-wildcard characters. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003952 p = vim_strsave_escaped((char_u *)filev[i],
3953#ifdef UNIX
3954 PATH_ESC_CHARS
3955#else
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003956 (char_u *)" \t%#"
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003957#endif
3958 );
3959 if (p == NULL)
3960 {
3961 vim_free(ga.ga_data);
3962 return NULL;
3963 }
3964 ga_concat(&ga, (char_u *)" ");
3965 ga_concat(&ga, p);
3966 vim_free(p);
3967 }
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003968 ga_concat(&ga, (char_u *)"|if exists('*inputrestore')|call inputrestore()|endif<CR>");
3969
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003970 /* The :drop commands goes to Insert mode when 'insertmode' is set, use
3971 * CTRL-\ CTRL-N again. */
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003972 ga_concat(&ga, (char_u *)"<C-\\><C-N>");
3973
3974 /* Switch back to the correct current directory (prior to temporary path
3975 * switch) unless 'autochdir' is set, in which case it will already be
3976 * correct after the :drop command. */
3977 ga_concat(&ga, (char_u *)":if !exists('+acd')||!&acd|cd -|endif<CR>");
3978
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003979 if (sendReply)
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003980 ga_concat(&ga, (char_u *)":call SetupRemoteReplies()<CR>");
3981 ga_concat(&ga, (char_u *)":");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003982 if (inicmd != NULL)
3983 {
3984 /* Can't use <CR> after "inicmd", because an "startinsert" would cause
3985 * the following commands to be inserted as text. Use a "|",
3986 * hopefully "inicmd" does allow this... */
3987 ga_concat(&ga, inicmd);
3988 ga_concat(&ga, (char_u *)"|");
3989 }
3990 /* Bring the window to the foreground, goto Insert mode when 'im' set and
3991 * clear command line. */
Bram Moolenaar567e4de2004-12-31 21:01:02 +00003992 ga_concat(&ga, (char_u *)"cal foreground()|if &im|star|en|redr|f<CR>");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003993 ga_append(&ga, NUL);
3994 return ga.ga_data;
3995}
3996
3997/*
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003998 * Make our basic server name: use the specified "arg" if given, otherwise use
3999 * the tail of the command "cmd" we were started with.
4000 * Return the name in allocated memory. This doesn't include a serial number.
4001 */
4002 static char_u *
4003serverMakeName(arg, cmd)
4004 char_u *arg;
4005 char *cmd;
4006{
4007 char_u *p;
4008
4009 if (arg != NULL && *arg != NUL)
4010 p = vim_strsave_up(arg);
4011 else
4012 {
4013 p = vim_strsave_up(gettail((char_u *)cmd));
4014 /* Remove .exe or .bat from the name. */
4015 if (p != NULL && vim_strchr(p, '.') != NULL)
4016 *vim_strchr(p, '.') = NUL;
4017 }
4018 return p;
4019}
4020#endif /* FEAT_CLIENTSERVER */
4021
4022#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
4023/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004024 * Replace termcodes such as <CR> and insert as key presses if there is room.
4025 */
4026 void
4027server_to_input_buf(str)
4028 char_u *str;
4029{
4030 char_u *ptr = NULL;
4031 char_u *cpo_save = p_cpo;
4032
4033 /* Set 'cpoptions' the way we want it.
4034 * B set - backslashes are *not* treated specially
4035 * k set - keycodes are *not* reverse-engineered
4036 * < unset - <Key> sequences *are* interpreted
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004037 * The last but one parameter of replace_termcodes() is TRUE so that the
4038 * <lt> sequence is recognised - needed for a real backslash.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004039 */
4040 p_cpo = (char_u *)"Bk";
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004041 str = replace_termcodes((char_u *)str, &ptr, FALSE, TRUE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004042 p_cpo = cpo_save;
4043
4044 if (*ptr != NUL) /* trailing CTRL-V results in nothing */
4045 {
4046 /*
4047 * Add the string to the input stream.
4048 * Can't use add_to_input_buf() here, we now have K_SPECIAL bytes.
4049 *
4050 * First clear typed characters from the typeahead buffer, there could
4051 * be half a mapping there. Then append to the existing string, so
4052 * that multiple commands from a client are concatenated.
4053 */
4054 if (typebuf.tb_maplen < typebuf.tb_len)
4055 del_typebuf(typebuf.tb_len - typebuf.tb_maplen, typebuf.tb_maplen);
4056 (void)ins_typebuf(str, REMAP_NONE, typebuf.tb_len, TRUE, FALSE);
4057
4058 /* Let input_available() know we inserted text in the typeahead
4059 * buffer. */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004060 typebuf_was_filled = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004061 }
4062 vim_free((char_u *)ptr);
4063}
4064
4065/*
4066 * Evaluate an expression that the client sent to a string.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004067 */
4068 char_u *
4069eval_client_expr_to_string(expr)
4070 char_u *expr;
4071{
4072 char_u *res;
4073 int save_dbl = debug_break_level;
4074 int save_ro = redir_off;
4075
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004076 /* Disable debugging, otherwise Vim hangs, waiting for "cont" to be
4077 * typed. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004078 debug_break_level = -1;
4079 redir_off = 0;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004080 /* Do not display error message, otherwise Vim hangs, waiting for "cont"
4081 * to be typed. Do generate errors so that try/catch works. */
4082 ++emsg_silent;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004083
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004084 res = eval_to_string(expr, NULL, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004085
4086 debug_break_level = save_dbl;
4087 redir_off = save_ro;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004088 --emsg_silent;
4089 if (emsg_silent < 0)
4090 emsg_silent = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004091
Bram Moolenaar63a121b2005-12-11 21:36:39 +00004092 /* A client can tell us to redraw, but not to display the cursor, so do
4093 * that here. */
4094 setcursor();
4095 out_flush();
4096#ifdef FEAT_GUI
4097 if (gui.in_use)
4098 gui_update_cursor(FALSE, FALSE);
4099#endif
4100
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004101 return res;
4102}
4103
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004104/*
4105 * If conversion is needed, convert "data" from "client_enc" to 'encoding' and
4106 * return an allocated string. Otherwise return "data".
4107 * "*tofree" is set to the result when it needs to be freed later.
4108 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004109 char_u *
4110serverConvert(client_enc, data, tofree)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004111 char_u *client_enc UNUSED;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004112 char_u *data;
4113 char_u **tofree;
4114{
4115 char_u *res = data;
4116
4117 *tofree = NULL;
4118# ifdef FEAT_MBYTE
4119 if (client_enc != NULL && p_enc != NULL)
4120 {
4121 vimconv_T vimconv;
4122
4123 vimconv.vc_type = CONV_NONE;
4124 if (convert_setup(&vimconv, client_enc, p_enc) != FAIL
4125 && vimconv.vc_type != CONV_NONE)
4126 {
4127 res = string_convert(&vimconv, data, NULL);
4128 if (res == NULL)
4129 res = data;
4130 else
4131 *tofree = res;
4132 }
4133 convert_setup(&vimconv, NULL, NULL);
4134 }
4135# endif
4136 return res;
4137}
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004138#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004139
4140/*
4141 * When FEAT_FKMAP is defined, also compile the Farsi source code.
4142 */
4143#if defined(FEAT_FKMAP) || defined(PROTO)
4144# include "farsi.c"
4145#endif
4146
4147/*
4148 * When FEAT_ARABIC is defined, also compile the Arabic source code.
4149 */
4150#if defined(FEAT_ARABIC) || defined(PROTO)
4151# include "arabic.c"
4152#endif