diff options
author | Shougo <Shougo.Matsu@gmail.com> | 2022-05-09 13:52:31 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-05-09 12:52:31 +0800 |
commit | dbdd58e548fcf55848359b696275fd848756db7b (patch) | |
tree | 145c279909c7175c6a46ed76591dbebab124a3ba | |
parent | f6be28c61a66df1bd88e5aac3d2c20792c541e18 (diff) | |
download | rneovim-dbdd58e548fcf55848359b696275fd848756db7b.tar.gz rneovim-dbdd58e548fcf55848359b696275fd848756db7b.tar.bz2 rneovim-dbdd58e548fcf55848359b696275fd848756db7b.zip |
feat: cmdline funcs (#18284)
vim-patch:8.2.4903: cannot get the current cmdline completion type and position
Problem: Cannot get the current cmdline completion type and position.
Solution: Add getcmdcompltype() and getcmdscreenpos(). (Shougo Matsushita,
closes vim/vim#10344)
https://github.com/vim/vim/commit/79d599b8772022af1d657f368c2fc97aa342c0da
vim-patch:8.2.4910: imperfect coding
Problem: Imperfect coding.
Solution: Make code nicer.
https://github.com/vim/vim/commit/9ff7d717aa3176de5c61de340deb93f41c7780fc
-rw-r--r-- | runtime/doc/builtin.txt | 20 | ||||
-rw-r--r-- | runtime/doc/usr_41.txt | 4 | ||||
-rw-r--r-- | src/nvim/eval.lua | 2 | ||||
-rw-r--r-- | src/nvim/eval/funcs.c | 13 | ||||
-rw-r--r-- | src/nvim/ex_getln.c | 32 | ||||
-rw-r--r-- | src/nvim/testdir/test_cmdline.vim | 12 |
6 files changed, 82 insertions, 1 deletions
diff --git a/runtime/doc/builtin.txt b/runtime/doc/builtin.txt index a909fd0d6b..b9954dc99c 100644 --- a/runtime/doc/builtin.txt +++ b/runtime/doc/builtin.txt @@ -179,8 +179,12 @@ getcharmod() Number modifiers for the last typed character getcharpos({expr}) List position of cursor, mark, etc. getcharsearch() Dict last character search getcharstr([expr]) String get one character from the user +getcmdcompltype() String return the type of the current + command-line completion getcmdline() String return the current command-line getcmdpos() Number return cursor position in command-line +getcmdscreenpos() Number return cursor screen position in + command-line getcmdtype() String return current command-line type getcmdwintype() String return current command-line window type getcompletion({pat}, {type} [, {filtered}]) @@ -2792,6 +2796,13 @@ getcharstr([expr]) *getcharstr()* Otherwise this works like |getchar()|, except that a number result is converted to a string. +getcmdcompltype() *getcmdcompltype()* + Return the type of the current command-line completion. + Only works when the command line is being edited, thus + requires use of |c_CTRL-\_e| or |c_CTRL-R_=|. + See |command-completion| for the return string. + Also see |getcmdtype()|, |setcmdpos()| and |getcmdline()|. + Returns an empty string when completion is not defined. getcmdline() *getcmdline()* Return the current command-line. Only works when the command @@ -2811,6 +2822,15 @@ getcmdpos() *getcmdpos()* Returns 0 otherwise. Also see |getcmdtype()|, |setcmdpos()| and |getcmdline()|. +getcmdscreenpos() *getcmdscreenpos()* + Return the screen position of the cursor in the command line + as a byte count. The first column is 1. + Instead of |getcmdpos()|, it adds the prompt position. + Only works when editing the command line, thus requires use of + |c_CTRL-\_e| or |c_CTRL-R_=| or an expression mapping. + Returns 0 otherwise. + Also see |getcmdpos()|, |setcmdpos()|. + getcmdtype() *getcmdtype()* Return the current command-line type. Possible return values are: diff --git a/runtime/doc/usr_41.txt b/runtime/doc/usr_41.txt index ff69b8f224..60bcadd582 100644 --- a/runtime/doc/usr_41.txt +++ b/runtime/doc/usr_41.txt @@ -855,8 +855,12 @@ Buffers, windows and the argument list: swapname() get the swap file path of a buffer Command line: *command-line-functions* + getcmdcompltype() get the type of the current command line + completion getcmdline() get the current command line getcmdpos() get position of the cursor in the command line + getcmdscreenpos() get screen position of the cursor in the + command line setcmdpos() set position of the cursor in the command line getcmdtype() return the current command-line type getcmdwintype() return the current command-line window type diff --git a/src/nvim/eval.lua b/src/nvim/eval.lua index 698cffe2fa..d7a17d6e15 100644 --- a/src/nvim/eval.lua +++ b/src/nvim/eval.lua @@ -152,8 +152,10 @@ return { getcharpos={args=1, base=1}, getcharsearch={}, getcharstr={args={0, 1}}, + getcmdcompltype={}, getcmdline={}, getcmdpos={}, + getcmdscreenpos={}, getcmdtype={}, getcmdwintype={}, getcompletion={args={2, 3}, base=1}, diff --git a/src/nvim/eval/funcs.c b/src/nvim/eval/funcs.c index be43c3010a..7946ed75e1 100644 --- a/src/nvim/eval/funcs.c +++ b/src/nvim/eval/funcs.c @@ -3175,6 +3175,13 @@ static void f_getcharsearch(typval_T *argvars, typval_T *rettv, FunPtr fptr) tv_dict_add_nr(dict, S_LEN("until"), last_csearch_until()); } +/// "getcmdcompltype()" function +static void f_getcmdcompltype(typval_T *argvars, typval_T *rettv, FunPtr fptr) +{ + rettv->v_type = VAR_STRING; + rettv->vval.v_string = (char *)get_cmdline_completion(); +} + /// "getcmdline()" function static void f_getcmdline(typval_T *argvars, typval_T *rettv, FunPtr fptr) { @@ -3188,6 +3195,12 @@ static void f_getcmdpos(typval_T *argvars, typval_T *rettv, FunPtr fptr) rettv->vval.v_number = get_cmdline_pos() + 1; } +/// "getcmdscreenpos()" function +static void f_getcmdscreenpos(typval_T *argvars, typval_T *rettv, FunPtr fptr) +{ + rettv->vval.v_number = get_cmdline_screen_pos() + 1; +} + /// "getcmdtype()" function static void f_getcmdtype(typval_T *argvars, typval_T *rettv, FunPtr fptr) { diff --git a/src/nvim/ex_getln.c b/src/nvim/ex_getln.c index d4cea15d20..771e84651f 100644 --- a/src/nvim/ex_getln.c +++ b/src/nvim/ex_getln.c @@ -4683,7 +4683,7 @@ char_u *addstar(char_u *fname, size_t len, int context) * EXPAND_ENV_VARS Complete environment variable names * EXPAND_USER Complete user names */ -static void set_expand_context(expand_T *xp) +void set_expand_context(expand_T *xp) { // only expansion for ':', '>' and '=' command-lines if (ccline.cmdfirstc != ':' @@ -5949,6 +5949,25 @@ static struct cmdline_info *get_ccline_ptr(void) } } +/// Get the current command-line completion type. +char_u *get_cmdline_completion(void) +{ + if (cmdline_star > 0) { + return NULL; + } + struct cmdline_info *p = get_ccline_ptr(); + + if (p != NULL && p->xpc != NULL) { + set_expand_context(p->xpc); + char_u *cmd_compl = get_user_cmd_complete(p->xpc, p->xpc->xp_context); + if (cmd_compl != NULL) { + return vim_strsave(cmd_compl); + } + } + + return NULL; +} + /* * Get the current command line in allocated memory. * Only works when the command line is being edited. @@ -5983,6 +6002,17 @@ int get_cmdline_pos(void) return p->cmdpos; } +/// Get the command line cursor screen position. +int get_cmdline_screen_pos(void) +{ + struct cmdline_info *p = get_ccline_ptr(); + + if (p == NULL) { + return -1; + } + return p->cmdspos; +} + /* * Set the command line byte position to "pos". Zero is the first position. * Only works when the command line is being edited. diff --git a/src/nvim/testdir/test_cmdline.vim b/src/nvim/testdir/test_cmdline.vim index 5c2f6f8d0b..759caac878 100644 --- a/src/nvim/testdir/test_cmdline.vim +++ b/src/nvim/testdir/test_cmdline.vim @@ -1212,4 +1212,16 @@ func Test_recalling_cmdline() cunmap <Plug>(save-cmdline) endfunc +func Check_completion() + call assert_equal('let a', getcmdline()) + call assert_equal(6, getcmdpos()) + call assert_equal(7, getcmdscreenpos()) + call assert_equal('var', getcmdcompltype()) + return '' +endfunc + +func Test_screenpos_and_completion() + call feedkeys(":let a\<C-R>=Check_completion()\<CR>\<Esc>", "xt") +endfunc + " vim: shiftwidth=2 sts=2 expandtab |