tmux:textobjs: Merge all copy-mode commands

`copy_exec` was stupid since it brought unnecessary shell overhead even
though it was meant to improve performance (one could argue that it is
not even necessary here).

Continue the idea of reducing calls to `tmux` by "globally" chaining
together all copy-mode commands instead of doing it only "locally" with
`copy_exec`. This has the added benefit that also calls having arguments
can be included and merged.
This commit is contained in:
2024-09-04 16:29:24 +02:00
parent 882ce0de93
commit ec417a439c

View File

@@ -10,23 +10,6 @@ command_prompt() {
tmux command-prompt -k -I "$2" -p "$1" 'display-message -p "%%"' tmux command-prompt -k -I "$2" -p "$1" 'display-message -p "%%"'
} }
copy_exec() {
num_cmds=$#
# since POSIX shell does not include arrays I use `set` here. This keeps
# the initial arguments though (for now), since I cannot copy $@ (as an
# array) to a different variable to have a clean $@ in the first
# iteration.
for arg in "$@"; do
set -- "$@" \; send -X "$arg"
done
# get rid of initial arguments + first semicolon
shift $((num_cmds + 1))
tmux "$@"
}
mode="$(get_var pane_mode)" mode="$(get_var pane_mode)"
if [ "$mode" != copy-mode ]; then if [ "$mode" != copy-mode ]; then
>&2 printf "%s: Not in copy mode\n" "$0" >&2 printf "%s: Not in copy mode\n" "$0"
@@ -36,13 +19,16 @@ fi
# cancel copy-mode when not in 'visual' mode (i.e. selection is present) # cancel copy-mode when not in 'visual' mode (i.e. selection is present)
selection_present="$(get_var selection_present)" selection_present="$(get_var selection_present)"
if [ "$selection_present" -eq 0 ]; then if [ "$selection_present" -eq 0 ]; then
copy_exec cancel tmux send -X cancel
exit 0 exit 0
fi fi
# get motion # get motion
motion="$(command_prompt "(operator-pending)")" motion="$(command_prompt "(operator-pending)")"
# ignore any args
set --
case "$motion" in case "$motion" in
w) w)
copy_x="$(get_var copy_cursor_x)" copy_x="$(get_var copy_cursor_x)"
@@ -62,12 +48,13 @@ case "$motion" in
if [ "${copy_line_post#"$copy_word"}" = "$copy_line_post" ]; if [ "${copy_line_post#"$copy_word"}" = "$copy_line_post" ];
then then
# not on beginning of word # not on beginning of word
copy_exec previous-word other-end set -- "$@" send -X previous-word \;
set -- "$@" send -X other-end \;
fi fi
if [ "${copy_line_pre%"$copy_word"}" = "$copy_line_pre" ]; if [ "${copy_line_pre%"$copy_word"}" = "$copy_line_pre" ];
then then
# not on end of word # not on end of word
copy_exec next-word-end set -- "$@" send -X next-word-end \;
fi fi
;; ;;
W) W)
@@ -89,17 +76,18 @@ case "$motion" in
# send "BoE" # send "BoE"
if [ "$char_pre" != " " ]; then if [ "$char_pre" != " " ]; then
# not on beginning of WORD # not on beginning of WORD
copy_exec previous-space other-end set -- "$@" send -X previous-space \;
set -- "$@" send -X other-end \;
fi fi
if [ "$char_post" != " " ]; then if [ "$char_post" != " " ]; then
# not on end of WORD # not on end of WORD
copy_exec next-space-end set -- "$@" send -X next-space-end \;
fi fi
;; ;;
p) p)
# send "{j0o}k$" # send "{j0o}k$"
copy_exec previous-paragraph set -- "$@" send -X previous-paragraph \;
scroll_pos="$(get_var scroll_position)" scroll_pos="$(get_var scroll_position)"
hist_size="$(get_var history_size)" hist_size="$(get_var history_size)"
@@ -107,10 +95,12 @@ case "$motion" in
# don't move down if we're at the very first paragraph # don't move down if we're at the very first paragraph
if [ "$scroll_pos" -lt "$hist_size" ] || [ "$cursor_y" -gt 0 ] if [ "$scroll_pos" -lt "$hist_size" ] || [ "$cursor_y" -gt 0 ]
then then
copy_exec cursor-down set -- "$@" send -X cursor-down \;
fi fi
copy_exec start-of-line other-end next-paragraph set -- "$@" send -X start-of-line \;
set -- "$@" send -X other-end \;
set -- "$@" send -X next-paragraph \;
scroll_pos="$(get_var scroll_position)" scroll_pos="$(get_var scroll_position)"
cursor_y="$(get_var copy_cursor_y)" cursor_y="$(get_var copy_cursor_y)"
@@ -119,46 +109,48 @@ case "$motion" in
# don't move up if we're at the very last paragraph # don't move up if we're at the very last paragraph
if [ "$scroll_pos" -gt 0 ] || [ "$cursor_y" -lt "$pane_height" ] if [ "$scroll_pos" -gt 0 ] || [ "$cursor_y" -lt "$pane_height" ]
then then
copy_exec cursor-up set -- "$@" send -X cursor-up \;
fi fi
copy_exec end-of-line set -- "$@" send -X end-of-line \;
;; ;;
# TODO: All following break when the cursor sits on the start or end # TODO: All following break when the cursor sits on the start or end
\") \")
copy_exec jump-to-backward '"' set -- "$@" send -X jump-to-backward '"' \;
copy_exec other-end set -- "$@" send -X other-end \;
copy_exec jump-to-forward '"' set -- "$@" send -X jump-to-forward '"' \;
;; ;;
\') \')
copy_exec jump-to-backward "'" set -- "$@" send -X jump-to-backward "'" \;
copy_exec other-end set -- "$@" send -X other-end \;
copy_exec jump-to-forward "'" set -- "$@" send -X jump-to-forward "'" \;
;; ;;
\`) \`)
copy_exec jump-to-backward '`' set -- "$@" send -X jump-to-backward '`' \;
copy_exec other-end set -- "$@" send -X other-end \;
copy_exec jump-to-forward '`' set -- "$@" send -X jump-to-forward '`' \;
;; ;;
'[|]') '[|]')
copy_exec jump-to-backward '[' set -- "$@" send -X jump-to-backward '[' \;
copy_exec other-end set -- "$@" send -X other-end \;
copy_exec jump-to-forward ']' set -- "$@" send -X jump-to-forward ']' \;
;; ;;
'b|(|)') 'b|(|)')
copy_exec jump-to-backward '(' set -- "$@" send -X jump-to-backward '(' \;
copy_exec other-end set -- "$@" send -X other-end \;
copy_exec jump-to-forward ')' set -- "$@" send -X jump-to-forward ')' \;
;; ;;
'<|>') '<|>')
copy_exec jump-to-backward '<' set -- "$@" send -X jump-to-backward '<' \;
copy_exec other-end set -- "$@" send -X other-end \;
copy_exec jump-to-forward '>' set -- "$@" send -X jump-to-forward '>' \;
;; ;;
'B|{|}') 'B|{|}')
# TODO: make this work over multiple lines # TODO: make this work over multiple lines
copy_exec jump-to-backward '{' set -- "$@" send -X jump-to-backward '{' \;
copy_exec other-end set -- "$@" send -X other-end \;
copy_exec jump-to-forward '}' set -- "$@" send -X jump-to-forward '}' \;
;; ;;
esac esac
[ $# -eq 0 ] || tmux "$@"