help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: To setq or not setq, that is the question.


From: Tim X
Subject: Re: To setq or not setq, that is the question.
Date: Fri, 26 Feb 2010 14:43:25 +1100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1.92 (gnu/linux)

Bill <bsagert@gmail.com> writes:

> Newbie here.
>
> In my .emacs file (blink-cursor-mode nil) works correctly.
> Also (setq visible-bell t) works as promised.
>
> Is there some clue I am missing as to when setq is required and when
> not?

The first one is a function call and the second one is setting a
variable. 

The following is meant to be very high level and is not 100% technically
correct. However, it should be good enough to explain some of the basic
concepts wihtout getting too bogged down in technical detail. 

In general, in elisp, the first value in a list i.e. (a b c d) is
expected to be a function and the remaining values are arguments.


so

(blink-cursor-mode nil) 

is a call to the function blink-cursor-mode with an argument of nil. C-h
f will explain what the function and its arguments mean i.e.

,----[ C-h f blink-cursor-mode RET ]
| blink-cursor-mode is an interactive compiled Lisp function in `frame.el'.
| 
| It is bound to <menu-bar> <options> <blink-cursor-mode> .
| 
| (blink-cursor-mode &optional ARG)
| 
| Toggle blinking cursor mode.
| With a numeric argument, turn blinking cursor mode on if ARG is positive,
| otherwise turn it off.  When blinking cursor mode is enabled, the
| cursor of the selected window blinks.
| 
| Note that this command is effective only when Emacs
| displays through a window system, because then Emacs does its own
| cursor display.  On a text-only terminal, this is not implemented.
| 
| [back]
`----

The line

(setq visible-bell t)

is setting the variable visible-bell to the value 't' (also referred to
as binding the value t to the symbol visible-bell. You can find out more
details on this variable with C-h v i.e.

,----[ C-h v visible-bell RET ]
| visible-bell is a variable defined in `C source code'.
| Its value is nil
| 
| Documentation:
| *Non-nil means try to flash the frame to represent a bell.
| 
| See also `ring-bell-function'.
| 
| You can customize this variable.
| 
| [back]
`----

Note that (setq visible-bell t) follows the rule that the first argument
in a list is expected to be a function i.e. C-h f setq

,----[ C-h f setq RET ]
| setq is a special form in `C source code'.
| 
| (setq [SYM VAL]...)
| 
| Set each SYM to the value of its VAL.
| The symbols SYM are variables; they are literal (not evaluated).
| The values VAL are expressions; they are evaluated.
| Thus, (setq x (1+ y)) sets `x' to the value of `(1+ y)'.
| The second VAL is not computed until after the first SYM is set, and so on;
| each VAL can use the new value of variables set earlier in the `setq'.
| The return value of the `setq' form is the value of the last VAL.
| 
| [back]

Note that the reference to 'special form' means that setq is slightly
different to 'real' functions. It is special because unlike other
functions, it does not evaluate its arguments. In lisp like languages,
these special forms are macros and defined with defmacro rather than
defun, but you probably don't need to worry about that too much right
now. Note also that some lists are quoted, which prevents the lisp
interpreter from trying to evaluate the list i.e. stops it from trying
to evaluate the first item in the list as a function. A quoted list
starts with a apostrophe (') i.e.

'(a b c) will evaluate to (a b c) while (a b c) without the ' will cause
the interpreter to try and evaluate the function 'a' with the arguments
b and c. The ' is actually short hand for the function quote i.e.

,----[ C-h f quote RET ]
| quote is a special form in `C source code'.
| 
| (quote ARG)
| 
| Return the argument, without evaluating it.  `(quote x)' yields `x'.
| 
| [back]
`----

Notice that 'quote' is also a special form, which means it doesn't
evaluate its argument. So

'(a b c) is the same as (quote (a b c)) and therfore follows the rule
regarding the first argument being something that is evaluated by the
interpreter. This is also a good example of why special forms are
needed. If quote was a function, it would try to evaluate its arguments.
As its argument is a list, it would try to evaluate the first element
i.e. 'a' which would cause an error if 'a' is not a defined function. 

In elisp, like other lisps, certain values evaluate to themselves i.e.

33 => 33
"fred" => fred
'c => c
t => t
nil => nil

Emacs comes with a very good introduction to emacs lisp that has been
written for people with little or no programming expeerience. I would
highly recommend reading it as it will help you learn how you can extend
and customize emacs for your own requirements. 

HTH

Tim


-- 
tcross (at) rapttech dot com dot au


reply via email to

[Prev in Thread] Current Thread [Next in Thread]