vim-patch:9.1.0748: :keep* commmands are sometimes misidentified as :k

Problem:  The :keep{alt,jumps,marks,patterns} commmands are sometimes
          misidentified as :k.
Solution: Make sure one_letter_cmd() only returns true for :k and not
          other :keep* commands (Doug Kearns).

This currently manifests as missing completion for :keep* commands and
incorrect results from fullcommand().

E.g., fullcommand("keepmarks") returns "k" rather than "keepmarks".

The correct command, however, is executed as command modifiers are
handled specially in do_one_cmd() rather than using find_ex_command().

Fix exists(':k') so that it returns 2 for a full match.

closes: vim/vim#15742

ea84202372

Cherry-pick Test_ex_command_completion() from patch 9.1.0624.

Co-authored-by: Doug Kearns <dougkearns@gmail.com>
This commit is contained in:
zeertzjq
2025-08-09 07:32:55 +08:00
parent a0d94ac469
commit 70bb7999f7
4 changed files with 79 additions and 2 deletions

View File

@@ -3025,13 +3025,16 @@ static void append_command(const char *cmd)
} }
/// Return true and set "*idx" if "p" points to a one letter command. /// Return true and set "*idx" if "p" points to a one letter command.
/// - The 'k' command can directly be followed by any character. /// - The 'k' command can directly be followed by any character
/// but :keepa[lt] is another command, as are :keepj[umps],
/// :kee[pmarks] and :keepp[atterns].
/// - The 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r' /// - The 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
/// but :sre[wind] is another command, as are :scr[iptnames], /// but :sre[wind] is another command, as are :scr[iptnames],
/// :scs[cope], :sim[alt], :sig[ns] and :sil[ent]. /// :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
static int one_letter_cmd(const char *p, cmdidx_T *idx) static int one_letter_cmd(const char *p, cmdidx_T *idx)
{ {
if (*p == 'k') { if (p[0] == 'k'
&& (p[1] != 'e' || (p[1] == 'e' && p[2] != 'e'))) {
*idx = CMD_k; *idx = CMD_k;
return true; return true;
} }
@@ -3064,6 +3067,9 @@ char *find_ex_command(exarg_T *eap, int *full)
char *p = eap->cmd; char *p = eap->cmd;
if (one_letter_cmd(p, &eap->cmdidx)) { if (one_letter_cmd(p, &eap->cmdidx)) {
p++; p++;
if (full != NULL) {
*full = true;
}
} else { } else {
while (ASCII_ISALPHA(*p)) { while (ASCII_ISALPHA(*p)) {
p++; p++;

View File

@@ -1244,6 +1244,10 @@ func Test_cmdline_complete_various()
call feedkeys(":ka\<C-A>\<C-B>\"\<CR>", 'xt') call feedkeys(":ka\<C-A>\<C-B>\"\<CR>", 'xt')
call assert_equal("\"ka\<C-A>", @:) call assert_equal("\"ka\<C-A>", @:)
" completion for :keepmarks command
call feedkeys(":kee edi\<C-A>\<C-B>\"\<CR>", 'xt')
call assert_equal("\"kee edit", @:)
" completion for short version of the :s command " completion for short version of the :s command
call feedkeys(":sI \<C-A>\<C-B>\"\<CR>", 'xt') call feedkeys(":sI \<C-A>\<C-B>\"\<CR>", 'xt')
call assert_equal("\"sI \<C-A>", @:) call assert_equal("\"sI \<C-A>", @:)
@@ -4350,6 +4354,28 @@ func Test_term_option()
let &cpo = _cpo let &cpo = _cpo
endfunc endfunc
func Test_ex_command_completion()
" required for :*
" set cpo+=*
let list = filter(getcompletion('', 'command'), 'exists(":" . v:val) == 0')
" :++ and :-- are only valid in Vim9 script context, so they can be ignored
" call assert_equal(['++', '--'], sort(list))
call assert_equal([], sort(list))
call assert_equal(2, exists(':k'))
call assert_equal(0, exists(':ke'))
call assert_equal(1, exists(':kee'))
call assert_equal(1, exists(':keep'))
call assert_equal(1, exists(':keepm'))
call assert_equal(1, exists(':keepma'))
call assert_equal(1, exists(':keepmar'))
call assert_equal(1, exists(':keepmark'))
call assert_equal(2, exists(':keepmarks'))
call assert_equal(2, exists(':keepalt'))
call assert_equal(2, exists(':keepjumps'))
call assert_equal(2, exists(':keeppatterns'))
set cpo-=*
endfunc
func Test_cd_bslash_completion_windows() func Test_cd_bslash_completion_windows()
CheckMSWindows CheckMSWindows
let save_shellslash = &shellslash let save_shellslash = &shellslash

View File

@@ -0,0 +1,38 @@
" Test for all command modifiers in
func Test_keep_cmdmods_names()
call assert_equal('k', fullcommand(':k'))
call assert_equal('k', fullcommand(':ke'))
call assert_equal('keepmarks', fullcommand(':kee'))
call assert_equal('keepmarks', fullcommand(':keep'))
call assert_equal('keepmarks', fullcommand(':keepm'))
call assert_equal('keepmarks', fullcommand(':keepma'))
call assert_equal('keepmarks', fullcommand(':keepmar'))
call assert_equal('keepmarks', fullcommand(':keepmark'))
call assert_equal('keepmarks', fullcommand(':keepmarks'))
call assert_equal('keepalt', fullcommand(':keepa'))
call assert_equal('keepalt', fullcommand(':keepal'))
call assert_equal('keepalt', fullcommand(':keepalt'))
call assert_equal('keepjumps', fullcommand(':keepj'))
call assert_equal('keepjumps', fullcommand(':keepju'))
call assert_equal('keepjumps', fullcommand(':keepjum'))
call assert_equal('keepjumps', fullcommand(':keepjump'))
call assert_equal('keepjumps', fullcommand(':keepjumps'))
call assert_equal('keeppatterns', fullcommand(':keepp'))
call assert_equal('keeppatterns', fullcommand(':keeppa'))
call assert_equal('keeppatterns', fullcommand(':keeppat'))
call assert_equal('keeppatterns', fullcommand(':keeppatt'))
call assert_equal('keeppatterns', fullcommand(':keeppatte'))
call assert_equal('keeppatterns', fullcommand(':keeppatter'))
call assert_equal('keeppatterns', fullcommand(':keeppattern'))
call assert_equal('keeppatterns', fullcommand(':keeppatterns'))
endfunc
func Test_cmdmod_completion()
call assert_equal('edit', getcompletion('keepalt ed', 'cmdline')[0])
call assert_equal('edit', getcompletion('keepjumps ed', 'cmdline')[0])
call assert_equal('edit', getcompletion('keepmarks ed', 'cmdline')[0])
call assert_equal('edit', getcompletion('keeppatterns ed', 'cmdline')[0])
endfunc
" vim: shiftwidth=2 sts=2 expandtab

View File

@@ -105,6 +105,13 @@ func Test_exists()
" Internal command with a count " Internal command with a count
call assert_equal(0, exists(':3buffer')) call assert_equal(0, exists(':3buffer'))
" Valid internal command (full match)
call assert_equal(2, exists(':k'))
" Non-existing internal command (':k' with arg 'e')
call assert_equal(0, exists(':ke'))
" Valid internal command (partial match)
call assert_equal(1, exists(':kee'))
" User defined command (full match) " User defined command (full match)
command! MyCmd :echo 'My command' command! MyCmd :echo 'My command'
call assert_equal(2, exists(':MyCmd')) call assert_equal(2, exists(':MyCmd'))