🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: gnu.emacs.help
Article #189298

Re: how do i set a variable to contents of a file?

#189298
From: Boaz Ben-Zvi
Date: Wed, 20 Mar 1991 19:42
70 lines
3090 bytes
In article <SHAWN.91Mar14112850@litsun.epfl.ch>, shawn@litsun.epfl.ch (Shawn Koppenhoefer) writes:
|> 
|> I need to set a variable in then gnuspost.el file to have the
|> value stored in a specific file. something like
|> 
|> (setq thisvariable (catfile "/u/user/joe/file"))
|> 
|> How do I do this?
|> --
|> 

TWO WAYS:
========
1. Well, the trick is to store it right in the file, like

(defun store-variable-in-file (variable variable-name file)
    "Store VARIABLE (whose name is VARIABLE-NAME (a string)) in FILE so that 
VARIABLE can be later set by executing (load FILE)"
    (let ((buf (generate-new-buffer "*var-file*"))
	  (var-file (expand-file-name file)))
	(save-excursion
	    (set-buffer buf)
	    (insert "(setq " variable-name " '")
	    (insert (prin1-to-string variable))
	    (insert ")\n")
	    (write-file var-file))
	(kill-buffer buf)))

and then to set the variable just do  (load "whatever-name-you-gave-it")

--------------------------------------------------------------------------

2. If you want the freedom to choose the name of the variable:

;; dump-variable.el
;;
;; dump-variable and load-variable are two functions that dump the
;; current value of a variable into a file and reload it respectively.
;; 
;; Version 1.0  Boaz Ben-Zvi  (boaz@mit.edu) May 11, 1990
;; 

(defvar dump-buff "*dump-var*") ;; name of temporary buffer

(defun dump-variable (var to-file)
    "Dump the contents of variable VAR into the file TO-FILE, to
be loaded later using load-variable"
    (let ((buff (generate-new-buffer dump-buff)))
	(save-excursion
	    (set-buffer buff)
	    (insert (prin1-to-string var))
	    (if (file-exists-p to-file) (delete-file to-file))
	    (write-file to-file)
	    (kill-buffer buff))))

(defun load-variable (var from-file)
    "Load a variable (must be quoted!) VAR from FROM-FILE, a file created by calling dump-variable"
    (let ((buff (generate-new-buffer dump-buff)))
	(save-excursion
	    (set-buffer buff)
	    (insert-file-contents from-file nil)
	    (set var (read (buffer-string)))
	    (kill-buffer buff))))

-- Boaz

========================================================
*   Boaz Ben-Zvi   (boaz@neutron.lcs.mit.edu)          *
*   MIT: NE43-526   Tel: (617) 253-6212                *
========================================================
Technical Details
Message-ID: <1991Mar20.194218.5015@mintaka.lcs.mit.edu>
Path: arc1-ca.pugleaf.net!gmdzi!unido!mcsun!uunet!stanford.edu!snorkelwacker.mit.edu!mintaka!CONCERTO.LCS.MIT.EDU!boaz
References: <SHAWN.91Mar14112850@litsun.epfl.ch>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 103b8b,4dc1adf2098ba0a4
X-Google-Attributes: gid103b8b,public
X-Google-ArrivalTime: 1991-03-21 01:15:26 PST
Path: arc1-ca.pugleaf.net!gmdzi!unido!mcsun!uunet!stanford.edu!snorkelwacker.mit.edu!mintaka!CONCERTO.LCS.MIT.EDU!boaz
From: boaz@CONCERTO.LCS.MIT.EDU (Boaz Ben-Zvi)
Newsgroups: gnu.emacs.help
Subject: Re: how do i set a variable to contents of a file?
Message-ID: <1991Mar20.194218.5015@mintaka.lcs.mit.edu>
Date: 20 Mar 91 19:42:18 GMT
References: <SHAWN.91Mar14112850@litsun.epfl.ch>
Sender: daemon@mintaka.lcs.mit.edu (Lucifer Maleficius)
Reply-To: boaz@lcs.mit.edu
Distribution: gnu
Organization: Laboratory for Computer Science, MIT
Lines: 70