Skip to content

Commit 1697d39

Browse files
author
skywind3000
committed
fixed position calculation
1 parent ade9705 commit 1697d39

File tree

4 files changed

+152
-3
lines changed

4 files changed

+152
-3
lines changed

autoload/quickui/core.vim

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,7 @@ let s:border_styles = {}
381381
let s:border_styles[1] = quickui#core#border_extract('+-+|-|+-+++')
382382
let s:border_styles[2] = quickui#core#border_extract('┌─┐│─│└─┘├┤')
383383
let s:border_styles[3] = quickui#core#border_extract('╔═╗║─║╚═╝╟╢')
384+
let s:border_styles[4] = quickui#core#border_extract('/-\|-|\-/++')
384385

385386
let s:border_ascii = quickui#core#border_extract('+-+|-|+-+++')
386387

@@ -464,9 +465,17 @@ function! quickui#core#around_cursor(width, height)
464465
return [row, col]
465466
endif
466467
endif
467-
let row = cursor_pos[0] + 1
468-
let col = cursor_pos[1] + 1
469-
return quickui#core#screen_fit(row, col, a:height, a:height)
468+
if cursor_pos[0] + a:height + 2 < &line
469+
let row = cursor_pos[0] + 1
470+
else
471+
let row = cursor_pos[0] - a:height
472+
endif
473+
if cursor_pos[1] + a:width + 2 < &columns
474+
let col = cursor_pos[1] + 1
475+
else
476+
let col = cursor_pos[1] - a:width
477+
endif
478+
return quickui#core#screen_fit(row, col, a:width, a:height)
470479
endfunc
471480

472481

autoload/quickui/preview.vim

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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+

autoload/quickui/tags.vim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"
88
"======================================================================
99

10+
" vim: set noet fenc=utf-8 ff=unix sts=4 sw=4 ts=4 :
1011

1112
"----------------------------------------------------------------------
1213
" wrapping of vim's taglist()

plugin/quickui.vim

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,5 +97,12 @@ call s:hilink('QuickKey', 'QuickDefaultKey')
9797
call s:hilink('QuickOff', 'QuickDefaultDisable')
9898
call s:hilink('QuickHelp', 'QuickDefaultHelp')
9999

100+
if !hlexists('QuickPreview')
101+
if &background == 'dark'
102+
hi! QuickPreview ctermbg=237 guibg=#3c3836
103+
else
104+
hi! QuickPreview ctermbg=237 guibg=#3c3836
105+
endif
106+
endif
100107

101108

0 commit comments

Comments
 (0)