aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWilfred Hughes <me@wilfred.me.uk>2018-04-07 11:51:30 +0100
committerWilfred Hughes <me@wilfred.me.uk>2018-04-07 11:51:42 +0100
commit5e9f90776ddfa64e11823c98406362bfabc03a3c (patch)
treebd2ceca8e4b5fe9753526ed182792b8b9a13ca4d
parentf2d6751c48d8318f93124e380f0beba5a1c802cb (diff)
parentfe126eac03e510a47a12dc468f0be70d024e130e (diff)
Merge branch 'linkify'
Closes #91.
-rw-r--r--CHANGELOG.md2
-rw-r--r--helpful.el44
-rw-r--r--test/unit-test.el31
3 files changed, 76 insertions, 1 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 52bc2a6..073aa99 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,6 +1,6 @@
# v0.10
-No changes yet.
+URLs in docstrings are now converted to buttons.
# v0.9
diff --git a/helpful.el b/helpful.el
index 1a7d427..2c07900 100644
--- a/helpful.el
+++ b/helpful.el
@@ -834,10 +834,54 @@ unescaping too."
(helpful--format-command-keys)
(helpful--split-first-line)
(helpful--propertize-info)
+ (helpful--propertize-links)
+ (helpful--propertize-bare-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--propertize-bare-links (docstring)
+ "Convert URL links in docstrings to buttons."
+ (replace-regexp-in-string
+ (rx (group (or string-start space))
+ (group "http" (? "s") "://" (+? (not (any space))))
+ (group (? (any "." ">" ")"))
+ (or space string-end)))
+ (lambda (match)
+ (let ((space-before (match-string 1 match))
+ (url (match-string 2 match))
+ (after (match-string 3 match)))
+ (concat
+ space-before
+ (helpful--button
+ url
+ 'helpful-link-button
+ 'url url)
+ after)))
+ 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
diff --git a/test/unit-test.el b/test/unit-test.el
index f232569..99eb5af 100644
--- a/test/unit-test.el
+++ b/test/unit-test.el
@@ -229,6 +229,37 @@ symbol (not a form)."
(should
(get-text-property paren-position 'button formatted))))
+(ert-deftest helpful--format-docstring--url ()
+ "Ensure we propertize URLs with backticks."
+ (let* ((formatted (helpful--format-docstring "URL `http://example.com'"))
+ (url-position (s-index-of "h" formatted)))
+ (should
+ (string-equal formatted "URL http://example.com"))
+ (should
+ (get-text-property url-position 'button formatted))))
+
+(ert-deftest helpful--format-docstring--bare-url ()
+ "Ensure we propertize URLs without backticks."
+ (let* ((formatted (helpful--format-docstring "http://example.com\nbar"))
+ (url-position (s-index-of "h" formatted)))
+ (should
+ (string-equal formatted "http://example.com\nbar"))
+ (should
+ (get-text-property url-position 'button formatted))
+ (should
+ (equal
+ (get-text-property url-position 'url formatted)
+ "http://example.com")))
+ ;; Don't consider trailing punctuation to be part of the URL.
+ (let* ((formatted (helpful--format-docstring "See http://example.com."))
+ (url-position (s-index-of "h" formatted)))
+ (should
+ (string-equal formatted "See http://example.com."))
+ (should
+ (equal
+ (get-text-property url-position 'url formatted)
+ "http://example.com"))))
+
(ert-deftest helpful--definition-c-vars ()
"Handle definitions of variables in C source code."
(let* ((emacs-src-path (f-join default-directory "emacs-25.3" "src")))