blob: 6480377b7b95a507995583a6ab73f074414684cb [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)
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015# include "vimio.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +000016#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 }
524
525 if (bp->dbg_name == NULL)
526 return FAIL;
527 return OK;
528}
529
530/*
531 * ":breakadd".
532 */
533 void
534ex_breakadd(eap)
535 exarg_T *eap;
536{
537 struct debuggy *bp;
538 char_u *pat;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000539 garray_T *gap;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540
Bram Moolenaar05159a02005-02-26 23:04:13 +0000541 gap = &dbg_breakp;
542#ifdef FEAT_PROFILE
543 if (eap->cmdidx == CMD_profile)
544 gap = &prof_ga;
545#endif
546
547 if (dbg_parsearg(eap->arg, gap) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000549 bp = &DEBUGGY(gap, gap->ga_len);
550 bp->dbg_forceit = eap->forceit;
551
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 pat = file_pat_to_reg_pat(bp->dbg_name, NULL, NULL, FALSE);
553 if (pat != NULL)
554 {
555 bp->dbg_prog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
556 vim_free(pat);
557 }
558 if (pat == NULL || bp->dbg_prog == NULL)
559 vim_free(bp->dbg_name);
560 else
561 {
562 if (bp->dbg_lnum == 0) /* default line number is 1 */
563 bp->dbg_lnum = 1;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000564#ifdef FEAT_PROFILE
565 if (eap->cmdidx != CMD_profile)
566#endif
567 {
568 DEBUGGY(gap, gap->ga_len).dbg_nr = ++last_breakp;
569 ++debug_tick;
570 }
571 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572 }
573 }
574}
575
576/*
577 * ":debuggreedy".
578 */
579 void
580ex_debuggreedy(eap)
581 exarg_T *eap;
582{
583 if (eap->addr_count == 0 || eap->line2 != 0)
584 debug_greedy = TRUE;
585 else
586 debug_greedy = FALSE;
587}
588
589/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000590 * ":breakdel" and ":profdel".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591 */
592 void
593ex_breakdel(eap)
594 exarg_T *eap;
595{
596 struct debuggy *bp, *bpi;
597 int nr;
598 int todel = -1;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000599 int del_all = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600 int i;
601 linenr_T best_lnum = 0;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000602 garray_T *gap;
603
604 gap = &dbg_breakp;
605#ifdef FEAT_PROFILE
606 if (eap->cmdidx == CMD_profdel)
607 gap = &prof_ga;
608#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609
610 if (vim_isdigit(*eap->arg))
611 {
612 /* ":breakdel {nr}" */
613 nr = atol((char *)eap->arg);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000614 for (i = 0; i < gap->ga_len; ++i)
615 if (DEBUGGY(gap, i).dbg_nr == nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616 {
617 todel = i;
618 break;
619 }
620 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000621 else if (*eap->arg == '*')
622 {
623 todel = 0;
624 del_all = TRUE;
625 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 else
627 {
628 /* ":breakdel {func|file} [lnum] {name}" */
Bram Moolenaard9fba312005-06-26 22:34:35 +0000629 if (dbg_parsearg(eap->arg, gap) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000631 bp = &DEBUGGY(gap, gap->ga_len);
632 for (i = 0; i < gap->ga_len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 {
Bram Moolenaard9fba312005-06-26 22:34:35 +0000634 bpi = &DEBUGGY(gap, i);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635 if (bp->dbg_type == bpi->dbg_type
636 && STRCMP(bp->dbg_name, bpi->dbg_name) == 0
637 && (bp->dbg_lnum == bpi->dbg_lnum
638 || (bp->dbg_lnum == 0
639 && (best_lnum == 0
640 || bpi->dbg_lnum < best_lnum))))
641 {
642 todel = i;
643 best_lnum = bpi->dbg_lnum;
644 }
645 }
646 vim_free(bp->dbg_name);
647 }
648
649 if (todel < 0)
650 EMSG2(_("E161: Breakpoint not found: %s"), eap->arg);
651 else
Bram Moolenaard9fba312005-06-26 22:34:35 +0000652 {
653 while (gap->ga_len > 0)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000654 {
Bram Moolenaard9fba312005-06-26 22:34:35 +0000655 vim_free(DEBUGGY(gap, todel).dbg_name);
656 vim_free(DEBUGGY(gap, todel).dbg_prog);
657 --gap->ga_len;
658 if (todel < gap->ga_len)
659 mch_memmove(&DEBUGGY(gap, todel), &DEBUGGY(gap, todel + 1),
660 (gap->ga_len - todel) * sizeof(struct debuggy));
661#ifdef FEAT_PROFILE
662 if (eap->cmdidx == CMD_breakdel)
663#endif
664 ++debug_tick;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000665 if (!del_all)
666 break;
667 }
Bram Moolenaard9fba312005-06-26 22:34:35 +0000668
669 /* If all breakpoints were removed clear the array. */
670 if (gap->ga_len == 0)
671 ga_clear(gap);
672 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673}
674
675/*
676 * ":breaklist".
677 */
678/*ARGSUSED*/
679 void
680ex_breaklist(eap)
681 exarg_T *eap;
682{
683 struct debuggy *bp;
684 int i;
685
686 if (dbg_breakp.ga_len == 0)
687 MSG(_("No breakpoints defined"));
688 else
689 for (i = 0; i < dbg_breakp.ga_len; ++i)
690 {
691 bp = &BREAKP(i);
692 smsg((char_u *)_("%3d %s %s line %ld"),
693 bp->dbg_nr,
694 bp->dbg_type == DBG_FUNC ? "func" : "file",
695 bp->dbg_name,
696 (long)bp->dbg_lnum);
697 }
698}
699
700/*
701 * Find a breakpoint for a function or sourced file.
702 * Returns line number at which to break; zero when no matching breakpoint.
703 */
704 linenr_T
705dbg_find_breakpoint(file, fname, after)
706 int file; /* TRUE for a file, FALSE for a function */
707 char_u *fname; /* file or function name */
708 linenr_T after; /* after this line number */
709{
Bram Moolenaar05159a02005-02-26 23:04:13 +0000710 return debuggy_find(file, fname, after, &dbg_breakp, NULL);
711}
712
713#if defined(FEAT_PROFILE) || defined(PROTO)
714/*
715 * Return TRUE if profiling is on for a function or sourced file.
716 */
717 int
718has_profiling(file, fname, fp)
719 int file; /* TRUE for a file, FALSE for a function */
720 char_u *fname; /* file or function name */
721 int *fp; /* return: forceit */
722{
723 return (debuggy_find(file, fname, (linenr_T)0, &prof_ga, fp)
724 != (linenr_T)0);
725}
726#endif
727
728/*
729 * Common code for dbg_find_breakpoint() and has_profiling().
730 */
731 static linenr_T
732debuggy_find(file, fname, after, gap, fp)
733 int file; /* TRUE for a file, FALSE for a function */
734 char_u *fname; /* file or function name */
735 linenr_T after; /* after this line number */
736 garray_T *gap; /* either &dbg_breakp or &prof_ga */
737 int *fp; /* if not NULL: return forceit */
738{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 struct debuggy *bp;
740 int i;
741 linenr_T lnum = 0;
742 regmatch_T regmatch;
743 char_u *name = fname;
744 int prev_got_int;
745
Bram Moolenaar05159a02005-02-26 23:04:13 +0000746 /* Return quickly when there are no breakpoints. */
747 if (gap->ga_len == 0)
748 return (linenr_T)0;
749
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 /* Replace K_SNR in function name with "<SNR>". */
751 if (!file && fname[0] == K_SPECIAL)
752 {
753 name = alloc((unsigned)STRLEN(fname) + 3);
754 if (name == NULL)
755 name = fname;
756 else
757 {
758 STRCPY(name, "<SNR>");
759 STRCPY(name + 5, fname + 3);
760 }
761 }
762
Bram Moolenaar05159a02005-02-26 23:04:13 +0000763 for (i = 0; i < gap->ga_len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000765 /* Skip entries that are not useful or are for a line that is beyond
766 * an already found breakpoint. */
767 bp = &DEBUGGY(gap, i);
768 if (((bp->dbg_type == DBG_FILE) == file && (
769#ifdef FEAT_PROFILE
770 gap == &prof_ga ||
771#endif
772 (bp->dbg_lnum > after && (lnum == 0 || bp->dbg_lnum < lnum)))))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 {
774 regmatch.regprog = bp->dbg_prog;
775 regmatch.rm_ic = FALSE;
776 /*
Bram Moolenaar05159a02005-02-26 23:04:13 +0000777 * Save the value of got_int and reset it. We don't want a
778 * previous interruption cancel matching, only hitting CTRL-C
779 * while matching should abort it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 */
781 prev_got_int = got_int;
782 got_int = FALSE;
783 if (vim_regexec(&regmatch, name, (colnr_T)0))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000784 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 lnum = bp->dbg_lnum;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000786 if (fp != NULL)
787 *fp = bp->dbg_forceit;
788 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 got_int |= prev_got_int;
790 }
791 }
792 if (name != fname)
793 vim_free(name);
794
795 return lnum;
796}
797
798/*
799 * Called when a breakpoint was encountered.
800 */
801 void
802dbg_breakpoint(name, lnum)
803 char_u *name;
804 linenr_T lnum;
805{
806 /* We need to check if this line is actually executed in do_one_cmd() */
807 debug_breakpoint_name = name;
808 debug_breakpoint_lnum = lnum;
809}
Bram Moolenaar05159a02005-02-26 23:04:13 +0000810
811
Bram Moolenaar433f7c82006-03-21 21:29:36 +0000812# if defined(FEAT_PROFILE) || defined(FEAT_RELTIME) || defined(PROTO)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000813/*
814 * Store the current time in "tm".
815 */
816 void
817profile_start(tm)
818 proftime_T *tm;
819{
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +0000820# ifdef WIN3264
821 QueryPerformanceCounter(tm);
822# else
Bram Moolenaar05159a02005-02-26 23:04:13 +0000823 gettimeofday(tm, NULL);
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +0000824# endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000825}
826
827/*
828 * Compute the elapsed time from "tm" till now and store in "tm".
829 */
830 void
831profile_end(tm)
832 proftime_T *tm;
833{
834 proftime_T now;
835
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +0000836# ifdef WIN3264
837 QueryPerformanceCounter(&now);
838 tm->QuadPart = now.QuadPart - tm->QuadPart;
839# else
Bram Moolenaar05159a02005-02-26 23:04:13 +0000840 gettimeofday(&now, NULL);
841 tm->tv_usec = now.tv_usec - tm->tv_usec;
842 tm->tv_sec = now.tv_sec - tm->tv_sec;
843 if (tm->tv_usec < 0)
844 {
845 tm->tv_usec += 1000000;
846 --tm->tv_sec;
847 }
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +0000848# endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000849}
850
851/*
852 * Subtract the time "tm2" from "tm".
853 */
854 void
855profile_sub(tm, tm2)
856 proftime_T *tm, *tm2;
857{
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +0000858# ifdef WIN3264
859 tm->QuadPart -= tm2->QuadPart;
860# else
Bram Moolenaar05159a02005-02-26 23:04:13 +0000861 tm->tv_usec -= tm2->tv_usec;
862 tm->tv_sec -= tm2->tv_sec;
863 if (tm->tv_usec < 0)
864 {
865 tm->tv_usec += 1000000;
866 --tm->tv_sec;
867 }
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +0000868# endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000869}
870
871/*
Bram Moolenaar433f7c82006-03-21 21:29:36 +0000872 * Return a string that represents the time in "tm".
873 * Uses a static buffer!
874 */
875 char *
876profile_msg(tm)
877 proftime_T *tm;
878{
879 static char buf[50];
880
881# ifdef WIN3264
882 LARGE_INTEGER fr;
883
884 QueryPerformanceFrequency(&fr);
885 sprintf(buf, "%10.6lf", (double)tm->QuadPart / (double)fr.QuadPart);
886# else
887 sprintf(buf, "%3ld.%06ld", (long)tm->tv_sec, (long)tm->tv_usec);
888#endif
889 return buf;
890}
891
892# endif /* FEAT_PROFILE || FEAT_RELTIME */
893
894# if defined(FEAT_PROFILE) || defined(PROTO)
895/*
896 * Functions for profiling.
897 */
898static void script_do_profile __ARGS((scriptitem_T *si));
899static void script_dump_profile __ARGS((FILE *fd));
900static proftime_T prof_wait_time;
901
902/*
903 * Set the time in "tm" to zero.
904 */
905 void
906profile_zero(tm)
907 proftime_T *tm;
908{
909# ifdef WIN3264
910 tm->QuadPart = 0;
911# else
912 tm->tv_usec = 0;
913 tm->tv_sec = 0;
914# endif
915}
916
917/*
Bram Moolenaar05159a02005-02-26 23:04:13 +0000918 * Add the time "tm2" to "tm".
919 */
920 void
921profile_add(tm, tm2)
922 proftime_T *tm, *tm2;
923{
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +0000924# ifdef WIN3264
925 tm->QuadPart += tm2->QuadPart;
926# else
Bram Moolenaar05159a02005-02-26 23:04:13 +0000927 tm->tv_usec += tm2->tv_usec;
928 tm->tv_sec += tm2->tv_sec;
929 if (tm->tv_usec >= 1000000)
930 {
931 tm->tv_usec -= 1000000;
932 ++tm->tv_sec;
933 }
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +0000934# endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000935}
936
937/*
Bram Moolenaar1056d982006-03-09 22:37:52 +0000938 * Add the "self" time from the total time and the children's time.
939 */
940 void
941profile_self(self, total, children)
942 proftime_T *self, *total, *children;
943{
944 /* Check that the result won't be negative. Can happen with recursive
945 * calls. */
946#ifdef WIN3264
947 if (total->QuadPart <= children->QuadPart)
948 return;
949#else
950 if (total->tv_sec < children->tv_sec
951 || (total->tv_sec == children->tv_sec
952 && total->tv_usec <= children->tv_usec))
953 return;
954#endif
955 profile_add(self, total);
956 profile_sub(self, children);
957}
958
959/*
Bram Moolenaar05159a02005-02-26 23:04:13 +0000960 * Get the current waittime.
961 */
962 void
963profile_get_wait(tm)
964 proftime_T *tm;
965{
966 *tm = prof_wait_time;
967}
968
969/*
970 * Subtract the passed waittime since "tm" from "tma".
971 */
972 void
973profile_sub_wait(tm, tma)
974 proftime_T *tm, *tma;
975{
976 proftime_T tm3 = prof_wait_time;
977
978 profile_sub(&tm3, tm);
979 profile_sub(tma, &tm3);
980}
981
982/*
983 * Return TRUE if "tm1" and "tm2" are equal.
984 */
985 int
986profile_equal(tm1, tm2)
987 proftime_T *tm1, *tm2;
988{
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +0000989# ifdef WIN3264
990 return (tm1->QuadPart == tm2->QuadPart);
991# else
Bram Moolenaar05159a02005-02-26 23:04:13 +0000992 return (tm1->tv_usec == tm2->tv_usec && tm1->tv_sec == tm2->tv_sec);
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +0000993# endif
994}
995
996/*
997 * Return <0, 0 or >0 if "tm1" < "tm2", "tm1" == "tm2" or "tm1" > "tm2"
998 */
999 int
1000profile_cmp(tm1, tm2)
1001 proftime_T *tm1, *tm2;
1002{
1003# ifdef WIN3264
1004 return (int)(tm2->QuadPart - tm1->QuadPart);
1005# else
1006 if (tm1->tv_sec == tm2->tv_sec)
1007 return tm2->tv_usec - tm1->tv_usec;
1008 return tm2->tv_sec - tm1->tv_sec;
1009# endif
Bram Moolenaar05159a02005-02-26 23:04:13 +00001010}
1011
Bram Moolenaar05159a02005-02-26 23:04:13 +00001012static char_u *profile_fname = NULL;
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00001013static proftime_T pause_time;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001014
1015/*
1016 * ":profile cmd args"
1017 */
1018 void
1019ex_profile(eap)
1020 exarg_T *eap;
1021{
1022 char_u *e;
1023 int len;
1024
1025 e = skiptowhite(eap->arg);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001026 len = (int)(e - eap->arg);
Bram Moolenaar05159a02005-02-26 23:04:13 +00001027 e = skipwhite(e);
1028
1029 if (len == 5 && STRNCMP(eap->arg, "start", 5) == 0 && *e != NUL)
1030 {
1031 vim_free(profile_fname);
1032 profile_fname = vim_strsave(e);
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00001033 do_profiling = PROF_YES;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001034 profile_zero(&prof_wait_time);
1035 set_vim_var_nr(VV_PROFILING, 1L);
1036 }
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00001037 else if (do_profiling == PROF_NONE)
Bram Moolenaar05159a02005-02-26 23:04:13 +00001038 EMSG(_("E750: First use :profile start <fname>"));
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00001039 else if (STRCMP(eap->arg, "pause") == 0)
1040 {
1041 if (do_profiling == PROF_YES)
1042 profile_start(&pause_time);
1043 do_profiling = PROF_PAUSED;
1044 }
1045 else if (STRCMP(eap->arg, "continue") == 0)
1046 {
1047 if (do_profiling == PROF_PAUSED)
1048 {
1049 profile_end(&pause_time);
1050 profile_add(&prof_wait_time, &pause_time);
1051 }
1052 do_profiling = PROF_YES;
1053 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00001054 else
1055 {
1056 /* The rest is similar to ":breakadd". */
1057 ex_breakadd(eap);
1058 }
1059}
1060
1061/*
1062 * Dump the profiling info.
1063 */
1064 void
1065profile_dump()
1066{
1067 FILE *fd;
1068
1069 if (profile_fname != NULL)
1070 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001071 fd = mch_fopen((char *)profile_fname, "w");
Bram Moolenaar05159a02005-02-26 23:04:13 +00001072 if (fd == NULL)
1073 EMSG2(_(e_notopen), profile_fname);
1074 else
1075 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00001076 script_dump_profile(fd);
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001077 func_dump_profile(fd);
Bram Moolenaar05159a02005-02-26 23:04:13 +00001078 fclose(fd);
1079 }
1080 }
1081}
1082
1083/*
1084 * Start profiling script "fp".
1085 */
1086 static void
1087script_do_profile(si)
1088 scriptitem_T *si;
1089{
1090 si->sn_pr_count = 0;
1091 profile_zero(&si->sn_pr_total);
1092 profile_zero(&si->sn_pr_self);
1093
1094 ga_init2(&si->sn_prl_ga, sizeof(sn_prl_T), 100);
1095 si->sn_prl_idx = -1;
1096 si->sn_prof_on = TRUE;
1097 si->sn_pr_nest = 0;
1098}
1099
1100/*
1101 * save time when starting to invoke another script or function.
1102 */
1103 void
1104script_prof_save(tm)
1105 proftime_T *tm; /* place to store wait time */
1106{
1107 scriptitem_T *si;
1108
1109 if (current_SID > 0 && current_SID <= script_items.ga_len)
1110 {
1111 si = &SCRIPT_ITEM(current_SID);
1112 if (si->sn_prof_on && si->sn_pr_nest++ == 0)
1113 profile_start(&si->sn_pr_child);
1114 }
1115 profile_get_wait(tm);
1116}
1117
1118/*
1119 * Count time spent in children after invoking another script or function.
1120 */
1121 void
1122script_prof_restore(tm)
1123 proftime_T *tm;
1124{
1125 scriptitem_T *si;
1126
1127 if (current_SID > 0 && current_SID <= script_items.ga_len)
1128 {
1129 si = &SCRIPT_ITEM(current_SID);
1130 if (si->sn_prof_on && --si->sn_pr_nest == 0)
1131 {
1132 profile_end(&si->sn_pr_child);
1133 profile_sub_wait(tm, &si->sn_pr_child); /* don't count wait time */
1134 profile_add(&si->sn_pr_children, &si->sn_pr_child);
1135 profile_add(&si->sn_prl_children, &si->sn_pr_child);
1136 }
1137 }
1138}
1139
1140static proftime_T inchar_time;
1141
1142/*
1143 * Called when starting to wait for the user to type a character.
1144 */
1145 void
1146prof_inchar_enter()
1147{
1148 profile_start(&inchar_time);
1149}
1150
1151/*
1152 * Called when finished waiting for the user to type a character.
1153 */
1154 void
1155prof_inchar_exit()
1156{
1157 profile_end(&inchar_time);
1158 profile_add(&prof_wait_time, &inchar_time);
1159}
1160
1161/*
1162 * Dump the profiling results for all scripts in file "fd".
1163 */
1164 static void
1165script_dump_profile(fd)
1166 FILE *fd;
1167{
1168 int id;
1169 scriptitem_T *si;
1170 int i;
1171 FILE *sfd;
1172 sn_prl_T *pp;
1173
1174 for (id = 1; id <= script_items.ga_len; ++id)
1175 {
1176 si = &SCRIPT_ITEM(id);
1177 if (si->sn_prof_on)
1178 {
1179 fprintf(fd, "SCRIPT %s\n", si->sn_name);
1180 if (si->sn_pr_count == 1)
1181 fprintf(fd, "Sourced 1 time\n");
1182 else
1183 fprintf(fd, "Sourced %d times\n", si->sn_pr_count);
1184 fprintf(fd, "Total time: %s\n", profile_msg(&si->sn_pr_total));
1185 fprintf(fd, " Self time: %s\n", profile_msg(&si->sn_pr_self));
1186 fprintf(fd, "\n");
1187 fprintf(fd, "count total (s) self (s)\n");
1188
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001189 sfd = mch_fopen((char *)si->sn_name, "r");
Bram Moolenaar05159a02005-02-26 23:04:13 +00001190 if (sfd == NULL)
1191 fprintf(fd, "Cannot open file!\n");
1192 else
1193 {
1194 for (i = 0; i < si->sn_prl_ga.ga_len; ++i)
1195 {
1196 if (vim_fgets(IObuff, IOSIZE, sfd))
1197 break;
1198 pp = &PRL_ITEM(si, i);
1199 if (pp->snp_count > 0)
1200 {
1201 fprintf(fd, "%5d ", pp->snp_count);
1202 if (profile_equal(&pp->sn_prl_total, &pp->sn_prl_self))
1203 fprintf(fd, " ");
1204 else
1205 fprintf(fd, "%s ", profile_msg(&pp->sn_prl_total));
1206 fprintf(fd, "%s ", profile_msg(&pp->sn_prl_self));
1207 }
1208 else
1209 fprintf(fd, " ");
1210 fprintf(fd, "%s", IObuff);
1211 }
1212 fclose(sfd);
1213 }
1214 fprintf(fd, "\n");
1215 }
1216 }
1217}
1218
1219/*
1220 * Return TRUE when a function defined in the current script should be
1221 * profiled.
1222 */
1223 int
1224prof_def_func()
1225{
Bram Moolenaar53180ce2005-07-05 21:48:14 +00001226 if (current_SID > 0)
1227 return SCRIPT_ITEM(current_SID).sn_pr_force;
1228 return FALSE;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001229}
1230
1231# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232#endif
1233
1234/*
1235 * If 'autowrite' option set, try to write the file.
1236 * Careful: autocommands may make "buf" invalid!
1237 *
1238 * return FAIL for failure, OK otherwise
1239 */
1240 int
1241autowrite(buf, forceit)
1242 buf_T *buf;
1243 int forceit;
1244{
Bram Moolenaar373154b2007-02-13 05:19:30 +00001245 int r;
1246
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 if (!(p_aw || p_awa) || !p_write
1248#ifdef FEAT_QUICKFIX
Bram Moolenaar373154b2007-02-13 05:19:30 +00001249 /* never autowrite a "nofile" or "nowrite" buffer */
1250 || bt_dontwrite(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251#endif
Bram Moolenaar373154b2007-02-13 05:19:30 +00001252 || (!forceit && buf->b_p_ro) || buf->b_ffname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 return FAIL;
Bram Moolenaar373154b2007-02-13 05:19:30 +00001254 r = buf_write_all(buf, forceit);
1255
1256 /* Writing may succeed but the buffer still changed, e.g., when there is a
1257 * conversion error. We do want to return FAIL then. */
1258 if (buf_valid(buf) && bufIsChanged(buf))
1259 r = FAIL;
1260 return r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261}
1262
1263/*
1264 * flush all buffers, except the ones that are readonly
1265 */
1266 void
1267autowrite_all()
1268{
1269 buf_T *buf;
1270
1271 if (!(p_aw || p_awa) || !p_write)
1272 return;
1273 for (buf = firstbuf; buf; buf = buf->b_next)
1274 if (bufIsChanged(buf) && !buf->b_p_ro)
1275 {
1276 (void)buf_write_all(buf, FALSE);
1277#ifdef FEAT_AUTOCMD
1278 /* an autocommand may have deleted the buffer */
1279 if (!buf_valid(buf))
1280 buf = firstbuf;
1281#endif
1282 }
1283}
1284
1285/*
1286 * return TRUE if buffer was changed and cannot be abandoned.
1287 */
1288/*ARGSUSED*/
1289 int
1290check_changed(buf, checkaw, mult_win, forceit, allbuf)
1291 buf_T *buf;
1292 int checkaw; /* do autowrite if buffer was changed */
1293 int mult_win; /* check also when several wins for the buf */
1294 int forceit;
1295 int allbuf; /* may write all buffers */
1296{
1297 if ( !forceit
1298 && bufIsChanged(buf)
1299 && (mult_win || buf->b_nwindows <= 1)
1300 && (!checkaw || autowrite(buf, forceit) == FAIL))
1301 {
1302#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1303 if ((p_confirm || cmdmod.confirm) && p_write)
1304 {
1305 buf_T *buf2;
1306 int count = 0;
1307
1308 if (allbuf)
1309 for (buf2 = firstbuf; buf2 != NULL; buf2 = buf2->b_next)
1310 if (bufIsChanged(buf2)
1311 && (buf2->b_ffname != NULL
1312# ifdef FEAT_BROWSE
1313 || cmdmod.browse
1314# endif
1315 ))
1316 ++count;
1317# ifdef FEAT_AUTOCMD
1318 if (!buf_valid(buf))
1319 /* Autocommand deleted buffer, oops! It's not changed now. */
1320 return FALSE;
1321# endif
1322 dialog_changed(buf, count > 1);
1323# ifdef FEAT_AUTOCMD
1324 if (!buf_valid(buf))
1325 /* Autocommand deleted buffer, oops! It's not changed now. */
1326 return FALSE;
1327# endif
1328 return bufIsChanged(buf);
1329 }
1330#endif
1331 EMSG(_(e_nowrtmsg));
1332 return TRUE;
1333 }
1334 return FALSE;
1335}
1336
1337#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
1338
1339#if defined(FEAT_BROWSE) || defined(PROTO)
1340/*
1341 * When wanting to write a file without a file name, ask the user for a name.
1342 */
1343 void
1344browse_save_fname(buf)
1345 buf_T *buf;
1346{
1347 if (buf->b_fname == NULL)
1348 {
1349 char_u *fname;
1350
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00001351 fname = do_browse(BROWSE_SAVE, (char_u *)_("Save As"),
1352 NULL, NULL, NULL, NULL, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 if (fname != NULL)
1354 {
1355 if (setfname(buf, fname, NULL, TRUE) == OK)
1356 buf->b_flags |= BF_NOTEDITED;
1357 vim_free(fname);
1358 }
1359 }
1360}
1361#endif
1362
1363/*
1364 * Ask the user what to do when abondoning a changed buffer.
1365 * Must check 'write' option first!
1366 */
1367 void
1368dialog_changed(buf, checkall)
1369 buf_T *buf;
1370 int checkall; /* may abandon all changed buffers */
1371{
1372 char_u buff[IOSIZE];
1373 int ret;
1374 buf_T *buf2;
1375
Bram Moolenaar82cf9b62005-06-07 21:09:25 +00001376 dialog_msg(buff, _("Save changes to \"%s\"?"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 (buf->b_fname != NULL) ?
1378 buf->b_fname : (char_u *)_("Untitled"));
1379 if (checkall)
1380 ret = vim_dialog_yesnoallcancel(VIM_QUESTION, NULL, buff, 1);
1381 else
1382 ret = vim_dialog_yesnocancel(VIM_QUESTION, NULL, buff, 1);
1383
1384 if (ret == VIM_YES)
1385 {
1386#ifdef FEAT_BROWSE
1387 /* May get file name, when there is none */
1388 browse_save_fname(buf);
1389#endif
1390 if (buf->b_fname != NULL) /* didn't hit Cancel */
1391 (void)buf_write_all(buf, FALSE);
1392 }
1393 else if (ret == VIM_NO)
1394 {
1395 unchanged(buf, TRUE);
1396 }
1397 else if (ret == VIM_ALL)
1398 {
1399 /*
1400 * Write all modified files that can be written.
1401 * Skip readonly buffers, these need to be confirmed
1402 * individually.
1403 */
1404 for (buf2 = firstbuf; buf2 != NULL; buf2 = buf2->b_next)
1405 {
1406 if (bufIsChanged(buf2)
1407 && (buf2->b_ffname != NULL
1408#ifdef FEAT_BROWSE
1409 || cmdmod.browse
1410#endif
1411 )
1412 && !buf2->b_p_ro)
1413 {
1414#ifdef FEAT_BROWSE
1415 /* May get file name, when there is none */
1416 browse_save_fname(buf2);
1417#endif
1418 if (buf2->b_fname != NULL) /* didn't hit Cancel */
1419 (void)buf_write_all(buf2, FALSE);
1420#ifdef FEAT_AUTOCMD
1421 /* an autocommand may have deleted the buffer */
1422 if (!buf_valid(buf2))
1423 buf2 = firstbuf;
1424#endif
1425 }
1426 }
1427 }
1428 else if (ret == VIM_DISCARDALL)
1429 {
1430 /*
1431 * mark all buffers as unchanged
1432 */
1433 for (buf2 = firstbuf; buf2 != NULL; buf2 = buf2->b_next)
1434 unchanged(buf2, TRUE);
1435 }
1436}
1437#endif
1438
1439/*
1440 * Return TRUE if the buffer "buf" can be abandoned, either by making it
1441 * hidden, autowriting it or unloading it.
1442 */
1443 int
1444can_abandon(buf, forceit)
1445 buf_T *buf;
1446 int forceit;
1447{
1448 return ( P_HID(buf)
1449 || !bufIsChanged(buf)
1450 || buf->b_nwindows > 1
1451 || autowrite(buf, forceit) == OK
1452 || forceit);
1453}
1454
1455/*
1456 * Return TRUE if any buffer was changed and cannot be abandoned.
1457 * That changed buffer becomes the current buffer.
1458 */
1459 int
1460check_changed_any(hidden)
1461 int hidden; /* Only check hidden buffers */
1462{
1463 buf_T *buf;
1464 int save;
1465#ifdef FEAT_WINDOWS
1466 win_T *wp;
1467#endif
1468
1469 for (;;)
1470 {
1471 /* check curbuf first: if it was changed we can't abandon it */
1472 if (!hidden && curbufIsChanged())
1473 buf = curbuf;
1474 else
1475 {
1476 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1477 if ((!hidden || buf->b_nwindows == 0) && bufIsChanged(buf))
1478 break;
1479 }
1480 if (buf == NULL) /* No buffers changed */
1481 return FALSE;
1482
Bram Moolenaar373154b2007-02-13 05:19:30 +00001483 /* Try auto-writing the buffer. If this fails but the buffer no
1484 * longer exists it's not changed, that's OK. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 if (check_changed(buf, p_awa, TRUE, FALSE, TRUE) && buf_valid(buf))
1486 break; /* didn't save - still changes */
1487 }
1488
1489 exiting = FALSE;
1490#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1491 /*
1492 * When ":confirm" used, don't give an error message.
1493 */
1494 if (!(p_confirm || cmdmod.confirm))
1495#endif
1496 {
1497 /* There must be a wait_return for this message, do_buffer()
1498 * may cause a redraw. But wait_return() is a no-op when vgetc()
1499 * is busy (Quit used from window menu), then make sure we don't
1500 * cause a scroll up. */
Bram Moolenaar61660ea2006-04-07 21:40:07 +00001501 if (vgetc_busy > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502 {
1503 msg_row = cmdline_row;
1504 msg_col = 0;
1505 msg_didout = FALSE;
1506 }
1507 if (EMSG2(_("E162: No write since last change for buffer \"%s\""),
1508 buf_spname(buf) != NULL ? (char_u *)buf_spname(buf) :
1509 buf->b_fname))
1510 {
1511 save = no_wait_return;
1512 no_wait_return = FALSE;
1513 wait_return(FALSE);
1514 no_wait_return = save;
1515 }
1516 }
1517
1518#ifdef FEAT_WINDOWS
1519 /* Try to find a window that contains the buffer. */
1520 if (buf != curbuf)
1521 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1522 if (wp->w_buffer == buf)
1523 {
1524 win_goto(wp);
1525# ifdef FEAT_AUTOCMD
1526 /* Paranoia: did autocms wipe out the buffer with changes? */
1527 if (!buf_valid(buf))
1528 return TRUE;
1529# endif
1530 break;
1531 }
1532#endif
1533
1534 /* Open the changed buffer in the current window. */
1535 if (buf != curbuf)
1536 set_curbuf(buf, DOBUF_GOTO);
1537
1538 return TRUE;
1539}
1540
1541/*
1542 * return FAIL if there is no file name, OK if there is one
1543 * give error message for FAIL
1544 */
1545 int
1546check_fname()
1547{
1548 if (curbuf->b_ffname == NULL)
1549 {
1550 EMSG(_(e_noname));
1551 return FAIL;
1552 }
1553 return OK;
1554}
1555
1556/*
1557 * flush the contents of a buffer, unless it has no file name
1558 *
1559 * return FAIL for failure, OK otherwise
1560 */
1561 int
1562buf_write_all(buf, forceit)
1563 buf_T *buf;
1564 int forceit;
1565{
1566 int retval;
1567#ifdef FEAT_AUTOCMD
1568 buf_T *old_curbuf = curbuf;
1569#endif
1570
1571 retval = (buf_write(buf, buf->b_ffname, buf->b_fname,
1572 (linenr_T)1, buf->b_ml.ml_line_count, NULL,
1573 FALSE, forceit, TRUE, FALSE));
1574#ifdef FEAT_AUTOCMD
1575 if (curbuf != old_curbuf)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001576 {
1577 msg_source(hl_attr(HLF_W));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 MSG(_("Warning: Entered other buffer unexpectedly (check autocommands)"));
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001579 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580#endif
1581 return retval;
1582}
1583
1584/*
1585 * Code to handle the argument list.
1586 */
1587
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001588static char_u *do_one_arg __ARGS((char_u *str));
1589static int do_arglist __ARGS((char_u *str, int what, int after));
1590static void alist_check_arg_idx __ARGS((void));
1591static int editing_arg_idx __ARGS((win_T *win));
1592#ifdef FEAT_LISTCMDS
1593static int alist_add_list __ARGS((int count, char_u **files, int after));
1594#endif
1595#define AL_SET 1
1596#define AL_ADD 2
1597#define AL_DEL 3
1598
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001600 * Isolate one argument, taking backticks.
1601 * Changes the argument in-place, puts a NUL after it. Backticks remain.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 * Return a pointer to the start of the next argument.
1603 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001604 static char_u *
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605do_one_arg(str)
1606 char_u *str;
1607{
1608 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 int inbacktick;
1610
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 inbacktick = FALSE;
1612 for (p = str; *str; ++str)
1613 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001614 /* When the backslash is used for escaping the special meaning of a
1615 * character we need to keep it until wildcard expansion. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 if (rem_backslash(str))
1617 {
1618 *p++ = *str++;
1619 *p++ = *str;
1620 }
1621 else
1622 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001623 /* An item ends at a space not in backticks */
1624 if (!inbacktick && vim_isspace(*str))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 break;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001626 if (*str == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 inbacktick ^= TRUE;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001628 *p++ = *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 }
1630 }
1631 str = skipwhite(str);
1632 *p = NUL;
1633
1634 return str;
1635}
1636
Bram Moolenaar86b68352004-12-27 21:59:20 +00001637/*
1638 * Separate the arguments in "str" and return a list of pointers in the
1639 * growarray "gap".
1640 */
1641 int
1642get_arglist(gap, str)
1643 garray_T *gap;
1644 char_u *str;
1645{
1646 ga_init2(gap, (int)sizeof(char_u *), 20);
1647 while (*str != NUL)
1648 {
1649 if (ga_grow(gap, 1) == FAIL)
1650 {
1651 ga_clear(gap);
1652 return FAIL;
1653 }
1654 ((char_u **)gap->ga_data)[gap->ga_len++] = str;
1655
1656 /* Isolate one argument, change it in-place, put a NUL after it. */
1657 str = do_one_arg(str);
1658 }
1659 return OK;
1660}
1661
Bram Moolenaar7df351e2006-01-23 22:30:28 +00001662#if defined(FEAT_QUICKFIX) || defined(FEAT_SYN_HL) || defined(PROTO)
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00001663/*
1664 * Parse a list of arguments (file names), expand them and return in
1665 * "fnames[fcountp]".
1666 * Return FAIL or OK.
1667 */
1668 int
1669get_arglist_exp(str, fcountp, fnamesp)
1670 char_u *str;
1671 int *fcountp;
1672 char_u ***fnamesp;
1673{
1674 garray_T ga;
1675 int i;
1676
1677 if (get_arglist(&ga, str) == FAIL)
1678 return FAIL;
1679 i = gen_expand_wildcards(ga.ga_len, (char_u **)ga.ga_data,
1680 fcountp, fnamesp, EW_FILE|EW_NOTFOUND);
1681 ga_clear(&ga);
1682 return i;
1683}
1684#endif
1685
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686#if defined(FEAT_GUI) || defined(FEAT_CLIENTSERVER) || defined(PROTO)
1687/*
1688 * Redefine the argument list.
1689 */
1690 void
1691set_arglist(str)
1692 char_u *str;
1693{
1694 do_arglist(str, AL_SET, 0);
1695}
1696#endif
1697
1698/*
1699 * "what" == AL_SET: Redefine the argument list to 'str'.
1700 * "what" == AL_ADD: add files in 'str' to the argument list after "after".
1701 * "what" == AL_DEL: remove files in 'str' from the argument list.
1702 *
1703 * Return FAIL for failure, OK otherwise.
1704 */
1705/*ARGSUSED*/
1706 static int
1707do_arglist(str, what, after)
1708 char_u *str;
1709 int what;
1710 int after; /* 0 means before first one */
1711{
1712 garray_T new_ga;
1713 int exp_count;
1714 char_u **exp_files;
1715 int i;
1716#ifdef FEAT_LISTCMDS
1717 char_u *p;
1718 int match;
1719#endif
1720
1721 /*
1722 * Collect all file name arguments in "new_ga".
1723 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00001724 if (get_arglist(&new_ga, str) == FAIL)
1725 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726
1727#ifdef FEAT_LISTCMDS
1728 if (what == AL_DEL)
1729 {
1730 regmatch_T regmatch;
1731 int didone;
1732
1733 /*
1734 * Delete the items: use each item as a regexp and find a match in the
1735 * argument list.
1736 */
1737#ifdef CASE_INSENSITIVE_FILENAME
1738 regmatch.rm_ic = TRUE; /* Always ignore case */
1739#else
1740 regmatch.rm_ic = FALSE; /* Never ignore case */
1741#endif
1742 for (i = 0; i < new_ga.ga_len && !got_int; ++i)
1743 {
1744 p = ((char_u **)new_ga.ga_data)[i];
1745 p = file_pat_to_reg_pat(p, NULL, NULL, FALSE);
1746 if (p == NULL)
1747 break;
1748 regmatch.regprog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
1749 if (regmatch.regprog == NULL)
1750 {
1751 vim_free(p);
1752 break;
1753 }
1754
1755 didone = FALSE;
1756 for (match = 0; match < ARGCOUNT; ++match)
1757 if (vim_regexec(&regmatch, alist_name(&ARGLIST[match]),
1758 (colnr_T)0))
1759 {
1760 didone = TRUE;
1761 vim_free(ARGLIST[match].ae_fname);
1762 mch_memmove(ARGLIST + match, ARGLIST + match + 1,
1763 (ARGCOUNT - match - 1) * sizeof(aentry_T));
1764 --ALIST(curwin)->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 if (curwin->w_arg_idx > match)
1766 --curwin->w_arg_idx;
1767 --match;
1768 }
1769
1770 vim_free(regmatch.regprog);
1771 vim_free(p);
1772 if (!didone)
1773 EMSG2(_(e_nomatch2), ((char_u **)new_ga.ga_data)[i]);
1774 }
1775 ga_clear(&new_ga);
1776 }
1777 else
1778#endif
1779 {
1780 i = expand_wildcards(new_ga.ga_len, (char_u **)new_ga.ga_data,
1781 &exp_count, &exp_files, EW_DIR|EW_FILE|EW_ADDSLASH|EW_NOTFOUND);
1782 ga_clear(&new_ga);
1783 if (i == FAIL)
1784 return FAIL;
1785 if (exp_count == 0)
1786 {
1787 EMSG(_(e_nomatch));
1788 return FAIL;
1789 }
1790
1791#ifdef FEAT_LISTCMDS
1792 if (what == AL_ADD)
1793 {
1794 (void)alist_add_list(exp_count, exp_files, after);
1795 vim_free(exp_files);
1796 }
1797 else /* what == AL_SET */
1798#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00001799 alist_set(ALIST(curwin), exp_count, exp_files, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 }
1801
1802 alist_check_arg_idx();
1803
1804 return OK;
1805}
1806
1807/*
1808 * Check the validity of the arg_idx for each other window.
1809 */
1810 static void
1811alist_check_arg_idx()
1812{
1813#ifdef FEAT_WINDOWS
1814 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001815 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816
Bram Moolenaarf740b292006-02-16 22:11:02 +00001817 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 if (win->w_alist == curwin->w_alist)
1819 check_arg_idx(win);
1820#else
1821 check_arg_idx(curwin);
1822#endif
1823}
1824
1825/*
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001826 * Return TRUE if window "win" is editing then file at the current argument
1827 * index.
1828 */
1829 static int
1830editing_arg_idx(win)
1831 win_T *win;
1832{
1833 return !(win->w_arg_idx >= WARGCOUNT(win)
1834 || (win->w_buffer->b_fnum
1835 != WARGLIST(win)[win->w_arg_idx].ae_fnum
1836 && (win->w_buffer->b_ffname == NULL
1837 || !(fullpathcmp(
1838 alist_name(&WARGLIST(win)[win->w_arg_idx]),
1839 win->w_buffer->b_ffname, TRUE) & FPC_SAME))));
1840}
1841
1842/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 * Check if window "win" is editing the w_arg_idx file in its argument list.
1844 */
1845 void
1846check_arg_idx(win)
1847 win_T *win;
1848{
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001849 if (WARGCOUNT(win) > 1 && !editing_arg_idx(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 {
1851 /* We are not editing the current entry in the argument list.
1852 * Set "arg_had_last" if we are editing the last one. */
1853 win->w_arg_idx_invalid = TRUE;
1854 if (win->w_arg_idx != WARGCOUNT(win) - 1
1855 && arg_had_last == FALSE
1856#ifdef FEAT_WINDOWS
1857 && ALIST(win) == &global_alist
1858#endif
1859 && GARGCOUNT > 0
1860 && win->w_arg_idx < GARGCOUNT
1861 && (win->w_buffer->b_fnum == GARGLIST[GARGCOUNT - 1].ae_fnum
1862 || (win->w_buffer->b_ffname != NULL
1863 && (fullpathcmp(alist_name(&GARGLIST[GARGCOUNT - 1]),
1864 win->w_buffer->b_ffname, TRUE) & FPC_SAME))))
1865 arg_had_last = TRUE;
1866 }
1867 else
1868 {
1869 /* We are editing the current entry in the argument list.
1870 * Set "arg_had_last" if it's also the last one */
1871 win->w_arg_idx_invalid = FALSE;
1872 if (win->w_arg_idx == WARGCOUNT(win) - 1
1873#ifdef FEAT_WINDOWS
1874 && win->w_alist == &global_alist
1875#endif
1876 )
1877 arg_had_last = TRUE;
1878 }
1879}
1880
1881/*
1882 * ":args", ":argslocal" and ":argsglobal".
1883 */
1884 void
1885ex_args(eap)
1886 exarg_T *eap;
1887{
1888 int i;
1889
1890 if (eap->cmdidx != CMD_args)
1891 {
1892#if defined(FEAT_WINDOWS) && defined(FEAT_LISTCMDS)
1893 alist_unlink(ALIST(curwin));
1894 if (eap->cmdidx == CMD_argglobal)
1895 ALIST(curwin) = &global_alist;
1896 else /* eap->cmdidx == CMD_arglocal */
1897 alist_new();
1898#else
1899 ex_ni(eap);
1900 return;
1901#endif
1902 }
1903
1904 if (!ends_excmd(*eap->arg))
1905 {
1906 /*
1907 * ":args file ..": define new argument list, handle like ":next"
1908 * Also for ":argslocal file .." and ":argsglobal file ..".
1909 */
1910 ex_next(eap);
1911 }
1912 else
1913#if defined(FEAT_WINDOWS) && defined(FEAT_LISTCMDS)
1914 if (eap->cmdidx == CMD_args)
1915#endif
1916 {
1917 /*
1918 * ":args": list arguments.
1919 */
1920 if (ARGCOUNT > 0)
1921 {
1922 /* Overwrite the command, for a short list there is no scrolling
1923 * required and no wait_return(). */
1924 gotocmdline(TRUE);
1925 for (i = 0; i < ARGCOUNT; ++i)
1926 {
1927 if (i == curwin->w_arg_idx)
1928 msg_putchar('[');
1929 msg_outtrans(alist_name(&ARGLIST[i]));
1930 if (i == curwin->w_arg_idx)
1931 msg_putchar(']');
1932 msg_putchar(' ');
1933 }
1934 }
1935 }
1936#if defined(FEAT_WINDOWS) && defined(FEAT_LISTCMDS)
1937 else if (eap->cmdidx == CMD_arglocal)
1938 {
1939 garray_T *gap = &curwin->w_alist->al_ga;
1940
1941 /*
1942 * ":argslocal": make a local copy of the global argument list.
1943 */
1944 if (ga_grow(gap, GARGCOUNT) == OK)
1945 for (i = 0; i < GARGCOUNT; ++i)
1946 if (GARGLIST[i].ae_fname != NULL)
1947 {
1948 AARGLIST(curwin->w_alist)[gap->ga_len].ae_fname =
1949 vim_strsave(GARGLIST[i].ae_fname);
1950 AARGLIST(curwin->w_alist)[gap->ga_len].ae_fnum =
1951 GARGLIST[i].ae_fnum;
1952 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 }
1954 }
1955#endif
1956}
1957
1958/*
1959 * ":previous", ":sprevious", ":Next" and ":sNext".
1960 */
1961 void
1962ex_previous(eap)
1963 exarg_T *eap;
1964{
1965 /* If past the last one already, go to the last one. */
1966 if (curwin->w_arg_idx - (int)eap->line2 >= ARGCOUNT)
1967 do_argfile(eap, ARGCOUNT - 1);
1968 else
1969 do_argfile(eap, curwin->w_arg_idx - (int)eap->line2);
1970}
1971
1972/*
1973 * ":rewind", ":first", ":sfirst" and ":srewind".
1974 */
1975 void
1976ex_rewind(eap)
1977 exarg_T *eap;
1978{
1979 do_argfile(eap, 0);
1980}
1981
1982/*
1983 * ":last" and ":slast".
1984 */
1985 void
1986ex_last(eap)
1987 exarg_T *eap;
1988{
1989 do_argfile(eap, ARGCOUNT - 1);
1990}
1991
1992/*
1993 * ":argument" and ":sargument".
1994 */
1995 void
1996ex_argument(eap)
1997 exarg_T *eap;
1998{
1999 int i;
2000
2001 if (eap->addr_count > 0)
2002 i = eap->line2 - 1;
2003 else
2004 i = curwin->w_arg_idx;
2005 do_argfile(eap, i);
2006}
2007
2008/*
2009 * Edit file "argn" of the argument lists.
2010 */
2011 void
2012do_argfile(eap, argn)
2013 exarg_T *eap;
2014 int argn;
2015{
2016 int other;
2017 char_u *p;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002018 int old_arg_idx = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019
2020 if (argn < 0 || argn >= ARGCOUNT)
2021 {
2022 if (ARGCOUNT <= 1)
2023 EMSG(_("E163: There is only one file to edit"));
2024 else if (argn < 0)
2025 EMSG(_("E164: Cannot go before first file"));
2026 else
2027 EMSG(_("E165: Cannot go beyond last file"));
2028 }
2029 else
2030 {
2031 setpcmark();
2032#ifdef FEAT_GUI
2033 need_mouse_correct = TRUE;
2034#endif
2035
2036#ifdef FEAT_WINDOWS
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00002037 /* split window or create new tab page first */
2038 if (*eap->cmd == 's' || cmdmod.tab != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 {
2040 if (win_split(0, 0) == FAIL)
2041 return;
2042# ifdef FEAT_SCROLLBIND
2043 curwin->w_p_scb = FALSE;
2044# endif
2045 }
2046 else
2047#endif
2048 {
2049 /*
2050 * if 'hidden' set, only check for changed file when re-editing
2051 * the same buffer
2052 */
2053 other = TRUE;
2054 if (P_HID(curbuf))
2055 {
2056 p = fix_fname(alist_name(&ARGLIST[argn]));
2057 other = otherfile(p);
2058 vim_free(p);
2059 }
2060 if ((!P_HID(curbuf) || !other)
2061 && check_changed(curbuf, TRUE, !other, eap->forceit, FALSE))
2062 return;
2063 }
2064
2065 curwin->w_arg_idx = argn;
2066 if (argn == ARGCOUNT - 1
2067#ifdef FEAT_WINDOWS
2068 && curwin->w_alist == &global_alist
2069#endif
2070 )
2071 arg_had_last = TRUE;
2072
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002073 /* Edit the file; always use the last known line number.
2074 * When it fails (e.g. Abort for already edited file) restore the
2075 * argument index. */
2076 if (do_ecmd(0, alist_name(&ARGLIST[curwin->w_arg_idx]), NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 eap, ECMD_LAST,
2078 (P_HID(curwin->w_buffer) ? ECMD_HIDE : 0) +
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002079 (eap->forceit ? ECMD_FORCEIT : 0)) == FAIL)
2080 curwin->w_arg_idx = old_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 /* like Vi: set the mark where the cursor is in the file. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002082 else if (eap->cmdidx != CMD_argdo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 setmark('\'');
2084 }
2085}
2086
2087/*
2088 * ":next", and commands that behave like it.
2089 */
2090 void
2091ex_next(eap)
2092 exarg_T *eap;
2093{
2094 int i;
2095
2096 /*
2097 * check for changed buffer now, if this fails the argument list is not
2098 * redefined.
2099 */
2100 if ( P_HID(curbuf)
2101 || eap->cmdidx == CMD_snext
2102 || !check_changed(curbuf, TRUE, FALSE, eap->forceit, FALSE))
2103 {
2104 if (*eap->arg != NUL) /* redefine file list */
2105 {
2106 if (do_arglist(eap->arg, AL_SET, 0) == FAIL)
2107 return;
2108 i = 0;
2109 }
2110 else
2111 i = curwin->w_arg_idx + (int)eap->line2;
2112 do_argfile(eap, i);
2113 }
2114}
2115
2116#ifdef FEAT_LISTCMDS
2117/*
2118 * ":argedit"
2119 */
2120 void
2121ex_argedit(eap)
2122 exarg_T *eap;
2123{
2124 int fnum;
2125 int i;
2126 char_u *s;
2127
2128 /* Add the argument to the buffer list and get the buffer number. */
2129 fnum = buflist_add(eap->arg, BLN_LISTED);
2130
2131 /* Check if this argument is already in the argument list. */
2132 for (i = 0; i < ARGCOUNT; ++i)
2133 if (ARGLIST[i].ae_fnum == fnum)
2134 break;
2135 if (i == ARGCOUNT)
2136 {
2137 /* Can't find it, add it to the argument list. */
2138 s = vim_strsave(eap->arg);
2139 if (s == NULL)
2140 return;
2141 i = alist_add_list(1, &s,
2142 eap->addr_count > 0 ? (int)eap->line2 : curwin->w_arg_idx + 1);
2143 if (i < 0)
2144 return;
2145 curwin->w_arg_idx = i;
2146 }
2147
2148 alist_check_arg_idx();
2149
2150 /* Edit the argument. */
2151 do_argfile(eap, i);
2152}
2153
2154/*
2155 * ":argadd"
2156 */
2157 void
2158ex_argadd(eap)
2159 exarg_T *eap;
2160{
2161 do_arglist(eap->arg, AL_ADD,
2162 eap->addr_count > 0 ? (int)eap->line2 : curwin->w_arg_idx + 1);
2163#ifdef FEAT_TITLE
2164 maketitle();
2165#endif
2166}
2167
2168/*
2169 * ":argdelete"
2170 */
2171 void
2172ex_argdelete(eap)
2173 exarg_T *eap;
2174{
2175 int i;
2176 int n;
2177
2178 if (eap->addr_count > 0)
2179 {
2180 /* ":1,4argdel": Delete all arguments in the range. */
2181 if (eap->line2 > ARGCOUNT)
2182 eap->line2 = ARGCOUNT;
2183 n = eap->line2 - eap->line1 + 1;
2184 if (*eap->arg != NUL || n <= 0)
2185 EMSG(_(e_invarg));
2186 else
2187 {
2188 for (i = eap->line1; i <= eap->line2; ++i)
2189 vim_free(ARGLIST[i - 1].ae_fname);
2190 mch_memmove(ARGLIST + eap->line1 - 1, ARGLIST + eap->line2,
2191 (size_t)((ARGCOUNT - eap->line2) * sizeof(aentry_T)));
2192 ALIST(curwin)->al_ga.ga_len -= n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 if (curwin->w_arg_idx >= eap->line2)
2194 curwin->w_arg_idx -= n;
2195 else if (curwin->w_arg_idx > eap->line1)
2196 curwin->w_arg_idx = eap->line1;
2197 }
2198 }
2199 else if (*eap->arg == NUL)
2200 EMSG(_(e_argreq));
2201 else
2202 do_arglist(eap->arg, AL_DEL, 0);
2203#ifdef FEAT_TITLE
2204 maketitle();
2205#endif
2206}
2207
2208/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002209 * ":argdo", ":windo", ":bufdo", ":tabdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 */
2211 void
2212ex_listdo(eap)
2213 exarg_T *eap;
2214{
2215 int i;
2216#ifdef FEAT_WINDOWS
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002217 win_T *wp;
2218 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219#endif
2220 buf_T *buf;
2221 int next_fnum = 0;
2222#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
2223 char_u *save_ei = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002225 char_u *p_shm_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226
2227#ifndef FEAT_WINDOWS
2228 if (eap->cmdidx == CMD_windo)
2229 {
2230 ex_ni(eap);
2231 return;
2232 }
2233#endif
2234
2235#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002236 if (eap->cmdidx != CMD_windo && eap->cmdidx != CMD_tabdo)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002237 /* Don't do syntax HL autocommands. Skipping the syntax file is a
2238 * great speed improvement. */
2239 save_ei = au_event_disable(",Syntax");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240#endif
2241
2242 if (eap->cmdidx == CMD_windo
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002243 || eap->cmdidx == CMD_tabdo
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 || P_HID(curbuf)
2245 || !check_changed(curbuf, TRUE, FALSE, eap->forceit, FALSE))
2246 {
2247 /* start at the first argument/window/buffer */
2248 i = 0;
2249#ifdef FEAT_WINDOWS
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002250 wp = firstwin;
2251 tp = first_tabpage;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252#endif
2253 /* set pcmark now */
2254 if (eap->cmdidx == CMD_bufdo)
2255 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
2256 else
2257 setpcmark();
2258 listcmd_busy = TRUE; /* avoids setting pcmark below */
2259
2260 while (!got_int)
2261 {
2262 if (eap->cmdidx == CMD_argdo)
2263 {
2264 /* go to argument "i" */
2265 if (i == ARGCOUNT)
2266 break;
2267 /* Don't call do_argfile() when already there, it will try
2268 * reloading the file. */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002269 if (curwin->w_arg_idx != i || !editing_arg_idx(curwin))
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002270 {
2271 /* Clear 'shm' to avoid that the file message overwrites
2272 * 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 do_argfile(eap, i);
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 (curwin->w_arg_idx != i)
2280 break;
2281 ++i;
2282 }
2283#ifdef FEAT_WINDOWS
2284 else if (eap->cmdidx == CMD_windo)
2285 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002286 /* go to window "wp" */
2287 if (!win_valid(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 break;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002289 win_goto(wp);
Bram Moolenaar41423242007-05-03 20:11:13 +00002290 if (curwin != wp)
2291 break; /* something must be wrong */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002292 wp = curwin->w_next;
2293 }
2294 else if (eap->cmdidx == CMD_tabdo)
2295 {
2296 /* go to window "tp" */
2297 if (!valid_tabpage(tp))
2298 break;
2299 goto_tabpage_tp(tp);
2300 tp = tp->tp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 }
2302#endif
2303 else if (eap->cmdidx == CMD_bufdo)
2304 {
2305 /* Remember the number of the next listed buffer, in case
2306 * ":bwipe" is used or autocommands do something strange. */
2307 next_fnum = -1;
2308 for (buf = curbuf->b_next; buf != NULL; buf = buf->b_next)
2309 if (buf->b_p_bl)
2310 {
2311 next_fnum = buf->b_fnum;
2312 break;
2313 }
2314 }
2315
2316 /* execute the command */
2317 do_cmdline(eap->arg, eap->getline, eap->cookie,
2318 DOCMD_VERBOSE + DOCMD_NOWAIT);
2319
2320 if (eap->cmdidx == CMD_bufdo)
2321 {
2322 /* Done? */
2323 if (next_fnum < 0)
2324 break;
2325 /* Check if the buffer still exists. */
2326 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2327 if (buf->b_fnum == next_fnum)
2328 break;
2329 if (buf == NULL)
2330 break;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002331
2332 /* Go to the next buffer. Clear 'shm' to avoid that the file
2333 * message overwrites any output from the command. */
2334 p_shm_save = vim_strsave(p_shm);
2335 set_option_value((char_u *)"shm", 0L, (char_u *)"", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 goto_buffer(eap, DOBUF_FIRST, FORWARD, next_fnum);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002337 set_option_value((char_u *)"shm", 0L, p_shm_save, 0);
2338 vim_free(p_shm_save);
2339
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 /* If autocommands took us elsewhere, quit here */
2341 if (curbuf->b_fnum != next_fnum)
2342 break;
2343 }
2344
2345 if (eap->cmdidx == CMD_windo)
2346 {
2347 validate_cursor(); /* cursor may have moved */
2348#ifdef FEAT_SCROLLBIND
2349 /* required when 'scrollbind' has been set */
2350 if (curwin->w_p_scb)
2351 do_check_scrollbind(TRUE);
2352#endif
2353 }
2354 }
2355 listcmd_busy = FALSE;
2356 }
2357
2358#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
Bram Moolenaar6ac54292005-02-02 23:07:25 +00002359 if (save_ei != NULL)
2360 {
2361 au_event_restore(save_ei);
2362 apply_autocmds(EVENT_SYNTAX, curbuf->b_p_syn,
2363 curbuf->b_fname, TRUE, curbuf);
2364 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365#endif
2366}
2367
2368/*
2369 * Add files[count] to the arglist of the current window after arg "after".
2370 * The file names in files[count] must have been allocated and are taken over.
2371 * Files[] itself is not taken over.
2372 * Returns index of first added argument. Returns -1 when failed (out of mem).
2373 */
2374 static int
2375alist_add_list(count, files, after)
2376 int count;
2377 char_u **files;
2378 int after; /* where to add: 0 = before first one */
2379{
2380 int i;
2381
2382 if (ga_grow(&ALIST(curwin)->al_ga, count) == OK)
2383 {
2384 if (after < 0)
2385 after = 0;
2386 if (after > ARGCOUNT)
2387 after = ARGCOUNT;
2388 if (after < ARGCOUNT)
2389 mch_memmove(&(ARGLIST[after + count]), &(ARGLIST[after]),
2390 (ARGCOUNT - after) * sizeof(aentry_T));
2391 for (i = 0; i < count; ++i)
2392 {
2393 ARGLIST[after + i].ae_fname = files[i];
2394 ARGLIST[after + i].ae_fnum = buflist_add(files[i], BLN_LISTED);
2395 }
2396 ALIST(curwin)->al_ga.ga_len += count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 if (curwin->w_arg_idx >= after)
2398 ++curwin->w_arg_idx;
2399 return after;
2400 }
2401
2402 for (i = 0; i < count; ++i)
2403 vim_free(files[i]);
2404 return -1;
2405}
2406
2407#endif /* FEAT_LISTCMDS */
2408
2409#ifdef FEAT_EVAL
2410/*
2411 * ":compiler[!] {name}"
2412 */
2413 void
2414ex_compiler(eap)
2415 exarg_T *eap;
2416{
2417 char_u *buf;
2418 char_u *old_cur_comp = NULL;
2419 char_u *p;
2420
2421 if (*eap->arg == NUL)
2422 {
2423 /* List all compiler scripts. */
2424 do_cmdline_cmd((char_u *)"echo globpath(&rtp, 'compiler/*.vim')");
2425 /* ) keep the indenter happy... */
2426 }
2427 else
2428 {
2429 buf = alloc((unsigned)(STRLEN(eap->arg) + 14));
2430 if (buf != NULL)
2431 {
2432 if (eap->forceit)
2433 {
2434 /* ":compiler! {name}" sets global options */
2435 do_cmdline_cmd((char_u *)
2436 "command -nargs=* CompilerSet set <args>");
2437 }
2438 else
2439 {
2440 /* ":compiler! {name}" sets local options.
2441 * To remain backwards compatible "current_compiler" is always
2442 * used. A user's compiler plugin may set it, the distributed
2443 * plugin will then skip the settings. Afterwards set
2444 * "b:current_compiler" and restore "current_compiler". */
2445 old_cur_comp = get_var_value((char_u *)"current_compiler");
2446 if (old_cur_comp != NULL)
2447 old_cur_comp = vim_strsave(old_cur_comp);
2448 do_cmdline_cmd((char_u *)
2449 "command -nargs=* CompilerSet setlocal <args>");
2450 }
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00002451 do_unlet((char_u *)"current_compiler", TRUE);
2452 do_unlet((char_u *)"b:current_compiler", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453
2454 sprintf((char *)buf, "compiler/%s.vim", eap->arg);
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00002455 if (source_runtime(buf, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 EMSG2(_("E666: compiler not supported: %s"), eap->arg);
2457 vim_free(buf);
2458
2459 do_cmdline_cmd((char_u *)":delcommand CompilerSet");
2460
2461 /* Set "b:current_compiler" from "current_compiler". */
2462 p = get_var_value((char_u *)"current_compiler");
2463 if (p != NULL)
2464 set_internal_string_var((char_u *)"b:current_compiler", p);
2465
2466 /* Restore "current_compiler" for ":compiler {name}". */
2467 if (!eap->forceit)
2468 {
2469 if (old_cur_comp != NULL)
2470 {
2471 set_internal_string_var((char_u *)"current_compiler",
2472 old_cur_comp);
2473 vim_free(old_cur_comp);
2474 }
2475 else
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00002476 do_unlet((char_u *)"current_compiler", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 }
2478 }
2479 }
2480}
2481#endif
2482
2483/*
2484 * ":runtime {name}"
2485 */
2486 void
2487ex_runtime(eap)
2488 exarg_T *eap;
2489{
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00002490 source_runtime(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491}
2492
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00002493static void source_callback __ARGS((char_u *fname, void *cookie));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00002495/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496 static void
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00002497source_callback(fname, cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 char_u *fname;
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00002499 void *cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500{
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002501 (void)do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502}
2503
2504/*
2505 * Source the file "name" from all directories in 'runtimepath'.
2506 * "name" can contain wildcards.
2507 * When "all" is TRUE, source all files, otherwise only the first one.
2508 * return FAIL when no file could be sourced, OK otherwise.
2509 */
2510 int
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00002511source_runtime(name, all)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 char_u *name;
2513 int all;
2514{
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00002515 return do_in_runtimepath(name, all, source_callback, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516}
2517
2518/*
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00002519 * Find "name" in 'runtimepath'. When found, invoke the callback function for
2520 * it: callback(fname, "cookie")
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 * When "all" is TRUE repeat for all matches, otherwise only the first one is
2522 * used.
2523 * Returns OK when at least one match found, FAIL otherwise.
2524 */
2525 int
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00002526do_in_runtimepath(name, all, callback, cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 char_u *name;
2528 int all;
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00002529 void (*callback)__ARGS((char_u *fname, void *ck));
2530 void *cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531{
2532 char_u *rtp;
2533 char_u *np;
2534 char_u *buf;
2535 char_u *rtp_copy;
2536 char_u *tail;
2537 int num_files;
2538 char_u **files;
2539 int i;
2540 int did_one = FALSE;
2541#ifdef AMIGA
2542 struct Process *proc = (struct Process *)FindTask(0L);
2543 APTR save_winptr = proc->pr_WindowPtr;
2544
2545 /* Avoid a requester here for a volume that doesn't exist. */
2546 proc->pr_WindowPtr = (APTR)-1L;
2547#endif
2548
2549 /* Make a copy of 'runtimepath'. Invoking the callback may change the
2550 * value. */
2551 rtp_copy = vim_strsave(p_rtp);
2552 buf = alloc(MAXPATHL);
2553 if (buf != NULL && rtp_copy != NULL)
2554 {
2555 if (p_verbose > 1)
Bram Moolenaar54ee7752005-05-31 22:22:17 +00002556 {
2557 verbose_enter();
Bram Moolenaar555b2802005-05-19 21:08:39 +00002558 smsg((char_u *)_("Searching for \"%s\" in \"%s\""),
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 (char *)name, (char *)p_rtp);
Bram Moolenaar54ee7752005-05-31 22:22:17 +00002560 verbose_leave();
2561 }
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002562
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 /* Loop over all entries in 'runtimepath'. */
2564 rtp = rtp_copy;
2565 while (*rtp != NUL && (all || !did_one))
2566 {
2567 /* Copy the path from 'runtimepath' to buf[]. */
2568 copy_option_part(&rtp, buf, MAXPATHL, ",");
2569 if (STRLEN(buf) + STRLEN(name) + 2 < MAXPATHL)
2570 {
2571 add_pathsep(buf);
2572 tail = buf + STRLEN(buf);
2573
2574 /* Loop over all patterns in "name" */
2575 np = name;
2576 while (*np != NUL && (all || !did_one))
2577 {
2578 /* Append the pattern from "name" to buf[]. */
2579 copy_option_part(&np, tail, (int)(MAXPATHL - (tail - buf)),
2580 "\t ");
2581
2582 if (p_verbose > 2)
Bram Moolenaar54ee7752005-05-31 22:22:17 +00002583 {
2584 verbose_enter();
Bram Moolenaar555b2802005-05-19 21:08:39 +00002585 smsg((char_u *)_("Searching for \"%s\""), buf);
Bram Moolenaar54ee7752005-05-31 22:22:17 +00002586 verbose_leave();
2587 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588
2589 /* Expand wildcards, invoke the callback for each match. */
2590 if (gen_expand_wildcards(1, &buf, &num_files, &files,
2591 EW_FILE) == OK)
2592 {
2593 for (i = 0; i < num_files; ++i)
2594 {
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00002595 (*callback)(files[i], cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 did_one = TRUE;
2597 if (!all)
2598 break;
2599 }
2600 FreeWild(num_files, files);
2601 }
2602 }
2603 }
2604 }
2605 }
2606 vim_free(buf);
2607 vim_free(rtp_copy);
2608 if (p_verbose > 0 && !did_one)
Bram Moolenaar54ee7752005-05-31 22:22:17 +00002609 {
2610 verbose_enter();
Bram Moolenaar555b2802005-05-19 21:08:39 +00002611 smsg((char_u *)_("not found in 'runtimepath': \"%s\""), name);
Bram Moolenaar54ee7752005-05-31 22:22:17 +00002612 verbose_leave();
2613 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614
2615#ifdef AMIGA
2616 proc->pr_WindowPtr = save_winptr;
2617#endif
2618
2619 return did_one ? OK : FAIL;
2620}
2621
2622#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD)
2623/*
2624 * ":options"
2625 */
2626/*ARGSUSED*/
2627 void
2628ex_options(eap)
2629 exarg_T *eap;
2630{
2631 cmd_source((char_u *)SYS_OPTWIN_FILE, NULL);
2632}
2633#endif
2634
2635/*
2636 * ":source {fname}"
2637 */
2638 void
2639ex_source(eap)
2640 exarg_T *eap;
2641{
2642#ifdef FEAT_BROWSE
2643 if (cmdmod.browse)
2644 {
2645 char_u *fname = NULL;
2646
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002647 fname = do_browse(0, (char_u *)_("Source Vim script"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 NULL, NULL, BROWSE_FILTER_MACROS, NULL);
2649 if (fname != NULL)
2650 {
2651 cmd_source(fname, eap);
2652 vim_free(fname);
2653 }
2654 }
2655 else
2656#endif
2657 cmd_source(eap->arg, eap);
2658}
2659
2660 static void
2661cmd_source(fname, eap)
2662 char_u *fname;
2663 exarg_T *eap;
2664{
2665 if (*fname == NUL)
2666 EMSG(_(e_argreq));
2667
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 else if (eap != NULL && eap->forceit)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00002669 /* ":source!": read Normal mdoe commands
2670 * Need to execute the commands directly. This is required at least
2671 * for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 * - ":g" command busy
2673 * - after ":argdo", ":windo" or ":bufdo"
2674 * - another command follows
2675 * - inside a loop
2676 */
2677 openscript(fname, global_busy || listcmd_busy || eap->nextcmd != NULL
2678#ifdef FEAT_EVAL
2679 || eap->cstack->cs_idx >= 0
2680#endif
2681 );
2682
2683 /* ":source" read ex commands */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002684 else if (do_source(fname, FALSE, DOSO_NONE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 EMSG2(_(e_notopen), fname);
2686}
2687
2688/*
2689 * ":source" and associated commands.
2690 */
2691/*
2692 * Structure used to store info for each sourced file.
2693 * It is shared between do_source() and getsourceline().
2694 * This is required, because it needs to be handed to do_cmdline() and
2695 * sourcing can be done recursively.
2696 */
2697struct source_cookie
2698{
2699 FILE *fp; /* opened file for sourcing */
2700 char_u *nextline; /* if not NULL: line that was read ahead */
2701 int finished; /* ":finish" used */
2702#if defined (USE_CRNL) || defined (USE_CR)
2703 int fileformat; /* EOL_UNKNOWN, EOL_UNIX or EOL_DOS */
2704 int error; /* TRUE if LF found after CR-LF */
2705#endif
2706#ifdef FEAT_EVAL
2707 linenr_T breakpoint; /* next line with breakpoint or zero */
2708 char_u *fname; /* name of sourced file */
2709 int dbg_tick; /* debug_tick when breakpoint was set */
2710 int level; /* top nesting level of sourced file */
2711#endif
2712#ifdef FEAT_MBYTE
2713 vimconv_T conv; /* type of conversion */
2714#endif
2715};
2716
2717#ifdef FEAT_EVAL
2718/*
2719 * Return the address holding the next breakpoint line for a source cookie.
2720 */
2721 linenr_T *
2722source_breakpoint(cookie)
2723 void *cookie;
2724{
2725 return &((struct source_cookie *)cookie)->breakpoint;
2726}
2727
2728/*
2729 * Return the address holding the debug tick for a source cookie.
2730 */
2731 int *
2732source_dbg_tick(cookie)
2733 void *cookie;
2734{
2735 return &((struct source_cookie *)cookie)->dbg_tick;
2736}
2737
2738/*
2739 * Return the nesting level for a source cookie.
2740 */
2741 int
2742source_level(cookie)
2743 void *cookie;
2744{
2745 return ((struct source_cookie *)cookie)->level;
2746}
2747#endif
2748
2749static char_u *get_one_sourceline __ARGS((struct source_cookie *sp));
2750
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751#if defined(WIN32) && defined(FEAT_CSCOPE)
2752static FILE *fopen_noinh_readbin __ARGS((char *filename));
2753
2754/*
2755 * Special function to open a file without handle inheritance.
2756 */
2757 static FILE *
2758fopen_noinh_readbin(filename)
2759 char *filename;
2760{
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00002761 int fd_tmp = mch_open(filename, O_RDONLY | O_BINARY | O_NOINHERIT, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762
2763 if (fd_tmp == -1)
2764 return NULL;
2765 return fdopen(fd_tmp, READBIN);
2766}
2767#endif
2768
2769
2770/*
2771 * do_source: Read the file "fname" and execute its lines as EX commands.
2772 *
2773 * This function may be called recursively!
2774 *
2775 * return FAIL if file could not be opened, OK otherwise
2776 */
2777 int
2778do_source(fname, check_other, is_vimrc)
2779 char_u *fname;
2780 int check_other; /* check for .vimrc and _vimrc */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002781 int is_vimrc; /* DOSO_ value */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782{
2783 struct source_cookie cookie;
2784 char_u *save_sourcing_name;
2785 linenr_T save_sourcing_lnum;
2786 char_u *p;
2787 char_u *fname_exp;
2788 int retval = FAIL;
2789#ifdef FEAT_EVAL
2790 scid_T save_current_SID;
2791 static scid_T last_current_SID = 0;
2792 void *save_funccalp;
2793 int save_debug_break_level = debug_break_level;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002794 scriptitem_T *si = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795# ifdef UNIX
2796 struct stat st;
2797 int stat_ok;
2798# endif
2799#endif
2800#ifdef STARTUPTIME
2801 struct timeval tv_rel;
2802 struct timeval tv_start;
2803#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +00002804#ifdef FEAT_PROFILE
2805 proftime_T wait_start;
2806#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807
2808#ifdef RISCOS
2809 p = mch_munge_fname(fname);
2810#else
2811 p = expand_env_save(fname);
2812#endif
2813 if (p == NULL)
2814 return retval;
2815 fname_exp = fix_fname(p);
2816 vim_free(p);
2817 if (fname_exp == NULL)
2818 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 if (mch_isdir(fname_exp))
2820 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00002821 smsg((char_u *)_("Cannot source a directory: \"%s\""), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 goto theend;
2823 }
2824
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00002825#ifdef FEAT_AUTOCMD
Bram Moolenaar8dd1aa52007-01-16 20:33:19 +00002826 /* Apply SourceCmd autocommands, they should get the file and source it. */
2827 if (has_autocmd(EVENT_SOURCECMD, fname_exp, NULL)
2828 && apply_autocmds(EVENT_SOURCECMD, fname_exp, fname_exp,
2829 FALSE, curbuf))
2830# ifdef FEAT_EVAL
2831 return aborting() ? FAIL : OK;
2832# else
2833 return OK;
2834# endif
2835
2836 /* Apply SourcePre autocommands, they may get the file. */
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00002837 apply_autocmds(EVENT_SOURCEPRE, fname_exp, fname_exp, FALSE, curbuf);
2838#endif
2839
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840#if defined(WIN32) && defined(FEAT_CSCOPE)
2841 cookie.fp = fopen_noinh_readbin((char *)fname_exp);
2842#else
2843 cookie.fp = mch_fopen((char *)fname_exp, READBIN);
2844#endif
2845 if (cookie.fp == NULL && check_other)
2846 {
2847 /*
2848 * Try again, replacing file name ".vimrc" by "_vimrc" or vice versa,
2849 * and ".exrc" by "_exrc" or vice versa.
2850 */
2851 p = gettail(fname_exp);
2852 if ((*p == '.' || *p == '_')
2853 && (STRICMP(p + 1, "vimrc") == 0
2854 || STRICMP(p + 1, "gvimrc") == 0
2855 || STRICMP(p + 1, "exrc") == 0))
2856 {
2857 if (*p == '_')
2858 *p = '.';
2859 else
2860 *p = '_';
2861#if defined(WIN32) && defined(FEAT_CSCOPE)
2862 cookie.fp = fopen_noinh_readbin((char *)fname_exp);
2863#else
2864 cookie.fp = mch_fopen((char *)fname_exp, READBIN);
2865#endif
2866 }
2867 }
2868
2869 if (cookie.fp == NULL)
2870 {
2871 if (p_verbose > 0)
2872 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +00002873 verbose_enter();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 if (sourcing_name == NULL)
Bram Moolenaar555b2802005-05-19 21:08:39 +00002875 smsg((char_u *)_("could not source \"%s\""), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 else
2877 smsg((char_u *)_("line %ld: could not source \"%s\""),
Bram Moolenaar555b2802005-05-19 21:08:39 +00002878 sourcing_lnum, fname);
Bram Moolenaar54ee7752005-05-31 22:22:17 +00002879 verbose_leave();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880 }
2881 goto theend;
2882 }
2883
2884 /*
2885 * The file exists.
2886 * - In verbose mode, give a message.
2887 * - For a vimrc file, may want to set 'compatible', call vimrc_found().
2888 */
2889 if (p_verbose > 1)
2890 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +00002891 verbose_enter();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892 if (sourcing_name == NULL)
Bram Moolenaar555b2802005-05-19 21:08:39 +00002893 smsg((char_u *)_("sourcing \"%s\""), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894 else
2895 smsg((char_u *)_("line %ld: sourcing \"%s\""),
Bram Moolenaar555b2802005-05-19 21:08:39 +00002896 sourcing_lnum, fname);
Bram Moolenaar54ee7752005-05-31 22:22:17 +00002897 verbose_leave();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002899 if (is_vimrc == DOSO_VIMRC)
2900 vimrc_found(fname_exp, (char_u *)"MYVIMRC");
2901 else if (is_vimrc == DOSO_GVIMRC)
2902 vimrc_found(fname_exp, (char_u *)"MYGVIMRC");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903
2904#ifdef USE_CRNL
2905 /* If no automatic file format: Set default to CR-NL. */
2906 if (*p_ffs == NUL)
2907 cookie.fileformat = EOL_DOS;
2908 else
2909 cookie.fileformat = EOL_UNKNOWN;
2910 cookie.error = FALSE;
2911#endif
2912
2913#ifdef USE_CR
2914 /* If no automatic file format: Set default to CR. */
2915 if (*p_ffs == NUL)
2916 cookie.fileformat = EOL_MAC;
2917 else
2918 cookie.fileformat = EOL_UNKNOWN;
2919 cookie.error = FALSE;
2920#endif
2921
2922 cookie.nextline = NULL;
2923 cookie.finished = FALSE;
2924
2925#ifdef FEAT_EVAL
2926 /*
2927 * Check if this script has a breakpoint.
2928 */
2929 cookie.breakpoint = dbg_find_breakpoint(TRUE, fname_exp, (linenr_T)0);
2930 cookie.fname = fname_exp;
2931 cookie.dbg_tick = debug_tick;
2932
2933 cookie.level = ex_nesting_level;
2934#endif
2935#ifdef FEAT_MBYTE
2936 cookie.conv.vc_type = CONV_NONE; /* no conversion */
2937
2938 /* Try reading the first few bytes to check for a UTF-8 BOM. */
2939 {
2940 char_u buf[3];
2941
2942 if (fread((char *)buf, sizeof(char_u), (size_t)3, cookie.fp)
2943 == (size_t)3
2944 && buf[0] == 0xef && buf[1] == 0xbb && buf[2] == 0xbf)
2945 /* Found BOM, setup conversion and skip over it. */
2946 convert_setup(&cookie.conv, (char_u *)"utf-8", p_enc);
2947 else
2948 /* No BOM found, rewind. */
2949 fseek(cookie.fp, 0L, SEEK_SET);
2950 }
2951#endif
2952
2953 /*
2954 * Keep the sourcing name/lnum, for recursive calls.
2955 */
2956 save_sourcing_name = sourcing_name;
2957 sourcing_name = fname_exp;
2958 save_sourcing_lnum = sourcing_lnum;
2959 sourcing_lnum = 0;
2960
2961#ifdef STARTUPTIME
2962 time_push(&tv_rel, &tv_start);
2963#endif
2964
2965#ifdef FEAT_EVAL
Bram Moolenaar05159a02005-02-26 23:04:13 +00002966# ifdef FEAT_PROFILE
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00002967 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002968 prof_child_enter(&wait_start); /* entering a child now */
2969# endif
2970
2971 /* Don't use local function variables, if called from a function.
2972 * Also starts profiling timer for nested script. */
2973 save_funccalp = save_funccal();
2974
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 /*
2976 * Check if this script was sourced before to finds its SID.
2977 * If it's new, generate a new SID.
2978 */
2979 save_current_SID = current_SID;
2980# ifdef UNIX
2981 stat_ok = (mch_stat((char *)fname_exp, &st) >= 0);
2982# endif
Bram Moolenaar05159a02005-02-26 23:04:13 +00002983 for (current_SID = script_items.ga_len; current_SID > 0; --current_SID)
2984 {
2985 si = &SCRIPT_ITEM(current_SID);
2986 if (si->sn_name != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 && (
2988# ifdef UNIX
Bram Moolenaar7c626922005-02-07 22:01:03 +00002989 /* Compare dev/ino when possible, it catches symbolic
2990 * links. Also compare file names, the inode may change
2991 * when the file was edited. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00002992 ((stat_ok && si->sn_dev != -1)
2993 && (si->sn_dev == st.st_dev
2994 && si->sn_ino == st.st_ino)) ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995# endif
Bram Moolenaar05159a02005-02-26 23:04:13 +00002996 fnamecmp(si->sn_name, fname_exp) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002998 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999 if (current_SID == 0)
3000 {
3001 current_SID = ++last_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003002 if (ga_grow(&script_items, (int)(current_SID - script_items.ga_len))
3003 == FAIL)
3004 goto almosttheend;
3005 while (script_items.ga_len < current_SID)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003007 ++script_items.ga_len;
3008 SCRIPT_ITEM(script_items.ga_len).sn_name = NULL;
3009# ifdef FEAT_PROFILE
3010 SCRIPT_ITEM(script_items.ga_len).sn_prof_on = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003013 si = &SCRIPT_ITEM(current_SID);
3014 si->sn_name = fname_exp;
3015 fname_exp = NULL;
3016# ifdef UNIX
3017 if (stat_ok)
3018 {
3019 si->sn_dev = st.st_dev;
3020 si->sn_ino = st.st_ino;
3021 }
3022 else
3023 si->sn_dev = -1;
3024# endif
3025
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 /* Allocate the local script variables to use for this script. */
3027 new_script_vars(current_SID);
3028 }
3029
Bram Moolenaar05159a02005-02-26 23:04:13 +00003030# ifdef FEAT_PROFILE
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00003031 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003032 {
3033 int forceit;
3034
3035 /* Check if we do profiling for this script. */
3036 if (!si->sn_prof_on && has_profiling(TRUE, si->sn_name, &forceit))
3037 {
3038 script_do_profile(si);
3039 si->sn_pr_force = forceit;
3040 }
3041 if (si->sn_prof_on)
3042 {
3043 ++si->sn_pr_count;
3044 profile_start(&si->sn_pr_start);
3045 profile_zero(&si->sn_pr_children);
3046 }
3047 }
3048# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049#endif
3050
3051 /*
3052 * Call do_cmdline, which will call getsourceline() to get the lines.
3053 */
3054 do_cmdline(NULL, getsourceline, (void *)&cookie,
3055 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_REPEAT);
3056
3057 retval = OK;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003058
3059#ifdef FEAT_PROFILE
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00003060 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003061 {
3062 /* Get "si" again, "script_items" may have been reallocated. */
3063 si = &SCRIPT_ITEM(current_SID);
3064 if (si->sn_prof_on)
3065 {
3066 profile_end(&si->sn_pr_start);
3067 profile_sub_wait(&wait_start, &si->sn_pr_start);
3068 profile_add(&si->sn_pr_total, &si->sn_pr_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +00003069 profile_self(&si->sn_pr_self, &si->sn_pr_start,
3070 &si->sn_pr_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003071 }
3072 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073#endif
3074
3075 if (got_int)
3076 EMSG(_(e_interr));
3077 sourcing_name = save_sourcing_name;
3078 sourcing_lnum = save_sourcing_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 if (p_verbose > 1)
3080 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +00003081 verbose_enter();
Bram Moolenaar555b2802005-05-19 21:08:39 +00003082 smsg((char_u *)_("finished sourcing %s"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 if (sourcing_name != NULL)
Bram Moolenaar555b2802005-05-19 21:08:39 +00003084 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar54ee7752005-05-31 22:22:17 +00003085 verbose_leave();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 }
3087#ifdef STARTUPTIME
Bram Moolenaar555b2802005-05-19 21:08:39 +00003088 vim_snprintf(IObuff, IOSIZE, "sourcing %s", fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 time_msg(IObuff, &tv_start);
3090 time_pop(&tv_rel);
3091#endif
3092
3093#ifdef FEAT_EVAL
3094 /*
3095 * After a "finish" in debug mode, need to break at first command of next
3096 * sourced file.
3097 */
3098 if (save_debug_break_level > ex_nesting_level
3099 && debug_break_level == ex_nesting_level)
3100 ++debug_break_level;
3101#endif
3102
Bram Moolenaar05159a02005-02-26 23:04:13 +00003103#ifdef FEAT_EVAL
3104almosttheend:
3105 current_SID = save_current_SID;
3106 restore_funccal(save_funccalp);
3107# ifdef FEAT_PROFILE
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00003108 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003109 prof_child_exit(&wait_start); /* leaving a child now */
3110# endif
3111#endif
3112 fclose(cookie.fp);
3113 vim_free(cookie.nextline);
3114#ifdef FEAT_MBYTE
3115 convert_setup(&cookie.conv, NULL, NULL);
3116#endif
3117
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118theend:
3119 vim_free(fname_exp);
3120 return retval;
3121}
3122
3123#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00003124
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125/*
3126 * ":scriptnames"
3127 */
3128/*ARGSUSED*/
3129 void
3130ex_scriptnames(eap)
3131 exarg_T *eap;
3132{
3133 int i;
3134
Bram Moolenaar05159a02005-02-26 23:04:13 +00003135 for (i = 1; i <= script_items.ga_len && !got_int; ++i)
3136 if (SCRIPT_ITEM(i).sn_name != NULL)
3137 smsg((char_u *)"%3d: %s", i, SCRIPT_ITEM(i).sn_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138}
3139
3140# if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
3141/*
3142 * Fix slashes in the list of script names for 'shellslash'.
3143 */
3144 void
3145scriptnames_slash_adjust()
3146{
3147 int i;
3148
Bram Moolenaar05159a02005-02-26 23:04:13 +00003149 for (i = 1; i <= script_items.ga_len; ++i)
3150 if (SCRIPT_ITEM(i).sn_name != NULL)
3151 slash_adjust(SCRIPT_ITEM(i).sn_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152}
3153# endif
3154
3155/*
3156 * Get a pointer to a script name. Used for ":verbose set".
3157 */
3158 char_u *
3159get_scriptname(id)
3160 scid_T id;
3161{
3162 if (id == SID_MODELINE)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003163 return (char_u *)_("modeline");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 if (id == SID_CMDARG)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003165 return (char_u *)_("--cmd argument");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 if (id == SID_CARG)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003167 return (char_u *)_("-c argument");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 if (id == SID_ENV)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003169 return (char_u *)_("environment variable");
3170 if (id == SID_ERROR)
3171 return (char_u *)_("error handler");
Bram Moolenaar05159a02005-02-26 23:04:13 +00003172 return SCRIPT_ITEM(id).sn_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173}
Bram Moolenaar05159a02005-02-26 23:04:13 +00003174
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00003175# if defined(EXITFREE) || defined(PROTO)
3176 void
3177free_scriptnames()
3178{
3179 int i;
3180
3181 for (i = script_items.ga_len; i > 0; --i)
3182 vim_free(SCRIPT_ITEM(i).sn_name);
3183 ga_clear(&script_items);
3184}
3185# endif
3186
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187#endif
3188
3189#if defined(USE_CR) || defined(PROTO)
3190
3191# if defined(__MSL__) && (__MSL__ >= 22)
3192/*
3193 * Newer version of the Metrowerks library handle DOS and UNIX files
3194 * without help.
3195 * Test with earlier versions, MSL 2.2 is the library supplied with
3196 * Codewarrior Pro 2.
3197 */
3198 char *
3199fgets_cr(s, n, stream)
3200 char *s;
3201 int n;
3202 FILE *stream;
3203{
3204 return fgets(s, n, stream);
3205}
3206# else
3207/*
3208 * Version of fgets() which also works for lines ending in a <CR> only
3209 * (Macintosh format).
3210 * For older versions of the Metrowerks library.
3211 * At least CodeWarrior 9 needed this code.
3212 */
3213 char *
3214fgets_cr(s, n, stream)
3215 char *s;
3216 int n;
3217 FILE *stream;
3218{
3219 int c = 0;
3220 int char_read = 0;
3221
3222 while (!feof(stream) && c != '\r' && c != '\n' && char_read < n - 1)
3223 {
3224 c = fgetc(stream);
3225 s[char_read++] = c;
3226 /* If the file is in DOS format, we need to skip a NL after a CR. I
3227 * thought it was the other way around, but this appears to work... */
3228 if (c == '\n')
3229 {
3230 c = fgetc(stream);
3231 if (c != '\r')
3232 ungetc(c, stream);
3233 }
3234 }
3235
3236 s[char_read] = 0;
3237 if (char_read == 0)
3238 return NULL;
3239
3240 if (feof(stream) && char_read == 1)
3241 return NULL;
3242
3243 return s;
3244}
3245# endif
3246#endif
3247
3248/*
3249 * Get one full line from a sourced file.
3250 * Called by do_cmdline() when it's called from do_source().
3251 *
3252 * Return a pointer to the line in allocated memory.
3253 * Return NULL for end-of-file or some error.
3254 */
3255/* ARGSUSED */
3256 char_u *
3257getsourceline(c, cookie, indent)
3258 int c; /* not used */
3259 void *cookie;
3260 int indent; /* not used */
3261{
3262 struct source_cookie *sp = (struct source_cookie *)cookie;
3263 char_u *line;
3264 char_u *p, *s;
3265
3266#ifdef FEAT_EVAL
3267 /* If breakpoints have been added/deleted need to check for it. */
3268 if (sp->dbg_tick < debug_tick)
3269 {
3270 sp->breakpoint = dbg_find_breakpoint(TRUE, sp->fname, sourcing_lnum);
3271 sp->dbg_tick = debug_tick;
3272 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003273# ifdef FEAT_PROFILE
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00003274 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +00003275 script_line_end();
3276# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277#endif
3278 /*
3279 * Get current line. If there is a read-ahead line, use it, otherwise get
3280 * one now.
3281 */
3282 if (sp->finished)
3283 line = NULL;
3284 else if (sp->nextline == NULL)
3285 line = get_one_sourceline(sp);
3286 else
3287 {
3288 line = sp->nextline;
3289 sp->nextline = NULL;
3290 ++sourcing_lnum;
3291 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003292#ifdef FEAT_PROFILE
Bram Moolenaar9b2200a2006-03-20 21:55:45 +00003293 if (line != NULL && do_profiling == PROF_YES)
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003294 script_line_start();
3295#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296
3297 /* Only concatenate lines starting with a \ when 'cpoptions' doesn't
3298 * contain the 'C' flag. */
3299 if (line != NULL && (vim_strchr(p_cpo, CPO_CONCAT) == NULL))
3300 {
3301 /* compensate for the one line read-ahead */
3302 --sourcing_lnum;
3303 for (;;)
3304 {
3305 sp->nextline = get_one_sourceline(sp);
3306 if (sp->nextline == NULL)
3307 break;
3308 p = skipwhite(sp->nextline);
3309 if (*p != '\\')
3310 break;
3311 s = alloc((int)(STRLEN(line) + STRLEN(p)));
3312 if (s == NULL) /* out of memory */
3313 break;
3314 STRCPY(s, line);
3315 STRCAT(s, p + 1);
3316 vim_free(line);
3317 line = s;
3318 vim_free(sp->nextline);
3319 }
3320 }
3321
3322#ifdef FEAT_MBYTE
3323 if (line != NULL && sp->conv.vc_type != CONV_NONE)
3324 {
3325 /* Convert the encoding of the script line. */
3326 s = string_convert(&sp->conv, line, NULL);
3327 if (s != NULL)
3328 {
3329 vim_free(line);
3330 line = s;
3331 }
3332 }
3333#endif
3334
3335#ifdef FEAT_EVAL
3336 /* Did we encounter a breakpoint? */
3337 if (sp->breakpoint != 0 && sp->breakpoint <= sourcing_lnum)
3338 {
3339 dbg_breakpoint(sp->fname, sourcing_lnum);
3340 /* Find next breakpoint. */
3341 sp->breakpoint = dbg_find_breakpoint(TRUE, sp->fname, sourcing_lnum);
3342 sp->dbg_tick = debug_tick;
3343 }
3344#endif
3345
3346 return line;
3347}
3348
3349 static char_u *
3350get_one_sourceline(sp)
3351 struct source_cookie *sp;
3352{
3353 garray_T ga;
3354 int len;
3355 int c;
3356 char_u *buf;
3357#ifdef USE_CRNL
3358 int has_cr; /* CR-LF found */
3359#endif
3360#ifdef USE_CR
3361 char_u *scan;
3362#endif
3363 int have_read = FALSE;
3364
3365 /* use a growarray to store the sourced line */
Bram Moolenaar6ac54292005-02-02 23:07:25 +00003366 ga_init2(&ga, 1, 250);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367
3368 /*
3369 * Loop until there is a finished line (or end-of-file).
3370 */
3371 sourcing_lnum++;
3372 for (;;)
3373 {
Bram Moolenaar6ac54292005-02-02 23:07:25 +00003374 /* make room to read at least 120 (more) characters */
3375 if (ga_grow(&ga, 120) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 break;
3377 buf = (char_u *)ga.ga_data;
3378
3379#ifdef USE_CR
3380 if (sp->fileformat == EOL_MAC)
3381 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00003382 if (fgets_cr((char *)buf + ga.ga_len, ga.ga_maxlen - ga.ga_len,
3383 sp->fp) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 break;
3385 }
3386 else
3387#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00003388 if (fgets((char *)buf + ga.ga_len, ga.ga_maxlen - ga.ga_len,
3389 sp->fp) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 break;
Bram Moolenaar6ac54292005-02-02 23:07:25 +00003391 len = ga.ga_len + (int)STRLEN(buf + ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392#ifdef USE_CRNL
3393 /* Ignore a trailing CTRL-Z, when in Dos mode. Only recognize the
3394 * CTRL-Z by its own, or after a NL. */
3395 if ( (len == 1 || (len >= 2 && buf[len - 2] == '\n'))
3396 && sp->fileformat == EOL_DOS
3397 && buf[len - 1] == Ctrl_Z)
3398 {
3399 buf[len - 1] = NUL;
3400 break;
3401 }
3402#endif
3403
3404#ifdef USE_CR
3405 /* If the read doesn't stop on a new line, and there's
3406 * some CR then we assume a Mac format */
3407 if (sp->fileformat == EOL_UNKNOWN)
3408 {
3409 if (buf[len - 1] != '\n' && vim_strchr(buf, '\r') != NULL)
3410 sp->fileformat = EOL_MAC;
3411 else
3412 sp->fileformat = EOL_UNIX;
3413 }
3414
3415 if (sp->fileformat == EOL_MAC)
3416 {
3417 scan = vim_strchr(buf, '\r');
3418
3419 if (scan != NULL)
3420 {
3421 *scan = '\n';
3422 if (*(scan + 1) != 0)
3423 {
3424 *(scan + 1) = 0;
3425 fseek(sp->fp, (long)(scan - buf - len + 1), SEEK_CUR);
3426 }
3427 }
3428 len = STRLEN(buf);
3429 }
3430#endif
3431
3432 have_read = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 ga.ga_len = len;
3434
3435 /* If the line was longer than the buffer, read more. */
Bram Moolenaar86b68352004-12-27 21:59:20 +00003436 if (ga.ga_maxlen - ga.ga_len == 1 && buf[len - 1] != '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 continue;
3438
3439 if (len >= 1 && buf[len - 1] == '\n') /* remove trailing NL */
3440 {
3441#ifdef USE_CRNL
3442 has_cr = (len >= 2 && buf[len - 2] == '\r');
3443 if (sp->fileformat == EOL_UNKNOWN)
3444 {
3445 if (has_cr)
3446 sp->fileformat = EOL_DOS;
3447 else
3448 sp->fileformat = EOL_UNIX;
3449 }
3450
3451 if (sp->fileformat == EOL_DOS)
3452 {
3453 if (has_cr) /* replace trailing CR */
3454 {
3455 buf[len - 2] = '\n';
3456 --len;
3457 --ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 }
3459 else /* lines like ":map xx yy^M" will have failed */
3460 {
3461 if (!sp->error)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003462 {
3463 msg_source(hl_attr(HLF_W));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 EMSG(_("W15: Warning: Wrong line separator, ^M may be missing"));
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003465 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 sp->error = TRUE;
3467 sp->fileformat = EOL_UNIX;
3468 }
3469 }
3470#endif
3471 /* The '\n' is escaped if there is an odd number of ^V's just
3472 * before it, first set "c" just before the 'V's and then check
3473 * len&c parities (is faster than ((len-c)%2 == 0)) -- Acevedo */
3474 for (c = len - 2; c >= 0 && buf[c] == Ctrl_V; c--)
3475 ;
3476 if ((len & 1) != (c & 1)) /* escaped NL, read more */
3477 {
3478 sourcing_lnum++;
3479 continue;
3480 }
3481
3482 buf[len - 1] = NUL; /* remove the NL */
3483 }
3484
3485 /*
3486 * Check for ^C here now and then, so recursive :so can be broken.
3487 */
3488 line_breakcheck();
3489 break;
3490 }
3491
3492 if (have_read)
3493 return (char_u *)ga.ga_data;
3494
3495 vim_free(ga.ga_data);
3496 return NULL;
3497}
3498
Bram Moolenaar05159a02005-02-26 23:04:13 +00003499#if defined(FEAT_PROFILE) || defined(PROTO)
3500/*
3501 * Called when starting to read a script line.
3502 * "sourcing_lnum" must be correct!
3503 * When skipping lines it may not actually be executed, but we won't find out
3504 * until later and we need to store the time now.
3505 */
3506 void
3507script_line_start()
3508{
3509 scriptitem_T *si;
3510 sn_prl_T *pp;
3511
3512 if (current_SID <= 0 || current_SID > script_items.ga_len)
3513 return;
3514 si = &SCRIPT_ITEM(current_SID);
3515 if (si->sn_prof_on && sourcing_lnum >= 1)
3516 {
3517 /* Grow the array before starting the timer, so that the time spend
3518 * here isn't counted. */
3519 ga_grow(&si->sn_prl_ga, (int)(sourcing_lnum - si->sn_prl_ga.ga_len));
3520 si->sn_prl_idx = sourcing_lnum - 1;
3521 while (si->sn_prl_ga.ga_len <= si->sn_prl_idx
3522 && si->sn_prl_ga.ga_len < si->sn_prl_ga.ga_maxlen)
3523 {
3524 /* Zero counters for a line that was not used before. */
3525 pp = &PRL_ITEM(si, si->sn_prl_ga.ga_len);
3526 pp->snp_count = 0;
3527 profile_zero(&pp->sn_prl_total);
3528 profile_zero(&pp->sn_prl_self);
3529 ++si->sn_prl_ga.ga_len;
3530 }
3531 si->sn_prl_execed = FALSE;
3532 profile_start(&si->sn_prl_start);
3533 profile_zero(&si->sn_prl_children);
3534 profile_get_wait(&si->sn_prl_wait);
3535 }
3536}
3537
3538/*
3539 * Called when actually executing a function line.
3540 */
3541 void
3542script_line_exec()
3543{
3544 scriptitem_T *si;
3545
3546 if (current_SID <= 0 || current_SID > script_items.ga_len)
3547 return;
3548 si = &SCRIPT_ITEM(current_SID);
3549 if (si->sn_prof_on && si->sn_prl_idx >= 0)
3550 si->sn_prl_execed = TRUE;
3551}
3552
3553/*
3554 * Called when done with a function line.
3555 */
3556 void
3557script_line_end()
3558{
3559 scriptitem_T *si;
3560 sn_prl_T *pp;
3561
3562 if (current_SID <= 0 || current_SID > script_items.ga_len)
3563 return;
3564 si = &SCRIPT_ITEM(current_SID);
3565 if (si->sn_prof_on && si->sn_prl_idx >= 0
3566 && si->sn_prl_idx < si->sn_prl_ga.ga_len)
3567 {
3568 if (si->sn_prl_execed)
3569 {
3570 pp = &PRL_ITEM(si, si->sn_prl_idx);
3571 ++pp->snp_count;
3572 profile_end(&si->sn_prl_start);
3573 profile_sub_wait(&si->sn_prl_wait, &si->sn_prl_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003574 profile_add(&pp->sn_prl_total, &si->sn_prl_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +00003575 profile_self(&pp->sn_prl_self, &si->sn_prl_start,
3576 &si->sn_prl_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003577 }
3578 si->sn_prl_idx = -1;
3579 }
3580}
3581#endif
3582
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583/*
3584 * ":scriptencoding": Set encoding conversion for a sourced script.
3585 * Without the multi-byte feature it's simply ignored.
3586 */
3587/*ARGSUSED*/
3588 void
3589ex_scriptencoding(eap)
3590 exarg_T *eap;
3591{
3592#ifdef FEAT_MBYTE
3593 struct source_cookie *sp;
3594 char_u *name;
3595
3596 if (!getline_equal(eap->getline, eap->cookie, getsourceline))
3597 {
3598 EMSG(_("E167: :scriptencoding used outside of a sourced file"));
3599 return;
3600 }
3601
3602 if (*eap->arg != NUL)
3603 {
3604 name = enc_canonize(eap->arg);
3605 if (name == NULL) /* out of memory */
3606 return;
3607 }
3608 else
3609 name = eap->arg;
3610
3611 /* Setup for conversion from the specified encoding to 'encoding'. */
3612 sp = (struct source_cookie *)getline_cookie(eap->getline, eap->cookie);
3613 convert_setup(&sp->conv, name, p_enc);
3614
3615 if (name != eap->arg)
3616 vim_free(name);
3617#endif
3618}
3619
3620#if defined(FEAT_EVAL) || defined(PROTO)
3621/*
3622 * ":finish": Mark a sourced file as finished.
3623 */
3624 void
3625ex_finish(eap)
3626 exarg_T *eap;
3627{
3628 if (getline_equal(eap->getline, eap->cookie, getsourceline))
3629 do_finish(eap, FALSE);
3630 else
3631 EMSG(_("E168: :finish used outside of a sourced file"));
3632}
3633
3634/*
3635 * Mark a sourced file as finished. Possibly makes the ":finish" pending.
3636 * Also called for a pending finish at the ":endtry" or after returning from
3637 * an extra do_cmdline(). "reanimate" is used in the latter case.
3638 */
3639 void
3640do_finish(eap, reanimate)
3641 exarg_T *eap;
3642 int reanimate;
3643{
3644 int idx;
3645
3646 if (reanimate)
3647 ((struct source_cookie *)getline_cookie(eap->getline,
3648 eap->cookie))->finished = FALSE;
3649
3650 /*
3651 * Cleanup (and inactivate) conditionals, but stop when a try conditional
3652 * not in its finally clause (which then is to be executed next) is found.
3653 * In this case, make the ":finish" pending for execution at the ":endtry".
3654 * Otherwise, finish normally.
3655 */
3656 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
3657 if (idx >= 0)
3658 {
3659 eap->cstack->cs_pending[idx] = CSTP_FINISH;
3660 report_make_pending(CSTP_FINISH, NULL);
3661 }
3662 else
3663 ((struct source_cookie *)getline_cookie(eap->getline,
3664 eap->cookie))->finished = TRUE;
3665}
3666
3667
3668/*
3669 * Return TRUE when a sourced file had the ":finish" command: Don't give error
3670 * message for missing ":endif".
3671 * Return FALSE when not sourcing a file.
3672 */
3673 int
Bram Moolenaar89d40322006-08-29 15:30:07 +00003674source_finished(fgetline, cookie)
3675 char_u *(*fgetline) __ARGS((int, void *, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 void *cookie;
3677{
Bram Moolenaar89d40322006-08-29 15:30:07 +00003678 return (getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 && ((struct source_cookie *)getline_cookie(
Bram Moolenaar89d40322006-08-29 15:30:07 +00003680 fgetline, cookie))->finished);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681}
3682#endif
3683
3684#if defined(FEAT_LISTCMDS) || defined(PROTO)
3685/*
3686 * ":checktime [buffer]"
3687 */
3688 void
3689ex_checktime(eap)
3690 exarg_T *eap;
3691{
3692 buf_T *buf;
3693 int save_no_check_timestamps = no_check_timestamps;
3694
3695 no_check_timestamps = 0;
3696 if (eap->addr_count == 0) /* default is all buffers */
3697 check_timestamps(FALSE);
3698 else
3699 {
3700 buf = buflist_findnr((int)eap->line2);
3701 if (buf != NULL) /* cannot happen? */
3702 (void)buf_check_timestamp(buf, FALSE);
3703 }
3704 no_check_timestamps = save_no_check_timestamps;
3705}
3706#endif
3707
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
3709 && (defined(FEAT_EVAL) || defined(FEAT_MULTI_LANG))
3710static char *get_locale_val __ARGS((int what));
3711
3712 static char *
3713get_locale_val(what)
3714 int what;
3715{
3716 char *loc;
3717
3718 /* Obtain the locale value from the libraries. For DJGPP this is
3719 * redefined and it doesn't use the arguments. */
3720 loc = setlocale(what, NULL);
3721
Bram Moolenaar61660ea2006-04-07 21:40:07 +00003722# ifdef WIN32
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 if (loc != NULL)
3724 {
3725 char_u *p;
3726
Bram Moolenaar61660ea2006-04-07 21:40:07 +00003727 /* setocale() returns something like "LC_COLLATE=<name>;LC_..." when
3728 * one of the values (e.g., LC_CTYPE) differs. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 p = vim_strchr(loc, '=');
3730 if (p != NULL)
3731 {
3732 loc = ++p;
3733 while (*p != NUL) /* remove trailing newline */
3734 {
Bram Moolenaar61660ea2006-04-07 21:40:07 +00003735 if (*p < ' ' || *p == ';')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 {
3737 *p = NUL;
3738 break;
3739 }
3740 ++p;
3741 }
3742 }
3743 }
3744# endif
3745
3746 return loc;
3747}
3748#endif
3749
3750
3751#ifdef WIN32
3752/*
3753 * On MS-Windows locale names are strings like "German_Germany.1252", but
3754 * gettext expects "de". Try to translate one into another here for a few
3755 * supported languages.
3756 */
3757 static char_u *
3758gettext_lang(char_u *name)
3759{
3760 int i;
3761 static char *(mtable[]) = {
3762 "afrikaans", "af",
3763 "czech", "cs",
3764 "dutch", "nl",
3765 "german", "de",
3766 "english_united kingdom", "en_GB",
3767 "spanish", "es",
3768 "french", "fr",
3769 "italian", "it",
3770 "japanese", "ja",
3771 "korean", "ko",
3772 "norwegian", "no",
3773 "polish", "pl",
3774 "russian", "ru",
3775 "slovak", "sk",
3776 "swedish", "sv",
3777 "ukrainian", "uk",
3778 "chinese_china", "zh_CN",
3779 "chinese_taiwan", "zh_TW",
3780 NULL};
3781
3782 for (i = 0; mtable[i] != NULL; i += 2)
3783 if (STRNICMP(mtable[i], name, STRLEN(mtable[i])) == 0)
3784 return mtable[i + 1];
3785 return name;
3786}
3787#endif
3788
3789#if defined(FEAT_MULTI_LANG) || defined(PROTO)
3790/*
3791 * Obtain the current messages language. Used to set the default for
3792 * 'helplang'. May return NULL or an empty string.
3793 */
3794 char_u *
3795get_mess_lang()
3796{
3797 char_u *p;
3798
3799# if (defined(HAVE_LOCALE_H) || defined(X_LOCALE))
3800# if defined(LC_MESSAGES)
3801 p = (char_u *)get_locale_val(LC_MESSAGES);
3802# else
3803 /* This is necessary for Win32, where LC_MESSAGES is not defined and $LANG
Bram Moolenaar61660ea2006-04-07 21:40:07 +00003804 * may be set to the LCID number. LC_COLLATE is the best guess, LC_TIME
3805 * and LC_MONETARY may be set differently for a Japanese working in the
3806 * US. */
3807 p = (char_u *)get_locale_val(LC_COLLATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808# endif
3809# else
3810 p = mch_getenv((char_u *)"LC_ALL");
3811 if (p == NULL || *p == NUL)
3812 {
3813 p = mch_getenv((char_u *)"LC_MESSAGES");
3814 if (p == NULL || *p == NUL)
3815 p = mch_getenv((char_u *)"LANG");
3816 }
3817# endif
3818# ifdef WIN32
3819 p = gettext_lang(p);
3820# endif
3821 return p;
3822}
3823#endif
3824
Bram Moolenaardef9e822004-12-31 20:58:58 +00003825/* Complicated #if; matches with where get_mess_env() is used below. */
3826#if (defined(FEAT_EVAL) && !((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
3827 && defined(LC_MESSAGES))) \
3828 || ((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
3829 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)) \
3830 && !defined(LC_MESSAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831static char_u *get_mess_env __ARGS((void));
3832
3833/*
3834 * Get the language used for messages from the environment.
3835 */
3836 static char_u *
3837get_mess_env()
3838{
3839 char_u *p;
3840
3841 p = mch_getenv((char_u *)"LC_ALL");
3842 if (p == NULL || *p == NUL)
3843 {
3844 p = mch_getenv((char_u *)"LC_MESSAGES");
3845 if (p == NULL || *p == NUL)
3846 {
3847 p = mch_getenv((char_u *)"LANG");
3848 if (p != NULL && VIM_ISDIGIT(*p))
3849 p = NULL; /* ignore something like "1043" */
3850# if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
3851 if (p == NULL || *p == NUL)
3852 p = (char_u *)get_locale_val(LC_CTYPE);
3853# endif
3854 }
3855 }
3856 return p;
3857}
3858#endif
3859
3860#if defined(FEAT_EVAL) || defined(PROTO)
3861
3862/*
3863 * Set the "v:lang" variable according to the current locale setting.
3864 * Also do "v:lc_time"and "v:ctype".
3865 */
3866 void
3867set_lang_var()
3868{
3869 char_u *loc;
3870
3871# if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
3872 loc = (char_u *)get_locale_val(LC_CTYPE);
3873# else
3874 /* setlocale() not supported: use the default value */
3875 loc = (char_u *)"C";
3876# endif
3877 set_vim_var_string(VV_CTYPE, loc, -1);
3878
3879 /* When LC_MESSAGES isn't defined use the value from $LC_MESSAGES, fall
3880 * back to LC_CTYPE if it's empty. */
3881# if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) && defined(LC_MESSAGES)
3882 loc = (char_u *)get_locale_val(LC_MESSAGES);
3883# else
3884 loc = get_mess_env();
3885# endif
3886 set_vim_var_string(VV_LANG, loc, -1);
3887
3888# if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
3889 loc = (char_u *)get_locale_val(LC_TIME);
3890# endif
3891 set_vim_var_string(VV_LC_TIME, loc, -1);
3892}
3893#endif
3894
3895#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
3896 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
3897/*
3898 * ":language": Set the language (locale).
3899 */
3900 void
3901ex_language(eap)
3902 exarg_T *eap;
3903{
3904 char *loc;
3905 char_u *p;
3906 char_u *name;
3907 int what = LC_ALL;
3908 char *whatstr = "";
3909#ifdef LC_MESSAGES
3910# define VIM_LC_MESSAGES LC_MESSAGES
3911#else
3912# define VIM_LC_MESSAGES 6789
3913#endif
3914
3915 name = eap->arg;
3916
3917 /* Check for "messages {name}", "ctype {name}" or "time {name}" argument.
3918 * Allow abbreviation, but require at least 3 characters to avoid
3919 * confusion with a two letter language name "me" or "ct". */
3920 p = skiptowhite(eap->arg);
3921 if ((*p == NUL || vim_iswhite(*p)) && p - eap->arg >= 3)
3922 {
3923 if (STRNICMP(eap->arg, "messages", p - eap->arg) == 0)
3924 {
3925 what = VIM_LC_MESSAGES;
3926 name = skipwhite(p);
3927 whatstr = "messages ";
3928 }
3929 else if (STRNICMP(eap->arg, "ctype", p - eap->arg) == 0)
3930 {
3931 what = LC_CTYPE;
3932 name = skipwhite(p);
3933 whatstr = "ctype ";
3934 }
3935 else if (STRNICMP(eap->arg, "time", p - eap->arg) == 0)
3936 {
3937 what = LC_TIME;
3938 name = skipwhite(p);
3939 whatstr = "time ";
3940 }
3941 }
3942
3943 if (*name == NUL)
3944 {
3945#ifndef LC_MESSAGES
3946 if (what == VIM_LC_MESSAGES)
3947 p = get_mess_env();
3948 else
3949#endif
3950 p = (char_u *)setlocale(what, NULL);
3951 if (p == NULL || *p == NUL)
3952 p = (char_u *)"Unknown";
3953 smsg((char_u *)_("Current %slanguage: \"%s\""), whatstr, p);
3954 }
3955 else
3956 {
3957#ifndef LC_MESSAGES
3958 if (what == VIM_LC_MESSAGES)
3959 loc = "";
3960 else
3961#endif
3962 loc = setlocale(what, (char *)name);
3963 if (loc == NULL)
3964 EMSG2(_("E197: Cannot set language to \"%s\""), name);
3965 else
3966 {
3967#ifdef HAVE_NL_MSG_CAT_CNTR
3968 /* Need to do this for GNU gettext, otherwise cached translations
3969 * will be used again. */
3970 extern int _nl_msg_cat_cntr;
3971
3972 ++_nl_msg_cat_cntr;
3973#endif
3974 /* Reset $LC_ALL, otherwise it would overrule everyting. */
3975 vim_setenv((char_u *)"LC_ALL", (char_u *)"");
3976
3977 if (what != LC_TIME)
3978 {
3979 /* Tell gettext() what to translate to. It apparently doesn't
3980 * use the currently effective locale. Also do this when
3981 * FEAT_GETTEXT isn't defined, so that shell commands use this
3982 * value. */
3983 if (what == LC_ALL)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003984 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 vim_setenv((char_u *)"LANG", name);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003986# ifdef WIN32
3987 /* Apparently MS-Windows printf() may cause a crash when
3988 * we give it 8-bit text while it's expecting text in the
3989 * current locale. This call avoids that. */
3990 setlocale(LC_CTYPE, "C");
3991# endif
3992 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 if (what != LC_CTYPE)
3994 {
3995 char_u *mname;
3996#ifdef WIN32
3997 mname = gettext_lang(name);
3998#else
3999 mname = name;
4000#endif
4001 vim_setenv((char_u *)"LC_MESSAGES", mname);
4002#ifdef FEAT_MULTI_LANG
4003 set_helplang_default(mname);
4004#endif
4005 }
4006
4007 /* Set $LC_CTYPE, because it overrules $LANG, and
4008 * gtk_set_locale() calls setlocale() again. gnome_init()
4009 * sets $LC_CTYPE to "en_US" (that's a bug!). */
4010 if (what != VIM_LC_MESSAGES)
4011 vim_setenv((char_u *)"LC_CTYPE", name);
4012# ifdef FEAT_GUI_GTK
4013 /* Let GTK know what locale we're using. Not sure this is
4014 * really needed... */
4015 if (gui.in_use)
4016 (void)gtk_set_locale();
4017# endif
4018 }
4019
4020# ifdef FEAT_EVAL
4021 /* Set v:lang, v:lc_time and v:ctype to the final result. */
4022 set_lang_var();
4023# endif
4024 }
4025 }
4026}
4027
4028# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4029/*
4030 * Function given to ExpandGeneric() to obtain the possible arguments of the
4031 * ":language" command.
4032 */
4033/*ARGSUSED*/
4034 char_u *
4035get_lang_arg(xp, idx)
4036 expand_T *xp;
4037 int idx;
4038{
4039 if (idx == 0)
4040 return (char_u *)"messages";
4041 if (idx == 1)
4042 return (char_u *)"ctype";
4043 if (idx == 2)
4044 return (char_u *)"time";
4045 return NULL;
4046}
4047# endif
4048
4049#endif