1111# Author: Ryan Caloras ([email protected] )1212# Forked from Original Author: Glyph Lefkowitz
1313#
14- # V0.2.1
14+ # V0.2.3
1515#
1616
1717# General Usage:
@@ -40,6 +40,20 @@ if [[ "$__bp_imported" == "defined" ]]; then
4040fi
4141__bp_imported=" defined"
4242
43+
44+ # Remove ignorespace and or replace ignoreboth from HISTCONTROL
45+ # so we can accurately invoke preexec with a command from our
46+ # history even if it starts with a space.
47+ __bp_adjust_histcontrol () {
48+ local histcontrol
49+ histcontrol=" ${HISTCONTROL// ignorespace} "
50+ # Replace ignoreboth with ignoredups
51+ if [[ " $histcontrol " == * " ignoreboth" * ]]; then
52+ histcontrol=" ignoredups:${histcontrol// ignoreboth} "
53+ fi ;
54+ export HISTCONTROL=" $histcontrol "
55+ }
56+
4357# This variable describes whether we are currently in "interactive mode";
4458# i.e. whether this shell has just executed a prompt and is waiting for user
4559# input. It documents whether the current command invoked by the trace hook is
@@ -71,10 +85,11 @@ __bp_precmd_invoke_cmd() {
7185
7286 # For every function defined in our function array. Invoke it.
7387 local precmd_function
74- for precmd_function in ${precmd_functions[@]} ; do
88+ for precmd_function in " ${precmd_functions[@]} " ; do
7589
7690 # Only execute this function if it actually exists.
77- if [[ -n $( type -t $precmd_function ) ]]; then
91+ # Test existence of functions with: declare -[Ff]
92+ if type -t " $precmd_function " 1> /dev/null; then
7893 __bp_set_ret_value $ret_value
7994 $precmd_function
8095 fi
@@ -96,7 +111,7 @@ __bp_in_prompt_command() {
96111 local trimmed_arg
97112 trimmed_arg=$( __bp_trim_whitespace " $1 " )
98113
99- local prompt_command_function
114+ local command
100115 for command in " ${prompt_command_array[@]} " ; do
101116 local trimmed_command
102117 trimmed_command=$( __bp_trim_whitespace " $command " )
@@ -146,10 +161,11 @@ __bp_preexec_invoke_exec() {
146161 return
147162 fi
148163
149- local this_command=" $( history 1 | sed -e " s/^[ ]*[0-9]*[ ]*//g" ) " ;
164+ local this_command
165+ this_command=$( HISTTIMEFORMAT= history 1 | { read -r _ this_command; echo " $this_command " ; })
150166
151167 # Sanity check to make sure we have something to invoke our function with.
152- if [ -z " $this_command " ]; then
168+ if [[ -z " $this_command " ] ]; then
153169 return
154170 fi
155171
@@ -162,7 +178,8 @@ __bp_preexec_invoke_exec() {
162178 for preexec_function in " ${preexec_functions[@]} " ; do
163179
164180 # Only execute each function if it actually exists.
165- if [[ -n $( type -t $preexec_function ) ]]; then
181+ # Test existence of function with: declare -[fF]
182+ if type -t " $preexec_function " 1> /dev/null; then
166183 $preexec_function " $this_command "
167184 fi
168185 done
@@ -172,7 +189,7 @@ __bp_preexec_invoke_exec() {
172189__bp_preexec_and_precmd_install () {
173190
174191 # Make sure this is bash that's running this and return otherwise.
175- if [ -z " $BASH_VERSION " ]; then
192+ if [[ -z " $BASH_VERSION " ] ]; then
176193 return 1;
177194 fi
178195
@@ -181,12 +198,17 @@ __bp_preexec_and_precmd_install() {
181198 return 1;
182199 fi
183200
201+ # Adjust our HISTCONTROL Variable if needed.
202+ __bp_adjust_histcontrol
203+
184204 # Take our existing prompt command and append a semicolon to it
185205 # if it doesn't already have one.
186206 local existing_prompt_command
187207
188- if [ -n " $PROMPT_COMMAND " ]; then
189- existing_prompt_command=$( echo " $PROMPT_COMMAND " | sed ' /; *$/!s/$/;/' )
208+ if [[ -n " $PROMPT_COMMAND " ]]; then
209+ existing_prompt_command=${PROMPT_COMMAND% ${PROMPT_COMMAND##* [![:space:]]} }
210+ existing_prompt_command=${existing_prompt_command% ;}
211+ existing_prompt_command=${existing_prompt_command/%/ ;}
190212 else
191213 existing_prompt_command=" "
192214 fi
0 commit comments