Commit afd28bf1 authored by Marc Cornellà's avatar Marc Cornellà
Browse files

Merge pull request #4368 from psprint/master

Update Zsh Navigation Tools plugin
parents fea74b4b 4c292ea2
# Copy this file into /usr/share/zsh/site-functions/
# and add 'autoload n-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
_nlist_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 hscroll="$6"
local key="$7"
integer search="$8"
local buffer="$9"
integer uniq_mode="$10"
#
# 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;
_nlist_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;
_nlist_compute_first_to_show_idx
;;
(PPAGE)
current_idx=current_idx-page_height
[ "$current_idx" -lt 1 ] && current_idx=1;
_nlist_compute_first_to_show_idx
;;
(NPAGE|" ")
current_idx=current_idx+page_height
[ "$current_idx" -gt "$last_element" ] && current_idx=last_element;
_nlist_compute_first_to_show_idx
;;
($'\C-U')
current_idx=current_idx-page_height/2
[ "$current_idx" -lt 1 ] && current_idx=1;
_nlist_compute_first_to_show_idx
;;
($'\C-D')
current_idx=current_idx+page_height/2
[ "$current_idx" -gt "$last_element" ] && current_idx=last_element;
_nlist_compute_first_to_show_idx
;;
(HOME|g)
current_idx=1
_nlist_compute_first_to_show_idx
;;
(END|G)
current_idx=last_element
_nlist_compute_first_to_show_idx
;;
($'\n')
# Is that element selectable?
# Check for this only when there is no search
if [[ "$NLIST_SEARCH_BUFFER" != "" || "$NLIST_IS_UNIQ_MODE" -eq 1 ||
${NLIST_NONSELECTABLE_ELEMENTS[(r)$current_idx]} != $current_idx ]]
then
# Save current element in the result variable
reply=( $current_idx SELECT )
fi
;;
(q)
reply=( -1 QUIT )
;;
(/)
search=1
_nlist_cursor_visibility 1
;;
($'\t')
reply=( $current_idx LEAVE )
;;
($'\C-L')
reply=( -1 REDRAW )
;;
(\])
[[ "${(t)NLIST_HOP_INDEXES}" = "array" || "${(t)NLIST_HOP_INDEXES}" = "array-local" ]] &&
[ -z "$NLIST_SEARCH_BUFFER" ] && [ "$NLIST_IS_UNIQ_MODE" -eq 0 ] &&
for idx in "${(n)NLIST_HOP_INDEXES[@]}"; do
if [ "$idx" -gt "$current_idx" ]; then
current_idx=$idx
_nlist_compute_first_to_show_idx
break
fi
done
;;
(\[)
[[ "${(t)NLIST_HOP_INDEXES}" = "array" || "${(t)NLIST_HOP_INDEXES}" = "array-local" ]] &&
[ -z "$NLIST_SEARCH_BUFFER" ] && [ "$NLIST_IS_UNIQ_MODE" -eq 0 ] &&
for idx in "${(nO)NLIST_HOP_INDEXES[@]}"; do
if [ "$idx" -lt "$current_idx" ]; then
current_idx=$idx
_nlist_compute_first_to_show_idx
break
fi
done
;;
('<'|'{'|LEFT|'h')
hscroll=hscroll-7
[ "$hscroll" -lt 0 ] && hscroll=0
;;
('>'|'}'|RIGHT|'l')
hscroll+=7
;;
($'\E')
buffer=""
;;
(o|$'\C-O')
uniq_mode=1-uniq_mode
;;
(*)
;;
esac
else
case "$key" in
($'\n')
search=0
_nlist_cursor_visibility 0
;;
($'\C-L')
reply=( -1 REDRAW )
;;
#
# Slightly limited navigation
#
(UP|$'\C-P')
[ "$current_idx" -gt 1 ] && current_idx=current_idx-1;
_nlist_compute_first_to_show_idx
;;
(DOWN|$'\C-N')
[ "$current_idx" -lt "$last_element" ] && current_idx=current_idx+1;
_nlist_compute_first_to_show_idx
;;
(PPAGE)
current_idx=current_idx-page_height
[ "$current_idx" -lt 1 ] && current_idx=1;
_nlist_compute_first_to_show_idx
;;
(NPAGE)
current_idx=current_idx+page_height
[ "$current_idx" -gt "$last_element" ] && current_idx=last_element;
_nlist_compute_first_to_show_idx
;;
($'\C-U')
current_idx=current_idx-page_height/2
[ "$current_idx" -lt 1 ] && current_idx=1;
_nlist_compute_first_to_show_idx
;;
($'\C-D')
current_idx=current_idx+page_height/2
[ "$current_idx" -gt "$last_element" ] && current_idx=last_element;
_nlist_compute_first_to_show_idx
;;
(HOME)
current_idx=1
_nlist_compute_first_to_show_idx
;;
(END)
current_idx=last_element
_nlist_compute_first_to_show_idx
;;
(LEFT)
hscroll=hscroll-7
[ "$hscroll" -lt 0 ] && hscroll=0
;;
(RIGHT)
hscroll+=7
;;
(F1|F2|F3|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
_nlist_cursor_visibility 0
;;
($'\C-O')
uniq_mode=1-uniq_mode
;;
(*)
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:
# Copy this file into /usr/share/zsh/site-functions/
# and add 'autoload n-options` to .zshrc
#
# This function allows to browse and toggle shell's options
#
# Uses n-list
#emulate -L zsh
zmodload zsh/curses
local IFS="
"
unset NLIST_COLORING_PATTERN
[ -f ~/.config/znt/n-list.conf ] && . ~/.config/znt/n-list.conf
[ -f ~/.config/znt/n-options.conf ] && . ~/.config/znt/n-options.conf
# TODO restore options
unsetopt localoptions
integer kshoptionprint=0
[[ -o kshoptionprint ]] && kshoptionprint=1
setopt kshoptionprint
local list
local selected
local option
local state
# 0 - don't remember, 1 - remember, 2 - init once, then remember
NLIST_REMEMBER_STATE=2
local NLIST_GREP_STRING="${1:=}"
while (( 1 )); do
list=( `setopt` )
list=( "${(M)list[@]:#*${1:=}*}" )
list=( "${list[@]:#kshoptionprint*}" )
if [ "$#list" -eq 0 ]; then
echo "No matching options"
break
fi
local red=$'\x1b[00;31m' green=$'\x1b[00;32m' reset=$'\x1b[00;00m'
list=( "${list[@]/ off/${red} off$reset}" )
#list=( "${list[@]/ on/${green} on$reset}" )
list=( "${(i)list[@]}" )
n-list "${list[@]}"
if [ "$REPLY" -gt 0 ]; then
[[ -o ksharrays ]] && selected="${reply[$(( REPLY - 1 ))]}" || selected="${reply[$REPLY]}"
option="${selected%% *}"
state="${selected##* }"
if [[ -o globsubst ]]; then
unsetopt globsubst
state="${state%$reset}"
setopt globsubst
else
state="${state%$reset}"
fi
# Toggle the option
if [ "$state" = "on" ]; then
echo "Setting |$option| to off"
unsetopt "$option"
else
echo "Setting |$option| to on"
setopt "$option"
fi
else
break
fi
done
NLIST_REMEMBER_STATE=0
[[ "$kshoptionprint" -eq 0 ]] && unsetopt kshoptionprint
# vim: set filetype=zsh:
# Copy this file into /usr/share/zsh/site-functions/
# and add 'autoload n-panelize` to .zshrc
#
# This function somewhat reminds the panelize feature from Midnight Commander
# It allows browsing output of arbitrary command. Example usage:
# v-panelize ls /usr/local/bin
#
# Uses n-list
emulate -L zsh
setopt extendedglob
zmodload zsh/curses
local IFS="
"
unset NLIST_COLORING_PATTERN
[ -f ~/.config/znt/n-list.conf ] && . ~/.config/znt/n-list.conf
[ -f ~/.config/znt/n-panelize.conf ] && . ~/.config/znt/n-panelize.conf
local list
local selected
NLIST_REMEMBER_STATE=0
if [ -t 0 ]; then
# Check if there is proper input
if [ "$#" -lt 1 ]; then
echo "Usage: n-panelize {command} [option|argument] ... or command | n-panelize"
return 1
fi
list=( `"$@"` )
# TODO: $? doesn't reach user
[ "$?" -eq 127 ] && return $?
else
# Check if can reattach to terminal
if [[ ! -c /dev/tty && ! -t 2 ]]; then
echo "No terminal available (no /dev/tty)"
return 1
fi
list=( "${(@f)"$(<&0)"}" )
if [[ ! -c /dev/tty ]]; then
exec <&2
else
exec </dev/tty
fi
fi
n-list "${list[@]}"
if [ "$REPLY" -gt 0 ]; then
selected="$reply[REPLY]"
print -zr "# $selected"
fi
# vim: set filetype=zsh:
autoload znt-usetty-wrapper n-cd
local NLIST_START_IN_SEARCH_MODE=0
local NLIST_START_IN_UNIQ_MODE=0
znt-usetty-wrapper n-cd "$@"
unset NLIST_START_IN_SEARCH_MODE
unset NLIST_START_IN_UNIQ_MODE
autoload znt-usetty-wrapper n-history
local NLIST_START_IN_SEARCH_MODE=1
local NLIST_START_IN_UNIQ_MODE=1
znt-usetty-wrapper n-history "$@"
unset NLIST_START_IN_SEARCH_MODE
unset NLIST_START_IN_UNIQ_MODE
autoload znt-usetty-wrapper n-kill
local NLIST_START_IN_SEARCH_MODE=0
local NLIST_START_IN_UNIQ_MODE=0
znt-usetty-wrapper n-kill "$@"
unset NLIST_START_IN_SEARCH_MODE
unset NLIST_START_IN_UNIQ_MODE
emulate -L zsh
zmodload zsh/curses
test_fd0() {
true <&0
}
local restore=0 FD
# Reattach to terminal
if [ ! -t 0 ]; then
# Check if can reattach to terminal in any way
if [[ ! -c /dev/tty && ! -t 2 ]]; then
echo "No terminal available (no /dev/tty and no terminal at stderr)"
return 1
fi
if test_fd0 2>/dev/null; then
exec {FD}<&0
restore=2
else
restore=1
fi
if [[ ! -c /dev/tty ]]; then
exec <&2
else
exec </dev/tty
fi
fi
# Run the command
"$@"
# Restore FD state
(( restore == 1 )) && exec <&-
(( restore == 2 )) && exec <&$FD && exec {FD}<&-
# vim: set filetype=zsh:
#!/usr/bin/env zsh
REPO_DIR="${0%/*}"
CONFIG_DIR="$HOME/.config/znt"
#
# Copy configs
#
if ! test -d "$HOME/.config"; then
mkdir "$HOME/.config"
fi
if ! test -d "$CONFIG_DIR"; then
mkdir "$CONFIG_DIR"
fi
set n-aliases.conf n-env.conf n-history.conf n-list.conf n-panelize.conf n-cd.conf n-functions.conf n-kill.conf n-options.conf
for i; do
if ! test -f "$CONFIG_DIR/$i"; then
cp "$REPO_DIR/.config/znt/$i" "$CONFIG_DIR"
fi
done
#
# Load functions
#
autoload n-aliases n-cd n-env n-functions n-history n-kill n-list n-list-draw n-list-input n-options n-panelize
autoload znt-usetty-wrapper znt-history-widget znt-cd-widget znt-kill-widget
alias naliases=n-aliases ncd=n-cd nenv=n-env nfunctions=n-functions nhistory=n-history
alias nkill=n-kill noptions=n-options npanelize=n-panelize
zle -N znt-history-widget
bindkey '^R' znt-history-widget
setopt AUTO_PUSHD HIST_IGNORE_DUPS PUSHD_IGNORE_DUPS
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment