I'm trying to insert a string pathname into a buffer on a Windows machine. The variable holding the string has the right number of backslashes (two) in the directory pathname. I know this because when I "Eval: mypathname" in the minibuffer, the double backslashes are visible.
BUT.. it seems that whenever Emacs evaluates the pathname variable in a function (such as concat, or insert-string), one level of backslashes is stripped out. So what starts out as C:\good\path\slashes in the variable turns into "C:\bad\path\slashes" in the inserted (or concatenated) string in the buffer.
I wrote a small function to try to double the slashes, but all manner of regexp expressions failed for me. I tried doubles, and quadruples, rx-to-string, etc.
Here is my code and a debugger trace and execution trace taken from running the function in a scratch lisp-interaction-mode buffer.
Notice that when the variable value is returned by the function, it still has the right number of slashes. But when I try to use the variable anywhere (in a concat, or in the insert-string calls in the trace, only one slash shows up. And I need TWO slashes to show up in the inserted string.
Any ideas would be appreciated. Thank you
(defun ct-u-path-redouble (path)
;;; This line gives an error:
;;; (setq foo (replace-regexp-in-string "\\" "\\\\" path))
;;;
;;; Debugger entered--Lisp error: (invalid-regexp "Trailing backslash")
;;; string-match("\\" "C:\\joe\\blow" 0)
;;; replace-regexp-in-string("\\" "\\\\" "C:\\joe\\blow")
;;; (setq foo (replace-regexp-in-string "\\" "\\\\" path))
;;; ct-u-path-redouble("C:\\joe\\blow")
;;; eval((ct-u-path-redouble "C:\\joe\\blow") nil)
(insert-string "\n")
(insert-string (concat "\nAfter concat: " path "\n"))
path)
(ct-u-path-redouble "C:\joe\blow") After concat: C:joelow (the weird one-slashified ctl chars don't show here) Returned ==> "C:joelow"
(ct-u-path-redouble "C:\\joe\\blow") After concat: C:\joe\blow Returned ==> "C:\\joe\\blow"
C:\joe\blow
? If so, use this:(replace-regexp-in-string "\\\\" "\\\\" "C:\\joe\\blow")
.c:/joe/blow
. No need to fiddle with backslashes for Windows file syntax - unless you need that for use ultimately outside Emacs. And in that case you can reintroduce it at the end, when you really need it. In the meantime, i.e., within Emacs, just stick with /, not \.