views:

581

answers:

4

I know there's got to be a built-in function to decode a URL-encoded string (query string) in Emacs Lisp, but for the life of me I can't find it today, either in my lisp/ folder or with google.

Anybody remember what it's called?

A: 

I think you're making it a little too hard: split-string will probably do most of what you want. For fancier stuff, have a look at the functions in url-expand.el; unfortunately, many of them don't have doc-strings, so you may have to read code.

url-generic-parse-url looks like a potential winner.

Charlie Martin
Hmm, maybe I misspoke. I've got a string like "foo%20bar" and I want "foo bar" out of it. I don't see how split-string or url-generic-parse-url or anything in url-expand.el really helps here.
Ken
+2  A: 

org-link-unescape does the job for very simple cases ... w3m-url-decode-string is better, but it isn't built in and the version I have locally isn't working with Emacs 23.

nullptr
That's good enough for now. It is strange that it uses only a constant list of 17 chars, though. Maybe I'll fix that sometime.
Ken
+4  A: 
url-unhex-string
huaiyuan
+1  A: 

Emacs is shipped with an URL library, that provides a bunch of URL parsing functions—as huaiyuan and Charlie Martin already pointed out. Here is a small example that'd give you an idea how to use it:

(let ((url "http://www.google.hu/search?q=elisp+decode+url&btnG=Google+keres%E9s&meta="))
  ;; Return list of arguments and values
  (url-parse-query-string
   ;; Decode hexas
   (url-unhex-string
    ;; Retrieve argument list
    (url-filename
     ;; Parse URL, return a struct
     (url-generic-parse-url url)))))
=> (("meta" "") ("btnG" "Google+keresés") ("/search?q" "elisp+decode+url"))

I think is better to rely on it than org-mode as it is its main purpose to parse URL.

Török Gábor