Skip to content

Commit fa6fd41

Browse files
girishjichrisbra
authored andcommitted
patch 9.1.1742: complete: preinsert does not work well with preinsert
Problem: complete: preinsert does not work well with preinsert Solution: Make "preinsert" completeopt value work with autocompletion (Girish Palya) This change extends Insert mode autocompletion so that 'preinsert' also works when 'autocomplete' is enabled. Try: `:set ac cot=preinsert` See `:help 'cot'` for more details. closes: #18213 Signed-off-by: Girish Palya <[email protected]> Signed-off-by: Christian Brabandt <[email protected]>
1 parent 4ed19fd commit fa6fd41

14 files changed

Lines changed: 363 additions & 49 deletions

File tree

runtime/doc/insert.txt

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*insert.txt* For Vim version 9.1. Last change: 2025 Aug 25
1+
*insert.txt* For Vim version 9.1. Last change: 2025 Sep 08
22

33

44
VIM REFERENCE MANUAL by Bram Moolenaar
@@ -1150,7 +1150,26 @@ any of them at any time by typing |CTRL-X|, which temporarily suspends
11501150
autocompletion. To use |i_CTRL-N| or |i_CTRL-X_CTRL-N| specifically, press
11511151
|CTRL-E| first to dismiss the popup menu (see |complete_CTRL-E|).
11521152

1153-
See also 'autocomplete', 'autocompletetimeout' and 'autocompletedelay'.
1153+
*ins-autocompletion-example*
1154+
Example setup~
1155+
A typical configuration for automatic completion with a popup menu: >
1156+
set autocomplete
1157+
set complete=.^5,w^5,b^5,u^5
1158+
set completeopt=popup
1159+
1160+
inoremap <silent><expr> <Tab> pumvisible() ? "\<C-n>" : "\<Tab>"
1161+
inoremap <silent><expr> <S-Tab> pumvisible() ? "\<C-p>" : "\<S-Tab>"
1162+
<
1163+
This enables automatic completion with suggestions from the current buffer,
1164+
other windows, and listed buffers, displayed in a popup menu. Each source is
1165+
limited to 5 candidates. <Tab> and <S-Tab> move through the items when the
1166+
menu is visible. Optionally, add "preinsert" to 'completeopt' to insert the
1167+
longest common prefix automatically. You can also add other completion
1168+
sources to 'complete' as needed.
1169+
1170+
See also 'autocomplete', 'autocompletedelay' and 'autocompletetimeout'.
1171+
1172+
For command-line autocompletion, see |cmdline-autocompletion|.
11541173

11551174

11561175
FUNCTIONS FOR FINDING COMPLETIONS *complete-functions*

runtime/doc/options.txt

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*options.txt* For Vim version 9.1. Last change: 2025 Sep 02
1+
*options.txt* For Vim version 9.1. Last change: 2025 Sep 08
22

33

44
VIM REFERENCE MANUAL by Bram Moolenaar
@@ -2124,10 +2124,11 @@ A jump table for the options with a short description can be found at |Q_op|.
21242124
*'complete'* *'cpt'* *E535*
21252125
'complete' 'cpt' string (default: ".,w,b,u,t,i")
21262126
local to buffer
2127-
This option specifies how keyword completion |ins-completion| works
2128-
when CTRL-P or CTRL-N are used. It is also used for whole-line
2129-
completion |i_CTRL-X_CTRL-L|. It indicates the type of completion
2130-
and the places to scan. It is a comma-separated list of flags:
2127+
This option controls how completion |ins-completion| behaves when
2128+
using CTRL-P, CTRL-N, or |ins-autocompletion|. It is also used for
2129+
whole-line completion |i_CTRL-X_CTRL-L|. It indicates the type of
2130+
completion and the places to scan. It is a comma-separated list of
2131+
flags:
21312132
. scan the current buffer ('wrapscan' is ignored)
21322133
w scan buffers from other windows
21332134
b scan other loaded buffers that are in the buffer list
@@ -2179,9 +2180,11 @@ A jump table for the options with a short description can be found at |Q_op|.
21792180
5. tags
21802181
6. included files
21812182

2182-
As you can see, CTRL-N and CTRL-P can be used to do any 'iskeyword'-
2183-
based expansion (e.g., dictionary |i_CTRL-X_CTRL-K|, included patterns
2184-
|i_CTRL-X_CTRL-I|, tags |i_CTRL-X_CTRL-]| and normal expansions).
2183+
CTRL-N, CTRL-P, and |ins-autocompletion| can be used for any
2184+
'iskeyword'-based completion (dictionary |i_CTRL-X_CTRL-K|, included
2185+
patterns |i_CTRL-X_CTRL-I|, tags |i_CTRL-X_CTRL-]|, and normal
2186+
expansions). With the "F" and "o" flags in 'complete', non-keywords
2187+
can also be completed.
21852188

