helpful

a fork of Wilfred's helpful
git clone https://git.trogloxene.org/helpful.git
Log | Files | Refs | README

commit 89668dd787248c7f0d35d4ab26f4a61f2efefdcb
parent 50ff4f3aec573602eb7195e393b5acab23185a53
Author: Wilfred Hughes <me@wilfred.me.uk>
Date:   Sun, 22 Jul 2018 23:57:25 +0100

Allow users to view all the buffer-local values

Fixes #49

Diffstat:
MCHANGELOG.md | 3++-
Mhelpful.el | 95++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------
2 files changed, 87 insertions(+), 11 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md @@ -1,6 +1,7 @@ # v0.13 -Buffer-local variables are now highlighted. +Buffer-local variables are now highlighted, and it's possible to see +all the different values of a buffer-local variable. Variable values are now shown before docstrings. diff --git a/helpful.el b/helpful.el @@ -444,9 +444,10 @@ or disable if already enabled." (let ((buf (button-get button 'buffer)) (pos (button-get button 'position))) (switch-to-buffer buf) - (goto-char pos))) + (when pos + (goto-char pos)))) -(defun helpful--buffer-button (buffer pos) +(defun helpful--buffer-button (buffer &optional pos) "Return a button that switches to BUFFER and puts point at POS." (helpful--button (buffer-name buffer) @@ -464,6 +465,38 @@ or disable if already enabled." "Open Customize for this symbol." (customize-variable (button-get button 'symbol))) +(define-button-type 'helpful-associated-buffer-button + 'action #'helpful--associated-buffer + 'symbol nil + 'prompt-p nil + 'follow-link t + 'help-echo "Change associated buffer") + +(defun helpful--read-live-buffer (prompt predicate) + "Read a live buffer name, and return the buffer object. + +This is largely equivalent to `read-buffer', but counsel.el +overrides that to include previously opened buffers." + (get-buffer + (completing-read + prompt + (-map #'buffer-name (buffer-list)) + predicate + t))) + +(defun helpful--associated-buffer (button) + "Change the associated buffer, so we can see buffer-local values." + (let ((sym (button-get button 'symbol)) + (prompt-p (button-get button 'prompt-p))) + (if prompt-p + (setq helpful--associated-buffer + (helpful--read-live-buffer + "View variable in: " + (lambda (buf-name) + (local-variable-p sym (get-buffer buf-name))))) + (setq helpful--associated-buffer nil))) + (helpful-update)) + (define-button-type 'helpful-toggle-button 'action #'helpful--toggle 'symbol nil @@ -1378,8 +1411,19 @@ POSITION-HEADS takes the form ((123 (defun foo)) (456 (defun bar)))." (defun helpful--sym-value (sym buf) "Return the value of SYM in BUF." - (with-current-buffer buf - (symbol-value sym))) + (cond + ;; If we're given a buffer, look up the variable in that buffer. + (buf + (with-current-buffer buf + (symbol-value sym))) + ;; If we don't have a buffer, and this is a buffer-local variable, + ;; ensure we return the default value. + ((local-variable-if-set-p sym) + (default-value sym)) + ;; Otherwise, just return the value in the current buffer, which is + ;; the global value. + (t + (symbol-value sym)))) (defun helpful--insert-section-break () "Insert section break into helpful buffer." @@ -1720,15 +1764,31 @@ state of the current symbol." (when (not helpful--callable-p) (helpful--insert-section-break) (let* ((sym helpful--sym) - (val-buf (or helpful--associated-buffer (current-buffer))) - (val (helpful--sym-value sym val-buf)) + (val (helpful--sym-value sym helpful--associated-buffer)) (multiple-views-p (or (stringp val) (keymapp val) (and (s-ends-with-p "-hook" (symbol-name sym)) (consp val))))) (insert - (helpful--heading "Value") + (helpful--heading + (cond + ;; Buffer-local variable and we're looking at the value in + ;; a specific buffer. + ((and + helpful--associated-buffer + (local-variable-p sym helpful--associated-buffer)) + (format "Value in %s" + (helpful--button + (format "#<buffer %s>" (buffer-name helpful--associated-buffer)) + 'helpful-buffer-button + 'buffer helpful--associated-buffer + 'position pos))) + ;; Buffer-local variable but default value. + ((local-variable-if-set-p sym) + "Default Value") + ;; This variable is not buffer-local. + (t "Value"))) (cond (helpful--view-literal (helpful--pretty-print val)) @@ -1749,9 +1809,24 @@ state of the current symbol." "\n\n") (when multiple-views-p (insert (helpful--make-toggle-literal-button) " ")) - (when (memq (helpful--sym-value helpful--sym val-buf) '(nil t)) - (insert (helpful--make-toggle-button helpful--sym val-buf) " ")) - (insert (helpful--make-set-button helpful--sym val-buf)) + + (when (local-variable-if-set-p sym) + (insert + (helpful--button + "Buffer values" + 'helpful-associated-buffer-button + 'symbol sym + 'prompt-p t) + " " + (helpful--button + "Default value" + 'helpful-associated-buffer-button + 'symbol sym + 'prompt-p nil) + " ")) + (when (memq (helpful--sym-value helpful--sym helpful--associated-buffer) '(nil t)) + (insert (helpful--make-toggle-button helpful--sym helpful--associated-buffer) " ")) + (insert (helpful--make-set-button helpful--sym helpful--associated-buffer)) (when (custom-variable-p helpful--sym) (insert " " (helpful--make-customize-button helpful--sym)))))