help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to get rid of Microsoft dumb quotes, e.g. \222 for apostrophe?


From: ken
Subject: Re: How to get rid of Microsoft dumb quotes, e.g. \222 for apostrophe?
Date: Mon, 19 Feb 2007 09:17:36 -0500
User-agent: Thunderbird 1.5.0.9 (X11/20061206)

> "Endless Story" <usable.thought@gmail.com> writes:
> 
>> I have just started seeing lots of nasty stuff like \222 instead of
>> apostrophes in working on text files in Emacs on XP, then trying to
>> reformat these files for LaTeX. 
> 
> ....

The below is not textbook elisp, but it works, is easily understandable,
and so too is easy to modify and add other "characters" to.  For
example, if you have a typical set of chars which signal the beginning
of paragraph (like "\n\n"), you could insert another replace-string line
to convert that to the appropriate LaTeX (or HTML or whatever) coding
for "paragraph".  Such a set of replacements might be better organized
into a separate (but similar) function however.  Open Source == Your Choice.

(defun replace-garbage-chars ()
"Replace goofy MS and other garbage characters with latin1 equivalents."
(interactive)
(save-excursion                         ;save the current point
  (replace-string "—" "--" nil (point-min) (point-max)) ; multi-byte
  (replace-string "‘" "`" nil (point-min) (point-max))
  (replace-string "’" "'" nil (point-min) (point-max))
  (replace-string "“" "``" nil (point-min) (point-max))
  (replace-string "”" "''" nil (point-min) (point-max))
  (replace-string "–" "--" nil (point-min) (point-max))
))

Note that chars/strings within the first set of double-quotes in each
pair of replace-string args appear in emacs as, e.g., "\221".  To enter
these escaped numbers, e.g. "\221", do C-q 2 2 1 RETURN.

Also, multi-byte strings such as the first should be toward
the top of the list so that single-byte replacements don't
cut them up, making subsequent searches for them impossible.

To discover the code for a new (garbage) char to be replaced,
put the point over it and do "C-x="; the first code returned in
the minibuffer tells you the escaped number you want to replace.

With this function in a file in directory in the emacs path and this in
my ~/.emacs:

(global-set-key "\C-cr" 'replace-garbage-chars)

doing C-cr in an emacs buffer performs the replacements without moving
the point... exactly what I was looking for.


Enjoy,
ken




reply via email to

[Prev in Thread] Current Thread [Next in Thread]