home *** CD-ROM | disk | FTP | other *** search
/ Gold Fish 3 / goldfish_volume_3.bin / files / util / edit / jade / man / jade.info-5 (.txt) < prev    next >
GNU Info File  |  1994-10-16  |  50KB  |  980 lines

  1. This is Info file jade.info, produced by Makeinfo-1.55 from the input
  2. file jade.texi.
  3. START-INFO-DIR-ENTRY
  4. * Jade: (jade).            An editor for X11 and AmigaDOS
  5. END-INFO-DIR-ENTRY
  6.    This is Edition 1.3, last updated 7 October 1994, of `The Jade
  7. Manual', for Jade, Version 3.2.
  8.    Jade is a text editor for X11 (on Unix) and the Amiga.
  9.    Copyright 1993, 1994 John Harper.
  10.    Permission is granted to make and distribute verbatim copies of this
  11. manual provided the copyright notice and this permission notice are
  12. preserved on all copies.
  13.    Permission is granted to copy and distribute modified versions of
  14. this manual under the conditions for verbatim copying, provided that
  15. the entire resulting derived work is distributed under the terms of a
  16. permission notice identical to this one.
  17. File: jade.info,  Node: Control Structures,  Next: Variables,  Prev: Evaluation,  Up: Programming Jade
  18. Control Structures
  19. ==================
  20.    Control structures are special forms or macros which control which
  21. forms get evaluated, when they get evaluated and the number of times to
  22. evaluate them. This includes conditional structures, loops, etc...
  23.    The simplest control structures are the sequencing structures; they
  24. are used to evaluate a list of forms in left to right order.
  25. * Menu:
  26. * Sequencing Structures::       Evaluating several forms in sequence
  27. * Conditional Structures::      Making decisions based on truth values
  28. * Looping Structures::          `while' loops
  29. * Non-Local Exits::             Exiting from several levels of evaluation
  30. File: jade.info,  Node: Sequencing Structures,  Next: Conditional Structures,  Up: Control Structures
  31. Sequencing Structures
  32. ---------------------
  33.    Each of the special forms in this section simply evaluates its
  34. argument forms in left-to-right order. The only difference is the
  35. result they return.
  36.    The most widely used sequencing special form is `progn': it
  37. evaluates all its argument forms and returns the computed value of the
  38. last one. Many other control structures are said to perform an
  39. "implicit progn", this means that they call `progn' with a list of
  40. forms.
  41.    `progn' in Lisp is nearly analogous to a `begin...end' block in
  42. Pascal; it is used in much the same places -- to allow you to evaluate
  43. a sequence of form where only one form was allowed (for example the
  44. true clause of an `if' structure).
  45.  - Special Form: progn FORMS...
  46.      All of the FORMS are evaluated sequentially (from left-to-right),
  47.      the result of the last evaluated FORM is the return value of this
  48.      structure. If no arguments are given to `progn' it returns `nil'.
  49.           (progn 'one (+ 1 1) "three")
  50.               => "three"
  51.           
  52.           (progn)
  53.               => nil
  54.  - Special Form: prog1 FIRST FORMS...
  55.      This special form evaluates its FIRST form then performs an
  56.      implicit progn on the rest of its arguments. The result of this
  57.      structure is the computed value of the first form.
  58.           (prog1 'one (+ 1 1) "three")
  59.               => one
  60.  - Special Form: prog2 FIRST SECOND FORMS...
  61.      This is similar to `prog1' except that the evaluated value of its
  62.      SECOND form is returned.
  63.      The FIRST form is evaluated, then its SECOND, then it performs an
  64.      implicit progn on the remaining arguments.
  65.           (prog2 'one (+ 1 1) "three")
  66.               => 2
  67. File: jade.info,  Node: Conditional Structures,  Next: Looping Structures,  Prev: Sequencing Structures,  Up: Control Structures
  68. Conditional Structures
  69. ----------------------
  70.    Lisp provides a number of conditional constructs, the most complex of
  71. which (`cond') will take a list of conditions, the first of which is
  72. `t' then has its associated list of forms evaluated. Theoretically this
  73. is the only conditional special form necessary -- the rest could be
  74. implemented as macros.
  75.  - Special Form: if CONDITION TRUE-FORM ELSE-FORMS...
  76.      The `if' construct is the nearest thing in Lisp to the
  77.      "if-then-else" construct found in most programming languages.
  78.      First the CONDITION form is evaluated, if it returns `t' (not
  79.      `nil') the TRUE-FORM is evaluated and its result returned.
  80.      Otherwise the result of an implicit progn on the ELSE-FORMS is
  81.      returned. If there are no ELSE-FORMS `nil' is returned.
  82.      Note that one of the TRUE-FORM or the ELSE-FORMS is completely
  83.      ignored -- it is not evaluated.
  84.           (if (special-form-p 'if)
  85.               "`if' is a special form"
  86.             "`if' is not a special form")
  87.               => "`if' is a special form"
  88.  - Special Form: when CONDITION TRUE-FORMS...
  89.      CONDITION is evaluated, if it is `t' the result of an implicit
  90.      progn on the TRUE-FORMS is returned, otherwise `nil' is returned.
  91.           (when t
  92.             (message "Pointless")
  93.             'foo)
  94.               => foo
  95.  - Special Form: unless CONDITION ELSE-FORMS...
  96.      This special forms first evaluates CONDITION, if its computed
  97.      value is not `nil' its value is returned. Otherwise the ELSE-FORMS
  98.      are evaluated sequentially, the value of the last is returned.
  99.  - Special Form: cond CLAUSE...
  100.      The `cond' special form is used to choose between an arbitrary
  101.      number of conditions. Each CLAUSE is a list; its car is the
  102.      CONDITION the list which is the cdr of the CLAUSE is the
  103.      BODY-FORMS. This means that each CLAUSE looks something like:
  104.           (CONDITION BODY-FORMS...)
  105.      and a whole `cond' form looks like:
  106.           (cond
  107.            (CONDITION-1 BODY-FORMS-1...)
  108.            (CONDITION-2 BODY-FORMS-2...)
  109.            ...)
  110.      The CONDITION in each CLAUSE is evaluated in sequence
  111.      (CONDITION-1, then CONDITION-2, ...), the first one which
  112.      evaluates to a non-`nil' has an implicit progn performed on its
  113.      BODY-FORMS, the value of which is the value returned by the `cond'
  114.      form.
  115.      If the true CONDITION has no BODY-FORMS the value returned by
  116.      `cond' is the value of the CONDITION. If none of the clauses has a
  117.      non-`nil' CONDITION the value of the `cond' is `nil'.
  118.      Often you want a "default" clause; one which has its BODY-FORMS to
  119.      be evaluated if none of the other clauses are true. The way to do
  120.      this is to add a clause with a CONDITION of `t' and BODY-FORMS of
  121.      whatever you want the default action to be.
  122.           (cond
  123.            ((stringp buffer-list))        ;Clause with no BODY-FORMS
  124.            ((consp buffer-list)
  125.             (setq x buffer-list)          ;Two BODY-FORMS
  126.             t)
  127.            (t                             ;Default clause
  128.             (error "`buffer-list' is corrupted!")))
  129.               => t
  130.      All of the other conditionals can be written in terms of `cond',
  131.           (if C T E...) == (cond (C T) (t E...))
  132.           
  133.           (when C T...) == (cond (C T...))
  134.           
  135.           (unless C E...) == (cond (E) (t E...))
  136.    There are also a number of special forms which combine conditions
  137. together by the normal logical rules.
  138.  - Special Form: or FORMS...
  139.      The first of the FORMS is evaluated, if it is non-`nil' its value
  140.      becomes the value of the `or' form and no more of `forms' are
  141.      evaluated. Otherwise this step is repeated for the next member of
  142.      FORMS.
  143.      If all of the FORMS have been evaluated and none have a non-`nil'
  144.      value `nil' becomes the value of the `or' form.
  145.      If there are no FORMS `nil' is returned.
  146.           (or nil 1 nil (beep))           ;`(beep)' won't be evaluated
  147.               => 1
  148.  - Special Form: and FORMS...
  149.      The first of the FORMS is evaluated. If it is `nil' no more of the
  150.      FORMS are evaluated and `nil' becomes the value of the `and'
  151.      structure. Otherwise the next member of FORMS is evaluated and its
  152.      value tested. If none of the FORMS are `nil' the computed value of
  153.      the last member of FORMS becomes the value of the `and' form.
  154.           (and 1 2 nil (beep))            ;`(beep)' won't be evaluated
  155.               => nil
  156.           
  157.           (and 1 2 3)                     ;All forms are evaluated
  158.               => 3
  159.  - Function: not OBJECT
  160.      This function inverts the boolean value of its argument. If OBJECT
  161.      is non-`nil', `nil' is returned, otherwise `t' is returned.
  162.           (not nil)
  163.               => t
  164.           
  165.           (not t)
  166.               => nil
  167.           
  168.           (not 42)
  169.               => nil
  170. File: jade.info,  Node: Looping Structures,  Next: Non-Local Exits,  Prev: Conditional Structures,  Up: Control Structures
  171. Looping Structures
  172. ------------------
  173.    Jade's version of Lisp has only one structure for looping -- a
  174. "while" loop similar to those found in most programming languages.
  175.  - Special Form: while CONDITION BODY-FORMS...
  176.      The CONDITION form is evaluated. If it is non-`nil' an implicit
  177.      progn is performed on the BODY-FORMS and the whole thing is
  178.      repeated again.
  179.      This continues until the CONDITION form evaluates to `nil'. The
  180.      value of any `while' structure is `nil'.
  181.      `while' can be recursively defined in terms of `when':
  182.           (while C B ...)
  183.           ==
  184.           (when C (progn B ... (while C B ...)))
  185.           ;; Step through a list X
  186.           (while X
  187.             ;; Do something with the current element, `(car X)'
  188.             (setq X (cdr X)))
  189. File: jade.info,  Node: Non-Local Exits,  Prev: Looping Structures,  Up: Control Structures
  190. Non-Local Exits
  191. ---------------
  192.    A "non-local exit" is a transfer of control from the current point
  193. of evaluation to a different point (somewhat similar to the
  194. much-maligned `goto' statement in some imperative languages).
  195.    Non-local exits can either be used explicitly (`catch' and `throw')
  196. or implicitly (errors).
  197. * Menu:
  198. * Catch and Throw::             Programmed non-local exits
  199. * Function Exits::              Returning values from a function
  200. * Cleanup Forms::               Forms which will always be evaluated
  201. * Errors::                      Signalling that an error occurred
  202. File: jade.info,  Node: Catch and Throw,  Next: Function Exits,  Up: Non-Local Exits
  203. Catch and Throw
  204. ...............
  205.    The `catch' and `throw' structures are used to perform explicit
  206. transfers of control. First a `catch' form is used to setup a "tag",
  207. this acts like a label for the C language's `goto' statement. To
  208. transfer control a `throw' form is then used to transfer to the named
  209. tag. The tag is destroyed and the `catch' form exits with the value
  210. provided by the `throw'.
  211.    In a program this looks like,
  212.      (catch 'TAG
  213.        ;; Forms which may `throw' back to TAG
  214.        ...
  215.        (throw 'TAG VALUE)
  216.        ;; Control has now passed to the `catch',
  217.        ;; no more forms in this progn will be evaluated.
  218.        ...)
  219.          => VALUE
  220. where TAG is the tag to be used (this is normally a symbol) and VALUE
  221. is the result of the `catch' form.
  222.    When a throw actually happens all catches in scope are searched for
  223. one with a tag which is `eq' to the tag in the throw. If more than one
  224. exists the most-recent is chosen. Now that the catch has been located
  225. the environment is `wound-back' to the catch's position (i.e. local
  226. variables are unbound, cleanup forms removed, unused catches forgotten,
  227. etc...) and all Lisp constructs between the current point of control and
  228. the catch are exited.
  229.    For example,
  230.      (let
  231.          ((test 'outer))
  232.        (cons (catch 'foo
  233.                (let
  234.                    ((test 'inner))
  235.                  (throw 'foo test)
  236.                  (setq test 'unreachable)))  ;Never reached
  237.              test))
  238.          => (inner . outer)
  239. when the throw executes the second binding of `test' is unwound and the
  240. first binding comes back into effect. For more details on variable
  241. binding see *Note Local Variables::.
  242.    Note that catch tags are *dynamically* scoped, the thrower does not
  243. have to be within the same lexical scope (this means you can throw
  244. through functions).
  245.  - Special Form: catch TAG BODY-FORMS...
  246.      This special form defines a catch tag which will be accessible
  247.      while the BODY-FORMS are being evaluated.
  248.      TAG is evaluated and recorded as the tag for this catch. Next the
  249.      BODY-FORMS are evaluated as an implicit progn. The value of the
  250.      `catch' form is either the value of the progn, or, if a `throw'
  251.      happened, the value specified in the THROW form.
  252.      Before exiting the tag installed by this form is removed.
  253.  - Function: throw TAG &optional CATCH-VALUE
  254.      This function transfers the point of control to the catch form
  255.      with a tag which is `eq' to TAG. The value returned by this catch
  256.      form is either CATCH-VALUE or `nil' if CATCH-VALUE is undefined.
  257.      If there is no catch with a tag of TAG an error is signalled and
  258.      the editor returns to the top-level of evaluation.
  259. File: jade.info,  Node: Function Exits,  Next: Cleanup Forms,  Prev: Catch and Throw,  Up: Non-Local Exits
  260. Function Exits
  261. ..............
  262.    It is often useful to be able to immediately return control from a
  263. function definition (like the C `return' statement). Jade's version of
  264. Lisp has the `return' function for this.
  265.  - Function: return &optional VALUE
  266.      This function transfers control out of the most-recent
  267.      lambda-expression (i.e. a function or macro definition) so that
  268.      the result of the lambda- expression is VALUE.
  269.           (funcall '(lambda () (return 'x) 'y))
  270.               => x
  271.      The `'y' form is never evaluated since control is passed straight
  272.      from the `(return 'y)' form back to the `funcall' form.
  273. File: jade.info,  Node: Cleanup Forms,  Next: Errors,  Prev: Function Exits,  Up: Non-Local Exits
  274. Cleanup Forms
  275. .............
  276.    It is sometimes necessary to be sure that a certain form is *always*
  277. evaluated, even when a non-local exit would normally bypass that form.
  278. The `unwind-protect' special form is used to stop this happening.
  279.  - Special Form: unwind-protect BODY-FORM CLEANUP-FORMS...
  280.      The BODY-FORM is evaluated, if it exits normally the CLEANUP-FORMS
  281.      are evaluated sequentially then the value which the BODY-FORM
  282.      returned becomes the value of the `unwind-protect' form. If the
  283.      BODY-FORM exits abnormally though (i.e. a non-local exit happened)
  284.      the CLEANUP-FORMS are evaluated anyway and the non-local exit
  285.      continues.
  286.      One use of this is to ensure that an opened file is always closed,
  287.      for example,
  288.           (catch 'foo
  289.             (unwind-protect
  290.                 (let
  291.                     ((temporary-file (open (tmp-file-name) "w")))
  292.                   ;; Use `temporary-file'
  293.                   (write temporary-file "A test\n")
  294.                   ;; Now force a non-local exit
  295.                   (throw 'foo))
  296.               ;; This is the CLEANUP-FORM it will *always*
  297.               ;; be evaluated no matter what happens.
  298.               (close temporary-file)))
  299.               => nil
  300. File: jade.info,  Node: Errors,  Prev: Cleanup Forms,  Up: Non-Local Exits
  301. Errors
  302. ......
  303.    Errors are a type of non-local exit; when a form can not be evaluated
  304. properly an error is normally "signalled". If an error-handler has been
  305. installed for that type of error control is unwound back to the handler
  306. and evaluation continues. If there is no suitable handler control is
  307. passed back to the event loop of the most-recent recursive edit and a
  308. suitable error message is printed.
  309.  - Function: signal ERROR-SYMBOL DATA
  310.      Signals that an error has happened. ERROR-SYMBOL is a symbol
  311.      classifying the type of error, it should have a property
  312.      `error-message' (a string) which is the error message to be
  313.      printed.
  314.      DATA is a list of objects which are relevant to the error -- they
  315.      will be made available to any error-handler or printed with the
  316.      error message otherwise.
  317.           (signal 'void-value '(some-symbol))
  318.               error--> Value as variable is void: some-symbol
  319.  - Variable: debug-on-error
  320.      This variable is consulted by the function `signal'. If its value
  321.      is either `t' or a list containing the ERROR-SYMBOL to `signal' as
  322.      one of its elements, the Lisp debugger is entered.  When the
  323.      debugger exits the error is signalled as normal.
  324.    When you expect an error to occur and need to be able to regain
  325. control afterwards the `error-protect' form can be used.
  326.  - Special Form: error-protect BODY-FORM ERROR-HANDLERS...
  327.      `error-protect' evaluates the BODY-FORM with error handlers in
  328.      place.
  329.      Each of the ERROR-HANDLERS is a list whose car is a symbol
  330.      defining the type of error which this handler catches. The cdr of
  331.      the list is a list of forms to be evaluated sequentially when the
  332.      handler is invoked.
  333.      While the forms of the error handler are being evaluated the
  334.      variable `error-info' is bound to the value `(ERROR-SYMBOL . DATA)'
  335.      (these were the arguments to the `signal' form which caused the
  336.      error).
  337.      The special value, the symbol `error', in the car of one of the
  338.      ERROR-HANDLERS will catch *all* types of errors.
  339.           (error-protect
  340.               (signal 'file-error '("File not found" "/tmp/foo"))
  341.             (file-error
  342.              error-info)
  343.             (error
  344.              (setq x z)))         ;Default handler
  345.               => (file-error "File not found" "/tmp/foo")
  346. File: jade.info,  Node: Variables,  Next: Functions,  Prev: Control Structures,  Up: Programming Jade
  347. Variables
  348. =========
  349.    In Lisp symbols are used to represent variables. Each symbol
  350. contains a slot which is used to contain the value of the symbol when
  351. it is used as a symbol.
  352.    The normal way to obtain the current value of a variable is simply to
  353. evaluate the symbol it lives in (i.e. write the name of the variable in
  354. your program).
  355.  - Function: symbol-value VARIABLE
  356.      This function returns the value of the symbol VARIABLE in the
  357.      current environment.
  358. * Menu:
  359. * Local Variables::             Creating temporary variables
  360. * Setting Variables::           Altering a variable's value
  361. * Scope and Extent::            Technical jargon
  362. * Buffer-Local Variables::      Variables with distinct values in
  363.                                   each buffer.
  364. * Void Variables::              Some variables have no values
  365. * Constant Variables::          Variables which may not be altered
  366. * Defining Variables::          How to define a variable before
  367.                                   using it
  368. File: jade.info,  Node: Local Variables,  Next: Setting Variables,  Up: Variables
  369. Local Variables
  370. ---------------
  371.    A "local variable" is a variable which has a temporary value while a
  372. program is executing, for example, when a function is called the
  373. variables which are the names of its arguments are temporarily bound (a
  374. "binding" is a particular instance of a local variable) to the values
  375. of the arguments passed to the function. When the function call exits
  376. its arguments are unbound and the previous definitions of the variables
  377. come back into view.
  378.    Even if a variable has more than one binding still `active' only the
  379. most recent is visible -- there is absolutely no way the previous
  380. bindings can be accessed until the bindings are unbound one-by-one.
  381.    A nice way of visualising variable binding is to think of each
  382. variable as a stack. When the variable is bound to, a new value is
  383. pushed onto the stack, when it is unbound the top of the stack is
  384. popped. Similarly when the stack is empty the value of the variable is
  385. void (*note Void Variables::.). Assigning a value to the variable
  386. (*note Setting Variables::.) overwrites the top value on the stack with
  387. a new value. When the value of the variable is required it is simply
  388. read from the top of the stack.
  389.    Apart from function calls there are two special forms which perform
  390. variable binding (i.e. creating local variables), `let' and `let*'.
  391.  - Special Form: let BINDINGS BODY-FORMS...
  392.      `let' creates new variable bindings as specified by the BINDINGS
  393.      argument then evaluates the BODY-FORMS in order. The variables are
  394.      then unbound to their state before this `let' form and the value
  395.      of the implicit progn of the BODY-FORMS becomes the value of the
  396.      `let' form.
  397.      The BINDINGS argument is a list of the bindings to perform. Each
  398.      binding is either a symbol, in which case that variable is bound to
  399.      nil, or a list whose car is a symbol. The cdr of this list is a
  400.      list of forms which, when evaluated, give the value to bind the
  401.      variable to.
  402.           (setq foo 42)
  403.               => 42
  404.           (let
  405.               ((foo (+ 1 2))
  406.                bar)
  407.             ;; Body forms
  408.             (setq foo (1+ foo))   ;This sets the new binding
  409.             (cons foo bar))
  410.               => (4 . nil)
  411.           foo
  412.               => 42        ;The original values is back
  413.      Note that no variables are bound until all the new values have been
  414.      computed (unlike in `let*'). For example,
  415.           (setq foo 42)
  416.               => 42
  417.           (let
  418.               ((foo 100)
  419.                (bar foo))
  420.             (cons foo bar))
  421.               => (100 . 42)
  422.      Although `foo' is given a new binding this is not actually done
  423.      until all the new bindings have been computed, hence `bar' is
  424.      bound to the *old* value of `foo'.
  425.  - Special Form: let* BINDINGS BODY-FORMS...
  426.      This special form is exactly the same as `let' except for one
  427.      important difference: the new bindings are installed *as they are
  428.      computed*.
  429.      You can see the difference by comparing the following example with
  430.      the last example in the `let' documentation (above),
  431.           (setq foo 42)
  432.               => 42
  433.           (let*                   ;Using `let*' this time
  434.               ((foo 100)
  435.                (bar foo))
  436.             (cons foo bar))
  437.               => (100 . 100)
  438.      By the time the binding of `bar' is computed the new binding of
  439.      `foo' has already been installed.
  440. File: jade.info,  Node: Setting Variables,  Next: Scope and Extent,  Prev: Local Variables,  Up: Variables
  441. Setting Variables
  442. -----------------
  443.    "Setting" a variable means to overwrite its current value (that is,
  444. the value of its most recent binding) with a new one. The old value is
  445. irretrievably lost (unlike when a new value is bound to a variable,
  446. *note Local Variables::.).
  447.  - Special Form: setq VARIABLE FORM ...
  448.      The special form `setq' is the usual method of altering the value
  449.      of a variable. Each VARIABLE is set to the result of evaluating its
  450.      corresponding FORM. The last value assigned becomes the value of
  451.      the `setq' form.
  452.           (setq x 20 y (+ 2 3))
  453.               => 5
  454.      In the above example the variable `x' is set to `20' and `y' is
  455.      set to the value of the form `(+ 2 3)' (5).
  456.      When the variable is marked as being buffer-local (*note
  457.      Buffer-Local Variables::.) the current buffer's instance of the
  458.      variable is set.
  459.  - Function: set VARIABLE NEW-VALUE
  460.      The value of the variable VARIABLE (a symbol) is set to NEW-VALUE
  461.      and the NEW-VALUE is returned.
  462.      This function is used when the VARIABLE is unknown until run-time,
  463.      and therefore has to be computed from a form.
  464.           (set 'foo 20)
  465.           ==
  466.           (setq foo 20)           ;`setq' means `set-quoted'
  467.               => 20
  468. File: jade.info,  Node: Scope and Extent,  Next: Buffer-Local Variables,  Prev: Setting Variables,  Up: Variables
  469. Scope and Extent
  470. ----------------
  471.    In Jade's version of Lisp all variables have "indefinite scope" and
  472. "dynamic extent". What this means is that references to variables may
  473. occur anywhere in a program (i.e. bindings established in one function
  474. are not only accessible within that function, that's lexical scope) and
  475. that references may occur at any point in the time between the binding
  476. being created and it being unbound.
  477.    The combination of indefinite scope and dynamic extent is often
  478. termed "dynamic scope".
  479.    As an aside, Lisp objects have "indefinite extent", meaning that the
  480. object will exist for as long as there is a possibility of it being
  481. referenced (and possibly longer -- until the garbage collector runs).
  482.    Note that in Common Lisp only those variables declared `special' have
  483. indefinite scope and dynamic extent.
  484.    Try not to abuse the dynamic scoping, although it is often very
  485. useful to be able to bind a variable in one function and use it in
  486. another this can be confusing if not controlled and documented properly.
  487.    A quick example of the use of dynamic scope,
  488.      (defun foo (x)
  489.        (let
  490.            ((foo-var (* x 20)))
  491.          (bar x)
  492.          ...
  493.      
  494.      (defun bar (y)
  495.        ;; Since this function is called from
  496.        ;; the function `foo' it can refer
  497.        ;; to any bindings which `foo' can.
  498.        (setq y (+ y foo-var))
  499.        ...
  500. File: jade.info,  Node: Buffer-Local Variables,  Next: Void Variables,  Prev: Scope and Extent,  Up: Variables
  501. Buffer-Local Variables
  502. ----------------------
  503.    It is often very useful to be able to give variables different
  504. values for different editor buffers -- most major modes need to record
  505. some buffer-specific information. Jade allows you to do this by giving a
  506. variable buffer-local bindings.
  507.    There are two strengths of buffer-local variables: you can either
  508. give a variable a buffer-local value in a single buffer, with other
  509. buffers treating the variable as normal, or a variable can be marked as
  510. being *automatically* buffer-local, each time the variable is set the
  511. current buffer's value of the variable is updated.
  512.    Each buffer maintains an alist of the symbols which have buffer-local
  513. values in the buffer and the actual values themselves, this alist may
  514. be read with the `buffer-variables' function.
  515.    When the value of a variable is referenced (via the `symbol-value'
  516. function) the current buffer's alist of local values is examined for a
  517. binding of the variable being referenced; if one is found that is the
  518. value of the variable, otherwise the "default value" (the value stored
  519. in the symbol's value cell) is used.
  520.    Setting a variable also searches for a buffer-local binding; if one
  521. exists its value is modified, not the default value. If the variable
  522. has previously been marked as being automatically buffer-local (by
  523. `make-variable-buffer-local') a buffer-local binding is automatically
  524. created if one doesn't already exist.
  525.    Currently there is one main problem with buffer-local variables:
  526. they can't have temporary values bound to them (or rather, they can but
  527. I guarantee it won't work how you expect), so for the time being, don't
  528. try to bind local values (with `let' or `let*') to a buffer-local
  529. variable.
  530.  - Function: make-local-variable SYMBOL
  531.      This function gives the variable SYMBOL a buffer-local binding in
  532.      the current buffer. The value of this binding will be the same as
  533.      the variable's default value.
  534.      If SYMBOL already has a buffer-local value in this buffer nothing
  535.      happens.
  536.      Returns SYMBOL.
  537.  - Function: make-variable-buffer-local SYMBOL
  538.      This function marks the variable SYMBOL as being automatically
  539.      buffer-local.
  540.      This means that any attempts at setting the value of SYMBOL will
  541.      actually set the current buffer's local value (if necessary a new
  542.      buffer-local binding will be created in the buffer).
  543.      Returns SYMBOL.
  544.           (make-variable-buffer-local 'buffer-modtime)
  545.               => buffer-modtime
  546.  - Function: default-value SYMBOL
  547.      This function returns the default value of the variable SYMBOL.
  548.           (setq foo 'default)
  549.               => default
  550.           (make-local-variable 'foo)      ;Create a value in this buffer
  551.               => foo
  552.           (setq foo 'local)
  553.               => local
  554.           foo
  555.               => local
  556.           (symbol-value 'foo)
  557.               => local
  558.           (default-value 'foo)
  559.               => default
  560.  - Function: default-boundp SYMBOL
  561.      Returns `t' if the variable SYMBOL has a non-void default value.
  562.  - Special Form: setq-default SYMBOL FORM ...
  563.      Similar to the `setq' special form except that the default value
  564.      of each VARIABLE is set. In non-buffer-local symbols there is no
  565.      difference between `setq' and `setq-default'.
  566.  - Function: set-default SYMBOL NEW-VALUE
  567.      Sets the default value of the variable SYMBOL to NEW-VALUE, then
  568.      returns NEW-VALUE.
  569.  - Function: kill-local-variable SYMBOL
  570.      This function removes the buffer-local binding of the variable
  571.      SYMBOL from the current buffer (if one exists) then returns SYMBOL.
  572.  - Function: kill-all-local-variables
  573.      This function removes all the buffer-local bindings associated
  574.      with the current buffer. Subsequently, any buffer-local variables
  575.      referenced while this buffer is current will use their default
  576.      values.
  577.    The usual way to define an automatically buffer-local variable is to
  578. use `defvar' and `make-variable-buffer-local', for example,
  579.      (defvar my-local-variable DEFAULT-VALUE
  580.        "Doc string for `my-local-variable'.")
  581.      (make-variable-buffer-local 'my-local-variable)
  582.    Note that if you want to reference the value of a buffer-local
  583. variable in a buffer other than the current buffer, use the
  584. `with-buffer' special form (*note The Current Buffer::.). For example,
  585. the form,
  586.      (with-buffer OTHER-BUFFER SOME-VARIABLE)
  587. will produce the value of the variable SOME-VARIABLE in the buffer
  588. OTHER-BUFFER.
  589. File: jade.info,  Node: Void Variables,  Next: Constant Variables,  Prev: Buffer-Local Variables,  Up: Variables
  590. Void Variables
  591. --------------
  592.    A variable which has no value is said to be "void", attempting to
  593. reference the value of such a symbol will result in an error. It is
  594. possible for the most recent binding of a variable to be void even
  595. though the inactive bindings may have values.
  596.  - Function: boundp VARIABLE
  597.      Returns `t' if the symbol VARIABLE has a value, `nil' if its value
  598.      is void.
  599.  - Function: makunbound VARIABLE
  600.      This function makes the current binding of the symbol VARIABLE be
  601.      void, then returns VARIABLE.
  602.           (setq foo 42)
  603.               => 42
  604.           foo
  605.               => 42
  606.           (boundp 'foo)
  607.               => t
  608.           (makunbound 'foo)
  609.               => foo
  610.           (boundp 'foo)
  611.               => nil
  612.           foo
  613.               error--> Value as variable is void: foo
  614. File: jade.info,  Node: Constant Variables,  Next: Defining Variables,  Prev: Void Variables,  Up: Variables
  615. Constant Variables
  616. ------------------
  617.    In Lisp constants are represented by variables which have been
  618. marked as being read-only. Any attempt to alter the value of a constant
  619. results in an error.
  620.    Two of the most commonly used constants are `nil' and `t'.
  621.  - Function: set-const-variable VARIABLE &optional READ-WRITE
  622.      This function defines whether or not the value of the symbol
  623.      VARIABLE may be modified. If READ-WRITE is `nil' or undefined the
  624.      variable is marked to be constant, otherwise it's marked to be a
  625.      normal variable.  The value returned is VARIABLE.
  626.  - Function: const-variable-p VARIABLE
  627.      Returns `t' if the value of the symbol VARIABLE may be altered,
  628.      `nil' otherwise.
  629.    Constants may behave a bit strangely when you compile the program
  630. they are used in: the value of the constant is likely to be hardwired
  631. into the compiled functions it is used in, and the constant is unlikely
  632. to be `eq' to itself!
  633.    The compiler assumes that constant is always the same, whenever it is
  634. evaluated. It may even be evaluated more than once. *Note Compiled
  635. Lisp::.
  636.    The special form `defconst' can be used to define constants, see
  637. *Note Defining Variables::.
  638. File: jade.info,  Node: Defining Variables,  Prev: Constant Variables,  Up: Variables
  639. Defining Variables
  640. ------------------
  641.    The special forms `defvar' and `defconst' allow you to define the
  642. global variables which will be used in a program. This is entirely
  643. optional; it is highly recommended though.
  644.  - Special Form: defvar VARIABLE FORM [DOC-STRING]
  645.      This special form defines a global variable, the symbol VARIABLE.
  646.      If the value of VARIABLE is void the FORM is evaluated and its
  647.      value is stored as the value of VARIABLE (note that the default
  648.      value is modified, never a buffer-local value).
  649.      If the DOC-STRING argument is defined it is a string documenting
  650.      VARIABLE. This string is then stored as the symbol's
  651.      `variable-documentation' property and can be accessed by the
  652.      `describe-variable' function.
  653.           (defvar my-variable '(x y)
  654.             "This variable is an example showing the usage of the `defvar'
  655.           special form.")
  656.               => my-variable
  657.  - Special Form: defconst CONSTANT FORM [DOC-STRING]
  658.      `defconst' defines a constant, the symbol CONSTANT. Its value (in
  659.      the case of a buffer-local symbol, its default value) is set to the
  660.      result of evaluating FORM. Note that unlike `defvar' the value of
  661.      the symbol is *always* set, even if it already has a value.
  662.      The DOC-STRING argument, if defined, is the documentation string
  663.      for the constant.
  664.           (defconst the-answer 42
  665.             "An example constant.")
  666.               => the-answer
  667.      *Note Constant Variables::.
  668. File: jade.info,  Node: Functions,  Next: Macros,  Prev: Variables,  Up: Programming Jade
  669. Functions
  670. =========
  671.    A "function" is a Lisp object which, when applied to a sequence of
  672. argument values, produces a value -- the function's "result". It may
  673. also produce side-effects. All Lisp functions return results -- there
  674. is nothing like a procedure in Pascal.
  675.    Functions are the main building-block in Lisp programs, each program
  676. is usually a system of inter-related functions.
  677.    There are two types of function: "primitive functions" are functions
  678. written in the C language, these are sometimes called built-in
  679. functions, the object containing the C code itself is called a "subr".
  680. All other functions are written in Lisp.
  681.  - Function: functionp OBJECT
  682.      Returns `t' if OBJECT is a function (i.e. it can be used as the
  683.      function argument of `funcall'.
  684.           (functionp 'set)
  685.               => t
  686.           
  687.           (functionp 'setq)
  688.               => nil
  689.           
  690.           (functionp #'(lambda (x) (+ x 2)))
  691.              => t
  692. * Menu:
  693. * Lambda Expressions::          Structure of a function object
  694. * Named Functions::             Functions can be named by symbols,
  695. * Anonymous Functions::         Or they can be un-named
  696. * Predicate Functions::         Functions which return boolean values
  697. * Defining Functions::          How to write a function definition
  698. * Calling Functions::           Functions can be called by hand
  699. * Mapping Functions::           Map a function to the elements of a list
  700. File: jade.info,  Node: Lambda Expressions,  Next: Named Functions,  Up: Functions
  701. Lambda Expressions
  702. ------------------
  703.    "Lambda expressions" are used to create an object of type function
  704. from other Lisp objects, it is a list whose first element is the symbol
  705. `lambda'. All functions written in Lisp (as opposed to the primitive
  706. functions in C) are represented by a lambda expression.
  707.    Note that a lambda expression is *not* an expression, evaluating a
  708. lambda expression will give an error (unless there is a function called
  709. `lambda').
  710.    The format of a lambda expression is:
  711.      (lambda LAMBDA-LIST [DOC] [INTERACTIVE-DECLARATION] BODY-FORMS... )
  712. Where LAMBDA-LIST is the argument specification of the function, DOC is
  713. an optional documentation string, INTERACTIVE-DECLARATION is only
  714. required by editor commands (*note Commands::.) and the BODY-FORMS is
  715. the actual function code (when the function is called each form is
  716. evaluated in sequence, the last form's value is the result returned by
  717. the function).
  718.    The LAMBDA-LIST is a list, it defines how the argument values
  719. applied to the function are bound to local variables which represent
  720. the arguments within the function. At its simplest it is simply a list
  721. of symbols, each symbol will have the corresponding argument value
  722. bound to it. For example, the lambda list,
  723.      (lambda (x y) (+ x y))
  724. takes two arguments, `x' and `y'. When this function is called with two
  725. arguments the first will be bound to `x' and the second to `y' (then
  726. the function will return their sum).
  727.    To complicate matters there are several "lambda-list keywords" which
  728. modify the meaning of symbols in the lambda-list. Each keyword is a
  729. symbol whose name begins with an ampersand, they are:
  730. `&optional'
  731.      All the variables following this keyword are considered "optional"
  732.      (all variables before the first keyword are "required": an error
  733.      will be signalled if a required argument is undefined in a
  734.      function call). If an optional argument is undefined it will
  735.      simply be given the value `nil'.
  736.      Note that optional arguments must be specified if a later optional
  737.      argument is also specified. Use `nil' to explicitly show that an
  738.      optional argument is undefined.
  739.      For example, if a function `foo' takes two optional arguments and
  740.      you want to call it with only the second argument defined, the
  741.      first argument must be specified as `nil' to ensure that the
  742.      correct argument value is bound to the correct variable.
  743.           (defun foo (&optional arg-1 arg-2)
  744.             ...
  745.           
  746.           (foo nil arg-2-value)   ;Leave the first argument undefined
  747. `&rest'
  748.      The `&rest' keyword allows a variable number of arguments to be
  749.      applied to a function, all the argument values which have not been
  750.      bound to argument variables are simply consed into a list and bound
  751.      to the variable after the `&rest' keyword. For example, in,
  752.           (lambda (x &rest y) ...)
  753.      the first argument, `x', is required. Any other arguments applied
  754.      to this function are made into a list and this list is bound to the
  755.      `y' variable.
  756.    When a function represented by a lambda-list is called the first
  757. thing that happens is to bind the argument values to the argument
  758. variables. The LAMBDA-LIST and the list of argument values applied to
  759. the function are worked through in parallel. Any required arguments
  760. which are left undefined when the end of the argument values has been
  761. reached causes an error.
  762.    After the arguments have been processed the BODY-FORMS are evaluated
  763. by an implicit progn, the value of which becomes the value of the
  764. function call. Finally, all argument variables are unbound and control
  765. passes back to the caller.
  766. File: jade.info,  Node: Named Functions,  Next: Anonymous Functions,  Prev: Lambda Expressions,  Up: Functions
  767. Named Functions
  768. ---------------
  769.    Functions are normally associated with symbols, the name of the
  770. symbol being the same as the name of its associated function. Each
  771. symbol has a special function cell (this is totally separate from the
  772. symbol's value as a variable -- variables and functions may have the
  773. same name without any problems occurring) which is used to store the
  774. function's definition, either a lambda expression (*note Lambda
  775. Expressions::.) or a subr (C code) object.
  776.    The evaluator knows to indirect through the function value of a
  777. symbol in any function call (*note Function Call Forms::.) so the
  778. normal way to call a function is simply write its name as the first
  779. element in a list, any arguments making up the other elements in the
  780. list.  *Note List Forms::.
  781.    The functions and special forms which take functions as their
  782. arguments (i.e. `funcall') can also take symbols. For example,
  783.      (funcall 'message "An example")
  784.      ==
  785.      (message "An example")
  786.  - Function: symbol-function SYMBOL
  787.      Returns the value of the function cell in the symbol SYMBOL.
  788.           (symbol-function 'symbol-function)
  789.               => #<subr symbol-function>
  790.  - Function: fboundp SYMBOL
  791.      This function returns `t' if the symbol SYMBOL has a non-void
  792.      value in its function cell, `nil' otherwise.
  793.           (fboundp 'setq)
  794.               => t
  795.  - Function: fset SYMBOL NEW-VALUE
  796.      Sets the value of the function cell in the symbol SYMBOL to
  797.      NEW-VALUE, then returns NEW-VALUE.
  798.      This function is rarely used, see *Note Defining Functions::.
  799.  - Function: fmakunbound SYMBOL
  800.      This function makes the value of the function cell in SYMBOL void,
  801.      then returns SYMBOL.
  802. File: jade.info,  Node: Anonymous Functions,  Next: Predicate Functions,  Prev: Named Functions,  Up: Functions
  803. Anonymous Functions
  804. -------------------
  805.    When giving function names as arguments to functions it is useful to
  806. give an actual function *definition* (i.e. a lambda expression) instead
  807. of the name of a function.
  808.    In Lisp, unlike most other programming languages, functions have no
  809. inherent name. As seen in the last section named-functions are created
  810. by storing a function in a special slot of a symbol, if you want, a
  811. function can have many different names: simply store the function in
  812. many different symbols!
  813.    So, when you want to pass a function as an argument there is the
  814. option of just writing down its definition. This is especially useful
  815. with functions like `mapcar' and `delete-if'. For example, the
  816. following form removes all elements from the LIST which are even and
  817. greater than 20.
  818.      (setq LIST (delete-if #'(lambda (x)
  819.                                      (and (zerop (% x 2))
  820.                                           (> x 20)))
  821.                                  LIST))
  822.    The lambda expression is very simple, it combines two predicates
  823. applied to its argument.
  824.    Note that the function definition is quoted by `#'', not the normal
  825. `''. This is a special shortcut for the `function' special form (like
  826. `'' is a shortcut to `quote'). In general, `#'X' is expanded by the
  827. Lisp reader to `(function X)'.
  828.  - Special Form: function ARG
  829.      This special form is nearly identical to the `quote' form, it
  830.      always returns its argument without evaluating it. The difference
  831.      is that the Lisp compiler knows to compile the ARG into a byte-code
  832.      form (unless ARG is a symbol in which case it is not compiled).
  833.      What this means is when you have to quote a function, use the `#''
  834.      syntax.
  835. File: jade.info,  Node: Predicate Functions,  Next: Defining Functions,  Prev: Anonymous Functions,  Up: Functions
  836. Predicate Functions
  837. -------------------
  838.    In Lisp, a function which returns a boolean `true' or boolean `false'
  839. value is called a "predicate". As is the convention in Lisp a value of
  840. `nil' means false, anything else means true. The symbol `t' is often
  841. used to represent a true value (in fact, sometimes the symbol `t'
  842. should be read as *any* non-`nil' value).
  843.    Another Lisp convention is that the names of predicate functions
  844. should be the concept the predicate is testing for and either `p' or
  845. `-p'.
  846.    The `p' variant is used when the concept name does not contain any
  847. hyphens.
  848.    For example a predicate to test for the concept "const-variable" (a
  849. variable which has a constant value, *note Constant Variables::.) would
  850. be called `const-variable-p'. On the other hand a predicate to test for
  851. the concept "buffer" (a Lisp object which is a buffer) would be called
  852. `bufferp'.
  853. File: jade.info,  Node: Defining Functions,  Next: Calling Functions,  Prev: Predicate Functions,  Up: Functions
  854. Defining Functions
  855. ------------------
  856.    Named functions are normally defined by the `defun' special form.
  857.  - Special Form: defun NAME LAMBDA-LIST BODY-FORMS...
  858.      `defun' initialises the function definition of the symbol NAME to
  859.      the lambda expression resulting from the concatenation of the
  860.      symbol `lambda', LAMBDA-LIST and the BODY-FORMS. So,
  861.           (defun foo (x y)
  862.             ...
  863.           ==
  864.           (fset 'foo #'(lambda (x y)
  865.                          ...
  866.      The BODY-FORMS may contain a documentation string for the function
  867.      as its first form and an interactive calling specification as its
  868.      first (if there is no doc-string) or second form if the function
  869.      may be called interactively by the user (*note Commands::.).
  870.    An example function definition (actually a command) taken from Jade's
  871. source is,
  872.      (defun upcase-word (count)
  873.        "Makes the next COUNT words from the cursor upper-case."
  874.        (interactive "p")
  875.        (let
  876.            ((pos (forward-word count)))
  877.          (upcase-area (cursor-pos) pos)
  878.          (goto-char pos)))
  879. File: jade.info,  Node: Calling Functions,  Next: Mapping Functions,  Prev: Defining Functions,  Up: Functions
  880. Calling Functions
  881. -----------------
  882.    Most of the time function calls are done by the evaluator when it
  883. detects a function call form (*note List Forms::.); when the function
  884. to be called is not known until run-time it is easier to use a special
  885. function to call the function directly than create a custom form to
  886. apply to the `eval' function.
  887.  - Function: funcall FUNCTION &rest ARGS
  888.      Applies the argument values ARGS to the function FUNCTION, then
  889.      returns its result.
  890.      Note that the argument values ARGS are *not* evaluated again. This
  891.      also means that `funcall' can *not* be used to call macros or
  892.      special forms -- they would need the unevaluated versions of ARGS,
  893.      which are not available to `funcall'.
  894.           (funcall '+ 1 2 3)
  895.               => 6
  896.  - Function: apply FUNCTION &rest ARGS
  897.      Similar to `funcall' except that the last of its arguments is a
  898.      *list* of arguments which are appended to the other members of
  899.      ARGS to form the list of argument values to apply to the function
  900.      FUNCTION.
  901.      Constructs a list of arguments to apply to the function FUNCTION
  902.      from ARGS.
  903. File: jade.info,  Node: Mapping Functions,  Prev: Calling Functions,  Up: Functions
  904. Mapping Functions
  905. -----------------
  906.    A "mapping function" applies a function to each of a collection of
  907. objects. Jade currently has two mapping functions, `mapcar' and `mapc'.
  908.  - Function: mapcar FUNCTION LIST
  909.      Each element in the list LIST is individually applied to the
  910.      function FUNCTION. The values returned are made into a new list
  911.      which is returned.
  912.      The FUNCTION should be able to be called with one argument.
  913.           (mapcar '1+ '(1 2 3 4 5))
  914.               => (2 3 4 5 6)
  915.  - Function: mapc FUNCTION LIST
  916.      Similar to `mapcar' except that the values returned when each
  917.      element is applied to the function FUNCTION are discarded. The
  918.      value returned is LIST.
  919.      This function is generally used where the side effects of calling
  920.      the function are the important thing, not the results.
  921.    The two following functions are also mapping functions of a sort.
  922. They are variants of the `delete' function (*note Modifying Lists::.)
  923. and use predicate functions to classify the elements of the list which
  924. are to be deleted.
  925.  - Function: delete-if PREDICATE LIST
  926.      This function is a variant of the `delete' function. Instead of
  927.      comparing each element of LIST with a specified object, each
  928.      element of LIST is applied to the predicate function PREDICATE.
  929.      If it returns `t' (i.e. not `nil') then the element is
  930.      destructively removed from LIST.
  931.           (delete-if 'stringp '(1 "foo" 2 "bar" 3 "baz"))
  932.               => (1 2 3)
  933.  - Function: delete-if-not PREDICATE LIST
  934.      This function does the inverse of `delete-if'. It applies PREDICATE
  935.      to each element of LIST, if it returns `nil' then the element is
  936.      destructively removed from the list.
  937.           (delete-if-not 'stringp '(1 "foo" 2 "bar" 3 "baz"))
  938.               => ("foo" "bar" "baz")
  939. File: jade.info,  Node: Macros,  Next: Streams,  Prev: Functions,  Up: Programming Jade
  940. Macros
  941. ======
  942.    "Macros" are used to extend the Lisp language, they are basically a
  943. function which instead of returning its value, return a new form which
  944. will produce the macro call's value when evaluated.
  945.    When a function being compiled calls a macro the macro is expanded
  946. immediately and the resultant form is open-coded into the compiler's
  947. output.
  948. * Menu:
  949. * Defining Macros::             Macros are defined like functions
  950. * Macro Expansion::             How macros are used by the evaluator
  951. * Compiling Macros::            The compiler expands macros at compile-
  952.                                   time.
  953. File: jade.info,  Node: Defining Macros,  Next: Macro Expansion,  Up: Macros
  954. Defining Macros
  955. ---------------
  956.    Macros are defined in the same style as functions, the only
  957. difference is the name of the special form used to define them.
  958.    A macro object is a list whose car is the symbol `macro', its cdr is
  959. the function which creates the expansion of the macro when applied to
  960. the macro calls unevaluated arguments.
  961.  - Special Form: defmacro NAME LAMBDA-LIST BODY-FORMS...
  962.      Defines the macro stored in the function cell of the symbol NAME.
  963.      lAMBDA-LIST is the lambda-list specifying the arguments to the
  964.      macro (*note Lambda Expressions::.) and BODY-FORMS are the forms
  965.      evaluated when the macro is expanded. The first of BODY-FORMS may
  966.      be a documentation string describing the macro's use.
  967.    Here is a simple macro definition, it is a possible definition for
  968. the `when' construct (which might even be useful if `when' wasn't
  969. already defined as a special form...),
  970.      (defmacro when (condition &rest body)
  971.        "Evaluates CONDITION, if it's non-`nil' evaluates the BODY
  972.      forms."
  973.        (list 'if condition (cons 'progn body)))
  974. When a form of the type `(when C B ...)' is evaluated the macro
  975. definition of `when' expands to the form `(if C (progn B ...))' which
  976. is then evaluated to perform my when-construct.
  977.    When you define a macro ensure that the forms which produce the
  978. expansion have no side effects; it would fail spectacularly when you
  979. attempt to compile your program!
  980.