commit 5e9f90776ddfa64e11823c98406362bfabc03a3c parent f2d6751c48d8318f93124e380f0beba5a1c802cb Author: Wilfred Hughes <me@wilfred.me.uk> Date: Sat, 7 Apr 2018 11:51:30 +0100 Merge branch 'linkify' Closes #91. Diffstat:
| M | CHANGELOG.md | | | 2 | +- |
| M | helpful.el | | | 44 | ++++++++++++++++++++++++++++++++++++++++++++ |
| M | test/unit-test.el | | | 31 | +++++++++++++++++++++++++++++++ |
3 files changed, 76 insertions(+), 1 deletion(-)
diff --git 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 @@ -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 @@ -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")))