@@ -14,41 +14,15 @@ endfunction
14
14
" 'h': 5,
15
15
" 'x': 1,
16
16
" 'y': 1,
17
+ " 'pos': 'topleft|topright|bottomleft|bottomright|topcenter|bottomcenter',
17
18
" }
18
19
function ! s: create (opt ) abort
19
20
let data = {}
20
- call s: _set_w (data, get (a: opt , ' w' , 5 ))
21
- call s: _set_h (data, get (a: opt , ' h' , 5 ))
22
- call s: _set_x (data, get (a: opt , ' x' , 1 ))
23
- call s: _set_y (data, get (a: opt , ' y' , 1 ))
24
- call s: _set_pos (data, get (a: opt , ' pos' , ' topleft' ))
25
- call s: _set_contents (data, get (a: opt , ' contents' , []))
26
-
27
- if data[' pos' ] == # ' topleft'
28
- let data[' sx' ] = data[' x' ]
29
- let data[' sy' ] = data[' y' ]
30
- elseif data[' pos' ] == # ' topright'
31
- let data[' sx' ] = data[' x' ] + data[' w' ] + 1
32
- let data[' sy' ] = data[' y' ]
33
- elseif data[' pos' ] == # ' bottomleft'
34
- let data[' sx' ] = data[' x' ]
35
- let data[' sy' ] = data[' y' ] - data[' h' ] + 1
36
- elseif data[' pos' ] == # ' bottomright'
37
- let data[' sx' ] = data[' x' ] + data[' w' ] + 1
38
- let data[' sy' ] = data[' y' ] - data[' h' ] + 1
39
- elseif data[' pos' ] == # ' topcenter'
40
- let data[' sx' ] = data[' x' ] + float2nr (data[' x' ] / 2 ) + 1
41
- let data[' sy' ] = data[' y' ] - data[' h' ]
42
- elseif data[' pos' ] == # ' bottomcenter'
43
- let data[' sx' ] = data[' x' ] + float2nr (data[' x' ] / 2 ) + 1
44
- let data[' sy' ] = data[' y' ] + 1
45
- else
46
- throw ' vital: Experimental.UI.Popup: Invalid pos'
47
- endif
21
+ call s: _set (data, a: opt )
48
22
49
23
if s: _has_nvim
50
24
let buf = nvim_create_buf (0 , 1 )
51
- call nvim_buf_set_lines (buf , 0 , -1 , 1 , s: _get_contents ( data) )
25
+ call nvim_buf_set_lines (buf , 0 , -1 , 1 , data[ ' contents ' ] )
52
26
let opt = {
53
27
\ ' relative' : ' editor' ,
54
28
\ ' style' : ' minimal' ,
@@ -61,7 +35,7 @@ function! s:create(opt) abort
61
35
let id = nvim_open_win (buf , 1 , opt )
62
36
else
63
37
" neovim doesn't support scrollbar so don't enable it
64
- let id = popup_create (s: _get_contents ( data) , {
38
+ let id = popup_create (data[ ' contents ' ] , {
65
39
\ ' width' : data[' w' ],
66
40
\ ' height' : data[' h' ],
67
41
\ ' minwidth' : data[' w' ],
@@ -77,42 +51,6 @@ function! s:create(opt) abort
77
51
return id
78
52
endfunction
79
53
80
- function ! s: _set_contents (data, contents) abort
81
- let a: data [' contents' ] = a: contents
82
- endfunction
83
-
84
- function ! s: _get_contents (data) abort
85
- return get (a: data , ' contents' , [])
86
- endfunction
87
-
88
- function ! s: _set_w (data, w ) abort
89
- let a: data [' w' ] = a: w
90
- endfunction
91
-
92
- function ! s: _set_h (data, h ) abort
93
- let a: data [' h' ] = a: h
94
- endfunction
95
-
96
- function ! s: _set_y (data, y ) abort
97
- if s: _has_nvim
98
- let a: data [' y' ] = a: y - 1
99
- else
100
- let a: data [' y' ] = a: y
101
- endif
102
- endfunction
103
-
104
- function ! s: _set_x (data, x ) abort
105
- if s: _has_nvim
106
- let a: data [' x' ] = a: x - 1
107
- else
108
- let a: data [' x' ] = a: x
109
- endif
110
- endfunction
111
-
112
- function ! s: _set_pos (data, pos) abort
113
- let a: data [' pos' ] = a: pos
114
- endfunction
115
-
116
54
function ! s: close (id) abort
117
55
if has_key (s: _popups , a: id )
118
56
if s: _has_nvim
@@ -124,6 +62,47 @@ function! s:close(id) abort
124
62
endif
125
63
endfunction
126
64
65
+ function ! s: _set (data, opt ) abort
66
+ " try to set values from a:opt, if not set from a:data, if not default
67
+ let a: data [' w' ] = get (a: opt , ' w' , get (a: data , ' w' , 5 ))
68
+ let a: data [' h' ] = get (a: opt , ' h' , get (a: data , ' h' , 5 ))
69
+
70
+ if s: _has_nvim
71
+ " a:opt[x/y] need to - 1
72
+ " a:data['x/y'] already normalized
73
+ let a: data [' x' ] = has_key (a: opt , ' x' ) ? a: opt [' x' ] - 1 : get (a: data , ' x' , 0 )
74
+ let a: data [' y' ] = has_key (a: opt , ' y' ) ? a: opt [' y' ] - 1 : get (a: data , ' y' , 0 )
75
+ else
76
+ let a: data [' x' ] = get (a: opt , ' x' , get (a: data , ' x' , 1 ))
77
+ let a: data [' y' ] = get (a: opt , ' y' , get (a: data , ' y' , 1 ))
78
+ endif
79
+
80
+ let a: data [' contents' ] = get (a: opt , ' contents' , get (a: data , ' contents' , []))
81
+ let a: data [' pos' ] = get (a: opt , ' pos' , get (a: data , ' pos' , ' topleft' ))
82
+
83
+ if a: data [' pos' ] == # ' topleft'
84
+ let a: data [' sx' ] = a: data [' x' ]
85
+ let a: data [' sy' ] = a: data [' y' ]
86
+ elseif a: data [' pos' ] == # ' topright'
87
+ let a: data [' sx' ] = a: data [' x' ] + a: data [' w' ] + 1
88
+ let a: data [' sy' ] = a: data [' y' ]
89
+ elseif a: data [' pos' ] == # ' bottomleft'
90
+ let a: data [' sx' ] = a: data [' x' ]
91
+ let a: data [' sy' ] = a: data [' y' ] - a: data [' h' ] + 1
92
+ elseif a: data [' pos' ] == # ' bottomright'
93
+ let a: data [' sx' ] = a: data [' x' ] + a: data [' w' ] + 1
94
+ let a: data [' sy' ] = a: data [' y' ] - a: data [' h' ] + 1
95
+ elseif a: data [' pos' ] == # ' topcenter'
96
+ let a: data [' sx' ] = a: data [' x' ] + float2nr (a: data [' x' ] / 2 ) + 1
97
+ let a: data [' sy' ] = a: data [' y' ] - a: data [' h' ]
98
+ elseif a: data [' pos' ] == # ' bottomcenter'
99
+ let a: data [' sx' ] = a: data [' x' ] + float2nr (a: data [' x' ] / 2 ) + 1
100
+ let a: data [' sy' ] = a: data [' y' ] + 1
101
+ else
102
+ throw ' vital: Experimental.UI.Popup: Invalid pos'
103
+ endif
104
+ endfunction
105
+
127
106
let &cpo = s: save_cpo
128
107
unlet s: save_cpo
129
108
" vim: sts=2 sw=2 smarttab et ai textwidth=0 fdm=marker
0 commit comments