emacs-devel
[Top][All Lists]
Advanced

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

format-quote?


From: Drew Adams
Subject: format-quote?
Date: Thu, 3 Aug 2006 11:25:09 -0700

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?





reply via email to

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