Skip to content

Commit d29459b

Browse files
committed
patch 7.4.2263
Problem: :filter does not work for many commands. Can only get matching messages. Solution: Make :filter work for :command, :map, :list, :number and :print. Make ":filter!" show non-matching lines.
1 parent 2570957 commit d29459b

File tree

8 files changed

+68
-6
lines changed

8 files changed

+68
-6
lines changed

src/ex_cmds.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2918,6 +2918,10 @@ print_line(linenr_T lnum, int use_number, int list)
29182918
{
29192919
int save_silent = silent_mode;
29202920

2921+
/* apply :filter /pat/ */
2922+
if (message_filtered(ml_get(lnum)))
2923+
return;
2924+
29212925
msg_start();
29222926
silent_mode = FALSE;
29232927
info_message = TRUE; /* use mch_msg(), not mch_errmsg() */

src/ex_cmds.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,7 @@ EX(CMD_filetype, "filetype", ex_filetype,
545545
EXTRA|TRLBAR|CMDWIN,
546546
ADDR_LINES),
547547
EX(CMD_filter, "filter", ex_wrongmodifier,
548-
NEEDARG|EXTRA|NOTRLCOM,
548+
BANG|NEEDARG|EXTRA|NOTRLCOM,
549549
ADDR_LINES),
550550
EX(CMD_find, "find", ex_find,
551551
RANGE|NOTADR|BANG|FILE1|EDITCMD|ARGOPT|TRLBAR,

src/ex_docmd.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1925,6 +1925,13 @@ do_one_cmd(
19251925
if (!checkforcmd(&p, "filter", 4)
19261926
|| *p == NUL || ends_excmd(*p))
19271927
break;
1928+
if (*p == '!')
1929+
{
1930+
cmdmod.filter_force = TRUE;
1931+
p = skipwhite(p + 1);
1932+
if (*p == NUL || ends_excmd(*p))
1933+
break;
1934+
}
19281935
p = skip_vimgrep_pat(p, &reg_pat, NULL);
19291936
if (p == NULL || *p == NUL)
19301937
break;
@@ -5928,8 +5935,10 @@ uc_list(char_u *name, size_t name_len)
59285935
cmd = USER_CMD_GA(gap, i);
59295936
a = (long)cmd->uc_argt;
59305937

5931-
/* Skip commands which don't match the requested prefix */
5932-
if (STRNCMP(name, cmd->uc_name, name_len) != 0)
5938+
/* Skip commands which don't match the requested prefix and
5939+
* commands filtered out. */
5940+
if (STRNCMP(name, cmd->uc_name, name_len) != 0
5941+
|| message_filtered(cmd->uc_name))
59335942
continue;
59345943

59355944
/* Put out the title first time */

src/getchar.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1919,7 +1919,7 @@ vungetc(int c)
19191919
* This may do a blocking wait if "advance" is TRUE.
19201920
*
19211921
* if "advance" is TRUE (vgetc()):
1922-
* really get the character.
1922+
* Really get the character.
19231923
* KeyTyped is set to TRUE in the case the user typed the key.
19241924
* KeyStuffed is TRUE if the character comes from the stuff buffer.
19251925
* if "advance" is FALSE (vpeekc()):
@@ -3987,6 +3987,9 @@ showmap(
39873987
int len = 1;
39883988
char_u *mapchars;
39893989

3990+
if (message_filtered(mp->m_keys) && message_filtered(mp->m_str))
3991+
return;
3992+
39903993
if (msg_didout || msg_silent != 0)
39913994
{
39923995
msg_putchar('\n');

src/message.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2161,8 +2161,12 @@ msg_puts_display(
21612161
int
21622162
message_filtered(char_u *msg)
21632163
{
2164-
return cmdmod.filter_regmatch.regprog != NULL
2165-
&& !vim_regexec(&cmdmod.filter_regmatch, msg, (colnr_T)0);
2164+
int match;
2165+
2166+
if (cmdmod.filter_regmatch.regprog == NULL)
2167+
return FALSE;
2168+
match = vim_regexec(&cmdmod.filter_regmatch, msg, (colnr_T)0);
2169+
return cmdmod.filter_force ? match : !match;
21662170
}
21672171

21682172
/*

src/structs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,7 @@ typedef struct
572572
char_u *save_ei; /* saved value of 'eventignore' */
573573
# endif
574574
regmatch_T filter_regmatch; /* set by :filter /pat/ */
575+
int filter_force; /* set for :filter! */
575576
} cmdmod_T;
576577

577578
#define MF_SEED_LEN 8

src/testdir/test_filter_cmd.vim

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,39 @@ func Test_filter()
44
edit Xdoesnotmatch
55
edit Xwillmatch
66
call assert_equal('"Xwillmatch"', substitute(execute('filter willma ls'), '[^"]*\(".*"\)[^"]*', '\1', ''))
7+
bwipe Xdoesnotmatch
8+
bwipe Xwillmatch
9+
10+
new
11+
call setline(1, ['foo1', 'foo2', 'foo3', 'foo4', 'foo5'])
12+
call assert_equal("\nfoo2\nfoo4", execute('filter /foo[24]/ 1,$print'))
13+
call assert_equal("\n 2 foo2\n 4 foo4", execute('filter /foo[24]/ 1,$number'))
14+
call assert_equal("\nfoo2$\nfoo4$", execute('filter /foo[24]/ 1,$list'))
15+
16+
call assert_equal("\nfoo1$\nfoo3$\nfoo5$", execute('filter! /foo[24]/ 1,$list'))
17+
bwipe!
18+
19+
command XTryThis echo 'this'
20+
command XTryThat echo 'that'
21+
command XDoThat echo 'that'
22+
let lines = split(execute('filter XTry command'), "\n")
23+
call assert_equal(3, len(lines))
24+
call assert_match("XTryThat", lines[1])
25+
call assert_match("XTryThis", lines[2])
26+
delcommand XTryThis
27+
delcommand XTryThat
28+
delcommand XDoThat
29+
30+
map f1 the first key
31+
map f2 the second key
32+
map f3 not a key
33+
let lines = split(execute('filter the map f'), "\n")
34+
call assert_equal(2, len(lines))
35+
call assert_match("f2", lines[0])
36+
call assert_match("f1", lines[1])
37+
unmap f1
38+
unmap f2
39+
unmap f3
740
endfunc
841

942
func Test_filter_fails()
@@ -12,4 +45,10 @@ func Test_filter_fails()
1245
call assert_fails('filter /pat', 'E476:')
1346
call assert_fails('filter /pat/', 'E476:')
1447
call assert_fails('filter /pat/ asdf', 'E492:')
48+
49+
call assert_fails('filter!', 'E471:')
50+
call assert_fails('filter! pat', 'E476:')
51+
call assert_fails('filter! /pat', 'E476:')
52+
call assert_fails('filter! /pat/', 'E476:')
53+
call assert_fails('filter! /pat/ asdf', 'E492:')
1554
endfunc

src/version.c

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

764764
static int included_patches[] =
765765
{ /* Add new patch number below this line */
766+
/**/
767+
2263,
766768
/**/
767769
2262,
768770
/**/

0 commit comments

Comments
 (0)