From 6d5f603058211ade3b5db118cb3a7d04fbf52a6e Mon Sep 17 00:00:00 2001 From: Michael Brand Date: Sat, 5 Oct 2013 15:00:25 +0200 Subject: [PATCH] Small improvement of maintainability of link escaping * lisp/org.el (org-link-escape-chars): Add comment with escape numbers alphabetically ordered. (org-link-escape-chars-browser): Add comment with escape numbers. (org-link-escape): Use better readable char constant instead of number constant for percent char. (org-link-escape-browser): New function to substitute duplicate source code. (org-open-at-point): Substitute duplicate source code. * testing/README: Make comment in source code example clearer. * testing/lisp/test-org.el (test-org/org-link-escape-chars-browser): Change URL to real use case and use the new function `org-link-escape-browser'. --- lisp/org.el | 32 ++++++++++++++------------------ testing/README | 4 ++-- testing/lisp/test-org.el | 8 +++++--- 3 files changed, 21 insertions(+), 23 deletions(-) diff --git a/lisp/org.el b/lisp/org.el index 5ff9969..9149441 100644 --- a/lisp/org.el +++ b/lisp/org.el @@ -9732,11 +9732,13 @@ according to FMT (default from `org-email-link-description-format')." "]")) (defconst org-link-escape-chars - '(?\ ?\[ ?\] ?\; ?\= ?\+) + ;;%20 %2B %3B %3D %5B %5D + '(?\ ?\+ ?\; ?\= ?\[ ?\]) "List of characters that should be escaped in link. This is the list that is used for internal purposes.") (defconst org-link-escape-chars-browser + ;;%20 %22 '(?\ ?\") "List of escapes for characters that are problematic in links. This is the list that is used before handing over to the browser.") @@ -9758,7 +9760,7 @@ If optional argument MERGE is set, merge TABLE into (mapconcat (lambda (char) (if (or (member char table) - (and (or (< char 32) (= char 37) (> char 126)) + (and (or (< char 32) (= char ?\%) (> char 126)) org-url-hexify-p)) (mapconcat (lambda (sequence-element) (format "%%%.2X" sequence-element)) @@ -9767,6 +9769,13 @@ If optional argument MERGE is set, merge TABLE into (char-to-string char))) "") (char-to-string char))) text "")) +(defun org-link-escape-browser (text) + (if (org-string-match-p + (concat "[[:nonascii:]" org-link-escape-chars-browser "]") + text) + (org-link-escape text org-link-escape-chars-browser) + text)) + (defun org-link-unescape (str) "Unhex hexified Unicode strings as returned from the JavaScript function encodeURIComponent. E.g. `%C3%B6' is the german o-Umlaut." @@ -10467,24 +10476,11 @@ application the system uses for this file type." (apply cmd (nreverse args1)))) ((member type '("http" "https" "ftp" "news")) - (browse-url - (concat type ":" - (if (org-string-match-p - (concat "[[:nonascii:]" - org-link-escape-chars-browser "]") - path) - (org-link-escape path org-link-escape-chars-browser) - path)))) + (browse-url (concat type ":" (org-link-escape-browser path)))) ((string= type "doi") - (browse-url - (concat org-doi-server-url - (if (org-string-match-p - (concat "[[:nonascii:]" - org-link-escape-chars-browser "]") - path) - (org-link-escape path org-link-escape-chars-browser) - path)))) + (browse-url (concat org-doi-server-url + (org-link-escape-browser path)))) ((member type '("message")) (browse-url (concat type ":" path))) diff --git a/testing/README b/testing/README index 9601ea7..e25a109 100644 --- a/testing/README +++ b/testing/README @@ -64,9 +64,9 @@ load and run the test suite with the following commands. Use this as a demo example of a failing test #+BEGIN_SRC emacs-lisp (ert-deftest test-org/org-link-escape-ascii-character-demo-of-fail () - (should (string= "%5B" ;; expected is right + (should (string= "%5B" ;; expecting %5B is right (org-link-escape "["))) - (should (string= "%5C" ;; expected is wrong, "%5D" would be right + (should (string= "%5C" ;; expecting %5C is wrong, %5D right (org-link-escape "]")))) #+END_SRC or evaluate the ert-deftest form of the test you want to run. Then diff --git a/testing/lisp/test-org.el b/testing/lisp/test-org.el index b6c5558..32fa69e 100644 --- a/testing/lisp/test-org.el +++ b/testing/lisp/test-org.el @@ -524,9 +524,11 @@ http://article.gmane.org/gmane.emacs.orgmode/21459/" "Escape a URL to pass to `browse-url'." (should (string= - "http://some.host.com/search?q=%22Org%20mode%22" - (org-link-escape "http://some.host.com/search?q=\"Org mode\"" - org-link-escape-chars-browser)))) + (concat "http://lists.gnu.org/archive/cgi-bin/namazu.cgi?query=" + "%22Release%208.2%22&idxname=emacs-orgmode") + (org-link-escape-browser + (concat "http://lists.gnu.org/archive/cgi-bin/namazu.cgi?query=" + "\"Release 8.2\"&idxname=emacs-orgmode"))))) -- 1.7.12.4 (Apple Git-37)