helpful

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

commit c98f271b4c164cc54c4149ab8f486e3a67b55e1c
parent d461f395456560a14c796585379c616a813bda7d
Author: Wilfred Hughes <me@wilfred.me.uk>
Date:   Mon, 20 Nov 2017 11:19:13 +0000

Go to the source when pressing RET on the code excerpt

Diffstat:
MCHANGELOG.md | 3+++
Mhelpful.el | 27+++++++++++++++++++++++++--
2 files changed, 28 insertions(+), 2 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md @@ -8,6 +8,9 @@ commands. Fixed an issue where viewing help for a function opened a buffer with its source code. Helpful now cleans up any extra buffers it created. +Pressing RET on the extracted source code now goes to the buffer and +position where the source code is located. + # v0.2 Fixed a crash on viewing aliased primitive functions. diff --git a/helpful.el b/helpful.el @@ -327,7 +327,6 @@ hooks.") (font-lock-fontify-buffer))) (buffer-string))) -;; TODO: allow RET to go the relevant line of code. (defun helpful--source (sym callable-p) "Return the source code of SYM. If the source code cannot be found, return the sexp used." @@ -344,6 +343,11 @@ If the source code cannot be found, return the sexp used." ;; If we've just created this buffer, close it. (unless (-contains-p initial-buffers buf) (kill-buffer buf)) + (when (and source (buffer-file-name buf)) + (setq source (propertize source + 'helpful-path (buffer-file-name buf) + 'helpful-pos start-pos + 'helpful-pos-is-start t))) source) ;; Could not find source -- probably defined interactively, or via ;; a macro, or file has changed. @@ -860,8 +864,27 @@ See also `helpful-callable' and `helpful-variable'." "Go to the reference at point." (interactive) (let* ((path (get-text-property (point) 'helpful-path)) - (pos (get-text-property (point) 'helpful-pos))) + (pos (get-text-property (point) 'helpful-pos)) + (pos-is-start (get-text-property (point) 'helpful-pos-is-start))) (when (and path pos) + ;; If we're looking at a source excerpt, calculate the offset of + ;; point, so we don't just go the start of the excerpt. + (when pos-is-start + (save-excursion + (let ((offset 0)) + (while (and + (get-text-property (point) 'helpful-pos) + (not (eobp))) + (backward-char 1) + (setq offset (1+ offset))) + ;; On the last iteration we moved outside the source + ;; excerpt, so we overcounted by one character. + (setq offset (1- offset)) + + ;; Set POS so we go to exactly the place in the source + ;; code where point was in the helpful excerpt. + (setq pos (+ pos offset))))) + (find-file path) (goto-char pos))))