* 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 Dump file content into a variable, how to? 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?
[not found] <mailman.2375.1182241055.32220.help-gnu-emacs@gnu.org>
@ 2007-06-19 9:23 ` Thien-Thi Nguyen
0 siblings, 0 replies; 5+ messages in thread
From: Thien-Thi Nguyen @ 2007-06-19 9:23 UTC (permalink / raw)
To: help-gnu-emacs
() Ismael Valladolid Torres <ivalladt@punkass.com>
() Tue, 19 Jun 2007 10:17:28 +0200
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.
C-h f shell-command TAB TAB
thi
^ 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
* Re: Dump file content into a variable, how to?
2007-06-19 11:20 ` Ismael Valladolid Torres
@ 2007-06-19 11:48 ` Juanma Barranquero
0 siblings, 0 replies; 5+ messages in thread
From: Juanma Barranquero @ 2007-06-19 11:48 UTC (permalink / raw)
To: Ismael Valladolid Torres; +Cc: help-gnu-emacs
On 6/19/07, Ismael Valladolid Torres <ivalladt@punkass.com> wrote:
> Code below did it:
I'd wrap it in the `condition-case' and all the paraphernalia, just to
be on the safe side.
> Gracias. ;)
Un placer,
Juanma
^ 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 --
2007-06-19 8:17 Dump file content into a variable, how to? 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
[not found] <mailman.2375.1182241055.32220.help-gnu-emacs@gnu.org>
2007-06-19 9:23 ` Thien-Thi Nguyen
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.