aboutsummaryrefslogtreecommitdiff
path: root/helpful.el
diff options
context:
space:
mode:
authorSean Allred <code@seanallred.com>2018-01-19 20:16:42 -0600
committerWilfred Hughes <me@wilfred.me.uk>2018-04-07 11:20:23 +0100
commit5b487625d04bdbbf63b5bd12368f926ad446d4e9 (patch)
tree60c9c57621c1456bb56fdbf6b4a7e27b87312feb /helpful.el
parentf2d6751c48d8318f93124e380f0beba5a1c802cb (diff)
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 (limited to 'helpful.el')
-rw-r--r--helpful.el23
1 files changed, 23 insertions, 0 deletions
diff --git a/helpful.el b/helpful.el
index 1a7d427..2def3e2 100644
--- 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