Refactor doom--help-insert-button

Changes:
- Fixes a bug where opening a file which a buffer was already visiting
didn't raise the buffer.
- The function had unused functionality where it would split a string on
'::' and then search for the text after the first '::' in the buffer;
this has been removed.
- The searching functionality has been replaced with the option to pass
a line number, which the opened buffer will jump to. This is now used by
the part of doom/help-packages that shows the places a package is configured.
- It now fails earlier. If there's an invalid file, it fails at call
time rather than when the button is pressed.
- Add a docstring
This commit is contained in:
Yuri Pieters 2021-01-31 16:51:12 +00:00
parent 026d961985
commit 5293c460db

View file

@ -404,33 +404,31 @@ variables, this only lists variables that exist to be customized (defined with
;; ;;
;;; `doom/help-packages' ;;; `doom/help-packages'
(defun doom--help-insert-button (label &optional path) (defun doom--help-insert-button (label &optional uri line)
(cl-destructuring-bind (uri . qs) "Helper function to insert a button at point.
(let ((parts (split-string label "::" t)))
(cons (string-trim (car parts)) The button will have the text LABEL. If URI is given, the button will open it,
(string-join (cdr parts) "::"))) otherwise the LABEL will be used. If the uri to open is a url it will be opened
(let ((path (or path label))) in a browser. If LINE is given (and the uri to open is not a url), then the file
(insert-text-button will open with point on that line."
uri (let ((uri (or uri label)))
'face 'link (insert-text-button
'follow-link t label
'action 'face 'link
'follow-link t
'action
(if (string-match-p "^https?://" uri)
(lambda (_) (browse-url uri))
(unless (file-exists-p uri)
(user-error "Path does not exist: %S" uri))
(lambda (_) (lambda (_)
(when (window-dedicated-p) (when (window-dedicated-p)
(other-window 1)) (other-window 1))
(pcase (cond ((string-match-p "^https?://" qs) 'url) (find-file uri)
('file)) (when line
((or `file `nil) (goto-char (point-min))
(unless (file-exists-p path) (forward-line (1- line))
(user-error "Path does not exist: %S" path)) (recenter)))))))
(let ((buffer (or (get-file-buffer path)
(find-file path))))
(when qs
(with-current-buffer buffer
(goto-char (point-min))
(re-search-forward qs)
(recenter)))))
(`url (browse-url uri))))))))
(defun doom--help-package-configs (package) (defun doom--help-package-configs (package)
(let ((default-directory doom-emacs-dir)) (let ((default-directory doom-emacs-dir))
@ -584,19 +582,11 @@ If prefix arg is present, refresh the cache."
(insert "This package is configured in the following locations:") (insert "This package is configured in the following locations:")
(dolist (location (doom--help-package-configs package)) (dolist (location (doom--help-package-configs package))
(insert "\n" indent) (insert "\n" indent)
(insert-text-button (cl-destructuring-bind (file line _match)
location (split-string location ":")
'face 'link (doom--help-insert-button location
'follow-link t (expand-file-name file doom-emacs-dir)
'action (string-to-number line))))
`(lambda (_)
(cl-destructuring-bind (file line _match)
',(split-string location ":")
(find-file (expand-file-name file doom-emacs-dir))
(goto-char (point-min))
(forward-line (1- (string-to-number line)))
(recenter)))))
(insert "\n\n"))))) (insert "\n\n")))))
(defvar doom--package-cache nil) (defvar doom--package-cache nil)