commit 5b487625d04bdbbf63b5bd12368f926ad446d4e9
parent f2d6751c48d8318f93124e380f0beba5a1c802cb
Author: Sean Allred <code@seanallred.com>
Date: Fri, 19 Jan 2018 20:16:42 -0600
Linkify URLs in documentation strings
URLs written as
URL `some-url'
is transformed into a button
URL some-url
with `some-url' being the button. The URL "some-url" is passed to
`browse-url' as the action (i.e., `(browse-url "some-url")').
The positioning of `helpful--propertize-links' inside
`helpful--format-docstring' is of some importance; the call to
`helpful--propertize-quoted' presently catches `some-url' as a symbol
and deals with it as such. We need to look for URLs before symbols.
Diffstat:
1 file changed, 23 insertions(+), 0 deletions(-)
diff --git a/helpful.el b/helpful.el
@@ -834,10 +834,33 @@ unescaping too."
(helpful--format-command-keys)
(helpful--split-first-line)
(helpful--propertize-info)
+ (helpful--propertize-links)
(helpful--propertize-keywords)
(helpful--propertize-quoted)
(s-trim)))
+(define-button-type 'helpful-link-button
+ 'action #'helpful--follow-link
+ 'follow-link t
+ 'help-echo "Follow this link")
+
+(defun helpful--propertize-links (docstring)
+ "Convert URL links in docstrings to buttons."
+ (replace-regexp-in-string
+ (rx "URL `" (group (*? any)) "'")
+ (lambda (match)
+ (let ((url (match-string 1 match)))
+ (concat "URL "
+ (helpful--button
+ url
+ 'helpful-link-button
+ 'url url))))
+ docstring))
+
+(defun helpful--follow-link (button)
+ "Follow the URL specified by BUTTON."
+ (browse-url (button-get button 'url)))
+
(defconst helpful--highlighting-funcs
'(ert--activate-font-lock-keywords
highlight-quoted-mode