unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Jeronimo Pellegrini <j_p@aleph0.info>
To: help-gnu-emacs@gnu.org
Subject: elisp: Replacing dots in strings (. -> \.)
Date: Sun, 8 Jul 2007 08:10:51 -0300	[thread overview]
Message-ID: <20070708111051.GA15999@randomnode.info> (raw)

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.

             reply	other threads:[~2007-07-08 11:10 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-07-08 11:10 Jeronimo Pellegrini [this message]
2007-07-08 13:27 ` elisp: Replacing dots in strings (. -> \.) Nikolaj Schumacher
2007-07-08 15:07   ` Eric Hanchrow
2007-07-08 17:25   ` Jeronimo Pellegrini

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20070708111051.GA15999@randomnode.info \
    --to=j_p@aleph0.info \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).