From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Stefan Monnier Newsgroups: gmane.emacs.devel Subject: Re: [PATCH] xml-escape-region Date: Thu, 08 Oct 2009 01:29:33 -0400 Message-ID: References: <200910071456.31966.danc@merrillprint.com> <18A0FD1E-DAFE-4058-B6FC-630750EBBCEA@merrillpress.com> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: ger.gmane.org 1254979816 7633 80.91.229.12 (8 Oct 2009 05:30:16 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Thu, 8 Oct 2009 05:30:16 +0000 (UTC) Cc: Emacs-Devel devel To: Daniel Colascione Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Thu Oct 08 07:30:06 2009 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1MvlZk-00088v-Qz for ged-emacs-devel@m.gmane.org; Thu, 08 Oct 2009 07:30:05 +0200 Original-Received: from localhost ([127.0.0.1]:36087 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1MvlZk-0007fg-Cy for ged-emacs-devel@m.gmane.org; Thu, 08 Oct 2009 01:30:04 -0400 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1MvlZN-0007VX-F2 for emacs-devel@gnu.org; Thu, 08 Oct 2009 01:29:41 -0400 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1MvlZI-0007SI-8Y for emacs-devel@gnu.org; Thu, 08 Oct 2009 01:29:40 -0400 Original-Received: from [199.232.76.173] (port=45159 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1MvlZH-0007S7-Lm for emacs-devel@gnu.org; Thu, 08 Oct 2009 01:29:36 -0400 Original-Received: from ironport2-out.teksavvy.com ([206.248.154.181]:5574 helo=ironport2-out.pppoe.ca) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1MvlZH-0005Mq-7k for emacs-devel@gnu.org; Thu, 08 Oct 2009 01:29:35 -0400 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: Aq0EAPMVzUpMCqeU/2dsb2JhbACBUtZthCoEhzg X-IronPort-AV: E=Sophos;i="4.44,523,1249272000"; d="scan'208";a="47279069" Original-Received: from 76-10-167-148.dsl.teksavvy.com (HELO ceviche.home) ([76.10.167.148]) by ironport2-out.pppoe.ca with ESMTP; 08 Oct 2009 01:29:33 -0400 Original-Received: by ceviche.home (Postfix, from userid 20848) id AD358B4190; Thu, 8 Oct 2009 01:29:33 -0400 (EDT) In-Reply-To: <18A0FD1E-DAFE-4058-B6FC-630750EBBCEA@merrillpress.com> (Daniel Colascione's message of "Wed, 7 Oct 2009 22:13:27 -0400") User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1.50 (gnu/linux) X-detected-operating-system: by monty-python.gnu.org: Genre and OS details not recognized. X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.devel:115978 Archived-At: >>> +;;;##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