split dual-roled function into two distinct ones
This commit is contained in:
parent
56d178984e
commit
9fd4576ae7
1 changed files with 83 additions and 73 deletions
|
|
@ -43,12 +43,8 @@ F_ordinary_highlight="bg=magenta,fg=white,bold"
|
||||||
F_out_of_matches_highlight="bg=red,fg=white,bold"
|
F_out_of_matches_highlight="bg=red,fg=white,bold"
|
||||||
F_max_buffer_size=250000
|
F_max_buffer_size=250000
|
||||||
|
|
||||||
history-substring-search-backward() {
|
history-substring-search-begin() {
|
||||||
|
|
||||||
emulate -L zsh
|
|
||||||
|
|
||||||
setopt extendedglob
|
setopt extendedglob
|
||||||
|
|
||||||
zmodload -i zsh/parameter
|
zmodload -i zsh/parameter
|
||||||
|
|
||||||
F_zsh_highlighting_available=0
|
F_zsh_highlighting_available=0
|
||||||
|
|
@ -81,58 +77,101 @@ history-substring-search-backward() {
|
||||||
# initial value of $F_match_number, which can initially only be decreased by
|
# initial value of $F_match_number, which can initially only be decreased by
|
||||||
# ${WIDGET/forward/backward}
|
# ${WIDGET/forward/backward}
|
||||||
fi
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
if [[ $WIDGET = *backward* ]]; then # we have pressed arrow-up
|
history-substring-search-end() {
|
||||||
if [[ ($F_match_number -ge 2 && $F_match_number -le $F_number_of_matches_plus_one) ]]; then
|
CURSOR=${#BUFFER}
|
||||||
|
#"zle .end-of-line" does not move CURSOR to the final end of line in multi-line buffers.
|
||||||
|
|
||||||
|
# for debugging purposes:
|
||||||
|
# zle -R "mn: "$F_match_number" m#: "${#F_matches}
|
||||||
|
# read -k -t 200 && zle -U $REPLY
|
||||||
|
}
|
||||||
|
|
||||||
|
history-substring-search-backward() {
|
||||||
|
history-substring-search-begin
|
||||||
|
|
||||||
|
if [[ ($F_match_number -ge 2 && $F_match_number -le $F_number_of_matches_plus_one) ]]; then
|
||||||
|
let "F_match_number = $F_match_number - 1"
|
||||||
|
F_command_to_be_retrieved=$history[$F_matches[$F_match_number]]
|
||||||
|
BUFFER=$F_command_to_be_retrieved
|
||||||
|
if [[ ((( $F_zsh_highlighting_available == 1 ) && ( $+BUFFER < $F_max_buffer_size) )) ]]; then
|
||||||
|
_zsh_highlight-zle-buffer
|
||||||
|
fi
|
||||||
|
if [[ $F_search4later != "" ]]; then # F_search string was not empty: highlight it
|
||||||
|
: ${(S)BUFFER##(#m)($F_search4later##)}
|
||||||
|
# among other things the following expression yields a variable $MEND,
|
||||||
|
# which indicates the end position of the first occurrence of $F_search in
|
||||||
|
# $BUFFER
|
||||||
|
let "F_my_mbegin = $MEND - $#F_search4later"
|
||||||
|
region_highlight=("$F_my_mbegin $MEND $F_ordinary_highlight")
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
if [[ ($F_match_number -eq 1) ]]; then
|
||||||
|
# we will move out of the F_matches
|
||||||
let "F_match_number = $F_match_number - 1"
|
let "F_match_number = $F_match_number - 1"
|
||||||
F_command_to_be_retrieved=$history[$F_matches[$F_match_number]]
|
F_old_buffer_backward=$BUFFER
|
||||||
BUFFER=$F_command_to_be_retrieved
|
BUFFER=$F_search4later
|
||||||
if [[ ((( $F_zsh_highlighting_available == 1 ) && ( $+BUFFER < $F_max_buffer_size) )) ]]; then
|
if [[ ((( $F_zsh_highlighting_available == 1 ) && ( $+BUFFER < $F_max_buffer_size) )) ]]; then
|
||||||
_zsh_highlight-zle-buffer
|
_zsh_highlight-zle-buffer
|
||||||
fi
|
fi
|
||||||
if [[ $F_search4later != "" ]]; then # F_search string was not empty: highlight it
|
if [[ $F_search4later != "" ]]; then
|
||||||
: ${(S)BUFFER##(#m)($F_search4later##)}
|
: ${(S)BUFFER##(#m)($F_search4later##)}
|
||||||
# among other things the following expression yields a variable $MEND,
|
|
||||||
# which indicates the end position of the first occurrence of $F_search in
|
|
||||||
# $BUFFER
|
|
||||||
let "F_my_mbegin = $MEND - $#F_search4later"
|
let "F_my_mbegin = $MEND - $#F_search4later"
|
||||||
region_highlight=("$F_my_mbegin $MEND $F_ordinary_highlight")
|
region_highlight=("$F_my_mbegin $MEND $F_out_of_matches_highlight")
|
||||||
|
# this is slightly more informative than highlighting
|
||||||
|
# that fish performs
|
||||||
fi
|
fi
|
||||||
else
|
else
|
||||||
if [[ ($F_match_number -eq 1) ]]; then
|
if [[ ($F_match_number -eq $F_number_of_matches_plus_one ) ]]; then
|
||||||
# we will move out of the F_matches
|
# we will move back to the F_matches
|
||||||
let "F_match_number = $F_match_number - 1"
|
let "F_match_number = $F_match_number - 1"
|
||||||
F_old_buffer_backward=$BUFFER
|
BUFFER=$F_old_buffer_forward
|
||||||
BUFFER=$F_search4later
|
|
||||||
if [[ ((( $F_zsh_highlighting_available == 1 ) && ( $+BUFFER < $F_max_buffer_size) )) ]]; then
|
if [[ ((( $F_zsh_highlighting_available == 1 ) && ( $+BUFFER < $F_max_buffer_size) )) ]]; then
|
||||||
_zsh_highlight-zle-buffer
|
_zsh_highlight-zle-buffer
|
||||||
fi
|
fi
|
||||||
if [[ $F_search4later != "" ]]; then
|
if [[ $F_search4later != "" ]]; then
|
||||||
: ${(S)BUFFER##(#m)($F_search4later##)}
|
: ${(S)BUFFER##(#m)($F_search4later##)}
|
||||||
let "F_my_mbegin = $MEND - $#F_search4later"
|
let "F_my_mbegin = $MEND - $#F_search4later"
|
||||||
region_highlight=("$F_my_mbegin $MEND $F_out_of_matches_highlight")
|
region_highlight=("$F_my_mbegin $MEND $F_ordinary_highlight")
|
||||||
# this is slightly more informative than highlighting
|
|
||||||
# that fish performs
|
|
||||||
fi
|
|
||||||
else
|
|
||||||
if [[ ($F_match_number -eq $F_number_of_matches_plus_one ) ]]; then
|
|
||||||
# we will move back to the F_matches
|
|
||||||
let "F_match_number = $F_match_number - 1"
|
|
||||||
BUFFER=$F_old_buffer_forward
|
|
||||||
if [[ ((( $F_zsh_highlighting_available == 1 ) && ( $+BUFFER < $F_max_buffer_size) )) ]]; then
|
|
||||||
_zsh_highlight-zle-buffer
|
|
||||||
fi
|
|
||||||
if [[ $F_search4later != "" ]]; then
|
|
||||||
: ${(S)BUFFER##(#m)($F_search4later##)}
|
|
||||||
let "F_my_mbegin = $MEND - $#F_search4later"
|
|
||||||
region_highlight=("$F_my_mbegin $MEND $F_ordinary_highlight")
|
|
||||||
fi
|
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
else # arrow-down
|
fi
|
||||||
if [[ ($F_match_number -eq $F_number_of_matches_plus_one ) ]]; then
|
|
||||||
let "F_match_number = $F_match_number"
|
history-substring-search-end
|
||||||
|
}
|
||||||
|
|
||||||
|
history-substring-search-forward() {
|
||||||
|
history-substring-search-begin
|
||||||
|
|
||||||
|
if [[ ($F_match_number -eq $F_number_of_matches_plus_one ) ]]; then
|
||||||
|
let "F_match_number = $F_match_number"
|
||||||
|
F_old_buffer_forward=$BUFFER
|
||||||
|
BUFFER=$F_search4later
|
||||||
|
if [[ ((( $F_zsh_highlighting_available == 1 ) && ( $+BUFFER < $F_max_buffer_size) )) ]]; then
|
||||||
|
_zsh_highlight-zle-buffer
|
||||||
|
fi
|
||||||
|
if [[ $F_search4later != "" ]]; then
|
||||||
|
: ${(S)BUFFER##(#m)($F_search4later##)}
|
||||||
|
let "F_my_mbegin = $MEND - $#F_search4later"
|
||||||
|
region_highlight=("$F_my_mbegin $MEND $F_out_of_matches_highlight")
|
||||||
|
fi
|
||||||
|
elif [[ ($F_match_number -ge 0 && $F_match_number -le $F_number_of_matches_minus_one) ]]; then
|
||||||
|
let "F_match_number = $F_match_number + 1"
|
||||||
|
F_command_to_be_retrieved=$history[$F_matches[$F_match_number]]
|
||||||
|
BUFFER=$F_command_to_be_retrieved
|
||||||
|
if [[ ((( $F_zsh_highlighting_available == 1 ) && ( $+BUFFER < $F_max_buffer_size) )) ]]; then
|
||||||
|
_zsh_highlight-zle-buffer
|
||||||
|
fi
|
||||||
|
if [[ $F_search4later != "" ]]; then
|
||||||
|
: ${(S)BUFFER##(#m)($F_search4later##)}
|
||||||
|
let "F_my_mbegin = $MEND - $#F_search4later"
|
||||||
|
region_highlight=("$F_my_mbegin $MEND $F_ordinary_highlight")
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
if [[ ($F_match_number -eq $F_number_of_matches ) ]]; then
|
||||||
|
let "F_match_number = $F_match_number + 1"
|
||||||
F_old_buffer_forward=$BUFFER
|
F_old_buffer_forward=$BUFFER
|
||||||
BUFFER=$F_search4later
|
BUFFER=$F_search4later
|
||||||
if [[ ((( $F_zsh_highlighting_available == 1 ) && ( $+BUFFER < $F_max_buffer_size) )) ]]; then
|
if [[ ((( $F_zsh_highlighting_available == 1 ) && ( $+BUFFER < $F_max_buffer_size) )) ]]; then
|
||||||
|
|
@ -143,56 +182,27 @@ history-substring-search-backward() {
|
||||||
let "F_my_mbegin = $MEND - $#F_search4later"
|
let "F_my_mbegin = $MEND - $#F_search4later"
|
||||||
region_highlight=("$F_my_mbegin $MEND $F_out_of_matches_highlight")
|
region_highlight=("$F_my_mbegin $MEND $F_out_of_matches_highlight")
|
||||||
fi
|
fi
|
||||||
elif [[ ($F_match_number -ge 0 && $F_match_number -le $F_number_of_matches_minus_one) ]]; then
|
|
||||||
let "F_match_number = $F_match_number + 1"
|
|
||||||
F_command_to_be_retrieved=$history[$F_matches[$F_match_number]]
|
|
||||||
BUFFER=$F_command_to_be_retrieved
|
|
||||||
if [[ ((( $F_zsh_highlighting_available == 1 ) && ( $+BUFFER < $F_max_buffer_size) )) ]]; then
|
|
||||||
_zsh_highlight-zle-buffer
|
|
||||||
fi
|
|
||||||
if [[ $F_search4later != "" ]]; then
|
|
||||||
: ${(S)BUFFER##(#m)($F_search4later##)}
|
|
||||||
let "F_my_mbegin = $MEND - $#F_search4later"
|
|
||||||
region_highlight=("$F_my_mbegin $MEND $F_ordinary_highlight")
|
|
||||||
fi
|
|
||||||
else
|
else
|
||||||
if [[ ($F_match_number -eq $F_number_of_matches ) ]]; then
|
if [[ ($F_match_number -eq 0 ) ]]; then
|
||||||
let "F_match_number = $F_match_number + 1"
|
let "F_match_number = $F_match_number + 1"
|
||||||
F_old_buffer_forward=$BUFFER
|
BUFFER=$F_old_buffer_backward
|
||||||
BUFFER=$F_search4later
|
|
||||||
if [[ ((( $F_zsh_highlighting_available == 1 ) && ( $+BUFFER < $F_max_buffer_size) )) ]]; then
|
if [[ ((( $F_zsh_highlighting_available == 1 ) && ( $+BUFFER < $F_max_buffer_size) )) ]]; then
|
||||||
_zsh_highlight-zle-buffer
|
_zsh_highlight-zle-buffer
|
||||||
fi
|
fi
|
||||||
if [[ $F_search4later != "" ]]; then
|
if [[ $F_search4later != "" ]]; then
|
||||||
: ${(S)BUFFER##(#m)($F_search4later##)}
|
: ${(S)BUFFER##(#m)($F_search4later##)}
|
||||||
let "F_my_mbegin = $MEND - $#F_search4later"
|
let "F_my_mbegin = $MEND - $#F_search4later"
|
||||||
region_highlight=("$F_my_mbegin $MEND $F_out_of_matches_highlight")
|
region_highlight=("$F_my_mbegin $MEND $F_ordinary_highlight")
|
||||||
fi
|
|
||||||
else
|
|
||||||
if [[ ($F_match_number -eq 0 ) ]]; then
|
|
||||||
let "F_match_number = $F_match_number + 1"
|
|
||||||
BUFFER=$F_old_buffer_backward
|
|
||||||
if [[ ((( $F_zsh_highlighting_available == 1 ) && ( $+BUFFER < $F_max_buffer_size) )) ]]; then
|
|
||||||
_zsh_highlight-zle-buffer
|
|
||||||
fi
|
|
||||||
if [[ $F_search4later != "" ]]; then
|
|
||||||
: ${(S)BUFFER##(#m)($F_search4later##)}
|
|
||||||
let "F_my_mbegin = $MEND - $#F_search4later"
|
|
||||||
region_highlight=("$F_my_mbegin $MEND $F_ordinary_highlight")
|
|
||||||
fi
|
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
CURSOR=${#BUFFER}
|
|
||||||
#"zle .end-of-line" does not move CURSOR to the final end of line in multi-line buffers.
|
|
||||||
|
|
||||||
# for debugging purposes:
|
history-substring-search-end
|
||||||
# zle -R "mn: "$F_match_number" m#: "${#F_matches}
|
|
||||||
# read -k -t 200 && zle -U $REPLY
|
|
||||||
}
|
}
|
||||||
|
|
||||||
zle -N history-substring-search-forward history-substring-search-backward
|
|
||||||
zle -N history-substring-search-backward
|
zle -N history-substring-search-backward
|
||||||
|
zle -N history-substring-search-forward
|
||||||
|
|
||||||
bindkey '\e[A' history-substring-search-backward
|
bindkey '\e[A' history-substring-search-backward
|
||||||
bindkey '\e[B' history-substring-search-forward
|
bindkey '\e[B' history-substring-search-forward
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue