emacs lisp: unicode to ascii words

Perm url with updates: http://xahlee.org/emacs/emacs_zap_gremlins.html

Here's a more polished solution. The “asciify-word-or-selection” is a command. It works on current word or text selection.

(defun asciify-string (inputstr)
"Make Unicode string into equivalent ASCII ones.
For example, “passé” becomes “passe”.
This function works on chars in European languages, and does
not transcode arbitrary unicode chars (such as Greek).
Un-transformed unicode char remains in the string."
  (let ()
   (setq inputstr (replace-regexp-in-string \\|à\\|â\\|ä\\|ã\\|å" "a" inputstr))
   (setq inputstr (replace-regexp-in-string \\|è\\|ê\\|ë" "e" inputstr))
   (setq inputstr (replace-regexp-in-string \\|ì\\|î\\|ï" "i" inputstr))
   (setq inputstr (replace-regexp-in-string \\|ò\\|ô\\|ö\\|õ\\|ø" "o" inputstr))
   (setq inputstr (replace-regexp-in-string \\|ù\\|û\\|ü" "u" inputstr))
   (setq inputstr (replace-regexp-in-string "ñ" "n" inputstr))
   (setq inputstr (replace-regexp-in-string "ç" "c" inputstr))
   (setq inputstr (replace-regexp-in-string "ð" "d" inputstr))
   (setq inputstr (replace-regexp-in-string "þ" "th" inputstr))
   (setq inputstr (replace-regexp-in-string "ß" "ss" inputstr))
   (setq inputstr (replace-regexp-in-string "æ" "ae" inputstr))
    inputstr
    ))
(defun asciify-word-or-selection ()
  "Make Unicode string into equivalent ASCII ones.
For example, “passé” becomes “passe”.
This command works on chars in European languages, and does
not transcode arbitrary unicode chars (such as Greek).
They remain in the string.
This command calls `asciify-string' to do the string transformation."
  (interactive)
  (let (bds p1 p2 inputstr)
    (setq bds (get-selection-or-unit 'word))
    (setq inputstr (elt bds 0) p1 (elt bds 1) p2 (elt bds 2)  )
    (setq inputstr (asciify-string inputstr))
    (delete-region p1 p2 )
    (insert inputstr)
    ))

The command uses “get-selection-or-unit”. You can get the code for that at Emacs Lisp: Using thing-at-point.

Popular posts from this blog

Browser User Agent Strings 2012

11 Years of Writing About Emacs

does md5 creates more randomness?