patch 9.1.1797: completion: autocompletion can be improved

Problem:  completion: autocompletion can be improved
Solution: Add support for "longest" and "preinsert" in 'autocomplete';
          add preinserted() (Girish Palya)

* Add support for "longest" in 'completeopt' when 'autocomplete'
  is enabled. (Note: the cursor position does not change automatically
  when 'autocomplete' is enabled.)
* Add support for "preinsert" when 'autocomplete' is enabled. Ensure
  "preinsert" works the same with and without 'autocomplete'
* introduce the preinserted() Vim script function, useful for defining
  custom key mappings.

fixes: #18314
closes: #18387

Signed-off-by: Girish Palya <girishji@gmail.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
This commit is contained in:
Girish Palya
2025-09-26 17:29:38 +00:00
committed by Christian Brabandt
parent 3fc1f2a00e
commit c05335082a
13 changed files with 336 additions and 114 deletions

View File

@ -453,6 +453,7 @@ popup_setoptions({id}, {options})
popup_settext({id}, {text}) none set the text of popup window {id}
popup_show({id}) Number unhide popup window {id}
pow({x}, {y}) Float {x} to the power of {y}
preinserted() Number whether text is inserted after cursor
prevnonblank({lnum}) Number line nr of non-blank line <= {lnum}
printf({fmt}, {expr1}...) String format text
prompt_getprompt({buf}) String get prompt text
@ -8033,6 +8034,15 @@ pow({x}, {y}) *pow()*
Return type: |Number|
preinserted() *preinserted()*
Returns non-zero if text has been inserted after the cursor
because "preinsert" is present in 'completeopt', or if
"longest" is present in 'completeopt' while 'autocomplete'
is enabled. Otherwise returns zero.
Return type: |Number|
prevnonblank({lnum}) *prevnonblank()*
Return the line number of the first line at or above {lnum}
that is not blank. Example: >

View File

@ -1,4 +1,4 @@
*options.txt* For Vim version 9.1. Last change: 2025 Sep 22
*options.txt* For Vim version 9.1. Last change: 2025 Sep 26
VIM REFERENCE MANUAL by Bram Moolenaar
@ -2256,11 +2256,22 @@ A jump table for the options with a short description can be found at |Q_op|.
to gather more alternatives for your candidate list,
see 'completefuzzycollect'.
longest Only insert the longest common text of the matches. If
the menu is displayed you can use CTRL-L to add more
characters. Whether case is ignored depends on the kind
of completion. For buffer text the 'ignorecase' option is
used.
longest
When 'autocomplete' is not active, only the longest
common prefix of the matches is inserted. If the popup
menu is displayed, you can use CTRL-L to add more
characters. Whether case is ignored depends on the type
of completion. For buffer text the 'ignorecase' option
applies.
When 'autocomplete' is active and no completion item is
selected, the longest common prefix of the matches is
inserted after the cursor. The prefix is taken either
from all displayed items or only from items in the current
buffer. The inserted text is highlighted with
|hl-PreInsert|, and the cursor position does not change
(similar to `"preinsert"`). Press CTRL-Y to accept.
See also |preinserted()|.
menu Use a popup menu to show the possible completions. The
menu is only shown when there is more than one match and
@ -2302,22 +2313,21 @@ A jump table for the options with a short description can be found at |Q_op|.
{only works when compiled with the |+textprop| feature}
preinsert
When 'autocomplete' is not active, inserts the part of the
first candidate word beyond the current completion leader,
highlighted with |hl-PreInsert|. The cursor doesn't move.
Requires "fuzzy" unset and "menuone" in 'completeopt'.
When 'autocomplete' is active, inserts the longest common
prefix of matches (from all shown items or from the
current buffer items). This occurs only when no menu item
is selected. Press CTRL-Y to accept.
Inserts the text of the first completion candidate
beyond the current leader, highlighted with |hl-PreInsert|.
The cursor does not move.
Requires "fuzzy" to be unset, and either "menuone" in
'completeopt' or 'autocomplete' enabled. When
'autocomplete' is enabled, this does not work if
'ignorecase' is set without 'infercase'.
See also |preinserted()|.
preview Show extra information about the currently selected
completion in the preview window. Only works in
combination with "menu" or "menuone".
Only "fuzzy", "popup", "popuphidden", "preinsert" and "preview" have
an effect when 'autocomplete' is enabled.
Only "fuzzy", "longest", "popup", "popuphidden", "preinsert" and
"preview" have an effect when 'autocomplete' is enabled.
This option does not apply to |cmdline-completion|. See 'wildoptions'
for that.

