home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 2001 May / SGI Freeware 2001 May - Disc 3.iso / dist / fw_elisp-intro.idb / usr / freeware / info / emacs-lisp-intro.info-5.z / emacs-lisp-intro.info-5
Encoding:
GNU Info File  |  1998-10-28  |  49.4 KB  |  1,251 lines

  1. This is Info file emacs-lisp-intro.info, produced by Makeinfo version
  2. 1.67 from the input file emacs-lisp-intro.texi.
  3.  
  4.    This is an introduction to `Programming in Emacs Lisp', for people
  5. who are not programmers.
  6.  
  7.    Edition 1.05, 21 October 1997
  8.  
  9.    Copyright (C) 1990, '91, '92, '93, '94, '95, '97 Free Software
  10. Foundation, Inc.
  11.  
  12.    Permission is granted to make and distribute verbatim copies of this
  13. manual provided the copyright notice and this permission notice are
  14. preserved on all copies.
  15.  
  16.    Permission is granted to copy and distribute modified versions of
  17. this manual under the conditions for verbatim copying, provided also
  18. that the sections entitled "Copying" and "GNU General Public License"
  19. are included exactly as in the original, and provided that the entire
  20. resulting derived work is distributed under the terms of a permission
  21. notice identical to this one.
  22.  
  23.    Permission is granted to copy and distribute translations of this
  24. manual into another language, under the above conditions for modified
  25. versions, except that this permission notice may be stated in a
  26. translation approved by the Free Software Foundation.
  27.  
  28. 
  29. File: emacs-lisp-intro.info,  Node: beginning-of-buffer,  Next: Second Buffer Related Review,  Prev: insert-buffer,  Up: More Complex
  30.  
  31. Complete Definition of `beginning-of-buffer'
  32. ============================================
  33.  
  34.    The basic structure of the `beginning-of-buffer' function has
  35. already been discussed.  (*Note A Simplified `beginning-of-buffer'
  36. Definition: simplified-beginning-of-buffer.) This section describes the
  37. complex part of the definition.
  38.  
  39.    As previously described, when invoked without an argument,
  40. `beginning-of-buffer' moves the cursor to the beginning of the buffer,
  41. leaving the mark at the previous position.  However, when the command
  42. is invoked with a number between one and ten, the function considers
  43. that number to be a fraction of the length of the buffer, measured in
  44. tenths, and Emacs moves the cursor that fraction of the way from the
  45. beginning of the buffer.  Thus, you can either call this function with
  46. the key command `M-<', which will move the cursor to the beginning of
  47. the buffer, or with a key command such as `C-u 7 M-<' which will move
  48. the cursor to a point 70% of the way through the buffer.  If a number
  49. bigger than ten is used for the argument, it moves to the end of the
  50. buffer.
  51.  
  52.    The `beginning-of-buffer' function can be called with or without an
  53. argument.  The use of the argument is optional.
  54.  
  55. * Menu:
  56.  
  57. * Optional Arguments::
  58. * beginning-of-buffer opt arg::  Example with optional argument.
  59. * beginning-of-buffer complete::
  60.  
  61. 
  62. File: emacs-lisp-intro.info,  Node: Optional Arguments,  Next: beginning-of-buffer opt arg,  Prev: beginning-of-buffer,  Up: beginning-of-buffer
  63.  
  64. Optional Arguments
  65. ------------------
  66.  
  67.    Unless told otherwise, Lisp expects that a function with an argument
  68. in its function definition will be called with a value for that
  69. argument.  If that does not happen, you get an error and a message that
  70. says `Wrong number of arguments'.
  71.  
  72.    However, optional arguments are a feature of Lisp: a "keyword" may
  73. be used to tell the Lisp interpreter that an argument is optional.  The
  74. keyword is `&optional'.  (The `&' in front of `optional' is part of the
  75. keyword.)  In a function definition, if an argument follows the keyword
  76. `&optional', a value does not need to be passed to that argument when
  77. the function is called.
  78.  
  79.    The first line of the function definition of `beginning-of-buffer'
  80. therefore looks like this:
  81.  
  82.      (defun beginning-of-buffer (&optional arg)
  83.  
  84.    In outline, the whole function looks like this:
  85.  
  86.      (defun beginning-of-buffer (&optional arg)
  87.        "DOCUMENTATION..."
  88.        (interactive "P")
  89.        (push-mark)
  90.        (goto-char
  91.          (IF-THERE-IS-AN-ARGUMENT
  92.              FIGURE-OUT-WHERE-TO-GO
  93.            ELSE-GO-TO
  94.            (point-min))))
  95.  
  96.    The function is similar to `simplified-beginning-of-buffer' except
  97. that the `interactive' expression has `"P"' as an argument and the
  98. `goto-char' function is followed by an if-then-else expression that
  99. figures out where to put the cursor if there is an argument.
  100.  
  101.    The `"P"' in the `interactive' expression tells Emacs to pass a
  102. prefix argument, if there is one, to the function.  A prefix argument
  103. is made by typing the <META> key followed by a number, or by typing
  104. `C-u' and then a number (if you don't type a number, `C-u' defaults to
  105. 4).
  106.  
  107.    The true-or-false-test of the `if' expression is simple: it is
  108. simply the argument `arg'.  If `arg' has a value that is not `nil',
  109. which will be the case if `beginning-of-buffer' is called with an
  110. argument, then this true-or-false-test will return true and the
  111. then-part of the `if' expression will be evaluated.  On the other hand,
  112. if `beginning-of-buffer' is not called with an argument, the value of
  113. `arg' will be `nil' and the else-part of the `if' expression will be
  114. evaluated.  The else-part is simply `point-min', and when this is the
  115. outcome, the whole `goto-char' expression is `(goto-char (point-min))',
  116. which is how we saw the `beginning-of-buffer' function in its simplified
  117. form.
  118.  
  119. 
  120. File: emacs-lisp-intro.info,  Node: beginning-of-buffer opt arg,  Next: beginning-of-buffer complete,  Prev: Optional Arguments,  Up: beginning-of-buffer
  121.  
  122. `beginning-of-buffer' with an Argument
  123. --------------------------------------
  124.  
  125.    When `beginning-of-buffer' is called with an argument, an expression
  126. is evaluated which calculates what value to pass to `goto-char'.  This
  127. expression is rather complicated at first sight.  It includes an inner
  128. `if' expression and much arithmetic.  It looks like this:
  129.  
  130.      (if (> (buffer-size) 10000)
  131.          ;; Avoid overflow for large buffer sizes!
  132.          (* (prefix-numeric-value arg) (/ (buffer-size) 10))
  133.        (/
  134.         (+ 10
  135.            (*
  136.             (buffer-size) (prefix-numeric-value arg))) 10))
  137.  
  138.    Like other complex-looking expressions, this one can be distangled by
  139. looking at it as parts of a template, in this case, the template for an
  140. if-then-else expression.  When in skeletal form, the expression looks
  141. like this:
  142.  
  143.      (if (BUFFER-IS-LARGE
  144.          DIVIDE-BUFFER-SIZE-BY-10-AND-MULTIPLY-BY-ARG
  145.        ELSE-USE-ALTERNATE-CALCULATION
  146.  
  147.    The true-or-false-test of this inner `if' expression checks the size
  148. of the buffer.  The reason for this is that version 18 Emacs Lisp uses
  149. numbers that are no bigger than eight million or so (bigger numbers are
  150. not needed) and in the computation that follows, Emacs might try to use
  151. over-large numbers if the buffer were large.  The term `overflow',
  152. mentioned in the comment, means numbers that are over large.
  153.  
  154.    There are two cases:  if the buffer is large and if it is not.
  155.  
  156. * Menu:
  157.  
  158. * large-case::                  Division and multiplication in a large buffer.
  159. * small-case::                  Functions embedded in parentheses.
  160.  
  161. 
  162. File: emacs-lisp-intro.info,  Node: large-case,  Next: small-case,  Prev: beginning-of-buffer opt arg,  Up: beginning-of-buffer opt arg
  163.  
  164. What happens in a large buffer
  165. ..............................
  166.  
  167.    In `beginning-of-buffer', the inner `if' expression tests whether
  168. the size of the buffer is greater than 10,000 characters.  To do this,
  169. it uses the `>' function and the `buffer-size' function.  The line
  170. looks like this:
  171.  
  172.      (if (> (buffer-size) 10000)
  173.  
  174. When the buffer is large, the then-part of the `if' expression is
  175. evaluated.  It reads like this (after formatting for easy reading):
  176.  
  177.      (*
  178.        (prefix-numeric-value arg)
  179.        (/ (buffer-size) 10))
  180.  
  181. This expression is a multiplication, with two arguments to the function
  182. `*'.
  183.  
  184.    The first argument is `(prefix-numeric-value arg)'.  When `"P"' is
  185. used as the argument for `interactive', the value passed to the
  186. function as its argument is passed a "raw prefix argument", and not a
  187. number.  (It is a number in a list.)  To perform the arithmetic, a
  188. conversion is necessary, and `prefix-numeric-value' does the job.
  189.  
  190.    The second argument is `(/ (buffer-size) 10)'.  This expression
  191. divides the numeric value of the buffer by ten.  This produces a number
  192. that tells how many characters make up one tenth of the buffer size.
  193. (In Lisp, `/' is used for division, just as `*' is used for
  194. multiplication.)
  195.  
  196.    In the multiplication expression as a whole, this amount is
  197. multiplied by the value of the prefix argument--the multiplication
  198. looks like this:
  199.  
  200.      (* NUMERIC-VALUE-OF-PREFIX-ARG
  201.         NUMBER-OF-CHARACTERS-IN-ONE-TENTH-OF-THE-BUFFER)
  202.  
  203. If, for example, the prefix argument is `7', the one-tenth value will
  204. be multiplied by 7 to give a position 70% of the way through the buffer.
  205.  
  206.    The result of all this is that if the buffer is large, the
  207. `goto-char' expression reads like this:
  208.  
  209.      (goto-char (* (prefix-numeric-value arg)
  210.                    (/ (buffer-size) 10)))
  211.  
  212.    This puts the cursor where we want it.
  213.  
  214. 
  215. File: emacs-lisp-intro.info,  Node: small-case,  Prev: large-case,  Up: beginning-of-buffer opt arg
  216.  
  217. What happens in a small buffer
  218. ..............................
  219.  
  220.    If the buffer contains fewer than 10,000 characters, a slightly
  221. different computation is performed.  You might think this is not
  222. necessary, since the first computation could do the job.  However, in a
  223. small buffer, the first method may not put the cursor on exactly the
  224. desired line; the second method does a better job.
  225.  
  226.    The code looks like this:
  227.  
  228.      (/ (+ 10 (* (buffer-size) (prefix-numeric-value arg))) 10))
  229.  
  230. This is code in which you figure out what happens by discovering how the
  231. functions are embedded in parentheses.  It is easier to read if you
  232. reformat it with each expression indented more deeply than its
  233. enclosing expression:
  234.  
  235.        (/
  236.         (+ 10
  237.            (*
  238.             (buffer-size)
  239.             (prefix-numeric-value arg)))
  240.         10))
  241.  
  242. Looking at parentheses, we see that the innermost operation is
  243. `(prefix-numeric-value arg)', which converts the raw argument to a
  244. number.  This number is multiplied by the buffer size in the following
  245. expression:
  246.  
  247.      (* (buffer-size) (prefix-numeric-value arg)
  248.  
  249. This multiplication creates a number that may be larger than the size of
  250. the buffer--seven times larger if the argument is 7, for example.  Ten
  251. is then added to this number and finally the large number is divided by
  252. ten to provide a value that is one character larger than the percentage
  253. position in the buffer.
  254.  
  255.    The number that results from all this is passed to `goto-char' and
  256. the cursor is moved to that point.
  257.  
  258. 
  259. File: emacs-lisp-intro.info,  Node: beginning-of-buffer complete,  Prev: beginning-of-buffer opt arg,  Up: beginning-of-buffer
  260.  
  261. The Complete `beginning-of-buffer'
  262. ----------------------------------
  263.  
  264.    Here is the complete text of the `beginning-of-buffer' function:
  265.  
  266.      (defun beginning-of-buffer (&optional arg)
  267.        "Move point to the beginning of the buffer;
  268.      leave mark at previous position.
  269.      With arg N, put point N/10 of the way
  270.      from the true beginning.
  271.      Don't use this in Lisp programs!
  272.      \(goto-char (point-min)) is faster
  273.      and does not set the mark."
  274.        (interactive "P")
  275.        (push-mark)
  276.        (goto-char
  277.         (if arg
  278.             (if (> (buffer-size) 10000)
  279.                 ;; Avoid overflow for large buffer sizes!
  280.                 (* (prefix-numeric-value arg)
  281.                    (/ (buffer-size) 10))
  282.               (/ (+ 10 (* (buffer-size)
  283.                           (prefix-numeric-value arg)))
  284.                  10))
  285.           (point-min)))
  286.        (if arg (forward-line 1)))
  287.  
  288. Except for two small points, the previous discussion shows how this
  289. function works.  The first point deals with a detail in the
  290. documentation string, and the second point concerns the last line of
  291. the function.
  292.  
  293.    In the documentation string, there is reference to an expression:
  294.  
  295.      \(goto-char (point-min))
  296.  
  297. A `\' is used before the first parenthesis of this expression.  This
  298. `\' tells the Lisp interpreter that the expression should be printed as
  299. shown in the documentation rather than evaluated as a symbolic
  300. expression, which is what it looks like.
  301.  
  302.    Finally, the last line of the `beginning-of-buffer' command says to
  303. move point to the beginning of the next line if the command is invoked
  304. with an argument:
  305.  
  306.      (if arg (forward-line 1)))
  307.  
  308. This puts the cursor at the beginning of the first line after the
  309. appropriate tenths position in the buffer.  This is a flourish that
  310. means that the cursor is always located *at least* the requested tenths
  311. of the way through the buffer, which is a nicety that is, perhaps, not
  312. necessary, but which, if it did not occur, would be sure to draw
  313. complaints.
  314.  
  315. 
  316. File: emacs-lisp-intro.info,  Node: Second Buffer Related Review,  Next: &optional Exercise,  Prev: beginning-of-buffer,  Up: More Complex
  317.  
  318. Review
  319. ======
  320.  
  321.    Here is a brief summary of some of the topics covered in this
  322. chapter.
  323.  
  324. `or'
  325.      Evaluate each argument in sequence, and return the value of the
  326.      first argument that is not `nil'; if none return a value that is
  327.      not `nil', return `nil'.  In brief, return the first true value of
  328.      the arguments; return a true value if one *or* any of the other
  329.      are true.
  330.  
  331. `and'
  332.      Evaluate each argument in sequence, and if any are `nil', return
  333.      `nil'; if none are `nil', return the value of the last argument.
  334.      In brief, return a true value only if all the arguments are true;
  335.      return a true value if one *and* each of the others is true.
  336.  
  337. `&optional'
  338.      A keyword used to indicate that an argument to a function
  339.      definition is optional; this means that the function can be
  340.      evaluated without the argument, if desired.
  341.  
  342. `prefix-numeric-value'
  343.      Convert the `raw prefix argument' produced by `(interactive "P")'
  344.      to a numeric value.
  345.  
  346. `forward-line'
  347.      Move point forward to the beginning of the next line, or if the
  348.      argument is greater than one, forward that many lines.  If it
  349.      can't move as far forward as it is supposed to, `forward-line'
  350.      goes forward as far as it can and then returns a count of the
  351.      number of additional lines it was supposed to move but couldn't.
  352.  
  353. `erase-buffer'
  354.      Delete the entire contents of the current buffer.
  355.  
  356. `bufferp'
  357.      Return `t' if its argument is a buffer; otherwise return `nil'.
  358.  
  359. 
  360. File: emacs-lisp-intro.info,  Node: &optional Exercise,  Prev: Second Buffer Related Review,  Up: More Complex
  361.  
  362. `&optional' Argument Exercise
  363. =============================
  364.  
  365.    Write an interactive function with an optional argument that tests
  366. whether its argument, a number, is greater or less than the value of
  367. `fill-column', and tells you which, in a message.  However, if you do
  368. not pass an argument to the function, use 56 as a default value.
  369.  
  370. 
  371. File: emacs-lisp-intro.info,  Node: Narrowing & Widening,  Next: car cdr & cons,  Prev: More Complex,  Up: Top
  372.  
  373. Narrowing and Widening
  374. **********************
  375.  
  376.    Narrowing is a feature of Emacs that makes it possible for you to
  377. focus on a specific part of a buffer, and work without accidentally
  378. changing other parts.  Narrowing is normally disabled since it can
  379. confuse novices.
  380.  
  381. * Menu:
  382.  
  383. * narrowing advantages::        The Advantages of Narrowing
  384. * save-restriction::            The `save-restriction' special form.
  385. * what-line::                   The number of the line that point is on.
  386. * narrow Exercise::
  387.  
  388. 
  389. File: emacs-lisp-intro.info,  Node: narrowing advantages,  Next: save-restriction,  Prev: Narrowing & Widening,  Up: Narrowing & Widening
  390.  
  391. The Advantages of Narrowing
  392. ===========================
  393.  
  394.    With narrowing, the rest of a buffer is made invisible, as if it
  395. weren't there.  This is an advantage if, for example, you want to
  396. replace a word in one part of a buffer but not in another: you narrow
  397. to the part you want and the replacement is carried out only in that
  398. section, not in the rest of the buffer.  Searches will only work within
  399. a narrowed region, not outside of one, so if you are fixing a part of a
  400. document, you can keep yourself from accidentally finding parts you do
  401. not need to fix by narrowing just to the region you want.
  402.  
  403.    However, narrowing does make the rest of the buffer invisible, which
  404. can scare people who inadvertently invoke narrowing and think they have
  405. deleted a part of their file.  Moreover, the `undo' command (which is
  406. usually bound to `C-x u') does not turn off narrowing (nor should it),
  407. so people can become quite desperate if they do not know that they can
  408. return the rest of a buffer to visibility with the `widen' command.
  409. (In Emacs version 18, the key binding for `widen' is `C-x w'; in
  410. version 19, it is `C-x n w'.)
  411.  
  412.    Narrowing is just as useful to the Lisp interpreter as to a human.
  413. Often, an Emacs Lisp function is designed to work on just part of a
  414. buffer; or conversely, an Emacs Lisp function needs to work on all of a
  415. buffer that has been narrowed.  The `what-line' function, for example,
  416. removes the narrowing from a buffer, if it has any narrowing and when
  417. it has finished its job, restores the narrowing to what it was.  On the
  418. other hand, the `count-lines' function, which is called by `what-line',
  419. uses narrowing to restrict itself to just that portion of the buffer in
  420. which it is interested and then restores the previous situation.
  421.  
  422. 
  423. File: emacs-lisp-intro.info,  Node: save-restriction,  Next: what-line,  Prev: narrowing advantages,  Up: Narrowing & Widening
  424.  
  425. The `save-restriction' Special Form
  426. ===================================
  427.  
  428.    In Emacs Lisp, you can use the `save-restriction' special form to
  429. keep track of whatever narrowing is in effect, if any.  When the Lisp
  430. interpreter meets with `save-restriction', it executes the code in the
  431. body of the `save-restriction' expression, and then undoes any changes
  432. to narrowing that the code caused.  If, for example, the buffer is
  433. narrowed and the code that follows `save-restriction' gets rid of the
  434. narrowing, `save-restriction' returns the buffer to its narrowed region
  435. afterwards.  In the `what-line' command, any narrowing the buffer may
  436. have is undone by the `widen' command that immediately follows the
  437. `save-restriction' command.  Any original narrowing is restored just
  438. before the completion of the function.
  439.  
  440.    The template for a `save-restriction' expression is simple:
  441.  
  442.      (save-restriction
  443.        BODY... )
  444.  
  445. The body of the `save-restriction' is one or more expressions that will
  446. be evaluated in sequence by the Lisp interpreter.
  447.  
  448.    Finally, a point to note: when you use both `save-excursion' and
  449. `save-restriction', one right after the other, you should use
  450. `save-excursion' outermost.  If you write them in reverse order, you
  451. may fail to record narrowing in the buffer to which Emacs switches
  452. after calling `save-excursion'.  Thus, when written together,
  453. `save-excursion' and `save-restriction' should be written like this:
  454.  
  455.      (save-excursion
  456.        (save-restriction
  457.          BODY...))
  458.  
  459. 
  460. File: emacs-lisp-intro.info,  Node: what-line,  Next: narrow Exercise,  Prev: save-restriction,  Up: Narrowing & Widening
  461.  
  462. `what-line'
  463. ===========
  464.  
  465.    The `what-line' command tells you the number of the line in which
  466. the cursor is located.  The function illustrates the use of the
  467. `save-restriction' and `save-excursion' commands.  Here is the text of
  468. the function in full:
  469.  
  470.      (defun what-line ()
  471.        "Print the current line number (in the buffer) of point."
  472.        (interactive)
  473.        (save-restriction
  474.          (widen)
  475.          (save-excursion
  476.            (beginning-of-line)
  477.            (message "Line %d"
  478.                     (1+ (count-lines 1 (point)))))))
  479.  
  480.    The function has a documentation line and is interactive, as you
  481. would expect.  The next two lines use the functions `save-restriction'
  482. and `widen'.
  483.  
  484.    The `save-restriction' special form notes whatever narrowing is in
  485. effect, if any, in the current buffer and restores that narrowing after
  486. the code in the body of the `save-restriction' has been evaluated.
  487.  
  488.    The `save-restriction' special form is followed by `widen'.  This
  489. function undoes any narrowing the current buffer may have had when
  490. `what-line' was called.  (The narrowing that was there is the narrowing
  491. that `save-restriction' remembers.)  This widening makes it possible
  492. for the line counting commands to count from the beginning of the
  493. buffer.  Otherwise, they would have been limited to counting within the
  494. accessible region.  Any original narrowing is restored just before the
  495. completion of the function by the `save-restriction' special form.
  496.  
  497.    The call to `widen' is followed by `save-excursion', which saves the
  498. location of the cursor (i.e., of point) and of the mark, and restores
  499. them after the code in the body of the `save-excursion' uses the
  500. `beginning-of-line' function to move point.
  501.  
  502.    (Note that the `(widen)' expression comes between `save-restriction'
  503. and `save-excursion'.  When you write the two `save- ...' expressions
  504. in sequence, write `save-excursion' outermost.)
  505.  
  506.    The last two lines of the `what-line' function are functions to
  507. count the number of lines in the buffer and then print the number in the
  508. echo area.
  509.  
  510.      (message "Line %d"
  511.               (1+ (count-lines 1 (point)))))))
  512.  
  513.    The `message' function prints a one-line message at the bottom of the
  514. Emacs screen.  The first argument is inside of quotation marks and is
  515. printed as a string of characters.  However, it may contain `%d', `%s',
  516. or `%c' to print arguments that follow the string.  `%d' prints the
  517. argument as a decimal, so the message will say something such as `Line
  518. 243'.
  519.  
  520.    The number that is printed in place of the `%d' is computed by the
  521. last line of the function:
  522.  
  523.      (1+ (count-lines 1 (point)))
  524.  
  525. What this does is count the lines from the first position of the
  526. buffer, indicated by the `1', up to `(point)', and then add one to that
  527. number.  (The `1+' function adds one to its argument.)  We add one to
  528. it because line 2 has only one line before it, and `count-lines' counts
  529. only the lines *before* the current line.
  530.  
  531.    After `count-lines' has done it job, and the message has been
  532. printed in the echo area, the `save-excursion' restores point and mark
  533. to their original positions; and `save-restriction' restores the
  534. original narrowing, if any.
  535.  
  536. 
  537. File: emacs-lisp-intro.info,  Node: narrow Exercise,  Prev: what-line,  Up: Narrowing & Widening
  538.  
  539. Exercise with Narrowing
  540. =======================
  541.  
  542.    Write a function that will display the first 60 characters of the
  543. current buffer, even if you have narrowed the buffer to its latter half
  544. so that the first line is inaccessible.  Restore point, mark, and
  545. narrowing.  For this exercise, you need to use `save-restriction',
  546. `widen', `goto-char', `point-min', `buffer-substring', `message', and
  547. other functions, a whole potpourri.
  548.  
  549. 
  550. File: emacs-lisp-intro.info,  Node: car cdr & cons,  Next: Cutting & Storing Text,  Prev: Narrowing & Widening,  Up: Top
  551.  
  552. `car', `cdr', `cons': Fundamental Functions
  553. *******************************************
  554.  
  555.    In Lisp, `car', `cdr', and `cons' are fundamental functions.  The
  556. `cons' function is used to construct lists, and the `car' and `cdr'
  557. functions are used to take them apart.
  558.  
  559.    In the walk through of the `copy-region-as-kill' function, we will
  560. see `cons' as well as two variants on `cdr', namely, `setcdr' and
  561. `nthcdr'.  (*Note copy-region-as-kill::.)
  562.  
  563. * Menu:
  564.  
  565. * Strange Names::               An historical aside: why the strange names?
  566. * car & cdr::                   Functions for extracting part of a list.
  567. * cons::                        Constructing a list.
  568. * nthcdr::                      Calling `cdr' repeatedly.
  569. * setcar::                      Changing the first element of a list.
  570. * setcdr::                      Changing the rest of a list.
  571. * cons Exercise::
  572.  
  573. 
  574. File: emacs-lisp-intro.info,  Node: Strange Names,  Next: car & cdr,  Prev: car cdr & cons,  Up: car cdr & cons
  575.  
  576. Strange Names
  577. =============
  578.  
  579.    The name of the `cons' function is not unreasonable: it is an
  580. abbreviation of the word `construct'.  The origins of the names for
  581. `car' and `cdr', on the other hand, are esoteric: `car' is an acronym
  582. from the phrase `Contents of the Address part of the Register'; and
  583. `cdr' (pronounced `could-er') is an acronym from the phrase `Contents
  584. of the Decrement part of the Register'.  These phrases refer to
  585. specific pieces of hardware on the very early computer on which the
  586. original Lisp was developed.  Besides being obsolete, the phrases have
  587. been completely irrelevant for more than 25 years to anyone thinking
  588. about Lisp.  Nonetheless, although a few brave scholars have begun to
  589. use more reasonable names for these functions, the old terms are still
  590. in use.  In particular, since the terms are used in the Emacs Lisp
  591. source code, we will use them in this introduction.
  592.  
  593. 
  594. File: emacs-lisp-intro.info,  Node: car & cdr,  Next: cons,  Prev: Strange Names,  Up: car cdr & cons
  595.  
  596. `car' and `cdr'
  597. ===============
  598.  
  599.    The `car' of a list is, quite simply, the first item in the list.
  600. Thus the `car' of the list `(rose violet daisy buttercup)' is `rose'.
  601.  
  602.    If you are reading this in Info in GNU Emacs, you can see this by
  603. evaluating the following:
  604.  
  605.      (car '(rose violet daisy buttercup))
  606.  
  607. After evaluating the expression, `rose' will appear in the echo area.
  608.  
  609.    Clearly, a more reasonable name for the `car' function would be
  610. `first' and this is often suggested.
  611.  
  612.    `car' does not remove the first item from the list; it only reports
  613. what it is.  After `car' has been applied to a list, the list is still
  614. the same as it was.  In the jargon, `car' is `non-destructive'.  This
  615. feature turns out to be important.
  616.  
  617.    The `cdr' of a list is the rest of the list, that is, the `cdr'
  618. function returns the part of the list that follows the first item.
  619. Thus, while the `car' of the list `'(rose violet daisy buttercup)' is
  620. `rose', the rest of the list, the value returned by `cdr', is `(violet
  621. daisy buttercup)'.
  622.  
  623.    You can see this by evaluating the following in the usual way:
  624.  
  625.      (cdr '(rose violet daisy buttercup))
  626.  
  627. When you evaluate this, `(violet daisy buttercup)' will appear in the
  628. echo area.
  629.  
  630.    Like `car', `cdr' does not remove any elements from the list--it
  631. just returns a report of what the second and subsequent elements are.
  632.  
  633.    Incidentally, in the example, the list of flowers is quoted.  If it
  634. were not, the Lisp interpreter would try to evaluate the list by calling
  635. `rose' as a function.  In this example, we do not want to do that.
  636.  
  637.    Clearly, a more reasonable name for `cdr' would be `rest'.
  638.  
  639.    (There is a lesson here: when you name new functions, consider very
  640. carefully about what you are doing, since you may be stuck with the
  641. names for far longer than you expect.  The reason this document
  642. perpetuates these names is that the Emacs Lisp source code uses them,
  643. and if I did not use them, you would have a hard time reading the code;
  644. but do please try to avoid using these terms yourself.  The people who
  645. come after you will be grateful to you.)
  646.  
  647.    When `car' and `cdr' are applied to a list made up of symbols, such
  648. as the list `(pine fir oak maple)', the element of the list returned by
  649. the function `car' is the symbol `pine' without any parentheses around
  650. it.  `pine' is the first element in the list.  However, the `cdr' of
  651. the list is a list itself, `(fir oak maple)', as you can see by
  652. evaluating the following expressions in the usual way:
  653.  
  654.      (car '(pine fir oak maple))
  655.      
  656.      (cdr '(pine fir oak maple))
  657.  
  658.    On the other hand, in a list of lists, the first element is itself a
  659. list.  `car' returns this first element as a list.  For example, the
  660. following list contains three sub-lists, a list of carnivores, a list
  661. of herbivores and a list of sea mammals:
  662.  
  663.      (car '((lion tiger cheetah)
  664.             (gazelle antelope zebra)
  665.             (whale dolphin seal)))
  666.  
  667. In this case, the first element or `car' of the list is the list of
  668. carnivores, `(lion tiger cheetah)', and the rest of the list is
  669. `((gazelle antelope zebra) (whale dolphin seal))'.
  670.  
  671.      (cdr '((lion tiger cheetah)
  672.             (gazelle antelope zebra)
  673.             (whale dolphin seal)))
  674.  
  675.    It is worth saying again that `car' and `cdr' are
  676. non-destructive--that is, they do not modify or change lists to which
  677. they are applied.  This is very important for how they are used.
  678.  
  679.    Also, in the first chapter, in the discussion about atoms, I said
  680. that in Lisp, "certain kinds of atom, such as an array, can be separated
  681. into parts; but the mechanism for doing this is different from the
  682. mechanism for splitting a list.  As far as Lisp is concerned, the atoms
  683. of a list are unsplittable."  (*Note Lisp Atoms::.)  The `car' and
  684. `cdr' functions are used for splitting lists and are considered
  685. fundamental to Lisp.  Since they cannot split or gain access to the
  686. parts of an array, an array is considered an atom.  Conversely, the
  687. other fundamental function, `cons', can put together or construct a
  688. list, but not an array.  (Arrays are handled by array-specific
  689. functions.  *Note Arrays: (elisp)Arrays.)
  690.  
  691. 
  692. File: emacs-lisp-intro.info,  Node: cons,  Next: nthcdr,  Prev: car & cdr,  Up: car cdr & cons
  693.  
  694. `cons'
  695. ======
  696.  
  697.    The `cons' function constructs lists; it is the inverse of `car' and
  698. `cdr'.  For example, `cons' can be used to make a four element list
  699. from the three element list, `(fir oak maple)':
  700.  
  701.      (cons 'pine '(fir oak maple))
  702.  
  703. After evaluating this list, you will see
  704.  
  705.      (pine fir oak maple)
  706.  
  707. appear in the echo area.  `cons' puts a new element at the beginning of
  708. a list; it attaches or pushes elements onto the list.
  709.  
  710.    `cons' must have a list to attach to.(1)  You cannot start from
  711. absolutely nothing.  If you are building a list, you need to provide at
  712. least an empty list at the beginning.  Here is a series of `cons''s
  713. that build up a list of flowers.  If you are reading this in Info in
  714. GNU Emacs, you can evaluate each of the expressions in the usual way;
  715. the value is printed in this text after `=>', which you may read as
  716. `evaluates to'.
  717.  
  718.      (cons 'buttercup ())
  719.           => (buttercup)
  720.      
  721.      (cons 'daisy '(buttercup))
  722.           => (daisy buttercup)
  723.      
  724.      (cons 'violet '(daisy buttercup))
  725.           => (violet daisy buttercup)
  726.      
  727.      (cons 'rose '(violet daisy buttercup))
  728.           => (rose violet daisy buttercup)
  729.  
  730. In the first example, the empty list is shown as `()' and a list made
  731. up of `buttercup' followed by the empty list is constructed.  As you
  732. can see, the empty list is not shown in the list that was constructed.
  733. All that you see is `(buttercup)'.  The empty list is not counted as an
  734. element of a list because there is nothing in an empty list.  Generally
  735. speaking, an empty list is invisible.
  736.  
  737.    The second example, `(cons 'daisy '(buttercup))' constructs a new,
  738. two element list by putting `daisy' in front of `buttercup'; and the
  739. third example constructs a three element list by putting `violet' in
  740. front of `daisy' and `buttercup'.
  741.  
  742. * Menu:
  743.  
  744. * length::                      How to find the length of a list.
  745.  
  746.    ---------- Footnotes ----------
  747.  
  748.    (1)  Actually, you can `cons' an element to an atom to produce a
  749. dotted pair.  Dotted pairs are not discussed here; see *Note Dotted
  750. Pair Notation: (elisp)Dotted Pair Notation.
  751.  
  752. 
  753. File: emacs-lisp-intro.info,  Node: length,  Prev: cons,  Up: cons
  754.  
  755. Find the Length of a List: `length'
  756. -----------------------------------
  757.  
  758.    You can find out how many elements there are in a list by using the
  759. Lisp function `length', as in the following examples:
  760.  
  761.      (length '(buttercup))
  762.           => 1
  763.      
  764.      (length '(daisy buttercup))
  765.           => 2
  766.      
  767.      (length (cons 'violet '(daisy buttercup)))
  768.           => 3
  769.  
  770. In the third example, the `cons' function is used to construct a three
  771. element list which is then passed to the `length' function as its
  772. argument.
  773.  
  774.    We can also use `length' to count the number of elements in an empty
  775. list:
  776.  
  777.      (length ())
  778.           => 0
  779.  
  780. As you would expect, the number of elements in an empty list is zero.
  781.  
  782.    An interesting experiment is to find out what happens if you try to
  783. find the length of no list at all; that is, if you try to call `length'
  784. without giving it an argument, not even an empty list:
  785.  
  786.      (length )
  787.  
  788. What you see, if you evaluate this, is the error message
  789.  
  790.      Wrong number of arguments: #<subr length>, 0
  791.  
  792. This means that the function receives the wrong number of arguments,
  793. zero, when it expects some other number of arguments.  In this case,
  794. one argument is expected, the argument being a list whose length the
  795. function is measuring.  (Note that *one* list is *one* argument, even
  796. if the list has many elements inside it.)
  797.  
  798.    The part of the error message that says `#<subr length>' is the name
  799. of the function.  This is written with a special notation, `#<subr',
  800. that indicates that the function `length' is one of the primitive
  801. functions written in C rather than in Emacs Lisp.  (`subr' is an
  802. abbreviation for `subroutine'.)  *Note What Is a Function?: (elisp)What
  803. Is a Function, for more about subroutines.
  804.  
  805. 
  806. File: emacs-lisp-intro.info,  Node: nthcdr,  Next: setcar,  Prev: cons,  Up: car cdr & cons
  807.  
  808. `nthcdr'
  809. ========
  810.  
  811.    The `nthcdr' function is associated with the `cdr' function.  What
  812. it does is take the `cdr' of a list repeatedly.
  813.  
  814.    If you take the `cdr' of the list `(pine fir oak maple)', you will
  815. be returned the list `(fir oak maple)'.  If you repeat this on what was
  816. returned, you will be returned the list `(oak maple)'.  (Of course,
  817. repeated `cdr'ing on the original list will just give you the original
  818. `cdr' since the function does not change the list.  You need to
  819. evaluate the `cdr' of the `cdr' and so on.)  If you continue this,
  820. eventually you will be returned an empty list, which in this case,
  821. instead of being shown as `()' is shown as `nil'.
  822.  
  823.    For review, here is a series of repeated `cdr's, the text following
  824. the `=>' shows what is returned.
  825.  
  826.      (cdr '(pine fir oak maple))
  827.           =>(fir oak maple)
  828.      
  829.      (cdr '(fir oak maple))
  830.           => (oak maple)
  831.      
  832.      (cdr '(oak maple))
  833.           =>(maple)
  834.      
  835.      (cdr '(maple))
  836.           => nil
  837.      
  838.      (cdr 'nil)
  839.           => nil
  840.      
  841.      (cdr ())
  842.           => nil
  843.  
  844.    You can also do several `cdr's without printing the values in
  845. between, like this:
  846.  
  847.      (cdr (cdr '(pine fir oak maple)))
  848.           => (oak maple)
  849.  
  850. In this case, the Lisp interpreter evaluates the innermost list first.
  851. The innermost list is quoted, so it just passes the list as it is to the
  852. innermost `cdr'.  This `cdr' passes a list made up of the second and
  853. subsequent elements of the list to the outermost `cdr', which produces
  854. a list composed of the third and subsequent elements of the original
  855. list.  In this example, the `cdr' function is repeated and returns a
  856. list that consists of the original list without its first two elements.
  857.  
  858.    The `nthcdr' function does the same as repeating the call to `cdr'.
  859. In the following example, the argument 2 is passed to the function
  860. `nthcdr', along with the list, and the value returned is the list
  861. without its first two items, which is exactly the same as repeating
  862. `cdr' twice on the list:
  863.  
  864.      (nthcdr 2 '(pine fir oak maple))
  865.           => (oak maple)
  866.  
  867.    Using the original four element list, we can see what happens when
  868. various numeric arguments are passed to `nthcdr', including 0, 1, and 5:
  869.  
  870.      ;; Leave the list as it was.
  871.      (nthcdr 0 '(pine fir oak maple))
  872.           => (pine fir oak maple)
  873.      
  874.      ;; Return a copy without the first element.
  875.      (nthcdr 1 '(pine fir oak maple))
  876.           => (fir oak maple)
  877.      
  878.      ;; Return a copy of the list without three elements.
  879.      (nthcdr 3 '(pine fir oak maple))
  880.           => (maple)
  881.      
  882.      ;; Return a copy lacking all four elements.
  883.      (nthcdr 4 '(pine fir oak maple))
  884.           => nil
  885.      
  886.      ;; Return a copy lacking all elements.
  887.      (nthcdr 5 '(pine fir oak maple))
  888.           => nil
  889.  
  890.    It is worth mentioning that `nthcdr', like `cdr', does not change
  891. the original list--the function is non-destructive.  This is in sharp
  892. contrast to the `setcar' and `setcdr' functions.
  893.  
  894. 
  895. File: emacs-lisp-intro.info,  Node: setcar,  Next: setcdr,  Prev: nthcdr,  Up: car cdr & cons
  896.  
  897. `setcar'
  898. ========
  899.  
  900.    As you might guess from their names, the `setcar' and `setcdr'
  901. functions set the `car' or the `cdr' of a list to a new value.  They
  902. actually change the original list, unlike `car' and `cdr' which leave
  903. the original list as it was.  One way to find out how this works is to
  904. experiment.  We will start with the `setcar' function.
  905.  
  906.    First, we can make a list and then set the value of a variable to the
  907. list, using the `setq' function.  Here is a list of animals:
  908.  
  909.      (setq animals '(giraffe antelope tiger lion))
  910.  
  911. If you are reading this in Info inside of GNU Emacs, you can evaluate
  912. this expression in the usual fashion, by positioning the cursor after
  913. the expression and typing `C-x C-e'.  (I'm doing this right here as I
  914. write this.  This is one of the advantages of having the interpreter
  915. built into the computing environment.)
  916.  
  917.    When we evaluate the variable `animals', we see that it is bound to
  918. the list `(giraffe antelope tiger lion)':
  919.  
  920.      animals
  921.           => (giraffe antelope tiger lion)
  922.  
  923. Put another way, the variable `animals' points to the list `(giraffe
  924. antelope tiger lion)'.
  925.  
  926.    Next, evaluate the function `setcar' while passing it two arguments,
  927. the variable `animals' and the quoted symbol `hippopotamus'; this is
  928. done by writing the three element list `(setcar animals 'hippopotamus)'
  929. and then evaluating it in the usual fashion:
  930.  
  931.      (setcar animals 'hippopotamus)
  932.  
  933. After evaluating this expression, evaluate the variable `animals'
  934. again.  You will see that the list of animals has changed:
  935.  
  936.      animals
  937.           => (hippopotamus antelope tiger lion)
  938.  
  939. The first element on the list, `giraffe' is replaced by `hippopotamus'.
  940.  
  941.    So we can see that `setcar' did not add a new element to the list as
  942. `cons' would have; it replaced `giraffe' with `hippopotamus'; it
  943. *changed* the list.
  944.  
  945. 
  946. File: emacs-lisp-intro.info,  Node: setcdr,  Next: cons Exercise,  Prev: setcar,  Up: car cdr & cons
  947.  
  948. `setcdr'
  949. ========
  950.  
  951.    The `setcdr' function is similar to the `setcar' function, except
  952. that the function replaces the second and subsequent elements of a list
  953. rather than the first element.
  954.  
  955.    To see how this works, set the value of the variable to a list of
  956. domesticated animals by evaluating the following expression:
  957.  
  958.      (setq domesticated-animals '(horse cow sheep goat))
  959.  
  960. If you now evaluate the list, you will be returned the list `(horse cow
  961. sheep goat)':
  962.  
  963.      domesticated-animals
  964.           => (horse cow sheep goat)
  965.  
  966.    Next, evaluate `setcdr' with two arguments, the name of the variable
  967. which has a list as its value, and the list to which the `cdr' of the
  968. first list will be set;
  969.  
  970.      (setcdr domesticated-animals '(cat dog))
  971.  
  972. If you evaluate this expression, the list `(cat dog)' will appear in
  973. the echo area.  This is the value returned by the function.  The result
  974. we are interested in is the "side effect", which we can see by
  975. evaluating the variable `domesticated-animals':
  976.  
  977.      domesticated-animals
  978.           => (horse cat dog)
  979.  
  980. Indeed, the list is changed from `(horse cow sheep goat)' to `(horse
  981. cat dog)'.  The `cdr' of the list is changed from `(cow sheep goat)' to
  982. `(cat dog)'.
  983.  
  984. 
  985. File: emacs-lisp-intro.info,  Node: cons Exercise,  Prev: setcdr,  Up: car cdr & cons
  986.  
  987. Exercise
  988. ========
  989.  
  990.    Construct a list of four birds by evaluating several expressions with
  991. `cons'.  Find out what happens when you `cons' a list onto itself.
  992. Replace the first element of the list of four birds with a fish.
  993. Replace the rest of that list with a list of other fish.
  994.  
  995. 
  996. File: emacs-lisp-intro.info,  Node: Cutting & Storing Text,  Next: List Implementation,  Prev: car cdr & cons,  Up: Top
  997.  
  998. Cutting and Storing Text
  999. ************************
  1000.  
  1001.    Whenever you cut or clip text out of a buffer with a `kill' command
  1002. in GNU Emacs, it is stored in a list and you can bring it back with a
  1003. `yank' command.
  1004.  
  1005.    (The use of the word `kill' in Emacs for processes which specifically
  1006. *do not* destroy the values of the entities is an unfortunate
  1007. historical accident.  A much more appropriate word would be `clip' since
  1008. that is what the kill commands do; they clip text out of a buffer and
  1009. put it into storage from which it can be brought back.  I have often
  1010. been tempted to replace globally all occurrences of `kill' in the Emacs
  1011. sources with `clip' and all occurrences of `killed' with `clipped'.)
  1012.  
  1013. * Menu:
  1014.  
  1015. * Storing Text::                Text is stored in a list.
  1016. * zap-to-char::                 Cutting out text up to a character.
  1017. * kill-region::                 Cutting text out of a region.
  1018. * delete-region::               A digression into C.
  1019. * defvar::                      How to give a variable an initial value.
  1020. * copy-region-as-kill::         A definition for copying text.
  1021. * cons & search-fwd Review::
  1022. * search Exercises::
  1023.  
  1024. 
  1025. File: emacs-lisp-intro.info,  Node: Storing Text,  Next: zap-to-char,  Prev: Cutting & Storing Text,  Up: Cutting & Storing Text
  1026.  
  1027. Storing Text in a List
  1028. ======================
  1029.  
  1030.    When text is cut out of a buffer, it is stored on a list.  Successive
  1031. pieces of text are stored on the list successively, so the list might
  1032. look like this:
  1033.  
  1034.      ("a piece of text" "last piece")
  1035.  
  1036. The function `cons' can be used to add a piece of text to the list,
  1037. like this:
  1038.  
  1039.      (cons "another piece"
  1040.            '("a piece of text" "last piece"))
  1041.  
  1042. If you evaluate this expression, a list of three elements will appear in
  1043. the echo area:
  1044.  
  1045.      ("another piece" "a piece of text" "last piece")
  1046.  
  1047.    With the `car' and `nthcdr' functions, you can retrieve whichever
  1048. piece of text you want.  For example, in the following code, `nthcdr 1
  1049. ...' returns the list with the first item removed; and the `car'
  1050. returns the first element of that remainder--the second element of the
  1051. original list:
  1052.  
  1053.      (car (nthcdr 1 '("another piece"
  1054.                       "a piece of text"
  1055.                       "last piece")))
  1056.           => "a piece of text"
  1057.  
  1058.    The actual functions in Emacs are more complex than this, of course.
  1059. The code for cutting and retrieving text has to be written so that
  1060. Emacs can figure out which element in the list you want--the first,
  1061. second, third, or whatever.  In addition, when you get to the end of
  1062. the list, Emacs should give you the first element of the list, rather
  1063. than nothing at all.
  1064.  
  1065.    The list that holds the pieces of text is called the "kill ring".
  1066. This chapter leads up to a description of the kill ring and how it is
  1067. used by first tracing how the `zap-to-char' function works.  This
  1068. function uses (or `calls') a function that invokes a function that
  1069. manipulates the kill ring.  Thus, before reaching the mountains, we
  1070. climb the foothills.
  1071.  
  1072.    A subsequent chapter describes how text that is cut from the buffer
  1073. is retrieved.  *Note Yanking Text Back: Yanking.
  1074.  
  1075. 
  1076. File: emacs-lisp-intro.info,  Node: zap-to-char,  Next: kill-region,  Prev: Storing Text,  Up: Cutting & Storing Text
  1077.  
  1078. `zap-to-char'
  1079. =============
  1080.  
  1081.    The `zap-to-char' function is written differently in GNU Emacs
  1082. version 18 and version 19.  The version 19 implementation is simpler,
  1083. and works slightly differently.  We will first show the function as it
  1084. is written for version 19 and then for version 18.
  1085.  
  1086.    The Emacs version 19 implementation of the interactive `zap-to-char'
  1087. function removes the text in the region between the location of the
  1088. cursor (i.e., of point) up to and including the next occurrence of a
  1089. specified character.  The text that `zap-to-char' removes is put in the
  1090. kill ring; and it can be retrieved from the kill ring by typing `C-y'
  1091. (`yank').  If the command is given an argument, it removes text through
  1092. that number of occurrences.  Thus, if the cursor were at the beginning
  1093. of this sentence and the character were `s', `Thus' would be removed.
  1094. If the argument were two, `Thus, if the curs' would be removed, up to
  1095. and including the `s' in `cursor'.
  1096.  
  1097.    The Emacs version 18 implementation removes the text from point up to
  1098. *but not including* the specified character.  Thus, in the example
  1099. shown in the previous paragraph, the `s' would *not* be removed.
  1100.  
  1101.    In addition, the version 18 implementation will go to the end of the
  1102. buffer if the specified character is not found; but the version 19
  1103. implementation will simply generate an error (and not remove any text).
  1104.  
  1105.    In order to determine how much text to remove, both versions of
  1106. `zap-to-char' use a search function.  Searches are used extensively in
  1107. code that manipulates text, and it is worth focusing attention on the
  1108. search function as well as on the deletion command.
  1109.  
  1110.    Here is the complete text of the version 19 implementation of the
  1111. function:
  1112.  
  1113.      (defun zap-to-char (arg char)  ; version 19 implementation
  1114.        "Kill up to and including ARG'th occurrence of CHAR.
  1115.      Goes backward if ARG is negative; error if CHAR not found."
  1116.        (interactive "*p\ncZap to char: ")
  1117.        (kill-region (point)
  1118.                     (progn
  1119.                       (search-forward
  1120.                        (char-to-string char) nil nil arg)
  1121.                       (point))))
  1122.  
  1123. * Menu:
  1124.  
  1125. * zap-to-char interactive::     A three part interactive expression.
  1126. * zap-to-char body::            A short overview.
  1127. * search-forward::              How to search for a string.
  1128. * progn::                       The `progn' function.
  1129. * Summing up zap-to-char::      Using `point' and `search-forward'.
  1130. * v-18-zap-to-char::            The version 18 implementation.
  1131.  
  1132. 
  1133. File: emacs-lisp-intro.info,  Node: zap-to-char interactive,  Next: zap-to-char body,  Prev: zap-to-char,  Up: zap-to-char
  1134.  
  1135. The `interactive' Expression
  1136. ----------------------------
  1137.  
  1138.    The interactive expression in the `zap-to-char' command looks like
  1139. this:
  1140.  
  1141.      (interactive "*p\ncZap to char: ")
  1142.  
  1143.    The part within quotation marks, `"*p\ncZap to char: "', specifies
  1144. three different things.  First, and most simply, the asterisk, `*',
  1145. causes an error to be signalled if the buffer is read-only.  This means
  1146. that if you try `zap-to-char' in a read-only buffer you will not be
  1147. able to remove text, and you will receive a message that says "Buffer is
  1148. read-only"; your terminal may beep at you as well.
  1149.  
  1150.    The second part of `"*p\ncZap to char: "' is the `p'.  This part is
  1151. ended by a newline, `\n'.  The `p' means that the first argument to the
  1152. function will be passed the value of a `processed prefix'.  The prefix
  1153. argument is passed by typing `C-u' and a number, or `M-' and a number.
  1154. If the function is called interactively without a prefix, 1 is passed
  1155. to this argument.
  1156.  
  1157.    The third part of `"*p\ncZap to char: "' is `cZap to char: '.  In
  1158. this part, the lower case `c' indicates that `interactive' expects a
  1159. prompt and that the argument will be a character.  The prompt follows
  1160. the `c' and is the string `Zap to char: ' (with a space after the colon
  1161. to make it look good).
  1162.  
  1163.    What all this does is prepare the arguments to `zap-to-char' so they
  1164. are of the right type, and give the user a prompt.
  1165.  
  1166. 
  1167. File: emacs-lisp-intro.info,  Node: zap-to-char body,  Next: search-forward,  Prev: zap-to-char interactive,  Up: zap-to-char
  1168.  
  1169. The Body of `zap-to-char'
  1170. -------------------------
  1171.  
  1172.    The body of the `zap-to-char' function contains the code that kills
  1173. (that is, removes) the text in the region from the current position of
  1174. the cursor up to and including the specified character.  The first part
  1175. of the code looks like this:
  1176.  
  1177.      (kill-region (point) ...
  1178.  
  1179. `(point)' is the current position of the cursor.
  1180.  
  1181.    The next part of the code is an expression using `progn'.  The body
  1182. of the `progn' consists of calls to `search-forward' and `point'.
  1183.  
  1184.    It is easier to understand how `progn' works after learning about
  1185. `search-forward', so we will look at `search-forward' and then at
  1186. `progn'.
  1187.  
  1188. 
  1189. File: emacs-lisp-intro.info,  Node: search-forward,  Next: progn,  Prev: zap-to-char body,  Up: zap-to-char
  1190.  
  1191. The `search-forward' Function
  1192. -----------------------------
  1193.  
  1194.    The `search-forward' function is used to locate the
  1195. zapped-for-character in `zap-to-char'.  If the search is successful,
  1196. `search-forward' leaves point immediately after the last character in
  1197. the target string.  (In this case the target string is just one
  1198. character long.)  If the search is backwards, `search-forward' leaves
  1199. point just before the first character in the target.  Also,
  1200. `search-forward' returns `t' for true.  (Moving point is therefore a
  1201. `side effect'.)
  1202.  
  1203.    In `zap-to-char', the `search-forward' function looks like this:
  1204.  
  1205.      (search-forward (char-to-string char) nil nil arg)
  1206.  
  1207.    The `search-forward' function takes four arguments:
  1208.  
  1209.   1. The first argument is the target, what is searched for.  This must
  1210.      be a string, such as `"z"'.
  1211.  
  1212.      As it happens, the argument passed to `zap-to-char' is a single
  1213.      character.  Because of the way computers are built, the Lisp
  1214.      interpreter treats a single character as being different from a
  1215.      string of characters.  Inside the computer, a single character has
  1216.      a different electronic format than a string of one character.  (A
  1217.      single character can often be recorded in the computer using
  1218.      exactly one byte; but a string may be longer or shorter, and the
  1219.      computer needs to be ready for this.)  Since the `search-forward'
  1220.      function searches for a string, the character that the
  1221.      `zap-to-char' function receives as its argument must be converted
  1222.      inside the computer from one format to the other; otherwise the
  1223.      `search-forward' function will fail.  The `char-to-string'
  1224.      function is used to make this conversion.
  1225.  
  1226.   2. The second argument bounds the search; it is specified as a
  1227.      position in the buffer.  In this case, the search can go to the
  1228.      end of the buffer, so no bound is set and the second argument is
  1229.      `nil'.
  1230.  
  1231.   3. The third argument tells the function what it should do if the
  1232.      search fails--it can signal an error (and print a message) or it
  1233.      can return `nil'.  A `nil' as the third argument causes the
  1234.      function to signal an error when the search fails.
  1235.  
  1236.   4. The fourth argument to `search-forward' is the repeat count--how
  1237.      many occurrences of the string to look for.  This argument is
  1238.      optional and if the function is called without a repeat count,
  1239.      this argument is passed the value 1.  If this argument is
  1240.      negative, the search goes backwards.
  1241.  
  1242.    In template form, a `search-forward' expression looks like this:
  1243.  
  1244.      (search-forward "TARGET-STRING"
  1245.                      LIMIT-OF-SEARCH
  1246.                      WHAT-TO-DO-IF-SEARCH-FAILS
  1247.                      REPEAT-COUNT)
  1248.  
  1249.    We will look at `progn' next.
  1250.  
  1251.