1 ;;; data-debug.el --- Data structure debugger
3 ;; Copyright (C) 2007-2014 Free Software Foundation, Inc.
5 ;; Author: Eric M. Ludlam <zappo@gnu.org>
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; Provide a simple way to investigate particularly large and complex
30 ;; The best way to get started is to bind M-: to 'data-debug-eval-expression.
32 ;; (global-set-key "\M-:" 'data-debug-eval-expression)
34 ;; If you write functions with complex output that need debugging, you
35 ;; can make them interactive with data-debug-show-stuff. For example:
37 ;; (defun my-complex-output-fcn ()
38 ;; "Calculate something complicated at point, and return it."
39 ;; (interactive) ;; function not normally interactive
40 ;; (let ((stuff (do-stuff)))
41 ;; (when (interactive-p)
42 ;; (data-debug-show-stuff stuff "myStuff"))
52 (if (featurep 'xemacs
)
54 (defalias 'data-debug-overlay-properties
'extent-properties
)
55 (defalias 'data-debug-overlay-p
'extentp
)
56 (if (not (fboundp 'propertize
))
57 (defun dd-propertize (string &rest properties
)
58 "Mimic 'propertize' in from Emacs 23."
59 (add-text-properties 0 (length string
) properties string
)
62 (defalias 'dd-propertize
'propertize
))
66 (defalias 'data-debug-overlay-properties
'overlay-properties
)
67 (defalias 'data-debug-overlay-p
'overlayp
)
68 (defalias 'dd-propertize
'propertize
)
74 (defun data-debug-insert-property-list (proplist prefix
&optional parent
)
75 "Insert the property list PROPLIST.
76 Each line starts with PREFIX.
77 The attributes belong to the tag PARENT."
79 (let ((pretext (concat (symbol-name (car proplist
)) " : ")))
80 (data-debug-insert-thing (car (cdr proplist
))
84 (setq proplist
(cdr (cdr proplist
)))))
88 (defun data-debug-insert-overlay-props (overlay prefix
)
89 "Insert all the parts of OVERLAY.
90 PREFIX specifies what to insert at the start of each line."
91 (let ((attrprefix (concat (make-string (length prefix
) ?
) "# "))
92 (proplist (data-debug-overlay-properties overlay
)))
93 (data-debug-insert-property-list
98 (defun data-debug-insert-overlay-from-point (point)
99 "Insert the overlay found at the overlay button at POINT."
100 (let ((overlay (get-text-property point
'ddebug
))
101 (indent (get-text-property point
'ddebug-indent
))
107 (data-debug-insert-overlay-props overlay
108 (concat (make-string indent ?
)
113 (defun data-debug-insert-overlay-button (overlay prefix prebuttontext
)
114 "Insert a button representing OVERLAY.
115 PREFIX is the text that precedes the button.
116 PREBUTTONTEXT is some text between prefix and the overlay button."
117 (let ((start (point))
119 (str (format "%s" overlay
))
121 (insert prefix prebuttontext str
)
123 (put-text-property (- end
(length str
)) end
'face
'font-lock-comment-face
)
124 (put-text-property start end
'ddebug overlay
)
125 (put-text-property start end
'ddebug-indent
(length prefix
))
126 (put-text-property start end
'ddebug-prefix prefix
)
127 (put-text-property start end
'help-echo tip
)
128 (put-text-property start end
'ddebug-function
129 'data-debug-insert-overlay-from-point
)
136 (defun data-debug-insert-overlay-list (overlaylist prefix
)
137 "Insert all the parts of OVERLAYLIST.
138 PREFIX specifies what to insert at the start of each line."
140 (data-debug-insert-overlay-button (car overlaylist
)
143 (setq overlaylist
(cdr overlaylist
))))
145 (defun data-debug-insert-overlay-list-from-point (point)
146 "Insert the overlay found at the overlay list button at POINT."
147 (let ((overlaylist (get-text-property point
'ddebug
))
148 (indent (get-text-property point
'ddebug-indent
))
154 (data-debug-insert-overlay-list overlaylist
155 (concat (make-string indent ?
)
160 (defun data-debug-insert-overlay-list-button (overlaylist
163 "Insert a button representing OVERLAYLIST.
164 PREFIX is the text that precedes the button.
165 PREBUTTONTEXT is some text between prefix and the overlay list button."
166 (let ((start (point))
168 (str (format "#<overlay list: %d entries>" (length overlaylist
)))
170 (insert prefix prebuttontext str
)
172 (put-text-property (- end
(length str
)) end
'face
'font-lock-comment-face
)
173 (put-text-property start end
'ddebug overlaylist
)
174 (put-text-property start end
'ddebug-indent
(length prefix
))
175 (put-text-property start end
'ddebug-prefix prefix
)
176 (put-text-property start end
'help-echo tip
)
177 (put-text-property start end
'ddebug-function
178 'data-debug-insert-overlay-list-from-point
)
185 (defun data-debug-insert-buffer-props (buffer prefix
)
186 "Insert all the parts of BUFFER.
187 PREFIX specifies what to insert at the start of each line."
188 (let ((attrprefix (concat (make-string (length prefix
) ?
) "# "))
190 (list :filename
(buffer-file-name buffer
)
191 :live
(buffer-live-p buffer
)
192 :modified
(buffer-modified-p buffer
)
193 :size
(buffer-size buffer
)
194 :process
(get-buffer-process buffer
)
195 :localvars
(buffer-local-variables buffer
)
197 (data-debug-insert-property-list
202 (defun data-debug-insert-buffer-from-point (point)
203 "Insert the buffer found at the buffer button at POINT."
204 (let ((buffer (get-text-property point
'ddebug
))
205 (indent (get-text-property point
'ddebug-indent
))
211 (data-debug-insert-buffer-props buffer
212 (concat (make-string indent ?
)
217 (defun data-debug-insert-buffer-button (buffer prefix prebuttontext
)
218 "Insert a button representing BUFFER.
219 PREFIX is the text that precedes the button.
220 PREBUTTONTEXT is some text between prefix and the buffer button."
221 (let ((start (point))
223 (str (format "%S" buffer
))
225 (insert prefix prebuttontext str
)
227 (put-text-property (- end
(length str
)) end
'face
'font-lock-comment-face
)
228 (put-text-property start end
'ddebug buffer
)
229 (put-text-property start end
'ddebug-indent
(length prefix
))
230 (put-text-property start end
'ddebug-prefix prefix
)
231 (put-text-property start end
'help-echo tip
)
232 (put-text-property start end
'ddebug-function
233 'data-debug-insert-buffer-from-point
)
240 (defun data-debug-insert-buffer-list (bufferlist prefix
)
241 "Insert all the parts of BUFFERLIST.
242 PREFIX specifies what to insert at the start of each line."
244 (data-debug-insert-buffer-button (car bufferlist
)
247 (setq bufferlist
(cdr bufferlist
))))
249 (defun data-debug-insert-buffer-list-from-point (point)
250 "Insert the buffer found at the buffer list button at POINT."
251 (let ((bufferlist (get-text-property point
'ddebug
))
252 (indent (get-text-property point
'ddebug-indent
))
258 (data-debug-insert-buffer-list bufferlist
259 (concat (make-string indent ?
)
264 (defun data-debug-insert-buffer-list-button (bufferlist
267 "Insert a button representing BUFFERLIST.
268 PREFIX is the text that precedes the button.
269 PREBUTTONTEXT is some text between prefix and the buffer list button."
270 (let ((start (point))
272 (str (format "#<buffer list: %d entries>" (length bufferlist
)))
274 (insert prefix prebuttontext str
)
276 (put-text-property (- end
(length str
)) end
'face
'font-lock-comment-face
)
277 (put-text-property start end
'ddebug bufferlist
)
278 (put-text-property start end
'ddebug-indent
(length prefix
))
279 (put-text-property start end
'ddebug-prefix prefix
)
280 (put-text-property start end
'help-echo tip
)
281 (put-text-property start end
'ddebug-function
282 'data-debug-insert-buffer-list-from-point
)
289 (defun data-debug-insert-process-props (process prefix
)
290 "Insert all the parts of PROCESS.
291 PREFIX specifies what to insert at the start of each line."
292 (let ((attrprefix (concat (make-string (length prefix
) ?
) "# "))
293 (id (process-id process
))
294 (tty (process-tty-name process
))
295 (pcontact (process-contact process t
))
296 (proplist (process-plist process
)))
297 (data-debug-insert-property-list
299 (if id
(list 'id id
))
300 (if tty
(list 'tty tty
))
301 (if pcontact pcontact
)
307 (defun data-debug-insert-process-from-point (point)
308 "Insert the process found at the process button at POINT."
309 (let ((process (get-text-property point
'ddebug
))
310 (indent (get-text-property point
'ddebug-indent
))
316 (data-debug-insert-process-props process
317 (concat (make-string indent ?
)
322 (defun data-debug-insert-process-button (process prefix prebuttontext
)
323 "Insert a button representing PROCESS.
324 PREFIX is the text that precedes the button.
325 PREBUTTONTEXT is some text between prefix and the process button."
326 (let ((start (point))
328 (str (format "%S : %s" process
(process-status process
)))
330 (insert prefix prebuttontext str
)
332 (put-text-property (- end
(length str
)) end
'face
'font-lock-comment-face
)
333 (put-text-property start end
'ddebug process
)
334 (put-text-property start end
'ddebug-indent
(length prefix
))
335 (put-text-property start end
'ddebug-prefix prefix
)
336 (put-text-property start end
'help-echo tip
)
337 (put-text-property start end
'ddebug-function
338 'data-debug-insert-process-from-point
)
345 ;; A ring (like kill-ring, or whatever.)
346 (defun data-debug-insert-ring-contents (ring prefix
)
347 "Insert all the parts of RING.
348 PREFIX specifies what to insert at the start of each line."
349 (let ((len (ring-length ring
))
353 (data-debug-insert-thing (ring-ref ring idx
) prefix
"")
357 (defun data-debug-insert-ring-items-from-point (point)
358 "Insert the ring found at the ring button at POINT."
359 (let ((ring (get-text-property point
'ddebug
))
360 (indent (get-text-property point
'ddebug-indent
))
366 (data-debug-insert-ring-contents ring
367 (concat (make-string indent ?
)
372 (defun data-debug-insert-ring-button (ring
375 "Insert a button representing RING.
376 PREFIX is the text that precedes the button.
377 PREBUTTONTEXT is some text between prefix and the stuff list button."
378 (let* ((start (point))
380 (str (format "#<RING: %d, %d max>"
384 (if (= (ring-length ring
) 0) nil
(ring-ref ring
0)))
385 (tip (format "Ring max-size %d, length %d."
389 (insert prefix prebuttontext str
)
391 (put-text-property (- end
(length str
)) end
'face
'font-lock-type-face
)
392 (put-text-property start end
'ddebug ring
)
393 (put-text-property start end
'ddebug-indent
(length prefix
))
394 (put-text-property start end
'ddebug-prefix prefix
)
395 (put-text-property start end
'help-echo tip
)
396 (put-text-property start end
'ddebug-function
397 'data-debug-insert-ring-items-from-point
)
406 (defun data-debug-insert-hash-table (hash-table prefix
)
407 "Insert the contents of HASH-TABLE inserting PREFIX before each element."
410 (data-debug-insert-thing
412 (dd-propertize "key " 'face font-lock-comment-face
))
413 (data-debug-insert-thing
415 (dd-propertize "val " 'face font-lock-comment-face
)))
418 (defun data-debug-insert-hash-table-from-point (point)
419 "Insert the contents of the hash-table button at POINT."
420 (let ((hash-table (get-text-property point
'ddebug
))
421 (indent (get-text-property point
'ddebug-indent
))
426 (data-debug-insert-hash-table
428 (concat (make-string indent ?
) "> "))
432 (defun data-debug-insert-hash-table-button (hash-table prefix prebuttontext
)
433 "Insert HASH-TABLE as expandable button with recursive prefix PREFIX and PREBUTTONTEXT in front of the button text."
434 (let ((string (dd-propertize (format "%s" hash-table
)
435 'face
'font-lock-keyword-face
)))
436 (insert (dd-propertize
437 (concat prefix prebuttontext string
)
439 'ddebug-indent
(length prefix
)
440 'ddebug-prefix prefix
442 (format "Hash-table\nTest: %s\nWeakness: %s\nElements: %d (of %d)"
443 (hash-table-test hash-table
)
444 (if (hash-table-weakness hash-table
) "yes" "no")
445 (hash-table-count hash-table
)
446 (hash-table-size hash-table
))
448 'data-debug-insert-hash-table-from-point
)
454 ;; Widgets have a long list of properties
455 (defun data-debug-insert-widget-properties (widget prefix
)
456 "Insert the contents of WIDGET inserting PREFIX before each element."
457 (let ((type (car widget
))
460 (data-debug-insert-thing (car (cdr rest
))
463 (dd-propertize (format "%s" (car rest
))
464 'face font-lock-comment-face
)
466 (setq rest
(cdr (cdr rest
))))
469 (defun data-debug-insert-widget-from-point (point)
470 "Insert the contents of the widget button at POINT."
471 (let ((widget (get-text-property point
'ddebug
))
472 (indent (get-text-property point
'ddebug-indent
))
477 (data-debug-insert-widget-properties
478 widget
(concat (make-string indent ?
) "# "))
482 (defun data-debug-insert-widget (widget prefix prebuttontext
)
484 A Symbol is a simple thing, but this provides some face and prefix rules.
485 PREFIX is the text that precedes the button.
486 PREBUTTONTEXT is some text between prefix and the thing."
487 (let ((string (dd-propertize (format "#<WIDGET %s>" (car widget
))
488 'face
'font-lock-keyword-face
)))
489 (insert (dd-propertize
490 (concat prefix prebuttontext string
)
492 'ddebug-indent
(length prefix
)
493 'ddebug-prefix prefix
495 (format "Widget\nType: %s\n# Properties: %d"
497 (/ (1- (length widget
)) 2))
499 'data-debug-insert-widget-from-point
)
504 ;; just a list. random stuff inside.
505 (defun data-debug-insert-stuff-list (stufflist prefix
)
506 "Insert all the parts of STUFFLIST.
507 PREFIX specifies what to insert at the start of each line."
509 (data-debug-insert-thing
510 ;; Some lists may put a value in the CDR
511 (if (listp stufflist
) (car stufflist
) stufflist
)
515 (if (listp stufflist
)
519 (defun data-debug-insert-stuff-list-from-point (point)
520 "Insert the stuff found at the stuff list button at POINT."
521 (let ((stufflist (get-text-property point
'ddebug
))
522 (indent (get-text-property point
'ddebug-indent
))
528 (data-debug-insert-stuff-list stufflist
529 (concat (make-string indent ?
)
534 (defun data-debug-insert-stuff-list-button (stufflist
537 "Insert a button representing STUFFLIST.
538 PREFIX is the text that precedes the button.
539 PREBUTTONTEXT is some text between prefix and the stuff list button."
540 (let ((start (point))
544 (format "#<list o' stuff: %d entries>" (safe-length stufflist
))
545 (error "#<list o' stuff>")))
546 (tip (if (or (listp (car stufflist
))
547 (vectorp (car stufflist
)))
549 (format "%s" stufflist
))))
550 (insert prefix prebuttontext str
)
552 (put-text-property (- end
(length str
)) end
'face
'font-lock-variable-name-face
)
553 (put-text-property start end
'ddebug stufflist
)
554 (put-text-property start end
'ddebug-indent
(length prefix
))
555 (put-text-property start end
'ddebug-prefix prefix
)
556 (put-text-property start end
'help-echo tip
)
557 (put-text-property start end
'ddebug-function
558 'data-debug-insert-stuff-list-from-point
)
565 ;; just a vector. random stuff inside.
566 (defun data-debug-insert-stuff-vector (stuffvector prefix
)
567 "Insert all the parts of STUFFVECTOR.
568 PREFIX specifies what to insert at the start of each line."
570 (while (< idx
(length stuffvector
))
571 (data-debug-insert-thing
572 ;; Some vectors may put a value in the CDR
573 (aref stuffvector idx
)
576 (setq idx
(1+ idx
)))))
578 (defun data-debug-insert-stuff-vector-from-point (point)
579 "Insert the stuff found at the stuff vector button at POINT."
580 (let ((stuffvector (get-text-property point
'ddebug
))
581 (indent (get-text-property point
'ddebug-indent
))
587 (data-debug-insert-stuff-vector stuffvector
588 (concat (make-string indent ?
)
593 (defun data-debug-insert-stuff-vector-button (stuffvector
596 "Insert a button representing STUFFVECTOR.
597 PREFIX is the text that precedes the button.
598 PREBUTTONTEXT is some text between prefix and the stuff vector button."
599 (let* ((start (point))
601 (str (format "#<vector o' stuff: %d entries>" (length stuffvector
)))
603 (insert prefix prebuttontext str
)
605 (put-text-property (- end
(length str
)) end
'face
'font-lock-variable-name-face
)
606 (put-text-property start end
'ddebug stuffvector
)
607 (put-text-property start end
'ddebug-indent
(length prefix
))
608 (put-text-property start end
'ddebug-prefix prefix
)
609 (put-text-property start end
'help-echo tip
)
610 (put-text-property start end
'ddebug-function
611 'data-debug-insert-stuff-vector-from-point
)
619 (defun data-debug-insert-symbol-from-point (point)
620 "Insert attached properties and possibly the value of symbol at POINT."
621 (let ((symbol (get-text-property point
'ddebug
))
622 (indent (get-text-property point
'ddebug-indent
))
627 (when (and (not (fboundp symbol
)) (boundp symbol
))
628 (data-debug-insert-thing
629 (symbol-value symbol
)
630 (concat (make-string indent ?
) "> ")
632 (dd-propertize "value"
633 'face
'font-lock-comment-face
)
635 (data-debug-insert-property-list
636 (symbol-plist symbol
)
637 (concat (make-string indent ?
) "> "))
641 (defun data-debug-insert-symbol-button (symbol prefix prebuttontext
)
642 "Insert a button representing SYMBOL.
643 PREFIX is the text that precedes the button.
644 PREBUTTONTEXT is some text between prefix and the symbol button."
646 (cond ((fboundp symbol
)
647 (dd-propertize (concat "#'" (symbol-name symbol
))
648 'face
'font-lock-function-name-face
))
650 (dd-propertize (concat "'" (symbol-name symbol
))
651 'face
'font-lock-variable-name-face
))
652 (t (format "'%s" symbol
)))))
653 (insert (dd-propertize
654 (concat prefix prebuttontext string
)
656 'ddebug-indent
(length prefix
)
657 'ddebug-prefix prefix
660 'data-debug-insert-symbol-from-point
)
665 (defun data-debug-insert-string (thing prefix prebuttontext
)
666 "Insert one symbol THING.
667 A Symbol is a simple thing, but this provides some face and prefix rules.
668 PREFIX is the text that precedes the button.
669 PREBUTTONTEXT is some text between prefix and the thing."
670 (let ((newstr thing
))
671 (while (string-match "\n" newstr
)
672 (setq newstr
(replace-match "\\n" t t newstr
)))
673 (while (string-match "\t" newstr
)
674 (setq newstr
(replace-match "\\t" t t newstr
)))
675 (insert prefix prebuttontext
676 (dd-propertize (format "\"%s\"" newstr
)
677 'face font-lock-string-face
)
681 (defun data-debug-insert-number (thing prefix prebuttontext
)
682 "Insert one symbol THING.
683 A Symbol is a simple thing, but this provides some face and prefix rules.
684 PREFIX is the text that precedes the button.
685 PREBUTTONTEXT is some text between prefix and the thing."
686 (insert prefix prebuttontext
687 (dd-propertize (format "%S" thing
)
688 'face font-lock-string-face
)
691 ;;; Lambda Expression
692 (defun data-debug-insert-lambda-expression (thing prefix prebuttontext
)
693 "Insert one lambda expression THING.
694 A Symbol is a simple thing, but this provides some face and prefix rules.
695 PREFIX is the text that precedes the button.
696 PREBUTTONTEXT is some text between prefix and the thing."
697 (let ((txt (prin1-to-string thing
)))
698 (data-debug-insert-simple-thing
699 txt prefix prebuttontext
'font-lock-keyword-face
))
703 (defun data-debug-insert-nil (thing prefix prebuttontext
)
704 "Insert one simple THING with a face.
705 PREFIX is the text that precedes the button.
706 PREBUTTONTEXT is some text between prefix and the thing.
707 FACE is the face to use."
708 (insert prefix prebuttontext
)
710 (let ((start (point))
715 (put-text-property start end
'face
'font-lock-variable-name-face
)
719 (defun data-debug-insert-simple-thing (thing prefix prebuttontext face
)
720 "Insert one simple THING with a face.
721 PREFIX is the text that precedes the button.
722 PREBUTTONTEXT is some text between prefix and the thing.
723 FACE is the face to use."
724 (insert prefix prebuttontext
)
725 (let ((start (point))
727 (insert (format "%s" thing
))
730 (put-text-property start end
'face face
)
734 (defun data-debug-insert-custom (thingstring prefix prebuttontext face
)
735 "Insert one simple THINGSTRING with a face.
736 Use for simple items that need a custom insert.
737 PREFIX is the text that precedes the button.
738 PREBUTTONTEXT is some text between prefix and the thing.
739 FACE is the face to use."
740 (insert prefix prebuttontext
)
741 (let ((start (point))
746 (put-text-property start end
'face face
)
750 (defvar data-debug-thing-alist
753 (null . data-debug-insert-nil
)
756 (data-debug-overlay-p . data-debug-insert-overlay-button
)
759 ((lambda (thing) (and (consp thing
) (data-debug-overlay-p (car thing
)))) .
760 data-debug-insert-overlay-list-button
)
763 (bufferp . data-debug-insert-buffer-button
)
766 ((lambda (thing) (and (consp thing
) (bufferp (car thing
)))) .
767 data-debug-insert-buffer-list-button
)
770 (processp . data-debug-insert-process-button
)
773 (stringp . data-debug-insert-string
)
776 (numberp . data-debug-insert-number
)
779 (symbolp . data-debug-insert-symbol-button
)
782 (ring-p . data-debug-insert-ring-button
)
785 ((lambda (thing) (and (consp thing
) (eq (car thing
) 'lambda
))) .
786 data-debug-insert-lambda-expression
)
789 (hash-table-p . data-debug-insert-hash-table-button
)
792 (widgetp . data-debug-insert-widget
)
795 (listp . data-debug-insert-stuff-list-button
)
798 (vectorp . data-debug-insert-stuff-vector-button
)
800 "Alist of methods used to insert things into an Ddebug buffer.")
802 ;; An augmentation function for the thing alist.
803 (defun data-debug-add-specialized-thing (predicate fcn
)
804 "Add a new specialized thing to display with data-debug.
805 PREDICATE is a function that returns t if a thing is this new type.
806 FCN is a function that will display stuff in the data debug buffer."
807 (let ((entry (cons predicate fcn
))
808 ;; Specialized entries show up AFTER nil,
809 ;; but before listp, vectorp, symbolp, and
810 ;; other general things. Splice it into
812 (first (nthcdr 0 data-debug-thing-alist
))
813 (second (nthcdr 1 data-debug-thing-alist
))
815 (when (not (member entry data-debug-thing-alist
))
816 (setcdr first
(cons entry second
)))))
818 ;; uber insert method
819 (defun data-debug-insert-thing (thing prefix prebuttontext
&optional parent
)
820 "Insert THING with PREFIX.
821 PREBUTTONTEXT is some text to insert between prefix and the thing
822 that is not included in the indentation calculation of any children.
823 If PARENT is non-nil, it is somehow related as a parent to thing."
824 (let ((inhibit-read-only t
))
826 (dolist (test data-debug-thing-alist
)
827 (when (funcall (car test
) thing
)
830 (funcall (cdr test
) thing prefix prebuttontext parent
)
835 (funcall (cdr test
) thing prefix prebuttontext
)
838 ;; Only throw the 'done if no error was caught.
839 ;; If an error was caught, skip this predicate as being
840 ;; unsuccessful, and move on.
843 (data-debug-insert-simple-thing (format "%S" thing
)
847 (set-buffer-modified-p nil
))
851 ;; The Ddebug major mode provides an interactive space to explore
852 ;; complicated data structures.
854 (defgroup data-debug nil
858 (defvar data-debug-mode-syntax-table
859 (let ((table (make-syntax-table (standard-syntax-table))))
860 (modify-syntax-entry ?\
; ". 12" table) ;; SEMI, Comment start ;;
861 (modify-syntax-entry ?
\n ">" table
) ;; Comment end
862 (modify-syntax-entry ?
\" "\"" table
) ;; String
863 (modify-syntax-entry ?\-
"_" table
) ;; Symbol
864 (modify-syntax-entry ?
\\ "\\" table
) ;; Quote
865 (modify-syntax-entry ?\
` "'" table
) ;; Prefix ` (backquote)
866 (modify-syntax-entry ?
\' "'" table
) ;; Prefix ' (quote)
867 (modify-syntax-entry ?\
, "'" table
) ;; Prefix , (comma)
870 "Syntax table used in data-debug macro buffers.")
872 (define-obsolete-variable-alias 'data-debug-map
'data-debug-mode-map
"24.1")
873 (defvar data-debug-mode-map
874 (let ((km (make-sparse-keymap)))
876 (define-key km
[mouse-2
] 'data-debug-expand-or-contract-mouse
)
877 (define-key km
" " 'data-debug-expand-or-contract
)
878 (define-key km
"\C-m" 'data-debug-expand-or-contract
)
879 (define-key km
"n" 'data-debug-next
)
880 (define-key km
"p" 'data-debug-prev
)
881 (define-key km
"N" 'data-debug-next-expando
)
882 (define-key km
"P" 'data-debug-prev-expando
)
884 "Keymap used in data-debug.")
886 (defcustom data-debug-mode-hook nil
887 "Hook run when data-debug starts."
891 (define-derived-mode data-debug-mode fundamental-mode
"DATA-DEBUG"
892 "Major-mode for the Analyzer debugger.
894 \\{data-debug-mode-map}"
895 (setq comment-start
";;"
898 (setq-local comment-start-skip
899 "\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\);+ *")
900 (buffer-disable-undo)
901 (set (make-local-variable 'font-lock-global-modes
) nil
)
906 (defun data-debug-new-buffer (name)
907 "Create a new data-debug buffer with NAME."
908 (let ((b (get-buffer-create name
)))
911 (setq buffer-read-only nil
) ; disable read-only
916 ;;; Ddebug mode commands
918 (defun data-debug-next ()
919 "Go to the next line in the Ddebug buffer."
923 (skip-chars-forward " *-><[]" (point-at-eol)))
925 (defun data-debug-prev ()
926 "Go to the previous line in the Ddebug buffer."
930 (skip-chars-forward " *-><[]" (point-at-eol)))
932 (defun data-debug-next-expando ()
933 "Go to the next line in the Ddebug buffer.
934 Contract the current line (if open) and expand the line
937 (data-debug-contract-current-line)
939 (data-debug-expand-current-line)
942 (defun data-debug-prev-expando ()
943 "Go to the previous line in the Ddebug buffer.
944 Contract the current line (if open) and expand the line
947 (data-debug-contract-current-line)
949 (data-debug-expand-current-line)
952 (defun data-debug-current-line-expanded-p ()
953 "Return non-nil if the current line is expanded."
954 (let ((ti (current-indentation))
955 (ni (condition-case nil
959 (current-indentation))
963 (defun data-debug-line-expandable-p ()
964 "Return non-nil if the current line is expandable.
965 Lines that are not expandable are assumed to not be contractible."
966 (not (get-text-property (point) 'ddebug-noexpand
)))
968 (defun data-debug-expand-current-line ()
969 "Expand the current line (if possible).
970 Do nothing if already expanded."
971 (when (or (not (data-debug-line-expandable-p))
972 (not (data-debug-current-line-expanded-p)))
973 ;; If the next line is the same or less indentation, expand.
974 (let ((fcn (get-text-property (point) 'ddebug-function
))
975 (inhibit-read-only t
))
977 (funcall fcn
(point))
981 (defun data-debug-contract-current-line ()
982 "Contract the current line (if possible).
983 Do nothing if already contracted."
984 (when (and (data-debug-current-line-expanded-p)
985 ;; Don't contract if the current line is not expandable.
986 (get-text-property (point) 'ddebug-function
))
987 (let ((ti (current-indentation))
988 (inhibit-read-only t
)
990 ;; If next indentation is larger, collapse.
993 (let ((start (point))
997 ;; Keep checking indentation
998 (while (or (> (current-indentation) ti
)
999 (looking-at "^\\s-*$"))
1004 (error (setq end
(point-max))))
1005 (delete-region start end
)
1007 (beginning-of-line))))
1008 (set-buffer-modified-p nil
))
1010 (defun data-debug-expand-or-contract ()
1011 "Expand or contract anything at the current point."
1013 (if (and (data-debug-line-expandable-p)
1014 (data-debug-current-line-expanded-p))
1015 (data-debug-contract-current-line)
1016 (data-debug-expand-current-line))
1017 (skip-chars-forward " *-><[]" (point-at-eol)))
1019 (defun data-debug-expand-or-contract-mouse (event)
1020 "Expand or contract anything at event EVENT."
1022 (let* ((win (car (car (cdr event
))))
1024 (select-window win t
)
1026 ;(goto-char (window-start win))
1027 (mouse-set-point event
)
1028 (data-debug-expand-or-contract))
1031 ;;; GENERIC STRUCTURE DUMP
1033 (defun data-debug-show-stuff (stuff name
)
1034 "Data debug STUFF in a buffer named *NAME DDebug*."
1035 (data-debug-new-buffer (concat "*" name
" DDebug*"))
1036 (data-debug-insert-thing stuff
"?" "")
1037 (goto-char (point-min))
1038 (when (data-debug-line-expandable-p)
1039 (data-debug-expand-current-line)))
1043 ;; Various commands for displaying complex data structures.
1045 (defun data-debug-edebug-expr (expr)
1046 "Dump out the contents of some expression EXPR in edebug with ddebug."
1048 (list (let ((minibuffer-completing-symbol t
))
1049 (read-from-minibuffer "Eval: "
1050 nil read-expression-map t
1051 'read-expression-history
))
1053 (let ((v (eval expr
)))
1055 (message "Expression %s is nil." expr
)
1056 (data-debug-show-stuff v
"expression"))))
1058 (defun data-debug-eval-expression (expr)
1059 "Evaluate EXPR and display the value.
1060 If the result is something simple, show it in the echo area.
1061 If the result is a list or vector, then use the data debugger to display it."
1063 (list (let ((minibuffer-completing-symbol t
))
1064 (read-from-minibuffer "Eval: "
1065 nil read-expression-map t
1066 'read-expression-history
))
1069 (if (null eval-expression-debug-on-error
)
1070 (setq values
(cons (eval expr
) values
))
1071 (let ((old-value (make-symbol "t")) new-value
)
1072 ;; Bind debug-on-error to something unique so that we can
1073 ;; detect when evalled code changes it.
1074 (let ((debug-on-error old-value
))
1075 (setq values
(cons (eval expr
) values
))
1076 (setq new-value debug-on-error
))
1077 ;; If evalled code has changed the value of debug-on-error,
1078 ;; propagate that change to the global binding.
1079 (unless (eq old-value new-value
)
1080 (setq debug-on-error new-value
))))
1082 (if (or (consp (car values
)) (vectorp (car values
)))
1083 (let ((v (car values
)))
1084 (data-debug-show-stuff v
"Expression"))
1087 (prin1 (car values
) t
)
1088 (let ((str (eval-expression-print-format (car values
))))
1089 (if str
(princ str t
))))))
1091 (provide 'data-debug
)
1093 ;;; data-debug.el ends here