From 82fbf979e2f13800ffa1f386442f4e12713e05c8 Mon Sep 17 00:00:00 2001 From: Gordon Guthrie Date: Mon, 24 Jan 2011 21:09:18 +0000 Subject: [PATCH] Partial commit of lesson 5-3 on local and global scope 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... --- ...-3-scope-of-variables-set-setq-let-etc.rst | 72 +++++++++++++++++++ index.rst | 1 + 2 files changed, 73 insertions(+) create mode 100644 contents/lesson-5-3-scope-of-variables-set-setq-let-etc.rst diff --git a/contents/lesson-5-3-scope-of-variables-set-setq-let-etc.rst b/contents/lesson-5-3-scope-of-variables-set-setq-let-etc.rst new file mode 100644 index 0000000..d5b5e56 --- /dev/null +++ b/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 +---------------- + + diff --git a/index.rst b/index.rst index 42df59e..69f4135 100644 --- a/index.rst +++ b/index.rst @@ -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