aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CHANGELOG.md2
-rw-r--r--helpful.el21
-rw-r--r--test/unit-test.el25
3 files changed, 45 insertions, 3 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 2def3e2..2c07900 100644
--- a/helpful.el
+++ b/helpful.el
@@ -835,6 +835,7 @@ unescaping too."
(helpful--split-first-line)
(helpful--propertize-info)
(helpful--propertize-links)
+ (helpful--propertize-bare-links)
(helpful--propertize-keywords)
(helpful--propertize-quoted)
(s-trim)))
@@ -857,6 +858,26 @@ unescaping too."
'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)))
diff --git a/test/unit-test.el b/test/unit-test.el
index 0309876..99eb5af 100644
--- a/test/unit-test.el
+++ b/test/unit-test.el
@@ -230,8 +230,7 @@ symbol (not a form)."
(get-text-property paren-position 'button formatted))))
(ert-deftest helpful--format-docstring--url ()
- "Ensure we propertize URLs."
- ;; This is the typical format.
+ "Ensure we propertize URLs with backticks."
(let* ((formatted (helpful--format-docstring "URL `http://example.com'"))
(url-position (s-index-of "h" formatted)))
(should
@@ -239,6 +238,28 @@ symbol (not a form)."
(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")))