blob: 79521ef468ca7c89de9429192418b8c2e5276bdd [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;
1060# ifdef FEAT_VISUAL
1061 VIsual_active = FALSE;
1062# endif
1063 got_int = TRUE;
1064 need_wait_return = FALSE;
1065 global_busy = FALSE;
1066 exmode_active = 0;
1067 skip_redraw = FALSE;
1068 RedrawingDisabled = 0;
1069 no_wait_return = 0;
Bram Moolenaar7701c242011-10-04 16:43:53 +02001070 vgetc_busy = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001071# ifdef FEAT_EVAL
1072 emsg_skip = 0;
1073# endif
1074 emsg_off = 0;
1075# ifdef FEAT_MOUSE
1076 setmouse();
1077# endif
1078 settmode(TMODE_RAW);
1079 starttermcap();
1080 scroll_start();
1081 redraw_later_clear();
1082 }
1083#endif
1084
1085 clear_oparg(&oa);
1086 while (!cmdwin
1087#ifdef FEAT_CMDWIN
1088 || cmdwin_result == 0
1089#endif
1090 )
1091 {
1092 if (stuff_empty())
1093 {
1094 did_check_timestamps = FALSE;
1095 if (need_check_timestamps)
1096 check_timestamps(FALSE);
1097 if (need_wait_return) /* if wait_return still needed ... */
1098 wait_return(FALSE); /* ... call it now */
1099 if (need_start_insertmode && goto_im()
1100#ifdef FEAT_VISUAL
1101 && !VIsual_active
1102#endif
1103 )
1104 {
1105 need_start_insertmode = FALSE;
1106 stuffReadbuff((char_u *)"i"); /* start insert mode next */
1107 /* skip the fileinfo message now, because it would be shown
1108 * after insert mode finishes! */
1109 need_fileinfo = FALSE;
1110 }
1111 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001112
1113 /* Reset "got_int" now that we got back to the main loop. Except when
1114 * inside a ":g/pat/cmd" command, then the "got_int" needs to abort
1115 * the ":g" command.
1116 * For ":g/pat/vi" we reset "got_int" when used once. When used
1117 * a second time we go back to Ex mode and abort the ":g" command. */
1118 if (got_int)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001119 {
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001120 if (noexmode && global_busy && !exmode_active && previous_got_int)
1121 {
1122 /* Typed two CTRL-C in a row: go back to ex mode as if "Q" was
1123 * used and keep "got_int" set, so that it aborts ":g". */
1124 exmode_active = EXMODE_NORMAL;
1125 State = NORMAL;
1126 }
1127 else if (!global_busy || !exmode_active)
1128 {
1129 if (!quit_more)
1130 (void)vgetc(); /* flush all buffers */
1131 got_int = FALSE;
1132 }
1133 previous_got_int = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001134 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001135 else
1136 previous_got_int = FALSE;
1137
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001138 if (!exmode_active)
1139 msg_scroll = FALSE;
1140 quit_more = FALSE;
1141
1142 /*
1143 * If skip redraw is set (for ":" in wait_return()), don't redraw now.
1144 * If there is nothing in the stuff_buffer or do_redraw is TRUE,
1145 * update cursor and redraw.
1146 */
1147 if (skip_redraw || exmode_active)
1148 skip_redraw = FALSE;
1149 else if (do_redraw || stuff_empty())
1150 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001151#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001152 /* Trigger CursorMoved if the cursor moved. */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001153 if (!finish_op && (
1154# ifdef FEAT_AUTOCMD
1155 has_cursormoved()
1156# endif
1157# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
1158 ||
1159# endif
1160# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001161 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001162# endif
1163 )
1164 && !equalpos(last_cursormoved, curwin->w_cursor))
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001165 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001166# ifdef FEAT_AUTOCMD
1167 if (has_cursormoved())
1168 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL,
1169 FALSE, curbuf);
1170# endif
1171# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001172 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001173 {
1174 conceal_old_cursor_line = last_cursormoved.lnum;
1175 conceal_new_cursor_line = curwin->w_cursor.lnum;
1176 conceal_update_lines = TRUE;
1177 }
1178# endif
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001179 last_cursormoved = curwin->w_cursor;
1180 }
1181#endif
1182
Bram Moolenaar186628f2013-03-19 13:33:23 +01001183#ifdef FEAT_AUTOCMD
1184 /* Trigger TextChanged if b_changedtick differs. */
1185 if (!finish_op && has_textchanged()
1186 && last_changedtick != curbuf->b_changedtick)
1187 {
1188 if (last_changedtick_buf == curbuf)
1189 apply_autocmds(EVENT_TEXTCHANGED, NULL, NULL,
1190 FALSE, curbuf);
1191 last_changedtick_buf = curbuf;
1192 last_changedtick = curbuf->b_changedtick;
1193 }
1194#endif
1195
Bram Moolenaar33aec762006-01-22 23:30:12 +00001196#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
1197 /* Scroll-binding for diff mode may have been postponed until
1198 * here. Avoids doing it for every change. */
1199 if (diff_need_scrollbind)
1200 {
1201 check_scrollbind((linenr_T)0, 0L);
1202 diff_need_scrollbind = FALSE;
1203 }
1204#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001205#if defined(FEAT_FOLDING) && defined(FEAT_VISUAL)
1206 /* Include a closed fold completely in the Visual area. */
1207 foldAdjustVisual();
1208#endif
1209#ifdef FEAT_FOLDING
1210 /*
1211 * When 'foldclose' is set, apply 'foldlevel' to folds that don't
1212 * contain the cursor.
1213 * When 'foldopen' is "all", open the fold(s) under the cursor.
1214 * This may mark the window for redrawing.
1215 */
1216 if (hasAnyFolding(curwin) && !char_avail())
1217 {
1218 foldCheckClose();
1219 if (fdo_flags & FDO_ALL)
1220 foldOpenCursor();
1221 }
1222#endif
1223
1224 /*
1225 * Before redrawing, make sure w_topline is correct, and w_leftcol
1226 * if lines don't wrap, and w_skipcol if lines wrap.
1227 */
1228 update_topline();
1229 validate_cursor();
1230
1231#ifdef FEAT_VISUAL
1232 if (VIsual_active)
1233 update_curbuf(INVERTED);/* update inverted part */
1234 else
1235#endif
1236 if (must_redraw)
1237 update_screen(0);
1238 else if (redraw_cmdline || clear_cmdline)
1239 showmode();
1240#ifdef FEAT_WINDOWS
1241 redraw_statuslines();
1242#endif
1243#ifdef FEAT_TITLE
1244 if (need_maketitle)
1245 maketitle();
1246#endif
1247 /* display message after redraw */
1248 if (keep_msg != NULL)
1249 {
1250 char_u *p;
1251
1252 /* msg_attr_keep() will set keep_msg to NULL, must free the
1253 * string here. */
1254 p = keep_msg;
Bram Moolenaar238a5642006-02-21 22:12:05 +00001255 keep_msg = NULL;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001256 msg_attr(p, keep_msg_attr);
1257 vim_free(p);
1258 }
1259 if (need_fileinfo) /* show file info after redraw */
1260 {
1261 fileinfo(FALSE, TRUE, FALSE);
1262 need_fileinfo = FALSE;
1263 }
1264
1265 emsg_on_display = FALSE; /* can delete error message now */
1266 did_emsg = FALSE;
1267 msg_didany = FALSE; /* reset lines_left in msg_start() */
Bram Moolenaar661b1822005-07-28 22:36:45 +00001268 may_clear_sb_text(); /* clear scroll-back text on next msg */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001269 showruler(FALSE);
1270
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001271# if defined(FEAT_CONCEAL)
1272 if (conceal_update_lines
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001273 && (conceal_old_cursor_line != conceal_new_cursor_line
1274 || conceal_cursor_line(curwin)
1275 || need_cursor_line_redraw))
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001276 {
Bram Moolenaarc2b4c622011-02-15 16:29:59 +01001277 if (conceal_old_cursor_line != conceal_new_cursor_line
1278 && conceal_old_cursor_line
1279 <= curbuf->b_ml.ml_line_count)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001280 update_single_line(curwin, conceal_old_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001281 update_single_line(curwin, conceal_new_cursor_line);
1282 curwin->w_valid &= ~VALID_CROW;
1283 }
1284# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001285 setcursor();
1286 cursor_on();
1287
1288 do_redraw = FALSE;
Bram Moolenaar3f269672009-11-03 11:11:11 +00001289
1290#ifdef STARTUPTIME
1291 /* Now that we have drawn the first screen all the startup stuff
1292 * has been done, close any file for startup messages. */
1293 if (time_fd != NULL)
1294 {
1295 TIME_MSG("first screen update");
1296 TIME_MSG("--- VIM STARTED ---");
1297 fclose(time_fd);
1298 time_fd = NULL;
1299 }
1300#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001301 }
1302#ifdef FEAT_GUI
1303 if (need_mouse_correct)
1304 gui_mouse_correct();
1305#endif
1306
1307 /*
1308 * Update w_curswant if w_set_curswant has been set.
1309 * Postponed until here to avoid computing w_virtcol too often.
1310 */
1311 update_curswant();
1312
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001313#ifdef FEAT_EVAL
1314 /*
1315 * May perform garbage collection when waiting for a character, but
1316 * only at the very toplevel. Otherwise we may be using a List or
1317 * Dict internally somewhere.
1318 * "may_garbage_collect" is reset in vgetc() which is invoked through
1319 * do_exmode() and normal_cmd().
1320 */
1321 may_garbage_collect = (!cmdwin && !noexmode);
1322#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001323 /*
1324 * If we're invoked as ex, do a round of ex commands.
1325 * Otherwise, get and execute a normal mode command.
1326 */
1327 if (exmode_active)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001328 {
1329 if (noexmode) /* End of ":global/path/visual" commands */
1330 return;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001331 do_exmode(exmode_active == EXMODE_VIM);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001332 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001333 else
1334 normal_cmd(&oa, TRUE);
1335 }
1336}
1337
1338
1339#if defined(USE_XSMP) || defined(FEAT_GUI_MSWIN) || defined(PROTO)
1340/*
1341 * Exit, but leave behind swap files for modified buffers.
1342 */
1343 void
1344getout_preserve_modified(exitval)
1345 int exitval;
1346{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001347# if defined(SIGHUP) && defined(SIG_IGN)
1348 /* Ignore SIGHUP, because a dropped connection causes a read error, which
1349 * makes Vim exit and then handling SIGHUP causes various reentrance
1350 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001351 signal(SIGHUP, SIG_IGN);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001352# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001353
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001354 ml_close_notmod(); /* close all not-modified buffers */
1355 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
1356 ml_close_all(FALSE); /* close all memfiles, without deleting */
1357 getout(exitval); /* exit Vim properly */
1358}
1359#endif
1360
1361
1362/* Exit properly */
1363 void
1364getout(exitval)
1365 int exitval;
1366{
1367#ifdef FEAT_AUTOCMD
1368 buf_T *buf;
1369 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001370 tabpage_T *tp, *next_tp;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001371#endif
1372
1373 exiting = TRUE;
1374
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001375 /* When running in Ex mode an error causes us to exit with a non-zero exit
1376 * code. POSIX requires this, although it's not 100% clear from the
1377 * standard. */
1378 if (exmode_active)
1379 exitval += ex_exitval;
1380
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001381 /* Position the cursor on the last screen line, below all the text */
1382#ifdef FEAT_GUI
1383 if (!gui.in_use)
1384#endif
1385 windgoto((int)Rows - 1, 0);
1386
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +00001387#if defined(FEAT_EVAL) || defined(FEAT_SYN_HL)
1388 /* Optionally print hashtable efficiency. */
1389 hash_debug_results();
1390#endif
1391
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001392#ifdef FEAT_GUI
1393 msg_didany = FALSE;
1394#endif
1395
1396#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001397 if (get_vim_var_nr(VV_DYING) <= 1)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001398 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001399 /* Trigger BufWinLeave for all windows, but only once per buffer. */
1400# if defined FEAT_WINDOWS
1401 for (tp = first_tabpage; tp != NULL; tp = next_tp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001402 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001403 next_tp = tp->tp_next;
1404 for (wp = (tp == curtab)
1405 ? firstwin : tp->tp_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001406 {
Bram Moolenaar802418d2013-01-17 14:00:11 +01001407 if (wp->w_buffer == NULL)
1408 /* Autocmd must have close the buffer already, skip. */
1409 continue;
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001410 buf = wp->w_buffer;
1411 if (buf->b_changedtick != -1)
1412 {
1413 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname,
1414 buf->b_fname, FALSE, buf);
Bram Moolenaarb429cde2012-04-25 18:24:29 +02001415 buf->b_changedtick = -1; /* note that we did it already */
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001416 /* start all over, autocommands may mess up the lists */
1417 next_tp = first_tabpage;
1418 break;
1419 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001420 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001421 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001422# else
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001423 apply_autocmds(EVENT_BUFWINLEAVE, curbuf, curbuf->b_fname,
1424 FALSE, curbuf);
Bram Moolenaarf740b292006-02-16 22:11:02 +00001425# endif
Bram Moolenaar33aec762006-01-22 23:30:12 +00001426
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001427 /* Trigger BufUnload for buffers that are loaded */
1428 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1429 if (buf->b_ml.ml_mfp != NULL)
1430 {
1431 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001432 FALSE, buf);
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001433 if (!buf_valid(buf)) /* autocmd may delete the buffer */
1434 break;
1435 }
1436 apply_autocmds(EVENT_VIMLEAVEPRE, NULL, NULL, FALSE, curbuf);
1437 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001438#endif
1439
1440#ifdef FEAT_VIMINFO
1441 if (*p_viminfo != NUL)
1442 /* Write out the registers, history, marks etc, to the viminfo file */
1443 write_viminfo(NULL, FALSE);
1444#endif
1445
1446#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001447 if (get_vim_var_nr(VV_DYING) <= 1)
1448 apply_autocmds(EVENT_VIMLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001449#endif
1450
Bram Moolenaar05159a02005-02-26 23:04:13 +00001451#ifdef FEAT_PROFILE
1452 profile_dump();
1453#endif
1454
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001455 if (did_emsg
1456#ifdef FEAT_GUI
1457 || (gui.in_use && msg_didany && p_verbose > 0)
1458#endif
1459 )
1460 {
1461 /* give the user a chance to read the (error) message */
1462 no_wait_return = FALSE;
1463 wait_return(FALSE);
1464 }
1465
1466#ifdef FEAT_AUTOCMD
1467 /* Position the cursor again, the autocommands may have moved it */
1468# ifdef FEAT_GUI
1469 if (!gui.in_use)
1470# endif
1471 windgoto((int)Rows - 1, 0);
1472#endif
1473
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001474#ifdef FEAT_LUA
1475 lua_end();
1476#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001477#ifdef FEAT_MZSCHEME
1478 mzscheme_end();
1479#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001480#ifdef FEAT_TCL
1481 tcl_end();
1482#endif
1483#ifdef FEAT_RUBY
1484 ruby_end();
1485#endif
1486#ifdef FEAT_PYTHON
1487 python_end();
1488#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001489#ifdef FEAT_PYTHON3
1490 python3_end();
1491#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001492#ifdef FEAT_PERL
1493 perl_end();
1494#endif
1495#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
1496 iconv_end();
1497#endif
1498#ifdef FEAT_NETBEANS_INTG
1499 netbeans_end();
1500#endif
Bram Moolenaar02b06312007-09-06 15:39:22 +00001501#ifdef FEAT_CSCOPE
1502 cs_end();
1503#endif
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001504#ifdef FEAT_EVAL
1505 if (garbage_collect_at_exit)
1506 garbage_collect();
1507#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001508
1509 mch_exit(exitval);
1510}
1511
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01001512#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001513/*
1514 * Get a (optional) count for a Vim argument.
1515 */
1516 static int
1517get_number_arg(p, idx, def)
1518 char_u *p; /* pointer to argument */
1519 int *idx; /* index in argument, is incremented */
1520 int def; /* default value */
1521{
1522 if (vim_isdigit(p[*idx]))
1523 {
1524 def = atoi((char *)&(p[*idx]));
1525 while (vim_isdigit(p[*idx]))
1526 *idx = *idx + 1;
1527 }
1528 return def;
1529}
1530
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001531#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1532/*
1533 * Setup to use the current locale (for ctype() and many other things).
1534 */
1535 static void
1536init_locale()
1537{
1538 setlocale(LC_ALL, "");
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001539
Bram Moolenaarc6af8122010-05-21 12:04:55 +02001540# ifdef FEAT_GUI_GTK
1541 /* Tell Gtk not to change our locale settings. */
1542 gtk_disable_setlocale();
1543# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001544# if defined(FEAT_FLOAT) && defined(LC_NUMERIC)
1545 /* Make sure strtod() uses a decimal point, not a comma. */
1546 setlocale(LC_NUMERIC, "C");
1547# endif
1548
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001549# ifdef WIN32
1550 /* Apparently MS-Windows printf() may cause a crash when we give it 8-bit
1551 * text while it's expecting text in the current locale. This call avoids
1552 * that. */
1553 setlocale(LC_CTYPE, "C");
1554# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001555
1556# ifdef FEAT_GETTEXT
1557 {
1558 int mustfree = FALSE;
1559 char_u *p;
1560
1561# ifdef DYNAMIC_GETTEXT
1562 /* Initialize the gettext library */
1563 dyn_libintl_init(NULL);
1564# endif
1565 /* expand_env() doesn't work yet, because chartab[] is not initialized
1566 * yet, call vim_getenv() directly */
1567 p = vim_getenv((char_u *)"VIMRUNTIME", &mustfree);
1568 if (p != NULL && *p != NUL)
1569 {
Bram Moolenaar1ad2f132007-06-19 18:27:18 +00001570 vim_snprintf((char *)NameBuff, MAXPATHL, "%s/lang", p);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001571 bindtextdomain(VIMPACKAGE, (char *)NameBuff);
1572 }
1573 if (mustfree)
1574 vim_free(p);
1575 textdomain(VIMPACKAGE);
1576 }
1577# endif
1578}
1579#endif
1580
1581/*
1582 * Check for: [r][e][g][vi|vim|view][diff][ex[im]]
1583 * If the executable name starts with "r" we disable shell commands.
1584 * If the next character is "e" we run in Easy mode.
1585 * If the next character is "g" we run the GUI version.
1586 * If the next characters are "view" we start in readonly mode.
1587 * If the next characters are "diff" or "vimdiff" we start in diff mode.
1588 * If the next characters are "ex" we start in Ex mode. If it's followed
1589 * by "im" use improved Ex mode.
1590 */
1591 static void
1592parse_command_name(parmp)
1593 mparm_T *parmp;
1594{
1595 char_u *initstr;
1596
1597 initstr = gettail((char_u *)parmp->argv[0]);
1598
1599#ifdef MACOS_X_UNIX
1600 /* An issue has been seen when launching Vim in such a way that
1601 * $PWD/$ARGV[0] or $ARGV[0] is not the absolute path to the
1602 * executable or a symbolic link of it. Until this issue is resolved
1603 * we prohibit the GUI from being used.
1604 */
1605 if (STRCMP(initstr, parmp->argv[0]) == 0)
1606 disallow_gui = TRUE;
1607
1608 /* TODO: On MacOS X default to gui if argv[0] ends in:
Bram Moolenaar27dc1952006-03-15 23:06:44 +00001609 * /Vim.app/Contents/MacOS/Vim */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001610#endif
1611
1612#ifdef FEAT_EVAL
1613 set_vim_var_string(VV_PROGNAME, initstr, -1);
1614#endif
1615
1616 if (TOLOWER_ASC(initstr[0]) == 'r')
1617 {
1618 restricted = TRUE;
1619 ++initstr;
1620 }
1621
Bram Moolenaarad249fb2010-05-07 16:35:04 +02001622 /* Use evim mode for "evim" and "egvim", not for "editor". */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001623 if (TOLOWER_ASC(initstr[0]) == 'e'
1624 && (TOLOWER_ASC(initstr[1]) == 'v'
1625 || TOLOWER_ASC(initstr[1]) == 'g'))
1626 {
1627#ifdef FEAT_GUI
1628 gui.starting = TRUE;
1629#endif
1630 parmp->evim_mode = TRUE;
1631 ++initstr;
1632 }
1633
Bram Moolenaar806875d2008-09-18 18:57:10 +00001634 /* "gvim" starts the GUI. Also accept "Gvim" for MS-Windows. */
1635 if (TOLOWER_ASC(initstr[0]) == 'g')
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001636 {
1637 main_start_gui();
1638#ifdef FEAT_GUI
1639 ++initstr;
1640#endif
1641 }
1642
1643 if (STRNICMP(initstr, "view", 4) == 0)
1644 {
1645 readonlymode = TRUE;
1646 curbuf->b_p_ro = TRUE;
1647 p_uc = 10000; /* don't update very often */
1648 initstr += 4;
1649 }
1650 else if (STRNICMP(initstr, "vim", 3) == 0)
1651 initstr += 3;
1652
1653 /* Catch "[r][g]vimdiff" and "[r][g]viewdiff". */
1654 if (STRICMP(initstr, "diff") == 0)
1655 {
1656#ifdef FEAT_DIFF
1657 parmp->diff_mode = TRUE;
1658#else
1659 mch_errmsg(_("This Vim was not compiled with the diff feature."));
1660 mch_errmsg("\n");
1661 mch_exit(2);
1662#endif
1663 }
1664
1665 if (STRNICMP(initstr, "ex", 2) == 0)
1666 {
1667 if (STRNICMP(initstr + 2, "im", 2) == 0)
1668 exmode_active = EXMODE_VIM;
1669 else
1670 exmode_active = EXMODE_NORMAL;
1671 change_compatible(TRUE); /* set 'compatible' */
1672 }
1673}
1674
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001675/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00001676 * Get the name of the display, before gui_prepare() removes it from
1677 * argv[]. Used for the xterm-clipboard display.
1678 *
Bram Moolenaar78e17622007-08-30 10:26:19 +00001679 * Also find the --server... arguments and --socketid and --windowid
Bram Moolenaar58d98232005-07-23 22:25:46 +00001680 */
Bram Moolenaar58d98232005-07-23 22:25:46 +00001681 static void
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001682early_arg_scan(parmp)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00001683 mparm_T *parmp UNUSED;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001684{
Bram Moolenaar03005972008-11-20 13:12:36 +00001685#if defined(FEAT_XCLIPBOARD) || defined(FEAT_CLIENTSERVER) \
1686 || !defined(FEAT_NETBEANS_INTG)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001687 int argc = parmp->argc;
1688 char **argv = parmp->argv;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001689 int i;
1690
1691 for (i = 1; i < argc; i++)
1692 {
1693 if (STRCMP(argv[i], "--") == 0)
1694 break;
1695# ifdef FEAT_XCLIPBOARD
1696 else if (STRICMP(argv[i], "-display") == 0
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001697# if defined(FEAT_GUI_GTK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001698 || STRICMP(argv[i], "--display") == 0
1699# endif
1700 )
1701 {
1702 if (i == argc - 1)
1703 mainerr_arg_missing((char_u *)argv[i]);
1704 xterm_display = argv[++i];
1705 }
1706# endif
1707# ifdef FEAT_CLIENTSERVER
1708 else if (STRICMP(argv[i], "--servername") == 0)
1709 {
1710 if (i == argc - 1)
1711 mainerr_arg_missing((char_u *)argv[i]);
1712 parmp->serverName_arg = (char_u *)argv[++i];
1713 }
Bram Moolenaareb94e552006-03-11 21:35:11 +00001714 else if (STRICMP(argv[i], "--serverlist") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001715 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001716 else if (STRNICMP(argv[i], "--remote", 8) == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001717 {
1718 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001719# ifdef FEAT_GUI
1720 if (strstr(argv[i], "-wait") != 0)
1721 /* don't fork() when starting the GUI to edit files ourself */
1722 gui.dofork = FALSE;
1723# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001724 }
1725# endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001726
1727# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1728# ifdef FEAT_GUI_W32
1729 else if (STRICMP(argv[i], "--windowid") == 0)
1730# else
Bram Moolenaar58d98232005-07-23 22:25:46 +00001731 else if (STRICMP(argv[i], "--socketid") == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001732# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001733 {
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001734 long_u id;
1735 int count;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001736
1737 if (i == argc - 1)
1738 mainerr_arg_missing((char_u *)argv[i]);
1739 if (STRNICMP(argv[i+1], "0x", 2) == 0)
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001740 count = sscanf(&(argv[i + 1][2]), SCANF_HEX_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001741 else
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001742 count = sscanf(argv[i + 1], SCANF_DECIMAL_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001743 if (count != 1)
1744 mainerr(ME_INVALID_ARG, (char_u *)argv[i]);
1745 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00001746# ifdef FEAT_GUI_W32
1747 win_socket_id = id;
1748# else
1749 gtk_socket_id = id;
1750# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001751 i++;
1752 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001753# endif
1754# ifdef FEAT_GUI_GTK
Bram Moolenaar58d98232005-07-23 22:25:46 +00001755 else if (STRICMP(argv[i], "--echo-wid") == 0)
1756 echo_wid_arg = TRUE;
1757# endif
Bram Moolenaar03005972008-11-20 13:12:36 +00001758# ifndef FEAT_NETBEANS_INTG
1759 else if (strncmp(argv[i], "-nb", (size_t)3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +02001760 {
1761 mch_errmsg(_("'-nb' cannot be used: not enabled at compile time\n"));
1762 mch_exit(2);
1763 }
Bram Moolenaar03005972008-11-20 13:12:36 +00001764# endif
1765
Bram Moolenaar58d98232005-07-23 22:25:46 +00001766 }
1767#endif
1768}
1769
1770/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001771 * Scan the command line arguments.
1772 */
1773 static void
1774command_line_scan(parmp)
1775 mparm_T *parmp;
1776{
1777 int argc = parmp->argc;
1778 char **argv = parmp->argv;
1779 int argv_idx; /* index in argv[n][] */
1780 int had_minmin = FALSE; /* found "--" argument */
1781 int want_argument; /* option argument with argument */
1782 int c;
Bram Moolenaar231334e2005-07-25 20:46:57 +00001783 char_u *p = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001784 long n;
1785
1786 --argc;
1787 ++argv;
1788 argv_idx = 1; /* active option letter is argv[0][argv_idx] */
1789 while (argc > 0)
1790 {
1791 /*
1792 * "+" or "+{number}" or "+/{pat}" or "+{command}" argument.
1793 */
1794 if (argv[0][0] == '+' && !had_minmin)
1795 {
1796 if (parmp->n_commands >= MAX_ARG_CMDS)
1797 mainerr(ME_EXTRA_CMD, NULL);
1798 argv_idx = -1; /* skip to next argument */
1799 if (argv[0][1] == NUL)
1800 parmp->commands[parmp->n_commands++] = (char_u *)"$";
1801 else
1802 parmp->commands[parmp->n_commands++] = (char_u *)&(argv[0][1]);
1803 }
1804
1805 /*
1806 * Optional argument.
1807 */
1808 else if (argv[0][0] == '-' && !had_minmin)
1809 {
1810 want_argument = FALSE;
1811 c = argv[0][argv_idx++];
1812#ifdef VMS
1813 /*
1814 * VMS only uses upper case command lines. Interpret "-X" as "-x"
1815 * and "-/X" as "-X".
1816 */
1817 if (c == '/')
1818 {
1819 c = argv[0][argv_idx++];
1820 c = TOUPPER_ASC(c);
1821 }
1822 else
1823 c = TOLOWER_ASC(c);
1824#endif
1825 switch (c)
1826 {
1827 case NUL: /* "vim -" read from stdin */
1828 /* "ex -" silent mode */
1829 if (exmode_active)
1830 silent_mode = TRUE;
1831 else
1832 {
1833 if (parmp->edit_type != EDIT_NONE)
1834 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1835 parmp->edit_type = EDIT_STDIN;
1836 read_cmd_fd = 2; /* read from stderr instead of stdin */
1837 }
1838 argv_idx = -1; /* skip to next argument */
1839 break;
1840
1841 case '-': /* "--" don't take any more option arguments */
1842 /* "--help" give help message */
1843 /* "--version" give version message */
1844 /* "--literal" take files literally */
1845 /* "--nofork" don't fork */
1846 /* "--noplugin[s]" skip plugins */
1847 /* "--cmd <cmd>" execute cmd before vimrc */
1848 if (STRICMP(argv[0] + argv_idx, "help") == 0)
1849 usage();
1850 else if (STRICMP(argv[0] + argv_idx, "version") == 0)
1851 {
1852 Columns = 80; /* need to init Columns */
1853 info_message = TRUE; /* use mch_msg(), not mch_errmsg() */
1854 list_version();
1855 msg_putchar('\n');
1856 msg_didout = FALSE;
1857 mch_exit(0);
1858 }
1859 else if (STRNICMP(argv[0] + argv_idx, "literal", 7) == 0)
1860 {
1861#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
1862 parmp->literal = TRUE;
1863#endif
1864 }
1865 else if (STRNICMP(argv[0] + argv_idx, "nofork", 6) == 0)
1866 {
1867#ifdef FEAT_GUI
1868 gui.dofork = FALSE; /* don't fork() when starting GUI */
1869#endif
1870 }
1871 else if (STRNICMP(argv[0] + argv_idx, "noplugin", 8) == 0)
1872 p_lpl = FALSE;
1873 else if (STRNICMP(argv[0] + argv_idx, "cmd", 3) == 0)
1874 {
1875 want_argument = TRUE;
1876 argv_idx += 3;
1877 }
Bram Moolenaaref94eec2009-11-11 13:22:11 +00001878 else if (STRNICMP(argv[0] + argv_idx, "startuptime", 11) == 0)
1879 {
1880 want_argument = TRUE;
1881 argv_idx += 11;
1882 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001883#ifdef FEAT_CLIENTSERVER
1884 else if (STRNICMP(argv[0] + argv_idx, "serverlist", 10) == 0)
1885 ; /* already processed -- no arg */
1886 else if (STRNICMP(argv[0] + argv_idx, "servername", 10) == 0
1887 || STRNICMP(argv[0] + argv_idx, "serversend", 10) == 0)
1888 {
1889 /* already processed -- snatch the following arg */
1890 if (argc > 1)
1891 {
1892 --argc;
1893 ++argv;
1894 }
1895 }
1896#endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001897#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1898# ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001899 else if (STRNICMP(argv[0] + argv_idx, "socketid", 8) == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001900# else
1901 else if (STRNICMP(argv[0] + argv_idx, "windowid", 8) == 0)
1902# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001903 {
1904 /* already processed -- snatch the following arg */
1905 if (argc > 1)
1906 {
1907 --argc;
1908 ++argv;
1909 }
1910 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001911#endif
1912#ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001913 else if (STRNICMP(argv[0] + argv_idx, "echo-wid", 8) == 0)
1914 {
1915 /* already processed, skip */
1916 }
1917#endif
1918 else
1919 {
1920 if (argv[0][argv_idx])
1921 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
1922 had_minmin = TRUE;
1923 }
1924 if (!want_argument)
1925 argv_idx = -1; /* skip to next argument */
1926 break;
1927
1928 case 'A': /* "-A" start in Arabic mode */
1929#ifdef FEAT_ARABIC
1930 set_option_value((char_u *)"arabic", 1L, NULL, 0);
1931#else
1932 mch_errmsg(_(e_noarabic));
1933 mch_exit(2);
1934#endif
1935 break;
1936
1937 case 'b': /* "-b" binary mode */
Bram Moolenaar231334e2005-07-25 20:46:57 +00001938 /* Needs to be effective before expanding file names, because
1939 * for Win32 this makes us edit a shortcut file itself,
1940 * instead of the file it links to. */
1941 set_options_bin(curbuf->b_p_bin, 1, 0);
1942 curbuf->b_p_bin = 1; /* binary file I/O */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001943 break;
1944
1945 case 'C': /* "-C" Compatible */
1946 change_compatible(TRUE);
1947 break;
1948
1949 case 'e': /* "-e" Ex mode */
1950 exmode_active = EXMODE_NORMAL;
1951 break;
1952
1953 case 'E': /* "-E" Improved Ex mode */
1954 exmode_active = EXMODE_VIM;
1955 break;
1956
1957 case 'f': /* "-f" GUI: run in foreground. Amiga: open
1958 window directly, not with newcli */
1959#ifdef FEAT_GUI
1960 gui.dofork = FALSE; /* don't fork() when starting GUI */
1961#endif
1962 break;
1963
1964 case 'g': /* "-g" start GUI */
1965 main_start_gui();
1966 break;
1967
1968 case 'F': /* "-F" start in Farsi mode: rl + fkmap set */
1969#ifdef FEAT_FKMAP
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00001970 p_fkmap = TRUE;
1971 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001972#else
1973 mch_errmsg(_(e_nofarsi));
1974 mch_exit(2);
1975#endif
1976 break;
1977
1978 case 'h': /* "-h" give help message */
1979#ifdef FEAT_GUI_GNOME
1980 /* Tell usage() to exit for "gvim". */
1981 gui.starting = FALSE;
1982#endif
1983 usage();
1984 break;
1985
1986 case 'H': /* "-H" start in Hebrew mode: rl + hkmap set */
1987#ifdef FEAT_RIGHTLEFT
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00001988 p_hkmap = TRUE;
1989 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001990#else
1991 mch_errmsg(_(e_nohebrew));
1992 mch_exit(2);
1993#endif
1994 break;
1995
1996 case 'l': /* "-l" lisp mode, 'lisp' and 'showmatch' on */
1997#ifdef FEAT_LISP
1998 set_option_value((char_u *)"lisp", 1L, NULL, 0);
1999 p_sm = TRUE;
2000#endif
2001 break;
2002
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002003 case 'M': /* "-M" no changes or writing of files */
2004 reset_modifiable();
2005 /* FALLTHROUGH */
2006
2007 case 'm': /* "-m" no writing of files */
2008 p_write = FALSE;
2009 break;
2010
2011 case 'y': /* "-y" easy mode */
2012#ifdef FEAT_GUI
2013 gui.starting = TRUE; /* start GUI a bit later */
2014#endif
2015 parmp->evim_mode = TRUE;
2016 break;
2017
2018 case 'N': /* "-N" Nocompatible */
2019 change_compatible(FALSE);
2020 break;
2021
2022 case 'n': /* "-n" no swap file */
Bram Moolenaar67c53842010-05-22 18:28:27 +02002023#ifdef FEAT_NETBEANS_INTG
2024 /* checking for "-nb", netbeans parameters */
2025 if (argv[0][argv_idx] == 'b')
2026 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002027 netbeansArg = argv[0];
2028 argv_idx = -1; /* skip to next argument */
2029 }
2030 else
2031#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002032 parmp->no_swap_file = TRUE;
2033 break;
2034
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002035 case 'p': /* "-p[N]" open N tab pages */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002036#ifdef TARGET_API_MAC_OSX
2037 /* For some reason on MacOS X, an argument like:
2038 -psn_0_10223617 is passed in when invoke from Finder
2039 or with the 'open' command */
2040 if (argv[0][argv_idx] == 's')
2041 {
2042 argv_idx = -1; /* bypass full -psn */
2043 main_start_gui();
2044 break;
2045 }
2046#endif
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002047#ifdef FEAT_WINDOWS
2048 /* default is 0: open window for each file */
2049 parmp->window_count = get_number_arg((char_u *)argv[0],
2050 &argv_idx, 0);
2051 parmp->window_layout = WIN_TABS;
2052#endif
2053 break;
2054
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002055 case 'o': /* "-o[N]" open N horizontal split windows */
2056#ifdef 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_HOR;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002061#endif
2062 break;
2063
2064 case 'O': /* "-O[N]" open N vertical split windows */
2065#if defined(FEAT_VERTSPLIT) && defined(FEAT_WINDOWS)
2066 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002067 parmp->window_count = get_number_arg((char_u *)argv[0],
2068 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002069 parmp->window_layout = WIN_VER;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002070#endif
2071 break;
2072
2073#ifdef FEAT_QUICKFIX
2074 case 'q': /* "-q" QuickFix mode */
2075 if (parmp->edit_type != EDIT_NONE)
2076 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2077 parmp->edit_type = EDIT_QF;
2078 if (argv[0][argv_idx]) /* "-q{errorfile}" */
2079 {
2080 parmp->use_ef = (char_u *)argv[0] + argv_idx;
2081 argv_idx = -1;
2082 }
2083 else if (argc > 1) /* "-q {errorfile}" */
2084 want_argument = TRUE;
2085 break;
2086#endif
2087
2088 case 'R': /* "-R" readonly mode */
2089 readonlymode = TRUE;
2090 curbuf->b_p_ro = TRUE;
2091 p_uc = 10000; /* don't update very often */
2092 break;
2093
2094 case 'r': /* "-r" recovery mode */
2095 case 'L': /* "-L" recovery mode */
2096 recoverymode = 1;
2097 break;
2098
2099 case 's':
2100 if (exmode_active) /* "-s" silent (batch) mode */
2101 silent_mode = TRUE;
2102 else /* "-s {scriptin}" read from script file */
2103 want_argument = TRUE;
2104 break;
2105
2106 case 't': /* "-t {tag}" or "-t{tag}" jump to tag */
2107 if (parmp->edit_type != EDIT_NONE)
2108 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2109 parmp->edit_type = EDIT_TAG;
2110 if (argv[0][argv_idx]) /* "-t{tag}" */
2111 {
2112 parmp->tagname = (char_u *)argv[0] + argv_idx;
2113 argv_idx = -1;
2114 }
2115 else /* "-t {tag}" */
2116 want_argument = TRUE;
2117 break;
2118
2119#ifdef FEAT_EVAL
2120 case 'D': /* "-D" Debugging */
2121 parmp->use_debug_break_level = 9999;
2122 break;
2123#endif
2124#ifdef FEAT_DIFF
2125 case 'd': /* "-d" 'diff' */
2126# ifdef AMIGA
2127 /* check for "-dev {device}" */
2128 if (argv[0][argv_idx] == 'e' && argv[0][argv_idx + 1] == 'v')
2129 want_argument = TRUE;
2130 else
2131# endif
2132 parmp->diff_mode = TRUE;
2133 break;
2134#endif
2135 case 'V': /* "-V{N}" Verbose level */
2136 /* default is 10: a little bit verbose */
2137 p_verbose = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2138 if (argv[0][argv_idx] != NUL)
2139 {
2140 set_option_value((char_u *)"verbosefile", 0L,
2141 (char_u *)argv[0] + argv_idx, 0);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002142 argv_idx = (int)STRLEN(argv[0]);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002143 }
2144 break;
2145
2146 case 'v': /* "-v" Vi-mode (as if called "vi") */
2147 exmode_active = 0;
2148#ifdef FEAT_GUI
2149 gui.starting = FALSE; /* don't start GUI */
2150#endif
2151 break;
2152
2153 case 'w': /* "-w{number}" set window height */
2154 /* "-w {scriptout}" write to script */
2155 if (vim_isdigit(((char_u *)argv[0])[argv_idx]))
2156 {
2157 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2158 set_option_value((char_u *)"window", n, NULL, 0);
2159 break;
2160 }
2161 want_argument = TRUE;
2162 break;
2163
2164#ifdef FEAT_CRYPT
2165 case 'x': /* "-x" encrypted reading/writing of files */
2166 parmp->ask_for_key = TRUE;
2167 break;
2168#endif
2169
2170 case 'X': /* "-X" don't connect to X server */
2171#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
2172 x_no_connect = TRUE;
2173#endif
2174 break;
2175
2176 case 'Z': /* "-Z" restricted mode */
2177 restricted = TRUE;
2178 break;
2179
2180 case 'c': /* "-c{command}" or "-c {command}" execute
2181 command */
2182 if (argv[0][argv_idx] != NUL)
2183 {
2184 if (parmp->n_commands >= MAX_ARG_CMDS)
2185 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar231334e2005-07-25 20:46:57 +00002186 parmp->commands[parmp->n_commands++] = (char_u *)argv[0]
2187 + argv_idx;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002188 argv_idx = -1;
2189 break;
2190 }
2191 /*FALLTHROUGH*/
2192 case 'S': /* "-S {file}" execute Vim script */
2193 case 'i': /* "-i {viminfo}" use for viminfo */
2194#ifndef FEAT_DIFF
2195 case 'd': /* "-d {device}" device (for Amiga) */
2196#endif
2197 case 'T': /* "-T {terminal}" terminal name */
2198 case 'u': /* "-u {vimrc}" vim inits file */
2199 case 'U': /* "-U {gvimrc}" gvim inits file */
2200 case 'W': /* "-W {scriptout}" overwrite */
2201#ifdef FEAT_GUI_W32
2202 case 'P': /* "-P {parent title}" MDI parent */
2203#endif
2204 want_argument = TRUE;
2205 break;
2206
2207 default:
2208 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
2209 }
2210
2211 /*
2212 * Handle option arguments with argument.
2213 */
2214 if (want_argument)
2215 {
2216 /*
2217 * Check for garbage immediately after the option letter.
2218 */
2219 if (argv[0][argv_idx] != NUL)
2220 mainerr(ME_GARBAGE, (char_u *)argv[0]);
2221
2222 --argc;
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002223 if (argc < 1 && c != 'S') /* -S has an optional argument */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002224 mainerr_arg_missing((char_u *)argv[0]);
2225 ++argv;
2226 argv_idx = -1;
2227
2228 switch (c)
2229 {
2230 case 'c': /* "-c {command}" execute command */
2231 case 'S': /* "-S {file}" execute Vim script */
2232 if (parmp->n_commands >= MAX_ARG_CMDS)
2233 mainerr(ME_EXTRA_CMD, NULL);
2234 if (c == 'S')
2235 {
2236 char *a;
2237
2238 if (argc < 1)
2239 /* "-S" without argument: use default session file
2240 * name. */
2241 a = SESSION_FILE;
2242 else if (argv[0][0] == '-')
2243 {
2244 /* "-S" followed by another option: use default
2245 * session file name. */
2246 a = SESSION_FILE;
2247 ++argc;
2248 --argv;
2249 }
2250 else
2251 a = argv[0];
2252 p = alloc((unsigned)(STRLEN(a) + 4));
2253 if (p == NULL)
2254 mch_exit(2);
2255 sprintf((char *)p, "so %s", a);
2256 parmp->cmds_tofree[parmp->n_commands] = TRUE;
2257 parmp->commands[parmp->n_commands++] = p;
2258 }
2259 else
Bram Moolenaar231334e2005-07-25 20:46:57 +00002260 parmp->commands[parmp->n_commands++] =
2261 (char_u *)argv[0];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002262 break;
2263
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002264 case '-':
2265 if (argv[-1][2] == 'c')
2266 {
2267 /* "--cmd {command}" execute command */
2268 if (parmp->n_pre_commands >= MAX_ARG_CMDS)
2269 mainerr(ME_EXTRA_CMD, NULL);
2270 parmp->pre_commands[parmp->n_pre_commands++] =
Bram Moolenaar231334e2005-07-25 20:46:57 +00002271 (char_u *)argv[0];
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002272 }
2273 /* "--startuptime <file>" already handled */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002274 break;
2275
2276 /* case 'd': -d {device} is handled in mch_check_win() for the
2277 * Amiga */
2278
2279#ifdef FEAT_QUICKFIX
2280 case 'q': /* "-q {errorfile}" QuickFix mode */
2281 parmp->use_ef = (char_u *)argv[0];
2282 break;
2283#endif
2284
2285 case 'i': /* "-i {viminfo}" use for viminfo */
2286 use_viminfo = (char_u *)argv[0];
2287 break;
2288
2289 case 's': /* "-s {scriptin}" read from script file */
2290 if (scriptin[0] != NULL)
2291 {
2292scripterror:
2293 mch_errmsg(_("Attempt to open script file again: \""));
2294 mch_errmsg(argv[-1]);
2295 mch_errmsg(" ");
2296 mch_errmsg(argv[0]);
2297 mch_errmsg("\"\n");
2298 mch_exit(2);
2299 }
2300 if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL)
2301 {
2302 mch_errmsg(_("Cannot open for reading: \""));
2303 mch_errmsg(argv[0]);
2304 mch_errmsg("\"\n");
2305 mch_exit(2);
2306 }
2307 if (save_typebuf() == FAIL)
2308 mch_exit(2); /* out of memory */
2309 break;
2310
2311 case 't': /* "-t {tag}" */
2312 parmp->tagname = (char_u *)argv[0];
2313 break;
2314
2315 case 'T': /* "-T {terminal}" terminal name */
2316 /*
2317 * The -T term argument is always available and when
2318 * HAVE_TERMLIB is supported it overrides the environment
2319 * variable TERM.
2320 */
2321#ifdef FEAT_GUI
2322 if (term_is_gui((char_u *)argv[0]))
2323 gui.starting = TRUE; /* start GUI a bit later */
2324 else
2325#endif
2326 parmp->term = (char_u *)argv[0];
2327 break;
2328
2329 case 'u': /* "-u {vimrc}" vim inits file */
2330 parmp->use_vimrc = (char_u *)argv[0];
2331 break;
2332
2333 case 'U': /* "-U {gvimrc}" gvim inits file */
2334#ifdef FEAT_GUI
2335 use_gvimrc = (char_u *)argv[0];
2336#endif
2337 break;
2338
2339 case 'w': /* "-w {nr}" 'window' value */
2340 /* "-w {scriptout}" append to script file */
2341 if (vim_isdigit(*((char_u *)argv[0])))
2342 {
2343 argv_idx = 0;
2344 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2345 set_option_value((char_u *)"window", n, NULL, 0);
2346 argv_idx = -1;
2347 break;
2348 }
2349 /*FALLTHROUGH*/
2350 case 'W': /* "-W {scriptout}" overwrite script file */
2351 if (scriptout != NULL)
2352 goto scripterror;
2353 if ((scriptout = mch_fopen(argv[0],
2354 c == 'w' ? APPENDBIN : WRITEBIN)) == NULL)
2355 {
2356 mch_errmsg(_("Cannot open for script output: \""));
2357 mch_errmsg(argv[0]);
2358 mch_errmsg("\"\n");
2359 mch_exit(2);
2360 }
2361 break;
2362
2363#ifdef FEAT_GUI_W32
2364 case 'P': /* "-P {parent title}" MDI parent */
2365 gui_mch_set_parent(argv[0]);
2366 break;
2367#endif
2368 }
2369 }
2370 }
2371
2372 /*
2373 * File name argument.
2374 */
2375 else
2376 {
2377 argv_idx = -1; /* skip to next argument */
2378
2379 /* Check for only one type of editing. */
2380 if (parmp->edit_type != EDIT_NONE && parmp->edit_type != EDIT_FILE)
2381 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2382 parmp->edit_type = EDIT_FILE;
2383
2384#ifdef MSWIN
2385 /* Remember if the argument was a full path before changing
2386 * slashes to backslashes. */
2387 if (argv[0][0] != NUL && argv[0][1] == ':' && argv[0][2] == '\\')
2388 parmp->full_path = TRUE;
2389#endif
2390
2391 /* Add the file to the global argument list. */
2392 if (ga_grow(&global_alist.al_ga, 1) == FAIL
2393 || (p = vim_strsave((char_u *)argv[0])) == NULL)
2394 mch_exit(2);
2395#ifdef FEAT_DIFF
2396 if (parmp->diff_mode && mch_isdir(p) && GARGCOUNT > 0
2397 && !mch_isdir(alist_name(&GARGLIST[0])))
2398 {
2399 char_u *r;
2400
2401 r = concat_fnames(p, gettail(alist_name(&GARGLIST[0])), TRUE);
2402 if (r != NULL)
2403 {
2404 vim_free(p);
2405 p = r;
2406 }
2407 }
2408#endif
2409#if defined(__CYGWIN32__) && !defined(WIN32)
2410 /*
2411 * If vim is invoked by non-Cygwin tools, convert away any
2412 * DOS paths, so things like .swp files are created correctly.
2413 * Look for evidence of non-Cygwin paths before we bother.
2414 * This is only for when using the Unix files.
2415 */
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002416 if (vim_strpbrk(p, "\\:") != NULL && !path_with_url(p))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002417 {
2418 char posix_path[PATH_MAX];
2419
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002420# if CYGWIN_VERSION_DLL_MAJOR >= 1007
2421 cygwin_conv_path(CCP_WIN_A_TO_POSIX, p, posix_path, PATH_MAX);
2422# else
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002423 cygwin_conv_to_posix_path(p, posix_path);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002424# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002425 vim_free(p);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002426 p = vim_strsave((char_u *)posix_path);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002427 if (p == NULL)
2428 mch_exit(2);
2429 }
2430#endif
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002431
2432#ifdef USE_FNAME_CASE
2433 /* Make the case of the file name match the actual file. */
2434 fname_case(p, 0);
2435#endif
2436
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002437 alist_add(&global_alist, p,
2438#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
Bram Moolenaar231334e2005-07-25 20:46:57 +00002439 parmp->literal ? 2 : 0 /* add buffer nr after exp. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002440#else
2441 2 /* add buffer number now and use curbuf */
2442#endif
2443 );
2444
2445#if defined(FEAT_MBYTE) && defined(WIN32)
2446 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002447 /* Remember this argument has been added to the argument list.
2448 * Needed when 'encoding' is changed. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002449 used_file_arg(argv[0], parmp->literal, parmp->full_path,
Bram Moolenaar688e5f72008-07-24 11:51:40 +00002450# ifdef FEAT_DIFF
2451 parmp->diff_mode
2452# else
2453 FALSE
2454# endif
2455 );
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002456 }
2457#endif
2458 }
2459
2460 /*
2461 * If there are no more letters after the current "-", go to next
2462 * argument. argv_idx is set to -1 when the current argument is to be
2463 * skipped.
2464 */
2465 if (argv_idx <= 0 || argv[0][argv_idx] == NUL)
2466 {
2467 --argc;
2468 ++argv;
2469 argv_idx = 1;
2470 }
2471 }
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002472
2473#ifdef FEAT_EVAL
2474 /* If there is a "+123" or "-c" command, set v:swapcommand to the first
2475 * one. */
2476 if (parmp->n_commands > 0)
2477 {
2478 p = alloc((unsigned)STRLEN(parmp->commands[0]) + 3);
2479 if (p != NULL)
2480 {
2481 sprintf((char *)p, ":%s\r", parmp->commands[0]);
2482 set_vim_var_string(VV_SWAPCOMMAND, p, -1);
2483 vim_free(p);
2484 }
2485 }
2486#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002487}
2488
2489/*
2490 * Print a warning if stdout is not a terminal.
2491 * When starting in Ex mode and commands come from a file, set Silent mode.
2492 */
2493 static void
2494check_tty(parmp)
2495 mparm_T *parmp;
2496{
2497 int input_isatty; /* is active input a terminal? */
2498
2499 input_isatty = mch_input_isatty();
2500 if (exmode_active)
2501 {
2502 if (!input_isatty)
2503 silent_mode = TRUE;
2504 }
2505 else if (parmp->want_full_screen && (!parmp->stdout_isatty || !input_isatty)
2506#ifdef FEAT_GUI
2507 /* don't want the delay when started from the desktop */
2508 && !gui.starting
2509#endif
2510 )
2511 {
2512#ifdef NBDEBUG
2513 /*
2514 * This shouldn't be necessary. But if I run netbeans with the log
2515 * output coming to the console and XOpenDisplay fails, I get vim
2516 * trying to start with input/output to my console tty. This fills my
2517 * input buffer so fast I can't even kill the process in under 2
Bram Moolenaar49325942007-05-10 19:19:59 +00002518 * minutes (and it beeps continuously the whole time :-)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002519 */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002520 if (netbeans_active() && (!parmp->stdout_isatty || !input_isatty))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002521 {
2522 mch_errmsg(_("Vim: Error: Failure to start gvim from NetBeans\n"));
2523 exit(1);
2524 }
2525#endif
2526 if (!parmp->stdout_isatty)
2527 mch_errmsg(_("Vim: Warning: Output is not to a terminal\n"));
2528 if (!input_isatty)
2529 mch_errmsg(_("Vim: Warning: Input is not from a terminal\n"));
2530 out_flush();
2531 if (scriptin[0] == NULL)
2532 ui_delay(2000L, TRUE);
2533 TIME_MSG("Warning delay");
2534 }
2535}
2536
2537/*
2538 * Read text from stdin.
2539 */
2540 static void
2541read_stdin()
2542{
2543 int i;
2544
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002545#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002546 /* When getting the ATTENTION prompt here, use a dialog */
2547 swap_exists_action = SEA_DIALOG;
2548#endif
2549 no_wait_return = TRUE;
2550 i = msg_didany;
2551 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002552 (void)open_buffer(TRUE, NULL, 0); /* create memfile and read file */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002553 no_wait_return = FALSE;
2554 msg_didany = i;
2555 TIME_MSG("reading stdin");
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002556#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002557 check_swap_exists_action();
2558#endif
2559#if !(defined(AMIGA) || defined(MACOS))
2560 /*
2561 * Close stdin and dup it from stderr. Required for GPM to work
2562 * properly, and for running external commands.
2563 * Is there any other system that cannot do this?
2564 */
2565 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00002566 ignored = dup(2);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002567#endif
2568}
2569
2570/*
2571 * Create the requested number of windows and edit buffers in them.
2572 * Also does recovery if "recoverymode" set.
2573 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002574 static void
2575create_windows(parmp)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002576 mparm_T *parmp UNUSED;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002577{
2578#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002579 int dorewind;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002580 int done = 0;
2581
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002582 /*
2583 * Create the number of windows that was requested.
2584 */
2585 if (parmp->window_count == -1) /* was not set */
2586 parmp->window_count = 1;
2587 if (parmp->window_count == 0)
2588 parmp->window_count = GARGCOUNT;
2589 if (parmp->window_count > 1)
2590 {
2591 /* Don't change the windows if there was a command in .vimrc that
2592 * already split some windows */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002593 if (parmp->window_layout == 0)
2594 parmp->window_layout = WIN_HOR;
2595 if (parmp->window_layout == WIN_TABS)
2596 {
2597 parmp->window_count = make_tabpages(parmp->window_count);
2598 TIME_MSG("making tab pages");
2599 }
2600 else if (firstwin->w_next == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002601 {
2602 parmp->window_count = make_windows(parmp->window_count,
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002603 parmp->window_layout == WIN_VER);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002604 TIME_MSG("making windows");
2605 }
2606 else
2607 parmp->window_count = win_count();
2608 }
2609 else
2610 parmp->window_count = 1;
2611#endif
2612
2613 if (recoverymode) /* do recover */
2614 {
2615 msg_scroll = TRUE; /* scroll message up */
2616 ml_recover();
2617 if (curbuf->b_ml.ml_mfp == NULL) /* failed */
2618 getout(1);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002619 do_modelines(0); /* do modelines */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002620 }
2621 else
2622 {
2623 /*
2624 * Open a buffer for windows that don't have one yet.
2625 * Commands in the .vimrc might have loaded a file or split the window.
2626 * Watch out for autocommands that delete a window.
2627 */
2628#ifdef FEAT_AUTOCMD
2629 /*
2630 * Don't execute Win/Buf Enter/Leave autocommands here
2631 */
2632 ++autocmd_no_enter;
2633 ++autocmd_no_leave;
2634#endif
2635#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002636 dorewind = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002637 while (done++ < 1000)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002638 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002639 if (dorewind)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002640 {
2641 if (parmp->window_layout == WIN_TABS)
2642 goto_tabpage(1);
2643 else
2644 curwin = firstwin;
2645 }
2646 else if (parmp->window_layout == WIN_TABS)
2647 {
2648 if (curtab->tp_next == NULL)
2649 break;
2650 goto_tabpage(0);
2651 }
2652 else
2653 {
2654 if (curwin->w_next == NULL)
2655 break;
2656 curwin = curwin->w_next;
2657 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002658 dorewind = FALSE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002659#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002660 curbuf = curwin->w_buffer;
2661 if (curbuf->b_ml.ml_mfp == NULL)
2662 {
2663#ifdef FEAT_FOLDING
2664 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2665 if (p_fdls >= 0)
2666 curwin->w_p_fdl = p_fdls;
2667#endif
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002668#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002669 /* When getting the ATTENTION prompt here, use a dialog */
2670 swap_exists_action = SEA_DIALOG;
2671#endif
2672 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002673
2674 /* create memfile, read file */
2675 (void)open_buffer(FALSE, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002676
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002677#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar84212822006-11-07 21:59:47 +00002678 if (swap_exists_action == SEA_QUIT)
2679 {
2680 if (got_int || only_one_window())
2681 {
2682 /* abort selected or quit and only one window */
2683 did_emsg = FALSE; /* avoid hit-enter prompt */
2684 getout(1);
2685 }
2686 /* We can't close the window, it would disturb what
2687 * happens next. Clear the file name and set the arg
2688 * index to -1 to delete it later. */
2689 setfname(curbuf, NULL, NULL, FALSE);
2690 curwin->w_arg_idx = -1;
2691 swap_exists_action = SEA_NONE;
2692 }
2693 else
2694 handle_swap_exists(NULL);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002695#endif
2696#ifdef FEAT_AUTOCMD
Bram Moolenaar89d40322006-08-29 15:30:07 +00002697 dorewind = TRUE; /* start again */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002698#endif
2699 }
2700#ifdef FEAT_WINDOWS
2701 ui_breakcheck();
2702 if (got_int)
2703 {
2704 (void)vgetc(); /* only break the file loading, not the rest */
2705 break;
2706 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002707 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002708#endif
2709#ifdef FEAT_WINDOWS
2710 if (parmp->window_layout == WIN_TABS)
2711 goto_tabpage(1);
2712 else
2713 curwin = firstwin;
2714 curbuf = curwin->w_buffer;
2715#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002716#ifdef FEAT_AUTOCMD
2717 --autocmd_no_enter;
2718 --autocmd_no_leave;
2719#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002720 }
2721}
2722
2723#ifdef FEAT_WINDOWS
2724 /*
2725 * If opened more than one window, start editing files in the other
2726 * windows. make_windows() has already opened the windows.
2727 */
2728 static void
2729edit_buffers(parmp)
2730 mparm_T *parmp;
2731{
2732 int arg_idx; /* index in argument list */
2733 int i;
Bram Moolenaar84212822006-11-07 21:59:47 +00002734 int advance = TRUE;
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002735 win_T *win;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002736
2737# ifdef FEAT_AUTOCMD
2738 /*
2739 * Don't execute Win/Buf Enter/Leave autocommands here
2740 */
2741 ++autocmd_no_enter;
2742 ++autocmd_no_leave;
2743# endif
Bram Moolenaar84212822006-11-07 21:59:47 +00002744
2745 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2746 if (curwin->w_arg_idx == -1)
2747 {
2748 win_close(curwin, TRUE);
2749 advance = FALSE;
2750 }
2751
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002752 arg_idx = 1;
2753 for (i = 1; i < parmp->window_count; ++i)
2754 {
Bram Moolenaar84212822006-11-07 21:59:47 +00002755 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2756 if (curwin->w_arg_idx == -1)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002757 {
Bram Moolenaar84212822006-11-07 21:59:47 +00002758 ++arg_idx;
2759 win_close(curwin, TRUE);
2760 advance = FALSE;
2761 continue;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002762 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002763
Bram Moolenaar84212822006-11-07 21:59:47 +00002764 if (advance)
2765 {
2766 if (parmp->window_layout == WIN_TABS)
2767 {
2768 if (curtab->tp_next == NULL) /* just checking */
2769 break;
2770 goto_tabpage(0);
2771 }
2772 else
2773 {
2774 if (curwin->w_next == NULL) /* just checking */
2775 break;
2776 win_enter(curwin->w_next, FALSE);
2777 }
2778 }
2779 advance = TRUE;
2780
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002781 /* Only open the file if there is no file in this window yet (that can
Bram Moolenaar84212822006-11-07 21:59:47 +00002782 * happen when .vimrc contains ":sall"). */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002783 if (curbuf == firstwin->w_buffer || curbuf->b_ffname == NULL)
2784 {
2785 curwin->w_arg_idx = arg_idx;
Bram Moolenaar84212822006-11-07 21:59:47 +00002786 /* Edit file from arg list, if there is one. When "Quit" selected
2787 * at the ATTENTION prompt close the window. */
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002788# ifdef HAS_SWAP_EXISTS_ACTION
2789 swap_exists_did_quit = FALSE;
2790# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002791 (void)do_ecmd(0, arg_idx < GARGCOUNT
2792 ? alist_name(&GARGLIST[arg_idx]) : NULL,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002793 NULL, NULL, ECMD_LASTL, ECMD_HIDE, curwin);
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002794# ifdef HAS_SWAP_EXISTS_ACTION
2795 if (swap_exists_did_quit)
Bram Moolenaar84212822006-11-07 21:59:47 +00002796 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002797 /* abort or quit selected */
Bram Moolenaar84212822006-11-07 21:59:47 +00002798 if (got_int || only_one_window())
2799 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002800 /* abort selected and only one window */
Bram Moolenaar84212822006-11-07 21:59:47 +00002801 did_emsg = FALSE; /* avoid hit-enter prompt */
2802 getout(1);
2803 }
2804 win_close(curwin, TRUE);
2805 advance = FALSE;
2806 }
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002807# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002808 if (arg_idx == GARGCOUNT - 1)
2809 arg_had_last = TRUE;
2810 ++arg_idx;
2811 }
2812 ui_breakcheck();
2813 if (got_int)
2814 {
2815 (void)vgetc(); /* only break the file loading, not the rest */
2816 break;
2817 }
2818 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002819
2820 if (parmp->window_layout == WIN_TABS)
2821 goto_tabpage(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002822# ifdef FEAT_AUTOCMD
2823 --autocmd_no_enter;
2824# endif
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002825
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002826 /* make the first window the current window */
2827 win = firstwin;
2828#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
2829 /* Avoid making a preview window the current window. */
2830 while (win->w_p_pvw)
2831 {
2832 win = win->w_next;
2833 if (win == NULL)
2834 {
2835 win = firstwin;
2836 break;
2837 }
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002838 }
2839#endif
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002840 win_enter(win, FALSE);
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002841
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002842# ifdef FEAT_AUTOCMD
2843 --autocmd_no_leave;
2844# endif
2845 TIME_MSG("editing files in windows");
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002846 if (parmp->window_count > 1 && parmp->window_layout != WIN_TABS)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002847 win_equal(curwin, FALSE, 'b'); /* adjust heights */
2848}
2849#endif /* FEAT_WINDOWS */
2850
2851/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002852 * Execute the commands from --cmd arguments "cmds[cnt]".
2853 */
2854 static void
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002855exe_pre_commands(parmp)
2856 mparm_T *parmp;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002857{
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002858 char_u **cmds = parmp->pre_commands;
2859 int cnt = parmp->n_pre_commands;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002860 int i;
2861
2862 if (cnt > 0)
2863 {
2864 curwin->w_cursor.lnum = 0; /* just in case.. */
2865 sourcing_name = (char_u *)_("pre-vimrc command line");
2866# ifdef FEAT_EVAL
2867 current_SID = SID_CMDARG;
2868# endif
2869 for (i = 0; i < cnt; ++i)
2870 do_cmdline_cmd(cmds[i]);
2871 sourcing_name = NULL;
2872# ifdef FEAT_EVAL
2873 current_SID = 0;
2874# endif
2875 TIME_MSG("--cmd commands");
2876 }
2877}
2878
2879/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002880 * Execute "+", "-c" and "-S" arguments.
2881 */
2882 static void
2883exe_commands(parmp)
2884 mparm_T *parmp;
2885{
2886 int i;
2887
2888 /*
2889 * We start commands on line 0, make "vim +/pat file" match a
2890 * pattern on line 1. But don't move the cursor when an autocommand
2891 * with g`" was used.
2892 */
2893 msg_scroll = TRUE;
2894 if (parmp->tagname == NULL && curwin->w_cursor.lnum <= 1)
2895 curwin->w_cursor.lnum = 0;
2896 sourcing_name = (char_u *)"command line";
2897#ifdef FEAT_EVAL
2898 current_SID = SID_CARG;
2899#endif
2900 for (i = 0; i < parmp->n_commands; ++i)
2901 {
2902 do_cmdline_cmd(parmp->commands[i]);
2903 if (parmp->cmds_tofree[i])
2904 vim_free(parmp->commands[i]);
2905 }
2906 sourcing_name = NULL;
2907#ifdef FEAT_EVAL
2908 current_SID = 0;
2909#endif
2910 if (curwin->w_cursor.lnum == 0)
2911 curwin->w_cursor.lnum = 1;
2912
2913 if (!exmode_active)
2914 msg_scroll = FALSE;
2915
2916#ifdef FEAT_QUICKFIX
2917 /* When started with "-q errorfile" jump to first error again. */
2918 if (parmp->edit_type == EDIT_QF)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002919 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002920#endif
2921 TIME_MSG("executing command arguments");
2922}
2923
2924/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002925 * Source startup scripts.
2926 */
2927 static void
2928source_startup_scripts(parmp)
2929 mparm_T *parmp;
2930{
2931 int i;
2932
2933 /*
2934 * For "evim" source evim.vim first of all, so that the user can overrule
2935 * any things he doesn't like.
2936 */
2937 if (parmp->evim_mode)
2938 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002939 (void)do_source((char_u *)EVIM_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002940 TIME_MSG("source evim file");
2941 }
2942
2943 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002944 * If -u argument given, use only the initializations from that file and
Bram Moolenaar58d98232005-07-23 22:25:46 +00002945 * nothing else.
2946 */
2947 if (parmp->use_vimrc != NULL)
2948 {
Bram Moolenaar231334e2005-07-25 20:46:57 +00002949 if (STRCMP(parmp->use_vimrc, "NONE") == 0
2950 || STRCMP(parmp->use_vimrc, "NORC") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002951 {
2952#ifdef FEAT_GUI
2953 if (use_gvimrc == NULL) /* don't load gvimrc either */
2954 use_gvimrc = parmp->use_vimrc;
2955#endif
2956 if (parmp->use_vimrc[2] == 'N')
2957 p_lpl = FALSE; /* don't load plugins either */
2958 }
2959 else
2960 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002961 if (do_source(parmp->use_vimrc, FALSE, DOSO_NONE) != OK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002962 EMSG2(_("E282: Cannot read from \"%s\""), parmp->use_vimrc);
2963 }
2964 }
2965 else if (!silent_mode)
2966 {
2967#ifdef AMIGA
2968 struct Process *proc = (struct Process *)FindTask(0L);
2969 APTR save_winptr = proc->pr_WindowPtr;
2970
2971 /* Avoid a requester here for a volume that doesn't exist. */
2972 proc->pr_WindowPtr = (APTR)-1L;
2973#endif
2974
2975 /*
2976 * Get system wide defaults, if the file name is defined.
2977 */
2978#ifdef SYS_VIMRC_FILE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002979 (void)do_source((char_u *)SYS_VIMRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002980#endif
Bram Moolenaar1056d982006-03-09 22:37:52 +00002981#ifdef MACOS_X
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002982 (void)do_source((char_u *)"$VIMRUNTIME/macmap.vim", FALSE, DOSO_NONE);
Bram Moolenaar1056d982006-03-09 22:37:52 +00002983#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00002984
2985 /*
2986 * Try to read initialization commands from the following places:
2987 * - environment variable VIMINIT
2988 * - user vimrc file (s:.vimrc for Amiga, ~/.vimrc otherwise)
2989 * - second user vimrc file ($VIM/.vimrc for Dos)
2990 * - environment variable EXINIT
2991 * - user exrc file (s:.exrc for Amiga, ~/.exrc otherwise)
2992 * - second user exrc file ($VIM/.exrc for Dos)
2993 * The first that exists is used, the rest is ignored.
2994 */
2995 if (process_env((char_u *)"VIMINIT", TRUE) != OK)
2996 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002997 if (do_source((char_u *)USR_VIMRC_FILE, TRUE, DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00002998#ifdef USR_VIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002999 && do_source((char_u *)USR_VIMRC_FILE2, TRUE,
3000 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003001#endif
3002#ifdef USR_VIMRC_FILE3
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003003 && do_source((char_u *)USR_VIMRC_FILE3, TRUE,
3004 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003005#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +02003006#ifdef USR_VIMRC_FILE4
3007 && do_source((char_u *)USR_VIMRC_FILE4, TRUE,
3008 DOSO_VIMRC) == FAIL
3009#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003010 && process_env((char_u *)"EXINIT", FALSE) == FAIL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003011 && do_source((char_u *)USR_EXRC_FILE, FALSE, DOSO_NONE) == FAIL)
Bram Moolenaar58d98232005-07-23 22:25:46 +00003012 {
3013#ifdef USR_EXRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003014 (void)do_source((char_u *)USR_EXRC_FILE2, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003015#endif
3016 }
3017 }
3018
3019 /*
3020 * Read initialization commands from ".vimrc" or ".exrc" in current
3021 * directory. This is only done if the 'exrc' option is set.
3022 * Because of security reasons we disallow shell and write commands
3023 * now, except for unix if the file is owned by the user or 'secure'
3024 * option has been reset in environment of global ".exrc" or ".vimrc".
3025 * Only do this if VIMRC_FILE is not the same as USR_VIMRC_FILE or
3026 * SYS_VIMRC_FILE.
3027 */
3028 if (p_exrc)
3029 {
3030#if defined(UNIX) || defined(VMS)
3031 /* If ".vimrc" file is not owned by user, set 'secure' mode. */
3032 if (!file_owned(VIMRC_FILE))
3033#endif
3034 secure = p_secure;
3035
3036 i = FAIL;
3037 if (fullpathcmp((char_u *)USR_VIMRC_FILE,
3038 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3039#ifdef USR_VIMRC_FILE2
3040 && fullpathcmp((char_u *)USR_VIMRC_FILE2,
3041 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3042#endif
3043#ifdef USR_VIMRC_FILE3
3044 && fullpathcmp((char_u *)USR_VIMRC_FILE3,
3045 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3046#endif
3047#ifdef SYS_VIMRC_FILE
3048 && fullpathcmp((char_u *)SYS_VIMRC_FILE,
3049 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3050#endif
3051 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003052 i = do_source((char_u *)VIMRC_FILE, TRUE, DOSO_VIMRC);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003053
3054 if (i == FAIL)
3055 {
3056#if defined(UNIX) || defined(VMS)
3057 /* if ".exrc" is not owned by user set 'secure' mode */
3058 if (!file_owned(EXRC_FILE))
3059 secure = p_secure;
3060 else
3061 secure = 0;
3062#endif
3063 if ( fullpathcmp((char_u *)USR_EXRC_FILE,
3064 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3065#ifdef USR_EXRC_FILE2
3066 && fullpathcmp((char_u *)USR_EXRC_FILE2,
3067 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3068#endif
3069 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003070 (void)do_source((char_u *)EXRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003071 }
3072 }
3073 if (secure == 2)
3074 need_wait_return = TRUE;
3075 secure = 0;
3076#ifdef AMIGA
3077 proc->pr_WindowPtr = save_winptr;
3078#endif
3079 }
3080 TIME_MSG("sourcing vimrc file(s)");
3081}
3082
3083/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003084 * Setup to start using the GUI. Exit with an error when not available.
3085 */
3086 static void
3087main_start_gui()
3088{
3089#ifdef FEAT_GUI
3090 gui.starting = TRUE; /* start GUI a bit later */
3091#else
3092 mch_errmsg(_(e_nogvim));
3093 mch_errmsg("\n");
3094 mch_exit(2);
3095#endif
3096}
3097
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003098#endif /* NO_VIM_MAIN */
3099
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003100/*
Bram Moolenaar49325942007-05-10 19:19:59 +00003101 * Get an environment variable, and execute it as Ex commands.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003102 * Returns FAIL if the environment variable was not executed, OK otherwise.
3103 */
3104 int
3105process_env(env, is_viminit)
3106 char_u *env;
3107 int is_viminit; /* when TRUE, called for VIMINIT */
3108{
3109 char_u *initstr;
3110 char_u *save_sourcing_name;
3111 linenr_T save_sourcing_lnum;
3112#ifdef FEAT_EVAL
3113 scid_T save_sid;
3114#endif
3115
3116 if ((initstr = mch_getenv(env)) != NULL && *initstr != NUL)
3117 {
3118 if (is_viminit)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003119 vimrc_found(NULL, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003120 save_sourcing_name = sourcing_name;
3121 save_sourcing_lnum = sourcing_lnum;
3122 sourcing_name = env;
3123 sourcing_lnum = 0;
3124#ifdef FEAT_EVAL
3125 save_sid = current_SID;
3126 current_SID = SID_ENV;
3127#endif
3128 do_cmdline_cmd(initstr);
3129 sourcing_name = save_sourcing_name;
3130 sourcing_lnum = save_sourcing_lnum;
3131#ifdef FEAT_EVAL
3132 current_SID = save_sid;;
3133#endif
3134 return OK;
3135 }
3136 return FAIL;
3137}
3138
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003139#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003140/*
3141 * Return TRUE if we are certain the user owns the file "fname".
3142 * Used for ".vimrc" and ".exrc".
3143 * Use both stat() and lstat() for extra security.
3144 */
3145 static int
3146file_owned(fname)
3147 char *fname;
3148{
3149 struct stat s;
3150# ifdef UNIX
3151 uid_t uid = getuid();
3152# else /* VMS */
3153 uid_t uid = ((getgid() << 16) | getuid());
3154# endif
3155
3156 return !(mch_stat(fname, &s) != 0 || s.st_uid != uid
3157# ifdef HAVE_LSTAT
3158 || mch_lstat(fname, &s) != 0 || s.st_uid != uid
3159# endif
3160 );
3161}
3162#endif
3163
3164/*
3165 * Give an error message main_errors["n"] and exit.
3166 */
3167 static void
3168mainerr(n, str)
3169 int n; /* one of the ME_ defines */
3170 char_u *str; /* extra argument or NULL */
3171{
3172#if defined(UNIX) || defined(__EMX__) || defined(VMS)
3173 reset_signals(); /* kill us with CTRL-C here, if you like */
3174#endif
3175
3176 mch_errmsg(longVersion);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003177 mch_errmsg("\n");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003178 mch_errmsg(_(main_errors[n]));
3179 if (str != NULL)
3180 {
3181 mch_errmsg(": \"");
3182 mch_errmsg((char *)str);
3183 mch_errmsg("\"");
3184 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003185 mch_errmsg(_("\nMore info with: \"vim -h\"\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003186
3187 mch_exit(1);
3188}
3189
3190 void
3191mainerr_arg_missing(str)
3192 char_u *str;
3193{
3194 mainerr(ME_ARG_MISSING, str);
3195}
3196
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003197#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003198/*
3199 * print a message with three spaces prepended and '\n' appended.
3200 */
3201 static void
3202main_msg(s)
3203 char *s;
3204{
3205 mch_msg(" ");
3206 mch_msg(s);
3207 mch_msg("\n");
3208}
3209
3210/*
3211 * Print messages for "vim -h" or "vim --help" and exit.
3212 */
3213 static void
3214usage()
3215{
3216 int i;
3217 static char *(use[]) =
3218 {
3219 N_("[file ..] edit specified file(s)"),
3220 N_("- read text from stdin"),
3221 N_("-t tag edit file where tag is defined"),
3222#ifdef FEAT_QUICKFIX
3223 N_("-q [errorfile] edit file with first error")
3224#endif
3225 };
3226
3227#if defined(UNIX) || defined(__EMX__) || defined(VMS)
3228 reset_signals(); /* kill us with CTRL-C here, if you like */
3229#endif
3230
3231 mch_msg(longVersion);
3232 mch_msg(_("\n\nusage:"));
3233 for (i = 0; ; ++i)
3234 {
3235 mch_msg(_(" vim [arguments] "));
3236 mch_msg(_(use[i]));
3237 if (i == (sizeof(use) / sizeof(char_u *)) - 1)
3238 break;
3239 mch_msg(_("\n or:"));
3240 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003241#ifdef VMS
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003242 mch_msg(_("\nWhere case is ignored prepend / to make flag upper case"));
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003243#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003244
3245 mch_msg(_("\n\nArguments:\n"));
3246 main_msg(_("--\t\t\tOnly file names after this"));
3247#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
3248 main_msg(_("--literal\t\tDon't expand wildcards"));
3249#endif
3250#ifdef FEAT_OLE
3251 main_msg(_("-register\t\tRegister this gvim for OLE"));
3252 main_msg(_("-unregister\t\tUnregister gvim for OLE"));
3253#endif
3254#ifdef FEAT_GUI
3255 main_msg(_("-g\t\t\tRun using GUI (like \"gvim\")"));
3256 main_msg(_("-f or --nofork\tForeground: Don't fork when starting GUI"));
3257#endif
3258 main_msg(_("-v\t\t\tVi mode (like \"vi\")"));
3259 main_msg(_("-e\t\t\tEx mode (like \"ex\")"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003260 main_msg(_("-E\t\t\tImproved Ex mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003261 main_msg(_("-s\t\t\tSilent (batch) mode (only for \"ex\")"));
3262#ifdef FEAT_DIFF
3263 main_msg(_("-d\t\t\tDiff mode (like \"vimdiff\")"));
3264#endif
3265 main_msg(_("-y\t\t\tEasy mode (like \"evim\", modeless)"));
3266 main_msg(_("-R\t\t\tReadonly mode (like \"view\")"));
3267 main_msg(_("-Z\t\t\tRestricted mode (like \"rvim\")"));
3268 main_msg(_("-m\t\t\tModifications (writing files) not allowed"));
3269 main_msg(_("-M\t\t\tModifications in text not allowed"));
3270 main_msg(_("-b\t\t\tBinary mode"));
3271#ifdef FEAT_LISP
3272 main_msg(_("-l\t\t\tLisp mode"));
3273#endif
3274 main_msg(_("-C\t\t\tCompatible with Vi: 'compatible'"));
3275 main_msg(_("-N\t\t\tNot fully Vi compatible: 'nocompatible'"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003276 main_msg(_("-V[N][fname]\t\tBe verbose [level N] [log messages to fname]"));
3277#ifdef FEAT_EVAL
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003278 main_msg(_("-D\t\t\tDebugging mode"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003279#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003280 main_msg(_("-n\t\t\tNo swap file, use memory only"));
3281 main_msg(_("-r\t\t\tList swap files and exit"));
3282 main_msg(_("-r (with file name)\tRecover crashed session"));
3283 main_msg(_("-L\t\t\tSame as -r"));
3284#ifdef AMIGA
3285 main_msg(_("-f\t\t\tDon't use newcli to open window"));
3286 main_msg(_("-dev <device>\t\tUse <device> for I/O"));
3287#endif
3288#ifdef FEAT_ARABIC
3289 main_msg(_("-A\t\t\tstart in Arabic mode"));
3290#endif
3291#ifdef FEAT_RIGHTLEFT
3292 main_msg(_("-H\t\t\tStart in Hebrew mode"));
3293#endif
3294#ifdef FEAT_FKMAP
3295 main_msg(_("-F\t\t\tStart in Farsi mode"));
3296#endif
3297 main_msg(_("-T <terminal>\tSet terminal type to <terminal>"));
3298 main_msg(_("-u <vimrc>\t\tUse <vimrc> instead of any .vimrc"));
3299#ifdef FEAT_GUI
3300 main_msg(_("-U <gvimrc>\t\tUse <gvimrc> instead of any .gvimrc"));
3301#endif
3302 main_msg(_("--noplugin\t\tDon't load plugin scripts"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003303#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003304 main_msg(_("-p[N]\t\tOpen N tab pages (default: one for each file)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003305 main_msg(_("-o[N]\t\tOpen N windows (default: one for each file)"));
3306 main_msg(_("-O[N]\t\tLike -o but split vertically"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003307#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003308 main_msg(_("+\t\t\tStart at end of file"));
3309 main_msg(_("+<lnum>\t\tStart at line <lnum>"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003310 main_msg(_("--cmd <command>\tExecute <command> before loading any vimrc file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003311 main_msg(_("-c <command>\t\tExecute <command> after loading the first file"));
3312 main_msg(_("-S <session>\t\tSource file <session> after loading the first file"));
3313 main_msg(_("-s <scriptin>\tRead Normal mode commands from file <scriptin>"));
3314 main_msg(_("-w <scriptout>\tAppend all typed commands to file <scriptout>"));
3315 main_msg(_("-W <scriptout>\tWrite all typed commands to file <scriptout>"));
3316#ifdef FEAT_CRYPT
3317 main_msg(_("-x\t\t\tEdit encrypted files"));
3318#endif
3319#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
3320# if defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)
3321 main_msg(_("-display <display>\tConnect vim to this particular X-server"));
3322# endif
3323 main_msg(_("-X\t\t\tDo not connect to X server"));
3324#endif
3325#ifdef FEAT_CLIENTSERVER
3326 main_msg(_("--remote <files>\tEdit <files> in a Vim server if possible"));
3327 main_msg(_("--remote-silent <files> Same, don't complain if there is no server"));
3328 main_msg(_("--remote-wait <files> As --remote but wait for files to have been edited"));
3329 main_msg(_("--remote-wait-silent <files> Same, don't complain if there is no server"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003330# ifdef FEAT_WINDOWS
Bram Moolenaar82ad3242008-01-11 19:26:36 +00003331 main_msg(_("--remote-tab[-wait][-silent] <files> As --remote but use tab page per file"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003332# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003333 main_msg(_("--remote-send <keys>\tSend <keys> to a Vim server and exit"));
3334 main_msg(_("--remote-expr <expr>\tEvaluate <expr> in a Vim server and print result"));
3335 main_msg(_("--serverlist\t\tList available Vim server names and exit"));
3336 main_msg(_("--servername <name>\tSend to/become the Vim server <name>"));
3337#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003338#ifdef STARTUPTIME
Bram Moolenaar34ef52d2009-11-17 11:31:25 +00003339 main_msg(_("--startuptime <file>\tWrite startup timing messages to <file>"));
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003340#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003341#ifdef FEAT_VIMINFO
3342 main_msg(_("-i <viminfo>\t\tUse <viminfo> instead of .viminfo"));
3343#endif
3344 main_msg(_("-h or --help\tPrint Help (this message) and exit"));
3345 main_msg(_("--version\t\tPrint version information and exit"));
3346
3347#ifdef FEAT_GUI_X11
3348# ifdef FEAT_GUI_MOTIF
3349 mch_msg(_("\nArguments recognised by gvim (Motif version):\n"));
3350# else
3351# ifdef FEAT_GUI_ATHENA
3352# ifdef FEAT_GUI_NEXTAW
3353 mch_msg(_("\nArguments recognised by gvim (neXtaw version):\n"));
3354# else
3355 mch_msg(_("\nArguments recognised by gvim (Athena version):\n"));
3356# endif
3357# endif
3358# endif
3359 main_msg(_("-display <display>\tRun vim on <display>"));
3360 main_msg(_("-iconic\t\tStart vim iconified"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003361 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
3362 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
3363 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3364 main_msg(_("-boldfont <font>\tUse <font> for bold text"));
3365 main_msg(_("-italicfont <font>\tUse <font> for italic text"));
3366 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3367 main_msg(_("-borderwidth <width>\tUse a border width of <width> (also: -bw)"));
3368 main_msg(_("-scrollbarwidth <width> Use a scrollbar width of <width> (also: -sw)"));
3369# ifdef FEAT_GUI_ATHENA
3370 main_msg(_("-menuheight <height>\tUse a menu bar height of <height> (also: -mh)"));
3371# endif
3372 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3373 main_msg(_("+reverse\t\tDon't use reverse video (also: +rv)"));
3374 main_msg(_("-xrm <resource>\tSet the specified resource"));
3375#endif /* FEAT_GUI_X11 */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003376#ifdef FEAT_GUI_GTK
3377 mch_msg(_("\nArguments recognised by gvim (GTK+ version):\n"));
3378 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3379 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3380 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3381 main_msg(_("-display <display>\tRun vim on <display> (also: --display)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003382 main_msg(_("--role <role>\tSet a unique role to identify the main window"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003383 main_msg(_("--socketid <xid>\tOpen Vim inside another GTK widget"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003384 main_msg(_("--echo-wid\t\tMake gvim echo the Window ID on stdout"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003385#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003386#ifdef FEAT_GUI_W32
3387 main_msg(_("-P <parent title>\tOpen Vim inside parent application"));
Bram Moolenaar78e17622007-08-30 10:26:19 +00003388 main_msg(_("--windowid <HWND>\tOpen Vim inside another win32 widget"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003389#endif
3390
3391#ifdef FEAT_GUI_GNOME
3392 /* Gnome gives extra messages for --help if we continue, but not for -h. */
3393 if (gui.starting)
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003394 {
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003395 mch_msg("\n");
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003396 gui.dofork = FALSE;
3397 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003398 else
3399#endif
3400 mch_exit(0);
3401}
3402
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00003403#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003404/*
3405 * Check the result of the ATTENTION dialog:
3406 * When "Quit" selected, exit Vim.
3407 * When "Recover" selected, recover the file.
3408 */
3409 static void
3410check_swap_exists_action()
3411{
3412 if (swap_exists_action == SEA_QUIT)
3413 getout(1);
3414 handle_swap_exists(NULL);
3415}
3416#endif
3417
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003418#endif
3419
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003420#if defined(STARTUPTIME) || defined(PROTO)
3421static void time_diff __ARGS((struct timeval *then, struct timeval *now));
3422
3423static struct timeval prev_timeval;
3424
Bram Moolenaar3f269672009-11-03 11:11:11 +00003425# ifdef WIN3264
3426/*
3427 * Windows doesn't have gettimeofday(), although it does have struct timeval.
3428 */
3429 static int
3430gettimeofday(struct timeval *tv, char *dummy)
3431{
3432 long t = clock();
3433 tv->tv_sec = t / CLOCKS_PER_SEC;
3434 tv->tv_usec = (t - tv->tv_sec * CLOCKS_PER_SEC) * 1000000 / CLOCKS_PER_SEC;
3435 return 0;
3436}
3437# endif
3438
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003439/*
3440 * Save the previous time before doing something that could nest.
3441 * set "*tv_rel" to the time elapsed so far.
3442 */
3443 void
3444time_push(tv_rel, tv_start)
3445 void *tv_rel, *tv_start;
3446{
3447 *((struct timeval *)tv_rel) = prev_timeval;
3448 gettimeofday(&prev_timeval, NULL);
3449 ((struct timeval *)tv_rel)->tv_usec = prev_timeval.tv_usec
3450 - ((struct timeval *)tv_rel)->tv_usec;
3451 ((struct timeval *)tv_rel)->tv_sec = prev_timeval.tv_sec
3452 - ((struct timeval *)tv_rel)->tv_sec;
3453 if (((struct timeval *)tv_rel)->tv_usec < 0)
3454 {
3455 ((struct timeval *)tv_rel)->tv_usec += 1000000;
3456 --((struct timeval *)tv_rel)->tv_sec;
3457 }
3458 *(struct timeval *)tv_start = prev_timeval;
3459}
3460
3461/*
3462 * Compute the previous time after doing something that could nest.
3463 * Subtract "*tp" from prev_timeval;
3464 * Note: The arguments are (void *) to avoid trouble with systems that don't
3465 * have struct timeval.
3466 */
3467 void
3468time_pop(tp)
3469 void *tp; /* actually (struct timeval *) */
3470{
3471 prev_timeval.tv_usec -= ((struct timeval *)tp)->tv_usec;
3472 prev_timeval.tv_sec -= ((struct timeval *)tp)->tv_sec;
3473 if (prev_timeval.tv_usec < 0)
3474 {
3475 prev_timeval.tv_usec += 1000000;
3476 --prev_timeval.tv_sec;
3477 }
3478}
3479
3480 static void
3481time_diff(then, now)
3482 struct timeval *then;
3483 struct timeval *now;
3484{
3485 long usec;
3486 long msec;
3487
3488 usec = now->tv_usec - then->tv_usec;
3489 msec = (now->tv_sec - then->tv_sec) * 1000L + usec / 1000L,
3490 usec = usec % 1000L;
3491 fprintf(time_fd, "%03ld.%03ld", msec, usec >= 0 ? usec : usec + 1000L);
3492}
3493
3494 void
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003495time_msg(mesg, tv_start)
3496 char *mesg;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003497 void *tv_start; /* only for do_source: start time; actually
3498 (struct timeval *) */
3499{
3500 static struct timeval start;
3501 struct timeval now;
3502
3503 if (time_fd != NULL)
3504 {
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003505 if (strstr(mesg, "STARTING") != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003506 {
3507 gettimeofday(&start, NULL);
3508 prev_timeval = start;
3509 fprintf(time_fd, "\n\ntimes in msec\n");
3510 fprintf(time_fd, " clock self+sourced self: sourced script\n");
3511 fprintf(time_fd, " clock elapsed: other lines\n\n");
3512 }
3513 gettimeofday(&now, NULL);
3514 time_diff(&start, &now);
3515 if (((struct timeval *)tv_start) != NULL)
3516 {
3517 fprintf(time_fd, " ");
3518 time_diff(((struct timeval *)tv_start), &now);
3519 }
3520 fprintf(time_fd, " ");
3521 time_diff(&prev_timeval, &now);
3522 prev_timeval = now;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003523 fprintf(time_fd, ": %s\n", mesg);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003524 }
3525}
3526
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003527#endif
3528
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003529#if (defined(FEAT_CLIENTSERVER) && !defined(NO_VIM_MAIN)) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003530
3531/*
3532 * Common code for the X command server and the Win32 command server.
3533 */
3534
Bram Moolenaareb94e552006-03-11 21:35:11 +00003535static char_u *build_drop_cmd __ARGS((int filec, char **filev, int tabs, int sendReply));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003536
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003537/*
3538 * Do the client-server stuff, unless "--servername ''" was used.
3539 */
3540 static void
3541exec_on_server(parmp)
3542 mparm_T *parmp;
3543{
3544 if (parmp->serverName_arg == NULL || *parmp->serverName_arg != NUL)
3545 {
3546# ifdef WIN32
3547 /* Initialise the client/server messaging infrastructure. */
3548 serverInitMessaging();
3549# endif
3550
3551 /*
3552 * When a command server argument was found, execute it. This may
3553 * exit Vim when it was successful. Otherwise it's executed further
3554 * on. Remember the encoding used here in "serverStrEnc".
3555 */
3556 if (parmp->serverArg)
3557 {
3558 cmdsrv_main(&parmp->argc, parmp->argv,
3559 parmp->serverName_arg, &parmp->serverStr);
3560# ifdef FEAT_MBYTE
3561 parmp->serverStrEnc = vim_strsave(p_enc);
3562# endif
3563 }
3564
3565 /* If we're still running, get the name to register ourselves.
3566 * On Win32 can register right now, for X11 need to setup the
3567 * clipboard first, it's further down. */
3568 parmp->servername = serverMakeName(parmp->serverName_arg,
3569 parmp->argv[0]);
3570# ifdef WIN32
3571 if (parmp->servername != NULL)
3572 {
3573 serverSetName(parmp->servername);
3574 vim_free(parmp->servername);
3575 }
3576# endif
3577 }
3578}
3579
3580/*
3581 * Prepare for running as a Vim server.
3582 */
3583 static void
3584prepare_server(parmp)
3585 mparm_T *parmp;
3586{
3587# if defined(FEAT_X11)
3588 /*
3589 * Register for remote command execution with :serversend and --remote
3590 * unless there was a -X or a --servername '' on the command line.
3591 * Only register nongui-vim's with an explicit --servername argument.
Bram Moolenaar5f402312006-08-15 19:40:35 +00003592 * When running as root --servername is also required.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003593 */
3594 if (X_DISPLAY != NULL && parmp->servername != NULL && (
3595# ifdef FEAT_GUI
Bram Moolenaar5f402312006-08-15 19:40:35 +00003596 (gui.in_use
3597# ifdef UNIX
Bram Moolenaar311d9822007-02-27 15:48:28 +00003598 && getuid() != ROOT_UID
Bram Moolenaar5f402312006-08-15 19:40:35 +00003599# endif
3600 ) ||
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003601# endif
3602 parmp->serverName_arg != NULL))
3603 {
3604 (void)serverRegisterName(X_DISPLAY, parmp->servername);
3605 vim_free(parmp->servername);
3606 TIME_MSG("register server name");
3607 }
3608 else
3609 serverDelayedStartName = parmp->servername;
3610# endif
3611
3612 /*
3613 * Execute command ourselves if we're here because the send failed (or
3614 * else we would have exited above).
3615 */
3616 if (parmp->serverStr != NULL)
3617 {
3618 char_u *p;
3619
3620 server_to_input_buf(serverConvert(parmp->serverStrEnc,
3621 parmp->serverStr, &p));
3622 vim_free(p);
3623 }
3624}
3625
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003626 static void
3627cmdsrv_main(argc, argv, serverName_arg, serverStr)
3628 int *argc;
3629 char **argv;
3630 char_u *serverName_arg;
3631 char_u **serverStr;
3632{
3633 char_u *res;
3634 int i;
3635 char_u *sname;
3636 int ret;
3637 int didone = FALSE;
3638 int exiterr = 0;
3639 char **newArgV = argv + 1;
3640 int newArgC = 1,
3641 Argc = *argc;
3642 int argtype;
3643#define ARGTYPE_OTHER 0
3644#define ARGTYPE_EDIT 1
3645#define ARGTYPE_EDIT_WAIT 2
3646#define ARGTYPE_SEND 3
3647 int silent = FALSE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003648 int tabs = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003649# ifndef FEAT_X11
3650 HWND srv;
3651# else
3652 Window srv;
3653
3654 setup_term_clip();
3655# endif
3656
3657 sname = serverMakeName(serverName_arg, argv[0]);
3658 if (sname == NULL)
3659 return;
3660
3661 /*
3662 * Execute the command server related arguments and remove them
3663 * from the argc/argv array; We may have to return into main()
3664 */
3665 for (i = 1; i < Argc; i++)
3666 {
3667 res = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003668 if (STRCMP(argv[i], "--") == 0) /* end of option arguments */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003669 {
3670 for (; i < *argc; i++)
3671 {
3672 *newArgV++ = argv[i];
3673 newArgC++;
3674 }
3675 break;
3676 }
3677
Bram Moolenaareb94e552006-03-11 21:35:11 +00003678 if (STRICMP(argv[i], "--remote-send") == 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003679 argtype = ARGTYPE_SEND;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003680 else if (STRNICMP(argv[i], "--remote", 8) == 0)
3681 {
3682 char *p = argv[i] + 8;
3683
3684 argtype = ARGTYPE_EDIT;
3685 while (*p != NUL)
3686 {
3687 if (STRNICMP(p, "-wait", 5) == 0)
3688 {
3689 argtype = ARGTYPE_EDIT_WAIT;
3690 p += 5;
3691 }
3692 else if (STRNICMP(p, "-silent", 7) == 0)
3693 {
3694 silent = TRUE;
3695 p += 7;
3696 }
3697 else if (STRNICMP(p, "-tab", 4) == 0)
3698 {
3699 tabs = TRUE;
3700 p += 4;
3701 }
3702 else
3703 {
3704 argtype = ARGTYPE_OTHER;
3705 break;
3706 }
3707 }
3708 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003709 else
3710 argtype = ARGTYPE_OTHER;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003711
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003712 if (argtype != ARGTYPE_OTHER)
3713 {
3714 if (i == *argc - 1)
3715 mainerr_arg_missing((char_u *)argv[i]);
3716 if (argtype == ARGTYPE_SEND)
3717 {
3718 *serverStr = (char_u *)argv[i + 1];
3719 i++;
3720 }
3721 else
3722 {
3723 *serverStr = build_drop_cmd(*argc - i - 1, argv + i + 1,
Bram Moolenaareb94e552006-03-11 21:35:11 +00003724 tabs, argtype == ARGTYPE_EDIT_WAIT);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003725 if (*serverStr == NULL)
3726 {
3727 /* Probably out of memory, exit. */
3728 didone = TRUE;
3729 exiterr = 1;
3730 break;
3731 }
3732 Argc = i;
3733 }
3734# ifdef FEAT_X11
3735 if (xterm_dpy == NULL)
3736 {
3737 mch_errmsg(_("No display"));
3738 ret = -1;
3739 }
3740 else
3741 ret = serverSendToVim(xterm_dpy, sname, *serverStr,
3742 NULL, &srv, 0, 0, silent);
3743# else
3744 /* Win32 always works? */
3745 ret = serverSendToVim(sname, *serverStr, NULL, &srv, 0, silent);
3746# endif
3747 if (ret < 0)
3748 {
3749 if (argtype == ARGTYPE_SEND)
3750 {
3751 /* Failed to send, abort. */
3752 mch_errmsg(_(": Send failed.\n"));
3753 didone = TRUE;
3754 exiterr = 1;
3755 }
3756 else if (!silent)
3757 /* Let vim start normally. */
3758 mch_errmsg(_(": Send failed. Trying to execute locally\n"));
3759 break;
3760 }
3761
3762# ifdef FEAT_GUI_W32
3763 /* Guess that when the server name starts with "g" it's a GUI
3764 * server, which we can bring to the foreground here.
3765 * Foreground() in the server doesn't work very well. */
3766 if (argtype != ARGTYPE_SEND && TOUPPER_ASC(*sname) == 'G')
3767 SetForegroundWindow(srv);
3768# endif
3769
3770 /*
3771 * For --remote-wait: Wait until the server did edit each
3772 * file. Also detect that the server no longer runs.
3773 */
3774 if (ret >= 0 && argtype == ARGTYPE_EDIT_WAIT)
3775 {
3776 int numFiles = *argc - i - 1;
3777 int j;
3778 char_u *done = alloc(numFiles);
3779 char_u *p;
3780# ifdef FEAT_GUI_W32
3781 NOTIFYICONDATA ni;
3782 int count = 0;
3783 extern HWND message_window;
3784# endif
3785
3786 if (numFiles > 0 && argv[i + 1][0] == '+')
3787 /* Skip "+cmd" argument, don't wait for it to be edited. */
3788 --numFiles;
3789
3790# ifdef FEAT_GUI_W32
3791 ni.cbSize = sizeof(ni);
3792 ni.hWnd = message_window;
3793 ni.uID = 0;
3794 ni.uFlags = NIF_ICON|NIF_TIP;
3795 ni.hIcon = LoadIcon((HINSTANCE)GetModuleHandle(0), "IDR_VIM");
3796 sprintf(ni.szTip, _("%d of %d edited"), count, numFiles);
3797 Shell_NotifyIcon(NIM_ADD, &ni);
3798# endif
3799
3800 /* Wait for all files to unload in remote */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02003801 vim_memset(done, 0, numFiles);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003802 while (memchr(done, 0, numFiles) != NULL)
3803 {
3804# ifdef WIN32
3805 p = serverGetReply(srv, NULL, TRUE, TRUE);
3806 if (p == NULL)
3807 break;
3808# else
3809 if (serverReadReply(xterm_dpy, srv, &p, TRUE) < 0)
3810 break;
3811# endif
3812 j = atoi((char *)p);
3813 if (j >= 0 && j < numFiles)
3814 {
3815# ifdef FEAT_GUI_W32
3816 ++count;
3817 sprintf(ni.szTip, _("%d of %d edited"),
3818 count, numFiles);
3819 Shell_NotifyIcon(NIM_MODIFY, &ni);
3820# endif
3821 done[j] = 1;
3822 }
3823 }
3824# ifdef FEAT_GUI_W32
3825 Shell_NotifyIcon(NIM_DELETE, &ni);
3826# endif
3827 }
3828 }
3829 else if (STRICMP(argv[i], "--remote-expr") == 0)
3830 {
3831 if (i == *argc - 1)
3832 mainerr_arg_missing((char_u *)argv[i]);
3833# ifdef WIN32
3834 /* Win32 always works? */
3835 if (serverSendToVim(sname, (char_u *)argv[i + 1],
3836 &res, NULL, 1, FALSE) < 0)
3837# else
3838 if (xterm_dpy == NULL)
3839 mch_errmsg(_("No display: Send expression failed.\n"));
3840 else if (serverSendToVim(xterm_dpy, sname, (char_u *)argv[i + 1],
3841 &res, NULL, 1, 1, FALSE) < 0)
3842# endif
3843 {
3844 if (res != NULL && *res != NUL)
3845 {
3846 /* Output error from remote */
3847 mch_errmsg((char *)res);
3848 vim_free(res);
3849 res = NULL;
3850 }
3851 mch_errmsg(_(": Send expression failed.\n"));
3852 }
3853 }
3854 else if (STRICMP(argv[i], "--serverlist") == 0)
3855 {
3856# ifdef WIN32
3857 /* Win32 always works? */
3858 res = serverGetVimNames();
3859# else
3860 if (xterm_dpy != NULL)
3861 res = serverGetVimNames(xterm_dpy);
3862# endif
3863 if (called_emsg)
3864 mch_errmsg("\n");
3865 }
3866 else if (STRICMP(argv[i], "--servername") == 0)
3867 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +00003868 /* Already processed. Take it out of the command line */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003869 i++;
3870 continue;
3871 }
3872 else
3873 {
3874 *newArgV++ = argv[i];
3875 newArgC++;
3876 continue;
3877 }
3878 didone = TRUE;
3879 if (res != NULL && *res != NUL)
3880 {
3881 mch_msg((char *)res);
3882 if (res[STRLEN(res) - 1] != '\n')
3883 mch_msg("\n");
3884 }
3885 vim_free(res);
3886 }
3887
3888 if (didone)
3889 {
3890 display_errors(); /* display any collected messages */
3891 exit(exiterr); /* Mission accomplished - get out */
3892 }
3893
3894 /* Return back into main() */
3895 *argc = newArgC;
3896 vim_free(sname);
3897}
3898
3899/*
3900 * Build a ":drop" command to send to a Vim server.
3901 */
3902 static char_u *
Bram Moolenaareb94e552006-03-11 21:35:11 +00003903build_drop_cmd(filec, filev, tabs, sendReply)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003904 int filec;
3905 char **filev;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003906 int tabs; /* Use ":tab drop" instead of ":drop". */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003907 int sendReply;
3908{
3909 garray_T ga;
3910 int i;
3911 char_u *inicmd = NULL;
3912 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003913 char_u *cwd;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003914
3915 if (filec > 0 && filev[0][0] == '+')
3916 {
3917 inicmd = (char_u *)filev[0] + 1;
3918 filev++;
3919 filec--;
3920 }
3921 /* Check if we have at least one argument. */
3922 if (filec <= 0)
3923 mainerr_arg_missing((char_u *)filev[-1]);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003924
3925 /* Temporarily cd to the current directory to handle relative file names. */
Bram Moolenaard9462e32011-04-11 21:35:11 +02003926 cwd = alloc(MAXPATHL);
3927 if (cwd == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003928 return NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003929 if (mch_dirname(cwd, MAXPATHL) != OK)
3930 {
3931 vim_free(cwd);
3932 return NULL;
3933 }
3934 p = vim_strsave_escaped_ext(cwd,
Bram Moolenaar02b06312007-09-06 15:39:22 +00003935#ifdef BACKSLASH_IN_FILENAME
3936 "", /* rem_backslash() will tell what chars to escape */
3937#else
3938 PATH_ESC_CHARS,
3939#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +02003940 '\\', TRUE);
3941 vim_free(cwd);
3942 if (p == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003943 return NULL;
3944 ga_init2(&ga, 1, 100);
3945 ga_concat(&ga, (char_u *)"<C-\\><C-N>:cd ");
3946 ga_concat(&ga, p);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003947 vim_free(p);
Bram Moolenaareb94e552006-03-11 21:35:11 +00003948
3949 /* Call inputsave() so that a prompt for an encryption key works. */
3950 ga_concat(&ga, (char_u *)"<CR>:if exists('*inputsave')|call inputsave()|endif|");
3951 if (tabs)
3952 ga_concat(&ga, (char_u *)"tab ");
3953 ga_concat(&ga, (char_u *)"drop");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003954 for (i = 0; i < filec; i++)
3955 {
3956 /* On Unix the shell has already expanded the wildcards, don't want to
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003957 * do it again in the Vim server. On MS-Windows only escape
3958 * non-wildcard characters. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003959 p = vim_strsave_escaped((char_u *)filev[i],
3960#ifdef UNIX
3961 PATH_ESC_CHARS
3962#else
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003963 (char_u *)" \t%#"
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003964#endif
3965 );
3966 if (p == NULL)
3967 {
3968 vim_free(ga.ga_data);
3969 return NULL;
3970 }
3971 ga_concat(&ga, (char_u *)" ");
3972 ga_concat(&ga, p);
3973 vim_free(p);
3974 }
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003975 ga_concat(&ga, (char_u *)"|if exists('*inputrestore')|call inputrestore()|endif<CR>");
3976
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003977 /* The :drop commands goes to Insert mode when 'insertmode' is set, use
3978 * CTRL-\ CTRL-N again. */
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003979 ga_concat(&ga, (char_u *)"<C-\\><C-N>");
3980
3981 /* Switch back to the correct current directory (prior to temporary path
3982 * switch) unless 'autochdir' is set, in which case it will already be
3983 * correct after the :drop command. */
3984 ga_concat(&ga, (char_u *)":if !exists('+acd')||!&acd|cd -|endif<CR>");
3985
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003986 if (sendReply)
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003987 ga_concat(&ga, (char_u *)":call SetupRemoteReplies()<CR>");
3988 ga_concat(&ga, (char_u *)":");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003989 if (inicmd != NULL)
3990 {
3991 /* Can't use <CR> after "inicmd", because an "startinsert" would cause
3992 * the following commands to be inserted as text. Use a "|",
3993 * hopefully "inicmd" does allow this... */
3994 ga_concat(&ga, inicmd);
3995 ga_concat(&ga, (char_u *)"|");
3996 }
3997 /* Bring the window to the foreground, goto Insert mode when 'im' set and
3998 * clear command line. */
Bram Moolenaar567e4de2004-12-31 21:01:02 +00003999 ga_concat(&ga, (char_u *)"cal foreground()|if &im|star|en|redr|f<CR>");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004000 ga_append(&ga, NUL);
4001 return ga.ga_data;
4002}
4003
4004/*
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004005 * Make our basic server name: use the specified "arg" if given, otherwise use
4006 * the tail of the command "cmd" we were started with.
4007 * Return the name in allocated memory. This doesn't include a serial number.
4008 */
4009 static char_u *
4010serverMakeName(arg, cmd)
4011 char_u *arg;
4012 char *cmd;
4013{
4014 char_u *p;
4015
4016 if (arg != NULL && *arg != NUL)
4017 p = vim_strsave_up(arg);
4018 else
4019 {
4020 p = vim_strsave_up(gettail((char_u *)cmd));
4021 /* Remove .exe or .bat from the name. */
4022 if (p != NULL && vim_strchr(p, '.') != NULL)
4023 *vim_strchr(p, '.') = NUL;
4024 }
4025 return p;
4026}
4027#endif /* FEAT_CLIENTSERVER */
4028
4029#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
4030/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004031 * Replace termcodes such as <CR> and insert as key presses if there is room.
4032 */
4033 void
4034server_to_input_buf(str)
4035 char_u *str;
4036{
4037 char_u *ptr = NULL;
4038 char_u *cpo_save = p_cpo;
4039
4040 /* Set 'cpoptions' the way we want it.
4041 * B set - backslashes are *not* treated specially
4042 * k set - keycodes are *not* reverse-engineered
4043 * < unset - <Key> sequences *are* interpreted
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004044 * The last but one parameter of replace_termcodes() is TRUE so that the
4045 * <lt> sequence is recognised - needed for a real backslash.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004046 */
4047 p_cpo = (char_u *)"Bk";
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004048 str = replace_termcodes((char_u *)str, &ptr, FALSE, TRUE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004049 p_cpo = cpo_save;
4050
4051 if (*ptr != NUL) /* trailing CTRL-V results in nothing */
4052 {
4053 /*
4054 * Add the string to the input stream.
4055 * Can't use add_to_input_buf() here, we now have K_SPECIAL bytes.
4056 *
4057 * First clear typed characters from the typeahead buffer, there could
4058 * be half a mapping there. Then append to the existing string, so
4059 * that multiple commands from a client are concatenated.
4060 */
4061 if (typebuf.tb_maplen < typebuf.tb_len)
4062 del_typebuf(typebuf.tb_len - typebuf.tb_maplen, typebuf.tb_maplen);
4063 (void)ins_typebuf(str, REMAP_NONE, typebuf.tb_len, TRUE, FALSE);
4064
4065 /* Let input_available() know we inserted text in the typeahead
4066 * buffer. */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004067 typebuf_was_filled = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004068 }
4069 vim_free((char_u *)ptr);
4070}
4071
4072/*
4073 * Evaluate an expression that the client sent to a string.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004074 */
4075 char_u *
4076eval_client_expr_to_string(expr)
4077 char_u *expr;
4078{
4079 char_u *res;
4080 int save_dbl = debug_break_level;
4081 int save_ro = redir_off;
4082
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004083 /* Disable debugging, otherwise Vim hangs, waiting for "cont" to be
4084 * typed. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004085 debug_break_level = -1;
4086 redir_off = 0;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004087 /* Do not display error message, otherwise Vim hangs, waiting for "cont"
4088 * to be typed. Do generate errors so that try/catch works. */
4089 ++emsg_silent;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004090
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004091 res = eval_to_string(expr, NULL, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004092
4093 debug_break_level = save_dbl;
4094 redir_off = save_ro;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004095 --emsg_silent;
4096 if (emsg_silent < 0)
4097 emsg_silent = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004098
Bram Moolenaar63a121b2005-12-11 21:36:39 +00004099 /* A client can tell us to redraw, but not to display the cursor, so do
4100 * that here. */
4101 setcursor();
4102 out_flush();
4103#ifdef FEAT_GUI
4104 if (gui.in_use)
4105 gui_update_cursor(FALSE, FALSE);
4106#endif
4107
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004108 return res;
4109}
4110
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004111/*
4112 * If conversion is needed, convert "data" from "client_enc" to 'encoding' and
4113 * return an allocated string. Otherwise return "data".
4114 * "*tofree" is set to the result when it needs to be freed later.
4115 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004116 char_u *
4117serverConvert(client_enc, data, tofree)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004118 char_u *client_enc UNUSED;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004119 char_u *data;
4120 char_u **tofree;
4121{
4122 char_u *res = data;
4123
4124 *tofree = NULL;
4125# ifdef FEAT_MBYTE
4126 if (client_enc != NULL && p_enc != NULL)
4127 {
4128 vimconv_T vimconv;
4129
4130 vimconv.vc_type = CONV_NONE;
4131 if (convert_setup(&vimconv, client_enc, p_enc) != FAIL
4132 && vimconv.vc_type != CONV_NONE)
4133 {
4134 res = string_convert(&vimconv, data, NULL);
4135 if (res == NULL)
4136 res = data;
4137 else
4138 *tofree = res;
4139 }
4140 convert_setup(&vimconv, NULL, NULL);
4141 }
4142# endif
4143 return res;
4144}
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004145#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004146
4147/*
4148 * When FEAT_FKMAP is defined, also compile the Farsi source code.
4149 */
4150#if defined(FEAT_FKMAP) || defined(PROTO)
4151# include "farsi.c"
4152#endif
4153
4154/*
4155 * When FEAT_ARABIC is defined, also compile the Arabic source code.
4156 */
4157#if defined(FEAT_ARABIC) || defined(PROTO)
4158# include "arabic.c"
4159#endif