-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtmux-cc.el
325 lines (290 loc) · 11.3 KB
/
tmux-cc.el
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
;;; tmux-cc.el --- control tmux via emacs -*- lexical-binding: t; -*-
;; Copyright (C) 2020 Chunye Wang
;; Author: Chunye Wang <chunywan@xbjlabdpsvr15>
;; Keywords: emulations, terminals
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;;
;;; Code:
(require 'seq)
(require 'shell)
(defvar tmux-cc-delimiter-begin "^```\n"
"begin of a delimter")
(defvar tmux-cc-delimiter-end "^```\n"
"end of a delimter")
(defvar tmux-cc-target-window "1")
(defvar tmux-cc-target-host nil)
(defvar tmux-cc-search-for-prompt nil)
(defvar tmux-cc-mimic-delay nil
"To mimic human typing. in milleseconds")
(defvar tmux-cc-shell-bash-history '()
"the command history, sync with ~/.bash_history")
(defun tmux-cc--convert-keys(strings)
(seq-map #'(lambda(c) (format "%x" c))
(with-temp-buffer
(insert strings)
(goto-char (point-min))
(when tmux-cc-search-for-prompt
(when (re-search-forward shell-prompt-pattern nil t nil)
(replace-match "")))
(buffer-string))))
(defvar-local tmux-cc--shell-process nil
"the shell process for tmux cc. `tmux-cc-send-keys` send
command via this process")
(defun tmux-cc--delete-process-on-exit()
(when (and tmux-cc--shell-process (process-live-p tmux-cc--shell-process))
(process-send-string tmux-cc--shell-process "exit\n")
(sleep-for 1)
(delete-process tmux-cc--shell-process)))
(defun tmux-cc--maybe-start-shell-process ()
(unless (and tmux-cc--shell-process (process-live-p tmux-cc--shell-process))
(setq tmux-cc--shell-process
(start-file-process "tmux-cc-shell" nil "bash" "-i")))
(unless (and tmux-cc--shell-process (process-live-p tmux-cc--shell-process))
(error "cannot start shell for tmux-cc"))
(add-hook 'kill-buffer-hook #'tmux-cc--delete-process-on-exit t t))
(defvar tmux-cc--cmd-buffer "*tmux cc*")
(defun tmux-cc--tmux-cmd (&rest args)
(let* ((buffer (cond
((bufferp tmux-cc--cmd-buffer) tmux-cc--cmd-buffer)
((stringp tmux-cc--cmd-buffer) (get-buffer-create
tmux-cc--cmd-buffer))
(:else (error "%S must be a buffer or string" tmux-cc--cmd-buffer))))
(apply-args (if tmux-cc-target-host
`("ssh" nil ,buffer t
,tmux-cc-target-host
"tmux" ,@args)
`("tmux" nil ,buffer t ,@args))))
;;(message "%S" apply-args)
(apply 'call-process apply-args)))
(defun tmux-cc-send-keys (strings)
(cond
((and nil :hack) (let ()
(with-temp-file "/tmp/a.cmd"
(insert strings))
(call-process "osascript" nil nil nil "-e"
(concat
"
tell application \"iTerm\"
tell current window
tell current tab
tell current session
write contents of file \"/tmp/a.cmd\" newline false
end tell
end tell
end tell
end tell"
))))
(tmux-cc-mimic-delay
(dolist (c (tmux-cc--convert-keys strings))
(tmux-cc--tmux-cmd "send-keys" "-t" tmux-cc-target-window "-H" c)
(sleep-for 0 (random tmux-cc-mimic-delay))))
(:else
(let ((ret (apply #'tmux-cc--tmux-cmd
`("send-keys" "-t" ,tmux-cc-target-window "-H" ,@(tmux-cc--convert-keys
strings)))))
(list 'message "call return %S %S" ret (tmux-cc--convert-keys strings))))))
(defun tmux-cc-send-region(begin end)
(interactive "r")
(unless mark-active
(error "no region is selected"))
(let ((content (buffer-substring begin end)))
(tmux-cc-send-keys content)))
(defun tmux-cc--begining-of-line()
(save-excursion
(goto-char (line-beginning-position))
(while (and (eq (char-before) ?\n)
(eq (char-before (- (point) 1)) ?\\))
(goto-char (- (point) 1))
(goto-char (line-beginning-position)))
(point)))
(defun tmux-cc--end-of-line()
(save-excursion
(goto-char (line-end-position))
(while (eq (char-before) ?\\)
(goto-char (+ 1 (point)))
(goto-char (line-end-position)))
(min (point-max) (+ 1 (point)))))
(defun tmux-cc-send-current-line ()
(interactive)
(tmux-cc-send-keys (buffer-substring (tmux-cc--begining-of-line)
(tmux-cc--end-of-line))))
;; (defun tmux-cc--search-end ()
;; (save-excursion
;; (when (search-forward-regexp tmux-cc-delimiter-begin nil t nil)
;; (match-beginning 0))))
;; (defun tmux-cc--search-begin ()
;; (save-excursion
;; (when (search-backward-regexp tmux-cc-delimiter-end nil t nil)
;; (match-end 0))))
;; (defun tmux-cc-select-block ()
;; (interactive)
;; (let ((begin (tmux-cc--search-begin))
;; (end (tmux-cc--search-end)))
;; (when (and begin end)
;; (push-mark begin nil t)
;; (goto-char end))))
;; (defun tmux-cc-shell-read-bash-history ()
;; (reverse (split-string
;; (with-temp-buffer
;; (insert-file-contents "~/.bash_history")
;; (buffer-substring-no-properties
;; (point-min)
;; (point-max)))
;; "\n"
;; t)))
;;;###autoload
;; (defun tmux-cc-shell-command (command)
;; (interactive
;; (list (read-shell-command
;; "Tmux CC Shell command: "
;; nil nil
;; ;; (progn (setq tmux-cc-shell-bash-history
;; ;; (tmux-cc-shell-read-bash-history)
;; ;; )
;; ;; 'tmux-cc-shell-bash-history)
;; (let ((filename (cond (buffer-file-name)
;; ((eq major-mode 'dired-mode) (dired-get-filename nil t)))))
;; (and filename
;; (file-relative-name
;; filename))))))
;; (tmux-cc-send-keys
;; (concat ;; "cd "
;; ;;(shell-quote-argument
;; ;; (directory-file-name default-directory))
;; ;; "\n"
;; command
;; "\n")))
;;;###autoload
(defun tmux-cc-set-target-window (target-window)
(interactive "sSet tmux target window: ")
(setq tmux-cc-target-window target-window))
;;;###autoload
(defun tmux-cc-get-panel ()
(interactive ;; "stmux panel (empty for current): "
)
(let ((tmux-cc--cmd-buffer (concat " *tmux "
(format
"%s@%s"
tmux-cc-target-window
tmux-cc-target-host) " *")))
(with-current-buffer (get-buffer-create tmux-cc--cmd-buffer)
(erase-buffer)
(tmux-cc--tmux-cmd "capture-pane" "-p" ;; "-t" target-panel
"-S" "-" "-E" "-")
(save-excursion
(goto-char (point-min))
(replace-regexp-in-region "^[[:blank:]]*\n+\\'" "\n"))
(switch-to-buffer (current-buffer)))))
;;;
(defun tmux-cc-clear ()
(interactive)
(tmux-cc-send-keys "clear\n")
(tmux-cc--tmux-cmd "clear-history"))
;;;
(defun tmux-cc-run-single-command (command)
(interactive "scommand: ")
(tmux-cc-send-keys "clear\n")
(tmux-cc--tmux-cmd "tmux" "clear-history")
(tmux-cc-send-keys (concat command "\n"))
(let ((tmux-cc--cmd-buffer (current-buffer)))
(tmux-cc--tmux-cmd "capture-pane" "-p" ;; "-t" target-panel
"-S" "0" "-E" "-")))
;;;###autoload
(defun tmux-cc-bind-command (command &rest args)
`(lambda ()
(interactive)
(tmux-cc--tmux-cmd ,command ,@args)))
(defun convert-key (key)
(cond
((eq key 'up)
(list "1b" "5b" "41"))
((eq key 'down)
(list "1b" "5b" "42"))
((eq key 'right)
(list "1b" "5b" "43"))
((eq key 'left)
(list "1b" "5b" "44"))
((eq key 'home)
(list "1b" "5b" "48"))
((eq key 'end)
(list "1b" "5b" "31" "3b" "32" "46"))
((eq key 'f6)
(list "3"))
((eq key 'f7)
(list "7"))
((integerp key)
(let ((ascii (logand key #xff)))
(if (not (= (logand key #x8000000) 0))
(list "1b" (format "%x" ascii))
(list (format "%x" ascii)))))
(t (error "unknown key %S" key ))))
;;;###autoload
(defun tmux-start-special-key ()
(interactive)
(message "tmux special key mode start")
(let (key)
(while (not (or (eq (setq key
(read-key "sending key stroke to tmux,press s-g
(cmd+g or win+g)
to quit .... "))
(aref (kbd "s-g") 0))))
(condition-case err
(apply #'tmux-cc--tmux-cmd
"send-key" "-t"
tmux-cc-target-window
"-H" (convert-key key))
(error (warn "%s" (cdr err))))))
(message "tmux special key mode done"))
(defun tmux-cc-send-keys-from-a-file (file)
(interactive "fSend keys from a file")
(message file)
(with-temp-buffer
(insert-file-literally file)
(goto-char (point-min))
(while (not (eobp))
(tmux-cc-send-keys
(concat (buffer-substring
(line-beginning-position) (line-end-position))
"\n"))
(forward-line 1))))
(defun tmux-cc-send-a-key (key)
(interactive "kKey:")
(tmux-cc-send-keys key))
(defvar tmux-cc-key-map
(let ((map (make-sparse-keymap)))
;; These bindings roughly imitate those used by Outline mode.
(define-key map (kbd "C-b") 'tmux-start-special-key)
(define-key map (kbd "g") 'tmux-cc-get-panel)
(define-key map (kbd "C-l") 'tmux-cc-clear)
(define-key map "%" (tmux-cc-bind-command "split-window" "-h"))
(define-key map "0" (tmux-cc-bind-command "select-window" "-t" ":=0"))
(define-key map "1" (tmux-cc-bind-command "select-window" "-t" ":=1"))
(define-key map "2" (tmux-cc-bind-command "select-window" "-t" ":=2"))
(define-key map "3" (tmux-cc-bind-command "select-window" "-t" ":=3"))
(define-key map "4" (tmux-cc-bind-command "select-window" "-t" ":=4"))
(define-key map "5" (tmux-cc-bind-command "select-window" "-t" ":=5"))
(define-key map "6" (tmux-cc-bind-command "select-window" "-t" ":=6"))
(define-key map "7" (tmux-cc-bind-command "select-window" "-t" ":=7"))
(define-key map "8" (tmux-cc-bind-command "select-window" "-t" ":=8"))
(define-key map "9" (tmux-cc-bind-command "select-window" "-t" ":=9"))
(define-key map "c" (tmux-cc-bind-command "new-window"))
(define-key map "o" (tmux-cc-bind-command "last-pane" "-t" ":.+"))
(define-key map "l" (tmux-cc-bind-command "last-window"))
(define-key map "z" (tmux-cc-bind-command "resize-pane" "-Z"))
(define-key map "q" #'tmux-cc-send-a-key)
(define-key map (kbd "C-c") (tmux-cc-bind-command "send-key"
"-H" "3"))
map)
"Keymap for mimic tmux key bindings")
(provide 'tmux-cc)
;;; tmux-cc.el ends here