blob: c635bfc4ce7733ec6a70c5a660f4454d298987c8 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +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/*
11 * ex_cmds2.c: some more functions for command line commands
12 */
13
14#if defined(WIN32) && defined(FEAT_CSCOPE)
15# include <io.h>
16#endif
17
18#include "vim.h"
19
20#if defined(WIN32) && defined(FEAT_CSCOPE)
21# include <fcntl.h>
22#endif
23
24#include "version.h"
25
26static void cmd_source __ARGS((char_u *fname, exarg_T *eap));
27
Bram Moolenaar05159a02005-02-26 23:04:13 +000028#ifdef FEAT_EVAL
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +000029/* Growarray to store info about already sourced scripts.
Bram Moolenaar05159a02005-02-26 23:04:13 +000030 * For Unix also store the dev/ino, so that we don't have to stat() each
31 * script when going through the list. */
32typedef struct scriptitem_S
33{
34 char_u *sn_name;
35# ifdef UNIX
36 int sn_dev;
37 ino_t sn_ino;
38# endif
39# ifdef FEAT_PROFILE
40 int sn_prof_on; /* TRUE when script is/was profiled */
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +000041 int sn_pr_force; /* forceit: profile functions in this script */
Bram Moolenaar05159a02005-02-26 23:04:13 +000042 proftime_T sn_pr_child; /* time set when going into first child */
43 int sn_pr_nest; /* nesting for sn_pr_child */
44 /* profiling the script as a whole */
45 int sn_pr_count; /* nr of times sourced */
46 proftime_T sn_pr_total; /* time spend in script + children */
47 proftime_T sn_pr_self; /* time spend in script itself */
48 proftime_T sn_pr_start; /* time at script start */
49 proftime_T sn_pr_children; /* time in children after script start */
50 /* profiling the script per line */
51 garray_T sn_prl_ga; /* things stored for every line */
52 proftime_T sn_prl_start; /* start time for current line */
53 proftime_T sn_prl_children; /* time spent in children for this line */
54 proftime_T sn_prl_wait; /* wait start time for current line */
55 int sn_prl_idx; /* index of line being timed; -1 if none */
56 int sn_prl_execed; /* line being timed was executed */
57# endif
58} scriptitem_T;
59
60static garray_T script_items = {0, 0, sizeof(scriptitem_T), 4, NULL};
61#define SCRIPT_ITEM(id) (((scriptitem_T *)script_items.ga_data)[(id) - 1])
62
63# ifdef FEAT_PROFILE
64/* Struct used in sn_prl_ga for every line of a script. */
65typedef struct sn_prl_S
66{
67 int snp_count; /* nr of times line was executed */
68 proftime_T sn_prl_total; /* time spend in a line + children */
69 proftime_T sn_prl_self; /* time spend in a line itself */
70} sn_prl_T;
71
72# define PRL_ITEM(si, idx) (((sn_prl_T *)(si)->sn_prl_ga.ga_data)[(idx)])
73# endif
74#endif
75
Bram Moolenaar071d4272004-06-13 20:20:40 +000076#if defined(FEAT_EVAL) || defined(PROTO)
77static int debug_greedy = FALSE; /* batch mode debugging: don't save
78 and restore typeahead. */
79
80/*
81 * do_debug(): Debug mode.
82 * Repeatedly get Ex commands, until told to continue normal execution.
83 */
84 void
85do_debug(cmd)
86 char_u *cmd;
87{
88 int save_msg_scroll = msg_scroll;
89 int save_State = State;
90 int save_did_emsg = did_emsg;
91 int save_cmd_silent = cmd_silent;
92 int save_msg_silent = msg_silent;
93 int save_emsg_silent = emsg_silent;
94 int save_redir_off = redir_off;
95 tasave_T typeaheadbuf;
96# ifdef FEAT_EX_EXTRA
97 int save_ex_normal_busy;
98# endif
99 int n;
100 char_u *cmdline = NULL;
101 char_u *p;
102 char *tail = NULL;
103 static int last_cmd = 0;
104#define CMD_CONT 1
105#define CMD_NEXT 2
106#define CMD_STEP 3
107#define CMD_FINISH 4
108#define CMD_QUIT 5
109#define CMD_INTERRUPT 6
110
111#ifdef ALWAYS_USE_GUI
112 /* Can't do this when there is no terminal for input/output. */
113 if (!gui.in_use)
114 {
115 /* Break as soon as possible. */
116 debug_break_level = 9999;
117 return;
118 }
119#endif
120
121 /* Make sure we are in raw mode and start termcap mode. Might have side
122 * effects... */
123 settmode(TMODE_RAW);
124 starttermcap();
125
126 ++RedrawingDisabled; /* don't redisplay the window */
127 ++no_wait_return; /* don't wait for return */
128 did_emsg = FALSE; /* don't use error from debugged stuff */
129 cmd_silent = FALSE; /* display commands */
130 msg_silent = FALSE; /* display messages */
131 emsg_silent = FALSE; /* display error messages */
132 redir_off = TRUE; /* don't redirect debug commands */
133
134 State = NORMAL;
135#ifdef FEAT_SNIFF
136 want_sniff_request = 0; /* No K_SNIFF wanted */
137#endif
138
139 if (!debug_did_msg)
140 MSG(_("Entering Debug mode. Type \"cont\" to continue."));
141 if (sourcing_name != NULL)
142 msg(sourcing_name);
143 if (sourcing_lnum != 0)
Bram Moolenaar555b2802005-05-19 21:08:39 +0000144 smsg((char_u *)_("line %ld: %s"), (long)sourcing_lnum, cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145 else
Bram Moolenaar555b2802005-05-19 21:08:39 +0000146 smsg((char_u *)_("cmd: %s"), cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147
148 /*
149 * Repeat getting a command and executing it.
150 */
151 for (;;)
152 {
153 msg_scroll = TRUE;
154 need_wait_return = FALSE;
155#ifdef FEAT_SNIFF
156 ProcessSniffRequests();
157#endif
158 /* Save the current typeahead buffer and replace it with an empty one.
159 * This makes sure we get input from the user here and don't interfere
160 * with the commands being executed. Reset "ex_normal_busy" to avoid
161 * the side effects of using ":normal". Save the stuff buffer and make
162 * it empty. */
163# ifdef FEAT_EX_EXTRA
164 save_ex_normal_busy = ex_normal_busy;
165 ex_normal_busy = 0;
166# endif
167 if (!debug_greedy)
168 save_typeahead(&typeaheadbuf);
169
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000170 cmdline = getcmdline_prompt('>', NULL, 0, EXPAND_NOTHING, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171
172 if (!debug_greedy)
173 restore_typeahead(&typeaheadbuf);
174# ifdef FEAT_EX_EXTRA
175 ex_normal_busy = save_ex_normal_busy;
176# endif
177
178 cmdline_row = msg_row;
179 if (cmdline != NULL)
180 {
181 /* If this is a debug command, set "last_cmd".
182 * If not, reset "last_cmd".
183 * For a blank line use previous command. */
184 p = skipwhite(cmdline);
185 if (*p != NUL)
186 {
187 switch (*p)
188 {
189 case 'c': last_cmd = CMD_CONT;
190 tail = "ont";
191 break;
192 case 'n': last_cmd = CMD_NEXT;
193 tail = "ext";
194 break;
195 case 's': last_cmd = CMD_STEP;
196 tail = "tep";
197 break;
198 case 'f': last_cmd = CMD_FINISH;
199 tail = "inish";
200 break;
201 case 'q': last_cmd = CMD_QUIT;
202 tail = "uit";
203 break;
204 case 'i': last_cmd = CMD_INTERRUPT;
205 tail = "nterrupt";
206 break;
207 default: last_cmd = 0;
208 }
209 if (last_cmd != 0)
210 {
211 /* Check that the tail matches. */
212 ++p;
213 while (*p != NUL && *p == *tail)
214 {
215 ++p;
216 ++tail;
217 }
218 if (ASCII_ISALPHA(*p))
219 last_cmd = 0;
220 }
221 }
222
223 if (last_cmd != 0)
224 {
225 /* Execute debug command: decided where to break next and
226 * return. */
227 switch (last_cmd)
228 {
229 case CMD_CONT:
230 debug_break_level = -1;
231 break;
232 case CMD_NEXT:
233 debug_break_level = ex_nesting_level;
234 break;
235 case CMD_STEP:
236 debug_break_level = 9999;
237 break;
238 case CMD_FINISH:
239 debug_break_level = ex_nesting_level - 1;
240 break;
241 case CMD_QUIT:
242 got_int = TRUE;
243 debug_break_level = -1;
244 break;
245 case CMD_INTERRUPT:
246 got_int = TRUE;
247 debug_break_level = 9999;
248 /* Do not repeat ">interrupt" cmd, continue stepping. */
249 last_cmd = CMD_STEP;
250 break;
251 }
252 break;
253 }
254
255 /* don't debug this command */
256 n = debug_break_level;
257 debug_break_level = -1;
258 (void)do_cmdline(cmdline, getexline, NULL,
259 DOCMD_VERBOSE|DOCMD_EXCRESET);
260 debug_break_level = n;
261
262 vim_free(cmdline);
263 }
264 lines_left = Rows - 1;
265 }
266 vim_free(cmdline);
267
268 --RedrawingDisabled;
269 --no_wait_return;
270 redraw_all_later(NOT_VALID);
271 need_wait_return = FALSE;
272 msg_scroll = save_msg_scroll;
273 lines_left = Rows - 1;
274 State = save_State;
275 did_emsg = save_did_emsg;
276 cmd_silent = save_cmd_silent;
277 msg_silent = save_msg_silent;
278 emsg_silent = save_emsg_silent;
279 redir_off = save_redir_off;
280
281 /* Only print the message again when typing a command before coming back
282 * here. */
283 debug_did_msg = TRUE;
284}
285
286/*
287 * ":debug".
288 */
289 void
290ex_debug(eap)
291 exarg_T *eap;
292{
293 int debug_break_level_save = debug_break_level;
294
295 debug_break_level = 9999;
296 do_cmdline_cmd(eap->arg);
297 debug_break_level = debug_break_level_save;
298}
299
300static char_u *debug_breakpoint_name = NULL;
301static linenr_T debug_breakpoint_lnum;
302
303/*
304 * When debugging or a breakpoint is set on a skipped command, no debug prompt
305 * is shown by do_one_cmd(). This situation is indicated by debug_skipped, and
306 * debug_skipped_name is then set to the source name in the breakpoint case. If
307 * a skipped command decides itself that a debug prompt should be displayed, it
308 * can do so by calling dbg_check_skipped().
309 */
310static int debug_skipped;
311static char_u *debug_skipped_name;
312
313/*
314 * Go to debug mode when a breakpoint was encountered or "ex_nesting_level" is
315 * at or below the break level. But only when the line is actually
316 * executed. Return TRUE and set breakpoint_name for skipped commands that
317 * decide to execute something themselves.
318 * Called from do_one_cmd() before executing a command.
319 */
320 void
321dbg_check_breakpoint(eap)
322 exarg_T *eap;
323{
324 char_u *p;
325
326 debug_skipped = FALSE;
327 if (debug_breakpoint_name != NULL)
328 {
329 if (!eap->skip)
330 {
331 /* replace K_SNR with "<SNR>" */
332 if (debug_breakpoint_name[0] == K_SPECIAL
333 && debug_breakpoint_name[1] == KS_EXTRA
334 && debug_breakpoint_name[2] == (int)KE_SNR)
335 p = (char_u *)"<SNR>";
336 else
337 p = (char_u *)"";
Bram Moolenaar555b2802005-05-19 21:08:39 +0000338 smsg((char_u *)_("Breakpoint in \"%s%s\" line %ld"),
339 p,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340 debug_breakpoint_name + (*p == NUL ? 0 : 3),
341 (long)debug_breakpoint_lnum);
342 debug_breakpoint_name = NULL;
343 do_debug(eap->cmd);
344 }
345 else
346 {
347 debug_skipped = TRUE;
348 debug_skipped_name = debug_breakpoint_name;
349 debug_breakpoint_name = NULL;
350 }
351 }
352 else if (ex_nesting_level <= debug_break_level)
353 {
354 if (!eap->skip)
355 do_debug(eap->cmd);
356 else
357 {
358 debug_skipped = TRUE;
359 debug_skipped_name = NULL;
360 }
361 }
362}
363
364/*
365 * Go to debug mode if skipped by dbg_check_breakpoint() because eap->skip was
366 * set. Return TRUE when the debug mode is entered this time.
367 */
368 int
369dbg_check_skipped(eap)
370 exarg_T *eap;
371{
372 int prev_got_int;
373
374 if (debug_skipped)
375 {
376 /*
377 * Save the value of got_int and reset it. We don't want a previous
378 * interruption cause flushing the input buffer.
379 */
380 prev_got_int = got_int;
381 got_int = FALSE;
382 debug_breakpoint_name = debug_skipped_name;
383 /* eap->skip is TRUE */
384 eap->skip = FALSE;
385 (void)dbg_check_breakpoint(eap);
386 eap->skip = TRUE;
387 got_int |= prev_got_int;
388 return TRUE;
389 }
390 return FALSE;
391}
392
393/*
394 * The list of breakpoints: dbg_breakp.
395 * This is a grow-array of structs.
396 */
397struct debuggy
398{
399 int dbg_nr; /* breakpoint number */
400 int dbg_type; /* DBG_FUNC or DBG_FILE */
401 char_u *dbg_name; /* function or file name */
402 regprog_T *dbg_prog; /* regexp program */
403 linenr_T dbg_lnum; /* line number in function or file */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000404 int dbg_forceit; /* ! used */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405};
406
407static garray_T dbg_breakp = {0, 0, sizeof(struct debuggy), 4, NULL};
Bram Moolenaar05159a02005-02-26 23:04:13 +0000408#define BREAKP(idx) (((struct debuggy *)dbg_breakp.ga_data)[idx])
409#define DEBUGGY(gap, idx) (((struct debuggy *)gap->ga_data)[idx])
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410static int last_breakp = 0; /* nr of last defined breakpoint */
411
Bram Moolenaar05159a02005-02-26 23:04:13 +0000412#ifdef FEAT_PROFILE
413/* Profiling uses file and func names similar to breakpoints. */
414static garray_T prof_ga = {0, 0, sizeof(struct debuggy), 4, NULL};
415#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416#define DBG_FUNC 1
417#define DBG_FILE 2
418
Bram Moolenaar05159a02005-02-26 23:04:13 +0000419static int dbg_parsearg __ARGS((char_u *arg, garray_T *gap));
420static linenr_T debuggy_find __ARGS((int file,char_u *fname, linenr_T after, garray_T *gap, int *fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421
422/*
Bram Moolenaar05159a02005-02-26 23:04:13 +0000423 * Parse the arguments of ":profile", ":breakadd" or ":breakdel" and put them
424 * in the entry just after the last one in dbg_breakp. Note that "dbg_name"
425 * is allocated.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426 * Returns FAIL for failure.
427 */
428 static int
Bram Moolenaar05159a02005-02-26 23:04:13 +0000429dbg_parsearg(arg, gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430 char_u *arg;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000431 garray_T *gap; /* either &dbg_breakp or &prof_ga */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000432{
433 char_u *p = arg;
434 char_u *q;
435 struct debuggy *bp;
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000436 int here = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437
Bram Moolenaar05159a02005-02-26 23:04:13 +0000438 if (ga_grow(gap, 1) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000439 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000440 bp = &DEBUGGY(gap, gap->ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441
442 /* Find "func" or "file". */
443 if (STRNCMP(p, "func", 4) == 0)
444 bp->dbg_type = DBG_FUNC;
445 else if (STRNCMP(p, "file", 4) == 0)
446 bp->dbg_type = DBG_FILE;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000447 else if (
448#ifdef FEAT_PROFILE
449 gap != &prof_ga &&
450#endif
451 STRNCMP(p, "here", 4) == 0)
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000452 {
453 if (curbuf->b_ffname == NULL)
454 {
455 EMSG(_(e_noname));
456 return FAIL;
457 }
458 bp->dbg_type = DBG_FILE;
459 here = TRUE;
460 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000461 else
462 {
463 EMSG2(_(e_invarg2), p);
464 return FAIL;
465 }
466 p = skipwhite(p + 4);
467
468 /* Find optional line number. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000469 if (here)
470 bp->dbg_lnum = curwin->w_cursor.lnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000471 else if (
472#ifdef FEAT_PROFILE
473 gap != &prof_ga &&
474#endif
475 VIM_ISDIGIT(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000476 {
477 bp->dbg_lnum = getdigits(&p);
478 p = skipwhite(p);
479 }
480 else
481 bp->dbg_lnum = 0;
482
483 /* Find the function or file name. Don't accept a function name with (). */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000484 if ((!here && *p == NUL)
485 || (here && *p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486 || (bp->dbg_type == DBG_FUNC && strstr((char *)p, "()") != NULL))
487 {
488 EMSG2(_(e_invarg2), arg);
489 return FAIL;
490 }
491
492 if (bp->dbg_type == DBG_FUNC)
493 bp->dbg_name = vim_strsave(p);
Bram Moolenaarf4b8e572004-06-24 15:53:16 +0000494 else if (here)
495 bp->dbg_name = vim_strsave(curbuf->b_ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496 else
497 {
498 /* Expand the file name in the same way as do_source(). This means
499 * doing it twice, so that $DIR/file gets expanded when $DIR is
500 * "~/dir". */
501#ifdef RISCOS
502 q = mch_munge_fname(p);
503#else
504 q = expand_env_save(p);
505#endif
506 if (q == NULL)
507 return FAIL;
508#ifdef RISCOS
509 p = mch_munge_fname(q);
510#else
511 p = expand_env_save(q);
512#endif
513 vim_free(q);
514 if (p == NULL)
515 return FAIL;
Bram Moolenaar843ee412004-06-30 16:16:41 +0000516 if (*p != '*')
517 {
518 bp->dbg_name = fix_fname(p);
519 vim_free(p);
520 }
521 else
522 bp->dbg_name = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523#ifdef MACOS_CLASSIC
524 if (bp->dbg_name != NULL)
525 slash_n_colon_adjust(bp->dbg_name);
526#endif
527 }
528
529 if (bp->dbg_name == NULL)
530 return FAIL;
531 return OK;
532}
533
534/*
535 * ":breakadd".
536 */
537 void
538ex_breakadd(eap)
539 exarg_T *eap;
540{
541 struct debuggy *bp;
542 char_u *pat;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000543 garray_T *gap;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544
Bram Moolenaar05159a02005-02-26 23:04:13 +0000545 gap = &dbg_breakp;
546#ifdef FEAT_PROFILE
547 if (eap->cmdidx == CMD_profile)
548 gap = &prof_ga;
549#endif
550
551 if (dbg_parsearg(eap->arg, gap) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000553 bp = &DEBUGGY(gap, gap->ga_len);
554 bp->dbg_forceit = eap->forceit;
555
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556 pat = file_pat_to_reg_pat(bp->dbg_name, NULL, NULL, FALSE);
557 if (pat != NULL)
558 {
559 bp->dbg_prog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
560 vim_free(pat);
561 }
562 if (pat == NULL || bp->dbg_prog == NULL)
563 vim_free(bp->dbg_name);
564 else
565 {
566 if (bp->dbg_lnum == 0) /* default line number is 1 */
567 bp->dbg_lnum = 1;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000568#ifdef FEAT_PROFILE
569 if (eap->cmdidx != CMD_profile)
570#endif
571 {
572 DEBUGGY(gap, gap->ga_len).dbg_nr = ++last_breakp;
573 ++debug_tick;
574 }
575 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576 }
577 }
578}
579
580/*
581 * ":debuggreedy".
582 */
583 void
584ex_debuggreedy(eap)
585 exarg_T *eap;
586{
587 if (eap->addr_count == 0 || eap->line2 != 0)
588 debug_greedy = TRUE;
589 else
590 debug_greedy = FALSE;
591}
592
593/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000594 * ":breakdel" and ":profdel".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595 */
596 void
597ex_breakdel(eap)
598 exarg_T *eap;
599{
600 struct debuggy *bp, *bpi;
601 int nr;
602 int todel = -1;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000603 int del_all = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604 int i;
605 linenr_T best_lnum = 0;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000606 garray_T *gap;
607
608 gap = &dbg_breakp;
609#ifdef FEAT_PROFILE
610 if (eap->cmdidx == CMD_profdel)
611 gap = &prof_ga;
612#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613
614 if (vim_isdigit(*eap->arg))
615 {
616 /* ":breakdel {nr}" */
617 nr = atol((char *)eap->arg);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000618 for (i = 0; i < gap->ga_len; ++i)
619 if (DEBUGGY(gap, i).dbg_nr == nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 {
621 todel = i;
622 break;
623 }
624 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000625 else if (*eap->arg == '*')
626 {
627 todel = 0;
628 del_all = TRUE;
629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630 else
631 {
632 /* ":breakdel {func|file} [lnum] {name}" */
Bram Moolenaard9fba312005-06-26 22:34:35 +0000633 if (dbg_parsearg(eap->arg, gap) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000635 bp = &DEBUGGY(gap, gap->ga_len);
636 for (i = 0; i < gap->ga_len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637 {
Bram Moolenaard9fba312005-06-26 22:34:35 +0000638 bpi = &DEBUGGY(gap, i);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639 if (bp->dbg_type == bpi->dbg_type
640 && STRCMP(bp->dbg_name, bpi->dbg_name) == 0
641 && (bp->dbg_lnum == bpi->dbg_lnum
642 || (bp->dbg_lnum == 0
643 && (best_lnum == 0
644 || bpi->dbg_lnum < best_lnum))))
645 {
646 todel = i;
647 best_lnum = bpi->dbg_lnum;
648 }
649 }
650 vim_free(bp->dbg_name);
651 }
652
653 if (todel < 0)
654 EMSG2(_("E161: Breakpoint not found: %s"), eap->arg);
655 else
Bram Moolenaard9fba312005-06-26 22:34:35 +0000656 {
657 while (gap->ga_len > 0)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000658 {
Bram Moolenaard9fba312005-06-26 22:34:35 +0000659 vim_free(DEBUGGY(gap, todel).dbg_name);
660 vim_free(DEBUGGY(gap, todel).dbg_prog);
661 --gap->ga_len;
662 if (todel < gap->ga_len)
663 mch_memmove(&DEBUGGY(gap, todel), &DEBUGGY(gap, todel + 1),
664 (gap->ga_len - todel) * sizeof(struct debuggy));
665#ifdef FEAT_PROFILE
666 if (eap->cmdidx == CMD_breakdel)
667#endif
668 ++debug_tick;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000669 if (!del_all)
670 break;
671 }
Bram Moolenaard9fba312005-06-26 22:34:35 +0000672
673 /* If all breakpoints were removed clear the array. */
674 if (gap->ga_len == 0)
675 ga_clear(gap);
676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677}
678
679/*
680 * ":breaklist".
681 */
682/*ARGSUSED*/
683 void
684ex_breaklist(eap)
685 exarg_T *eap;
686{
687 struct debuggy *bp;
688 int i;
689
690 if (dbg_breakp.ga_len == 0)
691 MSG(_("No breakpoints defined"));
692 else
693 for (i = 0; i < dbg_breakp.ga_len; ++i)
694 {
695 bp = &BREAKP(i);
696 smsg((char_u *)_("%3d %s %s line %ld"),
697 bp->dbg_nr,
698 bp->dbg_type == DBG_FUNC ? "func" : "file",
699 bp->dbg_name,
700 (long)bp->dbg_lnum);
701 }
702}
703
704/*
705 * Find a breakpoint for a function or sourced file.
706 * Returns line number at which to break; zero when no matching breakpoint.
707 */
708 linenr_T
709dbg_find_breakpoint(file, fname, after)
710 int file; /* TRUE for a file, FALSE for a function */
711 char_u *fname; /* file or function name */
712 linenr_T after; /* after this line number */
713{
Bram Moolenaar05159a02005-02-26 23:04:13 +0000714 return debuggy_find(file, fname, after, &dbg_breakp, NULL);
715}
716
717#if defined(FEAT_PROFILE) || defined(PROTO)
718/*
719 * Return TRUE if profiling is on for a function or sourced file.
720 */
721 int
722has_profiling(file, fname, fp)
723 int file; /* TRUE for a file, FALSE for a function */
724 char_u *fname; /* file or function name */
725 int *fp; /* return: forceit */
726{
727 return (debuggy_find(file, fname, (linenr_T)0, &prof_ga, fp)
728 != (linenr_T)0);
729}
730#endif
731
732/*
733 * Common code for dbg_find_breakpoint() and has_profiling().
734 */
735 static linenr_T
736debuggy_find(file, fname, after, gap, fp)
737 int file; /* TRUE for a file, FALSE for a function */
738 char_u *fname; /* file or function name */
739 linenr_T after; /* after this line number */
740 garray_T *gap; /* either &dbg_breakp or &prof_ga */
741 int *fp; /* if not NULL: return forceit */
742{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 struct debuggy *bp;
744 int i;
745 linenr_T lnum = 0;
746 regmatch_T regmatch;
747 char_u *name = fname;
748 int prev_got_int;
749
Bram Moolenaar05159a02005-02-26 23:04:13 +0000750 /* Return quickly when there are no breakpoints. */
751 if (gap->ga_len == 0)
752 return (linenr_T)0;
753
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 /* Replace K_SNR in function name with "<SNR>". */
755 if (!file && fname[0] == K_SPECIAL)
756 {
757 name = alloc((unsigned)STRLEN(fname) + 3);
758 if (name == NULL)
759 name = fname;
760 else
761 {
762 STRCPY(name, "<SNR>");
763 STRCPY(name + 5, fname + 3);
764 }
765 }
766
Bram Moolenaar05159a02005-02-26 23:04:13 +0000767 for (i = 0; i < gap->ga_len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000769 /* Skip entries that are not useful or are for a line that is beyond
770 * an already found breakpoint. */
771 bp = &DEBUGGY(gap, i);
772 if (((bp->dbg_type == DBG_FILE) == file && (
773#ifdef FEAT_PROFILE
774 gap == &prof_ga ||
775#endif
776 (bp->dbg_lnum > after && (lnum == 0 || bp->dbg_lnum < lnum)))))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 {
778 regmatch.regprog = bp->dbg_prog;
779 regmatch.rm_ic = FALSE;
780 /*
Bram Moolenaar05159a02005-02-26 23:04:13 +0000781 * Save the value of got_int and reset it. We don't want a
782 * previous interruption cancel matching, only hitting CTRL-C
783 * while matching should abort it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 */
785 prev_got_int = got_int;
786 got_int = FALSE;
787 if (vim_regexec(&regmatch, name, (colnr_T)0))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000788 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 lnum = bp->dbg_lnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000790 if (fp != NULL)
791 *fp = bp->dbg_forceit;
792 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 got_int |= prev_got_int;
794 }
795 }
796 if (name != fname)
797 vim_free(name);
798
799 return lnum;
800}
801
802/*
803 * Called when a breakpoint was encountered.
804 */
805 void
806dbg_breakpoint(name, lnum)
807 char_u *name;
808 linenr_T lnum;
809{
810 /* We need to check if this line is actually executed in do_one_cmd() */
811 debug_breakpoint_name = name;
812 debug_breakpoint_lnum = lnum;
813}
Bram Moolenaar05159a02005-02-26 23:04:13 +0000814
815
816# if defined(FEAT_PROFILE) || defined(PROTO)
817/*
818 * Functions for profiling.
819 */
820static void script_do_profile __ARGS((scriptitem_T *si));
821static void script_dump_profile __ARGS((FILE *fd));
822static proftime_T prof_wait_time;
823
824/*
825 * Set the time in "tm" to zero.
826 */
827 void
828profile_zero(tm)
829 proftime_T *tm;
830{
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +0000831# ifdef WIN3264
832 tm->QuadPart = 0;
833# else
Bram Moolenaar05159a02005-02-26 23:04:13 +0000834 tm->tv_usec = 0;
835 tm->tv_sec = 0;
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +0000836# endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000837}
838
839/*
840 * Store the current time in "tm".
841 */
842 void
843profile_start(tm)
844 proftime_T *tm;
845{
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +0000846# ifdef WIN3264
847 QueryPerformanceCounter(tm);
848# else
Bram Moolenaar05159a02005-02-26 23:04:13 +0000849 gettimeofday(tm, NULL);
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +0000850# endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000851}
852
853/*
854 * Compute the elapsed time from "tm" till now and store in "tm".
855 */
856 void
857profile_end(tm)
858 proftime_T *tm;
859{
860 proftime_T now;
861
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +0000862# ifdef WIN3264
863 QueryPerformanceCounter(&now);
864 tm->QuadPart = now.QuadPart - tm->QuadPart;
865# else
Bram Moolenaar05159a02005-02-26 23:04:13 +0000866 gettimeofday(&now, NULL);
867 tm->tv_usec = now.tv_usec - tm->tv_usec;
868 tm->tv_sec = now.tv_sec - tm->tv_sec;
869 if (tm->tv_usec < 0)
870 {
871 tm->tv_usec += 1000000;
872 --tm->tv_sec;
873 }
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +0000874# endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000875}
876
877/*
878 * Subtract the time "tm2" from "tm".
879 */
880 void
881profile_sub(tm, tm2)
882 proftime_T *tm, *tm2;
883{
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +0000884# ifdef WIN3264
885 tm->QuadPart -= tm2->QuadPart;
886# else
Bram Moolenaar05159a02005-02-26 23:04:13 +0000887 tm->tv_usec -= tm2->tv_usec;
888 tm->tv_sec -= tm2->tv_sec;
889 if (tm->tv_usec < 0)
890 {
891 tm->tv_usec += 1000000;
892 --tm->tv_sec;
893 }
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +0000894# endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000895}
896
897/*
898 * Add the time "tm2" to "tm".
899 */
900 void
901profile_add(tm, tm2)
902 proftime_T *tm, *tm2;
903{
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +0000904# ifdef WIN3264
905 tm->QuadPart += tm2->QuadPart;
906# else
Bram Moolenaar05159a02005-02-26 23:04:13 +0000907 tm->tv_usec += tm2->tv_usec;
908 tm->tv_sec += tm2->tv_sec;
909 if (tm->tv_usec >= 1000000)
910 {
911 tm->tv_usec -= 1000000;
912 ++tm->tv_sec;
913 }
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +0000914# endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000915}
916
917/*
918 * Get the current waittime.
919 */
920 void
921profile_get_wait(tm)
922 proftime_T *tm;
923{
924 *tm = prof_wait_time;
925}
926
927/*
928 * Subtract the passed waittime since "tm" from "tma".
929 */
930 void
931profile_sub_wait(tm, tma)
932 proftime_T *tm, *tma;
933{
934 proftime_T tm3 = prof_wait_time;
935
936 profile_sub(&tm3, tm);
937 profile_sub(tma, &tm3);
938}
939
940/*
941 * Return TRUE if "tm1" and "tm2" are equal.
942 */
943 int
944profile_equal(tm1, tm2)
945 proftime_T *tm1, *tm2;
946{
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +0000947# ifdef WIN3264
948 return (tm1->QuadPart == tm2->QuadPart);
949# else
Bram Moolenaar05159a02005-02-26 23:04:13 +0000950 return (tm1->tv_usec == tm2->tv_usec && tm1->tv_sec == tm2->tv_sec);
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +0000951# endif
952}
953
954/*
955 * Return <0, 0 or >0 if "tm1" < "tm2", "tm1" == "tm2" or "tm1" > "tm2"
956 */
957 int
958profile_cmp(tm1, tm2)
959 proftime_T *tm1, *tm2;
960{
961# ifdef WIN3264
962 return (int)(tm2->QuadPart - tm1->QuadPart);
963# else
964 if (tm1->tv_sec == tm2->tv_sec)
965 return tm2->tv_usec - tm1->tv_usec;
966 return tm2->tv_sec - tm1->tv_sec;
967# endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000968}
969
970/*
971 * Return a string that represents a time.
972 * Uses a static buffer!
973 */
974 char *
975profile_msg(tm)
976 proftime_T *tm;
977{
978 static char buf[50];
979
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +0000980# ifdef WIN3264
981 LARGE_INTEGER fr;
982
983 QueryPerformanceFrequency(&fr);
984 sprintf(buf, "%10.6lf", (double)tm->QuadPart / (double)fr.QuadPart);
985# else
Bram Moolenaar05159a02005-02-26 23:04:13 +0000986 sprintf(buf, "%3ld.%06ld", (long)tm->tv_sec, (long)tm->tv_usec);
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +0000987#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000988 return buf;
989}
990
991static char_u *profile_fname = NULL;
992
993/*
994 * ":profile cmd args"
995 */
996 void
997ex_profile(eap)
998 exarg_T *eap;
999{
1000 char_u *e;
1001 int len;
1002
1003 e = skiptowhite(eap->arg);
1004 len = e - eap->arg;
1005 e = skipwhite(e);
1006
1007 if (len == 5 && STRNCMP(eap->arg, "start", 5) == 0 && *e != NUL)
1008 {
1009 vim_free(profile_fname);
1010 profile_fname = vim_strsave(e);
1011 do_profiling = TRUE;
1012 profile_zero(&prof_wait_time);
1013 set_vim_var_nr(VV_PROFILING, 1L);
1014 }
1015 else if (!do_profiling)
1016 EMSG(_("E750: First use :profile start <fname>"));
1017 else
1018 {
1019 /* The rest is similar to ":breakadd". */
1020 ex_breakadd(eap);
1021 }
1022}
1023
1024/*
1025 * Dump the profiling info.
1026 */
1027 void
1028profile_dump()
1029{
1030 FILE *fd;
1031
1032 if (profile_fname != NULL)
1033 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001034 fd = mch_fopen((char *)profile_fname, "w");
Bram Moolenaar05159a02005-02-26 23:04:13 +00001035 if (fd == NULL)
1036 EMSG2(_(e_notopen), profile_fname);
1037 else
1038 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00001039 script_dump_profile(fd);
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001040 func_dump_profile(fd);
Bram Moolenaar05159a02005-02-26 23:04:13 +00001041 fclose(fd);
1042 }
1043 }
1044}
1045
1046/*
1047 * Start profiling script "fp".
1048 */
1049 static void
1050script_do_profile(si)
1051 scriptitem_T *si;
1052{
1053 si->sn_pr_count = 0;
1054 profile_zero(&si->sn_pr_total);
1055 profile_zero(&si->sn_pr_self);
1056
1057 ga_init2(&si->sn_prl_ga, sizeof(sn_prl_T), 100);
1058 si->sn_prl_idx = -1;
1059 si->sn_prof_on = TRUE;
1060 si->sn_pr_nest = 0;
1061}
1062
1063/*
1064 * save time when starting to invoke another script or function.
1065 */
1066 void
1067script_prof_save(tm)
1068 proftime_T *tm; /* place to store wait time */
1069{
1070 scriptitem_T *si;
1071
1072 if (current_SID > 0 && current_SID <= script_items.ga_len)
1073 {
1074 si = &SCRIPT_ITEM(current_SID);
1075 if (si->sn_prof_on && si->sn_pr_nest++ == 0)
1076 profile_start(&si->sn_pr_child);
1077 }
1078 profile_get_wait(tm);
1079}
1080
1081/*
1082 * Count time spent in children after invoking another script or function.
1083 */
1084 void
1085script_prof_restore(tm)
1086 proftime_T *tm;
1087{
1088 scriptitem_T *si;
1089
1090 if (current_SID > 0 && current_SID <= script_items.ga_len)
1091 {
1092 si = &SCRIPT_ITEM(current_SID);
1093 if (si->sn_prof_on && --si->sn_pr_nest == 0)
1094 {
1095 profile_end(&si->sn_pr_child);
1096 profile_sub_wait(tm, &si->sn_pr_child); /* don't count wait time */
1097 profile_add(&si->sn_pr_children, &si->sn_pr_child);
1098 profile_add(&si->sn_prl_children, &si->sn_pr_child);
1099 }
1100 }
1101}
1102
1103static proftime_T inchar_time;
1104
1105/*
1106 * Called when starting to wait for the user to type a character.
1107 */
1108 void
1109prof_inchar_enter()
1110{
1111 profile_start(&inchar_time);
1112}
1113
1114/*
1115 * Called when finished waiting for the user to type a character.
1116 */
1117 void
1118prof_inchar_exit()
1119{
1120 profile_end(&inchar_time);
1121 profile_add(&prof_wait_time, &inchar_time);
1122}
1123
1124/*
1125 * Dump the profiling results for all scripts in file "fd".
1126 */
1127 static void
1128script_dump_profile(fd)
1129 FILE *fd;
1130{
1131 int id;
1132 scriptitem_T *si;
1133 int i;
1134 FILE *sfd;
1135 sn_prl_T *pp;
1136
1137 for (id = 1; id <= script_items.ga_len; ++id)
1138 {
1139 si = &SCRIPT_ITEM(id);
1140 if (si->sn_prof_on)
1141 {
1142 fprintf(fd, "SCRIPT %s\n", si->sn_name);
1143 if (si->sn_pr_count == 1)
1144 fprintf(fd, "Sourced 1 time\n");
1145 else
1146 fprintf(fd, "Sourced %d times\n", si->sn_pr_count);
1147 fprintf(fd, "Total time: %s\n", profile_msg(&si->sn_pr_total));
1148 fprintf(fd, " Self time: %s\n", profile_msg(&si->sn_pr_self));
1149 fprintf(fd, "\n");
1150 fprintf(fd, "count total (s) self (s)\n");
1151
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001152 sfd = mch_fopen((char *)si->sn_name, "r");
Bram Moolenaar05159a02005-02-26 23:04:13 +00001153 if (sfd == NULL)
1154 fprintf(fd, "Cannot open file!\n");
1155 else
1156 {
1157 for (i = 0; i < si->sn_prl_ga.ga_len; ++i)
1158 {
1159 if (vim_fgets(IObuff, IOSIZE, sfd))
1160 break;
1161 pp = &PRL_ITEM(si, i);
1162 if (pp->snp_count > 0)
1163 {
1164 fprintf(fd, "%5d ", pp->snp_count);
1165 if (profile_equal(&pp->sn_prl_total, &pp->sn_prl_self))
1166 fprintf(fd, " ");
1167 else
1168 fprintf(fd, "%s ", profile_msg(&pp->sn_prl_total));
1169 fprintf(fd, "%s ", profile_msg(&pp->sn_prl_self));
1170 }
1171 else
1172 fprintf(fd, " ");
1173 fprintf(fd, "%s", IObuff);
1174 }
1175 fclose(sfd);
1176 }
1177 fprintf(fd, "\n");
1178 }
1179 }
1180}
1181
1182/*
1183 * Return TRUE when a function defined in the current script should be
1184 * profiled.
1185 */
1186 int
1187prof_def_func()
1188{
Bram Moolenaar53180ce2005-07-05 21:48:14 +00001189 if (current_SID > 0)
1190 return SCRIPT_ITEM(current_SID).sn_pr_force;
1191 return FALSE;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001192}
1193
1194# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195#endif
1196
1197/*
1198 * If 'autowrite' option set, try to write the file.
1199 * Careful: autocommands may make "buf" invalid!
1200 *
1201 * return FAIL for failure, OK otherwise
1202 */
1203 int
1204autowrite(buf, forceit)
1205 buf_T *buf;
1206 int forceit;
1207{
1208 if (!(p_aw || p_awa) || !p_write
1209#ifdef FEAT_QUICKFIX
1210 /* never autowrite a "nofile" or "nowrite" buffer */
1211 || bt_dontwrite(buf)
1212#endif
1213 || (!forceit && buf->b_p_ro) || buf->b_ffname == NULL)
1214 return FAIL;
1215 return buf_write_all(buf, forceit);
1216}
1217
1218/*
1219 * flush all buffers, except the ones that are readonly
1220 */
1221 void
1222autowrite_all()
1223{
1224 buf_T *buf;
1225
1226 if (!(p_aw || p_awa) || !p_write)
1227 return;
1228 for (buf = firstbuf; buf; buf = buf->b_next)
1229 if (bufIsChanged(buf) && !buf->b_p_ro)
1230 {
1231 (void)buf_write_all(buf, FALSE);
1232#ifdef FEAT_AUTOCMD
1233 /* an autocommand may have deleted the buffer */
1234 if (!buf_valid(buf))
1235 buf = firstbuf;
1236#endif
1237 }
1238}
1239
1240/*
1241 * return TRUE if buffer was changed and cannot be abandoned.
1242 */
1243/*ARGSUSED*/
1244 int
1245check_changed(buf, checkaw, mult_win, forceit, allbuf)
1246 buf_T *buf;
1247 int checkaw; /* do autowrite if buffer was changed */
1248 int mult_win; /* check also when several wins for the buf */
1249 int forceit;
1250 int allbuf; /* may write all buffers */
1251{
1252 if ( !forceit
1253 && bufIsChanged(buf)
1254 && (mult_win || buf->b_nwindows <= 1)
1255 && (!checkaw || autowrite(buf, forceit) == FAIL))
1256 {
1257#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1258 if ((p_confirm || cmdmod.confirm) && p_write)
1259 {
1260 buf_T *buf2;
1261 int count = 0;
1262
1263 if (allbuf)
1264 for (buf2 = firstbuf; buf2 != NULL; buf2 = buf2->b_next)
1265 if (bufIsChanged(buf2)
1266 && (buf2->b_ffname != NULL
1267# ifdef FEAT_BROWSE
1268 || cmdmod.browse
1269# endif
1270 ))
1271 ++count;
1272# ifdef FEAT_AUTOCMD
1273 if (!buf_valid(buf))
1274 /* Autocommand deleted buffer, oops! It's not changed now. */
1275 return FALSE;
1276# endif
1277 dialog_changed(buf, count > 1);
1278# ifdef FEAT_AUTOCMD
1279 if (!buf_valid(buf))
1280 /* Autocommand deleted buffer, oops! It's not changed now. */
1281 return FALSE;
1282# endif
1283 return bufIsChanged(buf);
1284 }
1285#endif
1286 EMSG(_(e_nowrtmsg));
1287 return TRUE;
1288 }
1289 return FALSE;
1290}
1291
1292#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
1293
1294#if defined(FEAT_BROWSE) || defined(PROTO)
1295/*
1296 * When wanting to write a file without a file name, ask the user for a name.
1297 */
1298 void
1299browse_save_fname(buf)
1300 buf_T *buf;
1301{
1302 if (buf->b_fname == NULL)
1303 {
1304 char_u *fname;
1305
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00001306 fname = do_browse(BROWSE_SAVE, (char_u *)_("Save As"),
1307 NULL, NULL, NULL, NULL, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 if (fname != NULL)
1309 {
1310 if (setfname(buf, fname, NULL, TRUE) == OK)
1311 buf->b_flags |= BF_NOTEDITED;
1312 vim_free(fname);
1313 }
1314 }
1315}
1316#endif
1317
1318/*
1319 * Ask the user what to do when abondoning a changed buffer.
1320 * Must check 'write' option first!
1321 */
1322 void
1323dialog_changed(buf, checkall)
1324 buf_T *buf;
1325 int checkall; /* may abandon all changed buffers */
1326{
1327 char_u buff[IOSIZE];
1328 int ret;
1329 buf_T *buf2;
1330
Bram Moolenaar82cf9b62005-06-07 21:09:25 +00001331 dialog_msg(buff, _("Save changes to \"%s\"?"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 (buf->b_fname != NULL) ?
1333 buf->b_fname : (char_u *)_("Untitled"));
1334 if (checkall)
1335 ret = vim_dialog_yesnoallcancel(VIM_QUESTION, NULL, buff, 1);
1336 else
1337 ret = vim_dialog_yesnocancel(VIM_QUESTION, NULL, buff, 1);
1338
1339 if (ret == VIM_YES)
1340 {
1341#ifdef FEAT_BROWSE
1342 /* May get file name, when there is none */
1343 browse_save_fname(buf);
1344#endif
1345 if (buf->b_fname != NULL) /* didn't hit Cancel */
1346 (void)buf_write_all(buf, FALSE);
1347 }
1348 else if (ret == VIM_NO)
1349 {
1350 unchanged(buf, TRUE);
1351 }
1352 else if (ret == VIM_ALL)
1353 {
1354 /*
1355 * Write all modified files that can be written.
1356 * Skip readonly buffers, these need to be confirmed
1357 * individually.
1358 */
1359 for (buf2 = firstbuf; buf2 != NULL; buf2 = buf2->b_next)
1360 {
1361 if (bufIsChanged(buf2)
1362 && (buf2->b_ffname != NULL
1363#ifdef FEAT_BROWSE
1364 || cmdmod.browse
1365#endif
1366 )
1367 && !buf2->b_p_ro)
1368 {
1369#ifdef FEAT_BROWSE
1370 /* May get file name, when there is none */
1371 browse_save_fname(buf2);
1372#endif
1373 if (buf2->b_fname != NULL) /* didn't hit Cancel */
1374 (void)buf_write_all(buf2, FALSE);
1375#ifdef FEAT_AUTOCMD
1376 /* an autocommand may have deleted the buffer */
1377 if (!buf_valid(buf2))
1378 buf2 = firstbuf;
1379#endif
1380 }
1381 }
1382 }
1383 else if (ret == VIM_DISCARDALL)
1384 {
1385 /*
1386 * mark all buffers as unchanged
1387 */
1388 for (buf2 = firstbuf; buf2 != NULL; buf2 = buf2->b_next)
1389 unchanged(buf2, TRUE);
1390 }
1391}
1392#endif
1393
1394/*
1395 * Return TRUE if the buffer "buf" can be abandoned, either by making it
1396 * hidden, autowriting it or unloading it.
1397 */
1398 int
1399can_abandon(buf, forceit)
1400 buf_T *buf;
1401 int forceit;
1402{
1403 return ( P_HID(buf)
1404 || !bufIsChanged(buf)
1405 || buf->b_nwindows > 1
1406 || autowrite(buf, forceit) == OK
1407 || forceit);
1408}
1409
1410/*
1411 * Return TRUE if any buffer was changed and cannot be abandoned.
1412 * That changed buffer becomes the current buffer.
1413 */
1414 int
1415check_changed_any(hidden)
1416 int hidden; /* Only check hidden buffers */
1417{
1418 buf_T *buf;
1419 int save;
1420#ifdef FEAT_WINDOWS
1421 win_T *wp;
1422#endif
1423
1424 for (;;)
1425 {
1426 /* check curbuf first: if it was changed we can't abandon it */
1427 if (!hidden && curbufIsChanged())
1428 buf = curbuf;
1429 else
1430 {
1431 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1432 if ((!hidden || buf->b_nwindows == 0) && bufIsChanged(buf))
1433 break;
1434 }
1435 if (buf == NULL) /* No buffers changed */
1436 return FALSE;
1437
1438 if (check_changed(buf, p_awa, TRUE, FALSE, TRUE) && buf_valid(buf))
1439 break; /* didn't save - still changes */
1440 }
1441
1442 exiting = FALSE;
1443#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1444 /*
1445 * When ":confirm" used, don't give an error message.
1446 */
1447 if (!(p_confirm || cmdmod.confirm))
1448#endif
1449 {
1450 /* There must be a wait_return for this message, do_buffer()
1451 * may cause a redraw. But wait_return() is a no-op when vgetc()
1452 * is busy (Quit used from window menu), then make sure we don't
1453 * cause a scroll up. */
1454 if (vgetc_busy)
1455 {
1456 msg_row = cmdline_row;
1457 msg_col = 0;
1458 msg_didout = FALSE;
1459 }
1460 if (EMSG2(_("E162: No write since last change for buffer \"%s\""),
1461 buf_spname(buf) != NULL ? (char_u *)buf_spname(buf) :
1462 buf->b_fname))
1463 {
1464 save = no_wait_return;
1465 no_wait_return = FALSE;
1466 wait_return(FALSE);
1467 no_wait_return = save;
1468 }
1469 }
1470
1471#ifdef FEAT_WINDOWS
1472 /* Try to find a window that contains the buffer. */
1473 if (buf != curbuf)
1474 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1475 if (wp->w_buffer == buf)
1476 {
1477 win_goto(wp);
1478# ifdef FEAT_AUTOCMD
1479 /* Paranoia: did autocms wipe out the buffer with changes? */
1480 if (!buf_valid(buf))
1481 return TRUE;
1482# endif
1483 break;
1484 }
1485#endif
1486
1487 /* Open the changed buffer in the current window. */
1488 if (buf != curbuf)
1489 set_curbuf(buf, DOBUF_GOTO);
1490
1491 return TRUE;
1492}
1493
1494/*
1495 * return FAIL if there is no file name, OK if there is one
1496 * give error message for FAIL
1497 */
1498 int
1499check_fname()
1500{
1501 if (curbuf->b_ffname == NULL)
1502 {
1503 EMSG(_(e_noname));
1504 return FAIL;
1505 }
1506 return OK;
1507}
1508
1509/*
1510 * flush the contents of a buffer, unless it has no file name
1511 *
1512 * return FAIL for failure, OK otherwise
1513 */
1514 int
1515buf_write_all(buf, forceit)
1516 buf_T *buf;
1517 int forceit;
1518{
1519 int retval;
1520#ifdef FEAT_AUTOCMD
1521 buf_T *old_curbuf = curbuf;
1522#endif
1523
1524 retval = (buf_write(buf, buf->b_ffname, buf->b_fname,
1525 (linenr_T)1, buf->b_ml.ml_line_count, NULL,
1526 FALSE, forceit, TRUE, FALSE));
1527#ifdef FEAT_AUTOCMD
1528 if (curbuf != old_curbuf)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001529 {
1530 msg_source(hl_attr(HLF_W));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531 MSG(_("Warning: Entered other buffer unexpectedly (check autocommands)"));
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533#endif
1534 return retval;
1535}
1536
1537/*
1538 * Code to handle the argument list.
1539 */
1540
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001541static char_u *do_one_arg __ARGS((char_u *str));
1542static int do_arglist __ARGS((char_u *str, int what, int after));
1543static void alist_check_arg_idx __ARGS((void));
1544static int editing_arg_idx __ARGS((win_T *win));
1545#ifdef FEAT_LISTCMDS
1546static int alist_add_list __ARGS((int count, char_u **files, int after));
1547#endif
1548#define AL_SET 1
1549#define AL_ADD 2
1550#define AL_DEL 3
1551
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001553 * Isolate one argument, taking backticks.
1554 * Changes the argument in-place, puts a NUL after it. Backticks remain.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555 * Return a pointer to the start of the next argument.
1556 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001557 static char_u *
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558do_one_arg(str)
1559 char_u *str;
1560{
1561 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 int inbacktick;
1563
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564 inbacktick = FALSE;
1565 for (p = str; *str; ++str)
1566 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001567 /* When the backslash is used for escaping the special meaning of a
1568 * character we need to keep it until wildcard expansion. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 if (rem_backslash(str))
1570 {
1571 *p++ = *str++;
1572 *p++ = *str;
1573 }
1574 else
1575 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001576 /* An item ends at a space not in backticks */
1577 if (!inbacktick && vim_isspace(*str))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 break;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001579 if (*str == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 inbacktick ^= TRUE;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001581 *p++ = *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 }
1583 }
1584 str = skipwhite(str);
1585 *p = NUL;
1586
1587 return str;
1588}
1589
Bram Moolenaar86b68352004-12-27 21:59:20 +00001590/*
1591 * Separate the arguments in "str" and return a list of pointers in the
1592 * growarray "gap".
1593 */
1594 int
1595get_arglist(gap, str)
1596 garray_T *gap;
1597 char_u *str;
1598{
1599 ga_init2(gap, (int)sizeof(char_u *), 20);
1600 while (*str != NUL)
1601 {
1602 if (ga_grow(gap, 1) == FAIL)
1603 {
1604 ga_clear(gap);
1605 return FAIL;
1606 }
1607 ((char_u **)gap->ga_data)[gap->ga_len++] = str;
1608
1609 /* Isolate one argument, change it in-place, put a NUL after it. */
1610 str = do_one_arg(str);
1611 }
1612 return OK;
1613}
1614
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00001615#if defined(FEAT_QUICKFIX) || (defined(FEAT_SYN_HL) && defined(FEAT_MBYTE)) \
1616 || defined(PROTO)
1617/*
1618 * Parse a list of arguments (file names), expand them and return in
1619 * "fnames[fcountp]".
1620 * Return FAIL or OK.
1621 */
1622 int
1623get_arglist_exp(str, fcountp, fnamesp)
1624 char_u *str;
1625 int *fcountp;
1626 char_u ***fnamesp;
1627{
1628 garray_T ga;
1629 int i;
1630
1631 if (get_arglist(&ga, str) == FAIL)
1632 return FAIL;
1633 i = gen_expand_wildcards(ga.ga_len, (char_u **)ga.ga_data,
1634 fcountp, fnamesp, EW_FILE|EW_NOTFOUND);
1635 ga_clear(&ga);
1636 return i;
1637}
1638#endif
1639
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640#if defined(FEAT_GUI) || defined(FEAT_CLIENTSERVER) || defined(PROTO)
1641/*
1642 * Redefine the argument list.
1643 */
1644 void
1645set_arglist(str)
1646 char_u *str;
1647{
1648 do_arglist(str, AL_SET, 0);
1649}
1650#endif
1651
1652/*
1653 * "what" == AL_SET: Redefine the argument list to 'str'.
1654 * "what" == AL_ADD: add files in 'str' to the argument list after "after".
1655 * "what" == AL_DEL: remove files in 'str' from the argument list.
1656 *
1657 * Return FAIL for failure, OK otherwise.
1658 */
1659/*ARGSUSED*/
1660 static int
1661do_arglist(str, what, after)
1662 char_u *str;
1663 int what;
1664 int after; /* 0 means before first one */
1665{
1666 garray_T new_ga;
1667 int exp_count;
1668 char_u **exp_files;
1669 int i;
1670#ifdef FEAT_LISTCMDS
1671 char_u *p;
1672 int match;
1673#endif
1674
1675 /*
1676 * Collect all file name arguments in "new_ga".
1677 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00001678 if (get_arglist(&new_ga, str) == FAIL)
1679 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680
1681#ifdef FEAT_LISTCMDS
1682 if (what == AL_DEL)
1683 {
1684 regmatch_T regmatch;
1685 int didone;
1686
1687 /*
1688 * Delete the items: use each item as a regexp and find a match in the
1689 * argument list.
1690 */
1691#ifdef CASE_INSENSITIVE_FILENAME
1692 regmatch.rm_ic = TRUE; /* Always ignore case */
1693#else
1694 regmatch.rm_ic = FALSE; /* Never ignore case */
1695#endif
1696 for (i = 0; i < new_ga.ga_len && !got_int; ++i)
1697 {
1698 p = ((char_u **)new_ga.ga_data)[i];
1699 p = file_pat_to_reg_pat(p, NULL, NULL, FALSE);
1700 if (p == NULL)
1701 break;
1702 regmatch.regprog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
1703 if (regmatch.regprog == NULL)
1704 {
1705 vim_free(p);
1706 break;
1707 }
1708
1709 didone = FALSE;
1710 for (match = 0; match < ARGCOUNT; ++match)
1711 if (vim_regexec(&regmatch, alist_name(&ARGLIST[match]),
1712 (colnr_T)0))
1713 {
1714 didone = TRUE;
1715 vim_free(ARGLIST[match].ae_fname);
1716 mch_memmove(ARGLIST + match, ARGLIST + match + 1,
1717 (ARGCOUNT - match - 1) * sizeof(aentry_T));
1718 --ALIST(curwin)->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 if (curwin->w_arg_idx > match)
1720 --curwin->w_arg_idx;
1721 --match;
1722 }
1723
1724 vim_free(regmatch.regprog);
1725 vim_free(p);
1726 if (!didone)
1727 EMSG2(_(e_nomatch2), ((char_u **)new_ga.ga_data)[i]);
1728 }
1729 ga_clear(&new_ga);
1730 }
1731 else
1732#endif
1733 {
1734 i = expand_wildcards(new_ga.ga_len, (char_u **)new_ga.ga_data,
1735 &exp_count, &exp_files, EW_DIR|EW_FILE|EW_ADDSLASH|EW_NOTFOUND);
1736 ga_clear(&new_ga);
1737 if (i == FAIL)
1738 return FAIL;
1739 if (exp_count == 0)
1740 {
1741 EMSG(_(e_nomatch));
1742 return FAIL;
1743 }
1744
1745#ifdef FEAT_LISTCMDS
1746 if (what == AL_ADD)
1747 {
1748 (void)alist_add_list(exp_count, exp_files, after);
1749 vim_free(exp_files);
1750 }
1751 else /* what == AL_SET */
1752#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00001753 alist_set(ALIST(curwin), exp_count, exp_files, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 }
1755
1756 alist_check_arg_idx();
1757
1758 return OK;
1759}
1760
1761/*
1762 * Check the validity of the arg_idx for each other window.
1763 */
1764 static void
1765alist_check_arg_idx()
1766{
1767#ifdef FEAT_WINDOWS
1768 win_T *win;
1769
1770 for (win = firstwin; win != NULL; win = win->w_next)
1771 if (win->w_alist == curwin->w_alist)
1772 check_arg_idx(win);
1773#else
1774 check_arg_idx(curwin);
1775#endif
1776}
1777
1778/*
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001779 * Return TRUE if window "win" is editing then file at the current argument
1780 * index.
1781 */
1782 static int
1783editing_arg_idx(win)
1784 win_T *win;
1785{
1786 return !(win->w_arg_idx >= WARGCOUNT(win)
1787 || (win->w_buffer->b_fnum
1788 != WARGLIST(win)[win->w_arg_idx].ae_fnum
1789 && (win->w_buffer->b_ffname == NULL
1790 || !(fullpathcmp(
1791 alist_name(&WARGLIST(win)[win->w_arg_idx]),
1792 win->w_buffer->b_ffname, TRUE) & FPC_SAME))));
1793}
1794
1795/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 * Check if window "win" is editing the w_arg_idx file in its argument list.
1797 */
1798 void
1799check_arg_idx(win)
1800 win_T *win;
1801{
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001802 if (WARGCOUNT(win) > 1 && !editing_arg_idx(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 {
1804 /* We are not editing the current entry in the argument list.
1805 * Set "arg_had_last" if we are editing the last one. */
1806 win->w_arg_idx_invalid = TRUE;
1807 if (win->w_arg_idx != WARGCOUNT(win) - 1
1808 && arg_had_last == FALSE
1809#ifdef FEAT_WINDOWS
1810 && ALIST(win) == &global_alist
1811#endif
1812 && GARGCOUNT > 0
1813 && win->w_arg_idx < GARGCOUNT
1814 && (win->w_buffer->b_fnum == GARGLIST[GARGCOUNT - 1].ae_fnum
1815 || (win->w_buffer->b_ffname != NULL
1816 && (fullpathcmp(alist_name(&GARGLIST[GARGCOUNT - 1]),
1817 win->w_buffer->b_ffname, TRUE) & FPC_SAME))))
1818 arg_had_last = TRUE;
1819 }
1820 else
1821 {
1822 /* We are editing the current entry in the argument list.
1823 * Set "arg_had_last" if it's also the last one */
1824 win->w_arg_idx_invalid = FALSE;
1825 if (win->w_arg_idx == WARGCOUNT(win) - 1
1826#ifdef FEAT_WINDOWS
1827 && win->w_alist == &global_alist
1828#endif
1829 )
1830 arg_had_last = TRUE;
1831 }
1832}
1833
1834/*
1835 * ":args", ":argslocal" and ":argsglobal".
1836 */
1837 void
1838ex_args(eap)
1839 exarg_T *eap;
1840{
1841 int i;
1842
1843 if (eap->cmdidx != CMD_args)
1844 {
1845#if defined(FEAT_WINDOWS) && defined(FEAT_LISTCMDS)
1846 alist_unlink(ALIST(curwin));
1847 if (eap->cmdidx == CMD_argglobal)
1848 ALIST(curwin) = &global_alist;
1849 else /* eap->cmdidx == CMD_arglocal */
1850 alist_new();
1851#else
1852 ex_ni(eap);
1853 return;
1854#endif
1855 }
1856
1857 if (!ends_excmd(*eap->arg))
1858 {
1859 /*
1860 * ":args file ..": define new argument list, handle like ":next"
1861 * Also for ":argslocal file .." and ":argsglobal file ..".
1862 */
1863 ex_next(eap);
1864 }
1865 else
1866#if defined(FEAT_WINDOWS) && defined(FEAT_LISTCMDS)
1867 if (eap->cmdidx == CMD_args)
1868#endif
1869 {
1870 /*
1871 * ":args": list arguments.
1872 */
1873 if (ARGCOUNT > 0)
1874 {
1875 /* Overwrite the command, for a short list there is no scrolling
1876 * required and no wait_return(). */
1877 gotocmdline(TRUE);
1878 for (i = 0; i < ARGCOUNT; ++i)
1879 {
1880 if (i == curwin->w_arg_idx)
1881 msg_putchar('[');
1882 msg_outtrans(alist_name(&ARGLIST[i]));
1883 if (i == curwin->w_arg_idx)
1884 msg_putchar(']');
1885 msg_putchar(' ');
1886 }
1887 }
1888 }
1889#if defined(FEAT_WINDOWS) && defined(FEAT_LISTCMDS)
1890 else if (eap->cmdidx == CMD_arglocal)
1891 {
1892 garray_T *gap = &curwin->w_alist->al_ga;
1893
1894 /*
1895 * ":argslocal": make a local copy of the global argument list.
1896 */
1897 if (ga_grow(gap, GARGCOUNT) == OK)
1898 for (i = 0; i < GARGCOUNT; ++i)
1899 if (GARGLIST[i].ae_fname != NULL)
1900 {
1901 AARGLIST(curwin->w_alist)[gap->ga_len].ae_fname =
1902 vim_strsave(GARGLIST[i].ae_fname);
1903 AARGLIST(curwin->w_alist)[gap->ga_len].ae_fnum =
1904 GARGLIST[i].ae_fnum;
1905 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 }
1907 }
1908#endif
1909}
1910
1911/*
1912 * ":previous", ":sprevious", ":Next" and ":sNext".
1913 */
1914 void
1915ex_previous(eap)
1916 exarg_T *eap;
1917{
1918 /* If past the last one already, go to the last one. */
1919 if (curwin->w_arg_idx - (int)eap->line2 >= ARGCOUNT)
1920 do_argfile(eap, ARGCOUNT - 1);
1921 else
1922 do_argfile(eap, curwin->w_arg_idx - (int)eap->line2);
1923}
1924
1925/*
1926 * ":rewind", ":first", ":sfirst" and ":srewind".
1927 */
1928 void
1929ex_rewind(eap)
1930 exarg_T *eap;
1931{
1932 do_argfile(eap, 0);
1933}
1934
1935/*
1936 * ":last" and ":slast".
1937 */
1938 void
1939ex_last(eap)
1940 exarg_T *eap;
1941{
1942 do_argfile(eap, ARGCOUNT - 1);
1943}
1944
1945/*
1946 * ":argument" and ":sargument".
1947 */
1948 void
1949ex_argument(eap)
1950 exarg_T *eap;
1951{
1952 int i;
1953
1954 if (eap->addr_count > 0)
1955 i = eap->line2 - 1;
1956 else
1957 i = curwin->w_arg_idx;
1958 do_argfile(eap, i);
1959}
1960
1961/*
1962 * Edit file "argn" of the argument lists.
1963 */
1964 void
1965do_argfile(eap, argn)
1966 exarg_T *eap;
1967 int argn;
1968{
1969 int other;
1970 char_u *p;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001971 int old_arg_idx = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972
1973 if (argn < 0 || argn >= ARGCOUNT)
1974 {
1975 if (ARGCOUNT <= 1)
1976 EMSG(_("E163: There is only one file to edit"));
1977 else if (argn < 0)
1978 EMSG(_("E164: Cannot go before first file"));
1979 else
1980 EMSG(_("E165: Cannot go beyond last file"));
1981 }
1982 else
1983 {
1984 setpcmark();
1985#ifdef FEAT_GUI
1986 need_mouse_correct = TRUE;
1987#endif
1988
1989#ifdef FEAT_WINDOWS
1990 if (*eap->cmd == 's') /* split window first */
1991 {
1992 if (win_split(0, 0) == FAIL)
1993 return;
1994# ifdef FEAT_SCROLLBIND
1995 curwin->w_p_scb = FALSE;
1996# endif
1997 }
1998 else
1999#endif
2000 {
2001 /*
2002 * if 'hidden' set, only check for changed file when re-editing
2003 * the same buffer
2004 */
2005 other = TRUE;
2006 if (P_HID(curbuf))
2007 {
2008 p = fix_fname(alist_name(&ARGLIST[argn]));
2009 other = otherfile(p);
2010 vim_free(p);
2011 }
2012 if ((!P_HID(curbuf) || !other)
2013 && check_changed(curbuf, TRUE, !other, eap->forceit, FALSE))
2014 return;
2015 }
2016
2017 curwin->w_arg_idx = argn;
2018 if (argn == ARGCOUNT - 1
2019#ifdef FEAT_WINDOWS
2020 && curwin->w_alist == &global_alist
2021#endif
2022 )
2023 arg_had_last = TRUE;
2024
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002025 /* Edit the file; always use the last known line number.
2026 * When it fails (e.g. Abort for already edited file) restore the
2027 * argument index. */
2028 if (do_ecmd(0, alist_name(&ARGLIST[curwin->w_arg_idx]), NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 eap, ECMD_LAST,
2030 (P_HID(curwin->w_buffer) ? ECMD_HIDE : 0) +
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002031 (eap->forceit ? ECMD_FORCEIT : 0)) == FAIL)
2032 curwin->w_arg_idx = old_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 /* like Vi: set the mark where the cursor is in the file. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002034 else if (eap->cmdidx != CMD_argdo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 setmark('\'');
2036 }
2037}
2038
2039/*
2040 * ":next", and commands that behave like it.
2041 */
2042 void
2043ex_next(eap)
2044 exarg_T *eap;
2045{
2046 int i;
2047
2048 /*
2049 * check for changed buffer now, if this fails the argument list is not
2050 * redefined.
2051 */
2052 if ( P_HID(curbuf)
2053 || eap->cmdidx == CMD_snext
2054 || !check_changed(curbuf, TRUE, FALSE, eap->forceit, FALSE))
2055 {
2056 if (*eap->arg != NUL) /* redefine file list */
2057 {
2058 if (do_arglist(eap->arg, AL_SET, 0) == FAIL)
2059 return;
2060 i = 0;
2061 }
2062 else
2063 i = curwin->w_arg_idx + (int)eap->line2;
2064 do_argfile(eap, i);
2065 }
2066}
2067
2068#ifdef FEAT_LISTCMDS
2069/*
2070 * ":argedit"
2071 */
2072 void
2073ex_argedit(eap)
2074 exarg_T *eap;
2075{
2076 int fnum;
2077 int i;
2078 char_u *s;
2079
2080 /* Add the argument to the buffer list and get the buffer number. */
2081 fnum = buflist_add(eap->arg, BLN_LISTED);
2082
2083 /* Check if this argument is already in the argument list. */
2084 for (i = 0; i < ARGCOUNT; ++i)
2085 if (ARGLIST[i].ae_fnum == fnum)
2086 break;
2087 if (i == ARGCOUNT)
2088 {
2089 /* Can't find it, add it to the argument list. */
2090 s = vim_strsave(eap->arg);
2091 if (s == NULL)
2092 return;
2093 i = alist_add_list(1, &s,
2094 eap->addr_count > 0 ? (int)eap->line2 : curwin->w_arg_idx + 1);
2095 if (i < 0)
2096 return;
2097 curwin->w_arg_idx = i;
2098 }
2099
2100 alist_check_arg_idx();
2101
2102 /* Edit the argument. */
2103 do_argfile(eap, i);
2104}
2105
2106/*
2107 * ":argadd"
2108 */
2109 void
2110ex_argadd(eap)
2111 exarg_T *eap;
2112{
2113 do_arglist(eap->arg, AL_ADD,
2114 eap->addr_count > 0 ? (int)eap->line2 : curwin->w_arg_idx + 1);
2115#ifdef FEAT_TITLE
2116 maketitle();
2117#endif
2118}
2119
2120/*
2121 * ":argdelete"
2122 */
2123 void
2124ex_argdelete(eap)
2125 exarg_T *eap;
2126{
2127 int i;
2128 int n;
2129
2130 if (eap->addr_count > 0)
2131 {
2132 /* ":1,4argdel": Delete all arguments in the range. */
2133 if (eap->line2 > ARGCOUNT)
2134 eap->line2 = ARGCOUNT;
2135 n = eap->line2 - eap->line1 + 1;
2136 if (*eap->arg != NUL || n <= 0)
2137 EMSG(_(e_invarg));
2138 else
2139 {
2140 for (i = eap->line1; i <= eap->line2; ++i)
2141 vim_free(ARGLIST[i - 1].ae_fname);
2142 mch_memmove(ARGLIST + eap->line1 - 1, ARGLIST + eap->line2,
2143 (size_t)((ARGCOUNT - eap->line2) * sizeof(aentry_T)));
2144 ALIST(curwin)->al_ga.ga_len -= n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 if (curwin->w_arg_idx >= eap->line2)
2146 curwin->w_arg_idx -= n;
2147 else if (curwin->w_arg_idx > eap->line1)
2148 curwin->w_arg_idx = eap->line1;
2149 }
2150 }
2151 else if (*eap->arg == NUL)
2152 EMSG(_(e_argreq));
2153 else
2154 do_arglist(eap->arg, AL_DEL, 0);
2155#ifdef FEAT_TITLE
2156 maketitle();
2157#endif
2158}
2159
2160/*
2161 * ":argdo", ":windo", ":bufdo"
2162 */
2163 void
2164ex_listdo(eap)
2165 exarg_T *eap;
2166{
2167 int i;
2168#ifdef FEAT_WINDOWS
2169 win_T *win;
2170#endif
2171 buf_T *buf;
2172 int next_fnum = 0;
2173#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
2174 char_u *save_ei = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002176 char_u *p_shm_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177
2178#ifndef FEAT_WINDOWS
2179 if (eap->cmdidx == CMD_windo)
2180 {
2181 ex_ni(eap);
2182 return;
2183 }
2184#endif
2185
2186#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
2187 if (eap->cmdidx != CMD_windo)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002188 /* Don't do syntax HL autocommands. Skipping the syntax file is a
2189 * great speed improvement. */
2190 save_ei = au_event_disable(",Syntax");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191#endif
2192
2193 if (eap->cmdidx == CMD_windo
2194 || P_HID(curbuf)
2195 || !check_changed(curbuf, TRUE, FALSE, eap->forceit, FALSE))
2196 {
2197 /* start at the first argument/window/buffer */
2198 i = 0;
2199#ifdef FEAT_WINDOWS
2200 win = firstwin;
2201#endif
2202 /* set pcmark now */
2203 if (eap->cmdidx == CMD_bufdo)
2204 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
2205 else
2206 setpcmark();
2207 listcmd_busy = TRUE; /* avoids setting pcmark below */
2208
2209 while (!got_int)
2210 {
2211 if (eap->cmdidx == CMD_argdo)
2212 {
2213 /* go to argument "i" */
2214 if (i == ARGCOUNT)
2215 break;
2216 /* Don't call do_argfile() when already there, it will try
2217 * reloading the file. */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002218 if (curwin->w_arg_idx != i || !editing_arg_idx(curwin))
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002219 {
2220 /* Clear 'shm' to avoid that the file message overwrites
2221 * any output from the command. */
2222 p_shm_save = vim_strsave(p_shm);
2223 set_option_value((char_u *)"shm", 0L, (char_u *)"", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 do_argfile(eap, i);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002225 set_option_value((char_u *)"shm", 0L, p_shm_save, 0);
2226 vim_free(p_shm_save);
2227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 if (curwin->w_arg_idx != i)
2229 break;
2230 ++i;
2231 }
2232#ifdef FEAT_WINDOWS
2233 else if (eap->cmdidx == CMD_windo)
2234 {
2235 /* go to window "win" */
2236 if (!win_valid(win))
2237 break;
2238 win_goto(win);
2239 win = win->w_next;
2240 }
2241#endif
2242 else if (eap->cmdidx == CMD_bufdo)
2243 {
2244 /* Remember the number of the next listed buffer, in case
2245 * ":bwipe" is used or autocommands do something strange. */
2246 next_fnum = -1;
2247 for (buf = curbuf->b_next; buf != NULL; buf = buf->b_next)
2248 if (buf->b_p_bl)
2249 {
2250 next_fnum = buf->b_fnum;
2251 break;
2252 }
2253 }
2254
2255 /* execute the command */
2256 do_cmdline(eap->arg, eap->getline, eap->cookie,
2257 DOCMD_VERBOSE + DOCMD_NOWAIT);
2258
2259 if (eap->cmdidx == CMD_bufdo)
2260 {
2261 /* Done? */
2262 if (next_fnum < 0)
2263 break;
2264 /* Check if the buffer still exists. */
2265 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2266 if (buf->b_fnum == next_fnum)
2267 break;
2268 if (buf == NULL)
2269 break;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002270
2271 /* Go to the next buffer. Clear 'shm' to avoid that the file
2272 * message overwrites any output from the command. */
2273 p_shm_save = vim_strsave(p_shm);
2274 set_option_value((char_u *)"shm", 0L, (char_u *)"", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 goto_buffer(eap, DOBUF_FIRST, FORWARD, next_fnum);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002276 set_option_value((char_u *)"shm", 0L, p_shm_save, 0);
2277 vim_free(p_shm_save);
2278
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 /* If autocommands took us elsewhere, quit here */
2280 if (curbuf->b_fnum != next_fnum)
2281 break;
2282 }
2283
2284 if (eap->cmdidx == CMD_windo)
2285 {
2286 validate_cursor(); /* cursor may have moved */
2287#ifdef FEAT_SCROLLBIND
2288 /* required when 'scrollbind' has been set */
2289 if (curwin->w_p_scb)
2290 do_check_scrollbind(TRUE);
2291#endif
2292 }
2293 }
2294 listcmd_busy = FALSE;
2295 }
2296
2297#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
Bram Moolenaar6ac54292005-02-02 23:07:25 +00002298 if (save_ei != NULL)
2299 {
2300 au_event_restore(save_ei);
2301 apply_autocmds(EVENT_SYNTAX, curbuf->b_p_syn,
2302 curbuf->b_fname, TRUE, curbuf);
2303 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304#endif
2305}
2306
2307/*
2308 * Add files[count] to the arglist of the current window after arg "after".
2309 * The file names in files[count] must have been allocated and are taken over.
2310 * Files[] itself is not taken over.
2311 * Returns index of first added argument. Returns -1 when failed (out of mem).
2312 */
2313 static int
2314alist_add_list(count, files, after)
2315 int count;
2316 char_u **files;
2317 int after; /* where to add: 0 = before first one */
2318{
2319 int i;
2320
2321 if (ga_grow(&ALIST(curwin)->al_ga, count) == OK)
2322 {
2323 if (after < 0)
2324 after = 0;
2325 if (after > ARGCOUNT)
2326 after = ARGCOUNT;
2327 if (after < ARGCOUNT)
2328 mch_memmove(&(ARGLIST[after + count]), &(ARGLIST[after]),
2329 (ARGCOUNT - after) * sizeof(aentry_T));
2330 for (i = 0; i < count; ++i)
2331 {
2332 ARGLIST[after + i].ae_fname = files[i];
2333 ARGLIST[after + i].ae_fnum = buflist_add(files[i], BLN_LISTED);
2334 }
2335 ALIST(curwin)->al_ga.ga_len += count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 if (curwin->w_arg_idx >= after)
2337 ++curwin->w_arg_idx;
2338 return after;
2339 }
2340
2341 for (i = 0; i < count; ++i)
2342 vim_free(files[i]);
2343 return -1;
2344}
2345
2346#endif /* FEAT_LISTCMDS */
2347
2348#ifdef FEAT_EVAL
2349/*
2350 * ":compiler[!] {name}"
2351 */
2352 void
2353ex_compiler(eap)
2354 exarg_T *eap;
2355{
2356 char_u *buf;
2357 char_u *old_cur_comp = NULL;
2358 char_u *p;
2359
2360 if (*eap->arg == NUL)
2361 {
2362 /* List all compiler scripts. */
2363 do_cmdline_cmd((char_u *)"echo globpath(&rtp, 'compiler/*.vim')");
2364 /* ) keep the indenter happy... */
2365 }
2366 else
2367 {
2368 buf = alloc((unsigned)(STRLEN(eap->arg) + 14));
2369 if (buf != NULL)
2370 {
2371 if (eap->forceit)
2372 {
2373 /* ":compiler! {name}" sets global options */
2374 do_cmdline_cmd((char_u *)
2375 "command -nargs=* CompilerSet set <args>");
2376 }
2377 else
2378 {
2379 /* ":compiler! {name}" sets local options.
2380 * To remain backwards compatible "current_compiler" is always
2381 * used. A user's compiler plugin may set it, the distributed
2382 * plugin will then skip the settings. Afterwards set
2383 * "b:current_compiler" and restore "current_compiler". */
2384 old_cur_comp = get_var_value((char_u *)"current_compiler");
2385 if (old_cur_comp != NULL)
2386 old_cur_comp = vim_strsave(old_cur_comp);
2387 do_cmdline_cmd((char_u *)
2388 "command -nargs=* CompilerSet setlocal <args>");
2389 }
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00002390 do_unlet((char_u *)"current_compiler", TRUE);
2391 do_unlet((char_u *)"b:current_compiler", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392
2393 sprintf((char *)buf, "compiler/%s.vim", eap->arg);
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00002394 if (source_runtime(buf, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 EMSG2(_("E666: compiler not supported: %s"), eap->arg);
2396 vim_free(buf);
2397
2398 do_cmdline_cmd((char_u *)":delcommand CompilerSet");
2399
2400 /* Set "b:current_compiler" from "current_compiler". */
2401 p = get_var_value((char_u *)"current_compiler");
2402 if (p != NULL)
2403 set_internal_string_var((char_u *)"b:current_compiler", p);
2404
2405 /* Restore "current_compiler" for ":compiler {name}". */
2406 if (!eap->forceit)
2407 {
2408 if (old_cur_comp != NULL)
2409 {
2410 set_internal_string_var((char_u *)"current_compiler",
2411 old_cur_comp);
2412 vim_free(old_cur_comp);
2413 }
2414 else
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00002415 do_unlet((char_u *)"current_compiler", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 }
2417 }
2418 }
2419}
2420#endif
2421
2422/*
2423 * ":runtime {name}"
2424 */
2425 void
2426ex_runtime(eap)
2427 exarg_T *eap;
2428{
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00002429 source_runtime(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430}
2431
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00002432static void source_callback __ARGS((char_u *fname, void *cookie));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00002434/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 static void
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00002436source_callback(fname, cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 char_u *fname;
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00002438 void *cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439{
2440 (void)do_source(fname, FALSE, FALSE);
2441}
2442
2443/*
2444 * Source the file "name" from all directories in 'runtimepath'.
2445 * "name" can contain wildcards.
2446 * When "all" is TRUE, source all files, otherwise only the first one.
2447 * return FAIL when no file could be sourced, OK otherwise.
2448 */
2449 int
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00002450source_runtime(name, all)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 char_u *name;
2452 int all;
2453{
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00002454 return do_in_runtimepath(name, all, source_callback, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455}
2456
2457/*
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00002458 * Find "name" in 'runtimepath'. When found, invoke the callback function for
2459 * it: callback(fname, "cookie")
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 * When "all" is TRUE repeat for all matches, otherwise only the first one is
2461 * used.
2462 * Returns OK when at least one match found, FAIL otherwise.
2463 */
2464 int
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00002465do_in_runtimepath(name, all, callback, cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 char_u *name;
2467 int all;
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00002468 void (*callback)__ARGS((char_u *fname, void *ck));
2469 void *cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470{
2471 char_u *rtp;
2472 char_u *np;
2473 char_u *buf;
2474 char_u *rtp_copy;
2475 char_u *tail;
2476 int num_files;
2477 char_u **files;
2478 int i;
2479 int did_one = FALSE;
2480#ifdef AMIGA
2481 struct Process *proc = (struct Process *)FindTask(0L);
2482 APTR save_winptr = proc->pr_WindowPtr;
2483
2484 /* Avoid a requester here for a volume that doesn't exist. */
2485 proc->pr_WindowPtr = (APTR)-1L;
2486#endif
2487
2488 /* Make a copy of 'runtimepath'. Invoking the callback may change the
2489 * value. */
2490 rtp_copy = vim_strsave(p_rtp);
2491 buf = alloc(MAXPATHL);
2492 if (buf != NULL && rtp_copy != NULL)
2493 {
2494 if (p_verbose > 1)
Bram Moolenaar54ee7752005-05-31 22:22:17 +00002495 {
2496 verbose_enter();
Bram Moolenaar555b2802005-05-19 21:08:39 +00002497 smsg((char_u *)_("Searching for \"%s\" in \"%s\""),
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 (char *)name, (char *)p_rtp);
Bram Moolenaar54ee7752005-05-31 22:22:17 +00002499 verbose_leave();
2500 }
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002501
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 /* Loop over all entries in 'runtimepath'. */
2503 rtp = rtp_copy;
2504 while (*rtp != NUL && (all || !did_one))
2505 {
2506 /* Copy the path from 'runtimepath' to buf[]. */
2507 copy_option_part(&rtp, buf, MAXPATHL, ",");
2508 if (STRLEN(buf) + STRLEN(name) + 2 < MAXPATHL)
2509 {
2510 add_pathsep(buf);
2511 tail = buf + STRLEN(buf);
2512
2513 /* Loop over all patterns in "name" */
2514 np = name;
2515 while (*np != NUL && (all || !did_one))
2516 {
2517 /* Append the pattern from "name" to buf[]. */
2518 copy_option_part(&np, tail, (int)(MAXPATHL - (tail - buf)),
2519 "\t ");
2520
2521 if (p_verbose > 2)
Bram Moolenaar54ee7752005-05-31 22:22:17 +00002522 {
2523 verbose_enter();
Bram Moolenaar555b2802005-05-19 21:08:39 +00002524 smsg((char_u *)_("Searching for \"%s\""), buf);
Bram Moolenaar54ee7752005-05-31 22:22:17 +00002525 verbose_leave();
2526 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527
2528 /* Expand wildcards, invoke the callback for each match. */
2529 if (gen_expand_wildcards(1, &buf, &num_files, &files,
2530 EW_FILE) == OK)
2531 {
2532 for (i = 0; i < num_files; ++i)
2533 {
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00002534 (*callback)(files[i], cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 did_one = TRUE;
2536 if (!all)
2537 break;
2538 }
2539 FreeWild(num_files, files);
2540 }
2541 }
2542 }
2543 }
2544 }
2545 vim_free(buf);
2546 vim_free(rtp_copy);
2547 if (p_verbose > 0 && !did_one)
Bram Moolenaar54ee7752005-05-31 22:22:17 +00002548 {
2549 verbose_enter();
Bram Moolenaar555b2802005-05-19 21:08:39 +00002550 smsg((char_u *)_("not found in 'runtimepath': \"%s\""), name);
Bram Moolenaar54ee7752005-05-31 22:22:17 +00002551 verbose_leave();
2552 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553
2554#ifdef AMIGA
2555 proc->pr_WindowPtr = save_winptr;
2556#endif
2557
2558 return did_one ? OK : FAIL;
2559}
2560
2561#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD)
2562/*
2563 * ":options"
2564 */
2565/*ARGSUSED*/
2566 void
2567ex_options(eap)
2568 exarg_T *eap;
2569{
2570 cmd_source((char_u *)SYS_OPTWIN_FILE, NULL);
2571}
2572#endif
2573
2574/*
2575 * ":source {fname}"
2576 */
2577 void
2578ex_source(eap)
2579 exarg_T *eap;
2580{
2581#ifdef FEAT_BROWSE
2582 if (cmdmod.browse)
2583 {
2584 char_u *fname = NULL;
2585
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002586 fname = do_browse(0, (char_u *)_("Source Vim script"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 NULL, NULL, BROWSE_FILTER_MACROS, NULL);
2588 if (fname != NULL)
2589 {
2590 cmd_source(fname, eap);
2591 vim_free(fname);
2592 }
2593 }
2594 else
2595#endif
2596 cmd_source(eap->arg, eap);
2597}
2598
2599 static void
2600cmd_source(fname, eap)
2601 char_u *fname;
2602 exarg_T *eap;
2603{
2604 if (*fname == NUL)
2605 EMSG(_(e_argreq));
2606
2607 /* ":source!" read vi commands */
2608 else if (eap != NULL && eap->forceit)
2609 /* Need to execute the commands directly when:
2610 * - ":g" command busy
2611 * - after ":argdo", ":windo" or ":bufdo"
2612 * - another command follows
2613 * - inside a loop
2614 */
2615 openscript(fname, global_busy || listcmd_busy || eap->nextcmd != NULL
2616#ifdef FEAT_EVAL
2617 || eap->cstack->cs_idx >= 0
2618#endif
2619 );
2620
2621 /* ":source" read ex commands */
2622 else if (do_source(fname, FALSE, FALSE) == FAIL)
2623 EMSG2(_(e_notopen), fname);
2624}
2625
2626/*
2627 * ":source" and associated commands.
2628 */
2629/*
2630 * Structure used to store info for each sourced file.
2631 * It is shared between do_source() and getsourceline().
2632 * This is required, because it needs to be handed to do_cmdline() and
2633 * sourcing can be done recursively.
2634 */
2635struct source_cookie
2636{
2637 FILE *fp; /* opened file for sourcing */
2638 char_u *nextline; /* if not NULL: line that was read ahead */
2639 int finished; /* ":finish" used */
2640#if defined (USE_CRNL) || defined (USE_CR)
2641 int fileformat; /* EOL_UNKNOWN, EOL_UNIX or EOL_DOS */
2642 int error; /* TRUE if LF found after CR-LF */
2643#endif
2644#ifdef FEAT_EVAL
2645 linenr_T breakpoint; /* next line with breakpoint or zero */
2646 char_u *fname; /* name of sourced file */
2647 int dbg_tick; /* debug_tick when breakpoint was set */
2648 int level; /* top nesting level of sourced file */
2649#endif
2650#ifdef FEAT_MBYTE
2651 vimconv_T conv; /* type of conversion */
2652#endif
2653};
2654
2655#ifdef FEAT_EVAL
2656/*
2657 * Return the address holding the next breakpoint line for a source cookie.
2658 */
2659 linenr_T *
2660source_breakpoint(cookie)
2661 void *cookie;
2662{
2663 return &((struct source_cookie *)cookie)->breakpoint;
2664}
2665
2666/*
2667 * Return the address holding the debug tick for a source cookie.
2668 */
2669 int *
2670source_dbg_tick(cookie)
2671 void *cookie;
2672{
2673 return &((struct source_cookie *)cookie)->dbg_tick;
2674}
2675
2676/*
2677 * Return the nesting level for a source cookie.
2678 */
2679 int
2680source_level(cookie)
2681 void *cookie;
2682{
2683 return ((struct source_cookie *)cookie)->level;
2684}
2685#endif
2686
2687static char_u *get_one_sourceline __ARGS((struct source_cookie *sp));
2688
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689#if defined(WIN32) && defined(FEAT_CSCOPE)
2690static FILE *fopen_noinh_readbin __ARGS((char *filename));
2691
2692/*
2693 * Special function to open a file without handle inheritance.
2694 */
2695 static FILE *
2696fopen_noinh_readbin(filename)
2697 char *filename;
2698{
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00002699 int fd_tmp = mch_open(filename, O_RDONLY | O_BINARY | O_NOINHERIT, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700
2701 if (fd_tmp == -1)
2702 return NULL;
2703 return fdopen(fd_tmp, READBIN);
2704}
2705#endif
2706
2707
2708/*
2709 * do_source: Read the file "fname" and execute its lines as EX commands.
2710 *
2711 * This function may be called recursively!
2712 *
2713 * return FAIL if file could not be opened, OK otherwise
2714 */
2715 int
2716do_source(fname, check_other, is_vimrc)
2717 char_u *fname;
2718 int check_other; /* check for .vimrc and _vimrc */
2719 int is_vimrc; /* call vimrc_found() when file exists */
2720{
2721 struct source_cookie cookie;
2722 char_u *save_sourcing_name;
2723 linenr_T save_sourcing_lnum;
2724 char_u *p;
2725 char_u *fname_exp;
2726 int retval = FAIL;
2727#ifdef FEAT_EVAL
2728 scid_T save_current_SID;
2729 static scid_T last_current_SID = 0;
2730 void *save_funccalp;
2731 int save_debug_break_level = debug_break_level;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002732 scriptitem_T *si = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733# ifdef UNIX
2734 struct stat st;
2735 int stat_ok;
2736# endif
2737#endif
2738#ifdef STARTUPTIME
2739 struct timeval tv_rel;
2740 struct timeval tv_start;
2741#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +00002742#ifdef FEAT_PROFILE
2743 proftime_T wait_start;
2744#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745
2746#ifdef RISCOS
2747 p = mch_munge_fname(fname);
2748#else
2749 p = expand_env_save(fname);
2750#endif
2751 if (p == NULL)
2752 return retval;
2753 fname_exp = fix_fname(p);
2754 vim_free(p);
2755 if (fname_exp == NULL)
2756 return retval;
2757#ifdef MACOS_CLASSIC
2758 slash_n_colon_adjust(fname_exp);
2759#endif
2760 if (mch_isdir(fname_exp))
2761 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00002762 smsg((char_u *)_("Cannot source a directory: \"%s\""), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 goto theend;
2764 }
2765
2766#if defined(WIN32) && defined(FEAT_CSCOPE)
2767 cookie.fp = fopen_noinh_readbin((char *)fname_exp);
2768#else
2769 cookie.fp = mch_fopen((char *)fname_exp, READBIN);
2770#endif
2771 if (cookie.fp == NULL && check_other)
2772 {
2773 /*
2774 * Try again, replacing file name ".vimrc" by "_vimrc" or vice versa,
2775 * and ".exrc" by "_exrc" or vice versa.
2776 */
2777 p = gettail(fname_exp);
2778 if ((*p == '.' || *p == '_')
2779 && (STRICMP(p + 1, "vimrc") == 0
2780 || STRICMP(p + 1, "gvimrc") == 0
2781 || STRICMP(p + 1, "exrc") == 0))
2782 {
2783 if (*p == '_')
2784 *p = '.';
2785 else
2786 *p = '_';
2787#if defined(WIN32) && defined(FEAT_CSCOPE)
2788 cookie.fp = fopen_noinh_readbin((char *)fname_exp);
2789#else
2790 cookie.fp = mch_fopen((char *)fname_exp, READBIN);
2791#endif
2792 }
2793 }
2794
2795 if (cookie.fp == NULL)
2796 {
2797 if (p_verbose > 0)
2798 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +00002799 verbose_enter();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 if (sourcing_name == NULL)
Bram Moolenaar555b2802005-05-19 21:08:39 +00002801 smsg((char_u *)_("could not source \"%s\""), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 else
2803 smsg((char_u *)_("line %ld: could not source \"%s\""),
Bram Moolenaar555b2802005-05-19 21:08:39 +00002804 sourcing_lnum, fname);
Bram Moolenaar54ee7752005-05-31 22:22:17 +00002805 verbose_leave();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 }
2807 goto theend;
2808 }
2809
2810 /*
2811 * The file exists.
2812 * - In verbose mode, give a message.
2813 * - For a vimrc file, may want to set 'compatible', call vimrc_found().
2814 */
2815 if (p_verbose > 1)
2816 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +00002817 verbose_enter();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 if (sourcing_name == NULL)
Bram Moolenaar555b2802005-05-19 21:08:39 +00002819 smsg((char_u *)_("sourcing \"%s\""), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820 else
2821 smsg((char_u *)_("line %ld: sourcing \"%s\""),
Bram Moolenaar555b2802005-05-19 21:08:39 +00002822 sourcing_lnum, fname);
Bram Moolenaar54ee7752005-05-31 22:22:17 +00002823 verbose_leave();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 }
2825 if (is_vimrc)
2826 vimrc_found();
2827
2828#ifdef USE_CRNL
2829 /* If no automatic file format: Set default to CR-NL. */
2830 if (*p_ffs == NUL)
2831 cookie.fileformat = EOL_DOS;
2832 else
2833 cookie.fileformat = EOL_UNKNOWN;
2834 cookie.error = FALSE;
2835#endif
2836
2837#ifdef USE_CR
2838 /* If no automatic file format: Set default to CR. */
2839 if (*p_ffs == NUL)
2840 cookie.fileformat = EOL_MAC;
2841 else
2842 cookie.fileformat = EOL_UNKNOWN;
2843 cookie.error = FALSE;
2844#endif
2845
2846 cookie.nextline = NULL;
2847 cookie.finished = FALSE;
2848
2849#ifdef FEAT_EVAL
2850 /*
2851 * Check if this script has a breakpoint.
2852 */
2853 cookie.breakpoint = dbg_find_breakpoint(TRUE, fname_exp, (linenr_T)0);
2854 cookie.fname = fname_exp;
2855 cookie.dbg_tick = debug_tick;
2856
2857 cookie.level = ex_nesting_level;
2858#endif
2859#ifdef FEAT_MBYTE
2860 cookie.conv.vc_type = CONV_NONE; /* no conversion */
2861
2862 /* Try reading the first few bytes to check for a UTF-8 BOM. */
2863 {
2864 char_u buf[3];
2865
2866 if (fread((char *)buf, sizeof(char_u), (size_t)3, cookie.fp)
2867 == (size_t)3
2868 && buf[0] == 0xef && buf[1] == 0xbb && buf[2] == 0xbf)
2869 /* Found BOM, setup conversion and skip over it. */
2870 convert_setup(&cookie.conv, (char_u *)"utf-8", p_enc);
2871 else
2872 /* No BOM found, rewind. */
2873 fseek(cookie.fp, 0L, SEEK_SET);
2874 }
2875#endif
2876
2877 /*
2878 * Keep the sourcing name/lnum, for recursive calls.
2879 */
2880 save_sourcing_name = sourcing_name;
2881 sourcing_name = fname_exp;
2882 save_sourcing_lnum = sourcing_lnum;
2883 sourcing_lnum = 0;
2884
2885#ifdef STARTUPTIME
2886 time_push(&tv_rel, &tv_start);
2887#endif
2888
2889#ifdef FEAT_EVAL
Bram Moolenaar05159a02005-02-26 23:04:13 +00002890# ifdef FEAT_PROFILE
2891 if (do_profiling)
2892 prof_child_enter(&wait_start); /* entering a child now */
2893# endif
2894
2895 /* Don't use local function variables, if called from a function.
2896 * Also starts profiling timer for nested script. */
2897 save_funccalp = save_funccal();
2898
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 /*
2900 * Check if this script was sourced before to finds its SID.
2901 * If it's new, generate a new SID.
2902 */
2903 save_current_SID = current_SID;
2904# ifdef UNIX
2905 stat_ok = (mch_stat((char *)fname_exp, &st) >= 0);
2906# endif
Bram Moolenaar05159a02005-02-26 23:04:13 +00002907 for (current_SID = script_items.ga_len; current_SID > 0; --current_SID)
2908 {
2909 si = &SCRIPT_ITEM(current_SID);
2910 if (si->sn_name != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 && (
2912# ifdef UNIX
Bram Moolenaar7c626922005-02-07 22:01:03 +00002913 /* Compare dev/ino when possible, it catches symbolic
2914 * links. Also compare file names, the inode may change
2915 * when the file was edited. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00002916 ((stat_ok && si->sn_dev != -1)
2917 && (si->sn_dev == st.st_dev
2918 && si->sn_ino == st.st_ino)) ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919# endif
Bram Moolenaar05159a02005-02-26 23:04:13 +00002920 fnamecmp(si->sn_name, fname_exp) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002922 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923 if (current_SID == 0)
2924 {
2925 current_SID = ++last_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002926 if (ga_grow(&script_items, (int)(current_SID - script_items.ga_len))
2927 == FAIL)
2928 goto almosttheend;
2929 while (script_items.ga_len < current_SID)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00002931 ++script_items.ga_len;
2932 SCRIPT_ITEM(script_items.ga_len).sn_name = NULL;
2933# ifdef FEAT_PROFILE
2934 SCRIPT_ITEM(script_items.ga_len).sn_prof_on = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00002937 si = &SCRIPT_ITEM(current_SID);
2938 si->sn_name = fname_exp;
2939 fname_exp = NULL;
2940# ifdef UNIX
2941 if (stat_ok)
2942 {
2943 si->sn_dev = st.st_dev;
2944 si->sn_ino = st.st_ino;
2945 }
2946 else
2947 si->sn_dev = -1;
2948# endif
2949
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 /* Allocate the local script variables to use for this script. */
2951 new_script_vars(current_SID);
2952 }
2953
Bram Moolenaar05159a02005-02-26 23:04:13 +00002954# ifdef FEAT_PROFILE
2955 if (do_profiling)
2956 {
2957 int forceit;
2958
2959 /* Check if we do profiling for this script. */
2960 if (!si->sn_prof_on && has_profiling(TRUE, si->sn_name, &forceit))
2961 {
2962 script_do_profile(si);
2963 si->sn_pr_force = forceit;
2964 }
2965 if (si->sn_prof_on)
2966 {
2967 ++si->sn_pr_count;
2968 profile_start(&si->sn_pr_start);
2969 profile_zero(&si->sn_pr_children);
2970 }
2971 }
2972# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973#endif
2974
2975 /*
2976 * Call do_cmdline, which will call getsourceline() to get the lines.
2977 */
2978 do_cmdline(NULL, getsourceline, (void *)&cookie,
2979 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_REPEAT);
2980
2981 retval = OK;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002982
2983#ifdef FEAT_PROFILE
2984 if (do_profiling)
2985 {
2986 /* Get "si" again, "script_items" may have been reallocated. */
2987 si = &SCRIPT_ITEM(current_SID);
2988 if (si->sn_prof_on)
2989 {
2990 profile_end(&si->sn_pr_start);
2991 profile_sub_wait(&wait_start, &si->sn_pr_start);
2992 profile_add(&si->sn_pr_total, &si->sn_pr_start);
2993 profile_add(&si->sn_pr_self, &si->sn_pr_start);
2994 profile_sub(&si->sn_pr_self, &si->sn_pr_children);
2995 }
2996 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997#endif
2998
2999 if (got_int)
3000 EMSG(_(e_interr));
3001 sourcing_name = save_sourcing_name;
3002 sourcing_lnum = save_sourcing_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003 if (p_verbose > 1)
3004 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +00003005 verbose_enter();
Bram Moolenaar555b2802005-05-19 21:08:39 +00003006 smsg((char_u *)_("finished sourcing %s"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007 if (sourcing_name != NULL)
Bram Moolenaar555b2802005-05-19 21:08:39 +00003008 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar54ee7752005-05-31 22:22:17 +00003009 verbose_leave();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010 }
3011#ifdef STARTUPTIME
Bram Moolenaar555b2802005-05-19 21:08:39 +00003012 vim_snprintf(IObuff, IOSIZE, "sourcing %s", fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 time_msg(IObuff, &tv_start);
3014 time_pop(&tv_rel);
3015#endif
3016
3017#ifdef FEAT_EVAL
3018 /*
3019 * After a "finish" in debug mode, need to break at first command of next
3020 * sourced file.
3021 */
3022 if (save_debug_break_level > ex_nesting_level
3023 && debug_break_level == ex_nesting_level)
3024 ++debug_break_level;
3025#endif
3026
Bram Moolenaar05159a02005-02-26 23:04:13 +00003027#ifdef FEAT_EVAL
3028almosttheend:
3029 current_SID = save_current_SID;
3030 restore_funccal(save_funccalp);
3031# ifdef FEAT_PROFILE
3032 if (do_profiling)
3033 prof_child_exit(&wait_start); /* leaving a child now */
3034# endif
3035#endif
3036 fclose(cookie.fp);
3037 vim_free(cookie.nextline);
3038#ifdef FEAT_MBYTE
3039 convert_setup(&cookie.conv, NULL, NULL);
3040#endif
3041
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042theend:
3043 vim_free(fname_exp);
3044 return retval;
3045}
3046
3047#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00003048
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049/*
3050 * ":scriptnames"
3051 */
3052/*ARGSUSED*/
3053 void
3054ex_scriptnames(eap)
3055 exarg_T *eap;
3056{
3057 int i;
3058
Bram Moolenaar05159a02005-02-26 23:04:13 +00003059 for (i = 1; i <= script_items.ga_len && !got_int; ++i)
3060 if (SCRIPT_ITEM(i).sn_name != NULL)
3061 smsg((char_u *)"%3d: %s", i, SCRIPT_ITEM(i).sn_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062}
3063
3064# if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
3065/*
3066 * Fix slashes in the list of script names for 'shellslash'.
3067 */
3068 void
3069scriptnames_slash_adjust()
3070{
3071 int i;
3072
Bram Moolenaar05159a02005-02-26 23:04:13 +00003073 for (i = 1; i <= script_items.ga_len; ++i)
3074 if (SCRIPT_ITEM(i).sn_name != NULL)
3075 slash_adjust(SCRIPT_ITEM(i).sn_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076}
3077# endif
3078
3079/*
3080 * Get a pointer to a script name. Used for ":verbose set".
3081 */
3082 char_u *
3083get_scriptname(id)
3084 scid_T id;
3085{
3086 if (id == SID_MODELINE)
3087 return (char_u *)"modeline";
3088 if (id == SID_CMDARG)
3089 return (char_u *)"--cmd argument";
3090 if (id == SID_CARG)
3091 return (char_u *)"-c argument";
3092 if (id == SID_ENV)
3093 return (char_u *)"environment variable";
Bram Moolenaar05159a02005-02-26 23:04:13 +00003094 return SCRIPT_ITEM(id).sn_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095}
Bram Moolenaar05159a02005-02-26 23:04:13 +00003096
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00003097# if defined(EXITFREE) || defined(PROTO)
3098 void
3099free_scriptnames()
3100{
3101 int i;
3102
3103 for (i = script_items.ga_len; i > 0; --i)
3104 vim_free(SCRIPT_ITEM(i).sn_name);
3105 ga_clear(&script_items);
3106}
3107# endif
3108
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109#endif
3110
3111#if defined(USE_CR) || defined(PROTO)
3112
3113# if defined(__MSL__) && (__MSL__ >= 22)
3114/*
3115 * Newer version of the Metrowerks library handle DOS and UNIX files
3116 * without help.
3117 * Test with earlier versions, MSL 2.2 is the library supplied with
3118 * Codewarrior Pro 2.
3119 */
3120 char *
3121fgets_cr(s, n, stream)
3122 char *s;
3123 int n;
3124 FILE *stream;
3125{
3126 return fgets(s, n, stream);
3127}
3128# else
3129/*
3130 * Version of fgets() which also works for lines ending in a <CR> only
3131 * (Macintosh format).
3132 * For older versions of the Metrowerks library.
3133 * At least CodeWarrior 9 needed this code.
3134 */
3135 char *
3136fgets_cr(s, n, stream)
3137 char *s;
3138 int n;
3139 FILE *stream;
3140{
3141 int c = 0;
3142 int char_read = 0;
3143
3144 while (!feof(stream) && c != '\r' && c != '\n' && char_read < n - 1)
3145 {
3146 c = fgetc(stream);
3147 s[char_read++] = c;
3148 /* If the file is in DOS format, we need to skip a NL after a CR. I
3149 * thought it was the other way around, but this appears to work... */
3150 if (c == '\n')
3151 {
3152 c = fgetc(stream);
3153 if (c != '\r')
3154 ungetc(c, stream);
3155 }
3156 }
3157
3158 s[char_read] = 0;
3159 if (char_read == 0)
3160 return NULL;
3161
3162 if (feof(stream) && char_read == 1)
3163 return NULL;
3164
3165 return s;
3166}
3167# endif
3168#endif
3169
3170/*
3171 * Get one full line from a sourced file.
3172 * Called by do_cmdline() when it's called from do_source().
3173 *
3174 * Return a pointer to the line in allocated memory.
3175 * Return NULL for end-of-file or some error.
3176 */
3177/* ARGSUSED */
3178 char_u *
3179getsourceline(c, cookie, indent)
3180 int c; /* not used */
3181 void *cookie;
3182 int indent; /* not used */
3183{
3184 struct source_cookie *sp = (struct source_cookie *)cookie;
3185 char_u *line;
3186 char_u *p, *s;
3187
3188#ifdef FEAT_EVAL
3189 /* If breakpoints have been added/deleted need to check for it. */
3190 if (sp->dbg_tick < debug_tick)
3191 {
3192 sp->breakpoint = dbg_find_breakpoint(TRUE, sp->fname, sourcing_lnum);
3193 sp->dbg_tick = debug_tick;
3194 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003195# ifdef FEAT_PROFILE
3196 if (do_profiling)
3197 script_line_end();
3198# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199#endif
3200 /*
3201 * Get current line. If there is a read-ahead line, use it, otherwise get
3202 * one now.
3203 */
3204 if (sp->finished)
3205 line = NULL;
3206 else if (sp->nextline == NULL)
3207 line = get_one_sourceline(sp);
3208 else
3209 {
3210 line = sp->nextline;
3211 sp->nextline = NULL;
3212 ++sourcing_lnum;
3213 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003214#ifdef FEAT_PROFILE
3215 if (line != NULL && do_profiling)
3216 script_line_start();
3217#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218
3219 /* Only concatenate lines starting with a \ when 'cpoptions' doesn't
3220 * contain the 'C' flag. */
3221 if (line != NULL && (vim_strchr(p_cpo, CPO_CONCAT) == NULL))
3222 {
3223 /* compensate for the one line read-ahead */
3224 --sourcing_lnum;
3225 for (;;)
3226 {
3227 sp->nextline = get_one_sourceline(sp);
3228 if (sp->nextline == NULL)
3229 break;
3230 p = skipwhite(sp->nextline);
3231 if (*p != '\\')
3232 break;
3233 s = alloc((int)(STRLEN(line) + STRLEN(p)));
3234 if (s == NULL) /* out of memory */
3235 break;
3236 STRCPY(s, line);
3237 STRCAT(s, p + 1);
3238 vim_free(line);
3239 line = s;
3240 vim_free(sp->nextline);
3241 }
3242 }
3243
3244#ifdef FEAT_MBYTE
3245 if (line != NULL && sp->conv.vc_type != CONV_NONE)
3246 {
3247 /* Convert the encoding of the script line. */
3248 s = string_convert(&sp->conv, line, NULL);
3249 if (s != NULL)
3250 {
3251 vim_free(line);
3252 line = s;
3253 }
3254 }
3255#endif
3256
3257#ifdef FEAT_EVAL
3258 /* Did we encounter a breakpoint? */
3259 if (sp->breakpoint != 0 && sp->breakpoint <= sourcing_lnum)
3260 {
3261 dbg_breakpoint(sp->fname, sourcing_lnum);
3262 /* Find next breakpoint. */
3263 sp->breakpoint = dbg_find_breakpoint(TRUE, sp->fname, sourcing_lnum);
3264 sp->dbg_tick = debug_tick;
3265 }
3266#endif
3267
3268 return line;
3269}
3270
3271 static char_u *
3272get_one_sourceline(sp)
3273 struct source_cookie *sp;
3274{
3275 garray_T ga;
3276 int len;
3277 int c;
3278 char_u *buf;
3279#ifdef USE_CRNL
3280 int has_cr; /* CR-LF found */
3281#endif
3282#ifdef USE_CR
3283 char_u *scan;
3284#endif
3285 int have_read = FALSE;
3286
3287 /* use a growarray to store the sourced line */
Bram Moolenaar6ac54292005-02-02 23:07:25 +00003288 ga_init2(&ga, 1, 250);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289
3290 /*
3291 * Loop until there is a finished line (or end-of-file).
3292 */
3293 sourcing_lnum++;
3294 for (;;)
3295 {
Bram Moolenaar6ac54292005-02-02 23:07:25 +00003296 /* make room to read at least 120 (more) characters */
3297 if (ga_grow(&ga, 120) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 break;
3299 buf = (char_u *)ga.ga_data;
3300
3301#ifdef USE_CR
3302 if (sp->fileformat == EOL_MAC)
3303 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00003304 if (fgets_cr((char *)buf + ga.ga_len, ga.ga_maxlen - ga.ga_len,
3305 sp->fp) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 break;
3307 }
3308 else
3309#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00003310 if (fgets((char *)buf + ga.ga_len, ga.ga_maxlen - ga.ga_len,
3311 sp->fp) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 break;
Bram Moolenaar6ac54292005-02-02 23:07:25 +00003313 len = ga.ga_len + (int)STRLEN(buf + ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314#ifdef USE_CRNL
3315 /* Ignore a trailing CTRL-Z, when in Dos mode. Only recognize the
3316 * CTRL-Z by its own, or after a NL. */
3317 if ( (len == 1 || (len >= 2 && buf[len - 2] == '\n'))
3318 && sp->fileformat == EOL_DOS
3319 && buf[len - 1] == Ctrl_Z)
3320 {
3321 buf[len - 1] = NUL;
3322 break;
3323 }
3324#endif
3325
3326#ifdef USE_CR
3327 /* If the read doesn't stop on a new line, and there's
3328 * some CR then we assume a Mac format */
3329 if (sp->fileformat == EOL_UNKNOWN)
3330 {
3331 if (buf[len - 1] != '\n' && vim_strchr(buf, '\r') != NULL)
3332 sp->fileformat = EOL_MAC;
3333 else
3334 sp->fileformat = EOL_UNIX;
3335 }
3336
3337 if (sp->fileformat == EOL_MAC)
3338 {
3339 scan = vim_strchr(buf, '\r');
3340
3341 if (scan != NULL)
3342 {
3343 *scan = '\n';
3344 if (*(scan + 1) != 0)
3345 {
3346 *(scan + 1) = 0;
3347 fseek(sp->fp, (long)(scan - buf - len + 1), SEEK_CUR);
3348 }
3349 }
3350 len = STRLEN(buf);
3351 }
3352#endif
3353
3354 have_read = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 ga.ga_len = len;
3356
3357 /* If the line was longer than the buffer, read more. */
Bram Moolenaar86b68352004-12-27 21:59:20 +00003358 if (ga.ga_maxlen - ga.ga_len == 1 && buf[len - 1] != '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 continue;
3360
3361 if (len >= 1 && buf[len - 1] == '\n') /* remove trailing NL */
3362 {
3363#ifdef USE_CRNL
3364 has_cr = (len >= 2 && buf[len - 2] == '\r');
3365 if (sp->fileformat == EOL_UNKNOWN)
3366 {
3367 if (has_cr)
3368 sp->fileformat = EOL_DOS;
3369 else
3370 sp->fileformat = EOL_UNIX;
3371 }
3372
3373 if (sp->fileformat == EOL_DOS)
3374 {
3375 if (has_cr) /* replace trailing CR */
3376 {
3377 buf[len - 2] = '\n';
3378 --len;
3379 --ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 }
3381 else /* lines like ":map xx yy^M" will have failed */
3382 {
3383 if (!sp->error)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003384 {
3385 msg_source(hl_attr(HLF_W));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 EMSG(_("W15: Warning: Wrong line separator, ^M may be missing"));
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 sp->error = TRUE;
3389 sp->fileformat = EOL_UNIX;
3390 }
3391 }
3392#endif
3393 /* The '\n' is escaped if there is an odd number of ^V's just
3394 * before it, first set "c" just before the 'V's and then check
3395 * len&c parities (is faster than ((len-c)%2 == 0)) -- Acevedo */
3396 for (c = len - 2; c >= 0 && buf[c] == Ctrl_V; c--)
3397 ;
3398 if ((len & 1) != (c & 1)) /* escaped NL, read more */
3399 {
3400 sourcing_lnum++;
3401 continue;
3402 }
3403
3404 buf[len - 1] = NUL; /* remove the NL */
3405 }
3406
3407 /*
3408 * Check for ^C here now and then, so recursive :so can be broken.
3409 */
3410 line_breakcheck();
3411 break;
3412 }
3413
3414 if (have_read)
3415 return (char_u *)ga.ga_data;
3416
3417 vim_free(ga.ga_data);
3418 return NULL;
3419}
3420
Bram Moolenaar05159a02005-02-26 23:04:13 +00003421#if defined(FEAT_PROFILE) || defined(PROTO)
3422/*
3423 * Called when starting to read a script line.
3424 * "sourcing_lnum" must be correct!
3425 * When skipping lines it may not actually be executed, but we won't find out
3426 * until later and we need to store the time now.
3427 */
3428 void
3429script_line_start()
3430{
3431 scriptitem_T *si;
3432 sn_prl_T *pp;
3433
3434 if (current_SID <= 0 || current_SID > script_items.ga_len)
3435 return;
3436 si = &SCRIPT_ITEM(current_SID);
3437 if (si->sn_prof_on && sourcing_lnum >= 1)
3438 {
3439 /* Grow the array before starting the timer, so that the time spend
3440 * here isn't counted. */
3441 ga_grow(&si->sn_prl_ga, (int)(sourcing_lnum - si->sn_prl_ga.ga_len));
3442 si->sn_prl_idx = sourcing_lnum - 1;
3443 while (si->sn_prl_ga.ga_len <= si->sn_prl_idx
3444 && si->sn_prl_ga.ga_len < si->sn_prl_ga.ga_maxlen)
3445 {
3446 /* Zero counters for a line that was not used before. */
3447 pp = &PRL_ITEM(si, si->sn_prl_ga.ga_len);
3448 pp->snp_count = 0;
3449 profile_zero(&pp->sn_prl_total);
3450 profile_zero(&pp->sn_prl_self);
3451 ++si->sn_prl_ga.ga_len;
3452 }
3453 si->sn_prl_execed = FALSE;
3454 profile_start(&si->sn_prl_start);
3455 profile_zero(&si->sn_prl_children);
3456 profile_get_wait(&si->sn_prl_wait);
3457 }
3458}
3459
3460/*
3461 * Called when actually executing a function line.
3462 */
3463 void
3464script_line_exec()
3465{
3466 scriptitem_T *si;
3467
3468 if (current_SID <= 0 || current_SID > script_items.ga_len)
3469 return;
3470 si = &SCRIPT_ITEM(current_SID);
3471 if (si->sn_prof_on && si->sn_prl_idx >= 0)
3472 si->sn_prl_execed = TRUE;
3473}
3474
3475/*
3476 * Called when done with a function line.
3477 */
3478 void
3479script_line_end()
3480{
3481 scriptitem_T *si;
3482 sn_prl_T *pp;
3483
3484 if (current_SID <= 0 || current_SID > script_items.ga_len)
3485 return;
3486 si = &SCRIPT_ITEM(current_SID);
3487 if (si->sn_prof_on && si->sn_prl_idx >= 0
3488 && si->sn_prl_idx < si->sn_prl_ga.ga_len)
3489 {
3490 if (si->sn_prl_execed)
3491 {
3492 pp = &PRL_ITEM(si, si->sn_prl_idx);
3493 ++pp->snp_count;
3494 profile_end(&si->sn_prl_start);
3495 profile_sub_wait(&si->sn_prl_wait, &si->sn_prl_start);
3496 profile_add(&pp->sn_prl_self, &si->sn_prl_start);
3497 profile_add(&pp->sn_prl_total, &si->sn_prl_start);
3498 profile_sub(&pp->sn_prl_self, &si->sn_prl_children);
3499 }
3500 si->sn_prl_idx = -1;
3501 }
3502}
3503#endif
3504
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505/*
3506 * ":scriptencoding": Set encoding conversion for a sourced script.
3507 * Without the multi-byte feature it's simply ignored.
3508 */
3509/*ARGSUSED*/
3510 void
3511ex_scriptencoding(eap)
3512 exarg_T *eap;
3513{
3514#ifdef FEAT_MBYTE
3515 struct source_cookie *sp;
3516 char_u *name;
3517
3518 if (!getline_equal(eap->getline, eap->cookie, getsourceline))
3519 {
3520 EMSG(_("E167: :scriptencoding used outside of a sourced file"));
3521 return;
3522 }
3523
3524 if (*eap->arg != NUL)
3525 {
3526 name = enc_canonize(eap->arg);
3527 if (name == NULL) /* out of memory */
3528 return;
3529 }
3530 else
3531 name = eap->arg;
3532
3533 /* Setup for conversion from the specified encoding to 'encoding'. */
3534 sp = (struct source_cookie *)getline_cookie(eap->getline, eap->cookie);
3535 convert_setup(&sp->conv, name, p_enc);
3536
3537 if (name != eap->arg)
3538 vim_free(name);
3539#endif
3540}
3541
3542#if defined(FEAT_EVAL) || defined(PROTO)
3543/*
3544 * ":finish": Mark a sourced file as finished.
3545 */
3546 void
3547ex_finish(eap)
3548 exarg_T *eap;
3549{
3550 if (getline_equal(eap->getline, eap->cookie, getsourceline))
3551 do_finish(eap, FALSE);
3552 else
3553 EMSG(_("E168: :finish used outside of a sourced file"));
3554}
3555
3556/*
3557 * Mark a sourced file as finished. Possibly makes the ":finish" pending.
3558 * Also called for a pending finish at the ":endtry" or after returning from
3559 * an extra do_cmdline(). "reanimate" is used in the latter case.
3560 */
3561 void
3562do_finish(eap, reanimate)
3563 exarg_T *eap;
3564 int reanimate;
3565{
3566 int idx;
3567
3568 if (reanimate)
3569 ((struct source_cookie *)getline_cookie(eap->getline,
3570 eap->cookie))->finished = FALSE;
3571
3572 /*
3573 * Cleanup (and inactivate) conditionals, but stop when a try conditional
3574 * not in its finally clause (which then is to be executed next) is found.
3575 * In this case, make the ":finish" pending for execution at the ":endtry".
3576 * Otherwise, finish normally.
3577 */
3578 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
3579 if (idx >= 0)
3580 {
3581 eap->cstack->cs_pending[idx] = CSTP_FINISH;
3582 report_make_pending(CSTP_FINISH, NULL);
3583 }
3584 else
3585 ((struct source_cookie *)getline_cookie(eap->getline,
3586 eap->cookie))->finished = TRUE;
3587}
3588
3589
3590/*
3591 * Return TRUE when a sourced file had the ":finish" command: Don't give error
3592 * message for missing ":endif".
3593 * Return FALSE when not sourcing a file.
3594 */
3595 int
3596source_finished(getline, cookie)
3597 char_u *(*getline) __ARGS((int, void *, int));
3598 void *cookie;
3599{
3600 return (getline_equal(getline, cookie, getsourceline)
3601 && ((struct source_cookie *)getline_cookie(
3602 getline, cookie))->finished);
3603}
3604#endif
3605
3606#if defined(FEAT_LISTCMDS) || defined(PROTO)
3607/*
3608 * ":checktime [buffer]"
3609 */
3610 void
3611ex_checktime(eap)
3612 exarg_T *eap;
3613{
3614 buf_T *buf;
3615 int save_no_check_timestamps = no_check_timestamps;
3616
3617 no_check_timestamps = 0;
3618 if (eap->addr_count == 0) /* default is all buffers */
3619 check_timestamps(FALSE);
3620 else
3621 {
3622 buf = buflist_findnr((int)eap->line2);
3623 if (buf != NULL) /* cannot happen? */
3624 (void)buf_check_timestamp(buf, FALSE);
3625 }
3626 no_check_timestamps = save_no_check_timestamps;
3627}
3628#endif
3629
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
3631 && (defined(FEAT_EVAL) || defined(FEAT_MULTI_LANG))
3632static char *get_locale_val __ARGS((int what));
3633
3634 static char *
3635get_locale_val(what)
3636 int what;
3637{
3638 char *loc;
3639
3640 /* Obtain the locale value from the libraries. For DJGPP this is
3641 * redefined and it doesn't use the arguments. */
3642 loc = setlocale(what, NULL);
3643
3644# if defined(__BORLANDC__)
3645 if (loc != NULL)
3646 {
3647 char_u *p;
3648
3649 /* Borland returns something like "LC_CTYPE=<name>\n"
3650 * Let's try to fix that bug here... */
3651 p = vim_strchr(loc, '=');
3652 if (p != NULL)
3653 {
3654 loc = ++p;
3655 while (*p != NUL) /* remove trailing newline */
3656 {
3657 if (*p < ' ')
3658 {
3659 *p = NUL;
3660 break;
3661 }
3662 ++p;
3663 }
3664 }
3665 }
3666# endif
3667
3668 return loc;
3669}
3670#endif
3671
3672
3673#ifdef WIN32
3674/*
3675 * On MS-Windows locale names are strings like "German_Germany.1252", but
3676 * gettext expects "de". Try to translate one into another here for a few
3677 * supported languages.
3678 */
3679 static char_u *
3680gettext_lang(char_u *name)
3681{
3682 int i;
3683 static char *(mtable[]) = {
3684 "afrikaans", "af",
3685 "czech", "cs",
3686 "dutch", "nl",
3687 "german", "de",
3688 "english_united kingdom", "en_GB",
3689 "spanish", "es",
3690 "french", "fr",
3691 "italian", "it",
3692 "japanese", "ja",
3693 "korean", "ko",
3694 "norwegian", "no",
3695 "polish", "pl",
3696 "russian", "ru",
3697 "slovak", "sk",
3698 "swedish", "sv",
3699 "ukrainian", "uk",
3700 "chinese_china", "zh_CN",
3701 "chinese_taiwan", "zh_TW",
3702 NULL};
3703
3704 for (i = 0; mtable[i] != NULL; i += 2)
3705 if (STRNICMP(mtable[i], name, STRLEN(mtable[i])) == 0)
3706 return mtable[i + 1];
3707 return name;
3708}
3709#endif
3710
3711#if defined(FEAT_MULTI_LANG) || defined(PROTO)
3712/*
3713 * Obtain the current messages language. Used to set the default for
3714 * 'helplang'. May return NULL or an empty string.
3715 */
3716 char_u *
3717get_mess_lang()
3718{
3719 char_u *p;
3720
3721# if (defined(HAVE_LOCALE_H) || defined(X_LOCALE))
3722# if defined(LC_MESSAGES)
3723 p = (char_u *)get_locale_val(LC_MESSAGES);
3724# else
3725 /* This is necessary for Win32, where LC_MESSAGES is not defined and $LANG
3726 * may be set to the LCID number. */
3727 p = (char_u *)get_locale_val(LC_ALL);
3728# endif
3729# else
3730 p = mch_getenv((char_u *)"LC_ALL");
3731 if (p == NULL || *p == NUL)
3732 {
3733 p = mch_getenv((char_u *)"LC_MESSAGES");
3734 if (p == NULL || *p == NUL)
3735 p = mch_getenv((char_u *)"LANG");
3736 }
3737# endif
3738# ifdef WIN32
3739 p = gettext_lang(p);
3740# endif
3741 return p;
3742}
3743#endif
3744
Bram Moolenaardef9e822004-12-31 20:58:58 +00003745/* Complicated #if; matches with where get_mess_env() is used below. */
3746#if (defined(FEAT_EVAL) && !((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
3747 && defined(LC_MESSAGES))) \
3748 || ((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
3749 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)) \
3750 && !defined(LC_MESSAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751static char_u *get_mess_env __ARGS((void));
3752
3753/*
3754 * Get the language used for messages from the environment.
3755 */
3756 static char_u *
3757get_mess_env()
3758{
3759 char_u *p;
3760
3761 p = mch_getenv((char_u *)"LC_ALL");
3762 if (p == NULL || *p == NUL)
3763 {
3764 p = mch_getenv((char_u *)"LC_MESSAGES");
3765 if (p == NULL || *p == NUL)
3766 {
3767 p = mch_getenv((char_u *)"LANG");
3768 if (p != NULL && VIM_ISDIGIT(*p))
3769 p = NULL; /* ignore something like "1043" */
3770# if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
3771 if (p == NULL || *p == NUL)
3772 p = (char_u *)get_locale_val(LC_CTYPE);
3773# endif
3774 }
3775 }
3776 return p;
3777}
3778#endif
3779
3780#if defined(FEAT_EVAL) || defined(PROTO)
3781
3782/*
3783 * Set the "v:lang" variable according to the current locale setting.
3784 * Also do "v:lc_time"and "v:ctype".
3785 */
3786 void
3787set_lang_var()
3788{
3789 char_u *loc;
3790
3791# if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
3792 loc = (char_u *)get_locale_val(LC_CTYPE);
3793# else
3794 /* setlocale() not supported: use the default value */
3795 loc = (char_u *)"C";
3796# endif
3797 set_vim_var_string(VV_CTYPE, loc, -1);
3798
3799 /* When LC_MESSAGES isn't defined use the value from $LC_MESSAGES, fall
3800 * back to LC_CTYPE if it's empty. */
3801# if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) && defined(LC_MESSAGES)
3802 loc = (char_u *)get_locale_val(LC_MESSAGES);
3803# else
3804 loc = get_mess_env();
3805# endif
3806 set_vim_var_string(VV_LANG, loc, -1);
3807
3808# if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
3809 loc = (char_u *)get_locale_val(LC_TIME);
3810# endif
3811 set_vim_var_string(VV_LC_TIME, loc, -1);
3812}
3813#endif
3814
3815#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
3816 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
3817/*
3818 * ":language": Set the language (locale).
3819 */
3820 void
3821ex_language(eap)
3822 exarg_T *eap;
3823{
3824 char *loc;
3825 char_u *p;
3826 char_u *name;
3827 int what = LC_ALL;
3828 char *whatstr = "";
3829#ifdef LC_MESSAGES
3830# define VIM_LC_MESSAGES LC_MESSAGES
3831#else
3832# define VIM_LC_MESSAGES 6789
3833#endif
3834
3835 name = eap->arg;
3836
3837 /* Check for "messages {name}", "ctype {name}" or "time {name}" argument.
3838 * Allow abbreviation, but require at least 3 characters to avoid
3839 * confusion with a two letter language name "me" or "ct". */
3840 p = skiptowhite(eap->arg);
3841 if ((*p == NUL || vim_iswhite(*p)) && p - eap->arg >= 3)
3842 {
3843 if (STRNICMP(eap->arg, "messages", p - eap->arg) == 0)
3844 {
3845 what = VIM_LC_MESSAGES;
3846 name = skipwhite(p);
3847 whatstr = "messages ";
3848 }
3849 else if (STRNICMP(eap->arg, "ctype", p - eap->arg) == 0)
3850 {
3851 what = LC_CTYPE;
3852 name = skipwhite(p);
3853 whatstr = "ctype ";
3854 }
3855 else if (STRNICMP(eap->arg, "time", p - eap->arg) == 0)
3856 {
3857 what = LC_TIME;
3858 name = skipwhite(p);
3859 whatstr = "time ";
3860 }
3861 }
3862
3863 if (*name == NUL)
3864 {
3865#ifndef LC_MESSAGES
3866 if (what == VIM_LC_MESSAGES)
3867 p = get_mess_env();
3868 else
3869#endif
3870 p = (char_u *)setlocale(what, NULL);
3871 if (p == NULL || *p == NUL)
3872 p = (char_u *)"Unknown";
3873 smsg((char_u *)_("Current %slanguage: \"%s\""), whatstr, p);
3874 }
3875 else
3876 {
3877#ifndef LC_MESSAGES
3878 if (what == VIM_LC_MESSAGES)
3879 loc = "";
3880 else
3881#endif
3882 loc = setlocale(what, (char *)name);
3883 if (loc == NULL)
3884 EMSG2(_("E197: Cannot set language to \"%s\""), name);
3885 else
3886 {
3887#ifdef HAVE_NL_MSG_CAT_CNTR
3888 /* Need to do this for GNU gettext, otherwise cached translations
3889 * will be used again. */
3890 extern int _nl_msg_cat_cntr;
3891
3892 ++_nl_msg_cat_cntr;
3893#endif
3894 /* Reset $LC_ALL, otherwise it would overrule everyting. */
3895 vim_setenv((char_u *)"LC_ALL", (char_u *)"");
3896
3897 if (what != LC_TIME)
3898 {
3899 /* Tell gettext() what to translate to. It apparently doesn't
3900 * use the currently effective locale. Also do this when
3901 * FEAT_GETTEXT isn't defined, so that shell commands use this
3902 * value. */
3903 if (what == LC_ALL)
3904 vim_setenv((char_u *)"LANG", name);
3905 if (what != LC_CTYPE)
3906 {
3907 char_u *mname;
3908#ifdef WIN32
3909 mname = gettext_lang(name);
3910#else
3911 mname = name;
3912#endif
3913 vim_setenv((char_u *)"LC_MESSAGES", mname);
3914#ifdef FEAT_MULTI_LANG
3915 set_helplang_default(mname);
3916#endif
3917 }
3918
3919 /* Set $LC_CTYPE, because it overrules $LANG, and
3920 * gtk_set_locale() calls setlocale() again. gnome_init()
3921 * sets $LC_CTYPE to "en_US" (that's a bug!). */
3922 if (what != VIM_LC_MESSAGES)
3923 vim_setenv((char_u *)"LC_CTYPE", name);
3924# ifdef FEAT_GUI_GTK
3925 /* Let GTK know what locale we're using. Not sure this is
3926 * really needed... */
3927 if (gui.in_use)
3928 (void)gtk_set_locale();
3929# endif
3930 }
3931
3932# ifdef FEAT_EVAL
3933 /* Set v:lang, v:lc_time and v:ctype to the final result. */
3934 set_lang_var();
3935# endif
3936 }
3937 }
3938}
3939
3940# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3941/*
3942 * Function given to ExpandGeneric() to obtain the possible arguments of the
3943 * ":language" command.
3944 */
3945/*ARGSUSED*/
3946 char_u *
3947get_lang_arg(xp, idx)
3948 expand_T *xp;
3949 int idx;
3950{
3951 if (idx == 0)
3952 return (char_u *)"messages";
3953 if (idx == 1)
3954 return (char_u *)"ctype";
3955 if (idx == 2)
3956 return (char_u *)"time";
3957 return NULL;
3958}
3959# endif
3960
3961#endif