* format-quote?
@ 2006-08-03 18:25 Drew Adams
2006-08-03 18:48 ` format-quote? David Kastrup
2006-08-04 0:34 ` format-quote? Richard Stallman
0 siblings, 2 replies; 5+ messages in thread
From: Drew Adams @ 2006-08-03 18:25 UTC (permalink / raw)
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-.*'"
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?
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: format-quote?
2006-08-03 18:25 format-quote? Drew Adams
@ 2006-08-03 18:48 ` David Kastrup
2006-08-03 20:50 ` format-quote? Drew Adams
2006-08-04 0:34 ` format-quote? Richard Stallman
1 sibling, 1 reply; 5+ messages in thread
From: David Kastrup @ 2006-08-03 18:48 UTC (permalink / raw)
Cc: Emacs-Devel
"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
^ permalink raw reply [flat|nested] 5+ messages in thread
* RE: format-quote?
2006-08-03 18:48 ` format-quote? David Kastrup
@ 2006-08-03 20:50 ` Drew Adams
2006-08-04 10:42 ` format-quote? Eli Zaretskii
0 siblings, 1 reply; 5+ messages in thread
From: Drew Adams @ 2006-08-03 20:50 UTC (permalink / raw)
> AFAIK, there is no way to let `format' (and `error' etc.)
> know that you want the format string to be interpreted literally
(error "%s" (error-message-string foo))
Ah, yes; thanks.
How about mentioning this idiom where `format' is documented? The doc for
other functions (such as `message' and `error') that use formatting strings
probably already cross-references the `format' doc.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: format-quote?
2006-08-03 18:25 format-quote? Drew Adams
2006-08-03 18:48 ` format-quote? David Kastrup
@ 2006-08-04 0:34 ` Richard Stallman
1 sibling, 0 replies; 5+ messages in thread
From: Richard Stallman @ 2006-08-04 0:34 UTC (permalink / raw)
Cc: emacs-devel
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".
You should write (error "%s" (error-message-string foo)).
Are there places in Emacs now that get this wrong?
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2006-08-04 10:42 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-08-03 18:25 format-quote? Drew Adams
2006-08-03 18:48 ` format-quote? David Kastrup
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
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.