Skip to content

Commit f5916bd

Browse files
committed
integration: Refactor
- Move more stuff from plugin to binding - Drop snippet view function - Hardcode vim as editor, xdg-open as url viewer - Add bash binding support
1 parent 0120102 commit f5916bd

File tree

7 files changed

+115
-182
lines changed

7 files changed

+115
-182
lines changed

sphinxnotes/snippet/cli.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,11 @@ def main(argv:List[str]=sys.argv[1:]) -> int:
8888
igparser = subparsers.add_parser('integration', aliases=['i'],
8989
formatter_class=HelpFormatter,
9090
help='integration related commands')
91-
igparser.add_argument('--sh', '-s', action='store_true', help='dump POSIX shell integration script')
91+
igparser.add_argument('--sh', '-s', action='store_true', help='dump bash shell integration script')
92+
igparser.add_argument('--sh-binding', action='store_true', help='dump recommended bash key binding')
9293
igparser.add_argument('--zsh', '-z', action='store_true', help='dump zsh integration script')
9394
igparser.add_argument('--zsh-binding', action='store_true', help='dump recommended zsh key binding')
94-
igparser.add_argument('--vim', '-v', action='store_true', help='dump (neo)vim integration script (NOTE: for now, only neovim is supported)')
95+
igparser.add_argument('--vim', '-v', action='store_true', help='dump (neo)vim integration script')
9596
igparser.add_argument('--vim-binding', action='store_true', help='dump recommended (neo)vim key binding')
9697
igparser.set_defaults(func=_on_command_integration, parser=igparser)
9798

@@ -172,13 +173,17 @@ def _on_command_integration(args:argparse.Namespace):
172173
if args.sh:
173174
with open(get_integration_file('plugin.sh'), 'r') as f:
174175
print(f.read())
176+
if args.sh_binding:
177+
with open(get_integration_file('binding.sh'), 'r') as f:
178+
print(f.read())
175179
if args.zsh:
176-
# Zsh plugin depends on POSIX shell plugin
180+
# Zsh plugin depends on Bash shell plugin
177181
with open(get_integration_file('plugin.sh'), 'r') as f:
178182
print(f.read())
179-
with open(get_integration_file('plugin.zsh'), 'r') as f:
180-
print(f.read())
181183
if args.zsh_binding:
184+
# Zsh binding depends on Bash shell binding
185+
with open(get_integration_file('binding.sh'), 'r') as f:
186+
print(f.read())
182187
with open(get_integration_file('binding.zsh'), 'r') as f:
183188
print(f.read())
184189
if args.vim:
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Bash Shell key binding for sphinxnotes-snippet
2+
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
#
4+
# :Author: Shengyu Zhang
5+
# :Date: 2021-08-114
6+
# :Version: 20210814
7+
#
8+
# .. note:: Must source :file:`./plugin.sh` to get `snippet_list` functions.
9+
10+
function snippet_edit() {
11+
selection=$(snippet_list ds)
12+
[ -z "$selection" ] && return
13+
14+
echo "vim +\$($SNIPPET get --line-start $selection) \$($SNIPPET get --file $selection)"
15+
}
16+
17+
function snippet_url() {
18+
selection=$(snippet_list ds)
19+
[ -z "$selection" ] && return
20+
21+
echo "xdg-open \$($SNIPPET get --url $selection)"
22+
}
23+
24+
function snippet_sh_bind_wrapper() {
25+
READLINE_LINE="$($1)"
26+
READLINE_POINT=${#READLINE_LINE}
27+
}
28+
29+
function snippet_sh_do_bind() {
30+
bind '"\XXacceptline": accept-line'
31+
bind -x '"\XXsnippetedit": snippet_sh_bind_wrapper snippet_edit'
32+
bind -x '"\XXsnippeturl": snippet_sh_bind_wrapper snippet_url'
33+
bind '"\C-ke": "\XXsnippetedit\XXacceptline"'
34+
bind '"\C-ku": "\XXsnippeturl\XXacceptline"'
35+
}
36+
37+
# Bind key if bind command exists
38+
# (the script may sourced by Zsh)
39+
command -v bind 2>&1 1>/dev/null && snippet_sh_do_bind
40+
41+
# vim: set shiftwidth=2:

sphinxnotes/snippet/integration/binding.vim

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,36 @@
33
"
44
" :Author: Shengyu Zhang
55
" :Date: 2021-04-12
6-
" :Version: 20210412
6+
" :Version: 20210814
77
"
8-
nmap <C-k>v :call g:SphinxNotesSnippetListAndView()<CR>
8+
function! g:SphinxNotesSnippetEdit(id)
9+
let file = system(join([s:snippet, 'get', '--file', a:id], ' '))
10+
let line = system(join([s:snippet, 'get', '--line-start', a:id], ' '))
11+
execute 'tabedit ' . file
12+
execute line
13+
endfunction
14+
15+
function! g:SphinxNotesSnippetListAndEdit()
16+
function! s:CallEdit(selection)
17+
call g:SphinxNotesSnippetEdit(s:SplitID(a:selection))
18+
endfunction
19+
call g:SphinxNotesSnippetList(function('s:CallEdit'), 'ds')
20+
endfunction
21+
22+
function! g:SphinxNotesSnippetUrl(id)
23+
let url_list = systemlist(join([s:snippet, 'get', '--url', a:id], ' '))
24+
for url in url_list
25+
echo system('xdg-open ' . shellescape(url))
26+
endfor
27+
endfunction
28+
29+
function! g:SphinxNotesSnippetListAndUrl()
30+
function! s:CallUrl(selection)
31+
call g:SphinxNotesSnippetUrl(s:SplitID(a:selection))
32+
endfunction
33+
call g:SphinxNotesSnippetList(function('s:CallUrl'), 'ds')
34+
endfunction
35+
936
nmap <C-k>e :call g:SphinxNotesSnippetListAndEdit()<CR>
1037
nmap <C-k>u :call g:SphinxNotesSnippetListAndUrl()<CR>
1138

sphinxnotes/snippet/integration/binding.zsh

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,30 @@
33
#
44
# :Author: Shengyu Zhang
55
# :Date: 2021-04-12
6-
# :Version: 20210412
6+
# :Version: 20210814
77

8-
bindkey '^kv' snippet-view
9-
bindkey '^ke' snippet-edit
10-
bindkey '^ku' snippet-url
8+
# $1: One of snippet_* functions
9+
function snippet_z_bind_wrapper() {
10+
cmd=$($1)
11+
if [ ! -z "$cmd" ]; then
12+
BUFFER="$cmd"
13+
zle accept-line
14+
fi
15+
}
16+
17+
function snippet_z_edit() {
18+
snippet_z_bind_wrapper snippet_edit
19+
}
20+
21+
function snippet_z_url() {
22+
snippet_z_bind_wrapper snippet_url
23+
}
24+
25+
# Define widgets
26+
zle -N snippet_z_edit
27+
zle -N snippet_z_url
28+
29+
bindkey '^ke' snippet_z_edit
30+
bindkey '^ku' snippet_z_url
1131

1232
# vim: set shiftwidth=2:
Lines changed: 6 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,19 @@
1-
# POISX Shell integration for sphinxnotes-snippet
1+
# Bash Shell integration for sphinxnotes-snippet
22
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
33
#
44
# :Author: Shengyu Zhang
55
# :Date: 2021-03-20
6-
# :Version: 20210420
6+
# :Version: 20210814
77

88
# Make sure we have $SNIPPET
99
[ -z "$SNIPPET"] && SNIPPET='snippet'
1010

11-
# $1: kinds
11+
# Arguments: $1: kinds
12+
# Returns: snippet_id
1213
function snippet_list() {
13-
$SNIPPET list --kinds $1 --width $(($(tput cols) - 2))| \
14-
fzf --with-nth 2.. --no-hscroll --header-lines 1 | \
14+
$SNIPPET list --tags $1 --width $(($(tput cols) - 2)) | \
15+
fzf --with-nth 2.. --no-hscroll --header-lines 1 | \
1516
cut -d ' ' -f1
1617
}
1718

18-
function snippet_view() {
19-
index_id=$(snippet_list ds)
20-
[ -z "$index_id" ] && return
21-
22-
# Make sure we have $PAGER
23-
if [ -z "$PAGER" ]; then
24-
if [ ! -z "$(where bat)" ]; then
25-
PAGER='bat --language rst --italic-text always --style plain --paging always'
26-
elif [ ! -z "$(where less)" ]; then
27-
PAGER='less'
28-
elif [ ! -z "$(where more)" ]; then
29-
PAGER='more'
30-
else
31-
echo "No pager available!" >&2
32-
return
33-
fi
34-
fi
35-
36-
echo "$SNIPPET get --text $index_id | $PAGER"
37-
}
38-
39-
function snippet_edit() {
40-
index_id=$(snippet_list ds)
41-
[ -z "$index_id" ] && return
42-
43-
# Make sure we have $EDITOR
44-
if [ -z "$EDITOR" ]; then
45-
if [ ! -z "$(where vim)" ]; then
46-
EDITOR='vim'
47-
elif [ ! -z "$(where nvim)" ]; then
48-
EDITOR='nvim'
49-
elif [ ! -z "$(where nano)" ]; then
50-
EDITOR='nano'
51-
else
52-
echo "No editor available!" >&2
53-
return
54-
fi
55-
fi
56-
57-
echo "$EDITOR \$($SNIPPET get --file $index_id)"
58-
}
59-
60-
function snippet_url() {
61-
index_id=$(snippet_list ds)
62-
[ -z "$index_id" ] && return
63-
64-
# Make sure we have $BROWSER
65-
if [ -z "$BROWSER" ]; then
66-
if [ ! -z "$(where firefox)" ]; then
67-
BROWSER='firefox'
68-
elif [ ! -z "$(where chromium)" ]; then
69-
BROWSER='chromium'
70-
elif [ ! -z "$(where chrome)" ]; then
71-
BROWSER='chrome'
72-
elif [ ! -z "$(where xdg-open)" ]; then
73-
BROWSER='xdg-open'
74-
else
75-
echo "No browser available!" >&2
76-
return
77-
fi
78-
fi
79-
80-
echo "$BROWSER \$($SNIPPET get --url $index_id)"
81-
}
82-
8319
# vim: set shiftwidth=2:
Lines changed: 5 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
" NeoVim integration for sphinxnotes-snippet
2-
" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1+
" Vim integration for sphinxnotes-snippet
2+
" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
33
"
44
" :Author: Shengyu Zhang
55
" :Date: 2021-04-01
6-
" :Version: 20210413
6+
" :Version: 20210814
77
"
88
" NOTE: junegunn/fzf.vim is required
99

@@ -13,9 +13,9 @@ function! s:SplitID(row)
1313
return split(a:row, ' ')[0]
1414
endfunction
1515

16-
function! g:SphinxNotesSnippetList(callback, kinds)
16+
function! g:SphinxNotesSnippetList(callback, tags)
1717
let cmd = [s:snippet, 'list',
18-
\ '--kinds', a:kinds,
18+
\ '--tags', a:tags,
1919
\ '--width', &columns - 2,
2020
\ ]
2121
call fzf#run({
@@ -25,72 +25,4 @@ function! g:SphinxNotesSnippetList(callback, kinds)
2525
\ })
2626
endfunction
2727

