helpful

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

commit 35c4c17a850766401cd781ce25318ab135403e2f
parent 331f54e59bc31c09a347e0dece4fc99aad43cb96
Author: Wilfred Hughes <me@wilfred.me.uk>
Date:   Fri, 29 Jun 2018 22:13:59 +0100

Offer a prettier view for strings and keymaps

We show the literal view by default, but provide a toggle button to
switch to the pretty view.

Fixes #122

Diffstat:
MCHANGELOG.md | 5++++-
Mhelpful.el | 41+++++++++++++++++++++++++++++++++++++----
2 files changed, 41 insertions(+), 5 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md @@ -1,6 +1,9 @@ # v0.12 -No changes yet. +Added a 'pretty view' for variables that have string or keymap +values. For strings, we show properties natively in Emacs. For +keymaps, we render each keybinding in a human-readable way along with +a link to the relevant command. # v0.11 diff --git a/helpful.el b/helpful.el @@ -62,6 +62,9 @@ (defvar-local helpful--associated-buffer nil "We store a reference to the buffer we were called from, so we can show the value of buffer-local variables.") +(defvar-local helpful--view-literal t + "Whether to show a value as a literal, or a pretty interactive +view.") (defgroup helpful nil "A rich help system with contextual information." @@ -522,6 +525,16 @@ or disable if already enabled." (eval-expression expr)) (helpful-update))) +(define-button-type 'helpful-view-literal-button + 'action #'helpful--view-literal + 'help-echo "Toggle viewing as a literal") + +(defun helpful--view-literal (_button) + "Set the value of this symbol." + (setq helpful--view-literal + (not helpful--view-literal)) + (helpful-update)) + (define-button-type 'helpful-all-references-button 'action #'helpful--all-references 'symbol nil @@ -1365,6 +1378,14 @@ POSITION-HEADS takes the form ((123 (defun foo)) (456 (defun bar)))." 'symbol sym 'buffer buffer)) +(defun helpful--make-toggle-literal-button () + "Make set button for SYM in BUFFER." + (helpful--button + (if helpful--view-literal + "Pretty view" + "View as literal") + 'helpful-view-literal-button)) + (defun helpful--make-customize-button (sym) "Make customize button for SYM." (helpful--button @@ -1557,13 +1578,25 @@ state of the current symbol." (when (not helpful--callable-p) (helpful--insert-section-break) - (let ((sym helpful--sym) - (buf (or helpful--associated-buffer (current-buffer)))) + (let* ((sym helpful--sym) + (buf (or helpful--associated-buffer (current-buffer))) + (val (helpful--sym-value sym buf))) (insert (helpful--heading "Value") - (helpful--pretty-print - (helpful--sym-value sym buf)) + (cond + ;; Allow strings to be viewed with properties rendered in + ;; Emacs, rather than as a literal. + ((and (stringp val) (not helpful--view-literal)) + val) + ;; Allow keymaps to be viewed with keybindings shown and + ;; links to the commands bound. + ((and (keymapp val) (not helpful--view-literal)) + (helpful--format-keymap val)) + (t + (helpful--pretty-print val))) "\n\n") + (when (or (stringp val) (keymapp val)) + (insert (helpful--make-toggle-literal-button) " ")) (when (memq (helpful--sym-value helpful--sym buf) '(nil t)) (insert (helpful--make-toggle-button helpful--sym buf) " ")) (insert (helpful--make-set-button helpful--sym buf))