From: Stefan Monnier <monnier@iro.umontreal.ca>
To: Daniel Colascione <danc@merrillpress.com>
Cc: Emacs-Devel devel <emacs-devel@gnu.org>
Subject: Re: [PATCH] xml-escape-region
Date: Thu, 08 Oct 2009 01:29:33 -0400 [thread overview]
Message-ID: <jwv1vlea2eq.fsf-monnier+emacs@gnu.org> (raw)
In-Reply-To: <18A0FD1E-DAFE-4058-B6FC-630750EBBCEA@merrillpress.com> (Daniel Colascione's message of "Wed, 7 Oct 2009 22:13:27 -0400")
>>> +;;;##autoload
>>> +(defun xml-escape-region (beg end)
>>> + (interactive "*r")
>>> + (let ((escaped (xml-escape-string (buffer-substring beg end))))
>>> + (delete-region beg end)
>>> + (insert escaped)))
>>
>> I'd rather not autoload such a function.
> Do you mean that it should be loaded all the time, or that the user should
> have to explicitly load xml.el before using the function?
Yes.
> If the latter, then that would make binding it to a key
> less convenient.
Hmm... didn't notice you defined it as a command. How often/when do you
need to use/bind such a command other than in an sgml/xml-related file
(where the major mode might decide to preload such a command)?
>> But more importantly, this implementation is very inefficient.
>> xml-escape-string itself is rather inefficient except for short
>> strings; this is OK for its current uses, but for xml-escape-region
>> it's definitely not good (i.e. only usable for small regions).
> How's this? It's O(N) in the amount of text escaped.
Much better, thank you.
> (let ((search-re (mapconcat #'regexp-quote
> (mapcar #'cdr xml-entity-alist)
> "\\|"))
Rather than a big \| of single chars, why not make a [...] regexp?
If you use regexp-opt, it should happen automatically.
Actually, now that I look at it, xml-entity-alist is poorly defined.
Instead of being a list of pairs of string and string (where the second
string is always of size 1), it should be a list of pairs of string
and char. Also this code is also applicable to sgml and there's related
code in sgml-mode.el. If someone wants to consolidate, that would
be welcome.
> (save-excursion
> (goto-char beg)
> (while (re-search-forward search-re end t)
> (replace-match (concat "&"
> (car (rassoc (match-string 0)
> xml-entity-alist))
> ";"))))))
If you use a backward-search, you don't need to turn `end' (nor `start')
into a marker.
Stefan
next prev parent reply other threads:[~2009-10-08 5:29 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-10-07 18:56 [PATCH] xml-escape-region Daniel Colascione
2009-10-07 22:10 ` Stefan Monnier
2009-10-08 2:13 ` Daniel Colascione
2009-10-08 5:29 ` Stefan Monnier [this message]
2009-10-08 6:01 ` Daniel Colascione
2009-10-08 6:03 ` Daniel Colascione
2009-10-09 19:11 ` Stefan Monnier
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=jwv1vlea2eq.fsf-monnier+emacs@gnu.org \
--to=monnier@iro.umontreal.ca \
--cc=danc@merrillpress.com \
--cc=emacs-devel@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.
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/emacs.git
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).