Skip to content

Commit f7f164b

Browse files
committed
integration: Add snippet view function back
Partial revert f5916bd
1 parent 144b548 commit f7f164b

File tree

5 files changed

+89
-7
lines changed

5 files changed

+89
-7
lines changed

sphinxnotes/snippet/cli.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ def main(argv:List[str]=sys.argv[1:]) -> int:
9393
igparser.add_argument('--zsh', '-z', action='store_true', help='dump zsh integration script')
9494
igparser.add_argument('--zsh-binding', action='store_true', help='dump recommended zsh key binding')
9595
igparser.add_argument('--vim', '-v', action='store_true', help='dump (neo)vim integration script')
96-
igparser.add_argument('--vim-binding', action='store_true', help='dump recommended (neo)vim key binding')
96+
igparser.add_argument('--vim-binding', action='store_true', help='dump recommended vim key binding')
97+
igparser.add_argument('--nvim-binding', action='store_true', help='dump recommended neovim key binding')
9798
igparser.set_defaults(func=_on_command_integration, parser=igparser)
9899

99100
# Parse command line arguments
@@ -191,7 +192,12 @@ def _on_command_integration(args:argparse.Namespace):
191192
if args.vim_binding:
192193
with open(get_integration_file('binding.vim'), 'r') as f:
193194
print(f.read())
194-
195+
if args.nvim_binding:
196+
# NeoVim binding depends on Vim binding
197+
with open(get_integration_file('binding.vim'), 'r') as f:
198+
print(f.read())
199+
with open(get_integration_file('binding.nvim'), 'r') as f:
200+
print(f.read())
195201

196202
if __name__ == '__main__':
197203
sys.exit(main())
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
" NeoVim key binding for sphinxnotes-snippet
2+
" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
"
4+
" :Author: Shengyu Zhang
5+
" :Date: 2021-11-14
6+
" :Version: 20211114
7+
"
8+
" TODO: Support vim?
9+
10+
function! g:SphinxNotesSnippetListAndView()
11+
function! s:CallView(selection)
12+
call g:SphinxNotesSnippetView(s:SplitID(a:selection))
13+
endfunction
14+
call g:SphinxNotesSnippetList(function('s:CallView'), 'ds')
15+
endfunction
16+
17+
" https://github.com/anhmv/vim-float-window/blob/master/plugin/float-window.vim
18+
function! g:SphinxNotesSnippetView(id)
19+
let height = float2nr((&lines - 2) / 1.5)
20+
let row = float2nr((&lines - height) / 2)
21+
let width = float2nr(&columns / 1.5)
22+
let col = float2nr((&columns - width) / 2)
23+
24+
" Main Window
25+
let opts = {
26+
\ 'relative': 'editor',
27+
\ 'style': 'minimal',
28+
\ 'width': width,
29+
\ 'height': height,
30+
\ 'col': col,
31+
\ 'row': row,
32+
\ }
33+
34+
let buf = nvim_create_buf(v:false, v:true)
35+
" Global for :call
36+
let g:sphinx_notes_snippet_win = nvim_open_win(buf, v:true, opts)
37+
38+
" The content is always reStructuredText for now
39+
set filetype=rst
40+
" Press enter to return
41+
nmap <buffer> <CR> :call nvim_win_close(g:sphinx_notes_snippet_win, v:true)<CR>
42+
43+
let cmd = [s:snippet, 'get', '--text', a:id]
44+
call append(line('$'), ['.. hint:: Press <ENTER> to return'])
45+
execute '$read !' . '..'
46+
execute '$read !' . join(cmd, ' ')
47+
execute '$read !' . '..'
48+
call append(line('$'), ['.. hint:: Press <ENTER> to return'])
49+
endfunction
50+
51+
nmap <C-k>v :call g:SphinxNotesSnippetListAndView()<CR>
52+
53+
" vim: set shiftwidth=2:
54+
" vim: set ft=vim:

sphinxnotes/snippet/integration/binding.sh

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,24 @@
22
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
33
#
44
# :Author: Shengyu Zhang
5-
# :Date: 2021-08-114
5+
# :Date: 2021-08-14
66
# :Version: 20211114
7-
#
8-
# .. note:: Must source :file:`./plugin.sh` to get `snippet_list` functions.
7+
8+
function snippet_view() {
9+
selection=$(snippet_list ds)
10+
[ -z "$selection" ] && return
11+
12+
# Make sure we have $PAGER
13+
if [ -z "$PAGER" ]; then
14+
if [ ! -z "$(where less)" ]; then
15+
PAGER='less'
16+
else
17+
PAGER='cat'
18+
fi
19+
fi
20+
21+
echo "$SNIPPET get --text $selection | $PAGER"
22+
}
923

1024
function snippet_edit() {
1125
selection=$(snippet_list ds)
@@ -29,6 +43,7 @@ function snippet_sh_bind_wrapper() {
2943
}
3044

3145
function snippet_sh_do_bind() {
46+
bind -x '"\C-kv": snippet_sh_bind_wrapper snippet_view'
3247
bind -x '"\C-ke": snippet_sh_bind_wrapper snippet_edit'
3348
bind -x '"\C-ku": snippet_sh_bind_wrapper snippet_url'
3449
}

sphinxnotes/snippet/integration/binding.vim

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
" NeoVim key binding for sphinxnotes-snippet
2-
" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1+
" Vim key binding for sphinxnotes-snippet
2+
" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
33
"
44
" :Author: Shengyu Zhang
55
" :Date: 2021-04-12
66
" :Version: 20211114
77
"
8+
89
function! g:SphinxNotesSnippetEdit(id)
910
let file = system(join([s:snippet, 'get', '--file', a:id, '2>/dev/null'], ' '))
1011
let line = system(join([s:snippet, 'get', '--line-start', a:id, '2>/dev/null'], ' '))

sphinxnotes/snippet/integration/binding.zsh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ function snippet_z_bind_wrapper() {
1111
zle redisplay
1212
}
1313

14+
function snippet_z_view() {
15+
snippet_z_bind_wrapper snippet_view
16+
}
17+
1418
function snippet_z_edit() {
1519
snippet_z_bind_wrapper snippet_edit
1620
}
@@ -20,9 +24,11 @@ function snippet_z_url() {
2024
}
2125

2226
# Define widgets
27+
zle -N snippet_z_view
2328
zle -N snippet_z_edit
2429
zle -N snippet_z_url
2530

31+
bindkey '^kv' snippet_z_view
2632
bindkey '^ke' snippet_z_edit
2733
bindkey '^ku' snippet_z_url
2834

0 commit comments

Comments
 (0)