multi-line buffer editing; better than Fish now

This commit is contained in:
Guido van Steen 2011-06-04 16:24:03 -07:00 committed by Suraj N. Kurapati
parent 14adfd9836
commit dafeb77e2a

View file

@ -50,8 +50,32 @@ zmodload -F zsh/parameter
HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND='bg=magenta,fg=white,bold' HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND='bg=magenta,fg=white,bold'
HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_NOT_FOUND='bg=red,fg=white,bold' HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_NOT_FOUND='bg=red,fg=white,bold'
cursur-to-move-up-in-multi-line-buffer() {
# First create XLBUFFER (i.e. Xtended LBUFFER). Add an X before the cursor, so that
# we can later detect whether there is a newline immediately before the current cursor.
buflines=(${(f)BUFFER})
XLBUFFER=$LBUFFER"x"
xlbuflines=(${(f)XLBUFFER})
cursur_to_move_up_in_multi_line_buffer=0
[[ $#buflines -gt 1 && $#xlbuflines -ne 1 && $#BUFFER != $#LBUFFER ]] && cursur_to_move_up_in_multi_line_buffer=1
}
cursur-to-move-down-in-multi-line-buffer() {
# First create XRBUFFER (i.e. Xtended RBUFFER). Add an X after the cursor, so that
# we can later detect whether there is a newline immediately behind the current cursor.
buflines=(${(f)BUFFER})
XRBUFFER="x"$RBUFFER
xrbuflines=(${(f)XRBUFFER})
cursur_to_move_down_in_multi_line_buffer=0
[[ $#buflines -gt 1 && $#xrbuflines -ne 1 && $#BUFFER != $#LBUFFER ]] && cursur_to_move_down_in_multi_line_buffer=1
}
history-substring-search-begin() { history-substring-search-begin() {
if [[ $LASTWIDGET != history-substring-search-* && (-z $BUFFER || $BUFFER != $history_substring_search_result) ]]; then # use the previous "$history_substring_search_result" by default
history_new_search=0
[[ $BUFFER == "" ]] && history_new_search=1
[[ $BUFFER != $history_substring_search_result ]] && history_new_search=1
if [[ $history_new_search -eq 1 ]]; then
# $BUFFER contains the text that is in the command-line currently. # $BUFFER contains the text that is in the command-line currently.
# we put an extra "\\" before meta characters such as "\(" and "\)", # we put an extra "\\" before meta characters such as "\(" and "\)",
# so that they become "\\\(" and "\\\)" # so that they become "\\\(" and "\\\)"
@ -105,8 +129,7 @@ history-substring-search-end() {
# "zle .end-of-line" does not move CURSOR to the final end of line in # "zle .end-of-line" does not move CURSOR to the final end of line in
# multi-line buffers. # multi-line buffers.
CURSOR=${#BUFFER} [[ $cursor_just_moved_in_multiline_buffer -eq 0 ]] && CURSOR=${#BUFFER}
# for debugging purposes: # for debugging purposes:
#zle -R "mn: "$history_substring_search_match_number" m#: "${#history_substring_search_matches} #zle -R "mn: "$history_substring_search_match_number" m#: "${#history_substring_search_matches}
#read -k -t 200 && zle -U $REPLY #read -k -t 200 && zle -U $REPLY
@ -115,6 +138,11 @@ history-substring-search-end() {
history-substring-search-backward() { history-substring-search-backward() {
history-substring-search-begin history-substring-search-begin
# the following function sets the variable "$cursur_to_move_up_in_multi_line_buffer", which will be used one line later.
cursur-to-move-up-in-multi-line-buffer
if [[ $cursur_to_move_up_in_multi_line_buffer == 0 ]]; then
if [[ $history_substring_search_match_number -ge 2 && $history_substring_search_match_number -le $history_substring_search_number_of_matches_plus_one ]]; then if [[ $history_substring_search_match_number -ge 2 && $history_substring_search_match_number -le $history_substring_search_number_of_matches_plus_one ]]; then
let "history_substring_search_match_number = $history_substring_search_match_number - 1" let "history_substring_search_match_number = $history_substring_search_match_number - 1"
history_substring_search_command_to_be_retrieved=$history[$history_substring_search_matches[$history_substring_search_match_number]] history_substring_search_command_to_be_retrieved=$history[$history_substring_search_matches[$history_substring_search_match_number]]
@ -136,6 +164,13 @@ history-substring-search-backward() {
fi fi
fi fi
fi fi
# make sure the cursor will move to the end in this case
cursor_just_moved_in_multiline_buffer=0
else
up-line-or-history
# prevent the cursor from moving to the end in this case
cursor_just_moved_in_multiline_buffer=1
fi
history-substring-search-end history-substring-search-end
} }
@ -143,6 +178,9 @@ history-substring-search-backward() {
history-substring-search-forward() { history-substring-search-forward() {
history-substring-search-begin history-substring-search-begin
cursur-to-move-down-in-multi-line-buffer
if [[ $cursur_to_move_down_in_multi_line_buffer == 0 ]]; then
if [[ $history_substring_search_match_number -eq $history_substring_search_number_of_matches_plus_one ]]; then if [[ $history_substring_search_match_number -eq $history_substring_search_number_of_matches_plus_one ]]; then
let "history_substring_search_match_number = $history_substring_search_match_number" let "history_substring_search_match_number = $history_substring_search_match_number"
history_substring_search_old_buffer_forward=$BUFFER history_substring_search_old_buffer_forward=$BUFFER
@ -167,6 +205,11 @@ history-substring-search-forward() {
fi fi
fi fi
fi fi
cursor_just_moved_in_multiline_buffer=0
else
down-line-or-history
cursor_just_moved_in_multiline_buffer=1
fi
history-substring-search-end history-substring-search-end
} }