[lug] XEmacs quoting madness!

Tkil tkil at scrye.com
Mon Aug 20 12:03:40 MDT 2001


>>>>> "Chris" == Chris Riddoch <socket at peakpeak.com> writes:

Chris> In XEmacs, I'm trying to make a string with a backslash before
Chris> single quote.  I want the output string to be this: "Can\'t"
Chris> because the output will be passed as a string value to another
Chris> program (thus needing quoting).

i think you might be confused by how you're *viewing* the output
string.  take a look at the two functions "princ" and "prin1", and
you'll understand what i'm talking about:

| (prin1 "Can\\'t\n")
| "Can\\'t
| ""Can\\'t
| "
| 
| (princ "Can\\'t\n")
| Can\'t
| "Can\\'t
| "

note that the actual value (as printed out by `princ') has only one
backslash in it -- but the value returned to the user is one that the
lisp reader could read back in, thus it has two backslashes.

so, if you are just using a static string, you do want two
backslashes.

if you're doing anything with regexps, you have to remember that
backslashes are special inside emacs' (and most other) regexps.  so,
you want the "true value" to be a doubled backslash -- the backslash
is the quote/escape character, so you need to quote/escape it to get a
literal backslash.  so you end up doing things like this:

  (while (re-search-forward "Can\\\\'t" (point-max) t)
     ...)

just as an aside, this is a place where you really start noticing the
tradeoffs between regular, consistent, easy-to-parse languages (like
lisp, python, tcl) and nasty, irregular, special-case-all-over-the-
place languages (like perl).

t.




More information about the LUG mailing list