Compare commits

..

No commits in common. "e646389fab846c796e1028ae187e4bb7551896ac" and "d50534572d71195b78f5033c9e589a267c5dc0e5" have entirely different histories.

3 changed files with 20 additions and 90 deletions

View file

@ -1,106 +1,36 @@
# Make Bash more like Zsh
## Hooks
# https://zsh.sourceforge.io/Doc/Release/Functions.html#Hook-Functions
# Hooks
# derived from http://glyf.livejournal.com/63106.html
# TODO: support precmd_functions and chpwd_functions arrays
### Defaults: NOOPs
# default: NOOPs
function preexec { :; }
function precmd { :; }
function chpwd { :; }
### Implementations
# Alternative cd function that calls chpwd afterwards builtin cd
#
# zsh: Executed whenever the current working directory is changed.
__cd_invoke_chpwd() {
builtin cd "$@"
chpwd
# TODO: chpwd_functions array
}
# zsh: Executed before each prompt. Note that precommand functions are not
# re-executed simply because the command line is redrawn, as happens, for
# example, when a notification about an exiting job is displayed.
__prompt_command_invoke_precmd() {
precmd
# TODO: precmd_functions array
}
# to properly trigger only before command exec this must be the last prompt command
__prompt_command_hook_preexec() {
trap '__debug_invoke_preexec' DEBUG
}
__prompt_command_unhook_preexec() {
# Unfortunately there's no way to remove just one trap from a signal
trap - DEBUG
}
# zsh: Executed just after a command has been read and is about to be executed. If
# the history mechanism is active (regardless of whether the line was discarded
# from the history buffer), the string that the user typed is passed as the
# first argument, otherwise it is an empty string. The actual command that will
# be executed (including expanded aliases) is passed in two different forms:
# the second argument is a single-line, size-limited version of the command
# (with things like function bodies elided); the third argument contains the
# full text that is being executed.
__debug_invoke_preexec () {
# somehow PROMPT_COMMAND contents seems to be still affected by the
# just-removed trap. Ignore ours.
[[ "$BASH_COMMAND" == "__prompt_command_unhook_preexec" ]] && return
[[ "$BASH_COMMAND" == "__prompt_command_invoke_precmd" ]] && return
[[ "$BASH_COMMAND" == "__prompt_command_hook_preexec" ]] && return
# TODO: not sure this is still necessary
[[ -n "$COMP_LINE" ]] && return # completion
__preexec_invoke_exec () {
local this_command;
# TODO: hack: doesn't work when command is not added to history
[ -n "$COMP_LINE" ] && return # completion
[ "$BASH_COMMAND" = "$PROMPT_COMMAND" ] && return # precmd
this_command=`history 1 | sed -e "s/^[ ]*[0-9]*[ ]*//g"`;
preexec "$this_command"
}
### Set up the hooks
# Define a function to override the cd builtin
#
# - does not apply to `bash -c 'cd foo`
# - does apply to shubshells: `(cd foo)` will call so side effects beware (env
# vars are conveniently scoped to subshell)
# set up the hooks
PROMPT_COMMAND="precmd"
trap '__preexec_invoke_exec' DEBUG
cd() { __cd_invoke_chpwd "$@"; }
# Call on each prompt. This transitively sets preexec to work for each prompt
#
# PROMPT_COMMAND is evaluated right before PS1 is displayed. This matches zsh
# semantics for precmd.
#
# We forcefully set it instead of taking whatever was set:
# - to control order (precmd eval + preexec hooking must be last)
# - because of the trap issue
PROMPT_COMMAND=(
# this also conveniently clears DEBUG traps
__prompt_command_unhook_preexec
# precmd hook processing
__prompt_command_invoke_precmd
# must be last to properly have DEBUG trigger only before command exec
__prompt_command_hook_preexec
)
## Other bits
# clears a line that was not terminated by a LF fixing the dangling prompt
# issue by marking it with a reversed %, like zsh

View file

@ -1,5 +1,5 @@
# if test -n "$KITTY_INSTALLATION_DIR"; then
# export KITTY_SHELL_INTEGRATION="enabled"
# source "$KITTY_INSTALLATION_DIR/shell-integration/bash/kitty.bash"
# fi
if test -n "$KITTY_INSTALLATION_DIR"; then
export KITTY_SHELL_INTEGRATION="enabled"
source "$KITTY_INSTALLATION_DIR/shell-integration/bash/kitty.bash"
fi

View file

@ -1,6 +1,6 @@
# if test -n "$KITTY_INSTALLATION_DIR"; then
# export KITTY_SHELL_INTEGRATION="enabled"
# autoload -Uz -- "$KITTY_INSTALLATION_DIR"/shell-integration/zsh/kitty-integration
# kitty-integration
# unfunction kitty-integration
# fi
if test -n "$KITTY_INSTALLATION_DIR"; then
export KITTY_SHELL_INTEGRATION="enabled"
autoload -Uz -- "$KITTY_INSTALLATION_DIR"/shell-integration/zsh/kitty-integration
kitty-integration
unfunction kitty-integration
fi