Emacs Lisp: Convert Punctuation Between English/Chinese Forms

Buy Xah Emacs Tutorial. Master emacs benefits for life.
, , …,

This page shows a emacs lisp code to convert to/from Chinese/English punctuations.

If you type Chinese or Japanese mixed with English, then often you'll have mixed Asian/Western punctuations, and is laborious to fix manually. Here's a code that will help fix it.

This is used to convert punctuation from English to/from Asian's full-width form. 〔➤ Unicode Full-Width Characters〕Example:

(defun xah-convert-english-chinese-punctuation (φbegin φend &optional φto-direction)
  "Convert punctuation from/to English/Chinese characters.

When called interactively, do current line or selection. The conversion direction is automatically determined.

If `universal-argument' is called, ask user for change direction.

When called in lisp code, φbegin φend are region begin/end positions. φto-direction must be any of the following values: 「\"chinese\"」, 「\"english\"」, 「\"auto\"」.

See also: `xah-remove-punctuation-trailing-redundant-space'.

URL `http://ergoemacs.org/emacs/elisp_convert_chinese_punctuation.html'
Version 2015-04-29"
  (interactive
   (let (ξp1 ξp2)
     (if (use-region-p)
         (progn
           (setq ξp1 (region-beginning))
           (setq ξp2 (region-end)))
       (progn
         (setq ξp1 (line-beginning-position) )
         (setq ξp2 (line-end-position) )))
     (list
      ξp1
      ξp2
      (if current-prefix-arg
          (ido-completing-read
           "Change to: "
           '( "english"  "chinese")
           "PREDICATE"
           "REQUIRE-MATCH")
        "auto"
        ))))
  (let (
        (ξinput-str (buffer-substring-no-properties φbegin φend))
        (ξreplacePairs
         [
          [". " "。"]
          [".\n" "。\n"]
          [", " ","]
          [",\n" ",\n"]
          [": " ":"]
          ["; " ";"]
          ["? " "?"] ; no space after
          ["! " "!"]

          ;; for inside HTML
          [".</" "。</"]
          ["?</" "?</"]
          [":</" ":</"]
          ]
         ))

    (when (string= φto-direction "auto")
      (setq
       φto-direction
       (if
           (or (string-match "。" ξinput-str)
               (string-match "," ξinput-str)
               (string-match "?" ξinput-str)
               (string-match "!" ξinput-str))
           "english"
         "chinese")))
    (save-excursion
      (save-restriction
        (narrow-to-region φbegin φend)
        (mapc 
         (lambda (ξx) 
           (progn
             (goto-char (point-min))
             (while (search-forward (aref ξx 0) nil "noerror")
               (replace-match (aref ξx 1)))))
         (cond
          ((string= φto-direction "chinese") ξreplacePairs)
          ((string= φto-direction "english") (mapcar (lambda (ξpair) (vector (elt ξpair 1) (elt ξpair 0))) ξreplacePairs))
          (t (user-error "Your 3rd argument 「%s」 isn't valid" φto-direction))))))))

Remove Punctuation Trailing Redundant Spaces

Here's helpful command to remove redundant spaces after punctuation.

(require 'xah-replace-pairs)
(require 'xeu_elisp_util)

(defun xah-remove-punctuation-trailing-redundant-space (φp1 φp2)
  "Remove redundant whitespace after punctuation.
Works on current block or text selection.

When called in emacs lisp code, the φp1 φp2 are cursor positions for region.

See also `xah-convert-english-chinese-punctuation'.

URL `http://ergoemacs.org/emacs/elisp_convert_chinese_punctuation.html'
version 2015-02-04
"
  (interactive
   (let ((ξboundary (get-selection-or-unit 'block)))
     (list (elt ξboundary 1) (elt ξboundary 2))))
  (replace-regexp-pairs-region φp1 φp2
                               [
                                ;; clean up. Remove extra space.
                                [" +," ","]
                                [",  +" ", "]
                                ["?  +" "? "]
                                ["!  +" "! "]
                                ["\\.  +" ". "]

                                ;; fullwidth punctuations
                                [", +" ","]
                                ["。 +" "。"]
                                [": +" ":"]
                                ["? +" "?"]
                                ["; +" ";"]
                                ["! +" "!"]
                                ["、 +" "、"]
                                ]
                               "FIXEDCASE" "LITERAL"))

These commands are useful for Twitter too, for saving a few character in Twitter's character limit. Because, English punctuation takes 2 char each, while Chinese version needs just one char, the space is included in the punctuation symbol.

Like it?
Buy Xah Emacs Tutorial
or share
blog comments powered by Disqus