emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: format-quote?


From: David Kastrup
Subject: Re: format-quote?
Date: Thu, 03 Aug 2006 20:48:41 +0200
User-agent: Gnus/5.11 (Gnus v5.11) Emacs/22.0.50 (gnu/linux)

"Drew Adams" <address@hidden> 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 via email to

[Prev in Thread] Current Thread [Next in Thread]