Mercurial > vim
annotate src/ex_cmds2.c @ 3429:b35844f3eb49 v7.3.480
updated for version 7.3.480
Problem: When using ":qa" and there is a changed buffer picking the buffer
to jump to is not very good.
Solution: Consider current and other tab pages. (Hirohito Higashi)
author | Bram Moolenaar <bram@vim.org> |
---|---|
date | Fri, 23 Mar 2012 18:39:18 +0100 |
parents | e6392f2aed4c |
children | f9a865d97614 |
rev | line source |
---|---|
7 | 1 /* 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 #include "vim.h" | |
15 #include "version.h" | |
16 | |
17 static void cmd_source __ARGS((char_u *fname, exarg_T *eap)); | |
18 | |
170 | 19 #ifdef FEAT_EVAL |
177 | 20 /* Growarray to store info about already sourced scripts. |
170 | 21 * For Unix also store the dev/ino, so that we don't have to stat() each |
22 * script when going through the list. */ | |
23 typedef struct scriptitem_S | |
24 { | |
25 char_u *sn_name; | |
26 # ifdef UNIX | |
1882 | 27 int sn_dev_valid; |
28 dev_t sn_dev; | |
170 | 29 ino_t sn_ino; |
30 # endif | |
31 # ifdef FEAT_PROFILE | |
32 int sn_prof_on; /* TRUE when script is/was profiled */ | |
177 | 33 int sn_pr_force; /* forceit: profile functions in this script */ |
170 | 34 proftime_T sn_pr_child; /* time set when going into first child */ |
35 int sn_pr_nest; /* nesting for sn_pr_child */ | |
36 /* profiling the script as a whole */ | |
37 int sn_pr_count; /* nr of times sourced */ | |
1624 | 38 proftime_T sn_pr_total; /* time spent in script + children */ |
39 proftime_T sn_pr_self; /* time spent in script itself */ | |
170 | 40 proftime_T sn_pr_start; /* time at script start */ |
41 proftime_T sn_pr_children; /* time in children after script start */ | |
42 /* profiling the script per line */ | |
43 garray_T sn_prl_ga; /* things stored for every line */ | |
44 proftime_T sn_prl_start; /* start time for current line */ | |
45 proftime_T sn_prl_children; /* time spent in children for this line */ | |
46 proftime_T sn_prl_wait; /* wait start time for current line */ | |
47 int sn_prl_idx; /* index of line being timed; -1 if none */ | |
48 int sn_prl_execed; /* line being timed was executed */ | |
49 # endif | |
50 } scriptitem_T; | |
51 | |
52 static garray_T script_items = {0, 0, sizeof(scriptitem_T), 4, NULL}; | |
53 #define SCRIPT_ITEM(id) (((scriptitem_T *)script_items.ga_data)[(id) - 1]) | |
54 | |
55 # ifdef FEAT_PROFILE | |
56 /* Struct used in sn_prl_ga for every line of a script. */ | |
57 typedef struct sn_prl_S | |
58 { | |
59 int snp_count; /* nr of times line was executed */ | |
1624 | 60 proftime_T sn_prl_total; /* time spent in a line + children */ |
61 proftime_T sn_prl_self; /* time spent in a line itself */ | |
170 | 62 } sn_prl_T; |
63 | |
64 # define PRL_ITEM(si, idx) (((sn_prl_T *)(si)->sn_prl_ga.ga_data)[(idx)]) | |
65 # endif | |
66 #endif | |
67 | |
7 | 68 #if defined(FEAT_EVAL) || defined(PROTO) |
69 static int debug_greedy = FALSE; /* batch mode debugging: don't save | |
70 and restore typeahead. */ | |
71 | |
72 /* | |
73 * do_debug(): Debug mode. | |
74 * Repeatedly get Ex commands, until told to continue normal execution. | |
75 */ | |
76 void | |
77 do_debug(cmd) | |
78 char_u *cmd; | |
79 { | |
80 int save_msg_scroll = msg_scroll; | |
81 int save_State = State; | |
82 int save_did_emsg = did_emsg; | |
83 int save_cmd_silent = cmd_silent; | |
84 int save_msg_silent = msg_silent; | |
85 int save_emsg_silent = emsg_silent; | |
86 int save_redir_off = redir_off; | |
87 tasave_T typeaheadbuf; | |
1462 | 88 int typeahead_saved = FALSE; |
1485 | 89 int save_ignore_script = 0; |
7 | 90 # ifdef FEAT_EX_EXTRA |
91 int save_ex_normal_busy; | |
92 # endif | |
93 int n; | |
94 char_u *cmdline = NULL; | |
95 char_u *p; | |
96 char *tail = NULL; | |
97 static int last_cmd = 0; | |
98 #define CMD_CONT 1 | |
99 #define CMD_NEXT 2 | |
100 #define CMD_STEP 3 | |
101 #define CMD_FINISH 4 | |
102 #define CMD_QUIT 5 | |
103 #define CMD_INTERRUPT 6 | |
104 | |
105 #ifdef ALWAYS_USE_GUI | |
106 /* Can't do this when there is no terminal for input/output. */ | |
107 if (!gui.in_use) | |
108 { | |
109 /* Break as soon as possible. */ | |
110 debug_break_level = 9999; | |
111 return; | |
112 } | |
113 #endif | |
114 | |
115 /* Make sure we are in raw mode and start termcap mode. Might have side | |
116 * effects... */ | |
117 settmode(TMODE_RAW); | |
118 starttermcap(); | |
119 | |
120 ++RedrawingDisabled; /* don't redisplay the window */ | |
121 ++no_wait_return; /* don't wait for return */ | |
122 did_emsg = FALSE; /* don't use error from debugged stuff */ | |
123 cmd_silent = FALSE; /* display commands */ | |
124 msg_silent = FALSE; /* display messages */ | |
125 emsg_silent = FALSE; /* display error messages */ | |
126 redir_off = TRUE; /* don't redirect debug commands */ | |
127 | |
128 State = NORMAL; | |
129 #ifdef FEAT_SNIFF | |
130 want_sniff_request = 0; /* No K_SNIFF wanted */ | |
131 #endif | |
132 | |
133 if (!debug_did_msg) | |
134 MSG(_("Entering Debug mode. Type \"cont\" to continue.")); | |
135 if (sourcing_name != NULL) | |
136 msg(sourcing_name); | |
137 if (sourcing_lnum != 0) | |
274 | 138 smsg((char_u *)_("line %ld: %s"), (long)sourcing_lnum, cmd); |
7 | 139 else |
274 | 140 smsg((char_u *)_("cmd: %s"), cmd); |
7 | 141 |
142 /* | |
143 * Repeat getting a command and executing it. | |
144 */ | |
145 for (;;) | |
146 { | |
147 msg_scroll = TRUE; | |
148 need_wait_return = FALSE; | |
149 #ifdef FEAT_SNIFF | |
150 ProcessSniffRequests(); | |
151 #endif | |
152 /* Save the current typeahead buffer and replace it with an empty one. | |
153 * This makes sure we get input from the user here and don't interfere | |
154 * with the commands being executed. Reset "ex_normal_busy" to avoid | |
155 * the side effects of using ":normal". Save the stuff buffer and make | |
1462 | 156 * it empty. Set ignore_script to avoid reading from script input. */ |
7 | 157 # ifdef FEAT_EX_EXTRA |
158 save_ex_normal_busy = ex_normal_busy; | |
159 ex_normal_busy = 0; | |
160 # endif | |
161 if (!debug_greedy) | |
1462 | 162 { |
7 | 163 save_typeahead(&typeaheadbuf); |
1462 | 164 typeahead_saved = TRUE; |
165 save_ignore_script = ignore_script; | |
166 ignore_script = TRUE; | |
167 } | |
7 | 168 |
531 | 169 cmdline = getcmdline_prompt('>', NULL, 0, EXPAND_NOTHING, NULL); |
7 | 170 |
1462 | 171 if (typeahead_saved) |
172 { | |
7 | 173 restore_typeahead(&typeaheadbuf); |
1462 | 174 ignore_script = save_ignore_script; |
175 } | |
7 | 176 # ifdef FEAT_EX_EXTRA |
177 ex_normal_busy = save_ex_normal_busy; | |
178 # endif | |
179 | |
180 cmdline_row = msg_row; | |
181 if (cmdline != NULL) | |
182 { | |
183 /* If this is a debug command, set "last_cmd". | |
184 * If not, reset "last_cmd". | |
185 * For a blank line use previous command. */ | |
186 p = skipwhite(cmdline); | |
187 if (*p != NUL) | |
188 { | |
189 switch (*p) | |
190 { | |
191 case 'c': last_cmd = CMD_CONT; | |
192 tail = "ont"; | |
193 break; | |
194 case 'n': last_cmd = CMD_NEXT; | |
195 tail = "ext"; | |
196 break; | |
197 case 's': last_cmd = CMD_STEP; | |
198 tail = "tep"; | |
199 break; | |
200 case 'f': last_cmd = CMD_FINISH; | |
201 tail = "inish"; | |
202 break; | |
203 case 'q': last_cmd = CMD_QUIT; | |
204 tail = "uit"; | |
205 break; | |
206 case 'i': last_cmd = CMD_INTERRUPT; | |
207 tail = "nterrupt"; | |
208 break; | |
209 default: last_cmd = 0; | |
210 } | |
211 if (last_cmd != 0) | |
212 { | |
213 /* Check that the tail matches. */ | |
214 ++p; | |
215 while (*p != NUL && *p == *tail) | |
216 { | |
217 ++p; | |
218 ++tail; | |
219 } | |
220 if (ASCII_ISALPHA(*p)) | |
221 last_cmd = 0; | |
222 } | |
223 } | |
224 | |
225 if (last_cmd != 0) | |
226 { | |
227 /* Execute debug command: decided where to break next and | |
228 * return. */ | |
229 switch (last_cmd) | |
230 { | |
231 case CMD_CONT: | |
232 debug_break_level = -1; | |
233 break; | |
234 case CMD_NEXT: | |
235 debug_break_level = ex_nesting_level; | |
236 break; | |
237 case CMD_STEP: | |
238 debug_break_level = 9999; | |
239 break; | |
240 case CMD_FINISH: | |
241 debug_break_level = ex_nesting_level - 1; | |
242 break; | |
243 case CMD_QUIT: | |
244 got_int = TRUE; | |
245 debug_break_level = -1; | |
246 break; | |
247 case CMD_INTERRUPT: | |
248 got_int = TRUE; | |
249 debug_break_level = 9999; | |
250 /* Do not repeat ">interrupt" cmd, continue stepping. */ | |
251 last_cmd = CMD_STEP; | |
252 break; | |
253 } | |
254 break; | |
255 } | |
256 | |
257 /* don't debug this command */ | |
258 n = debug_break_level; | |
259 debug_break_level = -1; | |
260 (void)do_cmdline(cmdline, getexline, NULL, | |
261 DOCMD_VERBOSE|DOCMD_EXCRESET); | |
262 debug_break_level = n; | |
263 | |
264 vim_free(cmdline); | |
265 } | |
266 lines_left = Rows - 1; | |
267 } | |
268 vim_free(cmdline); | |
269 | |
270 --RedrawingDisabled; | |
271 --no_wait_return; | |
272 redraw_all_later(NOT_VALID); | |
273 need_wait_return = FALSE; | |
274 msg_scroll = save_msg_scroll; | |
275 lines_left = Rows - 1; | |
276 State = save_State; | |
277 did_emsg = save_did_emsg; | |
278 cmd_silent = save_cmd_silent; | |
279 msg_silent = save_msg_silent; | |
280 emsg_silent = save_emsg_silent; | |
281 redir_off = save_redir_off; | |
282 | |
283 /* Only print the message again when typing a command before coming back | |
284 * here. */ | |
285 debug_did_msg = TRUE; | |
286 } | |
287 | |
288 /* | |
289 * ":debug". | |
290 */ | |
291 void | |
292 ex_debug(eap) | |
293 exarg_T *eap; | |
294 { | |
295 int debug_break_level_save = debug_break_level; | |
296 | |
297 debug_break_level = 9999; | |
298 do_cmdline_cmd(eap->arg); | |
299 debug_break_level = debug_break_level_save; | |
300 } | |
301 | |
302 static char_u *debug_breakpoint_name = NULL; | |
303 static linenr_T debug_breakpoint_lnum; | |
304 | |
305 /* | |
306 * When debugging or a breakpoint is set on a skipped command, no debug prompt | |
307 * is shown by do_one_cmd(). This situation is indicated by debug_skipped, and | |
308 * debug_skipped_name is then set to the source name in the breakpoint case. If | |
309 * a skipped command decides itself that a debug prompt should be displayed, it | |
310 * can do so by calling dbg_check_skipped(). | |
311 */ | |
312 static int debug_skipped; | |
313 static char_u *debug_skipped_name; | |
314 | |
315 /* | |
316 * Go to debug mode when a breakpoint was encountered or "ex_nesting_level" is | |
317 * at or below the break level. But only when the line is actually | |
318 * executed. Return TRUE and set breakpoint_name for skipped commands that | |
319 * decide to execute something themselves. | |
320 * Called from do_one_cmd() before executing a command. | |
321 */ | |
322 void | |
323 dbg_check_breakpoint(eap) | |
324 exarg_T *eap; | |
325 { | |
326 char_u *p; | |
327 | |
328 debug_skipped = FALSE; | |
329 if (debug_breakpoint_name != NULL) | |
330 { | |
331 if (!eap->skip) | |
332 { | |
333 /* replace K_SNR with "<SNR>" */ | |
334 if (debug_breakpoint_name[0] == K_SPECIAL | |
335 && debug_breakpoint_name[1] == KS_EXTRA | |
336 && debug_breakpoint_name[2] == (int)KE_SNR) | |
337 p = (char_u *)"<SNR>"; | |
338 else | |
339 p = (char_u *)""; | |
274 | 340 smsg((char_u *)_("Breakpoint in \"%s%s\" line %ld"), |
341 p, | |
7 | 342 debug_breakpoint_name + (*p == NUL ? 0 : 3), |
343 (long)debug_breakpoint_lnum); | |
344 debug_breakpoint_name = NULL; | |
345 do_debug(eap->cmd); | |
346 } | |
347 else | |
348 { | |
349 debug_skipped = TRUE; | |
350 debug_skipped_name = debug_breakpoint_name; | |
351 debug_breakpoint_name = NULL; | |
352 } | |
353 } | |
354 else if (ex_nesting_level <= debug_break_level) | |
355 { | |
356 if (!eap->skip) | |
357 do_debug(eap->cmd); | |
358 else | |
359 { | |
360 debug_skipped = TRUE; | |
361 debug_skipped_name = NULL; | |
362 } | |
363 } | |
364 } | |
365 | |
366 /* | |
367 * Go to debug mode if skipped by dbg_check_breakpoint() because eap->skip was | |
368 * set. Return TRUE when the debug mode is entered this time. | |
369 */ | |
370 int | |
371 dbg_check_skipped(eap) | |
372 exarg_T *eap; | |
373 { | |
374 int prev_got_int; | |
375 | |
376 if (debug_skipped) | |
377 { | |
378 /* | |
379 * Save the value of got_int and reset it. We don't want a previous | |
380 * interruption cause flushing the input buffer. | |
381 */ | |
382 prev_got_int = got_int; | |
383 got_int = FALSE; | |
384 debug_breakpoint_name = debug_skipped_name; | |
385 /* eap->skip is TRUE */ | |
386 eap->skip = FALSE; | |
387 (void)dbg_check_breakpoint(eap); | |
388 eap->skip = TRUE; | |
389 got_int |= prev_got_int; | |
390 return TRUE; | |
391 } | |
392 return FALSE; | |
393 } | |
394 | |
395 /* | |
396 * The list of breakpoints: dbg_breakp. | |
397 * This is a grow-array of structs. | |
398 */ | |
399 struct debuggy | |
400 { | |
401 int dbg_nr; /* breakpoint number */ | |
402 int dbg_type; /* DBG_FUNC or DBG_FILE */ | |
403 char_u *dbg_name; /* function or file name */ | |
404 regprog_T *dbg_prog; /* regexp program */ | |
405 linenr_T dbg_lnum; /* line number in function or file */ | |
170 | 406 int dbg_forceit; /* ! used */ |
7 | 407 }; |
408 | |
409 static garray_T dbg_breakp = {0, 0, sizeof(struct debuggy), 4, NULL}; | |
170 | 410 #define BREAKP(idx) (((struct debuggy *)dbg_breakp.ga_data)[idx]) |
411 #define DEBUGGY(gap, idx) (((struct debuggy *)gap->ga_data)[idx]) | |
7 | 412 static int last_breakp = 0; /* nr of last defined breakpoint */ |
413 | |
170 | 414 #ifdef FEAT_PROFILE |
415 /* Profiling uses file and func names similar to breakpoints. */ | |
416 static garray_T prof_ga = {0, 0, sizeof(struct debuggy), 4, NULL}; | |
417 #endif | |
7 | 418 #define DBG_FUNC 1 |
419 #define DBG_FILE 2 | |
420 | |
170 | 421 static int dbg_parsearg __ARGS((char_u *arg, garray_T *gap)); |
422 static linenr_T debuggy_find __ARGS((int file,char_u *fname, linenr_T after, garray_T *gap, int *fp)); | |
7 | 423 |
424 /* | |
170 | 425 * Parse the arguments of ":profile", ":breakadd" or ":breakdel" and put them |
426 * in the entry just after the last one in dbg_breakp. Note that "dbg_name" | |
427 * is allocated. | |
7 | 428 * Returns FAIL for failure. |
429 */ | |
430 static int | |
170 | 431 dbg_parsearg(arg, gap) |
7 | 432 char_u *arg; |
170 | 433 garray_T *gap; /* either &dbg_breakp or &prof_ga */ |
7 | 434 { |
435 char_u *p = arg; | |
436 char_u *q; | |
437 struct debuggy *bp; | |
10 | 438 int here = FALSE; |
7 | 439 |
170 | 440 if (ga_grow(gap, 1) == FAIL) |
7 | 441 return FAIL; |
170 | 442 bp = &DEBUGGY(gap, gap->ga_len); |
7 | 443 |
444 /* Find "func" or "file". */ | |
445 if (STRNCMP(p, "func", 4) == 0) | |
446 bp->dbg_type = DBG_FUNC; | |
447 else if (STRNCMP(p, "file", 4) == 0) | |
448 bp->dbg_type = DBG_FILE; | |
170 | 449 else if ( |
450 #ifdef FEAT_PROFILE | |
451 gap != &prof_ga && | |
452 #endif | |
453 STRNCMP(p, "here", 4) == 0) | |
10 | 454 { |
455 if (curbuf->b_ffname == NULL) | |
456 { | |
457 EMSG(_(e_noname)); | |
458 return FAIL; | |
459 } | |
460 bp->dbg_type = DBG_FILE; | |
461 here = TRUE; | |
462 } | |
7 | 463 else |
464 { | |
465 EMSG2(_(e_invarg2), p); | |
466 return FAIL; | |
467 } | |
468 p = skipwhite(p + 4); | |
469 | |
470 /* Find optional line number. */ | |
10 | 471 if (here) |
472 bp->dbg_lnum = curwin->w_cursor.lnum; | |
170 | 473 else if ( |
474 #ifdef FEAT_PROFILE | |
475 gap != &prof_ga && | |
476 #endif | |
477 VIM_ISDIGIT(*p)) | |
7 | 478 { |
479 bp->dbg_lnum = getdigits(&p); | |
480 p = skipwhite(p); | |
481 } | |
482 else | |
483 bp->dbg_lnum = 0; | |
484 | |
485 /* Find the function or file name. Don't accept a function name with (). */ | |
10 | 486 if ((!here && *p == NUL) |
487 || (here && *p != NUL) | |
7 | 488 || (bp->dbg_type == DBG_FUNC && strstr((char *)p, "()") != NULL)) |
489 { | |
490 EMSG2(_(e_invarg2), arg); | |
491 return FAIL; | |
492 } | |
493 | |
494 if (bp->dbg_type == DBG_FUNC) | |
495 bp->dbg_name = vim_strsave(p); | |
10 | 496 else if (here) |
497 bp->dbg_name = vim_strsave(curbuf->b_ffname); | |
7 | 498 else |
499 { | |
500 /* Expand the file name in the same way as do_source(). This means | |
501 * doing it twice, so that $DIR/file gets expanded when $DIR is | |
502 * "~/dir". */ | |
503 q = expand_env_save(p); | |
504 if (q == NULL) | |
505 return FAIL; | |
506 p = expand_env_save(q); | |
507 vim_free(q); | |
508 if (p == NULL) | |
509 return FAIL; | |
11 | 510 if (*p != '*') |
511 { | |
512 bp->dbg_name = fix_fname(p); | |
513 vim_free(p); | |
514 } | |
515 else | |
516 bp->dbg_name = p; | |
7 | 517 } |
518 | |
519 if (bp->dbg_name == NULL) | |
520 return FAIL; | |
521 return OK; | |
522 } | |
523 | |
524 /* | |
525 * ":breakadd". | |
526 */ | |
527 void | |
528 ex_breakadd(eap) | |
529 exarg_T *eap; | |
530 { | |
531 struct debuggy *bp; | |
532 char_u *pat; | |
170 | 533 garray_T *gap; |
534 | |
535 gap = &dbg_breakp; | |
536 #ifdef FEAT_PROFILE | |
537 if (eap->cmdidx == CMD_profile) | |
538 gap = &prof_ga; | |
539 #endif | |
540 | |
541 if (dbg_parsearg(eap->arg, gap) == OK) | |
542 { | |
543 bp = &DEBUGGY(gap, gap->ga_len); | |
544 bp->dbg_forceit = eap->forceit; | |
545 | |
7 | 546 pat = file_pat_to_reg_pat(bp->dbg_name, NULL, NULL, FALSE); |
547 if (pat != NULL) | |
548 { | |
549 bp->dbg_prog = vim_regcomp(pat, RE_MAGIC + RE_STRING); | |
550 vim_free(pat); | |
551 } | |
552 if (pat == NULL || bp->dbg_prog == NULL) | |
553 vim_free(bp->dbg_name); | |
554 else | |
555 { | |
556 if (bp->dbg_lnum == 0) /* default line number is 1 */ | |
557 bp->dbg_lnum = 1; | |
170 | 558 #ifdef FEAT_PROFILE |
559 if (eap->cmdidx != CMD_profile) | |
560 #endif | |
561 { | |
562 DEBUGGY(gap, gap->ga_len).dbg_nr = ++last_breakp; | |
563 ++debug_tick; | |
564 } | |
565 ++gap->ga_len; | |
7 | 566 } |
567 } | |
568 } | |
569 | |
570 /* | |
571 * ":debuggreedy". | |
572 */ | |
573 void | |
574 ex_debuggreedy(eap) | |
575 exarg_T *eap; | |
576 { | |
577 if (eap->addr_count == 0 || eap->line2 != 0) | |
578 debug_greedy = TRUE; | |
579 else | |
580 debug_greedy = FALSE; | |
581 } | |
582 | |
583 /* | |
364 | 584 * ":breakdel" and ":profdel". |
7 | 585 */ |
586 void | |
587 ex_breakdel(eap) | |
588 exarg_T *eap; | |
589 { | |
590 struct debuggy *bp, *bpi; | |
591 int nr; | |
592 int todel = -1; | |
359 | 593 int del_all = FALSE; |
7 | 594 int i; |
595 linenr_T best_lnum = 0; | |
364 | 596 garray_T *gap; |
597 | |
598 gap = &dbg_breakp; | |
599 #ifdef FEAT_PROFILE | |
600 if (eap->cmdidx == CMD_profdel) | |
601 gap = &prof_ga; | |
602 #endif | |
7 | 603 |
604 if (vim_isdigit(*eap->arg)) | |
605 { | |
606 /* ":breakdel {nr}" */ | |
607 nr = atol((char *)eap->arg); | |
364 | 608 for (i = 0; i < gap->ga_len; ++i) |
609 if (DEBUGGY(gap, i).dbg_nr == nr) | |
7 | 610 { |
611 todel = i; | |
612 break; | |
613 } | |
614 } | |
359 | 615 else if (*eap->arg == '*') |
616 { | |
617 todel = 0; | |
618 del_all = TRUE; | |
619 } | |
7 | 620 else |
621 { | |
622 /* ":breakdel {func|file} [lnum] {name}" */ | |
364 | 623 if (dbg_parsearg(eap->arg, gap) == FAIL) |
7 | 624 return; |
364 | 625 bp = &DEBUGGY(gap, gap->ga_len); |
626 for (i = 0; i < gap->ga_len; ++i) | |
7 | 627 { |
364 | 628 bpi = &DEBUGGY(gap, i); |
7 | 629 if (bp->dbg_type == bpi->dbg_type |
630 && STRCMP(bp->dbg_name, bpi->dbg_name) == 0 | |
631 && (bp->dbg_lnum == bpi->dbg_lnum | |
632 || (bp->dbg_lnum == 0 | |
633 && (best_lnum == 0 | |
634 || bpi->dbg_lnum < best_lnum)))) | |
635 { | |
636 todel = i; | |
637 best_lnum = bpi->dbg_lnum; | |
638 } | |
639 } | |
640 vim_free(bp->dbg_name); | |
641 } | |
642 | |
643 if (todel < 0) | |
644 EMSG2(_("E161: Breakpoint not found: %s"), eap->arg); | |
645 else | |
364 | 646 { |
647 while (gap->ga_len > 0) | |
359 | 648 { |
364 | 649 vim_free(DEBUGGY(gap, todel).dbg_name); |
650 vim_free(DEBUGGY(gap, todel).dbg_prog); | |
651 --gap->ga_len; | |
652 if (todel < gap->ga_len) | |
653 mch_memmove(&DEBUGGY(gap, todel), &DEBUGGY(gap, todel + 1), | |
654 (gap->ga_len - todel) * sizeof(struct debuggy)); | |
655 #ifdef FEAT_PROFILE | |
656 if (eap->cmdidx == CMD_breakdel) | |
657 #endif | |
658 ++debug_tick; | |
359 | 659 if (!del_all) |
660 break; | |
661 } | |
364 | 662 |
663 /* If all breakpoints were removed clear the array. */ | |
664 if (gap->ga_len == 0) | |
665 ga_clear(gap); | |
666 } | |
7 | 667 } |
668 | |
669 /* | |
670 * ":breaklist". | |
671 */ | |
672 void | |
673 ex_breaklist(eap) | |
1876 | 674 exarg_T *eap UNUSED; |
7 | 675 { |
676 struct debuggy *bp; | |
677 int i; | |
678 | |
679 if (dbg_breakp.ga_len == 0) | |
680 MSG(_("No breakpoints defined")); | |
681 else | |
682 for (i = 0; i < dbg_breakp.ga_len; ++i) | |
683 { | |
684 bp = &BREAKP(i); | |
2921 | 685 if (bp->dbg_type == DBG_FILE) |
686 home_replace(NULL, bp->dbg_name, NameBuff, MAXPATHL, TRUE); | |
7 | 687 smsg((char_u *)_("%3d %s %s line %ld"), |
688 bp->dbg_nr, | |
689 bp->dbg_type == DBG_FUNC ? "func" : "file", | |
2921 | 690 bp->dbg_type == DBG_FUNC ? bp->dbg_name : NameBuff, |
7 | 691 (long)bp->dbg_lnum); |
692 } | |
693 } | |
694 | |
695 /* | |
696 * Find a breakpoint for a function or sourced file. | |
697 * Returns line number at which to break; zero when no matching breakpoint. | |
698 */ | |
699 linenr_T | |
700 dbg_find_breakpoint(file, fname, after) | |
701 int file; /* TRUE for a file, FALSE for a function */ | |
702 char_u *fname; /* file or function name */ | |
703 linenr_T after; /* after this line number */ | |
704 { | |
170 | 705 return debuggy_find(file, fname, after, &dbg_breakp, NULL); |
706 } | |
707 | |
708 #if defined(FEAT_PROFILE) || defined(PROTO) | |
709 /* | |
710 * Return TRUE if profiling is on for a function or sourced file. | |
711 */ | |
712 int | |
713 has_profiling(file, fname, fp) | |
714 int file; /* TRUE for a file, FALSE for a function */ | |
715 char_u *fname; /* file or function name */ | |
716 int *fp; /* return: forceit */ | |
717 { | |
718 return (debuggy_find(file, fname, (linenr_T)0, &prof_ga, fp) | |
719 != (linenr_T)0); | |
720 } | |
721 #endif | |
722 | |
723 /* | |
724 * Common code for dbg_find_breakpoint() and has_profiling(). | |
725 */ | |
726 static linenr_T | |
727 debuggy_find(file, fname, after, gap, fp) | |
728 int file; /* TRUE for a file, FALSE for a function */ | |
729 char_u *fname; /* file or function name */ | |
730 linenr_T after; /* after this line number */ | |
731 garray_T *gap; /* either &dbg_breakp or &prof_ga */ | |
732 int *fp; /* if not NULL: return forceit */ | |
733 { | |
7 | 734 struct debuggy *bp; |
735 int i; | |
736 linenr_T lnum = 0; | |
737 regmatch_T regmatch; | |
738 char_u *name = fname; | |
739 int prev_got_int; | |
740 | |
170 | 741 /* Return quickly when there are no breakpoints. */ |
742 if (gap->ga_len == 0) | |
743 return (linenr_T)0; | |
744 | |
7 | 745 /* Replace K_SNR in function name with "<SNR>". */ |
746 if (!file && fname[0] == K_SPECIAL) | |
747 { | |
748 name = alloc((unsigned)STRLEN(fname) + 3); | |
749 if (name == NULL) | |
750 name = fname; | |
751 else | |
752 { | |
753 STRCPY(name, "<SNR>"); | |
754 STRCPY(name + 5, fname + 3); | |
755 } | |
756 } | |
757 | |
170 | 758 for (i = 0; i < gap->ga_len; ++i) |
759 { | |
760 /* Skip entries that are not useful or are for a line that is beyond | |
761 * an already found breakpoint. */ | |
762 bp = &DEBUGGY(gap, i); | |
763 if (((bp->dbg_type == DBG_FILE) == file && ( | |
764 #ifdef FEAT_PROFILE | |
765 gap == &prof_ga || | |
766 #endif | |
767 (bp->dbg_lnum > after && (lnum == 0 || bp->dbg_lnum < lnum))))) | |
7 | 768 { |
769 regmatch.regprog = bp->dbg_prog; | |
770 regmatch.rm_ic = FALSE; | |
771 /* | |
170 | 772 * Save the value of got_int and reset it. We don't want a |
773 * previous interruption cancel matching, only hitting CTRL-C | |
774 * while matching should abort it. | |
7 | 775 */ |
776 prev_got_int = got_int; | |
777 got_int = FALSE; | |
778 if (vim_regexec(®match, name, (colnr_T)0)) | |
170 | 779 { |
7 | 780 lnum = bp->dbg_lnum; |
170 | 781 if (fp != NULL) |
782 *fp = bp->dbg_forceit; | |
783 } | |
7 | 784 got_int |= prev_got_int; |
785 } | |
786 } | |
787 if (name != fname) | |
788 vim_free(name); | |
789 | |
790 return lnum; | |
791 } | |
792 | |
793 /* | |
794 * Called when a breakpoint was encountered. | |
795 */ | |
796 void | |
797 dbg_breakpoint(name, lnum) | |
798 char_u *name; | |
799 linenr_T lnum; | |
800 { | |
801 /* We need to check if this line is actually executed in do_one_cmd() */ | |
802 debug_breakpoint_name = name; | |
803 debug_breakpoint_lnum = lnum; | |
804 } | |
170 | 805 |
806 | |
794 | 807 # if defined(FEAT_PROFILE) || defined(FEAT_RELTIME) || defined(PROTO) |
170 | 808 /* |
809 * Store the current time in "tm". | |
810 */ | |
811 void | |
812 profile_start(tm) | |
813 proftime_T *tm; | |
814 { | |
177 | 815 # ifdef WIN3264 |
816 QueryPerformanceCounter(tm); | |
817 # else | |
170 | 818 gettimeofday(tm, NULL); |
177 | 819 # endif |
170 | 820 } |
821 | |
822 /* | |
823 * Compute the elapsed time from "tm" till now and store in "tm". | |
824 */ | |
825 void | |
826 profile_end(tm) | |
827 proftime_T *tm; | |
828 { | |
829 proftime_T now; | |
830 | |
177 | 831 # ifdef WIN3264 |
832 QueryPerformanceCounter(&now); | |
833 tm->QuadPart = now.QuadPart - tm->QuadPart; | |
834 # else | |
170 | 835 gettimeofday(&now, NULL); |
836 tm->tv_usec = now.tv_usec - tm->tv_usec; | |
837 tm->tv_sec = now.tv_sec - tm->tv_sec; | |
838 if (tm->tv_usec < 0) | |
839 { | |
840 tm->tv_usec += 1000000; | |
841 --tm->tv_sec; | |
842 } | |
177 | 843 # endif |
170 | 844 } |
845 | |
846 /* | |
847 * Subtract the time "tm2" from "tm". | |
848 */ | |
849 void | |
850 profile_sub(tm, tm2) | |
851 proftime_T *tm, *tm2; | |
852 { | |
177 | 853 # ifdef WIN3264 |
854 tm->QuadPart -= tm2->QuadPart; | |
855 # else | |
170 | 856 tm->tv_usec -= tm2->tv_usec; |
857 tm->tv_sec -= tm2->tv_sec; | |
858 if (tm->tv_usec < 0) | |
859 { | |
860 tm->tv_usec += 1000000; | |
861 --tm->tv_sec; | |
862 } | |
177 | 863 # endif |
170 | 864 } |
865 | |
866 /* | |
794 | 867 * Return a string that represents the time in "tm". |
868 * Uses a static buffer! | |
869 */ | |
870 char * | |
871 profile_msg(tm) | |
872 proftime_T *tm; | |
873 { | |
874 static char buf[50]; | |
875 | |
876 # ifdef WIN3264 | |
877 LARGE_INTEGER fr; | |
878 | |
879 QueryPerformanceFrequency(&fr); | |
880 sprintf(buf, "%10.6lf", (double)tm->QuadPart / (double)fr.QuadPart); | |
881 # else | |
882 sprintf(buf, "%3ld.%06ld", (long)tm->tv_sec, (long)tm->tv_usec); | |
1496 | 883 # endif |
794 | 884 return buf; |
885 } | |
886 | |
887 /* | |
1496 | 888 * Put the time "msec" past now in "tm". |
794 | 889 */ |
1496 | 890 void |
891 profile_setlimit(msec, tm) | |
892 long msec; | |
893 proftime_T *tm; | |
894 { | |
895 if (msec <= 0) /* no limit */ | |
896 profile_zero(tm); | |
897 else | |
898 { | |
899 # ifdef WIN3264 | |
900 LARGE_INTEGER fr; | |
901 | |
902 QueryPerformanceCounter(tm); | |
903 QueryPerformanceFrequency(&fr); | |
1517 | 904 tm->QuadPart += (LONGLONG)((double)msec / 1000.0 * (double)fr.QuadPart); |
1496 | 905 # else |
906 long usec; | |
907 | |
908 gettimeofday(tm, NULL); | |
909 usec = (long)tm->tv_usec + (long)msec * 1000; | |
910 tm->tv_usec = usec % 1000000L; | |
911 tm->tv_sec += usec / 1000000L; | |
912 # endif | |
913 } | |
914 } | |
915 | |
916 /* | |
917 * Return TRUE if the current time is past "tm". | |
918 */ | |
919 int | |
920 profile_passed_limit(tm) | |
921 proftime_T *tm; | |
922 { | |
923 proftime_T now; | |
924 | |
925 # ifdef WIN3264 | |
926 if (tm->QuadPart == 0) /* timer was not set */ | |
927 return FALSE; | |
928 QueryPerformanceCounter(&now); | |
929 return (now.QuadPart > tm->QuadPart); | |
930 # else | |
931 if (tm->tv_sec == 0) /* timer was not set */ | |
932 return FALSE; | |
933 gettimeofday(&now, NULL); | |
934 return (now.tv_sec > tm->tv_sec | |
935 || (now.tv_sec == tm->tv_sec && now.tv_usec > tm->tv_usec)); | |
936 # endif | |
937 } | |
794 | 938 |
939 /* | |
940 * Set the time in "tm" to zero. | |
941 */ | |
942 void | |
943 profile_zero(tm) | |
944 proftime_T *tm; | |
945 { | |
946 # ifdef WIN3264 | |
947 tm->QuadPart = 0; | |
948 # else | |
949 tm->tv_usec = 0; | |
950 tm->tv_sec = 0; | |
951 # endif | |
952 } | |
953 | |
1496 | 954 # endif /* FEAT_PROFILE || FEAT_RELTIME */ |
955 | |
956 # if defined(FEAT_PROFILE) || defined(PROTO) | |
957 /* | |
958 * Functions for profiling. | |
959 */ | |
960 static void script_do_profile __ARGS((scriptitem_T *si)); | |
961 static void script_dump_profile __ARGS((FILE *fd)); | |
962 static proftime_T prof_wait_time; | |
963 | |
794 | 964 /* |
170 | 965 * Add the time "tm2" to "tm". |
966 */ | |
967 void | |
968 profile_add(tm, tm2) | |
969 proftime_T *tm, *tm2; | |
970 { | |
177 | 971 # ifdef WIN3264 |
972 tm->QuadPart += tm2->QuadPart; | |
973 # else | |
170 | 974 tm->tv_usec += tm2->tv_usec; |
975 tm->tv_sec += tm2->tv_sec; | |
976 if (tm->tv_usec >= 1000000) | |
977 { | |
978 tm->tv_usec -= 1000000; | |
979 ++tm->tv_sec; | |
980 } | |
177 | 981 # endif |
170 | 982 } |
983 | |
984 /* | |
720 | 985 * Add the "self" time from the total time and the children's time. |
986 */ | |
987 void | |
988 profile_self(self, total, children) | |
989 proftime_T *self, *total, *children; | |
990 { | |
991 /* Check that the result won't be negative. Can happen with recursive | |
992 * calls. */ | |
993 #ifdef WIN3264 | |
994 if (total->QuadPart <= children->QuadPart) | |
995 return; | |
996 #else | |
997 if (total->tv_sec < children->tv_sec | |
998 || (total->tv_sec == children->tv_sec | |
999 && total->tv_usec <= children->tv_usec)) | |
1000 return; | |
1001 #endif | |
1002 profile_add(self, total); | |
1003 profile_sub(self, children); | |
1004 } | |
1005 | |
1006 /* | |
170 | 1007 * Get the current waittime. |
1008 */ | |
1009 void | |
1010 profile_get_wait(tm) | |
1011 proftime_T *tm; | |
1012 { | |
1013 *tm = prof_wait_time; | |
1014 } | |
1015 | |
1016 /* | |
1017 * Subtract the passed waittime since "tm" from "tma". | |
1018 */ | |
1019 void | |
1020 profile_sub_wait(tm, tma) | |
1021 proftime_T *tm, *tma; | |
1022 { | |
1023 proftime_T tm3 = prof_wait_time; | |
1024 | |
1025 profile_sub(&tm3, tm); | |
1026 profile_sub(tma, &tm3); | |
1027 } | |
1028 | |
1029 /* | |
1030 * Return TRUE if "tm1" and "tm2" are equal. | |
1031 */ | |
1032 int | |
1033 profile_equal(tm1, tm2) | |
1034 proftime_T *tm1, *tm2; | |
1035 { | |
177 | 1036 # ifdef WIN3264 |
1037 return (tm1->QuadPart == tm2->QuadPart); | |
1038 # else | |
170 | 1039 return (tm1->tv_usec == tm2->tv_usec && tm1->tv_sec == tm2->tv_sec); |
177 | 1040 # endif |
1041 } | |
1042 | |
1043 /* | |
1044 * Return <0, 0 or >0 if "tm1" < "tm2", "tm1" == "tm2" or "tm1" > "tm2" | |
1045 */ | |
1046 int | |
1047 profile_cmp(tm1, tm2) | |
1048 proftime_T *tm1, *tm2; | |
1049 { | |
1050 # ifdef WIN3264 | |
1051 return (int)(tm2->QuadPart - tm1->QuadPart); | |
1052 # else | |
1053 if (tm1->tv_sec == tm2->tv_sec) | |
1054 return tm2->tv_usec - tm1->tv_usec; | |
1055 return tm2->tv_sec - tm1->tv_sec; | |
1056 # endif | |
170 | 1057 } |
1058 | |
1059 static char_u *profile_fname = NULL; | |
790 | 1060 static proftime_T pause_time; |
170 | 1061 |
1062 /* | |
1063 * ":profile cmd args" | |
1064 */ | |
1065 void | |
1066 ex_profile(eap) | |
1067 exarg_T *eap; | |
1068 { | |
1069 char_u *e; | |
1070 int len; | |
1071 | |
1072 e = skiptowhite(eap->arg); | |
835 | 1073 len = (int)(e - eap->arg); |
170 | 1074 e = skipwhite(e); |
1075 | |
1076 if (len == 5 && STRNCMP(eap->arg, "start", 5) == 0 && *e != NUL) | |
1077 { | |
1078 vim_free(profile_fname); | |
1079 profile_fname = vim_strsave(e); | |
790 | 1080 do_profiling = PROF_YES; |
170 | 1081 profile_zero(&prof_wait_time); |
1082 set_vim_var_nr(VV_PROFILING, 1L); | |
1083 } | |
790 | 1084 else if (do_profiling == PROF_NONE) |
2085
5a84b6388a55
updated for version 7.2.369
Bram Moolenaar <bram@zimbu.org>
parents:
2068
diff
changeset
|
1085 EMSG(_("E750: First use \":profile start {fname}\"")); |
790 | 1086 else if (STRCMP(eap->arg, "pause") == 0) |
1087 { | |
1088 if (do_profiling == PROF_YES) | |
1089 profile_start(&pause_time); | |
1090 do_profiling = PROF_PAUSED; | |
1091 } | |
1092 else if (STRCMP(eap->arg, "continue") == 0) | |
1093 { | |
1094 if (do_profiling == PROF_PAUSED) | |
1095 { | |
1096 profile_end(&pause_time); | |
1097 profile_add(&prof_wait_time, &pause_time); | |
1098 } | |
1099 do_profiling = PROF_YES; | |
1100 } | |
170 | 1101 else |
1102 { | |
1103 /* The rest is similar to ":breakadd". */ | |
1104 ex_breakadd(eap); | |
1105 } | |
1106 } | |
1107 | |
2068
98a2a6e6b966
updated for version 7.2.353
Bram Moolenaar <bram@zimbu.org>
parents:
2058
diff
changeset
|
1108 /* Command line expansion for :profile. */ |
98a2a6e6b966
updated for version 7.2.353
Bram Moolenaar <bram@zimbu.org>
parents:
2058
diff
changeset
|
1109 static enum |
98a2a6e6b966
updated for version 7.2.353
Bram Moolenaar <bram@zimbu.org>
parents:
2058
diff
changeset
|
1110 { |
98a2a6e6b966
updated for version 7.2.353
Bram Moolenaar <bram@zimbu.org>
parents:
2058
diff
changeset
|
1111 PEXP_SUBCMD, /* expand :profile sub-commands */ |
2711 | 1112 PEXP_FUNC /* expand :profile func {funcname} */ |
2068
98a2a6e6b966
updated for version 7.2.353
Bram Moolenaar <bram@zimbu.org>
parents:
2058
diff
changeset
|
1113 } pexpand_what; |
98a2a6e6b966
updated for version 7.2.353
Bram Moolenaar <bram@zimbu.org>
parents:
2058
diff
changeset
|
1114 |
98a2a6e6b966
updated for version 7.2.353
Bram Moolenaar <bram@zimbu.org>
parents:
2058
diff
changeset
|
1115 static char *pexpand_cmds[] = { |
98a2a6e6b966
updated for version 7.2.353
Bram Moolenaar <bram@zimbu.org>
parents:
2058
diff
changeset
|
1116 "start", |
98a2a6e6b966
updated for version 7.2.353
Bram Moolenaar <bram@zimbu.org>
parents:
2058
diff
changeset
|
1117 #define PROFCMD_START 0 |
98a2a6e6b966
updated for version 7.2.353
Bram Moolenaar <bram@zimbu.org>
parents:
2058
diff
changeset
|
1118 "pause", |
98a2a6e6b966
updated for version 7.2.353
Bram Moolenaar <bram@zimbu.org>
parents:
2058
diff
changeset
|
1119 #define PROFCMD_PAUSE 1 |
98a2a6e6b966
updated for version 7.2.353
Bram Moolenaar <bram@zimbu.org>
parents:
2058
diff
changeset
|
1120 "continue", |
98a2a6e6b966
updated for version 7.2.353
Bram Moolenaar <bram@zimbu.org>
parents:
2058
diff
changeset
|
1121 #define PROFCMD_CONTINUE 2 |
98a2a6e6b966
updated for version 7.2.353
Bram Moolenaar <bram@zimbu.org>
parents:
2058
diff
changeset
|
1122 "func", |
98a2a6e6b966
updated for version 7.2.353
Bram Moolenaar <bram@zimbu.org>
parents:
2058
diff
changeset
|
1123 #define PROFCMD_FUNC 3 |
98a2a6e6b966
updated for version 7.2.353
Bram Moolenaar <bram@zimbu.org>
parents:
2058
diff
changeset
|
1124 "file", |
98a2a6e6b966
updated for version 7.2.353
Bram Moolenaar <bram@zimbu.org>
parents:
2058
diff
changeset
|
1125 #define PROFCMD_FILE 4 |
98a2a6e6b966
updated for version 7.2.353
Bram Moolenaar <bram@zimbu.org>
parents:
2058
diff
changeset
|
1126 NULL |
98a2a6e6b966
updated for version 7.2.353
Bram Moolenaar <bram@zimbu.org>
parents:
2058
diff
changeset
|
1127 #define PROFCMD_LAST 5 |
98a2a6e6b966
updated for version 7.2.353
Bram Moolenaar <bram@zimbu.org>
parents:
2058
diff
changeset
|
1128 }; |
98a2a6e6b966
updated for version 7.2.353
Bram Moolenaar <bram@zimbu.org>
parents:
2058
diff
changeset
|
1129 |
98a2a6e6b966
updated for version 7.2.353
Bram Moolenaar <bram@zimbu.org>
parents:
2058
diff
changeset
|
1130 /* |
98a2a6e6b966
updated for version 7.2.353
Bram Moolenaar <bram@zimbu.org>
parents:
2058
diff
changeset
|
1131 * Function given to ExpandGeneric() to obtain the profile command |
98a2a6e6b966
updated for version 7.2.353
Bram Moolenaar <bram@zimbu.org>
parents:
2058
diff
changeset
|
1132 * specific expansion. |
98a2a6e6b966
updated for version 7.2.353
Bram Moolenaar <bram@zimbu.org>
parents:
2058
diff
changeset
|
1133 */ |
98a2a6e6b966
updated for version 7.2.353
Bram Moolenaar <bram@zimbu.org>
parents:
2058
diff
changeset
|
1134 char_u * |
98a2a6e6b966
updated for version 7.2.353
Bram Moolenaar <bram@zimbu.org>
parents:
2058
diff
changeset
|
1135 get_profile_name(xp, idx) |
98a2a6e6b966
updated for version 7.2.353
Bram Moolenaar <bram@zimbu.org>
parents:
2058
diff
changeset
|
1136 expand_T *xp UNUSED; |
98a2a6e6b966
updated for version 7.2.353
Bram Moolenaar <bram@zimbu.org>
parents:
2058
diff
changeset
|
1137 int idx; |
98a2a6e6b966
updated for version 7.2.353
Bram Moolenaar <bram@zimbu.org>
parents:
2058
diff
changeset
|
1138 { |
98a2a6e6b966
updated for version 7.2.353
Bram Moolenaar <bram@zimbu.org>
parents:
2058
diff
changeset
|
1139 switch (pexpand_what) |
98a2a6e6b966
updated for version 7.2.353
Bram Moolenaar <bram@zimbu.org>
parents:
2058
diff
changeset
|
1140 { |
98a2a6e6b966
updated for version 7.2.353
Bram Moolenaar <bram@zimbu.org>
parents:
2058
diff
changeset
|
1141 case PEXP_SUBCMD: |
98a2a6e6b966
updated for version 7.2.353
Bram Moolenaar <bram@zimbu.org>
parents:
2058
diff
changeset
|
1142 return (char_u *)pexpand_cmds[idx]; |
98a2a6e6b966
updated for version 7.2.353
Bram Moolenaar <bram@zimbu.org>
parents:
2058
diff
changeset
|
1143 /* case PEXP_FUNC: TODO */ |
98a2a6e6b966
updated for version 7.2.353
Bram Moolenaar <bram@zimbu.org>
parents:
2058
diff
changeset
|
1144 default: |
98a2a6e6b966
updated for version 7.2.353
Bram Moolenaar <bram@zimbu.org>
parents:
2058
diff
changeset
|
1145 return NULL; |
98a2a6e6b966
updated for version 7.2.353
Bram Moolenaar <bram@zimbu.org>
parents:
2058
diff
changeset
|
1146 } |
98a2a6e6b966
updated for version 7.2.353
Bram Moolenaar <bram@zimbu.org>
parents:
2058
diff
changeset
|
1147 } |
98a2a6e6b966
updated for version 7.2.353
Bram Moolenaar <bram@zimbu.org>
parents:
2058
diff
changeset
|
1148 |
98a2a6e6b966
updated for version 7.2.353
Bram Moolenaar <bram@zimbu.org>
parents:
2058
diff
changeset
|
1149 /* |
98a2a6e6b966
updated for version 7.2.353
Bram Moolenaar <bram@zimbu.org>
parents:
2058
diff
changeset
|
1150 * Handle command line completion for :profile command. |
98a2a6e6b966
updated for version 7.2.353
Bram Moolenaar <bram@zimbu.org>
parents:
2058
diff
changeset
|
1151 */ |
98a2a6e6b966
updated for version 7.2.353
Bram Moolenaar <bram@zimbu.org>
parents:
2058
diff
changeset
|
1152 void |
98a2a6e6b966
updated for version 7.2.353
Bram Moolenaar <bram@zimbu.org>
parents:
2058
diff
changeset
|
1153 set_context_in_profile_cmd(xp, arg) |
98a2a6e6b966
updated for version 7.2.353
Bram Moolenaar <bram@zimbu.org>
parents:
2058
diff
changeset
|
1154 expand_T *xp; |
98a2a6e6b966
updated for version 7.2.353
Bram Moolenaar <bram@zimbu.org>
parents:
2058
diff
changeset
|
1155 char_u *arg; |
98a2a6e6b966
updated for version 7.2.353
Bram Moolenaar <bram@zimbu.org>
parents:
2058
diff
changeset
|
1156 { |
98a2a6e6b966
updated for version 7.2.353
Bram Moolenaar <bram@zimbu.org>
parents:
2058
diff
changeset
|
1157 char_u *end_subcmd; |
98a2a6e6b966
updated for version 7.2.353
Bram Moolenaar <bram@zimbu.org>
parents:
2058
diff
changeset
|
1158 |
98a2a6e6b966
updated for version 7.2.353
Bram Moolenaar <bram@zimbu.org>
parents:
2058
diff
changeset
|
1159 /* Default: expand subcommands. */ |
98a2a6e6b966
updated for version 7.2.353
Bram Moolenaar <bram@zimbu.org>
parents:
2058
diff
changeset
|
1160 xp->xp_context = EXPAND_PROFILE; |
98a2a6e6b966
updated for version 7.2.353
Bram Moolenaar <bram@zimbu.org>
parents:
2058
diff
changeset
|
1161 pexpand_what = PEXP_SUBCMD; |
98a2a6e6b966
updated for version 7.2.353
Bram Moolenaar <bram@zimbu.org>
parents:
2058
diff
changeset
|
1162 xp->xp_pattern = arg; |
98a2a6e6b966
updated for version 7.2.353
Bram Moolenaar <bram@zimbu.org>
parents:
2058
diff
changeset
|
1163 |
98a2a6e6b966
updated for version 7.2.353
Bram Moolenaar <bram@zimbu.org>
parents:
2058
diff
changeset
|
1164 end_subcmd = skiptowhite(arg); |
98a2a6e6b966
updated for version 7.2.353
Bram Moolenaar <bram@zimbu.org>
parents:
2058
diff
changeset
|
1165 if (*end_subcmd == NUL) |
98a2a6e6b966
updated for version 7.2.353
Bram Moolenaar <bram@zimbu.org>
parents:
2058
diff
changeset
|
1166 return; |
98a2a6e6b966
updated for version 7.2.353
Bram Moolenaar <bram@zimbu.org>
parents:
2058
diff
changeset
|
1167 |
2100
7d121c69f540
updated for version 7.2.383
Bram Moolenaar <bram@zimbu.org>
parents:
2085
diff
changeset
|
1168 if (end_subcmd - arg == 5 && STRNCMP(arg, "start", 5) == 0) |
2068
98a2a6e6b966
updated for version 7.2.353
Bram Moolenaar <bram@zimbu.org>
parents:
2058
diff
changeset
|
1169 { |
98a2a6e6b966
updated for version 7.2.353
Bram Moolenaar <bram@zimbu.org>
parents:
2058
diff
changeset
|
1170 xp->xp_context = EXPAND_FILES; |
98a2a6e6b966
updated for version 7.2.353
Bram Moolenaar <bram@zimbu.org>
parents:
2058
diff
changeset
|
1171 xp->xp_pattern = skipwhite(end_subcmd); |
98a2a6e6b966
updated for version 7.2.353
Bram Moolenaar <bram@zimbu.org>
parents:
2058
diff
changeset
|
1172 return; |
98a2a6e6b966
updated for version 7.2.353
Bram Moolenaar <bram@zimbu.org>
parents:
2058
diff
changeset
|
1173 } |
98a2a6e6b966
updated for version 7.2.353
Bram Moolenaar <bram@zimbu.org>
parents:
2058
diff
changeset
|
1174 |
98a2a6e6b966
updated for version 7.2.353
Bram Moolenaar <bram@zimbu.org>
parents:
2058
diff
changeset
|
1175 /* TODO: expand function names after "func" */ |
98a2a6e6b966
updated for version 7.2.353
Bram Moolenaar <bram@zimbu.org>
parents:
2058
diff
changeset
|
1176 xp->xp_context = EXPAND_NOTHING; |
98a2a6e6b966
updated for version 7.2.353
Bram Moolenaar <bram@zimbu.org>
parents:
2058
diff
changeset
|
1177 } |
98a2a6e6b966
updated for version 7.2.353
Bram Moolenaar <bram@zimbu.org>
parents:
2058
diff
changeset
|
1178 |
170 | 1179 /* |
1180 * Dump the profiling info. | |
1181 */ | |
1182 void | |
1183 profile_dump() | |
1184 { | |
1185 FILE *fd; | |
1186 | |
1187 if (profile_fname != NULL) | |
1188 { | |
531 | 1189 fd = mch_fopen((char *)profile_fname, "w"); |
170 | 1190 if (fd == NULL) |
1191 EMSG2(_(e_notopen), profile_fname); | |
1192 else | |
1193 { | |
177 | 1194 script_dump_profile(fd); |
170 | 1195 func_dump_profile(fd); |
1196 fclose(fd); | |
1197 } | |
1198 } | |
1199 } | |
1200 | |
1201 /* | |
1202 * Start profiling script "fp". | |
1203 */ | |
1204 static void | |
1205 script_do_profile(si) | |
1206 scriptitem_T *si; | |
1207 { | |
1208 si->sn_pr_count = 0; | |
1209 profile_zero(&si->sn_pr_total); | |
1210 profile_zero(&si->sn_pr_self); | |
1211 | |
1212 ga_init2(&si->sn_prl_ga, sizeof(sn_prl_T), 100); | |
1213 si->sn_prl_idx = -1; | |
1214 si->sn_prof_on = TRUE; | |
1215 si->sn_pr_nest = 0; | |
1216 } | |
1217 | |
1218 /* | |
1219 * save time when starting to invoke another script or function. | |
1220 */ | |
1221 void | |
1222 script_prof_save(tm) | |
1223 proftime_T *tm; /* place to store wait time */ | |
1224 { | |
1225 scriptitem_T *si; | |
1226 | |
1227 if (current_SID > 0 && current_SID <= script_items.ga_len) | |
1228 { | |
1229 si = &SCRIPT_ITEM(current_SID); | |
1230 if (si->sn_prof_on && si->sn_pr_nest++ == 0) | |
1231 profile_start(&si->sn_pr_child); | |
1232 } | |
1233 profile_get_wait(tm); | |
1234 } | |
1235 | |
1236 /* | |
1237 * Count time spent in children after invoking another script or function. | |
1238 */ | |
1239 void | |
1240 script_prof_restore(tm) | |
1241 proftime_T *tm; | |
1242 { | |
1243 scriptitem_T *si; | |
1244 | |
1245 if (current_SID > 0 && current_SID <= script_items.ga_len) | |
1246 { | |
1247 si = &SCRIPT_ITEM(current_SID); | |
1248 if (si->sn_prof_on && --si->sn_pr_nest == 0) | |
1249 { | |
1250 profile_end(&si->sn_pr_child); | |
1251 profile_sub_wait(tm, &si->sn_pr_child); /* don't count wait time */ | |
1252 profile_add(&si->sn_pr_children, &si->sn_pr_child); | |
1253 profile_add(&si->sn_prl_children, &si->sn_pr_child); | |
1254 } | |
1255 } | |
1256 } | |
1257 | |
1258 static proftime_T inchar_time; | |
1259 | |
1260 /* | |
1261 * Called when starting to wait for the user to type a character. | |
1262 */ | |
1263 void | |
1264 prof_inchar_enter() | |
1265 { | |
1266 profile_start(&inchar_time); | |
1267 } | |
1268 | |
1269 /* | |
1270 * Called when finished waiting for the user to type a character. | |
1271 */ | |
1272 void | |
1273 prof_inchar_exit() | |
1274 { | |
1275 profile_end(&inchar_time); | |
1276 profile_add(&prof_wait_time, &inchar_time); | |
1277 } | |
1278 | |
1279 /* | |
1280 * Dump the profiling results for all scripts in file "fd". | |
1281 */ | |
1282 static void | |
1283 script_dump_profile(fd) | |
1284 FILE *fd; | |
1285 { | |
1286 int id; | |
1287 scriptitem_T *si; | |
1288 int i; | |
1289 FILE *sfd; | |
1290 sn_prl_T *pp; | |
1291 | |
1292 for (id = 1; id <= script_items.ga_len; ++id) | |
1293 { | |
1294 si = &SCRIPT_ITEM(id); | |
1295 if (si->sn_prof_on) | |
1296 { | |
1297 fprintf(fd, "SCRIPT %s\n", si->sn_name); | |
1298 if (si->sn_pr_count == 1) | |
1299 fprintf(fd, "Sourced 1 time\n"); | |
1300 else | |
1301 fprintf(fd, "Sourced %d times\n", si->sn_pr_count); | |
1302 fprintf(fd, "Total time: %s\n", profile_msg(&si->sn_pr_total)); | |
1303 fprintf(fd, " Self time: %s\n", profile_msg(&si->sn_pr_self)); | |
1304 fprintf(fd, "\n"); | |
1305 fprintf(fd, "count total (s) self (s)\n"); | |
1306 | |
531 | 1307 sfd = mch_fopen((char *)si->sn_name, "r"); |
170 | 1308 if (sfd == NULL) |
1309 fprintf(fd, "Cannot open file!\n"); | |
1310 else | |
1311 { | |
1312 for (i = 0; i < si->sn_prl_ga.ga_len; ++i) | |
1313 { | |
1314 if (vim_fgets(IObuff, IOSIZE, sfd)) | |
1315 break; | |
1316 pp = &PRL_ITEM(si, i); | |
1317 if (pp->snp_count > 0) | |
1318 { | |
1319 fprintf(fd, "%5d ", pp->snp_count); | |
1320 if (profile_equal(&pp->sn_prl_total, &pp->sn_prl_self)) | |
1321 fprintf(fd, " "); | |
1322 else | |
1323 fprintf(fd, "%s ", profile_msg(&pp->sn_prl_total)); | |
1324 fprintf(fd, "%s ", profile_msg(&pp->sn_prl_self)); | |
1325 } | |
1326 else | |
1327 fprintf(fd, " "); | |
1328 fprintf(fd, "%s", IObuff); | |
1329 } | |
1330 fclose(sfd); | |
1331 } | |
1332 fprintf(fd, "\n"); | |
1333 } | |
1334 } | |
1335 } | |
1336 | |
1337 /* | |
1338 * Return TRUE when a function defined in the current script should be | |
1339 * profiled. | |
1340 */ | |
1341 int | |
1342 prof_def_func() | |
1343 { | |
391 | 1344 if (current_SID > 0) |
1345 return SCRIPT_ITEM(current_SID).sn_pr_force; | |
1346 return FALSE; | |
170 | 1347 } |
1348 | |
1349 # endif | |
7 | 1350 #endif |
1351 | |
1352 /* | |
1353 * If 'autowrite' option set, try to write the file. | |
1354 * Careful: autocommands may make "buf" invalid! | |
1355 * | |
1356 * return FAIL for failure, OK otherwise | |
1357 */ | |
1358 int | |
1359 autowrite(buf, forceit) | |
1360 buf_T *buf; | |
1361 int forceit; | |
1362 { | |
1069 | 1363 int r; |
1364 | |
7 | 1365 if (!(p_aw || p_awa) || !p_write |
1366 #ifdef FEAT_QUICKFIX | |
1069 | 1367 /* never autowrite a "nofile" or "nowrite" buffer */ |
1368 || bt_dontwrite(buf) | |
7 | 1369 #endif |
1069 | 1370 || (!forceit && buf->b_p_ro) || buf->b_ffname == NULL) |
7 | 1371 return FAIL; |
1069 | 1372 r = buf_write_all(buf, forceit); |
1373 | |
1374 /* Writing may succeed but the buffer still changed, e.g., when there is a | |
1375 * conversion error. We do want to return FAIL then. */ | |
1376 if (buf_valid(buf) && bufIsChanged(buf)) | |
1377 r = FAIL; | |
1378 return r; | |
7 | 1379 } |
1380 | |
1381 /* | |
1382 * flush all buffers, except the ones that are readonly | |
1383 */ | |
1384 void | |
1385 autowrite_all() | |
1386 { | |
1387 buf_T *buf; | |
1388 | |
1389 if (!(p_aw || p_awa) || !p_write) | |
1390 return; | |
1391 for (buf = firstbuf; buf; buf = buf->b_next) | |
1392 if (bufIsChanged(buf) && !buf->b_p_ro) | |
1393 { | |
1394 (void)buf_write_all(buf, FALSE); | |
1395 #ifdef FEAT_AUTOCMD | |
1396 /* an autocommand may have deleted the buffer */ | |
1397 if (!buf_valid(buf)) | |
1398 buf = firstbuf; | |
1399 #endif | |
1400 } | |
1401 } | |
1402 | |
1403 /* | |
1404 * return TRUE if buffer was changed and cannot be abandoned. | |
1405 */ | |
1406 int | |
1407 check_changed(buf, checkaw, mult_win, forceit, allbuf) | |
1408 buf_T *buf; | |
1409 int checkaw; /* do autowrite if buffer was changed */ | |
1410 int mult_win; /* check also when several wins for the buf */ | |
1411 int forceit; | |
1876 | 1412 int allbuf UNUSED; /* may write all buffers */ |
7 | 1413 { |
1414 if ( !forceit | |
1415 && bufIsChanged(buf) | |
1416 && (mult_win || buf->b_nwindows <= 1) | |
1417 && (!checkaw || autowrite(buf, forceit) == FAIL)) | |
1418 { | |
1419 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) | |
1420 if ((p_confirm || cmdmod.confirm) && p_write) | |
1421 { | |
1422 buf_T *buf2; | |
1423 int count = 0; | |
1424 | |
1425 if (allbuf) | |
1426 for (buf2 = firstbuf; buf2 != NULL; buf2 = buf2->b_next) | |
1427 if (bufIsChanged(buf2) | |
1428 && (buf2->b_ffname != NULL | |
1429 # ifdef FEAT_BROWSE | |
1430 || cmdmod.browse | |
1431 # endif | |
1432 )) | |
1433 ++count; | |
1434 # ifdef FEAT_AUTOCMD | |
1435 if (!buf_valid(buf)) | |
1436 /* Autocommand deleted buffer, oops! It's not changed now. */ | |
1437 return FALSE; | |
1438 # endif | |
1439 dialog_changed(buf, count > 1); | |
1440 # ifdef FEAT_AUTOCMD | |
1441 if (!buf_valid(buf)) | |
1442 /* Autocommand deleted buffer, oops! It's not changed now. */ | |
1443 return FALSE; | |
1444 # endif | |
1445 return bufIsChanged(buf); | |
1446 } | |
1447 #endif | |
1448 EMSG(_(e_nowrtmsg)); | |
1449 return TRUE; | |
1450 } | |
1451 return FALSE; | |
1452 } | |
1453 | |
1454 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO) | |
1455 | |
1456 #if defined(FEAT_BROWSE) || defined(PROTO) | |
1457 /* | |
1458 * When wanting to write a file without a file name, ask the user for a name. | |
1459 */ | |
1460 void | |
1461 browse_save_fname(buf) | |
1462 buf_T *buf; | |
1463 { | |
1464 if (buf->b_fname == NULL) | |
1465 { | |
1466 char_u *fname; | |
1467 | |
29 | 1468 fname = do_browse(BROWSE_SAVE, (char_u *)_("Save As"), |
1469 NULL, NULL, NULL, NULL, buf); | |
7 | 1470 if (fname != NULL) |
1471 { | |
1472 if (setfname(buf, fname, NULL, TRUE) == OK) | |
1473 buf->b_flags |= BF_NOTEDITED; | |
1474 vim_free(fname); | |
1475 } | |
1476 } | |
1477 } | |
1478 #endif | |
1479 | |
1480 /* | |
2849 | 1481 * Ask the user what to do when abandoning a changed buffer. |
7 | 1482 * Must check 'write' option first! |
1483 */ | |
1484 void | |
1485 dialog_changed(buf, checkall) | |
1486 buf_T *buf; | |
1487 int checkall; /* may abandon all changed buffers */ | |
1488 { | |
2770 | 1489 char_u buff[DIALOG_MSG_SIZE]; |
7 | 1490 int ret; |
1491 buf_T *buf2; | |
1492 | |
314 | 1493 dialog_msg(buff, _("Save changes to \"%s\"?"), |
7 | 1494 (buf->b_fname != NULL) ? |
1495 buf->b_fname : (char_u *)_("Untitled")); | |
1496 if (checkall) | |
1497 ret = vim_dialog_yesnoallcancel(VIM_QUESTION, NULL, buff, 1); | |
1498 else | |
1499 ret = vim_dialog_yesnocancel(VIM_QUESTION, NULL, buff, 1); | |
1500 | |
1501 if (ret == VIM_YES) | |
1502 { | |
1503 #ifdef FEAT_BROWSE | |
1504 /* May get file name, when there is none */ | |
1505 browse_save_fname(buf); | |
1506 #endif | |
1507 if (buf->b_fname != NULL) /* didn't hit Cancel */ | |
1508 (void)buf_write_all(buf, FALSE); | |
1509 } | |
1510 else if (ret == VIM_NO) | |
1511 { | |
1512 unchanged(buf, TRUE); | |
1513 } | |
1514 else if (ret == VIM_ALL) | |
1515 { | |
1516 /* | |
1517 * Write all modified files that can be written. | |
1518 * Skip readonly buffers, these need to be confirmed | |
1519 * individually. | |
1520 */ | |
1521 for (buf2 = firstbuf; buf2 != NULL; buf2 = buf2->b_next) | |
1522 { | |
1523 if (bufIsChanged(buf2) | |
1524 && (buf2->b_ffname != NULL | |
1525 #ifdef FEAT_BROWSE | |
1526 || cmdmod.browse | |
1527 #endif | |
1528 ) | |
1529 && !buf2->b_p_ro) | |
1530 { | |
1531 #ifdef FEAT_BROWSE | |
1532 /* May get file name, when there is none */ | |
1533 browse_save_fname(buf2); | |
1534 #endif | |
1535 if (buf2->b_fname != NULL) /* didn't hit Cancel */ | |
1536 (void)buf_write_all(buf2, FALSE); | |
1537 #ifdef FEAT_AUTOCMD | |
1538 /* an autocommand may have deleted the buffer */ | |
1539 if (!buf_valid(buf2)) | |
1540 buf2 = firstbuf; | |
1541 #endif | |
1542 } | |
1543 } | |
1544 } | |
1545 else if (ret == VIM_DISCARDALL) | |
1546 { | |
1547 /* | |
1548 * mark all buffers as unchanged | |
1549 */ | |
1550 for (buf2 = firstbuf; buf2 != NULL; buf2 = buf2->b_next) | |
1551 unchanged(buf2, TRUE); | |
1552 } | |
1553 } | |
1554 #endif | |
1555 | |
1556 /* | |
1557 * Return TRUE if the buffer "buf" can be abandoned, either by making it | |
1558 * hidden, autowriting it or unloading it. | |
1559 */ | |
1560 int | |
1561 can_abandon(buf, forceit) | |
1562 buf_T *buf; | |
1563 int forceit; | |
1564 { | |
1565 return ( P_HID(buf) | |
1566 || !bufIsChanged(buf) | |
1567 || buf->b_nwindows > 1 | |
1568 || autowrite(buf, forceit) == OK | |
1569 || forceit); | |
1570 } | |
1571 | |
3429 | 1572 static void add_bufnum __ARGS((int *bufnrs, int *bufnump, int nr)); |
1573 | |
1574 /* | |
1575 * Add a buffer number to "bufnrs", unless it's already there. | |
1576 */ | |
1577 static void | |
1578 add_bufnum(bufnrs, bufnump, nr) | |
1579 int *bufnrs; | |
1580 int *bufnump; | |
1581 int nr; | |
1582 { | |
1583 int i; | |
1584 | |
1585 for (i = 0; i < *bufnump; ++i) | |
1586 if (bufnrs[i] == nr) | |
1587 return; | |
1588 bufnrs[*bufnump] = nr; | |
1589 *bufnump = *bufnump + 1; | |
1590 } | |
1591 | |
7 | 1592 /* |
1593 * Return TRUE if any buffer was changed and cannot be abandoned. | |
1594 * That changed buffer becomes the current buffer. | |
1595 */ | |
1596 int | |
1597 check_changed_any(hidden) | |
1598 int hidden; /* Only check hidden buffers */ | |
1599 { | |
3429 | 1600 int ret = FALSE; |
7 | 1601 buf_T *buf; |
1602 int save; | |
3429 | 1603 int i; |
1604 int bufnum = 0; | |
1605 int bufcount = 0; | |
1606 int *bufnrs; | |
7 | 1607 #ifdef FEAT_WINDOWS |
3429 | 1608 tabpage_T *tp; |
7 | 1609 win_T *wp; |
1610 #endif | |
1611 | |
3429 | 1612 for (buf = firstbuf; buf != NULL; buf = buf->b_next) |
1613 ++bufcount; | |
1614 | |
1615 if (bufcount == 0) | |
1616 return FALSE; | |
1617 | |
1618 bufnrs = (int *)alloc(sizeof(int) * bufcount); | |
1619 if (bufnrs == NULL) | |
1620 return FALSE; | |
1621 | |
1622 /* curbuf */ | |
1623 bufnrs[bufnum++] = curbuf->b_fnum; | |
1624 #ifdef FEAT_WINDOWS | |
1625 /* buf in curtab */ | |
1626 FOR_ALL_WINDOWS(wp) | |
1627 if (wp->w_buffer != curbuf) | |
1628 add_bufnum(bufnrs, &bufnum, wp->w_buffer->b_fnum); | |
1629 | |
1630 /* buf in other tab */ | |
1631 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next) | |
1632 if (tp != curtab) | |
1633 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next) | |
1634 add_bufnum(bufnrs, &bufnum, wp->w_buffer->b_fnum); | |
1635 #endif | |
1636 /* any other buf */ | |
1637 for (buf = firstbuf; buf != NULL; buf = buf->b_next) | |
1638 add_bufnum(bufnrs, &bufnum, buf->b_fnum); | |
1639 | |
1640 for (i = 0; i < bufnum; ++i) | |
7 | 1641 { |
3429 | 1642 buf = buflist_findnr(bufnrs[i]); |
1643 if (buf == NULL) | |
1644 continue; | |
1645 if ((!hidden || buf->b_nwindows == 0) && bufIsChanged(buf)) | |
7 | 1646 { |
3429 | 1647 /* Try auto-writing the buffer. If this fails but the buffer no |
1648 * longer exists it's not changed, that's OK. */ | |
1649 if (check_changed(buf, p_awa, TRUE, FALSE, TRUE) && buf_valid(buf)) | |
1650 break; /* didn't save - still changes */ | |
7 | 1651 } |
1652 } | |
1653 | |
3429 | 1654 if (i >= bufnum) |
1655 goto theend; | |
1656 | |
1657 ret = TRUE; | |
7 | 1658 exiting = FALSE; |
1659 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) | |
1660 /* | |
1661 * When ":confirm" used, don't give an error message. | |
1662 */ | |
1663 if (!(p_confirm || cmdmod.confirm)) | |
1664 #endif | |
1665 { | |
1666 /* There must be a wait_return for this message, do_buffer() | |
1667 * may cause a redraw. But wait_return() is a no-op when vgetc() | |
1668 * is busy (Quit used from window menu), then make sure we don't | |
1669 * cause a scroll up. */ | |
823 | 1670 if (vgetc_busy > 0) |
7 | 1671 { |
1672 msg_row = cmdline_row; | |
1673 msg_col = 0; | |
1674 msg_didout = FALSE; | |
1675 } | |
1676 if (EMSG2(_("E162: No write since last change for buffer \"%s\""), | |
1677 buf_spname(buf) != NULL ? (char_u *)buf_spname(buf) : | |
1678 buf->b_fname)) | |
1679 { | |
1680 save = no_wait_return; | |
1681 no_wait_return = FALSE; | |
1682 wait_return(FALSE); | |
1683 no_wait_return = save; | |
1684 } | |
1685 } | |
1686 | |
1687 #ifdef FEAT_WINDOWS | |
1688 /* Try to find a window that contains the buffer. */ | |
1689 if (buf != curbuf) | |
3429 | 1690 FOR_ALL_TAB_WINDOWS(tp, wp) |
7 | 1691 if (wp->w_buffer == buf) |
1692 { | |
3429 | 1693 goto_tabpage_win(tp, wp); |
7 | 1694 # ifdef FEAT_AUTOCMD |
1695 /* Paranoia: did autocms wipe out the buffer with changes? */ | |
1696 if (!buf_valid(buf)) | |
3429 | 1697 { |
1698 goto theend; | |
1699 } | |
7 | 1700 # endif |
3429 | 1701 goto buf_found; |
7 | 1702 } |
3429 | 1703 buf_found: |
7 | 1704 #endif |
1705 | |
1706 /* Open the changed buffer in the current window. */ | |
1707 if (buf != curbuf) | |
1708 set_curbuf(buf, DOBUF_GOTO); | |
1709 | |
3429 | 1710 theend: |
1711 vim_free(bufnrs); | |
1712 return ret; | |
7 | 1713 } |
1714 | |
1715 /* | |
1716 * return FAIL if there is no file name, OK if there is one | |
1717 * give error message for FAIL | |
1718 */ | |
1719 int | |
1720 check_fname() | |
1721 { | |
1722 if (curbuf->b_ffname == NULL) | |
1723 { | |
1724 EMSG(_(e_noname)); | |
1725 return FAIL; | |
1726 } | |
1727 return OK; | |
1728 } | |
1729 | |
1730 /* | |
1731 * flush the contents of a buffer, unless it has no file name | |
1732 * | |
1733 * return FAIL for failure, OK otherwise | |
1734 */ | |
1735 int | |
1736 buf_write_all(buf, forceit) | |
1737 buf_T *buf; | |
1738 int forceit; | |
1739 { | |
1740 int retval; | |
1741 #ifdef FEAT_AUTOCMD | |
1742 buf_T *old_curbuf = curbuf; | |
1743 #endif | |
1744 | |
1745 retval = (buf_write(buf, buf->b_ffname, buf->b_fname, | |
1746 (linenr_T)1, buf->b_ml.ml_line_count, NULL, | |
1747 FALSE, forceit, TRUE, FALSE)); | |
1748 #ifdef FEAT_AUTOCMD | |
1749 if (curbuf != old_curbuf) | |
16 | 1750 { |
1751 msg_source(hl_attr(HLF_W)); | |
7 | 1752 MSG(_("Warning: Entered other buffer unexpectedly (check autocommands)")); |
16 | 1753 } |
7 | 1754 #endif |
1755 return retval; | |
1756 } | |
1757 | |
1758 /* | |
1759 * Code to handle the argument list. | |
1760 */ | |
1761 | |
39 | 1762 static char_u *do_one_arg __ARGS((char_u *str)); |
1763 static int do_arglist __ARGS((char_u *str, int what, int after)); | |
1764 static void alist_check_arg_idx __ARGS((void)); | |
1765 static int editing_arg_idx __ARGS((win_T *win)); | |
1766 #ifdef FEAT_LISTCMDS | |
1767 static int alist_add_list __ARGS((int count, char_u **files, int after)); | |
1768 #endif | |
1769 #define AL_SET 1 | |
1770 #define AL_ADD 2 | |
1771 #define AL_DEL 3 | |
1772 | |
7 | 1773 /* |
39 | 1774 * Isolate one argument, taking backticks. |
1775 * Changes the argument in-place, puts a NUL after it. Backticks remain. | |
7 | 1776 * Return a pointer to the start of the next argument. |
1777 */ | |
39 | 1778 static char_u * |
7 | 1779 do_one_arg(str) |
1780 char_u *str; | |
1781 { | |
1782 char_u *p; | |
1783 int inbacktick; | |
1784 | |
1785 inbacktick = FALSE; | |
1786 for (p = str; *str; ++str) | |
1787 { | |
39 | 1788 /* When the backslash is used for escaping the special meaning of a |
1789 * character we need to keep it until wildcard expansion. */ | |
7 | 1790 if (rem_backslash(str)) |
1791 { | |
1792 *p++ = *str++; | |
1793 *p++ = *str; | |
1794 } | |
1795 else | |
1796 { | |
39 | 1797 /* An item ends at a space not in backticks */ |
1798 if (!inbacktick && vim_isspace(*str)) | |
7 | 1799 break; |
39 | 1800 if (*str == '`') |
7 | 1801 inbacktick ^= TRUE; |
39 | 1802 *p++ = *str; |
7 | 1803 } |
1804 } | |
1805 str = skipwhite(str); | |
1806 *p = NUL; | |
1807 | |
1808 return str; | |
1809 } | |
1810 | |
41 | 1811 /* |
1812 * Separate the arguments in "str" and return a list of pointers in the | |
1813 * growarray "gap". | |
1814 */ | |
1815 int | |
1816 get_arglist(gap, str) | |
1817 garray_T *gap; | |
1818 char_u *str; | |
1819 { | |
1820 ga_init2(gap, (int)sizeof(char_u *), 20); | |
1821 while (*str != NUL) | |
1822 { | |
1823 if (ga_grow(gap, 1) == FAIL) | |
1824 { | |
1825 ga_clear(gap); | |
1826 return FAIL; | |
1827 } | |
1828 ((char_u **)gap->ga_data)[gap->ga_len++] = str; | |
1829 | |
1830 /* Isolate one argument, change it in-place, put a NUL after it. */ | |
1831 str = do_one_arg(str); | |
1832 } | |
1833 return OK; | |
1834 } | |
1835 | |
642 | 1836 #if defined(FEAT_QUICKFIX) || defined(FEAT_SYN_HL) || defined(PROTO) |
237 | 1837 /* |
1838 * Parse a list of arguments (file names), expand them and return in | |
1839 * "fnames[fcountp]". | |
1840 * Return FAIL or OK. | |
1841 */ | |
1842 int | |
1843 get_arglist_exp(str, fcountp, fnamesp) | |
1844 char_u *str; | |
1845 int *fcountp; | |
1846 char_u ***fnamesp; | |
1847 { | |
1848 garray_T ga; | |
1849 int i; | |
1850 | |
1851 if (get_arglist(&ga, str) == FAIL) | |
1852 return FAIL; | |
1853 i = gen_expand_wildcards(ga.ga_len, (char_u **)ga.ga_data, | |
1854 fcountp, fnamesp, EW_FILE|EW_NOTFOUND); | |
1855 ga_clear(&ga); | |
1856 return i; | |
1857 } | |
1858 #endif | |
1859 | |
7 | 1860 #if defined(FEAT_GUI) || defined(FEAT_CLIENTSERVER) || defined(PROTO) |
1861 /* | |
1862 * Redefine the argument list. | |
1863 */ | |
1864 void | |
1865 set_arglist(str) | |
1866 char_u *str; | |
1867 { | |
1868 do_arglist(str, AL_SET, 0); | |
1869 } | |
1870 #endif | |
1871 | |
1872 /* | |
1873 * "what" == AL_SET: Redefine the argument list to 'str'. | |
1874 * "what" == AL_ADD: add files in 'str' to the argument list after "after". | |
1875 * "what" == AL_DEL: remove files in 'str' from the argument list. | |
1876 * | |
1877 * Return FAIL for failure, OK otherwise. | |
1878 */ | |
1879 static int | |
1880 do_arglist(str, what, after) | |
1881 char_u *str; | |
1876 | 1882 int what UNUSED; |
1883 int after UNUSED; /* 0 means before first one */ | |
7 | 1884 { |
1885 garray_T new_ga; | |
1886 int exp_count; | |
1887 char_u **exp_files; | |
1888 int i; | |
1889 #ifdef FEAT_LISTCMDS | |
1890 char_u *p; | |
1891 int match; | |
1892 #endif | |
1893 | |
1894 /* | |
1895 * Collect all file name arguments in "new_ga". | |
1896 */ | |
41 | 1897 if (get_arglist(&new_ga, str) == FAIL) |
1898 return FAIL; | |
7 | 1899 |
1900 #ifdef FEAT_LISTCMDS | |
1901 if (what == AL_DEL) | |
1902 { | |
1903 regmatch_T regmatch; | |
1904 int didone; | |
1905 | |
1906 /* | |
1907 * Delete the items: use each item as a regexp and find a match in the | |
1908 * argument list. | |
1909 */ | |
1910 #ifdef CASE_INSENSITIVE_FILENAME | |
1911 regmatch.rm_ic = TRUE; /* Always ignore case */ | |
1912 #else | |
1913 regmatch.rm_ic = FALSE; /* Never ignore case */ | |
1914 #endif | |
1915 for (i = 0; i < new_ga.ga_len && !got_int; ++i) | |
1916 { | |
1917 p = ((char_u **)new_ga.ga_data)[i]; | |
1918 p = file_pat_to_reg_pat(p, NULL, NULL, FALSE); | |
1919 if (p == NULL) | |
1920 break; | |
1921 regmatch.regprog = vim_regcomp(p, p_magic ? RE_MAGIC : 0); | |
1922 if (regmatch.regprog == NULL) | |
1923 { | |
1924 vim_free(p); | |
1925 break; | |
1926 } | |
1927 | |
1928 didone = FALSE; | |
1929 for (match = 0; match < ARGCOUNT; ++match) | |
1930 if (vim_regexec(®match, alist_name(&ARGLIST[match]), | |
1931 (colnr_T)0)) | |
1932 { | |
1933 didone = TRUE; | |
1934 vim_free(ARGLIST[match].ae_fname); | |
1935 mch_memmove(ARGLIST + match, ARGLIST + match + 1, | |
1936 (ARGCOUNT - match - 1) * sizeof(aentry_T)); | |
1937 --ALIST(curwin)->al_ga.ga_len; | |
1938 if (curwin->w_arg_idx > match) | |
1939 --curwin->w_arg_idx; | |
1940 --match; | |
1941 } | |
1942 | |
1943 vim_free(regmatch.regprog); | |
1944 vim_free(p); | |
1945 if (!didone) | |
1946 EMSG2(_(e_nomatch2), ((char_u **)new_ga.ga_data)[i]); | |
1947 } | |
1948 ga_clear(&new_ga); | |
1949 } | |
1950 else | |
1951 #endif | |
1952 { | |
1953 i = expand_wildcards(new_ga.ga_len, (char_u **)new_ga.ga_data, | |
1954 &exp_count, &exp_files, EW_DIR|EW_FILE|EW_ADDSLASH|EW_NOTFOUND); | |
1955 ga_clear(&new_ga); | |
1956 if (i == FAIL) | |
1957 return FAIL; | |
1958 if (exp_count == 0) | |
1959 { | |
1960 EMSG(_(e_nomatch)); | |
1961 return FAIL; | |
1962 } | |
1963 | |
1964 #ifdef FEAT_LISTCMDS | |
1965 if (what == AL_ADD) | |
1966 { | |
1967 (void)alist_add_list(exp_count, exp_files, after); | |
1968 vim_free(exp_files); | |
1969 } | |
1970 else /* what == AL_SET */ | |
1971 #endif | |
41 | 1972 alist_set(ALIST(curwin), exp_count, exp_files, FALSE, NULL, 0); |
7 | 1973 } |
1974 | |
1975 alist_check_arg_idx(); | |
1976 | |
1977 return OK; | |
1978 } | |
1979 | |
1980 /* | |
1981 * Check the validity of the arg_idx for each other window. | |
1982 */ | |
1983 static void | |
1984 alist_check_arg_idx() | |
1985 { | |
1986 #ifdef FEAT_WINDOWS | |
1987 win_T *win; | |
671 | 1988 tabpage_T *tp; |
1989 | |
1990 FOR_ALL_TAB_WINDOWS(tp, win) | |
7 | 1991 if (win->w_alist == curwin->w_alist) |
1992 check_arg_idx(win); | |
1993 #else | |
1994 check_arg_idx(curwin); | |
1995 #endif | |
1996 } | |
1997 | |
1998 /* | |
3312 | 1999 * Return TRUE if window "win" is editing the file at the current argument |
22 | 2000 * index. |
2001 */ | |
2002 static int | |
2003 editing_arg_idx(win) | |
2004 win_T *win; | |
2005 { | |
2006 return !(win->w_arg_idx >= WARGCOUNT(win) | |
2007 || (win->w_buffer->b_fnum | |
2008 != WARGLIST(win)[win->w_arg_idx].ae_fnum | |
2009 && (win->w_buffer->b_ffname == NULL | |
2010 || !(fullpathcmp( | |
2011 alist_name(&WARGLIST(win)[win->w_arg_idx]), | |
2012 win->w_buffer->b_ffname, TRUE) & FPC_SAME)))); | |
2013 } | |
2014 | |
2015 /* | |
7 | 2016 * Check if window "win" is editing the w_arg_idx file in its argument list. |
2017 */ | |
2018 void | |
2019 check_arg_idx(win) | |
2020 win_T *win; | |
2021 { | |
22 | 2022 if (WARGCOUNT(win) > 1 && !editing_arg_idx(win)) |
7 | 2023 { |
2024 /* We are not editing the current entry in the argument list. | |
2025 * Set "arg_had_last" if we are editing the last one. */ | |
2026 win->w_arg_idx_invalid = TRUE; | |
2027 if (win->w_arg_idx != WARGCOUNT(win) - 1 | |
2028 && arg_had_last == FALSE | |
2029 #ifdef FEAT_WINDOWS | |
2030 && ALIST(win) == &global_alist | |
2031 #endif | |
2032 && GARGCOUNT > 0 | |
2033 && win->w_arg_idx < GARGCOUNT | |
2034 && (win->w_buffer->b_fnum == GARGLIST[GARGCOUNT - 1].ae_fnum | |
2035 || (win->w_buffer->b_ffname != NULL | |
2036 && (fullpathcmp(alist_name(&GARGLIST[GARGCOUNT - 1]), | |
2037 win->w_buffer->b_ffname, TRUE) & FPC_SAME)))) | |
2038 arg_had_last = TRUE; | |
2039 } | |
2040 else | |
2041 { | |
2042 /* We are editing the current entry in the argument list. | |
2043 * Set "arg_had_last" if it's also the last one */ | |
2044 win->w_arg_idx_invalid = FALSE; | |
2045 if (win->w_arg_idx == WARGCOUNT(win) - 1 | |
2046 #ifdef FEAT_WINDOWS | |
2047 && win->w_alist == &global_alist | |
2048 #endif | |
2049 ) | |
2050 arg_had_last = TRUE; | |
2051 } | |
2052 } | |
2053 | |
2054 /* | |
2055 * ":args", ":argslocal" and ":argsglobal". | |
2056 */ | |
2057 void | |
2058 ex_args(eap) | |
2059 exarg_T *eap; | |
2060 { | |
2061 int i; | |
2062 | |
2063 if (eap->cmdidx != CMD_args) | |
2064 { | |
2065 #if defined(FEAT_WINDOWS) && defined(FEAT_LISTCMDS) | |
2066 alist_unlink(ALIST(curwin)); | |
2067 if (eap->cmdidx == CMD_argglobal) | |
2068 ALIST(curwin) = &global_alist; | |
2069 else /* eap->cmdidx == CMD_arglocal */ | |
2070 alist_new(); | |
2071 #else | |
2072 ex_ni(eap); | |
2073 return; | |
2074 #endif | |
2075 } | |
2076 | |
2077 if (!ends_excmd(*eap->arg)) | |
2078 { | |
2079 /* | |
2080 * ":args file ..": define new argument list, handle like ":next" | |
2081 * Also for ":argslocal file .." and ":argsglobal file ..". | |
2082 */ | |
2083 ex_next(eap); | |
2084 } | |
2085 else | |
2086 #if defined(FEAT_WINDOWS) && defined(FEAT_LISTCMDS) | |
2087 if (eap->cmdidx == CMD_args) | |
2088 #endif | |
2089 { | |
2090 /* | |
2091 * ":args": list arguments. | |
2092 */ | |
2093 if (ARGCOUNT > 0) | |
2094 { | |
2095 /* Overwrite the command, for a short list there is no scrolling | |
2096 * required and no wait_return(). */ | |
2097 gotocmdline(TRUE); | |
2098 for (i = 0; i < ARGCOUNT; ++i) | |
2099 { | |
2100 if (i == curwin->w_arg_idx) | |
2101 msg_putchar('['); | |
2102 msg_outtrans(alist_name(&ARGLIST[i])); | |
2103 if (i == curwin->w_arg_idx) | |
2104 msg_putchar(']'); | |
2105 msg_putchar(' '); | |
2106 } | |
2107 } | |
2108 } | |
2109 #if defined(FEAT_WINDOWS) && defined(FEAT_LISTCMDS) | |
2110 else if (eap->cmdidx == CMD_arglocal) | |
2111 { | |
2112 garray_T *gap = &curwin->w_alist->al_ga; | |
2113 | |
2114 /* | |
2115 * ":argslocal": make a local copy of the global argument list. | |
2116 */ | |
2117 if (ga_grow(gap, GARGCOUNT) == OK) | |
2118 for (i = 0; i < GARGCOUNT; ++i) | |
2119 if (GARGLIST[i].ae_fname != NULL) | |
2120 { | |
2121 AARGLIST(curwin->w_alist)[gap->ga_len].ae_fname = | |
2122 vim_strsave(GARGLIST[i].ae_fname); | |
2123 AARGLIST(curwin->w_alist)[gap->ga_len].ae_fnum = | |
2124 GARGLIST[i].ae_fnum; | |
2125 ++gap->ga_len; | |
2126 } | |
2127 } | |
2128 #endif | |
2129 } | |
2130 | |
2131 /* | |
2132 * ":previous", ":sprevious", ":Next" and ":sNext". | |
2133 */ | |
2134 void | |
2135 ex_previous(eap) | |
2136 exarg_T *eap; | |
2137 { | |
2138 /* If past the last one already, go to the last one. */ | |
2139 if (curwin->w_arg_idx - (int)eap->line2 >= ARGCOUNT) | |
2140 do_argfile(eap, ARGCOUNT - 1); | |
2141 else | |
2142 do_argfile(eap, curwin->w_arg_idx - (int)eap->line2); | |
2143 } | |
2144 | |
2145 /* | |
2146 * ":rewind", ":first", ":sfirst" and ":srewind". | |
2147 */ | |
2148 void | |
2149 ex_rewind(eap) | |
2150 exarg_T *eap; | |
2151 { | |
2152 do_argfile(eap, 0); | |
2153 } | |
2154 | |
2155 /* | |
2156 * ":last" and ":slast". | |
2157 */ | |
2158 void | |
2159 ex_last(eap) | |
2160 exarg_T *eap; | |
2161 { | |
2162 do_argfile(eap, ARGCOUNT - 1); | |
2163 } | |
2164 | |
2165 /* | |
2166 * ":argument" and ":sargument". | |
2167 */ | |
2168 void | |
2169 ex_argument(eap) | |
2170 exarg_T *eap; | |
2171 { | |
2172 int i; | |
2173 | |
2174 if (eap->addr_count > 0) | |
2175 i = eap->line2 - 1; | |
2176 else | |
2177 i = curwin->w_arg_idx; | |
2178 do_argfile(eap, i); | |
2179 } | |
2180 | |
2181 /* | |
2182 * Edit file "argn" of the argument lists. | |
2183 */ | |
2184 void | |
2185 do_argfile(eap, argn) | |
2186 exarg_T *eap; | |
2187 int argn; | |
2188 { | |
2189 int other; | |
2190 char_u *p; | |
271 | 2191 int old_arg_idx = curwin->w_arg_idx; |
7 | 2192 |
2193 if (argn < 0 || argn >= ARGCOUNT) | |
2194 { | |
2195 if (ARGCOUNT <= 1) | |
2196 EMSG(_("E163: There is only one file to edit")); | |
2197 else if (argn < 0) | |
2198 EMSG(_("E164: Cannot go before first file")); | |
2199 else | |
2200 EMSG(_("E165: Cannot go beyond last file")); | |
2201 } | |
2202 else | |
2203 { | |
2204 setpcmark(); | |
2205 #ifdef FEAT_GUI | |
2206 need_mouse_correct = TRUE; | |
2207 #endif | |
2208 | |
2209 #ifdef FEAT_WINDOWS | |
683 | 2210 /* split window or create new tab page first */ |
2211 if (*eap->cmd == 's' || cmdmod.tab != 0) | |
7 | 2212 { |
2213 if (win_split(0, 0) == FAIL) | |
2214 return; | |
2583 | 2215 RESET_BINDING(curwin); |
7 | 2216 } |
2217 else | |
2218 #endif | |
2219 { | |
2220 /* | |
2221 * if 'hidden' set, only check for changed file when re-editing | |
2222 * the same buffer | |
2223 */ | |
2224 other = TRUE; | |
2225 if (P_HID(curbuf)) | |
2226 { | |
2227 p = fix_fname(alist_name(&ARGLIST[argn])); | |
2228 other = otherfile(p); | |
2229 vim_free(p); | |
2230 } | |
2231 if ((!P_HID(curbuf) || !other) | |
2232 && check_changed(curbuf, TRUE, !other, eap->forceit, FALSE)) | |
2233 return; | |
2234 } | |
2235 | |
2236 curwin->w_arg_idx = argn; | |
2237 if (argn == ARGCOUNT - 1 | |
2238 #ifdef FEAT_WINDOWS | |
2239 && curwin->w_alist == &global_alist | |
2240 #endif | |
2241 ) | |
2242 arg_had_last = TRUE; | |
2243 | |
271 | 2244 /* Edit the file; always use the last known line number. |
2245 * When it fails (e.g. Abort for already edited file) restore the | |
2246 * argument index. */ | |
2247 if (do_ecmd(0, alist_name(&ARGLIST[curwin->w_arg_idx]), NULL, | |
7 | 2248 eap, ECMD_LAST, |
1743 | 2249 (P_HID(curwin->w_buffer) ? ECMD_HIDE : 0) |
2250 + (eap->forceit ? ECMD_FORCEIT : 0), curwin) == FAIL) | |
271 | 2251 curwin->w_arg_idx = old_arg_idx; |
7 | 2252 /* like Vi: set the mark where the cursor is in the file. */ |
271 | 2253 else if (eap->cmdidx != CMD_argdo) |
7 | 2254 setmark('\''); |
2255 } | |
2256 } | |
2257 | |
2258 /* | |
2259 * ":next", and commands that behave like it. | |
2260 */ | |
2261 void | |
2262 ex_next(eap) | |
2263 exarg_T *eap; | |
2264 { | |
2265 int i; | |
2266 | |
2267 /* | |
2268 * check for changed buffer now, if this fails the argument list is not | |
2269 * redefined. | |
2270 */ | |
2271 if ( P_HID(curbuf) | |
2272 || eap->cmdidx == CMD_snext | |
2273 || !check_changed(curbuf, TRUE, FALSE, eap->forceit, FALSE)) | |
2274 { | |
2275 if (*eap->arg != NUL) /* redefine file list */ | |
2276 { | |
2277 if (do_arglist(eap->arg, AL_SET, 0) == FAIL) | |
2278 return; | |
2279 i = 0; | |
2280 } | |
2281 else | |
2282 i = curwin->w_arg_idx + (int)eap->line2; | |
2283 do_argfile(eap, i); | |
2284 } | |
2285 } | |
2286 | |
2287 #ifdef FEAT_LISTCMDS | |
2288 /* | |
2289 * ":argedit" | |
2290 */ | |
2291 void | |
2292 ex_argedit(eap) | |
2293 exarg_T *eap; | |
2294 { | |
2295 int fnum; | |
2296 int i; | |
2297 char_u *s; | |
2298 | |
2299 /* Add the argument to the buffer list and get the buffer number. */ | |
2300 fnum = buflist_add(eap->arg, BLN_LISTED); | |
2301 | |
2302 /* Check if this argument is already in the argument list. */ | |
2303 for (i = 0; i < ARGCOUNT; ++i) | |
2304 if (ARGLIST[i].ae_fnum == fnum) | |
2305 break; | |
2306 if (i == ARGCOUNT) | |
2307 { | |
2308 /* Can't find it, add it to the argument list. */ | |
2309 s = vim_strsave(eap->arg); | |
2310 if (s == NULL) | |
2311 return; | |
2312 i = alist_add_list(1, &s, | |
2313 eap->addr_count > 0 ? (int)eap->line2 : curwin->w_arg_idx + 1); | |
2314 if (i < 0) | |
2315 return; | |
2316 curwin->w_arg_idx = i; | |
2317 } | |
2318 | |
2319 alist_check_arg_idx(); | |
2320 | |
2321 /* Edit the argument. */ | |
2322 do_argfile(eap, i); | |
2323 } | |
2324 | |
2325 /* | |
2326 * ":argadd" | |
2327 */ | |
2328 void | |
2329 ex_argadd(eap) | |
2330 exarg_T *eap; | |
2331 { | |
2332 do_arglist(eap->arg, AL_ADD, | |
2333 eap->addr_count > 0 ? (int)eap->line2 : curwin->w_arg_idx + 1); | |
2334 #ifdef FEAT_TITLE | |
2335 maketitle(); | |
2336 #endif | |
2337 } | |
2338 | |
2339 /* | |
2340 * ":argdelete" | |
2341 */ | |
2342 void | |
2343 ex_argdelete(eap) | |
2344 exarg_T *eap; | |
2345 { | |
2346 int i; | |
2347 int n; | |
2348 | |
2349 if (eap->addr_count > 0) | |
2350 { | |
2351 /* ":1,4argdel": Delete all arguments in the range. */ | |
2352 if (eap->line2 > ARGCOUNT) | |
2353 eap->line2 = ARGCOUNT; | |
2354 n = eap->line2 - eap->line1 + 1; | |
2355 if (*eap->arg != NUL || n <= 0) | |
2356 EMSG(_(e_invarg)); | |
2357 else | |
2358 { | |
2359 for (i = eap->line1; i <= eap->line2; ++i) | |
2360 vim_free(ARGLIST[i - 1].ae_fname); | |
2361 mch_memmove(ARGLIST + eap->line1 - 1, ARGLIST + eap->line2, | |
2362 (size_t)((ARGCOUNT - eap->line2) * sizeof(aentry_T))); | |
2363 ALIST(curwin)->al_ga.ga_len -= n; | |
2364 if (curwin->w_arg_idx >= eap->line2) | |
2365 curwin->w_arg_idx -= n; | |
2366 else if (curwin->w_arg_idx > eap->line1) | |
2367 curwin->w_arg_idx = eap->line1; | |
2368 } | |
2369 } | |
2370 else if (*eap->arg == NUL) | |
2371 EMSG(_(e_argreq)); | |
2372 else | |
2373 do_arglist(eap->arg, AL_DEL, 0); | |
2374 #ifdef FEAT_TITLE | |
2375 maketitle(); | |
2376 #endif | |
2377 } | |
2378 | |
2379 /* | |
685 | 2380 * ":argdo", ":windo", ":bufdo", ":tabdo" |
7 | 2381 */ |
2382 void | |
2383 ex_listdo(eap) | |
2384 exarg_T *eap; | |
2385 { | |
2386 int i; | |
2387 #ifdef FEAT_WINDOWS | |
685 | 2388 win_T *wp; |
2389 tabpage_T *tp; | |
7 | 2390 #endif |
2391 buf_T *buf; | |
2392 int next_fnum = 0; | |
2393 #if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL) | |
2394 char_u *save_ei = NULL; | |
2395 #endif | |
39 | 2396 char_u *p_shm_save; |
7 | 2397 |
2398 #ifndef FEAT_WINDOWS | |
2399 if (eap->cmdidx == CMD_windo) | |
2400 { | |
2401 ex_ni(eap); | |
2402 return; | |
2403 } | |
2404 #endif | |
2405 | |
2406 #if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL) | |
819 | 2407 if (eap->cmdidx != CMD_windo && eap->cmdidx != CMD_tabdo) |
123 | 2408 /* Don't do syntax HL autocommands. Skipping the syntax file is a |
2409 * great speed improvement. */ | |
2410 save_ei = au_event_disable(",Syntax"); | |
7 | 2411 #endif |
2412 | |
2413 if (eap->cmdidx == CMD_windo | |
685 | 2414 || eap->cmdidx == CMD_tabdo |
7 | 2415 || P_HID(curbuf) |
2416 || !check_changed(curbuf, TRUE, FALSE, eap->forceit, FALSE)) | |
2417 { | |
2418 /* start at the first argument/window/buffer */ | |
2419 i = 0; | |
2420 #ifdef FEAT_WINDOWS | |
685 | 2421 wp = firstwin; |
2422 tp = first_tabpage; | |
7 | 2423 #endif |
2424 /* set pcmark now */ | |
2425 if (eap->cmdidx == CMD_bufdo) | |
2426 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0); | |
2427 else | |
2428 setpcmark(); | |
2429 listcmd_busy = TRUE; /* avoids setting pcmark below */ | |
2430 | |
2431 while (!got_int) | |
2432 { | |
2433 if (eap->cmdidx == CMD_argdo) | |
2434 { | |
2435 /* go to argument "i" */ | |
2436 if (i == ARGCOUNT) | |
2437 break; | |
2438 /* Don't call do_argfile() when already there, it will try | |
2439 * reloading the file. */ | |
22 | 2440 if (curwin->w_arg_idx != i || !editing_arg_idx(curwin)) |
39 | 2441 { |
2442 /* Clear 'shm' to avoid that the file message overwrites | |
2443 * any output from the command. */ | |
2444 p_shm_save = vim_strsave(p_shm); | |
2445 set_option_value((char_u *)"shm", 0L, (char_u *)"", 0); | |
7 | 2446 do_argfile(eap, i); |
39 | 2447 set_option_value((char_u *)"shm", 0L, p_shm_save, 0); |
2448 vim_free(p_shm_save); | |
2449 } | |
7 | 2450 if (curwin->w_arg_idx != i) |
2451 break; | |
2452 ++i; | |
2453 } | |
2454 #ifdef FEAT_WINDOWS | |
2455 else if (eap->cmdidx == CMD_windo) | |
2456 { | |
685 | 2457 /* go to window "wp" */ |
2458 if (!win_valid(wp)) | |
7 | 2459 break; |
685 | 2460 win_goto(wp); |
1115 | 2461 if (curwin != wp) |
2462 break; /* something must be wrong */ | |
685 | 2463 wp = curwin->w_next; |
2464 } | |
2465 else if (eap->cmdidx == CMD_tabdo) | |
2466 { | |
2467 /* go to window "tp" */ | |
2468 if (!valid_tabpage(tp)) | |
2469 break; | |
2470 goto_tabpage_tp(tp); | |
2471 tp = tp->tp_next; | |
7 | 2472 } |
2473 #endif | |
2474 else if (eap->cmdidx == CMD_bufdo) | |
2475 { | |
2476 /* Remember the number of the next listed buffer, in case | |
2477 * ":bwipe" is used or autocommands do something strange. */ | |
2478 next_fnum = -1; | |
2479 for (buf = curbuf->b_next; buf != NULL; buf = buf->b_next) | |
2480 if (buf->b_p_bl) | |
2481 { | |
2482 next_fnum = buf->b_fnum; | |
2483 break; | |
2484 } | |
2485 } | |
2486 | |
2487 /* execute the command */ | |
2488 do_cmdline(eap->arg, eap->getline, eap->cookie, | |
2489 DOCMD_VERBOSE + DOCMD_NOWAIT); | |
2490 | |
2491 if (eap->cmdidx == CMD_bufdo) | |
2492 { | |
2493 /* Done? */ | |
2494 if (next_fnum < 0) | |
2495 break; | |
2496 /* Check if the buffer still exists. */ | |
2497 for (buf = firstbuf; buf != NULL; buf = buf->b_next) | |
2498 if (buf->b_fnum == next_fnum) | |
2499 break; | |
2500 if (buf == NULL) | |
2501 break; | |
39 | 2502 |
2503 /* Go to the next buffer. Clear 'shm' to avoid that the file | |
2504 * message overwrites any output from the command. */ | |
2505 p_shm_save = vim_strsave(p_shm); | |
2506 set_option_value((char_u *)"shm", 0L, (char_u *)"", 0); | |
7 | 2507 goto_buffer(eap, DOBUF_FIRST, FORWARD, next_fnum); |
39 | 2508 set_option_value((char_u *)"shm", 0L, p_shm_save, 0); |
2509 vim_free(p_shm_save); | |
2510 | |
7 | 2511 /* If autocommands took us elsewhere, quit here */ |
2512 if (curbuf->b_fnum != next_fnum) | |
2513 break; | |
2514 } | |
2515 | |
2516 if (eap->cmdidx == CMD_windo) | |
2517 { | |
2518 validate_cursor(); /* cursor may have moved */ | |
2519 #ifdef FEAT_SCROLLBIND | |
2520 /* required when 'scrollbind' has been set */ | |
2521 if (curwin->w_p_scb) | |
2522 do_check_scrollbind(TRUE); | |
2523 #endif | |
2524 } | |
2525 } | |
2526 listcmd_busy = FALSE; | |
2527 } | |
2528 | |
2529 #if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL) | |
154 | 2530 if (save_ei != NULL) |
2531 { | |
2532 au_event_restore(save_ei); | |
2533 apply_autocmds(EVENT_SYNTAX, curbuf->b_p_syn, | |
2534 curbuf->b_fname, TRUE, curbuf); | |
2535 } | |
7 | 2536 #endif |
2537 } | |
2538 | |
2539 /* | |
2540 * Add files[count] to the arglist of the current window after arg "after". | |
2541 * The file names in files[count] must have been allocated and are taken over. | |
2542 * Files[] itself is not taken over. | |
2543 * Returns index of first added argument. Returns -1 when failed (out of mem). | |
2544 */ | |
2545 static int | |
2546 alist_add_list(count, files, after) | |
2547 int count; | |
2548 char_u **files; | |
2549 int after; /* where to add: 0 = before first one */ | |
2550 { | |
2551 int i; | |
2552 | |
2553 if (ga_grow(&ALIST(curwin)->al_ga, count) == OK) | |
2554 { | |
2555 if (after < 0) | |
2556 after = 0; | |
2557 if (after > ARGCOUNT) | |
2558 after = ARGCOUNT; | |
2559 if (after < ARGCOUNT) | |
2560 mch_memmove(&(ARGLIST[after + count]), &(ARGLIST[after]), | |
2561 (ARGCOUNT - after) * sizeof(aentry_T)); | |
2562 for (i = 0; i < count; ++i) | |
2563 { | |
2564 ARGLIST[after + i].ae_fname = files[i]; | |
2565 ARGLIST[after + i].ae_fnum = buflist_add(files[i], BLN_LISTED); | |
2566 } | |
2567 ALIST(curwin)->al_ga.ga_len += count; | |
2568 if (curwin->w_arg_idx >= after) | |
2569 ++curwin->w_arg_idx; | |
2570 return after; | |
2571 } | |
2572 | |
2573 for (i = 0; i < count; ++i) | |
2574 vim_free(files[i]); | |
2575 return -1; | |
2576 } | |
2577 | |
2578 #endif /* FEAT_LISTCMDS */ | |
2579 | |
2580 #ifdef FEAT_EVAL | |
2581 /* | |
2582 * ":compiler[!] {name}" | |
2583 */ | |
2584 void | |
2585 ex_compiler(eap) | |
2586 exarg_T *eap; | |
2587 { | |
2588 char_u *buf; | |
2589 char_u *old_cur_comp = NULL; | |
2590 char_u *p; | |
2591 | |
2592 if (*eap->arg == NUL) | |
2593 { | |
2594 /* List all compiler scripts. */ | |
2595 do_cmdline_cmd((char_u *)"echo globpath(&rtp, 'compiler/*.vim')"); | |
2596 /* ) keep the indenter happy... */ | |
2597 } | |
2598 else | |
2599 { | |
2600 buf = alloc((unsigned)(STRLEN(eap->arg) + 14)); | |
2601 if (buf != NULL) | |
2602 { | |
2603 if (eap->forceit) | |
2604 { | |
2605 /* ":compiler! {name}" sets global options */ | |
2606 do_cmdline_cmd((char_u *) | |
2607 "command -nargs=* CompilerSet set <args>"); | |
2608 } | |
2609 else | |
2610 { | |
2611 /* ":compiler! {name}" sets local options. | |
2612 * To remain backwards compatible "current_compiler" is always | |
2613 * used. A user's compiler plugin may set it, the distributed | |
2614 * plugin will then skip the settings. Afterwards set | |
2051
ef2890033e88
updated for version 7.2.337
Bram Moolenaar <bram@zimbu.org>
parents:
1882
diff
changeset
|
2615 * "b:current_compiler" and restore "current_compiler". |
ef2890033e88
updated for version 7.2.337
Bram Moolenaar <bram@zimbu.org>
parents:
1882
diff
changeset
|
2616 * Explicitly prepend "g:" to make it work in a function. */ |
ef2890033e88
updated for version 7.2.337
Bram Moolenaar <bram@zimbu.org>
parents:
1882
diff
changeset
|
2617 old_cur_comp = get_var_value((char_u *)"g:current_compiler"); |
7 | 2618 if (old_cur_comp != NULL) |
2619 old_cur_comp = vim_strsave(old_cur_comp); | |
2620 do_cmdline_cmd((char_u *) | |
2621 "command -nargs=* CompilerSet setlocal <args>"); | |
2622 } | |
2051
ef2890033e88
updated for version 7.2.337
Bram Moolenaar <bram@zimbu.org>
parents:
1882
diff
changeset
|
2623 do_unlet((char_u *)"g:current_compiler", TRUE); |
148 | 2624 do_unlet((char_u *)"b:current_compiler", TRUE); |
7 | 2625 |
2626 sprintf((char *)buf, "compiler/%s.vim", eap->arg); | |
480 | 2627 if (source_runtime(buf, TRUE) == FAIL) |
7 | 2628 EMSG2(_("E666: compiler not supported: %s"), eap->arg); |
2629 vim_free(buf); | |
2630 | |
2631 do_cmdline_cmd((char_u *)":delcommand CompilerSet"); | |
2632 | |
2633 /* Set "b:current_compiler" from "current_compiler". */ | |
2051
ef2890033e88
updated for version 7.2.337
Bram Moolenaar <bram@zimbu.org>
parents:
1882
diff
changeset
|
2634 p = get_var_value((char_u *)"g:current_compiler"); |
7 | 2635 if (p != NULL) |
2636 set_internal_string_var((char_u *)"b:current_compiler", p); | |
2637 | |
2638 /* Restore "current_compiler" for ":compiler {name}". */ | |
2639 if (!eap->forceit) | |
2640 { | |
2641 if (old_cur_comp != NULL) | |
2642 { | |
2051
ef2890033e88
updated for version 7.2.337
Bram Moolenaar <bram@zimbu.org>
parents:
1882
diff
changeset
|
2643 set_internal_string_var((char_u *)"g:current_compiler", |
7 | 2644 old_cur_comp); |
2645 vim_free(old_cur_comp); | |
2646 } | |
2647 else | |
2051
ef2890033e88
updated for version 7.2.337
Bram Moolenaar <bram@zimbu.org>
parents:
1882
diff
changeset
|
2648 do_unlet((char_u *)"g:current_compiler", TRUE); |
7 | 2649 } |
2650 } | |
2651 } | |
2652 } | |
2653 #endif | |
2654 | |
2655 /* | |
2656 * ":runtime {name}" | |
2657 */ | |
2658 void | |
2659 ex_runtime(eap) | |
2660 exarg_T *eap; | |
2661 { | |
480 | 2662 source_runtime(eap->arg, eap->forceit); |
7 | 2663 } |
2664 | |
237 | 2665 static void source_callback __ARGS((char_u *fname, void *cookie)); |
2666 | |
7 | 2667 static void |
237 | 2668 source_callback(fname, cookie) |
7 | 2669 char_u *fname; |
1876 | 2670 void *cookie UNUSED; |
7 | 2671 { |
819 | 2672 (void)do_source(fname, FALSE, DOSO_NONE); |
7 | 2673 } |
2674 | |
2675 /* | |
2676 * Source the file "name" from all directories in 'runtimepath'. | |
2677 * "name" can contain wildcards. | |
2678 * When "all" is TRUE, source all files, otherwise only the first one. | |
2679 * return FAIL when no file could be sourced, OK otherwise. | |
2680 */ | |
2681 int | |
480 | 2682 source_runtime(name, all) |
7 | 2683 char_u *name; |
2684 int all; | |
2685 { | |
237 | 2686 return do_in_runtimepath(name, all, source_callback, NULL); |
7 | 2687 } |
2688 | |
2689 /* | |
237 | 2690 * Find "name" in 'runtimepath'. When found, invoke the callback function for |
2691 * it: callback(fname, "cookie") | |
7 | 2692 * When "all" is TRUE repeat for all matches, otherwise only the first one is |
2693 * used. | |
2694 * Returns OK when at least one match found, FAIL otherwise. | |
2695 */ | |
2696 int | |
237 | 2697 do_in_runtimepath(name, all, callback, cookie) |
7 | 2698 char_u *name; |
2699 int all; | |
237 | 2700 void (*callback)__ARGS((char_u *fname, void *ck)); |
2701 void *cookie; | |
7 | 2702 { |
2703 char_u *rtp; | |
2704 char_u *np; | |
2705 char_u *buf; | |
2706 char_u *rtp_copy; | |
2707 char_u *tail; | |
2708 int num_files; | |
2709 char_u **files; | |
2710 int i; | |
2711 int did_one = FALSE; | |
2712 #ifdef AMIGA | |
2713 struct Process *proc = (struct Process *)FindTask(0L); | |
2714 APTR save_winptr = proc->pr_WindowPtr; | |
2715 | |
2716 /* Avoid a requester here for a volume that doesn't exist. */ | |
2717 proc->pr_WindowPtr = (APTR)-1L; | |
2718 #endif | |
2719 | |
2720 /* Make a copy of 'runtimepath'. Invoking the callback may change the | |
2721 * value. */ | |
2722 rtp_copy = vim_strsave(p_rtp); | |
2723 buf = alloc(MAXPATHL); | |
2724 if (buf != NULL && rtp_copy != NULL) | |
2725 { | |
2726 if (p_verbose > 1) | |
294 | 2727 { |
2728 verbose_enter(); | |
274 | 2729 smsg((char_u *)_("Searching for \"%s\" in \"%s\""), |
7 | 2730 (char *)name, (char *)p_rtp); |
294 | 2731 verbose_leave(); |
2732 } | |
271 | 2733 |
7 | 2734 /* Loop over all entries in 'runtimepath'. */ |
2735 rtp = rtp_copy; | |
2736 while (*rtp != NUL && (all || !did_one)) | |
2737 { | |
2738 /* Copy the path from 'runtimepath' to buf[]. */ | |
2739 copy_option_part(&rtp, buf, MAXPATHL, ","); | |
2740 if (STRLEN(buf) + STRLEN(name) + 2 < MAXPATHL) | |
2741 { | |
2742 add_pathsep(buf); | |
2743 tail = buf + STRLEN(buf); | |
2744 | |
2745 /* Loop over all patterns in "name" */ | |
2746 np = name; | |
2747 while (*np != NUL && (all || !did_one)) | |
2748 { | |
2749 /* Append the pattern from "name" to buf[]. */ | |
2750 copy_option_part(&np, tail, (int)(MAXPATHL - (tail - buf)), | |
2751 "\t "); | |
2752 | |
2753 if (p_verbose > 2) | |
294 | 2754 { |
2755 verbose_enter(); | |
274 | 2756 smsg((char_u *)_("Searching for \"%s\""), buf); |
294 | 2757 verbose_leave(); |
2758 } | |
7 | 2759 |
2760 /* Expand wildcards, invoke the callback for each match. */ | |
2761 if (gen_expand_wildcards(1, &buf, &num_files, &files, | |
2762 EW_FILE) == OK) | |
2763 { | |
2764 for (i = 0; i < num_files; ++i) | |
2765 { | |
237 | 2766 (*callback)(files[i], cookie); |
7 | 2767 did_one = TRUE; |
2768 if (!all) | |
2769 break; | |
2770 } | |
2771 FreeWild(num_files, files); | |
2772 } | |
2773 } | |
2774 } | |
2775 } | |
2776 } | |
2777 vim_free(buf); | |
2778 vim_free(rtp_copy); | |
2779 if (p_verbose > 0 && !did_one) | |
294 | 2780 { |
2781 verbose_enter(); | |
274 | 2782 smsg((char_u *)_("not found in 'runtimepath': \"%s\""), name); |
294 | 2783 verbose_leave(); |
2784 } | |
7 | 2785 |
2786 #ifdef AMIGA | |
2787 proc->pr_WindowPtr = save_winptr; | |
2788 #endif | |
2789 | |
2790 return did_one ? OK : FAIL; | |
2791 } | |
2792 | |
2793 #if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) | |
2794 /* | |
2795 * ":options" | |
2796 */ | |
2797 void | |
2798 ex_options(eap) | |
1876 | 2799 exarg_T *eap UNUSED; |
7 | 2800 { |
2801 cmd_source((char_u *)SYS_OPTWIN_FILE, NULL); | |
2802 } | |
2803 #endif | |
2804 | |
2805 /* | |
2806 * ":source {fname}" | |
2807 */ | |
2808 void | |
2809 ex_source(eap) | |
2810 exarg_T *eap; | |
2811 { | |
2812 #ifdef FEAT_BROWSE | |
2813 if (cmdmod.browse) | |
2814 { | |
2815 char_u *fname = NULL; | |
2816 | |
29 | 2817 fname = do_browse(0, (char_u *)_("Source Vim script"), eap->arg, |
7 | 2818 NULL, NULL, BROWSE_FILTER_MACROS, NULL); |
2819 if (fname != NULL) | |
2820 { | |
2821 cmd_source(fname, eap); | |
2822 vim_free(fname); | |
2823 } | |
2824 } | |
2825 else | |
2826 #endif | |
2827 cmd_source(eap->arg, eap); | |
2828 } | |
2829 | |
2830 static void | |
2831 cmd_source(fname, eap) | |
2832 char_u *fname; | |
2833 exarg_T *eap; | |
2834 { | |
2835 if (*fname == NUL) | |
2836 EMSG(_(e_argreq)); | |
2837 | |
2838 else if (eap != NULL && eap->forceit) | |
716 | 2839 /* ":source!": read Normal mdoe commands |
2840 * Need to execute the commands directly. This is required at least | |
2841 * for: | |
7 | 2842 * - ":g" command busy |
2843 * - after ":argdo", ":windo" or ":bufdo" | |
2844 * - another command follows | |
2845 * - inside a loop | |
2846 */ | |
2847 openscript(fname, global_busy || listcmd_busy || eap->nextcmd != NULL | |
2848 #ifdef FEAT_EVAL | |
2849 || eap->cstack->cs_idx >= 0 | |
2850 #endif | |
2851 ); | |
2852 | |
2853 /* ":source" read ex commands */ | |
819 | 2854 else if (do_source(fname, FALSE, DOSO_NONE) == FAIL) |
7 | 2855 EMSG2(_(e_notopen), fname); |
2856 } | |
2857 | |
2858 /* | |
2859 * ":source" and associated commands. | |
2860 */ | |
2861 /* | |
2862 * Structure used to store info for each sourced file. | |
2863 * It is shared between do_source() and getsourceline(). | |
2864 * This is required, because it needs to be handed to do_cmdline() and | |
2865 * sourcing can be done recursively. | |
2866 */ | |
2867 struct source_cookie | |
2868 { | |
2869 FILE *fp; /* opened file for sourcing */ | |
2870 char_u *nextline; /* if not NULL: line that was read ahead */ | |
2871 int finished; /* ":finish" used */ | |
2250
1bac28a53fae
Add the conceal patch from Vince Negri.
Bram Moolenaar <bram@vim.org>
parents:
2201
diff
changeset
|
2872 #if defined(USE_CRNL) || defined(USE_CR) |
7 | 2873 int fileformat; /* EOL_UNKNOWN, EOL_UNIX or EOL_DOS */ |
2874 int error; /* TRUE if LF found after CR-LF */ | |
2875 #endif | |
2876 #ifdef FEAT_EVAL | |
2877 linenr_T breakpoint; /* next line with breakpoint or zero */ | |
2878 char_u *fname; /* name of sourced file */ | |
2879 int dbg_tick; /* debug_tick when breakpoint was set */ | |
2880 int level; /* top nesting level of sourced file */ | |
2881 #endif | |
2882 #ifdef FEAT_MBYTE | |
2883 vimconv_T conv; /* type of conversion */ | |
2884 #endif | |
2885 }; | |
2886 | |
2887 #ifdef FEAT_EVAL | |
2888 /* | |
2889 * Return the address holding the next breakpoint line for a source cookie. | |
2890 */ | |
2891 linenr_T * | |
2892 source_breakpoint(cookie) | |
2893 void *cookie; | |
2894 { | |
2895 return &((struct source_cookie *)cookie)->breakpoint; | |
2896 } | |
2897 | |
2898 /* | |
2899 * Return the address holding the debug tick for a source cookie. | |
2900 */ | |
2901 int * | |
2902 source_dbg_tick(cookie) | |
2903 void *cookie; | |
2904 { | |
2905 return &((struct source_cookie *)cookie)->dbg_tick; | |
2906 } | |
2907 | |
2908 /* | |
2909 * Return the nesting level for a source cookie. | |
2910 */ | |
2911 int | |
2912 source_level(cookie) | |
2913 void *cookie; | |
2914 { | |
2915 return ((struct source_cookie *)cookie)->level; | |
2916 } | |
2917 #endif | |
2918 | |
2919 static char_u *get_one_sourceline __ARGS((struct source_cookie *sp)); | |
2920 | |
2052
057029bf3470
updated for version 7.2.338
Bram Moolenaar <bram@zimbu.org>
parents:
2051
diff
changeset
|
2921 #if (defined(WIN32) && defined(FEAT_CSCOPE)) || defined(HAVE_FD_CLOEXEC) |
057029bf3470
updated for version 7.2.338
Bram Moolenaar <bram@zimbu.org>
parents:
2051
diff
changeset
|
2922 # define USE_FOPEN_NOINH |
7 | 2923 static FILE *fopen_noinh_readbin __ARGS((char *filename)); |
2924 | |
2925 /* | |
2926 * Special function to open a file without handle inheritance. | |
2052
057029bf3470
updated for version 7.2.338
Bram Moolenaar <bram@zimbu.org>
parents:
2051
diff
changeset
|
2927 * When possible the handle is closed on exec(). |
7 | 2928 */ |
2929 static FILE * | |
2930 fopen_noinh_readbin(filename) | |
2931 char *filename; | |
2932 { | |
2052
057029bf3470
updated for version 7.2.338
Bram Moolenaar <bram@zimbu.org>
parents:
2051
diff
changeset
|
2933 # ifdef WIN32 |
2058
fb1222c880fc
updated for version 7.2.344
Bram Moolenaar <bram@zimbu.org>
parents:
2057
diff
changeset
|
2934 int fd_tmp = mch_open(filename, O_RDONLY | O_BINARY | O_NOINHERIT, 0); |
fb1222c880fc
updated for version 7.2.344
Bram Moolenaar <bram@zimbu.org>
parents:
2057
diff
changeset
|
2935 # else |
fb1222c880fc
updated for version 7.2.344
Bram Moolenaar <bram@zimbu.org>
parents:
2057
diff
changeset
|
2936 int fd_tmp = mch_open(filename, O_RDONLY, 0); |
2052
057029bf3470
updated for version 7.2.338
Bram Moolenaar <bram@zimbu.org>
parents:
2051
diff
changeset
|
2937 # endif |
7 | 2938 |
2939 if (fd_tmp == -1) | |
2940 return NULL; | |
2052
057029bf3470
updated for version 7.2.338
Bram Moolenaar <bram@zimbu.org>
parents:
2051
diff
changeset
|
2941 |
057029bf3470
updated for version 7.2.338
Bram Moolenaar <bram@zimbu.org>
parents:
2051
diff
changeset
|
2942 # ifdef HAVE_FD_CLOEXEC |
057029bf3470
updated for version 7.2.338
Bram Moolenaar <bram@zimbu.org>
parents:
2051
diff
changeset
|
2943 { |
057029bf3470
updated for version 7.2.338
Bram Moolenaar <bram@zimbu.org>
parents:
2051
diff
changeset
|
2944 int fdflags = fcntl(fd_tmp, F_GETFD); |
057029bf3470
updated for version 7.2.338
Bram Moolenaar <bram@zimbu.org>
parents:
2051
diff
changeset
|
2945 if (fdflags >= 0 && (fdflags & FD_CLOEXEC) == 0) |
057029bf3470
updated for version 7.2.338
Bram Moolenaar <bram@zimbu.org>
parents:
2051
diff
changeset
|
2946 fcntl(fd_tmp, F_SETFD, fdflags | FD_CLOEXEC); |
057029bf3470
updated for version 7.2.338
Bram Moolenaar <bram@zimbu.org>
parents:
2051
diff
changeset
|
2947 } |
057029bf3470
updated for version 7.2.338
Bram Moolenaar <bram@zimbu.org>
parents:
2051
diff
changeset
|
2948 # endif |
057029bf3470
updated for version 7.2.338
Bram Moolenaar <bram@zimbu.org>
parents:
2051
diff
changeset
|
2949 |
7 | 2950 return fdopen(fd_tmp, READBIN); |
2951 } | |
2952 #endif | |
2953 | |
2954 | |
2955 /* | |
2956 * do_source: Read the file "fname" and execute its lines as EX commands. | |
2957 * | |
2958 * This function may be called recursively! | |
2959 * | |
2960 * return FAIL if file could not be opened, OK otherwise | |
2961 */ | |
2962 int | |
2963 do_source(fname, check_other, is_vimrc) | |
2964 char_u *fname; | |
2965 int check_other; /* check for .vimrc and _vimrc */ | |
819 | 2966 int is_vimrc; /* DOSO_ value */ |
7 | 2967 { |
2968 struct source_cookie cookie; | |
2969 char_u *save_sourcing_name; | |
2970 linenr_T save_sourcing_lnum; | |
2971 char_u *p; | |
2972 char_u *fname_exp; | |
1802 | 2973 char_u *firstline = NULL; |
7 | 2974 int retval = FAIL; |
2975 #ifdef FEAT_EVAL | |
2976 scid_T save_current_SID; | |
2977 static scid_T last_current_SID = 0; | |
2978 void *save_funccalp; | |
2979 int save_debug_break_level = debug_break_level; | |
170 | 2980 scriptitem_T *si = NULL; |
7 | 2981 # ifdef UNIX |
2982 struct stat st; | |
2983 int stat_ok; | |
2984 # endif | |
2985 #endif | |
2986 #ifdef STARTUPTIME | |
2987 struct timeval tv_rel; | |
2988 struct timeval tv_start; | |
2989 #endif | |
170 | 2990 #ifdef FEAT_PROFILE |
2991 proftime_T wait_start; | |
2992 #endif | |
7 | 2993 |
2994 p = expand_env_save(fname); | |
2995 if (p == NULL) | |
2996 return retval; | |
2997 fname_exp = fix_fname(p); | |
2998 vim_free(p); | |
2999 if (fname_exp == NULL) | |
3000 return retval; | |
3001 if (mch_isdir(fname_exp)) | |
3002 { | |
274 | 3003 smsg((char_u *)_("Cannot source a directory: \"%s\""), fname); |
7 | 3004 goto theend; |
3005 } | |
3006 | |
716 | 3007 #ifdef FEAT_AUTOCMD |
1061 | 3008 /* Apply SourceCmd autocommands, they should get the file and source it. */ |
3009 if (has_autocmd(EVENT_SOURCECMD, fname_exp, NULL) | |
3010 && apply_autocmds(EVENT_SOURCECMD, fname_exp, fname_exp, | |
3011 FALSE, curbuf)) | |
1515 | 3012 { |
1061 | 3013 # ifdef FEAT_EVAL |
1515 | 3014 retval = aborting() ? FAIL : OK; |
1061 | 3015 # else |
1515 | 3016 retval = OK; |
1061 | 3017 # endif |
1515 | 3018 goto theend; |
3019 } | |
1061 | 3020 |
3021 /* Apply SourcePre autocommands, they may get the file. */ | |
716 | 3022 apply_autocmds(EVENT_SOURCEPRE, fname_exp, fname_exp, FALSE, curbuf); |
3023 #endif | |
3024 | |
2052
057029bf3470
updated for version 7.2.338
Bram Moolenaar <bram@zimbu.org>
parents:
2051
diff
changeset
|
3025 #ifdef USE_FOPEN_NOINH |
7 | 3026 cookie.fp = fopen_noinh_readbin((char *)fname_exp); |
3027 #else | |
3028 cookie.fp = mch_fopen((char *)fname_exp, READBIN); | |
3029 #endif | |
3030 if (cookie.fp == NULL && check_other) | |
3031 { | |
3032 /* | |
3033 * Try again, replacing file name ".vimrc" by "_vimrc" or vice versa, | |
3034 * and ".exrc" by "_exrc" or vice versa. | |
3035 */ | |
3036 p = gettail(fname_exp); | |
3037 if ((*p == '.' || *p == '_') | |
3038 && (STRICMP(p + 1, "vimrc") == 0 | |
3039 || STRICMP(p + 1, "gvimrc") == 0 | |
3040 || STRICMP(p + 1, "exrc") == 0)) | |
3041 { | |
3042 if (*p == '_') | |
3043 *p = '.'; | |
3044 else | |
3045 *p = '_'; | |
2052
057029bf3470
updated for version 7.2.338
Bram Moolenaar <bram@zimbu.org>
parents:
2051
diff
changeset
|
3046 #ifdef USE_FOPEN_NOINH |
7 | 3047 cookie.fp = fopen_noinh_readbin((char *)fname_exp); |
3048 #else | |
3049 cookie.fp = mch_fopen((char *)fname_exp, READBIN); | |
3050 #endif | |
3051 } | |
3052 } | |
3053 | |
3054 if (cookie.fp == NULL) | |
3055 { | |
3056 if (p_verbose > 0) | |
3057 { | |
294 | 3058 verbose_enter(); |
7 | 3059 if (sourcing_name == NULL) |
274 | 3060 smsg((char_u *)_("could not source \"%s\""), fname); |
7 | 3061 else |
3062 smsg((char_u *)_("line %ld: could not source \"%s\""), | |
274 | 3063 sourcing_lnum, fname); |
294 | 3064 verbose_leave(); |
7 | 3065 } |
3066 goto theend; | |
3067 } | |
3068 | |
3069 /* | |
3070 * The file exists. | |
3071 * - In verbose mode, give a message. | |
3072 * - For a vimrc file, may want to set 'compatible', call vimrc_found(). | |
3073 */ | |
3074 if (p_verbose > 1) | |
3075 { | |
294 | 3076 verbose_enter(); |
7 | 3077 if (sourcing_name == NULL) |
274 | 3078 smsg((char_u *)_("sourcing \"%s\""), fname); |
7 | 3079 else |
3080 smsg((char_u *)_("line %ld: sourcing \"%s\""), | |
274 | 3081 sourcing_lnum, fname); |
294 | 3082 verbose_leave(); |
7 | 3083 } |
819 | 3084 if (is_vimrc == DOSO_VIMRC) |
3085 vimrc_found(fname_exp, (char_u *)"MYVIMRC"); | |
3086 else if (is_vimrc == DOSO_GVIMRC) | |
3087 vimrc_found(fname_exp, (char_u *)"MYGVIMRC"); | |
7 | 3088 |
3089 #ifdef USE_CRNL | |
3090 /* If no automatic file format: Set default to CR-NL. */ | |
3091 if (*p_ffs == NUL) | |
3092 cookie.fileformat = EOL_DOS; | |
3093 else | |
3094 cookie.fileformat = EOL_UNKNOWN; | |
3095 cookie.error = FALSE; | |
3096 #endif | |
3097 | |
3098 #ifdef USE_CR | |
3099 /* If no automatic file format: Set default to CR. */ | |
3100 if (*p_ffs == NUL) | |
3101 cookie.fileformat = EOL_MAC; | |
3102 else | |
3103 cookie.fileformat = EOL_UNKNOWN; | |
3104 cookie.error = FALSE; | |
3105 #endif | |
3106 | |
3107 cookie.nextline = NULL; | |
3108 cookie.finished = FALSE; | |
3109 | |
3110 #ifdef FEAT_EVAL | |
3111 /* | |
3112 * Check if this script has a breakpoint. | |
3113 */ | |
3114 cookie.breakpoint = dbg_find_breakpoint(TRUE, fname_exp, (linenr_T)0); | |
3115 cookie.fname = fname_exp; | |
3116 cookie.dbg_tick = debug_tick; | |
3117 | |
3118 cookie.level = ex_nesting_level; | |
3119 #endif | |
3120 | |
3121 /* | |
3122 * Keep the sourcing name/lnum, for recursive calls. | |
3123 */ | |
3124 save_sourcing_name = sourcing_name; | |
3125 sourcing_name = fname_exp; | |
3126 save_sourcing_lnum = sourcing_lnum; | |
3127 sourcing_lnum = 0; | |
3128 | |
1802 | 3129 #ifdef FEAT_MBYTE |
3130 cookie.conv.vc_type = CONV_NONE; /* no conversion */ | |
3131 | |
3132 /* Read the first line so we can check for a UTF-8 BOM. */ | |
3133 firstline = getsourceline(0, (void *)&cookie, 0); | |
3134 if (firstline != NULL && STRLEN(firstline) >= 3 && firstline[0] == 0xef | |
3135 && firstline[1] == 0xbb && firstline[2] == 0xbf) | |
3136 { | |
3137 /* Found BOM; setup conversion, skip over BOM and recode the line. */ | |
3138 convert_setup(&cookie.conv, (char_u *)"utf-8", p_enc); | |
3139 p = string_convert(&cookie.conv, firstline + 3, NULL); | |
1804 | 3140 if (p == NULL) |
3141 p = vim_strsave(firstline + 3); | |
1802 | 3142 if (p != NULL) |
3143 { | |
3144 vim_free(firstline); | |
3145 firstline = p; | |
3146 } | |
3147 } | |
3148 #endif | |
3149 | |
7 | 3150 #ifdef STARTUPTIME |
2053
94f44da44d2e
updated for version 7.2.339
Bram Moolenaar <bram@zimbu.org>
parents:
2052
diff
changeset
|
3151 if (time_fd != NULL) |
94f44da44d2e
updated for version 7.2.339
Bram Moolenaar <bram@zimbu.org>
parents:
2052
diff
changeset
|
3152 time_push(&tv_rel, &tv_start); |
7 | 3153 #endif |
3154 | |
3155 #ifdef FEAT_EVAL | |
170 | 3156 # ifdef FEAT_PROFILE |
790 | 3157 if (do_profiling == PROF_YES) |
170 | 3158 prof_child_enter(&wait_start); /* entering a child now */ |
3159 # endif | |
3160 | |
3161 /* Don't use local function variables, if called from a function. | |
3162 * Also starts profiling timer for nested script. */ | |
3163 save_funccalp = save_funccal(); | |
3164 | |
7 | 3165 /* |
3166 * Check if this script was sourced before to finds its SID. | |
3167 * If it's new, generate a new SID. | |
3168 */ | |
3169 save_current_SID = current_SID; | |
3170 # ifdef UNIX | |
3171 stat_ok = (mch_stat((char *)fname_exp, &st) >= 0); | |
3172 # endif | |
170 | 3173 for (current_SID = script_items.ga_len; current_SID > 0; --current_SID) |
3174 { | |
3175 si = &SCRIPT_ITEM(current_SID); | |
3176 if (si->sn_name != NULL | |
7 | 3177 && ( |
3178 # ifdef UNIX | |
161 | 3179 /* Compare dev/ino when possible, it catches symbolic |
3180 * links. Also compare file names, the inode may change | |
3181 * when the file was edited. */ | |
1882 | 3182 ((stat_ok && si->sn_dev_valid) |
170 | 3183 && (si->sn_dev == st.st_dev |
3184 && si->sn_ino == st.st_ino)) || | |
7 | 3185 # endif |
170 | 3186 fnamecmp(si->sn_name, fname_exp) == 0)) |
7 | 3187 break; |
170 | 3188 } |
7 | 3189 if (current_SID == 0) |
3190 { | |
3191 current_SID = ++last_current_SID; | |
170 | 3192 if (ga_grow(&script_items, (int)(current_SID - script_items.ga_len)) |
3193 == FAIL) | |
3194 goto almosttheend; | |
3195 while (script_items.ga_len < current_SID) | |
7 | 3196 { |
170 | 3197 ++script_items.ga_len; |
3198 SCRIPT_ITEM(script_items.ga_len).sn_name = NULL; | |
3199 # ifdef FEAT_PROFILE | |
3200 SCRIPT_ITEM(script_items.ga_len).sn_prof_on = FALSE; | |
3201 # endif | |
3202 } | |
3203 si = &SCRIPT_ITEM(current_SID); | |
3204 si->sn_name = fname_exp; | |
3205 fname_exp = NULL; | |
7 | 3206 # ifdef UNIX |
170 | 3207 if (stat_ok) |
3208 { | |
1882 | 3209 si->sn_dev_valid = TRUE; |
170 | 3210 si->sn_dev = st.st_dev; |
3211 si->sn_ino = st.st_ino; | |
3212 } | |
3213 else | |
1882 | 3214 si->sn_dev_valid = FALSE; |
7 | 3215 # endif |
170 | 3216 |
7 | 3217 /* Allocate the local script variables to use for this script. */ |
3218 new_script_vars(current_SID); | |
3219 } | |
3220 | |
170 | 3221 # ifdef FEAT_PROFILE |
790 | 3222 if (do_profiling == PROF_YES) |
170 | 3223 { |
3224 int forceit; | |
3225 | |
3226 /* Check if we do profiling for this script. */ | |
3227 if (!si->sn_prof_on && has_profiling(TRUE, si->sn_name, &forceit)) | |
3228 { | |
3229 script_do_profile(si); | |
3230 si->sn_pr_force = forceit; | |
3231 } | |
3232 if (si->sn_prof_on) | |
3233 { | |
3234 ++si->sn_pr_count; | |
3235 profile_start(&si->sn_pr_start); | |
3236 profile_zero(&si->sn_pr_children); | |
3237 } | |
3238 } | |
3239 # endif | |
7 | 3240 #endif |
3241 | |
3242 /* | |
3243 * Call do_cmdline, which will call getsourceline() to get the lines. | |
3244 */ | |
1802 | 3245 do_cmdline(firstline, getsourceline, (void *)&cookie, |
7 | 3246 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_REPEAT); |
3247 retval = OK; | |
170 | 3248 |
3249 #ifdef FEAT_PROFILE | |
790 | 3250 if (do_profiling == PROF_YES) |
170 | 3251 { |
3252 /* Get "si" again, "script_items" may have been reallocated. */ | |
3253 si = &SCRIPT_ITEM(current_SID); | |
3254 if (si->sn_prof_on) | |
3255 { | |
3256 profile_end(&si->sn_pr_start); | |
3257 profile_sub_wait(&wait_start, &si->sn_pr_start); | |
3258 profile_add(&si->sn_pr_total, &si->sn_pr_start); | |
720 | 3259 profile_self(&si->sn_pr_self, &si->sn_pr_start, |
3260 &si->sn_pr_children); | |
170 | 3261 } |
3262 } | |
7 | 3263 #endif |
3264 | |
3265 if (got_int) | |
3266 EMSG(_(e_interr)); | |
3267 sourcing_name = save_sourcing_name; | |
3268 sourcing_lnum = save_sourcing_lnum; | |
3269 if (p_verbose > 1) | |
3270 { | |
294 | 3271 verbose_enter(); |
274 | 3272 smsg((char_u *)_("finished sourcing %s"), fname); |
7 | 3273 if (sourcing_name != NULL) |
274 | 3274 smsg((char_u *)_("continuing in %s"), sourcing_name); |
294 | 3275 verbose_leave(); |
7 | 3276 } |
3277 #ifdef STARTUPTIME | |
2053
94f44da44d2e
updated for version 7.2.339
Bram Moolenaar <bram@zimbu.org>
parents:
2052
diff
changeset
|
3278 if (time_fd != NULL) |
94f44da44d2e
updated for version 7.2.339
Bram Moolenaar <bram@zimbu.org>
parents:
2052
diff
changeset
|
3279 { |
94f44da44d2e
updated for version 7.2.339
Bram Moolenaar <bram@zimbu.org>
parents:
2052
diff
changeset
|
3280 vim_snprintf((char *)IObuff, IOSIZE, "sourcing %s", fname); |
94f44da44d2e
updated for version 7.2.339
Bram Moolenaar <bram@zimbu.org>
parents:
2052
diff
changeset
|
3281 time_msg((char *)IObuff, &tv_start); |
94f44da44d2e
updated for version 7.2.339
Bram Moolenaar <bram@zimbu.org>
parents:
2052
diff
changeset
|
3282 time_pop(&tv_rel); |
94f44da44d2e
updated for version 7.2.339
Bram Moolenaar <bram@zimbu.org>
parents:
2052
diff
changeset
|
3283 } |
7 | 3284 #endif |
3285 | |
3286 #ifdef FEAT_EVAL | |
3287 /* | |
3288 * After a "finish" in debug mode, need to break at first command of next | |
3289 * sourced file. | |
3290 */ | |
3291 if (save_debug_break_level > ex_nesting_level | |
3292 && debug_break_level == ex_nesting_level) | |
3293 ++debug_break_level; | |
3294 #endif | |
3295 | |
170 | 3296 #ifdef FEAT_EVAL |
3297 almosttheend: | |
3298 current_SID = save_current_SID; | |
3299 restore_funccal(save_funccalp); | |
3300 # ifdef FEAT_PROFILE | |
790 | 3301 if (do_profiling == PROF_YES) |
170 | 3302 prof_child_exit(&wait_start); /* leaving a child now */ |
3303 # endif | |
3304 #endif | |
3305 fclose(cookie.fp); | |
3306 vim_free(cookie.nextline); | |
1802 | 3307 vim_free(firstline); |
170 | 3308 #ifdef FEAT_MBYTE |
3309 convert_setup(&cookie.conv, NULL, NULL); | |
3310 #endif | |
3311 | |
7 | 3312 theend: |
3313 vim_free(fname_exp); | |
3314 return retval; | |
3315 } | |
3316 | |
3317 #if defined(FEAT_EVAL) || defined(PROTO) | |
356 | 3318 |
7 | 3319 /* |
3320 * ":scriptnames" | |
3321 */ | |
3322 void | |
3323 ex_scriptnames(eap) | |
1876 | 3324 exarg_T *eap UNUSED; |
7 | 3325 { |
3326 int i; | |
3327 | |
170 | 3328 for (i = 1; i <= script_items.ga_len && !got_int; ++i) |
3329 if (SCRIPT_ITEM(i).sn_name != NULL) | |
2921 | 3330 { |
3331 home_replace(NULL, SCRIPT_ITEM(i).sn_name, | |
3332 NameBuff, MAXPATHL, TRUE); | |
3333 smsg((char_u *)"%3d: %s", i, NameBuff); | |
3429 | 3334 } |
7 | 3335 } |
3336 | |
3337 # if defined(BACKSLASH_IN_FILENAME) || defined(PROTO) | |
3338 /* | |
3339 * Fix slashes in the list of script names for 'shellslash'. | |
3340 */ | |
3341 void | |
3342 scriptnames_slash_adjust() | |
3343 { | |
3344 int i; | |
3345 | |
170 | 3346 for (i = 1; i <= script_items.ga_len; ++i) |
3347 if (SCRIPT_ITEM(i).sn_name != NULL) | |
3348 slash_adjust(SCRIPT_ITEM(i).sn_name); | |
7 | 3349 } |
3350 # endif | |
3351 | |
3352 /* | |
3353 * Get a pointer to a script name. Used for ":verbose set". | |
3354 */ | |
3355 char_u * | |
3356 get_scriptname(id) | |
3357 scid_T id; | |
3358 { | |
3359 if (id == SID_MODELINE) | |
681 | 3360 return (char_u *)_("modeline"); |
7 | 3361 if (id == SID_CMDARG) |
681 | 3362 return (char_u *)_("--cmd argument"); |
7 | 3363 if (id == SID_CARG) |
681 | 3364 return (char_u *)_("-c argument"); |
7 | 3365 if (id == SID_ENV) |
681 | 3366 return (char_u *)_("environment variable"); |
3367 if (id == SID_ERROR) | |
3368 return (char_u *)_("error handler"); | |
170 | 3369 return SCRIPT_ITEM(id).sn_name; |
3370 } | |
3371 | |
356 | 3372 # if defined(EXITFREE) || defined(PROTO) |
3373 void | |
3374 free_scriptnames() | |
3375 { | |
3376 int i; | |
3377 | |
3378 for (i = script_items.ga_len; i > 0; --i) | |
3379 vim_free(SCRIPT_ITEM(i).sn_name); | |
3380 ga_clear(&script_items); | |
3381 } | |
3382 # endif | |
3383 | |
7 | 3384 #endif |
3385 | |
3386 #if defined(USE_CR) || defined(PROTO) | |
3387 | |
3388 # if defined(__MSL__) && (__MSL__ >= 22) | |
3389 /* | |
3390 * Newer version of the Metrowerks library handle DOS and UNIX files | |
3391 * without help. | |
3392 * Test with earlier versions, MSL 2.2 is the library supplied with | |
3393 * Codewarrior Pro 2. | |
3394 */ | |
3395 char * | |
3396 fgets_cr(s, n, stream) | |
3397 char *s; | |
3398 int n; | |
3399 FILE *stream; | |
3400 { | |
3401 return fgets(s, n, stream); | |
3402 } | |
3403 # else | |
3404 /* | |
3405 * Version of fgets() which also works for lines ending in a <CR> only | |
3406 * (Macintosh format). | |
3407 * For older versions of the Metrowerks library. | |
3408 * At least CodeWarrior 9 needed this code. | |
3409 */ | |
3410 char * | |
3411 fgets_cr(s, n, stream) | |
3412 char *s; | |
3413 int n; | |
3414 FILE *stream; | |
3415 { | |
3416 int c = 0; | |
3417 int char_read = 0; | |
3418 | |
3419 while (!feof(stream) && c != '\r' && c != '\n' && char_read < n - 1) | |
3420 { | |
3421 c = fgetc(stream); | |
3422 s[char_read++] = c; | |
3423 /* If the file is in DOS format, we need to skip a NL after a CR. I | |
3424 * thought it was the other way around, but this appears to work... */ | |
3425 if (c == '\n') | |
3426 { | |
3427 c = fgetc(stream); | |
3428 if (c != '\r') | |
3429 ungetc(c, stream); | |
3430 } | |
3431 } | |
3432 | |
3433 s[char_read] = 0; | |
3434 if (char_read == 0) | |
3435 return NULL; | |
3436 | |
3437 if (feof(stream) && char_read == 1) | |
3438 return NULL; | |
3439 | |
3440 return s; | |
3441 } | |
3442 # endif | |
3443 #endif | |
3444 | |
3445 /* | |
3446 * Get one full line from a sourced file. | |
3447 * Called by do_cmdline() when it's called from do_source(). | |
3448 * | |
3449 * Return a pointer to the line in allocated memory. | |
3450 * Return NULL for end-of-file or some error. | |
3451 */ | |
3452 char_u * | |
3453 getsourceline(c, cookie, indent) | |
1876 | 3454 int c UNUSED; |
7 | 3455 void *cookie; |
1876 | 3456 int indent UNUSED; |
7 | 3457 { |
3458 struct source_cookie *sp = (struct source_cookie *)cookie; | |
3459 char_u *line; | |
3336 | 3460 char_u *p; |
7 | 3461 |
3462 #ifdef FEAT_EVAL | |
3463 /* If breakpoints have been added/deleted need to check for it. */ | |
3464 if (sp->dbg_tick < debug_tick) | |
3465 { | |
3466 sp->breakpoint = dbg_find_breakpoint(TRUE, sp->fname, sourcing_lnum); | |
3467 sp->dbg_tick = debug_tick; | |
3468 } | |
170 | 3469 # ifdef FEAT_PROFILE |
790 | 3470 if (do_profiling == PROF_YES) |
170 | 3471 script_line_end(); |
3472 # endif | |
7 | 3473 #endif |
3474 /* | |
3475 * Get current line. If there is a read-ahead line, use it, otherwise get | |
3476 * one now. | |
3477 */ | |
3478 if (sp->finished) | |
3479 line = NULL; | |
3480 else if (sp->nextline == NULL) | |
3481 line = get_one_sourceline(sp); | |
3482 else | |
3483 { | |
3484 line = sp->nextline; | |
3485 sp->nextline = NULL; | |
3486 ++sourcing_lnum; | |
205 | 3487 } |
170 | 3488 #ifdef FEAT_PROFILE |
790 | 3489 if (line != NULL && do_profiling == PROF_YES) |
205 | 3490 script_line_start(); |
3491 #endif | |
7 | 3492 |
3493 /* Only concatenate lines starting with a \ when 'cpoptions' doesn't | |
3494 * contain the 'C' flag. */ | |
3495 if (line != NULL && (vim_strchr(p_cpo, CPO_CONCAT) == NULL)) | |
3496 { | |
3497 /* compensate for the one line read-ahead */ | |
3498 --sourcing_lnum; | |
3332 | 3499 |
3500 /* Get the next line and concatenate it when it starts with a | |
3501 * backslash. We always need to read the next line, keep it in | |
3502 * sp->nextline. */ | |
3503 sp->nextline = get_one_sourceline(sp); | |
3504 if (sp->nextline != NULL && *(p = skipwhite(sp->nextline)) == '\\') | |
7 | 3505 { |
3332 | 3506 garray_T ga; |
3507 | |
3378 | 3508 ga_init2(&ga, (int)sizeof(char_u), 400); |
3332 | 3509 ga_concat(&ga, line); |
3510 ga_concat(&ga, p + 1); | |
3511 for (;;) | |
3512 { | |
3513 vim_free(sp->nextline); | |
3514 sp->nextline = get_one_sourceline(sp); | |
3515 if (sp->nextline == NULL) | |
3516 break; | |
3517 p = skipwhite(sp->nextline); | |
3518 if (*p != '\\') | |
3519 break; | |
3378 | 3520 /* Adjust the growsize to the current length to speed up |
3521 * concatenating many lines. */ | |
3522 if (ga.ga_len > 400) | |
3523 { | |
3524 if (ga.ga_len > 8000) | |
3525 ga.ga_growsize = 8000; | |
3526 else | |
3527 ga.ga_growsize = ga.ga_len; | |
3528 } | |
3332 | 3529 ga_concat(&ga, p + 1); |
3530 } | |
3531 ga_append(&ga, NUL); | |
7 | 3532 vim_free(line); |
3332 | 3533 line = ga.ga_data; |
7 | 3534 } |
3535 } | |
3536 | |
3537 #ifdef FEAT_MBYTE | |
3538 if (line != NULL && sp->conv.vc_type != CONV_NONE) | |
3539 { | |
3336 | 3540 char_u *s; |
3541 | |
7 | 3542 /* Convert the encoding of the script line. */ |
3543 s = string_convert(&sp->conv, line, NULL); | |
3544 if (s != NULL) | |
3545 { | |
3546 vim_free(line); | |
3547 line = s; | |
3548 } | |
3549 } | |
3550 #endif | |
3551 | |
3552 #ifdef FEAT_EVAL | |
3553 /* Did we encounter a breakpoint? */ | |
3554 if (sp->breakpoint != 0 && sp->breakpoint <= sourcing_lnum) | |
3555 { | |
3556 dbg_breakpoint(sp->fname, sourcing_lnum); | |
3557 /* Find next breakpoint. */ | |
3558 sp->breakpoint = dbg_find_breakpoint(TRUE, sp->fname, sourcing_lnum); | |
3559 sp->dbg_tick = debug_tick; | |
3560 } | |
3561 #endif | |
3562 | |
3563 return line; | |
3564 } | |
3565 | |
3566 static char_u * | |
3567 get_one_sourceline(sp) | |
3568 struct source_cookie *sp; | |
3569 { | |
3570 garray_T ga; | |
3571 int len; | |
3572 int c; | |
3573 char_u *buf; | |
3574 #ifdef USE_CRNL | |
3575 int has_cr; /* CR-LF found */ | |
3576 #endif | |
3577 #ifdef USE_CR | |
3578 char_u *scan; | |
3579 #endif | |
3580 int have_read = FALSE; | |
3581 | |
3582 /* use a growarray to store the sourced line */ | |
154 | 3583 ga_init2(&ga, 1, 250); |
7 | 3584 |
3585 /* | |
3586 * Loop until there is a finished line (or end-of-file). | |
3587 */ | |
3588 sourcing_lnum++; | |
3589 for (;;) | |
3590 { | |
154 | 3591 /* make room to read at least 120 (more) characters */ |
3592 if (ga_grow(&ga, 120) == FAIL) | |
7 | 3593 break; |
3594 buf = (char_u *)ga.ga_data; | |
3595 | |
3596 #ifdef USE_CR | |
3597 if (sp->fileformat == EOL_MAC) | |
3598 { | |
41 | 3599 if (fgets_cr((char *)buf + ga.ga_len, ga.ga_maxlen - ga.ga_len, |
3600 sp->fp) == NULL) | |
7 | 3601 break; |
3602 } | |
3603 else | |
3604 #endif | |
41 | 3605 if (fgets((char *)buf + ga.ga_len, ga.ga_maxlen - ga.ga_len, |
3606 sp->fp) == NULL) | |
7 | 3607 break; |
154 | 3608 len = ga.ga_len + (int)STRLEN(buf + ga.ga_len); |
7 | 3609 #ifdef USE_CRNL |
3610 /* Ignore a trailing CTRL-Z, when in Dos mode. Only recognize the | |
3611 * CTRL-Z by its own, or after a NL. */ | |
3612 if ( (len == 1 || (len >= 2 && buf[len - 2] == '\n')) | |
3613 && sp->fileformat == EOL_DOS | |
3614 && buf[len - 1] == Ctrl_Z) | |
3615 { | |
3616 buf[len - 1] = NUL; | |
3617 break; | |
3618 } | |
3619 #endif | |
3620 | |
3621 #ifdef USE_CR | |
3622 /* If the read doesn't stop on a new line, and there's | |
3623 * some CR then we assume a Mac format */ | |
3624 if (sp->fileformat == EOL_UNKNOWN) | |
3625 { | |
3626 if (buf[len - 1] != '\n' && vim_strchr(buf, '\r') != NULL) | |
3627 sp->fileformat = EOL_MAC; | |
3628 else | |
3629 sp->fileformat = EOL_UNIX; | |
3630 } | |
3631 | |
3632 if (sp->fileformat == EOL_MAC) | |
3633 { | |
3634 scan = vim_strchr(buf, '\r'); | |
3635 | |
3636 if (scan != NULL) | |
3637 { | |
3638 *scan = '\n'; | |
3639 if (*(scan + 1) != 0) | |
3640 { | |
3641 *(scan + 1) = 0; | |
3642 fseek(sp->fp, (long)(scan - buf - len + 1), SEEK_CUR); | |
3643 } | |
3644 } | |
3645 len = STRLEN(buf); | |
3646 } | |
3647 #endif | |
3648 | |
3649 have_read = TRUE; | |
3650 ga.ga_len = len; | |
3651 | |
3652 /* If the line was longer than the buffer, read more. */ | |
41 | 3653 if (ga.ga_maxlen - ga.ga_len == 1 && buf[len - 1] != '\n') |
7 | 3654 continue; |
3655 | |
3656 if (len >= 1 && buf[len - 1] == '\n') /* remove trailing NL */ | |
3657 { | |
3658 #ifdef USE_CRNL | |
3659 has_cr = (len >= 2 && buf[len - 2] == '\r'); | |
3660 if (sp->fileformat == EOL_UNKNOWN) | |
3661 { | |
3662 if (has_cr) | |
3663 sp->fileformat = EOL_DOS; | |
3664 else | |
3665 sp->fileformat = EOL_UNIX; | |
3666 } | |
3667 | |
3668 if (sp->fileformat == EOL_DOS) | |
3669 { | |
3670 if (has_cr) /* replace trailing CR */ | |
3671 { | |
3672 buf[len - 2] = '\n'; | |
3673 --len; | |
3674 --ga.ga_len; | |
3675 } | |
3676 else /* lines like ":map xx yy^M" will have failed */ | |
3677 { | |
3678 if (!sp->error) | |
16 | 3679 { |
3680 msg_source(hl_attr(HLF_W)); | |
7 | 3681 EMSG(_("W15: Warning: Wrong line separator, ^M may be missing")); |
16 | 3682 } |
7 | 3683 sp->error = TRUE; |
3684 sp->fileformat = EOL_UNIX; | |
3685 } | |
3686 } | |
3687 #endif | |
3688 /* The '\n' is escaped if there is an odd number of ^V's just | |
3689 * before it, first set "c" just before the 'V's and then check | |
3690 * len&c parities (is faster than ((len-c)%2 == 0)) -- Acevedo */ | |
3691 for (c = len - 2; c >= 0 && buf[c] == Ctrl_V; c--) | |
3692 ; | |
3693 if ((len & 1) != (c & 1)) /* escaped NL, read more */ | |
3694 { | |
3695 sourcing_lnum++; | |
3696 continue; | |
3697 } | |
3698 | |
3699 buf[len - 1] = NUL; /* remove the NL */ | |
3700 } | |
3701 | |
3702 /* | |
3703 * Check for ^C here now and then, so recursive :so can be broken. | |
3704 */ | |
3705 line_breakcheck(); | |
3706 break; | |
3707 } | |
3708 | |
3709 if (have_read) | |
3710 return (char_u *)ga.ga_data; | |
3711 | |
3712 vim_free(ga.ga_data); | |
3713 return NULL; | |
3714 } | |
3715 | |
170 | 3716 #if defined(FEAT_PROFILE) || defined(PROTO) |
3717 /* | |
3718 * Called when starting to read a script line. | |
3719 * "sourcing_lnum" must be correct! | |
3720 * When skipping lines it may not actually be executed, but we won't find out | |
3721 * until later and we need to store the time now. | |
3722 */ | |
3723 void | |
3724 script_line_start() | |
3725 { | |
3726 scriptitem_T *si; | |
3727 sn_prl_T *pp; | |
3728 | |
3729 if (current_SID <= 0 || current_SID > script_items.ga_len) | |
3730 return; | |
3731 si = &SCRIPT_ITEM(current_SID); | |
3732 if (si->sn_prof_on && sourcing_lnum >= 1) | |
3733 { | |
1624 | 3734 /* Grow the array before starting the timer, so that the time spent |
170 | 3735 * here isn't counted. */ |
3736 ga_grow(&si->sn_prl_ga, (int)(sourcing_lnum - si->sn_prl_ga.ga_len)); | |
3737 si->sn_prl_idx = sourcing_lnum - 1; | |
3738 while (si->sn_prl_ga.ga_len <= si->sn_prl_idx | |
3739 && si->sn_prl_ga.ga_len < si->sn_prl_ga.ga_maxlen) | |
3740 { | |
3741 /* Zero counters for a line that was not used before. */ | |
3742 pp = &PRL_ITEM(si, si->sn_prl_ga.ga_len); | |
3743 pp->snp_count = 0; | |
3744 profile_zero(&pp->sn_prl_total); | |
3745 profile_zero(&pp->sn_prl_self); | |
3746 ++si->sn_prl_ga.ga_len; | |
3747 } | |
3748 si->sn_prl_execed = FALSE; | |
3749 profile_start(&si->sn_prl_start); | |
3750 profile_zero(&si->sn_prl_children); | |
3751 profile_get_wait(&si->sn_prl_wait); | |
3752 } | |
3753 } | |
3754 | |
3755 /* | |
3756 * Called when actually executing a function line. | |
3757 */ | |
3758 void | |
3759 script_line_exec() | |
3760 { | |
3761 scriptitem_T *si; | |
3762 | |
3763 if (current_SID <= 0 || current_SID > script_items.ga_len) | |
3764 return; | |
3765 si = &SCRIPT_ITEM(current_SID); | |
3766 if (si->sn_prof_on && si->sn_prl_idx >= 0) | |
3767 si->sn_prl_execed = TRUE; | |
3768 } | |
3769 | |
3770 /* | |
3771 * Called when done with a function line. | |
3772 */ | |
3773 void | |
3774 script_line_end() | |
3775 { | |
3776 scriptitem_T *si; | |
3777 sn_prl_T *pp; | |
3778 | |
3779 if (current_SID <= 0 || current_SID > script_items.ga_len) | |
3780 return; | |
3781 si = &SCRIPT_ITEM(current_SID); | |
3782 if (si->sn_prof_on && si->sn_prl_idx >= 0 | |
3783 && si->sn_prl_idx < si->sn_prl_ga.ga_len) | |
3784 { | |
3785 if (si->sn_prl_execed) | |
3786 { | |
3787 pp = &PRL_ITEM(si, si->sn_prl_idx); | |
3788 ++pp->snp_count; | |
3789 profile_end(&si->sn_prl_start); | |
3790 profile_sub_wait(&si->sn_prl_wait, &si->sn_prl_start); | |
3791 profile_add(&pp->sn_prl_total, &si->sn_prl_start); | |
720 | 3792 profile_self(&pp->sn_prl_self, &si->sn_prl_start, |
3793 &si->sn_prl_children); | |
170 | 3794 } |
3795 si->sn_prl_idx = -1; | |
3796 } | |
3797 } | |
3798 #endif | |
3799 | |
7 | 3800 /* |
3801 * ":scriptencoding": Set encoding conversion for a sourced script. | |
3802 * Without the multi-byte feature it's simply ignored. | |
3803 */ | |
3804 void | |
3805 ex_scriptencoding(eap) | |
1876 | 3806 exarg_T *eap UNUSED; |
7 | 3807 { |
3808 #ifdef FEAT_MBYTE | |
3809 struct source_cookie *sp; | |
3810 char_u *name; | |
3811 | |
3812 if (!getline_equal(eap->getline, eap->cookie, getsourceline)) | |
3813 { | |
3814 EMSG(_("E167: :scriptencoding used outside of a sourced file")); | |
3815 return; | |
3816 } | |
3817 | |
3818 if (*eap->arg != NUL) | |
3819 { | |
3820 name = enc_canonize(eap->arg); | |
3821 if (name == NULL) /* out of memory */ | |
3822 return; | |
3823 } | |
3824 else | |
3825 name = eap->arg; | |
3826 | |
3827 /* Setup for conversion from the specified encoding to 'encoding'. */ | |
3828 sp = (struct source_cookie *)getline_cookie(eap->getline, eap->cookie); | |
3829 convert_setup(&sp->conv, name, p_enc); | |
3830 | |
3831 if (name != eap->arg) | |
3832 vim_free(name); | |
3833 #endif | |
3834 } | |
3835 | |
3836 #if defined(FEAT_EVAL) || defined(PROTO) | |
3837 /* | |
3838 * ":finish": Mark a sourced file as finished. | |
3839 */ | |
3840 void | |
3841 ex_finish(eap) | |
3842 exarg_T *eap; | |
3843 { | |
3844 if (getline_equal(eap->getline, eap->cookie, getsourceline)) | |
3845 do_finish(eap, FALSE); | |
3846 else | |
3847 EMSG(_("E168: :finish used outside of a sourced file")); | |
3848 } | |
3849 | |
3850 /* | |
3851 * Mark a sourced file as finished. Possibly makes the ":finish" pending. | |
3852 * Also called for a pending finish at the ":endtry" or after returning from | |
3853 * an extra do_cmdline(). "reanimate" is used in the latter case. | |
3854 */ | |
3855 void | |
3856 do_finish(eap, reanimate) | |
3857 exarg_T *eap; | |
3858 int reanimate; | |
3859 { | |
3860 int idx; | |
3861 | |
3862 if (reanimate) | |
3863 ((struct source_cookie *)getline_cookie(eap->getline, | |
3864 eap->cookie))->finished = FALSE; | |
3865 | |
3866 /* | |
3867 * Cleanup (and inactivate) conditionals, but stop when a try conditional | |
3868 * not in its finally clause (which then is to be executed next) is found. | |
3869 * In this case, make the ":finish" pending for execution at the ":endtry". | |
3870 * Otherwise, finish normally. | |
3871 */ | |
3872 idx = cleanup_conditionals(eap->cstack, 0, TRUE); | |
3873 if (idx >= 0) | |
3874 { | |
3875 eap->cstack->cs_pending[idx] = CSTP_FINISH; | |
3876 report_make_pending(CSTP_FINISH, NULL); | |
3877 } | |
3878 else | |
3879 ((struct source_cookie *)getline_cookie(eap->getline, | |
3880 eap->cookie))->finished = TRUE; | |
3881 } | |
3882 | |
3883 | |
3884 /* | |
3885 * Return TRUE when a sourced file had the ":finish" command: Don't give error | |
3886 * message for missing ":endif". | |
3887 * Return FALSE when not sourcing a file. | |
3888 */ | |
3889 int | |
944 | 3890 source_finished(fgetline, cookie) |
3891 char_u *(*fgetline) __ARGS((int, void *, int)); | |
7 | 3892 void *cookie; |
3893 { | |
944 | 3894 return (getline_equal(fgetline, cookie, getsourceline) |
7 | 3895 && ((struct source_cookie *)getline_cookie( |
944 | 3896 fgetline, cookie))->finished); |
7 | 3897 } |
3898 #endif | |
3899 | |
3900 #if defined(FEAT_LISTCMDS) || defined(PROTO) | |
3901 /* | |
3902 * ":checktime [buffer]" | |
3903 */ | |
3904 void | |
3905 ex_checktime(eap) | |
3906 exarg_T *eap; | |
3907 { | |
3908 buf_T *buf; | |
3909 int save_no_check_timestamps = no_check_timestamps; | |
3910 | |
3911 no_check_timestamps = 0; | |
3912 if (eap->addr_count == 0) /* default is all buffers */ | |
3913 check_timestamps(FALSE); | |
3914 else | |
3915 { | |
3916 buf = buflist_findnr((int)eap->line2); | |
3917 if (buf != NULL) /* cannot happen? */ | |
3918 (void)buf_check_timestamp(buf, FALSE); | |
3919 } | |
3920 no_check_timestamps = save_no_check_timestamps; | |
3921 } | |
3922 #endif | |
3923 | |
3924 #if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \ | |
3925 && (defined(FEAT_EVAL) || defined(FEAT_MULTI_LANG)) | |
2419
f579b934f51d
Fix build warnings and problems for tiny/small Win32 build. (Mike Williams)
Bram Moolenaar <bram@vim.org>
parents:
2250
diff
changeset
|
3926 # define HAVE_GET_LOCALE_VAL |
7 | 3927 static char *get_locale_val __ARGS((int what)); |
3928 | |
3929 static char * | |
3930 get_locale_val(what) | |
3931 int what; | |
3932 { | |
3933 char *loc; | |
3934 | |
3935 /* Obtain the locale value from the libraries. For DJGPP this is | |
3936 * redefined and it doesn't use the arguments. */ | |
3937 loc = setlocale(what, NULL); | |
3938 | |
823 | 3939 # ifdef WIN32 |
7 | 3940 if (loc != NULL) |
3941 { | |
3942 char_u *p; | |
3943 | |
823 | 3944 /* setocale() returns something like "LC_COLLATE=<name>;LC_..." when |
3945 * one of the values (e.g., LC_CTYPE) differs. */ | |
7 | 3946 p = vim_strchr(loc, '='); |
3947 if (p != NULL) | |
3948 { | |
3949 loc = ++p; | |
3950 while (*p != NUL) /* remove trailing newline */ | |
3951 { | |
823 | 3952 if (*p < ' ' || *p == ';') |
7 | 3953 { |
3954 *p = NUL; | |
3955 break; | |
3956 } | |
3957 ++p; | |
3958 } | |
3959 } | |
3960 } | |
3961 # endif | |
3962 | |
3963 return loc; | |
3964 } | |
3965 #endif | |
3966 | |
3967 | |
3968 #ifdef WIN32 | |
3969 /* | |
3970 * On MS-Windows locale names are strings like "German_Germany.1252", but | |
3971 * gettext expects "de". Try to translate one into another here for a few | |
3972 * supported languages. | |
3973 */ | |
3974 static char_u * | |
3975 gettext_lang(char_u *name) | |
3976 { | |
3977 int i; | |
3978 static char *(mtable[]) = { | |
3979 "afrikaans", "af", | |
3980 "czech", "cs", | |
3981 "dutch", "nl", | |
3982 "german", "de", | |
3983 "english_united kingdom", "en_GB", | |
3984 "spanish", "es", | |
3985 "french", "fr", | |
3986 "italian", "it", | |
3987 "japanese", "ja", | |
3988 "korean", "ko", | |
3989 "norwegian", "no", | |
3990 "polish", "pl", | |
3991 "russian", "ru", | |
3992 "slovak", "sk", | |
3993 "swedish", "sv", | |
3994 "ukrainian", "uk", | |
3995 "chinese_china", "zh_CN", | |
3996 "chinese_taiwan", "zh_TW", | |
3997 NULL}; | |
3998 | |
3999 for (i = 0; mtable[i] != NULL; i += 2) | |
4000 if (STRNICMP(mtable[i], name, STRLEN(mtable[i])) == 0) | |
4001 return mtable[i + 1]; | |
4002 return name; | |
4003 } | |
4004 #endif | |
4005 | |
4006 #if defined(FEAT_MULTI_LANG) || defined(PROTO) | |
4007 /* | |
4008 * Obtain the current messages language. Used to set the default for | |
4009 * 'helplang'. May return NULL or an empty string. | |
4010 */ | |
4011 char_u * | |
4012 get_mess_lang() | |
4013 { | |
4014 char_u *p; | |
4015 | |
2419
f579b934f51d
Fix build warnings and problems for tiny/small Win32 build. (Mike Williams)
Bram Moolenaar <bram@vim.org>
parents:
2250
diff
changeset
|
4016 # ifdef HAVE_GET_LOCALE_VAL |
7 | 4017 # if defined(LC_MESSAGES) |
4018 p = (char_u *)get_locale_val(LC_MESSAGES); | |
4019 # else | |
4020 /* This is necessary for Win32, where LC_MESSAGES is not defined and $LANG | |
823 | 4021 * may be set to the LCID number. LC_COLLATE is the best guess, LC_TIME |
4022 * and LC_MONETARY may be set differently for a Japanese working in the | |
4023 * US. */ | |
4024 p = (char_u *)get_locale_val(LC_COLLATE); | |
7 | 4025 # endif |
4026 # else | |
4027 p = mch_getenv((char_u *)"LC_ALL"); | |
4028 if (p == NULL || *p == NUL) | |
4029 { | |
4030 p = mch_getenv((char_u *)"LC_MESSAGES"); | |
4031 if (p == NULL || *p == NUL) | |
4032 p = mch_getenv((char_u *)"LANG"); | |
4033 } | |
4034 # endif | |
4035 # ifdef WIN32 | |
4036 p = gettext_lang(p); | |
4037 # endif | |
4038 return p; | |
4039 } | |
4040 #endif | |
4041 | |
45 | 4042 /* Complicated #if; matches with where get_mess_env() is used below. */ |
4043 #if (defined(FEAT_EVAL) && !((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \ | |
4044 && defined(LC_MESSAGES))) \ | |
4045 || ((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \ | |
4046 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)) \ | |
4047 && !defined(LC_MESSAGES)) | |
7 | 4048 static char_u *get_mess_env __ARGS((void)); |
4049 | |
4050 /* | |
4051 * Get the language used for messages from the environment. | |
4052 */ | |
4053 static char_u * | |
4054 get_mess_env() | |
4055 { | |
4056 char_u *p; | |
4057 | |
4058 p = mch_getenv((char_u *)"LC_ALL"); | |
4059 if (p == NULL || *p == NUL) | |
4060 { | |
4061 p = mch_getenv((char_u *)"LC_MESSAGES"); | |
4062 if (p == NULL || *p == NUL) | |
4063 { | |
4064 p = mch_getenv((char_u *)"LANG"); | |
4065 if (p != NULL && VIM_ISDIGIT(*p)) | |
4066 p = NULL; /* ignore something like "1043" */ | |
2419
f579b934f51d
Fix build warnings and problems for tiny/small Win32 build. (Mike Williams)
Bram Moolenaar <bram@vim.org>
parents:
2250
diff
changeset
|
4067 # ifdef HAVE_GET_LOCALE_VAL |
7 | 4068 if (p == NULL || *p == NUL) |
4069 p = (char_u *)get_locale_val(LC_CTYPE); | |
4070 # endif | |
4071 } | |
4072 } | |
4073 return p; | |
4074 } | |
4075 #endif | |
4076 | |
4077 #if defined(FEAT_EVAL) || defined(PROTO) | |
4078 | |
4079 /* | |
4080 * Set the "v:lang" variable according to the current locale setting. | |
4081 * Also do "v:lc_time"and "v:ctype". | |
4082 */ | |
4083 void | |
4084 set_lang_var() | |
4085 { | |
4086 char_u *loc; | |
4087 | |
2419
f579b934f51d
Fix build warnings and problems for tiny/small Win32 build. (Mike Williams)
Bram Moolenaar <bram@vim.org>
parents:
2250
diff
changeset
|
4088 # ifdef HAVE_GET_LOCALE_VAL |
7 | 4089 loc = (char_u *)get_locale_val(LC_CTYPE); |
4090 # else | |
4091 /* setlocale() not supported: use the default value */ | |
4092 loc = (char_u *)"C"; | |
4093 # endif | |
4094 set_vim_var_string(VV_CTYPE, loc, -1); | |
4095 | |
4096 /* When LC_MESSAGES isn't defined use the value from $LC_MESSAGES, fall | |
4097 * back to LC_CTYPE if it's empty. */ | |
2419
f579b934f51d
Fix build warnings and problems for tiny/small Win32 build. (Mike Williams)
Bram Moolenaar <bram@vim.org>
parents:
2250
diff
changeset
|
4098 # if defined(HAVE_GET_LOCALE_VAL) && defined(LC_MESSAGES) |
7 | 4099 loc = (char_u *)get_locale_val(LC_MESSAGES); |
4100 # else | |
4101 loc = get_mess_env(); | |
4102 # endif | |
4103 set_vim_var_string(VV_LANG, loc, -1); | |
4104 | |
2419
f579b934f51d
Fix build warnings and problems for tiny/small Win32 build. (Mike Williams)
Bram Moolenaar <bram@vim.org>
parents:
2250
diff
changeset
|
4105 # ifdef HAVE_GET_LOCALE_VAL |
7 | 4106 loc = (char_u *)get_locale_val(LC_TIME); |
4107 # endif | |
4108 set_vim_var_string(VV_LC_TIME, loc, -1); | |
4109 } | |
4110 #endif | |
4111 | |
4112 #if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \ | |
4113 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)) | |
4114 /* | |
4115 * ":language": Set the language (locale). | |
4116 */ | |
4117 void | |
4118 ex_language(eap) | |
4119 exarg_T *eap; | |
4120 { | |
4121 char *loc; | |
4122 char_u *p; | |
4123 char_u *name; | |
4124 int what = LC_ALL; | |
4125 char *whatstr = ""; | |
4126 #ifdef LC_MESSAGES | |
4127 # define VIM_LC_MESSAGES LC_MESSAGES | |
4128 #else | |
4129 # define VIM_LC_MESSAGES 6789 | |
4130 #endif | |
4131 | |
4132 name = eap->arg; | |
4133 | |
4134 /* Check for "messages {name}", "ctype {name}" or "time {name}" argument. | |
4135 * Allow abbreviation, but require at least 3 characters to avoid | |
4136 * confusion with a two letter language name "me" or "ct". */ | |
4137 p = skiptowhite(eap->arg); | |
4138 if ((*p == NUL || vim_iswhite(*p)) && p - eap->arg >= 3) | |
4139 { | |
4140 if (STRNICMP(eap->arg, "messages", p - eap->arg) == 0) | |
4141 { | |
4142 what = VIM_LC_MESSAGES; | |
4143 name = skipwhite(p); | |
4144 whatstr = "messages "; | |
4145 } | |
4146 else if (STRNICMP(eap->arg, "ctype", p - eap->arg) == 0) | |
4147 { | |
4148 what = LC_CTYPE; | |
4149 name = skipwhite(p); | |
4150 whatstr = "ctype "; | |
4151 } | |
4152 else if (STRNICMP(eap->arg, "time", p - eap->arg) == 0) | |
4153 { | |
4154 what = LC_TIME; | |
4155 name = skipwhite(p); | |
4156 whatstr = "time "; | |
4157 } | |
4158 } | |
4159 | |
4160 if (*name == NUL) | |
4161 { | |
4162 #ifndef LC_MESSAGES | |
4163 if (what == VIM_LC_MESSAGES) | |
4164 p = get_mess_env(); | |
4165 else | |
4166 #endif | |
4167 p = (char_u *)setlocale(what, NULL); | |
4168 if (p == NULL || *p == NUL) | |
4169 p = (char_u *)"Unknown"; | |
4170 smsg((char_u *)_("Current %slanguage: \"%s\""), whatstr, p); | |
4171 } | |
4172 else | |
4173 { | |
4174 #ifndef LC_MESSAGES | |
4175 if (what == VIM_LC_MESSAGES) | |
4176 loc = ""; | |
4177 else | |
4178 #endif | |
1624 | 4179 { |
7 | 4180 loc = setlocale(what, (char *)name); |
1624 | 4181 #if defined(FEAT_FLOAT) && defined(LC_NUMERIC) |
4182 /* Make sure strtod() uses a decimal point, not a comma. */ | |
4183 setlocale(LC_NUMERIC, "C"); | |
4184 #endif | |
4185 } | |
7 | 4186 if (loc == NULL) |
4187 EMSG2(_("E197: Cannot set language to \"%s\""), name); | |
4188 else | |
4189 { | |
4190 #ifdef HAVE_NL_MSG_CAT_CNTR | |
4191 /* Need to do this for GNU gettext, otherwise cached translations | |
4192 * will be used again. */ | |
4193 extern int _nl_msg_cat_cntr; | |
4194 | |
4195 ++_nl_msg_cat_cntr; | |
4196 #endif | |
1222 | 4197 /* Reset $LC_ALL, otherwise it would overrule everything. */ |
7 | 4198 vim_setenv((char_u *)"LC_ALL", (char_u *)""); |
4199 | |
4200 if (what != LC_TIME) | |
4201 { | |
4202 /* Tell gettext() what to translate to. It apparently doesn't | |
4203 * use the currently effective locale. Also do this when | |
4204 * FEAT_GETTEXT isn't defined, so that shell commands use this | |
4205 * value. */ | |
4206 if (what == LC_ALL) | |
534 | 4207 { |
7 | 4208 vim_setenv((char_u *)"LANG", name); |
534 | 4209 # ifdef WIN32 |
4210 /* Apparently MS-Windows printf() may cause a crash when | |
4211 * we give it 8-bit text while it's expecting text in the | |
4212 * current locale. This call avoids that. */ | |
4213 setlocale(LC_CTYPE, "C"); | |
4214 # endif | |
4215 } | |
7 | 4216 if (what != LC_CTYPE) |
4217 { | |
4218 char_u *mname; | |
4219 #ifdef WIN32 | |
4220 mname = gettext_lang(name); | |
4221 #else | |
4222 mname = name; | |
4223 #endif | |
4224 vim_setenv((char_u *)"LC_MESSAGES", mname); | |
4225 #ifdef FEAT_MULTI_LANG | |
4226 set_helplang_default(mname); | |
4227 #endif | |
4228 } | |
4229 } | |
4230 | |
4231 # ifdef FEAT_EVAL | |
4232 /* Set v:lang, v:lc_time and v:ctype to the final result. */ | |
4233 set_lang_var(); | |
4234 # endif | |
3149 | 4235 # ifdef FEAT_TITLE |
4236 maketitle(); | |
4237 # endif | |
7 | 4238 } |
4239 } | |
4240 } | |
4241 | |
4242 # if defined(FEAT_CMDL_COMPL) || defined(PROTO) | |
2849 | 4243 |
4244 static char_u **locales = NULL; /* Array of all available locales */ | |
4245 static int did_init_locales = FALSE; | |
4246 | |
4247 static void init_locales __ARGS((void)); | |
4248 static char_u **find_locales __ARGS((void)); | |
4249 | |
4250 /* | |
4251 * Lazy initialization of all available locales. | |
4252 */ | |
4253 static void | |
4254 init_locales() | |
4255 { | |
4256 if (!did_init_locales) | |
4257 { | |
4258 did_init_locales = TRUE; | |
4259 locales = find_locales(); | |
4260 } | |
4261 } | |
4262 | |
4263 /* Return an array of strings for all available locales + NULL for the | |
4264 * last element. Return NULL in case of error. */ | |
4265 static char_u ** | |
4266 find_locales() | |
4267 { | |
4268 garray_T locales_ga; | |
4269 char_u *loc; | |
4270 | |
4271 /* Find all available locales by running command "locale -a". If this | |
4272 * doesn't work we won't have completion. */ | |
4273 char_u *locale_a = get_cmd_output((char_u *)"locale -a", | |
4274 NULL, SHELL_SILENT); | |
4275 if (locale_a == NULL) | |
4276 return NULL; | |
4277 ga_init2(&locales_ga, sizeof(char_u *), 20); | |
4278 | |
4279 /* Transform locale_a string where each locale is separated by "\n" | |
4280 * into an array of locale strings. */ | |
4281 loc = (char_u *)strtok((char *)locale_a, "\n"); | |
4282 | |
4283 while (loc != NULL) | |
4284 { | |
4285 if (ga_grow(&locales_ga, 1) == FAIL) | |
4286 break; | |
4287 loc = vim_strsave(loc); | |
4288 if (loc == NULL) | |
4289 break; | |
4290 | |
4291 ((char_u **)locales_ga.ga_data)[locales_ga.ga_len++] = loc; | |
4292 loc = (char_u *)strtok(NULL, "\n"); | |
4293 } | |
4294 vim_free(locale_a); | |
4295 if (ga_grow(&locales_ga, 1) == FAIL) | |
4296 { | |
4297 ga_clear(&locales_ga); | |
4298 return NULL; | |
4299 } | |
4300 ((char_u **)locales_ga.ga_data)[locales_ga.ga_len] = NULL; | |
4301 return (char_u **)locales_ga.ga_data; | |
4302 } | |
4303 | |
4304 # if defined(EXITFREE) || defined(PROTO) | |
4305 void | |
4306 free_locales() | |
4307 { | |
4308 int i; | |
4309 if (locales != NULL) | |
4310 { | |
4311 for (i = 0; locales[i] != NULL; i++) | |
4312 vim_free(locales[i]); | |
4313 vim_free(locales); | |
4314 locales = NULL; | |
4315 } | |
4316 } | |
4317 # endif | |
4318 | |
7 | 4319 /* |
4320 * Function given to ExpandGeneric() to obtain the possible arguments of the | |
4321 * ":language" command. | |
4322 */ | |
4323 char_u * | |
4324 get_lang_arg(xp, idx) | |
1876 | 4325 expand_T *xp UNUSED; |
7 | 4326 int idx; |
4327 { | |
4328 if (idx == 0) | |
4329 return (char_u *)"messages"; | |
4330 if (idx == 1) | |
4331 return (char_u *)"ctype"; | |
4332 if (idx == 2) | |
4333 return (char_u *)"time"; | |
2849 | 4334 |
4335 init_locales(); | |
4336 if (locales == NULL) | |
4337 return NULL; | |
4338 return locales[idx - 3]; | |
4339 } | |
4340 | |
4341 /* | |
4342 * Function given to ExpandGeneric() to obtain the available locales. | |
4343 */ | |
4344 char_u * | |
4345 get_locales(xp, idx) | |
4346 expand_T *xp UNUSED; | |
4347 int idx; | |
4348 { | |
4349 init_locales(); | |
4350 if (locales == NULL) | |
4351 return NULL; | |
4352 return locales[idx]; | |
7 | 4353 } |
4354 # endif | |
4355 | |
4356 #endif |