blob: 00dcea9ebfbec709f1a3984a8cf0a917208e986b [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/*
914 * Get the current waittime.
915 */
916 void
917profile_get_wait(tm)
918 proftime_T *tm;
919{
920 *tm = prof_wait_time;
921}
922
923/*
924 * Subtract the passed waittime since "tm" from "tma".
925 */
926 void
927profile_sub_wait(tm, tma)
928 proftime_T *tm, *tma;
929{
930 proftime_T tm3 = prof_wait_time;
931
932 profile_sub(&tm3, tm);
933 profile_sub(tma, &tm3);
934}
935
936/*
937 * Return TRUE if "tm1" and "tm2" are equal.
938 */
939 int
940profile_equal(tm1, tm2)
941 proftime_T *tm1, *tm2;
942{
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +0000943# ifdef WIN3264
944 return (tm1->QuadPart == tm2->QuadPart);
945# else
Bram Moolenaar05159a02005-02-26 23:04:13 +0000946 return (tm1->tv_usec == tm2->tv_usec && tm1->tv_sec == tm2->tv_sec);
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +0000947# endif
948}
949
950/*
951 * Return <0, 0 or >0 if "tm1" < "tm2", "tm1" == "tm2" or "tm1" > "tm2"
952 */
953 int
954profile_cmp(tm1, tm2)
955 proftime_T *tm1, *tm2;
956{
957# ifdef WIN3264
958 return (int)(tm2->QuadPart - tm1->QuadPart);
959# else
960 if (tm1->tv_sec == tm2->tv_sec)
961 return tm2->tv_usec - tm1->tv_usec;
962 return tm2->tv_sec - tm1->tv_sec;
963# endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000964}
965
966/*
967 * Return a string that represents a time.
968 * Uses a static buffer!
969 */
970 char *
971profile_msg(tm)
972 proftime_T *tm;
973{
974 static char buf[50];
975
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +0000976# ifdef WIN3264
977 LARGE_INTEGER fr;
978
979 QueryPerformanceFrequency(&fr);
980 sprintf(buf, "%10.6lf", (double)tm->QuadPart / (double)fr.QuadPart);
981# else
Bram Moolenaar05159a02005-02-26 23:04:13 +0000982 sprintf(buf, "%3ld.%06ld", (long)tm->tv_sec, (long)tm->tv_usec);
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +0000983#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000984 return buf;
985}
986
987static char_u *profile_fname = NULL;
988
989/*
990 * ":profile cmd args"
991 */
992 void
993ex_profile(eap)
994 exarg_T *eap;
995{
996 char_u *e;
997 int len;
998
999 e = skiptowhite(eap->arg);
1000 len = e - eap->arg;
1001 e = skipwhite(e);
1002
1003 if (len == 5 && STRNCMP(eap->arg, "start", 5) == 0 && *e != NUL)
1004 {
1005 vim_free(profile_fname);
1006 profile_fname = vim_strsave(e);
1007 do_profiling = TRUE;
1008 profile_zero(&prof_wait_time);
1009 set_vim_var_nr(VV_PROFILING, 1L);
1010 }
1011 else if (!do_profiling)
1012 EMSG(_("E750: First use :profile start <fname>"));
1013 else
1014 {
1015 /* The rest is similar to ":breakadd". */
1016 ex_breakadd(eap);
1017 }
1018}
1019
1020/*
1021 * Dump the profiling info.
1022 */
1023 void
1024profile_dump()
1025{
1026 FILE *fd;
1027
1028 if (profile_fname != NULL)
1029 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001030 fd = mch_fopen((char *)profile_fname, "w");
Bram Moolenaar05159a02005-02-26 23:04:13 +00001031 if (fd == NULL)
1032 EMSG2(_(e_notopen), profile_fname);
1033 else
1034 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00001035 script_dump_profile(fd);
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001036 func_dump_profile(fd);
Bram Moolenaar05159a02005-02-26 23:04:13 +00001037 fclose(fd);
1038 }
1039 }
1040}
1041
1042/*
1043 * Start profiling script "fp".
1044 */
1045 static void
1046script_do_profile(si)
1047 scriptitem_T *si;
1048{
1049 si->sn_pr_count = 0;
1050 profile_zero(&si->sn_pr_total);
1051 profile_zero(&si->sn_pr_self);
1052
1053 ga_init2(&si->sn_prl_ga, sizeof(sn_prl_T), 100);
1054 si->sn_prl_idx = -1;
1055 si->sn_prof_on = TRUE;
1056 si->sn_pr_nest = 0;
1057}
1058
1059/*
1060 * save time when starting to invoke another script or function.
1061 */
1062 void
1063script_prof_save(tm)
1064 proftime_T *tm; /* place to store wait time */
1065{
1066 scriptitem_T *si;
1067
1068 if (current_SID > 0 && current_SID <= script_items.ga_len)
1069 {
1070 si = &SCRIPT_ITEM(current_SID);
1071 if (si->sn_prof_on && si->sn_pr_nest++ == 0)
1072 profile_start(&si->sn_pr_child);
1073 }
1074 profile_get_wait(tm);
1075}
1076
1077/*
1078 * Count time spent in children after invoking another script or function.
1079 */
1080 void
1081script_prof_restore(tm)
1082 proftime_T *tm;
1083{
1084 scriptitem_T *si;
1085
1086 if (current_SID > 0 && current_SID <= script_items.ga_len)
1087 {
1088 si = &SCRIPT_ITEM(current_SID);
1089 if (si->sn_prof_on && --si->sn_pr_nest == 0)
1090 {
1091 profile_end(&si->sn_pr_child);
1092 profile_sub_wait(tm, &si->sn_pr_child); /* don't count wait time */
1093 profile_add(&si->sn_pr_children, &si->sn_pr_child);
1094 profile_add(&si->sn_prl_children, &si->sn_pr_child);
1095 }
1096 }
1097}
1098
1099static proftime_T inchar_time;
1100
1101/*
1102 * Called when starting to wait for the user to type a character.
1103 */
1104 void
1105prof_inchar_enter()
1106{
1107 profile_start(&inchar_time);
1108}
1109
1110/*
1111 * Called when finished waiting for the user to type a character.
1112 */
1113 void
1114prof_inchar_exit()
1115{
1116 profile_end(&inchar_time);
1117 profile_add(&prof_wait_time, &inchar_time);
1118}
1119
1120/*
1121 * Dump the profiling results for all scripts in file "fd".
1122 */
1123 static void
1124script_dump_profile(fd)
1125 FILE *fd;
1126{
1127 int id;
1128 scriptitem_T *si;
1129 int i;
1130 FILE *sfd;
1131 sn_prl_T *pp;
1132
1133 for (id = 1; id <= script_items.ga_len; ++id)
1134 {
1135 si = &SCRIPT_ITEM(id);
1136 if (si->sn_prof_on)
1137 {
1138 fprintf(fd, "SCRIPT %s\n", si->sn_name);
1139 if (si->sn_pr_count == 1)
1140 fprintf(fd, "Sourced 1 time\n");
1141 else
1142 fprintf(fd, "Sourced %d times\n", si->sn_pr_count);
1143 fprintf(fd, "Total time: %s\n", profile_msg(&si->sn_pr_total));
1144 fprintf(fd, " Self time: %s\n", profile_msg(&si->sn_pr_self));
1145 fprintf(fd, "\n");
1146 fprintf(fd, "count total (s) self (s)\n");
1147
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001148 sfd = mch_fopen((char *)si->sn_name, "r");
Bram Moolenaar05159a02005-02-26 23:04:13 +00001149 if (sfd == NULL)
1150 fprintf(fd, "Cannot open file!\n");
1151 else
1152 {
1153 for (i = 0; i < si->sn_prl_ga.ga_len; ++i)
1154 {
1155 if (vim_fgets(IObuff, IOSIZE, sfd))
1156 break;
1157 pp = &PRL_ITEM(si, i);
1158 if (pp->snp_count > 0)
1159 {
1160 fprintf(fd, "%5d ", pp->snp_count);
1161 if (profile_equal(&pp->sn_prl_total, &pp->sn_prl_self))
1162 fprintf(fd, " ");
1163 else
1164 fprintf(fd, "%s ", profile_msg(&pp->sn_prl_total));
1165 fprintf(fd, "%s ", profile_msg(&pp->sn_prl_self));
1166 }
1167 else
1168 fprintf(fd, " ");
1169 fprintf(fd, "%s", IObuff);
1170 }
1171 fclose(sfd);
1172 }
1173 fprintf(fd, "\n");
1174 }
1175 }
1176}
1177
1178/*
1179 * Return TRUE when a function defined in the current script should be
1180 * profiled.
1181 */
1182 int
1183prof_def_func()
1184{
Bram Moolenaar53180ce2005-07-05 21:48:14 +00001185 if (current_SID > 0)
1186 return SCRIPT_ITEM(current_SID).sn_pr_force;
1187 return FALSE;
Bram Moolenaar05159a02005-02-26 23:04:13 +00001188}
1189
1190# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191#endif
1192
1193/*
1194 * If 'autowrite' option set, try to write the file.
1195 * Careful: autocommands may make "buf" invalid!
1196 *
1197 * return FAIL for failure, OK otherwise
1198 */
1199 int
1200autowrite(buf, forceit)
1201 buf_T *buf;
1202 int forceit;
1203{
1204 if (!(p_aw || p_awa) || !p_write
1205#ifdef FEAT_QUICKFIX
1206 /* never autowrite a "nofile" or "nowrite" buffer */
1207 || bt_dontwrite(buf)
1208#endif
1209 || (!forceit && buf->b_p_ro) || buf->b_ffname == NULL)
1210 return FAIL;
1211 return buf_write_all(buf, forceit);
1212}
1213
1214/*
1215 * flush all buffers, except the ones that are readonly
1216 */
1217 void
1218autowrite_all()
1219{
1220 buf_T *buf;
1221
1222 if (!(p_aw || p_awa) || !p_write)
1223 return;
1224 for (buf = firstbuf; buf; buf = buf->b_next)
1225 if (bufIsChanged(buf) && !buf->b_p_ro)
1226 {
1227 (void)buf_write_all(buf, FALSE);
1228#ifdef FEAT_AUTOCMD
1229 /* an autocommand may have deleted the buffer */
1230 if (!buf_valid(buf))
1231 buf = firstbuf;
1232#endif
1233 }
1234}
1235
1236/*
1237 * return TRUE if buffer was changed and cannot be abandoned.
1238 */
1239/*ARGSUSED*/
1240 int
1241check_changed(buf, checkaw, mult_win, forceit, allbuf)
1242 buf_T *buf;
1243 int checkaw; /* do autowrite if buffer was changed */
1244 int mult_win; /* check also when several wins for the buf */
1245 int forceit;
1246 int allbuf; /* may write all buffers */
1247{
1248 if ( !forceit
1249 && bufIsChanged(buf)
1250 && (mult_win || buf->b_nwindows <= 1)
1251 && (!checkaw || autowrite(buf, forceit) == FAIL))
1252 {
1253#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1254 if ((p_confirm || cmdmod.confirm) && p_write)
1255 {
1256 buf_T *buf2;
1257 int count = 0;
1258
1259 if (allbuf)
1260 for (buf2 = firstbuf; buf2 != NULL; buf2 = buf2->b_next)
1261 if (bufIsChanged(buf2)
1262 && (buf2->b_ffname != NULL
1263# ifdef FEAT_BROWSE
1264 || cmdmod.browse
1265# endif
1266 ))
1267 ++count;
1268# ifdef FEAT_AUTOCMD
1269 if (!buf_valid(buf))
1270 /* Autocommand deleted buffer, oops! It's not changed now. */
1271 return FALSE;
1272# endif
1273 dialog_changed(buf, count > 1);
1274# ifdef FEAT_AUTOCMD
1275 if (!buf_valid(buf))
1276 /* Autocommand deleted buffer, oops! It's not changed now. */
1277 return FALSE;
1278# endif
1279 return bufIsChanged(buf);
1280 }
1281#endif
1282 EMSG(_(e_nowrtmsg));
1283 return TRUE;
1284 }
1285 return FALSE;
1286}
1287
1288#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
1289
1290#if defined(FEAT_BROWSE) || defined(PROTO)
1291/*
1292 * When wanting to write a file without a file name, ask the user for a name.
1293 */
1294 void
1295browse_save_fname(buf)
1296 buf_T *buf;
1297{
1298 if (buf->b_fname == NULL)
1299 {
1300 char_u *fname;
1301
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00001302 fname = do_browse(BROWSE_SAVE, (char_u *)_("Save As"),
1303 NULL, NULL, NULL, NULL, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 if (fname != NULL)
1305 {
1306 if (setfname(buf, fname, NULL, TRUE) == OK)
1307 buf->b_flags |= BF_NOTEDITED;
1308 vim_free(fname);
1309 }
1310 }
1311}
1312#endif
1313
1314/*
1315 * Ask the user what to do when abondoning a changed buffer.
1316 * Must check 'write' option first!
1317 */
1318 void
1319dialog_changed(buf, checkall)
1320 buf_T *buf;
1321 int checkall; /* may abandon all changed buffers */
1322{
1323 char_u buff[IOSIZE];
1324 int ret;
1325 buf_T *buf2;
1326
Bram Moolenaar82cf9b62005-06-07 21:09:25 +00001327 dialog_msg(buff, _("Save changes to \"%s\"?"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 (buf->b_fname != NULL) ?
1329 buf->b_fname : (char_u *)_("Untitled"));
1330 if (checkall)
1331 ret = vim_dialog_yesnoallcancel(VIM_QUESTION, NULL, buff, 1);
1332 else
1333 ret = vim_dialog_yesnocancel(VIM_QUESTION, NULL, buff, 1);
1334
1335 if (ret == VIM_YES)
1336 {
1337#ifdef FEAT_BROWSE
1338 /* May get file name, when there is none */
1339 browse_save_fname(buf);
1340#endif
1341 if (buf->b_fname != NULL) /* didn't hit Cancel */
1342 (void)buf_write_all(buf, FALSE);
1343 }
1344 else if (ret == VIM_NO)
1345 {
1346 unchanged(buf, TRUE);
1347 }
1348 else if (ret == VIM_ALL)
1349 {
1350 /*
1351 * Write all modified files that can be written.
1352 * Skip readonly buffers, these need to be confirmed
1353 * individually.
1354 */
1355 for (buf2 = firstbuf; buf2 != NULL; buf2 = buf2->b_next)
1356 {
1357 if (bufIsChanged(buf2)
1358 && (buf2->b_ffname != NULL
1359#ifdef FEAT_BROWSE
1360 || cmdmod.browse
1361#endif
1362 )
1363 && !buf2->b_p_ro)
1364 {
1365#ifdef FEAT_BROWSE
1366 /* May get file name, when there is none */
1367 browse_save_fname(buf2);
1368#endif
1369 if (buf2->b_fname != NULL) /* didn't hit Cancel */
1370 (void)buf_write_all(buf2, FALSE);
1371#ifdef FEAT_AUTOCMD
1372 /* an autocommand may have deleted the buffer */
1373 if (!buf_valid(buf2))
1374 buf2 = firstbuf;
1375#endif
1376 }
1377 }
1378 }
1379 else if (ret == VIM_DISCARDALL)
1380 {
1381 /*
1382 * mark all buffers as unchanged
1383 */
1384 for (buf2 = firstbuf; buf2 != NULL; buf2 = buf2->b_next)
1385 unchanged(buf2, TRUE);
1386 }
1387}
1388#endif
1389
1390/*
1391 * Return TRUE if the buffer "buf" can be abandoned, either by making it
1392 * hidden, autowriting it or unloading it.
1393 */
1394 int
1395can_abandon(buf, forceit)
1396 buf_T *buf;
1397 int forceit;
1398{
1399 return ( P_HID(buf)
1400 || !bufIsChanged(buf)
1401 || buf->b_nwindows > 1
1402 || autowrite(buf, forceit) == OK
1403 || forceit);
1404}
1405
1406/*
1407 * Return TRUE if any buffer was changed and cannot be abandoned.
1408 * That changed buffer becomes the current buffer.
1409 */
1410 int
1411check_changed_any(hidden)
1412 int hidden; /* Only check hidden buffers */
1413{
1414 buf_T *buf;
1415 int save;
1416#ifdef FEAT_WINDOWS
1417 win_T *wp;
1418#endif
1419
1420 for (;;)
1421 {
1422 /* check curbuf first: if it was changed we can't abandon it */
1423 if (!hidden && curbufIsChanged())
1424 buf = curbuf;
1425 else
1426 {
1427 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1428 if ((!hidden || buf->b_nwindows == 0) && bufIsChanged(buf))
1429 break;
1430 }
1431 if (buf == NULL) /* No buffers changed */
1432 return FALSE;
1433
1434 if (check_changed(buf, p_awa, TRUE, FALSE, TRUE) && buf_valid(buf))
1435 break; /* didn't save - still changes */
1436 }
1437
1438 exiting = FALSE;
1439#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1440 /*
1441 * When ":confirm" used, don't give an error message.
1442 */
1443 if (!(p_confirm || cmdmod.confirm))
1444#endif
1445 {
1446 /* There must be a wait_return for this message, do_buffer()
1447 * may cause a redraw. But wait_return() is a no-op when vgetc()
1448 * is busy (Quit used from window menu), then make sure we don't
1449 * cause a scroll up. */
1450 if (vgetc_busy)
1451 {
1452 msg_row = cmdline_row;
1453 msg_col = 0;
1454 msg_didout = FALSE;
1455 }
1456 if (EMSG2(_("E162: No write since last change for buffer \"%s\""),
1457 buf_spname(buf) != NULL ? (char_u *)buf_spname(buf) :
1458 buf->b_fname))
1459 {
1460 save = no_wait_return;
1461 no_wait_return = FALSE;
1462 wait_return(FALSE);
1463 no_wait_return = save;
1464 }
1465 }
1466
1467#ifdef FEAT_WINDOWS
1468 /* Try to find a window that contains the buffer. */
1469 if (buf != curbuf)
1470 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1471 if (wp->w_buffer == buf)
1472 {
1473 win_goto(wp);
1474# ifdef FEAT_AUTOCMD
1475 /* Paranoia: did autocms wipe out the buffer with changes? */
1476 if (!buf_valid(buf))
1477 return TRUE;
1478# endif
1479 break;
1480 }
1481#endif
1482
1483 /* Open the changed buffer in the current window. */
1484 if (buf != curbuf)
1485 set_curbuf(buf, DOBUF_GOTO);
1486
1487 return TRUE;
1488}
1489
1490/*
1491 * return FAIL if there is no file name, OK if there is one
1492 * give error message for FAIL
1493 */
1494 int
1495check_fname()
1496{
1497 if (curbuf->b_ffname == NULL)
1498 {
1499 EMSG(_(e_noname));
1500 return FAIL;
1501 }
1502 return OK;
1503}
1504
1505/*
1506 * flush the contents of a buffer, unless it has no file name
1507 *
1508 * return FAIL for failure, OK otherwise
1509 */
1510 int
1511buf_write_all(buf, forceit)
1512 buf_T *buf;
1513 int forceit;
1514{
1515 int retval;
1516#ifdef FEAT_AUTOCMD
1517 buf_T *old_curbuf = curbuf;
1518#endif
1519
1520 retval = (buf_write(buf, buf->b_ffname, buf->b_fname,
1521 (linenr_T)1, buf->b_ml.ml_line_count, NULL,
1522 FALSE, forceit, TRUE, FALSE));
1523#ifdef FEAT_AUTOCMD
1524 if (curbuf != old_curbuf)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001525 {
1526 msg_source(hl_attr(HLF_W));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 MSG(_("Warning: Entered other buffer unexpectedly (check autocommands)"));
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001528 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529#endif
1530 return retval;
1531}
1532
1533/*
1534 * Code to handle the argument list.
1535 */
1536
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001537static char_u *do_one_arg __ARGS((char_u *str));
1538static int do_arglist __ARGS((char_u *str, int what, int after));
1539static void alist_check_arg_idx __ARGS((void));
1540static int editing_arg_idx __ARGS((win_T *win));
1541#ifdef FEAT_LISTCMDS
1542static int alist_add_list __ARGS((int count, char_u **files, int after));
1543#endif
1544#define AL_SET 1
1545#define AL_ADD 2
1546#define AL_DEL 3
1547
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001549 * Isolate one argument, taking backticks.
1550 * Changes the argument in-place, puts a NUL after it. Backticks remain.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 * Return a pointer to the start of the next argument.
1552 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001553 static char_u *
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554do_one_arg(str)
1555 char_u *str;
1556{
1557 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 int inbacktick;
1559
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 inbacktick = FALSE;
1561 for (p = str; *str; ++str)
1562 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001563 /* When the backslash is used for escaping the special meaning of a
1564 * character we need to keep it until wildcard expansion. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 if (rem_backslash(str))
1566 {
1567 *p++ = *str++;
1568 *p++ = *str;
1569 }
1570 else
1571 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001572 /* An item ends at a space not in backticks */
1573 if (!inbacktick && vim_isspace(*str))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574 break;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001575 if (*str == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 inbacktick ^= TRUE;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001577 *p++ = *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 }
1579 }
1580 str = skipwhite(str);
1581 *p = NUL;
1582
1583 return str;
1584}
1585
Bram Moolenaar86b68352004-12-27 21:59:20 +00001586/*
1587 * Separate the arguments in "str" and return a list of pointers in the
1588 * growarray "gap".
1589 */
1590 int
1591get_arglist(gap, str)
1592 garray_T *gap;
1593 char_u *str;
1594{
1595 ga_init2(gap, (int)sizeof(char_u *), 20);
1596 while (*str != NUL)
1597 {
1598 if (ga_grow(gap, 1) == FAIL)
1599 {
1600 ga_clear(gap);
1601 return FAIL;
1602 }
1603 ((char_u **)gap->ga_data)[gap->ga_len++] = str;
1604
1605 /* Isolate one argument, change it in-place, put a NUL after it. */
1606 str = do_one_arg(str);
1607 }
1608 return OK;
1609}
1610
Bram Moolenaar7df351e2006-01-23 22:30:28 +00001611#if defined(FEAT_QUICKFIX) || defined(FEAT_SYN_HL) || defined(PROTO)
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00001612/*
1613 * Parse a list of arguments (file names), expand them and return in
1614 * "fnames[fcountp]".
1615 * Return FAIL or OK.
1616 */
1617 int
1618get_arglist_exp(str, fcountp, fnamesp)
1619 char_u *str;
1620 int *fcountp;
1621 char_u ***fnamesp;
1622{
1623 garray_T ga;
1624 int i;
1625
1626 if (get_arglist(&ga, str) == FAIL)
1627 return FAIL;
1628 i = gen_expand_wildcards(ga.ga_len, (char_u **)ga.ga_data,
1629 fcountp, fnamesp, EW_FILE|EW_NOTFOUND);
1630 ga_clear(&ga);
1631 return i;
1632}
1633#endif
1634
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635#if defined(FEAT_GUI) || defined(FEAT_CLIENTSERVER) || defined(PROTO)
1636/*
1637 * Redefine the argument list.
1638 */
1639 void
1640set_arglist(str)
1641 char_u *str;
1642{
1643 do_arglist(str, AL_SET, 0);
1644}
1645#endif
1646
1647/*
1648 * "what" == AL_SET: Redefine the argument list to 'str'.
1649 * "what" == AL_ADD: add files in 'str' to the argument list after "after".
1650 * "what" == AL_DEL: remove files in 'str' from the argument list.
1651 *
1652 * Return FAIL for failure, OK otherwise.
1653 */
1654/*ARGSUSED*/
1655 static int
1656do_arglist(str, what, after)
1657 char_u *str;
1658 int what;
1659 int after; /* 0 means before first one */
1660{
1661 garray_T new_ga;
1662 int exp_count;
1663 char_u **exp_files;
1664 int i;
1665#ifdef FEAT_LISTCMDS
1666 char_u *p;
1667 int match;
1668#endif
1669
1670 /*
1671 * Collect all file name arguments in "new_ga".
1672 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00001673 if (get_arglist(&new_ga, str) == FAIL)
1674 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675
1676#ifdef FEAT_LISTCMDS
1677 if (what == AL_DEL)
1678 {
1679 regmatch_T regmatch;
1680 int didone;
1681
1682 /*
1683 * Delete the items: use each item as a regexp and find a match in the
1684 * argument list.
1685 */
1686#ifdef CASE_INSENSITIVE_FILENAME
1687 regmatch.rm_ic = TRUE; /* Always ignore case */
1688#else
1689 regmatch.rm_ic = FALSE; /* Never ignore case */
1690#endif
1691 for (i = 0; i < new_ga.ga_len && !got_int; ++i)
1692 {
1693 p = ((char_u **)new_ga.ga_data)[i];
1694 p = file_pat_to_reg_pat(p, NULL, NULL, FALSE);
1695 if (p == NULL)
1696 break;
1697 regmatch.regprog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
1698 if (regmatch.regprog == NULL)
1699 {
1700 vim_free(p);
1701 break;
1702 }
1703
1704 didone = FALSE;
1705 for (match = 0; match < ARGCOUNT; ++match)
1706 if (vim_regexec(&regmatch, alist_name(&ARGLIST[match]),
1707 (colnr_T)0))
1708 {
1709 didone = TRUE;
1710 vim_free(ARGLIST[match].ae_fname);
1711 mch_memmove(ARGLIST + match, ARGLIST + match + 1,
1712 (ARGCOUNT - match - 1) * sizeof(aentry_T));
1713 --ALIST(curwin)->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 if (curwin->w_arg_idx > match)
1715 --curwin->w_arg_idx;
1716 --match;
1717 }
1718
1719 vim_free(regmatch.regprog);
1720 vim_free(p);
1721 if (!didone)
1722 EMSG2(_(e_nomatch2), ((char_u **)new_ga.ga_data)[i]);
1723 }
1724 ga_clear(&new_ga);
1725 }
1726 else
1727#endif
1728 {
1729 i = expand_wildcards(new_ga.ga_len, (char_u **)new_ga.ga_data,
1730 &exp_count, &exp_files, EW_DIR|EW_FILE|EW_ADDSLASH|EW_NOTFOUND);
1731 ga_clear(&new_ga);
1732 if (i == FAIL)
1733 return FAIL;
1734 if (exp_count == 0)
1735 {
1736 EMSG(_(e_nomatch));
1737 return FAIL;
1738 }
1739
1740#ifdef FEAT_LISTCMDS
1741 if (what == AL_ADD)
1742 {
1743 (void)alist_add_list(exp_count, exp_files, after);
1744 vim_free(exp_files);
1745 }
1746 else /* what == AL_SET */
1747#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00001748 alist_set(ALIST(curwin), exp_count, exp_files, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 }
1750
1751 alist_check_arg_idx();
1752
1753 return OK;
1754}
1755
1756/*
1757 * Check the validity of the arg_idx for each other window.
1758 */
1759 static void
1760alist_check_arg_idx()
1761{
1762#ifdef FEAT_WINDOWS
1763 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001764 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765
Bram Moolenaarf740b292006-02-16 22:11:02 +00001766 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 if (win->w_alist == curwin->w_alist)
1768 check_arg_idx(win);
1769#else
1770 check_arg_idx(curwin);
1771#endif
1772}
1773
1774/*
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001775 * Return TRUE if window "win" is editing then file at the current argument
1776 * index.
1777 */
1778 static int
1779editing_arg_idx(win)
1780 win_T *win;
1781{
1782 return !(win->w_arg_idx >= WARGCOUNT(win)
1783 || (win->w_buffer->b_fnum
1784 != WARGLIST(win)[win->w_arg_idx].ae_fnum
1785 && (win->w_buffer->b_ffname == NULL
1786 || !(fullpathcmp(
1787 alist_name(&WARGLIST(win)[win->w_arg_idx]),
1788 win->w_buffer->b_ffname, TRUE) & FPC_SAME))));
1789}
1790
1791/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 * Check if window "win" is editing the w_arg_idx file in its argument list.
1793 */
1794 void
1795check_arg_idx(win)
1796 win_T *win;
1797{
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001798 if (WARGCOUNT(win) > 1 && !editing_arg_idx(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799 {
1800 /* We are not editing the current entry in the argument list.
1801 * Set "arg_had_last" if we are editing the last one. */
1802 win->w_arg_idx_invalid = TRUE;
1803 if (win->w_arg_idx != WARGCOUNT(win) - 1
1804 && arg_had_last == FALSE
1805#ifdef FEAT_WINDOWS
1806 && ALIST(win) == &global_alist
1807#endif
1808 && GARGCOUNT > 0
1809 && win->w_arg_idx < GARGCOUNT
1810 && (win->w_buffer->b_fnum == GARGLIST[GARGCOUNT - 1].ae_fnum
1811 || (win->w_buffer->b_ffname != NULL
1812 && (fullpathcmp(alist_name(&GARGLIST[GARGCOUNT - 1]),
1813 win->w_buffer->b_ffname, TRUE) & FPC_SAME))))
1814 arg_had_last = TRUE;
1815 }
1816 else
1817 {
1818 /* We are editing the current entry in the argument list.
1819 * Set "arg_had_last" if it's also the last one */
1820 win->w_arg_idx_invalid = FALSE;
1821 if (win->w_arg_idx == WARGCOUNT(win) - 1
1822#ifdef FEAT_WINDOWS
1823 && win->w_alist == &global_alist
1824#endif
1825 )
1826 arg_had_last = TRUE;
1827 }
1828}
1829
1830/*
1831 * ":args", ":argslocal" and ":argsglobal".
1832 */
1833 void
1834ex_args(eap)
1835 exarg_T *eap;
1836{
1837 int i;
1838
1839 if (eap->cmdidx != CMD_args)
1840 {
1841#if defined(FEAT_WINDOWS) && defined(FEAT_LISTCMDS)
1842 alist_unlink(ALIST(curwin));
1843 if (eap->cmdidx == CMD_argglobal)
1844 ALIST(curwin) = &global_alist;
1845 else /* eap->cmdidx == CMD_arglocal */
1846 alist_new();
1847#else
1848 ex_ni(eap);
1849 return;
1850#endif
1851 }
1852
1853 if (!ends_excmd(*eap->arg))
1854 {
1855 /*
1856 * ":args file ..": define new argument list, handle like ":next"
1857 * Also for ":argslocal file .." and ":argsglobal file ..".
1858 */
1859 ex_next(eap);
1860 }
1861 else
1862#if defined(FEAT_WINDOWS) && defined(FEAT_LISTCMDS)
1863 if (eap->cmdidx == CMD_args)
1864#endif
1865 {
1866 /*
1867 * ":args": list arguments.
1868 */
1869 if (ARGCOUNT > 0)
1870 {
1871 /* Overwrite the command, for a short list there is no scrolling
1872 * required and no wait_return(). */
1873 gotocmdline(TRUE);
1874 for (i = 0; i < ARGCOUNT; ++i)
1875 {
1876 if (i == curwin->w_arg_idx)
1877 msg_putchar('[');
1878 msg_outtrans(alist_name(&ARGLIST[i]));
1879 if (i == curwin->w_arg_idx)
1880 msg_putchar(']');
1881 msg_putchar(' ');
1882 }
1883 }
1884 }
1885#if defined(FEAT_WINDOWS) && defined(FEAT_LISTCMDS)
1886 else if (eap->cmdidx == CMD_arglocal)
1887 {
1888 garray_T *gap = &curwin->w_alist->al_ga;
1889
1890 /*
1891 * ":argslocal": make a local copy of the global argument list.
1892 */
1893 if (ga_grow(gap, GARGCOUNT) == OK)
1894 for (i = 0; i < GARGCOUNT; ++i)
1895 if (GARGLIST[i].ae_fname != NULL)
1896 {
1897 AARGLIST(curwin->w_alist)[gap->ga_len].ae_fname =
1898 vim_strsave(GARGLIST[i].ae_fname);
1899 AARGLIST(curwin->w_alist)[gap->ga_len].ae_fnum =
1900 GARGLIST[i].ae_fnum;
1901 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 }
1903 }
1904#endif
1905}
1906
1907/*
1908 * ":previous", ":sprevious", ":Next" and ":sNext".
1909 */
1910 void
1911ex_previous(eap)
1912 exarg_T *eap;
1913{
1914 /* If past the last one already, go to the last one. */
1915 if (curwin->w_arg_idx - (int)eap->line2 >= ARGCOUNT)
1916 do_argfile(eap, ARGCOUNT - 1);
1917 else
1918 do_argfile(eap, curwin->w_arg_idx - (int)eap->line2);
1919}
1920
1921/*
1922 * ":rewind", ":first", ":sfirst" and ":srewind".
1923 */
1924 void
1925ex_rewind(eap)
1926 exarg_T *eap;
1927{
1928 do_argfile(eap, 0);
1929}
1930
1931/*
1932 * ":last" and ":slast".
1933 */
1934 void
1935ex_last(eap)
1936 exarg_T *eap;
1937{
1938 do_argfile(eap, ARGCOUNT - 1);
1939}
1940
1941/*
1942 * ":argument" and ":sargument".
1943 */
1944 void
1945ex_argument(eap)
1946 exarg_T *eap;
1947{
1948 int i;
1949
1950 if (eap->addr_count > 0)
1951 i = eap->line2 - 1;
1952 else
1953 i = curwin->w_arg_idx;
1954 do_argfile(eap, i);
1955}
1956
1957/*
1958 * Edit file "argn" of the argument lists.
1959 */
1960 void
1961do_argfile(eap, argn)
1962 exarg_T *eap;
1963 int argn;
1964{
1965 int other;
1966 char_u *p;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001967 int old_arg_idx = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968
1969 if (argn < 0 || argn >= ARGCOUNT)
1970 {
1971 if (ARGCOUNT <= 1)
1972 EMSG(_("E163: There is only one file to edit"));
1973 else if (argn < 0)
1974 EMSG(_("E164: Cannot go before first file"));
1975 else
1976 EMSG(_("E165: Cannot go beyond last file"));
1977 }
1978 else
1979 {
1980 setpcmark();
1981#ifdef FEAT_GUI
1982 need_mouse_correct = TRUE;
1983#endif
1984
1985#ifdef FEAT_WINDOWS
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00001986 /* split window or create new tab page first */
1987 if (*eap->cmd == 's' || cmdmod.tab != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 {
1989 if (win_split(0, 0) == FAIL)
1990 return;
1991# ifdef FEAT_SCROLLBIND
1992 curwin->w_p_scb = FALSE;
1993# endif
1994 }
1995 else
1996#endif
1997 {
1998 /*
1999 * if 'hidden' set, only check for changed file when re-editing
2000 * the same buffer
2001 */
2002 other = TRUE;
2003 if (P_HID(curbuf))
2004 {
2005 p = fix_fname(alist_name(&ARGLIST[argn]));
2006 other = otherfile(p);
2007 vim_free(p);
2008 }
2009 if ((!P_HID(curbuf) || !other)
2010 && check_changed(curbuf, TRUE, !other, eap->forceit, FALSE))
2011 return;
2012 }
2013
2014 curwin->w_arg_idx = argn;
2015 if (argn == ARGCOUNT - 1
2016#ifdef FEAT_WINDOWS
2017 && curwin->w_alist == &global_alist
2018#endif
2019 )
2020 arg_had_last = TRUE;
2021
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002022 /* Edit the file; always use the last known line number.
2023 * When it fails (e.g. Abort for already edited file) restore the
2024 * argument index. */
2025 if (do_ecmd(0, alist_name(&ARGLIST[curwin->w_arg_idx]), NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 eap, ECMD_LAST,
2027 (P_HID(curwin->w_buffer) ? ECMD_HIDE : 0) +
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002028 (eap->forceit ? ECMD_FORCEIT : 0)) == FAIL)
2029 curwin->w_arg_idx = old_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 /* like Vi: set the mark where the cursor is in the file. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002031 else if (eap->cmdidx != CMD_argdo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 setmark('\'');
2033 }
2034}
2035
2036/*
2037 * ":next", and commands that behave like it.
2038 */
2039 void
2040ex_next(eap)
2041 exarg_T *eap;
2042{
2043 int i;
2044
2045 /*
2046 * check for changed buffer now, if this fails the argument list is not
2047 * redefined.
2048 */
2049 if ( P_HID(curbuf)
2050 || eap->cmdidx == CMD_snext
2051 || !check_changed(curbuf, TRUE, FALSE, eap->forceit, FALSE))
2052 {
2053 if (*eap->arg != NUL) /* redefine file list */
2054 {
2055 if (do_arglist(eap->arg, AL_SET, 0) == FAIL)
2056 return;
2057 i = 0;
2058 }
2059 else
2060 i = curwin->w_arg_idx + (int)eap->line2;
2061 do_argfile(eap, i);
2062 }
2063}
2064
2065#ifdef FEAT_LISTCMDS
2066/*
2067 * ":argedit"
2068 */
2069 void
2070ex_argedit(eap)
2071 exarg_T *eap;
2072{
2073 int fnum;
2074 int i;
2075 char_u *s;
2076
2077 /* Add the argument to the buffer list and get the buffer number. */
2078 fnum = buflist_add(eap->arg, BLN_LISTED);
2079
2080 /* Check if this argument is already in the argument list. */
2081 for (i = 0; i < ARGCOUNT; ++i)
2082 if (ARGLIST[i].ae_fnum == fnum)
2083 break;
2084 if (i == ARGCOUNT)
2085 {
2086 /* Can't find it, add it to the argument list. */
2087 s = vim_strsave(eap->arg);
2088 if (s == NULL)
2089 return;
2090 i = alist_add_list(1, &s,
2091 eap->addr_count > 0 ? (int)eap->line2 : curwin->w_arg_idx + 1);
2092 if (i < 0)
2093 return;
2094 curwin->w_arg_idx = i;
2095 }
2096
2097 alist_check_arg_idx();
2098
2099 /* Edit the argument. */
2100 do_argfile(eap, i);
2101}
2102
2103/*
2104 * ":argadd"
2105 */
2106 void
2107ex_argadd(eap)
2108 exarg_T *eap;
2109{
2110 do_arglist(eap->arg, AL_ADD,
2111 eap->addr_count > 0 ? (int)eap->line2 : curwin->w_arg_idx + 1);
2112#ifdef FEAT_TITLE
2113 maketitle();
2114#endif
2115}
2116
2117/*
2118 * ":argdelete"
2119 */
2120 void
2121ex_argdelete(eap)
2122 exarg_T *eap;
2123{
2124 int i;
2125 int n;
2126
2127 if (eap->addr_count > 0)
2128 {
2129 /* ":1,4argdel": Delete all arguments in the range. */
2130 if (eap->line2 > ARGCOUNT)
2131 eap->line2 = ARGCOUNT;
2132 n = eap->line2 - eap->line1 + 1;
2133 if (*eap->arg != NUL || n <= 0)
2134 EMSG(_(e_invarg));
2135 else
2136 {
2137 for (i = eap->line1; i <= eap->line2; ++i)
2138 vim_free(ARGLIST[i - 1].ae_fname);
2139 mch_memmove(ARGLIST + eap->line1 - 1, ARGLIST + eap->line2,
2140 (size_t)((ARGCOUNT - eap->line2) * sizeof(aentry_T)));
2141 ALIST(curwin)->al_ga.ga_len -= n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 if (curwin->w_arg_idx >= eap->line2)
2143 curwin->w_arg_idx -= n;
2144 else if (curwin->w_arg_idx > eap->line1)
2145 curwin->w_arg_idx = eap->line1;
2146 }
2147 }
2148 else if (*eap->arg == NUL)
2149 EMSG(_(e_argreq));
2150 else
2151 do_arglist(eap->arg, AL_DEL, 0);
2152#ifdef FEAT_TITLE
2153 maketitle();
2154#endif
2155}
2156
2157/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002158 * ":argdo", ":windo", ":bufdo", ":tabdo"
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 */
2160 void
2161ex_listdo(eap)
2162 exarg_T *eap;
2163{
2164 int i;
2165#ifdef FEAT_WINDOWS
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002166 win_T *wp;
2167 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168#endif
2169 buf_T *buf;
2170 int next_fnum = 0;
2171#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
2172 char_u *save_ei = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002174 char_u *p_shm_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175
2176#ifndef FEAT_WINDOWS
2177 if (eap->cmdidx == CMD_windo)
2178 {
2179 ex_ni(eap);
2180 return;
2181 }
2182#endif
2183
2184#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
2185 if (eap->cmdidx != CMD_windo)
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00002186 /* Don't do syntax HL autocommands. Skipping the syntax file is a
2187 * great speed improvement. */
2188 save_ei = au_event_disable(",Syntax");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189#endif
2190
2191 if (eap->cmdidx == CMD_windo
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002192 || eap->cmdidx == CMD_tabdo
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 || P_HID(curbuf)
2194 || !check_changed(curbuf, TRUE, FALSE, eap->forceit, FALSE))
2195 {
2196 /* start at the first argument/window/buffer */
2197 i = 0;
2198#ifdef FEAT_WINDOWS
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002199 wp = firstwin;
2200 tp = first_tabpage;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201#endif
2202 /* set pcmark now */
2203 if (eap->cmdidx == CMD_bufdo)
2204 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
2205 else
2206 setpcmark();
2207 listcmd_busy = TRUE; /* avoids setting pcmark below */
2208
2209 while (!got_int)
2210 {
2211 if (eap->cmdidx == CMD_argdo)
2212 {
2213 /* go to argument "i" */
2214 if (i == ARGCOUNT)
2215 break;
2216 /* Don't call do_argfile() when already there, it will try
2217 * reloading the file. */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002218 if (curwin->w_arg_idx != i || !editing_arg_idx(curwin))
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002219 {
2220 /* Clear 'shm' to avoid that the file message overwrites
2221 * any output from the command. */
2222 p_shm_save = vim_strsave(p_shm);
2223 set_option_value((char_u *)"shm", 0L, (char_u *)"", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 do_argfile(eap, i);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002225 set_option_value((char_u *)"shm", 0L, p_shm_save, 0);
2226 vim_free(p_shm_save);
2227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 if (curwin->w_arg_idx != i)
2229 break;
2230 ++i;
2231 }
2232#ifdef FEAT_WINDOWS
2233 else if (eap->cmdidx == CMD_windo)
2234 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002235 /* go to window "wp" */
2236 if (!win_valid(wp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 break;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002238 win_goto(wp);
2239 wp = curwin->w_next;
2240 }
2241 else if (eap->cmdidx == CMD_tabdo)
2242 {
2243 /* go to window "tp" */
2244 if (!valid_tabpage(tp))
2245 break;
2246 goto_tabpage_tp(tp);
2247 tp = tp->tp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 }
2249#endif
2250 else if (eap->cmdidx == CMD_bufdo)
2251 {
2252 /* Remember the number of the next listed buffer, in case
2253 * ":bwipe" is used or autocommands do something strange. */
2254 next_fnum = -1;
2255 for (buf = curbuf->b_next; buf != NULL; buf = buf->b_next)
2256 if (buf->b_p_bl)
2257 {
2258 next_fnum = buf->b_fnum;
2259 break;
2260 }
2261 }
2262
2263 /* execute the command */
2264 do_cmdline(eap->arg, eap->getline, eap->cookie,
2265 DOCMD_VERBOSE + DOCMD_NOWAIT);
2266
2267 if (eap->cmdidx == CMD_bufdo)
2268 {
2269 /* Done? */
2270 if (next_fnum < 0)
2271 break;
2272 /* Check if the buffer still exists. */
2273 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2274 if (buf->b_fnum == next_fnum)
2275 break;
2276 if (buf == NULL)
2277 break;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002278
2279 /* Go to the next buffer. Clear 'shm' to avoid that the file
2280 * message overwrites any output from the command. */
2281 p_shm_save = vim_strsave(p_shm);
2282 set_option_value((char_u *)"shm", 0L, (char_u *)"", 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 goto_buffer(eap, DOBUF_FIRST, FORWARD, next_fnum);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00002284 set_option_value((char_u *)"shm", 0L, p_shm_save, 0);
2285 vim_free(p_shm_save);
2286
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 /* If autocommands took us elsewhere, quit here */
2288 if (curbuf->b_fnum != next_fnum)
2289 break;
2290 }
2291
2292 if (eap->cmdidx == CMD_windo)
2293 {
2294 validate_cursor(); /* cursor may have moved */
2295#ifdef FEAT_SCROLLBIND
2296 /* required when 'scrollbind' has been set */
2297 if (curwin->w_p_scb)
2298 do_check_scrollbind(TRUE);
2299#endif
2300 }
2301 }
2302 listcmd_busy = FALSE;
2303 }
2304
2305#if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
Bram Moolenaar6ac54292005-02-02 23:07:25 +00002306 if (save_ei != NULL)
2307 {
2308 au_event_restore(save_ei);
2309 apply_autocmds(EVENT_SYNTAX, curbuf->b_p_syn,
2310 curbuf->b_fname, TRUE, curbuf);
2311 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312#endif
2313}
2314
2315/*
2316 * Add files[count] to the arglist of the current window after arg "after".
2317 * The file names in files[count] must have been allocated and are taken over.
2318 * Files[] itself is not taken over.
2319 * Returns index of first added argument. Returns -1 when failed (out of mem).
2320 */
2321 static int
2322alist_add_list(count, files, after)
2323 int count;
2324 char_u **files;
2325 int after; /* where to add: 0 = before first one */
2326{
2327 int i;
2328
2329 if (ga_grow(&ALIST(curwin)->al_ga, count) == OK)
2330 {
2331 if (after < 0)
2332 after = 0;
2333 if (after > ARGCOUNT)
2334 after = ARGCOUNT;
2335 if (after < ARGCOUNT)
2336 mch_memmove(&(ARGLIST[after + count]), &(ARGLIST[after]),
2337 (ARGCOUNT - after) * sizeof(aentry_T));
2338 for (i = 0; i < count; ++i)
2339 {
2340 ARGLIST[after + i].ae_fname = files[i];
2341 ARGLIST[after + i].ae_fnum = buflist_add(files[i], BLN_LISTED);
2342 }
2343 ALIST(curwin)->al_ga.ga_len += count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 if (curwin->w_arg_idx >= after)
2345 ++curwin->w_arg_idx;
2346 return after;
2347 }
2348
2349 for (i = 0; i < count; ++i)
2350 vim_free(files[i]);
2351 return -1;
2352}
2353
2354#endif /* FEAT_LISTCMDS */
2355
2356#ifdef FEAT_EVAL
2357/*
2358 * ":compiler[!] {name}"
2359 */
2360 void
2361ex_compiler(eap)
2362 exarg_T *eap;
2363{
2364 char_u *buf;
2365 char_u *old_cur_comp = NULL;
2366 char_u *p;
2367
2368 if (*eap->arg == NUL)
2369 {
2370 /* List all compiler scripts. */
2371 do_cmdline_cmd((char_u *)"echo globpath(&rtp, 'compiler/*.vim')");
2372 /* ) keep the indenter happy... */
2373 }
2374 else
2375 {
2376 buf = alloc((unsigned)(STRLEN(eap->arg) + 14));
2377 if (buf != NULL)
2378 {
2379 if (eap->forceit)
2380 {
2381 /* ":compiler! {name}" sets global options */
2382 do_cmdline_cmd((char_u *)
2383 "command -nargs=* CompilerSet set <args>");
2384 }
2385 else
2386 {
2387 /* ":compiler! {name}" sets local options.
2388 * To remain backwards compatible "current_compiler" is always
2389 * used. A user's compiler plugin may set it, the distributed
2390 * plugin will then skip the settings. Afterwards set
2391 * "b:current_compiler" and restore "current_compiler". */
2392 old_cur_comp = get_var_value((char_u *)"current_compiler");
2393 if (old_cur_comp != NULL)
2394 old_cur_comp = vim_strsave(old_cur_comp);
2395 do_cmdline_cmd((char_u *)
2396 "command -nargs=* CompilerSet setlocal <args>");
2397 }
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00002398 do_unlet((char_u *)"current_compiler", TRUE);
2399 do_unlet((char_u *)"b:current_compiler", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400
2401 sprintf((char *)buf, "compiler/%s.vim", eap->arg);
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00002402 if (source_runtime(buf, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 EMSG2(_("E666: compiler not supported: %s"), eap->arg);
2404 vim_free(buf);
2405
2406 do_cmdline_cmd((char_u *)":delcommand CompilerSet");
2407
2408 /* Set "b:current_compiler" from "current_compiler". */
2409 p = get_var_value((char_u *)"current_compiler");
2410 if (p != NULL)
2411 set_internal_string_var((char_u *)"b:current_compiler", p);
2412
2413 /* Restore "current_compiler" for ":compiler {name}". */
2414 if (!eap->forceit)
2415 {
2416 if (old_cur_comp != NULL)
2417 {
2418 set_internal_string_var((char_u *)"current_compiler",
2419 old_cur_comp);
2420 vim_free(old_cur_comp);
2421 }
2422 else
Bram Moolenaar2ce06f62005-01-31 19:19:04 +00002423 do_unlet((char_u *)"current_compiler", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 }
2425 }
2426 }
2427}
2428#endif
2429
2430/*
2431 * ":runtime {name}"
2432 */
2433 void
2434ex_runtime(eap)
2435 exarg_T *eap;
2436{
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00002437 source_runtime(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438}
2439
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00002440static void source_callback __ARGS((char_u *fname, void *cookie));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00002442/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 static void
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00002444source_callback(fname, cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 char_u *fname;
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00002446 void *cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447{
2448 (void)do_source(fname, FALSE, FALSE);
2449}
2450
2451/*
2452 * Source the file "name" from all directories in 'runtimepath'.
2453 * "name" can contain wildcards.
2454 * When "all" is TRUE, source all files, otherwise only the first one.
2455 * return FAIL when no file could be sourced, OK otherwise.
2456 */
2457 int
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00002458source_runtime(name, all)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 char_u *name;
2460 int all;
2461{
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00002462 return do_in_runtimepath(name, all, source_callback, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463}
2464
2465/*
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00002466 * Find "name" in 'runtimepath'. When found, invoke the callback function for
2467 * it: callback(fname, "cookie")
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 * When "all" is TRUE repeat for all matches, otherwise only the first one is
2469 * used.
2470 * Returns OK when at least one match found, FAIL otherwise.
2471 */
2472 int
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00002473do_in_runtimepath(name, all, callback, cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 char_u *name;
2475 int all;
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00002476 void (*callback)__ARGS((char_u *fname, void *ck));
2477 void *cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478{
2479 char_u *rtp;
2480 char_u *np;
2481 char_u *buf;
2482 char_u *rtp_copy;
2483 char_u *tail;
2484 int num_files;
2485 char_u **files;
2486 int i;
2487 int did_one = FALSE;
2488#ifdef AMIGA
2489 struct Process *proc = (struct Process *)FindTask(0L);
2490 APTR save_winptr = proc->pr_WindowPtr;
2491
2492 /* Avoid a requester here for a volume that doesn't exist. */
2493 proc->pr_WindowPtr = (APTR)-1L;
2494#endif
2495
2496 /* Make a copy of 'runtimepath'. Invoking the callback may change the
2497 * value. */
2498 rtp_copy = vim_strsave(p_rtp);
2499 buf = alloc(MAXPATHL);
2500 if (buf != NULL && rtp_copy != NULL)
2501 {
2502 if (p_verbose > 1)
Bram Moolenaar54ee7752005-05-31 22:22:17 +00002503 {
2504 verbose_enter();
Bram Moolenaar555b2802005-05-19 21:08:39 +00002505 smsg((char_u *)_("Searching for \"%s\" in \"%s\""),
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 (char *)name, (char *)p_rtp);
Bram Moolenaar54ee7752005-05-31 22:22:17 +00002507 verbose_leave();
2508 }
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002509
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 /* Loop over all entries in 'runtimepath'. */
2511 rtp = rtp_copy;
2512 while (*rtp != NUL && (all || !did_one))
2513 {
2514 /* Copy the path from 'runtimepath' to buf[]. */
2515 copy_option_part(&rtp, buf, MAXPATHL, ",");
2516 if (STRLEN(buf) + STRLEN(name) + 2 < MAXPATHL)
2517 {
2518 add_pathsep(buf);
2519 tail = buf + STRLEN(buf);
2520
2521 /* Loop over all patterns in "name" */
2522 np = name;
2523 while (*np != NUL && (all || !did_one))
2524 {
2525 /* Append the pattern from "name" to buf[]. */
2526 copy_option_part(&np, tail, (int)(MAXPATHL - (tail - buf)),
2527 "\t ");
2528
2529 if (p_verbose > 2)
Bram Moolenaar54ee7752005-05-31 22:22:17 +00002530 {
2531 verbose_enter();
Bram Moolenaar555b2802005-05-19 21:08:39 +00002532 smsg((char_u *)_("Searching for \"%s\""), buf);
Bram Moolenaar54ee7752005-05-31 22:22:17 +00002533 verbose_leave();
2534 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535
2536 /* Expand wildcards, invoke the callback for each match. */
2537 if (gen_expand_wildcards(1, &buf, &num_files, &files,
2538 EW_FILE) == OK)
2539 {
2540 for (i = 0; i < num_files; ++i)
2541 {
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +00002542 (*callback)(files[i], cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 did_one = TRUE;
2544 if (!all)
2545 break;
2546 }
2547 FreeWild(num_files, files);
2548 }
2549 }
2550 }
2551 }
2552 }
2553 vim_free(buf);
2554 vim_free(rtp_copy);
2555 if (p_verbose > 0 && !did_one)
Bram Moolenaar54ee7752005-05-31 22:22:17 +00002556 {
2557 verbose_enter();
Bram Moolenaar555b2802005-05-19 21:08:39 +00002558 smsg((char_u *)_("not found in 'runtimepath': \"%s\""), name);
Bram Moolenaar54ee7752005-05-31 22:22:17 +00002559 verbose_leave();
2560 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561
2562#ifdef AMIGA
2563 proc->pr_WindowPtr = save_winptr;
2564#endif
2565
2566 return did_one ? OK : FAIL;
2567}
2568
2569#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD)
2570/*
2571 * ":options"
2572 */
2573/*ARGSUSED*/
2574 void
2575ex_options(eap)
2576 exarg_T *eap;
2577{
2578 cmd_source((char_u *)SYS_OPTWIN_FILE, NULL);
2579}
2580#endif
2581
2582/*
2583 * ":source {fname}"
2584 */
2585 void
2586ex_source(eap)
2587 exarg_T *eap;
2588{
2589#ifdef FEAT_BROWSE
2590 if (cmdmod.browse)
2591 {
2592 char_u *fname = NULL;
2593
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002594 fname = do_browse(0, (char_u *)_("Source Vim script"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 NULL, NULL, BROWSE_FILTER_MACROS, NULL);
2596 if (fname != NULL)
2597 {
2598 cmd_source(fname, eap);
2599 vim_free(fname);
2600 }
2601 }
2602 else
2603#endif
2604 cmd_source(eap->arg, eap);
2605}
2606
2607 static void
2608cmd_source(fname, eap)
2609 char_u *fname;
2610 exarg_T *eap;
2611{
2612 if (*fname == NUL)
2613 EMSG(_(e_argreq));
2614
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 else if (eap != NULL && eap->forceit)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00002616 /* ":source!": read Normal mdoe commands
2617 * Need to execute the commands directly. This is required at least
2618 * for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 * - ":g" command busy
2620 * - after ":argdo", ":windo" or ":bufdo"
2621 * - another command follows
2622 * - inside a loop
2623 */
2624 openscript(fname, global_busy || listcmd_busy || eap->nextcmd != NULL
2625#ifdef FEAT_EVAL
2626 || eap->cstack->cs_idx >= 0
2627#endif
2628 );
2629
2630 /* ":source" read ex commands */
2631 else if (do_source(fname, FALSE, FALSE) == FAIL)
2632 EMSG2(_(e_notopen), fname);
2633}
2634
2635/*
2636 * ":source" and associated commands.
2637 */
2638/*
2639 * Structure used to store info for each sourced file.
2640 * It is shared between do_source() and getsourceline().
2641 * This is required, because it needs to be handed to do_cmdline() and
2642 * sourcing can be done recursively.
2643 */
2644struct source_cookie
2645{
2646 FILE *fp; /* opened file for sourcing */
2647 char_u *nextline; /* if not NULL: line that was read ahead */
2648 int finished; /* ":finish" used */
2649#if defined (USE_CRNL) || defined (USE_CR)
2650 int fileformat; /* EOL_UNKNOWN, EOL_UNIX or EOL_DOS */
2651 int error; /* TRUE if LF found after CR-LF */
2652#endif
2653#ifdef FEAT_EVAL
2654 linenr_T breakpoint; /* next line with breakpoint or zero */
2655 char_u *fname; /* name of sourced file */
2656 int dbg_tick; /* debug_tick when breakpoint was set */
2657 int level; /* top nesting level of sourced file */
2658#endif
2659#ifdef FEAT_MBYTE
2660 vimconv_T conv; /* type of conversion */
2661#endif
2662};
2663
2664#ifdef FEAT_EVAL
2665/*
2666 * Return the address holding the next breakpoint line for a source cookie.
2667 */
2668 linenr_T *
2669source_breakpoint(cookie)
2670 void *cookie;
2671{
2672 return &((struct source_cookie *)cookie)->breakpoint;
2673}
2674
2675/*
2676 * Return the address holding the debug tick for a source cookie.
2677 */
2678 int *
2679source_dbg_tick(cookie)
2680 void *cookie;
2681{
2682 return &((struct source_cookie *)cookie)->dbg_tick;
2683}
2684
2685/*
2686 * Return the nesting level for a source cookie.
2687 */
2688 int
2689source_level(cookie)
2690 void *cookie;
2691{
2692 return ((struct source_cookie *)cookie)->level;
2693}
2694#endif
2695
2696static char_u *get_one_sourceline __ARGS((struct source_cookie *sp));
2697
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698#if defined(WIN32) && defined(FEAT_CSCOPE)
2699static FILE *fopen_noinh_readbin __ARGS((char *filename));
2700
2701/*
2702 * Special function to open a file without handle inheritance.
2703 */
2704 static FILE *
2705fopen_noinh_readbin(filename)
2706 char *filename;
2707{
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00002708 int fd_tmp = mch_open(filename, O_RDONLY | O_BINARY | O_NOINHERIT, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709
2710 if (fd_tmp == -1)
2711 return NULL;
2712 return fdopen(fd_tmp, READBIN);
2713}
2714#endif
2715
2716
2717/*
2718 * do_source: Read the file "fname" and execute its lines as EX commands.
2719 *
2720 * This function may be called recursively!
2721 *
2722 * return FAIL if file could not be opened, OK otherwise
2723 */
2724 int
2725do_source(fname, check_other, is_vimrc)
2726 char_u *fname;
2727 int check_other; /* check for .vimrc and _vimrc */
2728 int is_vimrc; /* call vimrc_found() when file exists */
2729{
2730 struct source_cookie cookie;
2731 char_u *save_sourcing_name;
2732 linenr_T save_sourcing_lnum;
2733 char_u *p;
2734 char_u *fname_exp;
2735 int retval = FAIL;
2736#ifdef FEAT_EVAL
2737 scid_T save_current_SID;
2738 static scid_T last_current_SID = 0;
2739 void *save_funccalp;
2740 int save_debug_break_level = debug_break_level;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002741 scriptitem_T *si = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742# ifdef UNIX
2743 struct stat st;
2744 int stat_ok;
2745# endif
2746#endif
2747#ifdef STARTUPTIME
2748 struct timeval tv_rel;
2749 struct timeval tv_start;
2750#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +00002751#ifdef FEAT_PROFILE
2752 proftime_T wait_start;
2753#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754
2755#ifdef RISCOS
2756 p = mch_munge_fname(fname);
2757#else
2758 p = expand_env_save(fname);
2759#endif
2760 if (p == NULL)
2761 return retval;
2762 fname_exp = fix_fname(p);
2763 vim_free(p);
2764 if (fname_exp == NULL)
2765 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 if (mch_isdir(fname_exp))
2767 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00002768 smsg((char_u *)_("Cannot source a directory: \"%s\""), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 goto theend;
2770 }
2771
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00002772#ifdef FEAT_AUTOCMD
2773 apply_autocmds(EVENT_SOURCEPRE, fname_exp, fname_exp, FALSE, curbuf);
2774#endif
2775
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776#if defined(WIN32) && defined(FEAT_CSCOPE)
2777 cookie.fp = fopen_noinh_readbin((char *)fname_exp);
2778#else
2779 cookie.fp = mch_fopen((char *)fname_exp, READBIN);
2780#endif
2781 if (cookie.fp == NULL && check_other)
2782 {
2783 /*
2784 * Try again, replacing file name ".vimrc" by "_vimrc" or vice versa,
2785 * and ".exrc" by "_exrc" or vice versa.
2786 */
2787 p = gettail(fname_exp);
2788 if ((*p == '.' || *p == '_')
2789 && (STRICMP(p + 1, "vimrc") == 0
2790 || STRICMP(p + 1, "gvimrc") == 0
2791 || STRICMP(p + 1, "exrc") == 0))
2792 {
2793 if (*p == '_')
2794 *p = '.';
2795 else
2796 *p = '_';
2797#if defined(WIN32) && defined(FEAT_CSCOPE)
2798 cookie.fp = fopen_noinh_readbin((char *)fname_exp);
2799#else
2800 cookie.fp = mch_fopen((char *)fname_exp, READBIN);
2801#endif
2802 }
2803 }
2804
2805 if (cookie.fp == NULL)
2806 {
2807 if (p_verbose > 0)
2808 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +00002809 verbose_enter();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 if (sourcing_name == NULL)
Bram Moolenaar555b2802005-05-19 21:08:39 +00002811 smsg((char_u *)_("could not source \"%s\""), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 else
2813 smsg((char_u *)_("line %ld: could not source \"%s\""),
Bram Moolenaar555b2802005-05-19 21:08:39 +00002814 sourcing_lnum, fname);
Bram Moolenaar54ee7752005-05-31 22:22:17 +00002815 verbose_leave();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816 }
2817 goto theend;
2818 }
2819
2820 /*
2821 * The file exists.
2822 * - In verbose mode, give a message.
2823 * - For a vimrc file, may want to set 'compatible', call vimrc_found().
2824 */
2825 if (p_verbose > 1)
2826 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +00002827 verbose_enter();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 if (sourcing_name == NULL)
Bram Moolenaar555b2802005-05-19 21:08:39 +00002829 smsg((char_u *)_("sourcing \"%s\""), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 else
2831 smsg((char_u *)_("line %ld: sourcing \"%s\""),
Bram Moolenaar555b2802005-05-19 21:08:39 +00002832 sourcing_lnum, fname);
Bram Moolenaar54ee7752005-05-31 22:22:17 +00002833 verbose_leave();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 }
2835 if (is_vimrc)
2836 vimrc_found();
2837
2838#ifdef USE_CRNL
2839 /* If no automatic file format: Set default to CR-NL. */
2840 if (*p_ffs == NUL)
2841 cookie.fileformat = EOL_DOS;
2842 else
2843 cookie.fileformat = EOL_UNKNOWN;
2844 cookie.error = FALSE;
2845#endif
2846
2847#ifdef USE_CR
2848 /* If no automatic file format: Set default to CR. */
2849 if (*p_ffs == NUL)
2850 cookie.fileformat = EOL_MAC;
2851 else
2852 cookie.fileformat = EOL_UNKNOWN;
2853 cookie.error = FALSE;
2854#endif
2855
2856 cookie.nextline = NULL;
2857 cookie.finished = FALSE;
2858
2859#ifdef FEAT_EVAL
2860 /*
2861 * Check if this script has a breakpoint.
2862 */
2863 cookie.breakpoint = dbg_find_breakpoint(TRUE, fname_exp, (linenr_T)0);
2864 cookie.fname = fname_exp;
2865 cookie.dbg_tick = debug_tick;
2866
2867 cookie.level = ex_nesting_level;
2868#endif
2869#ifdef FEAT_MBYTE
2870 cookie.conv.vc_type = CONV_NONE; /* no conversion */
2871
2872 /* Try reading the first few bytes to check for a UTF-8 BOM. */
2873 {
2874 char_u buf[3];
2875
2876 if (fread((char *)buf, sizeof(char_u), (size_t)3, cookie.fp)
2877 == (size_t)3
2878 && buf[0] == 0xef && buf[1] == 0xbb && buf[2] == 0xbf)
2879 /* Found BOM, setup conversion and skip over it. */
2880 convert_setup(&cookie.conv, (char_u *)"utf-8", p_enc);
2881 else
2882 /* No BOM found, rewind. */
2883 fseek(cookie.fp, 0L, SEEK_SET);
2884 }
2885#endif
2886
2887 /*
2888 * Keep the sourcing name/lnum, for recursive calls.
2889 */
2890 save_sourcing_name = sourcing_name;
2891 sourcing_name = fname_exp;
2892 save_sourcing_lnum = sourcing_lnum;
2893 sourcing_lnum = 0;
2894
2895#ifdef STARTUPTIME
2896 time_push(&tv_rel, &tv_start);
2897#endif
2898
2899#ifdef FEAT_EVAL
Bram Moolenaar05159a02005-02-26 23:04:13 +00002900# ifdef FEAT_PROFILE
2901 if (do_profiling)
2902 prof_child_enter(&wait_start); /* entering a child now */
2903# endif
2904
2905 /* Don't use local function variables, if called from a function.
2906 * Also starts profiling timer for nested script. */
2907 save_funccalp = save_funccal();
2908
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909 /*
2910 * Check if this script was sourced before to finds its SID.
2911 * If it's new, generate a new SID.
2912 */
2913 save_current_SID = current_SID;
2914# ifdef UNIX
2915 stat_ok = (mch_stat((char *)fname_exp, &st) >= 0);
2916# endif
Bram Moolenaar05159a02005-02-26 23:04:13 +00002917 for (current_SID = script_items.ga_len; current_SID > 0; --current_SID)
2918 {
2919 si = &SCRIPT_ITEM(current_SID);
2920 if (si->sn_name != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 && (
2922# ifdef UNIX
Bram Moolenaar7c626922005-02-07 22:01:03 +00002923 /* Compare dev/ino when possible, it catches symbolic
2924 * links. Also compare file names, the inode may change
2925 * when the file was edited. */
Bram Moolenaar05159a02005-02-26 23:04:13 +00002926 ((stat_ok && si->sn_dev != -1)
2927 && (si->sn_dev == st.st_dev
2928 && si->sn_ino == st.st_ino)) ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929# endif
Bram Moolenaar05159a02005-02-26 23:04:13 +00002930 fnamecmp(si->sn_name, fname_exp) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 break;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002932 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 if (current_SID == 0)
2934 {
2935 current_SID = ++last_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002936 if (ga_grow(&script_items, (int)(current_SID - script_items.ga_len))
2937 == FAIL)
2938 goto almosttheend;
2939 while (script_items.ga_len < current_SID)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00002941 ++script_items.ga_len;
2942 SCRIPT_ITEM(script_items.ga_len).sn_name = NULL;
2943# ifdef FEAT_PROFILE
2944 SCRIPT_ITEM(script_items.ga_len).sn_prof_on = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00002947 si = &SCRIPT_ITEM(current_SID);
2948 si->sn_name = fname_exp;
2949 fname_exp = NULL;
2950# ifdef UNIX
2951 if (stat_ok)
2952 {
2953 si->sn_dev = st.st_dev;
2954 si->sn_ino = st.st_ino;
2955 }
2956 else
2957 si->sn_dev = -1;
2958# endif
2959
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960 /* Allocate the local script variables to use for this script. */
2961 new_script_vars(current_SID);
2962 }
2963
Bram Moolenaar05159a02005-02-26 23:04:13 +00002964# ifdef FEAT_PROFILE
2965 if (do_profiling)
2966 {
2967 int forceit;
2968
2969 /* Check if we do profiling for this script. */
2970 if (!si->sn_prof_on && has_profiling(TRUE, si->sn_name, &forceit))
2971 {
2972 script_do_profile(si);
2973 si->sn_pr_force = forceit;
2974 }
2975 if (si->sn_prof_on)
2976 {
2977 ++si->sn_pr_count;
2978 profile_start(&si->sn_pr_start);
2979 profile_zero(&si->sn_pr_children);
2980 }
2981 }
2982# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983#endif
2984
2985 /*
2986 * Call do_cmdline, which will call getsourceline() to get the lines.
2987 */
2988 do_cmdline(NULL, getsourceline, (void *)&cookie,
2989 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_REPEAT);
2990
2991 retval = OK;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002992
2993#ifdef FEAT_PROFILE
2994 if (do_profiling)
2995 {
2996 /* Get "si" again, "script_items" may have been reallocated. */
2997 si = &SCRIPT_ITEM(current_SID);
2998 if (si->sn_prof_on)
2999 {
3000 profile_end(&si->sn_pr_start);
3001 profile_sub_wait(&wait_start, &si->sn_pr_start);
3002 profile_add(&si->sn_pr_total, &si->sn_pr_start);
3003 profile_add(&si->sn_pr_self, &si->sn_pr_start);
3004 profile_sub(&si->sn_pr_self, &si->sn_pr_children);
3005 }
3006 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007#endif
3008
3009 if (got_int)
3010 EMSG(_(e_interr));
3011 sourcing_name = save_sourcing_name;
3012 sourcing_lnum = save_sourcing_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 if (p_verbose > 1)
3014 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +00003015 verbose_enter();
Bram Moolenaar555b2802005-05-19 21:08:39 +00003016 smsg((char_u *)_("finished sourcing %s"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017 if (sourcing_name != NULL)
Bram Moolenaar555b2802005-05-19 21:08:39 +00003018 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar54ee7752005-05-31 22:22:17 +00003019 verbose_leave();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 }
3021#ifdef STARTUPTIME
Bram Moolenaar555b2802005-05-19 21:08:39 +00003022 vim_snprintf(IObuff, IOSIZE, "sourcing %s", fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023 time_msg(IObuff, &tv_start);
3024 time_pop(&tv_rel);
3025#endif
3026
3027#ifdef FEAT_EVAL
3028 /*
3029 * After a "finish" in debug mode, need to break at first command of next
3030 * sourced file.
3031 */
3032 if (save_debug_break_level > ex_nesting_level
3033 && debug_break_level == ex_nesting_level)
3034 ++debug_break_level;
3035#endif
3036
Bram Moolenaar05159a02005-02-26 23:04:13 +00003037#ifdef FEAT_EVAL
3038almosttheend:
3039 current_SID = save_current_SID;
3040 restore_funccal(save_funccalp);
3041# ifdef FEAT_PROFILE
3042 if (do_profiling)
3043 prof_child_exit(&wait_start); /* leaving a child now */
3044# endif
3045#endif
3046 fclose(cookie.fp);
3047 vim_free(cookie.nextline);
3048#ifdef FEAT_MBYTE
3049 convert_setup(&cookie.conv, NULL, NULL);
3050#endif
3051
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052theend:
3053 vim_free(fname_exp);
3054 return retval;
3055}
3056
3057#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00003058
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059/*
3060 * ":scriptnames"
3061 */
3062/*ARGSUSED*/
3063 void
3064ex_scriptnames(eap)
3065 exarg_T *eap;
3066{
3067 int i;
3068
Bram Moolenaar05159a02005-02-26 23:04:13 +00003069 for (i = 1; i <= script_items.ga_len && !got_int; ++i)
3070 if (SCRIPT_ITEM(i).sn_name != NULL)
3071 smsg((char_u *)"%3d: %s", i, SCRIPT_ITEM(i).sn_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072}
3073
3074# if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
3075/*
3076 * Fix slashes in the list of script names for 'shellslash'.
3077 */
3078 void
3079scriptnames_slash_adjust()
3080{
3081 int i;
3082
Bram Moolenaar05159a02005-02-26 23:04:13 +00003083 for (i = 1; i <= script_items.ga_len; ++i)
3084 if (SCRIPT_ITEM(i).sn_name != NULL)
3085 slash_adjust(SCRIPT_ITEM(i).sn_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086}
3087# endif
3088
3089/*
3090 * Get a pointer to a script name. Used for ":verbose set".
3091 */
3092 char_u *
3093get_scriptname(id)
3094 scid_T id;
3095{
3096 if (id == SID_MODELINE)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003097 return (char_u *)_("modeline");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 if (id == SID_CMDARG)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003099 return (char_u *)_("--cmd argument");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 if (id == SID_CARG)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003101 return (char_u *)_("-c argument");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 if (id == SID_ENV)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003103 return (char_u *)_("environment variable");
3104 if (id == SID_ERROR)
3105 return (char_u *)_("error handler");
Bram Moolenaar05159a02005-02-26 23:04:13 +00003106 return SCRIPT_ITEM(id).sn_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107}
Bram Moolenaar05159a02005-02-26 23:04:13 +00003108
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00003109# if defined(EXITFREE) || defined(PROTO)
3110 void
3111free_scriptnames()
3112{
3113 int i;
3114
3115 for (i = script_items.ga_len; i > 0; --i)
3116 vim_free(SCRIPT_ITEM(i).sn_name);
3117 ga_clear(&script_items);
3118}
3119# endif
3120
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121#endif
3122
3123#if defined(USE_CR) || defined(PROTO)
3124
3125# if defined(__MSL__) && (__MSL__ >= 22)
3126/*
3127 * Newer version of the Metrowerks library handle DOS and UNIX files
3128 * without help.
3129 * Test with earlier versions, MSL 2.2 is the library supplied with
3130 * Codewarrior Pro 2.
3131 */
3132 char *
3133fgets_cr(s, n, stream)
3134 char *s;
3135 int n;
3136 FILE *stream;
3137{
3138 return fgets(s, n, stream);
3139}
3140# else
3141/*
3142 * Version of fgets() which also works for lines ending in a <CR> only
3143 * (Macintosh format).
3144 * For older versions of the Metrowerks library.
3145 * At least CodeWarrior 9 needed this code.
3146 */
3147 char *
3148fgets_cr(s, n, stream)
3149 char *s;
3150 int n;
3151 FILE *stream;
3152{
3153 int c = 0;
3154 int char_read = 0;
3155
3156 while (!feof(stream) && c != '\r' && c != '\n' && char_read < n - 1)
3157 {
3158 c = fgetc(stream);
3159 s[char_read++] = c;
3160 /* If the file is in DOS format, we need to skip a NL after a CR. I
3161 * thought it was the other way around, but this appears to work... */
3162 if (c == '\n')
3163 {
3164 c = fgetc(stream);
3165 if (c != '\r')
3166 ungetc(c, stream);
3167 }
3168 }
3169
3170 s[char_read] = 0;
3171 if (char_read == 0)
3172 return NULL;
3173
3174 if (feof(stream) && char_read == 1)
3175 return NULL;
3176
3177 return s;
3178}
3179# endif
3180#endif
3181
3182/*
3183 * Get one full line from a sourced file.
3184 * Called by do_cmdline() when it's called from do_source().
3185 *
3186 * Return a pointer to the line in allocated memory.
3187 * Return NULL for end-of-file or some error.
3188 */
3189/* ARGSUSED */
3190 char_u *
3191getsourceline(c, cookie, indent)
3192 int c; /* not used */
3193 void *cookie;
3194 int indent; /* not used */
3195{
3196 struct source_cookie *sp = (struct source_cookie *)cookie;
3197 char_u *line;
3198 char_u *p, *s;
3199
3200#ifdef FEAT_EVAL
3201 /* If breakpoints have been added/deleted need to check for it. */
3202 if (sp->dbg_tick < debug_tick)
3203 {
3204 sp->breakpoint = dbg_find_breakpoint(TRUE, sp->fname, sourcing_lnum);
3205 sp->dbg_tick = debug_tick;
3206 }
Bram Moolenaar05159a02005-02-26 23:04:13 +00003207# ifdef FEAT_PROFILE
3208 if (do_profiling)
3209 script_line_end();
3210# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211#endif
3212 /*
3213 * Get current line. If there is a read-ahead line, use it, otherwise get
3214 * one now.
3215 */
3216 if (sp->finished)
3217 line = NULL;
3218 else if (sp->nextline == NULL)
3219 line = get_one_sourceline(sp);
3220 else
3221 {
3222 line = sp->nextline;
3223 sp->nextline = NULL;
3224 ++sourcing_lnum;
3225 }
Bram Moolenaare2cc9702005-03-15 22:43:58 +00003226#ifdef FEAT_PROFILE
3227 if (line != NULL && do_profiling)
3228 script_line_start();
3229#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230
3231 /* Only concatenate lines starting with a \ when 'cpoptions' doesn't
3232 * contain the 'C' flag. */
3233 if (line != NULL && (vim_strchr(p_cpo, CPO_CONCAT) == NULL))
3234 {
3235 /* compensate for the one line read-ahead */
3236 --sourcing_lnum;
3237 for (;;)
3238 {
3239 sp->nextline = get_one_sourceline(sp);
3240 if (sp->nextline == NULL)
3241 break;
3242 p = skipwhite(sp->nextline);
3243 if (*p != '\\')
3244 break;
3245 s = alloc((int)(STRLEN(line) + STRLEN(p)));
3246 if (s == NULL) /* out of memory */
3247 break;
3248 STRCPY(s, line);
3249 STRCAT(s, p + 1);
3250 vim_free(line);
3251 line = s;
3252 vim_free(sp->nextline);
3253 }
3254 }
3255
3256#ifdef FEAT_MBYTE
3257 if (line != NULL && sp->conv.vc_type != CONV_NONE)
3258 {
3259 /* Convert the encoding of the script line. */
3260 s = string_convert(&sp->conv, line, NULL);
3261 if (s != NULL)
3262 {
3263 vim_free(line);
3264 line = s;
3265 }
3266 }
3267#endif
3268
3269#ifdef FEAT_EVAL
3270 /* Did we encounter a breakpoint? */
3271 if (sp->breakpoint != 0 && sp->breakpoint <= sourcing_lnum)
3272 {
3273 dbg_breakpoint(sp->fname, sourcing_lnum);
3274 /* Find next breakpoint. */
3275 sp->breakpoint = dbg_find_breakpoint(TRUE, sp->fname, sourcing_lnum);
3276 sp->dbg_tick = debug_tick;
3277 }
3278#endif
3279
3280 return line;
3281}
3282
3283 static char_u *
3284get_one_sourceline(sp)
3285 struct source_cookie *sp;
3286{
3287 garray_T ga;
3288 int len;
3289 int c;
3290 char_u *buf;
3291#ifdef USE_CRNL
3292 int has_cr; /* CR-LF found */
3293#endif
3294#ifdef USE_CR
3295 char_u *scan;
3296#endif
3297 int have_read = FALSE;
3298
3299 /* use a growarray to store the sourced line */
Bram Moolenaar6ac54292005-02-02 23:07:25 +00003300 ga_init2(&ga, 1, 250);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301
3302 /*
3303 * Loop until there is a finished line (or end-of-file).
3304 */
3305 sourcing_lnum++;
3306 for (;;)
3307 {
Bram Moolenaar6ac54292005-02-02 23:07:25 +00003308 /* make room to read at least 120 (more) characters */
3309 if (ga_grow(&ga, 120) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 break;
3311 buf = (char_u *)ga.ga_data;
3312
3313#ifdef USE_CR
3314 if (sp->fileformat == EOL_MAC)
3315 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00003316 if (fgets_cr((char *)buf + ga.ga_len, ga.ga_maxlen - ga.ga_len,
3317 sp->fp) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 break;
3319 }
3320 else
3321#endif
Bram Moolenaar86b68352004-12-27 21:59:20 +00003322 if (fgets((char *)buf + ga.ga_len, ga.ga_maxlen - ga.ga_len,
3323 sp->fp) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 break;
Bram Moolenaar6ac54292005-02-02 23:07:25 +00003325 len = ga.ga_len + (int)STRLEN(buf + ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326#ifdef USE_CRNL
3327 /* Ignore a trailing CTRL-Z, when in Dos mode. Only recognize the
3328 * CTRL-Z by its own, or after a NL. */
3329 if ( (len == 1 || (len >= 2 && buf[len - 2] == '\n'))
3330 && sp->fileformat == EOL_DOS
3331 && buf[len - 1] == Ctrl_Z)
3332 {
3333 buf[len - 1] = NUL;
3334 break;
3335 }
3336#endif
3337
3338#ifdef USE_CR
3339 /* If the read doesn't stop on a new line, and there's
3340 * some CR then we assume a Mac format */
3341 if (sp->fileformat == EOL_UNKNOWN)
3342 {
3343 if (buf[len - 1] != '\n' && vim_strchr(buf, '\r') != NULL)
3344 sp->fileformat = EOL_MAC;
3345 else
3346 sp->fileformat = EOL_UNIX;
3347 }
3348
3349 if (sp->fileformat == EOL_MAC)
3350 {
3351 scan = vim_strchr(buf, '\r');
3352
3353 if (scan != NULL)
3354 {
3355 *scan = '\n';
3356 if (*(scan + 1) != 0)
3357 {
3358 *(scan + 1) = 0;
3359 fseek(sp->fp, (long)(scan - buf - len + 1), SEEK_CUR);
3360 }
3361 }
3362 len = STRLEN(buf);
3363 }
3364#endif
3365
3366 have_read = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 ga.ga_len = len;
3368
3369 /* If the line was longer than the buffer, read more. */
Bram Moolenaar86b68352004-12-27 21:59:20 +00003370 if (ga.ga_maxlen - ga.ga_len == 1 && buf[len - 1] != '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 continue;
3372
3373 if (len >= 1 && buf[len - 1] == '\n') /* remove trailing NL */
3374 {
3375#ifdef USE_CRNL
3376 has_cr = (len >= 2 && buf[len - 2] == '\r');
3377 if (sp->fileformat == EOL_UNKNOWN)
3378 {
3379 if (has_cr)
3380 sp->fileformat = EOL_DOS;
3381 else
3382 sp->fileformat = EOL_UNIX;
3383 }
3384
3385 if (sp->fileformat == EOL_DOS)
3386 {
3387 if (has_cr) /* replace trailing CR */
3388 {
3389 buf[len - 2] = '\n';
3390 --len;
3391 --ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 }
3393 else /* lines like ":map xx yy^M" will have failed */
3394 {
3395 if (!sp->error)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003396 {
3397 msg_source(hl_attr(HLF_W));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 EMSG(_("W15: Warning: Wrong line separator, ^M may be missing"));
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00003399 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 sp->error = TRUE;
3401 sp->fileformat = EOL_UNIX;
3402 }
3403 }
3404#endif
3405 /* The '\n' is escaped if there is an odd number of ^V's just
3406 * before it, first set "c" just before the 'V's and then check
3407 * len&c parities (is faster than ((len-c)%2 == 0)) -- Acevedo */
3408 for (c = len - 2; c >= 0 && buf[c] == Ctrl_V; c--)
3409 ;
3410 if ((len & 1) != (c & 1)) /* escaped NL, read more */
3411 {
3412 sourcing_lnum++;
3413 continue;
3414 }
3415
3416 buf[len - 1] = NUL; /* remove the NL */
3417 }
3418
3419 /*
3420 * Check for ^C here now and then, so recursive :so can be broken.
3421 */
3422 line_breakcheck();
3423 break;
3424 }
3425
3426 if (have_read)
3427 return (char_u *)ga.ga_data;
3428
3429 vim_free(ga.ga_data);
3430 return NULL;
3431}
3432
Bram Moolenaar05159a02005-02-26 23:04:13 +00003433#if defined(FEAT_PROFILE) || defined(PROTO)
3434/*
3435 * Called when starting to read a script line.
3436 * "sourcing_lnum" must be correct!
3437 * When skipping lines it may not actually be executed, but we won't find out
3438 * until later and we need to store the time now.
3439 */
3440 void
3441script_line_start()
3442{
3443 scriptitem_T *si;
3444 sn_prl_T *pp;
3445
3446 if (current_SID <= 0 || current_SID > script_items.ga_len)
3447 return;
3448 si = &SCRIPT_ITEM(current_SID);
3449 if (si->sn_prof_on && sourcing_lnum >= 1)
3450 {
3451 /* Grow the array before starting the timer, so that the time spend
3452 * here isn't counted. */
3453 ga_grow(&si->sn_prl_ga, (int)(sourcing_lnum - si->sn_prl_ga.ga_len));
3454 si->sn_prl_idx = sourcing_lnum - 1;
3455 while (si->sn_prl_ga.ga_len <= si->sn_prl_idx
3456 && si->sn_prl_ga.ga_len < si->sn_prl_ga.ga_maxlen)
3457 {
3458 /* Zero counters for a line that was not used before. */
3459 pp = &PRL_ITEM(si, si->sn_prl_ga.ga_len);
3460 pp->snp_count = 0;
3461 profile_zero(&pp->sn_prl_total);
3462 profile_zero(&pp->sn_prl_self);
3463 ++si->sn_prl_ga.ga_len;
3464 }
3465 si->sn_prl_execed = FALSE;
3466 profile_start(&si->sn_prl_start);
3467 profile_zero(&si->sn_prl_children);
3468 profile_get_wait(&si->sn_prl_wait);
3469 }
3470}
3471
3472/*
3473 * Called when actually executing a function line.
3474 */
3475 void
3476script_line_exec()
3477{
3478 scriptitem_T *si;
3479
3480 if (current_SID <= 0 || current_SID > script_items.ga_len)
3481 return;
3482 si = &SCRIPT_ITEM(current_SID);
3483 if (si->sn_prof_on && si->sn_prl_idx >= 0)
3484 si->sn_prl_execed = TRUE;
3485}
3486
3487/*
3488 * Called when done with a function line.
3489 */
3490 void
3491script_line_end()
3492{
3493 scriptitem_T *si;
3494 sn_prl_T *pp;
3495
3496 if (current_SID <= 0 || current_SID > script_items.ga_len)
3497 return;
3498 si = &SCRIPT_ITEM(current_SID);
3499 if (si->sn_prof_on && si->sn_prl_idx >= 0
3500 && si->sn_prl_idx < si->sn_prl_ga.ga_len)
3501 {
3502 if (si->sn_prl_execed)
3503 {
3504 pp = &PRL_ITEM(si, si->sn_prl_idx);
3505 ++pp->snp_count;
3506 profile_end(&si->sn_prl_start);
3507 profile_sub_wait(&si->sn_prl_wait, &si->sn_prl_start);
3508 profile_add(&pp->sn_prl_self, &si->sn_prl_start);
3509 profile_add(&pp->sn_prl_total, &si->sn_prl_start);
3510 profile_sub(&pp->sn_prl_self, &si->sn_prl_children);
3511 }
3512 si->sn_prl_idx = -1;
3513 }
3514}
3515#endif
3516
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517/*
3518 * ":scriptencoding": Set encoding conversion for a sourced script.
3519 * Without the multi-byte feature it's simply ignored.
3520 */
3521/*ARGSUSED*/
3522 void
3523ex_scriptencoding(eap)
3524 exarg_T *eap;
3525{
3526#ifdef FEAT_MBYTE
3527 struct source_cookie *sp;
3528 char_u *name;
3529
3530 if (!getline_equal(eap->getline, eap->cookie, getsourceline))
3531 {
3532 EMSG(_("E167: :scriptencoding used outside of a sourced file"));
3533 return;
3534 }
3535
3536 if (*eap->arg != NUL)
3537 {
3538 name = enc_canonize(eap->arg);
3539 if (name == NULL) /* out of memory */
3540 return;
3541 }
3542 else
3543 name = eap->arg;
3544
3545 /* Setup for conversion from the specified encoding to 'encoding'. */
3546 sp = (struct source_cookie *)getline_cookie(eap->getline, eap->cookie);
3547 convert_setup(&sp->conv, name, p_enc);
3548
3549 if (name != eap->arg)
3550 vim_free(name);
3551#endif
3552}
3553
3554#if defined(FEAT_EVAL) || defined(PROTO)
3555/*
3556 * ":finish": Mark a sourced file as finished.
3557 */
3558 void
3559ex_finish(eap)
3560 exarg_T *eap;
3561{
3562 if (getline_equal(eap->getline, eap->cookie, getsourceline))
3563 do_finish(eap, FALSE);
3564 else
3565 EMSG(_("E168: :finish used outside of a sourced file"));
3566}
3567
3568/*
3569 * Mark a sourced file as finished. Possibly makes the ":finish" pending.
3570 * Also called for a pending finish at the ":endtry" or after returning from
3571 * an extra do_cmdline(). "reanimate" is used in the latter case.
3572 */
3573 void
3574do_finish(eap, reanimate)
3575 exarg_T *eap;
3576 int reanimate;
3577{
3578 int idx;
3579
3580 if (reanimate)
3581 ((struct source_cookie *)getline_cookie(eap->getline,
3582 eap->cookie))->finished = FALSE;
3583
3584 /*
3585 * Cleanup (and inactivate) conditionals, but stop when a try conditional
3586 * not in its finally clause (which then is to be executed next) is found.
3587 * In this case, make the ":finish" pending for execution at the ":endtry".
3588 * Otherwise, finish normally.
3589 */
3590 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
3591 if (idx >= 0)
3592 {
3593 eap->cstack->cs_pending[idx] = CSTP_FINISH;
3594 report_make_pending(CSTP_FINISH, NULL);
3595 }
3596 else
3597 ((struct source_cookie *)getline_cookie(eap->getline,
3598 eap->cookie))->finished = TRUE;
3599}
3600
3601
3602/*
3603 * Return TRUE when a sourced file had the ":finish" command: Don't give error
3604 * message for missing ":endif".
3605 * Return FALSE when not sourcing a file.
3606 */
3607 int
3608source_finished(getline, cookie)
3609 char_u *(*getline) __ARGS((int, void *, int));
3610 void *cookie;
3611{
3612 return (getline_equal(getline, cookie, getsourceline)
3613 && ((struct source_cookie *)getline_cookie(
3614 getline, cookie))->finished);
3615}
3616#endif
3617
3618#if defined(FEAT_LISTCMDS) || defined(PROTO)
3619/*
3620 * ":checktime [buffer]"
3621 */
3622 void
3623ex_checktime(eap)
3624 exarg_T *eap;
3625{
3626 buf_T *buf;
3627 int save_no_check_timestamps = no_check_timestamps;
3628
3629 no_check_timestamps = 0;
3630 if (eap->addr_count == 0) /* default is all buffers */
3631 check_timestamps(FALSE);
3632 else
3633 {
3634 buf = buflist_findnr((int)eap->line2);
3635 if (buf != NULL) /* cannot happen? */
3636 (void)buf_check_timestamp(buf, FALSE);
3637 }
3638 no_check_timestamps = save_no_check_timestamps;
3639}
3640#endif
3641
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
3643 && (defined(FEAT_EVAL) || defined(FEAT_MULTI_LANG))
3644static char *get_locale_val __ARGS((int what));
3645
3646 static char *
3647get_locale_val(what)
3648 int what;
3649{
3650 char *loc;
3651
3652 /* Obtain the locale value from the libraries. For DJGPP this is
3653 * redefined and it doesn't use the arguments. */
3654 loc = setlocale(what, NULL);
3655
3656# if defined(__BORLANDC__)
3657 if (loc != NULL)
3658 {
3659 char_u *p;
3660
3661 /* Borland returns something like "LC_CTYPE=<name>\n"
3662 * Let's try to fix that bug here... */
3663 p = vim_strchr(loc, '=');
3664 if (p != NULL)
3665 {
3666 loc = ++p;
3667 while (*p != NUL) /* remove trailing newline */
3668 {
3669 if (*p < ' ')
3670 {
3671 *p = NUL;
3672 break;
3673 }
3674 ++p;
3675 }
3676 }
3677 }
3678# endif
3679
3680 return loc;
3681}
3682#endif
3683
3684
3685#ifdef WIN32
3686/*
3687 * On MS-Windows locale names are strings like "German_Germany.1252", but
3688 * gettext expects "de". Try to translate one into another here for a few
3689 * supported languages.
3690 */
3691 static char_u *
3692gettext_lang(char_u *name)
3693{
3694 int i;
3695 static char *(mtable[]) = {
3696 "afrikaans", "af",
3697 "czech", "cs",
3698 "dutch", "nl",
3699 "german", "de",
3700 "english_united kingdom", "en_GB",
3701 "spanish", "es",
3702 "french", "fr",
3703 "italian", "it",
3704 "japanese", "ja",
3705 "korean", "ko",
3706 "norwegian", "no",
3707 "polish", "pl",
3708 "russian", "ru",
3709 "slovak", "sk",
3710 "swedish", "sv",
3711 "ukrainian", "uk",
3712 "chinese_china", "zh_CN",
3713 "chinese_taiwan", "zh_TW",
3714 NULL};
3715
3716 for (i = 0; mtable[i] != NULL; i += 2)
3717 if (STRNICMP(mtable[i], name, STRLEN(mtable[i])) == 0)
3718 return mtable[i + 1];
3719 return name;
3720}
3721#endif
3722
3723#if defined(FEAT_MULTI_LANG) || defined(PROTO)
3724/*
3725 * Obtain the current messages language. Used to set the default for
3726 * 'helplang'. May return NULL or an empty string.
3727 */
3728 char_u *
3729get_mess_lang()
3730{
3731 char_u *p;
3732
3733# if (defined(HAVE_LOCALE_H) || defined(X_LOCALE))
3734# if defined(LC_MESSAGES)
3735 p = (char_u *)get_locale_val(LC_MESSAGES);
3736# else
3737 /* This is necessary for Win32, where LC_MESSAGES is not defined and $LANG
3738 * may be set to the LCID number. */
3739 p = (char_u *)get_locale_val(LC_ALL);
3740# endif
3741# else
3742 p = mch_getenv((char_u *)"LC_ALL");
3743 if (p == NULL || *p == NUL)
3744 {
3745 p = mch_getenv((char_u *)"LC_MESSAGES");
3746 if (p == NULL || *p == NUL)
3747 p = mch_getenv((char_u *)"LANG");
3748 }
3749# endif
3750# ifdef WIN32
3751 p = gettext_lang(p);
3752# endif
3753 return p;
3754}
3755#endif
3756
Bram Moolenaardef9e822004-12-31 20:58:58 +00003757/* Complicated #if; matches with where get_mess_env() is used below. */
3758#if (defined(FEAT_EVAL) && !((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
3759 && defined(LC_MESSAGES))) \
3760 || ((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
3761 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)) \
3762 && !defined(LC_MESSAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763static char_u *get_mess_env __ARGS((void));
3764
3765/*
3766 * Get the language used for messages from the environment.
3767 */
3768 static char_u *
3769get_mess_env()
3770{
3771 char_u *p;
3772
3773 p = mch_getenv((char_u *)"LC_ALL");
3774 if (p == NULL || *p == NUL)
3775 {
3776 p = mch_getenv((char_u *)"LC_MESSAGES");
3777 if (p == NULL || *p == NUL)
3778 {
3779 p = mch_getenv((char_u *)"LANG");
3780 if (p != NULL && VIM_ISDIGIT(*p))
3781 p = NULL; /* ignore something like "1043" */
3782# if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
3783 if (p == NULL || *p == NUL)
3784 p = (char_u *)get_locale_val(LC_CTYPE);
3785# endif
3786 }
3787 }
3788 return p;
3789}
3790#endif
3791
3792#if defined(FEAT_EVAL) || defined(PROTO)
3793
3794/*
3795 * Set the "v:lang" variable according to the current locale setting.
3796 * Also do "v:lc_time"and "v:ctype".
3797 */
3798 void
3799set_lang_var()
3800{
3801 char_u *loc;
3802
3803# if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
3804 loc = (char_u *)get_locale_val(LC_CTYPE);
3805# else
3806 /* setlocale() not supported: use the default value */
3807 loc = (char_u *)"C";
3808# endif
3809 set_vim_var_string(VV_CTYPE, loc, -1);
3810
3811 /* When LC_MESSAGES isn't defined use the value from $LC_MESSAGES, fall
3812 * back to LC_CTYPE if it's empty. */
3813# if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) && defined(LC_MESSAGES)
3814 loc = (char_u *)get_locale_val(LC_MESSAGES);
3815# else
3816 loc = get_mess_env();
3817# endif
3818 set_vim_var_string(VV_LANG, loc, -1);
3819
3820# if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
3821 loc = (char_u *)get_locale_val(LC_TIME);
3822# endif
3823 set_vim_var_string(VV_LC_TIME, loc, -1);
3824}
3825#endif
3826
3827#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
3828 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
3829/*
3830 * ":language": Set the language (locale).
3831 */
3832 void
3833ex_language(eap)
3834 exarg_T *eap;
3835{
3836 char *loc;
3837 char_u *p;
3838 char_u *name;
3839 int what = LC_ALL;
3840 char *whatstr = "";
3841#ifdef LC_MESSAGES
3842# define VIM_LC_MESSAGES LC_MESSAGES
3843#else
3844# define VIM_LC_MESSAGES 6789
3845#endif
3846
3847 name = eap->arg;
3848
3849 /* Check for "messages {name}", "ctype {name}" or "time {name}" argument.
3850 * Allow abbreviation, but require at least 3 characters to avoid
3851 * confusion with a two letter language name "me" or "ct". */
3852 p = skiptowhite(eap->arg);
3853 if ((*p == NUL || vim_iswhite(*p)) && p - eap->arg >= 3)
3854 {
3855 if (STRNICMP(eap->arg, "messages", p - eap->arg) == 0)
3856 {
3857 what = VIM_LC_MESSAGES;
3858 name = skipwhite(p);
3859 whatstr = "messages ";
3860 }
3861 else if (STRNICMP(eap->arg, "ctype", p - eap->arg) == 0)
3862 {
3863 what = LC_CTYPE;
3864 name = skipwhite(p);
3865 whatstr = "ctype ";
3866 }
3867 else if (STRNICMP(eap->arg, "time", p - eap->arg) == 0)
3868 {
3869 what = LC_TIME;
3870 name = skipwhite(p);
3871 whatstr = "time ";
3872 }
3873 }
3874
3875 if (*name == NUL)
3876 {
3877#ifndef LC_MESSAGES
3878 if (what == VIM_LC_MESSAGES)
3879 p = get_mess_env();
3880 else
3881#endif
3882 p = (char_u *)setlocale(what, NULL);
3883 if (p == NULL || *p == NUL)
3884 p = (char_u *)"Unknown";
3885 smsg((char_u *)_("Current %slanguage: \"%s\""), whatstr, p);
3886 }
3887 else
3888 {
3889#ifndef LC_MESSAGES
3890 if (what == VIM_LC_MESSAGES)
3891 loc = "";
3892 else
3893#endif
3894 loc = setlocale(what, (char *)name);
3895 if (loc == NULL)
3896 EMSG2(_("E197: Cannot set language to \"%s\""), name);
3897 else
3898 {
3899#ifdef HAVE_NL_MSG_CAT_CNTR
3900 /* Need to do this for GNU gettext, otherwise cached translations
3901 * will be used again. */
3902 extern int _nl_msg_cat_cntr;
3903
3904 ++_nl_msg_cat_cntr;
3905#endif
3906 /* Reset $LC_ALL, otherwise it would overrule everyting. */
3907 vim_setenv((char_u *)"LC_ALL", (char_u *)"");
3908
3909 if (what != LC_TIME)
3910 {
3911 /* Tell gettext() what to translate to. It apparently doesn't
3912 * use the currently effective locale. Also do this when
3913 * FEAT_GETTEXT isn't defined, so that shell commands use this
3914 * value. */
3915 if (what == LC_ALL)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003916 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 vim_setenv((char_u *)"LANG", name);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003918# ifdef WIN32
3919 /* Apparently MS-Windows printf() may cause a crash when
3920 * we give it 8-bit text while it's expecting text in the
3921 * current locale. This call avoids that. */
3922 setlocale(LC_CTYPE, "C");
3923# endif
3924 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 if (what != LC_CTYPE)
3926 {
3927 char_u *mname;
3928#ifdef WIN32
3929 mname = gettext_lang(name);
3930#else
3931 mname = name;
3932#endif
3933 vim_setenv((char_u *)"LC_MESSAGES", mname);
3934#ifdef FEAT_MULTI_LANG
3935 set_helplang_default(mname);
3936#endif
3937 }
3938
3939 /* Set $LC_CTYPE, because it overrules $LANG, and
3940 * gtk_set_locale() calls setlocale() again. gnome_init()
3941 * sets $LC_CTYPE to "en_US" (that's a bug!). */
3942 if (what != VIM_LC_MESSAGES)
3943 vim_setenv((char_u *)"LC_CTYPE", name);
3944# ifdef FEAT_GUI_GTK
3945 /* Let GTK know what locale we're using. Not sure this is
3946 * really needed... */
3947 if (gui.in_use)
3948 (void)gtk_set_locale();
3949# endif
3950 }
3951
3952# ifdef FEAT_EVAL
3953 /* Set v:lang, v:lc_time and v:ctype to the final result. */
3954 set_lang_var();
3955# endif
3956 }
3957 }
3958}
3959
3960# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3961/*
3962 * Function given to ExpandGeneric() to obtain the possible arguments of the
3963 * ":language" command.
3964 */
3965/*ARGSUSED*/
3966 char_u *
3967get_lang_arg(xp, idx)
3968 expand_T *xp;
3969 int idx;
3970{
3971 if (idx == 0)
3972 return (char_u *)"messages";
3973 if (idx == 1)
3974 return (char_u *)"ctype";
3975 if (idx == 2)
3976 return (char_u *)"time";
3977 return NULL;
3978}
3979# endif
3980
3981#endif