Created
April 26, 2019 20:23
-
-
Save simlei/032470cfcd23641987f97a96749128d7 to your computer and use it in GitHub Desktop.
bash code that handles binds
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#TODO: these depend on fzf re-bindings to be present; rip them out and have that code here! | |
# bind '"\ec": "\C-x\C-addi`__fzf_cd__`\C-x\C-e\C-x\C-r\C-m"' | |
# bind -m vi-command '"\ec": "ddi`__fzf_cd__`\C-x\C-e\C-x\C-r\C-m"' | |
bindCmdVi() { #{{{ | |
local binding="$1" | |
shift | |
local cmd="$*" | |
local argNormal="$(printf '"%s": "\C-x\C-addi%s\C-x\C-e\C-x\C-r\C-m"' "$binding" "$cmd")" | |
local argCmd="$(printf '"%s": "ddi%s\C-x\C-e\C-x\C-r\C-m"' "$binding" "$cmd")" | |
bind "$argNormal" | |
bind -m vi-command "$argCmd" | |
} | |
bash_bind_inserteval() { #{{{ | |
local selected="$(eval "$*")" | |
# echo "$selected" to be evald >&2 | |
READLINE_LINE="${READLINE_LINE:0:$READLINE_POINT}$selected${READLINE_LINE:$READLINE_POINT}" | |
READLINE_POINT=$(( READLINE_POINT + ${#selected} )) | |
} | |
#}}} | |
#}}} | |
bindInsertEvalVi() { #{{{ | |
local binding="$1" | |
shift | |
local cmd="$*" | |
# echo bind -x "$(printf '"%s": "bash_bind_insert %s"' "$binding" "$cmd")" | |
local bindarg="$(printf '"%s":"bash_bind_inserteval %q"' "$binding" "$cmd")" | |
# echo bind -x "$bindarg" | |
bind -x "$bindarg" | |
#TODO: doesnt work yet: | |
bind -m vi-command "$(printf '"%s": "i%s"' "$binding" "$binding")" | |
} | |
#}}} | |
bindReplaceEvalVi() { #{{{ | |
local binding="$1" | |
local cmd="$2" | |
# bind '"\C-r": "\C-x\C-addi`__fzf_history__`\C-x\C-e\C-x\C-r\C-x^\C-x\C-a$a"' | |
# bind -m vi-command '"\C-r": "i\C-r"' | |
local argNormal="$(printf '"%s": "\C-x\C-addi`%s`\C-x\C-e\C-x\C-r\C-x^\C-x\C-a$a"' "$binding" "$cmd")" | |
local argCmd="$(printf '"%s": "i\C-x\C-addi`%s`\C-x\C-e\C-x\C-r\C-x^\C-x\C-a$a"' "$binding" "$cmd")" | |
# local argCmd="$(printf '"%s": "i\C-r"')" | |
bind "$argNormal" | |
bind -m vi-command "$argCmd" | |
} | |
#}}} | |
bindReplaceVi() { #{{{ | |
local binding="$1" | |
shift | |
local cmd="$*" | |
# bind '"\C-r": "\C-x\C-addi`__fzf_history__`\C-x\C-e\C-x\C-r\C-x^\C-x\C-a$a"' | |
# bind -m vi-command '"\C-r": "i\C-r"' | |
local argNormal="$(printf '"%s": "\C-x\C-addi%s"' "$binding" "$cmd")" | |
local argCmd="$(printf '"%s": "i\C-x\C-addi%s"' "$binding" "$cmd")" | |
bind "$argNormal" | |
bind -m vi-command "$argCmd" | |
} | |
#}}} | |
# interactive: cd function | |
if [[ "$CDHIST_ENABLE" == 1 ]]; then | |
cdhist_dir="$TS_ROOT/.unmanaged/.cdhist" | |
if [[ ! -d "$cdhist_dir" ]]; then | |
mkdir -p "$cdhist_dir" | |
fi | |
if [[ -z "${CDHIST_SESS_NR+set}" ]]; then | |
export CDHIST_SESS_NR=$RANDOM | |
export CDHIST_LOCALFILE="$cdhist_dir/$CDHIST_SESS_NR.cdhist" | |
export CDHIST_GLOBALFILE="$cdhist_dir/cdhist_global" | |
fi | |
cd() { | |
__cd_recorder "$@" | |
} | |
fi | |
__cd_recorder () | |
{ | |
echo $PWD >> "$CDHIST_LOCALFILE" | |
echo $PWD >> "$CDHIST_GLOBALFILE" | |
builtin cd "$@" | |
} | |
__list_cd_local () { | |
cat "$CDHIST_LOCALFILE" | tac | deduplicate | tac | |
} | |
__list_cd_global () { | |
cat "$CDHIST_GLOBALFILE" | tac | deduplicate | tac | |
} | |
decompress() { #{{{ | |
local opt | |
local OPTIND=1 | |
while getopts "hv" opt; do | |
case "$opt" in | |
h) | |
cat <<End-Of-Usage | |
Usage: ${FUNCNAME[0]} [option] <archives> | |
options: | |
-h show this message and exit | |
-v verbosely list files processed | |
End-Of-Usage | |
return | |
;; | |
v) | |
local -r verbose='v' | |
;; | |
?) | |
decompress -h >&2 | |
return 1 | |
;; | |
esac | |
done | |
shift $((OPTIND-1)) | |
[ $# -eq 0 ] && decompress -h && return 1 | |
while [ $# -gt 0 ]; do | |
if [ -f "$1" ]; then | |
case "$1" in | |
*.tar.bz2|*.tbz|*.tbz2) tar "x${verbose}jf" "$1" ;; | |
*.tar.gz|*.tgz) tar "x${verbose}zf" "$1" ;; | |
*.tar.xz) xz --decompress "$1"; set -- "$@" "${1:0:-3}" ;; | |
*.tar.Z) uncompress "$1"; set -- "$@" "${1:0:-2}" ;; | |
*.bz2) bunzip2 "$1" ;; | |
*.deb) dpkg-deb -x${verbose} "$1" "${1:0:-4}" ;; | |
*.pax.gz) gunzip "$1"; set -- "$@" "${1:0:-3}" ;; | |
*.gz) gunzip "$1" ;; | |
*.pax) pax -r -f "$1" ;; | |
*.pkg) pkgutil --expand "$1" "${1:0:-4}" ;; | |
*.rar) unrar x "$1" ;; | |
*.rpm) rpm2cpio "$1" | cpio -idm${verbose} ;; | |
*.tar) tar "x${verbose}f" "$1" ;; | |
*.txz) mv "$1" "${1:0:-4}.tar.xz"; set -- "$@" "${1:0:-4}.tar.xz" ;; | |
*.xz) xz --decompress "$1" ;; | |
*.zip|*.war|*.jar) unzip "$1" ;; | |
*.Z) uncompress "$1" ;; | |
*.7z) 7za x "$1" ;; | |
*) echo "'$1' cannot be decompressed via decompress" >&2;; | |
esac | |
else | |
echo "decompress: '$1' is not a valid file" >&2 | |
fi | |
shift | |
done | |
} | |
#}}} | |
# not failed anymore attempt with insertion, -x and all in one with MACRO #{{{ | |
__insertevalMacroFilter() { | |
local line="$1" | |
if [[ "$line" =~ ^\s*@@MACRO:\s*(.*)$ ]]; then | |
local toEval="${BASH_REMATCH[1]}" | |
# local args="${BASH_REMATCH[2]}" | |
# echo would do doWith "$toEval" :: cat < <(echo "$args") >&2 | |
echo "$toEval" | |
fi | |
} | |
__insertevalNonmacroFilter() { | |
local line="$1" | |
if [[ "$line" =~ ^\s*@@MACRO:(.*)$ ]]; then | |
: | |
else | |
echo "$line" | |
fi | |
} | |
# maintain state | |
bind -x '"\200": _TEMP_LINE=$READLINE_LINE; _TEMP_POINT=$READLINE_POINT' | |
bind -x '"\201": READLINE_LINE=$_TEMP_LINE; READLINE_POINT=$_TEMP_POINT; unset _TEMP_POINT; unset _TEMP_LINE' | |
_BINDING_DETOUR_COUNTER=206 | |
_makeDetourBinding() { | |
echo "\\"$_BINDING_DETOUR_COUNTER | |
_BINDING_DETOUR_COUNTER=$((_BINDING_DETOUR_COUNTER+1)) | |
} | |
_readline_insert_with_macros() { #{{{ | |
# echo "$selected" to be evald >&2 | |
# READLINE_LINE="${READLINE_LINE:0:$READLINE_POINT}$1${READLINE_LINE:$READLINE_POINT}" | |
# READLINE_POINT=$(( READLINE_POINT + ${#1} )) | |
_TEMP_LINE="${_TEMP_LINE:0:$_TEMP_POINT}$1${_TEMP_LINE:$_TEMP_POINT}" | |
_TEMP_POINT=$(( _TEMP_POINT + ${#1} )) | |
} | |
_bash_bind_inserteval_withmacros() { #{{{ | |
local selected="$(eval "$*")" | |
local macros="$(doWith '__insertevalMacroFilter {}' :: echo "$selected")" | |
local content="$(doWith '__insertevalNonmacroFilter {}' :: echo "$selected")" | |
# echo "selected: $selected" >&2 | |
# echo "macros: $macros" >&2 | |
# echo "content: $content" >&2 | |
if [[ $macros ]]; then | |
doWith 'echo macro: {}; eval {}' :: echo "$macros" | |
fi | |
if [[ $content ]]; then | |
_readline_insert_with_macros "$content" | |
fi | |
# echo "$selected" to be evald >&2 | |
# READLINE_LINE="${READLINE_LINE:0:$READLINE_POINT}$content${READLINE_LINE:$READLINE_POINT}" | |
# READLINE_POINT=$(( READLINE_POINT + ${#content} )) | |
} | |
bindInsertEvalWithMacrosVi() { #{{{ | |
local binding="$1" | |
shift | |
local cmd="$*" | |
# echo bind -x "$(printf '"%s": "bash_bind_insert %s"' "$binding" "$cmd")" | |
local bindingN="$binding" | |
local bindingX="$(_makeDetourBinding)" | |
# local bindingX="\206" | |
local bindargX="$(printf '"%s":"_bash_bind_inserteval_withmacros %q"' "$bindingX" "$cmd")" | |
local bindargN="$(printf '"%s":"\200\C-x\C-addi%s\C-m\201"' "$bindingN" "$bindingX")" | |
# echo bind -x "$bindarg" | |
echo bind -x "$bindargX" | |
echo bind "$bindargN" | |
bind -x "$bindargX" | |
bind "$bindargN" | |
# bind '"\eh":"\200\C-a\C-k\206\C-m\201"' | |
# bind -x '"\206": "cd .."' | |
#TODO: doesnt work yet: | |
bind -m vi-command "$(printf '"%s": "i%s"' "$binding" "$binding")" | |
} | |
#}}} | |
#}}} | |
# vim: fdm=marker |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment