forked from aperezdc/zsh-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
notes.plugin.zsh
153 lines (131 loc) · 2.57 KB
/
notes.plugin.zsh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#! /bin/zsh
#
# notes.plugin.zsh
# Copyright (C) 2018 Adrian Perez <[email protected]>
#
# Distributed under terms of the MIT license.
#
function notes-home
{
emulate -L zsh
local p
zstyle -s :notes home p || p="${HOME}/Notes"
builtin print -rl "${p:a}"
}
function notes-list-files
{
emulate -L zsh
setopt local_options nullglob
print -nrl "$(notes-home)"/**/*.md(.on) ''
}
function --notes-list-nullsep
{
emulate -L zsh
setopt local_options nullglob
print -nrN "$(notes-home)"/**/*.md(om:t:r) ''
}
function --notes-list-lines
{
emulate -L zsh
setopt local_options nullglob
print -nrl "$(notes-home)"/**/*.md(.on:t:r) ''
}
function notes-list
{
--notes-list-lines "$@"
}
function --notes-pick-fzf-or-skim
{
emulate -L zsh
local bin=$1
shift
local -a cmd=(
--read0
--ansi
--bind=ctrl-n:print-query
--prompt='(notes) '
"$@"
)
# Add preview command, if configured.
local -a previewcmd
if zstyle -t :notes:widget:preview enabled ; then
if ! zstyle -a :notes:widget:preview command previewcmd ; then
previewcmd=(cat)
fi
cmd+=(
--preview="${previewcmd[*]} '$(notes-home)'/{}.md"
--bind=tab:toggle-preview
--preview-window=right
)
fi
command "${bin}" "${cmd[@]}"
}
function --notes-pick-fzf
{
emulate -L zsh
--notes-pick-fzf-or-skim fzf --layout=reverse --inline-info --query="${1:-}"
}
function --notes-pick-skim
{
emulate -L zsh
--notes-pick-fzf-or-skim sk --reverse --query="${1:-}"
}
function --notes-pick-fzy
{
emulate -L zsh
command fzy --prompt='(notes) ' --query="${1:-}"
}
function notes-pick
{
emulate -L zsh
local pick
local list='nullsep'
if zstyle -s :notes:widget picker pick ; then
case "${pick}" in
fzf | skim)
;;
fzy)
list='lines'
;;
*)
print -lu 2 "Unsupported picker: ${pick}"
return 1
;;
esac
else
pick='fzf'
fi
local H=$(notes-home)
local chosen=$("--notes-list-${list}" | "--notes-pick-${pick}" "$@")
if [[ -z ${chosen} ]] ; then
return 1
fi
print -rl "$H/${chosen}.md"
}
function notes-edit-widget
{
emulate -L zsh
setopt local_options err_return
local -a editor=( "${(f)$(whence -p "${EDITOR}" "${VISUAL}" vim nvim vi)}" )
if [[ ${#editor} -eq 0 ]] ; then
print '\bCannot find a suitable text editor' 1>&2
zle redisplay
return
fi
local H=$(notes-home)
while true ; do
local chosen=$(notes-pick)
if [[ -z ${chosen} ]] ; then
break
fi
if [[ ! -r ${chosen} && ! -d $H ]] ; then
command mkdir -p "$H"
fi
command "${editor[1]}" "${chosen}" < /dev/tty
if zstyle -t :notes:widget once ; then
break
fi
done
zle redisplay
}
zle -N notes-edit-widget