* Dump file content into a variable, how to?
@ 2007-06-19 8:17 Ismael Valladolid Torres
2007-06-19 9:16 ` Juanma Barranquero
0 siblings, 1 reply; 5+ messages in thread
From: Ismael Valladolid Torres @ 2007-06-19 8:17 UTC (permalink / raw)
To: help-gnu-emacs
[-- Attachment #1.1: Type: text/plain, Size: 679 bytes --]
A hopefully obvious question from a probably Lisp newbie.
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.
But I don't know how to "cat" a file content into a
variable. shell-command will be no useful as I don't need the "cat"
output in the echo area nor in another buffer, I need it into a
variable.
Any ideas welcome.
Cordially, Ismael
--
Ismael Valladolid Torres m. +34679156321
La media hostia j. ivalladt@gmail.com
http://lamediahostia.blogspot.com/
[-- Attachment #1.2: Digital signature --]
[-- Type: application/pgp-signature, Size: 189 bytes --]
[-- Attachment #2: Type: text/plain, Size: 152 bytes --]
_______________________________________________
help-gnu-emacs mailing list
help-gnu-emacs@gnu.org
http://lists.gnu.org/mailman/listinfo/help-gnu-emacs
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Dump file content into a variable, how to?
2007-06-19 8:17 Ismael Valladolid Torres
@ 2007-06-19 9:16 ` Juanma Barranquero
2007-06-19 11:20 ` Ismael Valladolid Torres
0 siblings, 1 reply; 5+ messages in thread
From: Juanma Barranquero @ 2007-06-19 9:16 UTC (permalink / raw)
To: Ismael Valladolid Torres; +Cc: help-gnu-emacs
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
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Dump file content into a variable, how to?
2007-06-19 9:16 ` Juanma Barranquero
@ 2007-06-19 11:20 ` Ismael Valladolid Torres
2007-06-19 11:48 ` Juanma Barranquero
0 siblings, 1 reply; 5+ messages in thread
From: Ismael Valladolid Torres @ 2007-06-19 11:20 UTC (permalink / raw)
To: Juanma Barranquero; +Cc: help-gnu-emacs
Juanma Barranquero escribe:
> 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))))
>
Code below did it:
(when (file-readable-p (expand-file-name "~/.removable"))
(with-temp-buffer
(insert-file-contents (expand-file-name "~/.removable"))
(setq doc (concat (replace-regexp-in-string "\n$" "" (buffer-string)) "/share/doc"))
(dired doc)))
Gracias. ;)
Cordially, Ismael
--
Ismael Valladolid Torres m. +34679156321
La media hostia j. ivalladt@gmail.com
http://lamediahostia.blogspot.com/
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2007-06-19 11:48 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
[not found] <mailman.2375.1182241055.32220.help-gnu-emacs@gnu.org>
2007-06-19 9:23 ` Dump file content into a variable, how to? Thien-Thi Nguyen
2007-06-19 8:17 Ismael Valladolid Torres
2007-06-19 9:16 ` Juanma Barranquero
2007-06-19 11:20 ` Ismael Valladolid Torres
2007-06-19 11:48 ` Juanma Barranquero
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).