emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Michael Brand <michael.ch.brand@gmail.com>
To: Org Mode <emacs-orgmode@gnu.org>
Subject: Re: [PATCH] Small improvement of maintainability of link escaping
Date: Sat, 5 Oct 2013 15:31:09 +0200	[thread overview]
Message-ID: <CALn3zog6U-gedfZp0rL9hzG9x6x5G=doutGOw2BiSCXBqJWDEw@mail.gmail.com> (raw)
In-Reply-To: <CALn3zogi05x=n-DQX4J9sdptDC0r3jfrrKev=LVbrzz-Q6mAOA@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 97 bytes --]

Sorry, I attached the wrong patch (the other one that is waiting in
another thread), here again.

[-- Attachment #2: 0001-Small-improvement-of-maintainability-of-link-escapin.patch.txt --]
[-- Type: text/plain, Size: 5047 bytes --]

From 6d5f603058211ade3b5db118cb3a7d04fbf52a6e Mon Sep 17 00:00:00 2001
From: Michael Brand <michael.ch.brand@gmail.com>
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")))))
 
 
 \f
-- 
1.7.12.4 (Apple Git-37)


  reply	other threads:[~2013-10-05 13:31 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-10-05 13:04 [PATCH] Small improvement of maintainability of link escaping Michael Brand
2013-10-05 13:31 ` Michael Brand [this message]
2013-10-16 13:49   ` Michael Brand
2013-10-17  5:02     ` Carsten Dominik
2013-11-17 12:04 ` Michael Brand

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CALn3zog6U-gedfZp0rL9hzG9x6x5G=doutGOw2BiSCXBqJWDEw@mail.gmail.com' \
    --to=michael.ch.brand@gmail.com \
    --cc=emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).