Created
December 7, 2012 20:12
-
-
Save takehiko/4236156 to your computer and use it in GitHub Desktop.
URL encoding with Emacs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 領域を選択して 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)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
👍