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-7.z / emacs-lisp-intro.info-7
Encoding:
GNU Info File  |  1998-10-28  |  49.5 KB  |  1,212 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: kill-ring-yank-pointer,  Next: yank nthcdr Exercises,  Prev: Kill Ring Overview,  Up: Yanking
  30.  
  31. The `kill-ring-yank-pointer' Variable
  32. =====================================
  33.  
  34.    `kill-ring-yank-pointer' is a variable, just as `kill-ring' is a
  35. variable.  It points to something by being bound to the value of what
  36. it points to, like any other Lisp variable.
  37.  
  38.    Thus, if the value of the kill ring is:
  39.  
  40.      ("some text" "a different piece of text" "yet more text")
  41.  
  42. and the `kill-ring-yank-pointer' points to the second clause, the value
  43. of `kill-ring-yank-pointer' is:
  44.  
  45.      ("a different piece of text" "yet more text")
  46.  
  47.    As explained in the previous chapter (*note List Implementation::.),
  48. the computer does not keep two different copies of the text being
  49. pointed to by both the `kill-ring' and the `kill-ring-yank-pointer'.
  50. The words "a different piece of text" and "yet more text" are not
  51. duplicated.  Instead, the two Lisp variables point to the same pieces of
  52. text.  Here is a diagram:
  53.  
  54.      kill-ring     kill-ring-yank-pointer
  55.          |               |
  56.          |      ___ ___  |     ___ ___      ___ ___
  57.           ---> |   |   |  --> |   |   |    |   |   |
  58.                |___|___|----> |___|___|--> |___|___|--> nil
  59.                  |              |            |
  60.                  |              |            |
  61.                  |              |             --> "yet more text"
  62.                  |              |
  63.                  |               --> "a different piece of text
  64.                  |
  65.                   --> "some text"
  66.  
  67.    Both the variable `kill-ring' and the variable
  68. `kill-ring-yank-pointer' are pointers.  But the kill ring itself is
  69. usually described as if it were actually what it is composed of.  The
  70. `kill-ring' is spoken of as if it were the list rather than that it
  71. points to the list.  Conversely, the `kill-ring-yank-pointer' is spoken
  72. of as pointing to a list.
  73.  
  74.    These two ways of talking about the same thing sound confusing at
  75. first but make sense on reflection.  The kill ring is generally thought
  76. of as the complete structure of data that holds the information of what
  77. has recently been cut out of the Emacs buffers.  The
  78. `kill-ring-yank-pointer' on the other hand, serves to indicate--that
  79. is, to `point to'--that part of the kill ring of which the first
  80. element (the CAR) will be inserted.
  81.  
  82.    The `rotate-yank-pointer' function changes the element in the kill
  83. ring to which the `kill-ring-yank-pointer' points; when the pointer is
  84. set to point to the next element beyond the end of the kill ring, it
  85. automatically sets it to point to the first element of the kill ring.
  86. This is how the list is transformed into a ring.  The
  87. `rotate-yank-pointer' function itself is not difficult, but contains
  88. many details.  It and the much simpler `yank' and `yank-pop' functions
  89. are described in an appendix.  *Note Handling the Kill Ring: Kill Ring.
  90.  
  91. 
  92. File: emacs-lisp-intro.info,  Node: yank nthcdr Exercises,  Prev: kill-ring-yank-pointer,  Up: Yanking
  93.  
  94. Exercises with `yank' and `nthcdr'
  95. ==================================
  96.  
  97.    * Using `C-h v' (`describe-variable'), look at the value of your
  98.      kill ring.  Add several items to your kill ring; look at its value
  99.      again.  Using `M-y' (`yank-pop)', move all the way around the kill
  100.      ring.  How many items were in your kill ring?  Find the value of
  101.      `kill-ring-max'.  Was your kill ring full, or could you have kept
  102.      more blocks of text within it?
  103.  
  104.    * Using `nthcdr' and `car', construct a series of expressions to
  105.      return the first, second, third, and fourth elements of a list.
  106.  
  107. 
  108. File: emacs-lisp-intro.info,  Node: Loops & Recursion,  Next: Regexp Search,  Prev: Yanking,  Up: Top
  109.  
  110. Loops and Recursion
  111. *******************
  112.  
  113.    Emacs Lisp has two primary ways to cause an expression, or a series
  114. of expressions, to be evaluated repeatedly: one uses a `while' loop,
  115. and the other uses "recursion".
  116.  
  117.    Repetition can be very valuable.  For example, to move forward four
  118. sentences, you need only write a program that will move forward one
  119. sentence and then repeat the process four times.  Since a computer does
  120. not get bored or tired, such repetitive action does not have the
  121. deleterious effects that excessive or the wrong kinds of repetition can
  122. have on humans.
  123.  
  124. * Menu:
  125.  
  126. * while::                       Causing a stretch of code to repeat.
  127. * Recursion::                   Causing a function to call itself.
  128. * Looping exercise::
  129.  
  130. 
  131. File: emacs-lisp-intro.info,  Node: while,  Next: Recursion,  Prev: Loops & Recursion,  Up: Loops & Recursion
  132.  
  133. `while'
  134. =======
  135.  
  136.    The `while' special form tests whether the value returned by
  137. evaluating its first argument is true or false.  This is similar to what
  138. the Lisp interpreter does with an `if'; what the interpreter does next,
  139. however, is different.
  140.  
  141.    In a `while' expression, if the value returned by evaluating the
  142. first argument is false, the Lisp interpreter skips the rest of the
  143. expression (the "body" of the expression) and does not evaluate it.
  144. However, if the value is true, the Lisp interpreter evaluates the body
  145. of the expression and then again tests whether the first argument to
  146. `while' is true or false.  If the value returned by evaluating the
  147. first argument is again true, the Lisp interpreter again evaluates the
  148. body of the expression.
  149.  
  150.    The template for a `while' expression looks like this:
  151.  
  152.      (while TRUE-OR-FALSE-TEST
  153.        BODY...)
  154.  
  155.    So long as the true-or-false-test of the `while' expression returns
  156. a true value when it is evaluated, the body is repeatedly evaluated.
  157. This process is called a loop since the Lisp interpreter repeats the
  158. same thing again and again, like an airplane doing a loop.  When the
  159. result of evaluating the true-or-false-test is false, the Lisp
  160. interpreter does not evaluate the rest of the `while' expression and
  161. `exits the loop'.
  162.  
  163.    Clearly, if the value returned by evaluating the first argument to
  164. `while' is always true, the body following will be evaluated again and
  165. again ... and again ... forever.  Conversely, if the value returned is
  166. never true, the expressions in the body will never be evaluated.  The
  167. craft of writing a `while' loop consists of choosing a mechanism such
  168. that the true-or-false-test returns true just the number of times that
  169. you want the subsequent expressions to be evaluated, and then have the
  170. test return false.
  171.  
  172.    The value returned by evaluating a `while' is the value of the
  173. true-or-false-test.  An interesting consequence of this is that a
  174. `while' loop that evaluates without error will return `nil' or false
  175. regardless of whether it has looped 1 or 100 times or none at all.  A
  176. `while' expression that evaluates successfully never returns a true
  177. value!  What this means is that `while' is always evaluated for its
  178. side effects, which is to say, the consequences of evaluating the
  179. expressions within the body of the `while' loop.  This makes sense.  It
  180. is not the mere act of looping that is desired, but the consequences of
  181. what happens when the expressions in the loop are repeatedly evaluated.
  182.  
  183. * Menu:
  184.  
  185. * Loop Example::                A `while' loop that uses a list.
  186. * print-elements-of-list::      Uses `while', `car', `cdr'.
  187. * Incrementing Loop::           A loop with an incrementing counter.
  188. * Decrementing Loop::           A loop with a decrementing counter.
  189.  
  190. 
  191. File: emacs-lisp-intro.info,  Node: Loop Example,  Next: print-elements-of-list,  Prev: while,  Up: while
  192.  
  193. A `while' Loop and a List
  194. -------------------------
  195.  
  196.    A common way to control a `while' loop is to test whether a list has
  197. any elements.  If it does, the loop is repeated; but if it does not,
  198. the repetition is ended.  Since this is an important technique, we will
  199. create a short example to illustrate it.
  200.  
  201.    A simple way to test whether a list has elements is to evaluate the
  202. list: if it has no elements, it is an empty list and will return the
  203. empty list, `()', which is a synonym for `nil' or false.  On the other
  204. hand, a list with elements will return those elements when it is
  205. evaluated.  Since Lisp considers as true any value that is not `nil', a
  206. list that returns elements will test true in a `while' loop.
  207.  
  208.    For example, you can set the variable `empty-list' to `nil' by
  209. evaluating the following `setq' expression:
  210.  
  211.      (setq empty-list ())
  212.  
  213. After evaluating the `setq' expression, you can evaluate the variable
  214. `empty-list' in the usual way, by placing the cursor after the symbol
  215. and typing `C-x C-e'; `nil' will appear in your echo area:
  216.  
  217.      empty-list
  218.  
  219.    On the other hand, if you set a variable to be a list with elements,
  220. the list will appear when you evaluate the variable, as you can see by
  221. evaluating the following two expressions:
  222.  
  223.      (setq animals '(giraffe gazelle lion tiger))
  224.      
  225.      animals
  226.  
  227.    Thus, to create a `while' loop that tests whether there are any
  228. items in the list `animals', the first part of the loop will be written
  229. like this:
  230.  
  231.      (while animals
  232.             ...
  233.  
  234. When the `while' tests its first argument, the variable `animals' is
  235. evaluated.  It returns a list.  So long as the list has elements, the
  236. `while' considers the results of the test to be true; but when the list
  237. is empty, it considers the results of the test to be false.
  238.  
  239.    To prevent the `while' loop from running forever, some mechanism
  240. needs to be provided to empty the list eventually.  An oft-used
  241. technique is to have one of the subsequent forms in the `while'
  242. expression set the value of the list to be the CDR of the list.  Each
  243. time the `cdr' function is evaluated, the list will be made shorter,
  244. until eventually only the empty list will be left.  At this point, the
  245. test of the `while' loop will return false, and the arguments to the
  246. `while' will no longer be evaluated.
  247.  
  248.    For example, the list of animals bound to the variable `animals' can
  249. be set to be the CDR of the original list with the following expression:
  250.  
  251.      (setq animals (cdr animals))
  252.  
  253. If you have evaluated the previous expressions and then evaluate this
  254. expression, you will see `(gazelle lion tiger)' appear in the echo
  255. area.  If you evaluate the expression again, `(lion tiger)' will appear
  256. in the echo area.  If you evaluate it again and yet again, `(tiger)'
  257. appears and then the empty list, shown by `nil'.
  258.  
  259.    A template for a `while' loop that uses the `cdr' function
  260. repeatedly to cause the true-or-false-test eventually to test false
  261. looks like this:
  262.  
  263.      (while TEST-WHETHER-LIST-IS-EMPTY
  264.        BODY...
  265.        SET-LIST-TO-CDR-OF-LIST)
  266.  
  267.    This test and use of `cdr' can be put together in a function that
  268. goes through a list and prints each element of the list on a line of its
  269. own.
  270.  
  271. 
  272. File: emacs-lisp-intro.info,  Node: print-elements-of-list,  Next: Incrementing Loop,  Prev: Loop Example,  Up: while
  273.  
  274. An Example: `print-elements-of-list'
  275. ------------------------------------
  276.  
  277.    The `print-elements-of-list' function illustrates a `while' loop
  278. with a list.
  279.  
  280.    The function requires several lines for its output.  Since the echo
  281. area is only one line, we cannot illustrate how it works in the same
  282. way we have been illustrating functions in the past, by evaluating them
  283. inside Info.  Instead, you need to copy the necessary expressions to
  284. your `*scratch*' buffer and evaluate them there.  You can copy the
  285. expressions by marking the beginning of the region with `C-<SPC>'
  286. (`set-mark-command'), moving the cursor to the end of the region and
  287. then copying the region using `M-w' (`copy-region-as-kill').  In the
  288. `*scratch*' buffer, you can yank the expressions back by typing `C-y'
  289. (`yank').
  290.  
  291.    After you have copied the expressions to the `*scratch*' buffer,
  292. evaluate each expression in turn.  Be sure to evaluate the last
  293. expression, `(print-elements-of-list animals)', by typing `C-u C-x
  294. C-e', that is, by giving an argument to `eval-last-sexp'.  This will
  295. cause the result of the evaluation to be printed in the `*scratch*'
  296. buffer instead of being printed in the echo area.  (Otherwise you will
  297. see something like this in your echo area:
  298. `^Jgiraffe^J^Jgazelle^J^Jlion^J^Jtiger^Jnil', in which each `^J' stands
  299. for the newline that in the `*scratch*' buffer puts each word on its
  300. own line.  You can evaluate these expressions right now in the Info
  301. buffer, if you like, to see this effect.)
  302.  
  303.      (setq animals '(giraffe gazelle lion tiger))
  304.      
  305.      (defun print-elements-of-list (list)
  306.        "Print each element of LIST on a line of its own."
  307.        (while list
  308.          (print (car list))
  309.          (setq list (cdr list))))
  310.      
  311.      (print-elements-of-list animals)
  312.  
  313. When you evaluate the three expressions in sequence in the `*scratch*'
  314. buffer, this will be printed in the buffer:
  315.  
  316.      giraffe
  317.      
  318.      gazelle
  319.      
  320.      lion
  321.      
  322.      tiger
  323.      
  324.      nil
  325.  
  326.    Each element of the list is printed on a line of its own (that is
  327. what the function `print' does) and then the value returned by the
  328. function is printed.  Since the last expression in the function is the
  329. `while' loop, and since `while' loops always return `nil', a `nil' is
  330. printed after the last element of the list.
  331.  
  332. 
  333. File: emacs-lisp-intro.info,  Node: Incrementing Loop,  Next: Decrementing Loop,  Prev: print-elements-of-list,  Up: while
  334.  
  335. A Loop with an Incrementing Counter
  336. -----------------------------------
  337.  
  338.    A loop is not useful unless it stops when it ought.  Besides
  339. controlling a loop with a list, a common way of stopping a loop is to
  340. write the first argument as a test that returns false when the correct
  341. number of repetitions are complete.  This means that the loop must have
  342. a counter--an expression that counts how many times the loop repeats
  343. itself.
  344.  
  345.    The test can be an expression such as `(< count desired-number)'
  346. which returns `t' for true if the value of `count' is less than the
  347. `desired-number' of repetitions and `nil' for false if the value of
  348. `count' is equal to or is greater than the `desired-number'.  The
  349. expression that increments the count can be a simple `setq' such as
  350. `(setq count (1+ count))', where `1+' is a built-in function in Emacs
  351. Lisp that adds 1 to its argument.  (The expression `(1+ count)' has the
  352. same result as `(+ count 1)', but is easier for a human to read.)
  353.  
  354.    The template for a `while' loop controlled by an incrementing
  355. counter looks like this:
  356.  
  357.      SET-COUNT-TO-INITIAL-VALUE
  358.      (while (< count desired-number)         ; true-or-false-test
  359.        BODY...
  360.        (setq count (1+ count)))              ; incrementer
  361.  
  362. Note that you need to set the initial value of `count'; usually it is
  363. set to 1.
  364.  
  365. * Menu:
  366.  
  367. * Incrementing Example::        Counting pebbles in a triangle.
  368. * Inc Example parts::           The parts of the function definition.
  369. * Inc Example altogether::      Putting the function definition together.
  370.  
  371. 
  372. File: emacs-lisp-intro.info,  Node: Incrementing Example,  Next: Inc Example parts,  Prev: Incrementing Loop,  Up: Incrementing Loop
  373.  
  374. Example with incrementing counter
  375. .................................
  376.  
  377.    Suppose you are playing on the beach and decide to make a triangle of
  378. pebbles, putting one pebble in the first row, two in the second row,
  379. three in the third row and so on, like this:
  380.  
  381.                     *
  382.                    * *
  383.                   * * *
  384.                  * * * *
  385.  
  386. (About 2500 years ago, Pythagoras and others developed the beginnings of
  387. number theory by considering questions such as this.)
  388.  
  389.    Suppose you want to know how many pebbles you will need to make a
  390. triangle with 7 rows?
  391.  
  392.    Clearly, what you need to do is add up the numbers from 1 to 7.
  393. There are two ways to do this; start with the smallest number, one, and
  394. add up the list in sequence, 1, 2, 3, 4 and so on; or start with the
  395. largest number and add the list going down: 7, 6, 5, 4 and so on.
  396. Because both mechanisms illustrate common ways of writing `while'
  397. loops, we will create two examples, one counting up and the other
  398. counting down.  In this first example, we will start with 1 and add 2,
  399. 3, 4 and so on.
  400.  
  401.    If you are just adding up a short list of numbers, the easiest way
  402. to do it is to add up all the numbers at once.  However, if you do not
  403. know ahead of time how many numbers your list will have, or if you want
  404. to be prepared for a very long list, then you need to design your
  405. addition so that what you do is repeat a simple process many times
  406. instead of doing a more complex process once.
  407.  
  408.    For example, instead of adding up all the pebbles all at once, what
  409. you can do is add the number of pebbles in the first row, 1, to the
  410. number in the second row, 2, and then add the total of those two rows
  411. to the third row, 3.  Then you can add the number in the fourth row, 4,
  412. to the total of the first three rows; and so on.
  413.  
  414.    The critical characteristic of the process is that each repetitive
  415. action is simple.  In this case, at each step we add only two numbers,
  416. the number of pebbles in the row and the total already found.  This
  417. process of adding two numbers is repeated again and again until the last
  418. row has been added to the total of all the preceding rows.  In a more
  419. complex loop the repetitive action might not be so simple, but it will
  420. be simpler than doing everything all at once.
  421.  
  422. 
  423. File: emacs-lisp-intro.info,  Node: Inc Example parts,  Next: Inc Example altogether,  Prev: Incrementing Example,  Up: Incrementing Loop
  424.  
  425. The parts of the function definition
  426. ....................................
  427.  
  428.    The preceding analysis gives us the bones of our function definition:
  429. first, we will need a variable that we can call `total' that will be
  430. the total number of pebbles.  This will be the value returned by the
  431. function.
  432.  
  433.    Second, we know that the function will require an argument: this
  434. argument will be the total number of rows in the triangle.  It can be
  435. called `number-of-rows'.
  436.  
  437.    Finally, we need a variable to use as a counter.  We could call this
  438. variable `counter', but a better name is `row-number'.  That is because
  439. what the counter does is count rows, and a program should be written to
  440. be as understandable as possible.
  441.  
  442.    When the Lisp interpreter first starts evaluating the expressions in
  443. the function, the value of `total' should be set to zero, since we have
  444. not added anything to it.  Then the function should add the number of
  445. pebbles in the first row to the total, and then add the number of
  446. pebbles in the second to the total, and then add the number of pebbles
  447. in the third row to the total, and so on, until there are no more rows
  448. left to add.
  449.  
  450.    Both `total' and `row-number' are used only inside the function, so
  451. they can be declared as local variables with `let' and given initial
  452. values.  Clearly, the initial value for `total' should be 0.  The
  453. initial value of `row-number' should be 1, since we start with the
  454. first row.  This means that the `let' statement will look like this:
  455.  
  456.        (let ((total 0)
  457.              (row-number 1))
  458.          BODY...)
  459.  
  460.    After the internal variables are declared and bound to their initial
  461. values, we can begin the `while' loop.  The expression that serves as
  462. the test should return a value of `t' for true so long as the
  463. `row-number' is less than or equal to the `number-of-rows'.  (If the
  464. expression tests true only so long as the row number is less than the
  465. number of rows in the triangle, the last row will never be added to the
  466. total; hence the row number has to be either less than or equal to the
  467. number of rows.)
  468.  
  469.    Lisp provides the `<=' function that returns true if the value of
  470. its first argument is less than or equal to the value of its second
  471. argument and false otherwise.  So the expression that the `while' will
  472. evaluate as its test should look like this:
  473.  
  474.      (<= row-number number-of-rows)
  475.  
  476.    The total number of pebbles can be found by repeatedly adding the
  477. number of pebbles in a row to the total already found.  Since the
  478. number of pebbles in the row is equal to the row number, the total can
  479. be found by adding the row number to the total.  (Clearly, in a more
  480. complex situation, the number of pebbles in the row might be related to
  481. the row number in a more complicated way; if this were the case, the
  482. row number would be replaced by the appropriate expression.)
  483.  
  484.      (setq total (+ total row-number))
  485.  
  486. What this does is set the new value of `total' to be equal to the sum
  487. of adding the number of pebbles in the row to the previous total.
  488.  
  489.    After setting the value of `total', the conditions need to be
  490. established for the next repetition of the loop, if there is one.  This
  491. is done by incrementing the value of the `row-number' variable, which
  492. serves as a counter.  After the `row-number' variable has been
  493. incremented, the true-or-false-test at the beginning of the `while'
  494. loop tests whether its value is still less than or equal to the value
  495. of the `number-of-rows' and if it is, adds the new value of the
  496. `row-number' variable to the `total' of the previous repetition of the
  497. loop.
  498.  
  499.    The built-in Emacs Lisp function `1+' adds 1 to a number, so the
  500. `row-number' variable can be incremented with this expression:
  501.  
  502.      (setq row-number (1+ row-number))
  503.  
  504. 
  505. File: emacs-lisp-intro.info,  Node: Inc Example altogether,  Prev: Inc Example parts,  Up: Incrementing Loop
  506.  
  507. Putting the function definition together
  508. ........................................
  509.  
  510.    We have created the parts for the function definition; now we need to
  511. put them together.
  512.  
  513.    First, the contents of the `while' expression:
  514.  
  515.      (while (<= row-number number-of-rows)   ; true-or-false-test
  516.        (setq total (+ total row-number))
  517.        (setq row-number (1+ row-number)))    ; incrementer
  518.  
  519.    Along with the `let' expression varlist, this very nearly completes
  520. the body of the function definition.  However, it requires one final
  521. element, the need for which is somewhat subtle.
  522.  
  523.    The final touch is to place the variable `total' on a line by itself
  524. after the `while' expression.  Otherwise, the value returned by the
  525. whole function is the value of the last expression that is evaluated in
  526. the body of the `let', and this is the value returned by the `while',
  527. which is always `nil'.
  528.  
  529.    This may not be evident at first sight.  It almost looks as if the
  530. incrementing expression is the last expression of the whole function.
  531. But that expression is part of the body of the `while'; it is the last
  532. element of the list that starts with the symbol `while'.  Moreover, the
  533. whole of the `while' loop is a list within the body of the `let'.
  534.  
  535.    In outline, the function will look like this:
  536.  
  537.      (defun NAME-OF-FUNCTION (ARGUMENT-LIST)
  538.        "DOCUMENTATION..."
  539.        (let (VARLIST)
  540.          (while (TRUE-OR-FALSE-TEST)
  541.            BODY-OF-WHILE... )
  542.            ... )                     ; Need final expression here.
  543.  
  544.    The result of evaluating the `let' is what is going to be returned
  545. by the `defun' since the `let' is not embedded within any containing
  546. list, except for the `defun' as a whole.  However, if the `while' is
  547. the last element of the `let' expression, the function will always
  548. return `nil'.  This is not what we want!  Instead, what we want is the
  549. value of the variable `total'.  This is returned by simply placing the
  550. symbol as the last element of the list starting with `let'.  It gets
  551. evaluated after the preceding elements of the list are evaluated, which
  552. means it gets evaluated after it has been assigned the correct value
  553. for the total.
  554.  
  555.    It may be easier to see this by printing the list starting with
  556. `let' all on one line.  This format makes it evident that the VARLIST
  557. and `while' expressions are the second and third elements of the list
  558. starting with `let', and the `total' is the last element:
  559.  
  560.      (let (VARLIST) (while (TRUE-OR-FALSE-TEST) BODY-OF-WHILE... ) total)
  561.  
  562.    Putting everything together, the `triangle' function definition
  563. looks like this:
  564.  
  565.      (defun triangle (number-of-rows)    ; Version with
  566.                                          ;   incrementing counter.
  567.        "Add up the number of pebbles in a triangle.
  568.      The first row has one pebble, the second row two pebbles,
  569.      the third row three pebbles, and so on.
  570.      The argument is NUMBER-OF-ROWS."
  571.        (let ((total 0)
  572.              (row-number 1))
  573.          (while (<= row-number number-of-rows)
  574.            (setq total (+ total row-number))
  575.            (setq row-number (1+ row-number)))
  576.          total))
  577.  
  578.    After you have installed `triangle' by evaluating the function, you
  579. can try it out.  Here are two examples:
  580.  
  581.      (triangle 4)
  582.      
  583.      (triangle 7)
  584.  
  585. The sum of the first four numbers is 10 and the sum of the first seven
  586. numbers is 28.
  587.  
  588. 
  589. File: emacs-lisp-intro.info,  Node: Decrementing Loop,  Prev: Incrementing Loop,  Up: while
  590.  
  591. Loop with a Decrementing Counter
  592. --------------------------------
  593.  
  594.    Another common way to write a `while' loop is to write the test so
  595. that it determines whether a counter is greater than zero.  So long as
  596. the counter is greater than zero, the loop is repeated.  But when the
  597. counter is equal to or less than zero, the loop is stopped.  For this
  598. to work, the counter has to start out greater than zero and then be
  599. made smaller and smaller by one of the forms that is evaluated
  600. repeatedly.
  601.  
  602.    The test will be an expression such as `(> counter 0)' which returns
  603. `t' for true if the value of `counter' is greater than zero, and `nil'
  604. for false if the value of `counter' is equal to or less than zero.  The
  605. expression that makes the number smaller and smaller can be a simple
  606. `setq' such as `(setq counter (1- counter))', where `1-' is a built-in
  607. function in Emacs Lisp that subtracts 1 from its argument.
  608.  
  609.    The template for a decrementing `while' loop looks like this:
  610.  
  611.      (while (> counter 0)                    ; true-or-false-test
  612.        BODY...
  613.        (setq counter (1- counter)))          ; decrementer
  614.  
  615. * Menu:
  616.  
  617. * Decrementing Example::        More pebbles on the beach.
  618. * Dec Example parts::           The parts of the function definition.
  619. * Dec Example altogether::      Putting the function definition together.
  620.  
  621. 
  622. File: emacs-lisp-intro.info,  Node: Decrementing Example,  Next: Dec Example parts,  Prev: Decrementing Loop,  Up: Decrementing Loop
  623.  
  624. Example with decrementing counter
  625. .................................
  626.  
  627.    To illustrate a loop with a decrementing counter, we will rewrite the
  628. `triangle' function so the counter decreases to zero.
  629.  
  630.    This is the reverse of the earlier version of the function.  In this
  631. case, to find out how many pebbles are needed to make a triangle with 3
  632. rows, add the number of pebbles in the third row, 3, to the number in
  633. the preceding row, 2, and then add the total of those two rows to the
  634. row that precedes them, which is 1.
  635.  
  636.    Likewise, to find the number of pebbles in a triangle with 7 rows,
  637. add the number of pebbles in the seventh row, 7, to the number in the
  638. preceding row, which is 6, and then add the total of those two rows to
  639. the row that precedes them, which is 5, and so on.  As in the previous
  640. example, each addition only involves adding two numbers, the total of
  641. the rows already added up and the number of pebbles in the row that is
  642. being added to the total.  This process of adding two numbers is
  643. repeated again and again until there are no more pebbles to add.
  644.  
  645.    We know how many pebbles to start with: the number of pebbles in the
  646. last row is equal to the number of rows.  If the triangle has seven
  647. rows, the number of pebbles in the last row is 7.  Likewise, we know how
  648. many pebbles are in the preceding row: it is one less than the number in
  649. the row.
  650.  
  651. 
  652. File: emacs-lisp-intro.info,  Node: Dec Example parts,  Next: Dec Example altogether,  Prev: Decrementing Example,  Up: Decrementing Loop
  653.  
  654. The parts of the function definition
  655. ....................................
  656.  
  657.    We start with three variables: the total number of rows in the
  658. triangle; the number of pebbles in a row; and the total number of
  659. pebbles, which is what we want to calculate.  These variables can be
  660. named `number-of-rows', `number-of-pebbles-in-row', and `total',
  661. respectively.
  662.  
  663.    Both `total' and `number-of-pebbles-in-row' are used only inside the
  664. function and are declared with `let'.  The initial value of `total'
  665. should, of course, be zero.  However, the initial value of
  666. `number-of-pebbles-in-row' should be equal to the number of rows in the
  667. triangle, since the addition will start with the longest row.
  668.  
  669.    This means that the beginning of the `let' expression will look like
  670. this:
  671.  
  672.      (let ((total 0)
  673.            (number-of-pebbles-in-row number-of-rows))
  674.        BODY...)
  675.  
  676.    The total number of pebbles can be found by repeatedly adding the
  677. number of pebbles in a row to the total already found, that is, by
  678. repeatedly evaluating the following expression:
  679.  
  680.      (setq total (+ total number-of-pebbles-in-row))
  681.  
  682. After the `number-of-pebbles-in-row' is added to the `total', the
  683. `number-of-pebbles-in-row' should be decremented by one, since the next
  684. time the loop repeats, the preceding row will be added to the total.
  685.  
  686.    The number of pebbles in a preceding row is one less than the number
  687. of pebbles in a row, so the built-in Emacs Lisp function `1-' can be
  688. used to compute the number of pebbles in the preceding row.  This can be
  689. done with the following expression:
  690.  
  691.      (setq number-of-pebbles-in-row
  692.            (1- number-of-pebbles-in-row))
  693.  
  694.    Finally, we know that the `while' loop should stop making repeated
  695. additions when there are no pebbles in a row.  So the test for the
  696. `while' loop is simply:
  697.  
  698.      (while (> number-of-pebbles-in-row 0)
  699.  
  700. 
  701. File: emacs-lisp-intro.info,  Node: Dec Example altogether,  Prev: Dec Example parts,  Up: Decrementing Loop
  702.  
  703. Putting the function definition together
  704. ........................................
  705.  
  706.    Putting these expressions together, we have a function definition
  707. that looks like this:
  708.  
  709.      ;;; First subtractive version.
  710.      (defun triangle (number-of-rows)
  711.        "Add up the number of pebbles in a triangle."
  712.        (let ((total 0)
  713.              (number-of-pebbles-in-row number-of-rows))
  714.          (while (> number-of-pebbles-in-row 0)
  715.            (setq total (+ total number-of-pebbles-in-row))
  716.            (setq number-of-pebbles-in-row
  717.                  (1- number-of-pebbles-in-row)))
  718.          total))
  719.  
  720.    As written, this function works.
  721.  
  722.    However, it turns out that one of the local variables,
  723. `number-of-pebbles-in-row', is unneeded!
  724.  
  725.    When the `triangle' function is evaluated, the symbol
  726. `number-of-rows' will be bound to a number, giving it an initial value.
  727. That number can be changed in the body of the function as if it were a
  728. local variable, without any fear that such a change will effect the
  729. value of the variable outside of the function.  This is a very useful
  730. characteristic of Lisp; it means that the variable `number-of-rows' can
  731. be used anywhere in the function where `number-of-pebbles-in-row' is
  732. used.
  733.  
  734.    Here is a second version of the function written a bit more cleanly:
  735.  
  736.      (defun triangle (number)                ; Second version.
  737.        "Return sum of numbers 1 through NUMBER inclusive."
  738.        (let ((total 0))
  739.          (while (> number 0)
  740.            (setq total (+ total number))
  741.            (setq number (1- number)))
  742.          total))
  743.  
  744.    In brief, a properly written `while' loop will consist of three
  745. parts:
  746.  
  747.   1. A test that will return false after the loop has repeated itself
  748.      the correct number of times.
  749.  
  750.   2. An expression the evaluation of which will return the value desired
  751.      after being repeatedly evaluated.
  752.  
  753.   3. An expression to change the value passed to the true-or-false-test
  754.      so that the test returns false after the loop has repeated itself
  755.      the right number of times.
  756.  
  757. 
  758. File: emacs-lisp-intro.info,  Node: Recursion,  Next: Looping exercise,  Prev: while,  Up: Loops & Recursion
  759.  
  760. Recursion
  761. =========
  762.  
  763.    A recursive function contains code that tells itself to evaluate
  764. itself.  When the function evaluates itself, it again finds the code
  765. that tells itself to evaluate itself, so the function evaluates itself
  766. again ... and again ...  A recursive function will keep telling itself
  767. to evaluate itself again forever unless it is also provided with a stop
  768. condition.
  769.  
  770.    A recursive function typically contains a conditional expression
  771. which has three parts:
  772.  
  773.   1. A true-or-false-test that determines whether the function is called
  774.      again, here called the "do-again-test".
  775.  
  776.   2. The name of the function.
  777.  
  778.   3. An expression that causes the conditional expression to test false
  779.      after the correct number of repetitions, here called the
  780.      "next-step-expression".
  781.  
  782.    Recursive functions can be much simpler than any other kind of
  783. function.  Indeed, when people first start to use them, they often look
  784. so mysteriously simple as to be incomprehensible.  Like riding a
  785. bicycle, reading a recursive function definition takes a certain knack
  786. which is hard at first but then seems simple.
  787.  
  788.    A template for a recursive function looks like this:
  789.  
  790.      (defun NAME-OF-RECURSIVE-FUNCTION (ARGUMENT-LIST)
  791.        "DOCUMENTATION..."
  792.        BODY...
  793.        (if DO-AGAIN-TEST
  794.          (NAME-OF-RECURSIVE-FUNCTION
  795.               NEXT-STEP-EXPRESSION)))
  796.  
  797. Each time the recursive function is evaluated, an argument is bound to
  798. the value of the next-step-expression; and that value is used in the
  799. do-again-test.  The next-step-expression is designed so that the
  800. do-again-test returns false when the function should no longer be
  801. repeated.
  802.  
  803.    The do-again-test is sometimes called the "stop condition", since it
  804. stops the repetitions when it tests false.
  805.  
  806. * Menu:
  807.  
  808. * Recursion with list::         Using a list as the test whether to recurse.
  809. * Recursive triangle function::  Replacing a `while' loop with recursion.
  810. * Recursion with cond::         Recursion example with a different conditional.
  811.  
  812. 
  813. File: emacs-lisp-intro.info,  Node: Recursion with list,  Next: Recursive triangle function,  Prev: Recursion,  Up: Recursion
  814.  
  815. Recursion with a List
  816. ---------------------
  817.  
  818.    The example of a `while' loop that printed the elements of a list of
  819. numbers can be written recursively.  Here is the code, including an
  820. expression to set the value of the variable `animals' to a list.
  821.  
  822.    This example must be copied to the `*scratch*' buffer and each
  823. expression must be evaluated there.  Use `C-u C-x C-e' to evaluate the
  824. `(print-elements-recursively animals)' expression so that the results
  825. are printed in the buffer; otherwise the Lisp interpreter will try to
  826. squeeze the results into the one line of the echo area.
  827.  
  828.    Also, place your cursor immediately after the last closing
  829. parenthesis of the `print-elements-recursively' function, before the
  830. comment.  Otherwise, the Lisp interpreter will try to evaluate the
  831. comment.
  832.  
  833.      (setq animals '(giraffe gazelle lion tiger))
  834.      
  835.      (defun print-elements-recursively (list)
  836.        "Print each element of LIST on a line of its own.
  837.      Uses recursion."
  838.        (print (car list))                  ; body
  839.        (if list                            ; do-again-test
  840.            (print-elements-recursively     ; recursive call
  841.             (cdr list))))                  ; next-step-expression
  842.      
  843.      (print-elements-recursively animals)
  844.  
  845.    The `print-elements-recursively' function first prints the first
  846. element of the list, the CAR of the list.  Then, if the list is not
  847. empty, the function invokes itself, but gives itself as its argument,
  848. not the whole list, but the second and subsequent elements of the list,
  849. the CDR of the list.
  850.  
  851.    When this evaluation occurs, the function prints the first element of
  852. the list it receives as its argument (which is the second element of
  853. the original list).  Then, the `if' expression is evaluated and when
  854. true, the function calls itself with the CDR of the list it is invoked
  855. with, which (the second time around) is the CDR of the CDR of the
  856. original list.
  857.  
  858.    Each time the function invokes itself, it invokes itself on a shorter
  859. version of the original list.  Eventually, the function invokes itself
  860. on an empty list.  The `print' function prints the empty list as `nil'.
  861. Next, the conditional expression tests the value of `list'.  Since the
  862. value of `list' is `nil', the `if' expression tests false so the
  863. then-part is not evaluated.  The function as a whole then returns
  864. `nil'.  Consequently, you see `nil' twice when you evaluate the
  865. function.
  866.  
  867.    When you evaluate `(print-elements-recursively animals)' in the
  868. `*scratch*' buffer, you see this result:
  869.  
  870.      giraffe
  871.      
  872.      gazelle
  873.      
  874.      lion
  875.      
  876.      tiger
  877.      
  878.      nil
  879.      
  880.      nil
  881.  
  882.    (The first `nil' is the value of the empty list that is printed; the
  883. second `nil' is the value returned by the whole function.)
  884.  
  885. 
  886. File: emacs-lisp-intro.info,  Node: Recursive triangle function,  Next: Recursion with cond,  Prev: Recursion with list,  Up: Recursion
  887.  
  888. Recursion in Place of a Counter
  889. -------------------------------
  890.  
  891.    The `triangle' function described in a previous section can also be
  892. written recursively.  It looks like this:
  893.  
  894.      (defun triangle-recursively (number)
  895.        "Return the sum of the numbers 1 through NUMBER inclusive.
  896.      Uses recursion."
  897.        (if (= number 1)                    ; do-again-test
  898.            1                               ; then-part
  899.          (+ number                         ; else-part
  900.             (triangle-recursively          ; recursive call
  901.              (1- number)))))               ; next-step-expression
  902.      
  903.      (triangle-recursively 7)
  904.  
  905. You can install this function by evaluating it and then try it by
  906. evaluating `(triangle-recursively 7)'.  (Remember to put your cursor
  907. immediately after the last parenthesis of the function definition,
  908. before the comment.)
  909.  
  910.    To understand how this function works, let's consider what happens
  911. in the various cases when the function is passed 1, 2, 3, or 4 as the
  912. value of its argument.
  913.  
  914.    First, what happens if the value of the argument is 1?
  915.  
  916.    The function has an `if' expression after the documentation string.
  917. It tests whether the value of `number' is equal to 1; if so, Emacs
  918. evaluates the then-part of the `if' expression, which returns the
  919. number 1 as the value of the function.  (A triangle with one row has
  920. one pebble in it.)
  921.  
  922.    Suppose, however, that the value of the argument is 2.  In this case,
  923. Emacs evaluates the else-part of the `if' expression.
  924.  
  925.    The else-part consists of an addition, the recursive call to
  926. `triangle-recursively' and a decrementing action; and it looks like
  927. this:
  928.  
  929.      (+ number (triangle-recursively (1- number)))
  930.  
  931.    When Emacs evaluates this expression, the innermost expression is
  932. evaluated first; then the other parts in sequence.  Here are the steps
  933. in detail:
  934.  
  935. Step 1    Evaluate the innermost expression.
  936.      The innermost expression is `(1- number)' so Emacs decrements the
  937.      value of `number' from 2 to 1.
  938.  
  939. Step 2    Evaluate the `triangle-recursively' function.
  940.      It does not matter that this function is contained within itself.
  941.      Emacs passes the result Step 1 as the argument used by this
  942.      instance of the `triangle-recursively' function
  943.  
  944.      In this case, Emacs evaluates `triangle-recursively' with an
  945.      argument of 1.  This means that this evaluation of
  946.      `triangle-recursively' returns 1.
  947.  
  948. Step 3    Evaluate the value of `number'.
  949.      The variable `number' is the second element of the list that
  950.      starts with `+'; its value is 2.
  951.  
  952. Step 4    Evaluate the `+' expression.
  953.      The `+' expression receives two arguments, the first from the
  954.      evaluation of `number' (Step 3) and the second from the evaluation
  955.      of `triangle-recursively' (Step 2).
  956.  
  957.      The result of the addition is the sum of 2 plus 1, and the number
  958.      3 is returned, which is correct.  A triangle with two rows has
  959.      three pebbles in it.
  960.  
  961. * Menu:
  962.  
  963. * Recursive Example arg of 3::
  964.  
  965. 
  966. File: emacs-lisp-intro.info,  Node: Recursive Example arg of 3,  Prev: Recursive triangle function,  Up: Recursive triangle function
  967.  
  968. An argument of 3
  969. ................
  970.  
  971.    Suppose that `triangle-recursively' is called with an argument of 3.
  972.  
  973. Step 1    Evaluate the do-again-test.
  974.      The `if' expression is evaluated first.  This is the do-again test
  975.      and returns false, so the else-part of the `if' expression is
  976.      evaluated.  (Note that in this example, the do-again-test causes
  977.      the function to call itself when it tests false, not when it tests
  978.      true.)
  979.  
  980. Step 2    Evaluate the innermost expression of the else-part.
  981.      The innermost expression of the else-part is evaluated, which
  982.      decrements 3 to 2.  This is the next-step-expression.
  983.  
  984. Step 3    Evaluate the `triangle-recursively' function.
  985.      The number 2 is passed to the `triangle-recursively' function.
  986.  
  987.      We know what happens when Emacs evaluates `triangle-recursively'
  988.      with an argument of 2.  After going through the sequence of
  989.      actions described earlier, it returns a value of 3.  So that is
  990.      what will happen here.
  991.  
  992. Step 4    Evaluate the addition.
  993.      3 will be passed as an argument to the addition and will be added
  994.      to the number with which the function was called, which is 3.
  995.  
  996. The value returned by the function as a whole will be 6.
  997.  
  998.    Now that we know what will happen when `triangle-recursively' is
  999. called with an argument of 3, it is evident what will happen if it is
  1000. called with an argument of 4:
  1001.  
  1002.      In the recursive call, the evaluation of
  1003.  
  1004.           (triangle-recursively (1- 4))
  1005.  
  1006.      will return the value of evaluating
  1007.  
  1008.           (triangle-recursively 3)
  1009.  
  1010.      which is 6 and this value will be added to 4 by the addition in the
  1011.      third line.
  1012.  
  1013. The value returned by the function as a whole will be 10.
  1014.  
  1015.    Each time `triangle-recursively' is evaluated, it evaluates a
  1016. version of itself with a smaller argument, until the argument is small
  1017. enough so that it does not evaluate itself.
  1018.  
  1019. 
  1020. File: emacs-lisp-intro.info,  Node: Recursion with cond,  Prev: Recursive triangle function,  Up: Recursion
  1021.  
  1022. Recursion Example Using `cond'
  1023. ------------------------------
  1024.  
  1025.    The version of `triangle-recursively' described earlier is written
  1026. with the `if' special form.  It can also be written using another
  1027. special form called `cond'.  The name of the special form `cond' is an
  1028. abbreviation of the word `conditional'.
  1029.  
  1030.    Although the `cond' special form is not used as often in the Emacs
  1031. Lisp sources as `if', it is used often enough to justify explaining it.
  1032.  
  1033.    The template for a `cond' expression looks like this:
  1034.  
  1035.      (cond
  1036.       BODY...)
  1037.  
  1038. where the BODY is a series of lists.
  1039.  
  1040.    Written out more fully, the template looks like this:
  1041.  
  1042.      (cond
  1043.       ((FIRST-TRUE-OR-FALSE-TEST FIRST-CONSEQUENT)
  1044.        (SECOND-TRUE-OR-FALSE-TEST SECOND-CONSEQUENT)
  1045.        (THIRD-TRUE-OR-FALSE-TEST THIRD-CONSEQUENT)
  1046.        ...)
  1047.  
  1048.    When the Lisp interpreter evaluates the `cond' expression, it
  1049. evaluates the first element (the CAR or true-or-false-test) of the
  1050. first expression in a series of expressions within the body of the
  1051. `cond'.
  1052.  
  1053.    If the true-or-false-test returns `nil' the rest of that expression,
  1054. the consequent, is skipped and  the true-or-false-test of the next
  1055. expression is evaluated.  When an expression is found whose
  1056. true-or-false-test returns a value that is not `nil', the consequent of
  1057. that expression is evaluated.  The consequent can be one or more
  1058. expressions.  If the consequent consists of more than one expression,
  1059. the expressions are evaluated in sequence and the value of the last one
  1060. is returned.  If the expression does not have a consequent, the value
  1061. of the true-or-false-test is returned.
  1062.  
  1063.    If none of the true-or-false-tests test true, the `cond' expression
  1064. returns `nil'.
  1065.  
  1066.    Written using `cond', the `triangle' function looks like this:
  1067.  
  1068.      (defun triangle-using-cond (number)
  1069.        (cond ((<= number 0) 0)
  1070.              ((= number 1) 1)
  1071.              ((> number 1)
  1072.               (+ number (triangle-using-cond (1- number))))))
  1073.  
  1074. In this example, the `cond' returns 0 if the number is less than or
  1075. equal to 0, it returns 1 if the number is 1 and it evaluates `(+ number
  1076. (triangle-using-cond (1- number)))' if the number is greater than 1.
  1077.  
  1078. 
  1079. File: emacs-lisp-intro.info,  Node: Looping exercise,  Prev: Recursion,  Up: Loops & Recursion
  1080.  
  1081. Looping Exercise
  1082. ================
  1083.  
  1084.    * Write a function similar to `triangle' in which each row has a
  1085.      value which is the square of the row number.  Use a `while' loop.
  1086.  
  1087.    * Write a function similar to `triangle' that multiplies instead of
  1088.      adds the values.
  1089.  
  1090.    * Rewrite these two functions recursively.  Rewrite these functions
  1091.      using `cond'.
  1092.  
  1093.    * Write a function for Texinfo mode that creates an index entry at
  1094.      the beginning of a paragraph for every `@dfn' within the paragraph.
  1095.      (In a Texinfo file, `@dfn' marks a definition.  For more
  1096.      information, see *Note Indicating Definitions:
  1097.      (texinfo)Indicating.)
  1098.  
  1099. 
  1100. File: emacs-lisp-intro.info,  Node: Regexp Search,  Next: Counting Words,  Prev: Loops & Recursion,  Up: Top
  1101.  
  1102. Regular Expression Searches
  1103. ***************************
  1104.  
  1105.    Regular expression searches are used extensively in GNU Emacs.  The
  1106. two functions, `forward-sentence' and `forward-paragraph' illustrate
  1107. these searches well.
  1108.  
  1109.    Regular expression searches are described in *Note Regular
  1110. Expression Search: (emacs)Regexp Search, as well as in *Note Regular
  1111. Expressions: (elisp)Regular Expressions.  In writing this chapter, I am
  1112. presuming that you have at least a mild acquaintance with them.  The
  1113. major point to remember is that regular expressions permit you to
  1114. search for patterns as well as for literal strings of characters.  For
  1115. example, the code in `forward-sentence' searches for the pattern of
  1116. possible characters that could mark the end of a sentence, and moves
  1117. point to that spot.
  1118.  
  1119.    Before looking at the code for the `forward-sentence' function, it
  1120. is worth considering what the pattern that marks the end of a sentence
  1121. must be.  The pattern is discussed in the next section; following that
  1122. is a description of the regular expression search function,
  1123. `re-search-forward'.  The `forward-sentence' function is described in
  1124. the section following.  Finally, the `forward-paragraph' function is
  1125. described in the last section of this chapter.  `forward-paragraph' is
  1126. a complex function that introduces several new features.
  1127.  
  1128. * Menu:
  1129.  
  1130. * sentence-end::                The regular expression for `sentence-end'.
  1131. * re-search-forward::           Very similar to `search-forward'.
  1132. * forward-sentence::            A straightforward example of regexp search.
  1133. * forward-paragraph::           A somewhat complex example.
  1134. * etags::                       How to create your own `TAGS' table.
  1135. * Regexp Review::
  1136. * re-search Exercises::
  1137.  
  1138. 
  1139. File: emacs-lisp-intro.info,  Node: sentence-end,  Next: re-search-forward,  Prev: Regexp Search,  Up: Regexp Search
  1140.  
  1141. The Regular Expression for `sentence-end'
  1142. =========================================
  1143.  
  1144.    The symbol `sentence-end' is bound to the pattern that marks the end
  1145. of a sentence.  What should this regular expression be?
  1146.  
  1147.    Clearly, a sentence may be ended by a period, a question mark, or an
  1148. exclamation mark.  Indeed, only clauses that end with one of those three
  1149. characters should be considered the end of a sentence.  This means that
  1150. the pattern should include the character set:
  1151.  
  1152.      [.?!]
  1153.  
  1154.    However, we do not want `forward-sentence' merely to jump to a
  1155. period, a question mark, or an exclamation mark, because such a
  1156. character might be used in the middle of a sentence.  A period, for
  1157. example, is used after abbreviations.  So other information is needed.
  1158.  
  1159.    According to convention, you type two spaces after every sentence,
  1160. but only one space after a period, a question mark, or an exclamation
  1161. mark in the body of a sentence.  So a period, a question mark, or an
  1162. exclamation mark followed by two spaces is a good indicator of an end
  1163. of sentence.  However, in a file, the two spaces may instead be a tab
  1164. or the end of a line.  This means that the regular expression should
  1165. include these three items as alternatives.  This group of alternatives
  1166. will look like this:
  1167.  
  1168.      \\($\\| \\|  \\)
  1169.             ^   ^^
  1170.            TAB  SPC
  1171.  
  1172. Here, `$' indicates the end of the line, and I have pointed out where
  1173. the tab and two spaces are inserted in the expression.  Both are
  1174. inserted by putting the actual characters into the expression.
  1175.  
  1176.    Two backslashes, `\\', are required before the parentheses and
  1177. vertical bars: the first backslash to quote the following backslash in
  1178. Emacs; and the second to indicate that the following character, the
  1179. parenthesis or the vertical bar, is special.
  1180.  
  1181.    Also, a sentence may be followed by one or more carriage returns,
  1182. like this:
  1183.  
  1184.      [
  1185.      ]*
  1186.  
  1187. Like tabs and spaces, a carriage return is inserted into a regular
  1188. expression by inserting it literally.  The asterisk indicates that the
  1189. <RET> is repeated zero or more times.
  1190.  
  1191.    But a sentence end does not consist only of a period, a question
  1192. mark or an exclamation mark followed by appropriate space: a closing
  1193. quotation mark or a closing brace of some kind may precede the space.
  1194. Indeed more than one such mark or brace may precede the space.  These
  1195. require a expression that looks like this:
  1196.  
  1197.      []\"')}]*
  1198.  
  1199.    In this expression, the first `]' is the first character in the
  1200. expression; the second character is `"', which is preceded by a `\' to
  1201. tell Emacs the `"' is *not* special.  The last three characters are
  1202. `'', `)', and `}'.
  1203.  
  1204.    All this suggests what the regular expression pattern for matching
  1205. the end of a sentence should be; and, indeed, if we evaluate
  1206. `sentence-end' we find that it returns the following value:
  1207.  
  1208.      sentence-end
  1209.           => "[.?!][]\"')}]*\\($\\|     \\|  \\)[
  1210.      ]*"
  1211.  
  1212.