eat-emacs/integration/bash

81 lines
2.5 KiB
Text
Raw Normal View History

# integration/bash --- Bash integration
# Copyright (C) 2022 Akib Azmain Turja.
# This file is not part of GNU Emacs.
# This file is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3, or (at your option)
# any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# For a full copy of the GNU General Public License
# see <https://www.gnu.org/licenses/>.
2022-12-03 21:30:11 +06:00
__eat_prompt_command () {
# Send the current working directory, for directory tracking.
printf '\e]7;file://%s%s\e\\' "$HOSTNAME" "$PWD"
}
__eat_preexec () {
# "${PWD/$HOME/'~'}" converts "/home/akib/org/" to "~/org/".
# The next one is substituted with '$', or '#' if we're "root".
printf '\e]2;%s@%s:%s%s %s\e\\' "$USER" "$HOSTNAME" \
"${PWD/$HOME/'~'}" \
"$(test $UID -eq 0 && echo '#' || echo '$')" "$BASH_COMMAND"
}
__eat_in_prompt_command=no
__eat_before_prompt_command ()
{
__eat_in_prompt_command=yes
}
__eat_after_prompt_command ()
{
__eat_in_prompt_command=no
}
__eat_before_exec () {
if test $__eat_in_prompt_command = no \
&& test "$BASH_COMMAND" != __eat_before_prompt_command
then
__eat_preexec
fi
}
2022-12-03 21:30:11 +06:00
# Add '__eat_prompt_command' as the last element of 'PROMPT_COMMAND'.
if test -z "$__eat_bash_integration_enabled"
then
2022-12-03 21:30:11 +06:00
__eat_bash_integration_enabled=yes
PS1='\[\e]2;\u@\h:\w$\e\\\]'"$PS1"
2022-12-03 21:30:11 +06:00
PROMPT_COMMAND+=(__eat_prompt_command)
trap '__eat_before_exec' DEBUG
# Wrap 'PROMPT_COMMAND' to avoid it getting trapped in 'DEBUG' trap.
# Step 1: Append to PROMPT_COMMAND.
PROMPT_COMMAND+=(__eat_after_prompt_command)
# Step 2: Prepend to PROMPT_COMMAND.
# Step 2.1: Move all elements to make the first index free.
# Fun fact: Microsoft doesn't still know this simple trick. They
# use something as silly and pityful as 'VAR=$PROMPT_COMMAND' to
# copy a Bash array in VSCode Bash integration script, that simply
# won't work ever, and then complain about Bash in the comments!
# LOL. ;D
for i in $(eval "printf {${#PROMPT_COMMAND[*]}..1..-1}")
do
PROMPT_COMMAND[$i]=${PROMPT_COMMAND[$((i-1))]}
done
# Step 2.2: Assign the first element.
PROMPT_COMMAND[0]=__eat_before_prompt_command
fi
# Local Variables:
# mode: sh
# End: