[comp.emacs] accessing environment variables in Lisp expressions

tsilva%aaec1.UUCP@DSPVAX.MIT.EDU (Tony Silva) (05/03/91)

After putting:

    (setq mail-archive-file-name "/u/tsilva/SMAIL.tmp")

in my ~/.emacs file, I can successfully send a message (to myself)
with a copy written to SMAIL.tmp in my home directory:

    To: tsilva
    Subject: test
    FCC: /u/tsilva/SMAIL.tmp
    --text follows this line--
    using /u/tsilva/SMAIL.tmp

However, I don't like hardwiring the directory name "/u/tsilva" in the
Lisp expression above.  I can't seem to figure out how to utilize the
$HOME environment variable. I tried:

    (setq mail-archive-file-name "$HOME/SMAIL.tmp")

and then

    To: tsilva
    Subject: test
    FCC: $HOME/SMAIL.tmp
    --text follows this line--
    using $HOME/SMAIL.tmp

which failed with the following message:

    Opening output file: no such file or directory, /u/tsilva/$HOME/SMAIL.tmp

The expression:

    (setq mail-archive-file-name "/$HOME/SMAIL.tmp")

also failed. I'm sure there's a simple way to use $HOME. Any hints?
Thanks in advance.

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

Tony Silva
Atlantic Aerospace Electronics Corp.
470 Totten Pond Road
Waltham, MA 02154

voice:    (617)890-4200
FAX:      (617)890-0224
Internet: tsilva%aaec1.uucp@dspvax.mit.edu
UUCP:     ...!seismo!dspvax!aaec1!tsilva

tsilva%aaec1.UUCP@DSPVAX.MIT.EDU (Tony Silva) (05/07/91)

I received several excellent suggestions for avoiding hardwiring home
directory names, etc., in my ~/.emacs file, as in:

    (setq mail-archive-file-name "/u/tsilva/SMAIL")

The key Lisp functions are "substitute-in-file-name",
"expand-file-name", and (I'm embarrassed to say I never thought of
this one) "getenv". (I had tried using "command-apropos" and searching
through the Emacs topics using Info Reader, but apparently didn't try
hard enough.). Any of the following can be used in place of the
expression above:

    (setq mail-archive-file-name (substitute-in-file-name "$HOME/SMAIL"))
    (setq mail-archive-file-name (expand-file-name "~/SMAIL"))
    (setq mail-archive-file-name (concat (getenv "HOME") "/SMAIL"))

I'm particularly fond of "expand-file-name" since the csh-like tilde
expansion permits easy reference of other users' home directories, as
well.

Another useful tidbit that was mentioned was:

(setq host-name (substring (system-name) 0 (string-match "\\." (system-name))))

to yield the base portion of the current system name, ultimately for
invoking host-specific code.

Many thanks to all those who took the time to respond:
haydens%bullwinkle@xn.ll.mit.edu (Hayden Schultz)
rms@gnu.ai.mit.edu (Richard Stallman)
raymond@math.berkeley.edu (Raymond Chen)
Paul Palmer <palmerp@MATH.ORST.EDU>
rsw@cs.brown.edu (Bob Weiner)
eirik@theory.TN.CORNELL.EDU (Eirik Fuller)
mittal@ISI.EDU
Hamish Macdonald <himacdon@maytag.waterloo.edu>
Jim Hudgens <hudgens@sun13.scri.fsu.edu>
brookler@lentil.Berkeley.EDU (David Brookler)
Scott Brim <swb@nr-tech.cit.cornell.edu>
jimb@gnu.ai.mit.edu (Jim Blandy)
Olaf Bigalk <bigalk@opal.cs.tu-berlin.de>
Tim Bradshaw <tim@cstr.edinburgh.ac.uk>
spencer@spline.eecs.umich.edu
Scott Blachowicz <hp.com!hpubvwa.nsr.hp.com!grlab!scott@relay.MIT.EDU>
mark3@mtfme.att.com (Mark Hord)
mike-w@cs.aukuni.ac.nz