Skip to content

Instantly share code, notes, and snippets.

@nfunato
Last active July 9, 2016 02:35
Show Gist options
  • Select an option

  • Save nfunato/029e7b1853999fa049b1 to your computer and use it in GitHub Desktop.

Select an option

Save nfunato/029e7b1853999fa049b1 to your computer and use it in GitHub Desktop.
Convert numbers, alphabets, and some other characters (except punctuations) from Zenkaku format to Hankaku format.
;; checked under Emacs24
(defun eisuu-hankaku-region (from to &optional ascii-only)
"Convert JP `zenkaku' eisuu ([A-Z0-9a-z]) and some other chars, which does
not include punctuation chars, in the region to `hankaku' chars.
`Zenkaku' chars belong to `japanese-jisx0208'
`Hankaku' chars belong to `ascii' or `japanese-jisx0201-kana'.
NOTE: Optional argument ASCII-ONLY is ignored here."
(interactive "r\nP")
(save-restriction
(narrow-to-region from to)
(save-excursion
(goto-char (point-min))
(while (re-search-forward "\\cj" nil t)
(let* ((zenkaku (preceding-char))
(hankaku
;; original
; (or (and (not ascii-only)
; (get-char-code-property zenkaku 'jisx0201))
; (get-char-code-property zenkaku 'ascii))
;; patch for japanese-hankaku-region
;; printable ascii chars except [A-Z0-9a-z] are:
;; !¥″#$%&′()*+,ー./:;<=>?@[¥]^_’{|}〜
;; (in hankaku, !\"#$%&'()*+,-./:;<=>?@[\]^_'{|}~)
;; always ascii-only, but punctuations should be omitted
(if (member zenkaku '(?、 ?。 ?, ?. ?〜 ?ー))
nil
(get-char-code-property zenkaku 'ascii))
))
(if hankaku
(japanese-replace-region (match-beginning 0) (match-end 0)
hankaku)))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment