2 # zaw - zsh anything.el-like widget
6 # add following line to your .zshrc::
8 # source /path/to/zaw.zsh
12 # to create namespace, use anonymous function
15 zmodload zsh
/parameter
16 autoload
-U is-at-least
18 local this_file
="${funcsourcetrace[1]%:*}"
19 if is-at-least
4.3.10; then
20 # "A" flag (turn a file name into an absolute path with symlink
21 # resolution) is only available on 4.3.10 and latter
22 local cur_dir
="${this_file:A:h}"
24 local cur_dir
="${this_file:h}"
26 fpath
+=("${cur_dir}/functions")
28 autoload
-U filter-select
30 typeset
-g -A zaw_sources
33 function zaw-register-src
() {
36 # zaw-register-src [-n NAME] SOURCE
38 # SOURCE is function name that define (overrite) these variables
40 # $candidates -> array of candidates
41 # $cands_assoc -> assoc array of candidates
42 # $cand_descriptions -> (optional) array of candidates descriptions
43 # $cand_descriptions_assoc -> (optional) assoc array of candidates descriptions
44 # $actions -> list of callback function names that receive selected item
45 # $act_descriptions -> (optional) list of callback function descriptions
46 # $options -> (optional) array of options passed to filter-select
48 # whether one of candidates or cands-assoc is required
49 local name func widget_name opts OPTARG OPTIND
51 while getopts 'n:' opts
; do
54 if (( OPTIND
> 1 )); then
55 shift $
(( OPTIND
- 1 ))
60 if [[ -z "${name}" ]]; then
64 # TODO: check name duplication
65 zaw_sources
+=("${name}" "${func}")
67 # define shortcut function
68 widget_name
="zaw-${(L)name// /-}"
69 eval "function ${widget_name} { zle zaw ${func} \$@ }"
70 eval "zle -N ${widget_name}"
76 local -a reply candidates actions act_descriptions options selected cand_descriptions
79 if [[ $# == 0 ]]; then
80 if zle zaw-select-src
; then
91 zle
-R "now loading ..."
93 # call source function to generate candidates
97 if [[ "${ret}" != 0 ]]; then
103 if (( ${#cand_descriptions} )); then
104 options
=("-d" "cand_descriptions" "${(@)options}")
106 # TODO: cand_descriptions_assoc
108 # call filter-select to allow user select item
109 if (( ${#cands_assoc} )); then
110 filter-select
-e select-action
-A cands_assoc
"${(@)options}"
112 filter-select
-e select-action
"${(@)options}" -- "${(@)candidates}"
115 if [[ $?
== 0 ]]; then
116 if (( ${#reply_marked} > 0 )); then
117 selected
=("${(@)reply_marked}")
119 selected
=("${reply[2]}")
122 case "${reply[1]}" in
124 action
="${actions[1]}"
127 action
="${actions[2]}"
131 filter-select
-t "select action for '${(j:', ':)selected}'" -d act_descriptions
-- "${(@)actions}"
134 if [[ $ret == 0 ]]; then
142 if [[ -n "${action}" ]]; then
143 "${action}" "${(@)selected}"
151 function zaw-select-src
() {
156 for name
in "${(@ko)zaw_sources}"; do
157 cands
+="${zaw_sources[${name}]}"
161 filter-select
-e select-action
-d descs
-- "${(@)cands}"
164 zle
-N zaw-select-src
167 function zaw-print-src
() {
168 local name func widget_name
169 printf '%-16s %s\n' "source name" "shortcut widget name"
170 print
-- '----------------------------------------'
171 for name
in "${(@ko)zaw_sources}"; do
172 widget_name
="zaw-${(L)name// /-}"
173 printf '%-16s %s\n' "${name}" "${widget_name}"
179 function zaw-callback-execute
() {
184 function zaw-callback-replace-buffer
() {
185 LBUFFER
="${(j:; :)@}"
189 function zaw-callback-append-to-buffer
() {
190 LBUFFER
="${BUFFER}${(j:; :)@}"
193 function zaw-callback-edit-file
() {
197 if [ ! ${ZAW_EDITOR} ]; then
201 BUFFER
="${ZAW_EDITOR} ${args}"
207 setopt local_options extended_glob
208 local src_dir
="${cur_dir}/sources" f
209 if [[ -d "${src_dir}" ]]; then
210 for f
in "${src_dir}"/^
*.zwc
; do
216 # only used for exit-zle-widget-name
217 function select-action
() {}; zle
-N select-action
219 bindkey
-M filterselect
'^i' select-action