/
track-mode.el
339 lines (299 loc) · 11.8 KB
/
track-mode.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
;; Copyright (C) 2015-2017, 2020 Free Software Foundation, Inc
;; Author: Rocky Bernstein <rocky@gnu.org>
;; 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/>.
;; tracks shell output
(require 'shell)
(require 'load-relative)
(require-relative-list
'("core" "helper" "track" "loc" "lochist" "file"
"fringe" "window" "regexp" "menu" "backtrace-mode"
"send" "shortkey" "utils") "realgud-")
(require-relative-list '("buffer/command") "realgud-buffer-")
;; FIXME figure out if I can put this in something like a header file.
(declare-function realgud-fringe-erase-history-arrows 'realgud-buffer-command)
(declare-function realgud:track-set-debugger 'realgud-track)
(declare-function realgud-populate-debugger-menu 'realgud-menu)
(declare-function realgud-cmdbuf-info-divert-output?= 'realgud-buffer-command)
(declare-function realgud-cmdbuf-info-prior-prompt-regexp=
'realgud-buffer-command)
(declare-function realgud-cmdbuf-info-set?
'realgud-buffer-command)
(declare-function realgud:canonic-major-mode
'realgud-utils)
(declare-function shell-mode 'shell)
(defconst realgud-track-truncate-default
10
"When `realgud-truncate-buffer` is called without arguments, save this many history location steps.")
(defvar realgud-track-mode-map
(let ((map (copy-keymap shell-mode-map)))
(set-keymap-parent map comint-mode-map)
(realgud-populate-debugger-menu map)
(define-key map "\r" 'realgud:send-input)
(define-key map [M-right] 'realgud-track-hist-newest)
(define-key map [M-down] 'realgud-track-hist-newer)
(define-key map [M-up] 'realgud-track-hist-older)
(define-key map [M-print] 'realgud-track-hist-older)
(define-key map [M-S-down] 'realgud-track-hist-newest)
(define-key map [M-S-up] 'realgud-track-hist-oldest)
(define-key map "\C-cS" 'realgud-window-src-undisturb-cmd)
(define-key map (kbd "C-c !d") 'realgud:goto-debugger-loc-line)
map)
"Keymap used in `realgud-track-minor-mode'.
\\{realgud-track-mode-map}")
(defvar realgud:tool-bar-map
(let ((map (make-sparse-keymap)))
(dolist (x '((realgud:cmd-break . "gud/break")
;; (realgud:cmd-remove . "gud/remove")
;; (realgud:cmd-print . "gud/print")
;; (realgud:cmd-pstar . "gud/pstar")
;; (realgud:cmd-pp . "gud/pp")
;; (realgud:cmd-watch . "gud/watch")
(realgud:cmd-restart . "gud/run")
;; (realgud:cmd-go . "gud/go")
;; (realgud:cmd-stop-subjob . "gud/stop")
(realgud:cmd-continue . "gud/cont")
(realgud:cmd-until . "gud/until")
(realgud:cmd-next . "gud/next")
(realgud:cmd-step . "gud/step")
(realgud:cmd-finish . "gud/finish")
;; (realgud:cmd-nexti . "gud/nexti")
;; (realgud:cmd-stepi . "gud/stepi")
(realgud:cmd-older-frame . "gud/up")
(realgud:cmd-newer-frame . "gud/down")
(realgud:cmdbuf-info-describe . "info"))
map)
(tool-bar-local-item-from-menu
(car x) (cdr x) map realgud-track-mode-map)))
"Toolbar used when `realgud' interface is active."
)
;;;###autoload
(define-minor-mode realgud-track-mode
"Minor mode for tracking debugging inside a process shell."
:init-value nil
:global nil
:group 'realgud
:lighter
(:eval (progn
(concat " "
(if (realgud-cmdbuf-info-set?)
(realgud-sget 'cmdbuf-info 'debugger-name)
"dbgr??"))))
:keymap realgud-track-mode-map
;; Setup/teardown
(realgud-track-mode-setup realgud-track-mode)
)
(defun realgud-track-remove-hooks()
(let ((mode (realgud:canonic-major-mode)))
(cond ((eq mode 'eshell)
(remove-hook 'eshell-output-filter-functions
'realgud-track-eshell-output-filter-hook))
((eq mode 'comint)
(remove-hook 'comint-output-filter-functions
'realgud-track-comint-output-filter-hook))
)))
(defun realgud-track-add-hooks()
(let ((mode (realgud:canonic-major-mode)))
(cond ((eq mode 'eshell)
(add-hook 'eshell-output-filter-functions
'realgud-track-eshell-output-filter-hook))
((eq mode 'comint)
(add-hook 'comint-output-filter-functions
'realgud-track-comint-output-filter-hook))
)))
;; We will hijack comint-truncate-buffer to augment it with awareness of
;; realgud. We save the original value and restore that when leaving the mode.
(defalias 'comint-truncate-buffer-orig
(symbol-function 'comint-truncate-buffer))
(defun realgud-track-truncate-buffer (&optional last-n)
"Truncate the buffer to the last LAST-N history commands.
This function could be on `comint-output-filter-functions' or bound to a key."
(interactive "p")
(if (realgud-cmdbuf?)
(let* ((info realgud-cmdbuf-info)
(loc-hist (realgud-cmdbuf-info-loc-hist info))
(loc-ring (realgud-loc-hist-ring loc-hist))
(locs (ring-elements loc-ring))
(clamped-last-n (min (or last-n realgud-track-truncate-default)
(length locs)))
(i (max 0 clamped-last-n))
(loc (ring-ref loc-ring i))
(cmd-marker)
)
(when (y-or-n-p
(format-message
"Truncate buffer to last %d steps and destroy older realgud debug history? "
clamped-last-n))
(save-excursion
;; Find a location marker in the history associated with clamped-last-n
(while (and (not (realgud-loc? loc)) (> i 0))
(setq i (1- i))
(setq loc (ring-ref loc-ring i)))
(when (realgud-loc? loc)
;; Delete up to loc.
(setq cmd-marker (realgud-loc-cmd-marker loc))
(goto-char cmd-marker)
(forward-line 0)
(beginning-of-line)
(let ((inhibit-read-only t))
(delete-region
(or (and (boundp 'realgud-point-min) realgud-point-min)
(point-min))
(point)))
;; Clear out location history for portion that was deleted.
(while (> (ring-length loc-ring) clamped-last-n)
(ring-remove loc-ring))
;; Set new last position and restore realgud tracking hooks.
(setf (realgud-cmdbuf-info-last-input-end realgud-cmdbuf-info) (point-max))
(realgud-track-add-hooks)
))
))
;; else
(message "Nothing done - not in command buffer")
))
(defun realgud-track-clear-buffer()
"Remove the entire command buffer.
This is like `comint-clear-buffer' or `comint-truncate-buffer' except we
coordinate the delete with realgud so that it doesn't get bolixed
by marker removal."
(interactive "")
(if (realgud-cmdbuf?)
(when (y-or-n-p "Clear buffer and destroy realgud debug history? ")
(realgud-track-remove-hooks)
;; Delete buffer from the beginning to just before the last input region.
(save-excursion
(goto-char (process-mark (get-buffer-process (current-buffer))))
(forward-line 0)
(beginning-of-line)
(let ((inhibit-read-only t))
(delete-region
(or (and (boundp 'realgud-point-min) realgud-point-min))
(point))))
;; Set new last position, while location history, and restore realgud tracking hooks.
(setf (realgud-cmdbuf-info-last-input-end realgud-cmdbuf-info) (point-max))
(setf (realgud-cmdbuf-info-loc-hist realgud-cmdbuf-info) (make-realgud-loc-hist))
(realgud-track-add-hooks)
)
;; else
(message "Nothing done - not in command buffer")
))
;; FIXME: this should have been picked up by require'ing track.
(defvar realgud-track-divert-string)
(defun realgud-track-mode-setup (mode-on?)
"Mode setup when entering or leaving `realgud-track-mode'.
Variable MODE-ON? is a boolean which specifies if we are going
into or out of this mode."
(if mode-on?
(let ((process (get-buffer-process (current-buffer))))
(unless process
(setq realgud-track-mode nil)
(error "Can't find a process for buffer %s" (current-buffer)))
(setq realgud-track-divert-string "")
(setq realgud-track-mode 't)
;; FIXME: save and chain process-sentinel via
;; (process-sentinel (get-buffer-process (current-buffer)))
(set-process-sentinel process 'realgud-term-sentinel)
(unless (and (realgud-cmdbuf-info-set?)
(realgud-sget 'cmdbuf-info 'debugger-name))
(call-interactively 'realgud:track-set-debugger))
(if (boundp 'comint-last-output-start)
(progn
(realgud-cmdbuf-info-prior-prompt-regexp= comint-prompt-regexp)
(realgud-cmdbuf-info-divert-output?= nil)
(let* ((regexp-hash
(and (realgud-cmdbuf-info? realgud-cmdbuf-info)
(realgud-sget 'cmdbuf-info 'regexp-hash)))
(prompt-pat (and regexp-hash
(gethash "prompt" regexp-hash))))
(if prompt-pat
(setq comint-prompt-regexp
(realgud-loc-pat-regexp prompt-pat)))))
(set-marker comint-last-output-start (point)))
(set (make-local-variable 'tool-bar-map) realgud:tool-bar-map)
(realgud-track-add-hooks)
(run-mode-hooks 'realgud-track-mode-hook))
;; else
(progn
(if (and (boundp 'comint-last-output-start) realgud-cmdbuf-info)
(setq comint-prompt-regexp
(realgud-sget 'cmdbuf-info 'prior-prompt-regexp))
)
(kill-local-variable 'realgud:tool-bar-map)
(realgud-fringe-erase-history-arrows)
(realgud-track-remove-hooks)
(let* ((cmd-process (get-buffer-process (current-buffer)))
(status (if cmd-process
(list (propertize (format ":%s"
(process-status cmd-process))
'face 'realgud-debugger-running))
""))
)
(setq mode-line-process status)
;; Force mode line redisplay soon.
(force-mode-line-update)
;; FIXME: This is a workaround. Without this, we comint doesn't
;; process commands
(unless (member 'comint-mode minor-mode-list) (comint-mode))
)
;; FIXME: restore/unchain old process sentinels.
)
)
)
;; For name == "trepan", produces:
;; (defvar trepan-track-mode nil
;; "Non-nil if using trepan track-mode ... "
;; (defvar trepan-track-mode-map (make-sparse-keymap))
;; (defvar trepan-short-key-mode-map (make-sparse-keymap))
;; (set-keymap-parent trepan-short-key-mode-map realgud-short-key-mode-map)
(defmacro realgud-track-mode-vars (name)
"Create a number of track-mode variables based on the debugger name NAME."
`(progn
(defvar ,(intern (concat name "-track-mode")) nil
,(format "Non-nil if using %s-track-mode as a minor mode of some other mode.
Use the command `%s-track-mode' to toggle or set this variable." name name))
(defvar ,(intern (concat name "-track-mode-map")) (make-sparse-keymap)
,(format "Keymap used in `%s-track-mode'." name))
(defvar ,(intern (concat name "-short-key-mode-map")) (make-sparse-keymap))
))
;; FIXME: The below could be a macro? I have a hard time getting
;; macros right.
(defun realgud-track-mode-body(name)
"This function is used in by custom debuggers: trepan3k, remake, gdb, etc.
NAME is the name of the debugger which is used to preface variables."
(realgud:track-set-debugger name)
(funcall (intern (concat "realgud-define-" name "-commands")))
(if (intern (concat name "-track-mode"))
(progn
(setq realgud-track-mode 't)
(run-mode-hooks (intern (concat name "-track-mode-hook"))))
(progn
(setq realgud-track-mode nil)
)))
(defun realgud:track-mode-disable()
"Disable the debugger track-mode hook."
(interactive "")
(if realgud-track-mode
(progn
(setq realgud-track-mode nil)
;; FIXME: for some reason, disabling trak mode also
;; disables shell mode. Reinitialize it?
(if (equal mode-name "Shell")
(shell-mode))
)
(message "Debugger is not in track mode")))
(defun realgud:track-mode-enable()
"Enable the debugger track-mode hook."
(interactive "")
(if realgud-track-mode
(message "Debugger track mode is already enabled.")
(setq realgud-track-mode t))
)
(provide-me "realgud-")