Skip to content

Commit

Permalink
Preserve point when finding variables defined in an open buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
Wilfred committed Sep 19, 2022
1 parent cf6bc8b commit 6a65259
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 16 deletions.
47 changes: 31 additions & 16 deletions helpful.el
Original file line number Diff line number Diff line change
Expand Up @@ -1421,6 +1421,7 @@ buffer."
(let ((initial-buffers (buffer-list))
(primitive-p (helpful--primitive-p sym callable-p))
(library-name nil)
(src-path nil)
(buf nil)
(pos nil)
(opened nil))
Expand All @@ -1430,14 +1431,23 @@ buffer."
(or find-function-C-source-directory
(not primitive-p)))

(when (and (symbolp sym) callable-p)
(setq library-name (cdr (find-function-library sym))))
(when (symbolp sym)
(if callable-p
(setq library-name (cdr (find-function-library sym)))
;; Based on `find-variable-noselect'.
(setq library-name
(or
(symbol-file sym 'defvar)
(help-C-file-name sym 'var)))))

(when library-name
(setq src-path (helpful--library-path library-name)))

(cond
((and (not (symbolp sym)) (functionp sym))
(list nil nil nil))
((and callable-p library-name)
(-when-let (src-path (helpful--library-path library-name))
(when src-path
(-let [(src-buf src-opened) (helpful--open-if-needed src-path)]
(setq buf src-buf)
(setq opened src-opened))
Expand Down Expand Up @@ -1467,19 +1477,24 @@ buffer."
edebug-info)]
(setq buf (marker-buffer marker))
(setq pos (marker-position marker)))))
((not callable-p)
(condition-case _err
(-let [(sym-buf . sym-pos) (find-definition-noselect sym 'defvar)]
(setq buf sym-buf)
(unless (-contains-p initial-buffers buf)
(setq opened t))
(setq pos sym-pos))
(search-failed nil)
;; If your current Emacs instance doesn't match the source
;; code configured in find-function-C-source-directory, we can
;; get an error about not finding source. Try
;; `default-tab-width' against Emacs trunk.
(error nil))))
((and (not callable-p) src-path)
(-let [(src-buf src-opened) (helpful--open-if-needed src-path)]
(setq buf src-buf)
(setq opened src-opened)

(with-current-buffer buf
;; `find-function-search-for-symbol' moves point. Prevent
;; that.
(save-excursion
(condition-case _err
(setq pos (cdr (find-variable-noselect sym 'defvar)))
(search-failed nil)
;; If your current Emacs instance doesn't match the source
;; code configured in find-function-C-source-directory, we can
;; get an error about not finding source. Try
;; `default-tab-width' against Emacs trunk.
(error nil)))))))

(list buf pos opened)))

(defun helpful--reference-positions (sym callable-p buf)
Expand Down
9 changes: 9 additions & 0 deletions test/helpful-unit-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -1057,6 +1057,15 @@ find the source code."
(should
(s-contains-p "Original Value\n123" (buffer-string))))

(ert-deftest helpful--preserve-position ()
"Show the original value for defcustom variables."
(-let [(buf _pos _opened) (helpful--definition 'helpful-test-custom-var nil)]
(with-current-buffer buf
(goto-char (point-min))
(save-current-buffer
(helpful-variable 'helpful-test-custom-var))
(should (eq (point) (point-min))))))

(ert-deftest helpful--package-version ()
"Report when a variable was added"
(helpful-variable 'helpful-test-custom-var)
Expand Down

0 comments on commit 6a65259

Please sign in to comment.