21862189
An optional match limit can be specified for a completion source by
21872190
appending a caret ("^") followed by a {count} to the source flag.
@@ -2298,17 +2301,22 @@ A jump table for the options with a short description can be found at |Q_op|.
22982301
{only works when compiled with the |+textprop| feature}
22992302

23002303
preinsert
2301-
Preinsert the portion of the first candidate word that is
2302-
not part of the current completion leader and using the
2303-
|hl-ComplMatchIns| highlight group. In order for it to
2304-
work, "fuzzy" must not be set and "menuone" must be set.
2304+
When autocompletion is not enabled, inserts the part of the
2305+
first candidate word beyond the current completion leader,
2306+
highlighted with |hl-ComplMatchIns|. The cursor does not
2307+
move. Requires 'fuzzy' unset and 'menuone' in 'completeopt'.
2308+
2309+
When 'autocomplete' is enabled, inserts the longest common
2310+
prefix of matches (from all shown items or buffer-specific
2311+
matches), highlighted with |hl-PreInsert|. This occurs only
2312+
when no menu item is selected. Press CTRL-Y to accept.
23052313

23062314
preview Show extra information about the currently selected
23072315
completion in the preview window. Only works in
23082316
combination with "menu" or "menuone".
23092317

2310-
Only "fuzzy", "popup", "popuphidden" and "preview" have an effect when
2311-
'autocomplete' is enabled.
2318+
Only "fuzzy", "popup", "popuphidden", "preinsert" and "preview" have
2319+
an effect when 'autocomplete' is enabled.
23122320

23132321
This option does not apply to |cmdline-completion|. See 'wildoptions'
23142322
for that.
@@ -4654,7 +4662,7 @@ A jump table for the options with a short description can be found at |Q_op|.
46544662
#:TabLineSel,_:TabLineFill,!:CursorColumn,
46554663
.:CursorLine,o:ColorColumn,q:QuickFixLine,
46564664
z:StatusLineTerm,Z:StatusLineTermNC,
4657-
g:MsgArea,h:ComplMatchIns")
4665+
g:MsgArea,h:ComplMatchIns,I:PreInsert")
46584666
global
46594667
This option can be used to set highlighting mode for various
46604668
occasions. It is a comma-separated list of character pairs. The
@@ -4717,6 +4725,7 @@ A jump table for the options with a short description can be found at |Q_op|.
47174725
|hl-PmenuThumb| X popup menu scrollbar thumb
47184726
|hl-PmenuMatch| k popup menu matched text
47194727
|hl-PmenuMatchSel| < popup menu matched text in selected line
4728+
|hl-PreInsert| I text inserted when "preinsert" and 'autocomplete'
47204729

47214730
The display modes are:
47224731
r reverse (termcap entry "mr" and "me")

runtime/doc/syntax.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*syntax.txt* For Vim version 9.1. Last change: 2025 Aug 18
1+
*syntax.txt* For Vim version 9.1. Last change: 2025 Sep 08
22

33

44
VIM REFERENCE MANUAL by Bram Moolenaar
@@ -5998,6 +5998,8 @@ PmenuMatchSel Popup menu: Matched text in selected item. Applied in
59985998
combination with |hl-PmenuSel|.
59995999
*hl-ComplMatchIns*
60006000
ComplMatchIns Matched text of the currently inserted completion.
6001+
*hl-PreInsert*
6002+
PreInsert Text inserted during autocompletion when "preinsert".
60016003
*hl-PopupSelected*
60026004
PopupSelected Popup window created with |popup_menu()|. Linked to
60036005
|hl-PmenuSel| by default.

