* elisp: Replacing dots in strings (. -> \.)
@ 2007-07-08 11:10 Jeronimo Pellegrini
2007-07-08 13:27 ` Nikolaj Schumacher
0 siblings, 1 reply; 4+ messages in thread
From: Jeronimo Pellegrini @ 2007-07-08 11:10 UTC (permalink / raw)
To: help-gnu-emacs
Hi.
I'm using Emacs 22.1, and while working on my .emacs file I found
a problem.
I have written functions to add a face to font-lock, and then let me
add words to be highlighted with a different color.
;; The new special face:
;;
(defface special-face
'((t (:foreground "red" :background "black")))
"Special face used to dynamically add words to be highlighted.")
(set-face-foreground 'special-face "red")
;; 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))))))
;; f9 will add the symbol at point:
;;
(global-set-key [f9] 'add-special-at-point)
This doesn't work really well, because when I write a symbol that starts
with a dot:
.my_special_var
And put it in the list, then Emacs will treat the leading dot as
in a regexp, and here:
(my_special_var ...)
the opening parenthesis would be highlighted also.
This seems to be because replace-regexp-in-string is ignoring
the :literal argument (I'm guessing).
I have tried passing "t" for the "literal" argument in
replace-regexp-in-string, and tried several different amounts of
backslashes in the replacement string and found something funny:
(replace-regexp-in-string "\\." "\." "aaa . bbb" t t)
==> "aaa . bbb"
(replace-regexp-in-string "\\." "\\." "aaa . bbb" t t)
==> "aaa \\. bbb"
(replace-regexp-in-string "\\." "\\\." "aaa . bbb" t t)
==> "aaa \\. bbb"
(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?
Or, is there an easier way to replace the dots in a string so that
function will work properly?
Thanks a lot,
J.
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: elisp: Replacing dots in strings (. -> \.)
2007-07-08 11:10 elisp: Replacing dots in strings (. -> \.) Jeronimo Pellegrini
@ 2007-07-08 13:27 ` Nikolaj Schumacher
2007-07-08 15:07 ` Eric Hanchrow
2007-07-08 17:25 ` Jeronimo Pellegrini
0 siblings, 2 replies; 4+ messages in thread
From: Nikolaj Schumacher @ 2007-07-08 13:27 UTC (permalink / raw)
To: help-gnu-emacs; +Cc: Jeronimo Pellegrini
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
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: elisp: Replacing dots in strings (. -> \.)
2007-07-08 13:27 ` Nikolaj Schumacher
@ 2007-07-08 15:07 ` Eric Hanchrow
2007-07-08 17:25 ` Jeronimo Pellegrini
1 sibling, 0 replies; 4+ messages in thread
From: Eric Hanchrow @ 2007-07-08 15:07 UTC (permalink / raw)
To: help-gnu-emacs
Jeronimo Pellegrini <j_p@aleph0.info> wrote:
> (replace-regexp-in-string "\\." "\\\." "aaa . bbb" t t)
> ==> "aaa \\. bbb"
To preserve your sanity, and that of anyone who reads your code, I beg
you to use the fabulous "rx" package (it's already in your Emacs --
nothing to install!)
(replace-regexp-in-string (rx ".") "\\." "aaa . bbb" t t)
==> "aaa \\. bbb"
--
Покажи мне твой .emacs, и я скажу, кто ты.
-- Russian Proverb
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: elisp: Replacing dots in strings (. -> \.)
2007-07-08 13:27 ` Nikolaj Schumacher
2007-07-08 15:07 ` Eric Hanchrow
@ 2007-07-08 17:25 ` Jeronimo Pellegrini
1 sibling, 0 replies; 4+ messages in thread
From: Jeronimo Pellegrini @ 2007-07-08 17:25 UTC (permalink / raw)
To: help-gnu-emacs
On Sun, Jul 08, 2007 at 03:27:14PM +0200, Nikolaj Schumacher wrote:
> 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'?
Ouch! I read it several times and missed that.
> > 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.
Yes, that's OK -- I'll use this with Lisp mode.
> > (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
Ah, now I see what happened.
> > 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'.
And that definetely fixed it. Thanks!
> Also, you might want to check out this package of mine:
> http://nschum.de/src/emacs/highlight-symbol/
Thanks -- that's also very nice! I see it uses hi-lock, so I'll probably
keep my function until I find some time to switch to hi-lock (I use
font-lock currently).
J.
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2007-07-08 17:25 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-07-08 11:10 elisp: Replacing dots in strings (. -> \.) Jeronimo Pellegrini
2007-07-08 13:27 ` Nikolaj Schumacher
2007-07-08 15:07 ` Eric Hanchrow
2007-07-08 17:25 ` Jeronimo Pellegrini
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).