-
Notifications
You must be signed in to change notification settings - Fork 9
/
inspector.el
1054 lines (945 loc) · 39.1 KB
/
inspector.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
;;; inspector.el --- Tool for inspection of Emacs Lisp objects -*- lexical-binding: t -*-
;; Copyright (C) 2021-2023 Free Software Foundation, Inc.
;; Author: Mariano Montone <[email protected]>
;; URL: https://github.com/mmontone/emacs-inspector
;; Keywords: debugging, tool, lisp, development
;; Version: 0.38
;; Package-Requires: ((emacs "27.1"))
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Tool for inspection of Emacs Lisp objects.
;;
;; Usage:
;;
;; M-x `inspector-inspect-expression' to evaluate an elisp expression and inspect the result.
;; M-x `inspector-inspect-last-sexp' to evaluate last sexp in current buffer and inspect the result.
;;
;; Inside the inspector:
;;
;; M-x `inspector-pop' bound to letter l, to navigate to previous object.
;; M-x `inspector-quit' bound to letter q, to exit the inspector.
;;
;; Also, M-x `forward-button' and M-x `backward-button' are conveniently bound to n and p.
;; They can be used for fast navigation across the buttons that the inspector displays.
;;
;; Finally, you can use M-x `eval-expression' bound to letter e,
;; to evaluate an elisp expression using the object currently being inspected (it is bound to *).
;;
;; From the Emacs debugger:
;;
;; When on an Emacs debugging backtrace, press letter i to inspect the pointed frame and its local variables.
;;
;; When on edebug-mode, use C-c C-i for inspecting expressions in the debugger.
;;; Code:
(require 'eieio)
(require 'debug)
(require 'edebug)
(require 'backtrace)
(require 'pp)
;;---- Utils ----------
(defun inspector--princ-to-string (object)
"Print OBJECT to string using `princ'."
(with-output-to-string
(let ((print-circle t))
(princ object))))
(defun inspector--plistp (list)
"Return T if LIST is a property list."
(let ((expected t))
(and (inspector--proper-list-p list)
(cl-evenp (length list))
(cl-every (lambda (x)
(setq expected (if (eql expected t) 'symbol t))
(cl-typep x expected))
list))))
(defun inspector--alistp (list)
"Return T if LIST is an association list."
(and (inspector--proper-list-p list)
(cl-every (lambda (x) (consp x)) list)))
(defun inspector--alist-to-plist (alist)
"Convert association list ALIST to a property list."
(let ((plist))
(dolist (cons (reverse alist))
(push (cdr cons) plist)
(push (car cons) plist))
plist))
(defun inspector--proper-list-p (val)
"Is VAL a proper list?"
(if (fboundp 'format-proper-list-p)
;; Emacs stable.
(with-no-warnings (format-proper-list-p val))
;; Function was renamed in Emacs master:
;; http://git.savannah.gnu.org/cgit/emacs.git/commit/?id=2fde6275b69fd113e78243790bf112bbdd2fe2bf
(with-no-warnings (proper-list-p val))))
;;--- Customization ----------------------------
(defgroup inspector nil
"Emacs Lisp inspector customizations."
:group 'lisp)
(defgroup inspector-faces nil
"Emacs Lisp inspector faces."
:group 'faces)
(defface inspector-button-face
'((t :inherit link))
"Face for inspector buttons."
:group 'inspector-faces)
(defface inspector-title-face
'((t ()))
"Face for title describing object."
:group 'inspector-faces)
(defface inspector-label-face
'((t (:inherit font-lock-constant-face)))
"Face for labels in the inspector."
:group 'inspector-faces)
(defface inspector-value-face
'((t (:inherit font-lock-builtin-face)))
"Face for things which can themselves be inspected."
:group 'inspector-faces)
(defface inspector-action-face
'((t (:inherit font-lock-warning-face)))
"Face for labels of inspector actions."
:group 'inspector-faces)
(defface inspector-type-face
'((t (:inherit font-lock-type-face)))
"Face for type description in inspector."
:group 'inspector-faces)
(defcustom inspector-truncation-limit 500
"Control truncation limit in inspector."
:type 'integer
:group 'inspector)
(defcustom inspector-show-documentation t
"Whether to show variables and function documentation or not."
:type 'boolean
:group 'inspector)
(defcustom inspector-use-specialized-inspectors-for-lists t
"Whether to use specialized inspectors for plists and alists."
:type 'boolean
:group 'inspector)
(defcustom inspector-use-font-lock-faces t
"Use font-lock faces in inspector, instead of button faces."
:type 'boolean
:group 'inspector)
(defcustom inspector-slice-size 100
"Size of sequence slices in inspector."
:type 'integer
:group 'inspector)
(defcustom inspector-plist-test-function 'inspector--plistp
"Function used by the inspector to determine if a list is a property list."
:type 'symbol
:group 'inspector)
(defcustom inspector-alist-test-function 'inspector--alistp
"Function used by the inspector to determine if a list is an association list."
:type 'symbol
:group 'inspector)
(defcustom inspector-pp-max-width
(if (boundp 'pp-max-width)
(symbol-value 'pp-max-width)
"window width")
"Max width to use when inspector pretty printing of objects.
If nil, there's no max width. If t, use the window width.
Otherwise this should be a number.
See `pp-max-width'"
:type '(choice (const :tag "none" nil)
(const :tag "window width" t)
number)
:group 'inspector)
(defcustom inspector-pp-use-max-width
(if (boundp 'pp-use-max-width)
(symbol-value 'pp-use-max-width)
nil)
"If non-nil, `pp'-related functions will try to fold lines.
The target width is given by the `pp-max-width' variable."
:type 'boolean
:group 'inspector)
(defcustom inspector-switch-to-buffer t
"Use `switch-to-buffer-other-window' after an inspector buffer is opened."
:type 'boolean
:group 'inspector)
(define-button-type 'inspector-button
'follow-link t
'face 'inspector-button-face
'help-echo "Inspect object")
;;-------- Inspector code -------------------
(defvar-local inspector-history nil
"The inspector buffer history.")
(defvar-local inspector-inspected-object nil
"The current inspected object.")
(defun inspector--insert-horizontal-line (&rest width)
"Insert an horizontal line with width WIDTH."
(ignore width)
(insert (propertize " "
'display '(space :align-to right-fringe)
'face '(:strike-through t))))
(defun inspector--insert-label (label)
"Show a LABEL in inspector buffer."
(insert (propertize label 'face 'inspector-label-face))
(insert ": "))
(defun inspector--insert-value (value)
"Show a property VALUE in inspector buffer."
(insert (propertize (inspector--princ-to-string value) 'face 'inspector-value-face)))
(defun inspector--insert-title (title)
"Insert TITLE for inspector."
(insert (propertize title 'face 'inspector-title-face))
(newline)
(inspector--insert-horizontal-line)
(newline))
(defun inspector--prin1 (thing &optional stream)
"Print THING to STREAM."
(if (stringp thing)
(cl-print-object (substring-no-properties thing) stream)
(cl-print-object thing stream)))
(defvar inspector--temp-buffer nil
"Variable with the current temporal buffer.
Used as an optimization for printing objects.
See: `inspector--with-inspector-temp-buffer'.")
(defun inspector--get-inspector-temp-buffer ()
"Return a (cached) inspector temporary buffer."
(unless (buffer-live-p inspector--temp-buffer)
(setq inspector--temp-buffer
(get-buffer-create "*inspector-temp*")))
inspector--temp-buffer)
(defun inspector--print-truncated (object &optional limit)
"Print OBJECT to a string, truncated.
LIMIT controls the truncation."
(setq limit (or limit inspector-truncation-limit))
(let ((temp-buffer (inspector--get-inspector-temp-buffer)))
(with-current-buffer temp-buffer
(erase-buffer)
(insert (cl-print-to-string-with-limit #'inspector--prin1 object limit))
;; Add a unique inspector-form property.
(put-text-property (point-min) (point) 'inspector-form (gensym))
;; Make buttons from all the "..."s. Since there might be many of
;; them, use text property buttons.
(unless (boundp 'cl-print-expand-ellipsis-function) ;Emacs-30
(goto-char (point-min))
(while (< (point) (point-max))
(let ((end (next-single-property-change (point) 'cl-print-ellipsis
nil (point-max))))
(when (get-text-property (point) 'cl-print-ellipsis)
(make-text-button (point) end :type 'backtrace-ellipsis))
(goto-char end))))
(buffer-string))))
(cl-defgeneric inspector--face-for-object (object)
"Return face to use for OBJECT.")
(cl-defmethod inspector--face-for-object (_object)
"Use builtin face by default for non matching OBJECTs."
'inspector-value-face)
(cl-defmethod inspector--face-for-object ((_ string))
"Inspector face for STRING."
font-lock-string-face)
(cl-defmethod inspector--face-for-object ((symbol symbol))
"Inspector face for SYMBOLs."
(if (keywordp symbol)
font-lock-builtin-face
font-lock-variable-name-face))
(cl-defmethod inspector--face-for-object ((_ integer))
"Inspector face for INTEGERs."
nil)
(defun inspector--insert-inspect-button (object &optional label)
"Insert button for inspecting OBJECT.
If LABEL has a value, then it is used as button label.
Otherwise, button label is the printed representation of OBJECT."
(insert-button (or (and label (inspector--princ-to-string label))
(inspector--print-truncated object))
:type 'inspector-button
'face (if inspector-use-font-lock-faces
(inspector--face-for-object object)
'inspector-value-face)
'action (lambda (_btn)
(inspector-inspect object t))
'follow-link t))
(defun inspector--do-with-slicer (slicer function)
"Use SLICER and call FUNCTION on the resulting slice.
SLICE should be a function that returns a slice of some data.
FUNCTION is passed the resulting slice and a continuation function that when
called continues the consumption of slices of data, until there are no more
slices (the returned slice is nil)."
(let ((slice (funcall slicer)))
(when slice
(funcall function slice
(lambda () (inspector--do-with-slicer slicer function))))))
(defun inspector--do-with-slicer-and-more-button (slicer function)
"Apply the SLICER function and apply FUNCTION to the resulting slice.
When FUNCTION returns non-nil, adds a [More] button that inserts the next
slice in buffer."
(inspector--do-with-slicer
slicer
(lambda (slice cont)
(let ((more-p (funcall function slice cont)))
(when more-p
(insert-button "[more]"
'action (let ((pos (point)))
(lambda (_btn)
(setq buffer-read-only nil)
(goto-char pos)
(delete-char (length "[More]"))
(funcall cont)
(setq buffer-read-only nil)))
'follow-link t))))))
;;--------- Object inspectors ----------------------------------
(cl-defgeneric inspector-inspect-object (object)
"Render inspector buffer for OBJECT.
Methods of this generic function are expected to specialize on the type of
Emacs Lisp OBJECT being inspected, and write into the `current-buffer'.
The `current-buffer' is presumed to be empty.
An inspector buffer is expected to have a title, some properties and a body.
See `inspector--insert-title', `inspector--insert-label'
and `inspector--insert-value' for inserting title,and properties and its values.
For linking to another object, `inspector--insert-inspect-button'
is expected to be used.")
(cl-defmethod inspector-inspect-object ((class (subclass eieio-default-superclass)))
"Render inspector buffer for EIEIO CLASS."
(inspector--insert-title (format "%s class" (eieio-class-name class)))
(insert "direct superclasses: ")
(dolist (superclass (eieio-class-parents class))
(inspector--insert-inspect-button
(eieio-class-name superclass) (eieio-class-name superclass))
(insert " "))
(newline)
(insert "class slots: ")
(dolist (slot (eieio-class-slots class))
(insert (format "%s " (cl--slot-descriptor-name slot))))
(newline)
(insert "direct subclasses:")
(dolist (subclass (eieio-class-children class))
(inspector--insert-inspect-button
(eieio-class-name subclass) (eieio-class-name subclass))
(insert " ")))
(cl-defmethod inspector-inspect-object ((_object (eql t)))
"Render inspector buffer for boolean T."
(inspector--insert-title "boolean")
(insert "value: t"))
(cl-defmethod inspector-inspect-object ((_object (eql nil)))
"Render inspector buffer for nil object."
(inspector--insert-title "nil")
(insert "value: nil"))
(cl-defmethod inspector-inspect-object ((symbol symbol))
"Render inspector buffer for SYMBOL."
(insert (propertize "symbol" 'face 'inspector-title-face))
(insert " ")
(insert-button "[find definitions]"
'action (lambda (_btn)
(xref-find-definitions (symbol-name symbol)))
'follow-link t)
(insert " ")
(insert-button "[describe]"
'action (lambda (_btn)
(describe-symbol symbol))
'follow-link t)
(newline)
(inspector--insert-horizontal-line)
(newline)
(inspector--insert-label "name")
(inspector--insert-value (symbol-name symbol))
(newline)
(inspector--insert-label "is bound")
(inspector--insert-value (format "%s" (boundp symbol)))
(newline)
(inspector--insert-label "function")
(inspector--insert-inspect-button (symbol-function symbol))
(newline)
(inspector--insert-label "property list")
(inspector--insert-inspect-button (symbol-plist symbol))
(newline))
(cl-defmethod inspector-inspect-object ((object t))
"Render inspector buffer for OBJECT."
(cond
;; FIXME: Why use this `cond' instead of using separate methods?
((eieio-object-p object)
(insert "instance of ")
(inspector--insert-inspect-button
(eieio-object-class object)
(eieio-class-name (eieio-object-class object)))
(newline)
(inspector--insert-horizontal-line)
(newline)
(inspector--insert-label "slot values")
(newline)
(dolist (slot (eieio-class-slots (eieio-object-class object)))
(insert (format "%s: " (cl--slot-descriptor-name slot)))
(if (not (slot-boundp object (cl--slot-descriptor-name slot)))
(insert "unbound")
(inspector--insert-inspect-button
(slot-value object (cl--slot-descriptor-name slot))))
(newline)))
((cl-struct-p object)
(inspector--insert-title (format "%s struct" (type-of object)))
(inspector--insert-label "slot values")
(newline)
(dolist (slot (cdr (cl-struct-slot-info (type-of object))))
(insert (format "%s: " (car slot)))
(inspector--insert-inspect-button
(cl-struct-slot-value (type-of object) (car slot) object))
(newline)))
((recordp object)
(inspector--insert-title (format "%s record" (type-of object)))
(inspector--insert-label "members")
(newline)
(cl-do ((i 1 (cl-incf i)))
((= i (length object)))
(inspector--insert-inspect-button
(aref object i))
(newline)))
((functionp object)
(if (subrp object)
(inspector--insert-title "builtin function")
(inspector--insert-title "function"))
(inspector--insert-value (inspector--princ-to-string object))
(newline)
(inspector--insert-label "arglist")
(inspector--insert-value (elisp-get-fnsym-args-string object)))
((eq (type-of object) 'finalizer)
(inspector--insert-title "finalizer")
(inspector--insert-value (inspector--princ-to-string object)))
((user-ptrp object)
(inspector--insert-title "user-ptr")
(inspector--insert-value (inspector--princ-to-string object)))
(t ;; Just print the object
(inspector--insert-title (inspector--princ-to-string (type-of object)))
(inspector--insert-value (inspector--princ-to-string object)))))
(cl-defmethod inspector-inspect-object ((process process))
"Inspect a PROCESS."
(inspector--insert-title "process")
(inspector--insert-value (inspector--princ-to-string process))
(newline 2)
(inspector--insert-label "PID")
(inspector--insert-value (inspector--princ-to-string (process-id process)))
(newline)
(inspector--insert-label "Status")
(inspector--insert-value (inspector--princ-to-string (process-status process)))
(newline)
(inspector--insert-label "TTY name")
(inspector--insert-value (inspector--princ-to-string (process-tty-name process)))
(newline)
(inspector--insert-label "Contact")
(inspector--insert-value (inspector--princ-to-string (process-contact process)))
(newline)
(inspector--insert-label "Properties")
(newline)
(let ((plist (cl-copy-list (process-plist process))))
(while plist
(let ((key (pop plist)))
(inspector--insert-inspect-button key))
(insert ": ")
(let ((value (pop plist)))
(inspector--insert-inspect-button value))
(newline))))
(cl-defmethod inspector-inspect-object ((cons cons))
"Inspect a CONS object."
(cond
((and inspector-use-specialized-inspectors-for-lists
(funcall inspector-plist-test-function cons))
(insert (propertize "property list" 'face 'inspector-title-face))
(insert " ")
(insert-button "[as proper list]"
'action (lambda (_btn)
(let ((inspector-use-specialized-inspectors-for-lists nil))
(setf buffer-read-only nil
(buffer-string) "")
(inspector-inspect-object cons)
(setq buffer-read-only t)
(goto-char 0)))
'follow-link t)
(newline)
(inspector--insert-horizontal-line)
(newline)
(inspector--insert-label "length")
(insert (inspector--princ-to-string (length cons)))
(newline 2)
(let ((plist (cl-copy-list cons)))
(while plist
(let ((key (pop plist)))
(inspector--insert-inspect-button key))
(insert ": ")
(let ((value (pop plist)))
(inspector--insert-inspect-button value))
(newline))))
((and inspector-use-specialized-inspectors-for-lists
(funcall inspector-alist-test-function cons))
(insert (propertize "association list" 'face 'inspector-title-face))
(insert " ")
(insert-button "[as proper list]"
'action (lambda (_btn)
(let ((inspector-use-specialized-inspectors-for-lists nil))
(setf buffer-read-only nil
(buffer-string) "")
(inspector-inspect-object cons)
(setq buffer-read-only t)
(goto-char 0)))
'follow-link t)
(newline)
(inspector--insert-horizontal-line)
(newline)
(inspector--insert-label "length")
(insert (inspector--princ-to-string (length cons)))
(newline 2)
(let ((i 0))
(inspector--do-with-slicer-and-more-button
(lambda ()
(when (< i (length cons))
(cl-subseq cons i (min (cl-incf i inspector-slice-size)
(length cons)))))
(lambda (slice _cont)
(dolist (cons slice)
(insert "(")
(inspector--insert-inspect-button (car cons))
(insert " . ")
(inspector--insert-inspect-button (cdr cons))
(insert ")")
(newline))
;; A [more] button is inserted or not depending on the boolean returned here:
(< i (length cons))))))
((inspector--proper-list-p cons)
(and inspector-use-specialized-inspectors-for-lists
(funcall inspector-plist-test-function cons))
(insert (propertize "proper list" 'face 'inspector-title-face))
(insert " ")
(when (funcall inspector-alist-test-function cons)
(insert-button "[as alist]"
'action (lambda (_btn)
(let ((inspector-use-specialized-inspectors-for-lists t))
(setf buffer-read-only nil
(buffer-string) "")
(inspector-inspect-object cons)
(setq buffer-read-only t)
(goto-char 0)))
'follow-link t))
(when (funcall inspector-plist-test-function cons)
(insert-button "[as plist]"
'action (lambda (_btn)
(let ((inspector-use-specialized-inspectors-for-lists t))
(setf buffer-read-only nil
(buffer-string) "")
(inspector-inspect-object cons)
(setq buffer-read-only t)
(goto-char 0)))
'follow-link t))
(newline)
(inspector--insert-horizontal-line)
(newline)
(inspector--insert-label "length")
(insert (inspector--princ-to-string (length cons)))
(newline 2)
(let ((i 0)
(j 0))
(inspector--do-with-slicer-and-more-button
(lambda ()
(when (< i (length cons))
(cl-subseq cons i (min (cl-incf i inspector-slice-size)
(length cons)))))
(lambda (slice _cont)
(dolist (elem slice)
(insert (format "%d: " j))
(cl-incf j)
(inspector--insert-inspect-button elem)
(newline))
;; A [more] button is inserted or not depending on the boolean returned here:
(< i (length cons))))))
(t ;; It is a cons cell
(inspector--insert-title "cons cell")
(inspector--insert-label "car")
(inspector--insert-inspect-button (car cons))
(newline)
(inspector--insert-label "cdr")
(inspector--insert-inspect-button (cdr cons)))))
;; NOTE: this is code extracted from https://git.savannah.gnu.org/cgit/emacs/org-mode.git/tree/lisp/org-fold-core.el#n1450
(defun inspector--object-intervals (string)
(if (fboundp 'object-intervals)
(object-intervals string)
;; Backward compatibility with Emacs <28.
;; FIXME: Is there any better way to do it?
;; Yes, it is a hack.
;; The below gives us string representation as a list.
;; Note that we need to remove unreadable values, like markers (#<...>).
(seq-partition
(cdr (let ((data (read (replace-regexp-in-string
"^#(" "("
(replace-regexp-in-string
" #(" " ("
(replace-regexp-in-string
"#<[^>]+>" "dummy"
;; Get text representation of the string object.
;; Make sure to print everything (see `prin1' docstring).
;; `prin1' is used to print "%S" format.
(let (print-level print-length)
(format "%S" string))))))))
(if (listp data) data (list data))))
3)))
(cl-defmethod inspector-inspect-object ((string string))
"Render inspector buffer for STRING."
(inspector--insert-title "string")
(insert (propertize (cl-prin1-to-string (substring-no-properties string))
'face 'font-lock-string-face))
(let ((text-properties (inspector--object-intervals string)))
(when text-properties
(newline 2)
(inspector--insert-label "Text properties")
(newline)
(dolist (interval-props text-properties)
(cl-destructuring-bind (from to props) interval-props
(insert (format " [%d-%d]: " from to))
(inspector--insert-inspect-button props)
(newline))))))
(cl-defmethod inspector-inspect-object ((array array))
"Render inspector buffer for ARRAY."
(inspector--insert-title (inspector--princ-to-string (type-of array)))
(let ((length (length array)))
(inspector--insert-label "length")
(insert (inspector--princ-to-string length))
(newline 2)
(let ((i 0))
(inspector--do-with-slicer-and-more-button
(lambda ()
(when (< i length)
(cons i (1- (min (cl-incf i inspector-slice-size)
length)))))
(lambda (slice _cont)
(cl-loop for k from (car slice) to (cdr slice)
do
(insert (format "%d: " k))
(inspector--insert-inspect-button (aref array k))
(newline))
;; Insert [more] button?:
(< i length))))))
(cl-defmethod inspector-inspect-object ((buffer buffer))
"Render inspector buffer for Emacs BUFFER."
(inspector--insert-title (prin1-to-string buffer))
(inspector--insert-label "name")
(inspector--insert-inspect-button (buffer-name buffer))
(newline)
(inspector--insert-label "window")
(inspector--insert-inspect-button (get-buffer-window buffer))
(newline)
(let ((buffer-string (with-current-buffer buffer
(buffer-string)))
(cursor-position (with-current-buffer buffer
(what-cursor-position))))
(inspector--insert-label "contents")
(inspector--insert-inspect-button buffer-string)
(newline)
(inspector--insert-label "cursor position")
(inspector--insert-inspect-button cursor-position)
(newline)
(inspector--insert-label "buffer file")
(inspector--insert-inspect-button (buffer-file-name buffer))
(newline)
(inspector--insert-label "readonly")
(inspector--insert-inspect-button (with-current-buffer buffer
buffer-read-only))))
(cl-defmethod inspector-inspect-object ((window window))
"Render inspector buffer for Emacs WINDOW."
(inspector--insert-title (prin1-to-string window))
(inspector--insert-label "parent")
(inspector--insert-inspect-button (window-parent window))
(newline)
(inspector--insert-label "buffer")
(inspector--insert-inspect-button (window-buffer window))
(newline)
(inspector--insert-label "parameters")
(inspector--insert-inspect-button (window-parameters window))
(newline)
(inspector--insert-label "frame")
(inspector--insert-inspect-button (window-frame window)))
(cl-defmethod inspector-inspect-object ((frame frame))
"Render inspector buffer for Emacs FRAME."
(inspector--insert-title (prin1-to-string frame))
(inspector--insert-label "first window")
(inspector--insert-inspect-button (frame-first-window frame))
(newline)
(inspector--insert-label "parameters")
(inspector--insert-inspect-button (frame-parameters frame)))
(cl-defmethod inspector-inspect-object ((overlay overlay))
"Render inspector buffer for Emacs OVERLAY."
(inspector--insert-title (prin1-to-string overlay))
(inspector--insert-label "buffer")
(inspector--insert-inspect-button (overlay-buffer overlay))
(newline)
(inspector--insert-label "start")
(inspector--insert-inspect-button (overlay-start overlay))
(newline)
(inspector--insert-label "end")
(inspector--insert-inspect-button (overlay-end overlay))
(newline)
(inspector--insert-label "properties")
(inspector--insert-inspect-button (overlay-properties overlay)))
(cl-defmethod inspector-inspect-object ((number number))
"Render inspector buffer for NUMBER."
(inspector--insert-title (inspector--princ-to-string (type-of number)))
(inspector--insert-label "value")
(insert (inspector--princ-to-string number)))
(cl-defmethod inspector-inspect-object ((integer integer))
"Render inspector buffer for INTEGER."
(inspector--insert-title (inspector--princ-to-string (type-of integer)))
(inspector--insert-label "integer")
(insert (inspector--princ-to-string integer))
(when (characterp integer)
(newline)
(inspector--insert-label "character")
(insert (inspector--princ-to-string (char-to-string integer)))))
(cl-defmethod inspector-inspect-object ((hash-table hash-table))
"Render inspector buffer for HASH-TABLEs."
(inspector--insert-title (inspector--print-truncated hash-table))
(inspector--insert-label "size")
(insert (inspector--princ-to-string (hash-table-size hash-table)))
(newline)
(inspector--insert-label "count")
(insert (inspector--princ-to-string (hash-table-count hash-table)))
(newline 2)
(if (zerop (hash-table-count hash-table))
(insert "The hash table is empty.")
(progn
(inspector--insert-label "values")
(newline)
(let ((i 0)
(keys (hash-table-keys hash-table)))
(inspector--do-with-slicer-and-more-button
(lambda ()
(when (< i (length keys))
(cl-subseq keys i (min (cl-incf i inspector-slice-size)
(length keys)))))
(lambda (slice _cont)
(dolist (key slice)
(inspector--insert-inspect-button key)
(insert " : ")
(inspector--insert-inspect-button (gethash key hash-table))
(newline))
;; Insert [more] button?
(< i (length keys))))))))
;;--- Buffers ------------------------------
(defun inspector-make-inspector-buffer ()
"Create an inspector buffer."
(let ((buffer (or (get-buffer "*inspector*")
(let ((buf (get-buffer-create "*inspector*")))
(with-current-buffer buf
(inspector-mode)
(make-local-variable '*))
buf))))
(with-current-buffer buffer
(add-hook 'xref-backend-functions 'elisp--xref-backend 0 'local)
(setq-local revert-buffer-function #'inspector--revert-buffer)
(setq buffer-read-only nil)
(erase-buffer))
buffer))
;;------ Commands -----------------------------
;;;###autoload
(defun inspector-inspect-expression (exp)
"Evaluate EXP and inspect its result."
(interactive (list (read--expression "Eval and inspect: ")))
(inspector-inspect (eval exp t)))
(defun inspector--basic-inspect (object)
"Create and prepare a new buffer for inspecting OBJECT."
(defvar *)
(let ((buffer (inspector-make-inspector-buffer)))
(with-current-buffer buffer
(setq inspector-inspected-object object)
(setq * object)
(inspector-inspect-object object)
(goto-char 0)
(setq buffer-read-only t)
buffer)))
;;;###autoload
(defun inspector-inspect (object &optional preserve-history)
"Top-level function for inspecting OBJECTs.
When PRESERVE-HISTORY is T, inspector history is not cleared."
(let ((current-inspected-object inspector-inspected-object)
(buffer (inspector--basic-inspect object)))
(when (not preserve-history)
(if inspector-switch-to-buffer
(switch-to-buffer-other-window buffer)
(display-buffer buffer)))
(with-current-buffer buffer
(unless preserve-history
(setq inspector-history nil))
(when preserve-history
(push current-inspected-object inspector-history)))))
(defun inspector-refresh ()
"Refresh inspector buffer."
(interactive)
(let ((buffer (get-buffer "*inspector*")))
(when buffer
(with-current-buffer buffer
(setq buffer-read-only nil)
(erase-buffer)
(inspector--basic-inspect inspector-inspected-object)))))
(defun inspector--revert-buffer (&rest _ignore)
"Function bound to `revert-buffer-function'."
(inspector-refresh))
(defun inspector-quit ()
"Quit the Emacs inspector."
(interactive)
(setq inspector-history nil)
(when inspector--temp-buffer
(kill-buffer inspector--temp-buffer)
(setq inspector--temp-buffer nil))
(if (window-prev-buffers)
(quit-window)
(delete-window))
(kill-buffer "*inspector*"))
(defun inspector-pop ()
"Inspect previous object in inspector history."
(interactive)
(when inspector-history
(let ((object (pop inspector-history)))
(inspector--basic-inspect object))))
;;;###autoload
(defun inspector-inspect-last-sexp ()
"Evaluate sexp before point and inspect the result."
(interactive)
(let ((result (eval (eval-sexp-add-defvars (elisp--preceding-sexp)) lexical-binding)))
(inspector-inspect result)))
(defun inspector--elisp-defun-at-point ()
"Return the name of the function at point."
(save-excursion
(beginning-of-defun)
(let ((sexp (read (current-buffer))))
(when (eq (car sexp) 'defun)
(cadr sexp)))))
;;;###autoload
(defun inspector-inspect-defun ()
"Evaluate the top s-exp - simmilar the effect
of M-x or eval-defun and inspect the result"
(interactive)
(let* ((s-exp (read
(save-excursion
(beginning-of-defun)
(buffer-substring-no-properties (point) (point-max)))))
(result (eval s-exp lexical-binding)))
(inspector-inspect result)))
;;;###autoload
(defun inspector-inspect-region (start end)
"Evaluate the region from START TO END and inspect the result."
(interactive "r")
(let* ((region (read
(save-excursion
(buffer-substring-no-properties start end))))
(result (eval region lexical-binding)))
(inspector-inspect result)))
;;;###autoload
(defun inspector-pprint-inspected-object ()
"Pretty print the object being inspected."
(interactive)
(let ((object (buffer-local-value '* (current-buffer))))
(with-current-buffer-window "*inspector pprint*"
nil nil
(emacs-lisp-mode)
;; local-set-key modifies the mode map of the entire buffer's major mode (emacs-lisp-mode-map).
;; to modify the map for this buffer only, we need to use a copy of the mode-map:
(use-local-map (copy-keymap emacs-lisp-mode-map))
(local-set-key "q" #'kill-this-buffer)
(let ((pp-use-max-width inspector-pp-use-max-width)
(pp-max-width inspector-pp-max-width))
(ignore pp-use-max-width pp-max-width)
(pp object)
;; Jump to this buffer
(if inspector-switch-to-buffer
(switch-to-buffer-other-window "*inspector pprint*")
(display-buffer "*inspector pprint*"))))))
;;-- Inspection from Emacs debugger
;;;###autoload
(defun inspector-inspect-debugger-locals ()
"Inspect local variables of the frame at point in debugger backtrace."
(interactive)
(when (not (backtrace-get-index))
(user-error "No backtrace frame at point. Please move cursor to a backtrace frame"))
(let* ((nframe (debugger-frame-number))
(locals (backtrace--locals nframe)))
(inspector-inspect (inspector--alist-to-plist locals))))
;;;###autoload
(defun inspector-inspect-debugger-local (varname)
"Inspect local variable named VARNAME of frame at point in debugger backtrace."
(interactive
(list
(completing-read "Inspect local variable: "
(with-current-buffer "*Backtrace*"
;; The addition of 0 to the return value of (debugger-frame-number) is necessary here. Why?? Ugly hack ...
;; On Emacs 29.0.50 with native comp at least ..
(let ((n (+ (debugger-frame-number) 0)))
(mapcar #'car (backtrace--locals n)))))))
(with-current-buffer "*Backtrace*"
(let* ((n (debugger-frame-number))
(locals (backtrace--locals n)))
(inspector-inspect (cdr (assoc (intern varname) locals))))))
;;;###autoload
(defun inspector-inspect-debugger-return-value ()
"Inspect the current return value in the debugger."
(interactive)
;; Find the backtrace-frame for the function 'debug.
;; When there's an 'exit arg, assume that is the return-value and inspect it.
(let ((debug-frame (cl-find-if (lambda (frame)
(eq (backtrace-frame-fun frame) 'debug))
(backtrace-get-frames))))
(when (not debug-frame)
(user-error "Can't read debugger status"))
(let ((debug-exit (cl-getf (backtrace-frame-args debug-frame) 'exit :_not_found_)))
(when (eq debug-exit :_not_found_)
(user-error "Debugger is not in return-value state"))
(inspector-inspect debug-exit))))
;;;###autoload
(defun inspector-inspect-stack-frame ()
"Inspect current frame and locals in debugger backtrace."
(interactive)
(when (not (backtrace-get-index))
(user-error "No backtrace frame at point. Please move cursor to a backtrace frame"))
(let* ((nframe (debugger-frame-number))
(frames (backtrace-get-frames)))
(inspector-inspect (nth nframe frames))))
;; Press letter 'i' in debugger backtrace to inspect locals.
(define-key debugger-mode-map "i" #'inspector-inspect-stack-frame)
;;;###autoload
(defun inspector-inspect-in-stack-frame (exp)
"Inspect an expression, in an environment like that outside the debugger.
The environment used is the one when entering the activation frame at point."
(interactive
(list (read--expression "Inspect in stack frame: ")))
(inspector-inspect (debugger-eval-expression exp)))