runtime/doc/tags

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8366,6 +8366,7 @@ hl-PmenuSel syntax.txt /*hl-PmenuSel*
83668366
hl-PmenuThumb syntax.txt /*hl-PmenuThumb*
83678367
hl-PopupNotification syntax.txt /*hl-PopupNotification*
83688368
hl-PopupSelected syntax.txt /*hl-PopupSelected*
8369+
hl-PreInsert syntax.txt /*hl-PreInsert*
83698370
hl-Question syntax.txt /*hl-Question*
83708371
hl-QuickFixLine syntax.txt /*hl-QuickFixLine*
83718372
hl-Scrollbar syntax.txt /*hl-Scrollbar*
@@ -8627,6 +8628,7 @@ inputrestore() builtin.txt /*inputrestore()*
86278628
inputsave() builtin.txt /*inputsave()*
86288629
inputsecret() builtin.txt /*inputsecret()*
86298630
ins-autocompletion insert.txt /*ins-autocompletion*
8631+
ins-autocompletion-example insert.txt /*ins-autocompletion-example*
86308632
ins-completion insert.txt /*ins-completion*
86318633
ins-completion-menu insert.txt /*ins-completion-menu*
86328634
ins-expandtab insert.txt /*ins-expandtab*

runtime/syntax/vim.vim

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
" Language: Vim script
33
" Maintainer: Hirohito Higashi <h.east.727 ATMARK gmail.com>
44
" Doug Kearns <[email protected]>
5-
" Last Change: 2025 Aug 29
5+
" Last Change: 2025 Sep 08
66
" Former Maintainer: Charles E. Campbell
77

88
" DO NOT CHANGE DIRECTLY.
@@ -145,8 +145,8 @@ syn keyword vimGroup contained Added Bold BoldItalic Boolean Changed Character C
145145

146146
" Default highlighting groups {{{2
147147
" GEN_SYN_VIM: vimHLGroup, START_STR='syn keyword vimHLGroup contained', END_STR=''
148-
syn keyword vimHLGroup contained ErrorMsg IncSearch ModeMsg NonText StatusLine StatusLineNC EndOfBuffer VertSplit VisualNOS DiffText DiffTextAdd PmenuSbar TabLineSel TabLineFill TabPanel TabPanelSel TabPanelFill Cursor lCursor QuickFixLine CursorLineSign CursorLineFold CurSearch PmenuKind PmenuKindSel PmenuMatch PmenuMatchSel PmenuExtra PmenuExtraSel PopupSelected MessageWindow PopupNotification Normal Directory LineNr CursorLineNr MoreMsg Question Search SpellBad SpellCap SpellRare SpellLocal PmenuThumb Pmenu PmenuSel SpecialKey Title WarningMsg WildMenu Folded FoldColumn SignColumn Visual DiffAdd DiffChange DiffDelete TabLine CursorColumn CursorLine ColorColumn MatchParen StatusLineTerm StatusLineTermNC ToolbarLine ToolbarButton Menu Tooltip Scrollbar CursorIM
149-
syn keyword vimHLGroup contained ComplMatchIns LineNrAbove LineNrBelow MsgArea Terminal User1 User2 User3 User4 User5 User6 User7 User8 User9
148+
syn keyword vimHLGroup contained ErrorMsg IncSearch ModeMsg NonText StatusLine StatusLineNC EndOfBuffer VertSplit VisualNOS DiffText DiffTextAdd PmenuSbar TabLineSel TabLineFill TabPanel TabPanelSel TabPanelFill Cursor lCursor QuickFixLine CursorLineSign CursorLineFold CurSearch PmenuKind PmenuKindSel PmenuMatch PmenuMatchSel PmenuExtra PmenuExtraSel PopupSelected MessageWindow PopupNotification PreInsert Normal Directory LineNr CursorLineNr MoreMsg Question Search SpellBad SpellCap SpellRare SpellLocal PmenuThumb Pmenu PmenuSel SpecialKey Title WarningMsg WildMenu Folded FoldColumn SignColumn Visual DiffAdd DiffChange DiffDelete TabLine CursorColumn CursorLine ColorColumn MatchParen StatusLineTerm StatusLineTermNC ToolbarLine ToolbarButton Menu Tooltip Scrollbar
149+
syn keyword vimHLGroup contained CursorIM ComplMatchIns LineNrAbove LineNrBelow MsgArea Terminal User1 User2 User3 User4 User5 User6 User7 User8 User9
150150
syn match vimHLGroup contained "\<Conceal\>"
151151
syn case match
152152

src/edit.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -693,7 +693,11 @@ edit(
693693
&& stop_arrow() == OK)
694694
{
695695
ins_compl_delete();
696-
ins_compl_insert(FALSE);
696+
if (ins_compl_has_preinsert()
697+
&& ins_compl_has_autocomplete())
698+
(void)ins_compl_insert(FALSE, TRUE);
699+
else
700+
(void)ins_compl_insert(FALSE, FALSE);
697701
}
698702
// Delete preinserted text when typing special chars
699703
else if (IS_WHITE_NL_OR_NUL(c) && ins_compl_preinsert_effect())

src/highlight.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ static char *(highlight_init_both[]) = {
269269
"default link PopupSelected PmenuSel",
270270
"default link MessageWindow WarningMsg",
271271
"default link PopupNotification WarningMsg",
272+
"default link PreInsert Added",
272273
CENT("Normal cterm=NONE", "Normal gui=NONE"),
273274
NULL
274275
};

0 commit comments

Comments
 (0)