tags:

views:

881

answers:

4

[I cannot find a question tagged emacs-lisp, so let's start, it will be the first on StackOverflow.]

Emacs Lisp has replace-string but I cannot find a replace-char. If I want to replace "typographic" curly quotes (Emacs code for this character is hexa 53979) with regular ASCII quotes, I write:

(replace-string (make-string 1 ?\x53979) "'")

which would certainly be better with replace-char. Any way to improve my code?

+1  A: 

which would certainly be better with replace-char. Any way to improve my code?

Is it actually slow to the point where it matters? My elisp is usually ridiculously inefficient and I never notice. (I only use it for editor tools though, YMMV if you're building the next MS live search with it.)

Also, reading the docs:

This function is usually the wrong thing to use in a Lisp program.
What you probably want is a loop like this:
  (while (search-forward "’" nil t)
    (replace-match "'" nil t))

This answer is probably GPL licensed now.

0124816
+3  A: 

Why not just use

(replace-string "\x53979" "'")

or

(while (search-forward "\x53979" nil t)
    (replace-match "'" nil t))

as recommended in the documentation for replace-string?

cjm
A: 

@cjm: I simply missed the "\x53979" syntax. Indeed, it is much simpler and it works. Thanks.

bortzmeyer
A: 

What about this

(defun my-replace-smart-quotes (beg end)
  "replaces ’ (the curly typographical quote, unicode hexa 2019) to ' (ordinary ascii quote)."
  (interactive "r")
  (save-excursion
    (format-replace-strings '(("\x2019" . "'")) nil beg end)))

Once you have that in your dotemacs, you can paste elisp example codes (from blogs and etc) to your scratch buffer and then immediately press C-M-\ (to indent it properly) and then M-x my-replace-smart-quotes (to fix smart quotes) and finally C-x C-e (to run it).

I find that the curly quote is always hexa 2019, are you sure it's 53979 in your case? You can check characters in buffer with C-u C-x =.

I think you can write "’" in place of "\x2019" in the definition of my-replace-smart-quotes and be fine. It's just to be on the safe side.

RamyenHead