blob: ad49d45340be93a05c94a0a9ff791f018f6a233d [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
812# if defined(FEAT_PROFILE) || defined(PROTO)
813/*
814 * Functions for profiling.
815 */
816static void script_do_profile __ARGS((scriptitem_T *si));
817static void script_dump_profile __ARGS((FILE *fd));
818static proftime_T prof_wait_time;
819
820/*
821 * Set the time in "tm" to zero.
822 */
823 void
824profile_zero(tm)
825 proftime_T *tm;
826{
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +0000827# ifdef WIN3264
828 tm->QuadPart = 0;
829# else
Bram Moolenaar05159a02005-02-26 23:04:13 +0000830 tm->tv_usec = 0;
831 tm->tv_sec = 0;
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +0000832# endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000833}
834
835/*
836 * Store the current time in "tm".
837 */
838 void
839profile_start(tm)
840 proftime_T *tm;
841{
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +0000842# ifdef WIN3264
843 QueryPerformanceCounter(tm);
844# else
Bram Moolenaar05159a02005-02-26 23:04:13 +0000845 gettimeofday(tm, NULL);
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +0000846# endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000847}
848
849/*
850 * Compute the elapsed time from "tm" till now and store in "tm".
851 */
852 void
853profile_end(tm)
854 proftime_T *tm;
855{
856 proftime_T now;
857
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +0000858# ifdef WIN3264
859 QueryPerformanceCounter(&now);
860 tm->QuadPart = now.QuadPart - tm->QuadPart;
861# else
Bram Moolenaar05159a02005-02-26 23:04:13 +0000862 gettimeofday(&now, NULL);
863 tm->tv_usec = now.tv_usec - tm->tv_usec;
864 tm->tv_sec = now.tv_sec - tm->tv_sec;
865 if (tm->tv_usec < 0)
866 {
867 tm->tv_usec += 1000000;
868 --tm->tv_sec;
869 }
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +0000870# endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000871}
872
873/*
874 * Subtract the time "tm2" from "tm".
875 */
876 void
877profile_sub(tm, tm2)
878 proftime_T *tm, *tm2;
879{
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +0000880# ifdef WIN3264
881 tm->QuadPart -= tm2->QuadPart;
882# else
Bram Moolenaar05159a02005-02-26 23:04:13 +0000883 tm->tv_usec -= tm2->tv_usec;
884 tm->tv_sec -= tm2->tv_sec;
885 if (tm->tv_usec < 0)
886 {
887 tm->tv_usec += 1000000;
888 --tm->tv_sec;
889 }
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +0000890# endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000891}
892
893/*
894 * Add the time "tm2" to "tm".
895 */
896 void
897profile_add(tm, tm2)
898 proftime_T *tm, *tm2;
899{
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +0000900# ifdef WIN3264
901 tm->QuadPart += tm2->QuadPart;
902# else
Bram Moolenaar05159a02005-02-26 23:04:13 +0000903 tm->tv_usec += tm2->tv_usec;
904 tm->tv_sec += tm2->tv_sec;
905 if (tm->tv_usec >= 1000000)
906 {
907 tm->tv_usec -= 1000000;
908 ++tm->tv_sec;
909 }
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +0000910# endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000911}
912
913/*
Bram Moolenaar1056d982006-03-09 22:37:52 +0000914 * Add the "self" time from the total time and the children's time.
915 */
916 void
917profile_self(self, total, children)
918 proftime_T *self, *total, *children;
919{
920 /* Check that the result won't be negative. Can happen with recursive
921 * calls. */
922#ifdef WIN3264
923 if (total->QuadPart <= children->QuadPart)
924 return;
925#else
926 if (total->tv_sec < children->tv_sec
927 || (total->tv_sec == children->tv_sec
928 && total->tv_usec <= children->tv_usec))
929 return;
930#endif
931 profile_add(self, total);
932 profile_sub(self, children);
933}
934
935/*
Bram Moolenaar05159a02005-02-26 23:04:13 +0000936 * Get the current waittime.
937 */
938 void
939profile_get_wait(tm)
940 proftime_T *tm;
941{
942 *tm = prof_wait_time;
943}
944
945/*
946 * Subtract the passed waittime since "tm" from "tma".
947 */
948 void
949profile_sub_wait(tm, tma)
950 proftime_T *tm, *tma;
951{
952 proftime_T tm3 = prof_wait_time;
953
954 profile_sub(&tm3, tm);
955 profile_sub(tma, &tm3);
956}
957
958/*
959 * Return TRUE if "tm1" and "tm2" are equal.
960 */
961 int
962profile_equal(tm1, tm2)
963 proftime_T *tm1, *tm2;
964{
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +0000965# ifdef WIN3264
966 return (tm1->QuadPart == tm2->QuadPart);
967# else
Bram Moolenaar05159a02005-02-26 23:04:13 +0000968 return (tm1->tv_usec == tm2->tv_usec && tm1->tv_sec == tm2->tv_sec);
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +0000969# endif
970}
971
972/*
973 * Return <0, 0 or >0 if "tm1" < "tm2", "tm1" == "tm2" or "tm1" > "tm2"
974 */
975 int
976profile_cmp(tm1, tm2)
977 proftime_T *tm1, *tm2;
978{
979# ifdef WIN3264
980 return (int)(tm2->QuadPart - tm1->QuadPart);
981# else
982 if (tm1->tv_sec == tm2->tv_sec)
983 return tm2->tv_usec - tm1->tv_usec;
984 return tm2->tv_sec - tm1->tv_sec;
985# endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000986}
987
988/*
989 * Return a string that represents a time.
990 * Uses a static buffer!
991 */
992 char *
993profile_msg(tm)
994 proftime_T *tm;
995{
996 static char buf[50];
997
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +0000998# ifdef WIN3264
999 LARGE_INTEGER fr;
1000
1001 QueryPerformanceFrequency(&fr);
1002 sprintf(buf, "%10.6lf", (double)tm->QuadPart / (double)fr.QuadPart);
1003# else
Bram Moolenaar05159a02005-02-26 23:04:13 +00001004 sprintf(buf, "%3ld.%06ld", (long)tm->tv_sec, (long)tm->tv_usec);
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001005#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +00001006 return buf;
1007}
1008
1009static char_u *profile_fname = NULL;
1010
1011/*
1012 * ":profile cmd args"
1013 */
1014 void
1015ex_profile(eap)
1016 exarg_T *eap;
1017{
1018 char_u *e;
1019 int len;
1020
1021 e = skiptowhite(eap->arg);
1022 len = e - eap->arg;
1023 e = skipwhite(e);
1024
1025 if (len == 5 && STRNCMP(eap->arg, "start", 5) == 0 && *e != NUL)
1026 {
1027 vim_free(profile_fname);
1028 profile_fname = vim_strsave(e);
1029 do_profiling = TRUE;
1030 profile_zero(&prof_wait_time);
1031 set_vim_var_nr(VV_PROFILING, 1L);
1032 }
1033 else if (!do_profiling)
1034 EMSG(_("E750: First use :profile start <fname>"));
1035 else
1036 {
1037 /* The rest is similar to ":breakadd". */
1038 ex_breakadd(eap);
1039 }
1040}
1041
1042/*
1043 * Dump the profiling info.
1044 */
1045 void
1046profile_dump()
1047{
1048 FILE *fd;
1049
1050 if (profile_fname != NULL)
1051 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001052 fd = mch_fopen((char *)profile_fname, "w");
Bram Moolenaar05159a02005-02-26 23:04:13 +00001053 if (fd == NULL)
1054 EMSG2(_(e_notopen), profile_fname);
1055 else
1056 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00001057 script_dump_profile(fd);
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001058 func_dump_profile(fd);
Bram Moolenaar05159a02005-02-26 23:04:13 +00001059 fclose(fd);
1060 }
1061 }
1062}
1063
1064/*
1065 * Start profiling script "fp".
1066 */
1067 static void
1068script_do_profile(si)
1069 scriptitem_T *si;
1070{
1071 si->sn_pr_count = 0;
1072 profile_zero(&si->sn_pr_total);
1073 profile_zero(&si->sn_pr_self);
1074
1075 ga_init2(&si->sn_prl_ga, sizeof(sn_prl_T), 100);
1076 si->sn_prl_idx = -1;
1077 si->sn_prof_on = TRUE;
1078 si->sn_pr_nest = 0;
1079}
1080
1081/*
1082 * save time when starting to invoke another script or function.
1083 */
1084 void
1085script_prof_save(tm)
1086 proftime_T *tm; /* place to store wait time */
1087{
1088 scriptitem_T *si;
1089
1090 if (current_SID > 0 && current_SID <= script_items.ga_len)
1091 {
1092 si = &SCRIPT_ITEM(current_SID);
1093 if (si->sn_prof_on && si->sn_pr_nest++ == 0)
1094 profile_start(&si->sn_pr_child);
1095 }
1096 profile_get_wait(tm);
1097}
1098
1099/*
1100 * Count time spent in children after invoking another script or function.
1101 */
1102 void
1103script_prof_restore(tm)
1104 proftime_T *tm;
1105{
1106 scriptitem_T *si;
1107
1108 if (current_SID > 0 && current_SID <= script_items.ga_len)
1109 {
1110 si = &SCRIPT_ITEM(current_SID);
1111 if (si->sn_prof_on && --si->sn_pr_nest == 0)
1112 {
1113 profile_end(&si->sn_pr_child);
1114 profile_sub_wait(tm, &si->sn_pr_child); /* don't count wait time */
1115 profile_add(&si->sn_pr_children, &si->sn_pr_child);
1116 profile_add(&si->sn_prl_children, &si->sn_pr_child);
1117 }
1118 }
1119}
1120
1121static proftime_T inchar_time;
1122
1123/*
1124 * Called when starting to wait for the user to type a character.
1125 */
1126 void
1127prof_inchar_enter()
1128{
1129 profile_start(&inchar_time);
1130}
1131
1132/*
1133 * Called when finished waiting for the user to type a character.
1134 */
1135 void
1136prof_inchar_exit()
1137{
1138 profile_end(&inchar_time);
1139 profile_add(&prof_wait_time, &inchar_time);
1140}
1141
1142/*
1143 * Dump the profiling results for all scripts in file "fd".
1144 */
1145 static void
1146script_dump_profile(fd)
1147 FILE *fd;
1148{
1149 int id;
1150 scriptitem_T *si;
1151 int i;
1152 FILE *sfd;
1153 sn_prl_T *pp;
1154
1155 for (id = 1; id <= script_items.ga_len; ++id)
1156 {
1157 si = &SCRIPT_ITEM(id);
1158 if (si->sn_prof_on)
1159 {
1160 fprintf(fd, "SCRIPT %s\n", si->sn_name);
1161 if (si->sn_pr_count == 1)
1162 fprintf(fd, "Sourced 1 time\n");
1163 else
1164 fprintf(fd, "Sourced %d times\n", si->sn_pr_count);
1165 fprintf(fd, "Total time: %s\n", profile_msg(&si->sn_pr_total));
1166 fprintf(fd, " Self time: %s\n", profile_msg(&si->sn_pr_self));
1167 fprintf(fd, "\n");
1168 fprintf(fd, "count total (s) self (s)\n");
1169
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001170 sfd = mch_fopen((char *)si->sn_name, "r");
Bram Moolenaar05159a02005-02-26 23:04:13 +00001171 if (sfd == NULL)
1172 fprintf(fd, "Cannot open file!\n");
1173 else
1174 {
1175 for (i = 0; i < si->sn_prl_ga.ga_len; ++i)
1176 {
1177 if (vim_fgets(IObuff, IOSIZE, sfd))
1178 break;
1179 pp = &PRL_ITEM(si, i);
1180 if (pp->snp_count > 0)
1181 {
1182 fprintf(fd, "%5d ", pp->snp_count);
1183 if (profile_equal(&pp->sn_prl_total, &pp->sn_prl_self))
1184 fprintf(fd, " ");
1185 else
1186 fprintf(fd, "%s ", profile_msg(&pp->sn_prl_total));
1187 fprintf(fd, "%s ", profile_msg(&pp->sn_prl_self));
1188 }
1189 else
1190 fprintf(fd, " ");
1191 fprintf(fd, "%s", IObuff);
1192 }
1193 fclose(sfd);
1194 }
1195 fprintf(fd, "\n");
1196 }
1197 }
1198}
1199
1200/*
1201 * Return TRUE when a function defined in the current script should be
1202 * profiled.
1203 */
1204 int
1205prof_def_func()
1206{
Bram Moolenaar53180ce2005-07-05 21:48:14 +00001207 if (current_SID > 0)
1208 return SCRIPT_ITEM(current_SID).sn_pr_force;
1209 return FALSE;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001210}
1211
1212# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213#endif
1214
1215/*
1216 * If 'autowrite' option set, try to write the file.
1217 * Careful: autocommands may make "buf" invalid!
1218 *
1219 * return FAIL for failure, OK otherwise
1220 */
1221 int
1222autowrite(buf, forceit)
1223 buf_T *buf;
1224 int forceit;
1225{
1226 if (!(p_aw || p_awa) || !p_write
1227#ifdef FEAT_QUICKFIX
1228 /* never autowrite a "nofile" or "nowrite" buffer */
1229 || bt_dontwrite(buf)
1230#endif
1231 || (!forceit && buf->b_p_ro) || buf->b_ffname == NULL)
1232 return FAIL;
1233 return buf_write_all(buf, forceit);
1234}
1235
1236/*
1237 * flush all buffers, except the ones that are readonly
1238 */
1239 void
1240autowrite_all()
1241{
1242 buf_T *buf;
1243
1244 if (!(p_aw || p_awa) || !p_write)
1245 return;
1246 for (buf = firstbuf; buf; buf = buf->b_next)
1247 if (bufIsChanged(buf) && !buf->b_p_ro)
1248 {
1249 (void)buf_write_all(buf, FALSE);
1250#ifdef FEAT_AUTOCMD
1251 /* an autocommand may have deleted the buffer */
1252 if (!buf_valid(buf))
1253 buf = firstbuf;
1254#endif
1255 }
1256}
1257
1258/*
1259 * return TRUE if buffer was changed and cannot be abandoned.
1260 */
1261/*ARGSUSED*/
1262 int
1263check_changed(buf, checkaw, mult_win, forceit, allbuf)
1264 buf_T *buf;
1265 int checkaw; /* do autowrite if buffer was changed */
1266 int mult_win; /* check also when several wins for the buf */
1267 int forceit;
1268 int allbuf; /* may write all buffers */
1269{
1270 if ( !forceit
1271 && bufIsChanged(buf)
1272 && (mult_win || buf->b_nwindows <= 1)
1273 && (!checkaw || autowrite(buf, forceit) == FAIL))
1274 {
1275#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1276 if ((p_confirm || cmdmod.confirm) && p_write)
1277 {
1278 buf_T *buf2;
1279 int count = 0;
1280
1281 if (allbuf)
1282 for (buf2 = firstbuf; buf2 != NULL; buf2 = buf2->b_next)
1283 if (bufIsChanged(buf2)
1284 && (buf2->b_ffname != NULL
1285# ifdef FEAT_BROWSE
1286 || cmdmod.browse
1287# endif
1288 ))
1289 ++count;
1290# ifdef FEAT_AUTOCMD
1291 if (!buf_valid(buf))
1292 /* Autocommand deleted buffer, oops! It's not changed now. */
1293 return FALSE;
1294# endif
1295 dialog_changed(buf, count > 1);
1296# ifdef FEAT_AUTOCMD
1297 if (!buf_valid(buf))
1298 /* Autocommand deleted buffer, oops! It's not changed now. */
1299 return FALSE;
1300# endif
1301 return bufIsChanged(buf);
1302 }
1303#endif
1304 EMSG(_(e_nowrtmsg));
1305 return TRUE;
1306 }
1307 return FALSE;
1308}
1309
1310#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
1311
1312#if defined(FEAT_BROWSE) || defined(PROTO)
1313/*
1314 * When wanting to write a file without a file name, ask the user for a name.
1315 */
1316 void
1317browse_save_fname(buf)
1318 buf_T *buf;
1319{
1320 if (buf->b_fname == NULL)
1321 {
1322 char_u *fname;
1323
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00001324 fname = do_browse(BROWSE_SAVE, (char_u *)_("Save As"),
1325 NULL, NULL, NULL, NULL, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 if (fname != NULL)
1327 {
1328 if (setfname(buf, fname, NULL, TRUE) == OK)
1329 buf->b_flags |= BF_NOTEDITED;
1330 vim_free(fname);
1331 }
1332 }
1333}
1334#endif
1335
1336/*
1337 * Ask the user what to do when abondoning a changed buffer.
1338 * Must check 'write' option first!
1339 */
1340 void
1341dialog_changed(buf, checkall)
1342 buf_T *buf;
1343 int checkall; /* may abandon all changed buffers */
1344{
1345 char_u buff[IOSIZE];
1346 int ret;
1347 buf_T *buf2;
1348
Bram Moolenaar82cf9b62005-06-07 21:09:25 +00001349 dialog_msg(buff, _("Save changes to \"%s\"?"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 (buf->b_fname != NULL) ?
1351 buf->b_fname : (char_u *)_("Untitled"));
1352 if (checkall)
1353 ret = vim_dialog_yesnoallcancel(VIM_QUESTION, NULL, buff, 1);
1354 else
1355 ret = vim_dialog_yesnocancel(VIM_QUESTION, NULL, buff, 1);
1356
1357 if (ret == VIM_YES)
1358 {
1359#ifdef FEAT_BROWSE
1360 /* May get file name, when there is none */
1361 browse_save_fname(buf);
1362#endif
1363 if (buf->b_fname != NULL) /* didn't hit Cancel */
1364 (void)buf_write_all(buf, FALSE);
1365 }
1366 else if (ret == VIM_NO)
1367 {
1368 unchanged(buf, TRUE);
1369 }
1370 else if (ret == VIM_ALL)
1371 {
1372 /*
1373 * Write all modified files that can be written.
1374 * Skip readonly buffers, these need to be confirmed
1375 * individually.
1376 */
1377 for (buf2 = firstbuf; buf2 != NULL; buf2 = buf2->b_next)
1378 {
1379 if (bufIsChanged(buf2)
1380 && (buf2->b_ffname != NULL
1381#ifdef FEAT_BROWSE
1382 || cmdmod.browse
1383#endif
1384 )
1385 && !buf2->b_p_ro)
1386 {
1387#ifdef FEAT_BROWSE
1388 /* May get file name, when there is none */
1389 browse_save_fname(buf2);
1390#endif
1391 if (buf2->b_fname != NULL) /* didn't hit Cancel */
1392 (void)buf_write_all(buf2, FALSE);
1393#ifdef FEAT_AUTOCMD
1394 /* an autocommand may have deleted the buffer */
1395 if (!buf_valid(buf2))
1396 buf2 = firstbuf;
1397#endif
1398 }
1399 }
1400 }
1401 else if (ret == VIM_DISCARDALL)
1402 {
1403 /*
1404 * mark all buffers as unchanged
1405 */
1406 for (buf2 = firstbuf; buf2 != NULL; buf2 = buf2->b_next)
1407 unchanged(buf2, TRUE);
1408 }
1409}
1410#endif
1411
1412/*
1413 * Return TRUE if the buffer "buf" can be abandoned, either by making it
1414 * hidden, autowriting it or unloading it.
1415 */
1416 int
1417can_abandon(buf, forceit)
1418 buf_T *buf;
1419 int forceit;
1420{
1421 return ( P_HID(buf)
1422 || !bufIsChanged(buf)
1423 || buf->b_nwindows > 1
1424 || autowrite(buf, forceit) == OK
1425 || forceit);
1426}
1427
1428/*
1429 * Return TRUE if any buffer was changed and cannot be abandoned.
1430 * That changed buffer becomes the current buffer.
1431 */
1432 int
1433check_changed_any(hidden)
1434 int hidden; /* Only check hidden buffers */
1435{
1436 buf_T *buf;
1437 int save;
1438#ifdef FEAT_WINDOWS
1439 win_T *wp;
1440#endif
1441
1442 for (;;)
1443 {
1444 /* check curbuf first: if it was changed we can't abandon it */
1445 if (!hidden && curbufIsChanged())
1446 buf = curbuf;
1447 else
1448 {
1449 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1450 if ((!hidden || buf->b_nwindows == 0) && bufIsChanged(buf))
1451 break;
1452 }
1453 if (buf == NULL) /* No buffers changed */
1454 return FALSE;
1455
1456 if (check_changed(buf, p_awa, TRUE, FALSE, TRUE) && buf_valid(buf))
1457 break; /* didn't save - still changes */
1458 }
1459
1460 exiting = FALSE;
1461#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1462 /*
1463 * When ":confirm" used, don't give an error message.
1464 */
1465 if (!(p_confirm || cmdmod.confirm))
1466#endif
1467 {
1468 /* There must be a wait_return for this message, do_buffer()
1469 * may cause a redraw. But wait_return() is a no-op when vgetc()
1470 * is busy (Quit used from window menu), then make sure we don't
1471 * cause a scroll up. */
1472 if (vgetc_busy)
1473 {
1474 msg_row = cmdline_row;
1475 msg_col = 0;
1476 msg_didout = FALSE;
1477 }
1478 if (EMSG2(_("E162: No write since last change for buffer \"%s\""),
1479 buf_spname(buf) != NULL ? (char_u *)buf_spname(buf) :
1480 buf->b_fname))
1481 {
1482 save = no_wait_return;
1483 no_wait_return = FALSE;
1484 wait_return(FALSE);
1485 no_wait_return = save;
1486 }
1487 }
1488
1489#ifdef FEAT_WINDOWS
1490 /* Try to find a window that contains the buffer. */
1491 if (buf != curbuf)
1492 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1493 if (wp->w_buffer == buf)
1494 {
1495 win_goto(wp);
1496# ifdef FEAT_AUTOCMD
1497 /* Paranoia: did autocms wipe out the buffer with changes? */
1498 if (!buf_valid(buf))
1499 return TRUE;
1500# endif
1501 break;
1502 }
1503#endif
1504
1505 /* Open the changed buffer in the current window. */
1506 if (buf != curbuf)
1507 set_curbuf(buf, DOBUF_GOTO);
1508
1509 return TRUE;
1510}
1511
1512/*
1513 * return FAIL if there is no file name, OK if there is one
1514 * give error message for FAIL
1515 */
1516 int
1517check_fname()
1518{
1519 if (curbuf->b_ffname == NULL)
1520 {
1521 EMSG(_(e_noname));
1522 return FAIL;
1523 }
1524 return OK;
1525}
1526
1527/*
1528 * flush the contents of a buffer, unless it has no file name
1529 *
1530 * return FAIL for failure, OK otherwise
1531 */
1532 int
1533buf_write_all(buf, forceit)
1534 buf_T *buf;
1535 int forceit;
1536{
1537 int retval;
1538#ifdef FEAT_AUTOCMD
1539 buf_T *old_curbuf = curbuf;
1540#endif
1541
1542 retval = (buf_write(buf, buf->b_ffname, buf->b_fname,
1543 (linenr_T)1, buf->b_ml.ml_line_count, NULL,
1544 FALSE, forceit, TRUE, FALSE));
1545#ifdef FEAT_AUTOCMD
1546 if (curbuf != old_curbuf)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001547 {
1548 msg_source(hl_attr(HLF_W));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 MSG(_("Warning: Entered other buffer unexpectedly (check autocommands)"));
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001550 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551#endif
1552 return retval;
1553}
1554
1555/*
1556 * Code to handle the argument list.
1557 */
1558
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001559static char_u *do_one_arg __ARGS((char_u *str));
1560static int do_arglist __ARGS((char_u *str, int what, int after));
1561static void alist_check_arg_idx __ARGS((void));
1562static int editing_arg_idx __ARGS((win_T *win));
1563#ifdef FEAT_LISTCMDS
1564static int alist_add_list __ARGS((int count, char_u **files, int after));
1565#endif
1566#define AL_SET 1
1567#define AL_ADD 2
1568#define AL_DEL 3
1569
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001571 * Isolate one argument, taking backticks.
1572 * Changes the argument in-place, puts a NUL after it. Backticks remain.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 * Return a pointer to the start of the next argument.
1574 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001575 static char_u *
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576do_one_arg(str)
1577 char_u *str;
1578{
1579 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 int inbacktick;
1581
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 inbacktick = FALSE;
1583 for (p = str; *str; ++str)
1584 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001585 /* When the backslash is used for escaping the special meaning of a
1586 * character we need to keep it until wildcard expansion. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 if (rem_backslash(str))
1588 {
1589 *p++ = *str++;
1590 *p++ = *str;
1591 }
1592 else
1593 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001594 /* An item ends at a space not in backticks */
1595 if (!inbacktick && vim_isspace(*str))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 break;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001597 if (*str == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 inbacktick ^= TRUE;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001599 *p++ = *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 }
1601 }
1602 str = skipwhite(str);
1603 *p = NUL;
1604
1605 return str;
1606}
1607
Bram Moolenaar86b68352004-12-27 21:59:20 +00001608/*
1609 * Separate the arguments in "str" and return a list of pointers in the
1610 * growarray "gap".
1611 */
1612 int
1613get_arglist(gap, str)
1614 garray_T *gap;
1615 char_u *str;
1616{
1617 ga_init2(gap, (int)sizeof(char_u *), 20);
1618 while (*str != NUL)
1619 {
1620 if (ga_grow(gap, 1) == FAIL)
1621 {
1622 ga_clear(gap);
1623 return FAIL;
1624 }
1625 ((char_u **)gap->ga_data)[gap->ga_len++] = str;
1626
1627 /* Isolate one argument, change it in-place, put a NUL after it. */
1628 str = do_one_arg(str);
1629 }
1630 return OK;
1631}
1632
Bram Moolenaar7df351e2006-01-23 22:30:28 +00001633#if defined(FEAT_QUICKFIX) || defined(FEAT_SYN_HL) || defined(PROTO)
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00001634/*
1635 * Parse a list of arguments (file names), expand them and return in
1636 * "fnames[fcountp]".
1637 * Return FAIL or OK.
1638 */
1639 int
1640get_arglist_exp(str, fcountp, fnamesp)
1641 char_u *str;
1642 int *fcountp;
1643 char_u ***fnamesp;
1644{
1645 garray_T ga;
1646 int i;
1647
1648 if (get_arglist(&ga, str) == FAIL)
1649 return FAIL;
1650 i = gen_expand_wildcards(ga.ga_len, (char_u **)ga.ga_data,
1651 fcountp, fnamesp, EW_FILE|EW_NOTFOUND);
1652 ga_clear(&ga);
1653 return i;
1654}
1655#endif
1656
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657#if defined(FEAT_GUI) || defined(FEAT_CLIENTSERVER) || defined(PROTO)
1658/*
1659 * Redefine the argument list.
1660 */
1661 void
1662set_arglist(str)
1663 char_u *str;
1664{
1665 do_arglist(str, AL_SET, 0);
1666}
1667#endif
1668
1669/*
1670 * "what" == AL_SET: Redefine the argument list to 'str'.
1671 * "what" == AL_ADD: add files in 'str' to the argument list after "after".
1672 * "what" == AL_DEL: remove files in 'str' from the argument list.
1673 *
1674 * Return FAIL for failure, OK otherwise.
1675 */
1676/*ARGSUSED*/
1677 static int
1678do_arglist(str, what, after)
1679 char_u *str;
1680 int what;
1681 int after; /* 0 means before first one */
1682{
1683 garray_T new_ga;
1684 int exp_count;
1685 char_u **exp_files;
1686 int i;
1687#ifdef FEAT_LISTCMDS
1688 char_u *p;
1689 int match;
1690#endif
1691
1692 /*
1693 * Collect all file name arguments in "new_ga".
1694 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00001695 if (get_arglist(&new_ga, str) == FAIL)
1696 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697
1698#ifdef FEAT_LISTCMDS
1699 if (what == AL_DEL)
1700 {
1701 regmatch_T regmatch;
1702 int didone;
1703
1704 /*
1705 * Delete the items: use each item as a regexp and find a match in the
1706 * argument list.
1707 */
1708#ifdef CASE_INSENSITIVE_FILENAME
1709 regmatch.rm_ic = TRUE; /* Always ignore case */
1710#else
1711 regmatch.rm_ic = FALSE; /* Never ignore case */
1712#endif
1713 for (i = 0; i < new_ga.ga_len && !got_int; ++i)
1714 {
1715 p = ((char_u **)new_ga.ga_data)[i];
1716 p = file_pat_to_reg_pat(p, NULL, NULL, FALSE);
1717 if (p == NULL)
1718 break;
1719 regmatch.regprog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
1720 if (regmatch.regprog == NULL)
1721 {
1722 vim_free(p);
1723 break;
1724 }
1725
1726 didone = FALSE;
1727 for (match = 0; match < ARGCOUNT; ++match)
1728 if (vim_regexec(&regmatch, alist_name(&ARGLIST[match]),
1729 (colnr_T)0))
1730 {
1731 didone = TRUE;
1732 vim_free(ARGLIST[match].ae_fname);
1733 mch_memmove(ARGLIST + match, ARGLIST + match + 1,
1734 (ARGCOUNT - match - 1) * sizeof(aentry_T));
1735 --ALIST(curwin)->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 if (curwin->w_arg_idx > match)
1737 --curwin->w_arg_idx;
1738 --match;
1739 }
1740
1741 vim_free(regmatch.regprog);
1742 vim_free(p);
1743 if (!didone)
1744 EMSG2(_(e_nomatch2), ((char_u **)new_ga.ga_data)[i]);
1745 }
1746 ga_clear(&new_ga);
1747 }
1748 else
1749#endif
1750 {
1751 i = expand_wildcards(new_ga.ga_len, (char_u **)new_ga.ga_data,
1752 &exp_count, &exp_files, EW_DIR|EW_FILE|EW_ADDSLASH|EW_NOTFOUND);
1753 ga_clear(&new_ga);
1754 if (i == FAIL)
1755 return FAIL;
1756 if (exp_count == 0)
1757 {
1758 EMSG(_(e_nomatch));
1759 return FAIL;
1760 }
1761
1762#ifdef FEAT_LISTCMDS
1763 if (what == AL_ADD)
1764 {
1765 (void)alist_add_list(exp_count, exp_files, after);
1766 vim_free(exp_files);
1767 }
1768 else /* what == AL_SET */
1769#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00001770 alist_set(ALIST(curwin), exp_count, exp_files, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 }
1772
1773 alist_check_arg_idx();
1774
1775 return OK;
1776}
1777
1778/*
1779 * Check the validity of the arg_idx for each other window.
1780 */
1781 static void
1782alist_check_arg_idx()
1783{
1784#ifdef FEAT_WINDOWS
1785 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001786 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787
Bram Moolenaarf740b292006-02-16 22:11:02 +00001788 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 if (win->w_alist == curwin->w_alist)
1790 check_arg_idx(win);
1791#else
1792 check_arg_idx(curwin);
1793#endif
1794}
1795
1796/*
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001797 * Return TRUE if window "win" is editing then file at the current argument
1798 * index.
1799 */
1800 static int
1801editing_arg_idx(win)
1802 win_T *win;
1803{
1804 return !(win->w_arg_idx >= WARGCOUNT(win)
1805 || (win->w_buffer->b_fnum
1806 != WARGLIST(win)[win->w_arg_idx].ae_fnum
1807 && (win->w_buffer->b_ffname == NULL
1808 || !(fullpathcmp(
1809 alist_name(&WARGLIST(win)[win->w_arg_idx]),
1810 win->w_buffer->b_ffname, TRUE) & FPC_SAME))));
1811}
1812
1813/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 * Check if window "win" is editing the w_arg_idx file in its argument list.
1815 */
1816 void
1817check_arg_idx(win)
1818 win_T *win;
1819{
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001820 if (WARGCOUNT(win) > 1 && !editing_arg_idx(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 {
1822 /* We are not editing the current entry in the argument list.
1823 * Set "arg_had_last" if we are editing the last one. */
1824 win->w_arg_idx_invalid = TRUE;
1825 if (win->w_arg_idx != WARGCOUNT(win) - 1
1826 && arg_had_last == FALSE
1827#ifdef FEAT_WINDOWS
1828 && ALIST(win) == &global_alist
1829#endif
1830 && GARGCOUNT > 0
1831 && win->w_arg_idx < GARGCOUNT
1832 && (win->w_buffer->b_fnum == GARGLIST[GARGCOUNT - 1].ae_fnum
1833 || (win->w_buffer->b_ffname != NULL
1834 && (fullpathcmp(alist_name(&GARGLIST[GARGCOUNT - 1]),
1835 win->w_buffer->b_ffname, TRUE) & FPC_SAME))))
1836 arg_had_last = TRUE;
1837 }
1838 else
1839 {
1840 /* We are editing the current entry in the argument list.
1841 * Set "arg_had_last" if it's also the last one */
1842 win->w_arg_idx_invalid = FALSE;
1843 if (win->w_arg_idx == WARGCOUNT(win) - 1
1844#ifdef FEAT_WINDOWS
1845 && win->w_alist == &global_alist
1846#endif
1847 )
1848 arg_had_last = TRUE;
1849 }
1850}
1851
1852/*
1853 * ":args", ":argslocal" and ":argsglobal".
1854 */
1855 void
1856ex_args(eap)
1857 exarg_T *eap;
1858{
1859 int i;
1860
1861 if (eap->cmdidx != CMD_args)
1862 {
1863#if defined(FEAT_WINDOWS) && defined(FEAT_LISTCMDS)
1864 alist_unlink(ALIST(curwin));
1865 if (eap->cmdidx == CMD_argglobal)
1866 ALIST(curwin) = &global_alist;
1867 else /* eap->cmdidx == CMD_arglocal */
1868 alist_new();
1869#else
1870 ex_ni(eap);
1871 return;
1872#endif
1873 }
1874
1875 if (!ends_excmd(*eap->arg))
1876 {
1877 /*
1878 * ":args file ..": define new argument list, handle like ":next"
1879 * Also for ":argslocal file .." and ":argsglobal file ..".
1880 */
1881 ex_next(eap);
1882 }
1883 else
1884#if defined(FEAT_WINDOWS) && defined(FEAT_LISTCMDS)
1885 if (eap->cmdidx == CMD_args)
1886#endif
1887 {
1888 /*
1889 * ":args": list arguments.
1890 */
1891 if (ARGCOUNT > 0)
1892 {
1893 /* Overwrite the command, for a short list there is no scrolling
1894 * required and no wait_return(). */
1895 gotocmdline(TRUE);
1896 for (i = 0; i < ARGCOUNT; ++i)
1897 {
1898 if (i == curwin->w_arg_idx)
1899 msg_putchar('[');
1900 msg_outtrans(alist_name(&ARGLIST[i]));
1901 if (i == curwin->w_arg_idx)
1902 msg_putchar(']');
1903 msg_putchar(' ');
1904 }
1905 }
1906 }
1907#if defined(FEAT_WINDOWS) && defined(FEAT_LISTCMDS)
1908 else if (eap->cmdidx == CMD_arglocal)
1909 {
1910 garray_T *gap = &curwin->w_alist->al_ga;
1911
1912 /*
1913 * ":argslocal": make a local copy of the global argument list.
1914 */
1915 if (ga_grow(gap, GARGCOUNT) == OK)
1916 for (i = 0; i < GARGCOUNT; ++i)
1917 if (GARGLIST[i].ae_fname != NULL)
1918 {
1919 AARGLIST(curwin->w_alist)[gap->ga_len].ae_fname =
1920 vim_strsave(GARGLIST[i].ae_fname);
1921 AARGLIST(curwin->w_alist)[gap->ga_len].ae_fnum =
1922 GARGLIST[i].ae_fnum;
1923 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 }
1925 }
1926#endif
1927}
1928
1929/*
1930 * ":previous", ":sprevious", ":Next" and ":sNext".
1931 */
1932 void
1933ex_previous(eap)
1934 exarg_T *eap;
1935{
1936 /* If past the last one already, go to the last one. */
1937 if (curwin->w_arg_idx - (int)eap->line2 >= ARGCOUNT)
1938 do_argfile(eap, ARGCOUNT - 1);
1939 else
1940 do_argfile(eap, curwin->w_arg_idx - (int)eap->line2);
1941}
1942
1943/*
1944 * ":rewind", ":first", ":sfirst" and ":srewind".
1945 */
1946 void
1947ex_rewind(eap)
1948 exarg_T *eap;
1949{
1950 do_argfile(eap, 0);
1951}
1952
1953/*
1954 * ":last" and ":slast".
1955 */
1956 void
1957ex_last(eap)
1958 exarg_T *eap;
1959{
1960 do_argfile(eap, ARGCOUNT - 1);
1961}
1962
1963/*
1964 * ":argument" and ":sargument".
1965 */
1966 void
1967ex_argument(eap)
1968 exarg_T *eap;
1969{
1970 int i;
1971
1972 if (eap->addr_count > 0)
1973 i = eap->line2 - 1;
1974 else
1975 i = curwin->w_arg_idx;
1976 do_argfile(eap, i);
1977}
1978
1979/*
1980 * Edit file "argn" of the argument lists.
1981 */
1982 void
1983do_argfile(eap, argn)
1984 exarg_T *eap;
1985 int argn;
1986{
1987 int other;
1988 char_u *p;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001989 int old_arg_idx = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990
1991 if (argn < 0 || argn >= ARGCOUNT)
1992 {
1993 if (ARGCOUNT <= 1)
1994 EMSG(_("E163: There is only one file to edit"));
1995 else if (argn < 0)
1996 EMSG(_("E164: Cannot go before first file"));
1997 else
1998 EMSG(_("E165: Cannot go beyond last file"));
1999 }
2000 else
2001 {
2002 setpcmark();
2003#ifdef FEAT_GUI
2004 need_mouse_correct = TRUE;
2005#endif
2006
2007#ifdef FEAT_WINDOWS
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00002008 /* split window or create new tab page first */
2009 if (*eap->cmd == 's' || cmdmod.tab != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 {
2011 if (win_split(0, 0) == FAIL)
2012 return;
2013# ifdef FEAT_SCROLLBIND
2014 curwin->w_p_scb = FALSE;
2015# endif
2016 }
2017 else
2018#endif
2019 {
2020 /*
2021 * if 'hidden' set, only check for changed file when re-editing
2022 * the same buffer
2023 */
2024 other = TRUE;
2025 if (P_HID(curbuf))
2026 {
2027 p = fix_fname(alist_name(&ARGLIST[argn]));
2028 other = otherfile(p);
2029 vim_free(p);
2030 }
2031 if ((!P_HID(curbuf) || !other)
2032 && check_changed(curbuf, TRUE, !other, eap->forceit, FALSE))
2033 return;
2034 }
2035
2036 curwin->w_arg_idx = argn;
2037 if (argn == ARGCOUNT - 1
2038#ifdef FEAT_WINDOWS
2039 && curwin->w_alist == &global_alist
2040#endif
2041 )
2042 arg_had_last = TRUE;
2043
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002044 /* Edit the file; always use the last known line number.
2045 * When it fails (e.g. Abort for already edited file) restore the
2046 * argument index. */
2047 if (do_ecmd(0, alist_name(&ARGLIST[curwin->w_arg_idx]), NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 eap, ECMD_LAST,
2049 (P_HID(curwin->w_buffer) ? ECMD_HIDE : 0) +
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002050 (eap->forceit ? ECMD_FORCEIT : 0)) == FAIL)
2051 curwin->w_arg_idx = old_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 /* like Vi: set the mark where the cursor is in the file. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002053 else if (eap->cmdidx != CMD_argdo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 setmark('\'');
2055 }
2056}
2057
2058/*
2059 * ":next", and commands that behave like it.
2060 */
2061 void
2062ex_next(eap)
2063 exarg_T *eap;
2064{
2065 int i;
2066
2067 /*
2068 * check for changed buffer now, if this fails the argument list is not
2069 * redefined.
2070 */
2071 if ( P_HID(curbuf)
2072 || eap->cmdidx == CMD_snext
2073 || !check_changed(curbuf, TRUE, FALSE, eap->forceit, FALSE))
2074 {
2075 if (*eap->arg != NUL) /* redefine file list */
2076 {
2077 if (do_arglist(eap->arg, AL_SET, 0) == FAIL)
2078 return;
2079 i = 0;
2080 }
2081 else
2082 i = curwin->w_arg_idx + (int)eap->line2;
2083 do_argfile(eap, i);
2084 }
2085}
2086
2087#ifdef FEAT_LISTCMDS
2088/*
2089 * ":argedit"
2090 */
2091 void
2092ex_argedit(eap)
2093 exarg_T *eap;
2094{
2095 int fnum;
2096 int i;
2097 char_u *s;
2098
2099 /* Add the argument to the buffer list and get the buffer number. */
2100 fnum = buflist_add(eap->arg, BLN_LISTED);
2101
2102 /* Check if this argument is already in the argument list. */
2103 for (i = 0; i < ARGCOUNT; ++i)
2104 if (ARGLIST[i].ae_fnum == fnum)
2105 break;
2106 if (i == ARGCOUNT)
2107 {
2108 /* Can't find it, add it to the argument list. */
2109 s = vim_strsave(eap->arg);
2110 if (s == NULL)
2111 return;
2112 i = alist_add_list(1, &s,
2113 eap->addr_count > 0 ? (int)eap->line2 : curwin->w_arg_idx + 1);
2114 if (i < 0)
2115 return;
2116 curwin->w_arg_idx = i;
2117 }
2118
2119 alist_check_arg_idx();
2120
2121 /* Edit the argument. */
2122 do_argfile(eap, i);
2123}
2124
2125/*
2126 * ":argadd"
2127 */
2128 void
2129ex_argadd(eap)
2130 exarg_T *eap;
2131{
2132 do_arglist(eap->arg, AL_ADD,
2133 eap->addr_count > 0 ? (int)eap->line2 : curwin->w_arg_idx + 1);
2134#ifdef FEAT_TITLE
2135 maketitle();
2136#endif
2137}
2138
2139/*
2140 * ":argdelete"
2141 */
2142 void
2143ex_argdelete(eap)
2144 exarg_T *eap;
2145{
2146 int i;
2147 int n;
2148
2149 if (eap->addr_count > 0)
2150 {
2151 /* ":1,4argdel": Delete all arguments in the range. */
2152 if (eap->line2 > ARGCOUNT)
2153 eap->line2 = ARGCOUNT;
2154 n = eap->line2 - eap->line1 + 1;
2155 if (*eap->arg != NUL || n <= 0)
2156 EMSG(_(e_invarg));
2157 else
2158 {
2159 for (i = eap->line1; i <= eap->line2; ++i)
2160 vim_free(ARGLIST[i - 1].ae_fname);
2161 mch_memmove(ARGLIST + eap->line1 - 1, ARGLIST + eap->line2,
2162 (size_t)((ARGCOUNT - eap->line2) * sizeof(aentry_T)));
2163 ALIST(curwin)->al_ga.ga_len -= n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 if (curwin->w_arg_idx >= eap->line2)
2165 curwin->w_arg_idx -= n;
2166 else if (curwin->w_arg_idx > eap->line1)
2167 curwin->w_arg_idx = eap->line1;
2168 }
2169 }
2170 else if (*eap->arg == NUL)
2171 EMSG(_(e_argreq));
2172 else
2173 do_arglist(eap->arg, AL_DEL, 0);
2174#ifdef FEAT_TITLE
2175 maketitle();
2176#endif
2177}
2178
2179/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002180 * ":argdo", ":windo", ":bufdo", ":tabdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 */
2182 void
2183ex_listdo(eap)
2184 exarg_T *eap;
2185{
2186 int i;
2187#ifdef FEAT_WINDOWS
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002188 win_T *wp;
2189 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190#endif
2191 buf_T *buf;
2192 int next_fnum = 0;
2193#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
2194 char_u *save_ei = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002196 char_u *p_shm_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197
2198#ifndef FEAT_WINDOWS
2199 if (eap->cmdidx == CMD_windo)
2200 {
2201 ex_ni(eap);
2202 return;
2203 }
2204#endif
2205
2206#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
2207 if (eap->cmdidx != CMD_windo)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002208 /* Don't do syntax HL autocommands. Skipping the syntax file is a
2209 * great speed improvement. */
2210 save_ei = au_event_disable(",Syntax");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211#endif
2212
2213 if (eap->cmdidx == CMD_windo
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002214 || eap->cmdidx == CMD_tabdo
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 || P_HID(curbuf)
2216 || !check_changed(curbuf, TRUE, FALSE, eap->forceit, FALSE))
2217 {
2218 /* start at the first argument/window/buffer */
2219 i = 0;
2220#ifdef FEAT_WINDOWS
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002221 wp = firstwin;
2222 tp = first_tabpage;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223#endif
2224 /* set pcmark now */
2225 if (eap->cmdidx == CMD_bufdo)
2226 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
2227 else
2228 setpcmark();
2229 listcmd_busy = TRUE; /* avoids setting pcmark below */
2230
2231 while (!got_int)
2232 {
2233 if (eap->cmdidx == CMD_argdo)
2234 {
2235 /* go to argument "i" */
2236 if (i == ARGCOUNT)
2237 break;
2238 /* Don't call do_argfile() when already there, it will try
2239 * reloading the file. */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002240 if (curwin->w_arg_idx != i || !editing_arg_idx(curwin))
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002241 {
2242 /* Clear 'shm' to avoid that the file message overwrites
2243 * any output from the command. */
2244 p_shm_save = vim_strsave(p_shm);
2245 set_option_value((char_u *)"shm", 0L, (char_u *)"", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 do_argfile(eap, i);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002247 set_option_value((char_u *)"shm", 0L, p_shm_save, 0);
2248 vim_free(p_shm_save);
2249 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 if (curwin->w_arg_idx != i)
2251 break;
2252 ++i;
2253 }
2254#ifdef FEAT_WINDOWS
2255 else if (eap->cmdidx == CMD_windo)
2256 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002257 /* go to window "wp" */
2258 if (!win_valid(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 break;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002260 win_goto(wp);
2261 wp = curwin->w_next;
2262 }
2263 else if (eap->cmdidx == CMD_tabdo)
2264 {
2265 /* go to window "tp" */
2266 if (!valid_tabpage(tp))
2267 break;
2268 goto_tabpage_tp(tp);
2269 tp = tp->tp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 }
2271#endif
2272 else if (eap->cmdidx == CMD_bufdo)
2273 {
2274 /* Remember the number of the next listed buffer, in case
2275 * ":bwipe" is used or autocommands do something strange. */
2276 next_fnum = -1;
2277 for (buf = curbuf->b_next; buf != NULL; buf = buf->b_next)
2278 if (buf->b_p_bl)
2279 {
2280 next_fnum = buf->b_fnum;
2281 break;
2282 }
2283 }
2284
2285 /* execute the command */
2286 do_cmdline(eap->arg, eap->getline, eap->cookie,
2287 DOCMD_VERBOSE + DOCMD_NOWAIT);
2288
2289 if (eap->cmdidx == CMD_bufdo)
2290 {
2291 /* Done? */
2292 if (next_fnum < 0)
2293 break;
2294 /* Check if the buffer still exists. */
2295 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2296 if (buf->b_fnum == next_fnum)
2297 break;
2298 if (buf == NULL)
2299 break;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002300
2301 /* Go to the next buffer. Clear 'shm' to avoid that the file
2302 * message overwrites any output from the command. */
2303 p_shm_save = vim_strsave(p_shm);
2304 set_option_value((char_u *)"shm", 0L, (char_u *)"", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 goto_buffer(eap, DOBUF_FIRST, FORWARD, next_fnum);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002306 set_option_value((char_u *)"shm", 0L, p_shm_save, 0);
2307 vim_free(p_shm_save);
2308
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 /* If autocommands took us elsewhere, quit here */
2310 if (curbuf->b_fnum != next_fnum)
2311 break;
2312 }
2313
2314 if (eap->cmdidx == CMD_windo)
2315 {
2316 validate_cursor(); /* cursor may have moved */
2317#ifdef FEAT_SCROLLBIND
2318 /* required when 'scrollbind' has been set */
2319 if (curwin->w_p_scb)
2320 do_check_scrollbind(TRUE);
2321#endif
2322 }
2323 }
2324 listcmd_busy = FALSE;
2325 }
2326
2327#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
Bram Moolenaar6ac54292005-02-02 23:07:25 +00002328 if (save_ei != NULL)
2329 {
2330 au_event_restore(save_ei);
2331 apply_autocmds(EVENT_SYNTAX, curbuf->b_p_syn,
2332 curbuf->b_fname, TRUE, curbuf);
2333 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334#endif
2335}
2336
2337/*
2338 * Add files[count] to the arglist of the current window after arg "after".
2339 * The file names in files[count] must have been allocated and are taken over.
2340 * Files[] itself is not taken over.
2341 * Returns index of first added argument. Returns -1 when failed (out of mem).
2342 */
2343 static int
2344alist_add_list(count, files, after)
2345 int count;
2346 char_u **files;
2347 int after; /* where to add: 0 = before first one */
2348{
2349 int i;
2350
2351 if (ga_grow(&ALIST(curwin)->al_ga, count) == OK)
2352 {
2353 if (after < 0)
2354 after = 0;
2355 if (after > ARGCOUNT)
2356 after = ARGCOUNT;
2357 if (after < ARGCOUNT)
2358 mch_memmove(&(ARGLIST[after + count]), &(ARGLIST[after]),
2359 (ARGCOUNT - after) * sizeof(aentry_T));
2360 for (i = 0; i < count; ++i)
2361 {
2362 ARGLIST[after + i].ae_fname = files[i];
2363 ARGLIST[after + i].ae_fnum = buflist_add(files[i], BLN_LISTED);
2364 }
2365 ALIST(curwin)->al_ga.ga_len += count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 if (curwin->w_arg_idx >= after)
2367 ++curwin->w_arg_idx;
2368 return after;
2369 }
2370
2371 for (i = 0; i < count; ++i)
2372 vim_free(files[i]);
2373 return -1;
2374}
2375
2376#endif /* FEAT_LISTCMDS */
2377
2378#ifdef FEAT_EVAL
2379/*
2380 * ":compiler[!] {name}"
2381 */
2382 void
2383ex_compiler(eap)
2384 exarg_T *eap;
2385{
2386 char_u *buf;
2387 char_u *old_cur_comp = NULL;
2388 char_u *p;
2389
2390 if (*eap->arg == NUL)
2391 {
2392 /* List all compiler scripts. */
2393 do_cmdline_cmd((char_u *)"echo globpath(&rtp, 'compiler/*.vim')");
2394 /* ) keep the indenter happy... */
2395 }
2396 else
2397 {
2398 buf = alloc((unsigned)(STRLEN(eap->arg) + 14));
2399 if (buf != NULL)
2400 {
2401 if (eap->forceit)
2402 {
2403 /* ":compiler! {name}" sets global options */
2404 do_cmdline_cmd((char_u *)
2405 "command -nargs=* CompilerSet set <args>");
2406 }
2407 else
2408 {
2409 /* ":compiler! {name}" sets local options.
2410 * To remain backwards compatible "current_compiler" is always
2411 * used. A user's compiler plugin may set it, the distributed
2412 * plugin will then skip the settings. Afterwards set
2413 * "b:current_compiler" and restore "current_compiler". */
2414 old_cur_comp = get_var_value((char_u *)"current_compiler");
2415 if (old_cur_comp != NULL)
2416 old_cur_comp = vim_strsave(old_cur_comp);
2417 do_cmdline_cmd((char_u *)
2418 "command -nargs=* CompilerSet setlocal <args>");
2419 }
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00002420 do_unlet((char_u *)"current_compiler", TRUE);
2421 do_unlet((char_u *)"b:current_compiler", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422
2423 sprintf((char *)buf, "compiler/%s.vim", eap->arg);
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00002424 if (source_runtime(buf, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 EMSG2(_("E666: compiler not supported: %s"), eap->arg);
2426 vim_free(buf);
2427
2428 do_cmdline_cmd((char_u *)":delcommand CompilerSet");
2429
2430 /* Set "b:current_compiler" from "current_compiler". */
2431 p = get_var_value((char_u *)"current_compiler");
2432 if (p != NULL)
2433 set_internal_string_var((char_u *)"b:current_compiler", p);
2434
2435 /* Restore "current_compiler" for ":compiler {name}". */
2436 if (!eap->forceit)
2437 {
2438 if (old_cur_comp != NULL)
2439 {
2440 set_internal_string_var((char_u *)"current_compiler",
2441 old_cur_comp);
2442 vim_free(old_cur_comp);
2443 }
2444 else
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00002445 do_unlet((char_u *)"current_compiler", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 }
2447 }
2448 }
2449}
2450#endif
2451
2452/*
2453 * ":runtime {name}"
2454 */
2455 void
2456ex_runtime(eap)
2457 exarg_T *eap;
2458{
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00002459 source_runtime(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460}
2461
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00002462static void source_callback __ARGS((char_u *fname, void *cookie));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00002464/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 static void
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00002466source_callback(fname, cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 char_u *fname;
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00002468 void *cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469{
2470 (void)do_source(fname, FALSE, FALSE);
2471}
2472
2473/*
2474 * Source the file "name" from all directories in 'runtimepath'.
2475 * "name" can contain wildcards.
2476 * When "all" is TRUE, source all files, otherwise only the first one.
2477 * return FAIL when no file could be sourced, OK otherwise.
2478 */
2479 int
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00002480source_runtime(name, all)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 char_u *name;
2482 int all;
2483{
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00002484 return do_in_runtimepath(name, all, source_callback, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485}
2486
2487/*
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00002488 * Find "name" in 'runtimepath'. When found, invoke the callback function for
2489 * it: callback(fname, "cookie")
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 * When "all" is TRUE repeat for all matches, otherwise only the first one is
2491 * used.
2492 * Returns OK when at least one match found, FAIL otherwise.
2493 */
2494 int
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00002495do_in_runtimepath(name, all, callback, cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496 char_u *name;
2497 int all;
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00002498 void (*callback)__ARGS((char_u *fname, void *ck));
2499 void *cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500{
2501 char_u *rtp;
2502 char_u *np;
2503 char_u *buf;
2504 char_u *rtp_copy;
2505 char_u *tail;
2506 int num_files;
2507 char_u **files;
2508 int i;
2509 int did_one = FALSE;
2510#ifdef AMIGA
2511 struct Process *proc = (struct Process *)FindTask(0L);
2512 APTR save_winptr = proc->pr_WindowPtr;
2513
2514 /* Avoid a requester here for a volume that doesn't exist. */
2515 proc->pr_WindowPtr = (APTR)-1L;
2516#endif
2517
2518 /* Make a copy of 'runtimepath'. Invoking the callback may change the
2519 * value. */
2520 rtp_copy = vim_strsave(p_rtp);
2521 buf = alloc(MAXPATHL);
2522 if (buf != NULL && rtp_copy != NULL)
2523 {
2524 if (p_verbose > 1)
Bram Moolenaar54ee7752005-05-31 22:22:17 +00002525 {
2526 verbose_enter();
Bram Moolenaar555b2802005-05-19 21:08:39 +00002527 smsg((char_u *)_("Searching for \"%s\" in \"%s\""),
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 (char *)name, (char *)p_rtp);
Bram Moolenaar54ee7752005-05-31 22:22:17 +00002529 verbose_leave();
2530 }
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002531
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 /* Loop over all entries in 'runtimepath'. */
2533 rtp = rtp_copy;
2534 while (*rtp != NUL && (all || !did_one))
2535 {
2536 /* Copy the path from 'runtimepath' to buf[]. */
2537 copy_option_part(&rtp, buf, MAXPATHL, ",");
2538 if (STRLEN(buf) + STRLEN(name) + 2 < MAXPATHL)
2539 {
2540 add_pathsep(buf);
2541 tail = buf + STRLEN(buf);
2542
2543 /* Loop over all patterns in "name" */
2544 np = name;
2545 while (*np != NUL && (all || !did_one))
2546 {
2547 /* Append the pattern from "name" to buf[]. */
2548 copy_option_part(&np, tail, (int)(MAXPATHL - (tail - buf)),
2549 "\t ");
2550
2551 if (p_verbose > 2)
Bram Moolenaar54ee7752005-05-31 22:22:17 +00002552 {
2553 verbose_enter();
Bram Moolenaar555b2802005-05-19 21:08:39 +00002554 smsg((char_u *)_("Searching for \"%s\""), buf);
Bram Moolenaar54ee7752005-05-31 22:22:17 +00002555 verbose_leave();
2556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557
2558 /* Expand wildcards, invoke the callback for each match. */
2559 if (gen_expand_wildcards(1, &buf, &num_files, &files,
2560 EW_FILE) == OK)
2561 {
2562 for (i = 0; i < num_files; ++i)
2563 {
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00002564 (*callback)(files[i], cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 did_one = TRUE;
2566 if (!all)
2567 break;
2568 }
2569 FreeWild(num_files, files);
2570 }
2571 }
2572 }
2573 }
2574 }
2575 vim_free(buf);
2576 vim_free(rtp_copy);
2577 if (p_verbose > 0 && !did_one)
Bram Moolenaar54ee7752005-05-31 22:22:17 +00002578 {
2579 verbose_enter();
Bram Moolenaar555b2802005-05-19 21:08:39 +00002580 smsg((char_u *)_("not found in 'runtimepath': \"%s\""), name);
Bram Moolenaar54ee7752005-05-31 22:22:17 +00002581 verbose_leave();
2582 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583
2584#ifdef AMIGA
2585 proc->pr_WindowPtr = save_winptr;
2586#endif
2587
2588 return did_one ? OK : FAIL;
2589}
2590
2591#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD)
2592/*
2593 * ":options"
2594 */
2595/*ARGSUSED*/
2596 void
2597ex_options(eap)
2598 exarg_T *eap;
2599{
2600 cmd_source((char_u *)SYS_OPTWIN_FILE, NULL);
2601}
2602#endif
2603
2604/*
2605 * ":source {fname}"
2606 */
2607 void
2608ex_source(eap)
2609 exarg_T *eap;
2610{
2611#ifdef FEAT_BROWSE
2612 if (cmdmod.browse)
2613 {
2614 char_u *fname = NULL;
2615
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002616 fname = do_browse(0, (char_u *)_("Source Vim script"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 NULL, NULL, BROWSE_FILTER_MACROS, NULL);
2618 if (fname != NULL)
2619 {
2620 cmd_source(fname, eap);
2621 vim_free(fname);
2622 }
2623 }
2624 else
2625#endif
2626 cmd_source(eap->arg, eap);
2627}
2628
2629 static void
2630cmd_source(fname, eap)
2631 char_u *fname;
2632 exarg_T *eap;
2633{
2634 if (*fname == NUL)
2635 EMSG(_(e_argreq));
2636
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 else if (eap != NULL && eap->forceit)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00002638 /* ":source!": read Normal mdoe commands
2639 * Need to execute the commands directly. This is required at least
2640 * for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 * - ":g" command busy
2642 * - after ":argdo", ":windo" or ":bufdo"
2643 * - another command follows
2644 * - inside a loop
2645 */
2646 openscript(fname, global_busy || listcmd_busy || eap->nextcmd != NULL
2647#ifdef FEAT_EVAL
2648 || eap->cstack->cs_idx >= 0
2649#endif
2650 );
2651
2652 /* ":source" read ex commands */
2653 else if (do_source(fname, FALSE, FALSE) == FAIL)
2654 EMSG2(_(e_notopen), fname);
2655}
2656
2657/*
2658 * ":source" and associated commands.
2659 */
2660/*
2661 * Structure used to store info for each sourced file.
2662 * It is shared between do_source() and getsourceline().
2663 * This is required, because it needs to be handed to do_cmdline() and
2664 * sourcing can be done recursively.
2665 */
2666struct source_cookie
2667{
2668 FILE *fp; /* opened file for sourcing */
2669 char_u *nextline; /* if not NULL: line that was read ahead */
2670 int finished; /* ":finish" used */
2671#if defined (USE_CRNL) || defined (USE_CR)
2672 int fileformat; /* EOL_UNKNOWN, EOL_UNIX or EOL_DOS */
2673 int error; /* TRUE if LF found after CR-LF */
2674#endif
2675#ifdef FEAT_EVAL
2676 linenr_T breakpoint; /* next line with breakpoint or zero */
2677 char_u *fname; /* name of sourced file */
2678 int dbg_tick; /* debug_tick when breakpoint was set */
2679 int level; /* top nesting level of sourced file */
2680#endif
2681#ifdef FEAT_MBYTE
2682 vimconv_T conv; /* type of conversion */
2683#endif
2684};
2685
2686#ifdef FEAT_EVAL
2687/*
2688 * Return the address holding the next breakpoint line for a source cookie.
2689 */
2690 linenr_T *
2691source_breakpoint(cookie)
2692 void *cookie;
2693{
2694 return &((struct source_cookie *)cookie)->breakpoint;
2695}
2696
2697/*
2698 * Return the address holding the debug tick for a source cookie.
2699 */
2700 int *
2701source_dbg_tick(cookie)
2702 void *cookie;
2703{
2704 return &((struct source_cookie *)cookie)->dbg_tick;
2705}
2706
2707/*
2708 * Return the nesting level for a source cookie.
2709 */
2710 int
2711source_level(cookie)
2712 void *cookie;
2713{
2714 return ((struct source_cookie *)cookie)->level;
2715}
2716#endif
2717
2718static char_u *get_one_sourceline __ARGS((struct source_cookie *sp));
2719
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720#if defined(WIN32) && defined(FEAT_CSCOPE)
2721static FILE *fopen_noinh_readbin __ARGS((char *filename));
2722
2723/*
2724 * Special function to open a file without handle inheritance.
2725 */
2726 static FILE *
2727fopen_noinh_readbin(filename)
2728 char *filename;
2729{
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00002730 int fd_tmp = mch_open(filename, O_RDONLY | O_BINARY | O_NOINHERIT, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731
2732 if (fd_tmp == -1)
2733 return NULL;
2734 return fdopen(fd_tmp, READBIN);
2735}
2736#endif
2737
2738
2739/*
2740 * do_source: Read the file "fname" and execute its lines as EX commands.
2741 *
2742 * This function may be called recursively!
2743 *
2744 * return FAIL if file could not be opened, OK otherwise
2745 */
2746 int
2747do_source(fname, check_other, is_vimrc)
2748 char_u *fname;
2749 int check_other; /* check for .vimrc and _vimrc */
2750 int is_vimrc; /* call vimrc_found() when file exists */
2751{
2752 struct source_cookie cookie;
2753 char_u *save_sourcing_name;
2754 linenr_T save_sourcing_lnum;
2755 char_u *p;
2756 char_u *fname_exp;
2757 int retval = FAIL;
2758#ifdef FEAT_EVAL
2759 scid_T save_current_SID;
2760 static scid_T last_current_SID = 0;
2761 void *save_funccalp;
2762 int save_debug_break_level = debug_break_level;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002763 scriptitem_T *si = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764# ifdef UNIX
2765 struct stat st;
2766 int stat_ok;
2767# endif
2768#endif
2769#ifdef STARTUPTIME
2770 struct timeval tv_rel;
2771 struct timeval tv_start;
2772#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +00002773#ifdef FEAT_PROFILE
2774 proftime_T wait_start;
2775#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776
2777#ifdef RISCOS
2778 p = mch_munge_fname(fname);
2779#else
2780 p = expand_env_save(fname);
2781#endif
2782 if (p == NULL)
2783 return retval;
2784 fname_exp = fix_fname(p);
2785 vim_free(p);
2786 if (fname_exp == NULL)
2787 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 if (mch_isdir(fname_exp))
2789 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00002790 smsg((char_u *)_("Cannot source a directory: \"%s\""), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 goto theend;
2792 }
2793
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00002794#ifdef FEAT_AUTOCMD
2795 apply_autocmds(EVENT_SOURCEPRE, fname_exp, fname_exp, FALSE, curbuf);
2796#endif
2797
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798#if defined(WIN32) && defined(FEAT_CSCOPE)
2799 cookie.fp = fopen_noinh_readbin((char *)fname_exp);
2800#else
2801 cookie.fp = mch_fopen((char *)fname_exp, READBIN);
2802#endif
2803 if (cookie.fp == NULL && check_other)
2804 {
2805 /*
2806 * Try again, replacing file name ".vimrc" by "_vimrc" or vice versa,
2807 * and ".exrc" by "_exrc" or vice versa.
2808 */
2809 p = gettail(fname_exp);
2810 if ((*p == '.' || *p == '_')
2811 && (STRICMP(p + 1, "vimrc") == 0
2812 || STRICMP(p + 1, "gvimrc") == 0
2813 || STRICMP(p + 1, "exrc") == 0))
2814 {
2815 if (*p == '_')
2816 *p = '.';
2817 else
2818 *p = '_';
2819#if defined(WIN32) && defined(FEAT_CSCOPE)
2820 cookie.fp = fopen_noinh_readbin((char *)fname_exp);
2821#else
2822 cookie.fp = mch_fopen((char *)fname_exp, READBIN);
2823#endif
2824 }
2825 }
2826
2827 if (cookie.fp == NULL)
2828 {
2829 if (p_verbose > 0)
2830 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +00002831 verbose_enter();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832 if (sourcing_name == NULL)
Bram Moolenaar555b2802005-05-19 21:08:39 +00002833 smsg((char_u *)_("could not source \"%s\""), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 else
2835 smsg((char_u *)_("line %ld: could not source \"%s\""),
Bram Moolenaar555b2802005-05-19 21:08:39 +00002836 sourcing_lnum, fname);
Bram Moolenaar54ee7752005-05-31 22:22:17 +00002837 verbose_leave();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838 }
2839 goto theend;
2840 }
2841
2842 /*
2843 * The file exists.
2844 * - In verbose mode, give a message.
2845 * - For a vimrc file, may want to set 'compatible', call vimrc_found().
2846 */
2847 if (p_verbose > 1)
2848 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +00002849 verbose_enter();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 if (sourcing_name == NULL)
Bram Moolenaar555b2802005-05-19 21:08:39 +00002851 smsg((char_u *)_("sourcing \"%s\""), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 else
2853 smsg((char_u *)_("line %ld: sourcing \"%s\""),
Bram Moolenaar555b2802005-05-19 21:08:39 +00002854 sourcing_lnum, fname);
Bram Moolenaar54ee7752005-05-31 22:22:17 +00002855 verbose_leave();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 }
2857 if (is_vimrc)
2858 vimrc_found();
2859
2860#ifdef USE_CRNL
2861 /* If no automatic file format: Set default to CR-NL. */
2862 if (*p_ffs == NUL)
2863 cookie.fileformat = EOL_DOS;
2864 else
2865 cookie.fileformat = EOL_UNKNOWN;
2866 cookie.error = FALSE;
2867#endif
2868
2869#ifdef USE_CR
2870 /* If no automatic file format: Set default to CR. */
2871 if (*p_ffs == NUL)
2872 cookie.fileformat = EOL_MAC;
2873 else
2874 cookie.fileformat = EOL_UNKNOWN;
2875 cookie.error = FALSE;
2876#endif
2877
2878 cookie.nextline = NULL;
2879 cookie.finished = FALSE;
2880
2881#ifdef FEAT_EVAL
2882 /*
2883 * Check if this script has a breakpoint.
2884 */
2885 cookie.breakpoint = dbg_find_breakpoint(TRUE, fname_exp, (linenr_T)0);
2886 cookie.fname = fname_exp;
2887 cookie.dbg_tick = debug_tick;
2888
2889 cookie.level = ex_nesting_level;
2890#endif
2891#ifdef FEAT_MBYTE
2892 cookie.conv.vc_type = CONV_NONE; /* no conversion */
2893
2894 /* Try reading the first few bytes to check for a UTF-8 BOM. */
2895 {
2896 char_u buf[3];
2897
2898 if (fread((char *)buf, sizeof(char_u), (size_t)3, cookie.fp)
2899 == (size_t)3
2900 && buf[0] == 0xef && buf[1] == 0xbb && buf[2] == 0xbf)
2901 /* Found BOM, setup conversion and skip over it. */
2902 convert_setup(&cookie.conv, (char_u *)"utf-8", p_enc);
2903 else
2904 /* No BOM found, rewind. */
2905 fseek(cookie.fp, 0L, SEEK_SET);
2906 }
2907#endif
2908
2909 /*
2910 * Keep the sourcing name/lnum, for recursive calls.
2911 */
2912 save_sourcing_name = sourcing_name;
2913 sourcing_name = fname_exp;
2914 save_sourcing_lnum = sourcing_lnum;
2915 sourcing_lnum = 0;
2916
2917#ifdef STARTUPTIME
2918 time_push(&tv_rel, &tv_start);
2919#endif
2920
2921#ifdef FEAT_EVAL
Bram Moolenaar05159a02005-02-26 23:04:13 +00002922# ifdef FEAT_PROFILE
2923 if (do_profiling)
2924 prof_child_enter(&wait_start); /* entering a child now */
2925# endif
2926
2927 /* Don't use local function variables, if called from a function.
2928 * Also starts profiling timer for nested script. */
2929 save_funccalp = save_funccal();
2930
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 /*
2932 * Check if this script was sourced before to finds its SID.
2933 * If it's new, generate a new SID.
2934 */
2935 save_current_SID = current_SID;
2936# ifdef UNIX
2937 stat_ok = (mch_stat((char *)fname_exp, &st) >= 0);
2938# endif
Bram Moolenaar05159a02005-02-26 23:04:13 +00002939 for (current_SID = script_items.ga_len; current_SID > 0; --current_SID)
2940 {
2941 si = &SCRIPT_ITEM(current_SID);
2942 if (si->sn_name != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 && (
2944# ifdef UNIX
Bram Moolenaar7c626922005-02-07 22:01:03 +00002945 /* Compare dev/ino when possible, it catches symbolic
2946 * links. Also compare file names, the inode may change
2947 * when the file was edited. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00002948 ((stat_ok && si->sn_dev != -1)
2949 && (si->sn_dev == st.st_dev
2950 && si->sn_ino == st.st_ino)) ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951# endif
Bram Moolenaar05159a02005-02-26 23:04:13 +00002952 fnamecmp(si->sn_name, fname_exp) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002954 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 if (current_SID == 0)
2956 {
2957 current_SID = ++last_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002958 if (ga_grow(&script_items, (int)(current_SID - script_items.ga_len))
2959 == FAIL)
2960 goto almosttheend;
2961 while (script_items.ga_len < current_SID)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00002963 ++script_items.ga_len;
2964 SCRIPT_ITEM(script_items.ga_len).sn_name = NULL;
2965# ifdef FEAT_PROFILE
2966 SCRIPT_ITEM(script_items.ga_len).sn_prof_on = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00002969 si = &SCRIPT_ITEM(current_SID);
2970 si->sn_name = fname_exp;
2971 fname_exp = NULL;
2972# ifdef UNIX
2973 if (stat_ok)
2974 {
2975 si->sn_dev = st.st_dev;
2976 si->sn_ino = st.st_ino;
2977 }
2978 else
2979 si->sn_dev = -1;
2980# endif
2981
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 /* Allocate the local script variables to use for this script. */
2983 new_script_vars(current_SID);
2984 }
2985
Bram Moolenaar05159a02005-02-26 23:04:13 +00002986# ifdef FEAT_PROFILE
2987 if (do_profiling)
2988 {
2989 int forceit;
2990
2991 /* Check if we do profiling for this script. */
2992 if (!si->sn_prof_on && has_profiling(TRUE, si->sn_name, &forceit))
2993 {
2994 script_do_profile(si);
2995 si->sn_pr_force = forceit;
2996 }
2997 if (si->sn_prof_on)
2998 {
2999 ++si->sn_pr_count;
3000 profile_start(&si->sn_pr_start);
3001 profile_zero(&si->sn_pr_children);
3002 }
3003 }
3004# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005#endif
3006
3007 /*
3008 * Call do_cmdline, which will call getsourceline() to get the lines.
3009 */
3010 do_cmdline(NULL, getsourceline, (void *)&cookie,
3011 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_REPEAT);
3012
3013 retval = OK;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003014
3015#ifdef FEAT_PROFILE
3016 if (do_profiling)
3017 {
3018 /* Get "si" again, "script_items" may have been reallocated. */
3019 si = &SCRIPT_ITEM(current_SID);
3020 if (si->sn_prof_on)
3021 {
3022 profile_end(&si->sn_pr_start);
3023 profile_sub_wait(&wait_start, &si->sn_pr_start);
3024 profile_add(&si->sn_pr_total, &si->sn_pr_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +00003025 profile_self(&si->sn_pr_self, &si->sn_pr_start,
3026 &si->sn_pr_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003027 }
3028 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029#endif
3030
3031 if (got_int)
3032 EMSG(_(e_interr));
3033 sourcing_name = save_sourcing_name;
3034 sourcing_lnum = save_sourcing_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 if (p_verbose > 1)
3036 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +00003037 verbose_enter();
Bram Moolenaar555b2802005-05-19 21:08:39 +00003038 smsg((char_u *)_("finished sourcing %s"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039 if (sourcing_name != NULL)
Bram Moolenaar555b2802005-05-19 21:08:39 +00003040 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar54ee7752005-05-31 22:22:17 +00003041 verbose_leave();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 }
3043#ifdef STARTUPTIME
Bram Moolenaar555b2802005-05-19 21:08:39 +00003044 vim_snprintf(IObuff, IOSIZE, "sourcing %s", fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 time_msg(IObuff, &tv_start);
3046 time_pop(&tv_rel);
3047#endif
3048
3049#ifdef FEAT_EVAL
3050 /*
3051 * After a "finish" in debug mode, need to break at first command of next
3052 * sourced file.
3053 */
3054 if (save_debug_break_level > ex_nesting_level
3055 && debug_break_level == ex_nesting_level)
3056 ++debug_break_level;
3057#endif
3058
Bram Moolenaar05159a02005-02-26 23:04:13 +00003059#ifdef FEAT_EVAL
3060almosttheend:
3061 current_SID = save_current_SID;
3062 restore_funccal(save_funccalp);
3063# ifdef FEAT_PROFILE
3064 if (do_profiling)
3065 prof_child_exit(&wait_start); /* leaving a child now */
3066# endif
3067#endif
3068 fclose(cookie.fp);
3069 vim_free(cookie.nextline);
3070#ifdef FEAT_MBYTE
3071 convert_setup(&cookie.conv, NULL, NULL);
3072#endif
3073
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074theend:
3075 vim_free(fname_exp);
3076 return retval;
3077}
3078
3079#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00003080
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081/*
3082 * ":scriptnames"
3083 */
3084/*ARGSUSED*/
3085 void
3086ex_scriptnames(eap)
3087 exarg_T *eap;
3088{
3089 int i;
3090
Bram Moolenaar05159a02005-02-26 23:04:13 +00003091 for (i = 1; i <= script_items.ga_len && !got_int; ++i)
3092 if (SCRIPT_ITEM(i).sn_name != NULL)
3093 smsg((char_u *)"%3d: %s", i, SCRIPT_ITEM(i).sn_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094}
3095
3096# if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
3097/*
3098 * Fix slashes in the list of script names for 'shellslash'.
3099 */
3100 void
3101scriptnames_slash_adjust()
3102{
3103 int i;
3104
Bram Moolenaar05159a02005-02-26 23:04:13 +00003105 for (i = 1; i <= script_items.ga_len; ++i)
3106 if (SCRIPT_ITEM(i).sn_name != NULL)
3107 slash_adjust(SCRIPT_ITEM(i).sn_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108}
3109# endif
3110
3111/*
3112 * Get a pointer to a script name. Used for ":verbose set".
3113 */
3114 char_u *
3115get_scriptname(id)
3116 scid_T id;
3117{
3118 if (id == SID_MODELINE)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003119 return (char_u *)_("modeline");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 if (id == SID_CMDARG)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003121 return (char_u *)_("--cmd argument");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122 if (id == SID_CARG)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003123 return (char_u *)_("-c argument");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 if (id == SID_ENV)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003125 return (char_u *)_("environment variable");
3126 if (id == SID_ERROR)
3127 return (char_u *)_("error handler");
Bram Moolenaar05159a02005-02-26 23:04:13 +00003128 return SCRIPT_ITEM(id).sn_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129}
Bram Moolenaar05159a02005-02-26 23:04:13 +00003130
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00003131# if defined(EXITFREE) || defined(PROTO)
3132 void
3133free_scriptnames()
3134{
3135 int i;
3136
3137 for (i = script_items.ga_len; i > 0; --i)
3138 vim_free(SCRIPT_ITEM(i).sn_name);
3139 ga_clear(&script_items);
3140}
3141# endif
3142
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143#endif
3144
3145#if defined(USE_CR) || defined(PROTO)
3146
3147# if defined(__MSL__) && (__MSL__ >= 22)
3148/*
3149 * Newer version of the Metrowerks library handle DOS and UNIX files
3150 * without help.
3151 * Test with earlier versions, MSL 2.2 is the library supplied with
3152 * Codewarrior Pro 2.
3153 */
3154 char *
3155fgets_cr(s, n, stream)
3156 char *s;
3157 int n;
3158 FILE *stream;
3159{
3160 return fgets(s, n, stream);
3161}
3162# else
3163/*
3164 * Version of fgets() which also works for lines ending in a <CR> only
3165 * (Macintosh format).
3166 * For older versions of the Metrowerks library.
3167 * At least CodeWarrior 9 needed this code.
3168 */
3169 char *
3170fgets_cr(s, n, stream)
3171 char *s;
3172 int n;
3173 FILE *stream;
3174{
3175 int c = 0;
3176 int char_read = 0;
3177
3178 while (!feof(stream) && c != '\r' && c != '\n' && char_read < n - 1)
3179 {
3180 c = fgetc(stream);
3181 s[char_read++] = c;
3182 /* If the file is in DOS format, we need to skip a NL after a CR. I
3183 * thought it was the other way around, but this appears to work... */
3184 if (c == '\n')
3185 {
3186 c = fgetc(stream);
3187 if (c != '\r')
3188 ungetc(c, stream);
3189 }
3190 }
3191
3192 s[char_read] = 0;
3193 if (char_read == 0)
3194 return NULL;
3195
3196 if (feof(stream) && char_read == 1)
3197 return NULL;
3198
3199 return s;
3200}
3201# endif
3202#endif
3203
3204/*
3205 * Get one full line from a sourced file.
3206 * Called by do_cmdline() when it's called from do_source().
3207 *
3208 * Return a pointer to the line in allocated memory.
3209 * Return NULL for end-of-file or some error.
3210 */
3211/* ARGSUSED */
3212 char_u *
3213getsourceline(c, cookie, indent)
3214 int c; /* not used */
3215 void *cookie;
3216 int indent; /* not used */
3217{
3218 struct source_cookie *sp = (struct source_cookie *)cookie;
3219 char_u *line;
3220 char_u *p, *s;
3221
3222#ifdef FEAT_EVAL
3223 /* If breakpoints have been added/deleted need to check for it. */
3224 if (sp->dbg_tick < debug_tick)
3225 {
3226 sp->breakpoint = dbg_find_breakpoint(TRUE, sp->fname, sourcing_lnum);
3227 sp->dbg_tick = debug_tick;
3228 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003229# ifdef FEAT_PROFILE
3230 if (do_profiling)
3231 script_line_end();
3232# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233#endif
3234 /*
3235 * Get current line. If there is a read-ahead line, use it, otherwise get
3236 * one now.
3237 */
3238 if (sp->finished)
3239 line = NULL;
3240 else if (sp->nextline == NULL)
3241 line = get_one_sourceline(sp);
3242 else
3243 {
3244 line = sp->nextline;
3245 sp->nextline = NULL;
3246 ++sourcing_lnum;
3247 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003248#ifdef FEAT_PROFILE
3249 if (line != NULL && do_profiling)
3250 script_line_start();
3251#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252
3253 /* Only concatenate lines starting with a \ when 'cpoptions' doesn't
3254 * contain the 'C' flag. */
3255 if (line != NULL && (vim_strchr(p_cpo, CPO_CONCAT) == NULL))
3256 {
3257 /* compensate for the one line read-ahead */
3258 --sourcing_lnum;
3259 for (;;)
3260 {
3261 sp->nextline = get_one_sourceline(sp);
3262 if (sp->nextline == NULL)
3263 break;
3264 p = skipwhite(sp->nextline);
3265 if (*p != '\\')
3266 break;
3267 s = alloc((int)(STRLEN(line) + STRLEN(p)));
3268 if (s == NULL) /* out of memory */
3269 break;
3270 STRCPY(s, line);
3271 STRCAT(s, p + 1);
3272 vim_free(line);
3273 line = s;
3274 vim_free(sp->nextline);
3275 }
3276 }
3277
3278#ifdef FEAT_MBYTE
3279 if (line != NULL && sp->conv.vc_type != CONV_NONE)
3280 {
3281 /* Convert the encoding of the script line. */
3282 s = string_convert(&sp->conv, line, NULL);
3283 if (s != NULL)
3284 {
3285 vim_free(line);
3286 line = s;
3287 }
3288 }
3289#endif
3290
3291#ifdef FEAT_EVAL
3292 /* Did we encounter a breakpoint? */
3293 if (sp->breakpoint != 0 && sp->breakpoint <= sourcing_lnum)
3294 {
3295 dbg_breakpoint(sp->fname, sourcing_lnum);
3296 /* Find next breakpoint. */
3297 sp->breakpoint = dbg_find_breakpoint(TRUE, sp->fname, sourcing_lnum);
3298 sp->dbg_tick = debug_tick;
3299 }
3300#endif
3301
3302 return line;
3303}
3304
3305 static char_u *
3306get_one_sourceline(sp)
3307 struct source_cookie *sp;
3308{
3309 garray_T ga;
3310 int len;
3311 int c;
3312 char_u *buf;
3313#ifdef USE_CRNL
3314 int has_cr; /* CR-LF found */
3315#endif
3316#ifdef USE_CR
3317 char_u *scan;
3318#endif
3319 int have_read = FALSE;
3320
3321 /* use a growarray to store the sourced line */
Bram Moolenaar6ac54292005-02-02 23:07:25 +00003322 ga_init2(&ga, 1, 250);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323
3324 /*
3325 * Loop until there is a finished line (or end-of-file).
3326 */
3327 sourcing_lnum++;
3328 for (;;)
3329 {
Bram Moolenaar6ac54292005-02-02 23:07:25 +00003330 /* make room to read at least 120 (more) characters */
3331 if (ga_grow(&ga, 120) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332 break;
3333 buf = (char_u *)ga.ga_data;
3334
3335#ifdef USE_CR
3336 if (sp->fileformat == EOL_MAC)
3337 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00003338 if (fgets_cr((char *)buf + ga.ga_len, ga.ga_maxlen - ga.ga_len,
3339 sp->fp) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 break;
3341 }
3342 else
3343#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00003344 if (fgets((char *)buf + ga.ga_len, ga.ga_maxlen - ga.ga_len,
3345 sp->fp) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 break;
Bram Moolenaar6ac54292005-02-02 23:07:25 +00003347 len = ga.ga_len + (int)STRLEN(buf + ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348#ifdef USE_CRNL
3349 /* Ignore a trailing CTRL-Z, when in Dos mode. Only recognize the
3350 * CTRL-Z by its own, or after a NL. */
3351 if ( (len == 1 || (len >= 2 && buf[len - 2] == '\n'))
3352 && sp->fileformat == EOL_DOS
3353 && buf[len - 1] == Ctrl_Z)
3354 {
3355 buf[len - 1] = NUL;
3356 break;
3357 }
3358#endif
3359
3360#ifdef USE_CR
3361 /* If the read doesn't stop on a new line, and there's
3362 * some CR then we assume a Mac format */
3363 if (sp->fileformat == EOL_UNKNOWN)
3364 {
3365 if (buf[len - 1] != '\n' && vim_strchr(buf, '\r') != NULL)
3366 sp->fileformat = EOL_MAC;
3367 else
3368 sp->fileformat = EOL_UNIX;
3369 }
3370
3371 if (sp->fileformat == EOL_MAC)
3372 {
3373 scan = vim_strchr(buf, '\r');
3374
3375 if (scan != NULL)
3376 {
3377 *scan = '\n';
3378 if (*(scan + 1) != 0)
3379 {
3380 *(scan + 1) = 0;
3381 fseek(sp->fp, (long)(scan - buf - len + 1), SEEK_CUR);
3382 }
3383 }
3384 len = STRLEN(buf);
3385 }
3386#endif
3387
3388 have_read = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 ga.ga_len = len;
3390
3391 /* If the line was longer than the buffer, read more. */
Bram Moolenaar86b68352004-12-27 21:59:20 +00003392 if (ga.ga_maxlen - ga.ga_len == 1 && buf[len - 1] != '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 continue;
3394
3395 if (len >= 1 && buf[len - 1] == '\n') /* remove trailing NL */
3396 {
3397#ifdef USE_CRNL
3398 has_cr = (len >= 2 && buf[len - 2] == '\r');
3399 if (sp->fileformat == EOL_UNKNOWN)
3400 {
3401 if (has_cr)
3402 sp->fileformat = EOL_DOS;
3403 else
3404 sp->fileformat = EOL_UNIX;
3405 }
3406
3407 if (sp->fileformat == EOL_DOS)
3408 {
3409 if (has_cr) /* replace trailing CR */
3410 {
3411 buf[len - 2] = '\n';
3412 --len;
3413 --ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 }
3415 else /* lines like ":map xx yy^M" will have failed */
3416 {
3417 if (!sp->error)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003418 {
3419 msg_source(hl_attr(HLF_W));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 EMSG(_("W15: Warning: Wrong line separator, ^M may be missing"));
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003421 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 sp->error = TRUE;
3423 sp->fileformat = EOL_UNIX;
3424 }
3425 }
3426#endif
3427 /* The '\n' is escaped if there is an odd number of ^V's just
3428 * before it, first set "c" just before the 'V's and then check
3429 * len&c parities (is faster than ((len-c)%2 == 0)) -- Acevedo */
3430 for (c = len - 2; c >= 0 && buf[c] == Ctrl_V; c--)
3431 ;
3432 if ((len & 1) != (c & 1)) /* escaped NL, read more */
3433 {
3434 sourcing_lnum++;
3435 continue;
3436 }
3437
3438 buf[len - 1] = NUL; /* remove the NL */
3439 }
3440
3441 /*
3442 * Check for ^C here now and then, so recursive :so can be broken.
3443 */
3444 line_breakcheck();
3445 break;
3446 }
3447
3448 if (have_read)
3449 return (char_u *)ga.ga_data;
3450
3451 vim_free(ga.ga_data);
3452 return NULL;
3453}
3454
Bram Moolenaar05159a02005-02-26 23:04:13 +00003455#if defined(FEAT_PROFILE) || defined(PROTO)
3456/*
3457 * Called when starting to read a script line.
3458 * "sourcing_lnum" must be correct!
3459 * When skipping lines it may not actually be executed, but we won't find out
3460 * until later and we need to store the time now.
3461 */
3462 void
3463script_line_start()
3464{
3465 scriptitem_T *si;
3466 sn_prl_T *pp;
3467
3468 if (current_SID <= 0 || current_SID > script_items.ga_len)
3469 return;
3470 si = &SCRIPT_ITEM(current_SID);
3471 if (si->sn_prof_on && sourcing_lnum >= 1)
3472 {
3473 /* Grow the array before starting the timer, so that the time spend
3474 * here isn't counted. */
3475 ga_grow(&si->sn_prl_ga, (int)(sourcing_lnum - si->sn_prl_ga.ga_len));
3476 si->sn_prl_idx = sourcing_lnum - 1;
3477 while (si->sn_prl_ga.ga_len <= si->sn_prl_idx
3478 && si->sn_prl_ga.ga_len < si->sn_prl_ga.ga_maxlen)
3479 {
3480 /* Zero counters for a line that was not used before. */
3481 pp = &PRL_ITEM(si, si->sn_prl_ga.ga_len);
3482 pp->snp_count = 0;
3483 profile_zero(&pp->sn_prl_total);
3484 profile_zero(&pp->sn_prl_self);
3485 ++si->sn_prl_ga.ga_len;
3486 }
3487 si->sn_prl_execed = FALSE;
3488 profile_start(&si->sn_prl_start);
3489 profile_zero(&si->sn_prl_children);
3490 profile_get_wait(&si->sn_prl_wait);
3491 }
3492}
3493
3494/*
3495 * Called when actually executing a function line.
3496 */
3497 void
3498script_line_exec()
3499{
3500 scriptitem_T *si;
3501
3502 if (current_SID <= 0 || current_SID > script_items.ga_len)
3503 return;
3504 si = &SCRIPT_ITEM(current_SID);
3505 if (si->sn_prof_on && si->sn_prl_idx >= 0)
3506 si->sn_prl_execed = TRUE;
3507}
3508
3509/*
3510 * Called when done with a function line.
3511 */
3512 void
3513script_line_end()
3514{
3515 scriptitem_T *si;
3516 sn_prl_T *pp;
3517
3518 if (current_SID <= 0 || current_SID > script_items.ga_len)
3519 return;
3520 si = &SCRIPT_ITEM(current_SID);
3521 if (si->sn_prof_on && si->sn_prl_idx >= 0
3522 && si->sn_prl_idx < si->sn_prl_ga.ga_len)
3523 {
3524 if (si->sn_prl_execed)
3525 {
3526 pp = &PRL_ITEM(si, si->sn_prl_idx);
3527 ++pp->snp_count;
3528 profile_end(&si->sn_prl_start);
3529 profile_sub_wait(&si->sn_prl_wait, &si->sn_prl_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003530 profile_add(&pp->sn_prl_total, &si->sn_prl_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +00003531 profile_self(&pp->sn_prl_self, &si->sn_prl_start,
3532 &si->sn_prl_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003533 }
3534 si->sn_prl_idx = -1;
3535 }
3536}
3537#endif
3538
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539/*
3540 * ":scriptencoding": Set encoding conversion for a sourced script.
3541 * Without the multi-byte feature it's simply ignored.
3542 */
3543/*ARGSUSED*/
3544 void
3545ex_scriptencoding(eap)
3546 exarg_T *eap;
3547{
3548#ifdef FEAT_MBYTE
3549 struct source_cookie *sp;
3550 char_u *name;
3551
3552 if (!getline_equal(eap->getline, eap->cookie, getsourceline))
3553 {
3554 EMSG(_("E167: :scriptencoding used outside of a sourced file"));
3555 return;
3556 }
3557
3558 if (*eap->arg != NUL)
3559 {
3560 name = enc_canonize(eap->arg);
3561 if (name == NULL) /* out of memory */
3562 return;
3563 }
3564 else
3565 name = eap->arg;
3566
3567 /* Setup for conversion from the specified encoding to 'encoding'. */
3568 sp = (struct source_cookie *)getline_cookie(eap->getline, eap->cookie);
3569 convert_setup(&sp->conv, name, p_enc);
3570
3571 if (name != eap->arg)
3572 vim_free(name);
3573#endif
3574}
3575
3576#if defined(FEAT_EVAL) || defined(PROTO)
3577/*
3578 * ":finish": Mark a sourced file as finished.
3579 */
3580 void
3581ex_finish(eap)
3582 exarg_T *eap;
3583{
3584 if (getline_equal(eap->getline, eap->cookie, getsourceline))
3585 do_finish(eap, FALSE);
3586 else
3587 EMSG(_("E168: :finish used outside of a sourced file"));
3588}
3589
3590/*
3591 * Mark a sourced file as finished. Possibly makes the ":finish" pending.
3592 * Also called for a pending finish at the ":endtry" or after returning from
3593 * an extra do_cmdline(). "reanimate" is used in the latter case.
3594 */
3595 void
3596do_finish(eap, reanimate)
3597 exarg_T *eap;
3598 int reanimate;
3599{
3600 int idx;
3601
3602 if (reanimate)
3603 ((struct source_cookie *)getline_cookie(eap->getline,
3604 eap->cookie))->finished = FALSE;
3605
3606 /*
3607 * Cleanup (and inactivate) conditionals, but stop when a try conditional
3608 * not in its finally clause (which then is to be executed next) is found.
3609 * In this case, make the ":finish" pending for execution at the ":endtry".
3610 * Otherwise, finish normally.
3611 */
3612 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
3613 if (idx >= 0)
3614 {
3615 eap->cstack->cs_pending[idx] = CSTP_FINISH;
3616 report_make_pending(CSTP_FINISH, NULL);
3617 }
3618 else
3619 ((struct source_cookie *)getline_cookie(eap->getline,
3620 eap->cookie))->finished = TRUE;
3621}
3622
3623
3624/*
3625 * Return TRUE when a sourced file had the ":finish" command: Don't give error
3626 * message for missing ":endif".
3627 * Return FALSE when not sourcing a file.
3628 */
3629 int
3630source_finished(getline, cookie)
3631 char_u *(*getline) __ARGS((int, void *, int));
3632 void *cookie;
3633{
3634 return (getline_equal(getline, cookie, getsourceline)
3635 && ((struct source_cookie *)getline_cookie(
3636 getline, cookie))->finished);
3637}
3638#endif
3639
3640#if defined(FEAT_LISTCMDS) || defined(PROTO)
3641/*
3642 * ":checktime [buffer]"
3643 */
3644 void
3645ex_checktime(eap)
3646 exarg_T *eap;
3647{
3648 buf_T *buf;
3649 int save_no_check_timestamps = no_check_timestamps;
3650
3651 no_check_timestamps = 0;
3652 if (eap->addr_count == 0) /* default is all buffers */
3653 check_timestamps(FALSE);
3654 else
3655 {
3656 buf = buflist_findnr((int)eap->line2);
3657 if (buf != NULL) /* cannot happen? */
3658 (void)buf_check_timestamp(buf, FALSE);
3659 }
3660 no_check_timestamps = save_no_check_timestamps;
3661}
3662#endif
3663
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
3665 && (defined(FEAT_EVAL) || defined(FEAT_MULTI_LANG))
3666static char *get_locale_val __ARGS((int what));
3667
3668 static char *
3669get_locale_val(what)
3670 int what;
3671{
3672 char *loc;
3673
3674 /* Obtain the locale value from the libraries. For DJGPP this is
3675 * redefined and it doesn't use the arguments. */
3676 loc = setlocale(what, NULL);
3677
3678# if defined(__BORLANDC__)
3679 if (loc != NULL)
3680 {
3681 char_u *p;
3682
3683 /* Borland returns something like "LC_CTYPE=<name>\n"
3684 * Let's try to fix that bug here... */
3685 p = vim_strchr(loc, '=');
3686 if (p != NULL)
3687 {
3688 loc = ++p;
3689 while (*p != NUL) /* remove trailing newline */
3690 {
3691 if (*p < ' ')
3692 {
3693 *p = NUL;
3694 break;
3695 }
3696 ++p;
3697 }
3698 }
3699 }
3700# endif
3701
3702 return loc;
3703}
3704#endif
3705
3706
3707#ifdef WIN32
3708/*
3709 * On MS-Windows locale names are strings like "German_Germany.1252", but
3710 * gettext expects "de". Try to translate one into another here for a few
3711 * supported languages.
3712 */
3713 static char_u *
3714gettext_lang(char_u *name)
3715{
3716 int i;
3717 static char *(mtable[]) = {
3718 "afrikaans", "af",
3719 "czech", "cs",
3720 "dutch", "nl",
3721 "german", "de",
3722 "english_united kingdom", "en_GB",
3723 "spanish", "es",
3724 "french", "fr",
3725 "italian", "it",
3726 "japanese", "ja",
3727 "korean", "ko",
3728 "norwegian", "no",
3729 "polish", "pl",
3730 "russian", "ru",
3731 "slovak", "sk",
3732 "swedish", "sv",
3733 "ukrainian", "uk",
3734 "chinese_china", "zh_CN",
3735 "chinese_taiwan", "zh_TW",
3736 NULL};
3737
3738 for (i = 0; mtable[i] != NULL; i += 2)
3739 if (STRNICMP(mtable[i], name, STRLEN(mtable[i])) == 0)
3740 return mtable[i + 1];
3741 return name;
3742}
3743#endif
3744
3745#if defined(FEAT_MULTI_LANG) || defined(PROTO)
3746/*
3747 * Obtain the current messages language. Used to set the default for
3748 * 'helplang'. May return NULL or an empty string.
3749 */
3750 char_u *
3751get_mess_lang()
3752{
3753 char_u *p;
3754
3755# if (defined(HAVE_LOCALE_H) || defined(X_LOCALE))
3756# if defined(LC_MESSAGES)
3757 p = (char_u *)get_locale_val(LC_MESSAGES);
3758# else
3759 /* This is necessary for Win32, where LC_MESSAGES is not defined and $LANG
3760 * may be set to the LCID number. */
3761 p = (char_u *)get_locale_val(LC_ALL);
3762# endif
3763# else
3764 p = mch_getenv((char_u *)"LC_ALL");
3765 if (p == NULL || *p == NUL)
3766 {
3767 p = mch_getenv((char_u *)"LC_MESSAGES");
3768 if (p == NULL || *p == NUL)
3769 p = mch_getenv((char_u *)"LANG");
3770 }
3771# endif
3772# ifdef WIN32
3773 p = gettext_lang(p);
3774# endif
3775 return p;
3776}
3777#endif
3778
Bram Moolenaardef9e822004-12-31 20:58:58 +00003779/* Complicated #if; matches with where get_mess_env() is used below. */
3780#if (defined(FEAT_EVAL) && !((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
3781 && defined(LC_MESSAGES))) \
3782 || ((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
3783 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)) \
3784 && !defined(LC_MESSAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785static char_u *get_mess_env __ARGS((void));
3786
3787/*
3788 * Get the language used for messages from the environment.
3789 */
3790 static char_u *
3791get_mess_env()
3792{
3793 char_u *p;
3794
3795 p = mch_getenv((char_u *)"LC_ALL");
3796 if (p == NULL || *p == NUL)
3797 {
3798 p = mch_getenv((char_u *)"LC_MESSAGES");
3799 if (p == NULL || *p == NUL)
3800 {
3801 p = mch_getenv((char_u *)"LANG");
3802 if (p != NULL && VIM_ISDIGIT(*p))
3803 p = NULL; /* ignore something like "1043" */
3804# if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
3805 if (p == NULL || *p == NUL)
3806 p = (char_u *)get_locale_val(LC_CTYPE);
3807# endif
3808 }
3809 }
3810 return p;
3811}
3812#endif
3813
3814#if defined(FEAT_EVAL) || defined(PROTO)
3815
3816/*
3817 * Set the "v:lang" variable according to the current locale setting.
3818 * Also do "v:lc_time"and "v:ctype".
3819 */
3820 void
3821set_lang_var()
3822{
3823 char_u *loc;
3824
3825# if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
3826 loc = (char_u *)get_locale_val(LC_CTYPE);
3827# else
3828 /* setlocale() not supported: use the default value */
3829 loc = (char_u *)"C";
3830# endif
3831 set_vim_var_string(VV_CTYPE, loc, -1);
3832
3833 /* When LC_MESSAGES isn't defined use the value from $LC_MESSAGES, fall
3834 * back to LC_CTYPE if it's empty. */
3835# if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) && defined(LC_MESSAGES)
3836 loc = (char_u *)get_locale_val(LC_MESSAGES);
3837# else
3838 loc = get_mess_env();
3839# endif
3840 set_vim_var_string(VV_LANG, loc, -1);
3841
3842# if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
3843 loc = (char_u *)get_locale_val(LC_TIME);
3844# endif
3845 set_vim_var_string(VV_LC_TIME, loc, -1);
3846}
3847#endif
3848
3849#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
3850 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
3851/*
3852 * ":language": Set the language (locale).
3853 */
3854 void
3855ex_language(eap)
3856 exarg_T *eap;
3857{
3858 char *loc;
3859 char_u *p;
3860 char_u *name;
3861 int what = LC_ALL;
3862 char *whatstr = "";
3863#ifdef LC_MESSAGES
3864# define VIM_LC_MESSAGES LC_MESSAGES
3865#else
3866# define VIM_LC_MESSAGES 6789
3867#endif
3868
3869 name = eap->arg;
3870
3871 /* Check for "messages {name}", "ctype {name}" or "time {name}" argument.
3872 * Allow abbreviation, but require at least 3 characters to avoid
3873 * confusion with a two letter language name "me" or "ct". */
3874 p = skiptowhite(eap->arg);
3875 if ((*p == NUL || vim_iswhite(*p)) && p - eap->arg >= 3)
3876 {
3877 if (STRNICMP(eap->arg, "messages", p - eap->arg) == 0)
3878 {
3879 what = VIM_LC_MESSAGES;
3880 name = skipwhite(p);
3881 whatstr = "messages ";
3882 }
3883 else if (STRNICMP(eap->arg, "ctype", p - eap->arg) == 0)
3884 {
3885 what = LC_CTYPE;
3886 name = skipwhite(p);
3887 whatstr = "ctype ";
3888 }
3889 else if (STRNICMP(eap->arg, "time", p - eap->arg) == 0)
3890 {
3891 what = LC_TIME;
3892 name = skipwhite(p);
3893 whatstr = "time ";
3894 }
3895 }
3896
3897 if (*name == NUL)
3898 {
3899#ifndef LC_MESSAGES
3900 if (what == VIM_LC_MESSAGES)
3901 p = get_mess_env();
3902 else
3903#endif
3904 p = (char_u *)setlocale(what, NULL);
3905 if (p == NULL || *p == NUL)
3906 p = (char_u *)"Unknown";
3907 smsg((char_u *)_("Current %slanguage: \"%s\""), whatstr, p);
3908 }
3909 else
3910 {
3911#ifndef LC_MESSAGES
3912 if (what == VIM_LC_MESSAGES)
3913 loc = "";
3914 else
3915#endif
3916 loc = setlocale(what, (char *)name);
3917 if (loc == NULL)
3918 EMSG2(_("E197: Cannot set language to \"%s\""), name);
3919 else
3920 {
3921#ifdef HAVE_NL_MSG_CAT_CNTR
3922 /* Need to do this for GNU gettext, otherwise cached translations
3923 * will be used again. */
3924 extern int _nl_msg_cat_cntr;
3925
3926 ++_nl_msg_cat_cntr;
3927#endif
3928 /* Reset $LC_ALL, otherwise it would overrule everyting. */
3929 vim_setenv((char_u *)"LC_ALL", (char_u *)"");
3930
3931 if (what != LC_TIME)
3932 {
3933 /* Tell gettext() what to translate to. It apparently doesn't
3934 * use the currently effective locale. Also do this when
3935 * FEAT_GETTEXT isn't defined, so that shell commands use this
3936 * value. */
3937 if (what == LC_ALL)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003938 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 vim_setenv((char_u *)"LANG", name);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003940# ifdef WIN32
3941 /* Apparently MS-Windows printf() may cause a crash when
3942 * we give it 8-bit text while it's expecting text in the
3943 * current locale. This call avoids that. */
3944 setlocale(LC_CTYPE, "C");
3945# endif
3946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 if (what != LC_CTYPE)
3948 {
3949 char_u *mname;
3950#ifdef WIN32
3951 mname = gettext_lang(name);
3952#else
3953 mname = name;
3954#endif
3955 vim_setenv((char_u *)"LC_MESSAGES", mname);
3956#ifdef FEAT_MULTI_LANG
3957 set_helplang_default(mname);
3958#endif
3959 }
3960
3961 /* Set $LC_CTYPE, because it overrules $LANG, and
3962 * gtk_set_locale() calls setlocale() again. gnome_init()
3963 * sets $LC_CTYPE to "en_US" (that's a bug!). */
3964 if (what != VIM_LC_MESSAGES)
3965 vim_setenv((char_u *)"LC_CTYPE", name);
3966# ifdef FEAT_GUI_GTK
3967 /* Let GTK know what locale we're using. Not sure this is
3968 * really needed... */
3969 if (gui.in_use)
3970 (void)gtk_set_locale();
3971# endif
3972 }
3973
3974# ifdef FEAT_EVAL
3975 /* Set v:lang, v:lc_time and v:ctype to the final result. */
3976 set_lang_var();
3977# endif
3978 }
3979 }
3980}
3981
3982# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3983/*
3984 * Function given to ExpandGeneric() to obtain the possible arguments of the
3985 * ":language" command.
3986 */
3987/*ARGSUSED*/
3988 char_u *
3989get_lang_arg(xp, idx)
3990 expand_T *xp;
3991 int idx;
3992{
3993 if (idx == 0)
3994 return (char_u *)"messages";
3995 if (idx == 1)
3996 return (char_u *)"ctype";
3997 if (idx == 2)
3998 return (char_u *)"time";
3999 return NULL;
4000}
4001# endif
4002
4003#endif