mirrored from git://git.sv.gnu.org/emacs.git
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathedebug.el
4689 lines (4047 loc) · 171 KB
/
edebug.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
;;; edebug.el --- a source-level debugger for Emacs Lisp -*- lexical-binding: t -*-
;; Copyright (C) 1988-1995, 1997, 1999-2025 Free Software Foundation,
;; Inc.
;; Author: Daniel LaLiberte <[email protected]>
;; Maintainer: [email protected]
;; Keywords: lisp, tools, maint
;; This file is part of GNU Emacs.
;; GNU Emacs 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.
;; GNU Emacs 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 GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; This minor mode allows programmers to step through Emacs Lisp
;; source code while executing functions. You can also set
;; breakpoints, trace (stopping at each expression), evaluate
;; expressions as if outside Edebug, reevaluate and display a list of
;; expressions, trap errors normally caught by debug, and display a
;; debug style backtrace.
;;; Minimal Instructions
;; =====================
;; First evaluate a defun with C-M-x, then run the function. Step
;; through the code with SPC, mark breakpoints with b, go until a
;; breakpoint is reached with g, and quit execution with q. Use the
;; "?" command in edebug to describe other commands.
;; See the Emacs Lisp Reference Manual for more details.
;; If you wish to change the default edebug global command prefix, change:
;; (setq edebug-global-prefix "\C-xX")
;; Edebug was written by
;; Daniel LaLiberte
;; GTE Labs
;; 40 Sylvan Rd
;; Waltham, MA 02254
;;; Code:
(require 'backtrace)
(require 'macroexp)
(require 'cl-lib)
(require 'seq)
(eval-when-compile (require 'pcase))
(require 'debug)
;;; Options
(defgroup edebug nil
"A source-level debugger for Emacs Lisp."
:group 'lisp)
(defface edebug-enabled-breakpoint '((t :inherit highlight))
"Face used to mark enabled breakpoints."
:version "27.1")
(defface edebug-disabled-breakpoint
'((((class color) (min-colors 88) (background light))
:background "#ddffdd" :extend t)
(((class color) (min-colors 88) (background dark))
:background "#335533" :extend t))
"Face used to mark disabled breakpoints."
:version "27.1")
(defcustom edebug-setup-hook nil
"Functions to call before edebug is used.
Each time it is set to a new value, Edebug will call those functions
once and then reset `edebug-setup-hook' to nil. You could use this
to load up Edebug specifications associated with a package you are
using, but only when you also use Edebug."
:type 'hook)
;; edebug-all-defs and edebug-all-forms need to be autoloaded
;; because the byte compiler binds them; as a result, if edebug
;; is first loaded for a require in a compilation, they will be left unbound.
;;;###autoload
(defcustom edebug-all-defs nil
"If non-nil, evaluating defining forms instruments for Edebug.
This applies to `eval-defun', `eval-region' and `eval-buffer'.
`eval-region' is also called by `eval-last-sexp', and
`eval-print-last-sexp'.
You can use the command `edebug-all-defs' to toggle the value of this
variable. You may wish to make it local to each buffer with
\(make-local-variable \\='edebug-all-defs) in your
`emacs-lisp-mode-hook'.
Note that this user option has no effect unless the edebug
package has been loaded."
:require 'edebug
:type 'boolean)
;;;###autoload
(defcustom edebug-all-forms nil
"Non-nil means evaluation of all forms will instrument for Edebug.
This doesn't apply to loading or evaluations in the minibuffer.
Use the command `edebug-all-forms' to toggle the value of this option."
:type 'boolean)
(defcustom edebug-eval-macro-args nil
"Non-nil means all macro call arguments may be evaluated.
If this variable is nil, the default, Edebug will *not* wrap
macro call arguments as if they will be evaluated.
For each macro, an `edebug-form-spec' overrides this option.
So to specify exceptions for macros that have some arguments evaluated
and some not, use `def-edebug-spec' to specify an `edebug-form-spec'."
:type 'boolean)
(defcustom edebug-max-depth 150
"Maximum recursion depth when instrumenting code.
This limit is intended to stop recursion if an Edebug specification
contains an infinite loop. When Edebug is instrumenting code
containing very large quoted lists, it may reach this limit and give
the error message \"Too deep - perhaps infinite loop in spec?\".
Make this limit larger to countermand that, but you may also need to
increase `max-lisp-eval-depth'."
:type 'integer
:version "26.1")
(defcustom edebug-save-windows t
"If non-nil, Edebug saves and restores the window configuration.
That takes some time, so if your program does not care what happens to
the window configurations, it is better to set this variable to nil.
If the value is a list, only the listed windows are saved and
restored.
`edebug-toggle-save-windows' may be used to change this variable."
:type '(choice boolean (repeat string)))
(defcustom edebug-save-displayed-buffer-points nil
"If non-nil, save and restore point in all displayed buffers.
Saving and restoring point in other buffers is necessary if you are
debugging code that changes the point of a buffer that is displayed
in a non-selected window. If Edebug or the user then selects the
window, the buffer's point will be changed to the window's point.
Saving and restoring point in all buffers is expensive, since it
requires selecting each window twice, so enable this only if you
need it."
:type 'boolean)
(defcustom edebug-initial-mode 'step
"Initial execution mode for Edebug, if non-nil.
If this variable is non-nil, it specifies the initial execution mode
for Edebug when it is first activated. Possible values are step, next,
go, Go-nonstop, trace, Trace-fast, continue, and Continue-fast."
:type '(choice (const step) (const next) (const go)
(const Go-nonstop) (const trace)
(const Trace-fast) (const continue)
(const Continue-fast)))
(defcustom edebug-trace nil
"Non-nil means display a trace of function entry and exit.
Tracing output is displayed in a buffer named `*edebug-trace*', one
function entry or exit per line, indented by the recursion level.
You can customize by replacing functions `edebug-print-trace-before'
and `edebug-print-trace-after'."
:type 'boolean)
(defcustom edebug-test-coverage nil
"If non-nil, Edebug tests coverage of all expressions debugged.
This is done by comparing the result of each expression with the
previous result. Coverage is considered OK if two different
results are found.
Use `edebug-display-freq-count' to display the frequency count and
coverage information for a definition."
:type 'boolean)
(defcustom edebug-continue-kbd-macro nil
"If non-nil, continue defining or executing any keyboard macro.
Use this with caution since it is not debugged."
:type 'boolean)
(defcustom edebug-print-length 50
"Maximum length of list to print before abbreviating, when in Edebug.
If this is nil, use the value of `print-length' instead."
:type '(choice (integer :tag "A number")
(const :tag "Use `print-length'" nil)))
(defcustom edebug-print-level 50
"Maximum depth of list nesting to print before abbreviating, when in Edebug.
If nil, use the value of `print-level' instead."
:type '(choice (integer :tag "A number")
(const :tag "Use `print-level'" nil)))
(defcustom edebug-print-circle t
"If non-nil, default value of `print-circle' for printing results in Edebug."
:type 'boolean)
(defcustom edebug-unwrap-results nil
"Non-nil if Edebug should unwrap results of expressions.
That is, Edebug will try to remove its own instrumentation from the result.
This is useful when debugging macros where the results of expressions
are instrumented expressions."
:type 'boolean)
(defcustom edebug-on-error t
"Value bound to `debug-on-error' while Edebug is active.
If `debug-on-error' is non-nil, that value is still used.
If the value is a list of signal names, Edebug will stop when any of
these errors are signaled from Lisp code whether or not the signal is
handled by a `condition-case'. This option is useful for debugging
signals that *are* handled since they would otherwise be missed.
After execution is resumed, the error is signaled again."
:type '(choice (const :tag "off")
(repeat :menu-tag "When"
:value (nil)
(symbol :format "%v"))
(const :tag "always" t)))
(defcustom edebug-on-quit t
"Value bound to `debug-on-quit' while Edebug is active."
:type 'boolean)
(defcustom edebug-global-break-condition nil
"If non-nil, an expression to test for at every stop point.
If the result is non-nil, then break. Errors are ignored."
:type 'sexp
:risky t)
(defcustom edebug-sit-for-seconds 1
"Number of seconds to pause when execution mode is `trace' or `continue'."
:type 'number)
(defcustom edebug-sit-on-break t
"Whether or not to pause for `edebug-sit-for-seconds' on reaching a break."
:type 'boolean
:version "26.1")
;;; Form spec utilities.
(defun edebug-get-spec (symbol)
"Return the Edebug spec of a given Lisp expression's head SYMBOL.
The argument is usually a symbol, but it doesn't have to be."
;; Get the spec of symbol resolving all indirection.
(let ((spec nil)
(indirect symbol))
(while
(and (symbolp indirect)
(setq indirect
(function-get indirect 'edebug-form-spec 'macro)))
;; (edebug-trace "indirection: %s" edebug-form-spec)
(setq spec indirect))
spec))
(define-obsolete-function-alias 'get-edebug-spec #'edebug-get-spec "28.1")
(defun edebug--get-elem-spec (elem)
"Return the specs of the Edebug element ELEM, if any.
ELEM has to be a symbol."
(or (get elem 'edebug-elem-spec)
;; For backward compatibility, we also allow the use of
;; a form's name as a shorthand to refer to its spec.
(edebug-get-spec elem)))
;;;###autoload
(defun edebug-basic-spec (spec)
"Return t if SPEC uses only extant spec symbols.
An extant spec symbol is a symbol that is not a function and has a
`edebug-form-spec' property."
(cond ((listp spec)
(catch 'basic
(while spec
(unless (edebug-basic-spec (car spec)) (throw 'basic nil))
(setq spec (cdr spec)))
t))
((symbolp spec)
(unless (functionp spec)
(and (function-get spec 'edebug-form-spec) t)))))
;;; Utilities
(defun edebug-lambda-list-keywordp (object)
"Return t if OBJECT is a lambda list keyword.
A lambda list keyword is a symbol that starts with `&'."
(and (symbolp object)
(= ?& (aref (symbol-name object) 0))))
(defun edebug-last-sexp ()
;; Return the last sexp before point in current buffer.
;; Assumes Emacs Lisp syntax is active.
(car
(read-from-string
(buffer-substring
(save-excursion
(forward-sexp -1)
(point))
(point)))))
(defun edebug-window-list ()
"Return a list of windows, in order of `next-window'."
;; This doesn't work for epoch.
(let (window-list)
(walk-windows (lambda (w) (push w window-list)))
(nreverse window-list)))
;; Not used.
'(defun edebug-two-window-p ()
"Return t if there are two windows."
(and (not (one-window-p))
(eq (selected-window)
(next-window (next-window)))))
(defun edebug-sort-alist (alist function)
;; Return the ALIST sorted with comparison function FUNCTION.
;; This uses 'sort so the sorting is destructive.
(sort alist (lambda (e1 e2)
(funcall function (car e1) (car e2)))))
;; Not used.
'(defmacro edebug-save-restriction (&rest body)
"Evaluate BODY while saving the current buffers restriction.
BODY may change buffer outside of current restriction, unlike
save-restriction. BODY may change the current buffer,
and the restriction will be restored to the original buffer,
and the current buffer remains current.
Return the result of the last expression in BODY."
(declare (debug t))
`(let ((edebug:s-r-beg (point-min-marker))
(edebug:s-r-end (point-max-marker)))
(unwind-protect
(progn ,@body)
(with-current-buffer (marker-buffer edebug:s-r-beg)
(narrow-to-region edebug:s-r-beg edebug:s-r-end)))))
;;; Display
(defconst edebug-trace-buffer "*edebug-trace*"
"Name of the buffer to put trace info in.")
(defun edebug-pop-to-buffer (buffer &optional window)
;; Like pop-to-buffer, but select window where BUFFER was last shown.
;; Select WINDOW if it is provided and still exists. Otherwise,
;; if buffer is currently shown in several windows, choose one.
;; Otherwise, find a new window, possibly splitting one.
;; FIXME: We should probably just be using `pop-to-buffer'.
(setq window
(cond
((and (window-live-p window)
(eq (window-buffer window) buffer))
window)
((eq (window-buffer) buffer)
;; Selected window already displays BUFFER.
(selected-window))
((get-buffer-window buffer 0))
((one-window-p 'nomini)
;; When there's one window only, split it.
(split-window (minibuffer-selected-window)))
((let ((trace-window (get-buffer-window edebug-trace-buffer)))
(catch 'found
(dolist (elt (window-list nil 'nomini))
(unless (or (eq elt (selected-window)) (eq elt trace-window)
(window-dedicated-p elt))
;; Found a non-dedicated window not showing
;; `edebug-trace-buffer', use it.
(throw 'found elt))))))
;; All windows are dedicated or show `edebug-trace-buffer', split
;; selected one.
(t (split-window (minibuffer-selected-window)))))
(set-window-buffer window buffer)
(select-window window)
(unless (memq (framep (selected-frame)) '(nil t pc))
(x-focus-frame (selected-frame)))
(set-window-hscroll window 0)) ;; should this be??
(defun edebug-get-displayed-buffer-points ()
;; Return a list of buffer point pairs, for all displayed buffers.
(let (list)
(walk-windows (lambda (w)
(unless (eq w (selected-window))
(push (cons (window-buffer w)
(window-point w))
list))))
list))
(defun edebug-set-buffer-points (buffer-points)
;; Restore the buffer-points created by edebug-get-displayed-buffer-points.
(save-current-buffer
(mapcar (lambda (buf-point)
(when (buffer-live-p (car buf-point))
(set-buffer (car buf-point))
(goto-char (cdr buf-point))))
buffer-points)))
(defun edebug-current-windows (which-windows)
;; Get either a full window configuration or some window information.
(if (listp which-windows)
(mapcar (lambda (window)
(if (window-live-p window)
(list window
(window-buffer window)
(window-point window)
(window-start window)
(window-hscroll window))))
which-windows)
(current-window-configuration)))
(defun edebug-set-windows (window-info)
;; Set either a full window configuration or some window information.
(if (listp window-info)
(mapcar (lambda (one-window-info)
(if one-window-info
(apply (lambda (window buffer point start hscroll)
(if (window-live-p window)
(progn
(set-window-buffer window buffer)
(set-window-point window point)
(set-window-start window start)
(set-window-hscroll window hscroll))))
one-window-info)))
window-info)
(set-window-configuration window-info)))
;;; Redefine read and eval functions
;; read is redefined to maybe instrument forms.
;; eval-defun is redefined to check edebug-all-forms and edebug-all-defs.
(defun edebug--read (orig &optional stream)
"Read one Lisp expression as text from STREAM, return as Lisp object.
If STREAM is nil, use the value of `standard-input' (which see).
STREAM or the value of `standard-input' may be:
a buffer (read from point and advance it)
a marker (read from where it points and advance it)
a function (call it with no arguments for each character,
call it with a char as argument to push a char back)
a string (takes text from string, starting at the beginning)
t (read text line using minibuffer and use it).
This version, from Edebug, maybe instruments the expression. But the
STREAM must be the current buffer to do so. Whether it instruments is
also dependent on the values of the option `edebug-all-defs' and
the option `edebug-all-forms'."
(or stream (setq stream standard-input))
(if (eq stream (current-buffer))
(edebug-read-and-maybe-wrap-form)
(funcall (or orig #'read) stream)))
(defvar edebug-result) ; The result of the function call returned by body.
;; We should somehow arrange to be able to do this
;; without actually replacing the eval-defun command.
(defun edebug--eval-defun (orig-fun edebug-it)
"Setting option `edebug-all-defs' to a non-nil value reverses the meaning
of the prefix argument. Code is then instrumented when this function is
invoked without a prefix argument.
If acting on a `defun' for FUNCTION, and the function was instrumented,
`Edebug: FUNCTION' is printed in the minibuffer. If not instrumented,
just FUNCTION is printed."
;; Re-install our advice, in case `debug' re-bound `load-read-function' to
;; its default value.
(add-function :around load-read-function #'edebug--read)
(let* ((edebug-all-forms (not (eq (not edebug-it) (not edebug-all-defs))))
(edebug-all-defs edebug-all-forms))
(funcall orig-fun nil)))
(defun edebug-eval-defun (edebug-it)
(declare (obsolete "use `eval-defun' or `edebug--eval-defun' instead" "28.1"))
(interactive "P")
(if (advice-member-p #'edebug--eval-defun 'eval-defun)
(eval-defun edebug-it)
(edebug--eval-defun #'eval-defun edebug-it)))
;;;###autoload
(defalias 'edebug-defun #'edebug-eval-top-level-form)
;;;###autoload
(defun edebug-eval-top-level-form ()
"Evaluate the top level form point is in, stepping through with Edebug.
This is like `eval-defun' except that it steps the code for Edebug
before evaluating it. It displays the value in the echo area
using `eval-expression' (which see).
If you do this on a function definition such as a defun or defmacro,
it defines the function and instruments its definition for Edebug,
so it will do Edebug stepping when called later. It displays
`Edebug: FUNCTION' in the echo area to indicate that FUNCTION is now
instrumented for Edebug.
If the current defun is actually a call to `defvar' or `defcustom',
evaluating it this way resets the variable using its initial value
expression even if the variable already has some other value.
\(Normally `defvar' and `defcustom' do not alter the value if there
already is one.)"
(interactive)
(eval-expression
;; Bind edebug-all-forms only while reading, not while evalling
;; but this causes problems while edebugging edebug.
(let ((edebug-all-forms t)
(edebug-all-defs t))
(eval-sexp-add-defvars
(edebug-read-top-level-form)))))
(defvar edebug-active nil) ;; Non-nil when edebug is active
(defun edebug-read-top-level-form ()
(let ((starting-point (point))
;; Don't enter Edebug while doing that, in case we're trying to
;; instrument things like end-of-defun.
(edebug-active t))
(end-of-defun)
(beginning-of-defun)
(prog1
(edebug-read-and-maybe-wrap-form)
;; Recover point, but only if no error occurred.
(goto-char starting-point))))
;; Compatibility with old versions.
(define-obsolete-function-alias 'edebug-all-defuns #'edebug-all-defs "28.1")
;;;###autoload
(defun edebug-all-defs ()
"Toggle edebugging of all definitions."
(interactive)
(setq edebug-all-defs (not edebug-all-defs))
(message "Edebugging all definitions is %s."
(if edebug-all-defs "on" "off")))
;;;###autoload
(defun edebug-all-forms ()
"Toggle edebugging of all forms."
(interactive)
(setq edebug-all-forms (not edebug-all-forms))
(message "Edebugging all forms is %s."
(if edebug-all-forms "on" "off")))
(defun edebug-install-read-eval-functions ()
(interactive)
(add-function :around load-read-function #'edebug--read)
(advice-add 'eval-defun :around #'edebug--eval-defun))
(defun edebug-uninstall-read-eval-functions ()
(interactive)
(remove-function load-read-function #'edebug--read)
(advice-remove 'eval-defun #'edebug--eval-defun))
;;; Edebug internal data
;; The internal data that is needed for edebugging is kept in the
;; buffer-local variable `edebug-form-data'.
(defvar-local edebug-form-data nil
"A list of entries associating symbols with buffer regions.
Each entry is an `edebug--form-data' struct with fields:
SYMBOL, BEGIN-MARKER, and END-MARKER. The markers
are at the beginning and end of an instrumented form and SYMBOL is
a symbol that holds all edebug related information for the form on its
property list.
In the future (haha!), the symbol will be irrelevant and edebug data will
be stored in the definitions themselves rather than in the property
list of a symbol.")
(cl-defstruct (edebug--form-data
;; Some callers expect accessors to return nil when passed nil.
(:type list)
(:constructor edebug--make-form-data-entry (name begin end))
(:predicate nil) (:constructor nil) (:copier nil))
name begin end)
(defsubst edebug-set-form-data-entry (entry name begin end)
(setf (edebug--form-data-name entry) name) ;; In case name is changed.
(set-marker (edebug--form-data-begin entry) begin)
(set-marker (edebug--form-data-end entry) end))
(defun edebug-get-form-data-entry (pnt &optional end-point)
;; Find the edebug form data entry which is closest to PNT.
;; If END-POINT is supplied, match must be exact.
;; Return nil if none found.
(let ((rest edebug-form-data)
closest-entry
(closest-dist 999999)) ;; Need maxint here.
(while (and rest (< 0 closest-dist))
(let* ((entry (car rest))
(begin (edebug--form-data-begin entry))
(dist (- pnt begin)))
(setq rest (cdr rest))
(if (and (<= 0 dist)
(< dist closest-dist)
(or (not end-point)
(= end-point (edebug--form-data-end entry)))
(<= pnt (edebug--form-data-end entry)))
(setq closest-dist dist
closest-entry entry))))
closest-entry))
;; Also need to find all contained entries,
;; and find an entry given a symbol, which should be just assq.
(defun edebug-form-data-symbol ()
"Return the edebug data symbol of the form where point is in.
If point is not inside an edebuggable form, signal an error."
(or (edebug--form-data-name (edebug-get-form-data-entry (point)))
(error "Not inside instrumented form")))
(defun edebug-make-top-form-data-entry (new-entry)
;; Make NEW-ENTRY the first element in the `edebug-form-data' list.
(edebug-clear-form-data-entry new-entry)
(push new-entry edebug-form-data))
(defun edebug-clear-form-data-entry (entry)
"If non-nil, clear ENTRY out of the form data.
Maybe clear the markers and delete the symbol's edebug property?"
(if entry
(progn
;; Instead of this, we could just find all contained forms.
;; (put (car entry) 'edebug nil) ;
;; (mapcar #'edebug-clear-form-data-entry ; dangerous
;; (get (car entry) 'edebug-dependents))
;; (set-marker (nth 1 entry) nil)
;; (set-marker (nth 2 entry) nil)
(setq edebug-form-data (delq entry edebug-form-data)))))
;;; Parser utilities
(defun edebug-syntax-error (&rest args)
;; Signal an invalid-read-syntax with ARGS.
(signal 'invalid-read-syntax args))
(defconst edebug-read-syntax-table
;; Lookup table for significant characters indicating the class of the
;; token that follows. This is not a \"real\" syntax table.
(let ((table (make-char-table 'syntax-table 'symbol))
(i 0))
(while (< i ?!)
(aset table i 'space)
(setq i (1+ i)))
(aset table ?\( 'lparen)
(aset table ?\) 'rparen)
(aset table ?\' 'quote)
(aset table ?\` 'backquote)
(aset table ?\, 'comma)
(aset table ?\" 'string)
(aset table ?\? 'char)
(aset table ?\[ 'lbracket)
(aset table ?\] 'rbracket)
(aset table ?\. 'dot)
(aset table ?\# 'hash)
;; We treat numbers as symbols, because of confusion with -, -1, and 1-.
;; We don't care about any other chars since they won't be seen.
table))
(defun edebug-next-token-class ()
;; Move to the next token and return its class. We only care about
;; lparen, rparen, dot, quote, backquote, comma, string, char, vector,
;; or symbol.
(edebug-skip-whitespace)
(if (and (eq (following-char) ?.)
(save-excursion
(forward-char 1)
(or (and (eq (aref edebug-read-syntax-table (following-char))
'symbol)
(not (= (following-char) ?\;)))
(eq (following-char) ?.))))
'symbol
(aref edebug-read-syntax-table (following-char))))
(defun edebug-skip-whitespace ()
;; Leave point before the next token, skipping white space and comments.
(skip-chars-forward " \t\r\n\f")
(while (= (following-char) ?\;)
(skip-chars-forward "^\n") ; skip the comment
(skip-chars-forward " \t\r\n\f")))
;; Mostly obsolete reader; still used in one case.
(defun edebug-read-sexp ()
;; Read one sexp from the current buffer starting at point.
;; Leave point immediately after it. A sexp can be a list or atom.
;; An atom is a symbol (or number), character, string, or vector.
;; This works for reading anything legitimate, but it
;; is gummed up by parser inconsistencies (bugs?)
(let ((class (edebug-next-token-class)))
(cond
;; read goes one too far if a (possibly quoted) string or symbol
;; is immediately followed by non-whitespace.
((eq class 'symbol) (read (current-buffer)))
((eq class 'string) (read (current-buffer)))
((eq class 'quote) (forward-char 1)
(list 'quote (edebug-read-sexp)))
((eq class 'backquote) (forward-char 1)
(list '\` (edebug-read-sexp)))
((eq class 'comma) (forward-char 1)
(list '\, (edebug-read-sexp)))
(t ; anything else, just read it.
(read (current-buffer))))))
;;; Offsets for reader
(defun edebug-get-edebug-or-ghost (name)
"Get NAME's value of property `edebug' or property `ghost-edebug'.
The idea is that should function NAME be recompiled whilst
debugging is in progress, property `edebug' will get set to a
marker. The needed data will then come from property
`ghost-edebug'."
(let ((e (get name 'edebug)))
(if (consp e)
e
(let ((g (get name 'ghost-edebug)))
(if (consp g)
g
e)))))
;; Define a structure to represent offset positions of expressions.
;; Each offset structure looks like: (before . after) for constituents,
;; or for structures that have elements: (before <subexpressions> . after)
;; where the <subexpressions> are the offset structures for subexpressions
;; including the head of a list.
(defvar edebug-offsets nil)
;; Stack of offset structures in reverse order of the nesting.
;; This is used to get back to previous levels.
(defvar edebug-offsets-stack nil)
(defvar edebug-current-offset nil) ; Top of the stack, for convenience.
;; The association list of objects read with the #n=object form.
;; Each member of the list has the form (n . object), and is used to
;; look up the object for the corresponding #n# construct.
(defvar edebug-read-objects nil)
;; We must store whether we just read a list with a dotted form that
;; is itself a list. This structure will be condensed, so the offsets
;; must also be condensed.
(defvar edebug-read-dotted-list nil)
(defsubst edebug-initialize-offsets ()
;; Reinitialize offset recording.
(setq edebug-current-offset nil))
(defun edebug-store-before-offset (point)
;; Add a new offset pair with POINT as the before offset.
(let ((new-offset (list point)))
(if edebug-current-offset
(setcdr edebug-current-offset
(cons new-offset (cdr edebug-current-offset)))
;; Otherwise, we are at the top level, so initialize.
(setq edebug-offsets new-offset
edebug-offsets-stack nil
edebug-read-dotted-list nil))
;; Cons the new offset to the front of the stack.
(setq edebug-offsets-stack (cons new-offset edebug-offsets-stack)
edebug-current-offset new-offset)
))
(defun edebug-store-after-offset (point)
;; Finalize the current offset struct by reversing it and
;; store POINT as the after offset.
(if (not edebug-read-dotted-list)
;; Just reverse the offsets of all subexpressions.
(setcdr edebug-current-offset (nreverse (cdr edebug-current-offset)))
;; We just read a list after a dot, which will be abbreviated out.
(setq edebug-read-dotted-list nil)
;; Drop the corresponding offset pair.
;; That is, nconc the reverse of the rest of the offsets
;; with the cdr of last offset.
(setcdr edebug-current-offset
(nconc (nreverse (cdr (cdr edebug-current-offset)))
(cdr (car (cdr edebug-current-offset))))))
;; Now append the point using nconc.
(setq edebug-current-offset (nconc edebug-current-offset point))
;; Pop the stack.
(setq edebug-offsets-stack (cdr edebug-offsets-stack)
edebug-current-offset (car edebug-offsets-stack)))
(defun edebug-ignore-offset ()
;; Ignore the last created offset pair.
(setcdr edebug-current-offset (cdr (cdr edebug-current-offset))))
(defmacro edebug-storing-offsets (point &rest body)
(declare (debug (form body)) (indent 1))
`(unwind-protect
(progn
(edebug-store-before-offset ,point)
,@body)
(edebug-store-after-offset (point))))
;;; Reader for Emacs Lisp.
;; Uses edebug-next-token-class (and edebug-skip-whitespace) above.
(defconst edebug-read-alist
'((symbol . edebug-read-symbol)
(lparen . edebug-read-list)
(string . edebug-read-string)
(quote . edebug-read-quote)
(backquote . edebug-read-backquote)
(comma . edebug-read-comma)
(lbracket . edebug-read-vector)
(hash . edebug-read-special)
))
(defun edebug-read-storing-offsets (stream)
(let (edebug-read-dotted-list) ; see edebug-store-after-offset
(edebug-storing-offsets (point)
(funcall
(or (cdr (assq (edebug-next-token-class) edebug-read-alist))
;; anything else, just read it.
#'read)
stream))))
(defalias 'edebug-read-symbol #'read)
(defalias 'edebug-read-string #'read)
(defun edebug-read-quote (stream)
;; Turn 'thing into (quote thing)
(forward-char 1)
(list
(edebug-storing-offsets (1- (point)) 'quote)
(edebug-read-storing-offsets stream)))
(defun edebug-read-backquote (stream)
;; Turn `thing into (\` thing)
(forward-char 1)
(list
(edebug-storing-offsets (1- (point)) '\`)
(edebug-read-storing-offsets stream)))
(defun edebug-read-comma (stream)
;; Turn ,thing into (\, thing). Handle ,@ and ,. also.
(let ((opoint (point)))
(forward-char 1)
(let ((symbol '\,))
(cond ((eq (following-char) ?\.)
(setq symbol '\,\.)
(forward-char 1))
((eq (following-char) ?\@)
(setq symbol '\,@)
(forward-char 1)))
;; Generate the same structure of offsets we would have
;; if the resulting list appeared verbatim in the input text.
(list
(edebug-storing-offsets opoint symbol)
(edebug-read-storing-offsets stream)))))
(defun edebug-read-special (stream)
"Read from STREAM a Lisp object beginning with #.
Turn #\\='thing into (function thing) and handle the read syntax for
circular objects. Let `read' read everything else."
(catch 'return
(forward-char 1)
(let ((start (point)))
(cond
((eq ?\' (following-char))
(forward-char 1)
(throw 'return
(list
(edebug-storing-offsets (- (point) 2) 'function)
(edebug-read-storing-offsets stream))))
((and (>= (following-char) ?0) (<= (following-char) ?9))
(while (and (>= (following-char) ?0) (<= (following-char) ?9))
(forward-char 1))
(let ((n (string-to-number (buffer-substring start (point)))))
(when read-circle
(cond
((eq ?= (following-char))
;; Make a placeholder for #n# to use temporarily.
(let* ((placeholder (cons nil nil))
(elem (cons n placeholder)))
(push elem edebug-read-objects)
;; Read the object and then replace the placeholder
;; with the object itself, wherever it occurs.
(forward-char 1)
(let ((obj (edebug-read-storing-offsets stream)))
(lread--substitute-object-in-subtree obj placeholder t)
(throw 'return (setf (cdr elem) obj)))))
((eq ?# (following-char))
;; #n# returns a previously read object.
(let ((elem (assoc n edebug-read-objects)))
(when (consp elem)
(forward-char 1)
(throw 'return (cdr elem))))))))))
;; Let read handle errors, radix notation, and anything else.
(goto-char (1- start))
(read stream))))
(defun edebug-read-list (stream)
(forward-char 1) ; skip \(
(prog1
(let ((elements))
(while (not (memq (edebug-next-token-class) '(rparen dot)))
(push (edebug-read-storing-offsets stream) elements))
(setq elements (nreverse elements))
(if (eq 'dot (edebug-next-token-class))
(let (dotted-form)
(forward-char 1) ; skip \.
(setq dotted-form (edebug-read-storing-offsets stream))
elements (nconc elements dotted-form)
(if (not (eq (edebug-next-token-class) 'rparen))
(edebug-syntax-error "Expected `)'"))
(setq edebug-read-dotted-list (listp dotted-form))
))
elements)
(forward-char 1) ; skip \)
))
(defun edebug-read-vector (stream)
(forward-char 1) ; skip \[
(prog1
(let ((elements))
(while (not (eq 'rbracket (edebug-next-token-class)))
(push (edebug-read-storing-offsets stream) elements))
(apply #'vector (nreverse elements)))
(forward-char 1) ; skip \]
))
;;; Cursors for traversal of list and vector elements with offsets.
;; Edebug's instrumentation is based on parsing the sexps, which come with
;; auxiliary position information. Instead of keeping the position
;; information together with the sexps, it is kept in a "parallel
;; tree" of offsets.
;;
;; An "edebug cursor" is a pair of a *list of sexps* (called the
;; "expressions") together with a matching list of offsets.
;; When we're parsing the content of a list, the
;; `edebug-cursor-expressions' is simply the list but when parsing
;; a vector, the `edebug-cursor-expressions' is a list formed of the
;; elements of the vector.
(defvar edebug-dotted-spec nil
"Set to t when matching after the dot in a dotted spec list.")
(defun edebug-new-cursor (expressions offsets)
;; Return a new cursor for EXPRESSIONS with OFFSETS.
(if (vectorp expressions)
(setq expressions (append expressions nil)))
(cons expressions offsets))
(defsubst edebug-set-cursor (cursor expressions offsets)
;; Set the CURSOR's EXPRESSIONS and OFFSETS to the given.
;; Return the cursor.
(setcar cursor expressions)
(setcdr cursor offsets)
cursor)
(defun edebug-copy-cursor (cursor)
;; Copy the cursor using the same object and offsets.
(cons (car cursor) (cdr cursor)))
(defsubst edebug-cursor-expressions (cursor)
(car cursor))
(defsubst edebug-cursor-offsets (cursor)
(cdr cursor))
(defsubst edebug-empty-cursor (cursor)
;; Return non-nil if CURSOR is empty - meaning no more elements.
(null (car cursor)))
(defsubst edebug-top-element (cursor)
;; Return the top element at the cursor.
;; Assumes not empty.
(car (car cursor)))
(defun edebug-top-element-required (cursor &rest error)
;; Check if a dotted form is required.
(if edebug-dotted-spec (edebug-no-match cursor "Dot expected."))
;; Check if there is at least one more argument.
(if (edebug-empty-cursor cursor) (apply #'edebug-no-match cursor error))
;; Return that top element.
(edebug-top-element cursor))
(defsubst edebug-top-offset (cursor)
;; Return the top offset pair corresponding to the top element.
(car (cdr cursor)))
(defun edebug-move-cursor (cursor)