unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* 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

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 public inbox

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).