Skip to content

Commit

Permalink
new script "zaw" zsh anything like widget
Browse files Browse the repository at this point in the history
  • Loading branch information
nakamuray committed Feb 22, 2011
1 parent 68cc496 commit e93afd5
Show file tree
Hide file tree
Showing 2 changed files with 203 additions and 0 deletions.
35 changes: 35 additions & 0 deletions sources/history.zsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
zmodload zsh/parameter

function zaw-src-history() {
cands_assoc=("${(@kv)history}")
actions=("zaw-callback-history-execute" "zaw-callback-history-replace" "zaw-callback-history-append")
act_descriptions=("execute" "replace edit buffer" "append to edit buffer")
options="-r"
}

zle -N zaw-source-history

zaw-register-src -n history zaw-src-history


function zaw-callback-history-execute() {
BUFFER="$1"
zle accept-line
}

zle -N zaw-callback-history-execute


function zaw-callback-history-replace() {
LBUFFER="$1"
RBUFFER=""
}

zle -N zaw-callback-history-replace


function zaw-callback-history-append() {
LBUFFER="${BUFFER}$1"
}

zle -N zaw-callback-history-append
168 changes: 168 additions & 0 deletions zaw.zsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
#
# zaw - zsh anything like widget
#
# usage:
#
# add following line to your .zshrc::
#
# source /path/to/zaw.zsh
#
# and type "^X^Z"

local cur_dir="${${(%):-%N}:a:h}"
fpath+=("${cur_dir}")

autoload -U filter-select

zaw_sources=()

function zaw-register-src() {
# register zaw source
#
# zaw-register-src [-n NAME] SOURCE
#
# SOURCE is function name that define (overrite) these variables
#
# $candidates -> array of candidates
# $cands_assoc -> assoc array of candidates
# $descriptions -> (optional) array or assoc array of candidates descriptions
# $actions -> list of callback function names that receive selected item
# $act_descriptions -> (optional) list of callback function descriptions
# $options -> (optional) options passed to filter-select
#
# whether one of candidates or cands-assoc is required
local name func widget_name

while getopts 'n:' opts; do
name="${OPTARG}"
done
if (( OPTIND > 1 )); then
shift $(( OPTIND - 1 ))
fi

func="$1"

if [[ -z "${name}" ]]; then
name="${func}"
fi

# TODO: check name duplication
zaw_sources+=("${name}" "${func}")

# define shortcut function
widget_name="zaw-${(L)name// /-}"
eval "function ${widget_name} { zle zaw ${func} }"
eval "zle -N ${widget_name}"
}


function zaw() {
local options selected action
local -a reply candidates actions act_descriptions
local -A cands_assoc

# save ZLE related variables
local orig_lbuffer="${LBUFFER}"
local orig_rbuffer="${RBUFFER}"
LBUFFER=""
RBUFFER=""

if [[ $# == 0 ]]; then
if zle zaw-select-src; then
func="${reply[2]}"
reply=()
else
LBUFFER="${orig_lbuffer}"
RBUFFER="${orig_rbuffer}"
return 0
fi
else
func="$1"
fi

# call source function to generate candidates
"${func}"

reply=()

# call filter-select to allow user select item
if (( $#cands_assoc )); then
filter-select -e select-action -A cands_assoc ${options}
else
filter-select -e select-action ${options} -- "${(@)candidates}"
fi

if [[ $? == 0 ]]; then
selected="${reply[2]}"

case "${reply[1]}" in
accept-line)
action="${actions[1]}"
;;
accept-search)
action="${actions[2]}"
;;
select-action)
reply=()
filter-select -t "select action for '${selected}'" -d act_descriptions -- "${(@)actions}"
ret=$?

if [[ $ret == 0 ]]; then
action="${reply[2]}"
else
LBUFFER="${orig_lbuffer}"
RBUFFER="${orig_rbuffer}"
return 1
fi
;;
esac

LBUFFER="${orig_lbuffer}"
RBUFFER="${orig_rbuffer}"

"${action}" "${selected}"
else
LBUFFER="${orig_lbuffer}"
RBUFFER="${orig_rbuffer}"
fi
}

zle -N zaw


function zaw-select-src() {
local -a cands descs
cands=()
descs=()
for name func in "${(@)zaw_sources}"; do
cands+="${func}"
descs+="${name}"
done

filter-select -e select-action -d descs -- "${(@)cands}"
}

zle -N zaw-select-src


function zaw-print-src() {
local name func widget_name
for name func in "${(@)zaw_sources}"; do
widget_name="zaw-${(L)name// /-}"
print "${name}" "${widget_name}"
done
}


# load zaw sources
local src_dir="${cur_dir}/sources"
if [[ -d "${src_dir}" ]]; then
for f ("${src_dir}"/*) source "${f}"
fi

# dummy function
function select-action() {}; zle -N select-action
filter-select -i
bindkey -M filterselect '^i' select-action

bindkey '^X^Z' zaw

0 comments on commit e93afd5

Please sign in to comment.