33" menu.vim - main menu bar
44"
55" Created by skywind on 2019/12/24
6- " Last Modified: 2019/12/24 10:41:13
6+ " Last Modified: 2019/12/30 01:14
77"
88" ======================================================================
99
1010" vim: set noet fenc = utf- 8 ff = unix sts = 4 sw = 4 ts = 4 :
1111
1212
1313" ----------------------------------------------------------------------
14- " global
14+ " namespace of configuration
1515" ----------------------------------------------------------------------
16- let s: menucfg = {}
17- let s: weight = 100
16+ let s: namespace = { ' system' :{' config' :{}, ' weight' :100 , ' index' :0 } }
17+ let s: name = ' system'
18+
19+
20+ " ----------------------------------------------------------------------
21+ " switch config namespace
22+ " ----------------------------------------------------------------------
23+ function ! quickui#menu#switch (name)
24+ if ! has_key (s: namespace , a: name )
25+ let s: namespace [a: name ] = {}
26+ let s: namespace [a: name ].config = {}
27+ let s: namespace [a: name ].index = 0
28+ let s: namespace [a: name ].weight = 100
29+ endif
30+ let s: name = a: name
31+ endfunc
32+
33+
34+ " ----------------------------------------------------------------------
35+ " clear all entries in current namespace
36+ " ----------------------------------------------------------------------
37+ function ! quickui#menu#reset ()
38+ let current = s: namespace [s: name ].config
39+ let s: namespace [s: name ].weight = 100
40+ let s: namespace [s: name ].index = 0
41+ for key in keys (current)
42+ call remove (current, key )
43+ endfor
44+ endfunc
1845
1946
2047" ----------------------------------------------------------------------
2148" register entry: (section='File', entry='&Save', command='w')
2249" ----------------------------------------------------------------------
2350function ! quickui#menu#register (section, entry, command , help )
24- if ! has_key (s: menucfg , a: section )
51+ let current = s: namespace [s: name ].config
52+ if ! has_key (current, a: section )
2553 let index = 0
2654 let maximum = 0
27- for name in keys (s: menucfg )
28- let w = s: menucfg [name].weight
55+ for name in keys (current )
56+ let w = current [name].weight
2957 let maximum = (index == 0 )? w : ((maximum < w )? w : maximum)
3058 let index += 1
3159 endfor
32- let s: menucfg [a: section ] = {' name' :a: section , ' weight' :0 , ' items' :[]}
33- let s: menucfg [a: section ].weight = s: weight
34- let s: weight += 10
60+ let current [a: section ] = {' name' :a: section , ' weight' :0 , ' items' :[]}
61+ let current [a: section ].weight = s: namespace [ s: name ]. weight
62+ let s: namespace [ s: name ]. weight += 10
3563 endif
36- let menu = s: menucfg [a: section ]
64+ let menu = current [a: section ]
3765 let item = {' name' :a: entry , ' cmd' :a: command , ' help' :a: help }
3866 let menu .items += [item]
3967endfunc
@@ -43,10 +71,11 @@ endfunc
4371" remove entry:
4472" ----------------------------------------------------------------------
4573function ! quickui#menu#remove (section, index )
46- if ! has_key (s: menucfg , a: section )
74+ let current = s: namespace [s: name ].config
75+ if ! has_key (current, a: section )
4776 return -1
4877 endif
49- let menu = s: menucfg [a: section ]
78+ let menu = current [a: section ]
5079 if type (a: index ) == v: t_number
5180 let index = (a: index < 0 )? (len (menu .items ) + a: index ) : a: index
5281 if index < 0 || index >= len (menu .items )
@@ -74,27 +103,20 @@ function! quickui#menu#remove(section, index)
74103endfunc
75104
76105
77- " ----------------------------------------------------------------------
78- " clear all entries in menucfg
79- " ----------------------------------------------------------------------
80- function ! quickui#menu#reset ()
81- let s: menucfg = {}
82- let s: weight = 100
83- endfunc
84-
85-
86106" ----------------------------------------------------------------------
87107" return items key
88108" ----------------------------------------------------------------------
89109function ! quickui#menu#section (section)
90- return get (s: menucfg , a: section , v: null )
110+ let current = s: namespace [s: name ].config
111+ return get (current, a: section , v: null )
91112endfunc
92113
93114
94115" ----------------------------------------------------------------------
95116" install a how section
96117" ----------------------------------------------------------------------
97118function ! quickui#menu#install (section, content, ... )
119+ let current = s: namespace [s: name ].config
98120 if type (a: content ) == v: t_list
99121 for item in a: content
100122 if type (item) == v: t_dict
@@ -115,8 +137,8 @@ function! quickui#menu#install(section, content, ...)
115137 call quickui#menu#register (a: section , name, cmd, ' ' )
116138 endfor
117139 endif
118- if a: 0 > 0 && has_key (s: menucfg , a: section )
119- let s: menucfg [a: section ].weight = a: 1
140+ if a: 0 > 0 && has_key (current , a: section )
141+ let current [a: section ].weight = a: 1
120142 endif
121143endfunc
122144
@@ -125,8 +147,9 @@ endfunc
125147" change weight
126148" ----------------------------------------------------------------------
127149function ! quickui#menu#change_weight (section, weight)
128- if has_key (s: menucfg , a: section )
129- let s: menucfg [a: section ].weight = a: weight
150+ let current = s: namespace [s: name ].config
151+ if has_key (current, a: section )
152+ let current[a: section ].weight = a: weight
130153 endif
131154endfunc
132155
@@ -135,10 +158,11 @@ endfunc
135158" preset menu
136159" ----------------------------------------------------------------------
137160function ! quickui#menu#preset (section, context, ... )
161+ let current = s: namespace [s: name ].config
138162 let save_items = []
139- if has_key (s: menucfg , a: section )
140- let save_items = s: menucfg [a: section ].items
141- let s: menucfg [a: section ].items = []
163+ if has_key (current , a: section )
164+ let save_items = current [a: section ].items
165+ let current [a: section ].items = []
142166 endif
143167 if a: 0 == 0
144168 call quickui#menu#install (a: section , a: context )
@@ -171,10 +195,11 @@ endfunc
171195" ----------------------------------------------------------------------
172196" get section
173197" ----------------------------------------------------------------------
174- function ! quickui#menu#available ()
198+ function ! quickui#menu#available (name)
199+ let current = s: namespace [a: name ].config
175200 let menus = []
176- for name in keys (s: menucfg )
177- let menu = s: menucfg [name]
201+ for name in keys (current )
202+ let menu = current [name]
178203 if len (menu .items ) > 0
179204 let menus += [[menu .weight, menu .name]]
180205 endif
@@ -191,15 +216,16 @@ endfunc
191216" ----------------------------------------------------------------------
192217" parse
193218" ----------------------------------------------------------------------
194- function ! s: parse_menu ()
219+ function ! s: parse_menu (name)
220+ let current = s: namespace [a: name ].config
195221 let inst = {' items' :[], ' text' :' ' , ' width' :0 , ' hotkey' :{}}
196222 let start = 0
197223 let split = ' '
198- let names = quickui#menu#available ()
224+ let names = quickui#menu#available (a: name )
199225 let index = 0
200226 let size = len (names)
201227 for section in names
202- let menu = s: menucfg [section]
228+ let menu = current [section]
203229 let item = {' name' :menu .name, ' text' :' ' }
204230 let obj = quickui#core#escape (menu .name)
205231 let item.text = ' ' . obj[0 ] . ' '
@@ -234,9 +260,17 @@ function! quickui#menu#create(opts)
234260 if s: cmenu .state != 0
235261 return -1
236262 endif
237- let s: cmenu .inst = s: parse_menu ()
263+ let name = get (a: opts , ' name' , s: name )
264+ if ! has_key (s: namespace , name)
265+ return -1
266+ endif
267+ let current = s: namespace [name].config
268+ let s: cmenu .inst = s: parse_menu (name)
269+ let s: cmenu .name = name
270+ let s: cmenu .index = s: namespace [name].index
238271 let s: cmenu .width = &columns
239272 let s: cmenu .size = len (s: cmenu .inst.items )
273+ let s: cmenu .current = current
240274 let winid = popup_create ([s: cmenu .inst.text], {' hidden' :1 , ' wrap' :0 })
241275 let bufnr = winbufnr (winid)
242276 let s: cmenu .winid = winid
@@ -321,6 +355,7 @@ function! quickui#menu#callback(winid, code)
321355 " echom "quickui#menu#callback"
322356 let s: cmenu .state = 0
323357 let s: cmenu .winid = -1
358+ let s: namespace [s: cmenu .name].index = s: cmenu .index
324359 if s: cmenu .context >= 0
325360 call popup_close (s: cmenu .context, -3 )
326361 let s: cmenu .context = -1
@@ -432,7 +467,7 @@ function! s:context_dropdown()
432467 let opts = {' col' : item.x + 1 , ' line' : 2 , ' horizon' :1 , ' zindex' :31100 }
433468 let opts.callback = ' quickui#menu#context_exit'
434469 let opts.reserve = 1
435- let cfg = s: menucfg [item.name]
470+ let cfg = s: cmenu .current [item.name]
436471 let s: cmenu .dropdown = []
437472 for item in cfg.items
438473 let s: cmenu .dropdown += [[item.name, item.cmd, item.help ]]
@@ -455,7 +490,7 @@ function! quickui#menu#context_exit(code)
455490 let hwnd = g: quickui #context#current
456491 if has_key (hwnd, ' index' ) && hwnd.index >= 0
457492 let item = s: cmenu .inst.items [s: cmenu .index ]
458- let cfg = s: menucfg [item.name]
493+ let cfg = s: cmenu .current [item.name]
459494 let cfg.index = hwnd.index
460495 " echo "save index: ".hwnd.index
461496 endif
@@ -486,8 +521,11 @@ endfunc
486521" ----------------------------------------------------------------------
487522" open menu
488523" ----------------------------------------------------------------------
489- function ! quickui#menu#open ()
524+ function ! quickui#menu#open (... )
490525 let opts = {}
526+ if a: 0 > 0
527+ let opts.name = a: 1
528+ endif
491529 call quickui#menu#create (opts)
492530endfunc
493531
@@ -496,7 +534,8 @@ endfunc
496534" testing suit
497535" ----------------------------------------------------------------------
498536if 0
499- let s: menucfg = {}
537+ call quickui#menu#switch (' test' )
538+ call quickui#menu#reset ()
500539 call quickui#menu#install (' H&elp' , [
501540 \ [ ' &Content' , ' echo 4' ],
502541 \ [ ' &About' , ' echo 5' ],
@@ -525,13 +564,8 @@ if 0
525564
526565 call quickui#menu#install (' &Window' , [])
527566 call quickui#menu#change_weight (' H&elp' , 1000 )
528- let inst = s: parse_menu ()
529- " echo '"' . inst.text . '"'
530- let opts = {}
531- " let s:cmenu.index = -1
532- call quickui#menu#create (opts)
533- let keymap = quickui#utils#keymap ()
534- " echo keymap
567+ call quickui#menu#switch (' system' )
568+ call quickui#menu#open (' test' )
535569endif
536570
537571
0 commit comments