Skip to content

Commit 9a32768

Browse files
committed
Merge remote-tracking branch 'vim/master'
2 parents 3c0ba7c + 67435d9 commit 9a32768

10 files changed

Lines changed: 179 additions & 41 deletions

File tree

runtime/doc/autocmd.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,18 @@ CmdUndefined When a user command is used but it isn't
492492
command is defined. An alternative is to
493493
always define the user command and have it
494494
invoke an autoloaded function. See |autoload|.
495+
*CmdlineEnter*
496+
CmdlineEnter After moving the cursor to the command line,
497+
where the user can type a command or search
498+
string.
499+
<afile> is set to a single character,
500+
indicating the type of command-line.
501+
|cmdwin-char|
502+
*CmdlineLeave*
503+
CmdlineLeave Before leaving the command line.
504+
<afile> is set to a single character,
505+
indicating the type of command-line.
506+
|cmdwin-char|
495507
*CmdwinEnter*
496508
CmdwinEnter After entering the command-line window.
497509
Useful for setting options specifically for

src/ex_cmds2.c

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1714,7 +1714,7 @@ script_do_profile(scriptitem_T *si)
17141714
}
17151715

17161716
/*
1717-
* save time when starting to invoke another script or function.
1717+
* Save time when starting to invoke another script or function.
17181718
*/
17191719
void
17201720
script_prof_save(
@@ -1805,12 +1805,14 @@ script_dump_profile(FILE *fd)
18051805
fprintf(fd, "Cannot open file!\n");
18061806
else
18071807
{
1808-
for (i = 0; i < si->sn_prl_ga.ga_len; ++i)
1808+
/* Keep going till the end of file, so that trailing
1809+
* continuation lines are listed. */
1810+
for (i = 0; ; ++i)
18091811
{
18101812
if (vim_fgets(IObuff, IOSIZE, sfd))
18111813
break;
1812-
pp = &PRL_ITEM(si, i);
1813-
if (pp->snp_count > 0)
1814+
if (i < si->sn_prl_ga.ga_len
1815+
&& (pp = &PRL_ITEM(si, i))->snp_count > 0)
18141816
{
18151817
fprintf(fd, "%5d ", pp->snp_count);
18161818
if (profile_equal(&pp->sn_prl_total, &pp->sn_prl_self))
@@ -4307,27 +4309,6 @@ do_source(
43074309
save_sourcing_lnum = sourcing_lnum;
43084310
sourcing_lnum = 0;
43094311

4310-
#ifdef FEAT_MBYTE
4311-
cookie.conv.vc_type = CONV_NONE; /* no conversion */
4312-
4313-
/* Read the first line so we can check for a UTF-8 BOM. */
4314-
firstline = getsourceline(0, (void *)&cookie, 0);
4315-
if (firstline != NULL && STRLEN(firstline) >= 3 && firstline[0] == 0xef
4316-
&& firstline[1] == 0xbb && firstline[2] == 0xbf)
4317-
{
4318-
/* Found BOM; setup conversion, skip over BOM and recode the line. */
4319-
convert_setup(&cookie.conv, (char_u *)"utf-8", p_enc);
4320-
p = string_convert(&cookie.conv, firstline + 3, NULL);
4321-
if (p == NULL)
4322-
p = vim_strsave(firstline + 3);
4323-
if (p != NULL)
4324-
{
4325-
vim_free(firstline);
4326-
firstline = p;
4327-
}
4328-
}
4329-
#endif
4330-
43314312
#ifdef STARTUPTIME
43324313
if (time_fd != NULL)
43334314
time_push(&tv_rel, &tv_start);
@@ -4420,6 +4401,27 @@ do_source(
44204401
# endif
44214402
#endif
44224403

4404+
#ifdef FEAT_MBYTE
4405+
cookie.conv.vc_type = CONV_NONE; /* no conversion */
4406+
4407+
/* Read the first line so we can check for a UTF-8 BOM. */
4408+
firstline = getsourceline(0, (void *)&cookie, 0);
4409+
if (firstline != NULL && STRLEN(firstline) >= 3 && firstline[0] == 0xef
4410+
&& firstline[1] == 0xbb && firstline[2] == 0xbf)
4411+
{
4412+
/* Found BOM; setup conversion, skip over BOM and recode the line. */
4413+
convert_setup(&cookie.conv, (char_u *)"utf-8", p_enc);
4414+
p = string_convert(&cookie.conv, firstline + 3, NULL);
4415+
if (p == NULL)
4416+
p = vim_strsave(firstline + 3);
4417+
if (p != NULL)
4418+
{
4419+
vim_free(firstline);
4420+
firstline = p;
4421+
}
4422+
}
4423+
#endif
4424+
44234425
/*
44244426
* Call do_cmdline, which will call getsourceline() to get the lines.
44254427
*/
@@ -4902,7 +4904,8 @@ script_line_start(void)
49024904
{
49034905
/* Grow the array before starting the timer, so that the time spent
49044906
* here isn't counted. */
4905-
(void)ga_grow(&si->sn_prl_ga, (int)(sourcing_lnum - si->sn_prl_ga.ga_len));
4907+
(void)ga_grow(&si->sn_prl_ga,
4908+
(int)(sourcing_lnum - si->sn_prl_ga.ga_len));
49064909
si->sn_prl_idx = sourcing_lnum - 1;
49074910
while (si->sn_prl_ga.ga_len <= si->sn_prl_idx
49084911
&& si->sn_prl_ga.ga_len < si->sn_prl_ga.ga_maxlen)
@@ -4937,7 +4940,7 @@ script_line_exec(void)
49374940
}
49384941

49394942
/*
4940-
* Called when done with a function line.
4943+
* Called when done with a script line.
49414944
*/
49424945
void
49434946
script_line_end(void)

src/ex_docmd.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7241,10 +7241,14 @@ ex_quit(exarg_T *eap)
72417241
wp = curwin;
72427242

72437243
#ifdef FEAT_AUTOCMD
7244-
apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
7245-
/* Refuse to quit when locked or when the buffer in the last window is
7246-
* being closed (can only happen in autocommands). */
7247-
if (curbuf_locked() || !win_valid(wp)
7244+
/* Refuse to quit when locked. */
7245+
if (curbuf_locked())
7246+
return;
7247+
apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
7248+
/* Bail out when autocommands closed the window.
7249+
* Refuse to quit when the buffer in the last window is being closed (can
7250+
* only happen in autocommands). */
7251+
if (!win_valid(wp)
72487252
|| (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
72497253
return;
72507254
#endif
@@ -7258,8 +7262,8 @@ ex_quit(exarg_T *eap)
72587262
*/
72597263
if (check_more(FALSE, eap->forceit) == OK && only_one_window())
72607264
exiting = TRUE;
7261-
if ((!buf_hide(curbuf)
7262-
&& check_changed(curbuf, (p_awa ? CCGD_AW : 0)
7265+
if ((!buf_hide(wp->w_buffer)
7266+
&& check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
72637267
| (eap->forceit ? CCGD_FORCEIT : 0)
72647268
| CCGD_EXCMD))
72657269
|| check_more(TRUE, eap->forceit) == FAIL

src/ex_getln.c

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,19 @@ sort_func_compare(const void *s1, const void *s2);
145145
static void set_search_match(pos_T *t);
146146
#endif
147147

148+
149+
#ifdef FEAT_AUTOCMD
150+
static void
151+
trigger_cmd_autocmd(int typechar, int evt)
152+
{
153+
char_u typestr[2];
154+
155+
typestr[0] = typechar;
156+
typestr[1] = NUL;
157+
apply_autocmds(evt, typestr, typestr, FALSE, curbuf);
158+
}
159+
#endif
160+
148161
/*
149162
* getcmdline() - accept a command line starting with firstc.
150163
*
@@ -222,6 +235,9 @@ getcmdline(
222235
* custom status line may invoke ":normal". */
223236
struct cmdline_info save_ccline;
224237
#endif
238+
#ifdef FEAT_AUTOCMD
239+
int cmdline_type;
240+
#endif
225241

226242
#ifdef FEAT_EVAL
227243
if (firstc == -1)
@@ -349,6 +365,12 @@ getcmdline(
349365
* terminal mode set to cooked. Need to set raw mode here then. */
350366
settmode(TMODE_RAW);
351367

368+
#ifdef FEAT_AUTOCMD
369+
/* Trigger CmdlineEnter autocommands. */
370+
cmdline_type = firstc == NUL ? '-' : firstc;
371+
trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINEENTER);
372+
#endif
373+
352374
#ifdef FEAT_CMDHIST
353375
init_history();
354376
hiscnt = hislen; /* set hiscnt to impossible history value */
@@ -2093,6 +2115,11 @@ getcmdline(
20932115
if (some_key_typed)
20942116
need_wait_return = FALSE;
20952117

2118+
#ifdef FEAT_AUTOCMD
2119+
/* Trigger CmdlineLeave autocommands. */
2120+
trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINELEAVE);
2121+
#endif
2122+
20962123
State = save_State;
20972124
#ifdef USE_IM_CONTROL
20982125
if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
@@ -6848,9 +6875,6 @@ open_cmdwin(void)
68486875
linenr_T lnum;
68496876
int histtype;
68506877
garray_T winsizes;
6851-
#ifdef FEAT_AUTOCMD
6852-
char_u typestr[2];
6853-
#endif
68546878
int save_restart_edit = restart_edit;
68556879
int save_State = State;
68566880
int save_exmode = exmode_active;
@@ -6979,9 +7003,7 @@ open_cmdwin(void)
69797003

69807004
# ifdef FEAT_AUTOCMD
69817005
/* Trigger CmdwinEnter autocommands. */
6982-
typestr[0] = cmdwin_type;
6983-
typestr[1] = NUL;
6984-
apply_autocmds(EVENT_CMDWINENTER, typestr, typestr, FALSE, curbuf);
7006+
trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINENTER);
69857007
if (restart_edit != 0) /* autocmd with ":startinsert" */
69867008
stuffcharReadbuff(K_NOP);
69877009
# endif
@@ -7004,7 +7026,7 @@ open_cmdwin(void)
70047026
# endif
70057027

70067028
/* Trigger CmdwinLeave autocommands. */
7007-
apply_autocmds(EVENT_CMDWINLEAVE, typestr, typestr, FALSE, curbuf);
7029+
trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINLEAVE);
70087030

70097031
# ifdef FEAT_FOLDING
70107032
/* Restore KeyTyped in case it is modified by autocommands */

src/fileio.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7769,6 +7769,8 @@ static struct event_name
77697769
{"BufWritePost", EVENT_BUFWRITEPOST},
77707770
{"BufWritePre", EVENT_BUFWRITEPRE},
77717771
{"BufWriteCmd", EVENT_BUFWRITECMD},
7772+
{"CmdlineEnter", EVENT_CMDLINEENTER},
7773+
{"CmdlineLeave", EVENT_CMDLINELEAVE},
77727774
{"CmdwinEnter", EVENT_CMDWINENTER},
77737775
{"CmdwinLeave", EVENT_CMDWINLEAVE},
77747776
{"CmdUndefined", EVENT_CMDUNDEFINED},

src/testdir/test_autocmd.vim

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -779,3 +779,39 @@ func Test_BufLeave_Wipe()
779779
" check that bufinfo doesn't contain a pointer to freed memory
780780
call test_garbagecollect_now()
781781
endfunc
782+
783+
func Test_QuitPre()
784+
edit Xfoo
785+
let winid = win_getid(winnr())
786+
split Xbar
787+
au! QuitPre * let g:afile = expand('<afile>')
788+
" Close the other window, <afile> should be correct.
789+
exe win_id2win(winid) . 'q'
790+
call assert_equal('Xfoo', g:afile)
791+
792+
unlet g:afile
793+
bwipe Xfoo
794+
bwipe Xbar
795+
endfunc
796+
797+
func Test_Cmdline()
798+
au! CmdlineEnter : let g:entered = expand('<afile>')
799+
au! CmdlineLeave : let g:left = expand('<afile>')
800+
let g:entered = 0
801+
let g:left = 0
802+
call feedkeys(":echo 'hello'\<CR>", 'xt')
803+
call assert_equal(':', g:entered)
804+
call assert_equal(':', g:left)
805+
au! CmdlineEnter
806+
au! CmdlineLeave
807+
808+
au! CmdlineEnter / let g:entered = expand('<afile>')
809+
au! CmdlineLeave / let g:left = expand('<afile>')
810+
let g:entered = 0
811+
let g:left = 0
812+
call feedkeys("/hello<CR>", 'xt')
813+
call assert_equal('/', g:entered)
814+
call assert_equal('/', g:left)
815+
au! CmdlineEnter
816+
au! CmdlineLeave
817+
endfunc

src/testdir/test_edit.vim

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1373,3 +1373,16 @@ func Test_edit_complete_very_long_name()
13731373
endif
13741374
set swapfile&
13751375
endfunc
1376+
1377+
func Test_edit_quit()
1378+
edit foo.txt
1379+
split
1380+
new
1381+
call setline(1, 'hello')
1382+
3wincmd w
1383+
redraw!
1384+
call assert_fails('1q', 'E37:')
1385+
bwipe! foo.txt
1386+
only
1387+
endfunc
1388+

src/testdir/test_profile.vim

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ func Test_profile_file()
115115
call assert_match('^ Self time:\s\+\d\+\.\d\+$', lines[3])
116116
call assert_equal('', lines[4])
117117
call assert_equal('count total (s) self (s)', lines[5])
118-
call assert_equal(' func! Foo()', lines[6])
118+
call assert_match(' 2 0.\d\+ func! Foo()', lines[6])
119119
call assert_equal(' endfunc', lines[7])
120120
" Loop iterates 10 times. Since script runs twice, body executes 20 times.
121121
" First line of loop executes one more time than body to detect end of loop.
@@ -132,6 +132,42 @@ func Test_profile_file()
132132
call delete('Xprofile_file.log')
133133
endfunc
134134

135+
func Test_profile_file_with_cont()
136+
let lines = [
137+
\ 'echo "hello',
138+
\ ' \ world"',
139+
\ 'echo "foo ',
140+
\ ' \bar"',
141+
\ ]
142+
143+
call writefile(lines, 'Xprofile_file.vim')
144+
call system(v:progpath
145+
\ . ' -es --clean'
146+
\ . ' -c "profile start Xprofile_file.log"'
147+
\ . ' -c "profile file Xprofile_file.vim"'
148+
\ . ' -c "so Xprofile_file.vim"'
149+
\ . ' -c "qall!"')
150+
call assert_equal(0, v:shell_error)
151+
152+
let lines = readfile('Xprofile_file.log')
153+
call assert_equal(11, len(lines))
154+
155+
call assert_match('^SCRIPT .*Xprofile_file.vim$', lines[0])
156+
call assert_equal('Sourced 1 time', lines[1])
157+
call assert_match('^Total time:\s\+\d\+\.\d\+$', lines[2])
158+
call assert_match('^ Self time:\s\+\d\+\.\d\+$', lines[3])
159+
call assert_equal('', lines[4])
160+
call assert_equal('count total (s) self (s)', lines[5])
161+
call assert_match(' 1 0.\d\+ echo "hello', lines[6])
162+
call assert_equal(' \ world"', lines[7])
163+
call assert_match(' 1 0.\d\+ echo "foo ', lines[8])
164+
call assert_equal(' \bar"', lines[9])
165+
call assert_equal('', lines[10])
166+
167+
call delete('Xprofile_file.vim')
168+
call delete('Xprofile_file.log')
169+
endfunc
170+
135171
func Test_profile_completion()
136172
call feedkeys(":profile \<C-A>\<C-B>\"\<CR>", 'tx')
137173
call assert_equal('"profile continue file func pause start', @:)

src/version.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -776,6 +776,14 @@ static char *(features[]) =
776776

777777
static int included_patches[] =
778778
{ /* Add new patch number below this line */
779+
/**/
780+
1207,
781+
/**/
782+
1206,
783+
/**/
784+
1205,
785+
/**/
786+
1204,
779787
/**/
780788
1203,
781789
/**/

src/vim.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1298,6 +1298,8 @@ enum auto_event
12981298
EVENT_BUFWRITEPOST, /* after writing a buffer */
12991299
EVENT_BUFWRITEPRE, /* before writing a buffer */
13001300
EVENT_BUFWRITECMD, /* write buffer using command */
1301+
EVENT_CMDLINEENTER, /* after entering the command line */
1302+
EVENT_CMDLINELEAVE, /* before leaving the command line */
13011303
EVENT_CMDWINENTER, /* after entering the cmdline window */
13021304
EVENT_CMDWINLEAVE, /* before leaving the cmdline window */
13031305
EVENT_COLORSCHEME, /* after loading a colorscheme */

0 commit comments

Comments
 (0)