1
0
Fork 0
mirror of https://github.com/zdharma-continuum/ztrace.git synced 2025-01-30 17:08:23 +01:00
ztrace/h2-list-input
Sebastian Gniazdowski 76793c56dc History subwindow
2016-05-21 16:19:02 +02:00

289 lines
7.5 KiB
Bash

# Copy this file into /usr/share/zsh/site-functions/
# and add 'autoload h2-list-input` to .zshrc
#
# This is an internal function not for direct use
emulate -L zsh
zmodload zsh/curses
setopt typesetsilent
# Compute first to show index
_hlist_compute_first_to_show_idx() {
from_what_idx_list_is_shown=0+((current_idx-1)/page_height)*page_height+1
}
typeset -ga reply
reply=( -1 '' )
integer current_idx="$1"
integer from_what_idx_list_is_shown="$2"
integer page_height="$3"
integer page_width="$4"
integer last_element="$5"
integer nseg="$6"
integer hscroll="$7"
local key="$8"
integer search="$9"
local buffer="$10"
integer uniq_mode="$11"
integer disable_search="$12"
#
# Listening for input
#
if [ "$search" = "0" ]; then
case "$key" in
(UP|k|$'\C-P')
# Are there any elements before the current one?
[ "$current_idx" -gt 1 ] && current_idx=current_idx-1;
_hlist_compute_first_to_show_idx
;;
(DOWN|j|$'\C-N')
# Are there any elements after the current one?
[ "$current_idx" -lt "$last_element" ] && current_idx=current_idx+1;
_hlist_compute_first_to_show_idx
;;
(PPAGE|$'\b'|$'\C-?'|BACKSPACE)
current_idx=current_idx-page_height
[ "$current_idx" -lt 1 ] && current_idx=1;
_hlist_compute_first_to_show_idx
;;
(NPAGE|" ")
current_idx=current_idx+page_height
[ "$current_idx" -gt "$last_element" ] && current_idx=last_element;
_hlist_compute_first_to_show_idx
;;
($'\C-U')
current_idx=current_idx-page_height/2
[ "$current_idx" -lt 1 ] && current_idx=1;
_hlist_compute_first_to_show_idx
;;
($'\C-D')
current_idx=current_idx+page_height/2
[ "$current_idx" -gt "$last_element" ] && current_idx=last_element;
_hlist_compute_first_to_show_idx
;;
(HOME|g)
current_idx=1
_hlist_compute_first_to_show_idx
;;
(END|G)
current_idx=last_element
_hlist_compute_first_to_show_idx
;;
($'\n'|ENTER)
# Is that element selectable?
# Check for this only when there is no search
if [[ "$HLIST_SEARCH_BUFFER" != "" || "$HLIST_IS_UNIQ_MODE" -eq 1 ||
${HLIST_NONSELECTABLE_ELEMENTS[(r)$current_idx]} != $current_idx ]]
then
# Save current element in the result variable
reply=( $current_idx SELECT )
fi
;;
(q)
reply=( -1 QUIT )
;;
(/)
if [ "$disable_search" = "0" ]; then
search=1
_hlist_cursor_visibility 1
fi
;;
($'\t')
reply=( $current_idx LEAVE )
;;
($'\C-L')
reply=( -1 REDRAW )
;;
(\])
if [[ "${HLIST_ENABLED_EVENTS[(r)BR_MOVE_RIGHT]}" = "BR_MOVE_RIGHT" ]]; then
reply=( "$HLIST_CURRENT_SEGMENT" "BR_MOVE_RIGHT" )
else
[[ "${(t)HLIST_HOP_INDEXES}" = "array" || "${(t)HLIST_HOP_INDEXES}" = "array-local" ]] &&
[ -z "$HLIST_SEARCH_BUFFER" ] && [ "$HLIST_IS_UNIQ_MODE" -eq 0 ] &&
for idx in "${(n)HLIST_HOP_INDEXES[@]}"; do
if [ "$idx" -gt "$current_idx" ]; then
current_idx=$idx
_hlist_compute_first_to_show_idx
break
fi
done
fi
;;
(\[)
if [[ "${HLIST_ENABLED_EVENTS[(r)BR_MOVE_LEFT]}" = "BR_MOVE_LEFT" ]]; then
reply=( "$HLIST_CURRENT_SEGMENT" "BR_MOVE_LEFT" )
else
[[ "${(t)HLIST_HOP_INDEXES}" = "array" || "${(t)HLIST_HOP_INDEXES}" = "array-local" ]] &&
[ -z "$HLIST_SEARCH_BUFFER" ] && [ "$HLIST_IS_UNIQ_MODE" -eq 0 ] &&
for idx in "${(nO)HLIST_HOP_INDEXES[@]}"; do
if [ "$idx" -lt "$current_idx" ]; then
current_idx=$idx
_hlist_compute_first_to_show_idx
break
fi
done
fi
;;
('<'|'{')
hscroll=hscroll-7
[ "$hscroll" -lt 0 ] && hscroll=0
;;
('>'|'}')
hscroll+=7
;;
(LEFT|'h')
if [ "$HLIST_CURRENT_SEGMENT" -gt "$nseg" ]; then
(( HLIST_CURRENT_SEGMENT = nseg - 1 ))
[ "$HLIST_CURRENT_SEGMENT" -le 0 ] && HLIST_CURRENT_SEGMENT=1
else
(( HLIST_CURRENT_SEGMENT = HLIST_CURRENT_SEGMENT - 1 ))
fi
;;
(RIGHT|'l')
(( HLIST_CURRENT_SEGMENT = HLIST_CURRENT_SEGMENT + 1 ))
[ "$HLIST_CURRENT_SEGMENT" -gt "$nseg" ] && HLIST_CURRENT_SEGMENT="$nseg"
;;
(SLEFT|'H')
reply=( "$HLIST_CURRENT_SEGMENT" MOVE_LEFT )
;;
(SRIGHT|'L')
reply=( "$HLIST_CURRENT_SEGMENT" MOVE_RIGHT )
;;
(F1|F2|F3|F4)
reply=( -1 "$key" )
;;
($'\E')
buffer=""
;;
(o|$'\C-O')
uniq_mode=1-uniq_mode
;;
(e|$'\C-E')
reply=( "$current_idx" "SUBWIN" )
;;
(*)
;;
esac
else
case "$key" in
($'\n'|ENTER)
search=0
_hlist_cursor_visibility 0
;;
($'\C-L')
reply=( -1 REDRAW )
;;
#
# Slightly limited navigation
#
(UP|$'\C-P')
[ "$current_idx" -gt 1 ] && current_idx=current_idx-1;
_hlist_compute_first_to_show_idx
;;
(DOWN|$'\C-N')
[ "$current_idx" -lt "$last_element" ] && current_idx=current_idx+1;
_hlist_compute_first_to_show_idx
;;
(PPAGE)
current_idx=current_idx-page_height
[ "$current_idx" -lt 1 ] && current_idx=1;
_hlist_compute_first_to_show_idx
;;
(NPAGE)
current_idx=current_idx+page_height
[ "$current_idx" -gt "$last_element" ] && current_idx=last_element;
_hlist_compute_first_to_show_idx
;;
($'\C-U')
current_idx=current_idx-page_height/2
[ "$current_idx" -lt 1 ] && current_idx=1;
_hlist_compute_first_to_show_idx
;;
($'\C-D')
current_idx=current_idx+page_height/2
[ "$current_idx" -gt "$last_element" ] && current_idx=last_element;
_hlist_compute_first_to_show_idx
;;
(HOME)
current_idx=1
_hlist_compute_first_to_show_idx
;;
(END)
current_idx=last_element
_hlist_compute_first_to_show_idx
;;
(LEFT)
if [ "$HLIST_CURRENT_SEGMENT" -gt "$nseg" ]; then
(( HLIST_CURRENT_SEGMENT = nseg - 1 ))
[ "$HLIST_CURRENT_SEGMENT" -le 0 ] && HLIST_CURRENT_SEGMENT=1
else
(( HLIST_CURRENT_SEGMENT = HLIST_CURRENT_SEGMENT - 1 ))
fi
;;
(RIGHT)
(( HLIST_CURRENT_SEGMENT = HLIST_CURRENT_SEGMENT + 1 ))
[ "$HLIST_CURRENT_SEGMENT" -gt "$nseg" ] && HLIST_CURRENT_SEGMENT="$nseg"
;;
(F1|F2|F3|F4)
reply=( -1 "$key" )
;;
(F4|F5|F6|F7|F8|F9|F10)
# ignore
;;
#
# The input
#
($'\b'|$'\C-?'|BACKSPACE)
buffer="${buffer%?}"
;;
($'\C-W')
[ "$buffer" = "${buffer% *}" ] && buffer="" || buffer="${buffer% *}"
;;
($'\C-K')
buffer=""
;;
($'\E')
buffer=""
search=0
_hlist_cursor_visibility 0
;;
($'\C-O')
uniq_mode=1-uniq_mode
;;
($'\C-E')
reply=( "$current_idx" "SUBWIN" )
;;
(*)
if [[ $#key == 1 && $((#key)) -lt 31 ]]; then
# ignore all other control keys
else
buffer+="$key"
fi
;;
esac
fi
reply[3]="$current_idx"
reply[4]="$from_what_idx_list_is_shown"
reply[5]="$hscroll"
reply[6]="$search"
reply[7]="$buffer"
reply[8]="$uniq_mode"
# vim: set filetype=zsh: