Skip to content

Commit

Permalink
Partial commit of lesson 5-3 on local and global scope
Browse files Browse the repository at this point in the history
This lesson would be easier to write if I had a bloody clue about
eLisp scope :(

That would be why I am writing the damn book I suppose...
  • Loading branch information
hypernumbers committed Jan 24, 2011
1 parent 9c49ea7 commit 82fbf97
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
72 changes: 72 additions & 0 deletions contents/lesson-5-3-scope-of-variables-set-setq-let-etc.rst
@@ -0,0 +1,72 @@
====================================================
Lesson 5-3 - Scope Of Variables (set, setq, let etc)
====================================================

We have seen that eLisp has some variables that *appear* global in scope. We looked at the variable ``fill-column`` in lesson 2-4 and it appeared to one such.

If we evaluate ``fill-column`` in *\*scratch\** we get the value:

::

fill-column ;;its not in brackets because it is not a function!
70

(remember to evaluate an expression in *\*scratch\** use *[Crtl]-j*)

But if we use ``describe-variable`` to see the definition of ``fill-column`` it tells a slightly different story:

::

fill-column is a variable defined in `C source code'.
Its value is 70

Automatically becomes buffer-local when set in any fashion.
This variable is safe as a file local variable if its value
satisfies the predicate `integerp'.

Documentation:
*Column beyond which automatic line-wrapping should happen.
Interactively, you can set the buffer local value using C-x f.

You can customize this variable.

The thing that should get your eyebrows raised is the phrase *buffer-local*. What this means is that ``fill-column`` isn't actually a *global-variable* there is a ``fill-column`` variable for each buffer.

This chapter will look at how that sort of local scope is implemented in eLisp.

------------
Global Scope
------------

Lets create a variable with *global scope*. Edit the file ``omar-menu.el`` in ``./emacs.d/omars-dir/``

Add the line:

::
(setq omars-kills 123)

and then use the menu *Emacs-Lisp -> Evaluate Buffer* to execute that file again.

If you now go to *\*scratch\** and evaluate the variable ``omars-kills`` in it you will see ``123``.

The *global variable* ``omars-kills`` is available to any line of any function in the programme.

This is what we techies call **A Bad Thing**, or to put it another way **A Really Bad Thing (TM) (C) (R)**.

------------------------------------
The Dastardly Nature Of Global Scope
------------------------------------

---------------------
What You Have Learned
---------------------

------------------
Additional Reading
------------------

----------------
Extra Activities
----------------


1 change: 1 addition & 0 deletions index.rst
Expand Up @@ -69,3 +69,4 @@ Learn eLisp For Emacs

contents/lesson-5-1-elisp-in-files
contents/lesson-5-2-working-with-buffers-1
contents/lesson-5-3-scope-of-variables-set-setq-let-etc

0 comments on commit 82fbf97

Please sign in to comment.