|
| 1 | +"====================================================================== |
| 2 | +" |
| 3 | +" preview.vim - |
| 4 | +" |
| 5 | +" Created by skywind on 2020/01/11 |
| 6 | +" Last Modified: 2020/01/11 11:30:20 |
| 7 | +" |
| 8 | +"====================================================================== |
| 9 | + |
| 10 | +" vim: set noet fenc=utf-8 ff=unix sts=4 sw=4 ts=4 : |
| 11 | + |
| 12 | + |
| 13 | +"---------------------------------------------------------------------- |
| 14 | +" |
| 15 | +"---------------------------------------------------------------------- |
| 16 | +let s:private = {'winid': -1} |
| 17 | + |
| 18 | + |
| 19 | +"---------------------------------------------------------------------- |
| 20 | +" position to a proper location |
| 21 | +"---------------------------------------------------------------------- |
| 22 | +function! s:around_cursor(width, height) |
| 23 | + let cursor_pos = quickui#core#cursor_pos() |
| 24 | + let row = cursor_pos[0] - a:height |
| 25 | + let col = cursor_pos[1] + 1 |
| 26 | + if quickui#core#in_screen(row, col, a:width, a:height) |
| 27 | + return [row, col] |
| 28 | + endif |
| 29 | + if col + a:width - 1 > &columns |
| 30 | + let col = col - (1 + a:width) |
| 31 | + if quickui#core#in_screen(row, col, a:width, a:height) |
| 32 | + return [row, col] |
| 33 | + endif |
| 34 | + endif |
| 35 | + if row < 1 |
| 36 | + let row = row + (1 + a:height) |
| 37 | + if quickui#core#in_screen(row, col, a:width, a:height) |
| 38 | + return [row, col] |
| 39 | + endif |
| 40 | + endif |
| 41 | + if cursor_pos[0] - a:height - 2 < 1 |
| 42 | + let row = cursor_pos[0] + 1 |
| 43 | + else |
| 44 | + let row = cursor_pos[0] - a:height |
| 45 | + endif |
| 46 | + if cursor_pos[1] + a:width + 2 < &columns |
| 47 | + let col = cursor_pos[1] + 1 |
| 48 | + else |
| 49 | + let col = cursor_pos[1] - a:width |
| 50 | + endif |
| 51 | + return quickui#core#screen_fit(row, col, a:width, a:height) |
| 52 | +endfunc |
| 53 | + |
| 54 | + |
| 55 | +"---------------------------------------------------------------------- |
| 56 | +" |
| 57 | +"---------------------------------------------------------------------- |
| 58 | +function! quickui#preview#display(filename, lnum, opts) |
| 59 | + call quickui#preview#close() |
| 60 | + if !filereadable(a:filename) |
| 61 | + call quickui#utils#errmsg('E212: Can not open file: '. a:filename) |
| 62 | + return -1 |
| 63 | + endif |
| 64 | + let bid = bufadd(a:filename) |
| 65 | + let winid = -1 |
| 66 | + let title = has_key(a:opts, 'title')? (' ' . a:opts.title .' ') : '' |
| 67 | + let w = get(a:opts, 'w', -1) |
| 68 | + let h = get(a:opts, 'h', -1) |
| 69 | + let w = (w < 0)? 50 : w |
| 70 | + let h = (h < 0)? 10 : h |
| 71 | + let border = get(a:opts, 'border', g:quickui#style#border) |
| 72 | + let p = s:around_cursor(w + (border? 2 : 0), h + (border? 2 : 0)) |
| 73 | + " echo p |
| 74 | + if has('nvim') == 0 |
| 75 | + let winid = popup_create(bid, {'wrap':1, 'mapping':0, 'hidden':1}) |
| 76 | + let opts = {'maxwidth':w, 'maxheight':h, 'minwidth':w, 'minheight':h} |
| 77 | + call popup_move(winid, opts) |
| 78 | + let opts = {'close':'button', 'title':title} |
| 79 | + let opts.border = border? [1,1,1,1,1,1,1,1,1] : repeat([0], 9) |
| 80 | + let opts.resize = 0 |
| 81 | + let opts.highlight = 'QuickPreview' |
| 82 | + let opts.borderchars = quickui#core#border_vim(border) |
| 83 | + let opts.moved = 'any' |
| 84 | + let opts.drag = 1 |
| 85 | + let opts.line = p[0] |
| 86 | + let opts.col = p[1] |
| 87 | + " let opts.fixed = 'true' |
| 88 | + call popup_setoptions(winid, opts) |
| 89 | + let s:private.winid = winid |
| 90 | + call popup_show(winid) |
| 91 | + else |
| 92 | + endif |
| 93 | + let cmdlist = ['setlocal signcolumn=no norelativenumber'] |
| 94 | + if get(a:opts, 'number', 1) == 0 |
| 95 | + let cmdlist += ['setlocal nonumber'] |
| 96 | + else |
| 97 | + let cmdlist += ['setlocal number'] |
| 98 | + endif |
| 99 | + if has_key(a:opts, 'index') |
| 100 | + let index = a:opts.index |
| 101 | + let cmdlist += ['let g:quickui#utils#__cursor_index = '.index] |
| 102 | + endif |
| 103 | + call quickui#core#win_execute(winid, cmdlist) |
| 104 | + return winid |
| 105 | +endfunc |
| 106 | + |
| 107 | + |
| 108 | +"---------------------------------------------------------------------- |
| 109 | +" |
| 110 | +"---------------------------------------------------------------------- |
| 111 | +function! quickui#preview#callback(winid, code) |
| 112 | + if has('nvim') == 0 |
| 113 | + let s:private.winid = -1 |
| 114 | + endif |
| 115 | +endfunc |
| 116 | + |
| 117 | + |
| 118 | +"---------------------------------------------------------------------- |
| 119 | +" |
| 120 | +"---------------------------------------------------------------------- |
| 121 | +function! quickui#preview#close() |
| 122 | + if s:private.winid >= 0 |
| 123 | + if has('nvim') == 0 |
| 124 | + call popup_close(s:private.winid, 0) |
| 125 | + let s:private.winid = -1 |
| 126 | + else |
| 127 | + endif |
| 128 | + endif |
| 129 | +endfunc |
| 130 | + |
| 131 | + |
| 132 | + |
0 commit comments