28-
function! g:SphinxNotesSnippetListAndView()
29-
function! s:CallView(selection)
30-
call g:SphinxNotesSnippetView(s:SplitID(a:selection))
31-
endfunction
32-
call g:SphinxNotesSnippetList(function('s:CallView'), 'ds')
33-
endfunction
34-
35-
" https://github.com/anhmv/vim-float-window/blob/master/plugin/float-window.vim
36-
function! g:SphinxNotesSnippetView(id)
37-
let height = float2nr((&lines - 2) / 1.5)
38-
let row = float2nr((&lines - height) / 2)
39-
let width = float2nr(&columns / 1.5)
40-
let col = float2nr((&columns - width) / 2)
41-
42-
" Main Window
43-
let opts = {
44-
\ 'relative': 'editor',
45-
\ 'style': 'minimal',
46-
\ 'width': width,
47-
\ 'height': height,
48-
\ 'col': col,
49-
\ 'row': row,
50-
\ }
51-
52-
let buf = nvim_create_buf(v:false, v:true)
53-
" Global for :call
54-
let g:sphinx_notes_snippet_win = nvim_open_win(buf, v:true, opts)
55-
56-
" The content is always reStructuredText for now
57-
set filetype=rst
58-
" Press enter to return
59-
nmap <buffer> <CR> :call nvim_win_close(g:sphinx_notes_snippet_win, v:true)<CR>
60-
61-
let cmd = [s:snippet, 'get', '--text', a:id]
62-
execute '$read !' . join(cmd, ' ')
63-
execute '$read !' . '..'
64-
call append(line('$'), [
65-
\ '.. Inserted By sphinxnotes-snippet:',
66-
\ '',
67-
\ ' Press <ENTER> to return'])
68-
endfunction
69-
70-
function! g:SphinxNotesSnippetEdit(id)
71-
let cmd = [s:snippet, 'get', '--file', a:id]
72-
execute '$tabedit ' . system(join(cmd, ' '))
73-
endfunction
74-
75-
function! g:SphinxNotesSnippetListAndEdit()
76-
function! s:CallEdit(selection)
77-
call g:SphinxNotesSnippetEdit(s:SplitID(a:selection))
78-
endfunction
79-
call g:SphinxNotesSnippetList(function('s:CallEdit'), 'ds')
80-
endfunction
81-
82-
function! g:SphinxNotesSnippetUrl(id)
83-
let cmd = [s:snippet, 'get', '--url', a:id]
84-
for url in systemlist(join(cmd, ' '))
85-
echo system('xdg-open ' . shellescape(url))
86-
endfor
87-
endfunction
88-
89-
function! g:SphinxNotesSnippetListAndUrl()
90-
function! s:CallUrl(selection)
91-
call g:SphinxNotesSnippetUrl(s:SplitID(a:selection))
92-
endfunction
93-
call g:SphinxNotesSnippetList(function('s:CallUrl'), 'ds')
94-
endfunction
95-
9628
" vim: set shiftwidth=2:

sphinxnotes/snippet/integration/plugin.zsh

Lines changed: 0 additions & 28 deletions
This file was deleted.

0 commit comments

Comments
 (0)