[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: avoid interpretation of \n, \t, ... in string
From: |
Pascal J. Bourguignon |
Subject: |
Re: avoid interpretation of \n, \t, ... in string |
Date: |
Wed, 28 Jan 2009 10:59:33 +0100 |
User-agent: |
Gnus/5.1008 (Gnus v5.10.8) Emacs/22.2 (gnu/linux) |
Peter Tury <tury.peter@gmail.com> writes:
> I would like to pass paths to shell (extrenal command line programs)
> on MS Windows. The paths may contain \n, \t etc. (Eg. c:
> \directory-1\new-dir\temp...). However, the string is "evaluated" and
> only the result arrives to the shell program. (In the above example: c:
> \directory-1
> ew-dir emp...)
>
> I try to use `call-process-shell-command'.
>
> I know I could use double back-slash (e.g. c:\directory-1\\new-dir\
> \temp...), but I want to be able to handle any paths in their
> "natural" form. How to do it?
Switch to Common Lisp. There's no reader macro in emacs lisp, so you
cannot do much about it. In Common Lisp, you can trivially implement
a reader macro to read strings with no, or with a different escape
character. And there are also various "emacsen" written in Common
Lisp ;-)
Ok, another way to do it would be to store your paths in a file, and
to read it:
(defun read-paths (file)
(with-temp-buffer
(insert-file-contents file)
(delete "" (split-string (buffer-substring-no-properties
(point-min) (point-max))
"[\n\r]+"))))
Then with a file dirs.txt containing:
c:\test\directory\file.txt
D:\ANOTHER\DIRECTORY\FILE.TXT
RELATIVE\DIRECTORY\FILE.TXT
(read-paths "dirs.txt")
--> ("c:\\test\\directory\\file.txt" "D:\\ANOTHER\\DIRECTORY\\FILE.TXT"
"RELATIVE\\DIRECTORY\\FILE.TXT")
Note that this is a serrious proposition. Myself, I use a
directories.txt file containing keyed paths:
KEY /some/dir
OTHER_KEY /some/other/dir
that I read with:
(defvar *directories* '())
(defun get-directory (key &optional subpath)
(setf subpath (or subpath ""))
(unless (getf *directories* key)
(error "get-directory: No directory keyed %s" key))
(concat (getf *directories* key) subpath))
(defun load-directories ()
(interactive)
(setf *directories*
(progn
(find-file "~/directories.txt")
(prog1
(loop
for (k v)
on (split-string (buffer-substring-no-properties
(point-min) (point-max)))
by (function cddr)
nconc (list (intern
(format ":%s"
(substitute ?- ?_ (downcase k))))
v))
(kill-buffer (current-buffer))))))
(load-directories)
in emacs lisp, and something similar in Common Lisp, and with
'~/bin/get-directory':
#!/bin/bash
awk '/^ *'"$1"' */{print $2}' ~/directories.txt
in shell:
get-directory HYPERSPEC
--> /usr/share/doc/hyperspec/HyperSpec/
so I can write all my scripts, whatever the language, independently of
any directory location, and thus they can run identically on the
various systems I use (each having its own ~/directories.txt file with
its specific directories).
--
__Pascal Bourguignon__
- avoid interpretation of \n, \t, ... in string, Peter Tury, 2009/01/27
- Re: avoid interpretation of \n, \t, ... in string, Kevin Rodgers, 2009/01/28
- Re: avoid interpretation of \n, \t, ... in string,
Pascal J. Bourguignon <=
- Message not available