blob: f9bd8732ee21e97d19cdf5cd0e2127194e828fa9 [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 Moolenaarb4210b32004-06-13 14:51:16 +0000325
326 /*
327 * Set the default values for the options.
328 * NOTE: Non-latin1 translated messages are working only after this,
329 * because this is where "has_mbyte" will be set, which is used by
330 * msg_outtrans_len_attr().
331 * First find out the home directory, needed to expand "~" in options.
332 */
333 init_homedir(); /* find real value of $HOME */
334 set_init_1();
335 TIME_MSG("inits 1");
336
337#ifdef FEAT_EVAL
338 set_lang_var(); /* set v:lang and v:ctype */
339#endif
340
341#ifdef FEAT_CLIENTSERVER
342 /*
343 * Do the client-server stuff, unless "--servername ''" was used.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000344 * This may exit Vim if the command was sent to the server.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000345 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000346 exec_on_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000347#endif
348
349 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000350 * Figure out the way to work from the command name argv[0].
351 * "vimdiff" starts diff mode, "rvim" sets "restricted", etc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000352 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000353 parse_command_name(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000354
355 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000356 * Process the command line arguments. File names are put in the global
357 * argument list "global_alist".
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000358 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000359 command_line_scan(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000360 TIME_MSG("parsing arguments");
361
362 /*
363 * On some systems, when we compile with the GUI, we always use it. On Mac
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000364 * there is no terminal version, and on Windows we can't fork one off with
365 * :gui.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000366 */
367#ifdef ALWAYS_USE_GUI
368 gui.starting = TRUE;
369#else
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000370# if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000371 /*
372 * Check if the GUI can be started. Reset gui.starting if not.
373 * Don't know about other systems, stay on the safe side and don't check.
374 */
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000375 if (gui.starting)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000376 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000377 if (gui_init_check() == FAIL)
378 {
379 gui.starting = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000380
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000381 /* When running "evim" or "gvim -y" we need the menus, exit if we
382 * don't have them. */
383 if (params.evim_mode)
384 mch_exit(1);
385 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000386 }
387# endif
388#endif
389
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000390 if (GARGCOUNT > 0)
391 {
392#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
393 /*
394 * Expand wildcards in file names.
395 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000396 if (!params.literal)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000397 {
398 /* Temporarily add '(' and ')' to 'isfname'. These are valid
399 * filename characters but are excluded from 'isfname' to make
400 * "gf" work on a file name in parenthesis (e.g.: see vim.h). */
401 do_cmdline_cmd((char_u *)":set isf+=(,)");
Bram Moolenaar86b68352004-12-27 21:59:20 +0000402 alist_expand(NULL, 0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000403 do_cmdline_cmd((char_u *)":set isf&");
404 }
405#endif
406 fname = alist_name(&GARGLIST[0]);
407 }
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000408
409#if defined(WIN32) && defined(FEAT_MBYTE)
410 {
411 extern void set_alist_count(void);
412
413 /* Remember the number of entries in the argument list. If it changes
414 * we don't react on setting 'encoding'. */
415 set_alist_count();
416 }
417#endif
418
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000419#ifdef MSWIN
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000420 if (GARGCOUNT == 1 && params.full_path)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000421 {
422 /*
423 * If there is one filename, fully qualified, we have very probably
424 * been invoked from explorer, so change to the file's directory.
425 * Hint: to avoid this when typing a command use a forward slash.
426 * If the cd fails, it doesn't matter.
427 */
428 (void)vim_chdirfile(fname);
429 }
430#endif
431 TIME_MSG("expanding arguments");
432
433#ifdef FEAT_DIFF
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000434 if (params.diff_mode && params.window_count == -1)
435 params.window_count = 0; /* open up to 3 windows */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000436#endif
437
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000438 /* Don't redraw until much later. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000439 ++RedrawingDisabled;
440
441 /*
442 * When listing swap file names, don't do cursor positioning et. al.
443 */
444 if (recoverymode && fname == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000445 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000446
447 /*
448 * When certain to start the GUI, don't check capabilities of terminal.
449 * For GTK we can't be sure, but when started from the desktop it doesn't
450 * make sense to try using a terminal.
451 */
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000452#if defined(ALWAYS_USE_GUI) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000453 if (gui.starting
454# ifdef FEAT_GUI_GTK
455 && !isatty(2)
456# endif
457 )
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000458 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000459#endif
460
461#if defined(FEAT_GUI_MAC) && defined(MACOS_X_UNIX)
462 /* When the GUI is started from Finder, need to display messages in a
463 * message box. isatty(2) returns TRUE anyway, thus we need to check the
464 * name to know we're not started from a terminal. */
465 if (gui.starting && (!isatty(2) || strcmp("/dev/console", ttyname(2)) == 0))
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000466 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000467 params.want_full_screen = FALSE;
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000468
469 /* Avoid always using "/" as the current directory. Note that when
470 * started from Finder the arglist will be filled later in
471 * HandleODocAE() and "fname" will be NULL. */
472 if (getcwd((char *)NameBuff, MAXPATHL) != NULL
473 && STRCMP(NameBuff, "/") == 0)
474 {
475 if (fname != NULL)
476 (void)vim_chdirfile(fname);
477 else
478 {
479 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
480 vim_chdir(NameBuff);
481 }
482 }
483 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000484#endif
485
486 /*
487 * mch_init() sets up the terminal (window) for use. This must be
488 * done after resetting full_screen, otherwise it may move the cursor
489 * (MSDOS).
490 * Note that we may use mch_exit() before mch_init()!
491 */
492 mch_init();
493 TIME_MSG("shell init");
494
495#ifdef USE_XSMP
496 /*
497 * For want of anywhere else to do it, try to connect to xsmp here.
498 * Fitting it in after gui_mch_init, but before gui_init (via termcapinit).
499 * Hijacking -X 'no X connection' to also disable XSMP connection as that
500 * has a similar delay upon failure.
501 * Only try if SESSION_MANAGER is set to something non-null.
502 */
503 if (!x_no_connect)
504 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000505 char *p = getenv("SESSION_MANAGER");
506
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000507 if (p != NULL && *p != NUL)
508 {
509 xsmp_init();
510 TIME_MSG("xsmp init");
511 }
512 }
513#endif
514
515 /*
516 * Print a warning if stdout is not a terminal.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000517 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000518 check_tty(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000519
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000520 /* This message comes before term inits, but after setting "silent_mode"
521 * when the input is not a tty. */
522 if (GARGCOUNT > 1 && !silent_mode)
523 printf(_("%d files to edit\n"), GARGCOUNT);
524
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000525 if (params.want_full_screen && !silent_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000526 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000527 termcapinit(params.term); /* set terminal name and get terminal
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000528 capabilities (will set full_screen) */
529 screen_start(); /* don't know where cursor is now */
530 TIME_MSG("Termcap init");
531 }
532
533 /*
534 * Set the default values for the options that use Rows and Columns.
535 */
536 ui_get_shellsize(); /* inits Rows and Columns */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000537 win_init_size();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000538#ifdef FEAT_DIFF
539 /* Set the 'diff' option now, so that it can be checked for in a .vimrc
540 * file. There is no buffer yet though. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000541 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000542 diff_win_options(firstwin, FALSE);
543#endif
544
545 cmdline_row = Rows - p_ch;
546 msg_row = cmdline_row;
547 screenalloc(FALSE); /* allocate screen buffers */
548 set_init_2();
549 TIME_MSG("inits 2");
550
551 msg_scroll = TRUE;
552 no_wait_return = TRUE;
553
554 init_mappings(); /* set up initial mappings */
555
556 init_highlight(TRUE, FALSE); /* set the default highlight groups */
557 TIME_MSG("init highlight");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000558
559#ifdef FEAT_EVAL
560 /* Set the break level after the terminal is initialized. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000561 debug_break_level = params.use_debug_break_level;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000562#endif
563
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100564#ifdef FEAT_MZSCHEME
565 /*
566 * Newer version of MzScheme (Racket) require earlier (trampolined)
567 * initialisation via scheme_main_setup.
568 * Implement this by initialising it as early as possible
569 * and splitting off remaining Vim main into vim_main2
570 */
571 {
572 /* Pack up preprocessed command line arguments.
573 * It is safe because Scheme does not access argc/argv. */
574 char *args[2];
575 args[0] = (char *)fname;
576 args[1] = (char *)&params;
577 return mzscheme_main(2, args);
578 }
579}
Bram Moolenaar8866d272012-11-28 15:55:42 +0100580#endif
581#endif /* NO_VIM_MAIN */
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100582
Bram Moolenaar8866d272012-11-28 15:55:42 +0100583/* vim_main2() needs to be produced when FEAT_MZSCHEME is defined even when
584 * NO_VIM_MAIN is defined. */
585#ifdef FEAT_MZSCHEME
586 int
587vim_main2(int argc UNUSED, char **argv UNUSED)
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100588{
Bram Moolenaar8866d272012-11-28 15:55:42 +0100589# ifndef NO_VIM_MAIN
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100590 char_u *fname = (char_u *)argv[0];
591 mparm_T params;
592
593 memcpy(&params, argv[1], sizeof(params));
Bram Moolenaar8866d272012-11-28 15:55:42 +0100594# else
595 return 0;
596}
597# endif
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100598#endif
599
Bram Moolenaar8866d272012-11-28 15:55:42 +0100600#ifndef NO_VIM_MAIN
Bram Moolenaar58d98232005-07-23 22:25:46 +0000601 /* Execute --cmd arguments. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000602 exe_pre_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000603
Bram Moolenaar58d98232005-07-23 22:25:46 +0000604 /* Source startup scripts. */
605 source_startup_scripts(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000606
607#ifdef FEAT_EVAL
608 /*
609 * Read all the plugin files.
610 * Only when compiled with +eval, since most plugins need it.
611 */
612 if (p_lpl)
613 {
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000614# ifdef VMS /* Somehow VMS doesn't handle the "**". */
615 source_runtime((char_u *)"plugin/*.vim", TRUE);
616# else
Bram Moolenaar07d4d732005-10-03 22:04:08 +0000617 source_runtime((char_u *)"plugin/**/*.vim", TRUE);
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000618# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000619 TIME_MSG("loading plugins");
620 }
621#endif
622
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000623#ifdef FEAT_DIFF
624 /* Decide about window layout for diff mode after reading vimrc. */
625 if (params.diff_mode && params.window_layout == 0)
626 {
627 if (diffopt_horizontal())
628 params.window_layout = WIN_HOR; /* use horizontal split */
629 else
630 params.window_layout = WIN_VER; /* use vertical split */
631 }
632#endif
633
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000634 /*
635 * Recovery mode without a file name: List swap files.
636 * This uses the 'dir' option, therefore it must be after the
637 * initializations.
638 */
639 if (recoverymode && fname == NULL)
640 {
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200641 recover_names(NULL, TRUE, 0, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000642 mch_exit(0);
643 }
644
645 /*
646 * Set a few option defaults after reading .vimrc files:
647 * 'title' and 'icon', Unix: 'shellpipe' and 'shellredir'.
648 */
649 set_init_3();
650 TIME_MSG("inits 3");
651
652 /*
653 * "-n" argument: Disable swap file by setting 'updatecount' to 0.
654 * Note that this overrides anything from a vimrc file.
655 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000656 if (params.no_swap_file)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000657 p_uc = 0;
658
659#ifdef FEAT_FKMAP
660 if (curwin->w_p_rl && p_altkeymap)
661 {
662 p_hkmap = FALSE; /* Reset the Hebrew keymap mode */
663# ifdef FEAT_ARABIC
664 curwin->w_p_arab = FALSE; /* Reset the Arabic keymap mode */
665# endif
666 p_fkmap = TRUE; /* Set the Farsi keymap mode */
667 }
668#endif
669
670#ifdef FEAT_GUI
671 if (gui.starting)
672 {
673#if defined(UNIX) || defined(VMS)
674 /* When something caused a message from a vimrc script, need to output
675 * an extra newline before the shell prompt. */
676 if (did_emsg || msg_didout)
677 putchar('\n');
678#endif
679
680 gui_start(); /* will set full_screen to TRUE */
681 TIME_MSG("starting GUI");
682
683 /* When running "evim" or "gvim -y" we need the menus, exit if we
684 * don't have them. */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000685 if (!gui.in_use && params.evim_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000686 mch_exit(1);
687 }
688#endif
689
690#ifdef SPAWNO /* special MSDOS swapping library */
691 init_SPAWNO("", SWAP_ANY);
692#endif
693
694#ifdef FEAT_VIMINFO
695 /*
Bram Moolenaard812df62008-11-09 12:46:09 +0000696 * Read in registers, history etc, but not marks, from the viminfo file.
697 * This is where v:oldfiles gets filled.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000698 */
699 if (*p_viminfo != NUL)
700 {
Bram Moolenaard812df62008-11-09 12:46:09 +0000701 read_viminfo(NULL, VIF_WANT_INFO | VIF_GET_OLDFILES);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000702 TIME_MSG("reading viminfo");
703 }
704#endif
Bram Moolenaar2cd36962014-01-14 12:57:05 +0100705#ifdef FEAT_EVAL
706 /* It's better to make v:oldfiles an empty list than NULL. */
707 if (get_vim_var_list(VV_OLDFILES) == NULL)
708 set_vim_var_list(VV_OLDFILES, list_alloc());
709#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000710
711#ifdef FEAT_QUICKFIX
712 /*
713 * "-q errorfile": Load the error file now.
714 * If the error file can't be read, exit before doing anything else.
715 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000716 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000717 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000718 if (params.use_ef != NULL)
719 set_string_option_direct((char_u *)"ef", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000720 params.use_ef, OPT_FREE, SID_CARG);
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200721 vim_snprintf((char *)IObuff, IOSIZE, "cfile %s", p_ef);
722 if (qf_init(NULL, p_ef, p_efm, TRUE, IObuff) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000723 {
724 out_char('\n');
725 mch_exit(3);
726 }
727 TIME_MSG("reading errorfile");
728 }
729#endif
730
731 /*
732 * Start putting things on the screen.
733 * Scroll screen down before drawing over it
734 * Clear screen now, so file message will not be cleared.
735 */
736 starting = NO_BUFFERS;
737 no_wait_return = FALSE;
738 if (!exmode_active)
739 msg_scroll = FALSE;
740
741#ifdef FEAT_GUI
742 /*
743 * This seems to be required to make callbacks to be called now, instead
744 * of after things have been put on the screen, which then may be deleted
745 * when getting a resize callback.
746 * For the Mac this handles putting files dropped on the Vim icon to
747 * global_alist.
748 */
749 if (gui.in_use)
750 {
751# ifdef FEAT_SUN_WORKSHOP
752 if (!usingSunWorkShop)
753# endif
754 gui_wait_for_chars(50L);
755 TIME_MSG("GUI delay");
756 }
757#endif
758
759#if defined(FEAT_GUI_PHOTON) && defined(FEAT_CLIPBOARD)
760 qnx_clip_init();
761#endif
762
Bram Moolenaarc8bbaa32010-07-14 16:54:21 +0200763#if defined(MACOS_X) && defined(FEAT_CLIPBOARD)
764 clip_init(TRUE);
765#endif
766
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000767#ifdef FEAT_XCLIPBOARD
768 /* Start using the X clipboard, unless the GUI was started. */
769# ifdef FEAT_GUI
770 if (!gui.in_use)
771# endif
772 {
773 setup_term_clip();
774 TIME_MSG("setup clipboard");
775 }
776#endif
777
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000778#ifdef FEAT_CLIENTSERVER
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000779 /* Prepare for being a Vim server. */
780 prepare_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000781#endif
782
783 /*
784 * If "-" argument given: Read file from stdin.
785 * Do this before starting Raw mode, because it may change things that the
786 * writing end of the pipe doesn't like, e.g., in case stdin and stderr
787 * are the same terminal: "cat | vim -".
788 * Using autocommands here may cause trouble...
789 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000790 if (params.edit_type == EDIT_STDIN && !recoverymode)
791 read_stdin();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000792
793#if defined(UNIX) || defined(VMS)
794 /* When switching screens and something caused a message from a vimrc
795 * script, need to output an extra newline on exit. */
796 if ((did_emsg || msg_didout) && *T_TI != NUL)
797 newline_on_exit = TRUE;
798#endif
799
800 /*
801 * When done something that is not allowed or error message call
802 * wait_return. This must be done before starttermcap(), because it may
803 * switch to another screen. It must be done after settmode(TMODE_RAW),
804 * because we want to react on a single key stroke.
805 * Call settmode and starttermcap here, so the T_KS and T_TI may be
Bram Moolenaar49325942007-05-10 19:19:59 +0000806 * defined by termcapinit and redefined in .exrc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000807 */
808 settmode(TMODE_RAW);
809 TIME_MSG("setting raw mode");
810
811 if (need_wait_return || msg_didany)
812 {
813 wait_return(TRUE);
814 TIME_MSG("waiting for return");
815 }
816
817 starttermcap(); /* start termcap if not done by wait_return() */
818 TIME_MSG("start termcap");
Bram Moolenaar9584b312013-03-13 19:29:28 +0100819#if defined(FEAT_TERMRESPONSE) && defined(FEAT_MBYTE)
Bram Moolenaar386dcde2013-09-29 16:27:47 +0200820 may_req_ambiguous_char_width();
Bram Moolenaar9584b312013-03-13 19:29:28 +0100821#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000822
823#ifdef FEAT_MOUSE
824 setmouse(); /* may start using the mouse */
825#endif
826 if (scroll_region)
827 scroll_region_reset(); /* In case Rows changed */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000828 scroll_start(); /* may scroll the screen to the right position */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000829
830 /*
831 * Don't clear the screen when starting in Ex mode, unless using the GUI.
832 */
833 if (exmode_active
834#ifdef FEAT_GUI
835 && !gui.in_use
836#endif
837 )
838 must_redraw = CLEAR;
839 else
840 {
841 screenclear(); /* clear screen */
842 TIME_MSG("clearing screen");
843 }
844
845#ifdef FEAT_CRYPT
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000846 if (params.ask_for_key)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000847 {
Bram Moolenaar40e6a712010-05-16 22:32:54 +0200848 (void)blowfish_self_test();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000849 (void)get_crypt_key(TRUE, TRUE);
850 TIME_MSG("getting crypt key");
851 }
852#endif
853
854 no_wait_return = TRUE;
855
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000856 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000857 * Create the requested number of windows and edit buffers in them.
858 * Also does recovery if "recoverymode" set.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000859 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000860 create_windows(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000861 TIME_MSG("opening buffers");
862
Bram Moolenaar867a4b72007-03-18 20:51:46 +0000863#ifdef FEAT_EVAL
864 /* clear v:swapcommand */
865 set_vim_var_string(VV_SWAPCOMMAND, NULL, -1);
866#endif
867
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000868 /* Ex starts at last line of the file */
869 if (exmode_active)
870 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
871
872#ifdef FEAT_AUTOCMD
873 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
874 TIME_MSG("BufEnter autocommands");
875#endif
876 setpcmark();
877
878#ifdef FEAT_QUICKFIX
879 /*
880 * When started with "-q errorfile" jump to first error now.
881 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000882 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000883 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000884 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000885 TIME_MSG("jump to first error");
886 }
887#endif
888
889#ifdef FEAT_WINDOWS
890 /*
891 * If opened more than one window, start editing files in the other
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000892 * windows.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000893 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000894 edit_buffers(&params);
895#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000896
897#ifdef FEAT_DIFF
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000898 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000899 {
900 win_T *wp;
901
902 /* set options in each window for "vimdiff". */
903 for (wp = firstwin; wp != NULL; wp = wp->w_next)
904 diff_win_options(wp, TRUE);
905 }
906#endif
907
908 /*
909 * Shorten any of the filenames, but only when absolute.
910 */
911 shorten_fnames(FALSE);
912
913 /*
914 * Need to jump to the tag before executing the '-c command'.
915 * Makes "vim -c '/return' -t main" work.
916 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000917 if (params.tagname != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000918 {
Bram Moolenaar146522e2005-12-16 21:55:46 +0000919#if defined(HAS_SWAP_EXISTS_ACTION)
920 swap_exists_did_quit = FALSE;
921#endif
922
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000923 vim_snprintf((char *)IObuff, IOSIZE, "ta %s", params.tagname);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000924 do_cmdline_cmd(IObuff);
925 TIME_MSG("jumping to tag");
Bram Moolenaar146522e2005-12-16 21:55:46 +0000926
927#if defined(HAS_SWAP_EXISTS_ACTION)
928 /* If the user doesn't want to edit the file then we quit here. */
929 if (swap_exists_did_quit)
930 getout(1);
931#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000932 }
933
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000934 /* Execute any "+", "-c" and "-S" arguments. */
935 if (params.n_commands > 0)
936 exe_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000937
938 RedrawingDisabled = 0;
939 redraw_all_later(NOT_VALID);
940 no_wait_return = FALSE;
941 starting = 0;
942
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000943#ifdef FEAT_TERMRESPONSE
944 /* Requesting the termresponse is postponed until here, so that a "-c q"
945 * argument doesn't make it appear in the shell Vim was started from. */
946 may_req_termresponse();
947#endif
948
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000949 /* start in insert mode */
950 if (p_im)
951 need_start_insertmode = TRUE;
952
953#ifdef FEAT_AUTOCMD
954 apply_autocmds(EVENT_VIMENTER, NULL, NULL, FALSE, curbuf);
955 TIME_MSG("VimEnter autocommands");
956#endif
957
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200958#if defined(FEAT_EVAL) && defined(FEAT_CLIPBOARD)
959 /* Adjust default register name for "unnamed" in 'clipboard'. Can only be
960 * done after the clipboard is available and all initial commands that may
961 * modify the 'clipboard' setting have run; i.e. just before entering the
962 * main loop. */
963 {
964 int default_regname = 0;
965 adjust_clip_reg(&default_regname);
966 set_reg_var(default_regname);
967 }
968#endif
969
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000970#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
971 /* When a startup script or session file setup for diff'ing and
972 * scrollbind, sync the scrollbind now. */
973 if (curwin->w_p_diff && curwin->w_p_scb)
974 {
975 update_topline();
976 check_scrollbind((linenr_T)0, 0L);
977 TIME_MSG("diff scrollbinding");
978 }
979#endif
980
981#if defined(WIN3264) && !defined(FEAT_GUI_W32)
982 mch_set_winsize_now(); /* Allow winsize changes from now on */
983#endif
984
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000985#if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
986 /* When tab pages were created, may need to update the tab pages line and
987 * scrollbars. This is skipped while creating them. */
988 if (first_tabpage->tp_next != NULL)
989 {
990 out_flush();
991 gui_init_which_components(NULL);
992 gui_update_scrollbars(TRUE);
993 }
994 need_mouse_correct = TRUE;
995#endif
996
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000997 /* If ":startinsert" command used, stuff a dummy command to be able to
998 * call normal_cmd(), which will then start Insert mode. */
999 if (restart_edit != 0)
Bram Moolenaarebefac62005-12-28 22:39:57 +00001000 stuffcharReadbuff(K_NOP);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001001
1002#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001003 if (netbeansArg != NULL && strncmp("-nb", netbeansArg, 3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +02001004 {
1005# ifdef FEAT_GUI
Bram Moolenaar173c9852010-09-29 17:27:01 +02001006# if !defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK) \
Bram Moolenaar67c53842010-05-22 18:28:27 +02001007 && !defined(FEAT_GUI_W32)
1008 if (gui.in_use)
1009 {
1010 mch_errmsg(_("netbeans is not supported with this GUI\n"));
1011 mch_exit(2);
1012 }
1013# endif
1014# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001015 /* Tell the client that it can start sending commands. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001016 netbeans_open(netbeansArg + 3, TRUE);
Bram Moolenaar67c53842010-05-22 18:28:27 +02001017 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001018#endif
1019
1020 TIME_MSG("before starting main loop");
1021
1022 /*
1023 * Call the main command loop. This never returns.
Bram Moolenaarbbc98db2012-02-12 01:55:55 +01001024 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001025 main_loop(FALSE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001026
1027 return 0;
1028}
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01001029#endif /* NO_VIM_MAIN */
Bram Moolenaar8866d272012-11-28 15:55:42 +01001030#endif /* PROTO */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001031
1032/*
1033 * Main loop: Execute Normal mode commands until exiting Vim.
1034 * Also used to handle commands in the command-line window, until the window
1035 * is closed.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001036 * Also used to handle ":visual" command after ":global": execute Normal mode
1037 * commands, return when entering Ex mode. "noexmode" is TRUE then.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001038 */
1039 void
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001040main_loop(cmdwin, noexmode)
1041 int cmdwin; /* TRUE when working in the command-line window */
1042 int noexmode; /* TRUE when return on entering Ex mode */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001043{
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001044 oparg_T oa; /* operator arguments */
1045 int previous_got_int = FALSE; /* "got_int" was TRUE */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001046#ifdef FEAT_CONCEAL
1047 linenr_T conceal_old_cursor_line = 0;
1048 linenr_T conceal_new_cursor_line = 0;
1049 int conceal_update_lines = FALSE;
1050#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001051
1052#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
1053 /* Setup to catch a terminating error from the X server. Just ignore
1054 * it, restore the state and continue. This might not always work
1055 * properly, but at least we don't exit unexpectedly when the X server
Bram Moolenaar2cd36962014-01-14 12:57:05 +01001056 * exits while Vim is running in a console. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001057 if (!cmdwin && !noexmode && SETJMP(x_jump_env))
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001058 {
1059 State = NORMAL;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001060 VIsual_active = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001061 got_int = TRUE;
1062 need_wait_return = FALSE;
1063 global_busy = FALSE;
1064 exmode_active = 0;
1065 skip_redraw = FALSE;
1066 RedrawingDisabled = 0;
1067 no_wait_return = 0;
Bram Moolenaar7701c242011-10-04 16:43:53 +02001068 vgetc_busy = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001069# ifdef FEAT_EVAL
1070 emsg_skip = 0;
1071# endif
1072 emsg_off = 0;
1073# ifdef FEAT_MOUSE
1074 setmouse();
1075# endif
1076 settmode(TMODE_RAW);
1077 starttermcap();
1078 scroll_start();
1079 redraw_later_clear();
1080 }
1081#endif
1082
1083 clear_oparg(&oa);
1084 while (!cmdwin
1085#ifdef FEAT_CMDWIN
1086 || cmdwin_result == 0
1087#endif
1088 )
1089 {
1090 if (stuff_empty())
1091 {
1092 did_check_timestamps = FALSE;
1093 if (need_check_timestamps)
1094 check_timestamps(FALSE);
1095 if (need_wait_return) /* if wait_return still needed ... */
1096 wait_return(FALSE); /* ... call it now */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001097 if (need_start_insertmode && goto_im() && !VIsual_active)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001098 {
1099 need_start_insertmode = FALSE;
1100 stuffReadbuff((char_u *)"i"); /* start insert mode next */
1101 /* skip the fileinfo message now, because it would be shown
1102 * after insert mode finishes! */
1103 need_fileinfo = FALSE;
1104 }
1105 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001106
1107 /* Reset "got_int" now that we got back to the main loop. Except when
1108 * inside a ":g/pat/cmd" command, then the "got_int" needs to abort
1109 * the ":g" command.
1110 * For ":g/pat/vi" we reset "got_int" when used once. When used
1111 * a second time we go back to Ex mode and abort the ":g" command. */
1112 if (got_int)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001113 {
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001114 if (noexmode && global_busy && !exmode_active && previous_got_int)
1115 {
1116 /* Typed two CTRL-C in a row: go back to ex mode as if "Q" was
1117 * used and keep "got_int" set, so that it aborts ":g". */
1118 exmode_active = EXMODE_NORMAL;
1119 State = NORMAL;
1120 }
1121 else if (!global_busy || !exmode_active)
1122 {
1123 if (!quit_more)
1124 (void)vgetc(); /* flush all buffers */
1125 got_int = FALSE;
1126 }
1127 previous_got_int = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001128 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001129 else
1130 previous_got_int = FALSE;
1131
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001132 if (!exmode_active)
1133 msg_scroll = FALSE;
1134 quit_more = FALSE;
1135
1136 /*
1137 * If skip redraw is set (for ":" in wait_return()), don't redraw now.
1138 * If there is nothing in the stuff_buffer or do_redraw is TRUE,
1139 * update cursor and redraw.
1140 */
1141 if (skip_redraw || exmode_active)
1142 skip_redraw = FALSE;
1143 else if (do_redraw || stuff_empty())
1144 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001145#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001146 /* Trigger CursorMoved if the cursor moved. */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001147 if (!finish_op && (
1148# ifdef FEAT_AUTOCMD
1149 has_cursormoved()
1150# endif
1151# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
1152 ||
1153# endif
1154# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001155 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001156# endif
1157 )
1158 && !equalpos(last_cursormoved, curwin->w_cursor))
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001159 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001160# ifdef FEAT_AUTOCMD
1161 if (has_cursormoved())
1162 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL,
1163 FALSE, curbuf);
1164# endif
1165# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001166 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001167 {
1168 conceal_old_cursor_line = last_cursormoved.lnum;
1169 conceal_new_cursor_line = curwin->w_cursor.lnum;
1170 conceal_update_lines = TRUE;
1171 }
1172# endif
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001173 last_cursormoved = curwin->w_cursor;
1174 }
1175#endif
1176
Bram Moolenaar186628f2013-03-19 13:33:23 +01001177#ifdef FEAT_AUTOCMD
1178 /* Trigger TextChanged if b_changedtick differs. */
1179 if (!finish_op && has_textchanged()
1180 && last_changedtick != curbuf->b_changedtick)
1181 {
1182 if (last_changedtick_buf == curbuf)
1183 apply_autocmds(EVENT_TEXTCHANGED, NULL, NULL,
1184 FALSE, curbuf);
1185 last_changedtick_buf = curbuf;
1186 last_changedtick = curbuf->b_changedtick;
1187 }
1188#endif
1189
Bram Moolenaar33aec762006-01-22 23:30:12 +00001190#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
1191 /* Scroll-binding for diff mode may have been postponed until
1192 * here. Avoids doing it for every change. */
1193 if (diff_need_scrollbind)
1194 {
1195 check_scrollbind((linenr_T)0, 0L);
1196 diff_need_scrollbind = FALSE;
1197 }
1198#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001199#if defined(FEAT_FOLDING)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001200 /* Include a closed fold completely in the Visual area. */
1201 foldAdjustVisual();
1202#endif
1203#ifdef FEAT_FOLDING
1204 /*
1205 * When 'foldclose' is set, apply 'foldlevel' to folds that don't
1206 * contain the cursor.
1207 * When 'foldopen' is "all", open the fold(s) under the cursor.
1208 * This may mark the window for redrawing.
1209 */
1210 if (hasAnyFolding(curwin) && !char_avail())
1211 {
1212 foldCheckClose();
1213 if (fdo_flags & FDO_ALL)
1214 foldOpenCursor();
1215 }
1216#endif
1217
1218 /*
1219 * Before redrawing, make sure w_topline is correct, and w_leftcol
1220 * if lines don't wrap, and w_skipcol if lines wrap.
1221 */
1222 update_topline();
1223 validate_cursor();
1224
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001225 if (VIsual_active)
1226 update_curbuf(INVERTED);/* update inverted part */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001227 else if (must_redraw)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001228 update_screen(0);
1229 else if (redraw_cmdline || clear_cmdline)
1230 showmode();
1231#ifdef FEAT_WINDOWS
1232 redraw_statuslines();
1233#endif
1234#ifdef FEAT_TITLE
1235 if (need_maketitle)
1236 maketitle();
1237#endif
1238 /* display message after redraw */
1239 if (keep_msg != NULL)
1240 {
1241 char_u *p;
1242
1243 /* msg_attr_keep() will set keep_msg to NULL, must free the
1244 * string here. */
1245 p = keep_msg;
Bram Moolenaar238a5642006-02-21 22:12:05 +00001246 keep_msg = NULL;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001247 msg_attr(p, keep_msg_attr);
1248 vim_free(p);
1249 }
1250 if (need_fileinfo) /* show file info after redraw */
1251 {
1252 fileinfo(FALSE, TRUE, FALSE);
1253 need_fileinfo = FALSE;
1254 }
1255
1256 emsg_on_display = FALSE; /* can delete error message now */
1257 did_emsg = FALSE;
1258 msg_didany = FALSE; /* reset lines_left in msg_start() */
Bram Moolenaar661b1822005-07-28 22:36:45 +00001259 may_clear_sb_text(); /* clear scroll-back text on next msg */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001260 showruler(FALSE);
1261
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001262# if defined(FEAT_CONCEAL)
1263 if (conceal_update_lines
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001264 && (conceal_old_cursor_line != conceal_new_cursor_line
1265 || conceal_cursor_line(curwin)
1266 || need_cursor_line_redraw))
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001267 {
Bram Moolenaarc2b4c622011-02-15 16:29:59 +01001268 if (conceal_old_cursor_line != conceal_new_cursor_line
1269 && conceal_old_cursor_line
1270 <= curbuf->b_ml.ml_line_count)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001271 update_single_line(curwin, conceal_old_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001272 update_single_line(curwin, conceal_new_cursor_line);
1273 curwin->w_valid &= ~VALID_CROW;
1274 }
1275# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001276 setcursor();
1277 cursor_on();
1278
1279 do_redraw = FALSE;
Bram Moolenaar3f269672009-11-03 11:11:11 +00001280
1281#ifdef STARTUPTIME
1282 /* Now that we have drawn the first screen all the startup stuff
1283 * has been done, close any file for startup messages. */
1284 if (time_fd != NULL)
1285 {
1286 TIME_MSG("first screen update");
1287 TIME_MSG("--- VIM STARTED ---");
1288 fclose(time_fd);
1289 time_fd = NULL;
1290 }
1291#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001292 }
1293#ifdef FEAT_GUI
1294 if (need_mouse_correct)
1295 gui_mouse_correct();
1296#endif
1297
1298 /*
1299 * Update w_curswant if w_set_curswant has been set.
1300 * Postponed until here to avoid computing w_virtcol too often.
1301 */
1302 update_curswant();
1303
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001304#ifdef FEAT_EVAL
1305 /*
1306 * May perform garbage collection when waiting for a character, but
1307 * only at the very toplevel. Otherwise we may be using a List or
1308 * Dict internally somewhere.
1309 * "may_garbage_collect" is reset in vgetc() which is invoked through
1310 * do_exmode() and normal_cmd().
1311 */
1312 may_garbage_collect = (!cmdwin && !noexmode);
1313#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001314 /*
1315 * If we're invoked as ex, do a round of ex commands.
1316 * Otherwise, get and execute a normal mode command.
1317 */
1318 if (exmode_active)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001319 {
1320 if (noexmode) /* End of ":global/path/visual" commands */
1321 return;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001322 do_exmode(exmode_active == EXMODE_VIM);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001323 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001324 else
1325 normal_cmd(&oa, TRUE);
1326 }
1327}
1328
1329
1330#if defined(USE_XSMP) || defined(FEAT_GUI_MSWIN) || defined(PROTO)
1331/*
1332 * Exit, but leave behind swap files for modified buffers.
1333 */
1334 void
1335getout_preserve_modified(exitval)
1336 int exitval;
1337{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001338# if defined(SIGHUP) && defined(SIG_IGN)
1339 /* Ignore SIGHUP, because a dropped connection causes a read error, which
1340 * makes Vim exit and then handling SIGHUP causes various reentrance
1341 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001342 signal(SIGHUP, SIG_IGN);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001343# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001344
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001345 ml_close_notmod(); /* close all not-modified buffers */
1346 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
1347 ml_close_all(FALSE); /* close all memfiles, without deleting */
1348 getout(exitval); /* exit Vim properly */
1349}
1350#endif
1351
1352
1353/* Exit properly */
1354 void
1355getout(exitval)
1356 int exitval;
1357{
1358#ifdef FEAT_AUTOCMD
1359 buf_T *buf;
1360 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001361 tabpage_T *tp, *next_tp;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001362#endif
1363
1364 exiting = TRUE;
1365
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001366 /* When running in Ex mode an error causes us to exit with a non-zero exit
1367 * code. POSIX requires this, although it's not 100% clear from the
1368 * standard. */
1369 if (exmode_active)
1370 exitval += ex_exitval;
1371
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001372 /* Position the cursor on the last screen line, below all the text */
1373#ifdef FEAT_GUI
1374 if (!gui.in_use)
1375#endif
1376 windgoto((int)Rows - 1, 0);
1377
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +00001378#if defined(FEAT_EVAL) || defined(FEAT_SYN_HL)
1379 /* Optionally print hashtable efficiency. */
1380 hash_debug_results();
1381#endif
1382
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001383#ifdef FEAT_GUI
1384 msg_didany = FALSE;
1385#endif
1386
1387#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001388 if (get_vim_var_nr(VV_DYING) <= 1)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001389 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001390 /* Trigger BufWinLeave for all windows, but only once per buffer. */
1391# if defined FEAT_WINDOWS
1392 for (tp = first_tabpage; tp != NULL; tp = next_tp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001393 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001394 next_tp = tp->tp_next;
1395 for (wp = (tp == curtab)
1396 ? firstwin : tp->tp_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001397 {
Bram Moolenaar802418d2013-01-17 14:00:11 +01001398 if (wp->w_buffer == NULL)
1399 /* Autocmd must have close the buffer already, skip. */
1400 continue;
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001401 buf = wp->w_buffer;
1402 if (buf->b_changedtick != -1)
1403 {
1404 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname,
1405 buf->b_fname, FALSE, buf);
Bram Moolenaarb429cde2012-04-25 18:24:29 +02001406 buf->b_changedtick = -1; /* note that we did it already */
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001407 /* start all over, autocommands may mess up the lists */
1408 next_tp = first_tabpage;
1409 break;
1410 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001411 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001412 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001413# else
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001414 apply_autocmds(EVENT_BUFWINLEAVE, curbuf, curbuf->b_fname,
1415 FALSE, curbuf);
Bram Moolenaarf740b292006-02-16 22:11:02 +00001416# endif
Bram Moolenaar33aec762006-01-22 23:30:12 +00001417
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001418 /* Trigger BufUnload for buffers that are loaded */
1419 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1420 if (buf->b_ml.ml_mfp != NULL)
1421 {
1422 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001423 FALSE, buf);
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001424 if (!buf_valid(buf)) /* autocmd may delete the buffer */
1425 break;
1426 }
1427 apply_autocmds(EVENT_VIMLEAVEPRE, NULL, NULL, FALSE, curbuf);
1428 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001429#endif
1430
1431#ifdef FEAT_VIMINFO
1432 if (*p_viminfo != NUL)
1433 /* Write out the registers, history, marks etc, to the viminfo file */
1434 write_viminfo(NULL, FALSE);
1435#endif
1436
1437#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001438 if (get_vim_var_nr(VV_DYING) <= 1)
1439 apply_autocmds(EVENT_VIMLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001440#endif
1441
Bram Moolenaar05159a02005-02-26 23:04:13 +00001442#ifdef FEAT_PROFILE
1443 profile_dump();
1444#endif
1445
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001446 if (did_emsg
1447#ifdef FEAT_GUI
1448 || (gui.in_use && msg_didany && p_verbose > 0)
1449#endif
1450 )
1451 {
1452 /* give the user a chance to read the (error) message */
1453 no_wait_return = FALSE;
1454 wait_return(FALSE);
1455 }
1456
1457#ifdef FEAT_AUTOCMD
1458 /* Position the cursor again, the autocommands may have moved it */
1459# ifdef FEAT_GUI
1460 if (!gui.in_use)
1461# endif
1462 windgoto((int)Rows - 1, 0);
1463#endif
1464
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001465#ifdef FEAT_LUA
1466 lua_end();
1467#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001468#ifdef FEAT_MZSCHEME
1469 mzscheme_end();
1470#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001471#ifdef FEAT_TCL
1472 tcl_end();
1473#endif
1474#ifdef FEAT_RUBY
1475 ruby_end();
1476#endif
1477#ifdef FEAT_PYTHON
1478 python_end();
1479#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001480#ifdef FEAT_PYTHON3
1481 python3_end();
1482#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001483#ifdef FEAT_PERL
1484 perl_end();
1485#endif
1486#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
1487 iconv_end();
1488#endif
1489#ifdef FEAT_NETBEANS_INTG
1490 netbeans_end();
1491#endif
Bram Moolenaar02b06312007-09-06 15:39:22 +00001492#ifdef FEAT_CSCOPE
1493 cs_end();
1494#endif
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001495#ifdef FEAT_EVAL
1496 if (garbage_collect_at_exit)
1497 garbage_collect();
1498#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001499
1500 mch_exit(exitval);
1501}
1502
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01001503#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001504/*
1505 * Get a (optional) count for a Vim argument.
1506 */
1507 static int
1508get_number_arg(p, idx, def)
1509 char_u *p; /* pointer to argument */
1510 int *idx; /* index in argument, is incremented */
1511 int def; /* default value */
1512{
1513 if (vim_isdigit(p[*idx]))
1514 {
1515 def = atoi((char *)&(p[*idx]));
1516 while (vim_isdigit(p[*idx]))
1517 *idx = *idx + 1;
1518 }
1519 return def;
1520}
1521
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001522#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1523/*
1524 * Setup to use the current locale (for ctype() and many other things).
1525 */
1526 static void
1527init_locale()
1528{
1529 setlocale(LC_ALL, "");
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001530
Bram Moolenaarc6af8122010-05-21 12:04:55 +02001531# ifdef FEAT_GUI_GTK
1532 /* Tell Gtk not to change our locale settings. */
1533 gtk_disable_setlocale();
1534# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001535# if defined(FEAT_FLOAT) && defined(LC_NUMERIC)
1536 /* Make sure strtod() uses a decimal point, not a comma. */
1537 setlocale(LC_NUMERIC, "C");
1538# endif
1539
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001540# ifdef WIN32
1541 /* Apparently MS-Windows printf() may cause a crash when we give it 8-bit
1542 * text while it's expecting text in the current locale. This call avoids
1543 * that. */
1544 setlocale(LC_CTYPE, "C");
1545# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001546
1547# ifdef FEAT_GETTEXT
1548 {
1549 int mustfree = FALSE;
1550 char_u *p;
1551
1552# ifdef DYNAMIC_GETTEXT
1553 /* Initialize the gettext library */
1554 dyn_libintl_init(NULL);
1555# endif
1556 /* expand_env() doesn't work yet, because chartab[] is not initialized
1557 * yet, call vim_getenv() directly */
1558 p = vim_getenv((char_u *)"VIMRUNTIME", &mustfree);
1559 if (p != NULL && *p != NUL)
1560 {
Bram Moolenaar1ad2f132007-06-19 18:27:18 +00001561 vim_snprintf((char *)NameBuff, MAXPATHL, "%s/lang", p);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001562 bindtextdomain(VIMPACKAGE, (char *)NameBuff);
1563 }
1564 if (mustfree)
1565 vim_free(p);
1566 textdomain(VIMPACKAGE);
1567 }
1568# endif
1569}
1570#endif
1571
1572/*
1573 * Check for: [r][e][g][vi|vim|view][diff][ex[im]]
1574 * If the executable name starts with "r" we disable shell commands.
1575 * If the next character is "e" we run in Easy mode.
1576 * If the next character is "g" we run the GUI version.
1577 * If the next characters are "view" we start in readonly mode.
1578 * If the next characters are "diff" or "vimdiff" we start in diff mode.
1579 * If the next characters are "ex" we start in Ex mode. If it's followed
1580 * by "im" use improved Ex mode.
1581 */
1582 static void
1583parse_command_name(parmp)
1584 mparm_T *parmp;
1585{
1586 char_u *initstr;
1587
1588 initstr = gettail((char_u *)parmp->argv[0]);
1589
1590#ifdef MACOS_X_UNIX
1591 /* An issue has been seen when launching Vim in such a way that
1592 * $PWD/$ARGV[0] or $ARGV[0] is not the absolute path to the
1593 * executable or a symbolic link of it. Until this issue is resolved
1594 * we prohibit the GUI from being used.
1595 */
1596 if (STRCMP(initstr, parmp->argv[0]) == 0)
1597 disallow_gui = TRUE;
1598
1599 /* TODO: On MacOS X default to gui if argv[0] ends in:
Bram Moolenaar27dc1952006-03-15 23:06:44 +00001600 * /Vim.app/Contents/MacOS/Vim */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001601#endif
1602
1603#ifdef FEAT_EVAL
1604 set_vim_var_string(VV_PROGNAME, initstr, -1);
1605#endif
1606
1607 if (TOLOWER_ASC(initstr[0]) == 'r')
1608 {
1609 restricted = TRUE;
1610 ++initstr;
1611 }
1612
Bram Moolenaarad249fb2010-05-07 16:35:04 +02001613 /* Use evim mode for "evim" and "egvim", not for "editor". */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001614 if (TOLOWER_ASC(initstr[0]) == 'e'
1615 && (TOLOWER_ASC(initstr[1]) == 'v'
1616 || TOLOWER_ASC(initstr[1]) == 'g'))
1617 {
1618#ifdef FEAT_GUI
1619 gui.starting = TRUE;
1620#endif
1621 parmp->evim_mode = TRUE;
1622 ++initstr;
1623 }
1624
Bram Moolenaar806875d2008-09-18 18:57:10 +00001625 /* "gvim" starts the GUI. Also accept "Gvim" for MS-Windows. */
1626 if (TOLOWER_ASC(initstr[0]) == 'g')
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001627 {
1628 main_start_gui();
1629#ifdef FEAT_GUI
1630 ++initstr;
1631#endif
1632 }
1633
1634 if (STRNICMP(initstr, "view", 4) == 0)
1635 {
1636 readonlymode = TRUE;
1637 curbuf->b_p_ro = TRUE;
1638 p_uc = 10000; /* don't update very often */
1639 initstr += 4;
1640 }
1641 else if (STRNICMP(initstr, "vim", 3) == 0)
1642 initstr += 3;
1643
1644 /* Catch "[r][g]vimdiff" and "[r][g]viewdiff". */
1645 if (STRICMP(initstr, "diff") == 0)
1646 {
1647#ifdef FEAT_DIFF
1648 parmp->diff_mode = TRUE;
1649#else
1650 mch_errmsg(_("This Vim was not compiled with the diff feature."));
1651 mch_errmsg("\n");
1652 mch_exit(2);
1653#endif
1654 }
1655
1656 if (STRNICMP(initstr, "ex", 2) == 0)
1657 {
1658 if (STRNICMP(initstr + 2, "im", 2) == 0)
1659 exmode_active = EXMODE_VIM;
1660 else
1661 exmode_active = EXMODE_NORMAL;
1662 change_compatible(TRUE); /* set 'compatible' */
1663 }
1664}
1665
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001666/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00001667 * Get the name of the display, before gui_prepare() removes it from
1668 * argv[]. Used for the xterm-clipboard display.
1669 *
Bram Moolenaar78e17622007-08-30 10:26:19 +00001670 * Also find the --server... arguments and --socketid and --windowid
Bram Moolenaar58d98232005-07-23 22:25:46 +00001671 */
Bram Moolenaar58d98232005-07-23 22:25:46 +00001672 static void
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001673early_arg_scan(parmp)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00001674 mparm_T *parmp UNUSED;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001675{
Bram Moolenaar03005972008-11-20 13:12:36 +00001676#if defined(FEAT_XCLIPBOARD) || defined(FEAT_CLIENTSERVER) \
1677 || !defined(FEAT_NETBEANS_INTG)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001678 int argc = parmp->argc;
1679 char **argv = parmp->argv;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001680 int i;
1681
1682 for (i = 1; i < argc; i++)
1683 {
1684 if (STRCMP(argv[i], "--") == 0)
1685 break;
1686# ifdef FEAT_XCLIPBOARD
1687 else if (STRICMP(argv[i], "-display") == 0
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001688# if defined(FEAT_GUI_GTK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001689 || STRICMP(argv[i], "--display") == 0
1690# endif
1691 )
1692 {
1693 if (i == argc - 1)
1694 mainerr_arg_missing((char_u *)argv[i]);
1695 xterm_display = argv[++i];
1696 }
1697# endif
1698# ifdef FEAT_CLIENTSERVER
1699 else if (STRICMP(argv[i], "--servername") == 0)
1700 {
1701 if (i == argc - 1)
1702 mainerr_arg_missing((char_u *)argv[i]);
1703 parmp->serverName_arg = (char_u *)argv[++i];
1704 }
Bram Moolenaareb94e552006-03-11 21:35:11 +00001705 else if (STRICMP(argv[i], "--serverlist") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001706 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001707 else if (STRNICMP(argv[i], "--remote", 8) == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001708 {
1709 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001710# ifdef FEAT_GUI
1711 if (strstr(argv[i], "-wait") != 0)
1712 /* don't fork() when starting the GUI to edit files ourself */
1713 gui.dofork = FALSE;
1714# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001715 }
1716# endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001717
1718# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1719# ifdef FEAT_GUI_W32
1720 else if (STRICMP(argv[i], "--windowid") == 0)
1721# else
Bram Moolenaar58d98232005-07-23 22:25:46 +00001722 else if (STRICMP(argv[i], "--socketid") == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001723# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001724 {
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001725 long_u id;
1726 int count;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001727
1728 if (i == argc - 1)
1729 mainerr_arg_missing((char_u *)argv[i]);
1730 if (STRNICMP(argv[i+1], "0x", 2) == 0)
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001731 count = sscanf(&(argv[i + 1][2]), SCANF_HEX_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001732 else
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001733 count = sscanf(argv[i + 1], SCANF_DECIMAL_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001734 if (count != 1)
1735 mainerr(ME_INVALID_ARG, (char_u *)argv[i]);
1736 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00001737# ifdef FEAT_GUI_W32
1738 win_socket_id = id;
1739# else
1740 gtk_socket_id = id;
1741# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001742 i++;
1743 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001744# endif
1745# ifdef FEAT_GUI_GTK
Bram Moolenaar58d98232005-07-23 22:25:46 +00001746 else if (STRICMP(argv[i], "--echo-wid") == 0)
1747 echo_wid_arg = TRUE;
1748# endif
Bram Moolenaar03005972008-11-20 13:12:36 +00001749# ifndef FEAT_NETBEANS_INTG
1750 else if (strncmp(argv[i], "-nb", (size_t)3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +02001751 {
1752 mch_errmsg(_("'-nb' cannot be used: not enabled at compile time\n"));
1753 mch_exit(2);
1754 }
Bram Moolenaar03005972008-11-20 13:12:36 +00001755# endif
1756
Bram Moolenaar58d98232005-07-23 22:25:46 +00001757 }
1758#endif
1759}
1760
1761/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001762 * Scan the command line arguments.
1763 */
1764 static void
1765command_line_scan(parmp)
1766 mparm_T *parmp;
1767{
1768 int argc = parmp->argc;
1769 char **argv = parmp->argv;
1770 int argv_idx; /* index in argv[n][] */
1771 int had_minmin = FALSE; /* found "--" argument */
1772 int want_argument; /* option argument with argument */
1773 int c;
Bram Moolenaar231334e2005-07-25 20:46:57 +00001774 char_u *p = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001775 long n;
1776
1777 --argc;
1778 ++argv;
1779 argv_idx = 1; /* active option letter is argv[0][argv_idx] */
1780 while (argc > 0)
1781 {
1782 /*
1783 * "+" or "+{number}" or "+/{pat}" or "+{command}" argument.
1784 */
1785 if (argv[0][0] == '+' && !had_minmin)
1786 {
1787 if (parmp->n_commands >= MAX_ARG_CMDS)
1788 mainerr(ME_EXTRA_CMD, NULL);
1789 argv_idx = -1; /* skip to next argument */
1790 if (argv[0][1] == NUL)
1791 parmp->commands[parmp->n_commands++] = (char_u *)"$";
1792 else
1793 parmp->commands[parmp->n_commands++] = (char_u *)&(argv[0][1]);
1794 }
1795
1796 /*
1797 * Optional argument.
1798 */
1799 else if (argv[0][0] == '-' && !had_minmin)
1800 {
1801 want_argument = FALSE;
1802 c = argv[0][argv_idx++];
1803#ifdef VMS
1804 /*
1805 * VMS only uses upper case command lines. Interpret "-X" as "-x"
1806 * and "-/X" as "-X".
1807 */
1808 if (c == '/')
1809 {
1810 c = argv[0][argv_idx++];
1811 c = TOUPPER_ASC(c);
1812 }
1813 else
1814 c = TOLOWER_ASC(c);
1815#endif
1816 switch (c)
1817 {
1818 case NUL: /* "vim -" read from stdin */
1819 /* "ex -" silent mode */
1820 if (exmode_active)
1821 silent_mode = TRUE;
1822 else
1823 {
1824 if (parmp->edit_type != EDIT_NONE)
1825 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1826 parmp->edit_type = EDIT_STDIN;
1827 read_cmd_fd = 2; /* read from stderr instead of stdin */
1828 }
1829 argv_idx = -1; /* skip to next argument */
1830 break;
1831
1832 case '-': /* "--" don't take any more option arguments */
1833 /* "--help" give help message */
1834 /* "--version" give version message */
1835 /* "--literal" take files literally */
1836 /* "--nofork" don't fork */
1837 /* "--noplugin[s]" skip plugins */
1838 /* "--cmd <cmd>" execute cmd before vimrc */
1839 if (STRICMP(argv[0] + argv_idx, "help") == 0)
1840 usage();
1841 else if (STRICMP(argv[0] + argv_idx, "version") == 0)
1842 {
1843 Columns = 80; /* need to init Columns */
1844 info_message = TRUE; /* use mch_msg(), not mch_errmsg() */
1845 list_version();
1846 msg_putchar('\n');
1847 msg_didout = FALSE;
1848 mch_exit(0);
1849 }
1850 else if (STRNICMP(argv[0] + argv_idx, "literal", 7) == 0)
1851 {
1852#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
1853 parmp->literal = TRUE;
1854#endif
1855 }
1856 else if (STRNICMP(argv[0] + argv_idx, "nofork", 6) == 0)
1857 {
1858#ifdef FEAT_GUI
1859 gui.dofork = FALSE; /* don't fork() when starting GUI */
1860#endif
1861 }
1862 else if (STRNICMP(argv[0] + argv_idx, "noplugin", 8) == 0)
1863 p_lpl = FALSE;
1864 else if (STRNICMP(argv[0] + argv_idx, "cmd", 3) == 0)
1865 {
1866 want_argument = TRUE;
1867 argv_idx += 3;
1868 }
Bram Moolenaaref94eec2009-11-11 13:22:11 +00001869 else if (STRNICMP(argv[0] + argv_idx, "startuptime", 11) == 0)
1870 {
1871 want_argument = TRUE;
1872 argv_idx += 11;
1873 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001874#ifdef FEAT_CLIENTSERVER
1875 else if (STRNICMP(argv[0] + argv_idx, "serverlist", 10) == 0)
1876 ; /* already processed -- no arg */
1877 else if (STRNICMP(argv[0] + argv_idx, "servername", 10) == 0
1878 || STRNICMP(argv[0] + argv_idx, "serversend", 10) == 0)
1879 {
1880 /* already processed -- snatch the following arg */
1881 if (argc > 1)
1882 {
1883 --argc;
1884 ++argv;
1885 }
1886 }
1887#endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001888#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1889# ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001890 else if (STRNICMP(argv[0] + argv_idx, "socketid", 8) == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001891# else
1892 else if (STRNICMP(argv[0] + argv_idx, "windowid", 8) == 0)
1893# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001894 {
1895 /* already processed -- snatch the following arg */
1896 if (argc > 1)
1897 {
1898 --argc;
1899 ++argv;
1900 }
1901 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001902#endif
1903#ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001904 else if (STRNICMP(argv[0] + argv_idx, "echo-wid", 8) == 0)
1905 {
1906 /* already processed, skip */
1907 }
1908#endif
1909 else
1910 {
1911 if (argv[0][argv_idx])
1912 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
1913 had_minmin = TRUE;
1914 }
1915 if (!want_argument)
1916 argv_idx = -1; /* skip to next argument */
1917 break;
1918
1919 case 'A': /* "-A" start in Arabic mode */
1920#ifdef FEAT_ARABIC
1921 set_option_value((char_u *)"arabic", 1L, NULL, 0);
1922#else
1923 mch_errmsg(_(e_noarabic));
1924 mch_exit(2);
1925#endif
1926 break;
1927
1928 case 'b': /* "-b" binary mode */
Bram Moolenaar231334e2005-07-25 20:46:57 +00001929 /* Needs to be effective before expanding file names, because
1930 * for Win32 this makes us edit a shortcut file itself,
1931 * instead of the file it links to. */
1932 set_options_bin(curbuf->b_p_bin, 1, 0);
1933 curbuf->b_p_bin = 1; /* binary file I/O */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001934 break;
1935
1936 case 'C': /* "-C" Compatible */
1937 change_compatible(TRUE);
1938 break;
1939
1940 case 'e': /* "-e" Ex mode */
1941 exmode_active = EXMODE_NORMAL;
1942 break;
1943
1944 case 'E': /* "-E" Improved Ex mode */
1945 exmode_active = EXMODE_VIM;
1946 break;
1947
1948 case 'f': /* "-f" GUI: run in foreground. Amiga: open
1949 window directly, not with newcli */
1950#ifdef FEAT_GUI
1951 gui.dofork = FALSE; /* don't fork() when starting GUI */
1952#endif
1953 break;
1954
1955 case 'g': /* "-g" start GUI */
1956 main_start_gui();
1957 break;
1958
1959 case 'F': /* "-F" start in Farsi mode: rl + fkmap set */
1960#ifdef FEAT_FKMAP
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00001961 p_fkmap = TRUE;
1962 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001963#else
1964 mch_errmsg(_(e_nofarsi));
1965 mch_exit(2);
1966#endif
1967 break;
1968
1969 case 'h': /* "-h" give help message */
1970#ifdef FEAT_GUI_GNOME
1971 /* Tell usage() to exit for "gvim". */
1972 gui.starting = FALSE;
1973#endif
1974 usage();
1975 break;
1976
1977 case 'H': /* "-H" start in Hebrew mode: rl + hkmap set */
1978#ifdef FEAT_RIGHTLEFT
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00001979 p_hkmap = TRUE;
1980 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001981#else
1982 mch_errmsg(_(e_nohebrew));
1983 mch_exit(2);
1984#endif
1985 break;
1986
1987 case 'l': /* "-l" lisp mode, 'lisp' and 'showmatch' on */
1988#ifdef FEAT_LISP
1989 set_option_value((char_u *)"lisp", 1L, NULL, 0);
1990 p_sm = TRUE;
1991#endif
1992 break;
1993
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001994 case 'M': /* "-M" no changes or writing of files */
1995 reset_modifiable();
1996 /* FALLTHROUGH */
1997
1998 case 'm': /* "-m" no writing of files */
1999 p_write = FALSE;
2000 break;
2001
2002 case 'y': /* "-y" easy mode */
2003#ifdef FEAT_GUI
2004 gui.starting = TRUE; /* start GUI a bit later */
2005#endif
2006 parmp->evim_mode = TRUE;
2007 break;
2008
2009 case 'N': /* "-N" Nocompatible */
2010 change_compatible(FALSE);
2011 break;
2012
2013 case 'n': /* "-n" no swap file */
Bram Moolenaar67c53842010-05-22 18:28:27 +02002014#ifdef FEAT_NETBEANS_INTG
2015 /* checking for "-nb", netbeans parameters */
2016 if (argv[0][argv_idx] == 'b')
2017 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002018 netbeansArg = argv[0];
2019 argv_idx = -1; /* skip to next argument */
2020 }
2021 else
2022#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002023 parmp->no_swap_file = TRUE;
2024 break;
2025
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002026 case 'p': /* "-p[N]" open N tab pages */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002027#ifdef TARGET_API_MAC_OSX
2028 /* For some reason on MacOS X, an argument like:
2029 -psn_0_10223617 is passed in when invoke from Finder
2030 or with the 'open' command */
2031 if (argv[0][argv_idx] == 's')
2032 {
2033 argv_idx = -1; /* bypass full -psn */
2034 main_start_gui();
2035 break;
2036 }
2037#endif
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002038#ifdef FEAT_WINDOWS
2039 /* default is 0: open window for each file */
2040 parmp->window_count = get_number_arg((char_u *)argv[0],
2041 &argv_idx, 0);
2042 parmp->window_layout = WIN_TABS;
2043#endif
2044 break;
2045
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002046 case 'o': /* "-o[N]" open N horizontal split windows */
2047#ifdef FEAT_WINDOWS
2048 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002049 parmp->window_count = get_number_arg((char_u *)argv[0],
2050 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002051 parmp->window_layout = WIN_HOR;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002052#endif
2053 break;
2054
2055 case 'O': /* "-O[N]" open N vertical split windows */
2056#if defined(FEAT_VERTSPLIT) && defined(FEAT_WINDOWS)
2057 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002058 parmp->window_count = get_number_arg((char_u *)argv[0],
2059 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002060 parmp->window_layout = WIN_VER;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002061#endif
2062 break;
2063
2064#ifdef FEAT_QUICKFIX
2065 case 'q': /* "-q" QuickFix mode */
2066 if (parmp->edit_type != EDIT_NONE)
2067 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2068 parmp->edit_type = EDIT_QF;
2069 if (argv[0][argv_idx]) /* "-q{errorfile}" */
2070 {
2071 parmp->use_ef = (char_u *)argv[0] + argv_idx;
2072 argv_idx = -1;
2073 }
2074 else if (argc > 1) /* "-q {errorfile}" */
2075 want_argument = TRUE;
2076 break;
2077#endif
2078
2079 case 'R': /* "-R" readonly mode */
2080 readonlymode = TRUE;
2081 curbuf->b_p_ro = TRUE;
2082 p_uc = 10000; /* don't update very often */
2083 break;
2084
2085 case 'r': /* "-r" recovery mode */
2086 case 'L': /* "-L" recovery mode */
2087 recoverymode = 1;
2088 break;
2089
2090 case 's':
2091 if (exmode_active) /* "-s" silent (batch) mode */
2092 silent_mode = TRUE;
2093 else /* "-s {scriptin}" read from script file */
2094 want_argument = TRUE;
2095 break;
2096
2097 case 't': /* "-t {tag}" or "-t{tag}" jump to tag */
2098 if (parmp->edit_type != EDIT_NONE)
2099 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2100 parmp->edit_type = EDIT_TAG;
2101 if (argv[0][argv_idx]) /* "-t{tag}" */
2102 {
2103 parmp->tagname = (char_u *)argv[0] + argv_idx;
2104 argv_idx = -1;
2105 }
2106 else /* "-t {tag}" */
2107 want_argument = TRUE;
2108 break;
2109
2110#ifdef FEAT_EVAL
2111 case 'D': /* "-D" Debugging */
2112 parmp->use_debug_break_level = 9999;
2113 break;
2114#endif
2115#ifdef FEAT_DIFF
2116 case 'd': /* "-d" 'diff' */
2117# ifdef AMIGA
2118 /* check for "-dev {device}" */
2119 if (argv[0][argv_idx] == 'e' && argv[0][argv_idx + 1] == 'v')
2120 want_argument = TRUE;
2121 else
2122# endif
2123 parmp->diff_mode = TRUE;
2124 break;
2125#endif
2126 case 'V': /* "-V{N}" Verbose level */
2127 /* default is 10: a little bit verbose */
2128 p_verbose = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2129 if (argv[0][argv_idx] != NUL)
2130 {
2131 set_option_value((char_u *)"verbosefile", 0L,
2132 (char_u *)argv[0] + argv_idx, 0);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002133 argv_idx = (int)STRLEN(argv[0]);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002134 }
2135 break;
2136
2137 case 'v': /* "-v" Vi-mode (as if called "vi") */
2138 exmode_active = 0;
2139#ifdef FEAT_GUI
2140 gui.starting = FALSE; /* don't start GUI */
2141#endif
2142 break;
2143
2144 case 'w': /* "-w{number}" set window height */
2145 /* "-w {scriptout}" write to script */
2146 if (vim_isdigit(((char_u *)argv[0])[argv_idx]))
2147 {
2148 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2149 set_option_value((char_u *)"window", n, NULL, 0);
2150 break;
2151 }
2152 want_argument = TRUE;
2153 break;
2154
2155#ifdef FEAT_CRYPT
2156 case 'x': /* "-x" encrypted reading/writing of files */
2157 parmp->ask_for_key = TRUE;
2158 break;
2159#endif
2160
2161 case 'X': /* "-X" don't connect to X server */
2162#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
2163 x_no_connect = TRUE;
2164#endif
2165 break;
2166
2167 case 'Z': /* "-Z" restricted mode */
2168 restricted = TRUE;
2169 break;
2170
2171 case 'c': /* "-c{command}" or "-c {command}" execute
2172 command */
2173 if (argv[0][argv_idx] != NUL)
2174 {
2175 if (parmp->n_commands >= MAX_ARG_CMDS)
2176 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar231334e2005-07-25 20:46:57 +00002177 parmp->commands[parmp->n_commands++] = (char_u *)argv[0]
2178 + argv_idx;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002179 argv_idx = -1;
2180 break;
2181 }
2182 /*FALLTHROUGH*/
2183 case 'S': /* "-S {file}" execute Vim script */
2184 case 'i': /* "-i {viminfo}" use for viminfo */
2185#ifndef FEAT_DIFF
2186 case 'd': /* "-d {device}" device (for Amiga) */
2187#endif
2188 case 'T': /* "-T {terminal}" terminal name */
2189 case 'u': /* "-u {vimrc}" vim inits file */
2190 case 'U': /* "-U {gvimrc}" gvim inits file */
2191 case 'W': /* "-W {scriptout}" overwrite */
2192#ifdef FEAT_GUI_W32
2193 case 'P': /* "-P {parent title}" MDI parent */
2194#endif
2195 want_argument = TRUE;
2196 break;
2197
2198 default:
2199 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
2200 }
2201
2202 /*
2203 * Handle option arguments with argument.
2204 */
2205 if (want_argument)
2206 {
2207 /*
2208 * Check for garbage immediately after the option letter.
2209 */
2210 if (argv[0][argv_idx] != NUL)
2211 mainerr(ME_GARBAGE, (char_u *)argv[0]);
2212
2213 --argc;
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002214 if (argc < 1 && c != 'S') /* -S has an optional argument */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002215 mainerr_arg_missing((char_u *)argv[0]);
2216 ++argv;
2217 argv_idx = -1;
2218
2219 switch (c)
2220 {
2221 case 'c': /* "-c {command}" execute command */
2222 case 'S': /* "-S {file}" execute Vim script */
2223 if (parmp->n_commands >= MAX_ARG_CMDS)
2224 mainerr(ME_EXTRA_CMD, NULL);
2225 if (c == 'S')
2226 {
2227 char *a;
2228
2229 if (argc < 1)
2230 /* "-S" without argument: use default session file
2231 * name. */
2232 a = SESSION_FILE;
2233 else if (argv[0][0] == '-')
2234 {
2235 /* "-S" followed by another option: use default
2236 * session file name. */
2237 a = SESSION_FILE;
2238 ++argc;
2239 --argv;
2240 }
2241 else
2242 a = argv[0];
2243 p = alloc((unsigned)(STRLEN(a) + 4));
2244 if (p == NULL)
2245 mch_exit(2);
2246 sprintf((char *)p, "so %s", a);
2247 parmp->cmds_tofree[parmp->n_commands] = TRUE;
2248 parmp->commands[parmp->n_commands++] = p;
2249 }
2250 else
Bram Moolenaar231334e2005-07-25 20:46:57 +00002251 parmp->commands[parmp->n_commands++] =
2252 (char_u *)argv[0];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002253 break;
2254
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002255 case '-':
2256 if (argv[-1][2] == 'c')
2257 {
2258 /* "--cmd {command}" execute command */
2259 if (parmp->n_pre_commands >= MAX_ARG_CMDS)
2260 mainerr(ME_EXTRA_CMD, NULL);
2261 parmp->pre_commands[parmp->n_pre_commands++] =
Bram Moolenaar231334e2005-07-25 20:46:57 +00002262 (char_u *)argv[0];
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002263 }
2264 /* "--startuptime <file>" already handled */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002265 break;
2266
2267 /* case 'd': -d {device} is handled in mch_check_win() for the
2268 * Amiga */
2269
2270#ifdef FEAT_QUICKFIX
2271 case 'q': /* "-q {errorfile}" QuickFix mode */
2272 parmp->use_ef = (char_u *)argv[0];
2273 break;
2274#endif
2275
2276 case 'i': /* "-i {viminfo}" use for viminfo */
2277 use_viminfo = (char_u *)argv[0];
2278 break;
2279
2280 case 's': /* "-s {scriptin}" read from script file */
2281 if (scriptin[0] != NULL)
2282 {
2283scripterror:
2284 mch_errmsg(_("Attempt to open script file again: \""));
2285 mch_errmsg(argv[-1]);
2286 mch_errmsg(" ");
2287 mch_errmsg(argv[0]);
2288 mch_errmsg("\"\n");
2289 mch_exit(2);
2290 }
2291 if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL)
2292 {
2293 mch_errmsg(_("Cannot open for reading: \""));
2294 mch_errmsg(argv[0]);
2295 mch_errmsg("\"\n");
2296 mch_exit(2);
2297 }
2298 if (save_typebuf() == FAIL)
2299 mch_exit(2); /* out of memory */
2300 break;
2301
2302 case 't': /* "-t {tag}" */
2303 parmp->tagname = (char_u *)argv[0];
2304 break;
2305
2306 case 'T': /* "-T {terminal}" terminal name */
2307 /*
2308 * The -T term argument is always available and when
2309 * HAVE_TERMLIB is supported it overrides the environment
2310 * variable TERM.
2311 */
2312#ifdef FEAT_GUI
2313 if (term_is_gui((char_u *)argv[0]))
2314 gui.starting = TRUE; /* start GUI a bit later */
2315 else
2316#endif
2317 parmp->term = (char_u *)argv[0];
2318 break;
2319
2320 case 'u': /* "-u {vimrc}" vim inits file */
2321 parmp->use_vimrc = (char_u *)argv[0];
2322 break;
2323
2324 case 'U': /* "-U {gvimrc}" gvim inits file */
2325#ifdef FEAT_GUI
2326 use_gvimrc = (char_u *)argv[0];
2327#endif
2328 break;
2329
2330 case 'w': /* "-w {nr}" 'window' value */
2331 /* "-w {scriptout}" append to script file */
2332 if (vim_isdigit(*((char_u *)argv[0])))
2333 {
2334 argv_idx = 0;
2335 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2336 set_option_value((char_u *)"window", n, NULL, 0);
2337 argv_idx = -1;
2338 break;
2339 }
2340 /*FALLTHROUGH*/
2341 case 'W': /* "-W {scriptout}" overwrite script file */
2342 if (scriptout != NULL)
2343 goto scripterror;
2344 if ((scriptout = mch_fopen(argv[0],
2345 c == 'w' ? APPENDBIN : WRITEBIN)) == NULL)
2346 {
2347 mch_errmsg(_("Cannot open for script output: \""));
2348 mch_errmsg(argv[0]);
2349 mch_errmsg("\"\n");
2350 mch_exit(2);
2351 }
2352 break;
2353
2354#ifdef FEAT_GUI_W32
2355 case 'P': /* "-P {parent title}" MDI parent */
2356 gui_mch_set_parent(argv[0]);
2357 break;
2358#endif
2359 }
2360 }
2361 }
2362
2363 /*
2364 * File name argument.
2365 */
2366 else
2367 {
2368 argv_idx = -1; /* skip to next argument */
2369
2370 /* Check for only one type of editing. */
2371 if (parmp->edit_type != EDIT_NONE && parmp->edit_type != EDIT_FILE)
2372 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2373 parmp->edit_type = EDIT_FILE;
2374
2375#ifdef MSWIN
2376 /* Remember if the argument was a full path before changing
2377 * slashes to backslashes. */
2378 if (argv[0][0] != NUL && argv[0][1] == ':' && argv[0][2] == '\\')
2379 parmp->full_path = TRUE;
2380#endif
2381
2382 /* Add the file to the global argument list. */
2383 if (ga_grow(&global_alist.al_ga, 1) == FAIL
2384 || (p = vim_strsave((char_u *)argv[0])) == NULL)
2385 mch_exit(2);
2386#ifdef FEAT_DIFF
2387 if (parmp->diff_mode && mch_isdir(p) && GARGCOUNT > 0
2388 && !mch_isdir(alist_name(&GARGLIST[0])))
2389 {
2390 char_u *r;
2391
2392 r = concat_fnames(p, gettail(alist_name(&GARGLIST[0])), TRUE);
2393 if (r != NULL)
2394 {
2395 vim_free(p);
2396 p = r;
2397 }
2398 }
2399#endif
2400#if defined(__CYGWIN32__) && !defined(WIN32)
2401 /*
2402 * If vim is invoked by non-Cygwin tools, convert away any
2403 * DOS paths, so things like .swp files are created correctly.
2404 * Look for evidence of non-Cygwin paths before we bother.
2405 * This is only for when using the Unix files.
2406 */
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002407 if (vim_strpbrk(p, "\\:") != NULL && !path_with_url(p))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002408 {
2409 char posix_path[PATH_MAX];
2410
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002411# if CYGWIN_VERSION_DLL_MAJOR >= 1007
2412 cygwin_conv_path(CCP_WIN_A_TO_POSIX, p, posix_path, PATH_MAX);
2413# else
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002414 cygwin_conv_to_posix_path(p, posix_path);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002415# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002416 vim_free(p);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002417 p = vim_strsave((char_u *)posix_path);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002418 if (p == NULL)
2419 mch_exit(2);
2420 }
2421#endif
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002422
2423#ifdef USE_FNAME_CASE
2424 /* Make the case of the file name match the actual file. */
2425 fname_case(p, 0);
2426#endif
2427
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002428 alist_add(&global_alist, p,
2429#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
Bram Moolenaar231334e2005-07-25 20:46:57 +00002430 parmp->literal ? 2 : 0 /* add buffer nr after exp. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002431#else
2432 2 /* add buffer number now and use curbuf */
2433#endif
2434 );
2435
2436#if defined(FEAT_MBYTE) && defined(WIN32)
2437 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002438 /* Remember this argument has been added to the argument list.
2439 * Needed when 'encoding' is changed. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002440 used_file_arg(argv[0], parmp->literal, parmp->full_path,
Bram Moolenaar688e5f72008-07-24 11:51:40 +00002441# ifdef FEAT_DIFF
2442 parmp->diff_mode
2443# else
2444 FALSE
2445# endif
2446 );
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002447 }
2448#endif
2449 }
2450
2451 /*
2452 * If there are no more letters after the current "-", go to next
2453 * argument. argv_idx is set to -1 when the current argument is to be
2454 * skipped.
2455 */
2456 if (argv_idx <= 0 || argv[0][argv_idx] == NUL)
2457 {
2458 --argc;
2459 ++argv;
2460 argv_idx = 1;
2461 }
2462 }
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002463
2464#ifdef FEAT_EVAL
2465 /* If there is a "+123" or "-c" command, set v:swapcommand to the first
2466 * one. */
2467 if (parmp->n_commands > 0)
2468 {
2469 p = alloc((unsigned)STRLEN(parmp->commands[0]) + 3);
2470 if (p != NULL)
2471 {
2472 sprintf((char *)p, ":%s\r", parmp->commands[0]);
2473 set_vim_var_string(VV_SWAPCOMMAND, p, -1);
2474 vim_free(p);
2475 }
2476 }
2477#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002478}
2479
2480/*
2481 * Print a warning if stdout is not a terminal.
2482 * When starting in Ex mode and commands come from a file, set Silent mode.
2483 */
2484 static void
2485check_tty(parmp)
2486 mparm_T *parmp;
2487{
2488 int input_isatty; /* is active input a terminal? */
2489
2490 input_isatty = mch_input_isatty();
2491 if (exmode_active)
2492 {
2493 if (!input_isatty)
2494 silent_mode = TRUE;
2495 }
2496 else if (parmp->want_full_screen && (!parmp->stdout_isatty || !input_isatty)
2497#ifdef FEAT_GUI
2498 /* don't want the delay when started from the desktop */
2499 && !gui.starting
2500#endif
2501 )
2502 {
2503#ifdef NBDEBUG
2504 /*
2505 * This shouldn't be necessary. But if I run netbeans with the log
2506 * output coming to the console and XOpenDisplay fails, I get vim
2507 * trying to start with input/output to my console tty. This fills my
2508 * input buffer so fast I can't even kill the process in under 2
Bram Moolenaar49325942007-05-10 19:19:59 +00002509 * minutes (and it beeps continuously the whole time :-)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002510 */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002511 if (netbeans_active() && (!parmp->stdout_isatty || !input_isatty))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002512 {
2513 mch_errmsg(_("Vim: Error: Failure to start gvim from NetBeans\n"));
2514 exit(1);
2515 }
2516#endif
2517 if (!parmp->stdout_isatty)
2518 mch_errmsg(_("Vim: Warning: Output is not to a terminal\n"));
2519 if (!input_isatty)
2520 mch_errmsg(_("Vim: Warning: Input is not from a terminal\n"));
2521 out_flush();
2522 if (scriptin[0] == NULL)
2523 ui_delay(2000L, TRUE);
2524 TIME_MSG("Warning delay");
2525 }
2526}
2527
2528/*
2529 * Read text from stdin.
2530 */
2531 static void
2532read_stdin()
2533{
2534 int i;
2535
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002536#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002537 /* When getting the ATTENTION prompt here, use a dialog */
2538 swap_exists_action = SEA_DIALOG;
2539#endif
2540 no_wait_return = TRUE;
2541 i = msg_didany;
2542 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002543 (void)open_buffer(TRUE, NULL, 0); /* create memfile and read file */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002544 no_wait_return = FALSE;
2545 msg_didany = i;
2546 TIME_MSG("reading stdin");
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002547#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002548 check_swap_exists_action();
2549#endif
2550#if !(defined(AMIGA) || defined(MACOS))
2551 /*
2552 * Close stdin and dup it from stderr. Required for GPM to work
2553 * properly, and for running external commands.
2554 * Is there any other system that cannot do this?
2555 */
2556 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00002557 ignored = dup(2);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002558#endif
2559}
2560
2561/*
2562 * Create the requested number of windows and edit buffers in them.
2563 * Also does recovery if "recoverymode" set.
2564 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002565 static void
2566create_windows(parmp)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002567 mparm_T *parmp UNUSED;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002568{
2569#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002570 int dorewind;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002571 int done = 0;
2572
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002573 /*
2574 * Create the number of windows that was requested.
2575 */
2576 if (parmp->window_count == -1) /* was not set */
2577 parmp->window_count = 1;
2578 if (parmp->window_count == 0)
2579 parmp->window_count = GARGCOUNT;
2580 if (parmp->window_count > 1)
2581 {
2582 /* Don't change the windows if there was a command in .vimrc that
2583 * already split some windows */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002584 if (parmp->window_layout == 0)
2585 parmp->window_layout = WIN_HOR;
2586 if (parmp->window_layout == WIN_TABS)
2587 {
2588 parmp->window_count = make_tabpages(parmp->window_count);
2589 TIME_MSG("making tab pages");
2590 }
2591 else if (firstwin->w_next == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002592 {
2593 parmp->window_count = make_windows(parmp->window_count,
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002594 parmp->window_layout == WIN_VER);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002595 TIME_MSG("making windows");
2596 }
2597 else
2598 parmp->window_count = win_count();
2599 }
2600 else
2601 parmp->window_count = 1;
2602#endif
2603
2604 if (recoverymode) /* do recover */
2605 {
2606 msg_scroll = TRUE; /* scroll message up */
2607 ml_recover();
2608 if (curbuf->b_ml.ml_mfp == NULL) /* failed */
2609 getout(1);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002610 do_modelines(0); /* do modelines */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002611 }
2612 else
2613 {
2614 /*
2615 * Open a buffer for windows that don't have one yet.
2616 * Commands in the .vimrc might have loaded a file or split the window.
2617 * Watch out for autocommands that delete a window.
2618 */
2619#ifdef FEAT_AUTOCMD
2620 /*
2621 * Don't execute Win/Buf Enter/Leave autocommands here
2622 */
2623 ++autocmd_no_enter;
2624 ++autocmd_no_leave;
2625#endif
2626#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002627 dorewind = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002628 while (done++ < 1000)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002629 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002630 if (dorewind)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002631 {
2632 if (parmp->window_layout == WIN_TABS)
2633 goto_tabpage(1);
2634 else
2635 curwin = firstwin;
2636 }
2637 else if (parmp->window_layout == WIN_TABS)
2638 {
2639 if (curtab->tp_next == NULL)
2640 break;
2641 goto_tabpage(0);
2642 }
2643 else
2644 {
2645 if (curwin->w_next == NULL)
2646 break;
2647 curwin = curwin->w_next;
2648 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002649 dorewind = FALSE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002650#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002651 curbuf = curwin->w_buffer;
2652 if (curbuf->b_ml.ml_mfp == NULL)
2653 {
2654#ifdef FEAT_FOLDING
2655 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2656 if (p_fdls >= 0)
2657 curwin->w_p_fdl = p_fdls;
2658#endif
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002659#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002660 /* When getting the ATTENTION prompt here, use a dialog */
2661 swap_exists_action = SEA_DIALOG;
2662#endif
2663 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002664
2665 /* create memfile, read file */
2666 (void)open_buffer(FALSE, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002667
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002668#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar84212822006-11-07 21:59:47 +00002669 if (swap_exists_action == SEA_QUIT)
2670 {
2671 if (got_int || only_one_window())
2672 {
2673 /* abort selected or quit and only one window */
2674 did_emsg = FALSE; /* avoid hit-enter prompt */
2675 getout(1);
2676 }
2677 /* We can't close the window, it would disturb what
2678 * happens next. Clear the file name and set the arg
2679 * index to -1 to delete it later. */
2680 setfname(curbuf, NULL, NULL, FALSE);
2681 curwin->w_arg_idx = -1;
2682 swap_exists_action = SEA_NONE;
2683 }
2684 else
2685 handle_swap_exists(NULL);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002686#endif
2687#ifdef FEAT_AUTOCMD
Bram Moolenaar89d40322006-08-29 15:30:07 +00002688 dorewind = TRUE; /* start again */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002689#endif
2690 }
2691#ifdef FEAT_WINDOWS
2692 ui_breakcheck();
2693 if (got_int)
2694 {
2695 (void)vgetc(); /* only break the file loading, not the rest */
2696 break;
2697 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002698 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002699#endif
2700#ifdef FEAT_WINDOWS
2701 if (parmp->window_layout == WIN_TABS)
2702 goto_tabpage(1);
2703 else
2704 curwin = firstwin;
2705 curbuf = curwin->w_buffer;
2706#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002707#ifdef FEAT_AUTOCMD
2708 --autocmd_no_enter;
2709 --autocmd_no_leave;
2710#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002711 }
2712}
2713
2714#ifdef FEAT_WINDOWS
2715 /*
2716 * If opened more than one window, start editing files in the other
2717 * windows. make_windows() has already opened the windows.
2718 */
2719 static void
2720edit_buffers(parmp)
2721 mparm_T *parmp;
2722{
2723 int arg_idx; /* index in argument list */
2724 int i;
Bram Moolenaar84212822006-11-07 21:59:47 +00002725 int advance = TRUE;
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002726 win_T *win;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002727
2728# ifdef FEAT_AUTOCMD
2729 /*
2730 * Don't execute Win/Buf Enter/Leave autocommands here
2731 */
2732 ++autocmd_no_enter;
2733 ++autocmd_no_leave;
2734# endif
Bram Moolenaar84212822006-11-07 21:59:47 +00002735
2736 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2737 if (curwin->w_arg_idx == -1)
2738 {
2739 win_close(curwin, TRUE);
2740 advance = FALSE;
2741 }
2742
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002743 arg_idx = 1;
2744 for (i = 1; i < parmp->window_count; ++i)
2745 {
Bram Moolenaar84212822006-11-07 21:59:47 +00002746 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2747 if (curwin->w_arg_idx == -1)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002748 {
Bram Moolenaar84212822006-11-07 21:59:47 +00002749 ++arg_idx;
2750 win_close(curwin, TRUE);
2751 advance = FALSE;
2752 continue;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002753 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002754
Bram Moolenaar84212822006-11-07 21:59:47 +00002755 if (advance)
2756 {
2757 if (parmp->window_layout == WIN_TABS)
2758 {
2759 if (curtab->tp_next == NULL) /* just checking */
2760 break;
2761 goto_tabpage(0);
2762 }
2763 else
2764 {
2765 if (curwin->w_next == NULL) /* just checking */
2766 break;
2767 win_enter(curwin->w_next, FALSE);
2768 }
2769 }
2770 advance = TRUE;
2771
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002772 /* Only open the file if there is no file in this window yet (that can
Bram Moolenaar84212822006-11-07 21:59:47 +00002773 * happen when .vimrc contains ":sall"). */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002774 if (curbuf == firstwin->w_buffer || curbuf->b_ffname == NULL)
2775 {
2776 curwin->w_arg_idx = arg_idx;
Bram Moolenaar84212822006-11-07 21:59:47 +00002777 /* Edit file from arg list, if there is one. When "Quit" selected
2778 * at the ATTENTION prompt close the window. */
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002779# ifdef HAS_SWAP_EXISTS_ACTION
2780 swap_exists_did_quit = FALSE;
2781# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002782 (void)do_ecmd(0, arg_idx < GARGCOUNT
2783 ? alist_name(&GARGLIST[arg_idx]) : NULL,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002784 NULL, NULL, ECMD_LASTL, ECMD_HIDE, curwin);
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002785# ifdef HAS_SWAP_EXISTS_ACTION
2786 if (swap_exists_did_quit)
Bram Moolenaar84212822006-11-07 21:59:47 +00002787 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002788 /* abort or quit selected */
Bram Moolenaar84212822006-11-07 21:59:47 +00002789 if (got_int || only_one_window())
2790 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002791 /* abort selected and only one window */
Bram Moolenaar84212822006-11-07 21:59:47 +00002792 did_emsg = FALSE; /* avoid hit-enter prompt */
2793 getout(1);
2794 }
2795 win_close(curwin, TRUE);
2796 advance = FALSE;
2797 }
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002798# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002799 if (arg_idx == GARGCOUNT - 1)
2800 arg_had_last = TRUE;
2801 ++arg_idx;
2802 }
2803 ui_breakcheck();
2804 if (got_int)
2805 {
2806 (void)vgetc(); /* only break the file loading, not the rest */
2807 break;
2808 }
2809 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002810
2811 if (parmp->window_layout == WIN_TABS)
2812 goto_tabpage(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002813# ifdef FEAT_AUTOCMD
2814 --autocmd_no_enter;
2815# endif
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002816
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002817 /* make the first window the current window */
2818 win = firstwin;
2819#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
2820 /* Avoid making a preview window the current window. */
2821 while (win->w_p_pvw)
2822 {
2823 win = win->w_next;
2824 if (win == NULL)
2825 {
2826 win = firstwin;
2827 break;
2828 }
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002829 }
2830#endif
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002831 win_enter(win, FALSE);
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002832
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002833# ifdef FEAT_AUTOCMD
2834 --autocmd_no_leave;
2835# endif
2836 TIME_MSG("editing files in windows");
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002837 if (parmp->window_count > 1 && parmp->window_layout != WIN_TABS)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002838 win_equal(curwin, FALSE, 'b'); /* adjust heights */
2839}
2840#endif /* FEAT_WINDOWS */
2841
2842/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002843 * Execute the commands from --cmd arguments "cmds[cnt]".
2844 */
2845 static void
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002846exe_pre_commands(parmp)
2847 mparm_T *parmp;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002848{
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002849 char_u **cmds = parmp->pre_commands;
2850 int cnt = parmp->n_pre_commands;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002851 int i;
2852
2853 if (cnt > 0)
2854 {
2855 curwin->w_cursor.lnum = 0; /* just in case.. */
2856 sourcing_name = (char_u *)_("pre-vimrc command line");
2857# ifdef FEAT_EVAL
2858 current_SID = SID_CMDARG;
2859# endif
2860 for (i = 0; i < cnt; ++i)
2861 do_cmdline_cmd(cmds[i]);
2862 sourcing_name = NULL;
2863# ifdef FEAT_EVAL
2864 current_SID = 0;
2865# endif
2866 TIME_MSG("--cmd commands");
2867 }
2868}
2869
2870/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002871 * Execute "+", "-c" and "-S" arguments.
2872 */
2873 static void
2874exe_commands(parmp)
2875 mparm_T *parmp;
2876{
2877 int i;
2878
2879 /*
2880 * We start commands on line 0, make "vim +/pat file" match a
2881 * pattern on line 1. But don't move the cursor when an autocommand
2882 * with g`" was used.
2883 */
2884 msg_scroll = TRUE;
2885 if (parmp->tagname == NULL && curwin->w_cursor.lnum <= 1)
2886 curwin->w_cursor.lnum = 0;
2887 sourcing_name = (char_u *)"command line";
2888#ifdef FEAT_EVAL
2889 current_SID = SID_CARG;
2890#endif
2891 for (i = 0; i < parmp->n_commands; ++i)
2892 {
2893 do_cmdline_cmd(parmp->commands[i]);
2894 if (parmp->cmds_tofree[i])
2895 vim_free(parmp->commands[i]);
2896 }
2897 sourcing_name = NULL;
2898#ifdef FEAT_EVAL
2899 current_SID = 0;
2900#endif
2901 if (curwin->w_cursor.lnum == 0)
2902 curwin->w_cursor.lnum = 1;
2903
2904 if (!exmode_active)
2905 msg_scroll = FALSE;
2906
2907#ifdef FEAT_QUICKFIX
2908 /* When started with "-q errorfile" jump to first error again. */
2909 if (parmp->edit_type == EDIT_QF)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002910 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002911#endif
2912 TIME_MSG("executing command arguments");
2913}
2914
2915/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002916 * Source startup scripts.
2917 */
2918 static void
2919source_startup_scripts(parmp)
2920 mparm_T *parmp;
2921{
2922 int i;
2923
2924 /*
2925 * For "evim" source evim.vim first of all, so that the user can overrule
2926 * any things he doesn't like.
2927 */
2928 if (parmp->evim_mode)
2929 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002930 (void)do_source((char_u *)EVIM_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002931 TIME_MSG("source evim file");
2932 }
2933
2934 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002935 * If -u argument given, use only the initializations from that file and
Bram Moolenaar58d98232005-07-23 22:25:46 +00002936 * nothing else.
2937 */
2938 if (parmp->use_vimrc != NULL)
2939 {
Bram Moolenaar231334e2005-07-25 20:46:57 +00002940 if (STRCMP(parmp->use_vimrc, "NONE") == 0
2941 || STRCMP(parmp->use_vimrc, "NORC") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002942 {
2943#ifdef FEAT_GUI
2944 if (use_gvimrc == NULL) /* don't load gvimrc either */
2945 use_gvimrc = parmp->use_vimrc;
2946#endif
2947 if (parmp->use_vimrc[2] == 'N')
2948 p_lpl = FALSE; /* don't load plugins either */
2949 }
2950 else
2951 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002952 if (do_source(parmp->use_vimrc, FALSE, DOSO_NONE) != OK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002953 EMSG2(_("E282: Cannot read from \"%s\""), parmp->use_vimrc);
2954 }
2955 }
2956 else if (!silent_mode)
2957 {
2958#ifdef AMIGA
2959 struct Process *proc = (struct Process *)FindTask(0L);
2960 APTR save_winptr = proc->pr_WindowPtr;
2961
2962 /* Avoid a requester here for a volume that doesn't exist. */
2963 proc->pr_WindowPtr = (APTR)-1L;
2964#endif
2965
2966 /*
2967 * Get system wide defaults, if the file name is defined.
2968 */
2969#ifdef SYS_VIMRC_FILE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002970 (void)do_source((char_u *)SYS_VIMRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002971#endif
Bram Moolenaar1056d982006-03-09 22:37:52 +00002972#ifdef MACOS_X
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002973 (void)do_source((char_u *)"$VIMRUNTIME/macmap.vim", FALSE, DOSO_NONE);
Bram Moolenaar1056d982006-03-09 22:37:52 +00002974#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00002975
2976 /*
2977 * Try to read initialization commands from the following places:
2978 * - environment variable VIMINIT
2979 * - user vimrc file (s:.vimrc for Amiga, ~/.vimrc otherwise)
2980 * - second user vimrc file ($VIM/.vimrc for Dos)
2981 * - environment variable EXINIT
2982 * - user exrc file (s:.exrc for Amiga, ~/.exrc otherwise)
2983 * - second user exrc file ($VIM/.exrc for Dos)
2984 * The first that exists is used, the rest is ignored.
2985 */
2986 if (process_env((char_u *)"VIMINIT", TRUE) != OK)
2987 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002988 if (do_source((char_u *)USR_VIMRC_FILE, TRUE, DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00002989#ifdef USR_VIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002990 && do_source((char_u *)USR_VIMRC_FILE2, TRUE,
2991 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00002992#endif
2993#ifdef USR_VIMRC_FILE3
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002994 && do_source((char_u *)USR_VIMRC_FILE3, TRUE,
2995 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00002996#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +02002997#ifdef USR_VIMRC_FILE4
2998 && do_source((char_u *)USR_VIMRC_FILE4, TRUE,
2999 DOSO_VIMRC) == FAIL
3000#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003001 && process_env((char_u *)"EXINIT", FALSE) == FAIL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003002 && do_source((char_u *)USR_EXRC_FILE, FALSE, DOSO_NONE) == FAIL)
Bram Moolenaar58d98232005-07-23 22:25:46 +00003003 {
3004#ifdef USR_EXRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003005 (void)do_source((char_u *)USR_EXRC_FILE2, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003006#endif
3007 }
3008 }
3009
3010 /*
3011 * Read initialization commands from ".vimrc" or ".exrc" in current
3012 * directory. This is only done if the 'exrc' option is set.
3013 * Because of security reasons we disallow shell and write commands
3014 * now, except for unix if the file is owned by the user or 'secure'
3015 * option has been reset in environment of global ".exrc" or ".vimrc".
3016 * Only do this if VIMRC_FILE is not the same as USR_VIMRC_FILE or
3017 * SYS_VIMRC_FILE.
3018 */
3019 if (p_exrc)
3020 {
3021#if defined(UNIX) || defined(VMS)
3022 /* If ".vimrc" file is not owned by user, set 'secure' mode. */
3023 if (!file_owned(VIMRC_FILE))
3024#endif
3025 secure = p_secure;
3026
3027 i = FAIL;
3028 if (fullpathcmp((char_u *)USR_VIMRC_FILE,
3029 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3030#ifdef USR_VIMRC_FILE2
3031 && fullpathcmp((char_u *)USR_VIMRC_FILE2,
3032 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3033#endif
3034#ifdef USR_VIMRC_FILE3
3035 && fullpathcmp((char_u *)USR_VIMRC_FILE3,
3036 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3037#endif
3038#ifdef SYS_VIMRC_FILE
3039 && fullpathcmp((char_u *)SYS_VIMRC_FILE,
3040 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3041#endif
3042 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003043 i = do_source((char_u *)VIMRC_FILE, TRUE, DOSO_VIMRC);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003044
3045 if (i == FAIL)
3046 {
3047#if defined(UNIX) || defined(VMS)
3048 /* if ".exrc" is not owned by user set 'secure' mode */
3049 if (!file_owned(EXRC_FILE))
3050 secure = p_secure;
3051 else
3052 secure = 0;
3053#endif
3054 if ( fullpathcmp((char_u *)USR_EXRC_FILE,
3055 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3056#ifdef USR_EXRC_FILE2
3057 && fullpathcmp((char_u *)USR_EXRC_FILE2,
3058 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3059#endif
3060 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003061 (void)do_source((char_u *)EXRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003062 }
3063 }
3064 if (secure == 2)
3065 need_wait_return = TRUE;
3066 secure = 0;
3067#ifdef AMIGA
3068 proc->pr_WindowPtr = save_winptr;
3069#endif
3070 }
3071 TIME_MSG("sourcing vimrc file(s)");
3072}
3073
3074/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003075 * Setup to start using the GUI. Exit with an error when not available.
3076 */
3077 static void
3078main_start_gui()
3079{
3080#ifdef FEAT_GUI
3081 gui.starting = TRUE; /* start GUI a bit later */
3082#else
3083 mch_errmsg(_(e_nogvim));
3084 mch_errmsg("\n");
3085 mch_exit(2);
3086#endif
3087}
3088
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003089#endif /* NO_VIM_MAIN */
3090
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003091/*
Bram Moolenaar49325942007-05-10 19:19:59 +00003092 * Get an environment variable, and execute it as Ex commands.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003093 * Returns FAIL if the environment variable was not executed, OK otherwise.
3094 */
3095 int
3096process_env(env, is_viminit)
3097 char_u *env;
3098 int is_viminit; /* when TRUE, called for VIMINIT */
3099{
3100 char_u *initstr;
3101 char_u *save_sourcing_name;
3102 linenr_T save_sourcing_lnum;
3103#ifdef FEAT_EVAL
3104 scid_T save_sid;
3105#endif
3106
3107 if ((initstr = mch_getenv(env)) != NULL && *initstr != NUL)
3108 {
3109 if (is_viminit)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003110 vimrc_found(NULL, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003111 save_sourcing_name = sourcing_name;
3112 save_sourcing_lnum = sourcing_lnum;
3113 sourcing_name = env;
3114 sourcing_lnum = 0;
3115#ifdef FEAT_EVAL
3116 save_sid = current_SID;
3117 current_SID = SID_ENV;
3118#endif
3119 do_cmdline_cmd(initstr);
3120 sourcing_name = save_sourcing_name;
3121 sourcing_lnum = save_sourcing_lnum;
3122#ifdef FEAT_EVAL
3123 current_SID = save_sid;;
3124#endif
3125 return OK;
3126 }
3127 return FAIL;
3128}
3129
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003130#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003131/*
3132 * Return TRUE if we are certain the user owns the file "fname".
3133 * Used for ".vimrc" and ".exrc".
3134 * Use both stat() and lstat() for extra security.
3135 */
3136 static int
3137file_owned(fname)
3138 char *fname;
3139{
3140 struct stat s;
3141# ifdef UNIX
3142 uid_t uid = getuid();
3143# else /* VMS */
3144 uid_t uid = ((getgid() << 16) | getuid());
3145# endif
3146
3147 return !(mch_stat(fname, &s) != 0 || s.st_uid != uid
3148# ifdef HAVE_LSTAT
3149 || mch_lstat(fname, &s) != 0 || s.st_uid != uid
3150# endif
3151 );
3152}
3153#endif
3154
3155/*
3156 * Give an error message main_errors["n"] and exit.
3157 */
3158 static void
3159mainerr(n, str)
3160 int n; /* one of the ME_ defines */
3161 char_u *str; /* extra argument or NULL */
3162{
3163#if defined(UNIX) || defined(__EMX__) || defined(VMS)
3164 reset_signals(); /* kill us with CTRL-C here, if you like */
3165#endif
3166
3167 mch_errmsg(longVersion);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003168 mch_errmsg("\n");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003169 mch_errmsg(_(main_errors[n]));
3170 if (str != NULL)
3171 {
3172 mch_errmsg(": \"");
3173 mch_errmsg((char *)str);
3174 mch_errmsg("\"");
3175 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003176 mch_errmsg(_("\nMore info with: \"vim -h\"\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003177
3178 mch_exit(1);
3179}
3180
3181 void
3182mainerr_arg_missing(str)
3183 char_u *str;
3184{
3185 mainerr(ME_ARG_MISSING, str);
3186}
3187
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003188#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003189/*
3190 * print a message with three spaces prepended and '\n' appended.
3191 */
3192 static void
3193main_msg(s)
3194 char *s;
3195{
3196 mch_msg(" ");
3197 mch_msg(s);
3198 mch_msg("\n");
3199}
3200
3201/*
3202 * Print messages for "vim -h" or "vim --help" and exit.
3203 */
3204 static void
3205usage()
3206{
3207 int i;
3208 static char *(use[]) =
3209 {
3210 N_("[file ..] edit specified file(s)"),
3211 N_("- read text from stdin"),
3212 N_("-t tag edit file where tag is defined"),
3213#ifdef FEAT_QUICKFIX
3214 N_("-q [errorfile] edit file with first error")
3215#endif
3216 };
3217
3218#if defined(UNIX) || defined(__EMX__) || defined(VMS)
3219 reset_signals(); /* kill us with CTRL-C here, if you like */
3220#endif
3221
3222 mch_msg(longVersion);
3223 mch_msg(_("\n\nusage:"));
3224 for (i = 0; ; ++i)
3225 {
3226 mch_msg(_(" vim [arguments] "));
3227 mch_msg(_(use[i]));
3228 if (i == (sizeof(use) / sizeof(char_u *)) - 1)
3229 break;
3230 mch_msg(_("\n or:"));
3231 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003232#ifdef VMS
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003233 mch_msg(_("\nWhere case is ignored prepend / to make flag upper case"));
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003234#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003235
3236 mch_msg(_("\n\nArguments:\n"));
3237 main_msg(_("--\t\t\tOnly file names after this"));
3238#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
3239 main_msg(_("--literal\t\tDon't expand wildcards"));
3240#endif
3241#ifdef FEAT_OLE
3242 main_msg(_("-register\t\tRegister this gvim for OLE"));
3243 main_msg(_("-unregister\t\tUnregister gvim for OLE"));
3244#endif
3245#ifdef FEAT_GUI
3246 main_msg(_("-g\t\t\tRun using GUI (like \"gvim\")"));
3247 main_msg(_("-f or --nofork\tForeground: Don't fork when starting GUI"));
3248#endif
3249 main_msg(_("-v\t\t\tVi mode (like \"vi\")"));
3250 main_msg(_("-e\t\t\tEx mode (like \"ex\")"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003251 main_msg(_("-E\t\t\tImproved Ex mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003252 main_msg(_("-s\t\t\tSilent (batch) mode (only for \"ex\")"));
3253#ifdef FEAT_DIFF
3254 main_msg(_("-d\t\t\tDiff mode (like \"vimdiff\")"));
3255#endif
3256 main_msg(_("-y\t\t\tEasy mode (like \"evim\", modeless)"));
3257 main_msg(_("-R\t\t\tReadonly mode (like \"view\")"));
3258 main_msg(_("-Z\t\t\tRestricted mode (like \"rvim\")"));
3259 main_msg(_("-m\t\t\tModifications (writing files) not allowed"));
3260 main_msg(_("-M\t\t\tModifications in text not allowed"));
3261 main_msg(_("-b\t\t\tBinary mode"));
3262#ifdef FEAT_LISP
3263 main_msg(_("-l\t\t\tLisp mode"));
3264#endif
3265 main_msg(_("-C\t\t\tCompatible with Vi: 'compatible'"));
3266 main_msg(_("-N\t\t\tNot fully Vi compatible: 'nocompatible'"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003267 main_msg(_("-V[N][fname]\t\tBe verbose [level N] [log messages to fname]"));
3268#ifdef FEAT_EVAL
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003269 main_msg(_("-D\t\t\tDebugging mode"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003270#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003271 main_msg(_("-n\t\t\tNo swap file, use memory only"));
3272 main_msg(_("-r\t\t\tList swap files and exit"));
3273 main_msg(_("-r (with file name)\tRecover crashed session"));
3274 main_msg(_("-L\t\t\tSame as -r"));
3275#ifdef AMIGA
3276 main_msg(_("-f\t\t\tDon't use newcli to open window"));
3277 main_msg(_("-dev <device>\t\tUse <device> for I/O"));
3278#endif
3279#ifdef FEAT_ARABIC
3280 main_msg(_("-A\t\t\tstart in Arabic mode"));
3281#endif
3282#ifdef FEAT_RIGHTLEFT
3283 main_msg(_("-H\t\t\tStart in Hebrew mode"));
3284#endif
3285#ifdef FEAT_FKMAP
3286 main_msg(_("-F\t\t\tStart in Farsi mode"));
3287#endif
3288 main_msg(_("-T <terminal>\tSet terminal type to <terminal>"));
3289 main_msg(_("-u <vimrc>\t\tUse <vimrc> instead of any .vimrc"));
3290#ifdef FEAT_GUI
3291 main_msg(_("-U <gvimrc>\t\tUse <gvimrc> instead of any .gvimrc"));
3292#endif
3293 main_msg(_("--noplugin\t\tDon't load plugin scripts"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003294#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003295 main_msg(_("-p[N]\t\tOpen N tab pages (default: one for each file)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003296 main_msg(_("-o[N]\t\tOpen N windows (default: one for each file)"));
3297 main_msg(_("-O[N]\t\tLike -o but split vertically"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003298#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003299 main_msg(_("+\t\t\tStart at end of file"));
3300 main_msg(_("+<lnum>\t\tStart at line <lnum>"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003301 main_msg(_("--cmd <command>\tExecute <command> before loading any vimrc file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003302 main_msg(_("-c <command>\t\tExecute <command> after loading the first file"));
3303 main_msg(_("-S <session>\t\tSource file <session> after loading the first file"));
3304 main_msg(_("-s <scriptin>\tRead Normal mode commands from file <scriptin>"));
3305 main_msg(_("-w <scriptout>\tAppend all typed commands to file <scriptout>"));
3306 main_msg(_("-W <scriptout>\tWrite all typed commands to file <scriptout>"));
3307#ifdef FEAT_CRYPT
3308 main_msg(_("-x\t\t\tEdit encrypted files"));
3309#endif
3310#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
3311# if defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)
3312 main_msg(_("-display <display>\tConnect vim to this particular X-server"));
3313# endif
3314 main_msg(_("-X\t\t\tDo not connect to X server"));
3315#endif
3316#ifdef FEAT_CLIENTSERVER
3317 main_msg(_("--remote <files>\tEdit <files> in a Vim server if possible"));
3318 main_msg(_("--remote-silent <files> Same, don't complain if there is no server"));
3319 main_msg(_("--remote-wait <files> As --remote but wait for files to have been edited"));
3320 main_msg(_("--remote-wait-silent <files> Same, don't complain if there is no server"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003321# ifdef FEAT_WINDOWS
Bram Moolenaar82ad3242008-01-11 19:26:36 +00003322 main_msg(_("--remote-tab[-wait][-silent] <files> As --remote but use tab page per file"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003323# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003324 main_msg(_("--remote-send <keys>\tSend <keys> to a Vim server and exit"));
3325 main_msg(_("--remote-expr <expr>\tEvaluate <expr> in a Vim server and print result"));
3326 main_msg(_("--serverlist\t\tList available Vim server names and exit"));
3327 main_msg(_("--servername <name>\tSend to/become the Vim server <name>"));
3328#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003329#ifdef STARTUPTIME
Bram Moolenaar34ef52d2009-11-17 11:31:25 +00003330 main_msg(_("--startuptime <file>\tWrite startup timing messages to <file>"));
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003331#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003332#ifdef FEAT_VIMINFO
3333 main_msg(_("-i <viminfo>\t\tUse <viminfo> instead of .viminfo"));
3334#endif
3335 main_msg(_("-h or --help\tPrint Help (this message) and exit"));
3336 main_msg(_("--version\t\tPrint version information and exit"));
3337
3338#ifdef FEAT_GUI_X11
3339# ifdef FEAT_GUI_MOTIF
3340 mch_msg(_("\nArguments recognised by gvim (Motif version):\n"));
3341# else
3342# ifdef FEAT_GUI_ATHENA
3343# ifdef FEAT_GUI_NEXTAW
3344 mch_msg(_("\nArguments recognised by gvim (neXtaw version):\n"));
3345# else
3346 mch_msg(_("\nArguments recognised by gvim (Athena version):\n"));
3347# endif
3348# endif
3349# endif
3350 main_msg(_("-display <display>\tRun vim on <display>"));
3351 main_msg(_("-iconic\t\tStart vim iconified"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003352 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
3353 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
3354 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3355 main_msg(_("-boldfont <font>\tUse <font> for bold text"));
3356 main_msg(_("-italicfont <font>\tUse <font> for italic text"));
3357 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3358 main_msg(_("-borderwidth <width>\tUse a border width of <width> (also: -bw)"));
3359 main_msg(_("-scrollbarwidth <width> Use a scrollbar width of <width> (also: -sw)"));
3360# ifdef FEAT_GUI_ATHENA
3361 main_msg(_("-menuheight <height>\tUse a menu bar height of <height> (also: -mh)"));
3362# endif
3363 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3364 main_msg(_("+reverse\t\tDon't use reverse video (also: +rv)"));
3365 main_msg(_("-xrm <resource>\tSet the specified resource"));
3366#endif /* FEAT_GUI_X11 */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003367#ifdef FEAT_GUI_GTK
3368 mch_msg(_("\nArguments recognised by gvim (GTK+ version):\n"));
3369 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3370 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3371 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3372 main_msg(_("-display <display>\tRun vim on <display> (also: --display)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003373 main_msg(_("--role <role>\tSet a unique role to identify the main window"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003374 main_msg(_("--socketid <xid>\tOpen Vim inside another GTK widget"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003375 main_msg(_("--echo-wid\t\tMake gvim echo the Window ID on stdout"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003376#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003377#ifdef FEAT_GUI_W32
3378 main_msg(_("-P <parent title>\tOpen Vim inside parent application"));
Bram Moolenaar78e17622007-08-30 10:26:19 +00003379 main_msg(_("--windowid <HWND>\tOpen Vim inside another win32 widget"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003380#endif
3381
3382#ifdef FEAT_GUI_GNOME
3383 /* Gnome gives extra messages for --help if we continue, but not for -h. */
3384 if (gui.starting)
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003385 {
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003386 mch_msg("\n");
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003387 gui.dofork = FALSE;
3388 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003389 else
3390#endif
3391 mch_exit(0);
3392}
3393
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00003394#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003395/*
3396 * Check the result of the ATTENTION dialog:
3397 * When "Quit" selected, exit Vim.
3398 * When "Recover" selected, recover the file.
3399 */
3400 static void
3401check_swap_exists_action()
3402{
3403 if (swap_exists_action == SEA_QUIT)
3404 getout(1);
3405 handle_swap_exists(NULL);
3406}
3407#endif
3408
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003409#endif
3410
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003411#if defined(STARTUPTIME) || defined(PROTO)
3412static void time_diff __ARGS((struct timeval *then, struct timeval *now));
3413
3414static struct timeval prev_timeval;
3415
Bram Moolenaar3f269672009-11-03 11:11:11 +00003416# ifdef WIN3264
3417/*
3418 * Windows doesn't have gettimeofday(), although it does have struct timeval.
3419 */
3420 static int
3421gettimeofday(struct timeval *tv, char *dummy)
3422{
3423 long t = clock();
3424 tv->tv_sec = t / CLOCKS_PER_SEC;
3425 tv->tv_usec = (t - tv->tv_sec * CLOCKS_PER_SEC) * 1000000 / CLOCKS_PER_SEC;
3426 return 0;
3427}
3428# endif
3429
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003430/*
3431 * Save the previous time before doing something that could nest.
3432 * set "*tv_rel" to the time elapsed so far.
3433 */
3434 void
3435time_push(tv_rel, tv_start)
3436 void *tv_rel, *tv_start;
3437{
3438 *((struct timeval *)tv_rel) = prev_timeval;
3439 gettimeofday(&prev_timeval, NULL);
3440 ((struct timeval *)tv_rel)->tv_usec = prev_timeval.tv_usec
3441 - ((struct timeval *)tv_rel)->tv_usec;
3442 ((struct timeval *)tv_rel)->tv_sec = prev_timeval.tv_sec
3443 - ((struct timeval *)tv_rel)->tv_sec;
3444 if (((struct timeval *)tv_rel)->tv_usec < 0)
3445 {
3446 ((struct timeval *)tv_rel)->tv_usec += 1000000;
3447 --((struct timeval *)tv_rel)->tv_sec;
3448 }
3449 *(struct timeval *)tv_start = prev_timeval;
3450}
3451
3452/*
3453 * Compute the previous time after doing something that could nest.
3454 * Subtract "*tp" from prev_timeval;
3455 * Note: The arguments are (void *) to avoid trouble with systems that don't
3456 * have struct timeval.
3457 */
3458 void
3459time_pop(tp)
3460 void *tp; /* actually (struct timeval *) */
3461{
3462 prev_timeval.tv_usec -= ((struct timeval *)tp)->tv_usec;
3463 prev_timeval.tv_sec -= ((struct timeval *)tp)->tv_sec;
3464 if (prev_timeval.tv_usec < 0)
3465 {
3466 prev_timeval.tv_usec += 1000000;
3467 --prev_timeval.tv_sec;
3468 }
3469}
3470
3471 static void
3472time_diff(then, now)
3473 struct timeval *then;
3474 struct timeval *now;
3475{
3476 long usec;
3477 long msec;
3478
3479 usec = now->tv_usec - then->tv_usec;
3480 msec = (now->tv_sec - then->tv_sec) * 1000L + usec / 1000L,
3481 usec = usec % 1000L;
3482 fprintf(time_fd, "%03ld.%03ld", msec, usec >= 0 ? usec : usec + 1000L);
3483}
3484
3485 void
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003486time_msg(mesg, tv_start)
3487 char *mesg;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003488 void *tv_start; /* only for do_source: start time; actually
3489 (struct timeval *) */
3490{
3491 static struct timeval start;
3492 struct timeval now;
3493
3494 if (time_fd != NULL)
3495 {
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003496 if (strstr(mesg, "STARTING") != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003497 {
3498 gettimeofday(&start, NULL);
3499 prev_timeval = start;
3500 fprintf(time_fd, "\n\ntimes in msec\n");
3501 fprintf(time_fd, " clock self+sourced self: sourced script\n");
3502 fprintf(time_fd, " clock elapsed: other lines\n\n");
3503 }
3504 gettimeofday(&now, NULL);
3505 time_diff(&start, &now);
3506 if (((struct timeval *)tv_start) != NULL)
3507 {
3508 fprintf(time_fd, " ");
3509 time_diff(((struct timeval *)tv_start), &now);
3510 }
3511 fprintf(time_fd, " ");
3512 time_diff(&prev_timeval, &now);
3513 prev_timeval = now;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003514 fprintf(time_fd, ": %s\n", mesg);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003515 }
3516}
3517
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003518#endif
3519
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003520#if (defined(FEAT_CLIENTSERVER) && !defined(NO_VIM_MAIN)) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003521
3522/*
3523 * Common code for the X command server and the Win32 command server.
3524 */
3525
Bram Moolenaareb94e552006-03-11 21:35:11 +00003526static char_u *build_drop_cmd __ARGS((int filec, char **filev, int tabs, int sendReply));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003527
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003528/*
3529 * Do the client-server stuff, unless "--servername ''" was used.
3530 */
3531 static void
3532exec_on_server(parmp)
3533 mparm_T *parmp;
3534{
3535 if (parmp->serverName_arg == NULL || *parmp->serverName_arg != NUL)
3536 {
3537# ifdef WIN32
3538 /* Initialise the client/server messaging infrastructure. */
3539 serverInitMessaging();
3540# endif
3541
3542 /*
3543 * When a command server argument was found, execute it. This may
3544 * exit Vim when it was successful. Otherwise it's executed further
3545 * on. Remember the encoding used here in "serverStrEnc".
3546 */
3547 if (parmp->serverArg)
3548 {
3549 cmdsrv_main(&parmp->argc, parmp->argv,
3550 parmp->serverName_arg, &parmp->serverStr);
3551# ifdef FEAT_MBYTE
3552 parmp->serverStrEnc = vim_strsave(p_enc);
3553# endif
3554 }
3555
3556 /* If we're still running, get the name to register ourselves.
3557 * On Win32 can register right now, for X11 need to setup the
3558 * clipboard first, it's further down. */
3559 parmp->servername = serverMakeName(parmp->serverName_arg,
3560 parmp->argv[0]);
3561# ifdef WIN32
3562 if (parmp->servername != NULL)
3563 {
3564 serverSetName(parmp->servername);
3565 vim_free(parmp->servername);
3566 }
3567# endif
3568 }
3569}
3570
3571/*
3572 * Prepare for running as a Vim server.
3573 */
3574 static void
3575prepare_server(parmp)
3576 mparm_T *parmp;
3577{
3578# if defined(FEAT_X11)
3579 /*
3580 * Register for remote command execution with :serversend and --remote
3581 * unless there was a -X or a --servername '' on the command line.
3582 * Only register nongui-vim's with an explicit --servername argument.
Bram Moolenaar5f402312006-08-15 19:40:35 +00003583 * When running as root --servername is also required.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003584 */
3585 if (X_DISPLAY != NULL && parmp->servername != NULL && (
3586# ifdef FEAT_GUI
Bram Moolenaar5f402312006-08-15 19:40:35 +00003587 (gui.in_use
3588# ifdef UNIX
Bram Moolenaar311d9822007-02-27 15:48:28 +00003589 && getuid() != ROOT_UID
Bram Moolenaar5f402312006-08-15 19:40:35 +00003590# endif
3591 ) ||
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003592# endif
3593 parmp->serverName_arg != NULL))
3594 {
3595 (void)serverRegisterName(X_DISPLAY, parmp->servername);
3596 vim_free(parmp->servername);
3597 TIME_MSG("register server name");
3598 }
3599 else
3600 serverDelayedStartName = parmp->servername;
3601# endif
3602
3603 /*
3604 * Execute command ourselves if we're here because the send failed (or
3605 * else we would have exited above).
3606 */
3607 if (parmp->serverStr != NULL)
3608 {
3609 char_u *p;
3610
3611 server_to_input_buf(serverConvert(parmp->serverStrEnc,
3612 parmp->serverStr, &p));
3613 vim_free(p);
3614 }
3615}
3616
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003617 static void
3618cmdsrv_main(argc, argv, serverName_arg, serverStr)
3619 int *argc;
3620 char **argv;
3621 char_u *serverName_arg;
3622 char_u **serverStr;
3623{
3624 char_u *res;
3625 int i;
3626 char_u *sname;
3627 int ret;
3628 int didone = FALSE;
3629 int exiterr = 0;
3630 char **newArgV = argv + 1;
3631 int newArgC = 1,
3632 Argc = *argc;
3633 int argtype;
3634#define ARGTYPE_OTHER 0
3635#define ARGTYPE_EDIT 1
3636#define ARGTYPE_EDIT_WAIT 2
3637#define ARGTYPE_SEND 3
3638 int silent = FALSE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003639 int tabs = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003640# ifndef FEAT_X11
3641 HWND srv;
3642# else
3643 Window srv;
3644
3645 setup_term_clip();
3646# endif
3647
3648 sname = serverMakeName(serverName_arg, argv[0]);
3649 if (sname == NULL)
3650 return;
3651
3652 /*
3653 * Execute the command server related arguments and remove them
3654 * from the argc/argv array; We may have to return into main()
3655 */
3656 for (i = 1; i < Argc; i++)
3657 {
3658 res = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003659 if (STRCMP(argv[i], "--") == 0) /* end of option arguments */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003660 {
3661 for (; i < *argc; i++)
3662 {
3663 *newArgV++ = argv[i];
3664 newArgC++;
3665 }
3666 break;
3667 }
3668
Bram Moolenaareb94e552006-03-11 21:35:11 +00003669 if (STRICMP(argv[i], "--remote-send") == 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003670 argtype = ARGTYPE_SEND;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003671 else if (STRNICMP(argv[i], "--remote", 8) == 0)
3672 {
3673 char *p = argv[i] + 8;
3674
3675 argtype = ARGTYPE_EDIT;
3676 while (*p != NUL)
3677 {
3678 if (STRNICMP(p, "-wait", 5) == 0)
3679 {
3680 argtype = ARGTYPE_EDIT_WAIT;
3681 p += 5;
3682 }
3683 else if (STRNICMP(p, "-silent", 7) == 0)
3684 {
3685 silent = TRUE;
3686 p += 7;
3687 }
3688 else if (STRNICMP(p, "-tab", 4) == 0)
3689 {
3690 tabs = TRUE;
3691 p += 4;
3692 }
3693 else
3694 {
3695 argtype = ARGTYPE_OTHER;
3696 break;
3697 }
3698 }
3699 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003700 else
3701 argtype = ARGTYPE_OTHER;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003702
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003703 if (argtype != ARGTYPE_OTHER)
3704 {
3705 if (i == *argc - 1)
3706 mainerr_arg_missing((char_u *)argv[i]);
3707 if (argtype == ARGTYPE_SEND)
3708 {
3709 *serverStr = (char_u *)argv[i + 1];
3710 i++;
3711 }
3712 else
3713 {
3714 *serverStr = build_drop_cmd(*argc - i - 1, argv + i + 1,
Bram Moolenaareb94e552006-03-11 21:35:11 +00003715 tabs, argtype == ARGTYPE_EDIT_WAIT);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003716 if (*serverStr == NULL)
3717 {
3718 /* Probably out of memory, exit. */
3719 didone = TRUE;
3720 exiterr = 1;
3721 break;
3722 }
3723 Argc = i;
3724 }
3725# ifdef FEAT_X11
3726 if (xterm_dpy == NULL)
3727 {
3728 mch_errmsg(_("No display"));
3729 ret = -1;
3730 }
3731 else
3732 ret = serverSendToVim(xterm_dpy, sname, *serverStr,
3733 NULL, &srv, 0, 0, silent);
3734# else
3735 /* Win32 always works? */
3736 ret = serverSendToVim(sname, *serverStr, NULL, &srv, 0, silent);
3737# endif
3738 if (ret < 0)
3739 {
3740 if (argtype == ARGTYPE_SEND)
3741 {
3742 /* Failed to send, abort. */
3743 mch_errmsg(_(": Send failed.\n"));
3744 didone = TRUE;
3745 exiterr = 1;
3746 }
3747 else if (!silent)
3748 /* Let vim start normally. */
3749 mch_errmsg(_(": Send failed. Trying to execute locally\n"));
3750 break;
3751 }
3752
3753# ifdef FEAT_GUI_W32
3754 /* Guess that when the server name starts with "g" it's a GUI
3755 * server, which we can bring to the foreground here.
3756 * Foreground() in the server doesn't work very well. */
3757 if (argtype != ARGTYPE_SEND && TOUPPER_ASC(*sname) == 'G')
3758 SetForegroundWindow(srv);
3759# endif
3760
3761 /*
3762 * For --remote-wait: Wait until the server did edit each
3763 * file. Also detect that the server no longer runs.
3764 */
3765 if (ret >= 0 && argtype == ARGTYPE_EDIT_WAIT)
3766 {
3767 int numFiles = *argc - i - 1;
3768 int j;
3769 char_u *done = alloc(numFiles);
3770 char_u *p;
3771# ifdef FEAT_GUI_W32
3772 NOTIFYICONDATA ni;
3773 int count = 0;
3774 extern HWND message_window;
3775# endif
3776
3777 if (numFiles > 0 && argv[i + 1][0] == '+')
3778 /* Skip "+cmd" argument, don't wait for it to be edited. */
3779 --numFiles;
3780
3781# ifdef FEAT_GUI_W32
3782 ni.cbSize = sizeof(ni);
3783 ni.hWnd = message_window;
3784 ni.uID = 0;
3785 ni.uFlags = NIF_ICON|NIF_TIP;
3786 ni.hIcon = LoadIcon((HINSTANCE)GetModuleHandle(0), "IDR_VIM");
3787 sprintf(ni.szTip, _("%d of %d edited"), count, numFiles);
3788 Shell_NotifyIcon(NIM_ADD, &ni);
3789# endif
3790
3791 /* Wait for all files to unload in remote */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02003792 vim_memset(done, 0, numFiles);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003793 while (memchr(done, 0, numFiles) != NULL)
3794 {
3795# ifdef WIN32
3796 p = serverGetReply(srv, NULL, TRUE, TRUE);
3797 if (p == NULL)
3798 break;
3799# else
3800 if (serverReadReply(xterm_dpy, srv, &p, TRUE) < 0)
3801 break;
3802# endif
3803 j = atoi((char *)p);
3804 if (j >= 0 && j < numFiles)
3805 {
3806# ifdef FEAT_GUI_W32
3807 ++count;
3808 sprintf(ni.szTip, _("%d of %d edited"),
3809 count, numFiles);
3810 Shell_NotifyIcon(NIM_MODIFY, &ni);
3811# endif
3812 done[j] = 1;
3813 }
3814 }
3815# ifdef FEAT_GUI_W32
3816 Shell_NotifyIcon(NIM_DELETE, &ni);
3817# endif
3818 }
3819 }
3820 else if (STRICMP(argv[i], "--remote-expr") == 0)
3821 {
3822 if (i == *argc - 1)
3823 mainerr_arg_missing((char_u *)argv[i]);
3824# ifdef WIN32
3825 /* Win32 always works? */
3826 if (serverSendToVim(sname, (char_u *)argv[i + 1],
3827 &res, NULL, 1, FALSE) < 0)
3828# else
3829 if (xterm_dpy == NULL)
3830 mch_errmsg(_("No display: Send expression failed.\n"));
3831 else if (serverSendToVim(xterm_dpy, sname, (char_u *)argv[i + 1],
3832 &res, NULL, 1, 1, FALSE) < 0)
3833# endif
3834 {
3835 if (res != NULL && *res != NUL)
3836 {
3837 /* Output error from remote */
3838 mch_errmsg((char *)res);
3839 vim_free(res);
3840 res = NULL;
3841 }
3842 mch_errmsg(_(": Send expression failed.\n"));
3843 }
3844 }
3845 else if (STRICMP(argv[i], "--serverlist") == 0)
3846 {
3847# ifdef WIN32
3848 /* Win32 always works? */
3849 res = serverGetVimNames();
3850# else
3851 if (xterm_dpy != NULL)
3852 res = serverGetVimNames(xterm_dpy);
3853# endif
3854 if (called_emsg)
3855 mch_errmsg("\n");
3856 }
3857 else if (STRICMP(argv[i], "--servername") == 0)
3858 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +00003859 /* Already processed. Take it out of the command line */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003860 i++;
3861 continue;
3862 }
3863 else
3864 {
3865 *newArgV++ = argv[i];
3866 newArgC++;
3867 continue;
3868 }
3869 didone = TRUE;
3870 if (res != NULL && *res != NUL)
3871 {
3872 mch_msg((char *)res);
3873 if (res[STRLEN(res) - 1] != '\n')
3874 mch_msg("\n");
3875 }
3876 vim_free(res);
3877 }
3878
3879 if (didone)
3880 {
3881 display_errors(); /* display any collected messages */
3882 exit(exiterr); /* Mission accomplished - get out */
3883 }
3884
3885 /* Return back into main() */
3886 *argc = newArgC;
3887 vim_free(sname);
3888}
3889
3890/*
3891 * Build a ":drop" command to send to a Vim server.
3892 */
3893 static char_u *
Bram Moolenaareb94e552006-03-11 21:35:11 +00003894build_drop_cmd(filec, filev, tabs, sendReply)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003895 int filec;
3896 char **filev;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003897 int tabs; /* Use ":tab drop" instead of ":drop". */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003898 int sendReply;
3899{
3900 garray_T ga;
3901 int i;
3902 char_u *inicmd = NULL;
3903 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003904 char_u *cwd;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003905
3906 if (filec > 0 && filev[0][0] == '+')
3907 {
3908 inicmd = (char_u *)filev[0] + 1;
3909 filev++;
3910 filec--;
3911 }
3912 /* Check if we have at least one argument. */
3913 if (filec <= 0)
3914 mainerr_arg_missing((char_u *)filev[-1]);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003915
3916 /* Temporarily cd to the current directory to handle relative file names. */
Bram Moolenaard9462e32011-04-11 21:35:11 +02003917 cwd = alloc(MAXPATHL);
3918 if (cwd == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003919 return NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003920 if (mch_dirname(cwd, MAXPATHL) != OK)
3921 {
3922 vim_free(cwd);
3923 return NULL;
3924 }
3925 p = vim_strsave_escaped_ext(cwd,
Bram Moolenaar02b06312007-09-06 15:39:22 +00003926#ifdef BACKSLASH_IN_FILENAME
3927 "", /* rem_backslash() will tell what chars to escape */
3928#else
3929 PATH_ESC_CHARS,
3930#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +02003931 '\\', TRUE);
3932 vim_free(cwd);
3933 if (p == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003934 return NULL;
3935 ga_init2(&ga, 1, 100);
3936 ga_concat(&ga, (char_u *)"<C-\\><C-N>:cd ");
3937 ga_concat(&ga, p);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003938 vim_free(p);
Bram Moolenaareb94e552006-03-11 21:35:11 +00003939
3940 /* Call inputsave() so that a prompt for an encryption key works. */
3941 ga_concat(&ga, (char_u *)"<CR>:if exists('*inputsave')|call inputsave()|endif|");
3942 if (tabs)
3943 ga_concat(&ga, (char_u *)"tab ");
3944 ga_concat(&ga, (char_u *)"drop");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003945 for (i = 0; i < filec; i++)
3946 {
3947 /* On Unix the shell has already expanded the wildcards, don't want to
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003948 * do it again in the Vim server. On MS-Windows only escape
3949 * non-wildcard characters. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003950 p = vim_strsave_escaped((char_u *)filev[i],
3951#ifdef UNIX
3952 PATH_ESC_CHARS
3953#else
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003954 (char_u *)" \t%#"
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003955#endif
3956 );
3957 if (p == NULL)
3958 {
3959 vim_free(ga.ga_data);
3960 return NULL;
3961 }
3962 ga_concat(&ga, (char_u *)" ");
3963 ga_concat(&ga, p);
3964 vim_free(p);
3965 }
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003966 ga_concat(&ga, (char_u *)"|if exists('*inputrestore')|call inputrestore()|endif<CR>");
3967
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003968 /* The :drop commands goes to Insert mode when 'insertmode' is set, use
3969 * CTRL-\ CTRL-N again. */
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003970 ga_concat(&ga, (char_u *)"<C-\\><C-N>");
3971
3972 /* Switch back to the correct current directory (prior to temporary path
3973 * switch) unless 'autochdir' is set, in which case it will already be
3974 * correct after the :drop command. */
3975 ga_concat(&ga, (char_u *)":if !exists('+acd')||!&acd|cd -|endif<CR>");
3976
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003977 if (sendReply)
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003978 ga_concat(&ga, (char_u *)":call SetupRemoteReplies()<CR>");
3979 ga_concat(&ga, (char_u *)":");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003980 if (inicmd != NULL)
3981 {
3982 /* Can't use <CR> after "inicmd", because an "startinsert" would cause
3983 * the following commands to be inserted as text. Use a "|",
3984 * hopefully "inicmd" does allow this... */
3985 ga_concat(&ga, inicmd);
3986 ga_concat(&ga, (char_u *)"|");
3987 }
3988 /* Bring the window to the foreground, goto Insert mode when 'im' set and
3989 * clear command line. */
Bram Moolenaar567e4de2004-12-31 21:01:02 +00003990 ga_concat(&ga, (char_u *)"cal foreground()|if &im|star|en|redr|f<CR>");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003991 ga_append(&ga, NUL);
3992 return ga.ga_data;
3993}
3994
3995/*
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003996 * Make our basic server name: use the specified "arg" if given, otherwise use
3997 * the tail of the command "cmd" we were started with.
3998 * Return the name in allocated memory. This doesn't include a serial number.
3999 */
4000 static char_u *
4001serverMakeName(arg, cmd)
4002 char_u *arg;
4003 char *cmd;
4004{
4005 char_u *p;
4006
4007 if (arg != NULL && *arg != NUL)
4008 p = vim_strsave_up(arg);
4009 else
4010 {
4011 p = vim_strsave_up(gettail((char_u *)cmd));
4012 /* Remove .exe or .bat from the name. */
4013 if (p != NULL && vim_strchr(p, '.') != NULL)
4014 *vim_strchr(p, '.') = NUL;
4015 }
4016 return p;
4017}
4018#endif /* FEAT_CLIENTSERVER */
4019
4020#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
4021/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004022 * Replace termcodes such as <CR> and insert as key presses if there is room.
4023 */
4024 void
4025server_to_input_buf(str)
4026 char_u *str;
4027{
4028 char_u *ptr = NULL;
4029 char_u *cpo_save = p_cpo;
4030
4031 /* Set 'cpoptions' the way we want it.
4032 * B set - backslashes are *not* treated specially
4033 * k set - keycodes are *not* reverse-engineered
4034 * < unset - <Key> sequences *are* interpreted
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004035 * The last but one parameter of replace_termcodes() is TRUE so that the
4036 * <lt> sequence is recognised - needed for a real backslash.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004037 */
4038 p_cpo = (char_u *)"Bk";
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004039 str = replace_termcodes((char_u *)str, &ptr, FALSE, TRUE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004040 p_cpo = cpo_save;
4041
4042 if (*ptr != NUL) /* trailing CTRL-V results in nothing */
4043 {
4044 /*
4045 * Add the string to the input stream.
4046 * Can't use add_to_input_buf() here, we now have K_SPECIAL bytes.
4047 *
4048 * First clear typed characters from the typeahead buffer, there could
4049 * be half a mapping there. Then append to the existing string, so
4050 * that multiple commands from a client are concatenated.
4051 */
4052 if (typebuf.tb_maplen < typebuf.tb_len)
4053 del_typebuf(typebuf.tb_len - typebuf.tb_maplen, typebuf.tb_maplen);
4054 (void)ins_typebuf(str, REMAP_NONE, typebuf.tb_len, TRUE, FALSE);
4055
4056 /* Let input_available() know we inserted text in the typeahead
4057 * buffer. */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004058 typebuf_was_filled = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004059 }
4060 vim_free((char_u *)ptr);
4061}
4062
4063/*
4064 * Evaluate an expression that the client sent to a string.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004065 */
4066 char_u *
4067eval_client_expr_to_string(expr)
4068 char_u *expr;
4069{
4070 char_u *res;
4071 int save_dbl = debug_break_level;
4072 int save_ro = redir_off;
4073
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004074 /* Disable debugging, otherwise Vim hangs, waiting for "cont" to be
4075 * typed. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004076 debug_break_level = -1;
4077 redir_off = 0;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004078 /* Do not display error message, otherwise Vim hangs, waiting for "cont"
4079 * to be typed. Do generate errors so that try/catch works. */
4080 ++emsg_silent;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004081
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004082 res = eval_to_string(expr, NULL, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004083
4084 debug_break_level = save_dbl;
4085 redir_off = save_ro;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004086 --emsg_silent;
4087 if (emsg_silent < 0)
4088 emsg_silent = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004089
Bram Moolenaar63a121b2005-12-11 21:36:39 +00004090 /* A client can tell us to redraw, but not to display the cursor, so do
4091 * that here. */
4092 setcursor();
4093 out_flush();
4094#ifdef FEAT_GUI
4095 if (gui.in_use)
4096 gui_update_cursor(FALSE, FALSE);
4097#endif
4098
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004099 return res;
4100}
4101
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004102/*
4103 * If conversion is needed, convert "data" from "client_enc" to 'encoding' and
4104 * return an allocated string. Otherwise return "data".
4105 * "*tofree" is set to the result when it needs to be freed later.
4106 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004107 char_u *
4108serverConvert(client_enc, data, tofree)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004109 char_u *client_enc UNUSED;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004110 char_u *data;
4111 char_u **tofree;
4112{
4113 char_u *res = data;
4114
4115 *tofree = NULL;
4116# ifdef FEAT_MBYTE
4117 if (client_enc != NULL && p_enc != NULL)
4118 {
4119 vimconv_T vimconv;
4120
4121 vimconv.vc_type = CONV_NONE;
4122 if (convert_setup(&vimconv, client_enc, p_enc) != FAIL
4123 && vimconv.vc_type != CONV_NONE)
4124 {
4125 res = string_convert(&vimconv, data, NULL);
4126 if (res == NULL)
4127 res = data;
4128 else
4129 *tofree = res;
4130 }
4131 convert_setup(&vimconv, NULL, NULL);
4132 }
4133# endif
4134 return res;
4135}
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004136#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004137
4138/*
4139 * When FEAT_FKMAP is defined, also compile the Farsi source code.
4140 */
4141#if defined(FEAT_FKMAP) || defined(PROTO)
4142# include "farsi.c"
4143#endif
4144
4145/*
4146 * When FEAT_ARABIC is defined, also compile the Arabic source code.
4147 */
4148#if defined(FEAT_ARABIC) || defined(PROTO)
4149# include "arabic.c"
4150#endif