-
Notifications
You must be signed in to change notification settings - Fork 1
/
matlab-shell-gud.el
1122 lines (968 loc) · 40.9 KB
/
matlab-shell-gud.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; matlab-shell-gud.el --- GUD support in matlab-shell. -*- lexical-binding: t -*-
;; Copyright (C) 2024 Free Software Foundation, Inc.
;; Author: Eric Ludlam <[email protected]>
;;
;; 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:
;;
;; GUD (grand unified debugger) support for MATLAB shell.
;;
;; Includes setting up mlgud mode in the shell, and all filters, etc specific
;; to supporting mlgud.
(require 'matlab-shell)
(eval-and-compile
(require 'mlgud)
(require 'eieio)
)
;;; Code:
(defcustom matlab-shell-debug-tooltips-p nil
"*Enable tooltips displaying data values when at the K>> prompt.
Disable this option if the tooltips are too slow in your setup."
:group 'matlab-shell
:type 'boolean)
(defvar gud-matlab-debug-active nil
"Non-nil if MATLAB has a K>> prompt up.")
(defvar gud-matlab-debug-activate-hook nil
"Hooks run when MATLAB detects a K>> prompt after a >> prompt.")
(defvar gud-matlab-debug-deactivate-hook nil
"Hooks run when MATLAB detects a >> prompt after a K>> prompt.")
(defvar gud-matlab-tool-bar-map nil
"Toolbar keymap used when in MATLAB debug mode.")
(declare-function matlab-netshell-eval "matlab-netshell" (mode))
(defmacro matlab-at-fcn (cmd)
"Define CMD to be a GUD command that works w/ shell or netshell."
;; Note `arg' comes from mlgud-def declaration
`(if (matlab-shell-active-p)
(mlgud-call (concat ,cmd "%%") arg)
(if (matlab-netshell-active-p)
(matlab-netshell-eval (mlgud-format-command ,cmd arg))
(error "No MATLAB shell active"))))
(defmacro matlab-gud-fcn (cmd)
"Define CMD forms to be sent to a MATLAB shell."
;; Note `arg' comes from mlgud-def declaration
`(if gud-matlab-debug-active
(matlab-at-fcn ,cmd)
(error "MATLAB debugging not active")))
;;;###autoload
(defun matlab-shell-mode-gud-enable-bindings ()
"Enable GUD features for `matlab-shell' in the current buffer."
;; Make sure this is safe to use mlgud to debug MATLAB
(when (not (fboundp 'mlgud-def))
(error "Your Emacs is missing `mlgud-def' which means matlab-shell won't work correctly. Stopping"))
(mlgud-def mlgud-break (matlab-at-fcn "ebstop in %d%f at %l") "Set breakpoint at current line.")
(mlgud-def mlgud-remove (matlab-at-fcn "ebclear in %d%f at %l") "Remove breakpoint at current line.")
(mlgud-def mlgud-step (matlab-gud-fcn "dbstep in") "Step one source line, possibly into a function.")
(mlgud-def mlgud-next (matlab-gud-fcn "dbstep %p") "Step over one source line.")
(mlgud-def mlgud-cont (matlab-gud-fcn "dbcont") "Continue execution.")
(mlgud-def mlgud-stop-subjob (matlab-gud-fcn "dbquit") "Quit debugging.") ;; mlgud toolbar stop
(mlgud-def mlgud-finish (matlab-gud-fcn "dbstep out") "Finish executing current function.")
(mlgud-def mlgud-up (matlab-gud-fcn "dbup") "Up N stack frames (numeric arg).")
(mlgud-def mlgud-down (matlab-gud-fcn "dbdown") "Down N stack frames (numeric arg).")
(mlgud-def mlgud-list-breakpoints (matlab-at-fcn "ebstatus") "List breakpoints")
(mlgud-def mlgud-show-stack (matlab-at-fcn "ebstack") "Show stack")
;; using (mlgud-def mlgud-print "%e" "\C-p" "Eval expression at point") fails
;; (mlgud-def mlgud-print "% mlgud-print not available" "\C-p" "mlgud-print not available.")
(when window-system
(setq gud-matlab-tool-bar-map
(let ((map (make-sparse-keymap)))
(dolist (x '((mlgud-break . "gud/break")
(mlgud-remove . "gud/remove")
(mlgud-cont . "gud/cont")
(mlgud-next . "gud/next")
(mlgud-step . "gud/step")
(mlgud-finish . "gud/finish")
(mlgud-stop-subjob . "gud/stop")
(mlg-show-stack . "gud/all")
(mlgud-list-breakpoints . "describe")
))
(tool-bar-local-item-from-menu
(car x) (cdr x) map matlab-mode-map))
map))
)
(if (fboundp 'mlgud-make-debug-menu)
(mlgud-make-debug-menu))
(when (boundp 'tool-bar-map) ; not --without-x
(kill-local-variable 'tool-bar-map))
)
;;;###autoload
(defun matlab-shell-gud-startup ()
"Configure GUD when a new `matlab-shell' is initialized."
(mlgud-mode)
;; type of mlgud mode
(setq mlgud-minor-mode 'matlab)
;; This starts us supporting mlgud tooltips.
(add-to-list 'mlgud-tooltip-modes 'matlab-mode)
(make-local-variable 'mlgud-marker-filter)
(setq mlgud-marker-filter 'gud-matlab-marker-filter)
(make-local-variable 'mlgud-find-file)
(setq mlgud-find-file 'gud-matlab-find-file)
(global-matlab-shell-inactive-gud-minor-mode 1)
;; Setup our debug tracker.
(add-hook 'matlab-shell-prompt-appears-hook #'gud-matlab-debug-tracker)
(mlgud-set-buffer))
;;; GUD Functions
(defun gud-matlab-massage-args (file args)
"Argument message for starting matlab file.
I don't think I have to do anything, but I'm not sure.
FILE is ignored, and ARGS is returned."
(ignore file)
args)
(defun gud-matlab-find-file (f)
"Find file F when debugging frames in MATLAB."
(save-excursion
(let* ((realfname (if (string-match "\\.\\(p\\)$" f)
(progn
(aset f (match-beginning 1) ?m)
f)
f))
(buf (find-file-noselect realfname t)))
(set-buffer buf)
(if (fboundp 'mlgud-make-debug-menu)
(mlgud-make-debug-menu))
buf)))
;;; GUD Filter Function
;;
;; MATLAB's process filter handles output from the MATLAB process and
;; interprets it for formatting text, and for running the debugger.
(defvar matlab-shell-gud--marker-acc "")
(make-variable-buffer-local 'matlab-shell-gud--marker-acc)
(defvar gud-matlab-marker-regexp-plain-prompt "^K?>>"
"Regular expression for finding a prompt.")
(defvar gud-matlab-marker-regexp-K>> "^K>>"
"Regular expression for finding a file line-number.")
(defvar gud-matlab-marker-regexp->> "^>>"
"Regular expression for finding a file line-number.")
(defvar gud-matlab-dbhotlink nil
"Track if we've sent a dbhotlink request.")
(make-variable-buffer-local 'gud-matlab-dbhotlink)
(defun gud-matlab-marker-filter (string)
"Filters STRING for the Unified Debugger based on MATLAB output."
(setq matlab-shell-gud--marker-acc (concat matlab-shell-gud--marker-acc string))
(let ((output "") (frame nil))
;; ERROR DELIMITERS
;; Newer MATLAB's wrap error text in {^H }^H characters.
;; Convert into something COMINT won't delete so we can scan them.
(while (string-match "{" matlab-shell-gud--marker-acc)
(setq matlab-shell-gud--marker-acc (replace-match matlab-shell-errortext-start-text t t matlab-shell-gud--marker-acc 0)))
(while (string-match "}" matlab-shell-gud--marker-acc)
(setq matlab-shell-gud--marker-acc (replace-match matlab-shell-errortext-end-text t t matlab-shell-gud--marker-acc 0)))
;; DEBUG PROMPTS
(when (string-match gud-matlab-marker-regexp-K>> matlab-shell-gud--marker-acc)
;; Newer MATLAB's don't print useful info. We'll have to
;; search backward for the previous line to see if a frame was
;; displayed.
(when (and (not frame) (not gud-matlab-dbhotlink))
(let ((dbhlcmd (if matlab-shell-echoes
"dbhotlink()%%%\n"
;; If no echo, force an echo
"disp(['dbhotlink()%%%' newline]);dbhotlink();\n")))
;;(when matlab-shell-io-testing (message "!!> [%s]" dbhlcmd))
(process-send-string (get-buffer-process mlgud-comint-buffer) dbhlcmd)
)
(setq gud-matlab-dbhotlink t)
)
)
;; If we're forced to ask for a stack hotlink, we will see it come in via the
;; process output. Don't output anything until a K prompt is seen after the display
;; of the dbhotlink command.
(when gud-matlab-dbhotlink
(let ((start (string-match "dbhotlink()%%%" matlab-shell-gud--marker-acc))
(endprompt nil))
(if start
(progn
(setq output (substring matlab-shell-gud--marker-acc 0 start)
matlab-shell-gud--marker-acc (substring matlab-shell-gud--marker-acc start))
;; The hotlink text will persist until we see the K prompt.
(when (string-match gud-matlab-marker-regexp-plain-prompt matlab-shell-gud--marker-acc)
(setq endprompt (match-end 0))
;; (when matlab-shell-io-testing (message "!!xx [%s]" (substring matlab-shell-gud--marker-acc 0 endprompt)))
;; We're done with the text!
;; Capture the text that describes the new stack frame.
(save-match-data
(let* ((expr-end (match-beginning 0))
(m1 (string-match "dbhotlink()%%%\n" matlab-shell-gud--marker-acc))
(expr-start (match-end 0))
(expression (substring matlab-shell-gud--marker-acc expr-start expr-end)))
(ignore m1)
(when (> (length expression) 0)
(condition-case ERR
(let ((forms (read expression)))
(when forms
;;(message "About to evaluate forms: \"%S\"" forms)
(eval forms)))
(error
(message "Failed to evaluate dbhotlink expression: \"%s\"" expression)
(message "Error is: %S" ERR)
)
))
))
;;Remove it from the accumulator.
(setq matlab-shell-gud--marker-acc (substring matlab-shell-gud--marker-acc endprompt))
;; If we got all this at the same time, push output back onto the accumulator for
;; the next code bit to push it out.
(setq matlab-shell-gud--marker-acc (concat output matlab-shell-gud--marker-acc)
output ""
gud-matlab-dbhotlink nil)
))
;; Else, waiting for a link, but hasn't shown up yet.
;; TODO - what can I do here to fix var setting if it gets
;; locked?
(when (string-match gud-matlab-marker-regexp->> matlab-shell-gud--marker-acc)
;; A non-k prompt showed up. We're not going to get out request.
(setq gud-matlab-dbhotlink nil))
)))
;; This if makes sure that the entirety of an error output is brought in
;; so that matlab-shell-mode doesn't try to display a file that only partially
;; exists in the buffer. Thus, if MATLAB output:
;; error: /home/me/my/mo/mello.m,10,12
;; All of that is in the buffer, and it goes to mello.m, not just
;; the first half of that file name.
;; The below used to match against the prompt, not \n, but then text that
;; had error: in it for some other reason wouldn't display at all.
(if (and matlab-prompt-seen ;; don't pause output if prompt not seen
gud-matlab-dbhotlink ;; pause output if waiting on debugger
)
;; We could be collecting debug info. Wait before output.
nil
;; Finish off this part of the output. None of our special stuff
;; ends with a \n, so display those as they show up...
(while (string-match "^[^\n]*\n" matlab-shell-gud--marker-acc)
(setq output (concat output (substring matlab-shell-gud--marker-acc 0 (match-end 0)))
matlab-shell-gud--marker-acc (substring matlab-shell-gud--marker-acc (match-end 0))))
(if (string-match (concat gud-matlab-marker-regexp-plain-prompt "\\s-*$") matlab-shell-gud--marker-acc)
(setq output (concat output matlab-shell-gud--marker-acc)
matlab-shell-gud--marker-acc ""))
;; Check our output for a prompt, and existence of a frame.
;; If this is true, throw out the debug arrow stuff.
(if (and (string-match (concat gud-matlab-marker-regexp->> "\\s-*$") output)
mlgud-last-last-frame)
(progn
;; Clean up mlgud stuff.
(setq overlay-arrow-position nil
mlgud-last-last-frame nil
mlgud-overlay-arrow-position nil)
;; If stack is showing, clean it up.
(let* ((buff (mlg-set-stack nil))
(win (get-buffer-window buff)))
(when win
(select-window win)
(mlg-stack-quit)
))
;; Refresh stuff
(sit-for 0)
))
;; Check for any text that would be embarrassing to display partially.
;; If we don't see any, feel free to dump the rest of the accumulation buffer
(unless (or (string-match (regexp-quote "<a href=") matlab-shell-gud--marker-acc)
(string-match (regexp-quote "<EMACSCAP") matlab-shell-gud--marker-acc)
(string-match (regexp-quote "<ERROR") matlab-shell-gud--marker-acc))
(setq output (concat output matlab-shell-gud--marker-acc)
matlab-shell-gud--marker-acc "")
)
)
(if frame (setq mlgud-last-frame frame))
(when matlab-shell-io-testing
(message "-->[%s] [%s]" output matlab-shell-gud--marker-acc))
;;(message "Looking for prompt in %S" output)
(when (and (not matlab-shell-suppress-prompt-hooks)
(string-match gud-matlab-marker-regexp-plain-prompt output))
;; Now that we are about to dump this, run our prompt hook.
;;(message "PROMPT!")
(setq matlab-shell-prompt-hook-cookie t))
output))
;;; Stack tracking
;;
(defclass mlg-stack-frame ()
((file :initarg :file
:type string
:documentation
"The filename this frame belongs to.")
(name :initarg :name
:type string
:documentation
"The name of the location of this frame")
(line :initarg :line
:type integer
:documentation
"The line number for this frame"))
"A single stack frame from MATLAB.")
(cl-defmethod mlg-print ((frame mlg-stack-frame) longestname)
"Use print to output this stack FRAME.
LONGESTNAME specifies the how long the longest name we can expect is."
(let* ((namefmt (concat "%" (number-to-string (or longestname 10)) "s"))
(str (concat (propertize (format namefmt (oref frame name)) 'face 'font-lock-function-name-face)
" "
(propertize (format "%3d" (oref frame line)) 'face 'bold)
" "
(propertize (oref frame file) 'face 'font-lock-constant-face))))
(setq str (propertize str 'object frame))
str))
(defvar mlg-stack nil
"The last stack sent to us from MATLAB.")
(defvar mlg-frame nil
"The last frame sent to use from MATLAB.")
(defun mlg-set-stack (newstack)
"Specify a NEWSTACK provided by MATLAB to replace the old one."
(setq mlg-stack nil)
(dolist (L newstack)
(push (make-instance 'mlg-stack-frame
:file (nth 0 L)
:name (nth 1 L)
:line (nth 2 L))
mlg-stack))
(setq mlg-stack (nreverse mlg-stack))
(mlg-refresh-stack-buffer)
;;(message "Updated Stack")
)
(defun mlg-set-stack-frame (newframe)
"Specify a NEWFRAME provided by MATLAB we should visit."
(setq mlg-frame newframe)
(mlg-show-stack)
(mlg-show-frame newframe)
)
(defun mlg-set-stack-frame-via-gud (newframe)
"Specify a NEWFRAME provided by MATLAB we should visit."
(setq mlg-frame newframe)
(let ((file (oref (nth (1- newframe) mlg-stack) file))
(line (oref (nth (1- newframe) mlg-stack) line)))
(if (< line 0) (setq line (- line)))
(setq mlgud-last-frame (cons file line))
;;(message "Gud FRAME set to %S" mlgud-last-frame)
)
)
(defun mlg-show-frame (&optional frame)
"Setup windows to show FRAME from the current stack frame."
(let ((newframe (or frame mlg-frame)))
(if (and mlg-stack (<= newframe (length mlg-stack)))
;; Make sure we have a stack window.
(let* ((buff (get-buffer "*MATLAB stack*"))
(win (get-buffer-window buff)))
(if (or (not buff) (not win))
(mlg-show-stack)
;; else, do refresh stuff.
(select-window win))
;; Still around, go do it.
(goto-char (point-min))
(forward-line (1- frame))
(mlg-stack-choose)
)
;; Else no frame. Look for the window, and close it.
(let* ((buff (get-buffer "*MATLAB stack*"))
(win (get-buffer-window buff)))
(when win (delete-window win)))
)))
(defun mlg-refresh-stack-buffer ()
"Refresh the buffer displaying stack."
(save-excursion
(let ((buff (get-buffer-create "*MATLAB stack*"))
(namelen 5)
(inhibit-read-only t))
(dolist (S mlg-stack)
(when (> (length (oref S name)) namelen)
(setq namelen (length (oref S name)))))
(set-buffer buff)
(erase-buffer)
(let ((cnt 1))
(dolist (F mlg-stack)
(insert (format "%2d" cnt))
(if (and mlg-frame (= cnt mlg-frame))
(insert " >> ")
(insert " -- "))
(insert (mlg-print F namelen) "\n")
(setq cnt (1+ cnt))))
(mlg-stack-mode)
(goto-char (point-min))
(current-buffer))))
(defun mlg-show-stack ()
"Display the MATLAB stack in an interactive buffer."
(interactive)
(let ((buff (mlg-refresh-stack-buffer)))
(display-buffer
buff
'((display-buffer-at-bottom)
(inhibit-same-window . t)
(window-height . fit-window-to-buffer))
)
(select-window (get-buffer-window buff))
(goto-char 3)
))
(defvar mlg-stack-mode-map
(let ((km (make-sparse-keymap)))
(define-key km [return] 'mlg-stack-choose)
(define-key km "q" 'mlg-stack-quit)
(define-key km "n" 'mlg-stack-next)
(define-key km "p" 'mlg-stack-prev)
(define-key km [mouse-2] 'mlg-stack-click)
(define-key km [mouse-1] 'mlg-stack-click)
km)
"Keymap used in MATLAB stack mode.")
;; Need this to fix weird problem in define-derived-mode
(defvar mlg-stack-mode-syntax-table (make-syntax-table)
"Syntax table used in `matlab-shell-help-mode'.")
(define-derived-mode mlg-stack-mode
fundamental-mode "MStack"
"Major mode for viewing a MATLAB stack.
Commands:
\\{mlg-stack-mode-map}"
:syntax-table mlg-stack-mode-syntax-table
(setq buffer-read-only t)
)
(defun mlg-stack-quit ()
"Quit the MATLAB stack view."
(interactive)
(if (= (length (window-list)) 1)
(bury-buffer)
(delete-window (selected-window))))
(defun mlg-stack-next ()
"Visit stack on next line."
(interactive)
(forward-line 1)
(forward-char 2)
(mlg-stack-choose))
(defun mlg-stack-prev ()
"Visit stack on next line."
(interactive)
(forward-line -1)
(forward-char 2)
(mlg-stack-choose))
(defun mlg-stack-click (e)
"Click on a stack frame to visit it.
Must be bound to event E."
(interactive "e")
(mouse-set-point e)
(mlg-stack-choose))
(defun mlg-stack-choose ()
"Choose the stack the under the cursor.
Visit the file presented in that stack frame."
(interactive)
(save-excursion
(beginning-of-line)
(forward-char 10)
(let* ((sf (get-text-property (point) 'object))
(f (oref sf file))
(l (oref sf line))
(buff (find-file-noselect f t)))
(display-buffer
buff
'((display-buffer-reuse-window display-buffer-use-some-window)
(inhibit-same-window . t))
)
(let ((win (selected-window)))
(select-window (get-buffer-window buff))
(goto-char (point-min))
(forward-line (1- l))
(select-window win))
)))
;;; Breakpoint Trackers
;;
(defclass mlg-breakpoint ()
((file :initarg :file
:type string
:documentation
"The filename this breakpoint belongs to.")
(name :initarg :name
:type string
:documentation
"Name of the function this breakpoint is in.")
(line :initarg :line
:type integer
:documentation
"The line number for this breakpoint")
(overlay :documentation
:default nil
"The overlay indicating the presence of this breakpoint.")
)
"Representation of a breakpoint.
Used to track active breakpoints, and how to show them.")
(cl-defmethod mlg-print ((break mlg-breakpoint) longestname)
"Use print to output this breakpoint BREAK.
LONGESTNAME specifies the how long the longest name we can expect is."
(let* ((namefmt (concat "%" (number-to-string (or longestname 10)) "s"))
(str (concat (propertize (format namefmt (oref break name)) 'face 'font-lock-function-name-face)
" "
(propertize (format "%3d" (oref break line)) 'face 'bold)
" "
(propertize (oref break file) 'face 'font-lock-constant-face))))
(setq str (propertize str 'object break))
str))
(defvar matlab-gud-visible-breakpoints nil
"List of breakpoints MATLAB has sent to us.")
;;;###autoload
(defun mlg-reset-breakpoints ()
"Remove all cached breakpoints."
(dolist (BP matlab-gud-visible-breakpoints)
(mlg-deactivate BP))
(setq matlab-gud-visible-breakpoints nil))
(defun mlg-add-breakpoint (file fcn line)
"Add a visible breakpoint to FILE FCN at LINE."
(let ((found nil))
(dolist (BP matlab-gud-visible-breakpoints)
(when (and (string= (oref BP file) file)
(= (oref BP line) line))
(setq found t)))
(when (not found)
(setq matlab-gud-visible-breakpoints
(cons (make-instance 'mlg-breakpoint
:file file
:name fcn
:line line)
matlab-gud-visible-breakpoints))
(mlg-activate (car matlab-gud-visible-breakpoints))
))
;; The first time breakpoints are added, make sure we can activate breakpoints
;; when new files are opened in a buffer.
(add-hook 'matlab-mode-hook 'mlg-breakpoint-activate-buffer-opened-hook)
)
(defun mlg-del-breakpoint (file fcn line)
"Add a visible breakpoint to FILE at LINE.
FCN is ignored."
(ignore fcn)
(let ((BPS matlab-gud-visible-breakpoints)
(NBPS nil))
(while BPS
(if (and (string= (oref (car BPS) file) file)
(= (oref (car BPS) line) line))
;; Deactivate
(mlg-deactivate (car BPS))
;; Not being removed, add to list.
(setq NBPS (cons (car BPS) NBPS)))
(setq BPS (cdr BPS)))
(setq matlab-gud-visible-breakpoints
(nreverse NBPS))))
(defface mlg-breakpoint-face
(list
(list t
(list :background nil
:foreground nil
:underline "red1")))
"*Face to use to highlight breakpoints."
:group 'matlab-shell)
(cl-defmethod mlg-activate ((bp mlg-breakpoint))
"Activate breakpoint BP if needed."
;; yes overlay, but inactive
(when (and (slot-boundp bp 'overlay)
(oref bp overlay)
(not (overlay-buffer (oref bp overlay))))
(oset bp overlay nil))
(let ((buff (find-buffer-visiting (oref bp file))))
;; No overlay, and we can make one.
(when (and (or (not (slot-boundp bp 'overlay))
(not (oref bp overlay)))
buff)
(with-current-buffer buff
(goto-char (point-min))
(forward-line (1- (oref bp line)))
(let ((ol (make-overlay (save-excursion
(back-to-indentation)
(point))
(line-end-position) buff nil nil)))
;; Store it
(oset bp overlay ol)
;; Setup cool stuff
(overlay-put ol 'face 'mlg-breakpoint-face)
(overlay-put ol 'before-string
(propertize "#"
'display
'(left-fringe
filled-square
matlab-shell-error-face))
))))
))
(cl-defmethod mlg-deactivate ((bp mlg-breakpoint))
"Deactivate this breakpoint BP."
(when (slot-boundp bp 'overlay)
(with-slots (overlay) bp
(when (and overlay (overlayp overlay))
(delete-overlay overlay)
(setq overlay nil)))))
(defun mlg-breakpoint-activate-buffer-opened-hook ()
"Activate any breakpoints in a buffer when that buffer is read in."
(if (not (matlab-shell-active-p))
(mlg-reset-breakpoints)
;; Still going, activate.
(dolist (BP matlab-gud-visible-breakpoints)
(mlg-activate BP)
)))
(defun mlg-breakpoint-flush-and-reactivate ()
"Flush existing breakpoint markers, and reactivate."
(interactive)
(dolist (BP matlab-gud-visible-breakpoints)
(mlg-deactivate BP)
(mlg-activate BP))
)
(defun mlg-refresh-breakpoint-buffer ()
"Refresh the buffer displaying breakpoints."
(save-excursion
(let ((buff (get-buffer-create "*MATLAB breakpoints*"))
(namelen 5)
(inhibit-read-only t))
(dolist (S matlab-gud-visible-breakpoints)
(when (> (length (oref S name)) namelen)
(setq namelen (length (oref S name)))))
(set-buffer buff)
(erase-buffer)
(let ((cnt 1))
(dolist (F matlab-gud-visible-breakpoints)
(insert (format "%2d - " cnt))
(insert (mlg-print F namelen) "\n")
(setq cnt (1+ cnt))))
(mlg-breakpoint-mode)
(goto-char (point-min))
(current-buffer))))
(defun mlg-show-breakpoints ()
"Display the MATLAB stack in an interactive buffer."
(interactive)
(let ((buff (mlg-refresh-breakpoint-buffer)))
(display-buffer
buff
'((display-buffer-at-bottom)
(inhibit-same-window . t)
(window-height . fit-window-to-buffer))
)
(select-window (get-buffer-window buff))
(goto-char 3)
))
(defvar mlg-breakpoint-mode-map
(let ((km (make-sparse-keymap)))
(define-key km [return] 'mlg-breakpoint-choose)
(define-key km "q" 'mlg-breakpoint-quit)
(define-key km "n" 'mlg-breakpoint-next)
(define-key km "p" 'mlg-breakpoint-prev)
(define-key km [mouse-2] 'mlg-breakpoint-click)
(define-key km [mouse-1] 'mlg-breakpoint-click)
km)
"Keymap used in MATLAB breakpoint mode.")
;; Need this to fix weird problem in define-derived-mode
(defvar mlg-breakpoint-mode-syntax-table (make-syntax-table)
"Syntax table used in `matlab-shell-help-mode'.")
(define-derived-mode mlg-breakpoint-mode
fundamental-mode "MBreakpoints"
"Major mode for viewing a MATLAB breakpoints.
Commands:
\\{mlg-breakpoint-mode-map}"
:syntax-table mlg-breakpoint-mode-syntax-table
(setq buffer-read-only t)
)
(defun mlg-breakpoint-quit ()
"Quit the MATLAB breakpoint view."
(interactive)
(if (= (length (window-list)) 1)
(bury-buffer)
(delete-window (selected-window))))
(defun mlg-breakpoint-next ()
"Visit breakpoint on next line."
(interactive)
(forward-line 1)
(forward-char 2)
(mlg-breakpoint-choose))
(defun mlg-breakpoint-prev ()
"Visit breakpoint on next line."
(interactive)
(forward-line -1)
(forward-char 2)
(mlg-breakpoint-choose))
(defun mlg-breakpoint-click (e)
"Click on a breakpoint frame to visit it.
Must be bound to event E."
(interactive "e")
(mouse-set-point e)
(mlg-breakpoint-choose))
(defun mlg-breakpoint-choose ()
"Choose the breakpoint the under the cursor.
Visit the file presented in that breakpoint frame."
(interactive)
(save-excursion
(beginning-of-line)
(forward-char 10)
(let* ((sf (get-text-property (point) 'object))
(f (oref sf file))
(l (oref sf line))
(buff (find-file-noselect f t)))
(display-buffer
buff
'((display-buffer-reuse-window display-buffer-use-some-window)
(inhibit-same-window . t))
)
(let ((win (selected-window)))
(select-window (get-buffer-window buff))
(goto-char (point-min))
(forward-line (1- l))
(select-window win))
)))
;;; K prompt state and hooks.
;;
(defun gud-matlab-debug-tracker ()
"Function called when new prompt appear.
Call debug activate/deactivate features."
(save-excursion
(let ((inhibit-field-text-motion t))
(goto-char (point-max))
(beginning-of-line)
(cond
((and gud-matlab-debug-active (looking-at gud-matlab-marker-regexp->>))
;; Debugger was active and we are back at prompt
(setq gud-matlab-debug-active nil)
(when (boundp 'tool-bar-map) ; not --without-x
(with-current-buffer (matlab-shell-active-p) (kill-local-variable 'tool-bar-map)))
(global-matlab-shell-gud-minor-mode -1)
(global-matlab-shell-inactive-gud-minor-mode 1)
(run-hooks 'gud-matlab-debug-deactivate-hook))
((and (not gud-matlab-debug-active) (looking-at gud-matlab-marker-regexp-K>>))
;; Debugger was NOT active and we are now in debug prompt
(setq gud-matlab-debug-active t)
(when (boundp 'tool-bar-map) ; not --without-x
(with-current-buffer (matlab-shell-active-p)
(setq-local tool-bar-map gud-matlab-tool-bar-map)))
(global-matlab-shell-gud-minor-mode 1)
(global-matlab-shell-inactive-gud-minor-mode -1)
(run-hooks 'gud-matlab-debug-activate-hook))
(t
;; All clear
))))
)
;;; MATLAB SHELL GUD Minor Mode
;;
;; When K prompt is active, this minor mode is applied to frame buffers so
;; that GUD commands are easy to get to.
(defvar matlab-shell-gud-minor-mode-map
(let ((km (make-sparse-keymap))
(key ?\ ))
(while (<= key ?~)
(define-key km (string key) 'matlab-shell-gud-mode-help-notice)
(setq key (1+ key)))
(define-key km "h" 'matlab-shell-gud-mode-help)
;; mlgud bindings.
(define-key km "b" 'mlgud-break)
(define-key km "x" 'mlgud-remove)
(define-key km "c" 'mlgud-cont)
(define-key km " " 'mlgud-step)
(define-key km "s" 'mlgud-step)
(define-key km "n" 'mlgud-next)
(define-key km "f" 'mlgud-finish)
(define-key km "q" 'mlgud-stop-subjob)
(define-key km "<" 'mlgud-up)
(define-key km ">" 'mlgud-down)
(define-key km "w" 'mlg-show-stack)
(define-key km "v" 'mlgud-list-breakpoints)
(define-key km "e" 'matlab-shell-gud-show-symbol-value)
(define-key km "\C-x\C-q" 'matlab-shell-gud-mode-edit) ; like toggle-read-only
km)
"Keymap used by matlab mode maintainers.")
(defun matlab-shell-gud-mode-help-notice ()
"Default binding for most keys in `matlab-shell-gud-minor-mode'.
Shows a help message in the mini buffer."
(interactive)
(error "MATLAB shell GUD minor-mode: Press 'h' for help, 'e' to go back to editing"))
(defun matlab-shell-gud-mode-help ()
"Show the default binding for most keys in `matlab-shell-gud-minor-mode'."
(interactive)
(describe-minor-mode 'matlab-shell-gud-minor-mode))
(defun matlab-shell-gud-mode-edit ()
"Turn off `matlab-shell-gud-minor-mode' so you can edit again."
(interactive)
(global-matlab-shell-gud-minor-mode -1))
(defun matlab-shell-gud-show-symbol-value (sym)
"Show the value of the symbol SYM under point from MATLAB shell."
(interactive
(list
(if (use-region-p)
;; Don't ask user anything, just take it.
(buffer-substring-no-properties (region-beginning) (region-end))
(let ((word (matlab-read-word-at-point)))
(read-from-minibuffer "MATLAB variable: " (cons word 0))))))
(let ((txt (matlab-shell-collect-command-output
(concat "disp(" sym ")"))))
(if (not (string-match "ERRORTXT" txt))
(matlab-output-to-temp-buffer "*MATLAB Help*" txt)
(message "Error evaluating MATLAB expression"))))
;;;###autoload
(define-minor-mode matlab-shell-gud-minor-mode
"Minor mode activated when `matlab-shell' K>> prompt is active.
This minor mode makes MATLAB buffers read only so simple keystrokes
activate debug commands. It also enables tooltips to appear when the
mouse hovers over a symbol when debugging.
\\<matlab-shell-gud-minor-mode-map>
Debug commands are:
\\[matlab-shell-gud-mode-edit] - Edit file (toggle read-only)
Allows editing file without causing MATLAB to exit debug mode.
\\[mlgud-break] - Add breakpoint (ebstop in FILE at point)
\\[mlgud-remove] - Remove breakpoint (ebclear in FILE at point)
\\[mlgud-list-breakpoints] - List breakpoints (ebstatus)
\\[mlgud-step] - Step (dbstep in)
\\[mlgud-next] - Next (dbstep)
\\[mlgud-finish] - Finish function (dbstep out)
\\[mlgud-cont] - Continue (dbcont)
\\[matlab-shell-gud-show-symbol-value] - Evaluate expression
\\[mlg-show-stack] - Where am I (ebstack)
\\[mlgud-stop-subjob] - Quit (dbquit)"
:init-value nil
:lighter " MGUD"
:keymap matlab-shell-gud-minor-mode-map
;; Make the buffer read only
(if matlab-shell-gud-minor-mode
(progn
;; Enable
(when (buffer-file-name) (setq buffer-read-only t))
(when matlab-shell-debug-tooltips-p
(mlgud-tooltip-mode 1)
(add-hook 'tooltip-functions 'gud-matlab-tooltip-tips)
)
;; Replace mlgud's toolbar which keeps stomping
;; on our toolbar.
(make-local-variable 'mlgud-tool-bar-map)
(setq mlgud-tool-bar-map gud-matlab-tool-bar-map)
)
;; Disable
(when (buffer-file-name)
(setq buffer-read-only (not (file-writable-p (buffer-file-name)))))
;; Always disable tooltips, in case configured while in the mode.
(mlgud-tooltip-mode -1)
(remove-hook 'tooltip-functions 'gud-matlab-tooltip-tips)
;; Disable the debug toolboar
(when (boundp 'tool-bar-map) ; not --without-x
(kill-local-variable 'tool-bar-map))))
(defun matlab-shell-gud-minor-mode-activator ()
"Activate gud in matlab-shell when in MATLAB mode."
(when (eq major-mode 'matlab-mode)
(matlab-shell-gud-minor-mode 1)))
;;;###autoload
(define-globalized-minor-mode global-matlab-shell-gud-minor-mode
matlab-shell-gud-minor-mode
matlab-shell-gud-minor-mode-activator
:group 'matlab-shell)
;;; MATLAB SHELL Inactive GUD Minor Mode
(defvar matlab-shell-inactive-gud-minor-mode-map
(let ((km (make-sparse-keymap)))
(define-key km "\C-c\C-d\C-h" 'matlab-shell-inactive-gud-mode-help)
;; mlgud bindings when debugger is inactive. When inactive, only bindings such as mlgud-break
;; make sense. However, we also keep these bindings when the debugger is active for consistency.
(define-key km (kbd "C-c C-d b") 'mlgud-break)
(define-key km (kbd "C-c C-d x") 'mlgud-remove)
(define-key km (kbd "C-c C-d c") 'mlgud-cont)
(define-key km (kbd "C-c C-d SPC") 'mlgud-step)
(define-key km (kbd "C-c C-d s") 'mlgud-step)
(define-key km (kbd "C-c C-d n") 'mlgud-next)
(define-key km (kbd "C-c C-d f") 'mlgud-finish)
(define-key km (kbd "C-c C-d q") 'mlgud-stop-subjob)
(define-key km (kbd "C-c C-d <") 'mlgud-up)
(define-key km (kbd "C-c C-d >") 'mlgud-down)
(define-key km (kbd "C-c C-d w") 'mlg-show-stack)
(define-key km (kbd "C-c C-d v") 'mlgud-list-breakpoints)
(define-key km (kbd "C-c C-d e") 'matlab-shell-gud-show-symbol-value)
km)
"Keymap used by matlab mode maintainers.")