aboutsummaryrefslogtreecommitdiff
path: root/tmuxrdr/plugin/bin/move-window
blob: a81567a9a7ee42810fb13d96de0ce6cc2cc911dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/bin/sh
# tmux prefix+. (command-prompt -T target { move-window -t "%%" }): move the
# current window to index N. Herdr's tabs are positions -- tmux with
# renumber-windows on -- so N is the place in the tab bar it moves to, where
# tmux would refuse an index in use. Prompts for the target (run as a popup),
# or takes N as its argument. Over the socket's tab.move, whose insert_index
# counts slots in the list as it is before the move: moving right is one more.
. "$(dirname "$0")/../lib.sh"

n="${1:-}"
if [ -z "$n" ]; then
    printf '(target) '
    IFS= read -r n || exit 0
fi
n="${n#:}"
[ -n "$n" ] || exit 0
case "$n" in
    *[!0-9]*) fail "can't find window: $n" ;;
esac

p="$(active_pane)"
ws="$(pane_info "$p" workspace_id)"
tab="$(pane_info "$p" tab_id)"
tabs="$(workspace_tabs "$ws" | cut -f1)"
count="$(printf '%s\n' "$tabs" | wc -l | tr -d ' ')"
[ "$n" -ge 1 ] && [ "$n" -le "$count" ] || fail "index $n out of range"
from="$(printf '%s\n' "$tabs" | grep -n -x -F "$tab" | cut -d: -f1)"
[ "$n" -eq "$from" ] && exit 0
if [ "$n" -gt "$from" ]; then
    at="$n"
else
    at="$((n - 1))"
fi
api "{\"id\":\"tmux-mode\",\"method\":\"tab.move\",\"params\":{\"tab_id\":\"$tab\",\"insert_index\":$at}}" >/dev/null