all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: David Kastrup <dak@gnu.org>
Cc: Emacs-Devel <emacs-devel@gnu.org>
Subject: Re: format-quote?
Date: Thu, 03 Aug 2006 20:48:41 +0200	[thread overview]
Message-ID: <85fygdmz92.fsf@lola.goethe.zz> (raw)
In-Reply-To: <MEEKKIABFKKDFJMPIOEBCEOBDDAA.drew.adams@oracle.com> (Drew Adams's message of "Thu, 3 Aug 2006 11:25:09 -0700")

"Drew Adams" <drew.adams@oracle.com> 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

  reply	other threads:[~2006-08-03 18:48 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-08-03 18:25 format-quote? Drew Adams
2006-08-03 18:48 ` David Kastrup [this message]
2006-08-03 20:50   ` format-quote? Drew Adams
2006-08-04 10:42     ` format-quote? Eli Zaretskii
2006-08-04  0:34 ` format-quote? Richard Stallman

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

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

  git send-email \
    --in-reply-to=85fygdmz92.fsf@lola.goethe.zz \
    --to=dak@gnu.org \
    --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 external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.