|
| 1 | +"====================================================================== |
| 2 | +" |
| 3 | +" terminal.vim - |
| 4 | +" |
| 5 | +" Created by skywind on 2020/02/03 |
| 6 | +" Last Modified: 2020/02/03 10:31:33 |
| 7 | +" |
| 8 | +"====================================================================== |
| 9 | + |
| 10 | +" vim: set noet fenc=utf-8 ff=unix sts=4 sw=4 ts=4 : |
| 11 | + |
| 12 | + |
| 13 | +"---------------------------------------------------------------------- |
| 14 | +" create a terminal popup |
| 15 | +"---------------------------------------------------------------------- |
| 16 | +function! quickui#terminal#create(cmd, opts) |
| 17 | + let w = get(a:opts, 'w', 80) |
| 18 | + let h = get(a:opts, 'h', 24) |
| 19 | + let winid = -1 |
| 20 | + let title = has_key(a:opts, 'title')? (' ' . a:opts.title .' ') : '' |
| 21 | + let border = get(a:opts, 'border', g:quickui#style#border) |
| 22 | + let button = (get(a:opts, 'close', '') == 'button')? 1 : 0 |
| 23 | + let color = get(a:opts, 'color', 'QuickBG') |
| 24 | + let ww = w + ((border != 0)? 2 : 0) |
| 25 | + let hh = h + ((border != 0)? 2 : 0) |
| 26 | + let hwnd = {'opts':deepcopy(a:opts), 'code':-1} |
| 27 | + if !has_key(hwnd.opts, 'line') |
| 28 | + let limit1 = (&lines - 2) * 90 / 100 |
| 29 | + let limit2 = (&lines - 2) |
| 30 | + if h + 4 < limit1 |
| 31 | + let hwnd.opts.line = (limit1 - hh) / 2 |
| 32 | + else |
| 33 | + let hwnd.opts.line = (limit2 - hh) / 2 |
| 34 | + endif |
| 35 | + let hwnd.opts.line = (hwnd.opts.line < 1)? 1 : hwnd.opts.line |
| 36 | + endif |
| 37 | + if !has_key(hwnd.opts, 'col') |
| 38 | + let hwnd.opts.col = (&columns - ww) / 2 |
| 39 | + let hwnd.opts.col = (hwnd.opts.col < 1)? 1 : hwnd.opts.col |
| 40 | + endif |
| 41 | + if has('nvim') == 0 |
| 42 | + let opts = {'hidden': 1, 'term_rows':h, 'term_cols':w} |
| 43 | + let opts.term_kill = get(a:opts, 'term_kill', 'term') |
| 44 | + let opts.norestore = 1 |
| 45 | + let opts.exit_cb = function('s:vim_term_exit') |
| 46 | + let opts.term_finish = 'close' |
| 47 | + let savedir = getcwd() |
| 48 | + if has_key(a:opts, 'cwd') |
| 49 | + call quickui#core#chdir(a:opts.cwd) |
| 50 | + endif |
| 51 | + let bid = term_start(a:cmd, opts) |
| 52 | + if has_key(a:opts, 'cwd') |
| 53 | + call quickui#core#chdir(savedir) |
| 54 | + endif |
| 55 | + if bid <= 0 |
| 56 | + return -1 |
| 57 | + endif |
| 58 | + let opts = {'maxwidth':w, 'maxheight':h, 'minwidth':w, 'minheight':h} |
| 59 | + let opts.wrap = 0 |
| 60 | + let opts.mapping = 0 |
| 61 | + let opts.title = title |
| 62 | + let opts.close = (button)? 'button' : 'none' |
| 63 | + let opts.border = border? [1,1,1,1,1,1,1,1,1] : repeat([0], 9) |
| 64 | + let opts.highlight = color |
| 65 | + let opts.borderchars = quickui#core#border_vim(border) |
| 66 | + let opts.drag = get(a:opts, 'drag', 1) |
| 67 | + let opts.resize = 0 |
| 68 | + let opts.callback = function('s:vim_popup_callback') |
| 69 | + let winid = popup_create(bid, opts) |
| 70 | + call popup_move(winid, {'line':hwnd.opts.line, 'col':hwnd.opts.col}) |
| 71 | + let hwnd.winid = winid |
| 72 | + let g:quickui#terminal#current = hwnd |
| 73 | + let s:current = hwnd |
| 74 | + call popup_show(winid) |
| 75 | + else |
| 76 | + let bid = quickui#core#neovim_buffer('terminal', []) |
| 77 | + let opts = {'focusable':1, 'style':'minimal', 'relative':'editor'} |
| 78 | + let opts.width = w |
| 79 | + let opts.height = h |
| 80 | + let opts.row = hwnd.opts.line - 1 + ((border > 0)? 1 : 0) |
| 81 | + let opts.col = hwnd.opts.col - 1 + ((border > 0)? 1 : 0) |
| 82 | + let winid = nvim_open_win(bid, 1, opts) |
| 83 | + let hwnd.winid = winid |
| 84 | + let hwnd.background = -1 |
| 85 | + if winid < 0 |
| 86 | + return -1 |
| 87 | + endif |
| 88 | + let cc = get(g:, 'terminal_color_0', 0) |
| 89 | + let hl = 'Normal:'.cc.',NonText:'.cc.',EndOfBuffer:'.cc |
| 90 | + " silent! call nvim_win_set_option(winid, 'winhl', hl) |
| 91 | + call setwinvar(winid, '&winhighlight', 'NormalFloat:Normal') |
| 92 | + if border > 0 |
| 93 | + let title = has_key(a:opts, 'title')? ' ' . a:opts.title . ' ':'' |
| 94 | + let back = quickui#utils#make_border(w, h, border, title, button) |
| 95 | + let nbid = quickui#core#neovim_buffer('terminalborder', back) |
| 96 | + let op = {'relative':'editor', 'focusable':0, 'style':'minimal'} |
| 97 | + let op.width = w + 2 |
| 98 | + let op.height = h + 2 |
| 99 | + let pos = nvim_win_get_config(winid) |
| 100 | + let op.row = hwnd.opts.line - 1 |
| 101 | + let op.col = hwnd.opts.col - 1 |
| 102 | + let background = nvim_open_win(nbid, 0, op) |
| 103 | + call nvim_win_set_option(background, 'winhl', 'Normal:'. color) |
| 104 | + let hwnd.background = background |
| 105 | + endif |
| 106 | + call nvim_set_current_win(winid) |
| 107 | + setlocal nomodified |
| 108 | + let opts = {'width': w, 'height':h} |
| 109 | + let opts.on_exit = function('s:nvim_term_exit') |
| 110 | + if has_key(a:opts, 'cwd') |
| 111 | + let opts.cwd = a:opts.cwd |
| 112 | + endif |
| 113 | + call termopen(a:cmd, opts) |
| 114 | + let g:quickui#terminal#current = hwnd |
| 115 | + let s:current = hwnd |
| 116 | + let init = [] |
| 117 | + let init += ['setlocal nonumber norelativenumber scrolloff=0'] |
| 118 | + let init += ['setlocal signcolumn=no'] |
| 119 | + call quickui#core#win_execute(winid, init) |
| 120 | + startinsert |
| 121 | + endif |
| 122 | + return hwnd |
| 123 | +endfunc |
| 124 | + |
| 125 | + |
| 126 | +"---------------------------------------------------------------------- |
| 127 | +" terminal exit_cb |
| 128 | +"---------------------------------------------------------------------- |
| 129 | +function! s:vim_term_exit(job, message) |
| 130 | + if exists('s:current') |
| 131 | + let hwnd = s:current |
| 132 | + let hwnd.code = a:message |
| 133 | + endif |
| 134 | +endfunc |
| 135 | + |
| 136 | + |
| 137 | +"---------------------------------------------------------------------- |
| 138 | +" popup callback |
| 139 | +"---------------------------------------------------------------------- |
| 140 | +function! s:vim_popup_callback(winid, code) |
| 141 | + if exists('s:current') |
| 142 | + let hwnd = s:current |
| 143 | + let hwnd.winid = -1 |
| 144 | + if has_key(hwnd.opts, 'callback') |
| 145 | + let F = function(hwnd.opts.callback) |
| 146 | + call F(hwnd.code) |
| 147 | + endif |
| 148 | + endif |
| 149 | +endfunc |
| 150 | + |
| 151 | + |
| 152 | +"---------------------------------------------------------------------- |
| 153 | +" neovim exit |
| 154 | +"---------------------------------------------------------------------- |
| 155 | +function! s:nvim_term_exit(jobid, data, event) |
| 156 | + if exists('s:current') |
| 157 | + let hwnd = s:current |
| 158 | + let hwnd.code = a:data |
| 159 | + if hwnd.winid >= 0 |
| 160 | + call nvim_win_close(hwnd.winid, 0) |
| 161 | + endif |
| 162 | + if hwnd.background >= 0 |
| 163 | + call nvim_win_close(hwnd.background, 0) |
| 164 | + endif |
| 165 | + let hwnd.winid = -1 |
| 166 | + let hwnd.background = -1 |
| 167 | + if has_key(hwnd.opts, 'callback') |
| 168 | + let F = function(hwnd.opts.callback) |
| 169 | + call F(hwnd.code) |
| 170 | + endif |
| 171 | + endif |
| 172 | +endfunc |
| 173 | + |
| 174 | + |
| 175 | +"---------------------------------------------------------------------- |
| 176 | +" open terminal in popup window |
| 177 | +"---------------------------------------------------------------------- |
| 178 | +function! quickui#terminal#open(cmd, opts) |
| 179 | + let opts = deepcopy(a:opts) |
| 180 | + let border = get(a:opts, 'border', g:quickui#style#border) |
| 181 | + if border == 0 |
| 182 | + if has_key(opts, 'title') |
| 183 | + unlet opts['title'] |
| 184 | + endif |
| 185 | + if has_key(opts, 'close') |
| 186 | + unlet opts['close'] |
| 187 | + endif |
| 188 | + endif |
| 189 | + if has_key(opts, 'callback') |
| 190 | + if type(opts.callback) == v:t_string |
| 191 | + if opts.callback == '' |
| 192 | + unlet opts['callback'] |
| 193 | + endif |
| 194 | + endif |
| 195 | + endif |
| 196 | + return quickui#terminal#create(a:cmd, opts) |
| 197 | +endfunc |
| 198 | + |
| 199 | + |
0 commit comments