joostkremers / parsebib

Elisp library for reading .bib files
BSD 3-Clause "New" or "Revised" License
35 stars 9 forks source link

Unexpected escape characters #4

Closed tmalsburg closed 8 years ago

tmalsburg commented 8 years ago

When I read an entry that contains a backslash in its title (e.g., $\phi$), parsebib-read-entry returns a title in which the backslash is duplicated ($\\phi$). Is this intentional or a bug? In my case, it causes problems because I have to reconstruct what the original string was. (Context: tmalsburg/helm-bibtex#83)

joostkremers commented 8 years ago

Actually, the string that parsebib-read-entry returns contains only one backslash, but when that string is displayed as a Lisp object, the backslash needs to be escaped, so \phi appears as "\\phi".

The error "Invalid use of \' in replacement text" also indicates this. Somewhere (I suspect ins-format), the string\phiis used as the replacement text inreplace-regexp-in-stringor a similar function, but this replacement string cannot contain\p`. Cf. the following code example (note that the backslash needs to be escaped, so it appears twice, but there's really only one backslash in the string):

(replace-regexp-in-string "${title}" "\\phi" "This is the title: ${title}")

will result in the error:

"Invalid use of `\' in replacement text"

I suspect that a properly placed regexp-quote will solve your problem:

(replace-regexp-in-string "${title}" (regexp-quote "\\phi") "This is the title: ${title}")

will return the Lisp value:

"This is the title: \\phi"

which, when inserted into a buffer, displays as:

This is the title: \phi
tmalsburg commented 8 years ago

Ah, thanks for reminding me, Joost! I thought about this possibility but got confused by something. regexp-quote does not work because it causes other problems but a simple (s-replace "\\" "\\\\" "\\phi") does the trick.