1 /* vi:set ts=8 sts=4 sw=4:
3 * VIM - Vi IMproved by Bram Moolenaar
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.
11 * ex_cmds2.c: some more functions for command line commands
14 #if defined(WIN32) && defined(FEAT_CSCOPE)
20 #if defined(WIN32) && defined(FEAT_CSCOPE)
26 static void cmd_source
__ARGS((char_u
*fname
, exarg_T
*eap
));
29 /* Growarray to store info about already sourced scripts.
30 * For Unix also store the dev/ino, so that we don't have to stat() each
31 * script when going through the list. */
32 typedef struct scriptitem_S
40 int sn_prof_on
; /* TRUE when script is/was profiled */
41 int sn_pr_force
; /* forceit: profile functions in this script */
42 proftime_T sn_pr_child
; /* time set when going into first child */
43 int sn_pr_nest
; /* nesting for sn_pr_child */
44 /* profiling the script as a whole */
45 int sn_pr_count
; /* nr of times sourced */
46 proftime_T sn_pr_total
; /* time spend in script + children */
47 proftime_T sn_pr_self
; /* time spend in script itself */
48 proftime_T sn_pr_start
; /* time at script start */
49 proftime_T sn_pr_children
; /* time in children after script start */
50 /* profiling the script per line */
51 garray_T sn_prl_ga
; /* things stored for every line */
52 proftime_T sn_prl_start
; /* start time for current line */
53 proftime_T sn_prl_children
; /* time spent in children for this line */
54 proftime_T sn_prl_wait
; /* wait start time for current line */
55 int sn_prl_idx
; /* index of line being timed; -1 if none */
56 int sn_prl_execed
; /* line being timed was executed */
60 static garray_T script_items
= {0, 0, sizeof(scriptitem_T
), 4, NULL
};
61 #define SCRIPT_ITEM(id) (((scriptitem_T *)script_items.ga_data)[(id) - 1])
64 /* Struct used in sn_prl_ga for every line of a script. */
65 typedef struct sn_prl_S
67 int snp_count
; /* nr of times line was executed */
68 proftime_T sn_prl_total
; /* time spend in a line + children */
69 proftime_T sn_prl_self
; /* time spend in a line itself */
72 # define PRL_ITEM(si, idx) (((sn_prl_T *)(si)->sn_prl_ga.ga_data)[(idx)])
76 #if defined(FEAT_EVAL) || defined(PROTO)
77 static int debug_greedy
= FALSE
; /* batch mode debugging: don't save
78 and restore typeahead. */
81 * do_debug(): Debug mode.
82 * Repeatedly get Ex commands, until told to continue normal execution.
88 int save_msg_scroll
= msg_scroll
;
89 int save_State
= State
;
90 int save_did_emsg
= did_emsg
;
91 int save_cmd_silent
= cmd_silent
;
92 int save_msg_silent
= msg_silent
;
93 int save_emsg_silent
= emsg_silent
;
94 int save_redir_off
= redir_off
;
95 tasave_T typeaheadbuf
;
97 int save_ex_normal_busy
;
100 char_u
*cmdline
= NULL
;
103 static int last_cmd
= 0;
109 #define CMD_INTERRUPT 6
111 #ifdef ALWAYS_USE_GUI
112 /* Can't do this when there is no terminal for input/output. */
115 /* Break as soon as possible. */
116 debug_break_level
= 9999;
121 /* Make sure we are in raw mode and start termcap mode. Might have side
126 ++RedrawingDisabled
; /* don't redisplay the window */
127 ++no_wait_return
; /* don't wait for return */
128 did_emsg
= FALSE
; /* don't use error from debugged stuff */
129 cmd_silent
= FALSE
; /* display commands */
130 msg_silent
= FALSE
; /* display messages */
131 emsg_silent
= FALSE
; /* display error messages */
132 redir_off
= TRUE
; /* don't redirect debug commands */
136 want_sniff_request
= 0; /* No K_SNIFF wanted */
140 MSG(_("Entering Debug mode. Type \"cont\" to continue."));
141 if (sourcing_name
!= NULL
)
143 if (sourcing_lnum
!= 0)
144 smsg((char_u
*)_("line %ld: %s"), (long)sourcing_lnum
, cmd
);
146 smsg((char_u
*)_("cmd: %s"), cmd
);
149 * Repeat getting a command and executing it.
154 need_wait_return
= FALSE
;
156 ProcessSniffRequests();
158 /* Save the current typeahead buffer and replace it with an empty one.
159 * This makes sure we get input from the user here and don't interfere
160 * with the commands being executed. Reset "ex_normal_busy" to avoid
161 * the side effects of using ":normal". Save the stuff buffer and make
163 # ifdef FEAT_EX_EXTRA
164 save_ex_normal_busy
= ex_normal_busy
;
168 save_typeahead(&typeaheadbuf
);
170 cmdline
= getcmdline_prompt('>', NULL
, 0, EXPAND_NOTHING
, NULL
);
173 restore_typeahead(&typeaheadbuf
);
174 # ifdef FEAT_EX_EXTRA
175 ex_normal_busy
= save_ex_normal_busy
;
178 cmdline_row
= msg_row
;
181 /* If this is a debug command, set "last_cmd".
182 * If not, reset "last_cmd".
183 * For a blank line use previous command. */
184 p
= skipwhite(cmdline
);
189 case 'c': last_cmd
= CMD_CONT
;
192 case 'n': last_cmd
= CMD_NEXT
;
195 case 's': last_cmd
= CMD_STEP
;
198 case 'f': last_cmd
= CMD_FINISH
;
201 case 'q': last_cmd
= CMD_QUIT
;
204 case 'i': last_cmd
= CMD_INTERRUPT
;
207 default: last_cmd
= 0;
211 /* Check that the tail matches. */
213 while (*p
!= NUL
&& *p
== *tail
)
218 if (ASCII_ISALPHA(*p
))
225 /* Execute debug command: decided where to break next and
230 debug_break_level
= -1;
233 debug_break_level
= ex_nesting_level
;
236 debug_break_level
= 9999;
239 debug_break_level
= ex_nesting_level
- 1;
243 debug_break_level
= -1;
247 debug_break_level
= 9999;
248 /* Do not repeat ">interrupt" cmd, continue stepping. */
255 /* don't debug this command */
256 n
= debug_break_level
;
257 debug_break_level
= -1;
258 (void)do_cmdline(cmdline
, getexline
, NULL
,
259 DOCMD_VERBOSE
|DOCMD_EXCRESET
);
260 debug_break_level
= n
;
264 lines_left
= Rows
- 1;
270 redraw_all_later(NOT_VALID
);
271 need_wait_return
= FALSE
;
272 msg_scroll
= save_msg_scroll
;
273 lines_left
= Rows
- 1;
275 did_emsg
= save_did_emsg
;
276 cmd_silent
= save_cmd_silent
;
277 msg_silent
= save_msg_silent
;
278 emsg_silent
= save_emsg_silent
;
279 redir_off
= save_redir_off
;
281 /* Only print the message again when typing a command before coming back
283 debug_did_msg
= TRUE
;
293 int debug_break_level_save
= debug_break_level
;
295 debug_break_level
= 9999;
296 do_cmdline_cmd(eap
->arg
);
297 debug_break_level
= debug_break_level_save
;
300 static char_u
*debug_breakpoint_name
= NULL
;
301 static linenr_T debug_breakpoint_lnum
;
304 * When debugging or a breakpoint is set on a skipped command, no debug prompt
305 * is shown by do_one_cmd(). This situation is indicated by debug_skipped, and
306 * debug_skipped_name is then set to the source name in the breakpoint case. If
307 * a skipped command decides itself that a debug prompt should be displayed, it
308 * can do so by calling dbg_check_skipped().
310 static int debug_skipped
;
311 static char_u
*debug_skipped_name
;
314 * Go to debug mode when a breakpoint was encountered or "ex_nesting_level" is
315 * at or below the break level. But only when the line is actually
316 * executed. Return TRUE and set breakpoint_name for skipped commands that
317 * decide to execute something themselves.
318 * Called from do_one_cmd() before executing a command.
321 dbg_check_breakpoint(eap
)
326 debug_skipped
= FALSE
;
327 if (debug_breakpoint_name
!= NULL
)
331 /* replace K_SNR with "<SNR>" */
332 if (debug_breakpoint_name
[0] == K_SPECIAL
333 && debug_breakpoint_name
[1] == KS_EXTRA
334 && debug_breakpoint_name
[2] == (int)KE_SNR
)
335 p
= (char_u
*)"<SNR>";
338 smsg((char_u
*)_("Breakpoint in \"%s%s\" line %ld"),
340 debug_breakpoint_name
+ (*p
== NUL
? 0 : 3),
341 (long)debug_breakpoint_lnum
);
342 debug_breakpoint_name
= NULL
;
347 debug_skipped
= TRUE
;
348 debug_skipped_name
= debug_breakpoint_name
;
349 debug_breakpoint_name
= NULL
;
352 else if (ex_nesting_level
<= debug_break_level
)
358 debug_skipped
= TRUE
;
359 debug_skipped_name
= NULL
;
365 * Go to debug mode if skipped by dbg_check_breakpoint() because eap->skip was
366 * set. Return TRUE when the debug mode is entered this time.
369 dbg_check_skipped(eap
)
377 * Save the value of got_int and reset it. We don't want a previous
378 * interruption cause flushing the input buffer.
380 prev_got_int
= got_int
;
382 debug_breakpoint_name
= debug_skipped_name
;
383 /* eap->skip is TRUE */
385 (void)dbg_check_breakpoint(eap
);
387 got_int
|= prev_got_int
;
394 * The list of breakpoints: dbg_breakp.
395 * This is a grow-array of structs.
399 int dbg_nr
; /* breakpoint number */
400 int dbg_type
; /* DBG_FUNC or DBG_FILE */
401 char_u
*dbg_name
; /* function or file name */
402 regprog_T
*dbg_prog
; /* regexp program */
403 linenr_T dbg_lnum
; /* line number in function or file */
404 int dbg_forceit
; /* ! used */
407 static garray_T dbg_breakp
= {0, 0, sizeof(struct debuggy
), 4, NULL
};
408 #define BREAKP(idx) (((struct debuggy *)dbg_breakp.ga_data)[idx])
409 #define DEBUGGY(gap, idx) (((struct debuggy *)gap->ga_data)[idx])
410 static int last_breakp
= 0; /* nr of last defined breakpoint */
413 /* Profiling uses file and func names similar to breakpoints. */
414 static garray_T prof_ga
= {0, 0, sizeof(struct debuggy
), 4, NULL
};
419 static int dbg_parsearg
__ARGS((char_u
*arg
, garray_T
*gap
));
420 static linenr_T debuggy_find
__ARGS((int file
,char_u
*fname
, linenr_T after
, garray_T
*gap
, int *fp
));
423 * Parse the arguments of ":profile", ":breakadd" or ":breakdel" and put them
424 * in the entry just after the last one in dbg_breakp. Note that "dbg_name"
426 * Returns FAIL for failure.
429 dbg_parsearg(arg
, gap
)
431 garray_T
*gap
; /* either &dbg_breakp or &prof_ga */
438 if (ga_grow(gap
, 1) == FAIL
)
440 bp
= &DEBUGGY(gap
, gap
->ga_len
);
442 /* Find "func" or "file". */
443 if (STRNCMP(p
, "func", 4) == 0)
444 bp
->dbg_type
= DBG_FUNC
;
445 else if (STRNCMP(p
, "file", 4) == 0)
446 bp
->dbg_type
= DBG_FILE
;
451 STRNCMP(p
, "here", 4) == 0)
453 if (curbuf
->b_ffname
== NULL
)
458 bp
->dbg_type
= DBG_FILE
;
463 EMSG2(_(e_invarg2
), p
);
466 p
= skipwhite(p
+ 4);
468 /* Find optional line number. */
470 bp
->dbg_lnum
= curwin
->w_cursor
.lnum
;
477 bp
->dbg_lnum
= getdigits(&p
);
483 /* Find the function or file name. Don't accept a function name with (). */
484 if ((!here
&& *p
== NUL
)
485 || (here
&& *p
!= NUL
)
486 || (bp
->dbg_type
== DBG_FUNC
&& strstr((char *)p
, "()") != NULL
))
488 EMSG2(_(e_invarg2
), arg
);
492 if (bp
->dbg_type
== DBG_FUNC
)
493 bp
->dbg_name
= vim_strsave(p
);
495 bp
->dbg_name
= vim_strsave(curbuf
->b_ffname
);
498 /* Expand the file name in the same way as do_source(). This means
499 * doing it twice, so that $DIR/file gets expanded when $DIR is
502 q
= mch_munge_fname(p
);
504 q
= expand_env_save(p
);
509 p
= mch_munge_fname(q
);
511 p
= expand_env_save(q
);
518 bp
->dbg_name
= fix_fname(p
);
525 if (bp
->dbg_name
== NULL
)
543 if (eap
->cmdidx
== CMD_profile
)
547 if (dbg_parsearg(eap
->arg
, gap
) == OK
)
549 bp
= &DEBUGGY(gap
, gap
->ga_len
);
550 bp
->dbg_forceit
= eap
->forceit
;
552 pat
= file_pat_to_reg_pat(bp
->dbg_name
, NULL
, NULL
, FALSE
);
555 bp
->dbg_prog
= vim_regcomp(pat
, RE_MAGIC
+ RE_STRING
);
558 if (pat
== NULL
|| bp
->dbg_prog
== NULL
)
559 vim_free(bp
->dbg_name
);
562 if (bp
->dbg_lnum
== 0) /* default line number is 1 */
565 if (eap
->cmdidx
!= CMD_profile
)
568 DEBUGGY(gap
, gap
->ga_len
).dbg_nr
= ++last_breakp
;
583 if (eap
->addr_count
== 0 || eap
->line2
!= 0)
586 debug_greedy
= FALSE
;
590 * ":breakdel" and ":profdel".
596 struct debuggy
*bp
, *bpi
;
601 linenr_T best_lnum
= 0;
606 if (eap
->cmdidx
== CMD_profdel
)
610 if (vim_isdigit(*eap
->arg
))
612 /* ":breakdel {nr}" */
613 nr
= atol((char *)eap
->arg
);
614 for (i
= 0; i
< gap
->ga_len
; ++i
)
615 if (DEBUGGY(gap
, i
).dbg_nr
== nr
)
621 else if (*eap
->arg
== '*')
628 /* ":breakdel {func|file} [lnum] {name}" */
629 if (dbg_parsearg(eap
->arg
, gap
) == FAIL
)
631 bp
= &DEBUGGY(gap
, gap
->ga_len
);
632 for (i
= 0; i
< gap
->ga_len
; ++i
)
634 bpi
= &DEBUGGY(gap
, i
);
635 if (bp
->dbg_type
== bpi
->dbg_type
636 && STRCMP(bp
->dbg_name
, bpi
->dbg_name
) == 0
637 && (bp
->dbg_lnum
== bpi
->dbg_lnum
638 || (bp
->dbg_lnum
== 0
640 || bpi
->dbg_lnum
< best_lnum
))))
643 best_lnum
= bpi
->dbg_lnum
;
646 vim_free(bp
->dbg_name
);
650 EMSG2(_("E161: Breakpoint not found: %s"), eap
->arg
);
653 while (gap
->ga_len
> 0)
655 vim_free(DEBUGGY(gap
, todel
).dbg_name
);
656 vim_free(DEBUGGY(gap
, todel
).dbg_prog
);
658 if (todel
< gap
->ga_len
)
659 mch_memmove(&DEBUGGY(gap
, todel
), &DEBUGGY(gap
, todel
+ 1),
660 (gap
->ga_len
- todel
) * sizeof(struct debuggy
));
662 if (eap
->cmdidx
== CMD_breakdel
)
669 /* If all breakpoints were removed clear the array. */
670 if (gap
->ga_len
== 0)
686 if (dbg_breakp
.ga_len
== 0)
687 MSG(_("No breakpoints defined"));
689 for (i
= 0; i
< dbg_breakp
.ga_len
; ++i
)
692 smsg((char_u
*)_("%3d %s %s line %ld"),
694 bp
->dbg_type
== DBG_FUNC
? "func" : "file",
701 * Find a breakpoint for a function or sourced file.
702 * Returns line number at which to break; zero when no matching breakpoint.
705 dbg_find_breakpoint(file
, fname
, after
)
706 int file
; /* TRUE for a file, FALSE for a function */
707 char_u
*fname
; /* file or function name */
708 linenr_T after
; /* after this line number */
710 return debuggy_find(file
, fname
, after
, &dbg_breakp
, NULL
);
713 #if defined(FEAT_PROFILE) || defined(PROTO)
715 * Return TRUE if profiling is on for a function or sourced file.
718 has_profiling(file
, fname
, fp
)
719 int file
; /* TRUE for a file, FALSE for a function */
720 char_u
*fname
; /* file or function name */
721 int *fp
; /* return: forceit */
723 return (debuggy_find(file
, fname
, (linenr_T
)0, &prof_ga
, fp
)
729 * Common code for dbg_find_breakpoint() and has_profiling().
732 debuggy_find(file
, fname
, after
, gap
, fp
)
733 int file
; /* TRUE for a file, FALSE for a function */
734 char_u
*fname
; /* file or function name */
735 linenr_T after
; /* after this line number */
736 garray_T
*gap
; /* either &dbg_breakp or &prof_ga */
737 int *fp
; /* if not NULL: return forceit */
743 char_u
*name
= fname
;
746 /* Return quickly when there are no breakpoints. */
747 if (gap
->ga_len
== 0)
750 /* Replace K_SNR in function name with "<SNR>". */
751 if (!file
&& fname
[0] == K_SPECIAL
)
753 name
= alloc((unsigned)STRLEN(fname
) + 3);
758 STRCPY(name
, "<SNR>");
759 STRCPY(name
+ 5, fname
+ 3);
763 for (i
= 0; i
< gap
->ga_len
; ++i
)
765 /* Skip entries that are not useful or are for a line that is beyond
766 * an already found breakpoint. */
767 bp
= &DEBUGGY(gap
, i
);
768 if (((bp
->dbg_type
== DBG_FILE
) == file
&& (
772 (bp
->dbg_lnum
> after
&& (lnum
== 0 || bp
->dbg_lnum
< lnum
)))))
774 regmatch
.regprog
= bp
->dbg_prog
;
775 regmatch
.rm_ic
= FALSE
;
777 * Save the value of got_int and reset it. We don't want a
778 * previous interruption cancel matching, only hitting CTRL-C
779 * while matching should abort it.
781 prev_got_int
= got_int
;
783 if (vim_regexec(®match
, name
, (colnr_T
)0))
787 *fp
= bp
->dbg_forceit
;
789 got_int
|= prev_got_int
;
799 * Called when a breakpoint was encountered.
802 dbg_breakpoint(name
, lnum
)
806 /* We need to check if this line is actually executed in do_one_cmd() */
807 debug_breakpoint_name
= name
;
808 debug_breakpoint_lnum
= lnum
;
812 # if defined(FEAT_PROFILE) || defined(FEAT_RELTIME) || defined(PROTO)
814 * Store the current time in "tm".
821 QueryPerformanceCounter(tm
);
823 gettimeofday(tm
, NULL
);
828 * Compute the elapsed time from "tm" till now and store in "tm".
837 QueryPerformanceCounter(&now
);
838 tm
->QuadPart
= now
.QuadPart
- tm
->QuadPart
;
840 gettimeofday(&now
, NULL
);
841 tm
->tv_usec
= now
.tv_usec
- tm
->tv_usec
;
842 tm
->tv_sec
= now
.tv_sec
- tm
->tv_sec
;
845 tm
->tv_usec
+= 1000000;
852 * Subtract the time "tm2" from "tm".
856 proftime_T
*tm
, *tm2
;
859 tm
->QuadPart
-= tm2
->QuadPart
;
861 tm
->tv_usec
-= tm2
->tv_usec
;
862 tm
->tv_sec
-= tm2
->tv_sec
;
865 tm
->tv_usec
+= 1000000;
872 * Return a string that represents the time in "tm".
873 * Uses a static buffer!
884 QueryPerformanceFrequency(&fr
);
885 sprintf(buf
, "%10.6lf", (double)tm
->QuadPart
/ (double)fr
.QuadPart
);
887 sprintf(buf
, "%3ld.%06ld", (long)tm
->tv_sec
, (long)tm
->tv_usec
);
892 # endif /* FEAT_PROFILE || FEAT_RELTIME */
894 # if defined(FEAT_PROFILE) || defined(PROTO)
896 * Functions for profiling.
898 static void script_do_profile
__ARGS((scriptitem_T
*si
));
899 static void script_dump_profile
__ARGS((FILE *fd
));
900 static proftime_T prof_wait_time
;
903 * Set the time in "tm" to zero.
918 * Add the time "tm2" to "tm".
922 proftime_T
*tm
, *tm2
;
925 tm
->QuadPart
+= tm2
->QuadPart
;
927 tm
->tv_usec
+= tm2
->tv_usec
;
928 tm
->tv_sec
+= tm2
->tv_sec
;
929 if (tm
->tv_usec
>= 1000000)
931 tm
->tv_usec
-= 1000000;
938 * Add the "self" time from the total time and the children's time.
941 profile_self(self
, total
, children
)
942 proftime_T
*self
, *total
, *children
;
944 /* Check that the result won't be negative. Can happen with recursive
947 if (total
->QuadPart
<= children
->QuadPart
)
950 if (total
->tv_sec
< children
->tv_sec
951 || (total
->tv_sec
== children
->tv_sec
952 && total
->tv_usec
<= children
->tv_usec
))
955 profile_add(self
, total
);
956 profile_sub(self
, children
);
960 * Get the current waittime.
966 *tm
= prof_wait_time
;
970 * Subtract the passed waittime since "tm" from "tma".
973 profile_sub_wait(tm
, tma
)
974 proftime_T
*tm
, *tma
;
976 proftime_T tm3
= prof_wait_time
;
978 profile_sub(&tm3
, tm
);
979 profile_sub(tma
, &tm3
);
983 * Return TRUE if "tm1" and "tm2" are equal.
986 profile_equal(tm1
, tm2
)
987 proftime_T
*tm1
, *tm2
;
990 return (tm1
->QuadPart
== tm2
->QuadPart
);
992 return (tm1
->tv_usec
== tm2
->tv_usec
&& tm1
->tv_sec
== tm2
->tv_sec
);
997 * Return <0, 0 or >0 if "tm1" < "tm2", "tm1" == "tm2" or "tm1" > "tm2"
1000 profile_cmp(tm1
, tm2
)
1001 proftime_T
*tm1
, *tm2
;
1004 return (int)(tm2
->QuadPart
- tm1
->QuadPart
);
1006 if (tm1
->tv_sec
== tm2
->tv_sec
)
1007 return tm2
->tv_usec
- tm1
->tv_usec
;
1008 return tm2
->tv_sec
- tm1
->tv_sec
;
1012 static char_u
*profile_fname
= NULL
;
1013 static proftime_T pause_time
;
1016 * ":profile cmd args"
1025 e
= skiptowhite(eap
->arg
);
1026 len
= (int)(e
- eap
->arg
);
1029 if (len
== 5 && STRNCMP(eap
->arg
, "start", 5) == 0 && *e
!= NUL
)
1031 vim_free(profile_fname
);
1032 profile_fname
= vim_strsave(e
);
1033 do_profiling
= PROF_YES
;
1034 profile_zero(&prof_wait_time
);
1035 set_vim_var_nr(VV_PROFILING
, 1L);
1037 else if (do_profiling
== PROF_NONE
)
1038 EMSG(_("E750: First use :profile start <fname>"));
1039 else if (STRCMP(eap
->arg
, "pause") == 0)
1041 if (do_profiling
== PROF_YES
)
1042 profile_start(&pause_time
);
1043 do_profiling
= PROF_PAUSED
;
1045 else if (STRCMP(eap
->arg
, "continue") == 0)
1047 if (do_profiling
== PROF_PAUSED
)
1049 profile_end(&pause_time
);
1050 profile_add(&prof_wait_time
, &pause_time
);
1052 do_profiling
= PROF_YES
;
1056 /* The rest is similar to ":breakadd". */
1062 * Dump the profiling info.
1069 if (profile_fname
!= NULL
)
1071 fd
= mch_fopen((char *)profile_fname
, "w");
1073 EMSG2(_(e_notopen
), profile_fname
);
1076 script_dump_profile(fd
);
1077 func_dump_profile(fd
);
1084 * Start profiling script "fp".
1087 script_do_profile(si
)
1090 si
->sn_pr_count
= 0;
1091 profile_zero(&si
->sn_pr_total
);
1092 profile_zero(&si
->sn_pr_self
);
1094 ga_init2(&si
->sn_prl_ga
, sizeof(sn_prl_T
), 100);
1095 si
->sn_prl_idx
= -1;
1096 si
->sn_prof_on
= TRUE
;
1101 * save time when starting to invoke another script or function.
1104 script_prof_save(tm
)
1105 proftime_T
*tm
; /* place to store wait time */
1109 if (current_SID
> 0 && current_SID
<= script_items
.ga_len
)
1111 si
= &SCRIPT_ITEM(current_SID
);
1112 if (si
->sn_prof_on
&& si
->sn_pr_nest
++ == 0)
1113 profile_start(&si
->sn_pr_child
);
1115 profile_get_wait(tm
);
1119 * Count time spent in children after invoking another script or function.
1122 script_prof_restore(tm
)
1127 if (current_SID
> 0 && current_SID
<= script_items
.ga_len
)
1129 si
= &SCRIPT_ITEM(current_SID
);
1130 if (si
->sn_prof_on
&& --si
->sn_pr_nest
== 0)
1132 profile_end(&si
->sn_pr_child
);
1133 profile_sub_wait(tm
, &si
->sn_pr_child
); /* don't count wait time */
1134 profile_add(&si
->sn_pr_children
, &si
->sn_pr_child
);
1135 profile_add(&si
->sn_prl_children
, &si
->sn_pr_child
);
1140 static proftime_T inchar_time
;
1143 * Called when starting to wait for the user to type a character.
1148 profile_start(&inchar_time
);
1152 * Called when finished waiting for the user to type a character.
1157 profile_end(&inchar_time
);
1158 profile_add(&prof_wait_time
, &inchar_time
);
1162 * Dump the profiling results for all scripts in file "fd".
1165 script_dump_profile(fd
)
1174 for (id
= 1; id
<= script_items
.ga_len
; ++id
)
1176 si
= &SCRIPT_ITEM(id
);
1179 fprintf(fd
, "SCRIPT %s\n", si
->sn_name
);
1180 if (si
->sn_pr_count
== 1)
1181 fprintf(fd
, "Sourced 1 time\n");
1183 fprintf(fd
, "Sourced %d times\n", si
->sn_pr_count
);
1184 fprintf(fd
, "Total time: %s\n", profile_msg(&si
->sn_pr_total
));
1185 fprintf(fd
, " Self time: %s\n", profile_msg(&si
->sn_pr_self
));
1187 fprintf(fd
, "count total (s) self (s)\n");
1189 sfd
= mch_fopen((char *)si
->sn_name
, "r");
1191 fprintf(fd
, "Cannot open file!\n");
1194 for (i
= 0; i
< si
->sn_prl_ga
.ga_len
; ++i
)
1196 if (vim_fgets(IObuff
, IOSIZE
, sfd
))
1198 pp
= &PRL_ITEM(si
, i
);
1199 if (pp
->snp_count
> 0)
1201 fprintf(fd
, "%5d ", pp
->snp_count
);
1202 if (profile_equal(&pp
->sn_prl_total
, &pp
->sn_prl_self
))
1205 fprintf(fd
, "%s ", profile_msg(&pp
->sn_prl_total
));
1206 fprintf(fd
, "%s ", profile_msg(&pp
->sn_prl_self
));
1210 fprintf(fd
, "%s", IObuff
);
1220 * Return TRUE when a function defined in the current script should be
1226 if (current_SID
> 0)
1227 return SCRIPT_ITEM(current_SID
).sn_pr_force
;
1235 * If 'autowrite' option set, try to write the file.
1236 * Careful: autocommands may make "buf" invalid!
1238 * return FAIL for failure, OK otherwise
1241 autowrite(buf
, forceit
)
1245 if (!(p_aw
|| p_awa
) || !p_write
1246 #ifdef FEAT_QUICKFIX
1247 /* never autowrite a "nofile" or "nowrite" buffer */
1248 || bt_dontwrite(buf
)
1250 || (!forceit
&& buf
->b_p_ro
) || buf
->b_ffname
== NULL
)
1252 return buf_write_all(buf
, forceit
);
1256 * flush all buffers, except the ones that are readonly
1263 if (!(p_aw
|| p_awa
) || !p_write
)
1265 for (buf
= firstbuf
; buf
; buf
= buf
->b_next
)
1266 if (bufIsChanged(buf
) && !buf
->b_p_ro
)
1268 (void)buf_write_all(buf
, FALSE
);
1270 /* an autocommand may have deleted the buffer */
1271 if (!buf_valid(buf
))
1278 * return TRUE if buffer was changed and cannot be abandoned.
1282 check_changed(buf
, checkaw
, mult_win
, forceit
, allbuf
)
1284 int checkaw
; /* do autowrite if buffer was changed */
1285 int mult_win
; /* check also when several wins for the buf */
1287 int allbuf
; /* may write all buffers */
1290 && bufIsChanged(buf
)
1291 && (mult_win
|| buf
->b_nwindows
<= 1)
1292 && (!checkaw
|| autowrite(buf
, forceit
) == FAIL
))
1294 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1295 if ((p_confirm
|| cmdmod
.confirm
) && p_write
)
1301 for (buf2
= firstbuf
; buf2
!= NULL
; buf2
= buf2
->b_next
)
1302 if (bufIsChanged(buf2
)
1303 && (buf2
->b_ffname
!= NULL
1309 # ifdef FEAT_AUTOCMD
1310 if (!buf_valid(buf
))
1311 /* Autocommand deleted buffer, oops! It's not changed now. */
1314 dialog_changed(buf
, count
> 1);
1315 # ifdef FEAT_AUTOCMD
1316 if (!buf_valid(buf
))
1317 /* Autocommand deleted buffer, oops! It's not changed now. */
1320 return bufIsChanged(buf
);
1323 EMSG(_(e_nowrtmsg
));
1329 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
1331 #if defined(FEAT_BROWSE) || defined(PROTO)
1333 * When wanting to write a file without a file name, ask the user for a name.
1336 browse_save_fname(buf
)
1339 if (buf
->b_fname
== NULL
)
1343 fname
= do_browse(BROWSE_SAVE
, (char_u
*)_("Save As"),
1344 NULL
, NULL
, NULL
, NULL
, buf
);
1347 if (setfname(buf
, fname
, NULL
, TRUE
) == OK
)
1348 buf
->b_flags
|= BF_NOTEDITED
;
1356 * Ask the user what to do when abondoning a changed buffer.
1357 * Must check 'write' option first!
1360 dialog_changed(buf
, checkall
)
1362 int checkall
; /* may abandon all changed buffers */
1364 char_u buff
[IOSIZE
];
1368 dialog_msg(buff
, _("Save changes to \"%s\"?"),
1369 (buf
->b_fname
!= NULL
) ?
1370 buf
->b_fname
: (char_u
*)_("Untitled"));
1372 ret
= vim_dialog_yesnoallcancel(VIM_QUESTION
, NULL
, buff
, 1);
1374 ret
= vim_dialog_yesnocancel(VIM_QUESTION
, NULL
, buff
, 1);
1379 /* May get file name, when there is none */
1380 browse_save_fname(buf
);
1382 if (buf
->b_fname
!= NULL
) /* didn't hit Cancel */
1383 (void)buf_write_all(buf
, FALSE
);
1385 else if (ret
== VIM_NO
)
1387 unchanged(buf
, TRUE
);
1389 else if (ret
== VIM_ALL
)
1392 * Write all modified files that can be written.
1393 * Skip readonly buffers, these need to be confirmed
1396 for (buf2
= firstbuf
; buf2
!= NULL
; buf2
= buf2
->b_next
)
1398 if (bufIsChanged(buf2
)
1399 && (buf2
->b_ffname
!= NULL
1407 /* May get file name, when there is none */
1408 browse_save_fname(buf2
);
1410 if (buf2
->b_fname
!= NULL
) /* didn't hit Cancel */
1411 (void)buf_write_all(buf2
, FALSE
);
1413 /* an autocommand may have deleted the buffer */
1414 if (!buf_valid(buf2
))
1420 else if (ret
== VIM_DISCARDALL
)
1423 * mark all buffers as unchanged
1425 for (buf2
= firstbuf
; buf2
!= NULL
; buf2
= buf2
->b_next
)
1426 unchanged(buf2
, TRUE
);
1432 * Return TRUE if the buffer "buf" can be abandoned, either by making it
1433 * hidden, autowriting it or unloading it.
1436 can_abandon(buf
, forceit
)
1441 || !bufIsChanged(buf
)
1442 || buf
->b_nwindows
> 1
1443 || autowrite(buf
, forceit
) == OK
1448 * Return TRUE if any buffer was changed and cannot be abandoned.
1449 * That changed buffer becomes the current buffer.
1452 check_changed_any(hidden
)
1453 int hidden
; /* Only check hidden buffers */
1463 /* check curbuf first: if it was changed we can't abandon it */
1464 if (!hidden
&& curbufIsChanged())
1468 for (buf
= firstbuf
; buf
!= NULL
; buf
= buf
->b_next
)
1469 if ((!hidden
|| buf
->b_nwindows
== 0) && bufIsChanged(buf
))
1472 if (buf
== NULL
) /* No buffers changed */
1475 if (check_changed(buf
, p_awa
, TRUE
, FALSE
, TRUE
) && buf_valid(buf
))
1476 break; /* didn't save - still changes */
1480 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1482 * When ":confirm" used, don't give an error message.
1484 if (!(p_confirm
|| cmdmod
.confirm
))
1487 /* There must be a wait_return for this message, do_buffer()
1488 * may cause a redraw. But wait_return() is a no-op when vgetc()
1489 * is busy (Quit used from window menu), then make sure we don't
1490 * cause a scroll up. */
1493 msg_row
= cmdline_row
;
1497 if (EMSG2(_("E162: No write since last change for buffer \"%s\""),
1498 buf_spname(buf
) != NULL
? (char_u
*)buf_spname(buf
) :
1501 save
= no_wait_return
;
1502 no_wait_return
= FALSE
;
1504 no_wait_return
= save
;
1509 /* Try to find a window that contains the buffer. */
1511 for (wp
= firstwin
; wp
!= NULL
; wp
= wp
->w_next
)
1512 if (wp
->w_buffer
== buf
)
1515 # ifdef FEAT_AUTOCMD
1516 /* Paranoia: did autocms wipe out the buffer with changes? */
1517 if (!buf_valid(buf
))
1524 /* Open the changed buffer in the current window. */
1526 set_curbuf(buf
, DOBUF_GOTO
);
1532 * return FAIL if there is no file name, OK if there is one
1533 * give error message for FAIL
1538 if (curbuf
->b_ffname
== NULL
)
1547 * flush the contents of a buffer, unless it has no file name
1549 * return FAIL for failure, OK otherwise
1552 buf_write_all(buf
, forceit
)
1558 buf_T
*old_curbuf
= curbuf
;
1561 retval
= (buf_write(buf
, buf
->b_ffname
, buf
->b_fname
,
1562 (linenr_T
)1, buf
->b_ml
.ml_line_count
, NULL
,
1563 FALSE
, forceit
, TRUE
, FALSE
));
1565 if (curbuf
!= old_curbuf
)
1567 msg_source(hl_attr(HLF_W
));
1568 MSG(_("Warning: Entered other buffer unexpectedly (check autocommands)"));
1575 * Code to handle the argument list.
1578 static char_u
*do_one_arg
__ARGS((char_u
*str
));
1579 static int do_arglist
__ARGS((char_u
*str
, int what
, int after
));
1580 static void alist_check_arg_idx
__ARGS((void));
1581 static int editing_arg_idx
__ARGS((win_T
*win
));
1582 #ifdef FEAT_LISTCMDS
1583 static int alist_add_list
__ARGS((int count
, char_u
**files
, int after
));
1590 * Isolate one argument, taking backticks.
1591 * Changes the argument in-place, puts a NUL after it. Backticks remain.
1592 * Return a pointer to the start of the next argument.
1602 for (p
= str
; *str
; ++str
)
1604 /* When the backslash is used for escaping the special meaning of a
1605 * character we need to keep it until wildcard expansion. */
1606 if (rem_backslash(str
))
1613 /* An item ends at a space not in backticks */
1614 if (!inbacktick
&& vim_isspace(*str
))
1621 str
= skipwhite(str
);
1628 * Separate the arguments in "str" and return a list of pointers in the
1632 get_arglist(gap
, str
)
1636 ga_init2(gap
, (int)sizeof(char_u
*), 20);
1639 if (ga_grow(gap
, 1) == FAIL
)
1644 ((char_u
**)gap
->ga_data
)[gap
->ga_len
++] = str
;
1646 /* Isolate one argument, change it in-place, put a NUL after it. */
1647 str
= do_one_arg(str
);
1652 #if defined(FEAT_QUICKFIX) || defined(FEAT_SYN_HL) || defined(PROTO)
1654 * Parse a list of arguments (file names), expand them and return in
1655 * "fnames[fcountp]".
1656 * Return FAIL or OK.
1659 get_arglist_exp(str
, fcountp
, fnamesp
)
1667 if (get_arglist(&ga
, str
) == FAIL
)
1669 i
= gen_expand_wildcards(ga
.ga_len
, (char_u
**)ga
.ga_data
,
1670 fcountp
, fnamesp
, EW_FILE
|EW_NOTFOUND
);
1676 #if defined(FEAT_GUI) || defined(FEAT_CLIENTSERVER) || defined(PROTO)
1678 * Redefine the argument list.
1684 do_arglist(str
, AL_SET
, 0);
1689 * "what" == AL_SET: Redefine the argument list to 'str'.
1690 * "what" == AL_ADD: add files in 'str' to the argument list after "after".
1691 * "what" == AL_DEL: remove files in 'str' from the argument list.
1693 * Return FAIL for failure, OK otherwise.
1697 do_arglist(str
, what
, after
)
1700 int after
; /* 0 means before first one */
1706 #ifdef FEAT_LISTCMDS
1712 * Collect all file name arguments in "new_ga".
1714 if (get_arglist(&new_ga
, str
) == FAIL
)
1717 #ifdef FEAT_LISTCMDS
1720 regmatch_T regmatch
;
1724 * Delete the items: use each item as a regexp and find a match in the
1727 #ifdef CASE_INSENSITIVE_FILENAME
1728 regmatch
.rm_ic
= TRUE
; /* Always ignore case */
1730 regmatch
.rm_ic
= FALSE
; /* Never ignore case */
1732 for (i
= 0; i
< new_ga
.ga_len
&& !got_int
; ++i
)
1734 p
= ((char_u
**)new_ga
.ga_data
)[i
];
1735 p
= file_pat_to_reg_pat(p
, NULL
, NULL
, FALSE
);
1738 regmatch
.regprog
= vim_regcomp(p
, p_magic
? RE_MAGIC
: 0);
1739 if (regmatch
.regprog
== NULL
)
1746 for (match
= 0; match
< ARGCOUNT
; ++match
)
1747 if (vim_regexec(®match
, alist_name(&ARGLIST
[match
]),
1751 vim_free(ARGLIST
[match
].ae_fname
);
1752 mch_memmove(ARGLIST
+ match
, ARGLIST
+ match
+ 1,
1753 (ARGCOUNT
- match
- 1) * sizeof(aentry_T
));
1754 --ALIST(curwin
)->al_ga
.ga_len
;
1755 if (curwin
->w_arg_idx
> match
)
1756 --curwin
->w_arg_idx
;
1760 vim_free(regmatch
.regprog
);
1763 EMSG2(_(e_nomatch2
), ((char_u
**)new_ga
.ga_data
)[i
]);
1770 i
= expand_wildcards(new_ga
.ga_len
, (char_u
**)new_ga
.ga_data
,
1771 &exp_count
, &exp_files
, EW_DIR
|EW_FILE
|EW_ADDSLASH
|EW_NOTFOUND
);
1781 #ifdef FEAT_LISTCMDS
1784 (void)alist_add_list(exp_count
, exp_files
, after
);
1785 vim_free(exp_files
);
1787 else /* what == AL_SET */
1789 alist_set(ALIST(curwin
), exp_count
, exp_files
, FALSE
, NULL
, 0);
1792 alist_check_arg_idx();
1798 * Check the validity of the arg_idx for each other window.
1801 alist_check_arg_idx()
1807 FOR_ALL_TAB_WINDOWS(tp
, win
)
1808 if (win
->w_alist
== curwin
->w_alist
)
1811 check_arg_idx(curwin
);
1816 * Return TRUE if window "win" is editing then file at the current argument
1820 editing_arg_idx(win
)
1823 return !(win
->w_arg_idx
>= WARGCOUNT(win
)
1824 || (win
->w_buffer
->b_fnum
1825 != WARGLIST(win
)[win
->w_arg_idx
].ae_fnum
1826 && (win
->w_buffer
->b_ffname
== NULL
1828 alist_name(&WARGLIST(win
)[win
->w_arg_idx
]),
1829 win
->w_buffer
->b_ffname
, TRUE
) & FPC_SAME
))));
1833 * Check if window "win" is editing the w_arg_idx file in its argument list.
1839 if (WARGCOUNT(win
) > 1 && !editing_arg_idx(win
))
1841 /* We are not editing the current entry in the argument list.
1842 * Set "arg_had_last" if we are editing the last one. */
1843 win
->w_arg_idx_invalid
= TRUE
;
1844 if (win
->w_arg_idx
!= WARGCOUNT(win
) - 1
1845 && arg_had_last
== FALSE
1847 && ALIST(win
) == &global_alist
1850 && win
->w_arg_idx
< GARGCOUNT
1851 && (win
->w_buffer
->b_fnum
== GARGLIST
[GARGCOUNT
- 1].ae_fnum
1852 || (win
->w_buffer
->b_ffname
!= NULL
1853 && (fullpathcmp(alist_name(&GARGLIST
[GARGCOUNT
- 1]),
1854 win
->w_buffer
->b_ffname
, TRUE
) & FPC_SAME
))))
1855 arg_had_last
= TRUE
;
1859 /* We are editing the current entry in the argument list.
1860 * Set "arg_had_last" if it's also the last one */
1861 win
->w_arg_idx_invalid
= FALSE
;
1862 if (win
->w_arg_idx
== WARGCOUNT(win
) - 1
1864 && win
->w_alist
== &global_alist
1867 arg_had_last
= TRUE
;
1872 * ":args", ":argslocal" and ":argsglobal".
1880 if (eap
->cmdidx
!= CMD_args
)
1882 #if defined(FEAT_WINDOWS) && defined(FEAT_LISTCMDS)
1883 alist_unlink(ALIST(curwin
));
1884 if (eap
->cmdidx
== CMD_argglobal
)
1885 ALIST(curwin
) = &global_alist
;
1886 else /* eap->cmdidx == CMD_arglocal */
1894 if (!ends_excmd(*eap
->arg
))
1897 * ":args file ..": define new argument list, handle like ":next"
1898 * Also for ":argslocal file .." and ":argsglobal file ..".
1903 #if defined(FEAT_WINDOWS) && defined(FEAT_LISTCMDS)
1904 if (eap
->cmdidx
== CMD_args
)
1908 * ":args": list arguments.
1912 /* Overwrite the command, for a short list there is no scrolling
1913 * required and no wait_return(). */
1915 for (i
= 0; i
< ARGCOUNT
; ++i
)
1917 if (i
== curwin
->w_arg_idx
)
1919 msg_outtrans(alist_name(&ARGLIST
[i
]));
1920 if (i
== curwin
->w_arg_idx
)
1926 #if defined(FEAT_WINDOWS) && defined(FEAT_LISTCMDS)
1927 else if (eap
->cmdidx
== CMD_arglocal
)
1929 garray_T
*gap
= &curwin
->w_alist
->al_ga
;
1932 * ":argslocal": make a local copy of the global argument list.
1934 if (ga_grow(gap
, GARGCOUNT
) == OK
)
1935 for (i
= 0; i
< GARGCOUNT
; ++i
)
1936 if (GARGLIST
[i
].ae_fname
!= NULL
)
1938 AARGLIST(curwin
->w_alist
)[gap
->ga_len
].ae_fname
=
1939 vim_strsave(GARGLIST
[i
].ae_fname
);
1940 AARGLIST(curwin
->w_alist
)[gap
->ga_len
].ae_fnum
=
1941 GARGLIST
[i
].ae_fnum
;
1949 * ":previous", ":sprevious", ":Next" and ":sNext".
1955 /* If past the last one already, go to the last one. */
1956 if (curwin
->w_arg_idx
- (int)eap
->line2
>= ARGCOUNT
)
1957 do_argfile(eap
, ARGCOUNT
- 1);
1959 do_argfile(eap
, curwin
->w_arg_idx
- (int)eap
->line2
);
1963 * ":rewind", ":first", ":sfirst" and ":srewind".
1973 * ":last" and ":slast".
1979 do_argfile(eap
, ARGCOUNT
- 1);
1983 * ":argument" and ":sargument".
1991 if (eap
->addr_count
> 0)
1994 i
= curwin
->w_arg_idx
;
1999 * Edit file "argn" of the argument lists.
2002 do_argfile(eap
, argn
)
2008 int old_arg_idx
= curwin
->w_arg_idx
;
2010 if (argn
< 0 || argn
>= ARGCOUNT
)
2013 EMSG(_("E163: There is only one file to edit"));
2015 EMSG(_("E164: Cannot go before first file"));
2017 EMSG(_("E165: Cannot go beyond last file"));
2023 need_mouse_correct
= TRUE
;
2027 /* split window or create new tab page first */
2028 if (*eap
->cmd
== 's' || cmdmod
.tab
!= 0)
2030 if (win_split(0, 0) == FAIL
)
2032 # ifdef FEAT_SCROLLBIND
2033 curwin
->w_p_scb
= FALSE
;
2040 * if 'hidden' set, only check for changed file when re-editing
2046 p
= fix_fname(alist_name(&ARGLIST
[argn
]));
2047 other
= otherfile(p
);
2050 if ((!P_HID(curbuf
) || !other
)
2051 && check_changed(curbuf
, TRUE
, !other
, eap
->forceit
, FALSE
))
2055 curwin
->w_arg_idx
= argn
;
2056 if (argn
== ARGCOUNT
- 1
2058 && curwin
->w_alist
== &global_alist
2061 arg_had_last
= TRUE
;
2063 /* Edit the file; always use the last known line number.
2064 * When it fails (e.g. Abort for already edited file) restore the
2065 * argument index. */
2066 if (do_ecmd(0, alist_name(&ARGLIST
[curwin
->w_arg_idx
]), NULL
,
2068 (P_HID(curwin
->w_buffer
) ? ECMD_HIDE
: 0) +
2069 (eap
->forceit
? ECMD_FORCEIT
: 0)) == FAIL
)
2070 curwin
->w_arg_idx
= old_arg_idx
;
2071 /* like Vi: set the mark where the cursor is in the file. */
2072 else if (eap
->cmdidx
!= CMD_argdo
)
2078 * ":next", and commands that behave like it.
2087 * check for changed buffer now, if this fails the argument list is not
2091 || eap
->cmdidx
== CMD_snext
2092 || !check_changed(curbuf
, TRUE
, FALSE
, eap
->forceit
, FALSE
))
2094 if (*eap
->arg
!= NUL
) /* redefine file list */
2096 if (do_arglist(eap
->arg
, AL_SET
, 0) == FAIL
)
2101 i
= curwin
->w_arg_idx
+ (int)eap
->line2
;
2106 #ifdef FEAT_LISTCMDS
2118 /* Add the argument to the buffer list and get the buffer number. */
2119 fnum
= buflist_add(eap
->arg
, BLN_LISTED
);
2121 /* Check if this argument is already in the argument list. */
2122 for (i
= 0; i
< ARGCOUNT
; ++i
)
2123 if (ARGLIST
[i
].ae_fnum
== fnum
)
2127 /* Can't find it, add it to the argument list. */
2128 s
= vim_strsave(eap
->arg
);
2131 i
= alist_add_list(1, &s
,
2132 eap
->addr_count
> 0 ? (int)eap
->line2
: curwin
->w_arg_idx
+ 1);
2135 curwin
->w_arg_idx
= i
;
2138 alist_check_arg_idx();
2140 /* Edit the argument. */
2151 do_arglist(eap
->arg
, AL_ADD
,
2152 eap
->addr_count
> 0 ? (int)eap
->line2
: curwin
->w_arg_idx
+ 1);
2168 if (eap
->addr_count
> 0)
2170 /* ":1,4argdel": Delete all arguments in the range. */
2171 if (eap
->line2
> ARGCOUNT
)
2172 eap
->line2
= ARGCOUNT
;
2173 n
= eap
->line2
- eap
->line1
+ 1;
2174 if (*eap
->arg
!= NUL
|| n
<= 0)
2178 for (i
= eap
->line1
; i
<= eap
->line2
; ++i
)
2179 vim_free(ARGLIST
[i
- 1].ae_fname
);
2180 mch_memmove(ARGLIST
+ eap
->line1
- 1, ARGLIST
+ eap
->line2
,
2181 (size_t)((ARGCOUNT
- eap
->line2
) * sizeof(aentry_T
)));
2182 ALIST(curwin
)->al_ga
.ga_len
-= n
;
2183 if (curwin
->w_arg_idx
>= eap
->line2
)
2184 curwin
->w_arg_idx
-= n
;
2185 else if (curwin
->w_arg_idx
> eap
->line1
)
2186 curwin
->w_arg_idx
= eap
->line1
;
2189 else if (*eap
->arg
== NUL
)
2192 do_arglist(eap
->arg
, AL_DEL
, 0);
2199 * ":argdo", ":windo", ":bufdo", ":tabdo"
2212 #if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
2213 char_u
*save_ei
= NULL
;
2217 #ifndef FEAT_WINDOWS
2218 if (eap
->cmdidx
== CMD_windo
)
2225 #if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
2226 if (eap
->cmdidx
!= CMD_windo
&& eap
->cmdidx
!= CMD_tabdo
)
2227 /* Don't do syntax HL autocommands. Skipping the syntax file is a
2228 * great speed improvement. */
2229 save_ei
= au_event_disable(",Syntax");
2232 if (eap
->cmdidx
== CMD_windo
2233 || eap
->cmdidx
== CMD_tabdo
2235 || !check_changed(curbuf
, TRUE
, FALSE
, eap
->forceit
, FALSE
))
2237 /* start at the first argument/window/buffer */
2243 /* set pcmark now */
2244 if (eap
->cmdidx
== CMD_bufdo
)
2245 goto_buffer(eap
, DOBUF_FIRST
, FORWARD
, 0);
2248 listcmd_busy
= TRUE
; /* avoids setting pcmark below */
2252 if (eap
->cmdidx
== CMD_argdo
)
2254 /* go to argument "i" */
2257 /* Don't call do_argfile() when already there, it will try
2258 * reloading the file. */
2259 if (curwin
->w_arg_idx
!= i
|| !editing_arg_idx(curwin
))
2261 /* Clear 'shm' to avoid that the file message overwrites
2262 * any output from the command. */
2263 p_shm_save
= vim_strsave(p_shm
);
2264 set_option_value((char_u
*)"shm", 0L, (char_u
*)"", 0);
2266 set_option_value((char_u
*)"shm", 0L, p_shm_save
, 0);
2267 vim_free(p_shm_save
);
2269 if (curwin
->w_arg_idx
!= i
)
2274 else if (eap
->cmdidx
== CMD_windo
)
2276 /* go to window "wp" */
2280 wp
= curwin
->w_next
;
2282 else if (eap
->cmdidx
== CMD_tabdo
)
2284 /* go to window "tp" */
2285 if (!valid_tabpage(tp
))
2287 goto_tabpage_tp(tp
);
2291 else if (eap
->cmdidx
== CMD_bufdo
)
2293 /* Remember the number of the next listed buffer, in case
2294 * ":bwipe" is used or autocommands do something strange. */
2296 for (buf
= curbuf
->b_next
; buf
!= NULL
; buf
= buf
->b_next
)
2299 next_fnum
= buf
->b_fnum
;
2304 /* execute the command */
2305 do_cmdline(eap
->arg
, eap
->getline
, eap
->cookie
,
2306 DOCMD_VERBOSE
+ DOCMD_NOWAIT
);
2308 if (eap
->cmdidx
== CMD_bufdo
)
2313 /* Check if the buffer still exists. */
2314 for (buf
= firstbuf
; buf
!= NULL
; buf
= buf
->b_next
)
2315 if (buf
->b_fnum
== next_fnum
)
2320 /* Go to the next buffer. Clear 'shm' to avoid that the file
2321 * message overwrites any output from the command. */
2322 p_shm_save
= vim_strsave(p_shm
);
2323 set_option_value((char_u
*)"shm", 0L, (char_u
*)"", 0);
2324 goto_buffer(eap
, DOBUF_FIRST
, FORWARD
, next_fnum
);
2325 set_option_value((char_u
*)"shm", 0L, p_shm_save
, 0);
2326 vim_free(p_shm_save
);
2328 /* If autocommands took us elsewhere, quit here */
2329 if (curbuf
->b_fnum
!= next_fnum
)
2333 if (eap
->cmdidx
== CMD_windo
)
2335 validate_cursor(); /* cursor may have moved */
2336 #ifdef FEAT_SCROLLBIND
2337 /* required when 'scrollbind' has been set */
2338 if (curwin
->w_p_scb
)
2339 do_check_scrollbind(TRUE
);
2343 listcmd_busy
= FALSE
;
2346 #if defined(FEAT_AUTOCMD) && defined(FEAT_SYN_HL)
2347 if (save_ei
!= NULL
)
2349 au_event_restore(save_ei
);
2350 apply_autocmds(EVENT_SYNTAX
, curbuf
->b_p_syn
,
2351 curbuf
->b_fname
, TRUE
, curbuf
);
2357 * Add files[count] to the arglist of the current window after arg "after".
2358 * The file names in files[count] must have been allocated and are taken over.
2359 * Files[] itself is not taken over.
2360 * Returns index of first added argument. Returns -1 when failed (out of mem).
2363 alist_add_list(count
, files
, after
)
2366 int after
; /* where to add: 0 = before first one */
2370 if (ga_grow(&ALIST(curwin
)->al_ga
, count
) == OK
)
2374 if (after
> ARGCOUNT
)
2376 if (after
< ARGCOUNT
)
2377 mch_memmove(&(ARGLIST
[after
+ count
]), &(ARGLIST
[after
]),
2378 (ARGCOUNT
- after
) * sizeof(aentry_T
));
2379 for (i
= 0; i
< count
; ++i
)
2381 ARGLIST
[after
+ i
].ae_fname
= files
[i
];
2382 ARGLIST
[after
+ i
].ae_fnum
= buflist_add(files
[i
], BLN_LISTED
);
2384 ALIST(curwin
)->al_ga
.ga_len
+= count
;
2385 if (curwin
->w_arg_idx
>= after
)
2386 ++curwin
->w_arg_idx
;
2390 for (i
= 0; i
< count
; ++i
)
2395 #endif /* FEAT_LISTCMDS */
2399 * ":compiler[!] {name}"
2406 char_u
*old_cur_comp
= NULL
;
2409 if (*eap
->arg
== NUL
)
2411 /* List all compiler scripts. */
2412 do_cmdline_cmd((char_u
*)"echo globpath(&rtp, 'compiler/*.vim')");
2413 /* ) keep the indenter happy... */
2417 buf
= alloc((unsigned)(STRLEN(eap
->arg
) + 14));
2422 /* ":compiler! {name}" sets global options */
2423 do_cmdline_cmd((char_u
*)
2424 "command -nargs=* CompilerSet set <args>");
2428 /* ":compiler! {name}" sets local options.
2429 * To remain backwards compatible "current_compiler" is always
2430 * used. A user's compiler plugin may set it, the distributed
2431 * plugin will then skip the settings. Afterwards set
2432 * "b:current_compiler" and restore "current_compiler". */
2433 old_cur_comp
= get_var_value((char_u
*)"current_compiler");
2434 if (old_cur_comp
!= NULL
)
2435 old_cur_comp
= vim_strsave(old_cur_comp
);
2436 do_cmdline_cmd((char_u
*)
2437 "command -nargs=* CompilerSet setlocal <args>");
2439 do_unlet((char_u
*)"current_compiler", TRUE
);
2440 do_unlet((char_u
*)"b:current_compiler", TRUE
);
2442 sprintf((char *)buf
, "compiler/%s.vim", eap
->arg
);
2443 if (source_runtime(buf
, TRUE
) == FAIL
)
2444 EMSG2(_("E666: compiler not supported: %s"), eap
->arg
);
2447 do_cmdline_cmd((char_u
*)":delcommand CompilerSet");
2449 /* Set "b:current_compiler" from "current_compiler". */
2450 p
= get_var_value((char_u
*)"current_compiler");
2452 set_internal_string_var((char_u
*)"b:current_compiler", p
);
2454 /* Restore "current_compiler" for ":compiler {name}". */
2457 if (old_cur_comp
!= NULL
)
2459 set_internal_string_var((char_u
*)"current_compiler",
2461 vim_free(old_cur_comp
);
2464 do_unlet((char_u
*)"current_compiler", TRUE
);
2478 source_runtime(eap
->arg
, eap
->forceit
);
2481 static void source_callback
__ARGS((char_u
*fname
, void *cookie
));
2485 source_callback(fname
, cookie
)
2489 (void)do_source(fname
, FALSE
, DOSO_NONE
);
2493 * Source the file "name" from all directories in 'runtimepath'.
2494 * "name" can contain wildcards.
2495 * When "all" is TRUE, source all files, otherwise only the first one.
2496 * return FAIL when no file could be sourced, OK otherwise.
2499 source_runtime(name
, all
)
2503 return do_in_runtimepath(name
, all
, source_callback
, NULL
);
2507 * Find "name" in 'runtimepath'. When found, invoke the callback function for
2508 * it: callback(fname, "cookie")
2509 * When "all" is TRUE repeat for all matches, otherwise only the first one is
2511 * Returns OK when at least one match found, FAIL otherwise.
2514 do_in_runtimepath(name
, all
, callback
, cookie
)
2517 void (*callback
)__ARGS((char_u
*fname
, void *ck
));
2528 int did_one
= FALSE
;
2530 struct Process
*proc
= (struct Process
*)FindTask(0L);
2531 APTR save_winptr
= proc
->pr_WindowPtr
;
2533 /* Avoid a requester here for a volume that doesn't exist. */
2534 proc
->pr_WindowPtr
= (APTR
)-1L;
2537 /* Make a copy of 'runtimepath'. Invoking the callback may change the
2539 rtp_copy
= vim_strsave(p_rtp
);
2540 buf
= alloc(MAXPATHL
);
2541 if (buf
!= NULL
&& rtp_copy
!= NULL
)
2546 smsg((char_u
*)_("Searching for \"%s\" in \"%s\""),
2547 (char *)name
, (char *)p_rtp
);
2551 /* Loop over all entries in 'runtimepath'. */
2553 while (*rtp
!= NUL
&& (all
|| !did_one
))
2555 /* Copy the path from 'runtimepath' to buf[]. */
2556 copy_option_part(&rtp
, buf
, MAXPATHL
, ",");
2557 if (STRLEN(buf
) + STRLEN(name
) + 2 < MAXPATHL
)
2560 tail
= buf
+ STRLEN(buf
);
2562 /* Loop over all patterns in "name" */
2564 while (*np
!= NUL
&& (all
|| !did_one
))
2566 /* Append the pattern from "name" to buf[]. */
2567 copy_option_part(&np
, tail
, (int)(MAXPATHL
- (tail
- buf
)),
2573 smsg((char_u
*)_("Searching for \"%s\""), buf
);
2577 /* Expand wildcards, invoke the callback for each match. */
2578 if (gen_expand_wildcards(1, &buf
, &num_files
, &files
,
2581 for (i
= 0; i
< num_files
; ++i
)
2583 (*callback
)(files
[i
], cookie
);
2588 FreeWild(num_files
, files
);
2596 if (p_verbose
> 0 && !did_one
)
2599 smsg((char_u
*)_("not found in 'runtimepath': \"%s\""), name
);
2604 proc
->pr_WindowPtr
= save_winptr
;
2607 return did_one
? OK
: FAIL
;
2610 #if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD)
2619 cmd_source((char_u
*)SYS_OPTWIN_FILE
, NULL
);
2633 char_u
*fname
= NULL
;
2635 fname
= do_browse(0, (char_u
*)_("Source Vim script"), eap
->arg
,
2636 NULL
, NULL
, BROWSE_FILTER_MACROS
, NULL
);
2639 cmd_source(fname
, eap
);
2645 cmd_source(eap
->arg
, eap
);
2649 cmd_source(fname
, eap
)
2656 else if (eap
!= NULL
&& eap
->forceit
)
2657 /* ":source!": read Normal mdoe commands
2658 * Need to execute the commands directly. This is required at least
2660 * - ":g" command busy
2661 * - after ":argdo", ":windo" or ":bufdo"
2662 * - another command follows
2665 openscript(fname
, global_busy
|| listcmd_busy
|| eap
->nextcmd
!= NULL
2667 || eap
->cstack
->cs_idx
>= 0
2671 /* ":source" read ex commands */
2672 else if (do_source(fname
, FALSE
, DOSO_NONE
) == FAIL
)
2673 EMSG2(_(e_notopen
), fname
);
2677 * ":source" and associated commands.
2680 * Structure used to store info for each sourced file.
2681 * It is shared between do_source() and getsourceline().
2682 * This is required, because it needs to be handed to do_cmdline() and
2683 * sourcing can be done recursively.
2685 struct source_cookie
2687 FILE *fp
; /* opened file for sourcing */
2688 char_u
*nextline
; /* if not NULL: line that was read ahead */
2689 int finished
; /* ":finish" used */
2690 #if defined (USE_CRNL) || defined (USE_CR)
2691 int fileformat
; /* EOL_UNKNOWN, EOL_UNIX or EOL_DOS */
2692 int error
; /* TRUE if LF found after CR-LF */
2695 linenr_T breakpoint
; /* next line with breakpoint or zero */
2696 char_u
*fname
; /* name of sourced file */
2697 int dbg_tick
; /* debug_tick when breakpoint was set */
2698 int level
; /* top nesting level of sourced file */
2701 vimconv_T conv
; /* type of conversion */
2707 * Return the address holding the next breakpoint line for a source cookie.
2710 source_breakpoint(cookie
)
2713 return &((struct source_cookie
*)cookie
)->breakpoint
;
2717 * Return the address holding the debug tick for a source cookie.
2720 source_dbg_tick(cookie
)
2723 return &((struct source_cookie
*)cookie
)->dbg_tick
;
2727 * Return the nesting level for a source cookie.
2730 source_level(cookie
)
2733 return ((struct source_cookie
*)cookie
)->level
;
2737 static char_u
*get_one_sourceline
__ARGS((struct source_cookie
*sp
));
2739 #if defined(WIN32) && defined(FEAT_CSCOPE)
2740 static FILE *fopen_noinh_readbin
__ARGS((char *filename
));
2743 * Special function to open a file without handle inheritance.
2746 fopen_noinh_readbin(filename
)
2749 int fd_tmp
= mch_open(filename
, O_RDONLY
| O_BINARY
| O_NOINHERIT
, 0);
2753 return fdopen(fd_tmp
, READBIN
);
2759 * do_source: Read the file "fname" and execute its lines as EX commands.
2761 * This function may be called recursively!
2763 * return FAIL if file could not be opened, OK otherwise
2766 do_source(fname
, check_other
, is_vimrc
)
2768 int check_other
; /* check for .vimrc and _vimrc */
2769 int is_vimrc
; /* DOSO_ value */
2771 struct source_cookie cookie
;
2772 char_u
*save_sourcing_name
;
2773 linenr_T save_sourcing_lnum
;
2778 scid_T save_current_SID
;
2779 static scid_T last_current_SID
= 0;
2780 void *save_funccalp
;
2781 int save_debug_break_level
= debug_break_level
;
2782 scriptitem_T
*si
= NULL
;
2789 struct timeval tv_rel
;
2790 struct timeval tv_start
;
2793 proftime_T wait_start
;
2797 p
= mch_munge_fname(fname
);
2799 p
= expand_env_save(fname
);
2803 fname_exp
= fix_fname(p
);
2805 if (fname_exp
== NULL
)
2807 if (mch_isdir(fname_exp
))
2809 smsg((char_u
*)_("Cannot source a directory: \"%s\""), fname
);
2814 apply_autocmds(EVENT_SOURCEPRE
, fname_exp
, fname_exp
, FALSE
, curbuf
);
2817 #if defined(WIN32) && defined(FEAT_CSCOPE)
2818 cookie
.fp
= fopen_noinh_readbin((char *)fname_exp
);
2820 cookie
.fp
= mch_fopen((char *)fname_exp
, READBIN
);
2822 if (cookie
.fp
== NULL
&& check_other
)
2825 * Try again, replacing file name ".vimrc" by "_vimrc" or vice versa,
2826 * and ".exrc" by "_exrc" or vice versa.
2828 p
= gettail(fname_exp
);
2829 if ((*p
== '.' || *p
== '_')
2830 && (STRICMP(p
+ 1, "vimrc") == 0
2831 || STRICMP(p
+ 1, "gvimrc") == 0
2832 || STRICMP(p
+ 1, "exrc") == 0))
2838 #if defined(WIN32) && defined(FEAT_CSCOPE)
2839 cookie
.fp
= fopen_noinh_readbin((char *)fname_exp
);
2841 cookie
.fp
= mch_fopen((char *)fname_exp
, READBIN
);
2846 if (cookie
.fp
== NULL
)
2851 if (sourcing_name
== NULL
)
2852 smsg((char_u
*)_("could not source \"%s\""), fname
);
2854 smsg((char_u
*)_("line %ld: could not source \"%s\""),
2855 sourcing_lnum
, fname
);
2863 * - In verbose mode, give a message.
2864 * - For a vimrc file, may want to set 'compatible', call vimrc_found().
2869 if (sourcing_name
== NULL
)
2870 smsg((char_u
*)_("sourcing \"%s\""), fname
);
2872 smsg((char_u
*)_("line %ld: sourcing \"%s\""),
2873 sourcing_lnum
, fname
);
2876 if (is_vimrc
== DOSO_VIMRC
)
2877 vimrc_found(fname_exp
, (char_u
*)"MYVIMRC");
2878 else if (is_vimrc
== DOSO_GVIMRC
)
2879 vimrc_found(fname_exp
, (char_u
*)"MYGVIMRC");
2882 /* If no automatic file format: Set default to CR-NL. */
2884 cookie
.fileformat
= EOL_DOS
;
2886 cookie
.fileformat
= EOL_UNKNOWN
;
2887 cookie
.error
= FALSE
;
2891 /* If no automatic file format: Set default to CR. */
2893 cookie
.fileformat
= EOL_MAC
;
2895 cookie
.fileformat
= EOL_UNKNOWN
;
2896 cookie
.error
= FALSE
;
2899 cookie
.nextline
= NULL
;
2900 cookie
.finished
= FALSE
;
2904 * Check if this script has a breakpoint.
2906 cookie
.breakpoint
= dbg_find_breakpoint(TRUE
, fname_exp
, (linenr_T
)0);
2907 cookie
.fname
= fname_exp
;
2908 cookie
.dbg_tick
= debug_tick
;
2910 cookie
.level
= ex_nesting_level
;
2913 cookie
.conv
.vc_type
= CONV_NONE
; /* no conversion */
2915 /* Try reading the first few bytes to check for a UTF-8 BOM. */
2919 if (fread((char *)buf
, sizeof(char_u
), (size_t)3, cookie
.fp
)
2921 && buf
[0] == 0xef && buf
[1] == 0xbb && buf
[2] == 0xbf)
2922 /* Found BOM, setup conversion and skip over it. */
2923 convert_setup(&cookie
.conv
, (char_u
*)"utf-8", p_enc
);
2925 /* No BOM found, rewind. */
2926 fseek(cookie
.fp
, 0L, SEEK_SET
);
2931 * Keep the sourcing name/lnum, for recursive calls.
2933 save_sourcing_name
= sourcing_name
;
2934 sourcing_name
= fname_exp
;
2935 save_sourcing_lnum
= sourcing_lnum
;
2939 time_push(&tv_rel
, &tv_start
);
2943 # ifdef FEAT_PROFILE
2944 if (do_profiling
== PROF_YES
)
2945 prof_child_enter(&wait_start
); /* entering a child now */
2948 /* Don't use local function variables, if called from a function.
2949 * Also starts profiling timer for nested script. */
2950 save_funccalp
= save_funccal();
2953 * Check if this script was sourced before to finds its SID.
2954 * If it's new, generate a new SID.
2956 save_current_SID
= current_SID
;
2958 stat_ok
= (mch_stat((char *)fname_exp
, &st
) >= 0);
2960 for (current_SID
= script_items
.ga_len
; current_SID
> 0; --current_SID
)
2962 si
= &SCRIPT_ITEM(current_SID
);
2963 if (si
->sn_name
!= NULL
2966 /* Compare dev/ino when possible, it catches symbolic
2967 * links. Also compare file names, the inode may change
2968 * when the file was edited. */
2969 ((stat_ok
&& si
->sn_dev
!= -1)
2970 && (si
->sn_dev
== st
.st_dev
2971 && si
->sn_ino
== st
.st_ino
)) ||
2973 fnamecmp(si
->sn_name
, fname_exp
) == 0))
2976 if (current_SID
== 0)
2978 current_SID
= ++last_current_SID
;
2979 if (ga_grow(&script_items
, (int)(current_SID
- script_items
.ga_len
))
2982 while (script_items
.ga_len
< current_SID
)
2984 ++script_items
.ga_len
;
2985 SCRIPT_ITEM(script_items
.ga_len
).sn_name
= NULL
;
2986 # ifdef FEAT_PROFILE
2987 SCRIPT_ITEM(script_items
.ga_len
).sn_prof_on
= FALSE
;
2990 si
= &SCRIPT_ITEM(current_SID
);
2991 si
->sn_name
= fname_exp
;
2996 si
->sn_dev
= st
.st_dev
;
2997 si
->sn_ino
= st
.st_ino
;
3003 /* Allocate the local script variables to use for this script. */
3004 new_script_vars(current_SID
);
3007 # ifdef FEAT_PROFILE
3008 if (do_profiling
== PROF_YES
)
3012 /* Check if we do profiling for this script. */
3013 if (!si
->sn_prof_on
&& has_profiling(TRUE
, si
->sn_name
, &forceit
))
3015 script_do_profile(si
);
3016 si
->sn_pr_force
= forceit
;
3021 profile_start(&si
->sn_pr_start
);
3022 profile_zero(&si
->sn_pr_children
);
3029 * Call do_cmdline, which will call getsourceline() to get the lines.
3031 do_cmdline(NULL
, getsourceline
, (void *)&cookie
,
3032 DOCMD_VERBOSE
|DOCMD_NOWAIT
|DOCMD_REPEAT
);
3037 if (do_profiling
== PROF_YES
)
3039 /* Get "si" again, "script_items" may have been reallocated. */
3040 si
= &SCRIPT_ITEM(current_SID
);
3043 profile_end(&si
->sn_pr_start
);
3044 profile_sub_wait(&wait_start
, &si
->sn_pr_start
);
3045 profile_add(&si
->sn_pr_total
, &si
->sn_pr_start
);
3046 profile_self(&si
->sn_pr_self
, &si
->sn_pr_start
,
3047 &si
->sn_pr_children
);
3054 sourcing_name
= save_sourcing_name
;
3055 sourcing_lnum
= save_sourcing_lnum
;
3059 smsg((char_u
*)_("finished sourcing %s"), fname
);
3060 if (sourcing_name
!= NULL
)
3061 smsg((char_u
*)_("continuing in %s"), sourcing_name
);
3065 vim_snprintf(IObuff
, IOSIZE
, "sourcing %s", fname
);
3066 time_msg(IObuff
, &tv_start
);
3072 * After a "finish" in debug mode, need to break at first command of next
3075 if (save_debug_break_level
> ex_nesting_level
3076 && debug_break_level
== ex_nesting_level
)
3077 ++debug_break_level
;
3082 current_SID
= save_current_SID
;
3083 restore_funccal(save_funccalp
);
3084 # ifdef FEAT_PROFILE
3085 if (do_profiling
== PROF_YES
)
3086 prof_child_exit(&wait_start
); /* leaving a child now */
3090 vim_free(cookie
.nextline
);
3092 convert_setup(&cookie
.conv
, NULL
, NULL
);
3096 vim_free(fname_exp
);
3100 #if defined(FEAT_EVAL) || defined(PROTO)
3112 for (i
= 1; i
<= script_items
.ga_len
&& !got_int
; ++i
)
3113 if (SCRIPT_ITEM(i
).sn_name
!= NULL
)
3114 smsg((char_u
*)"%3d: %s", i
, SCRIPT_ITEM(i
).sn_name
);
3117 # if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
3119 * Fix slashes in the list of script names for 'shellslash'.
3122 scriptnames_slash_adjust()
3126 for (i
= 1; i
<= script_items
.ga_len
; ++i
)
3127 if (SCRIPT_ITEM(i
).sn_name
!= NULL
)
3128 slash_adjust(SCRIPT_ITEM(i
).sn_name
);
3133 * Get a pointer to a script name. Used for ":verbose set".
3139 if (id
== SID_MODELINE
)
3140 return (char_u
*)_("modeline");
3141 if (id
== SID_CMDARG
)
3142 return (char_u
*)_("--cmd argument");
3144 return (char_u
*)_("-c argument");
3146 return (char_u
*)_("environment variable");
3147 if (id
== SID_ERROR
)
3148 return (char_u
*)_("error handler");
3149 return SCRIPT_ITEM(id
).sn_name
;
3152 # if defined(EXITFREE) || defined(PROTO)
3158 for (i
= script_items
.ga_len
; i
> 0; --i
)
3159 vim_free(SCRIPT_ITEM(i
).sn_name
);
3160 ga_clear(&script_items
);
3166 #if defined(USE_CR) || defined(PROTO)
3168 # if defined(__MSL__) && (__MSL__ >= 22)
3170 * Newer version of the Metrowerks library handle DOS and UNIX files
3172 * Test with earlier versions, MSL 2.2 is the library supplied with
3173 * Codewarrior Pro 2.
3176 fgets_cr(s
, n
, stream
)
3181 return fgets(s
, n
, stream
);
3185 * Version of fgets() which also works for lines ending in a <CR> only
3186 * (Macintosh format).
3187 * For older versions of the Metrowerks library.
3188 * At least CodeWarrior 9 needed this code.
3191 fgets_cr(s
, n
, stream
)
3199 while (!feof(stream
) && c
!= '\r' && c
!= '\n' && char_read
< n
- 1)
3203 /* If the file is in DOS format, we need to skip a NL after a CR. I
3204 * thought it was the other way around, but this appears to work... */
3217 if (feof(stream
) && char_read
== 1)
3226 * Get one full line from a sourced file.
3227 * Called by do_cmdline() when it's called from do_source().
3229 * Return a pointer to the line in allocated memory.
3230 * Return NULL for end-of-file or some error.
3234 getsourceline(c
, cookie
, indent
)
3235 int c
; /* not used */
3237 int indent
; /* not used */
3239 struct source_cookie
*sp
= (struct source_cookie
*)cookie
;
3244 /* If breakpoints have been added/deleted need to check for it. */
3245 if (sp
->dbg_tick
< debug_tick
)
3247 sp
->breakpoint
= dbg_find_breakpoint(TRUE
, sp
->fname
, sourcing_lnum
);
3248 sp
->dbg_tick
= debug_tick
;
3250 # ifdef FEAT_PROFILE
3251 if (do_profiling
== PROF_YES
)
3256 * Get current line. If there is a read-ahead line, use it, otherwise get
3261 else if (sp
->nextline
== NULL
)
3262 line
= get_one_sourceline(sp
);
3265 line
= sp
->nextline
;
3266 sp
->nextline
= NULL
;
3270 if (line
!= NULL
&& do_profiling
== PROF_YES
)
3271 script_line_start();
3274 /* Only concatenate lines starting with a \ when 'cpoptions' doesn't
3275 * contain the 'C' flag. */
3276 if (line
!= NULL
&& (vim_strchr(p_cpo
, CPO_CONCAT
) == NULL
))
3278 /* compensate for the one line read-ahead */
3282 sp
->nextline
= get_one_sourceline(sp
);
3283 if (sp
->nextline
== NULL
)
3285 p
= skipwhite(sp
->nextline
);
3288 s
= alloc((int)(STRLEN(line
) + STRLEN(p
)));
3289 if (s
== NULL
) /* out of memory */
3295 vim_free(sp
->nextline
);
3300 if (line
!= NULL
&& sp
->conv
.vc_type
!= CONV_NONE
)
3302 /* Convert the encoding of the script line. */
3303 s
= string_convert(&sp
->conv
, line
, NULL
);
3313 /* Did we encounter a breakpoint? */
3314 if (sp
->breakpoint
!= 0 && sp
->breakpoint
<= sourcing_lnum
)
3316 dbg_breakpoint(sp
->fname
, sourcing_lnum
);
3317 /* Find next breakpoint. */
3318 sp
->breakpoint
= dbg_find_breakpoint(TRUE
, sp
->fname
, sourcing_lnum
);
3319 sp
->dbg_tick
= debug_tick
;
3327 get_one_sourceline(sp
)
3328 struct source_cookie
*sp
;
3335 int has_cr
; /* CR-LF found */
3340 int have_read
= FALSE
;
3342 /* use a growarray to store the sourced line */
3343 ga_init2(&ga
, 1, 250);
3346 * Loop until there is a finished line (or end-of-file).
3351 /* make room to read at least 120 (more) characters */
3352 if (ga_grow(&ga
, 120) == FAIL
)
3354 buf
= (char_u
*)ga
.ga_data
;
3357 if (sp
->fileformat
== EOL_MAC
)
3359 if (fgets_cr((char *)buf
+ ga
.ga_len
, ga
.ga_maxlen
- ga
.ga_len
,
3365 if (fgets((char *)buf
+ ga
.ga_len
, ga
.ga_maxlen
- ga
.ga_len
,
3368 len
= ga
.ga_len
+ (int)STRLEN(buf
+ ga
.ga_len
);
3370 /* Ignore a trailing CTRL-Z, when in Dos mode. Only recognize the
3371 * CTRL-Z by its own, or after a NL. */
3372 if ( (len
== 1 || (len
>= 2 && buf
[len
- 2] == '\n'))
3373 && sp
->fileformat
== EOL_DOS
3374 && buf
[len
- 1] == Ctrl_Z
)
3382 /* If the read doesn't stop on a new line, and there's
3383 * some CR then we assume a Mac format */
3384 if (sp
->fileformat
== EOL_UNKNOWN
)
3386 if (buf
[len
- 1] != '\n' && vim_strchr(buf
, '\r') != NULL
)
3387 sp
->fileformat
= EOL_MAC
;
3389 sp
->fileformat
= EOL_UNIX
;
3392 if (sp
->fileformat
== EOL_MAC
)
3394 scan
= vim_strchr(buf
, '\r');
3399 if (*(scan
+ 1) != 0)
3402 fseek(sp
->fp
, (long)(scan
- buf
- len
+ 1), SEEK_CUR
);
3412 /* If the line was longer than the buffer, read more. */
3413 if (ga
.ga_maxlen
- ga
.ga_len
== 1 && buf
[len
- 1] != '\n')
3416 if (len
>= 1 && buf
[len
- 1] == '\n') /* remove trailing NL */
3419 has_cr
= (len
>= 2 && buf
[len
- 2] == '\r');
3420 if (sp
->fileformat
== EOL_UNKNOWN
)
3423 sp
->fileformat
= EOL_DOS
;
3425 sp
->fileformat
= EOL_UNIX
;
3428 if (sp
->fileformat
== EOL_DOS
)
3430 if (has_cr
) /* replace trailing CR */
3432 buf
[len
- 2] = '\n';
3436 else /* lines like ":map xx yy^M" will have failed */
3440 msg_source(hl_attr(HLF_W
));
3441 EMSG(_("W15: Warning: Wrong line separator, ^M may be missing"));
3444 sp
->fileformat
= EOL_UNIX
;
3448 /* The '\n' is escaped if there is an odd number of ^V's just
3449 * before it, first set "c" just before the 'V's and then check
3450 * len&c parities (is faster than ((len-c)%2 == 0)) -- Acevedo */
3451 for (c
= len
- 2; c
>= 0 && buf
[c
] == Ctrl_V
; c
--)
3453 if ((len
& 1) != (c
& 1)) /* escaped NL, read more */
3459 buf
[len
- 1] = NUL
; /* remove the NL */
3463 * Check for ^C here now and then, so recursive :so can be broken.
3470 return (char_u
*)ga
.ga_data
;
3472 vim_free(ga
.ga_data
);
3476 #if defined(FEAT_PROFILE) || defined(PROTO)
3478 * Called when starting to read a script line.
3479 * "sourcing_lnum" must be correct!
3480 * When skipping lines it may not actually be executed, but we won't find out
3481 * until later and we need to store the time now.
3489 if (current_SID
<= 0 || current_SID
> script_items
.ga_len
)
3491 si
= &SCRIPT_ITEM(current_SID
);
3492 if (si
->sn_prof_on
&& sourcing_lnum
>= 1)
3494 /* Grow the array before starting the timer, so that the time spend
3495 * here isn't counted. */
3496 ga_grow(&si
->sn_prl_ga
, (int)(sourcing_lnum
- si
->sn_prl_ga
.ga_len
));
3497 si
->sn_prl_idx
= sourcing_lnum
- 1;
3498 while (si
->sn_prl_ga
.ga_len
<= si
->sn_prl_idx
3499 && si
->sn_prl_ga
.ga_len
< si
->sn_prl_ga
.ga_maxlen
)
3501 /* Zero counters for a line that was not used before. */
3502 pp
= &PRL_ITEM(si
, si
->sn_prl_ga
.ga_len
);
3504 profile_zero(&pp
->sn_prl_total
);
3505 profile_zero(&pp
->sn_prl_self
);
3506 ++si
->sn_prl_ga
.ga_len
;
3508 si
->sn_prl_execed
= FALSE
;
3509 profile_start(&si
->sn_prl_start
);
3510 profile_zero(&si
->sn_prl_children
);
3511 profile_get_wait(&si
->sn_prl_wait
);
3516 * Called when actually executing a function line.
3523 if (current_SID
<= 0 || current_SID
> script_items
.ga_len
)
3525 si
= &SCRIPT_ITEM(current_SID
);
3526 if (si
->sn_prof_on
&& si
->sn_prl_idx
>= 0)
3527 si
->sn_prl_execed
= TRUE
;
3531 * Called when done with a function line.
3539 if (current_SID
<= 0 || current_SID
> script_items
.ga_len
)
3541 si
= &SCRIPT_ITEM(current_SID
);
3542 if (si
->sn_prof_on
&& si
->sn_prl_idx
>= 0
3543 && si
->sn_prl_idx
< si
->sn_prl_ga
.ga_len
)
3545 if (si
->sn_prl_execed
)
3547 pp
= &PRL_ITEM(si
, si
->sn_prl_idx
);
3549 profile_end(&si
->sn_prl_start
);
3550 profile_sub_wait(&si
->sn_prl_wait
, &si
->sn_prl_start
);
3551 profile_add(&pp
->sn_prl_total
, &si
->sn_prl_start
);
3552 profile_self(&pp
->sn_prl_self
, &si
->sn_prl_start
,
3553 &si
->sn_prl_children
);
3555 si
->sn_prl_idx
= -1;
3561 * ":scriptencoding": Set encoding conversion for a sourced script.
3562 * Without the multi-byte feature it's simply ignored.
3566 ex_scriptencoding(eap
)
3570 struct source_cookie
*sp
;
3573 if (!getline_equal(eap
->getline
, eap
->cookie
, getsourceline
))
3575 EMSG(_("E167: :scriptencoding used outside of a sourced file"));
3579 if (*eap
->arg
!= NUL
)
3581 name
= enc_canonize(eap
->arg
);
3582 if (name
== NULL
) /* out of memory */
3588 /* Setup for conversion from the specified encoding to 'encoding'. */
3589 sp
= (struct source_cookie
*)getline_cookie(eap
->getline
, eap
->cookie
);
3590 convert_setup(&sp
->conv
, name
, p_enc
);
3592 if (name
!= eap
->arg
)
3597 #if defined(FEAT_EVAL) || defined(PROTO)
3599 * ":finish": Mark a sourced file as finished.
3605 if (getline_equal(eap
->getline
, eap
->cookie
, getsourceline
))
3606 do_finish(eap
, FALSE
);
3608 EMSG(_("E168: :finish used outside of a sourced file"));
3612 * Mark a sourced file as finished. Possibly makes the ":finish" pending.
3613 * Also called for a pending finish at the ":endtry" or after returning from
3614 * an extra do_cmdline(). "reanimate" is used in the latter case.
3617 do_finish(eap
, reanimate
)
3624 ((struct source_cookie
*)getline_cookie(eap
->getline
,
3625 eap
->cookie
))->finished
= FALSE
;
3628 * Cleanup (and inactivate) conditionals, but stop when a try conditional
3629 * not in its finally clause (which then is to be executed next) is found.
3630 * In this case, make the ":finish" pending for execution at the ":endtry".
3631 * Otherwise, finish normally.
3633 idx
= cleanup_conditionals(eap
->cstack
, 0, TRUE
);
3636 eap
->cstack
->cs_pending
[idx
] = CSTP_FINISH
;
3637 report_make_pending(CSTP_FINISH
, NULL
);
3640 ((struct source_cookie
*)getline_cookie(eap
->getline
,
3641 eap
->cookie
))->finished
= TRUE
;
3646 * Return TRUE when a sourced file had the ":finish" command: Don't give error
3647 * message for missing ":endif".
3648 * Return FALSE when not sourcing a file.
3651 source_finished(fgetline
, cookie
)
3652 char_u
*(*fgetline
) __ARGS((int, void *, int));
3655 return (getline_equal(fgetline
, cookie
, getsourceline
)
3656 && ((struct source_cookie
*)getline_cookie(
3657 fgetline
, cookie
))->finished
);
3661 #if defined(FEAT_LISTCMDS) || defined(PROTO)
3663 * ":checktime [buffer]"
3670 int save_no_check_timestamps
= no_check_timestamps
;
3672 no_check_timestamps
= 0;
3673 if (eap
->addr_count
== 0) /* default is all buffers */
3674 check_timestamps(FALSE
);
3677 buf
= buflist_findnr((int)eap
->line2
);
3678 if (buf
!= NULL
) /* cannot happen? */
3679 (void)buf_check_timestamp(buf
, FALSE
);
3681 no_check_timestamps
= save_no_check_timestamps
;
3685 #if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
3686 && (defined(FEAT_EVAL) || defined(FEAT_MULTI_LANG))
3687 static char *get_locale_val
__ARGS((int what
));
3690 get_locale_val(what
)
3695 /* Obtain the locale value from the libraries. For DJGPP this is
3696 * redefined and it doesn't use the arguments. */
3697 loc
= setlocale(what
, NULL
);
3704 /* setocale() returns something like "LC_COLLATE=<name>;LC_..." when
3705 * one of the values (e.g., LC_CTYPE) differs. */
3706 p
= vim_strchr(loc
, '=');
3710 while (*p
!= NUL
) /* remove trailing newline */
3712 if (*p
< ' ' || *p
== ';')
3730 * On MS-Windows locale names are strings like "German_Germany.1252", but
3731 * gettext expects "de". Try to translate one into another here for a few
3732 * supported languages.
3735 gettext_lang(char_u
*name
)
3738 static char *(mtable
[]) = {
3743 "english_united kingdom", "en_GB",
3755 "chinese_china", "zh_CN",
3756 "chinese_taiwan", "zh_TW",
3759 for (i
= 0; mtable
[i
] != NULL
; i
+= 2)
3760 if (STRNICMP(mtable
[i
], name
, STRLEN(mtable
[i
])) == 0)
3761 return mtable
[i
+ 1];
3766 #if defined(FEAT_MULTI_LANG) || defined(PROTO)
3768 * Obtain the current messages language. Used to set the default for
3769 * 'helplang'. May return NULL or an empty string.
3776 # if (defined(HAVE_LOCALE_H) || defined(X_LOCALE))
3777 # if defined(LC_MESSAGES)
3778 p
= (char_u
*)get_locale_val(LC_MESSAGES
);
3780 /* This is necessary for Win32, where LC_MESSAGES is not defined and $LANG
3781 * may be set to the LCID number. LC_COLLATE is the best guess, LC_TIME
3782 * and LC_MONETARY may be set differently for a Japanese working in the
3784 p
= (char_u
*)get_locale_val(LC_COLLATE
);
3787 p
= mch_getenv((char_u
*)"LC_ALL");
3788 if (p
== NULL
|| *p
== NUL
)
3790 p
= mch_getenv((char_u
*)"LC_MESSAGES");
3791 if (p
== NULL
|| *p
== NUL
)
3792 p
= mch_getenv((char_u
*)"LANG");
3796 p
= gettext_lang(p
);
3802 /* Complicated #if; matches with where get_mess_env() is used below. */
3803 #if (defined(FEAT_EVAL) && !((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
3804 && defined(LC_MESSAGES))) \
3805 || ((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
3806 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)) \
3807 && !defined(LC_MESSAGES))
3808 static char_u
*get_mess_env
__ARGS((void));
3811 * Get the language used for messages from the environment.
3818 p
= mch_getenv((char_u
*)"LC_ALL");
3819 if (p
== NULL
|| *p
== NUL
)
3821 p
= mch_getenv((char_u
*)"LC_MESSAGES");
3822 if (p
== NULL
|| *p
== NUL
)
3824 p
= mch_getenv((char_u
*)"LANG");
3825 if (p
!= NULL
&& VIM_ISDIGIT(*p
))
3826 p
= NULL
; /* ignore something like "1043" */
3827 # if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
3828 if (p
== NULL
|| *p
== NUL
)
3829 p
= (char_u
*)get_locale_val(LC_CTYPE
);
3837 #if defined(FEAT_EVAL) || defined(PROTO)
3840 * Set the "v:lang" variable according to the current locale setting.
3841 * Also do "v:lc_time"and "v:ctype".
3848 # if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
3849 loc
= (char_u
*)get_locale_val(LC_CTYPE
);
3851 /* setlocale() not supported: use the default value */
3852 loc
= (char_u
*)"C";
3854 set_vim_var_string(VV_CTYPE
, loc
, -1);
3856 /* When LC_MESSAGES isn't defined use the value from $LC_MESSAGES, fall
3857 * back to LC_CTYPE if it's empty. */
3858 # if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) && defined(LC_MESSAGES)
3859 loc
= (char_u
*)get_locale_val(LC_MESSAGES
);
3861 loc
= get_mess_env();
3863 set_vim_var_string(VV_LANG
, loc
, -1);
3865 # if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
3866 loc
= (char_u
*)get_locale_val(LC_TIME
);
3868 set_vim_var_string(VV_LC_TIME
, loc
, -1);
3872 #if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
3873 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
3875 * ":language": Set the language (locale).
3887 # define VIM_LC_MESSAGES LC_MESSAGES
3889 # define VIM_LC_MESSAGES 6789
3894 /* Check for "messages {name}", "ctype {name}" or "time {name}" argument.
3895 * Allow abbreviation, but require at least 3 characters to avoid
3896 * confusion with a two letter language name "me" or "ct". */
3897 p
= skiptowhite(eap
->arg
);
3898 if ((*p
== NUL
|| vim_iswhite(*p
)) && p
- eap
->arg
>= 3)
3900 if (STRNICMP(eap
->arg
, "messages", p
- eap
->arg
) == 0)
3902 what
= VIM_LC_MESSAGES
;
3903 name
= skipwhite(p
);
3904 whatstr
= "messages ";
3906 else if (STRNICMP(eap
->arg
, "ctype", p
- eap
->arg
) == 0)
3909 name
= skipwhite(p
);
3912 else if (STRNICMP(eap
->arg
, "time", p
- eap
->arg
) == 0)
3915 name
= skipwhite(p
);
3923 if (what
== VIM_LC_MESSAGES
)
3927 p
= (char_u
*)setlocale(what
, NULL
);
3928 if (p
== NULL
|| *p
== NUL
)
3929 p
= (char_u
*)"Unknown";
3930 smsg((char_u
*)_("Current %slanguage: \"%s\""), whatstr
, p
);
3935 if (what
== VIM_LC_MESSAGES
)
3939 loc
= setlocale(what
, (char *)name
);
3941 EMSG2(_("E197: Cannot set language to \"%s\""), name
);
3944 #ifdef HAVE_NL_MSG_CAT_CNTR
3945 /* Need to do this for GNU gettext, otherwise cached translations
3946 * will be used again. */
3947 extern int _nl_msg_cat_cntr
;
3951 /* Reset $LC_ALL, otherwise it would overrule everyting. */
3952 vim_setenv((char_u
*)"LC_ALL", (char_u
*)"");
3954 if (what
!= LC_TIME
)
3956 /* Tell gettext() what to translate to. It apparently doesn't
3957 * use the currently effective locale. Also do this when
3958 * FEAT_GETTEXT isn't defined, so that shell commands use this
3962 vim_setenv((char_u
*)"LANG", name
);
3964 /* Apparently MS-Windows printf() may cause a crash when
3965 * we give it 8-bit text while it's expecting text in the
3966 * current locale. This call avoids that. */
3967 setlocale(LC_CTYPE
, "C");
3970 if (what
!= LC_CTYPE
)
3974 mname
= gettext_lang(name
);
3978 vim_setenv((char_u
*)"LC_MESSAGES", mname
);
3979 #ifdef FEAT_MULTI_LANG
3980 set_helplang_default(mname
);
3984 /* Set $LC_CTYPE, because it overrules $LANG, and
3985 * gtk_set_locale() calls setlocale() again. gnome_init()
3986 * sets $LC_CTYPE to "en_US" (that's a bug!). */
3987 if (what
!= VIM_LC_MESSAGES
)
3988 vim_setenv((char_u
*)"LC_CTYPE", name
);
3989 # ifdef FEAT_GUI_GTK
3990 /* Let GTK know what locale we're using. Not sure this is
3991 * really needed... */
3993 (void)gtk_set_locale();
3998 /* Set v:lang, v:lc_time and v:ctype to the final result. */
4005 # if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4007 * Function given to ExpandGeneric() to obtain the possible arguments of the
4008 * ":language" command.
4012 get_lang_arg(xp
, idx
)
4017 return (char_u
*)"messages";
4019 return (char_u
*)"ctype";
4021 return (char_u
*)"time";