View File

@ -9774,6 +9774,7 @@ postscript-print-util print.txt /*postscript-print-util*
postscript-printing print.txt /*postscript-printing*
pow() builtin.txt /*pow()*
ppwiz.vim syntax.txt /*ppwiz.vim*
preinserted() builtin.txt /*preinserted()*
press-enter message.txt /*press-enter*
press-return message.txt /*press-return*
prevcount-variable eval.txt /*prevcount-variable*

View File

@ -1,4 +1,4 @@
*usr_41.txt* For Vim version 9.1. Last change: 2025 Aug 23
*usr_41.txt* For Vim version 9.1. Last change: 2025 Sep 26
VIM USER MANUAL - by Bram Moolenaar
@ -1135,6 +1135,7 @@ Insert mode completion: *completion-functions*
complete_info() get current completion information
complete_match() get insert completion start match col and
trigger text
preinserted() check if text is inserted after cursor
pumvisible() check if the popup menu is displayed
pum_getpos() position and size of popup menu if visible

View File

@ -1,4 +1,4 @@
*version9.txt* For Vim version 9.1. Last change: 2025 Sep 22
*version9.txt* For Vim version 9.1. Last change: 2025 Sep 26
VIM REFERENCE MANUAL by Bram Moolenaar
@ -41803,6 +41803,8 @@ Functions: ~
|matchstrlist()| all the matches of a pattern in a List of strings
|ngettext()| lookup single/plural message translation
|popup_setbuf()| switch to a different buffer in a popup
|preinserted()| whether preinserted text has been inserted during
completion (see 'completeopt')
|str2blob()| convert a List of strings into a blob
|test_null_tuple()| return a null tuple
|tuple2list()| turn a Tuple of items into a List

View File

@ -156,7 +156,7 @@ syn keyword vimFuncName contained abs acos add and append appendbufline argc arg
syn keyword vimFuncName contained char2nr charclass charcol charidx chdir cindent clearmatches cmdcomplete_info col complete complete_add complete_check complete_info complete_match confirm copy cos cosh count cscope_connection cursor debugbreak deepcopy delete deletebufline did_filetype diff diff_filler diff_hlID digraph_get digraph_getlist digraph_set digraph_setlist echoraw empty environ err_teapot escape eval eventhandler executable execute exepath exists exists_compiled exp expand expandcmd extend extendnew feedkeys filecopy filereadable filewritable filter finddir findfile flatten flattennew float2nr floor fmod fnameescape fnamemodify foldclosed foldclosedend foldlevel foldtext foldtextresult foreach foreground fullcommand funcref function garbagecollect
syn keyword vimFuncName contained get getbufinfo getbufline getbufoneline getbufvar getcellpixels getcellwidths getchangelist getchar getcharmod getcharpos getcharsearch getcharstr getcmdcomplpat getcmdcompltype getcmdline getcmdpos getcmdprompt getcmdscreenpos getcmdtype getcmdwintype getcompletion getcompletiontype getcurpos getcursorcharpos getcwd getenv getfontname getfperm getfsize getftime getftype getimstatus getjumplist getline getloclist getmarklist getmatches getmousepos getmouseshape getpid getpos getqflist getreg getreginfo getregion getregionpos getregtype getscriptinfo getstacktrace gettabinfo gettabvar gettabwinvar gettagstack gettext getwininfo getwinpos getwinposx getwinposy getwinvar glob glob2regpat globpath has has_key haslocaldir hasmapto
syn keyword vimFuncName contained histadd histdel histget histnr hlID hlexists hlget hlset hostname iconv id indent index indexof input inputdialog inputlist inputrestore inputsave inputsecret insert instanceof interrupt invert isabsolutepath isdirectory isinf islocked isnan items job_getchannel job_info job_setoptions job_start job_status job_stop join js_decode js_encode json_decode json_encode keys keytrans len libcall libcallnr line line2byte lispindent list2blob list2str list2tuple listener_add listener_flush listener_remove localtime log log10 luaeval map maparg mapcheck maplist mapnew mapset match matchadd matchaddpos matcharg matchbufline matchdelete matchend matchfuzzy matchfuzzypos matchlist matchstr matchstrlist matchstrpos max menu_info min mkdir mode
syn keyword vimFuncName contained mzeval nextnonblank ngettext nr2char or pathshorten perleval popup_atcursor popup_beval popup_clear popup_close popup_create popup_dialog popup_filter_menu popup_filter_yesno popup_findecho popup_findinfo popup_findpreview popup_getoptions popup_getpos popup_hide popup_list popup_locate popup_menu popup_move popup_notification popup_setbuf popup_setoptions popup_settext popup_show pow prevnonblank printf prompt_getprompt prompt_setcallback prompt_setinterrupt prompt_setprompt prop_add prop_add_list prop_clear prop_find prop_list prop_remove prop_type_add prop_type_change prop_type_delete prop_type_get prop_type_list pum_getpos pumvisible py3eval pyeval pyxeval rand range readblob readdir readdirex readfile reduce reg_executing
syn keyword vimFuncName contained mzeval nextnonblank ngettext nr2char or pathshorten perleval popup_atcursor popup_beval popup_clear popup_close popup_create popup_dialog popup_filter_menu popup_filter_yesno popup_findecho popup_findinfo popup_findpreview popup_getoptions popup_getpos popup_hide popup_list popup_locate popup_menu popup_move popup_notification popup_setbuf popup_setoptions popup_settext popup_show pow preinserted prevnonblank printf prompt_getprompt prompt_setcallback prompt_setinterrupt prompt_setprompt prop_add prop_add_list prop_clear prop_find prop_list prop_remove prop_type_add prop_type_change prop_type_delete prop_type_get prop_type_list pum_getpos pumvisible py3eval pyeval pyxeval rand range readblob readdir readdirex readfile reduce reg_executing
syn keyword vimFuncName contained reg_recording reltime reltimefloat reltimestr remote_expr remote_foreground remote_peek remote_read remote_send remote_startserver remove rename repeat resolve reverse round rubyeval screenattr screenchar screenchars screencol screenpos screenrow screenstring search searchcount searchdecl searchpair searchpairpos searchpos server2client serverlist setbufline setbufvar setcellwidths setcharpos setcharsearch setcmdline setcmdpos setcursorcharpos setenv setfperm setline setloclist setmatches setpos setqflist setreg settabvar settabwinvar settagstack setwinvar sha256 shellescape shiftwidth sign_define sign_getdefined sign_getplaced sign_jump sign_place sign_placelist sign_undefine sign_unplace sign_unplacelist simplify sin sinh slice
syn keyword vimFuncName contained sort sound_clear sound_playevent sound_playfile sound_stop soundfold spellbadword spellsuggest split sqrt srand state str2blob str2float str2list str2nr strcharlen strcharpart strchars strdisplaywidth strftime strgetchar stridx string strlen strpart strptime strridx strtrans strutf16len strwidth submatch substitute swapfilelist swapinfo swapname synID synIDattr synIDtrans synconcealed synstack system systemlist tabpagebuflist tabpagenr tabpagewinnr tagfiles taglist tan tanh tempname term_dumpdiff term_dumpload term_dumpwrite term_getaltscreen term_getansicolors term_getattr term_getcursor term_getjob term_getline term_getscrolled term_getsize term_getstatus term_gettitle term_gettty term_list term_scrape term_sendkeys term_setansicolors
syn keyword vimFuncName contained term_setapi term_setkill term_setrestore term_setsize term_start term_wait terminalprops test_alloc_fail test_autochdir test_feedinput test_garbagecollect_now test_garbagecollect_soon test_getvalue test_gui_event test_ignore_error test_mswin_event test_null_blob test_null_channel test_null_dict test_null_function test_null_job test_null_list test_null_partial test_null_string test_null_tuple test_option_not_set test_override test_refcount test_setmouse test_settime test_srand_seed test_unknown test_void timer_info timer_pause timer_start timer_stop timer_stopall tolower toupper tr trim trunc tuple2list type typename undofile undotree uniq uri_decode uri_encode utf16idx values virtcol virtcol2col visualmode wildmenumode wildtrigger