forked from rafi/vim-config
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathactionmenu.vim
More file actions
110 lines (95 loc) · 2.64 KB
/
actionmenu.vim
File metadata and controls
110 lines (95 loc) · 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
" actionmenu
" ---
" Forked from: https://github.com/kizza/actionmenu.nvim
" Style the buffer every time
setlocal sidescrolloff=0 scrolloff=0 nowrap
setlocal signcolumn=no listchars= showbreak=
let b:cursorword = 0
" Only load ftplugin once
if get(s:, 'loaded')
" Subsequent times, just open pum
call actionmenu#open_pum()
finish
endif
let s:loaded = 1
" Current menu selection
let s:selected_item = 0
" Trigger InsertEnter only the first time menu loads.
" This is to allow any lazy-loading insert mode plugins beforehand.
doautocmd <nomodeline> InsertEnter
function! actionmenu#open_pum()
call feedkeys("i\<C-x>\<C-u>")
endfunction
function! actionmenu#select_item()
if pumvisible()
if ! empty(v:completed_item)
let s:selected_item = copy(v:completed_item)
endif
" Close pum and leave insert
return "\<C-y>\<Esc>"
endif
" Leave insert mode
return "\<Esc>"
endfunction
function! actionmenu#on_insert_leave()
call actionmenu#close()
let l:index = -1
if type(s:selected_item) == type({})
let l:index = s:selected_item['user_data']
endif
if l:index ==# ''
let l:index = -1
endif
let l:data = l:index > -1 ? g:actionmenu#items[l:index] : {}
let s:selected_item = 0
let g:actionmenu#items = []
let g:actionmenu#selected = [l:index, l:data]
call actionmenu#callback(l:index, l:data)
endfunction
function! actionmenu#pum_parse_item(item, index) abort
if type(a:item) == type('')
return { 'word': a:item, 'user_data': a:index }
else
return { 'word': a:item['word'], 'user_data': a:index }
endif
endfunction
" Menu mappings
mapclear <buffer>
imapclear <buffer>
inoremap <silent><buffer><expr> <CR> actionmenu#select_item()
imap <buffer> <C-y> <CR>
imap <buffer> <C-e> <Esc>
" Navigate in menu
inoremap <buffer> <Up> <C-p>
inoremap <buffer> <Down> <C-n>
inoremap <buffer> k <C-p>
inoremap <buffer> j <C-n>
imap <buffer> <C-k> <C-p>
imap <buffer> <C-j> <C-n>
inoremap <buffer> <S-Tab> <C-p>
imap <buffer> <Tab> <C-n>
" Scroll pages in menu
inoremap <buffer> <C-b> <PageUp>
inoremap <buffer> <C-f> <PageDown>
imap <buffer> <C-u> <PageUp>
imap <buffer> <C-d> <PageDown>
" Events
augroup actionmenu
autocmd!
autocmd InsertLeave <buffer> call actionmenu#on_insert_leave()
augroup END
" Pum completion function
function! actionmenu#complete_func(findstart, base)
if a:findstart
return 1
else
return map(copy(g:actionmenu#items), {
\ index, item -> actionmenu#pum_parse_item(item, index) })
endif
endfunction
" Set the pum completion function
setlocal completefunc=actionmenu#complete_func
setlocal completeopt+=menuone
setlocal completeopt+=noinsert
" Open the pum immediately
call actionmenu#open_pum()