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

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

Re: elisp: Replacing dots in strings (. -> \.)


From: Nikolaj Schumacher
Subject: Re: elisp: Replacing dots in strings (. -> \.)
Date: Sun, 08 Jul 2007 15:27:14 +0200
User-agent: Gnus/5.11 (Gnus v5.11) Emacs/22.1 (darwin)

Jeronimo Pellegrini <j_p@aleph0.info> wrote:

> ;; Adds one symbol to be highlighted as special-face.
> ;;
> (defun add-special-at-point ()
>   "Adds the symbol at point to the list of symbols to be highlighted
>    with special-face."
>   (interactive)
>   (let ((name (symbol-name (symbol-at-point))))
>     (message name) 
>     (let ((newname (replace-regexp-in-string "\\." "\\\\." name t t)))
>       (font-lock-add-keywords nil `((,name . 'special-face))))))

Shouldn't you pass newname instead of name to `font-lock-add-keywords'?

> This doesn't work really well, because when I write a symbol that starts
> with a dot:
>
> .my_special_var

Beware that `symbol-at-point' will only include that period, if it has
symbol syntax in the current buffer.

> (replace-regexp-in-string "\\." "\\\." "aaa . bbb" t t)
>       ==> "aaa \\. bbb"
>
> Now, that confuses me since I was explicitly asking the replacement to
> be treated literally. What I am doing wrong?

You're asking the function to treat the input literally, yet the
backslash has a special meaning while being _parsed_.  That is "\\" is a
string with just one backslash.  Try `insert' on those results, and
there will be fewer of them.
The reason some of them are missing, is that Emacs apparently ignores
the single backslash in "\.", which is actually undefined

> Or, is there an easier way to replace the dots in a string so that
> function will work properly?

Yes.  You're probably looking for `regexp-quote'.


Also, you might want to check out this package of mine:
http://nschum.de/src/emacs/highlight-symbol/


regards,
Nikolaj Schumacher




reply via email to

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