-
Notifications
You must be signed in to change notification settings - Fork 2
/
multi-term-plus.el
242 lines (209 loc) · 7.96 KB
/
multi-term-plus.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
;;; multi-term-plus.el --- An extensions plug for multi-term -*- lexical-binding: t; -*-
;; Copyright (C) 2016 Aborn Jiang
;; Author: Aborn Jiang <[email protected]>
;; Version: 0.1.0
;; Package-Requires: ((emacs "24.4") (cl-lib "0.5") (let-alist "1.0.3") (s "1.10.0") (multi-term "1.3"))
;; Keywords: term, multi-term, multi-term-plus
;; Homepage: https://github.com/aborn/multi-term-plus
;; URL: https://github.com/aborn/multi-term-plus
;; This file is NOT part of GNU Emacs.
;; 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 <http://www.gnu.org/licenses/>.
;;; Commentary:
;; The multi-term-plus package is used as extension for multi-term mode.
;;
;; The leanote package provides follwoing features:
;; * multi-term-find Fast switch when you open multi-terms.
;; * multi-term-kill-line Smart kill-line in multi-term mode.
;; * Auto recover previous term buffers
;;
;; Usage:
;; see multi-term-config.el
;;
;; Optional
;;
;;; Code:
(require 'multi-term)
(require 'cl-lib)
(defvar multi-term-recover-alist '())
(defcustom multi-term-recovery-p t
"Is need to recover previous term buffers when emacs bootup."
:type 'boolean
:group 'multi-term)
(defcustom multi-term-recover-alist-file "~/.multi-term-recover-alist"
"Multi term alist save file."
:type 'string
:group 'multi-term)
(defcustom multi-term-recover-hook '()
"Called when each term recover."
:type 'hook
:group 'multi-term)
(defun multi-term-is-at-end-line ()
"Cursor is in last line."
(equal (line-number-at-pos) (count-lines (point-min) (point-max))))
(defun multi-term-is-term-mode ()
"Current is term-mode"
(eq major-mode 'term-mode))
(defun multi-term-backward-char ()
"Backward-char in term-mode. "
(interactive)
(if (not (multi-term-is-term-mode))
(backward-char)
(progn (if (not (multi-term-is-at-end-line))
(backward-char)
(progn (term-send-left)
;; (message "term-send-left")
)))))
(defun multi-term-forward-char ()
"Forward-char in term-mode."
(interactive)
(if (not (multi-term-is-term-mode))
(forward-char)
(progn (if (not (multi-term-is-at-end-line))
(forward-char)
(progn (term-send-right)
;; (message "term-send-right")
)))))
(defun multi-term-move-beginning-of-line ()
"Smart version of move-beginning-of-line in term-mode."
(interactive)
(if (not (multi-term-is-term-mode))
(beginning-of-line)
(if (not (multi-term-is-at-end-line))
(beginning-of-line)
(term-send-raw))))
(defun multi-term-move-end-of-line ()
"Smart version of move-end-of-line in term-mode."
(interactive)
(if (not (multi-term-is-term-mode))
(move-end-of-line nil)
(if (not (multi-term-is-at-end-line))
(progn
(move-end-of-line nil))
(term-send-raw-string "\C-e"))))
(defun multi-term-kill-line ()
"Smart kill-line in multi-term mode."
(interactive)
(if (and (eq 'term-mode major-mode)
(multi-term-is-at-end-line))
(term-send-raw-string "\C-k")
(kill-line)))
(defun multi-term-delete-char ()
"Delete char in term-mode"
(interactive)
(if (multi-term-is-at-end-line)
(term-send-raw)
(delete-char 1)))
(defun multi-term-expand-region ()
"Wrap er/expand-region function in term-mode."
(interactive)
(er/expand-region 1))
(defun multi-term--buffer-name-list ()
"Multi-term session list."
(let* ((multi-term-filter-buffer-list
(cl-remove-if-not
#'(lambda (x)
(and (if (string= system-type "windows-nt")
(eq 'comint-mode (buffer-mode x)) ;; windows system powershell
(member x multi-term-buffer-list))
(not (eq (current-buffer) x))))
(buffer-list))))
(mapcar (lambda (elt)
(save-current-buffer
(set-buffer elt)
(let (name)
(setq ab/debug3 default-directory)
(setq name (format "%s@%s"
(
decode-coding-string (buffer-name elt) 'utf-8)
(decode-coding-string default-directory 'utf-8)))
(list name elt))))
multi-term-filter-buffer-list)))
(defun multi-term-find ()
"Find multi-term by name, and switch it!"
(interactive)
(let* (collection key)
(setq collection (multi-term--buffer-name-list))
;; (setq ab/debug this-command) ;; only for debug
(setq key (completing-read "find multi-term by name: "
collection))
(let* ((buf (car (assoc-default key collection)))
(bufwind (get-buffer-window buf)))
(if (and bufwind (window-valid-p bufwind))
(progn
(message "switch to window %s" (buffer-name buf))
(select-window bufwind))
(when (bufferp buf)
(message "switch to buffer %s" (buffer-name buf))
(switch-to-buffer buf))))))
(defun multi-term-create-powershell ()
"Create a new power shell"
(interactive)
(let* (name)
(setq name (multi-term-generate-powershell-buffer-name))
(message "name = %s " name)
(setq koopa-powershell-buffer-name (multi-term-generate-powershell-buffer-name))
(koopa-run-powershell)))
(defun aborn/getname ()
"get"
(interactive)
(message "%s" (multi-term-generate-powershell-buffer-name)))
(defun multi-term-generate-powershell-buffer-name ()
"Auto create power shell buffer name"
(let* (blist name)
(setq blist (multi-term--buffer-name-list))
(setq name (format "*PowerShell%d*" (+ 1 (length blist))))
(format "%s" name)))
(defun multi-term-create (name)
"Create new term `NAME'"
(let ((old default-directory))
(unless (file-exists-p name)
(error "path %s does't exists, failed." name))
(setq default-directory name)
;; (message "old=%s dir=%s" old default-directory)
(multi-term)))
(defun multi-term-get-recover-alist ()
"Produce multi-term recover alist."
(mapcar (lambda (elt)
(save-current-buffer
(set-buffer elt)
(cons (buffer-name) default-directory)))
multi-term-buffer-list))
(defun multi-term-save-term-alist ()
"Save multi-term-recover-alist to file."
(setq multi-term-recover-alist (multi-term-get-recover-alist))
(with-temp-buffer
(insert
";; -*- mode: emacs-lisp -*-\n"
";; Opened multi-term alist used for recovery.\n")
(prin1 `(setq multi-term-recover-alist ',multi-term-recover-alist)
(current-buffer))
(write-region (point-min) (point-max) multi-term-recover-alist-file nil
'quiet)))
(defun multi-term-recover-terms ()
"Recover multi-term previous buffers."
(when multi-term-recovery-p
(message "recovery multi-term previous buffers.")
(dolist (elt multi-term-recover-alist)
(multi-term-create (cdr elt))
(run-hooks 'multi-term-recover-hook))))
(defun multi-term-plus-init ()
"Recover previous term sessions when emacs bootup."
(when (version<= "26.1" emacs-version) ;; emacs 26.1, t default
(setq term-char-mode-point-at-process-mark nil))
(add-hook 'kill-emacs-hook
'multi-term-save-term-alist)
(when (file-readable-p multi-term-recover-alist-file)
(load-file multi-term-recover-alist-file))
(multi-term-recover-terms)
(message "multi-term-plus inited."))
(provide 'multi-term-plus)
;;; multi-term-plus.el ends here