From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: David Kastrup Newsgroups: gmane.emacs.devel Subject: Re: format-quote? Date: Thu, 03 Aug 2006 20:48:41 +0200 Message-ID: <85fygdmz92.fsf@lola.goethe.zz> References: NNTP-Posting-Host: main.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: sea.gmane.org 1154630955 11517 80.91.229.2 (3 Aug 2006 18:49:15 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Thu, 3 Aug 2006 18:49:15 +0000 (UTC) Cc: Emacs-Devel Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Thu Aug 03 20:49:13 2006 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by ciao.gmane.org with esmtp (Exim 4.43) id 1G8iFk-0001Af-Py for ged-emacs-devel@m.gmane.org; Thu, 03 Aug 2006 20:49:05 +0200 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1G8iFk-00009L-24 for ged-emacs-devel@m.gmane.org; Thu, 03 Aug 2006 14:49:04 -0400 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1G8iFZ-000082-9E for emacs-devel@gnu.org; Thu, 03 Aug 2006 14:48:53 -0400 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1G8iFY-00007D-KF for emacs-devel@gnu.org; Thu, 03 Aug 2006 14:48:52 -0400 Original-Received: from [199.232.76.173] (helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1G8iFY-00006v-G9 for emacs-devel@gnu.org; Thu, 03 Aug 2006 14:48:52 -0400 Original-Received: from [199.232.76.164] (helo=fencepost.gnu.org) by monty-python.gnu.org with esmtp (Exim 4.52) id 1G8iIw-0008G5-Fb for emacs-devel@gnu.org; Thu, 03 Aug 2006 14:52:22 -0400 Original-Received: from localhost ([127.0.0.1] helo=lola.goethe.zz) by fencepost.gnu.org with esmtp (Exim 4.34) id 1G8iFW-0002Au-8a; Thu, 03 Aug 2006 14:48:50 -0400 Original-Received: by lola.goethe.zz (Postfix, from userid 1002) id 4C7521C39509; Thu, 3 Aug 2006 20:48:41 +0200 (CEST) Original-To: "Drew Adams" In-Reply-To: (Drew Adams's message of "Thu, 3 Aug 2006 11:25:09 -0700") User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.0.50 (gnu/linux) 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:58056 Archived-At: "Drew Adams" writes: > The first arg to `format' is a format string. In some contexts, it happens > that the string is the only argument used, it should be interpreted > literally, and it might not be known ahead of time. > > For example, if you use (error (error-message-string foo)), it's possible > that `error-message-string' will return something like this: "No match for > regexp `^[^#$%>\n]*[#$%>] *\\S-.*'". Calling `error' on this directly would > result in the (derivative) error "Not enough arguments for format string". > > AFAIK, there is no way to let `format' (and `error' etc.) know that you want > the format string to be interpreted literally, so (in the use case > mentioned) the `%'s need to be escaped before passing the string to > `format', in order to show the original error message. That is: "No match > for regexp `^[^#$%%>\n]*[#$%%>] *\\S-.*'" (error "%s" (error-message-string foo)) > This escaping is easy, but having a function that does only that might be > helpful, in terms of making people aware of this possible gotcha. > > (defun format-quote (string) > "Quote STRING, so it becomes a literal format string. > This just escapes each `%' in STRING." > (replace-regexp-in-string "%" "%%" string t t)) > > Just as reading about `regexp-quote' can draw attention to the > gotcha of forgetting to quote a regexp for literal regexp matching, > reading about `format-quote' might draw attention to the gotcha of > forgetting to quote a format string that should be treated literally > in some context. > > WDOT? Would it be useful to add a `format-quote' function? No, because the format "%s" does the trick perfectly well. The question is how to convey this better if you find it confusing. And a function format-quote would be quite the wrong choice, not least because it is quite more inefficient than using "%s". -- David Kastrup, Kriemhildstr. 15, 44793 Bochum