-
Notifications
You must be signed in to change notification settings - Fork 3
/
mobdebug-mode.el
342 lines (283 loc) · 12 KB
/
mobdebug-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
340
341
342
;;; mobdebug-mode.el --- Major mode for MobDebug -*- lexical-binding: t; -*-
;; Copyright (C) 2013 Shihpin Tseng
;; Author: Shihpin Tseng <[email protected]>
;; URL: https://github.com/deftsp/mobdebug-mode
;; Package-Requires: ((lua-mode "20130419") (emacs "24"))
;; Keywords:
;; 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:
;;
;;; Code:
(require 'lua-mode)
(require 'comint)
(defconst mobdebug-keywords
'("connect" "set"))
(defvar mobdebug-font-lock-keywords
(list
;; highlight all the reserved commands.
`(,(concat "\\_<" (regexp-opt mobdebug-keywords) "\\_>") . font-lock-keyword-face))
"Additional expressions to highlight in `mobdebug-mode'.")
(defvar mobdebug-prompt "MobDebug> "
"Prompt for `mobdebug'.")
(defface mobdebug-buffer-face
'((t (:foreground "#839496" :background "#002b36")))
"*Face used when mobdebug minor mode on")
(defvar mobdebug-mode-map
(let ((map (make-sparse-keymap)))
(define-key map "\t" 'completion-at-point)
map)
"Keymap for MobDebug mode.")
(defvar mobdebug-buffer nil "The current MobDebug process buffer.")
(defvar mobdebug-working-file nil "File in which MobDebug is debuging.")
(defvar mobdebug-use-evil-binding nil "If non-nil use evil mobdebug minor mode.")
(define-derived-mode mobdebug-mode comint-mode "MobDebug"
"Major mode for `mobdebug'.
\\<mobdebug-mode-map>"
:syntax-table lua-mode-syntax-table
(setq comint-prompt-regexp (concat "^" (regexp-quote mobdebug-prompt)))
(setq comint-prompt-read-only t)
(setq comint-process-echoes nil)
(set (make-local-variable 'comint-output-filter-functions)
'(ansi-color-process-output
comint-postoutput-scroll-to-bottom
comint-truncate-buffer
mobdebug-output-filter))
(set (make-local-variable 'comint-input-filter-functions)
'(mobdebug-input-filter))
(set (make-local-variable 'paragraph-separate) "\\'") ; this makes it so commands like M-{ and M-} work.
(set (make-local-variable 'paragraph-start) comint-prompt-regexp)
(set (make-local-variable 'font-lock-defaults) '(mobdebug-font-lock-keywords t))
(set (make-local-variable 'comint-completion-addsuffix) '("/" . ""))
(setq mode-line-process '(":%s on " (:eval (or mobdebug-working-file "_"))))
;; Useful for `hs-minor-mode'.
(setq-local comment-start "--")
(setq-local comment-use-global-state t))
(defun mobdebug-get-buffer-by-path (file-path)
(get-buffer (file-name-nondirectory file-path)))
(defun mobdebug-output-filter (output)
(cond ((string-match "Paused at file \\(.*\\) line \\([0-9]*\\)" output)
(let* ((file-path (match-string 1 output))
(line-num (string-to-number (match-string 2 output)))
(file-name (file-name-nondirectory file-path))
(buffer (get-buffer file-name)))
(cond ((and file-name (not line-num)) ;; first paused
(setq mobdebug-working-file file-name))
((and buffer line-num)
(with-current-buffer buffer
(let ((mark-bol (save-excursion
(goto-line line-num)
(point-marker))))
(if (not (string= mobdebug-working-file file-name))
(setq mobdebug-working-file file-name))
(setq overlay-arrow-position mark-bol)))))))))
(defun mobdebug-input-filter (input)
(let ((buffer (and mobdebug-working-file (get-buffer mobdebug-working-file))))
(when buffer
(with-current-buffer buffer
(let ((command (mobdebug-trim-string input)))
(cond ((member command '("run" "step" "out" "over" "exit"))
(setq overlay-arrow-position nil))))))))
;; http://ergoemacs.org/emacs/modernization_elisp_lib_problem.html
(defun mobdebug-trim-string (string)
"Remove white spaces in beginning and ending of STRING.
White space here is any of: space, tab, emacs newline (line feed, ASCII 10)."
(replace-regexp-in-string "\\`[ \t\n]*" "" (replace-regexp-in-string "[ \t\n]*\\'" "" string)))
(defvar mobdebug-lua-path "mobdebug.sh")
(defvar mobdebug-arguments '())
(defun run-mobdebug ()
"Run an inferior instance of `mobdebug' inside Emacs."
(interactive)
(let* ((mobdebug-program mobdebug-lua-path)
(buffer (comint-check-proc "MobDebug")))
;; pop to the "*MobDebug*" buffer if the process is dead, the
;; buffer is missing or it's got the wrong mode.
(pop-to-buffer
(if (or buffer (not (derived-mode-p 'mobdebug-mode))
(comint-check-proc (current-buffer)))
(get-buffer-create (or buffer "*MobDebug*"))
(current-buffer)))
;; create the comint process if there is no buffer.
(unless buffer
(apply 'make-comint-in-buffer "MobDebug" buffer
mobdebug-program mobdebug-arguments)
(mobdebug-mode)
(setq mobdebug-buffer "*MobDebug*"))))
(defun run-mobdebug-persistent-window ()
(interactive)
(let ((win (selected-window)))
(run-mobdebug)
(select-window win)))
(defvar mobdebug-minor-mode-map
(let ((map (make-sparse-keymap)))
;; (define-key map (kbd "F3") 'mobdebug-print-working-buffer)
(define-key map [f2] 'mobdebug-setb)
(define-key map [f7] 'mobdebug-into)
(define-key map [f8] 'mobdebug-over)
(define-key map [f9] 'mobdebug-run)
(define-key map [C-f9] 'mobdebug-out)
map)
"Keymap for mobdebug-minor-mode.")
;;;###autoload
(define-minor-mode mobdebug-minor-mode
"Toggle MobDebug mode.
Interactively with no argument, this command toggles the mode.
A positive prefix argument enables the mode, any other prefix
argument disables it. From Lisp, argument omitted or nil enables
the mode, `toggle' toggles the state."
:lighter " MobDebug"
:keymap mobdebug-minor-mode-map
(if mobdebug-minor-mode
(progn
(buffer-face-set 'mobdebug-buffer-face)
(buffer-face-mode t)
(if (and (fboundp 'evil-mobdebug-minor-mode) mobdebug-use-evil-binding)
(evil-mobdebug-minor-mode +1)))
(buffer-face-set nil)
(if (and (fboundp 'evil-mobdebug-minor-mode) mobdebug-use-evil-binding)
(evil-mobdebug-minor-mode -1))))
(defun mobdebug-send (string)
(let ((buffer (and mobdebug-buffer (get-buffer mobdebug-buffer))))
(if buffer
(with-current-buffer buffer
(comint-simple-send (mobdebug-proc) string)
(run-hook-with-args 'comint-input-filter-functions string))
(error "No MobDebug buffer, try `run-mobdebug'"))))
(defun mobdebug-setb ()
"sets a breakpoint"
(interactive)
(mobdebug-send
(format "setb %s %d" (buffer-file-name) (line-number-at-pos))))
(defun mobdebug-delb ()
"removes a breakpoint"
(interactive)
(mobdebug-send
(format "setb %s %d" (buffer-file-name) (line-number-at-pos))))
(defun mobdebug-delallb ()
"removes all breakpoints"
(interactive)
(mobdebug-send "delallb"))
(defun mobdebug-setw ()
"adds a new watch expression"
(interactive)
(mobdebug-send
(concat "setw " (read-string "watch expression: "))))
(defun mobdebug-delw ()
"removes the watch expression at index"
(interactive)
(mobdebug-send
(concat "delw " (read-string "watch index: "))))
(defun mobdebug-delallw ()
"removes all watch expressions"
(interactive)
(mobdebug-send "delallw"))
(defun mobdebug-run ()
"runs until next breakpoint"
(interactive)
(mobdebug-send "run"))
(defun mobdebug-step ()
"runs until next line, stepping into function calls"
(interactive)
(mobdebug-send "step"))
(defun mobdebug-over ()
"runs until next line, stepping over function calls"
(interactive)
(mobdebug-send "over"))
(defun mobdebug-out ()
"runs until line after returning from current function"
(interactive)
(mobdebug-send "out"))
(defun mobdebug-listb ()
"lists breakpoints"
(interactive)
(mobdebug-send "listb"))
(defun mobdebug-listw ()
"lists watch expressions"
(interactive)
(mobdebug-send "listw"))
(defun mobdebug-eval ()
"evaluates expression on the current context and returns its value"
(interactive)
(mobdebug-send
(concat "eval " (read-string "eval expression: "))))
(defun mobdebug-exec ()
"executes statement on the current context"
(interactive)
(mobdebug-send
(concat "exec " (read-string "exec statement: "))))
(defun mobdebug-load ()
"loads a local file for debugging"
(interactive)
(let ((file-name (read-file-name "Load file: ")))
(mobdebug-send
(concat "load " file-name))))
(defun mobdebug-reload ()
"restarts the current debugging session"
(interactive)
(mobdebug-send "reload"))
(defun mobdebug-stack ()
"reports stack trace"
(interactive)
(mobdebug-send "stack"))
(defun mobdebug-output ()
"capture and redirect io stream (default|copy|redirect)"
(interactive)
(mobdebug-send
(concat "output stdout " (completing-read
"Method: "
'(default copy redirect)))))
(defun mobdebug-basedir ()
"capture and redirect io stream (default|copy|redirect)"
(interactive)
(mobdebug-send
(concat "basedir " (read-directory-name "new basedir: "))))
(defun mobdebug-exit ()
"exits debugger"
(interactive)
(mobdebug-send "exit"))
(defun mobdebug-get-process ()
"Return the current MobDebug process or nil if none is running."
(get-buffer-process mobdebug-buffer))
(defun mobdebug-proc ()
(or (mobdebug-get-process)
(error "No current process. See variable `mobdebug-buffer'")))
;;; evil support
(eval-after-load "evil"
'(progn
(define-minor-mode evil-mobdebug-minor-mode
"minor mode to add evil keymappings to MobDebug mode."
:keymap (make-sparse-keymap)
(evil-local-mode t))
(evil-define-key 'normal evil-mobdebug-minor-mode-map "ba" 'mobdebug-setb)
(evil-define-key 'normal evil-mobdebug-minor-mode-map "bd" 'mobdebug-delb)
(evil-define-key 'normal evil-mobdebug-minor-mode-map "bc" 'mobdebug-delallb)
(evil-define-key 'normal evil-mobdebug-minor-mode-map "wa" 'mobdebug-setw)
(evil-define-key 'normal evil-mobdebug-minor-mode-map "wd" 'mobdebug-delw)
(evil-define-key 'normal evil-mobdebug-minor-mode-map "wc" 'mobdebug-delallw)
(evil-define-key 'normal evil-mobdebug-minor-mode-map "r" 'mobdebug-run)
(evil-define-key 'normal evil-mobdebug-minor-mode-map "s" 'mobdebug-step)
(evil-define-key 'normal evil-mobdebug-minor-mode-map "n" 'mobdebug-over)
(evil-define-key 'normal evil-mobdebug-minor-mode-map "o" 'mobdebug-over)
(evil-define-key 'normal evil-mobdebug-minor-mode-map "f" 'mobdebug-out)
(evil-define-key 'normal evil-mobdebug-minor-mode-map "bl" 'mobdebug-listb)
(evil-define-key 'normal evil-mobdebug-minor-mode-map "wl" 'mobdebug-listw)
(evil-define-key 'normal evil-mobdebug-minor-mode-map "e" 'mobdebug-eval)
(evil-define-key 'normal evil-mobdebug-minor-mode-map ";" 'mobdebug-exec)
(evil-define-key 'normal evil-mobdebug-minor-mode-map "L" 'mobdebug-load)
(evil-define-key 'normal evil-mobdebug-minor-mode-map "R" 'mobdebug-reload)
(evil-define-key 'normal evil-mobdebug-minor-mode-map "bt" 'mobdebug-stack)
(evil-define-key 'normal evil-mobdebug-minor-mode-map ">" 'mobdebug-output)
(evil-define-key 'normal evil-mobdebug-minor-mode-map "p" 'mobdebug-basedir)
(evil-define-key 'normal evil-mobdebug-minor-mode-map "X" 'mobdebug-exit)
(evil-define-key 'normal evil-mobdebug-minor-mode-map " " 'run-mobdebug-persistent-window)
(evil-define-key 'normal evil-mobdebug-minor-mode-map "z" 'run-mobdebug)))
(provide 'mobdebug-mode)
;;; mobdebug-mode.el ends here