Skip to content

Commit fef6764

Browse files
author
skywind3000
committed
context menu: can save last position and can filter with filetypes
1 parent 370e17d commit fef6764

File tree

1 file changed

+71
-5
lines changed

1 file changed

+71
-5
lines changed

autoload/quickui/context.vim

Lines changed: 71 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
" last position
1717
let g:quickui#context#cursor = -1
1818

19+
" save positions
20+
let s:previous_cursor = {}
21+
1922

2023
"----------------------------------------------------------------------
2124
" compile
@@ -90,7 +93,6 @@ function! quickui#context#compile(items, border)
9093
endfunc
9194

9295

93-
9496
"----------------------------------------------------------------------
9597
" create menu object
9698
"----------------------------------------------------------------------
@@ -103,6 +105,11 @@ function! s:vim_create_context(textlist, opts)
103105
let hwnd.winid = winid
104106
let hwnd.index = get(a:opts, 'index', -1)
105107
let hwnd.opts = deepcopy(a:opts)
108+
if has_key(hwnd.opts, 'savepos')
109+
if has_key(s:previous_cursor, hwnd.opts.savepos)
110+
let hwnd.index = get(s:previous_cursor, hwnd.opts.savepos, 0)
111+
endif
112+
endif
106113
let opts = {'minwidth':w, 'maxwidth':w, 'minheight':h, 'maxheight':h}
107114
if has_key(a:opts, 'line') && has_key(a:opts, 'col')
108115
let opts.line = a:opts.line
@@ -266,6 +273,9 @@ function! s:popup_exit(winid, code)
266273
let g:quickui#context#current = hwnd
267274
let g:quickui#context#cursor = hwnd.index
268275
let redrawed = 0
276+
if has_key(hwnd.opts, 'savepos')
277+
let s:previous_cursor[hwnd.opts.savepos] = hwnd.index
278+
endif
269279
if has_key(hwnd.opts, 'callback')
270280
let F = function(hwnd.opts.callback)
271281
call F(code)
@@ -491,6 +501,11 @@ function! s:nvim_create_context(textlist, opts)
491501
let h = hwnd.height
492502
let hwnd.index = get(a:opts, 'index', -1)
493503
let hwnd.opts = deepcopy(a:opts)
504+
if has_key(hwnd.opts, 'savepos')
505+
if has_key(s:previous_cursor, hwnd.opts.savepos)
506+
let hwnd.index = get(s:previous_cursor, hwnd.opts.savepos, 0)
507+
endif
508+
endif
494509
let opts = {'width':w, 'height':h, 'focusable':1, 'style':'minimal'}
495510
let opts.relative = 'editor'
496511
if has_key(a:opts, 'line') && has_key(a:opts, 'col')
@@ -603,6 +618,9 @@ function! s:nvim_create_context(textlist, opts)
603618
let g:quickui#context#code = retval
604619
let g:quickui#context#current = hwnd
605620
let g:quickui#context#cursor = hwnd.index
621+
if has_key(hwnd.opts, 'savepos')
622+
let s:previous_cursor[hwnd.opts.savepos] = hwnd.index
623+
endif
606624
if has_key(hwnd.opts, 'callback')
607625
let F = function(hwnd.opts.callback)
608626
call F(retval)
@@ -620,19 +638,64 @@ function! s:nvim_create_context(textlist, opts)
620638
endfunc
621639

622640

641+
"----------------------------------------------------------------------
642+
" reduce with file types
643+
"----------------------------------------------------------------------
644+
function! s:reduce_items(textlist)
645+
let output = []
646+
let state = 1
647+
let index = 0
648+
let limit = len(a:textlist)
649+
for item in a:textlist
650+
if len(item) > 0
651+
let issep = ((item[0]) =~ '^-\+$')? 1 : 0
652+
if issep != 0
653+
if state == 0
654+
if index + 1 < limit
655+
let output += [item]
656+
let state = 1
657+
endif
658+
endif
659+
else
660+
if type(item) == v:t_string
661+
let output += [item]
662+
let state = 0
663+
elseif len(item) < 4
664+
let output += [item]
665+
let state = 0
666+
else
667+
for check in split(item[3], ',')
668+
if &ft == check
669+
let output += [item]
670+
let state = 0
671+
break
672+
endif
673+
endfor
674+
endif
675+
endif
676+
endif
677+
let index += 1
678+
endfor
679+
return output
680+
endfunc
681+
682+
623683
"----------------------------------------------------------------------
624684
" create menu object
625685
"----------------------------------------------------------------------
626686
function! quickui#context#open(textlist, opts)
687+
let textlist = a:textlist
688+
if get(a:opts, 'reduce', 0) != 0
689+
let textlist = s:reduce_items(a:textlist)
690+
endif
627691
if g:quickui#core#has_nvim == 0
628-
return s:vim_create_context(a:textlist, a:opts)
692+
return s:vim_create_context(textlist, a:opts)
629693
else
630-
return s:nvim_create_context(a:textlist, a:opts)
694+
return s:nvim_create_context(textlist, a:opts)
631695
endif
632696
endfunc
633697

634698

635-
636699
"----------------------------------------------------------------------
637700
" testing suit
638701
"----------------------------------------------------------------------
@@ -646,10 +709,11 @@ if 0
646709
\ "&Save\tCtrl+s",
647710
\ "Save &As",
648711
\ "Save All",
649-
\ "-",
712+
\ "--",
650713
\ "&User Menu\tF9",
651714
\ "&Dos Shell",
652715
\ "~&Time %{&undolevels? '+':'-'}",
716+
\ ["S&plit", 'help 1', '', 'vim2'],
653717
\ "--",
654718
\ "E&xit\tAlt+x",
655719
\ "&Help",
@@ -658,7 +722,9 @@ if 0
658722
" let menu = quickui#context#menu_compile(lines, 1)
659723
let opts = {'cursor': -1, 'line2':'cursor+1', 'col2': 'cursor', 'horizon':1}
660724
" let opts.index = 2
725+
let opts.savepos = 'f'
661726
let opts.callback = 'MyCallback'
727+
let opts.reduce = 1
662728
function! MyCallback(code)
663729
echo "callback: " . a:code
664730
endfunc

0 commit comments

Comments
 (0)