|
1 | 1 | let s:save_cpo = &cpo
|
2 | 2 | set cpo&vim
|
3 | 3 |
|
| 4 | +let s:_has_nvim = has('nvim') |
| 5 | +let s:_popups = {} |
| 6 | + |
| 7 | +function! s:is_supported() abort |
| 8 | + return has('nvim') && exists('*nvim_open_win') || !has('nvim') && exists('*popup_create') |
| 9 | +endfunction |
| 10 | + |
| 11 | +" opt = { |
| 12 | +" 'contents': ['This', 'is', 'a', 'line'], |
| 13 | +" 'width': 5, |
| 14 | +" 'height': 5, |
| 15 | +" 'line': 1, |
| 16 | +" 'col': 1, |
| 17 | +" } |
4 | 18 | function! s:create(opt) abort
|
5 |
| - echom 'create popup' |
| 19 | + let data = {} |
| 20 | + call s:_set_width(data, get(a:opt, 'width', 5)) |
| 21 | + call s:_set_height(data, get(a:opt, 'height', 5)) |
| 22 | + call s:_set_contents(data, get(a:opt, 'contents', [])) |
| 23 | + call s:_set_line(data, get(a:opt, 'line', 1)) |
| 24 | + call s:_set_col(data, get(a:opt, 'col', 1)) |
| 25 | + if s:_has_nvim |
| 26 | + let buf = nvim_create_buf(0, 1) |
| 27 | + call nvim_buf_set_lines(buf, 0, -1, 1, s:_get_contents(data)) |
| 28 | + let opt = { |
| 29 | + \ 'relative': 'editor', |
| 30 | + \ 'style': 'minimal', |
| 31 | + \ 'width': data['width'], |
| 32 | + \ 'height': data['height'], |
| 33 | + \ 'row': data['line'], |
| 34 | + \ 'col': data['col'], |
| 35 | + \ 'focusable': 0, |
| 36 | + \ } |
| 37 | + let id = nvim_open_win(buf, 1, opt) |
| 38 | + else |
| 39 | + let id = popup_create(s:_get_contents(data), { |
| 40 | + \ 'width': data['width'], |
| 41 | + \ 'height': data['height'], |
| 42 | + \ 'minwidth': data['width'], |
| 43 | + \ 'minheight': data['height'], |
| 44 | + \ 'maxwidth': data['width'], |
| 45 | + \ 'maxheight': data['height'], |
| 46 | + \ 'line': data['line'], |
| 47 | + \ 'col': data['col'], |
| 48 | + \ }) |
| 49 | + endif |
| 50 | + let s:_popups[id] = data |
| 51 | + return id |
| 52 | +endfunction |
| 53 | + |
| 54 | +function! s:_set_contents(data, contents) abort |
| 55 | + let a:data['contents'] = a:contents |
| 56 | +endfunction |
| 57 | + |
| 58 | +function! s:_get_contents(data) |
| 59 | + return get(a:data, 'contents', []) |
| 60 | +endfunction |
| 61 | + |
| 62 | +function! s:_set_width(data, width) abort |
| 63 | + let a:data['width'] = a:width |
| 64 | +endfunction |
| 65 | + |
| 66 | +function! s:_get_width(data) abort |
| 67 | + return a:data['width'] |
| 68 | +endfunction |
| 69 | + |
| 70 | +function! s:_get_height(data) abort |
| 71 | + return a:data['height'] |
| 72 | +endfunction |
| 73 | + |
| 74 | +function! s:_set_height(data, height) abort |
| 75 | + let a:data['height'] = a:height |
| 76 | +endfunction |
| 77 | + |
| 78 | +function! s:_set_line(data, line) abort |
| 79 | + if s:_has_nvim |
| 80 | + let a:data['line'] = a:line - 1 |
| 81 | + else |
| 82 | + let a:data['line'] = a:line |
| 83 | + endif |
| 84 | +endfunction |
| 85 | + |
| 86 | +function! s:_set_col(data, col) abort |
| 87 | + if s:_has_nvim |
| 88 | + let a:data['col'] = a:col - 1 |
| 89 | + else |
| 90 | + let a:data['col'] = a:col |
| 91 | + endif |
| 92 | +endfunction |
| 93 | + |
| 94 | +function! s:close(id) |
| 95 | + if has_key(s:_popups, a:id) |
| 96 | + if s:_has_nvim |
| 97 | + silent! call nvim_win_close(a:id, 1) |
| 98 | + else |
| 99 | + silent! call popup_close(a:id) |
| 100 | + endif |
| 101 | + call remove(s:_popups, a:id) |
| 102 | + endif |
6 | 103 | endfunction
|
7 | 104 |
|
8 | 105 | let &cpo = s:save_cpo
|
|
0 commit comments