Skip to content

Commit b111397

Browse files
author
skywind3000
committed
polish: fixed minor issues
preview window: parameter changed, **important**, see the documentation
1 parent 8a39402 commit b111397

File tree

4 files changed

+68
-35
lines changed

4 files changed

+68
-35
lines changed

README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,10 +346,18 @@ Preview window is used to replace traditional `pedit` command and can be used to
346346
You can open the preview window by:
347347

348348
```VimL
349-
quickui#preview#open(filename, lnum)
349+
quickui#preview#open(filename, opts)
350350
```
351351

352-
It will not interfere your work, and will immediately close if you move your cursor around. The second parameter `lnum` is a line number, and if you set it above zero, the certain line in the source will be highlighted.
352+
It will not interfere your work, and will immediately close if you move your cursor around. The second parameter `opts` is a dictionary with options, available options are:
353+
354+
| Option | Type | Default | Description |
355+
|-|-|-|-|
356+
| cursor | Number | -1 | if you set it above zero, the certain line will be highlighted (use cursorline). |
357+
| number | Number | 1 | set to zero to disable line number |
358+
| syntax | String | `unset` | additional syntax file type, eg: `cpp` or `python` |
359+
| title | String | `unset` | additional title for preview window |
360+
| persist | Number | 0 | By default the preview window will be closed automatically when `CursorMove` happens, set to 1 to close it manually by `quickui#preview#close()` |
353361

354362
Usually the syntax highlighting and cursorline will help you when you are using it to peek symbol definitions.
355363

autoload/quickui/core.vim

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,8 @@ function! quickui#core#neovim_buffer(name, textlist)
343343
endif
344344
call nvim_buf_set_option(bid, 'modifiable', v:true)
345345
call nvim_buf_set_lines(bid, 0, -1, v:true, a:textlist)
346+
call setbufvar(bid, 'current_syntax', '')
347+
call setbufvar(bid, '&filetype', '')
346348
return bid
347349
endfunc
348350

autoload/quickui/preview.vim

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,19 @@ endfunc
5555
"----------------------------------------------------------------------
5656
" create preview window
5757
"----------------------------------------------------------------------
58-
function! quickui#preview#display(filename, cursor, opts)
58+
function! quickui#preview#display(content, opts)
5959
call quickui#preview#close()
60-
if !filereadable(a:filename)
61-
call quickui#utils#errmsg('E212: Can not open file: '. a:filename)
60+
if type(a:content) == v:t_string && filereadable(a:content) == 0
61+
call quickui#utils#errmsg('E212: Can not open file: '. a:content)
6262
return -1
6363
endif
6464
let s:private.state = 0
65-
silent let bid = bufadd(a:filename)
66-
silent call bufload(bid)
65+
if type(a:content) == v:t_string
66+
silent let source = bufadd(a:content)
67+
silent call bufload(source)
68+
elseif type(a:content) == v:t_list
69+
let source = a:content
70+
endif
6771
let winid = -1
6872
let title = has_key(a:opts, 'title')? (' ' . a:opts.title .' ') : ''
6973
let w = get(a:opts, 'w', -1)
@@ -75,7 +79,7 @@ function! quickui#preview#display(filename, cursor, opts)
7579
let color = get(a:opts, 'color', 'QuickPreview')
7680
let p = s:around_cursor(w + (border? 2 : 0), h + (border? 2 : 0))
7781
if has('nvim') == 0
78-
let winid = popup_create(bid, {'wrap':1, 'mapping':0, 'hidden':1})
82+
let winid = popup_create(source, {'wrap':1, 'mapping':0, 'hidden':1})
7983
let opts = {'maxwidth':w, 'maxheight':h, 'minwidth':w, 'minheight':h}
8084
call popup_move(winid, opts)
8185
let opts = {'close':'button', 'title':title}
@@ -89,7 +93,7 @@ function! quickui#preview#display(filename, cursor, opts)
8993
let opts.drag = 1
9094
let opts.line = p[0]
9195
let opts.col = p[1]
92-
let opts.callback = 'quickui#preview#callback'
96+
let opts.callback = function('s:popup_exit')
9397
" let opts.fixed = 'true'
9498
call popup_setoptions(winid, opts)
9599
let s:private.winid = winid
@@ -103,6 +107,11 @@ function! quickui#preview#display(filename, cursor, opts)
103107
if has_key(a:opts, 'focusable')
104108
let opts.focusable = a:opts.focusable
105109
endif
110+
if type(source) == v:t_number
111+
let bid = source
112+
else
113+
let bid = quickui#core#neovim_buffer('preview', source)
114+
endif
106115
let winid = nvim_open_win(bid, 0, opts)
107116
let s:private.winid = winid
108117
let high = 'Normal:'.color.',NonText:'.color.',EndOfBuffer:'.color
@@ -128,13 +137,14 @@ function! quickui#preview#display(filename, cursor, opts)
128137
else
129138
let cmdlist += ['setlocal number']
130139
endif
131-
if a:cursor > 0
132-
let cmdlist += ['normal! gg' . a:cursor . 'Gzz']
140+
let cursor = get(a:opts, 'cursor', -1)
141+
if cursor > 0
142+
let cmdlist += ['exec "normal! gg' . cursor . 'Gzz"']
133143
endif
134144
if has_key(a:opts, 'syntax')
135-
let cmdlist += ['set ft=' . fnameescape(a:opts.syntax) ]
145+
let cmdlist += ['setl ft=' . fnameescape(a:opts.syntax) ]
136146
endif
137-
call setbufvar(winbufnr(winid), '__quickui_cursor__', a:cursor)
147+
call setbufvar(winbufnr(winid), '__quickui_cursor__', cursor)
138148
call quickui#core#win_execute(winid, cmdlist)
139149
call quickui#utils#update_cursor(winid)
140150
let s:private.state = 1
@@ -150,10 +160,8 @@ endfunc
150160
"----------------------------------------------------------------------
151161
" exit callback
152162
"----------------------------------------------------------------------
153-
function! quickui#preview#callback(winid, code)
154-
if has('nvim') == 0
155-
let s:private.winid = -1
156-
endif
163+
function! s:popup_exit(winid, code)
164+
let s:private.winid = -1
157165
let s:private.state = 0
158166
endfunc
159167

