From 882ce0de9346d23b153bdf51acf0d6d0afd8e63f Mon Sep 17 00:00:00 2001 From: Julian Prein Date: Wed, 4 Sep 2024 16:18:23 +0200 Subject: [PATCH] tmux:textobjs: Reduce calls to tmux Put multiple commands together in one tmux call instead of calling it every time. --- .config/tmux/textobjs.sh | 83 +++++++++++++++++++++++----------------- 1 file changed, 48 insertions(+), 35 deletions(-) diff --git a/.config/tmux/textobjs.sh b/.config/tmux/textobjs.sh index ba7722a..65805da 100755 --- a/.config/tmux/textobjs.sh +++ b/.config/tmux/textobjs.sh @@ -10,6 +10,23 @@ command_prompt() { 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)" if [ "$mode" != copy-mode ]; then >&2 printf "%s: Not in copy mode\n" "$0" @@ -19,7 +36,7 @@ fi # cancel copy-mode when not in 'visual' mode (i.e. selection is present) selection_present="$(get_var selection_present)" if [ "$selection_present" -eq 0 ]; then - tmux send -X cancel + copy_exec cancel exit 0 fi @@ -45,13 +62,12 @@ case "$motion" in if [ "${copy_line_post#"$copy_word"}" = "$copy_line_post" ]; then # not on beginning of word - tmux send -X previous-word - tmux send -X other-end + copy_exec previous-word other-end fi if [ "${copy_line_pre%"$copy_word"}" = "$copy_line_pre" ]; then # not on end of word - tmux send -X next-word-end + copy_exec next-word-end fi ;; W) @@ -73,18 +89,17 @@ case "$motion" in # send "BoE" if [ "$char_pre" != " " ]; then # not on beginning of WORD - tmux send -X previous-space - tmux send -X other-end + copy_exec previous-space other-end fi if [ "$char_post" != " " ]; then # not on end of WORD - tmux send -X next-space-end + copy_exec next-space-end fi ;; p) # send "{j0o}k$" - tmux send -X previous-paragraph + copy_exec previous-paragraph scroll_pos="$(get_var scroll_position)" hist_size="$(get_var history_size)" @@ -92,12 +107,10 @@ case "$motion" in # don't move down if we're at the very first paragraph if [ "$scroll_pos" -lt "$hist_size" ] || [ "$cursor_y" -gt 0 ] then - tmux send -X cursor-down + copy_exec cursor-down fi - tmux send -X start-of-line - tmux send -X other-end - tmux send -X next-paragraph + copy_exec start-of-line other-end next-paragraph scroll_pos="$(get_var scroll_position)" cursor_y="$(get_var copy_cursor_y)" @@ -106,46 +119,46 @@ case "$motion" in # don't move up if we're at the very last paragraph if [ "$scroll_pos" -gt 0 ] || [ "$cursor_y" -lt "$pane_height" ] then - tmux send -X cursor-up + copy_exec cursor-up fi - tmux send -X end-of-line + copy_exec end-of-line ;; # TODO: All following break when the cursor sits on the start or end \") - tmux send -X jump-to-backward '"' - tmux send -X other-end - tmux send -X jump-to-forward '"' + copy_exec jump-to-backward '"' + copy_exec other-end + copy_exec jump-to-forward '"' ;; \') - tmux send -X jump-to-backward "'" - tmux send -X other-end - tmux send -X jump-to-forward "'" + copy_exec jump-to-backward "'" + copy_exec other-end + copy_exec jump-to-forward "'" ;; \`) - tmux send -X jump-to-backward '`' - tmux send -X other-end - tmux send -X jump-to-forward '`' + copy_exec jump-to-backward '`' + copy_exec other-end + copy_exec jump-to-forward '`' ;; '[|]') - tmux send -X jump-to-backward '[' - tmux send -X other-end - tmux send -X jump-to-forward ']' + copy_exec jump-to-backward '[' + copy_exec other-end + copy_exec jump-to-forward ']' ;; 'b|(|)') - tmux send -X jump-to-backward '(' - tmux send -X other-end - tmux send -X jump-to-forward ')' + copy_exec jump-to-backward '(' + copy_exec other-end + copy_exec jump-to-forward ')' ;; '<|>') - tmux send -X jump-to-backward '<' - tmux send -X other-end - tmux send -X jump-to-forward '>' + copy_exec jump-to-backward '<' + copy_exec other-end + copy_exec jump-to-forward '>' ;; 'B|{|}') # TODO: make this work over multiple lines - tmux send -X jump-to-backward '{' - tmux send -X other-end - tmux send -X jump-to-forward '}' + copy_exec jump-to-backward '{' + copy_exec other-end + copy_exec jump-to-forward '}' ;; esac