Learning Elisp 2 - variables
I was planning on writing this yesterday but caught up in watching "Paths of Glory."
The plan was to have each topic revolve around a "real" project but I realized that first we have to cover some basics. Specifically, variables and functions. I was going to cover them together but the video was getting a little long so we're doing variables here and writing functions next time.
Like other languages, Emacs uses variables to store values. Also, like other, or at least some other languages, there are a number of subtleties and variations.
We're keeping things simple for now and will dive deeper as needed in the future.
To create a variable in Emacs we use the special form defvar
. The
defvar
form defines a symbol as a variable - (defvar name)
or,
with an initial value (defvar name "Tom")
. It turns out, that you
can keep using defvar
to change values in a variable:
(defvar name)
(defvar name "Tom")
(defvar name "Tim")
I'm unsure of the internal ramifications of this but that's not how
we're supposed to do things. To change (or set) variables, we're
supposed to use setq
- (setq name "Tammy")
for instance. Of
course, to make things less simple, we can use setq
without first
using defvar
.
Now, defvar
in our case, creates global variables but sometimes we
want something more locally scoped. For that we use the let
form. In
the following example, we create a "global" variable name with
defvar
and then a local one with let
:
(defvar name "Global Gabe")
(print name) ;; prints Global Gabe
(let ( (name "Local Larry") )
(print name) ;; prints Local Larry
(setq name "Local Lisa")
(print name) ;; prints Local Lisa - we changed the local one
)
(print name) ;; but Global Gabe was unchanged
The video goes over the details and a few more nuances.
Video Link: https://www.youtube.com/watch?v=eQNqIsyw1mo