[Top][All Lists]

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

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

From: Ismael Valladolid Torres
Subject: Re: Dump file content into a variable, how to?
Date: Tue, 19 Jun 2007 13:20:16 +0200
User-agent: Mutt/1.5.13 (2006-08-11)

Juanma Barranquero escribe:
> On 6/19/07, Ismael Valladolid Torres <address@hidden> 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))))

Code below did it:

(when (file-readable-p (expand-file-name "~/.removable"))
    (insert-file-contents (expand-file-name "~/.removable"))
    (setq doc (concat (replace-regexp-in-string "\n$" "" (buffer-string)) 
    (dired doc)))

Gracias. ;)

Cordially, Ismael
Ismael Valladolid Torres  m. +34679156321
La media hostia           j. address@hidden

reply via email to

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