@@ -216,23 +224,30 @@ endfunc
216224
"----------------------------------------------------------------------
217225
" preview file
218226
"----------------------------------------------------------------------
219-
function! quickui#preview#open(filename, ...)
220-
if !filereadable(a:filename)
221-
call quickui#utils#errmsg('E484: Cannot open file ' . a:filename)
227+
function! quickui#preview#open(content, opts)
228+
call quickui#preview#close()
229+
if type(a:content) == v:t_string && filereadable(a:content) == 0
230+
call quickui#utils#errmsg('E484: Cannot open file ' . a:content)
222231
return -1
223232
endif
224-
let lnum = (a:0 >= 1)? a:1 : -1
225-
let display_num = g:quickui#style#preview_number
226233
let opts = {}
227234
let opts.w = get(g:, 'quickui_preview_w', g:quickui#style#preview_w)
228235
let opts.h = get(g:, 'quickui_preview_h', g:quickui#style#preview_h)
229-
let opts.number = get(g:, 'quickui_preview_num', display_num)
230-
let name = fnamemodify(a:filename, ':p:t')
231-
let title = (a:0 >= 3)? (' ' . a:3) : ''
232-
let opts.title = 'Preview: ' . name . title
233-
let opts.persist = (a:0 >= 2)? a:2 : 0
236+
let opts.number = get(a:opts, 'number', g:quickui#style#preview_number)
237+
let opts.cursor = get(a:opts, 'cursor', -1)
238+
let title = has_key(a:opts, 'title')? (' ' .. a:opts.title) : ''
239+
if type(a:content) == v:t_string
240+
let name = fnamemodify(a:content, ':p:t')
241+
let opts.title = 'Preview: ' . name . title
242+
else
243+
let opts.title = 'Preview' .. ((title == '')? '' : (':' .. title))
244+
endif
245+
let opts.persist = get(a:opts, 'persist', 0)
234246
let opts.focusable = get(g:, 'quickui_preview_focusable', 1)
235-
return quickui#preview#display(a:filename, lnum, opts)
247+
if has_key(a:opts, 'syntax')
248+
let opts.syntax = a:opts.syntax
249+
endif
250+
return quickui#preview#display(a:content, opts)
236251
endfunc
237252

238253

autoload/quickui/tools.vim

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,16 @@ function! quickui#tools#buffer_switch(bid)
5353
elseif code == '1'
5454
exec 'b '. a:bid
5555
elseif code == '2'
56-
exec 'vs '. fnameescape(name)
56+
exec 'vs'
57+
exec 'b '. a:bid
5758
elseif code == '3'
58-
exec 'tabe '. fnameescape(name)
59+
exec 'split'
60+
exec 'b '. a:bid
5961
elseif code == '4'
60-
exec 'FileSwitch tabe ' . fnameescape(name)
62+
exec 'tab split'
63+
exec 'b '. a:bid
64+
elseif code == '5'
65+
exec 'tab drop ' . fnameescape(name)
6166
endif
6267
endfunc
6368

@@ -101,8 +106,9 @@ function! quickui#tools#list_buffer(switch)
101106
let opts.keymap = {}
102107
let opts.keymap["\<c-e>"] = 'TAG:1'
103108
let opts.keymap["\<c-]>"] = 'TAG:2'
104-
let opts.keymap["\<c-t>"] = 'TAG:3'
105-
let opts.keymap["\<c-g>"] = 'TAG:4'
109+
let opts.keymap["\<c-x>"] = 'TAG:3'
110+
let opts.keymap["\<c-t>"] = 'TAG:4'
111+
let opts.keymap["\<c-g>"] = 'TAG:5'
106112
if exists('g:quickui_tools_width')
107113
let opts.w = quickui#utils#tools_width()
108114
endif
@@ -264,7 +270,8 @@ function! quickui#tools#preview_quickfix(...)
264270
return -4
265271
endif
266272
let name = bufname(item.bufnr)
267-
call quickui#preview#open(name, item.lnum)
273+
let opts = {'cursor':item.lnum}
274+
call quickui#preview#open(name, opts)
268275
" echom 'lnum:'. item.lnum
269276
endfunc
270277

@@ -317,7 +324,8 @@ function! quickui#tools#preview_tag(tagname)
317324
return 3
318325
endif
319326
let text = '('.(ptag.index + 1).'/'.len(ptag.taglist).')'
320-
call quickui#preview#open(filename, taginfo.line, 0, text)
327+
let opts = {'cursor':taginfo.line, 'title':text}
328+
call quickui#preview#open(filename, opts)
321329
let text = taginfo.name
322330
let text.= ' ('.(ptag.index + 1).'/'.len(ptag.taglist).') '
323331
let text.= filename

0 commit comments

Comments
 (0)