blob: 1a71be9088dba3f87ee8ff7fec7f856a6e7280fe [file] [log] [blame]
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10#if defined(MSDOS) || defined(WIN32) || defined(_WIN64)
11# include <io.h> /* for close() and dup() */
12#endif
13
14#define EXTERN
15#include "vim.h"
16
17#ifdef SPAWNO
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +000018# include <spawno.h> /* special MS-DOS swapping library */
Bram Moolenaarb4210b32004-06-13 14:51:16 +000019#endif
20
21#ifdef HAVE_FCNTL_H
22# include <fcntl.h>
23#endif
24
25#ifdef __CYGWIN__
26# ifndef WIN32
27# include <sys/cygwin.h> /* for cygwin_conv_to_posix_path() */
28# endif
29# include <limits.h>
30#endif
31
Bram Moolenaarc013cb62005-07-24 21:18:31 +000032/* Maximum number of commands from + or -c arguments. */
33#define MAX_ARG_CMDS 10
34
Bram Moolenaar58d98232005-07-23 22:25:46 +000035/* Struct for various parameters passed between main() and other functions. */
36typedef struct
37{
Bram Moolenaarc013cb62005-07-24 21:18:31 +000038 int argc;
39 char **argv;
40
41 int evim_mode; /* started as "evim" */
Bram Moolenaarc013cb62005-07-24 21:18:31 +000042 char_u *use_vimrc; /* vimrc from -u argument */
43
44 int n_commands; /* no. of commands from + or -c */
45 char_u *commands[MAX_ARG_CMDS]; /* commands from + or -c arg. */
46 char_u cmds_tofree[MAX_ARG_CMDS]; /* commands that need free() */
47 int n_pre_commands; /* no. of commands from --cmd */
48 char_u *pre_commands[MAX_ARG_CMDS]; /* commands from --cmd argument */
49
50 int edit_type; /* type of editing to do */
51 char_u *tagname; /* tag from -t argument */
52#ifdef FEAT_QUICKFIX
53 char_u *use_ef; /* 'errorfile' from -q argument */
54#endif
55
56 int want_full_screen;
57 int stdout_isatty; /* is stdout a terminal? */
58 char_u *term; /* specified terminal name */
59#ifdef FEAT_CRYPT
60 int ask_for_key; /* -x argument */
61#endif
62 int no_swap_file; /* "-n" argument used */
63#ifdef FEAT_EVAL
64 int use_debug_break_level;
65#endif
66#ifdef FEAT_WINDOWS
67 int window_count; /* number of windows to use */
68 int vert_windows; /* "-O" used instead of "-o" */
69#endif
70
71#ifdef FEAT_CLIENTSERVER
Bram Moolenaar58d98232005-07-23 22:25:46 +000072 int serverArg; /* TRUE when argument for a server */
73 char_u *serverName_arg; /* cmdline arg for server name */
Bram Moolenaarc013cb62005-07-24 21:18:31 +000074 char_u *serverStr; /* remote server command */
75 char_u *serverStrEnc; /* encoding of serverStr */
76 char_u *servername; /* allocated name for our server */
77#endif
78#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
79 int literal; /* don't expand file names */
80#endif
81#ifdef MSWIN
82 int full_path; /* file name argument was full path */
83#endif
84#ifdef FEAT_DIFF
85 int diff_mode; /* start with 'diff' set */
86#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +000087} mparm_T;
88
Bram Moolenaarc013cb62005-07-24 21:18:31 +000089/* Values for edit_type. */
90#define EDIT_NONE 0 /* no edit type yet */
91#define EDIT_FILE 1 /* file name argument[s] given, use argument list */
92#define EDIT_STDIN 2 /* read file from stdin */
93#define EDIT_TAG 3 /* tag name argument given, use tagname */
94#define EDIT_QF 4 /* start in quickfix mode */
95
Bram Moolenaarb4210b32004-06-13 14:51:16 +000096#if defined(UNIX) || defined(VMS)
97static int file_owned __ARGS((char *fname));
98#endif
99static void mainerr __ARGS((int, char_u *));
100static 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 Moolenaarc013cb62005-07-24 21:18:31 +0000103#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
104static void init_locale __ARGS((void));
105#endif
106static 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));
112#ifdef FEAT_WINDOWS
113static void edit_buffers __ARGS((mparm_T *parmp));
114#endif
115static 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 Moolenaard5bc83f2005-12-07 21:07:59 +0000119#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000120static void check_swap_exists_action __ARGS((void));
121#endif
122#ifdef FEAT_CLIENTSERVER
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));
127#endif
128
129
130#ifdef STARTUPTIME
131static FILE *time_fd = NULL;
132#endif
133
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000134/*
135 * Different types of error messages.
136 */
137static char *(main_errors[]) =
138{
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000139 N_("Unknown option argument"),
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000140#define ME_UNKNOWN_OPTION 0
141 N_("Too many edit arguments"),
142#define ME_TOO_MANY_ARGS 1
143 N_("Argument missing after"),
144#define ME_ARG_MISSING 2
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000145 N_("Garbage after option argument"),
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000146#define ME_GARBAGE 3
147 N_("Too many \"+command\", \"-c command\" or \"--cmd command\" arguments"),
148#define ME_EXTRA_CMD 4
149 N_("Invalid argument for"),
150#define ME_INVALID_ARG 5
151};
152
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000153#ifndef PROTO /* don't want a prototype for main() */
154 int
155# ifdef VIMDLL
156_export
157# endif
158# ifdef FEAT_GUI_MSWIN
159# ifdef __BORLANDC__
160_cdecl
161# endif
162VimMain
163# else
164main
165# endif
166(argc, argv)
167 int argc;
168 char **argv;
169{
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000170 char_u *fname = NULL; /* file name from command line */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000171 mparm_T params; /* various parameters passed between
172 * main() and other functions. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000173
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000174 /*
175 * Do any system-specific initialisations. These can NOT use IObuff or
176 * NameBuff. Thus emsg2() cannot be called!
177 */
178 mch_early_init();
179
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000180 /* Many variables are in "params" so that we can pass them to invoked
181 * functions without a lot of arguments. "argc" and "argv" are also
182 * copied, so that they can be changed. */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000183 vim_memset(&params, 0, sizeof(params));
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000184 params.argc = argc;
185 params.argv = argv;
186 params.want_full_screen = TRUE;
187#ifdef FEAT_EVAL
188 params.use_debug_break_level = -1;
189#endif
190#ifdef FEAT_WINDOWS
191 params.window_count = -1;
192 params.vert_windows = MAYBE;
193#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +0000194
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000195#ifdef FEAT_TCL
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000196 vim_tcl_init(params.argv[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000197#endif
198
199#ifdef MEM_PROFILE
200 atexit(vim_mem_profile_dump);
201#endif
202
203#ifdef STARTUPTIME
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000204 time_fd = mch_fopen(STARTUPTIME, "a");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000205 TIME_MSG("--- VIM STARTING ---");
206#endif
207
208#ifdef __EMX__
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000209 _wildcard(&params.argc, &params.argv);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000210#endif
211
212#ifdef FEAT_MBYTE
213 (void)mb_init(); /* init mb_bytelen_tab[] to ones */
214#endif
Bram Moolenaardcaf10e2005-01-21 11:55:25 +0000215#ifdef FEAT_EVAL
216 eval_init(); /* init global variables */
217#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000218
219#ifdef __QNXNTO__
220 qnx_init(); /* PhAttach() for clipboard, (and gui) */
221#endif
222
223#ifdef MAC_OS_CLASSIC
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000224 /* Prepare for possibly starting GUI sometime */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000225 /* Macintosh needs this before any memory is allocated. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000226 gui_prepare(&params.argc, params.argv);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000227 TIME_MSG("GUI prepared");
228#endif
229
230 /* Init the table of Normal mode commands. */
231 init_normal_cmds();
232
233#if defined(HAVE_DATE_TIME) && defined(VMS) && defined(VAXC)
Bram Moolenaar58d98232005-07-23 22:25:46 +0000234 make_version(); /* Construct the long version string. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000235#endif
236
237 /*
238 * Allocate space for the generic buffers (needed for set_init_1() and
239 * EMSG2()).
240 */
241 if ((IObuff = alloc(IOSIZE)) == NULL
242 || (NameBuff = alloc(MAXPATHL)) == NULL)
243 mch_exit(0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000244 TIME_MSG("Allocated generic buffers");
245
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000246#ifdef NBDEBUG
247 /* Wait a moment for debugging NetBeans. Must be after allocating
248 * NameBuff. */
249 nbdebug_log_init("SPRO_GVIM_DEBUG", "SPRO_GVIM_DLEVEL");
250 nbdebug_wait(WT_ENV | WT_WAIT | WT_STOP, "SPRO_GVIM_WAIT", 20);
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000251 TIME_MSG("NetBeans debug wait");
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000252#endif
253
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000254#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
255 /*
256 * Setup to use the current locale (for ctype() and many other things).
257 * NOTE: Translated messages with encodings other than latin1 will not
258 * work until set_init_1() has been called!
259 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000260 init_locale();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000261 TIME_MSG("locale set");
262#endif
263
264#ifdef FEAT_GUI
265 gui.dofork = TRUE; /* default is to use fork() */
266#endif
267
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000268 /*
Bram Moolenaar58d98232005-07-23 22:25:46 +0000269 * Do a first scan of the arguments in "argv[]":
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000270 * -display or --display
Bram Moolenaar58d98232005-07-23 22:25:46 +0000271 * --server...
272 * --socketid
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000273 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000274 early_arg_scan(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000275
276#ifdef FEAT_SUN_WORKSHOP
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000277 findYourself(params.argv[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000278#endif
279#if defined(FEAT_GUI) && !defined(MAC_OS_CLASSIC)
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000280 /* Prepare for possibly starting GUI sometime */
281 gui_prepare(&params.argc, params.argv);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000282 TIME_MSG("GUI prepared");
283#endif
284
285#ifdef FEAT_CLIPBOARD
286 clip_init(FALSE); /* Initialise clipboard stuff */
287 TIME_MSG("clipboard setup");
288#endif
289
290 /*
291 * Check if we have an interactive window.
292 * On the Amiga: If there is no window, we open one with a newcli command
293 * (needed for :! to * work). mch_check_win() will also handle the -d or
294 * -dev argument.
295 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000296 params.stdout_isatty = (mch_check_win(params.argc, params.argv) != FAIL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000297 TIME_MSG("window checked");
298
299 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000300 * Allocate the first window and buffer.
301 * Can't do anything without it, exit when it fails.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000302 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000303 if (win_alloc_first() == FAIL)
304 mch_exit(0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000305
306 init_yank(); /* init yank buffers */
307
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000308 alist_init(&global_alist); /* Init the argument list to empty. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000309
310 /*
311 * Set the default values for the options.
312 * NOTE: Non-latin1 translated messages are working only after this,
313 * because this is where "has_mbyte" will be set, which is used by
314 * msg_outtrans_len_attr().
315 * First find out the home directory, needed to expand "~" in options.
316 */
317 init_homedir(); /* find real value of $HOME */
318 set_init_1();
319 TIME_MSG("inits 1");
320
321#ifdef FEAT_EVAL
322 set_lang_var(); /* set v:lang and v:ctype */
323#endif
324
325#ifdef FEAT_CLIENTSERVER
326 /*
327 * Do the client-server stuff, unless "--servername ''" was used.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000328 * This may exit Vim if the command was sent to the server.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000329 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000330 exec_on_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000331#endif
332
333 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000334 * Figure out the way to work from the command name argv[0].
335 * "vimdiff" starts diff mode, "rvim" sets "restricted", etc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000336 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000337 parse_command_name(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000338
339 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000340 * Process the command line arguments. File names are put in the global
341 * argument list "global_alist".
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000342 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000343 command_line_scan(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000344 TIME_MSG("parsing arguments");
345
346 /*
347 * On some systems, when we compile with the GUI, we always use it. On Mac
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000348 * there is no terminal version, and on Windows we can't fork one off with
349 * :gui.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000350 */
351#ifdef ALWAYS_USE_GUI
352 gui.starting = TRUE;
353#else
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000354# if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000355 /*
356 * Check if the GUI can be started. Reset gui.starting if not.
357 * Don't know about other systems, stay on the safe side and don't check.
358 */
359 if (gui.starting && gui_init_check() == FAIL)
360 {
361 gui.starting = FALSE;
362
363 /* When running "evim" or "gvim -y" we need the menus, exit if we
364 * don't have them. */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000365 if (params.evim_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000366 mch_exit(1);
367 }
368# endif
369#endif
370
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000371 if (GARGCOUNT > 0)
372 {
373#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
374 /*
375 * Expand wildcards in file names.
376 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000377 if (!params.literal)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000378 {
379 /* Temporarily add '(' and ')' to 'isfname'. These are valid
380 * filename characters but are excluded from 'isfname' to make
381 * "gf" work on a file name in parenthesis (e.g.: see vim.h). */
382 do_cmdline_cmd((char_u *)":set isf+=(,)");
Bram Moolenaar86b68352004-12-27 21:59:20 +0000383 alist_expand(NULL, 0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000384 do_cmdline_cmd((char_u *)":set isf&");
385 }
386#endif
387 fname = alist_name(&GARGLIST[0]);
388 }
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000389
390#if defined(WIN32) && defined(FEAT_MBYTE)
391 {
392 extern void set_alist_count(void);
393
394 /* Remember the number of entries in the argument list. If it changes
395 * we don't react on setting 'encoding'. */
396 set_alist_count();
397 }
398#endif
399
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000400#ifdef MSWIN
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000401 if (GARGCOUNT == 1 && params.full_path)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000402 {
403 /*
404 * If there is one filename, fully qualified, we have very probably
405 * been invoked from explorer, so change to the file's directory.
406 * Hint: to avoid this when typing a command use a forward slash.
407 * If the cd fails, it doesn't matter.
408 */
409 (void)vim_chdirfile(fname);
410 }
411#endif
412 TIME_MSG("expanding arguments");
413
414#ifdef FEAT_DIFF
Bram Moolenaar231334e2005-07-25 20:46:57 +0000415 if (params.diff_mode)
416 {
417 if (params.window_count == -1)
418 params.window_count = 0; /* open up to 3 windows */
419 if (params.vert_windows == MAYBE)
420 params.vert_windows = TRUE; /* use vertical split */
421 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000422#endif
423
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000424 /* Don't redraw until much later. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000425 ++RedrawingDisabled;
426
427 /*
428 * When listing swap file names, don't do cursor positioning et. al.
429 */
430 if (recoverymode && fname == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000431 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000432
433 /*
434 * When certain to start the GUI, don't check capabilities of terminal.
435 * For GTK we can't be sure, but when started from the desktop it doesn't
436 * make sense to try using a terminal.
437 */
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000438#if defined(ALWAYS_USE_GUI) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000439 if (gui.starting
440# ifdef FEAT_GUI_GTK
441 && !isatty(2)
442# endif
443 )
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000444 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000445#endif
446
447#if defined(FEAT_GUI_MAC) && defined(MACOS_X_UNIX)
448 /* When the GUI is started from Finder, need to display messages in a
449 * message box. isatty(2) returns TRUE anyway, thus we need to check the
450 * name to know we're not started from a terminal. */
451 if (gui.starting && (!isatty(2) || strcmp("/dev/console", ttyname(2)) == 0))
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000452 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000453#endif
454
455 /*
456 * mch_init() sets up the terminal (window) for use. This must be
457 * done after resetting full_screen, otherwise it may move the cursor
458 * (MSDOS).
459 * Note that we may use mch_exit() before mch_init()!
460 */
461 mch_init();
462 TIME_MSG("shell init");
463
464#ifdef USE_XSMP
465 /*
466 * For want of anywhere else to do it, try to connect to xsmp here.
467 * Fitting it in after gui_mch_init, but before gui_init (via termcapinit).
468 * Hijacking -X 'no X connection' to also disable XSMP connection as that
469 * has a similar delay upon failure.
470 * Only try if SESSION_MANAGER is set to something non-null.
471 */
472 if (!x_no_connect)
473 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000474 char *p = getenv("SESSION_MANAGER");
475
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000476 if (p != NULL && *p != NUL)
477 {
478 xsmp_init();
479 TIME_MSG("xsmp init");
480 }
481 }
482#endif
483
484 /*
485 * Print a warning if stdout is not a terminal.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000486 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000487 check_tty(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000488
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000489 /* This message comes before term inits, but after setting "silent_mode"
490 * when the input is not a tty. */
491 if (GARGCOUNT > 1 && !silent_mode)
492 printf(_("%d files to edit\n"), GARGCOUNT);
493
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000494 if (params.want_full_screen && !silent_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000495 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000496 termcapinit(params.term); /* set terminal name and get terminal
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000497 capabilities (will set full_screen) */
498 screen_start(); /* don't know where cursor is now */
499 TIME_MSG("Termcap init");
500 }
501
502 /*
503 * Set the default values for the options that use Rows and Columns.
504 */
505 ui_get_shellsize(); /* inits Rows and Columns */
506#ifdef FEAT_NETBEANS_INTG
507 if (usingNetbeans)
508 Columns += 2; /* leave room for glyph gutter */
509#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000510 win_init_size();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000511#ifdef FEAT_DIFF
512 /* Set the 'diff' option now, so that it can be checked for in a .vimrc
513 * file. There is no buffer yet though. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000514 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000515 diff_win_options(firstwin, FALSE);
516#endif
517
518 cmdline_row = Rows - p_ch;
519 msg_row = cmdline_row;
520 screenalloc(FALSE); /* allocate screen buffers */
521 set_init_2();
522 TIME_MSG("inits 2");
523
524 msg_scroll = TRUE;
525 no_wait_return = TRUE;
526
527 init_mappings(); /* set up initial mappings */
528
529 init_highlight(TRUE, FALSE); /* set the default highlight groups */
530 TIME_MSG("init highlight");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000531
532#ifdef FEAT_EVAL
533 /* Set the break level after the terminal is initialized. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000534 debug_break_level = params.use_debug_break_level;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000535#endif
536
Bram Moolenaar58d98232005-07-23 22:25:46 +0000537 /* Execute --cmd arguments. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000538 exe_pre_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000539
Bram Moolenaar58d98232005-07-23 22:25:46 +0000540 /* Source startup scripts. */
541 source_startup_scripts(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000542
543#ifdef FEAT_EVAL
544 /*
545 * Read all the plugin files.
546 * Only when compiled with +eval, since most plugins need it.
547 */
548 if (p_lpl)
549 {
Bram Moolenaar07d4d732005-10-03 22:04:08 +0000550 source_runtime((char_u *)"plugin/**/*.vim", TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000551 TIME_MSG("loading plugins");
552 }
553#endif
554
555 /*
556 * Recovery mode without a file name: List swap files.
557 * This uses the 'dir' option, therefore it must be after the
558 * initializations.
559 */
560 if (recoverymode && fname == NULL)
561 {
562 recover_names(NULL, TRUE, 0);
563 mch_exit(0);
564 }
565
566 /*
567 * Set a few option defaults after reading .vimrc files:
568 * 'title' and 'icon', Unix: 'shellpipe' and 'shellredir'.
569 */
570 set_init_3();
571 TIME_MSG("inits 3");
572
573 /*
574 * "-n" argument: Disable swap file by setting 'updatecount' to 0.
575 * Note that this overrides anything from a vimrc file.
576 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000577 if (params.no_swap_file)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000578 p_uc = 0;
579
580#ifdef FEAT_FKMAP
581 if (curwin->w_p_rl && p_altkeymap)
582 {
583 p_hkmap = FALSE; /* Reset the Hebrew keymap mode */
584# ifdef FEAT_ARABIC
585 curwin->w_p_arab = FALSE; /* Reset the Arabic keymap mode */
586# endif
587 p_fkmap = TRUE; /* Set the Farsi keymap mode */
588 }
589#endif
590
591#ifdef FEAT_GUI
592 if (gui.starting)
593 {
594#if defined(UNIX) || defined(VMS)
595 /* When something caused a message from a vimrc script, need to output
596 * an extra newline before the shell prompt. */
597 if (did_emsg || msg_didout)
598 putchar('\n');
599#endif
600
601 gui_start(); /* will set full_screen to TRUE */
602 TIME_MSG("starting GUI");
603
604 /* When running "evim" or "gvim -y" we need the menus, exit if we
605 * don't have them. */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000606 if (!gui.in_use && params.evim_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000607 mch_exit(1);
608 }
609#endif
610
611#ifdef SPAWNO /* special MSDOS swapping library */
612 init_SPAWNO("", SWAP_ANY);
613#endif
614
615#ifdef FEAT_VIMINFO
616 /*
617 * Read in registers, history etc, but not marks, from the viminfo file
618 */
619 if (*p_viminfo != NUL)
620 {
621 read_viminfo(NULL, TRUE, FALSE, FALSE);
622 TIME_MSG("reading viminfo");
623 }
624#endif
625
626#ifdef FEAT_QUICKFIX
627 /*
628 * "-q errorfile": Load the error file now.
629 * If the error file can't be read, exit before doing anything else.
630 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000631 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000632 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000633 if (params.use_ef != NULL)
634 set_string_option_direct((char_u *)"ef", -1,
635 params.use_ef, OPT_FREE);
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000636 if (qf_init(NULL, p_ef, p_efm, TRUE) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000637 {
638 out_char('\n');
639 mch_exit(3);
640 }
641 TIME_MSG("reading errorfile");
642 }
643#endif
644
645 /*
646 * Start putting things on the screen.
647 * Scroll screen down before drawing over it
648 * Clear screen now, so file message will not be cleared.
649 */
650 starting = NO_BUFFERS;
651 no_wait_return = FALSE;
652 if (!exmode_active)
653 msg_scroll = FALSE;
654
655#ifdef FEAT_GUI
656 /*
657 * This seems to be required to make callbacks to be called now, instead
658 * of after things have been put on the screen, which then may be deleted
659 * when getting a resize callback.
660 * For the Mac this handles putting files dropped on the Vim icon to
661 * global_alist.
662 */
663 if (gui.in_use)
664 {
665# ifdef FEAT_SUN_WORKSHOP
666 if (!usingSunWorkShop)
667# endif
668 gui_wait_for_chars(50L);
669 TIME_MSG("GUI delay");
670 }
671#endif
672
673#if defined(FEAT_GUI_PHOTON) && defined(FEAT_CLIPBOARD)
674 qnx_clip_init();
675#endif
676
677#ifdef FEAT_XCLIPBOARD
678 /* Start using the X clipboard, unless the GUI was started. */
679# ifdef FEAT_GUI
680 if (!gui.in_use)
681# endif
682 {
683 setup_term_clip();
684 TIME_MSG("setup clipboard");
685 }
686#endif
687
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000688#ifdef FEAT_CLIENTSERVER
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000689 /* Prepare for being a Vim server. */
690 prepare_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000691#endif
692
693 /*
694 * If "-" argument given: Read file from stdin.
695 * Do this before starting Raw mode, because it may change things that the
696 * writing end of the pipe doesn't like, e.g., in case stdin and stderr
697 * are the same terminal: "cat | vim -".
698 * Using autocommands here may cause trouble...
699 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000700 if (params.edit_type == EDIT_STDIN && !recoverymode)
701 read_stdin();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000702
703#if defined(UNIX) || defined(VMS)
704 /* When switching screens and something caused a message from a vimrc
705 * script, need to output an extra newline on exit. */
706 if ((did_emsg || msg_didout) && *T_TI != NUL)
707 newline_on_exit = TRUE;
708#endif
709
710 /*
711 * When done something that is not allowed or error message call
712 * wait_return. This must be done before starttermcap(), because it may
713 * switch to another screen. It must be done after settmode(TMODE_RAW),
714 * because we want to react on a single key stroke.
715 * Call settmode and starttermcap here, so the T_KS and T_TI may be
716 * defined by termcapinit and redifined in .exrc.
717 */
718 settmode(TMODE_RAW);
719 TIME_MSG("setting raw mode");
720
721 if (need_wait_return || msg_didany)
722 {
723 wait_return(TRUE);
724 TIME_MSG("waiting for return");
725 }
726
727 starttermcap(); /* start termcap if not done by wait_return() */
728 TIME_MSG("start termcap");
729
730#ifdef FEAT_MOUSE
731 setmouse(); /* may start using the mouse */
732#endif
733 if (scroll_region)
734 scroll_region_reset(); /* In case Rows changed */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000735 scroll_start(); /* may scroll the screen to the right position */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000736
737 /*
738 * Don't clear the screen when starting in Ex mode, unless using the GUI.
739 */
740 if (exmode_active
741#ifdef FEAT_GUI
742 && !gui.in_use
743#endif
744 )
745 must_redraw = CLEAR;
746 else
747 {
748 screenclear(); /* clear screen */
749 TIME_MSG("clearing screen");
750 }
751
752#ifdef FEAT_CRYPT
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000753 if (params.ask_for_key)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000754 {
755 (void)get_crypt_key(TRUE, TRUE);
756 TIME_MSG("getting crypt key");
757 }
758#endif
759
760 no_wait_return = TRUE;
761
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000762 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000763 * Create the requested number of windows and edit buffers in them.
764 * Also does recovery if "recoverymode" set.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000765 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000766 create_windows(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000767 TIME_MSG("opening buffers");
768
769 /* Ex starts at last line of the file */
770 if (exmode_active)
771 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
772
773#ifdef FEAT_AUTOCMD
774 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
775 TIME_MSG("BufEnter autocommands");
776#endif
777 setpcmark();
778
779#ifdef FEAT_QUICKFIX
780 /*
781 * When started with "-q errorfile" jump to first error now.
782 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000783 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000784 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000785 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000786 TIME_MSG("jump to first error");
787 }
788#endif
789
790#ifdef FEAT_WINDOWS
791 /*
792 * If opened more than one window, start editing files in the other
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000793 * windows.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000794 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000795 edit_buffers(&params);
796#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000797
798#ifdef FEAT_DIFF
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000799 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000800 {
801 win_T *wp;
802
803 /* set options in each window for "vimdiff". */
804 for (wp = firstwin; wp != NULL; wp = wp->w_next)
805 diff_win_options(wp, TRUE);
806 }
807#endif
808
809 /*
810 * Shorten any of the filenames, but only when absolute.
811 */
812 shorten_fnames(FALSE);
813
814 /*
815 * Need to jump to the tag before executing the '-c command'.
816 * Makes "vim -c '/return' -t main" work.
817 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000818 if (params.tagname != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000819 {
Bram Moolenaar146522e2005-12-16 21:55:46 +0000820#if defined(HAS_SWAP_EXISTS_ACTION)
821 swap_exists_did_quit = FALSE;
822#endif
823
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000824 vim_snprintf((char *)IObuff, IOSIZE, "ta %s", params.tagname);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000825 do_cmdline_cmd(IObuff);
826 TIME_MSG("jumping to tag");
Bram Moolenaar146522e2005-12-16 21:55:46 +0000827
828#if defined(HAS_SWAP_EXISTS_ACTION)
829 /* If the user doesn't want to edit the file then we quit here. */
830 if (swap_exists_did_quit)
831 getout(1);
832#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000833 }
834
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000835 /* Execute any "+", "-c" and "-S" arguments. */
836 if (params.n_commands > 0)
837 exe_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000838
839 RedrawingDisabled = 0;
840 redraw_all_later(NOT_VALID);
841 no_wait_return = FALSE;
842 starting = 0;
843
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000844#ifdef FEAT_TERMRESPONSE
845 /* Requesting the termresponse is postponed until here, so that a "-c q"
846 * argument doesn't make it appear in the shell Vim was started from. */
847 may_req_termresponse();
848#endif
849
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000850 /* start in insert mode */
851 if (p_im)
852 need_start_insertmode = TRUE;
853
854#ifdef FEAT_AUTOCMD
855 apply_autocmds(EVENT_VIMENTER, NULL, NULL, FALSE, curbuf);
856 TIME_MSG("VimEnter autocommands");
857#endif
858
859#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
860 /* When a startup script or session file setup for diff'ing and
861 * scrollbind, sync the scrollbind now. */
862 if (curwin->w_p_diff && curwin->w_p_scb)
863 {
864 update_topline();
865 check_scrollbind((linenr_T)0, 0L);
866 TIME_MSG("diff scrollbinding");
867 }
868#endif
869
870#if defined(WIN3264) && !defined(FEAT_GUI_W32)
871 mch_set_winsize_now(); /* Allow winsize changes from now on */
872#endif
873
874 /* If ":startinsert" command used, stuff a dummy command to be able to
875 * call normal_cmd(), which will then start Insert mode. */
876 if (restart_edit != 0)
Bram Moolenaarebefac62005-12-28 22:39:57 +0000877 stuffcharReadbuff(K_NOP);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000878
879#ifdef FEAT_NETBEANS_INTG
880 if (usingNetbeans)
881 /* Tell the client that it can start sending commands. */
882 netbeans_startup_done();
883#endif
884
885 TIME_MSG("before starting main loop");
886
887 /*
888 * Call the main command loop. This never returns.
889 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000890 main_loop(FALSE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000891
892 return 0;
893}
894#endif /* PROTO */
895
896/*
897 * Main loop: Execute Normal mode commands until exiting Vim.
898 * Also used to handle commands in the command-line window, until the window
899 * is closed.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000900 * Also used to handle ":visual" command after ":global": execute Normal mode
901 * commands, return when entering Ex mode. "noexmode" is TRUE then.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000902 */
903 void
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000904main_loop(cmdwin, noexmode)
905 int cmdwin; /* TRUE when working in the command-line window */
906 int noexmode; /* TRUE when return on entering Ex mode */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000907{
908 oparg_T oa; /* operator arguments */
909
910#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
911 /* Setup to catch a terminating error from the X server. Just ignore
912 * it, restore the state and continue. This might not always work
913 * properly, but at least we don't exit unexpectedly when the X server
914 * exists while Vim is running in a console. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000915 if (!cmdwin && !noexmode && SETJMP(x_jump_env))
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000916 {
917 State = NORMAL;
918# ifdef FEAT_VISUAL
919 VIsual_active = FALSE;
920# endif
921 got_int = TRUE;
922 need_wait_return = FALSE;
923 global_busy = FALSE;
924 exmode_active = 0;
925 skip_redraw = FALSE;
926 RedrawingDisabled = 0;
927 no_wait_return = 0;
928# ifdef FEAT_EVAL
929 emsg_skip = 0;
930# endif
931 emsg_off = 0;
932# ifdef FEAT_MOUSE
933 setmouse();
934# endif
935 settmode(TMODE_RAW);
936 starttermcap();
937 scroll_start();
938 redraw_later_clear();
939 }
940#endif
941
942 clear_oparg(&oa);
943 while (!cmdwin
944#ifdef FEAT_CMDWIN
945 || cmdwin_result == 0
946#endif
947 )
948 {
949 if (stuff_empty())
950 {
951 did_check_timestamps = FALSE;
952 if (need_check_timestamps)
953 check_timestamps(FALSE);
954 if (need_wait_return) /* if wait_return still needed ... */
955 wait_return(FALSE); /* ... call it now */
956 if (need_start_insertmode && goto_im()
957#ifdef FEAT_VISUAL
958 && !VIsual_active
959#endif
960 )
961 {
962 need_start_insertmode = FALSE;
963 stuffReadbuff((char_u *)"i"); /* start insert mode next */
964 /* skip the fileinfo message now, because it would be shown
965 * after insert mode finishes! */
966 need_fileinfo = FALSE;
967 }
968 }
969 if (got_int && !global_busy)
970 {
971 if (!quit_more)
972 (void)vgetc(); /* flush all buffers */
973 got_int = FALSE;
974 }
975 if (!exmode_active)
976 msg_scroll = FALSE;
977 quit_more = FALSE;
978
979 /*
980 * If skip redraw is set (for ":" in wait_return()), don't redraw now.
981 * If there is nothing in the stuff_buffer or do_redraw is TRUE,
982 * update cursor and redraw.
983 */
984 if (skip_redraw || exmode_active)
985 skip_redraw = FALSE;
986 else if (do_redraw || stuff_empty())
987 {
Bram Moolenaar3d0a6032006-02-09 23:54:54 +0000988#ifdef FEAT_AUTOCMD
989 /* Trigger CursorMoved if the cursor moved. */
990 if (!finish_op && has_cursormoved()
991 && !equalpos(last_cursormoved, curwin->w_cursor))
992
993 {
994 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL, FALSE, curbuf);
995 last_cursormoved = curwin->w_cursor;
996 }
997#endif
998
Bram Moolenaar33aec762006-01-22 23:30:12 +0000999#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
1000 /* Scroll-binding for diff mode may have been postponed until
1001 * here. Avoids doing it for every change. */
1002 if (diff_need_scrollbind)
1003 {
1004 check_scrollbind((linenr_T)0, 0L);
1005 diff_need_scrollbind = FALSE;
1006 }
1007#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001008#if defined(FEAT_FOLDING) && defined(FEAT_VISUAL)
1009 /* Include a closed fold completely in the Visual area. */
1010 foldAdjustVisual();
1011#endif
1012#ifdef FEAT_FOLDING
1013 /*
1014 * When 'foldclose' is set, apply 'foldlevel' to folds that don't
1015 * contain the cursor.
1016 * When 'foldopen' is "all", open the fold(s) under the cursor.
1017 * This may mark the window for redrawing.
1018 */
1019 if (hasAnyFolding(curwin) && !char_avail())
1020 {
1021 foldCheckClose();
1022 if (fdo_flags & FDO_ALL)
1023 foldOpenCursor();
1024 }
1025#endif
1026
1027 /*
1028 * Before redrawing, make sure w_topline is correct, and w_leftcol
1029 * if lines don't wrap, and w_skipcol if lines wrap.
1030 */
1031 update_topline();
1032 validate_cursor();
1033
1034#ifdef FEAT_VISUAL
1035 if (VIsual_active)
1036 update_curbuf(INVERTED);/* update inverted part */
1037 else
1038#endif
1039 if (must_redraw)
1040 update_screen(0);
1041 else if (redraw_cmdline || clear_cmdline)
1042 showmode();
1043#ifdef FEAT_WINDOWS
1044 redraw_statuslines();
1045#endif
1046#ifdef FEAT_TITLE
1047 if (need_maketitle)
1048 maketitle();
1049#endif
1050 /* display message after redraw */
1051 if (keep_msg != NULL)
1052 {
1053 char_u *p;
1054
1055 /* msg_attr_keep() will set keep_msg to NULL, must free the
1056 * string here. */
1057 p = keep_msg;
1058 msg_attr(p, keep_msg_attr);
1059 vim_free(p);
1060 }
1061 if (need_fileinfo) /* show file info after redraw */
1062 {
1063 fileinfo(FALSE, TRUE, FALSE);
1064 need_fileinfo = FALSE;
1065 }
1066
1067 emsg_on_display = FALSE; /* can delete error message now */
1068 did_emsg = FALSE;
1069 msg_didany = FALSE; /* reset lines_left in msg_start() */
Bram Moolenaar661b1822005-07-28 22:36:45 +00001070 may_clear_sb_text(); /* clear scroll-back text on next msg */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001071 showruler(FALSE);
1072
1073 setcursor();
1074 cursor_on();
1075
1076 do_redraw = FALSE;
1077 }
1078#ifdef FEAT_GUI
1079 if (need_mouse_correct)
1080 gui_mouse_correct();
1081#endif
1082
1083 /*
1084 * Update w_curswant if w_set_curswant has been set.
1085 * Postponed until here to avoid computing w_virtcol too often.
1086 */
1087 update_curswant();
1088
1089 /*
1090 * If we're invoked as ex, do a round of ex commands.
1091 * Otherwise, get and execute a normal mode command.
1092 */
1093 if (exmode_active)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001094 {
1095 if (noexmode) /* End of ":global/path/visual" commands */
1096 return;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001097 do_exmode(exmode_active == EXMODE_VIM);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001098 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001099 else
1100 normal_cmd(&oa, TRUE);
1101 }
1102}
1103
1104
1105#if defined(USE_XSMP) || defined(FEAT_GUI_MSWIN) || defined(PROTO)
1106/*
1107 * Exit, but leave behind swap files for modified buffers.
1108 */
1109 void
1110getout_preserve_modified(exitval)
1111 int exitval;
1112{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001113# if defined(SIGHUP) && defined(SIG_IGN)
1114 /* Ignore SIGHUP, because a dropped connection causes a read error, which
1115 * makes Vim exit and then handling SIGHUP causes various reentrance
1116 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001117 signal(SIGHUP, SIG_IGN);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001118# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001119
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001120 ml_close_notmod(); /* close all not-modified buffers */
1121 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
1122 ml_close_all(FALSE); /* close all memfiles, without deleting */
1123 getout(exitval); /* exit Vim properly */
1124}
1125#endif
1126
1127
1128/* Exit properly */
1129 void
1130getout(exitval)
1131 int exitval;
1132{
1133#ifdef FEAT_AUTOCMD
1134 buf_T *buf;
1135 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001136 tabpage_T *tp, *next_tp;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001137#endif
1138
1139 exiting = TRUE;
1140
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001141 /* When running in Ex mode an error causes us to exit with a non-zero exit
1142 * code. POSIX requires this, although it's not 100% clear from the
1143 * standard. */
1144 if (exmode_active)
1145 exitval += ex_exitval;
1146
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001147 /* Position the cursor on the last screen line, below all the text */
1148#ifdef FEAT_GUI
1149 if (!gui.in_use)
1150#endif
1151 windgoto((int)Rows - 1, 0);
1152
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +00001153#if defined(FEAT_EVAL) || defined(FEAT_SYN_HL)
1154 /* Optionally print hashtable efficiency. */
1155 hash_debug_results();
1156#endif
1157
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001158#ifdef FEAT_GUI
1159 msg_didany = FALSE;
1160#endif
1161
1162#ifdef FEAT_AUTOCMD
1163 /* Trigger BufWinLeave for all windows, but only once per buffer. */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001164# if defined FEAT_WINDOWS
1165 for (tp = first_tabpage; tp != NULL; tp = next_tp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001166 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001167 next_tp = tp->tp_next;
1168 for (wp = (tp->tp_topframe == topframe)
1169 ? firstwin : tp->tp_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001170 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001171 buf = wp->w_buffer;
1172 if (buf->b_changedtick != -1)
1173 {
1174 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001175 FALSE, buf);
Bram Moolenaarf740b292006-02-16 22:11:02 +00001176 buf->b_changedtick = -1; /* note that we did it already */
1177 /* start all over, autocommands may mess up the lists */
1178 next_tp = first_tabpage;
1179 break;
1180 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001181 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001182 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001183# else
1184 apply_autocmds(EVENT_BUFWINLEAVE, curbuf, curbuf->b_fname, FALSE, curbuf);
1185# endif
Bram Moolenaar33aec762006-01-22 23:30:12 +00001186
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001187 /* Trigger BufUnload for buffers that are loaded */
1188 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1189 if (buf->b_ml.ml_mfp != NULL)
1190 {
1191 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
1192 FALSE, buf);
1193 if (!buf_valid(buf)) /* autocmd may delete the buffer */
1194 break;
1195 }
1196 apply_autocmds(EVENT_VIMLEAVEPRE, NULL, NULL, FALSE, curbuf);
1197#endif
1198
1199#ifdef FEAT_VIMINFO
1200 if (*p_viminfo != NUL)
1201 /* Write out the registers, history, marks etc, to the viminfo file */
1202 write_viminfo(NULL, FALSE);
1203#endif
1204
1205#ifdef FEAT_AUTOCMD
1206 apply_autocmds(EVENT_VIMLEAVE, NULL, NULL, FALSE, curbuf);
1207#endif
1208
Bram Moolenaar05159a02005-02-26 23:04:13 +00001209#ifdef FEAT_PROFILE
1210 profile_dump();
1211#endif
1212
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001213 if (did_emsg
1214#ifdef FEAT_GUI
1215 || (gui.in_use && msg_didany && p_verbose > 0)
1216#endif
1217 )
1218 {
1219 /* give the user a chance to read the (error) message */
1220 no_wait_return = FALSE;
1221 wait_return(FALSE);
1222 }
1223
1224#ifdef FEAT_AUTOCMD
1225 /* Position the cursor again, the autocommands may have moved it */
1226# ifdef FEAT_GUI
1227 if (!gui.in_use)
1228# endif
1229 windgoto((int)Rows - 1, 0);
1230#endif
1231
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001232#ifdef FEAT_MZSCHEME
1233 mzscheme_end();
1234#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001235#ifdef FEAT_TCL
1236 tcl_end();
1237#endif
1238#ifdef FEAT_RUBY
1239 ruby_end();
1240#endif
1241#ifdef FEAT_PYTHON
1242 python_end();
1243#endif
1244#ifdef FEAT_PERL
1245 perl_end();
1246#endif
1247#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
1248 iconv_end();
1249#endif
1250#ifdef FEAT_NETBEANS_INTG
1251 netbeans_end();
1252#endif
1253
1254 mch_exit(exitval);
1255}
1256
1257/*
1258 * Get a (optional) count for a Vim argument.
1259 */
1260 static int
1261get_number_arg(p, idx, def)
1262 char_u *p; /* pointer to argument */
1263 int *idx; /* index in argument, is incremented */
1264 int def; /* default value */
1265{
1266 if (vim_isdigit(p[*idx]))
1267 {
1268 def = atoi((char *)&(p[*idx]));
1269 while (vim_isdigit(p[*idx]))
1270 *idx = *idx + 1;
1271 }
1272 return def;
1273}
1274
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001275#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1276/*
1277 * Setup to use the current locale (for ctype() and many other things).
1278 */
1279 static void
1280init_locale()
1281{
1282 setlocale(LC_ALL, "");
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001283# ifdef WIN32
1284 /* Apparently MS-Windows printf() may cause a crash when we give it 8-bit
1285 * text while it's expecting text in the current locale. This call avoids
1286 * that. */
1287 setlocale(LC_CTYPE, "C");
1288# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001289
1290# ifdef FEAT_GETTEXT
1291 {
1292 int mustfree = FALSE;
1293 char_u *p;
1294
1295# ifdef DYNAMIC_GETTEXT
1296 /* Initialize the gettext library */
1297 dyn_libintl_init(NULL);
1298# endif
1299 /* expand_env() doesn't work yet, because chartab[] is not initialized
1300 * yet, call vim_getenv() directly */
1301 p = vim_getenv((char_u *)"VIMRUNTIME", &mustfree);
1302 if (p != NULL && *p != NUL)
1303 {
1304 STRCPY(NameBuff, p);
1305 STRCAT(NameBuff, "/lang");
1306 bindtextdomain(VIMPACKAGE, (char *)NameBuff);
1307 }
1308 if (mustfree)
1309 vim_free(p);
1310 textdomain(VIMPACKAGE);
1311 }
1312# endif
1313}
1314#endif
1315
1316/*
1317 * Check for: [r][e][g][vi|vim|view][diff][ex[im]]
1318 * If the executable name starts with "r" we disable shell commands.
1319 * If the next character is "e" we run in Easy mode.
1320 * If the next character is "g" we run the GUI version.
1321 * If the next characters are "view" we start in readonly mode.
1322 * If the next characters are "diff" or "vimdiff" we start in diff mode.
1323 * If the next characters are "ex" we start in Ex mode. If it's followed
1324 * by "im" use improved Ex mode.
1325 */
1326 static void
1327parse_command_name(parmp)
1328 mparm_T *parmp;
1329{
1330 char_u *initstr;
1331
1332 initstr = gettail((char_u *)parmp->argv[0]);
1333
1334#ifdef MACOS_X_UNIX
1335 /* An issue has been seen when launching Vim in such a way that
1336 * $PWD/$ARGV[0] or $ARGV[0] is not the absolute path to the
1337 * executable or a symbolic link of it. Until this issue is resolved
1338 * we prohibit the GUI from being used.
1339 */
1340 if (STRCMP(initstr, parmp->argv[0]) == 0)
1341 disallow_gui = TRUE;
1342
1343 /* TODO: On MacOS X default to gui if argv[0] ends in:
1344 * /vim.app/Contents/MacOS/Vim */
1345#endif
1346
1347#ifdef FEAT_EVAL
1348 set_vim_var_string(VV_PROGNAME, initstr, -1);
1349#endif
1350
1351 if (TOLOWER_ASC(initstr[0]) == 'r')
1352 {
1353 restricted = TRUE;
1354 ++initstr;
1355 }
1356
1357 /* Avoid using evim mode for "editor". */
1358 if (TOLOWER_ASC(initstr[0]) == 'e'
1359 && (TOLOWER_ASC(initstr[1]) == 'v'
1360 || TOLOWER_ASC(initstr[1]) == 'g'))
1361 {
1362#ifdef FEAT_GUI
1363 gui.starting = TRUE;
1364#endif
1365 parmp->evim_mode = TRUE;
1366 ++initstr;
1367 }
1368
1369 if (TOLOWER_ASC(initstr[0]) == 'g' || initstr[0] == 'k')
1370 {
1371 main_start_gui();
1372#ifdef FEAT_GUI
1373 ++initstr;
1374#endif
1375 }
1376
1377 if (STRNICMP(initstr, "view", 4) == 0)
1378 {
1379 readonlymode = TRUE;
1380 curbuf->b_p_ro = TRUE;
1381 p_uc = 10000; /* don't update very often */
1382 initstr += 4;
1383 }
1384 else if (STRNICMP(initstr, "vim", 3) == 0)
1385 initstr += 3;
1386
1387 /* Catch "[r][g]vimdiff" and "[r][g]viewdiff". */
1388 if (STRICMP(initstr, "diff") == 0)
1389 {
1390#ifdef FEAT_DIFF
1391 parmp->diff_mode = TRUE;
1392#else
1393 mch_errmsg(_("This Vim was not compiled with the diff feature."));
1394 mch_errmsg("\n");
1395 mch_exit(2);
1396#endif
1397 }
1398
1399 if (STRNICMP(initstr, "ex", 2) == 0)
1400 {
1401 if (STRNICMP(initstr + 2, "im", 2) == 0)
1402 exmode_active = EXMODE_VIM;
1403 else
1404 exmode_active = EXMODE_NORMAL;
1405 change_compatible(TRUE); /* set 'compatible' */
1406 }
1407}
1408
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001409/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00001410 * Get the name of the display, before gui_prepare() removes it from
1411 * argv[]. Used for the xterm-clipboard display.
1412 *
1413 * Also find the --server... arguments and --socketid
1414 */
1415/*ARGSUSED*/
1416 static void
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001417early_arg_scan(parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001418 mparm_T *parmp;
1419{
1420#if defined(FEAT_XCLIPBOARD) || defined(FEAT_CLIENTSERVER)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001421 int argc = parmp->argc;
1422 char **argv = parmp->argv;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001423 int i;
1424
1425 for (i = 1; i < argc; i++)
1426 {
1427 if (STRCMP(argv[i], "--") == 0)
1428 break;
1429# ifdef FEAT_XCLIPBOARD
1430 else if (STRICMP(argv[i], "-display") == 0
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001431# if defined(FEAT_GUI_GTK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001432 || STRICMP(argv[i], "--display") == 0
1433# endif
1434 )
1435 {
1436 if (i == argc - 1)
1437 mainerr_arg_missing((char_u *)argv[i]);
1438 xterm_display = argv[++i];
1439 }
1440# endif
1441# ifdef FEAT_CLIENTSERVER
1442 else if (STRICMP(argv[i], "--servername") == 0)
1443 {
1444 if (i == argc - 1)
1445 mainerr_arg_missing((char_u *)argv[i]);
1446 parmp->serverName_arg = (char_u *)argv[++i];
1447 }
1448 else if (STRICMP(argv[i], "--serverlist") == 0
1449 || STRICMP(argv[i], "--remote-send") == 0
1450 || STRICMP(argv[i], "--remote-expr") == 0
1451 || STRICMP(argv[i], "--remote") == 0
1452 || STRICMP(argv[i], "--remote-silent") == 0)
1453 parmp->serverArg = TRUE;
1454 else if (STRICMP(argv[i], "--remote-wait") == 0
1455 || STRICMP(argv[i], "--remote-wait-silent") == 0)
1456 {
1457 parmp->serverArg = TRUE;
1458#ifdef FEAT_GUI
1459 /* don't fork() when starting the GUI to edit the files ourself */
1460 gui.dofork = FALSE;
1461#endif
1462 }
1463# endif
1464# ifdef FEAT_GUI_GTK
1465 else if (STRICMP(argv[i], "--socketid") == 0)
1466 {
1467 unsigned int socket_id;
1468 int count;
1469
1470 if (i == argc - 1)
1471 mainerr_arg_missing((char_u *)argv[i]);
1472 if (STRNICMP(argv[i+1], "0x", 2) == 0)
1473 count = sscanf(&(argv[i + 1][2]), "%x", &socket_id);
1474 else
1475 count = sscanf(argv[i+1], "%u", &socket_id);
1476 if (count != 1)
1477 mainerr(ME_INVALID_ARG, (char_u *)argv[i]);
1478 else
1479 gtk_socket_id = socket_id;
1480 i++;
1481 }
1482 else if (STRICMP(argv[i], "--echo-wid") == 0)
1483 echo_wid_arg = TRUE;
1484# endif
1485 }
1486#endif
1487}
1488
1489/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001490 * Scan the command line arguments.
1491 */
1492 static void
1493command_line_scan(parmp)
1494 mparm_T *parmp;
1495{
1496 int argc = parmp->argc;
1497 char **argv = parmp->argv;
1498 int argv_idx; /* index in argv[n][] */
1499 int had_minmin = FALSE; /* found "--" argument */
1500 int want_argument; /* option argument with argument */
1501 int c;
Bram Moolenaar231334e2005-07-25 20:46:57 +00001502 char_u *p = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001503 long n;
1504
1505 --argc;
1506 ++argv;
1507 argv_idx = 1; /* active option letter is argv[0][argv_idx] */
1508 while (argc > 0)
1509 {
1510 /*
1511 * "+" or "+{number}" or "+/{pat}" or "+{command}" argument.
1512 */
1513 if (argv[0][0] == '+' && !had_minmin)
1514 {
1515 if (parmp->n_commands >= MAX_ARG_CMDS)
1516 mainerr(ME_EXTRA_CMD, NULL);
1517 argv_idx = -1; /* skip to next argument */
1518 if (argv[0][1] == NUL)
1519 parmp->commands[parmp->n_commands++] = (char_u *)"$";
1520 else
1521 parmp->commands[parmp->n_commands++] = (char_u *)&(argv[0][1]);
1522 }
1523
1524 /*
1525 * Optional argument.
1526 */
1527 else if (argv[0][0] == '-' && !had_minmin)
1528 {
1529 want_argument = FALSE;
1530 c = argv[0][argv_idx++];
1531#ifdef VMS
1532 /*
1533 * VMS only uses upper case command lines. Interpret "-X" as "-x"
1534 * and "-/X" as "-X".
1535 */
1536 if (c == '/')
1537 {
1538 c = argv[0][argv_idx++];
1539 c = TOUPPER_ASC(c);
1540 }
1541 else
1542 c = TOLOWER_ASC(c);
1543#endif
1544 switch (c)
1545 {
1546 case NUL: /* "vim -" read from stdin */
1547 /* "ex -" silent mode */
1548 if (exmode_active)
1549 silent_mode = TRUE;
1550 else
1551 {
1552 if (parmp->edit_type != EDIT_NONE)
1553 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1554 parmp->edit_type = EDIT_STDIN;
1555 read_cmd_fd = 2; /* read from stderr instead of stdin */
1556 }
1557 argv_idx = -1; /* skip to next argument */
1558 break;
1559
1560 case '-': /* "--" don't take any more option arguments */
1561 /* "--help" give help message */
1562 /* "--version" give version message */
1563 /* "--literal" take files literally */
1564 /* "--nofork" don't fork */
1565 /* "--noplugin[s]" skip plugins */
1566 /* "--cmd <cmd>" execute cmd before vimrc */
1567 if (STRICMP(argv[0] + argv_idx, "help") == 0)
1568 usage();
1569 else if (STRICMP(argv[0] + argv_idx, "version") == 0)
1570 {
1571 Columns = 80; /* need to init Columns */
1572 info_message = TRUE; /* use mch_msg(), not mch_errmsg() */
1573 list_version();
1574 msg_putchar('\n');
1575 msg_didout = FALSE;
1576 mch_exit(0);
1577 }
1578 else if (STRNICMP(argv[0] + argv_idx, "literal", 7) == 0)
1579 {
1580#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
1581 parmp->literal = TRUE;
1582#endif
1583 }
1584 else if (STRNICMP(argv[0] + argv_idx, "nofork", 6) == 0)
1585 {
1586#ifdef FEAT_GUI
1587 gui.dofork = FALSE; /* don't fork() when starting GUI */
1588#endif
1589 }
1590 else if (STRNICMP(argv[0] + argv_idx, "noplugin", 8) == 0)
1591 p_lpl = FALSE;
1592 else if (STRNICMP(argv[0] + argv_idx, "cmd", 3) == 0)
1593 {
1594 want_argument = TRUE;
1595 argv_idx += 3;
1596 }
1597#ifdef FEAT_CLIENTSERVER
1598 else if (STRNICMP(argv[0] + argv_idx, "serverlist", 10) == 0)
1599 ; /* already processed -- no arg */
1600 else if (STRNICMP(argv[0] + argv_idx, "servername", 10) == 0
1601 || STRNICMP(argv[0] + argv_idx, "serversend", 10) == 0)
1602 {
1603 /* already processed -- snatch the following arg */
1604 if (argc > 1)
1605 {
1606 --argc;
1607 ++argv;
1608 }
1609 }
1610#endif
1611#ifdef FEAT_GUI_GTK
1612 else if (STRNICMP(argv[0] + argv_idx, "socketid", 8) == 0)
1613 {
1614 /* already processed -- snatch the following arg */
1615 if (argc > 1)
1616 {
1617 --argc;
1618 ++argv;
1619 }
1620 }
1621 else if (STRNICMP(argv[0] + argv_idx, "echo-wid", 8) == 0)
1622 {
1623 /* already processed, skip */
1624 }
1625#endif
1626 else
1627 {
1628 if (argv[0][argv_idx])
1629 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
1630 had_minmin = TRUE;
1631 }
1632 if (!want_argument)
1633 argv_idx = -1; /* skip to next argument */
1634 break;
1635
1636 case 'A': /* "-A" start in Arabic mode */
1637#ifdef FEAT_ARABIC
1638 set_option_value((char_u *)"arabic", 1L, NULL, 0);
1639#else
1640 mch_errmsg(_(e_noarabic));
1641 mch_exit(2);
1642#endif
1643 break;
1644
1645 case 'b': /* "-b" binary mode */
Bram Moolenaar231334e2005-07-25 20:46:57 +00001646 /* Needs to be effective before expanding file names, because
1647 * for Win32 this makes us edit a shortcut file itself,
1648 * instead of the file it links to. */
1649 set_options_bin(curbuf->b_p_bin, 1, 0);
1650 curbuf->b_p_bin = 1; /* binary file I/O */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001651 break;
1652
1653 case 'C': /* "-C" Compatible */
1654 change_compatible(TRUE);
1655 break;
1656
1657 case 'e': /* "-e" Ex mode */
1658 exmode_active = EXMODE_NORMAL;
1659 break;
1660
1661 case 'E': /* "-E" Improved Ex mode */
1662 exmode_active = EXMODE_VIM;
1663 break;
1664
1665 case 'f': /* "-f" GUI: run in foreground. Amiga: open
1666 window directly, not with newcli */
1667#ifdef FEAT_GUI
1668 gui.dofork = FALSE; /* don't fork() when starting GUI */
1669#endif
1670 break;
1671
1672 case 'g': /* "-g" start GUI */
1673 main_start_gui();
1674 break;
1675
1676 case 'F': /* "-F" start in Farsi mode: rl + fkmap set */
1677#ifdef FEAT_FKMAP
1678 curwin->w_p_rl = p_fkmap = TRUE;
1679#else
1680 mch_errmsg(_(e_nofarsi));
1681 mch_exit(2);
1682#endif
1683 break;
1684
1685 case 'h': /* "-h" give help message */
1686#ifdef FEAT_GUI_GNOME
1687 /* Tell usage() to exit for "gvim". */
1688 gui.starting = FALSE;
1689#endif
1690 usage();
1691 break;
1692
1693 case 'H': /* "-H" start in Hebrew mode: rl + hkmap set */
1694#ifdef FEAT_RIGHTLEFT
1695 curwin->w_p_rl = p_hkmap = TRUE;
1696#else
1697 mch_errmsg(_(e_nohebrew));
1698 mch_exit(2);
1699#endif
1700 break;
1701
1702 case 'l': /* "-l" lisp mode, 'lisp' and 'showmatch' on */
1703#ifdef FEAT_LISP
1704 set_option_value((char_u *)"lisp", 1L, NULL, 0);
1705 p_sm = TRUE;
1706#endif
1707 break;
1708
1709#ifdef TARGET_API_MAC_OSX
1710 /* For some reason on MacOS X, an argument like:
1711 -psn_0_10223617 is passed in when invoke from Finder
1712 or with the 'open' command */
1713 case 'p':
1714 argv_idx = -1; /* bypass full -psn */
1715 main_start_gui();
1716 break;
1717#endif
1718 case 'M': /* "-M" no changes or writing of files */
1719 reset_modifiable();
1720 /* FALLTHROUGH */
1721
1722 case 'm': /* "-m" no writing of files */
1723 p_write = FALSE;
1724 break;
1725
1726 case 'y': /* "-y" easy mode */
1727#ifdef FEAT_GUI
1728 gui.starting = TRUE; /* start GUI a bit later */
1729#endif
1730 parmp->evim_mode = TRUE;
1731 break;
1732
1733 case 'N': /* "-N" Nocompatible */
1734 change_compatible(FALSE);
1735 break;
1736
1737 case 'n': /* "-n" no swap file */
1738 parmp->no_swap_file = TRUE;
1739 break;
1740
1741 case 'o': /* "-o[N]" open N horizontal split windows */
1742#ifdef FEAT_WINDOWS
1743 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00001744 parmp->window_count = get_number_arg((char_u *)argv[0],
1745 &argv_idx, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001746 parmp->vert_windows = FALSE;
1747#endif
1748 break;
1749
1750 case 'O': /* "-O[N]" open N vertical split windows */
1751#if defined(FEAT_VERTSPLIT) && defined(FEAT_WINDOWS)
1752 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00001753 parmp->window_count = get_number_arg((char_u *)argv[0],
1754 &argv_idx, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001755 parmp->vert_windows = TRUE;
1756#endif
1757 break;
1758
1759#ifdef FEAT_QUICKFIX
1760 case 'q': /* "-q" QuickFix mode */
1761 if (parmp->edit_type != EDIT_NONE)
1762 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1763 parmp->edit_type = EDIT_QF;
1764 if (argv[0][argv_idx]) /* "-q{errorfile}" */
1765 {
1766 parmp->use_ef = (char_u *)argv[0] + argv_idx;
1767 argv_idx = -1;
1768 }
1769 else if (argc > 1) /* "-q {errorfile}" */
1770 want_argument = TRUE;
1771 break;
1772#endif
1773
1774 case 'R': /* "-R" readonly mode */
1775 readonlymode = TRUE;
1776 curbuf->b_p_ro = TRUE;
1777 p_uc = 10000; /* don't update very often */
1778 break;
1779
1780 case 'r': /* "-r" recovery mode */
1781 case 'L': /* "-L" recovery mode */
1782 recoverymode = 1;
1783 break;
1784
1785 case 's':
1786 if (exmode_active) /* "-s" silent (batch) mode */
1787 silent_mode = TRUE;
1788 else /* "-s {scriptin}" read from script file */
1789 want_argument = TRUE;
1790 break;
1791
1792 case 't': /* "-t {tag}" or "-t{tag}" jump to tag */
1793 if (parmp->edit_type != EDIT_NONE)
1794 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1795 parmp->edit_type = EDIT_TAG;
1796 if (argv[0][argv_idx]) /* "-t{tag}" */
1797 {
1798 parmp->tagname = (char_u *)argv[0] + argv_idx;
1799 argv_idx = -1;
1800 }
1801 else /* "-t {tag}" */
1802 want_argument = TRUE;
1803 break;
1804
1805#ifdef FEAT_EVAL
1806 case 'D': /* "-D" Debugging */
1807 parmp->use_debug_break_level = 9999;
1808 break;
1809#endif
1810#ifdef FEAT_DIFF
1811 case 'd': /* "-d" 'diff' */
1812# ifdef AMIGA
1813 /* check for "-dev {device}" */
1814 if (argv[0][argv_idx] == 'e' && argv[0][argv_idx + 1] == 'v')
1815 want_argument = TRUE;
1816 else
1817# endif
1818 parmp->diff_mode = TRUE;
1819 break;
1820#endif
1821 case 'V': /* "-V{N}" Verbose level */
1822 /* default is 10: a little bit verbose */
1823 p_verbose = get_number_arg((char_u *)argv[0], &argv_idx, 10);
1824 if (argv[0][argv_idx] != NUL)
1825 {
1826 set_option_value((char_u *)"verbosefile", 0L,
1827 (char_u *)argv[0] + argv_idx, 0);
1828 argv_idx = STRLEN(argv[0]);
1829 }
1830 break;
1831
1832 case 'v': /* "-v" Vi-mode (as if called "vi") */
1833 exmode_active = 0;
1834#ifdef FEAT_GUI
1835 gui.starting = FALSE; /* don't start GUI */
1836#endif
1837 break;
1838
1839 case 'w': /* "-w{number}" set window height */
1840 /* "-w {scriptout}" write to script */
1841 if (vim_isdigit(((char_u *)argv[0])[argv_idx]))
1842 {
1843 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
1844 set_option_value((char_u *)"window", n, NULL, 0);
1845 break;
1846 }
1847 want_argument = TRUE;
1848 break;
1849
1850#ifdef FEAT_CRYPT
1851 case 'x': /* "-x" encrypted reading/writing of files */
1852 parmp->ask_for_key = TRUE;
1853 break;
1854#endif
1855
1856 case 'X': /* "-X" don't connect to X server */
1857#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
1858 x_no_connect = TRUE;
1859#endif
1860 break;
1861
1862 case 'Z': /* "-Z" restricted mode */
1863 restricted = TRUE;
1864 break;
1865
1866 case 'c': /* "-c{command}" or "-c {command}" execute
1867 command */
1868 if (argv[0][argv_idx] != NUL)
1869 {
1870 if (parmp->n_commands >= MAX_ARG_CMDS)
1871 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar231334e2005-07-25 20:46:57 +00001872 parmp->commands[parmp->n_commands++] = (char_u *)argv[0]
1873 + argv_idx;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001874 argv_idx = -1;
1875 break;
1876 }
1877 /*FALLTHROUGH*/
1878 case 'S': /* "-S {file}" execute Vim script */
1879 case 'i': /* "-i {viminfo}" use for viminfo */
1880#ifndef FEAT_DIFF
1881 case 'd': /* "-d {device}" device (for Amiga) */
1882#endif
1883 case 'T': /* "-T {terminal}" terminal name */
1884 case 'u': /* "-u {vimrc}" vim inits file */
1885 case 'U': /* "-U {gvimrc}" gvim inits file */
1886 case 'W': /* "-W {scriptout}" overwrite */
1887#ifdef FEAT_GUI_W32
1888 case 'P': /* "-P {parent title}" MDI parent */
1889#endif
1890 want_argument = TRUE;
1891 break;
1892
1893 default:
1894 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
1895 }
1896
1897 /*
1898 * Handle option arguments with argument.
1899 */
1900 if (want_argument)
1901 {
1902 /*
1903 * Check for garbage immediately after the option letter.
1904 */
1905 if (argv[0][argv_idx] != NUL)
1906 mainerr(ME_GARBAGE, (char_u *)argv[0]);
1907
1908 --argc;
1909 if (argc < 1 && c != 'S')
1910 mainerr_arg_missing((char_u *)argv[0]);
1911 ++argv;
1912 argv_idx = -1;
1913
1914 switch (c)
1915 {
1916 case 'c': /* "-c {command}" execute command */
1917 case 'S': /* "-S {file}" execute Vim script */
1918 if (parmp->n_commands >= MAX_ARG_CMDS)
1919 mainerr(ME_EXTRA_CMD, NULL);
1920 if (c == 'S')
1921 {
1922 char *a;
1923
1924 if (argc < 1)
1925 /* "-S" without argument: use default session file
1926 * name. */
1927 a = SESSION_FILE;
1928 else if (argv[0][0] == '-')
1929 {
1930 /* "-S" followed by another option: use default
1931 * session file name. */
1932 a = SESSION_FILE;
1933 ++argc;
1934 --argv;
1935 }
1936 else
1937 a = argv[0];
1938 p = alloc((unsigned)(STRLEN(a) + 4));
1939 if (p == NULL)
1940 mch_exit(2);
1941 sprintf((char *)p, "so %s", a);
1942 parmp->cmds_tofree[parmp->n_commands] = TRUE;
1943 parmp->commands[parmp->n_commands++] = p;
1944 }
1945 else
Bram Moolenaar231334e2005-07-25 20:46:57 +00001946 parmp->commands[parmp->n_commands++] =
1947 (char_u *)argv[0];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001948 break;
1949
1950 case '-': /* "--cmd {command}" execute command */
1951 if (parmp->n_pre_commands >= MAX_ARG_CMDS)
1952 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar231334e2005-07-25 20:46:57 +00001953 parmp->pre_commands[parmp->n_pre_commands++] =
1954 (char_u *)argv[0];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001955 break;
1956
1957 /* case 'd': -d {device} is handled in mch_check_win() for the
1958 * Amiga */
1959
1960#ifdef FEAT_QUICKFIX
1961 case 'q': /* "-q {errorfile}" QuickFix mode */
1962 parmp->use_ef = (char_u *)argv[0];
1963 break;
1964#endif
1965
1966 case 'i': /* "-i {viminfo}" use for viminfo */
1967 use_viminfo = (char_u *)argv[0];
1968 break;
1969
1970 case 's': /* "-s {scriptin}" read from script file */
1971 if (scriptin[0] != NULL)
1972 {
1973scripterror:
1974 mch_errmsg(_("Attempt to open script file again: \""));
1975 mch_errmsg(argv[-1]);
1976 mch_errmsg(" ");
1977 mch_errmsg(argv[0]);
1978 mch_errmsg("\"\n");
1979 mch_exit(2);
1980 }
1981 if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL)
1982 {
1983 mch_errmsg(_("Cannot open for reading: \""));
1984 mch_errmsg(argv[0]);
1985 mch_errmsg("\"\n");
1986 mch_exit(2);
1987 }
1988 if (save_typebuf() == FAIL)
1989 mch_exit(2); /* out of memory */
1990 break;
1991
1992 case 't': /* "-t {tag}" */
1993 parmp->tagname = (char_u *)argv[0];
1994 break;
1995
1996 case 'T': /* "-T {terminal}" terminal name */
1997 /*
1998 * The -T term argument is always available and when
1999 * HAVE_TERMLIB is supported it overrides the environment
2000 * variable TERM.
2001 */
2002#ifdef FEAT_GUI
2003 if (term_is_gui((char_u *)argv[0]))
2004 gui.starting = TRUE; /* start GUI a bit later */
2005 else
2006#endif
2007 parmp->term = (char_u *)argv[0];
2008 break;
2009
2010 case 'u': /* "-u {vimrc}" vim inits file */
2011 parmp->use_vimrc = (char_u *)argv[0];
2012 break;
2013
2014 case 'U': /* "-U {gvimrc}" gvim inits file */
2015#ifdef FEAT_GUI
2016 use_gvimrc = (char_u *)argv[0];
2017#endif
2018 break;
2019
2020 case 'w': /* "-w {nr}" 'window' value */
2021 /* "-w {scriptout}" append to script file */
2022 if (vim_isdigit(*((char_u *)argv[0])))
2023 {
2024 argv_idx = 0;
2025 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2026 set_option_value((char_u *)"window", n, NULL, 0);
2027 argv_idx = -1;
2028 break;
2029 }
2030 /*FALLTHROUGH*/
2031 case 'W': /* "-W {scriptout}" overwrite script file */
2032 if (scriptout != NULL)
2033 goto scripterror;
2034 if ((scriptout = mch_fopen(argv[0],
2035 c == 'w' ? APPENDBIN : WRITEBIN)) == NULL)
2036 {
2037 mch_errmsg(_("Cannot open for script output: \""));
2038 mch_errmsg(argv[0]);
2039 mch_errmsg("\"\n");
2040 mch_exit(2);
2041 }
2042 break;
2043
2044#ifdef FEAT_GUI_W32
2045 case 'P': /* "-P {parent title}" MDI parent */
2046 gui_mch_set_parent(argv[0]);
2047 break;
2048#endif
2049 }
2050 }
2051 }
2052
2053 /*
2054 * File name argument.
2055 */
2056 else
2057 {
2058 argv_idx = -1; /* skip to next argument */
2059
2060 /* Check for only one type of editing. */
2061 if (parmp->edit_type != EDIT_NONE && parmp->edit_type != EDIT_FILE)
2062 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2063 parmp->edit_type = EDIT_FILE;
2064
2065#ifdef MSWIN
2066 /* Remember if the argument was a full path before changing
2067 * slashes to backslashes. */
2068 if (argv[0][0] != NUL && argv[0][1] == ':' && argv[0][2] == '\\')
2069 parmp->full_path = TRUE;
2070#endif
2071
2072 /* Add the file to the global argument list. */
2073 if (ga_grow(&global_alist.al_ga, 1) == FAIL
2074 || (p = vim_strsave((char_u *)argv[0])) == NULL)
2075 mch_exit(2);
2076#ifdef FEAT_DIFF
2077 if (parmp->diff_mode && mch_isdir(p) && GARGCOUNT > 0
2078 && !mch_isdir(alist_name(&GARGLIST[0])))
2079 {
2080 char_u *r;
2081
2082 r = concat_fnames(p, gettail(alist_name(&GARGLIST[0])), TRUE);
2083 if (r != NULL)
2084 {
2085 vim_free(p);
2086 p = r;
2087 }
2088 }
2089#endif
2090#if defined(__CYGWIN32__) && !defined(WIN32)
2091 /*
2092 * If vim is invoked by non-Cygwin tools, convert away any
2093 * DOS paths, so things like .swp files are created correctly.
2094 * Look for evidence of non-Cygwin paths before we bother.
2095 * This is only for when using the Unix files.
2096 */
2097 if (strpbrk(p, "\\:") != NULL)
2098 {
2099 char posix_path[PATH_MAX];
2100
2101 cygwin_conv_to_posix_path(p, posix_path);
2102 vim_free(p);
2103 p = vim_strsave(posix_path);
2104 if (p == NULL)
2105 mch_exit(2);
2106 }
2107#endif
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002108
2109#ifdef USE_FNAME_CASE
2110 /* Make the case of the file name match the actual file. */
2111 fname_case(p, 0);
2112#endif
2113
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002114 alist_add(&global_alist, p,
2115#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
Bram Moolenaar231334e2005-07-25 20:46:57 +00002116 parmp->literal ? 2 : 0 /* add buffer nr after exp. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002117#else
2118 2 /* add buffer number now and use curbuf */
2119#endif
2120 );
2121
2122#if defined(FEAT_MBYTE) && defined(WIN32)
2123 {
2124 extern void used_file_arg(char *, int, int);
2125
2126 /* Remember this argument has been added to the argument list.
2127 * Needed when 'encoding' is changed. */
2128 used_file_arg(argv[0], parmp->literal, parmp->full_path);
2129 }
2130#endif
2131 }
2132
2133 /*
2134 * If there are no more letters after the current "-", go to next
2135 * argument. argv_idx is set to -1 when the current argument is to be
2136 * skipped.
2137 */
2138 if (argv_idx <= 0 || argv[0][argv_idx] == NUL)
2139 {
2140 --argc;
2141 ++argv;
2142 argv_idx = 1;
2143 }
2144 }
2145}
2146
2147/*
2148 * Print a warning if stdout is not a terminal.
2149 * When starting in Ex mode and commands come from a file, set Silent mode.
2150 */
2151 static void
2152check_tty(parmp)
2153 mparm_T *parmp;
2154{
2155 int input_isatty; /* is active input a terminal? */
2156
2157 input_isatty = mch_input_isatty();
2158 if (exmode_active)
2159 {
2160 if (!input_isatty)
2161 silent_mode = TRUE;
2162 }
2163 else if (parmp->want_full_screen && (!parmp->stdout_isatty || !input_isatty)
2164#ifdef FEAT_GUI
2165 /* don't want the delay when started from the desktop */
2166 && !gui.starting
2167#endif
2168 )
2169 {
2170#ifdef NBDEBUG
2171 /*
2172 * This shouldn't be necessary. But if I run netbeans with the log
2173 * output coming to the console and XOpenDisplay fails, I get vim
2174 * trying to start with input/output to my console tty. This fills my
2175 * input buffer so fast I can't even kill the process in under 2
2176 * minutes (and it beeps continuosly the whole time :-)
2177 */
2178 if (usingNetbeans && (!parmp->stdout_isatty || !input_isatty))
2179 {
2180 mch_errmsg(_("Vim: Error: Failure to start gvim from NetBeans\n"));
2181 exit(1);
2182 }
2183#endif
2184 if (!parmp->stdout_isatty)
2185 mch_errmsg(_("Vim: Warning: Output is not to a terminal\n"));
2186 if (!input_isatty)
2187 mch_errmsg(_("Vim: Warning: Input is not from a terminal\n"));
2188 out_flush();
2189 if (scriptin[0] == NULL)
2190 ui_delay(2000L, TRUE);
2191 TIME_MSG("Warning delay");
2192 }
2193}
2194
2195/*
2196 * Read text from stdin.
2197 */
2198 static void
2199read_stdin()
2200{
2201 int i;
2202
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002203#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002204 /* When getting the ATTENTION prompt here, use a dialog */
2205 swap_exists_action = SEA_DIALOG;
2206#endif
2207 no_wait_return = TRUE;
2208 i = msg_didany;
2209 set_buflisted(TRUE);
2210 (void)open_buffer(TRUE, NULL); /* create memfile and read file */
2211 no_wait_return = FALSE;
2212 msg_didany = i;
2213 TIME_MSG("reading stdin");
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002214#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002215 check_swap_exists_action();
2216#endif
2217#if !(defined(AMIGA) || defined(MACOS))
2218 /*
2219 * Close stdin and dup it from stderr. Required for GPM to work
2220 * properly, and for running external commands.
2221 * Is there any other system that cannot do this?
2222 */
2223 close(0);
2224 dup(2);
2225#endif
2226}
2227
2228/*
2229 * Create the requested number of windows and edit buffers in them.
2230 * Also does recovery if "recoverymode" set.
2231 */
2232/*ARGSUSED*/
2233 static void
2234create_windows(parmp)
2235 mparm_T *parmp;
2236{
2237#ifdef FEAT_WINDOWS
2238 /*
2239 * Create the number of windows that was requested.
2240 */
2241 if (parmp->window_count == -1) /* was not set */
2242 parmp->window_count = 1;
2243 if (parmp->window_count == 0)
2244 parmp->window_count = GARGCOUNT;
2245 if (parmp->window_count > 1)
2246 {
2247 /* Don't change the windows if there was a command in .vimrc that
2248 * already split some windows */
2249 if (parmp->vert_windows == MAYBE)
2250 parmp->vert_windows = FALSE;
2251 if (firstwin->w_next == NULL)
2252 {
2253 parmp->window_count = make_windows(parmp->window_count,
2254 parmp->vert_windows);
2255 TIME_MSG("making windows");
2256 }
2257 else
2258 parmp->window_count = win_count();
2259 }
2260 else
2261 parmp->window_count = 1;
2262#endif
2263
2264 if (recoverymode) /* do recover */
2265 {
2266 msg_scroll = TRUE; /* scroll message up */
2267 ml_recover();
2268 if (curbuf->b_ml.ml_mfp == NULL) /* failed */
2269 getout(1);
2270 do_modelines(FALSE); /* do modelines */
2271 }
2272 else
2273 {
2274 /*
2275 * Open a buffer for windows that don't have one yet.
2276 * Commands in the .vimrc might have loaded a file or split the window.
2277 * Watch out for autocommands that delete a window.
2278 */
2279#ifdef FEAT_AUTOCMD
2280 /*
2281 * Don't execute Win/Buf Enter/Leave autocommands here
2282 */
2283 ++autocmd_no_enter;
2284 ++autocmd_no_leave;
2285#endif
2286#ifdef FEAT_WINDOWS
2287 for (curwin = firstwin; curwin != NULL; curwin = W_NEXT(curwin))
2288#endif
2289 {
2290 curbuf = curwin->w_buffer;
2291 if (curbuf->b_ml.ml_mfp == NULL)
2292 {
2293#ifdef FEAT_FOLDING
2294 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2295 if (p_fdls >= 0)
2296 curwin->w_p_fdl = p_fdls;
2297#endif
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002298#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002299 /* When getting the ATTENTION prompt here, use a dialog */
2300 swap_exists_action = SEA_DIALOG;
2301#endif
2302 set_buflisted(TRUE);
2303 (void)open_buffer(FALSE, NULL); /* create memfile, read file */
2304
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002305#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002306 check_swap_exists_action();
2307#endif
2308#ifdef FEAT_AUTOCMD
2309 curwin = firstwin; /* start again */
2310#endif
2311 }
2312#ifdef FEAT_WINDOWS
2313 ui_breakcheck();
2314 if (got_int)
2315 {
2316 (void)vgetc(); /* only break the file loading, not the rest */
2317 break;
2318 }
2319#endif
2320 }
2321#ifdef FEAT_AUTOCMD
2322 --autocmd_no_enter;
2323 --autocmd_no_leave;
2324#endif
2325#ifdef FEAT_WINDOWS
2326 curwin = firstwin;
2327 curbuf = curwin->w_buffer;
2328#endif
2329 }
2330}
2331
2332#ifdef FEAT_WINDOWS
2333 /*
2334 * If opened more than one window, start editing files in the other
2335 * windows. make_windows() has already opened the windows.
2336 */
2337 static void
2338edit_buffers(parmp)
2339 mparm_T *parmp;
2340{
2341 int arg_idx; /* index in argument list */
2342 int i;
2343
2344# ifdef FEAT_AUTOCMD
2345 /*
2346 * Don't execute Win/Buf Enter/Leave autocommands here
2347 */
2348 ++autocmd_no_enter;
2349 ++autocmd_no_leave;
2350# endif
2351 arg_idx = 1;
2352 for (i = 1; i < parmp->window_count; ++i)
2353 {
2354 if (curwin->w_next == NULL) /* just checking */
2355 break;
2356 win_enter(curwin->w_next, FALSE);
2357
2358 /* Only open the file if there is no file in this window yet (that can
2359 * happen when .vimrc contains ":sall") */
2360 if (curbuf == firstwin->w_buffer || curbuf->b_ffname == NULL)
2361 {
2362 curwin->w_arg_idx = arg_idx;
2363 /* edit file from arg list, if there is one */
2364 (void)do_ecmd(0, arg_idx < GARGCOUNT
2365 ? alist_name(&GARGLIST[arg_idx]) : NULL,
2366 NULL, NULL, ECMD_LASTL, ECMD_HIDE);
2367 if (arg_idx == GARGCOUNT - 1)
2368 arg_had_last = TRUE;
2369 ++arg_idx;
2370 }
2371 ui_breakcheck();
2372 if (got_int)
2373 {
2374 (void)vgetc(); /* only break the file loading, not the rest */
2375 break;
2376 }
2377 }
2378# ifdef FEAT_AUTOCMD
2379 --autocmd_no_enter;
2380# endif
2381 win_enter(firstwin, FALSE); /* back to first window */
2382# ifdef FEAT_AUTOCMD
2383 --autocmd_no_leave;
2384# endif
2385 TIME_MSG("editing files in windows");
2386 if (parmp->window_count > 1)
2387 win_equal(curwin, FALSE, 'b'); /* adjust heights */
2388}
2389#endif /* FEAT_WINDOWS */
2390
2391/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002392 * Execute the commands from --cmd arguments "cmds[cnt]".
2393 */
2394 static void
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002395exe_pre_commands(parmp)
2396 mparm_T *parmp;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002397{
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002398 char_u **cmds = parmp->pre_commands;
2399 int cnt = parmp->n_pre_commands;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002400 int i;
2401
2402 if (cnt > 0)
2403 {
2404 curwin->w_cursor.lnum = 0; /* just in case.. */
2405 sourcing_name = (char_u *)_("pre-vimrc command line");
2406# ifdef FEAT_EVAL
2407 current_SID = SID_CMDARG;
2408# endif
2409 for (i = 0; i < cnt; ++i)
2410 do_cmdline_cmd(cmds[i]);
2411 sourcing_name = NULL;
2412# ifdef FEAT_EVAL
2413 current_SID = 0;
2414# endif
2415 TIME_MSG("--cmd commands");
2416 }
2417}
2418
2419/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002420 * Execute "+", "-c" and "-S" arguments.
2421 */
2422 static void
2423exe_commands(parmp)
2424 mparm_T *parmp;
2425{
2426 int i;
2427
2428 /*
2429 * We start commands on line 0, make "vim +/pat file" match a
2430 * pattern on line 1. But don't move the cursor when an autocommand
2431 * with g`" was used.
2432 */
2433 msg_scroll = TRUE;
2434 if (parmp->tagname == NULL && curwin->w_cursor.lnum <= 1)
2435 curwin->w_cursor.lnum = 0;
2436 sourcing_name = (char_u *)"command line";
2437#ifdef FEAT_EVAL
2438 current_SID = SID_CARG;
2439#endif
2440 for (i = 0; i < parmp->n_commands; ++i)
2441 {
2442 do_cmdline_cmd(parmp->commands[i]);
2443 if (parmp->cmds_tofree[i])
2444 vim_free(parmp->commands[i]);
2445 }
2446 sourcing_name = NULL;
2447#ifdef FEAT_EVAL
2448 current_SID = 0;
2449#endif
2450 if (curwin->w_cursor.lnum == 0)
2451 curwin->w_cursor.lnum = 1;
2452
2453 if (!exmode_active)
2454 msg_scroll = FALSE;
2455
2456#ifdef FEAT_QUICKFIX
2457 /* When started with "-q errorfile" jump to first error again. */
2458 if (parmp->edit_type == EDIT_QF)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002459 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002460#endif
2461 TIME_MSG("executing command arguments");
2462}
2463
2464/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002465 * Source startup scripts.
2466 */
2467 static void
2468source_startup_scripts(parmp)
2469 mparm_T *parmp;
2470{
2471 int i;
2472
2473 /*
2474 * For "evim" source evim.vim first of all, so that the user can overrule
2475 * any things he doesn't like.
2476 */
2477 if (parmp->evim_mode)
2478 {
2479 (void)do_source((char_u *)EVIM_FILE, FALSE, FALSE);
2480 TIME_MSG("source evim file");
2481 }
2482
2483 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002484 * If -u argument given, use only the initializations from that file and
Bram Moolenaar58d98232005-07-23 22:25:46 +00002485 * nothing else.
2486 */
2487 if (parmp->use_vimrc != NULL)
2488 {
Bram Moolenaar231334e2005-07-25 20:46:57 +00002489 if (STRCMP(parmp->use_vimrc, "NONE") == 0
2490 || STRCMP(parmp->use_vimrc, "NORC") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002491 {
2492#ifdef FEAT_GUI
2493 if (use_gvimrc == NULL) /* don't load gvimrc either */
2494 use_gvimrc = parmp->use_vimrc;
2495#endif
2496 if (parmp->use_vimrc[2] == 'N')
2497 p_lpl = FALSE; /* don't load plugins either */
2498 }
2499 else
2500 {
2501 if (do_source(parmp->use_vimrc, FALSE, FALSE) != OK)
2502 EMSG2(_("E282: Cannot read from \"%s\""), parmp->use_vimrc);
2503 }
2504 }
2505 else if (!silent_mode)
2506 {
2507#ifdef AMIGA
2508 struct Process *proc = (struct Process *)FindTask(0L);
2509 APTR save_winptr = proc->pr_WindowPtr;
2510
2511 /* Avoid a requester here for a volume that doesn't exist. */
2512 proc->pr_WindowPtr = (APTR)-1L;
2513#endif
2514
2515 /*
2516 * Get system wide defaults, if the file name is defined.
2517 */
2518#ifdef SYS_VIMRC_FILE
2519 (void)do_source((char_u *)SYS_VIMRC_FILE, FALSE, FALSE);
2520#endif
2521
2522 /*
2523 * Try to read initialization commands from the following places:
2524 * - environment variable VIMINIT
2525 * - user vimrc file (s:.vimrc for Amiga, ~/.vimrc otherwise)
2526 * - second user vimrc file ($VIM/.vimrc for Dos)
2527 * - environment variable EXINIT
2528 * - user exrc file (s:.exrc for Amiga, ~/.exrc otherwise)
2529 * - second user exrc file ($VIM/.exrc for Dos)
2530 * The first that exists is used, the rest is ignored.
2531 */
2532 if (process_env((char_u *)"VIMINIT", TRUE) != OK)
2533 {
2534 if (do_source((char_u *)USR_VIMRC_FILE, TRUE, TRUE) == FAIL
2535#ifdef USR_VIMRC_FILE2
2536 && do_source((char_u *)USR_VIMRC_FILE2, TRUE, TRUE) == FAIL
2537#endif
2538#ifdef USR_VIMRC_FILE3
2539 && do_source((char_u *)USR_VIMRC_FILE3, TRUE, TRUE) == FAIL
2540#endif
2541 && process_env((char_u *)"EXINIT", FALSE) == FAIL
2542 && do_source((char_u *)USR_EXRC_FILE, FALSE, FALSE) == FAIL)
2543 {
2544#ifdef USR_EXRC_FILE2
2545 (void)do_source((char_u *)USR_EXRC_FILE2, FALSE, FALSE);
2546#endif
2547 }
2548 }
2549
2550 /*
2551 * Read initialization commands from ".vimrc" or ".exrc" in current
2552 * directory. This is only done if the 'exrc' option is set.
2553 * Because of security reasons we disallow shell and write commands
2554 * now, except for unix if the file is owned by the user or 'secure'
2555 * option has been reset in environment of global ".exrc" or ".vimrc".
2556 * Only do this if VIMRC_FILE is not the same as USR_VIMRC_FILE or
2557 * SYS_VIMRC_FILE.
2558 */
2559 if (p_exrc)
2560 {
2561#if defined(UNIX) || defined(VMS)
2562 /* If ".vimrc" file is not owned by user, set 'secure' mode. */
2563 if (!file_owned(VIMRC_FILE))
2564#endif
2565 secure = p_secure;
2566
2567 i = FAIL;
2568 if (fullpathcmp((char_u *)USR_VIMRC_FILE,
2569 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
2570#ifdef USR_VIMRC_FILE2
2571 && fullpathcmp((char_u *)USR_VIMRC_FILE2,
2572 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
2573#endif
2574#ifdef USR_VIMRC_FILE3
2575 && fullpathcmp((char_u *)USR_VIMRC_FILE3,
2576 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
2577#endif
2578#ifdef SYS_VIMRC_FILE
2579 && fullpathcmp((char_u *)SYS_VIMRC_FILE,
2580 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
2581#endif
2582 )
2583 i = do_source((char_u *)VIMRC_FILE, TRUE, TRUE);
2584
2585 if (i == FAIL)
2586 {
2587#if defined(UNIX) || defined(VMS)
2588 /* if ".exrc" is not owned by user set 'secure' mode */
2589 if (!file_owned(EXRC_FILE))
2590 secure = p_secure;
2591 else
2592 secure = 0;
2593#endif
2594 if ( fullpathcmp((char_u *)USR_EXRC_FILE,
2595 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
2596#ifdef USR_EXRC_FILE2
2597 && fullpathcmp((char_u *)USR_EXRC_FILE2,
2598 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
2599#endif
2600 )
2601 (void)do_source((char_u *)EXRC_FILE, FALSE, FALSE);
2602 }
2603 }
2604 if (secure == 2)
2605 need_wait_return = TRUE;
2606 secure = 0;
2607#ifdef AMIGA
2608 proc->pr_WindowPtr = save_winptr;
2609#endif
2610 }
2611 TIME_MSG("sourcing vimrc file(s)");
2612}
2613
2614/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002615 * Setup to start using the GUI. Exit with an error when not available.
2616 */
2617 static void
2618main_start_gui()
2619{
2620#ifdef FEAT_GUI
2621 gui.starting = TRUE; /* start GUI a bit later */
2622#else
2623 mch_errmsg(_(e_nogvim));
2624 mch_errmsg("\n");
2625 mch_exit(2);
2626#endif
2627}
2628
2629/*
2630 * Get an evironment variable, and execute it as Ex commands.
2631 * Returns FAIL if the environment variable was not executed, OK otherwise.
2632 */
2633 int
2634process_env(env, is_viminit)
2635 char_u *env;
2636 int is_viminit; /* when TRUE, called for VIMINIT */
2637{
2638 char_u *initstr;
2639 char_u *save_sourcing_name;
2640 linenr_T save_sourcing_lnum;
2641#ifdef FEAT_EVAL
2642 scid_T save_sid;
2643#endif
2644
2645 if ((initstr = mch_getenv(env)) != NULL && *initstr != NUL)
2646 {
2647 if (is_viminit)
2648 vimrc_found();
2649 save_sourcing_name = sourcing_name;
2650 save_sourcing_lnum = sourcing_lnum;
2651 sourcing_name = env;
2652 sourcing_lnum = 0;
2653#ifdef FEAT_EVAL
2654 save_sid = current_SID;
2655 current_SID = SID_ENV;
2656#endif
2657 do_cmdline_cmd(initstr);
2658 sourcing_name = save_sourcing_name;
2659 sourcing_lnum = save_sourcing_lnum;
2660#ifdef FEAT_EVAL
2661 current_SID = save_sid;;
2662#endif
2663 return OK;
2664 }
2665 return FAIL;
2666}
2667
2668#if defined(UNIX) || defined(VMS)
2669/*
2670 * Return TRUE if we are certain the user owns the file "fname".
2671 * Used for ".vimrc" and ".exrc".
2672 * Use both stat() and lstat() for extra security.
2673 */
2674 static int
2675file_owned(fname)
2676 char *fname;
2677{
2678 struct stat s;
2679# ifdef UNIX
2680 uid_t uid = getuid();
2681# else /* VMS */
2682 uid_t uid = ((getgid() << 16) | getuid());
2683# endif
2684
2685 return !(mch_stat(fname, &s) != 0 || s.st_uid != uid
2686# ifdef HAVE_LSTAT
2687 || mch_lstat(fname, &s) != 0 || s.st_uid != uid
2688# endif
2689 );
2690}
2691#endif
2692
2693/*
2694 * Give an error message main_errors["n"] and exit.
2695 */
2696 static void
2697mainerr(n, str)
2698 int n; /* one of the ME_ defines */
2699 char_u *str; /* extra argument or NULL */
2700{
2701#if defined(UNIX) || defined(__EMX__) || defined(VMS)
2702 reset_signals(); /* kill us with CTRL-C here, if you like */
2703#endif
2704
2705 mch_errmsg(longVersion);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002706 mch_errmsg("\n");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002707 mch_errmsg(_(main_errors[n]));
2708 if (str != NULL)
2709 {
2710 mch_errmsg(": \"");
2711 mch_errmsg((char *)str);
2712 mch_errmsg("\"");
2713 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002714 mch_errmsg(_("\nMore info with: \"vim -h\"\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002715
2716 mch_exit(1);
2717}
2718
2719 void
2720mainerr_arg_missing(str)
2721 char_u *str;
2722{
2723 mainerr(ME_ARG_MISSING, str);
2724}
2725
2726/*
2727 * print a message with three spaces prepended and '\n' appended.
2728 */
2729 static void
2730main_msg(s)
2731 char *s;
2732{
2733 mch_msg(" ");
2734 mch_msg(s);
2735 mch_msg("\n");
2736}
2737
2738/*
2739 * Print messages for "vim -h" or "vim --help" and exit.
2740 */
2741 static void
2742usage()
2743{
2744 int i;
2745 static char *(use[]) =
2746 {
2747 N_("[file ..] edit specified file(s)"),
2748 N_("- read text from stdin"),
2749 N_("-t tag edit file where tag is defined"),
2750#ifdef FEAT_QUICKFIX
2751 N_("-q [errorfile] edit file with first error")
2752#endif
2753 };
2754
2755#if defined(UNIX) || defined(__EMX__) || defined(VMS)
2756 reset_signals(); /* kill us with CTRL-C here, if you like */
2757#endif
2758
2759 mch_msg(longVersion);
2760 mch_msg(_("\n\nusage:"));
2761 for (i = 0; ; ++i)
2762 {
2763 mch_msg(_(" vim [arguments] "));
2764 mch_msg(_(use[i]));
2765 if (i == (sizeof(use) / sizeof(char_u *)) - 1)
2766 break;
2767 mch_msg(_("\n or:"));
2768 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002769#ifdef VMS
2770 mch_msg(_("where case is ignored prepend / to make flag upper case"));
2771#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002772
2773 mch_msg(_("\n\nArguments:\n"));
2774 main_msg(_("--\t\t\tOnly file names after this"));
2775#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
2776 main_msg(_("--literal\t\tDon't expand wildcards"));
2777#endif
2778#ifdef FEAT_OLE
2779 main_msg(_("-register\t\tRegister this gvim for OLE"));
2780 main_msg(_("-unregister\t\tUnregister gvim for OLE"));
2781#endif
2782#ifdef FEAT_GUI
2783 main_msg(_("-g\t\t\tRun using GUI (like \"gvim\")"));
2784 main_msg(_("-f or --nofork\tForeground: Don't fork when starting GUI"));
2785#endif
2786 main_msg(_("-v\t\t\tVi mode (like \"vi\")"));
2787 main_msg(_("-e\t\t\tEx mode (like \"ex\")"));
2788 main_msg(_("-s\t\t\tSilent (batch) mode (only for \"ex\")"));
2789#ifdef FEAT_DIFF
2790 main_msg(_("-d\t\t\tDiff mode (like \"vimdiff\")"));
2791#endif
2792 main_msg(_("-y\t\t\tEasy mode (like \"evim\", modeless)"));
2793 main_msg(_("-R\t\t\tReadonly mode (like \"view\")"));
2794 main_msg(_("-Z\t\t\tRestricted mode (like \"rvim\")"));
2795 main_msg(_("-m\t\t\tModifications (writing files) not allowed"));
2796 main_msg(_("-M\t\t\tModifications in text not allowed"));
2797 main_msg(_("-b\t\t\tBinary mode"));
2798#ifdef FEAT_LISP
2799 main_msg(_("-l\t\t\tLisp mode"));
2800#endif
2801 main_msg(_("-C\t\t\tCompatible with Vi: 'compatible'"));
2802 main_msg(_("-N\t\t\tNot fully Vi compatible: 'nocompatible'"));
2803 main_msg(_("-V[N]\t\tVerbose level"));
2804 main_msg(_("-D\t\t\tDebugging mode"));
2805 main_msg(_("-n\t\t\tNo swap file, use memory only"));
2806 main_msg(_("-r\t\t\tList swap files and exit"));
2807 main_msg(_("-r (with file name)\tRecover crashed session"));
2808 main_msg(_("-L\t\t\tSame as -r"));
2809#ifdef AMIGA
2810 main_msg(_("-f\t\t\tDon't use newcli to open window"));
2811 main_msg(_("-dev <device>\t\tUse <device> for I/O"));
2812#endif
2813#ifdef FEAT_ARABIC
2814 main_msg(_("-A\t\t\tstart in Arabic mode"));
2815#endif
2816#ifdef FEAT_RIGHTLEFT
2817 main_msg(_("-H\t\t\tStart in Hebrew mode"));
2818#endif
2819#ifdef FEAT_FKMAP
2820 main_msg(_("-F\t\t\tStart in Farsi mode"));
2821#endif
2822 main_msg(_("-T <terminal>\tSet terminal type to <terminal>"));
2823 main_msg(_("-u <vimrc>\t\tUse <vimrc> instead of any .vimrc"));
2824#ifdef FEAT_GUI
2825 main_msg(_("-U <gvimrc>\t\tUse <gvimrc> instead of any .gvimrc"));
2826#endif
2827 main_msg(_("--noplugin\t\tDon't load plugin scripts"));
2828 main_msg(_("-o[N]\t\tOpen N windows (default: one for each file)"));
2829 main_msg(_("-O[N]\t\tLike -o but split vertically"));
2830 main_msg(_("+\t\t\tStart at end of file"));
2831 main_msg(_("+<lnum>\t\tStart at line <lnum>"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002832 main_msg(_("--cmd <command>\tExecute <command> before loading any vimrc file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002833 main_msg(_("-c <command>\t\tExecute <command> after loading the first file"));
2834 main_msg(_("-S <session>\t\tSource file <session> after loading the first file"));
2835 main_msg(_("-s <scriptin>\tRead Normal mode commands from file <scriptin>"));
2836 main_msg(_("-w <scriptout>\tAppend all typed commands to file <scriptout>"));
2837 main_msg(_("-W <scriptout>\tWrite all typed commands to file <scriptout>"));
2838#ifdef FEAT_CRYPT
2839 main_msg(_("-x\t\t\tEdit encrypted files"));
2840#endif
2841#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
2842# if defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)
2843 main_msg(_("-display <display>\tConnect vim to this particular X-server"));
2844# endif
2845 main_msg(_("-X\t\t\tDo not connect to X server"));
2846#endif
2847#ifdef FEAT_CLIENTSERVER
2848 main_msg(_("--remote <files>\tEdit <files> in a Vim server if possible"));
2849 main_msg(_("--remote-silent <files> Same, don't complain if there is no server"));
2850 main_msg(_("--remote-wait <files> As --remote but wait for files to have been edited"));
2851 main_msg(_("--remote-wait-silent <files> Same, don't complain if there is no server"));
2852 main_msg(_("--remote-send <keys>\tSend <keys> to a Vim server and exit"));
2853 main_msg(_("--remote-expr <expr>\tEvaluate <expr> in a Vim server and print result"));
2854 main_msg(_("--serverlist\t\tList available Vim server names and exit"));
2855 main_msg(_("--servername <name>\tSend to/become the Vim server <name>"));
2856#endif
2857#ifdef FEAT_VIMINFO
2858 main_msg(_("-i <viminfo>\t\tUse <viminfo> instead of .viminfo"));
2859#endif
2860 main_msg(_("-h or --help\tPrint Help (this message) and exit"));
2861 main_msg(_("--version\t\tPrint version information and exit"));
2862
2863#ifdef FEAT_GUI_X11
2864# ifdef FEAT_GUI_MOTIF
2865 mch_msg(_("\nArguments recognised by gvim (Motif version):\n"));
2866# else
2867# ifdef FEAT_GUI_ATHENA
2868# ifdef FEAT_GUI_NEXTAW
2869 mch_msg(_("\nArguments recognised by gvim (neXtaw version):\n"));
2870# else
2871 mch_msg(_("\nArguments recognised by gvim (Athena version):\n"));
2872# endif
2873# endif
2874# endif
2875 main_msg(_("-display <display>\tRun vim on <display>"));
2876 main_msg(_("-iconic\t\tStart vim iconified"));
2877# if 0
2878 main_msg(_("-name <name>\t\tUse resource as if vim was <name>"));
2879 mch_msg(_("\t\t\t (Unimplemented)\n"));
2880# endif
2881 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
2882 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
2883 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
2884 main_msg(_("-boldfont <font>\tUse <font> for bold text"));
2885 main_msg(_("-italicfont <font>\tUse <font> for italic text"));
2886 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
2887 main_msg(_("-borderwidth <width>\tUse a border width of <width> (also: -bw)"));
2888 main_msg(_("-scrollbarwidth <width> Use a scrollbar width of <width> (also: -sw)"));
2889# ifdef FEAT_GUI_ATHENA
2890 main_msg(_("-menuheight <height>\tUse a menu bar height of <height> (also: -mh)"));
2891# endif
2892 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
2893 main_msg(_("+reverse\t\tDon't use reverse video (also: +rv)"));
2894 main_msg(_("-xrm <resource>\tSet the specified resource"));
2895#endif /* FEAT_GUI_X11 */
2896#if defined(FEAT_GUI) && defined(RISCOS)
2897 mch_msg(_("\nArguments recognised by gvim (RISC OS version):\n"));
2898 main_msg(_("--columns <number>\tInitial width of window in columns"));
2899 main_msg(_("--rows <number>\tInitial height of window in rows"));
2900#endif
2901#ifdef FEAT_GUI_GTK
2902 mch_msg(_("\nArguments recognised by gvim (GTK+ version):\n"));
2903 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
2904 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
2905 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
2906 main_msg(_("-display <display>\tRun vim on <display> (also: --display)"));
2907# ifdef HAVE_GTK2
2908 main_msg(_("--role <role>\tSet a unique role to identify the main window"));
2909# endif
2910 main_msg(_("--socketid <xid>\tOpen Vim inside another GTK widget"));
2911#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002912#ifdef FEAT_GUI_W32
2913 main_msg(_("-P <parent title>\tOpen Vim inside parent application"));
2914#endif
2915
2916#ifdef FEAT_GUI_GNOME
2917 /* Gnome gives extra messages for --help if we continue, but not for -h. */
2918 if (gui.starting)
2919 mch_msg("\n");
2920 else
2921#endif
2922 mch_exit(0);
2923}
2924
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002925#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002926/*
2927 * Check the result of the ATTENTION dialog:
2928 * When "Quit" selected, exit Vim.
2929 * When "Recover" selected, recover the file.
2930 */
2931 static void
2932check_swap_exists_action()
2933{
2934 if (swap_exists_action == SEA_QUIT)
2935 getout(1);
2936 handle_swap_exists(NULL);
2937}
2938#endif
2939
2940#if defined(STARTUPTIME) || defined(PROTO)
2941static void time_diff __ARGS((struct timeval *then, struct timeval *now));
2942
2943static struct timeval prev_timeval;
2944
2945/*
2946 * Save the previous time before doing something that could nest.
2947 * set "*tv_rel" to the time elapsed so far.
2948 */
2949 void
2950time_push(tv_rel, tv_start)
2951 void *tv_rel, *tv_start;
2952{
2953 *((struct timeval *)tv_rel) = prev_timeval;
2954 gettimeofday(&prev_timeval, NULL);
2955 ((struct timeval *)tv_rel)->tv_usec = prev_timeval.tv_usec
2956 - ((struct timeval *)tv_rel)->tv_usec;
2957 ((struct timeval *)tv_rel)->tv_sec = prev_timeval.tv_sec
2958 - ((struct timeval *)tv_rel)->tv_sec;
2959 if (((struct timeval *)tv_rel)->tv_usec < 0)
2960 {
2961 ((struct timeval *)tv_rel)->tv_usec += 1000000;
2962 --((struct timeval *)tv_rel)->tv_sec;
2963 }
2964 *(struct timeval *)tv_start = prev_timeval;
2965}
2966
2967/*
2968 * Compute the previous time after doing something that could nest.
2969 * Subtract "*tp" from prev_timeval;
2970 * Note: The arguments are (void *) to avoid trouble with systems that don't
2971 * have struct timeval.
2972 */
2973 void
2974time_pop(tp)
2975 void *tp; /* actually (struct timeval *) */
2976{
2977 prev_timeval.tv_usec -= ((struct timeval *)tp)->tv_usec;
2978 prev_timeval.tv_sec -= ((struct timeval *)tp)->tv_sec;
2979 if (prev_timeval.tv_usec < 0)
2980 {
2981 prev_timeval.tv_usec += 1000000;
2982 --prev_timeval.tv_sec;
2983 }
2984}
2985
2986 static void
2987time_diff(then, now)
2988 struct timeval *then;
2989 struct timeval *now;
2990{
2991 long usec;
2992 long msec;
2993
2994 usec = now->tv_usec - then->tv_usec;
2995 msec = (now->tv_sec - then->tv_sec) * 1000L + usec / 1000L,
2996 usec = usec % 1000L;
2997 fprintf(time_fd, "%03ld.%03ld", msec, usec >= 0 ? usec : usec + 1000L);
2998}
2999
3000 void
3001time_msg(msg, tv_start)
3002 char *msg;
3003 void *tv_start; /* only for do_source: start time; actually
3004 (struct timeval *) */
3005{
3006 static struct timeval start;
3007 struct timeval now;
3008
3009 if (time_fd != NULL)
3010 {
3011 if (strstr(msg, "STARTING") != NULL)
3012 {
3013 gettimeofday(&start, NULL);
3014 prev_timeval = start;
3015 fprintf(time_fd, "\n\ntimes in msec\n");
3016 fprintf(time_fd, " clock self+sourced self: sourced script\n");
3017 fprintf(time_fd, " clock elapsed: other lines\n\n");
3018 }
3019 gettimeofday(&now, NULL);
3020 time_diff(&start, &now);
3021 if (((struct timeval *)tv_start) != NULL)
3022 {
3023 fprintf(time_fd, " ");
3024 time_diff(((struct timeval *)tv_start), &now);
3025 }
3026 fprintf(time_fd, " ");
3027 time_diff(&prev_timeval, &now);
3028 prev_timeval = now;
3029 fprintf(time_fd, ": %s\n", msg);
3030 }
3031}
3032
3033# ifdef WIN3264
3034/*
3035 * Windows doesn't have gettimeofday(), although it does have struct timeval.
3036 */
3037 int
3038gettimeofday(struct timeval *tv, char *dummy)
3039{
3040 long t = clock();
3041 tv->tv_sec = t / CLOCKS_PER_SEC;
3042 tv->tv_usec = (t - tv->tv_sec * CLOCKS_PER_SEC) * 1000000 / CLOCKS_PER_SEC;
3043 return 0;
3044}
3045# endif
3046
3047#endif
3048
3049#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
3050
3051/*
3052 * Common code for the X command server and the Win32 command server.
3053 */
3054
3055static char_u *build_drop_cmd __ARGS((int filec, char **filev, int sendReply));
3056
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003057/*
3058 * Do the client-server stuff, unless "--servername ''" was used.
3059 */
3060 static void
3061exec_on_server(parmp)
3062 mparm_T *parmp;
3063{
3064 if (parmp->serverName_arg == NULL || *parmp->serverName_arg != NUL)
3065 {
3066# ifdef WIN32
3067 /* Initialise the client/server messaging infrastructure. */
3068 serverInitMessaging();
3069# endif
3070
3071 /*
3072 * When a command server argument was found, execute it. This may
3073 * exit Vim when it was successful. Otherwise it's executed further
3074 * on. Remember the encoding used here in "serverStrEnc".
3075 */
3076 if (parmp->serverArg)
3077 {
3078 cmdsrv_main(&parmp->argc, parmp->argv,
3079 parmp->serverName_arg, &parmp->serverStr);
3080# ifdef FEAT_MBYTE
3081 parmp->serverStrEnc = vim_strsave(p_enc);
3082# endif
3083 }
3084
3085 /* If we're still running, get the name to register ourselves.
3086 * On Win32 can register right now, for X11 need to setup the
3087 * clipboard first, it's further down. */
3088 parmp->servername = serverMakeName(parmp->serverName_arg,
3089 parmp->argv[0]);
3090# ifdef WIN32
3091 if (parmp->servername != NULL)
3092 {
3093 serverSetName(parmp->servername);
3094 vim_free(parmp->servername);
3095 }
3096# endif
3097 }
3098}
3099
3100/*
3101 * Prepare for running as a Vim server.
3102 */
3103 static void
3104prepare_server(parmp)
3105 mparm_T *parmp;
3106{
3107# if defined(FEAT_X11)
3108 /*
3109 * Register for remote command execution with :serversend and --remote
3110 * unless there was a -X or a --servername '' on the command line.
3111 * Only register nongui-vim's with an explicit --servername argument.
3112 */
3113 if (X_DISPLAY != NULL && parmp->servername != NULL && (
3114# ifdef FEAT_GUI
3115 gui.in_use ||
3116# endif
3117 parmp->serverName_arg != NULL))
3118 {
3119 (void)serverRegisterName(X_DISPLAY, parmp->servername);
3120 vim_free(parmp->servername);
3121 TIME_MSG("register server name");
3122 }
3123 else
3124 serverDelayedStartName = parmp->servername;
3125# endif
3126
3127 /*
3128 * Execute command ourselves if we're here because the send failed (or
3129 * else we would have exited above).
3130 */
3131 if (parmp->serverStr != NULL)
3132 {
3133 char_u *p;
3134
3135 server_to_input_buf(serverConvert(parmp->serverStrEnc,
3136 parmp->serverStr, &p));
3137 vim_free(p);
3138 }
3139}
3140
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003141 static void
3142cmdsrv_main(argc, argv, serverName_arg, serverStr)
3143 int *argc;
3144 char **argv;
3145 char_u *serverName_arg;
3146 char_u **serverStr;
3147{
3148 char_u *res;
3149 int i;
3150 char_u *sname;
3151 int ret;
3152 int didone = FALSE;
3153 int exiterr = 0;
3154 char **newArgV = argv + 1;
3155 int newArgC = 1,
3156 Argc = *argc;
3157 int argtype;
3158#define ARGTYPE_OTHER 0
3159#define ARGTYPE_EDIT 1
3160#define ARGTYPE_EDIT_WAIT 2
3161#define ARGTYPE_SEND 3
3162 int silent = FALSE;
3163# ifndef FEAT_X11
3164 HWND srv;
3165# else
3166 Window srv;
3167
3168 setup_term_clip();
3169# endif
3170
3171 sname = serverMakeName(serverName_arg, argv[0]);
3172 if (sname == NULL)
3173 return;
3174
3175 /*
3176 * Execute the command server related arguments and remove them
3177 * from the argc/argv array; We may have to return into main()
3178 */
3179 for (i = 1; i < Argc; i++)
3180 {
3181 res = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003182 if (STRCMP(argv[i], "--") == 0) /* end of option arguments */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003183 {
3184 for (; i < *argc; i++)
3185 {
3186 *newArgV++ = argv[i];
3187 newArgC++;
3188 }
3189 break;
3190 }
3191
3192 if (STRICMP(argv[i], "--remote") == 0)
3193 argtype = ARGTYPE_EDIT;
3194 else if (STRICMP(argv[i], "--remote-silent") == 0)
3195 {
3196 argtype = ARGTYPE_EDIT;
3197 silent = TRUE;
3198 }
3199 else if (STRICMP(argv[i], "--remote-wait") == 0)
3200 argtype = ARGTYPE_EDIT_WAIT;
3201 else if (STRICMP(argv[i], "--remote-wait-silent") == 0)
3202 {
3203 argtype = ARGTYPE_EDIT_WAIT;
3204 silent = TRUE;
3205 }
3206 else if (STRICMP(argv[i], "--remote-send") == 0)
3207 argtype = ARGTYPE_SEND;
3208 else
3209 argtype = ARGTYPE_OTHER;
3210 if (argtype != ARGTYPE_OTHER)
3211 {
3212 if (i == *argc - 1)
3213 mainerr_arg_missing((char_u *)argv[i]);
3214 if (argtype == ARGTYPE_SEND)
3215 {
3216 *serverStr = (char_u *)argv[i + 1];
3217 i++;
3218 }
3219 else
3220 {
3221 *serverStr = build_drop_cmd(*argc - i - 1, argv + i + 1,
3222 argtype == ARGTYPE_EDIT_WAIT);
3223 if (*serverStr == NULL)
3224 {
3225 /* Probably out of memory, exit. */
3226 didone = TRUE;
3227 exiterr = 1;
3228 break;
3229 }
3230 Argc = i;
3231 }
3232# ifdef FEAT_X11
3233 if (xterm_dpy == NULL)
3234 {
3235 mch_errmsg(_("No display"));
3236 ret = -1;
3237 }
3238 else
3239 ret = serverSendToVim(xterm_dpy, sname, *serverStr,
3240 NULL, &srv, 0, 0, silent);
3241# else
3242 /* Win32 always works? */
3243 ret = serverSendToVim(sname, *serverStr, NULL, &srv, 0, silent);
3244# endif
3245 if (ret < 0)
3246 {
3247 if (argtype == ARGTYPE_SEND)
3248 {
3249 /* Failed to send, abort. */
3250 mch_errmsg(_(": Send failed.\n"));
3251 didone = TRUE;
3252 exiterr = 1;
3253 }
3254 else if (!silent)
3255 /* Let vim start normally. */
3256 mch_errmsg(_(": Send failed. Trying to execute locally\n"));
3257 break;
3258 }
3259
3260# ifdef FEAT_GUI_W32
3261 /* Guess that when the server name starts with "g" it's a GUI
3262 * server, which we can bring to the foreground here.
3263 * Foreground() in the server doesn't work very well. */
3264 if (argtype != ARGTYPE_SEND && TOUPPER_ASC(*sname) == 'G')
3265 SetForegroundWindow(srv);
3266# endif
3267
3268 /*
3269 * For --remote-wait: Wait until the server did edit each
3270 * file. Also detect that the server no longer runs.
3271 */
3272 if (ret >= 0 && argtype == ARGTYPE_EDIT_WAIT)
3273 {
3274 int numFiles = *argc - i - 1;
3275 int j;
3276 char_u *done = alloc(numFiles);
3277 char_u *p;
3278# ifdef FEAT_GUI_W32
3279 NOTIFYICONDATA ni;
3280 int count = 0;
3281 extern HWND message_window;
3282# endif
3283
3284 if (numFiles > 0 && argv[i + 1][0] == '+')
3285 /* Skip "+cmd" argument, don't wait for it to be edited. */
3286 --numFiles;
3287
3288# ifdef FEAT_GUI_W32
3289 ni.cbSize = sizeof(ni);
3290 ni.hWnd = message_window;
3291 ni.uID = 0;
3292 ni.uFlags = NIF_ICON|NIF_TIP;
3293 ni.hIcon = LoadIcon((HINSTANCE)GetModuleHandle(0), "IDR_VIM");
3294 sprintf(ni.szTip, _("%d of %d edited"), count, numFiles);
3295 Shell_NotifyIcon(NIM_ADD, &ni);
3296# endif
3297
3298 /* Wait for all files to unload in remote */
3299 memset(done, 0, numFiles);
3300 while (memchr(done, 0, numFiles) != NULL)
3301 {
3302# ifdef WIN32
3303 p = serverGetReply(srv, NULL, TRUE, TRUE);
3304 if (p == NULL)
3305 break;
3306# else
3307 if (serverReadReply(xterm_dpy, srv, &p, TRUE) < 0)
3308 break;
3309# endif
3310 j = atoi((char *)p);
3311 if (j >= 0 && j < numFiles)
3312 {
3313# ifdef FEAT_GUI_W32
3314 ++count;
3315 sprintf(ni.szTip, _("%d of %d edited"),
3316 count, numFiles);
3317 Shell_NotifyIcon(NIM_MODIFY, &ni);
3318# endif
3319 done[j] = 1;
3320 }
3321 }
3322# ifdef FEAT_GUI_W32
3323 Shell_NotifyIcon(NIM_DELETE, &ni);
3324# endif
3325 }
3326 }
3327 else if (STRICMP(argv[i], "--remote-expr") == 0)
3328 {
3329 if (i == *argc - 1)
3330 mainerr_arg_missing((char_u *)argv[i]);
3331# ifdef WIN32
3332 /* Win32 always works? */
3333 if (serverSendToVim(sname, (char_u *)argv[i + 1],
3334 &res, NULL, 1, FALSE) < 0)
3335# else
3336 if (xterm_dpy == NULL)
3337 mch_errmsg(_("No display: Send expression failed.\n"));
3338 else if (serverSendToVim(xterm_dpy, sname, (char_u *)argv[i + 1],
3339 &res, NULL, 1, 1, FALSE) < 0)
3340# endif
3341 {
3342 if (res != NULL && *res != NUL)
3343 {
3344 /* Output error from remote */
3345 mch_errmsg((char *)res);
3346 vim_free(res);
3347 res = NULL;
3348 }
3349 mch_errmsg(_(": Send expression failed.\n"));
3350 }
3351 }
3352 else if (STRICMP(argv[i], "--serverlist") == 0)
3353 {
3354# ifdef WIN32
3355 /* Win32 always works? */
3356 res = serverGetVimNames();
3357# else
3358 if (xterm_dpy != NULL)
3359 res = serverGetVimNames(xterm_dpy);
3360# endif
3361 if (called_emsg)
3362 mch_errmsg("\n");
3363 }
3364 else if (STRICMP(argv[i], "--servername") == 0)
3365 {
3366 /* Alredy processed. Take it out of the command line */
3367 i++;
3368 continue;
3369 }
3370 else
3371 {
3372 *newArgV++ = argv[i];
3373 newArgC++;
3374 continue;
3375 }
3376 didone = TRUE;
3377 if (res != NULL && *res != NUL)
3378 {
3379 mch_msg((char *)res);
3380 if (res[STRLEN(res) - 1] != '\n')
3381 mch_msg("\n");
3382 }
3383 vim_free(res);
3384 }
3385
3386 if (didone)
3387 {
3388 display_errors(); /* display any collected messages */
3389 exit(exiterr); /* Mission accomplished - get out */
3390 }
3391
3392 /* Return back into main() */
3393 *argc = newArgC;
3394 vim_free(sname);
3395}
3396
3397/*
3398 * Build a ":drop" command to send to a Vim server.
3399 */
3400 static char_u *
3401build_drop_cmd(filec, filev, sendReply)
3402 int filec;
3403 char **filev;
3404 int sendReply;
3405{
3406 garray_T ga;
3407 int i;
3408 char_u *inicmd = NULL;
3409 char_u *p;
3410 char_u cwd[MAXPATHL];
3411
3412 if (filec > 0 && filev[0][0] == '+')
3413 {
3414 inicmd = (char_u *)filev[0] + 1;
3415 filev++;
3416 filec--;
3417 }
3418 /* Check if we have at least one argument. */
3419 if (filec <= 0)
3420 mainerr_arg_missing((char_u *)filev[-1]);
3421 if (mch_dirname(cwd, MAXPATHL) != OK)
3422 return NULL;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003423 if ((p = vim_strsave_escaped_ext(cwd, PATH_ESC_CHARS, '\\', TRUE)) == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003424 return NULL;
3425 ga_init2(&ga, 1, 100);
3426 ga_concat(&ga, (char_u *)"<C-\\><C-N>:cd ");
3427 ga_concat(&ga, p);
3428 /* Call inputsave() so that a prompt for an encryption key works. */
3429 ga_concat(&ga, (char_u *)"<CR>:if exists('*inputsave')|call inputsave()|endif|drop");
3430 vim_free(p);
3431 for (i = 0; i < filec; i++)
3432 {
3433 /* On Unix the shell has already expanded the wildcards, don't want to
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003434 * do it again in the Vim server. On MS-Windows only escape
3435 * non-wildcard characters. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003436 p = vim_strsave_escaped((char_u *)filev[i],
3437#ifdef UNIX
3438 PATH_ESC_CHARS
3439#else
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003440 (char_u *)" \t%#"
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003441#endif
3442 );
3443 if (p == NULL)
3444 {
3445 vim_free(ga.ga_data);
3446 return NULL;
3447 }
3448 ga_concat(&ga, (char_u *)" ");
3449 ga_concat(&ga, p);
3450 vim_free(p);
3451 }
3452 /* The :drop commands goes to Insert mode when 'insertmode' is set, use
3453 * CTRL-\ CTRL-N again. */
3454 ga_concat(&ga, (char_u *)"|if exists('*inputrestore')|call inputrestore()|endif<CR>");
3455 ga_concat(&ga, (char_u *)"<C-\\><C-N>:cd -");
3456 if (sendReply)
3457 ga_concat(&ga, (char_u *)"<CR>:call SetupRemoteReplies()");
3458 ga_concat(&ga, (char_u *)"<CR>:");
3459 if (inicmd != NULL)
3460 {
3461 /* Can't use <CR> after "inicmd", because an "startinsert" would cause
3462 * the following commands to be inserted as text. Use a "|",
3463 * hopefully "inicmd" does allow this... */
3464 ga_concat(&ga, inicmd);
3465 ga_concat(&ga, (char_u *)"|");
3466 }
3467 /* Bring the window to the foreground, goto Insert mode when 'im' set and
3468 * clear command line. */
Bram Moolenaar567e4de2004-12-31 21:01:02 +00003469 ga_concat(&ga, (char_u *)"cal foreground()|if &im|star|en|redr|f<CR>");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003470 ga_append(&ga, NUL);
3471 return ga.ga_data;
3472}
3473
3474/*
3475 * Replace termcodes such as <CR> and insert as key presses if there is room.
3476 */
3477 void
3478server_to_input_buf(str)
3479 char_u *str;
3480{
3481 char_u *ptr = NULL;
3482 char_u *cpo_save = p_cpo;
3483
3484 /* Set 'cpoptions' the way we want it.
3485 * B set - backslashes are *not* treated specially
3486 * k set - keycodes are *not* reverse-engineered
3487 * < unset - <Key> sequences *are* interpreted
3488 * The last parameter of replace_termcodes() is TRUE so that the <lt>
3489 * sequence is recognised - needed for a real backslash.
3490 */
3491 p_cpo = (char_u *)"Bk";
3492 str = replace_termcodes((char_u *)str, &ptr, FALSE, TRUE);
3493 p_cpo = cpo_save;
3494
3495 if (*ptr != NUL) /* trailing CTRL-V results in nothing */
3496 {
3497 /*
3498 * Add the string to the input stream.
3499 * Can't use add_to_input_buf() here, we now have K_SPECIAL bytes.
3500 *
3501 * First clear typed characters from the typeahead buffer, there could
3502 * be half a mapping there. Then append to the existing string, so
3503 * that multiple commands from a client are concatenated.
3504 */
3505 if (typebuf.tb_maplen < typebuf.tb_len)
3506 del_typebuf(typebuf.tb_len - typebuf.tb_maplen, typebuf.tb_maplen);
3507 (void)ins_typebuf(str, REMAP_NONE, typebuf.tb_len, TRUE, FALSE);
3508
3509 /* Let input_available() know we inserted text in the typeahead
3510 * buffer. */
3511 received_from_client = TRUE;
3512 }
3513 vim_free((char_u *)ptr);
3514}
3515
3516/*
3517 * Evaluate an expression that the client sent to a string.
3518 * Handles disabling error messages and disables debugging, otherwise Vim
3519 * hangs, waiting for "cont" to be typed.
3520 */
3521 char_u *
3522eval_client_expr_to_string(expr)
3523 char_u *expr;
3524{
3525 char_u *res;
3526 int save_dbl = debug_break_level;
3527 int save_ro = redir_off;
3528
3529 debug_break_level = -1;
3530 redir_off = 0;
3531 ++emsg_skip;
3532
3533 res = eval_to_string(expr, NULL);
3534
3535 debug_break_level = save_dbl;
3536 redir_off = save_ro;
3537 --emsg_skip;
3538
Bram Moolenaar63a121b2005-12-11 21:36:39 +00003539 /* A client can tell us to redraw, but not to display the cursor, so do
3540 * that here. */
3541 setcursor();
3542 out_flush();
3543#ifdef FEAT_GUI
3544 if (gui.in_use)
3545 gui_update_cursor(FALSE, FALSE);
3546#endif
3547
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003548 return res;
3549}
3550
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003551/*
3552 * If conversion is needed, convert "data" from "client_enc" to 'encoding' and
3553 * return an allocated string. Otherwise return "data".
3554 * "*tofree" is set to the result when it needs to be freed later.
3555 */
3556/*ARGSUSED*/
3557 char_u *
3558serverConvert(client_enc, data, tofree)
3559 char_u *client_enc;
3560 char_u *data;
3561 char_u **tofree;
3562{
3563 char_u *res = data;
3564
3565 *tofree = NULL;
3566# ifdef FEAT_MBYTE
3567 if (client_enc != NULL && p_enc != NULL)
3568 {
3569 vimconv_T vimconv;
3570
3571 vimconv.vc_type = CONV_NONE;
3572 if (convert_setup(&vimconv, client_enc, p_enc) != FAIL
3573 && vimconv.vc_type != CONV_NONE)
3574 {
3575 res = string_convert(&vimconv, data, NULL);
3576 if (res == NULL)
3577 res = data;
3578 else
3579 *tofree = res;
3580 }
3581 convert_setup(&vimconv, NULL, NULL);
3582 }
3583# endif
3584 return res;
3585}
3586
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003587
3588/*
3589 * Make our basic server name: use the specified "arg" if given, otherwise use
3590 * the tail of the command "cmd" we were started with.
3591 * Return the name in allocated memory. This doesn't include a serial number.
3592 */
3593 static char_u *
3594serverMakeName(arg, cmd)
3595 char_u *arg;
3596 char *cmd;
3597{
3598 char_u *p;
3599
3600 if (arg != NULL && *arg != NUL)
3601 p = vim_strsave_up(arg);
3602 else
3603 {
3604 p = vim_strsave_up(gettail((char_u *)cmd));
3605 /* Remove .exe or .bat from the name. */
3606 if (p != NULL && vim_strchr(p, '.') != NULL)
3607 *vim_strchr(p, '.') = NUL;
3608 }
3609 return p;
3610}
3611#endif /* FEAT_CLIENTSERVER */
3612
3613/*
3614 * When FEAT_FKMAP is defined, also compile the Farsi source code.
3615 */
3616#if defined(FEAT_FKMAP) || defined(PROTO)
3617# include "farsi.c"
3618#endif
3619
3620/*
3621 * When FEAT_ARABIC is defined, also compile the Arabic source code.
3622 */
3623#if defined(FEAT_ARABIC) || defined(PROTO)
3624# include "arabic.c"
3625#endif