Skip to content

Instantly share code, notes, and snippets.

@takehiko
Created December 7, 2012 20:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takehiko/4236156 to your computer and use it in GitHub Desktop.
Save takehiko/4236156 to your computer and use it in GitHub Desktop.
URL encoding with Emacs
;; 領域を選択して M-x url-quote-region-utf8 で," => URLエンコード文字列" を
;; 挿入します.UTF-8 でエンコードします.
;; また直後に C-x C-x や C-w などとすると,URLエンコード文字列に対する
;; 領域処理となります.
;; Thanks: https://gist.github.com/436913
;; もし (load "htmlutils") をしていなければ
;; 以下のコメントを外してください.
;(defun url-escape-point (c)
; "Escape (quote) a character for a URL"
; (format "%%%X" (string-to-char c)))
(defun url-quote-str-utf8 (s)
"Quote special characters in a URL string"
(let ((unquoted-re "[^a-zA-Z0-9_./-]")
(encoded (encode-coding-string s 'utf-8))
(n 0))
(while (setq n (string-match unquoted-re encoded n))
(setq encoded
(replace-match (url-escape-point (match-string 0 encoded))
t t encoded)
n (1+ n)))
encoded))
(defun url-quote-region-utf8 (min max)
"Quote text for inclusion in a HTTP URL"
(interactive "*r")
(let ((s (copy-sequence (buffer-substring min max))))
(goto-char max)
(insert " => ")
(set-mark-command nil)
(insert (url-quote-str-utf8 s))))
@holtzermann17
Copy link

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment