help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Dump file content into a variable, how to?


From: Juanma Barranquero
Subject: Re: Dump file content into a variable, how to?
Date: Tue, 19 Jun 2007 11:16:27 +0200

On 6/19/07, Ismael Valladolid Torres <ivalladt@punkass.com> wrote:

I want my .emacs to dired on launch a given directory. The name of the
directory is stored into a file ~/.removable. So I need to "cat" the
file content into a variable, then maybe do a file-directory-p and
finally dired the directory.

A simple way is

 (with-temp-buffer
    (insert-file-contents "your-file"
    (setq your-variable (buffer-string))
    ;;; your code here
    )

If you need the literal contents of the file you can use
`insert-file-contents-literally' instead. There are a few more tricks,
but depends on what you intend to do.

Here's a function to do what you requested. You may call it
interactively or just pass the filename as argument, like this

 (dired-from-file "~/.removable")

or even

 (dired-from-file ".removable")

(it defaults to ~/). It's more complex than strictly necessary because
I've opted to program quite defensively.

(defun dired-from-file (file)
 "Ejecuta `dired' en un directorio extraido de FILE."
 (interactive "f")
 (condition-case nil
     (with-temp-buffer
       (insert-file-contents (expand-file-name file "~/"))
       (let ((dir (convert-standard-filename
                   (replace-regexp-in-string "\n$" "" (buffer-string)))))
         (if (file-accessible-directory-p (directory-file-name dir))
             (dired dir)
           (message "%S no accesible" dir))))
   (error (message "Error leyendo %S" file))))

Good luck,
            Juanma




reply via email to

[Prev in Thread] Current Thread [Next in Thread]