all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: pjb@informatimago.com (Pascal J. Bourguignon)
To: help-gnu-emacs@gnu.org
Subject: Re: avoid interpretation of \n, \t, ... in string
Date: Wed, 28 Jan 2009 10:59:33 +0100	[thread overview]
Message-ID: <7c7i4fkayi.fsf@pbourguignon.anevia.com> (raw)
In-Reply-To: 665cbb9b-8140-489e-a4d8-a15acce224be@r37g2000prr.googlegroups.com

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__


  parent reply	other threads:[~2009-01-28  9:59 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-01-27 17:13 avoid interpretation of \n, \t, ... in string Peter Tury
2009-01-27 18:26 ` Peter Dyballa
     [not found] ` <mailman.5991.1233080786.26697.help-gnu-emacs@gnu.org>
2009-01-27 18:38   ` Peter Tury
2009-01-28  7:52 ` Kevin Rodgers
2009-01-28  9:59 ` Pascal J. Bourguignon [this message]
2009-01-28 12:24   ` Peter Tury
2009-01-28 14:02     ` Pascal J. Bourguignon
     [not found] ` <mailman.6050.1233129149.26697.help-gnu-emacs@gnu.org>
2009-01-28 10:16   ` Peter Tury

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=7c7i4fkayi.fsf@pbourguignon.anevia.com \
    --to=pjb@informatimago.com \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.