Skip to content

Commit

Permalink
Use a cached temporary buffer for truncated printing
Browse files Browse the repository at this point in the history
See: Github issue #30
  • Loading branch information
mmontone committed Sep 25, 2023
1 parent 86f26bf commit 4e85b25
Showing 1 changed file with 32 additions and 16 deletions.
48 changes: 32 additions & 16 deletions inspector.el
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
;; Author: Mariano Montone <[email protected]>
;; URL: https://github.com/mmontone/emacs-inspector
;; Keywords: debugging, tool, lisp, development
;; Version: 0.35
;; Version: 0.36
;; Package-Requires: ((emacs "27.1"))

;; This program is free software; you can redistribute it and/or modify
Expand Down Expand Up @@ -234,25 +234,38 @@ The target width is given by the `pp-max-width' variable."
(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."
(or inspector--temp-buffer
(setq inspector--temp-buffer
(get-buffer-create "*inspector-temp*"))))

(defun inspector--print-truncated (object &optional limit)
"Print OBJECT to a string, truncated.
LIMIT controls the truncation."
(setq limit (or limit inspector-truncation-limit))
(with-temp-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)))
(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.")
Expand Down Expand Up @@ -838,6 +851,9 @@ When PRESERVE-HISTORY is T, inspector history is not cleared."
"Quit the Emacs inspector."
(interactive)
(setq inspector-history nil)
(when inspector--temp-buffer

This comment has been minimized.

Copy link
@alphapapa

alphapapa Sep 25, 2023

FWIW, you might want to test with buffer-live-p here, just as a "best practice."

(kill-buffer inspector--temp-buffer)
(setq inspector--temp-buffer nil))
(if (window-prev-buffers)
(quit-window)
(delete-window))
Expand Down

0 comments on commit 4e85b25

Please sign in to comment.