Skip to content

Commit

Permalink
Forget, calles and show-all-references
Browse files Browse the repository at this point in the history
Store the source sexp in helpful buffers, since it is needed by
show-callees.

This should now be all buttons.
  • Loading branch information
nbfalcon committed Mar 12, 2021
1 parent b6b64e1 commit 0f20582
Showing 1 changed file with 52 additions and 18 deletions.
70 changes: 52 additions & 18 deletions helpful.el
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@

(defvar-local helpful--sym nil)
(defvar-local helpful--callable-p nil)
(defvar-local helpful--source-sexp nil
"Source-code sexp for the current symbol.")
(defvar-local helpful--associated-buffer nil
"The buffer being used when showing inspecting
buffer-local variables.")
Expand Down Expand Up @@ -312,19 +314,24 @@ Return SYM otherwise."
'follow-link t
'help-echo "Unbind this function")

;; TODO: it would be nice to optionally delete the source code too.
(defun helpful--forget (button)
"Unbind the current symbol."
(let* ((sym (button-get button 'symbol))
(callable-p (button-get button 'callable-p))
(kind (helpful--kind-name sym callable-p)))
(defun helpful--forget-1 (sym callable-p)
"Forge SYM interactively.
If CALLABLE-P is given, SYM is assumed to be a function."
(let ((kind (helpful--kind-name sym callable-p)))
(when (yes-or-no-p (format "Forget %s %s?" kind sym))
(if callable-p
(fmakunbound sym)
(makunbound sym))
(message "Forgot %s %s." kind sym)
(kill-buffer (current-buffer)))))

;; TODO: it would be nice to optionally delete the source code too.
(defun helpful--forget (button)
"Unbind the current symbol."
(let* ((sym (button-get button 'symbol))
(callable-p (button-get button 'callable-p)))
(helpful--forget-1 sym callable-p)))

(define-button-type 'helpful-c-source-directory
'action #'helpful--c-source-directory
'follow-link t
Expand Down Expand Up @@ -645,17 +652,22 @@ overrides that to include previously opened buffers."
'follow-link t
'help-echo "Find all references to this symbol")

(defun helpful--all-references-1 (sym callable-p)
"Find all references of SYM.
If CALLABLE-P, SYM is a function or macro."
(cond
((not callable-p)
(elisp-refs-variable sym))
((functionp sym)
(elisp-refs-function sym))
((macrop sym)
(elisp-refs-macro sym))))

(defun helpful--all-references (button)
"Find all the references to the symbol that this BUTTON represents."
(let ((sym (button-get button 'symbol))
(callable-p (button-get button 'callable-p)))
(cond
((not callable-p)
(elisp-refs-variable sym))
((functionp sym)
(elisp-refs-function sym))
((macrop sym)
(elisp-refs-macro sym)))))
(helpful--all-references-1 sym callable-p)))

(define-button-type 'helpful-callees-button
'action #'helpful--show-callees
Expand All @@ -675,11 +687,10 @@ overrides that to include previously opened buffers."
'callable-p t)
"\n")))

(defun helpful--show-callees (button)
"Find all the references to the symbol that this BUTTON represents."
(defun helpful--show-callees-1 (sym raw-source)
"Show all callees of SYM with RAW-SOURCE.
See `helpful--source-sexp' for RAW-SOURCE."
(let* ((buf (get-buffer-create "*helpful callees*"))
(sym (button-get button 'symbol))
(raw-source (button-get button 'source))
(source
(if (stringp raw-source)
(read raw-source)
Expand All @@ -705,6 +716,11 @@ overrides that to include previously opened buffers."

(helpful-mode))))

(defun helpful--show-callees (button)
"Find all the references to the symbol that this BUTTON represents."
(helpful--show-callees-1 (button-get button 'symbol)
(button-get button 'source)))

(define-button-type 'helpful-manual-button
'action #'helpful--manual
'symbol nil
Expand Down Expand Up @@ -1146,6 +1162,10 @@ buffer."
(goto-char pos)))
(pop-to-buffer buf)))

(defun helpful-forget ()
"Forget the current helpful symbol."
(helpful--forget-1 helpful--sym helpful--callable-p))

(defun helpful-disassemble ()
"Disassemble the current symbol."
(interactive)
Expand All @@ -1168,6 +1188,19 @@ buffer."
(helpful--toggle-tracing helpful--sym)
(helpful-update))

(defun helpful-callees ()
(interactive)
(helpful--ensure)
(unless (and helpful--callable-p
(not (helpful--primitive-p helpful--sym helpful--callable-p)))
(user-error "Must be a non-primtive function"))
(helpful--show-callees-1 helpful--sym helpful--source-sexp))

(defun helpful-all-references ()
(interactive)
(helpful--ensure)
(helpful--all-references-1 helpful--sym helpful--callable-p))

(defun helpful-set-value ()
(interactive)
(helpful--ensure)
Expand Down Expand Up @@ -2220,7 +2253,8 @@ state of the current symbol."
(helpful--definition helpful--sym helpful--callable-p)
'(nil nil nil)))
(source (when look-for-src
(helpful--source helpful--sym helpful--callable-p buf pos)))
(->> (helpful--source helpful--sym helpful--callable-p buf pos)
(setq-local helpful--source-sexp))))
(source-path (when buf
(buffer-file-name buf)))
(references (helpful--calculate-references
Expand Down

0 comments on commit 0f20582

Please sign in to comment.