Emacs: Convert to Full-Width Characters 🚀

By Xah Lee. Date: . Last updated: .

This command convert all English letters and digits and punctuations, from/to half-width and full-width.

[see Unicode: Full-Width Characters]

(defun xah-convert-fullwidth-chars (Begin End &optional ToASCII)
  "Convert English alphabets and punctuation to Chinese fullwidth chars. (or from)
Works on current line or text selection.

Example of fullwidth chars: abc

If `universal-argument' is called first, ask for conversion direction. else, if the command has been repeated, do toggle, else, to fullwidth.

When called in lisp code, Begin End are region begin/end positions. ToASCII, if true, change to ASCII (non-fullwidth chars).

URL `http://xahlee.info/emacs/emacs/elisp_convert_full_width_half_width_chars.html'
Version: 2018-08-02 2023-05-12_113123"
  (interactive
   (let (xp1 xp2)
     (if (use-region-p)
         (setq xp1 (region-beginning) xp2 (region-end))
       (setq xp1 (line-beginning-position) xp2 (line-end-position)))
     (list xp1 xp2
           (if current-prefix-arg
               (if (string-equal (completing-read "Direction:" '("fullwidth" "ascii")) "ascii")
                   t
                 nil
                 )
             (if (equal last-command this-command)
                 (let ((xstate (get 'xah-convert-fullwidth-chars 'toASCII)))
                   (cond
                    ((eq xstate 1) nil)
                    ((eq xstate 0) t)
                    (t nil)))
               nil
               )))))
  (let ((xcharMap
         [
          ["0" "0"] ["1" "1"] ["2" "2"] ["3" "3"] ["4" "4"] ["5" "5"] ["6" "6"] ["7" "7"] ["8" "8"] ["9" "9"]
          ["A" "A"] ["B" "B"] ["C" "C"] ["D" "D"] ["E" "E"] ["F" "F"] ["G" "G"] ["H" "H"] ["I" "I"] ["J" "J"] ["K" "K"] ["L" "L"] ["M" "M"] ["N" "N"] ["O" "O"] ["P" "P"] ["Q" "Q"] ["R" "R"] ["S" "S"] ["T" "T"] ["U" "U"] ["V" "V"] ["W" "W"] ["X" "X"] ["Y" "Y"] ["Z" "Z"]
          ["a" "a"] ["b" "b"] ["c" "c"] ["d" "d"] ["e" "e"] ["f" "f"] ["g" "g"] ["h" "h"] ["i" "i"] ["j" "j"] ["k" "k"] ["l" "l"] ["m" "m"] ["n" "n"] ["o" "o"] ["p" "p"] ["q" "q"] ["r" "r"] ["s" "s"] ["t" "t"] ["u" "u"] ["v" "v"] ["w" "w"] ["x" "x"] ["y" "y"] ["z" "z"]
          ["," ","] ["." "."] [":" ":"] [";" ";"] ["!" "!"] ["?" "?"] ["\"" """] ["'" "'"] ["`" "`"] ["^" "^"] ["~" "~"] ["¯" " ̄"] ["_" "_"]
          [" " " "]
          ["&" "&"] ["@" "@"] ["#" "#"] ["%" "%"] ["+" "+"] ["-" "-"] ["*" "*"] ["=" "="] ["<" "<"] [">" ">"] ["(" "("] [")" ")"] ["[" "["] ["]" "]"] ["{" "{"] ["}" "}"] ["(" "⦅"] [")" "⦆"] ["|" "|"] ["¦" "¦"] ["/" "/"] ["\\" "\"] ["¬" "¬"] ["$" "$"] ["£" "£"] ["¢" "¢"] ["₩" "₩"] ["¥" "¥"]
          ]
         ))

    (save-restriction
      (narrow-to-region Begin End)
      (let ((case-fold-search nil))
        (mapc
         (lambda (xx)
           (goto-char (point-min))
           (while (search-forward (elt xx 0) nil t)
             (replace-match (elt xx 1) t t)))
         (if ToASCII
             (mapcar (lambda (x) (vector (elt x 1) (elt x 0))) xcharMap)
           xcharMap
           ))))

    (put 'xah-convert-fullwidth-chars 'toASCII (if ToASCII 1 0))
    ;; if toASCII is 1, it means the last time the command was called, it changed text to ascii
    ))

Emacs Chinese Topics

Emacs, Convert English to Other