summaryrefslogtreecommitdiff
path: root/frontends/scripts/tmux-edit-send
blob: 9e7054453e4baabe612047c0a6217ba2fb7d227d (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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env bash
set -u -o pipefail

log_file="${TMPDIR:-/tmp}/tmux-edit-send.log"
log() {
  printf '%s\n' "$*" >> "$log_file"
}

target_file="${1:-}"
target=""
if [ -n "$target_file" ] && [ -f "$target_file" ]; then
  target="$(sed -n '1p' "$target_file" | tr -d '[:space:]')"
  log "file target=${target:-<empty>}"
  rm -f "$target_file"
fi

if [ -z "$target" ]; then
  target="${TMUX_EDIT_TARGET:-}"
fi
log "env target=${target:-<empty>}"
if [ -z "$target" ]; then
  env_line="$(tmux show-environment -g TMUX_EDIT_TARGET 2>/dev/null || true)"
  case "$env_line" in
    TMUX_EDIT_TARGET=*) target="${env_line#TMUX_EDIT_TARGET=}" ;;
  esac
fi
log "tmux env target=${target:-<empty>}"

current_pane="$(tmux display-message -p "#{pane_id}" 2>/dev/null || true)"
log "current pane=${current_pane:-<empty>}"
if [ -n "$target" ] && [[ "$target" == *"#{"* ]]; then
  log "format target detected, clearing"
  target=""
fi
if [ -z "$target" ]; then
  target="$(tmux display-message -p "#{last_pane}" 2>/dev/null || true)"
elif [ "$target" = "$current_pane" ]; then
  last_pane="$(tmux display-message -p "#{last_pane}" 2>/dev/null || true)"
  if [ -n "$last_pane" ]; then
    target="$last_pane"
  fi
fi
log "fallback target=${target:-<empty>}"

editor="${EDITOR:-vi}"
tmpfile="$(mktemp "./.tmux-edit-send.XXXXXX.md")"

cleanup() {
  rm -f "$tmpfile"
}
trap cleanup EXIT

prompt_text="$(tmux capture-pane -p -t "$target" -S -2000 2>/dev/null | \
  awk '/^ *│ *→/ && index($0,"INSERT")==0 && index($0,"Add a follow-up")==0 {line=$0} END { if (line!="") { sub(/^.*→ ?/, "", line); sub(/[[:space:]│]+$/, "", line); print line } }')"
if [ -n "$prompt_text" ]; then
  printf '%s\n' "$prompt_text" > "$tmpfile"
fi

"$editor" "$tmpfile"
log "editor exited with status $?"

if [ ! -s "$tmpfile" ]; then
  log "empty file, nothing sent"
  exit 0
fi

# Validate target after editor so popup stays open.
if [ -z "$target" ]; then
  log "error: no target pane determined"
  echo "Could not determine target pane." >&2
  exit 1
fi

target_found=0
for pane in $(tmux list-panes -a -F "#{pane_id}" 2>/dev/null || true); do
  if [ "$pane" = "$target" ]; then
    target_found=1
    break
  fi
done

if [ "$target_found" -ne 1 ]; then
  log "error: target pane not found: $target"
  echo "Target pane not found: $target" >&2
  exit 1
fi

# Send line by line to preserve newlines reliably.
first_line=1
while IFS= read -r line || [ -n "$line" ]; do
  if [ "$first_line" -eq 1 ] && [ -n "${prompt_text:-}" ]; then
    if [[ "$line" == "$prompt_text"* ]]; then
      line="${line#"$prompt_text"}"
      line="${line# }"
    fi
  fi
  first_line=0
  tmux send-keys -t "$target" -l "$line"
  tmux send-keys -t "$target" Enter
done < "$tmpfile"
log "sent content to $target"